ViennaLS
Loading...
Searching...
No Matches
lsMakeGeometry.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cassert>
4
6
7#include <hrleTypes.hpp>
8
9#include <lsConvexHull.hpp>
10#include <lsDomain.hpp>
11#include <lsFromSurfaceMesh.hpp>
12#include <lsGeometries.hpp>
13#include <lsMesh.hpp>
14#include <lsTransformMesh.hpp>
15
16#include <vcVectorType.hpp>
17
18#ifndef NDEBUG
19#include <lsVTKWriter.hpp>
20#endif
21
22namespace viennals {
23
24using namespace viennacore;
25
27template <class T, int D> class MakeGeometry {
28 typedef typename Domain<T, D>::PointValueVectorType pointDataType;
29
32 enum struct GeometryEnum : unsigned {
33 SPHERE = 0,
34 PLANE = 1,
35 BOX = 2,
36 CUSTOM = 3,
37 CYLINDER = 4
38 };
39
40 SmartPointer<Domain<T, D>> levelSet;
41 GeometryEnum geometry = GeometryEnum::SPHERE;
42 SmartPointer<Sphere<T, D>> sphere;
43 SmartPointer<Plane<T, D>> plane;
44 SmartPointer<Box<T, D>> box;
45 SmartPointer<Cylinder<T, D>> cylinder;
46 SmartPointer<PointCloud<T, D>> pointCloud;
47 const double numericEps = 1e-9;
48 // bool ignoreBoundaryConditions = false;
49 std::array<bool, 3> ignoreBoundaryConditions{false, false, false};
50
51public:
52 MakeGeometry() = default;
53
54 MakeGeometry(SmartPointer<Domain<T, D>> passedLevelSet)
55 : levelSet(passedLevelSet) {}
56
57 MakeGeometry(SmartPointer<Domain<T, D>> passedLevelSet,
58 SmartPointer<Sphere<T, D>> passedSphere)
59 : levelSet(passedLevelSet), sphere(passedSphere) {
60 geometry = GeometryEnum::SPHERE;
61 }
62
63 MakeGeometry(SmartPointer<Domain<T, D>> passedLevelSet,
64 SmartPointer<Plane<T, D>> passedPlane)
65 : levelSet(passedLevelSet), plane(passedPlane) {
66 geometry = GeometryEnum::PLANE;
67 }
68
69 MakeGeometry(SmartPointer<Domain<T, D>> passedLevelSet,
70 SmartPointer<Box<T, D>> passedBox)
71 : levelSet(passedLevelSet), box(passedBox) {
72 geometry = GeometryEnum::BOX;
73 }
74
75 MakeGeometry(SmartPointer<Domain<T, D>> passedLevelSet,
76 SmartPointer<Cylinder<T, D>> passedCylinder)
77 : levelSet(passedLevelSet), cylinder(passedCylinder) {
78 geometry = GeometryEnum::CYLINDER;
79 }
80
81 MakeGeometry(SmartPointer<Domain<T, D>> passedLevelSet,
82 SmartPointer<PointCloud<T, D>> passedPointCloud)
83 : levelSet(passedLevelSet), pointCloud(passedPointCloud) {
84 geometry = GeometryEnum::CUSTOM;
85 }
86
87 void setLevelSet(SmartPointer<Domain<T, D>> passedlsDomain) {
88 levelSet = passedlsDomain;
89 }
90
92 void setGeometry(SmartPointer<Sphere<T, D>> passedSphere) {
93 sphere = passedSphere;
94 geometry = GeometryEnum::SPHERE;
95 }
96
98 void setGeometry(SmartPointer<Plane<T, D>> passedPlane) {
99 plane = passedPlane;
100 geometry = GeometryEnum::PLANE;
101 }
102
104 void setGeometry(SmartPointer<Box<T, D>> passedBox) {
105 box = passedBox;
106 geometry = GeometryEnum::BOX;
107 }
108
110 void setGeometry(SmartPointer<Cylinder<T, D>> passedCylinder) {
111 cylinder = passedCylinder;
112 geometry = GeometryEnum::CYLINDER;
113 }
114
117 void setGeometry(SmartPointer<PointCloud<T, D>> passedPointCloud) {
118 if (passedPointCloud && passedPointCloud->empty()) {
119 Logger::getInstance()
120 .addWarning("Passing an empty point cloud to MakeGeometry. ")
121 .print();
122 }
123 pointCloud = passedPointCloud;
124 geometry = GeometryEnum::CUSTOM;
125 }
126
129 void setIgnoreBoundaryConditions(bool passedIgnoreBoundaryConditions) {
130 for (unsigned i = 0; i < D; ++i) {
131 ignoreBoundaryConditions[i] = passedIgnoreBoundaryConditions;
132 }
133 }
134
138 template <std::size_t N>
140 std::array<bool, N> passedIgnoreBoundaryConditions) {
141 for (unsigned i = 0; i < D && i < N; ++i) {
142 ignoreBoundaryConditions[i] = passedIgnoreBoundaryConditions[i];
143 }
144 }
145
146 void apply() {
147 if (levelSet == nullptr) {
148 Logger::getInstance()
149 .addError("No level set was passed to MakeGeometry.")
150 .print();
151 return;
152 }
153
154 switch (geometry) {
155 case GeometryEnum::SPHERE:
156 makeSphere(sphere->origin, sphere->radius);
157 break;
158 case GeometryEnum::PLANE:
159 makePlane(plane->origin, plane->normal);
160 break;
161 case GeometryEnum::BOX:
162 makeBox(box->minCorner, box->maxCorner);
163 break;
164 case GeometryEnum::CYLINDER:
165 makeCylinder(cylinder);
166 break;
167 case GeometryEnum::CUSTOM:
168 makeCustom(pointCloud);
169 break;
170 default:
171 Logger::getInstance()
172 .addError("Invalid geometry type was specified for MakeGeometry. "
173 "Not creating geometry.")
174 .print();
175 }
176 }
177
178private:
179 void makeSphere(VectorType<T, D> origin, T radius) {
180 // TODO, this is a stupid algorithm and scales with volume, which is madness
181 auto &grid = levelSet->getGrid();
182 viennahrle::CoordType gridDelta = grid.getGridDelta();
183
184 // calculate indices from sphere size
185 viennahrle::Index<D> index;
186 viennahrle::Index<D> endIndex;
187
188 for (unsigned i = 0; i < D; ++i) {
189 index[i] = (origin[i] - radius) / gridDelta - 1;
190 endIndex[i] = (origin[i] + radius) / gridDelta + 1;
191 }
192
193 constexpr double initialWidth = 2.;
194 const T valueLimit = initialWidth * 0.5 * gridDelta + 1e-5;
195 const T radius2 = radius * radius;
196
197 pointDataType pointData;
198 const viennahrle::Index<D> minIndex = index;
199
200 while (index < endIndex) {
201 // take the shortest manhattan distance to gridline intersection
202 T distance = std::numeric_limits<T>::max();
203 for (unsigned i = 0; i < D; ++i) {
204 T y = (index[(i + 1) % D] * gridDelta) - origin[(i + 1) % D];
205 T z = 0;
206 if constexpr (D == 3)
207 z = (index[(i + 2) % D] * gridDelta) - origin[(i + 2) % D];
208 T x = radius2 - y * y - z * z;
209 if (x < 0.)
210 continue;
211 T dirRadius =
212 std::abs((index[i] * gridDelta) - origin[i]) - std::sqrt(x);
213 if (std::abs(dirRadius) < std::abs(distance))
214 distance = dirRadius;
215 }
216
217 if (std::abs(distance) <= valueLimit) {
218 pointData.push_back(std::make_pair(index, distance / gridDelta));
219 }
220 int dim = 0;
221 for (; dim < D - 1; ++dim) {
222 if (index[dim] < endIndex[dim])
223 break;
224 index[dim] = minIndex[dim];
225 }
226 ++index[dim];
227 }
228
229 // Mirror indices correctly into domain, unless boundary conditions
230 // are ignored
231 for (unsigned i = 0; i < pointData.size(); ++i) {
232 for (unsigned j = 0; j < D; ++j) {
233 if (!ignoreBoundaryConditions[j] && grid.isBoundaryPeriodic(j)) {
234 pointData[i].first[j] =
235 grid.globalIndex2LocalIndex(j, pointData[i].first[j]);
236 }
237 }
238 }
239
240 levelSet->insertPoints(pointData);
241 levelSet->getDomain().segment();
242 levelSet->finalize(initialWidth);
243 }
244
247 void makePlane(VectorType<T, D> origin,
248 VectorType<T, D> const &passedNormal) {
249 auto &grid = levelSet->getGrid();
250 viennahrle::CoordType gridDelta = grid.getGridDelta();
251
252 // normalise passedNormal
253 double modulus = 0.;
254 VectorType<T, D> normal = passedNormal;
255 for (unsigned i = 0; i < D; ++i) {
256 modulus += normal[i] * normal[i];
257 }
258 modulus = std::sqrt(modulus);
259 for (unsigned i = 0; i < D; ++i) {
260 normal[i] /= modulus;
261 }
262
263 // check that boundary conditions are correct
264 unsigned i = 0;
265 bool infDimSet = false;
266 for (unsigned n = 0; n < D; ++n) {
267 if (grid.getBoundaryConditions(n) ==
268 viennahrle::BoundaryType::INFINITE_BOUNDARY) {
269 if (!infDimSet) {
270 i = n;
271 infDimSet = true;
272 } else {
273 Logger::getInstance().addError(
274 "Planes can only be created with one Infinite Boundary "
275 "Condition. More than one found!");
276 }
277 }
278 }
279 if (!infDimSet) {
280 Logger::getInstance().addError("Planes require exactly one Infinite "
281 "Boundary Condition. None found!");
282 }
283
284 if (passedNormal[i] == 0.) {
285 Logger::getInstance().addError(
286 "MakeGeometry: Plane cannot be parallel to Infinite Boundary "
287 "direction!");
288 }
289
290 // find minimum and maximum points in infinite direction
291 // there are 2*(D-1) points in the corners of the simulation domain
292 std::vector<Vec3D<T>> cornerPoints;
293 cornerPoints.resize(2 * (D - 1));
294
295 // cyclic permutations
296 unsigned j = (i + 1) % D;
297 unsigned k = (i + 2) % D;
298
299 double minCoord[2];
300 double maxCoord[2];
301 // Find grid boundaries, there used to be a +-1 for the coords.
302 // If an error pops up here, probably has to do with that.
303 // But if +-1 is added here, the boundaries are exceeded and
304 // the correct boundary conditions will add stray points for
305 // tilted planes in lsFromSurfaceMesh later on.
306 for (unsigned n = 0; n < D - 1; ++n) {
307 minCoord[n] = gridDelta * (grid.getMinIndex((i + n + 1) % D) - 1);
308 maxCoord[n] = gridDelta * (grid.getMaxIndex((i + n + 1) % D) + 1);
309 }
310
311 // set corner points
312 cornerPoints[0][j] = minCoord[0];
313 cornerPoints[1][j] = maxCoord[0];
314
315 if constexpr (D == 3) {
316 cornerPoints[0][k] = minCoord[1];
317 cornerPoints[1][k] = maxCoord[1];
318
319 cornerPoints[2][j] = minCoord[0];
320 cornerPoints[2][k] = maxCoord[1];
321 cornerPoints[3][j] = maxCoord[0];
322 cornerPoints[3][k] = minCoord[1];
323 }
324
325 // now find i coordinate of points
326 auto mesh = SmartPointer<Mesh<T>>::New();
327
328 for (unsigned n = 0; n < cornerPoints.size(); ++n) {
329 double numerator = (cornerPoints[n][j] - origin[j]) * normal[j];
330 if constexpr (D == 3)
331 numerator += (cornerPoints[n][k] - origin[k]) * normal[k];
332 else
333 cornerPoints[n][2] = 0.;
334 cornerPoints[n][i] = origin[i] - numerator / normal[i];
335 mesh->insertNextNode(cornerPoints[n]);
336 }
337
338 if (D == 2) {
339 std::array<unsigned, 2> line = {0, 1};
340 if (normal[i] < 0.)
341 std::swap(line[0], line[1]);
342 mesh->insertNextLine(line);
343 } else {
344 std::array<unsigned, 3> triangle = {0, 1, 2};
345 if (normal[i] < 0.)
346 std::swap(triangle[0], triangle[1]);
347 mesh->insertNextTriangle(triangle);
348 triangle = {0, 3, 1};
349 if (normal[i] < 0.)
350 std::swap(triangle[0], triangle[1]);
351 mesh->insertNextTriangle(triangle);
352 }
353
354#ifndef NDEBUG
355 static unsigned planeCounter = 0;
356 VTKWriter<T>(mesh, "plane" + std::to_string(planeCounter++) + ".vtk")
357 .apply();
358#endif
359
360 // now convert mesh to levelset
361 FromSurfaceMesh<T, D>(levelSet, mesh).apply();
362 }
363
364 // This function creates a box starting in minCorner spanning to maxCorner
365 void makeBox(VectorType<T, D> minCorner, VectorType<T, D> maxCorner) {
366 // draw all triangles for the surface and then import from the mesh
367 std::vector<Vec3D<T>> corners;
368 corners.resize(std::pow(2, D), Vec3D<T>{0, 0, 0});
369
370 // first corner is the minCorner
371 for (unsigned i = 0; i < D; ++i)
372 corners[0][i] = minCorner[i];
373
374 // last corner is maxCorner
375 for (unsigned i = 0; i < D; ++i)
376 corners.back()[i] = maxCorner[i];
377
378 // calculate all missing corners
379 corners[1] = corners[0];
380 corners[1][0] = corners.back()[0];
381
382 corners[2] = corners[0];
383 corners[2][1] = corners.back()[1];
384
385 if constexpr (D == 3) {
386 corners[3] = corners.back();
387 corners[3][2] = corners[0][2];
388
389 corners[4] = corners[0];
390 corners[4][2] = corners.back()[2];
391
392 corners[5] = corners.back();
393 corners[5][1] = corners[0][1];
394
395 corners[6] = corners.back();
396 corners[6][0] = corners[0][0];
397 }
398
399 // now add all corners to mesh
400 auto mesh = Mesh<T>::New();
401 for (unsigned i = 0; i < corners.size(); ++i) {
402 mesh->insertNextNode(corners[i]);
403 }
404
405 if (D == 2) {
406 std::array<unsigned, 2> lines[4] = {{0, 2}, {2, 3}, {3, 1}, {1, 0}};
407 for (auto &line : lines)
408 mesh->insertNextLine(line);
409 } else {
410 std::array<unsigned, 3> triangles[12] = {
411 {0, 3, 1}, {0, 2, 3}, {0, 1, 5}, {0, 5, 4}, {0, 4, 2}, {4, 6, 2},
412 {7, 6, 4}, {7, 4, 5}, {7, 2, 6}, {7, 3, 2}, {1, 3, 5}, {3, 7, 5}};
413 for (auto &triangle : triangles)
414 mesh->insertNextTriangle(triangle);
415 }
416
417 // now convert mesh to levelset
418 FromSurfaceMesh<T, D> mesher(levelSet, mesh);
419 mesher.setRemoveBoundaryTriangles(ignoreBoundaryConditions);
420 mesher.apply();
421 }
422
423 void makeCylinder(SmartPointer<Cylinder<T, D>> cylinder) {
424 if (D != 3) {
425 Logger::getInstance()
426 .addError("MakeGeometry: Cylinder can only be created in 3D!")
427 .print();
428 return;
429 }
430 // generate the points on the edges of the cylinders and mesh
431 // them manually
432 // cylinder axis will be (0,0,1)
433 auto gridDelta = levelSet->getGrid().getGridDelta();
434
435 auto points = SmartPointer<PointCloud<T, D>>::New();
436 const unsigned numPoints =
437 std::ceil(2 * M_PI * cylinder->radius / gridDelta);
438 const double smallAngle = 2.0 * M_PI / static_cast<double>(numPoints);
439
440 auto mesh = SmartPointer<Mesh<T>>::New();
441 // insert midpoint at base
442 mesh->insertNextNode(Vec3D<T>{0.0, 0.0, 0.0});
443 {
444 constexpr double limit = 2 * M_PI - 1e-6;
445 std::vector<Vec3D<T>> points;
446 if (cylinder->topRadius)
447 std::vector<Vec3D<T>> pointsTop;
448
449 // create and insert points at base
450 for (double angle = 0.; angle < limit; angle += smallAngle) {
451 Vec3D<T> point;
452 point[0] = cylinder->radius * std::cos(angle);
453 point[1] = cylinder->radius * std::sin(angle);
454 point[2] = 0.0;
455 points.push_back(point);
456 mesh->insertNextNode(point);
457 }
458
459 // insert midpoint at top
460 mesh->insertNextNode(Vec3D<T>{0.0, 0.0, cylinder->height});
461
462 double angle = 0;
463 for (unsigned i = 0; i < numPoints; ++i) {
464 // create triangles at base
465 std::array<unsigned, 3> triangle{};
466 triangle[0] = (i + 1) % numPoints + 1;
467 triangle[1] = i + 1;
468 triangle[2] = 0;
469 mesh->insertNextTriangle(triangle);
470
471 // insert points at top
472 // If topRadius is specified, update the first two coordinates of the
473 // points
474 if (cylinder->topRadius) {
475 points[i][0] = cylinder->topRadius * std::cos(angle);
476 points[i][1] = cylinder->topRadius * std::sin(angle);
477 angle += smallAngle;
478 }
479 points[i][2] = cylinder->height;
480 mesh->insertNextNode(points[i]);
481
482 // insert triangles at top
483 triangle[0] = numPoints + 1;
484 triangle[1] = numPoints + i + 2;
485 triangle[2] = (i + 1) % numPoints + 2 + numPoints;
486 mesh->insertNextTriangle(triangle);
487 }
488
489 // insert sidewall triangles
490 for (unsigned i = 0; i < numPoints; ++i) {
491 std::array<unsigned, 3> triangle{};
492 triangle[0] = i + 1;
493 triangle[1] = (i + 1) % numPoints + 1;
494 triangle[2] = i + numPoints + 2;
495 mesh->insertNextTriangle(triangle);
496
497 triangle[0] = (i + 1) % numPoints + 1;
498 triangle[1] = (i + 1) % numPoints + 2 + numPoints;
499 triangle[2] = i + numPoints + 2;
500 mesh->insertNextTriangle(triangle);
501 }
502 }
503
504 // rotate mesh
505 // normalise axis vector
506 T unit =
507 std::sqrt(DotProduct(cylinder->axisDirection, cylinder->axisDirection));
508 Vec3D<T> cylinderAxis;
509 for (int i = 0; i < 3; ++i) {
510 cylinderAxis[i] = cylinder->axisDirection[i] / unit;
511 }
512 // get rotation axis via cross product of (0,0,1) and axis of cylinder
513 Vec3D<T> rotAxis = {-cylinderAxis[1], cylinderAxis[0], 0.0};
514 // angle is acos of dot product
515 T rotationAngle = std::acos(cylinderAxis[2]);
516
517 // rotate mesh
518 TransformMesh<T>(mesh, TransformEnum::ROTATION, rotAxis, rotationAngle)
519 .apply();
520
521 // translate mesh
522 Vec3D<T> translationVector;
523 for (int i = 0; i < 3; ++i) {
524 translationVector[i] = cylinder->origin[i];
525 }
526 TransformMesh<T>(mesh, TransformEnum::TRANSLATION, translationVector)
527 .apply();
528
529 // read mesh from surface
530 FromSurfaceMesh<T, D> mesher(levelSet, mesh);
531 mesher.setRemoveBoundaryTriangles(ignoreBoundaryConditions);
532 mesher.apply();
533 }
534
535 void makeCustom(SmartPointer<PointCloud<T, D>> pointCloud) {
536 // create mesh from point cloud
537 auto mesh = SmartPointer<Mesh<T>>::New();
538 ConvexHull<T, D>(mesh, pointCloud).apply();
539
540 // read mesh from surface
541 FromSurfaceMesh<T, D> mesher(levelSet, mesh);
542 mesher.setRemoveBoundaryTriangles(ignoreBoundaryConditions);
543 mesher.apply();
544 }
545};
546
547// add all template specialisations for this class
549
550} // namespace viennals
constexpr int D
Definition Epitaxy.cpp:9
double T
Definition Epitaxy.cpp:10
Class describing a square box from one coordinate to another.
Definition lsGeometries.hpp:71
Class describing a square box from one coordinate to another.
Definition lsGeometries.hpp:100
Class containing all information about the level set, including the dimensions of the domain,...
Definition lsDomain.hpp:27
std::vector< std::pair< viennahrle::Index< D >, T > > PointValueVectorType
Definition lsDomain.hpp:34
Create level sets describing basic geometric forms.
Definition lsMakeGeometry.hpp:27
MakeGeometry(SmartPointer< Domain< T, D > > passedLevelSet, SmartPointer< Cylinder< T, D > > passedCylinder)
Definition lsMakeGeometry.hpp:75
void setIgnoreBoundaryConditions(bool passedIgnoreBoundaryConditions)
Ignore boundary conditions, meaning the parts of the generated geometry which are outside of the doma...
Definition lsMakeGeometry.hpp:129
void apply()
Definition lsMakeGeometry.hpp:146
void setLevelSet(SmartPointer< Domain< T, D > > passedlsDomain)
Definition lsMakeGeometry.hpp:87
void setGeometry(SmartPointer< Cylinder< T, D > > passedCylinder)
Set a cylinder to be created in the level set.
Definition lsMakeGeometry.hpp:110
void setGeometry(SmartPointer< PointCloud< T, D > > passedPointCloud)
Set a point cloud, which is used to create a geometry from its convex hull.
Definition lsMakeGeometry.hpp:117
void setIgnoreBoundaryConditions(std::array< bool, N > passedIgnoreBoundaryConditions)
Ignore boundary conditions, meaning the parts of the generated geometry which are outside of the doma...
Definition lsMakeGeometry.hpp:139
MakeGeometry(SmartPointer< Domain< T, D > > passedLevelSet, SmartPointer< Sphere< T, D > > passedSphere)
Definition lsMakeGeometry.hpp:57
MakeGeometry(SmartPointer< Domain< T, D > > passedLevelSet)
Definition lsMakeGeometry.hpp:54
MakeGeometry(SmartPointer< Domain< T, D > > passedLevelSet, SmartPointer< Plane< T, D > > passedPlane)
Definition lsMakeGeometry.hpp:63
MakeGeometry(SmartPointer< Domain< T, D > > passedLevelSet, SmartPointer< Box< T, D > > passedBox)
Definition lsMakeGeometry.hpp:69
void setGeometry(SmartPointer< Box< T, D > > passedBox)
Set a box to be created in the level set.
Definition lsMakeGeometry.hpp:104
void setGeometry(SmartPointer< Plane< T, D > > passedPlane)
Set a plane to be created in the level set.
Definition lsMakeGeometry.hpp:98
void setGeometry(SmartPointer< Sphere< T, D > > passedSphere)
Set sphere as geometry to be created in the level set.
Definition lsMakeGeometry.hpp:92
MakeGeometry(SmartPointer< Domain< T, D > > passedLevelSet, SmartPointer< PointCloud< T, D > > passedPointCloud)
Definition lsMakeGeometry.hpp:81
static auto New()
Definition lsMesh.hpp:63
Class describing a plane via a point in it and the plane normal.
Definition lsGeometries.hpp:42
Class describing a point cloud, which can be used to create geometries from its convex hull mesh.
Definition lsGeometries.hpp:145
Class describing a sphere via origin and radius.
Definition lsGeometries.hpp:15
#define PRECOMPILE_PRECISION_DIMENSION(className)
Definition lsPreCompileMacros.hpp:24
float gridDelta
Definition AirGapDeposition.py:21
tuple maxCorner
Definition AirGapDeposition.py:44
mesh
Definition AirGapDeposition.py:36
tuple origin
Definition AirGapDeposition.py:30
Definition lsAdvect.hpp:36
@ TRANSLATION
Definition lsTransformMesh.hpp:16
@ ROTATION
Definition lsTransformMesh.hpp:17