31 enum struct GeometryEnum :
unsigned {
39 SmartPointer<Domain<T, D>> levelSet;
40 GeometryEnum geometry = GeometryEnum::SPHERE;
41 SmartPointer<Sphere<T, D>> sphere;
42 SmartPointer<Plane<T, D>> plane;
43 SmartPointer<Box<T, D>> box;
44 SmartPointer<Cylinder<T, D>> cylinder;
45 SmartPointer<PointCloud<T, D>> pointCloud;
46 const double numericEps = 1e-9;
48 std::array<bool, 3> ignoreBoundaryConditions{
false,
false,
false};
54 : levelSet(passedLevelSet) {}
58 : levelSet(passedLevelSet), sphere(passedSphere) {
59 geometry = GeometryEnum::SPHERE;
64 : levelSet(passedLevelSet), plane(passedPlane) {
65 geometry = GeometryEnum::PLANE;
70 : levelSet(passedLevelSet), box(passedBox) {
71 geometry = GeometryEnum::BOX;
76 : levelSet(passedLevelSet), cylinder(passedCylinder) {
77 geometry = GeometryEnum::CYLINDER;
82 : levelSet(passedLevelSet), pointCloud(passedPointCloud) {
83 geometry = GeometryEnum::CUSTOM;
87 levelSet = passedlsDomain;
92 sphere = passedSphere;
93 geometry = GeometryEnum::SPHERE;
99 geometry = GeometryEnum::PLANE;
105 geometry = GeometryEnum::BOX;
110 cylinder = passedCylinder;
111 geometry = GeometryEnum::CYLINDER;
117 pointCloud = passedPointCloud;
118 geometry = GeometryEnum::CUSTOM;
124 for (
unsigned i = 0; i <
D; ++i) {
125 ignoreBoundaryConditions[i] = passedIgnoreBoundaryConditions;
132 template <std::
size_t N>
134 std::array<bool, N> passedIgnoreBoundaryConditions) {
135 for (
unsigned i = 0; i <
D && i < N; ++i) {
136 ignoreBoundaryConditions[i] = passedIgnoreBoundaryConditions[i];
141 if (levelSet ==
nullptr) {
142 Logger::getInstance()
143 .addWarning(
"No level set was passed to MakeGeometry.")
149 case GeometryEnum::SPHERE:
150 makeSphere(sphere->origin, sphere->radius);
152 case GeometryEnum::PLANE:
153 makePlane(plane->origin, plane->normal);
155 case GeometryEnum::BOX:
156 makeBox(box->minCorner, box->maxCorner);
158 case GeometryEnum::CYLINDER:
159 makeCylinder(cylinder);
161 case GeometryEnum::CUSTOM:
162 makeCustom(pointCloud);
165 Logger::getInstance()
166 .addWarning(
"Invalid geometry type was specified for MakeGeometry. "
167 "Not creating geometry.")
173 void makeSphere(hrleVectorType<T, D> origin,
T radius) {
174 if (levelSet ==
nullptr) {
175 Logger::getInstance()
176 .addWarning(
"No level set was passed to MakeGeometry.")
182 auto &grid = levelSet->getGrid();
183 hrleCoordType gridDelta = grid.getGridDelta();
186 hrleVectorType<hrleIndexType, D> index;
187 hrleVectorType<hrleIndexType, D> endIndex;
189 for (
unsigned i = 0; i <
D; ++i) {
190 index[i] = (origin[i] - radius) / gridDelta - 1;
191 endIndex[i] = (origin[i] + radius) / gridDelta + 1;
194 const double initialWidth = 2.;
195 const T valueLimit = initialWidth * 0.5 *
gridDelta + 1e-5;
196 const T radius2 = radius * radius;
198 pointDataType pointData;
199 const hrleVectorType<hrleIndexType, D> minIndex = index;
201 while (index < endIndex) {
203 T distance = std::numeric_limits<T>::max();
204 for (
unsigned i = 0; i <
D; ++i) {
205 T y = (index[(i + 1) %
D] *
gridDelta) - origin[(i + 1) %
D];
207 if constexpr (
D == 3)
208 z = (index[(i + 2) %
D] * gridDelta) -
origin[(i + 2) %
D];
209 T x = radius2 - y * y - z * z;
213 std::abs((index[i] * gridDelta) - origin[i]) - std::sqrt(x);
214 if (std::abs(dirRadius) < std::abs(distance))
215 distance = dirRadius;
218 if (std::abs(distance) <= valueLimit) {
219 pointData.push_back(std::make_pair(index, distance / gridDelta));
222 for (; dim <
D - 1; ++dim) {
223 if (index[dim] < endIndex[dim])
225 index[dim] = minIndex[dim];
232 for (
unsigned i = 0; i < pointData.size(); ++i) {
233 for (
unsigned j = 0; j <
D; ++j) {
234 if (!ignoreBoundaryConditions[j] && grid.isBoundaryPeriodic(j)) {
235 pointData[i].first[j] =
236 grid.globalIndex2LocalIndex(j, pointData[i].first[j]);
241 levelSet->insertPoints(pointData);
242 levelSet->getDomain().segment();
243 levelSet->finalize(initialWidth);
248 void makePlane(hrleVectorType<T, D> origin,
249 hrleVectorType<T, D> passedNormal) {
250 if (levelSet ==
nullptr) {
251 Logger::getInstance()
252 .addWarning(
"No level set was passed to MakeGeometry.")
257 auto &grid = levelSet->getGrid();
258 hrleCoordType
gridDelta = grid.getGridDelta();
262 hrleVectorType<double, D> normal(passedNormal);
263 for (
unsigned i = 0; i <
D; ++i) {
264 modulus += normal[i] * normal[i];
266 modulus = std::sqrt(modulus);
267 for (
unsigned i = 0; i <
D; ++i) {
268 normal[i] /= modulus;
273 bool infDimSet =
false;
274 for (
unsigned n = 0; n <
D; ++n) {
275 if (grid.getBoundaryConditions(n) ==
276 hrleGrid<D>::boundaryType::INFINITE_BOUNDARY) {
281 Logger::getInstance().addError(
282 "Planes can only be created with one Infinite Boundary "
283 "Condition. More than one found!");
288 Logger::getInstance().addError(
"Planes require exactly one Infinite "
289 "Boundary Condition. None found!");
292 if (passedNormal[i] == 0.) {
293 Logger::getInstance().addError(
294 "MakeGeometry: Plane cannot be parallel to Infinite Boundary "
300 std::vector<std::array<T, 3>> cornerPoints;
301 cornerPoints.resize(2 * (
D - 1));
304 unsigned j = (i + 1) %
D;
305 unsigned k = (i + 2) %
D;
314 for (
unsigned n = 0; n <
D - 1; ++n) {
315 minCoord[n] =
gridDelta * (grid.getMinIndex((i + n + 1) %
D) - 1);
316 maxCoord[n] =
gridDelta * (grid.getMaxIndex((i + n + 1) %
D) + 1);
320 cornerPoints[0][j] = minCoord[0];
321 cornerPoints[1][j] = maxCoord[0];
323 if constexpr (
D == 3) {
324 cornerPoints[0][k] = minCoord[1];
325 cornerPoints[1][k] = maxCoord[1];
327 cornerPoints[2][j] = minCoord[0];
328 cornerPoints[2][k] = maxCoord[1];
329 cornerPoints[3][j] = maxCoord[0];
330 cornerPoints[3][k] = minCoord[1];
334 auto mesh = SmartPointer<Mesh<T>>::New();
336 for (
unsigned n = 0; n < cornerPoints.size(); ++n) {
337 double numerator = (cornerPoints[n][j] -
origin[j]) * normal[j];
338 if constexpr (
D == 3)
339 numerator += (cornerPoints[n][k] - origin[k]) * normal[k];
341 cornerPoints[n][2] = 0.;
342 cornerPoints[n][i] =
origin[i] - numerator / normal[i];
343 mesh->insertNextNode(cornerPoints[n]);
347 std::array<unsigned, 2> line = {0, 1};
349 std::swap(line[0], line[1]);
350 mesh->insertNextLine(line);
352 std::array<unsigned, 3> triangle = {0, 1, 2};
354 std::swap(triangle[0], triangle[1]);
355 mesh->insertNextTriangle(triangle);
356 triangle = {0, 3, 1};
358 std::swap(triangle[0], triangle[1]);
359 mesh->insertNextTriangle(triangle);
363 static unsigned planeCounter = 0;
364 VTKWriter<T>(mesh,
"plane" + std::to_string(planeCounter++) +
".vtk")
369 FromSurfaceMesh<T, D>(levelSet, mesh).apply();
373 void makeBox(hrleVectorType<T, D> minCorner, hrleVectorType<T, D> maxCorner) {
374 if (levelSet ==
nullptr) {
375 Logger::getInstance()
376 .addWarning(
"No level set was passed to MakeGeometry.")
382 std::vector<std::array<T, 3>> corners;
383 corners.resize(std::pow(2,
D), {0, 0, 0});
386 for (
unsigned i = 0; i <
D; ++i)
387 corners[0][i] = minCorner[i];
390 for (
unsigned i = 0; i <
D; ++i)
394 corners[1] = corners[0];
395 corners[1][0] = corners.back()[0];
397 corners[2] = corners[0];
398 corners[2][1] = corners.back()[1];
400 if constexpr (
D == 3) {
401 corners[3] = corners.back();
402 corners[3][2] = corners[0][2];
404 corners[4] = corners[0];
405 corners[4][2] = corners.back()[2];
407 corners[5] = corners.back();
408 corners[5][1] = corners[0][1];
410 corners[6] = corners.back();
411 corners[6][0] = corners[0][0];
415 auto mesh = SmartPointer<Mesh<T>>::New();
416 for (
unsigned i = 0; i < corners.size(); ++i) {
417 mesh->insertNextNode(corners[i]);
421 std::array<unsigned, 2> lines[4] = {{0, 2}, {2, 3}, {3, 1}, {1, 0}};
422 for (
unsigned i = 0; i < 4; ++i)
423 mesh->insertNextLine(lines[i]);
425 std::array<unsigned, 3> triangles[12] = {
426 {0, 3, 1}, {0, 2, 3}, {0, 1, 5}, {0, 5, 4}, {0, 4, 2}, {4, 6, 2},
427 {7, 6, 4}, {7, 4, 5}, {7, 2, 6}, {7, 3, 2}, {1, 3, 5}, {3, 7, 5}};
428 for (
unsigned i = 0; i < 12; ++i)
429 mesh->insertNextTriangle(triangles[i]);
433 FromSurfaceMesh<T, D> mesher(levelSet, mesh);
434 mesher.setRemoveBoundaryTriangles(ignoreBoundaryConditions);
438 void makeCylinder(SmartPointer<Cylinder<T, D>> cylinder) {
440 Logger::getInstance()
441 .addWarning(
"MakeGeometry: Cylinder can only be created in 3D!")
448 auto gridDelta = levelSet->getGrid().getGridDelta();
450 auto points = SmartPointer<PointCloud<T, D>>::New();
451 unsigned numPoints = std::ceil(2 * M_PI * cylinder->radius / gridDelta);
452 double smallAngle = 2.0 * M_PI / double(numPoints);
454 auto mesh = SmartPointer<Mesh<T>>::New();
456 mesh->insertNextNode(std::array<T, 3>{0.0, 0.0, 0.0});
458 constexpr double limit = 2 * M_PI - 1e-6;
459 std::vector<std::array<T, 3>> points;
460 if (cylinder->topRadius)
461 std::vector<std::array<T, 3>> pointsTop;
464 for (
double angle = 0.; angle < limit; angle += smallAngle) {
465 std::array<T, 3> point;
466 point[0] = cylinder->radius * std::cos(angle);
467 point[1] = cylinder->radius * std::sin(angle);
469 points.push_back(point);
470 mesh->insertNextNode(point);
474 mesh->insertNextNode(std::array<T, 3>{0.0, 0.0, cylinder->height});
477 for (
unsigned i = 0; i < numPoints; ++i) {
479 std::array<unsigned, 3> triangle;
480 triangle[0] = (i + 1) % numPoints + 1;
483 mesh->insertNextTriangle(triangle);
488 if (cylinder->topRadius) {
489 points[i][0] = cylinder->topRadius * std::cos(angle);
490 points[i][1] = cylinder->topRadius * std::sin(angle);
493 points[i][2] = cylinder->height;
494 mesh->insertNextNode(points[i]);
497 triangle[0] = numPoints + 1;
498 triangle[1] = numPoints + i + 2;
499 triangle[2] = (i + 1) % numPoints + 2 + numPoints;
500 mesh->insertNextTriangle(triangle);
504 for (
unsigned i = 0; i < numPoints; ++i) {
505 std::array<unsigned, 3> triangle;
507 triangle[1] = (i + 1) % numPoints + 1;
508 triangle[2] = i + numPoints + 2;
509 mesh->insertNextTriangle(triangle);
511 triangle[0] = (i + 1) % numPoints + 1;
512 triangle[1] = (i + 1) % numPoints + 2 + numPoints;
513 triangle[2] = i + numPoints + 2;
514 mesh->insertNextTriangle(triangle);
521 std::sqrt(DotProduct(cylinder->axisDirection, cylinder->axisDirection));
522 hrleVectorType<double, 3> cylinderAxis;
523 for (
unsigned i = 0; i < 3; ++i) {
524 cylinderAxis[i] = cylinder->axisDirection[i] / unit;
527 hrleVectorType<double, 3> rotAxis(-cylinderAxis[1], cylinderAxis[0], 0.0);
529 T rotationAngle = std::acos(cylinderAxis[2]);
536 hrleVectorType<double, 3> translationVector;
537 for (
unsigned i = 0; i < 3; ++i) {
538 translationVector[i] = cylinder->origin[i];
544 FromSurfaceMesh<T, D> mesher(levelSet, mesh);
545 mesher.setRemoveBoundaryTriangles(ignoreBoundaryConditions);
549 void makeCustom(SmartPointer<PointCloud<T, D>> pointCloud) {
551 auto mesh = SmartPointer<Mesh<T>>::New();
552 ConvexHull<T, D>(mesh, pointCloud).apply();
555 FromSurfaceMesh<T, D> mesher(levelSet, mesh);
556 mesher.setRemoveBoundaryTriangles(ignoreBoundaryConditions);