void InitializeData::initializeArrayWithReals(IDataArray::Pointer p, int64_t dims[3]) { T rangeMin; T rangeMax; if (m_InitType == RandomWithRange) { rangeMin = static_cast<T>(m_InitRange.first); rangeMax = static_cast<T>(m_InitRange.second); } else { rangeMin = std::numeric_limits<T>().min(); rangeMax = std::numeric_limits<T>().max(); } typedef boost::mt19937 RandomNumberGenerator; typedef boost::uniform_real<T> RealDistribution; typedef boost::variate_generator<RandomNumberGenerator&, RealDistribution> RealGenerator; std::shared_ptr<RealDistribution> distribution = std::shared_ptr<RealDistribution>(new RealDistribution(rangeMin, rangeMax)); std::shared_ptr<RandomNumberGenerator> randomNumberGenerator = std::shared_ptr<RandomNumberGenerator>(new RandomNumberGenerator); randomNumberGenerator->seed(static_cast<size_t>(QDateTime::currentMSecsSinceEpoch())); // seed with the current time std::shared_ptr<RealGenerator> realGeneratorPtr = std::shared_ptr<RealGenerator>(new RealGenerator(*randomNumberGenerator, *distribution)); RealGenerator& realGenerator = *realGeneratorPtr; for (int32_t k = m_ZMin; k < m_ZMax + 1; k++) { for (int32_t j = m_YMin; j < m_YMax + 1; j++) { for (int32_t i = m_XMin; i < m_XMax + 1; i++) { size_t index = (k * dims[0] * dims[1]) + (j * dims[0]) + i; if (m_InitType == Manual) { T num = static_cast<T>(m_InitValue); p->initializeTuple(index, &num); } else { T temp = realGenerator(); p->initializeTuple(index, &temp); } } } } }
// ----------------------------------------------------------------------------- // // ----------------------------------------------------------------------------- void AddBadData::add_noise() { notifyStatusMessage(getHumanLabel(), "Adding Noise"); DREAM3D_RANDOMNG_NEW() DataContainer::Pointer m = getDataContainerArray()->getDataContainer(getGBEuclideanDistancesArrayPath().getDataContainerName()); QString attMatName = getGBEuclideanDistancesArrayPath().getAttributeMatrixName(); QList<QString> voxelArrayNames = m->getAttributeMatrix(attMatName)->getAttributeArrayNames(); float random = 0.0f; size_t totalPoints = m->getGeometryAs<ImageGeom>()->getNumberOfElements(); for (size_t i = 0; i < totalPoints; ++i) { if (m_BoundaryNoise == true && m_GBEuclideanDistances[i] < 1) { random = static_cast<float>( rg.genrand_res53() ); if (random < m_BoundaryVolFraction) { for (QList<QString>::iterator iter = voxelArrayNames.begin(); iter != voxelArrayNames.end(); ++iter) { IDataArray::Pointer p = m->getAttributeMatrix(attMatName)->getAttributeArray(*iter); p->initializeTuple(i, 0); } } } if (m_PoissonNoise == true) { random = static_cast<float>( rg.genrand_res53() ); if (random < m_PoissonVolFraction) { for (QList<QString>::iterator iter = voxelArrayNames.begin(); iter != voxelArrayNames.end(); ++iter) { IDataArray::Pointer p = m->getAttributeMatrix(attMatName)->getAttributeArray(*iter); p->initializeTuple(i, 0); } } } } }
// ----------------------------------------------------------------------------- // // ----------------------------------------------------------------------------- void InitializeData::execute() { setErrorCondition(0); dataCheck(); if(getErrorCondition() < 0) { return; } DataContainer::Pointer m = getDataContainerArray()->getDataContainer(m_CellAttributeMatrixPath.getDataContainerName()); size_t udims[3] = { 0, 0, 0 }; m->getGeometryAs<ImageGeom>()->getDimensions(udims); #if (CMP_SIZEOF_SIZE_T == 4) typedef int32_t DimType; #else typedef int64_t DimType; #endif DimType dims[3] = { static_cast<DimType>(udims[0]), static_cast<DimType>(udims[1]), static_cast<DimType>(udims[2]), }; int index; QString attrMatName = m_CellAttributeMatrixPath.getAttributeMatrixName(); QList<QString> voxelArrayNames = m->getAttributeMatrix(attrMatName)->getAttributeArrayNames(); for (int32_t k = m_ZMin; k < m_ZMax + 1; k++) { for (int32_t j = m_YMin; j < m_YMax + 1; j++) { for (int32_t i = m_XMin; i < m_XMax + 1; i++) { index = (k * dims[0] * dims[1]) + (j * dims[0]) + i; for (QList<QString>::iterator iter = voxelArrayNames.begin(); iter != voxelArrayNames.end(); ++iter) { IDataArray::Pointer p = m->getAttributeMatrix(attrMatName)->getAttributeArray(*iter); p->initializeTuple(index, 0); } } } } notifyStatusMessage(getHumanLabel(), "Complete"); }
// ----------------------------------------------------------------------------- // // ----------------------------------------------------------------------------- void WarpRegularGrid::execute() { setErrorCondition(0); dataCheck(); if(getErrorCondition() < 0) { return; } DataContainer::Pointer m; if (m_SaveAsNewDataContainer == false) { m = getDataContainerArray()->getDataContainer(getCellAttributeMatrixPath().getDataContainerName()); } else { m = getDataContainerArray()->getDataContainer(getNewDataContainerName()); } AttributeMatrix::Pointer cellAttrMat = m->getAttributeMatrix(getCellAttributeMatrixPath().getAttributeMatrixName()); AttributeMatrix::Pointer newCellAttrMat = cellAttrMat->deepCopy(); size_t dims[3] = { 0, 0, 0 }; m->getGeometryAs<ImageGeom>()->getDimensions(dims); float res[3] = { 0.0f, 0.0f, 0.0f }; m->getGeometryAs<ImageGeom>()->getResolution(res); size_t totalPoints = m->getGeometryAs<ImageGeom>()->getNumberOfElements(); float x = 0.0f, y = 0.0f, z = 0.0f; float newX = 0.0f, newY = 0.0f; int col = 0.0f, row = 0.0f, plane = 0.0f; size_t index; size_t index_old; std::vector<size_t> newindicies(totalPoints); std::vector<bool> goodPoint(totalPoints, true); for (size_t i = 0; i < dims[2]; i++) { QString ss = QObject::tr("Warping Data - %1 Percent Complete").arg(((float)i / dims[2]) * 100); notifyStatusMessage(getMessagePrefix(), getHumanLabel(), ss); for (size_t j = 0; j < dims[1]; j++) { for (size_t k = 0; k < dims[0]; k++) { x = static_cast<float>((k * res[0])); y = static_cast<float>((j * res[1])); z = static_cast<float>((i * res[2])); index = (i * dims[0] * dims[1]) + (j * dims[0]) + k; determine_warped_coordinates(x, y, newX, newY); col = newX / res[0]; row = newY / res[1]; plane = i; index_old = (plane * dims[0] * dims[1]) + (row * dims[0]) + col; newindicies[index] = index_old; if (col > 0 && col < dims[0] && row > 0 && row < dims[1]) { goodPoint[index] = true; } else { goodPoint[index] = false; } } } } QList<QString> voxelArrayNames = cellAttrMat->getAttributeArrayNames(); for (QList<QString>::iterator iter = voxelArrayNames.begin(); iter != voxelArrayNames.end(); ++iter) { IDataArray::Pointer p = cellAttrMat->getAttributeArray(*iter); // Make a copy of the 'p' array that has the same name. When placed into // the data container this will over write the current array with // the same name. At least in theory IDataArray::Pointer data = p->createNewArray(p->getNumberOfTuples(), p->getComponentDimensions(), p->getName()); data->resize(totalPoints); void* source = NULL; void* destination = NULL; size_t newIndicies_I = 0; int nComp = data->getNumberOfComponents(); for (size_t i = 0; i < static_cast<size_t>(totalPoints); i++) { newIndicies_I = newindicies[i]; if(goodPoint[i] == true) { source = p->getVoidPointer((nComp * newIndicies_I)); destination = data->getVoidPointer((data->getNumberOfComponents() * i)); ::memcpy(destination, source, p->getTypeSize() * data->getNumberOfComponents()); } else { int var = 0; data->initializeTuple(i, &var); } } cellAttrMat->removeAttributeArray(*iter); newCellAttrMat->addAttributeArray(*iter, data); } m->removeAttributeMatrix(getCellAttributeMatrixPath().getAttributeMatrixName()); m->addAttributeMatrix(getCellAttributeMatrixPath().getAttributeMatrixName(), newCellAttrMat); notifyStatusMessage(getHumanLabel(), "Complete"); }
// ----------------------------------------------------------------------------- // // ----------------------------------------------------------------------------- void RotateSampleRefFrame::execute() { setErrorCondition(0); dataCheck(); if(getErrorCondition() < 0) { return; } DataContainer::Pointer m = getDataContainerArray()->getDataContainer(getCellAttributeMatrixPath().getDataContainerName()); float rotAngle = m_RotationAngle * SIMPLib::Constants::k_Pi / 180.0; int64_t xp = 0, yp = 0, zp = 0; float xRes = 0.0f, yRes = 0.0f, zRes = 0.0f; int64_t xpNew = 0, ypNew = 0, zpNew = 0; float xResNew = 0.0f, yResNew = 0.0f, zResNew = 0.0f; RotateSampleRefFrameImplArg_t params; xp = static_cast<int64_t>(m->getGeometryAs<ImageGeom>()->getXPoints()); xRes = m->getGeometryAs<ImageGeom>()->getXRes(); yp = static_cast<int64_t>(m->getGeometryAs<ImageGeom>()->getYPoints()); yRes = m->getGeometryAs<ImageGeom>()->getYRes(); zp = static_cast<int64_t>(m->getGeometryAs<ImageGeom>()->getZPoints()); zRes = m->getGeometryAs<ImageGeom>()->getZRes(); params.xp = xp; params.xRes = xRes; params.yp = yp; params.yRes = yRes; params.zp = zp; params.zRes = zRes; size_t col = 0, row = 0, plane = 0; float rotMat[3][3] = { { 0.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 0.0f } }; float coords[3] = { 0.0f, 0.0f, 0.0f }; float newcoords[3] = { 0.0f, 0.0f, 0.0f }; float xMin = std::numeric_limits<float>::max(); float xMax = std::numeric_limits<float>::min(); float yMin = std::numeric_limits<float>::max(); float yMax = std::numeric_limits<float>::min(); float zMin = std::numeric_limits<float>::max(); float zMax = std::numeric_limits<float>::min(); FOrientArrayType om(9); FOrientTransformsType::ax2om(FOrientArrayType(m_RotationAxis.x, m_RotationAxis.y, m_RotationAxis.z, rotAngle), om); om.toGMatrix(rotMat); for (int32_t i = 0; i < 8; i++) { if (i == 0) { col = 0, row = 0, plane = 0; } if (i == 1) { col = xp - 1, row = 0, plane = 0; } if (i == 2) { col = 0, row = yp - 1, plane = 0; } if (i == 3) { col = xp - 1, row = yp - 1, plane = 0; } if (i == 4) { col = 0, row = 0, plane = zp - 1; } if (i == 5) { col = xp - 1, row = 0, plane = zp - 1; } if (i == 6) { col = 0, row = yp - 1, plane = zp - 1; } if (i == 7) { col = xp - 1, row = yp - 1, plane = zp - 1; } coords[0] = static_cast<float>(col * xRes); coords[1] = static_cast<float>(row * yRes); coords[2] = static_cast<float>(plane * zRes); MatrixMath::Multiply3x3with3x1(rotMat, coords, newcoords); if (newcoords[0] < xMin) { xMin = newcoords[0]; } if (newcoords[0] > xMax) { xMax = newcoords[0]; } if (newcoords[1] < yMin) { yMin = newcoords[1]; } if (newcoords[1] > yMax) { yMax = newcoords[1]; } if (newcoords[2] < zMin) { zMin = newcoords[2]; } if (newcoords[2] > zMax) { zMax = newcoords[2]; } } float xAxis[3] = {1, 0, 0}; float yAxis[3] = {0, 1, 0}; float zAxis[3] = {0, 0, 1}; float xAxisNew[3] = { 0.0f, 0.0f, 0.0f }; float yAxisNew[3] = { 0.0f, 0.0f, 0.0f }; float zAxisNew[3] = { 0.0f, 0.0f, 0.0f }; MatrixMath::Multiply3x3with3x1(rotMat, xAxis, xAxisNew); MatrixMath::Multiply3x3with3x1(rotMat, yAxis, yAxisNew); MatrixMath::Multiply3x3with3x1(rotMat, zAxis, zAxisNew); float closestAxis = 0.0f; xResNew = xRes; closestAxis = fabs(GeometryMath::CosThetaBetweenVectors(xAxis, xAxisNew)); if (fabs(GeometryMath::CosThetaBetweenVectors(yAxis, xAxisNew)) > closestAxis) { xResNew = yRes, closestAxis = fabs(GeometryMath::CosThetaBetweenVectors(yAxis, xAxisNew)); } if (fabs(GeometryMath::CosThetaBetweenVectors(zAxis, xAxisNew)) > closestAxis) { xResNew = zRes, closestAxis = fabs(GeometryMath::CosThetaBetweenVectors(zAxis, xAxisNew)); } yResNew = yRes; closestAxis = fabs(GeometryMath::CosThetaBetweenVectors(yAxis, yAxisNew)); if (fabs(GeometryMath::CosThetaBetweenVectors(xAxis, yAxisNew)) > closestAxis) { yResNew = xRes, closestAxis = fabs(GeometryMath::CosThetaBetweenVectors(xAxis, yAxisNew)); } if (fabs(GeometryMath::CosThetaBetweenVectors(zAxis, yAxisNew)) > closestAxis) { yResNew = zRes, closestAxis = fabs(GeometryMath::CosThetaBetweenVectors(zAxis, yAxisNew)); } zResNew = zRes; closestAxis = fabs(GeometryMath::CosThetaBetweenVectors(zAxis, zAxisNew)); if (fabs(GeometryMath::CosThetaBetweenVectors(xAxis, zAxisNew)) > closestAxis) { zResNew = xRes, closestAxis = fabs(GeometryMath::CosThetaBetweenVectors(xAxis, zAxisNew)); } if (fabs(GeometryMath::CosThetaBetweenVectors(yAxis, zAxisNew)) > closestAxis) { zResNew = yRes, closestAxis = fabs(GeometryMath::CosThetaBetweenVectors(yAxis, zAxisNew)); } xpNew = static_cast<int64_t>(nearbyint((xMax - xMin) / xResNew) + 1); ypNew = static_cast<int64_t>(nearbyint((yMax - yMin) / yResNew) + 1); zpNew = static_cast<int64_t>(nearbyint((zMax - zMin) / zResNew) + 1); params.xpNew = xpNew; params.xResNew = xResNew; params.xMinNew = xMin; params.ypNew = ypNew; params.yResNew = yResNew; params.yMinNew = yMin; params.zpNew = zpNew; params.zResNew = zResNew; params.zMinNew = zMin; int64_t newNumCellTuples = params.xpNew * params.ypNew * params.zpNew; DataArray<int64_t>::Pointer newIndiciesPtr = DataArray<int64_t>::CreateArray(newNumCellTuples, "_INTERNAL_USE_ONLY_RotateSampleRef_NewIndicies"); newIndiciesPtr->initializeWithValue(-1); int64_t* newindicies = newIndiciesPtr->getPointer(0); #ifdef SIMPLib_USE_PARALLEL_ALGORITHMS tbb::task_scheduler_init init; bool doParallel = true; #endif #ifdef SIMPLib_USE_PARALLEL_ALGORITHMS if (doParallel == true) { tbb::parallel_for(tbb::blocked_range3d<int64_t, int64_t, int64_t>(0, params.zpNew, 0, params.ypNew, 0, params.xpNew), RotateSampleRefFrameImpl(newIndiciesPtr, ¶ms, rotMat, m_SliceBySlice), tbb::auto_partitioner()); } else #endif { RotateSampleRefFrameImpl serial(newIndiciesPtr, ¶ms, rotMat, m_SliceBySlice); serial.convert(0, params.zpNew, 0, params.ypNew, 0, params.xpNew); } // This could technically be parallelized also where each thread takes an array to adjust. Except // that the DataContainer is NOT thread safe or re-entrant so that would actually be a BAD idea. QString attrMatName = getCellAttributeMatrixPath().getAttributeMatrixName(); QList<QString> voxelArrayNames = m->getAttributeMatrix(attrMatName)->getAttributeArrayNames(); // resize attribute matrix QVector<size_t> tDims(3); tDims[0] = params.xpNew; tDims[1] = params.ypNew; tDims[2] = params.zpNew; m->getAttributeMatrix(attrMatName)->resizeAttributeArrays(tDims); for (QList<QString>::iterator iter = voxelArrayNames.begin(); iter != voxelArrayNames.end(); ++iter) { IDataArray::Pointer p = m->getAttributeMatrix(attrMatName)->getAttributeArray(*iter); // Make a copy of the 'p' array that has the same name. When placed into // the data container this will over write the current array with // the same name. IDataArray::Pointer data = p->createNewArray(newNumCellTuples, p->getComponentDimensions(), p->getName()); void* source = NULL; void* destination = NULL; int64_t newIndicies_I = 0; int32_t nComp = data->getNumberOfComponents(); for (size_t i = 0; i < static_cast<size_t>(newNumCellTuples); i++) { newIndicies_I = newindicies[i]; if(newIndicies_I >= 0) { source = p->getVoidPointer((nComp * newIndicies_I)); if (NULL == source) { QString ss = QObject::tr("The index is outside the bounds of the source array"); setErrorCondition(-11004); notifyErrorMessage(getHumanLabel(), ss, getErrorCondition()); return; } destination = data->getVoidPointer((data->getNumberOfComponents() * i)); ::memcpy(destination, source, p->getTypeSize() * data->getNumberOfComponents()); } else { data->initializeTuple(i, 0); } } m->getAttributeMatrix(attrMatName)->addAttributeArray(*iter, data); } m->getGeometryAs<ImageGeom>()->setResolution(params.xResNew, params.yResNew, params.zResNew); m->getGeometryAs<ImageGeom>()->setDimensions(params.xpNew, params.ypNew, params.zpNew); m->getGeometryAs<ImageGeom>()->setOrigin(xMin, yMin, zMin); notifyStatusMessage(getHumanLabel(), "Complete"); }
// ----------------------------------------------------------------------------- // // ----------------------------------------------------------------------------- void AlignSections::execute() { setErrorCondition(0); dataCheck(); if(getErrorCondition() < 0) { return; } DataContainer::Pointer m = getDataContainerArray()->getDataContainer(getDataContainerName()); size_t udims[3] = { 0, 0, 0 }; m->getGeometryAs<ImageGeom>()->getDimensions(udims); #if (CMP_SIZEOF_SIZE_T == 4) typedef int32_t DimType; #else typedef int64_t DimType; #endif DimType dims[3] = { static_cast<DimType>(udims[0]), static_cast<DimType>(udims[1]), static_cast<DimType>(udims[2]), }; DimType slice = 0; DimType xspot = 0, yspot = 0; DimType newPosition = 0; DimType currentPosition = 0; std::vector<int64_t> xshifts(dims[2], 0); std::vector<int64_t> yshifts(dims[2], 0); find_shifts(xshifts, yshifts); if (getSubtractBackground()) { /**fit x and y shifts to lines * * y = mx + b * * m = (n*sum(x_i * y_i) - sum(x_i) * sum(y_i)) / (n*sum(x_i^2)-sum(x_i)^2 * * b = (sum(y_i)-m*sum(x_i))/n * */ // same for both double sumX = 0.0; // sum(x_i) double sumX_2 = 0.0; // sum(x_i^2) // x shift line double x_sumY = 0.0; // sum(y_i) double x_sumXY = 0.0; // sum(x_i * y_i) // y shift line double y_sumY = 0.0; // sum(y_i) double y_sumXY = 0.0; // sum(x_i * y_i) for (DimType iter = 0; iter < dims[2]; iter++) { slice = static_cast<DimType>( (dims[2] - 1) - iter ); sumX = static_cast<double>(sumX + iter); sumX_2 = static_cast<double>(sumX_2 + iter * iter); x_sumY = static_cast<double>(x_sumY + xshifts[iter]); x_sumXY = static_cast<double>(x_sumXY + iter * xshifts[iter]); y_sumY = static_cast<double>(y_sumY + yshifts[iter]); y_sumXY = static_cast<double>(y_sumXY + iter * yshifts[iter]); } double mx = static_cast<double>((dims[2] * x_sumXY - x_sumXY) / (dims[2] * sumX_2 - sumX)); double my = static_cast<double>((dims[2] * y_sumXY - y_sumXY) / (dims[2] * sumX_2 - sumX)); // adjust shifts so that fit line has 0 slope (~ends of the sample are fixed) for (DimType iter = 1; iter < dims[2]; iter++) { slice = (dims[2] - 1) - iter; xshifts[iter] = static_cast<int64_t>(xshifts[iter] - iter * mx); yshifts[iter] = static_cast<int64_t>(yshifts[iter] - iter * my); } } QList<QString> voxelArrayNames = m->getAttributeMatrix(getCellAttributeMatrixName())->getAttributeArrayNames(); DimType progIncrement = dims[2] / 100; DimType prog = 1; DimType progressInt = 0; for (DimType i = 1; i < dims[2]; i++) { if (i > prog) { progressInt = ((float)i / dims[2]) * 100.0f; QString ss = QObject::tr("Transferring Cell Data || %1% Complete").arg(progressInt); notifyStatusMessage(getMessagePrefix(), getHumanLabel(), ss); prog = prog + progIncrement; } if (getCancel() == true) { return; } slice = (dims[2] - 1) - i; for (DimType l = 0; l < dims[1]; l++) { for (DimType n = 0; n < dims[0]; n++) { if (yshifts[i] >= 0) { yspot = l; } else if (yshifts[i] < 0) { yspot = dims[1] - 1 - l; } if (xshifts[i] >= 0) { xspot = n; } else if (xshifts[i] < 0) { xspot = dims[0] - 1 - n; } newPosition = (slice * dims[0] * dims[1]) + (yspot * dims[0]) + xspot; currentPosition = (slice * dims[0] * dims[1]) + ((yspot + yshifts[i]) * dims[0]) + (xspot + xshifts[i]); if ((yspot + yshifts[i]) >= 0 && (yspot + yshifts[i]) <= dims[1] - 1 && (xspot + xshifts[i]) >= 0 && (xspot + xshifts[i]) <= dims[0] - 1) { for (QList<QString>::iterator iter = voxelArrayNames.begin(); iter != voxelArrayNames.end(); ++iter) { IDataArray::Pointer p = m->getAttributeMatrix(getCellAttributeMatrixName())->getAttributeArray(*iter); p->copyTuple(currentPosition, newPosition); } } if ((yspot + yshifts[i]) < 0 || (yspot + yshifts[i]) > dims[1] - 1 || (xspot + xshifts[i]) < 0 || (xspot + xshifts[i]) > dims[0] - 1) { for (QList<QString>::iterator iter = voxelArrayNames.begin(); iter != voxelArrayNames.end(); ++iter) { IDataArray::Pointer p = m->getAttributeMatrix(getCellAttributeMatrixName())->getAttributeArray(*iter); p->initializeTuple(newPosition, 0); } } } } } // If there is an error set this to something negative and also set a message notifyStatusMessage(getHumanLabel(), "Complete"); }