// Computes sum of the values of a unitary blob on grid points. The blob is // supposed to be at the origin of the absolute coordinate system double sum_blob_SimpleGrid(const struct blobtype &blob, const SimpleGrid &grid, const Matrix2D<double> *D) { SPEED_UP_temps012; Matrix1D<double> gr(3), ur(3), corner1(3), corner2(3); double actual_radius; int i, j, k; double sum = 0.0; // Compute the limits of the blob in the grid coordinate system grid.universe2grid(vectorR3(-blob.radius, -blob.radius, -blob.radius), corner1); grid.universe2grid(vectorR3(blob.radius, blob.radius, blob.radius), corner2); if (D != NULL) box_enclosing(corner1, corner2, *D, corner1, corner2); // Compute the sum in the points inside the grid // The integer part of the vectors is taken for not picking points // just in the border of the blob, which we know they are 0. for (i = (int)XX(corner1); i <= (int)XX(corner2); i++) for (j = (int)YY(corner1); j <= (int)YY(corner2); j++) for (k = (int)ZZ(corner1); k <= (int)ZZ(corner2); k++) { VECTOR_R3(gr, i, j, k); grid.grid2universe(gr, ur); if (D != NULL) M3x3_BY_V3x1(ur, *D, ur); actual_radius = ur.module(); if (actual_radius < blob.radius) sum += kaiser_value(actual_radius, blob.radius, blob.alpha, blob.order); } return sum; }
//Pre-calculate table values void TabBlob::fillTable(const int _nr_elem) { tabulatedValues.resize(_nr_elem); for (int i = 0; i < _nr_elem; i++) { double xx = (double) i * sampling; tabulatedValues(i) = kaiser_value(xx, radius, alpha, order); } }
void * blobs2voxels_SimpleGrid( void * data ) { ThreadBlobsToVoxels * thread_data = (ThreadBlobsToVoxels *) data; const MultidimArray<double> *vol_blobs = thread_data->vol_blobs; const SimpleGrid *grid = thread_data->grid; const struct blobtype *blob = thread_data->blob; MultidimArray<double> *vol_voxels = thread_data->vol_voxels; const Matrix2D<double> *D = thread_data->D; int istep = thread_data->istep; MultidimArray<double> *vol_corr = thread_data->vol_corr; const MultidimArray<double> *vol_mask = thread_data->vol_mask; ; bool FORW = thread_data->FORW; int eq_mode = thread_data->eq_mode; int min_separation = thread_data->min_separation; int z_planes = (int)(ZZ(grid->highest) - ZZ(grid->lowest) + 1); Matrix2D<double> Dinv; // Inverse of D Matrix1D<double> act_coord(3); // Coord: Actual position inside // the voxel volume without deforming Matrix1D<double> real_position(3); // Coord: actual position after // applying the V transformation Matrix1D<double> beginZ(3); // Coord: Voxel coordinates of the // blob at the 3D point // (z0,YY(lowest),XX(lowest)) Matrix1D<double> beginY(3); // Coord: Voxel coordinates of the // blob at the 3D point // (z0,y0,XX(lowest)) Matrix1D<double> corner2(3), corner1(3); // Coord: Corners of the // blob in the voxel volume Matrix1D<double> gcurrent(3); // Position in g of current point MultidimArray<double> blob_table; // Something like a blobprint // but with the values of the // blob in space double d; // Distance between the center // of the blob and a voxel position int id; // index inside the blob value // table for tha blob value at // a distance d double intx, inty, intz; // Nearest integer voxel int i, j, k; // Index within the blob volume int process; // True if this blob has to be // processed double vol_correction=0; // Correction to apply to the // volume when "projecting" back SPEED_UP_temps012; // Some aliases #define x0 STARTINGX(*vol_voxels) #define xF FINISHINGX(*vol_voxels) #define y0 STARTINGY(*vol_voxels) #define yF FINISHINGY(*vol_voxels) #define z0 STARTINGZ(*vol_voxels) #define zF FINISHINGZ(*vol_voxels) #ifdef DEBUG bool condition = !FORW; if (condition) { (*vol_voxels)().printShape(); std::cout << std::endl; std::cout << "x0= " << x0 << " xF= " << xF << std::endl; std::cout << "y0= " << y0 << " yF= " << yF << std::endl; std::cout << "z0= " << z0 << " zF= " << zF << std::endl; std::cout << grid; } #endif // Invert deformation matrix ............................................ if (D != NULL) Dinv = D->inv(); // Compute a blob value table ........................................... blob_table.resize((int)(blob->radius*istep + 1)); for (size_t i = 0; i < blob_table.xdim; i++) { A1D_ELEM(blob_table, i) = kaiser_value((double)i/istep, blob->radius, blob->alpha, blob->order); #ifdef DEBUG_MORE if (condition) std::cout << "Blob (" << i << ") r=" << (double)i / istep << " val= " << A1D_ELEM(blob_table, i) << std::endl; #endif } int assigned_slice; do { assigned_slice = -1; do { pthread_mutex_lock(&blobs_conv_mutex); if( slices_processed == z_planes ) { pthread_mutex_unlock(&blobs_conv_mutex); return (void*)NULL; } for(int w = 0 ; w < z_planes ; w++ ) { if( slices_status[w]==0 ) { slices_status[w] = -1; assigned_slice = w; slices_processed++; for( int in = (w-min_separation+1) ; in <= (w+min_separation-1 ) ; in ++ ) { if( in != w ) { if( ( in >= 0 ) && ( in < z_planes )) { if( slices_status[in] != -1 ) slices_status[in]++; } } } break; } } pthread_mutex_unlock(&blobs_conv_mutex); } while( assigned_slice == -1); // Convert the whole grid ............................................... // Corner of the plane defined by Z. These coordinates are in the // universal coord. system Matrix1D<double> aux( grid->lowest ); k = (int)(assigned_slice + ZZ( grid->lowest )); ZZ(aux) = k; grid->grid2universe(aux, beginZ); Matrix1D<double> grid_index(3); // Corner of the row defined by Y beginY = beginZ; for (i = (int) YY(grid->lowest); i <= (int) YY(grid->highest); i++) { // First point in the row act_coord = beginY; for (j = (int) XX(grid->lowest); j <= (int) XX(grid->highest); j++) { VECTOR_R3(grid_index, j, i, k); #ifdef DEBUG if (condition) { printf("Dealing blob at (%d,%d,%d) = %f\n", j, i, k, A3D_ELEM(*vol_blobs, k, i, j)); std::cout << "Center of the blob " << act_coord.transpose() << std::endl; } #endif // Place act_coord in its right place if (D != NULL) { M3x3_BY_V3x1(real_position, *D, act_coord); #ifdef DEBUG if (condition) std::cout << "Center of the blob moved to " //ROB, the "moved" coordinates are in // real_position not in act_coord << act_coord.transpose() << std::endl; << real_position.transpose() << std::endl; #endif // ROB This is OK if blob.radius is in Cartesian space as I // think is the case } else real_position = act_coord; // These two corners are also real valued process = true; //ROB //This is OK if blob.radius is in Cartesian space as I think is the case V3_PLUS_CT(corner1, real_position, -blob->radius); V3_PLUS_CT(corner2, real_position, blob->radius); #ifdef DEFORM_BLOB_WHEN_IN_CRYSTAL //ROB //we do not need this, it is already in Cartesian space //if (D!=NULL) // box_enclosing(corner1,corner2, *D, corner1, corner2); #endif if (XX(corner1) >= xF) process = false; if (YY(corner1) >= yF) process = false; if (ZZ(corner1) >= zF) process = false; if (XX(corner2) <= x0) process = false; if (YY(corner2) <= y0) process = false; if (ZZ(corner2) <= z0) process = false; #ifdef DEBUG if (!process && condition) std::cout << " It is outside output volume\n"; #endif if (!grid->is_interesting(real_position)) { #ifdef DEBUG if (process && condition) std::cout << " It is not interesting\n"; #endif process = false; } #ifdef DEBUG if (condition) { std::cout << "Corner 1 for this point " << corner1.transpose() << std::endl; std::cout << "Corner 2 for this point " << corner2.transpose() << std::endl; } #endif if (process) { // Clip the corners to the volume borders XX(corner1) = ROUND(CLIP(XX(corner1), x0, xF)); YY(corner1) = ROUND(CLIP(YY(corner1), y0, yF)); ZZ(corner1) = ROUND(CLIP(ZZ(corner1), z0, zF)); XX(corner2) = ROUND(CLIP(XX(corner2), x0, xF)); YY(corner2) = ROUND(CLIP(YY(corner2), y0, yF)); ZZ(corner2) = ROUND(CLIP(ZZ(corner2), z0, zF)); #ifdef DEBUG if (condition) { std::cout << "Clipped and rounded Corner 1 " << corner1.transpose() << std::endl; std::cout << "Clipped and rounded Corner 2 " << corner2.transpose() << std::endl; } #endif if (!FORW) switch (eq_mode) { case VARTK: vol_correction = 0; break; case VMAXARTK: vol_correction = -1e38; break; } // Effectively convert long N_eq; N_eq = 0; for (intz = ZZ(corner1); intz <= ZZ(corner2); intz++) for (inty = YY(corner1); inty <= YY(corner2); inty++) for (intx = XX(corner1); intx <= XX(corner2); intx++) { int iz = (int)intz, iy = (int)inty, ix = (int)intx; if (vol_mask != NULL) if (!A3D_ELEM(*vol_mask, iz, iy, ix)) continue; // Compute distance to the center of the blob VECTOR_R3(gcurrent, intx, inty, intz); #ifdef DEFORM_BLOB_WHEN_IN_CRYSTAL // ROB //if (D!=NULL) // M3x3_BY_V3x1(gcurrent,Dinv,gcurrent); #endif V3_MINUS_V3(gcurrent, real_position, gcurrent); d = sqrt(XX(gcurrent) * XX(gcurrent) + YY(gcurrent) * YY(gcurrent) + ZZ(gcurrent) * ZZ(gcurrent)); if (d > blob->radius) continue; id = (int)(d * istep); #ifdef DEBUG_MORE if (condition) { std::cout << "At (" << intx << "," << inty << "," << intz << ") distance=" << d; std::cout.flush(); } #endif // Add at that position the corresponding blob value if (FORW) { A3D_ELEM(*vol_voxels, iz, iy, ix) += A3D_ELEM(*vol_blobs, k, i, j) * A1D_ELEM(blob_table, id); #ifdef DEBUG_MORE if (condition) { std::cout << " adding " << A3D_ELEM(*vol_blobs, k, i, j) << " * " << A1D_ELEM(blob_table, id) << " = " << A3D_ELEM(*vol_blobs, k, i, j)* A1D_ELEM(blob_table, id) << std::endl; std::cout.flush(); } #endif if (vol_corr != NULL) A3D_ELEM(*vol_corr, iz, iy, ix) += A1D_ELEM(blob_table, id) * A1D_ELEM(blob_table, id); } else { double contrib = A3D_ELEM(*vol_corr, iz, iy, ix) * A1D_ELEM(blob_table, id); switch (eq_mode) { case VARTK: vol_correction += contrib; N_eq++; break; case VMAXARTK: if (contrib > vol_correction) vol_correction = contrib; break; } #ifdef DEBUG_MORE if (condition) { std::cout << " adding " << A3D_ELEM(*vol_corr, iz, iy, ix) << " * " << A1D_ELEM(blob_table, id) << " = " << contrib << std::endl; std::cout.flush(); } #endif } } if (N_eq == 0) N_eq = 1; if (!FORW) { A3D_ELEM(*vol_blobs, k, i, j) += vol_correction / N_eq; #ifdef DEBUG_MORE std::cout << " correction= " << vol_correction << std::endl << " Number of eqs= " << N_eq << std::endl << " Blob after correction= " << A3D_ELEM(*vol_blobs, k, i, j) << std::endl; #endif } } // Prepare for next iteration XX(act_coord) = XX(act_coord) + grid->relative_size * (grid->basis)( 0, 0); YY(act_coord) = YY(act_coord) + grid->relative_size * (grid->basis)( 1, 0); ZZ(act_coord) = ZZ(act_coord) + grid->relative_size * (grid->basis)( 2, 0); }