96 if (levelSet ==
nullptr) {
98 .addError(
"No level set passed to GeometricAdvect. Not Advecting.")
102 if (dist ==
nullptr) {
103 Logger::getInstance()
104 .addError(
"No GeometricAdvectDistribution passed to "
105 "GeometricAdvect. Not "
114 if (maskLevelSet !=
nullptr) {
118 dist->prepare(levelSet);
124 auto &grid = levelSet->getGrid();
125 const auto gridDelta = grid.getGridDelta();
126 const bool useSurfacePointId = dist->useSurfacePointId();
130 auto surfaceMesh = SmartPointer<Mesh<hrleCoordType>>::New();
131 auto pointIdTranslator =
132 SmartPointer<typename ToDiskMesh<T, D>::TranslatorType>::New();
135 if (!useSurfacePointId)
136 *pointIdTranslator = inverseTranslator(*pointIdTranslator);
139 auto distBounds = dist->getBounds();
142 viennahrle::Index<D> distMin, distMax;
144 bool minPointNegative = domain.getDomainSegment(0).definedValues[0] < 0.;
145 bool maxPointNegative =
146 domain.getDomainSegment(domain.getNumberOfSegments() - 1)
147 .definedValues.back() < 0.;
148 bool distIsPositive =
true;
151 hrleIndexType bounds[6];
152 domain.getDomainBounds(bounds);
153 viennahrle::Index<D> min, max;
154 for (
unsigned i = 0; i <
D; ++i) {
157 distBounds[2 * i] / gridDelta + ((distBounds[2 * i] < 0) ? -2 : 2);
158 distMax[i] = distBounds[2 * i + 1] / gridDelta +
159 ((distBounds[2 * i + 1] < 0) ? -2 : 2);
160 if (distBounds[2 * i] >= 0) {
161 distIsPositive =
false;
167 min[i] = surfaceMesh->minimumExtent[i] / gridDelta;
169 if (grid.isNegBoundaryInfinite(i) && minPointNegative && distMin[i] < 0) {
172 if (distIsPositive) {
173 min[i] += distMin[i];
175 min[i] -= distMin[i];
180 if (min[i] < grid.getMinGridPoint(i)) {
181 min[i] = grid.getMinGridPoint(i);
184 max[i] = surfaceMesh->maximumExtent[i] / gridDelta;
185 if (grid.isPosBoundaryInfinite(i) && maxPointNegative && distMax[i] > 0) {
188 if (distIsPositive) {
189 max[i] += distMax[i];
191 max[i] -= distMax[i];
194 if (max[i] > grid.getMaxGridPoint(i)) {
195 max[i] = grid.getMaxGridPoint(i);
202 if (maskLevelSet !=
nullptr) {
204 auto &maskDomain = maskLevelSet->getDomain();
205 auto values = surfaceMesh->cellData.getScalarData(
"LSValues");
206 auto valueIt = values->begin();
208 auto newSurfaceMesh = SmartPointer<Mesh<hrleCoordType>>::New();
210 viennahrle::ConstSparseIterator<DomainType> maskIt(maskDomain);
211 for (
auto &node : surfaceMesh->getNodes()) {
212 viennahrle::Index<D> index;
213 for (
unsigned i = 0; i <
D; ++i) {
214 index[i] = std::round(node[i] / gridDelta);
218 maskIt.goToIndicesSequential(index);
220 if (!maskIt.isDefined() || !(maskIt.getValue() < *valueIt + 1e-5)) {
221 newSurfaceMesh->insertNextNode(node);
222 newValues.push_back(*valueIt);
224 std::array<unsigned, 1> vertex{};
225 vertex[0] = newSurfaceMesh->nodes.size();
226 newSurfaceMesh->insertNextVertex(vertex);
230 newSurfaceMesh->cellData.insertNextScalarData(newValues,
"LSValues");
232 newSurfaceMesh->minimumExtent = surfaceMesh->minimumExtent;
233 newSurfaceMesh->maximumExtent = surfaceMesh->maximumExtent;
234 surfaceMesh = newSurfaceMesh;
239 VIENNACORE_LOG_DEBUG(
"GeomAdvect: Writing debug meshes");
241 "DEBUG_lsGeomAdvectMesh_contributewoMask.vtp")
243 auto mesh = SmartPointer<Mesh<T>>::New();
244 if (maskLevelSet !=
nullptr) {
247 "DEBUG_lsGeomAdvectMesh_mask.vtp")
252 "DEBUG_lsGeomAdvectMesh_initial.vtp")
258 const auto &surfaceNodes = surfaceMesh->getNodes();
261 typename viennahrle::Domain<T, D>::IndexPoints segmentation;
264 unsigned long long numPoints = 1;
265 unsigned long long pointsPerDimension[
D];
266 for (
unsigned i = 0; i <
D; ++i) {
267 pointsPerDimension[i] = numPoints;
268 numPoints *= max[i] - min[i];
270 unsigned long numberOfSegments = domain.getNumberOfSegments();
271 unsigned long long pointsPerSegment = numPoints / numberOfSegments;
272 unsigned long long pointId = 0;
273 for (
unsigned i = 0; i < numberOfSegments - 1; ++i) {
274 pointId = pointsPerSegment * (i + 1);
275 viennahrle::Index<D> segmentPoint;
276 for (
int j =
D - 1; j >= 0; --j) {
277 segmentPoint[j] = pointId / (pointsPerDimension[j]) + min[j];
278 pointId %= pointsPerDimension[j];
280 segmentation.push_back(segmentPoint);
284 typedef std::vector<std::pair<viennahrle::Index<D>,
T>> PointValueVector;
285 std::vector<PointValueVector> newPoints;
286 newPoints.resize(domain.getNumberOfSegments());
288 const T initialDistance = (distIsPositive)
289 ? std::numeric_limits<double>::max()
290 : std::numeric_limits<double>::lowest();
294 std::ostringstream oss;
295 oss <<
"GeomAdvect: Min: " << min <<
", Max: " << max << std::endl;
296 VIENNACORE_LOG_DEBUG(oss.str());
300#pragma omp parallel num_threads(domain.getNumberOfSegments())
304 p = omp_get_thread_num();
307 viennahrle::Index<D> startVector;
311 startVector = segmentation[p - 1];
312 incrementIndices(startVector, min, max);
315 viennahrle::Index<D> endVector =
316 (p !=
static_cast<int>(domain.getNumberOfSegments() - 1))
318 : grid.incrementIndices(max);
320 viennahrle::ConstSparseIterator<DomainType> checkIt(levelSet->getDomain(),
324 SmartPointer<viennahrle::ConstSparseIterator<DomainType>> maskIt =
326 if (maskLevelSet !=
nullptr) {
327 maskIt = SmartPointer<viennahrle::ConstSparseIterator<DomainType>>::New(
328 maskLevelSet->getDomain(), startVector);
332 for (viennahrle::Index<D> currentIndex = startVector;
333 currentIndex <= endVector;
334 incrementIndices(currentIndex, min, max)) {
336 checkIt.goToIndicesSequential(currentIndex);
337 T oldValue = checkIt.getValue();
339 if (distIsPositive) {
340 if (oldValue < -cutoffValue) {
343 }
else if (oldValue > cutoffValue) {
347 VectorType<hrleCoordType, 3> currentCoords{};
348 VectorType<hrleCoordType, 3> currentDistMin{};
349 VectorType<hrleCoordType, 3> currentDistMax{};
351 for (
unsigned i = 0; i <
D; ++i) {
352 currentCoords[i] = currentIndex[i] * gridDelta;
354 currentDistMin[i] = currentIndex[i] - std::abs(distMin[i]);
355 if (currentDistMin[i] < grid.getMinGridPoint(i)) {
356 currentDistMin[i] = grid.getMinGridPoint(i);
358 currentDistMin[i] *= gridDelta;
360 currentDistMax[i] = currentIndex[i] + std::abs(distMax[i]);
361 if (currentDistMin[i] > grid.getMaxGridPoint(i)) {
362 currentDistMin[i] = grid.getMaxGridPoint(i);
364 currentDistMax[i] *= gridDelta;
367 T distance = initialDistance;
369 unsigned long currentPointId = 0;
371 for (
auto surfIt = surfaceNodes.begin(); surfIt != surfaceNodes.end();
372 ++surfIt, ++currentPointId) {
374 auto ¤tNode = *surfIt;
378 bool outside =
false;
379 for (
unsigned i = 0; i <
D; ++i) {
380 if ((currentNode[i] < currentDistMin[i]) ||
381 (currentNode[i] > currentDistMax[i])) {
392 if (!dist->isInside(currentNode, currentCoords, 2 * gridDelta)) {
397 auto pointId = currentPointId;
398 if (!useSurfacePointId) {
399 pointId = pointIdTranslator->find(currentPointId)->second;
402 dist->getSignedDistance(currentNode, currentCoords, pointId) /
406 if (distIsPositive) {
407 if (tmpDistance <= -cutoffValue) {
408 distance = std::numeric_limits<T>::lowest();
412 if (tmpDistance < distance) {
413 distance = tmpDistance;
416 if (tmpDistance >= cutoffValue) {
417 distance = std::numeric_limits<T>::max();
421 if (tmpDistance > distance) {
422 distance = tmpDistance;
430 if (maskLevelSet !=
nullptr) {
431 maskIt->goToIndicesSequential(currentIndex);
435 (std::abs(oldValue - maskIt->getValue()) < 1e-6)) {
436 if (!distIsPositive && std::abs(oldValue) <= cutoffValue) {
437 newPoints[p].push_back(std::make_pair(currentIndex, oldValue));
441 if (distance != initialDistance) {
442 distance = std::min(maskIt->getValue(), distance);
443 }
else if (distIsPositive || oldValue >= 0.) {
444 newPoints[p].push_back(std::make_pair(currentIndex, oldValue));
450 if (std::abs(distance) <= cutoffValue) {
452 if (distIsPositive && oldValue >= 0.) {
453 newPoints[p].push_back(std::make_pair(currentIndex, distance));
454 }
else if (!distIsPositive && oldValue <= 0.) {
456 if (maskIt ==
nullptr || maskIt->getValue() > -cutoffValue) {
457 newPoints[p].push_back(std::make_pair(currentIndex, distance));
461 newPoints[p].push_back(std::make_pair(currentIndex, oldValue));
469 unsigned long long numberOfPoints = newPoints[0].size();
470 for (
unsigned i = 1; i < domain.getNumberOfSegments(); ++i) {
471 numberOfPoints += newPoints[i].size();
473 newPoints[0].reserve(numberOfPoints);
474 for (
unsigned i = 1; i < domain.getNumberOfSegments(); ++i) {
475 std::move(std::begin(newPoints[i]), std::end(newPoints[i]),
476 std::back_inserter(newPoints[0]));
480 auto mesh = SmartPointer<Mesh<T>>::New();
483 std::vector<T> scalarData;
484 for (
auto it = newPoints[0].begin(); it != newPoints[0].end(); ++it) {
485 Vec3D<T> node{0., 0., 0.};
486 for (
unsigned i = 0; i <
D; ++i) {
487 node[i] =
T((it->first)[i]) * gridDelta;
490 mesh->insertNextNode(node);
491 std::array<unsigned, 1> vertex{};
492 vertex[0] = mesh->vertices.size();
493 mesh->insertNextVertex(vertex);
494 scalarData.push_back(it->second);
496 mesh->cellData.insertNextScalarData(scalarData,
"LSValues");
500 VIENNACORE_LOG_DEBUG(
"GeomAdvect: Writing final mesh...");
508 VIENNACORE_LOG_DEBUG(
"GeomAdvect: Writing final LS...");
516 levelSet->getDomain().segment();
517 levelSet->finalize(1);