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 VIENNACORE_LOG_WARNING("Passing an empty point cloud to MakeGeometry. ");
120 }
121 pointCloud = passedPointCloud;
122 geometry = GeometryEnum::CUSTOM;
123 }
124
127 void setIgnoreBoundaryConditions(bool passedIgnoreBoundaryConditions) {
128 for (unsigned i = 0; i < D; ++i) {
129 ignoreBoundaryConditions[i] = passedIgnoreBoundaryConditions;
130 }
131 }
132
136 template <std::size_t N>
138 std::array<bool, N> passedIgnoreBoundaryConditions) {
139 for (unsigned i = 0; i < D && i < N; ++i) {
140 ignoreBoundaryConditions[i] = passedIgnoreBoundaryConditions[i];
141 }
142 }
143
144 void apply() {
145 if (levelSet == nullptr) {
146 Logger::getInstance()
147 .addError("No level set was passed to MakeGeometry.")
148 .print();
149 return;
150 }
151
152 switch (geometry) {
153 case GeometryEnum::SPHERE:
154 makeSphere(sphere->origin, sphere->radius);
155 break;
156 case GeometryEnum::PLANE:
157 makePlane(plane->origin, plane->normal);
158 break;
159 case GeometryEnum::BOX:
160 makeBox(box->minCorner, box->maxCorner);
161 break;
162 case GeometryEnum::CYLINDER:
163 makeCylinder(cylinder);
164 break;
165 case GeometryEnum::CUSTOM:
166 makeCustom(pointCloud);
167 break;
168 default:
169 Logger::getInstance()
170 .addError("Invalid geometry type was specified for MakeGeometry. "
171 "Not creating geometry.")
172 .print();
173 }
174 }
175
176private:
177 void makeSphere(VectorType<T, D> origin, T radius) {
178 // TODO, this is a stupid algorithm and scales with volume, which is madness
179 auto &grid = levelSet->getGrid();
180 viennahrle::CoordType gridDelta = grid.getGridDelta();
181
182 // calculate indices from sphere size
183 viennahrle::Index<D> index;
184 viennahrle::Index<D> endIndex;
185
186 for (unsigned i = 0; i < D; ++i) {
187 index[i] = (origin[i] - radius) / gridDelta - 1;
188 endIndex[i] = (origin[i] + radius) / gridDelta + 1;
189 }
190
191 constexpr double initialWidth = 2.;
192 const T valueLimit = initialWidth * 0.5 * gridDelta + 1e-5;
193 const T radius2 = radius * radius;
194
195 pointDataType pointData;
196 const viennahrle::Index<D> minIndex = index;
197
198 while (index < endIndex) {
199 // take the shortest manhattan distance to gridline intersection
200 T distance = std::numeric_limits<T>::max();
201 for (unsigned i = 0; i < D; ++i) {
202 T y = (index[(i + 1) % D] * gridDelta) - origin[(i + 1) % D];
203 T z = 0;
204 if constexpr (D == 3)
205 z = (index[(i + 2) % D] * gridDelta) - origin[(i + 2) % D];
206 T x = radius2 - y * y - z * z;
207 if (x < 0.)
208 continue;
209 T dirRadius =
210 std::abs((index[i] * gridDelta) - origin[i]) - std::sqrt(x);
211 if (std::abs(dirRadius) < std::abs(distance))
212 distance = dirRadius;
213 }
214
215 if (std::abs(distance) <= valueLimit) {
216 pointData.push_back(std::make_pair(index, distance / gridDelta));
217 }
218 int dim = 0;
219 for (; dim < D - 1; ++dim) {
220 if (index[dim] < endIndex[dim])
221 break;
222 index[dim] = minIndex[dim];
223 }
224 ++index[dim];
225 }
226
227 // Mirror indices correctly into domain, unless boundary conditions
228 // are ignored
229 for (unsigned i = 0; i < pointData.size(); ++i) {
230 for (unsigned j = 0; j < D; ++j) {
231 if (!ignoreBoundaryConditions[j] && grid.isBoundaryPeriodic(j)) {
232 pointData[i].first[j] =
233 grid.globalIndex2LocalIndex(j, pointData[i].first[j]);
234 }
235 }
236 }
237
238 levelSet->insertPoints(pointData);
239 levelSet->getDomain().segment();
240 levelSet->finalize(initialWidth);
241 }
242
245 void makePlane(VectorType<T, D> origin,
246 VectorType<T, D> const &passedNormal) {
247 auto &grid = levelSet->getGrid();
248 viennahrle::CoordType gridDelta = grid.getGridDelta();
249
250 // normalize passedNormal
251 double modulus = 0.;
252 VectorType<T, D> normal = passedNormal;
253 for (unsigned i = 0; i < D; ++i) {
254 modulus += normal[i] * normal[i];
255 }
256 modulus = std::sqrt(modulus);
257 for (unsigned i = 0; i < D; ++i) {
258 normal[i] /= modulus;
259 }
260
261 // check that boundary conditions are correct
262 unsigned i = 0;
263 bool infDimSet = false;
264 for (unsigned n = 0; n < D; ++n) {
265 if (grid.getBoundaryConditions(n) ==
266 viennahrle::BoundaryType::INFINITE_BOUNDARY) {
267 if (!infDimSet) {
268 i = n;
269 infDimSet = true;
270 } else {
271 Logger::getInstance().addError(
272 "Planes can only be created with one Infinite Boundary "
273 "Condition. More than one found!");
274 }
275 }
276 }
277 if (!infDimSet) {
278 Logger::getInstance().addError("Planes require exactly one Infinite "
279 "Boundary Condition. None found!");
280 }
281
282 if (passedNormal[i] == 0.) {
283 Logger::getInstance().addError(
284 "MakeGeometry: Plane cannot be parallel to Infinite Boundary "
285 "direction!");
286 }
287
288 // find minimum and maximum points in infinite direction
289 // there are 2*(D-1) points in the corners of the simulation domain
290 std::vector<Vec3D<T>> cornerPoints;
291 cornerPoints.resize(2 * (D - 1));
292
293 // cyclic permutations
294 unsigned j = (i + 1) % D;
295 unsigned k = (i + 2) % D;
296
297 double minCoord[2];
298 double maxCoord[2];
299 // Find grid boundaries, there used to be a +-1 for the coords.
300 // If an error pops up here, probably has to do with that.
301 // But if +-1 is added here, the boundaries are exceeded and
302 // the correct boundary conditions will add stray points for
303 // tilted planes in lsFromSurfaceMesh later on.
304 for (unsigned n = 0; n < D - 1; ++n) {
305 minCoord[n] = gridDelta * (grid.getMinIndex((i + n + 1) % D) - 1);
306 maxCoord[n] = gridDelta * (grid.getMaxIndex((i + n + 1) % D) + 1);
307 }
308
309 // set corner points
310 cornerPoints[0][j] = minCoord[0];
311 cornerPoints[1][j] = maxCoord[0];
312
313 if constexpr (D == 3) {
314 cornerPoints[0][k] = minCoord[1];
315 cornerPoints[1][k] = maxCoord[1];
316
317 cornerPoints[2][j] = minCoord[0];
318 cornerPoints[2][k] = maxCoord[1];
319 cornerPoints[3][j] = maxCoord[0];
320 cornerPoints[3][k] = minCoord[1];
321 }
322
323 // now find i coordinate of points
324 auto mesh = SmartPointer<Mesh<T>>::New();
325
326 for (unsigned n = 0; n < cornerPoints.size(); ++n) {
327 double numerator = (cornerPoints[n][j] - origin[j]) * normal[j];
328 if constexpr (D == 3)
329 numerator += (cornerPoints[n][k] - origin[k]) * normal[k];
330 else
331 cornerPoints[n][2] = 0.;
332 cornerPoints[n][i] = origin[i] - numerator / normal[i];
333 mesh->insertNextNode(cornerPoints[n]);
334 }
335
336 if (D == 2) {
337 std::array<unsigned, 2> line = {0, 1};
338 if (normal[i] < 0.)
339 std::swap(line[0], line[1]);
340 mesh->insertNextLine(line);
341 } else {
342 std::array<unsigned, 3> triangle = {0, 1, 2};
343 if (normal[i] < 0.)
344 std::swap(triangle[0], triangle[1]);
345 mesh->insertNextTriangle(triangle);
346 triangle = {0, 3, 1};
347 if (normal[i] < 0.)
348 std::swap(triangle[0], triangle[1]);
349 mesh->insertNextTriangle(triangle);
350 }
351
352#ifndef NDEBUG
353 static unsigned planeCounter = 0;
354 VTKWriter<T>(mesh, "plane" + std::to_string(planeCounter++) + ".vtk")
355 .apply();
356#endif
357
358 // now convert mesh to levelset
359 FromSurfaceMesh<T, D>(levelSet, mesh).apply();
360 }
361
362 // This function creates a box starting in minCorner spanning to maxCorner
363 void makeBox(VectorType<T, D> minCorner, VectorType<T, D> maxCorner) {
364 // draw all triangles for the surface and then import from the mesh
365 std::vector<Vec3D<T>> corners;
366 corners.resize(std::pow(2, D), Vec3D<T>{0, 0, 0});
367
368 // first corner is the minCorner
369 for (unsigned i = 0; i < D; ++i)
370 corners[0][i] = minCorner[i];
371
372 // last corner is maxCorner
373 for (unsigned i = 0; i < D; ++i)
374 corners.back()[i] = maxCorner[i];
375
376 // calculate all missing corners
377 corners[1] = corners[0];
378 corners[1][0] = corners.back()[0];
379
380 corners[2] = corners[0];
381 corners[2][1] = corners.back()[1];
382
383 if constexpr (D == 3) {
384 corners[3] = corners.back();
385 corners[3][2] = corners[0][2];
386
387 corners[4] = corners[0];
388 corners[4][2] = corners.back()[2];
389
390 corners[5] = corners.back();
391 corners[5][1] = corners[0][1];
392
393 corners[6] = corners.back();
394 corners[6][0] = corners[0][0];
395 }
396
397 // now add all corners to mesh
398 auto mesh = Mesh<T>::New();
399 for (unsigned i = 0; i < corners.size(); ++i) {
400 mesh->insertNextNode(corners[i]);
401 }
402
403 if (D == 2) {
404 std::array<unsigned, 2> lines[4] = {{0, 2}, {2, 3}, {3, 1}, {1, 0}};
405 for (auto &line : lines)
406 mesh->insertNextLine(line);
407 } else {
408 std::array<unsigned, 3> triangles[12] = {
409 {0, 3, 1}, {0, 2, 3}, {0, 1, 5}, {0, 5, 4}, {0, 4, 2}, {4, 6, 2},
410 {7, 6, 4}, {7, 4, 5}, {7, 2, 6}, {7, 3, 2}, {1, 3, 5}, {3, 7, 5}};
411 for (auto &triangle : triangles)
412 mesh->insertNextTriangle(triangle);
413 }
414
415 // now convert mesh to levelset
416 FromSurfaceMesh<T, D> mesher(levelSet, mesh);
417 mesher.setRemoveBoundaryTriangles(ignoreBoundaryConditions);
418 mesher.apply();
419 }
420
421 void makeCylinder(SmartPointer<Cylinder<T, D>> cylinder) {
422 if (D != 3) {
423 Logger::getInstance()
424 .addError("MakeGeometry: Cylinder can only be created in 3D!")
425 .print();
426 return;
427 }
428 // generate the points on the edges of the cylinders and mesh
429 // them manually
430 // cylinder axis will be (0,0,1)
431 auto gridDelta = levelSet->getGrid().getGridDelta();
432
433 auto points = PointCloud<T, D>::New();
434 const unsigned numPoints =
435 std::ceil(2 * M_PI * cylinder->radius / gridDelta);
436 const double smallAngle = 2.0 * M_PI / static_cast<double>(numPoints);
437
438 auto mesh = Mesh<T>::New();
439 // insert midpoint at base
440 mesh->insertNextNode(Vec3D<T>{0.0, 0.0, 0.0});
441 {
442 constexpr double limit = 2 * M_PI - 1e-6;
443 std::vector<Vec3D<T>> points;
444 if (cylinder->topRadius)
445 std::vector<Vec3D<T>> pointsTop;
446
447 // create and insert points at base
448 for (double angle = 0.; angle < limit; angle += smallAngle) {
449 Vec3D<T> point;
450 point[0] = cylinder->radius * std::cos(angle);
451 point[1] = cylinder->radius * std::sin(angle);
452 point[2] = 0.0;
453 points.push_back(point);
454 mesh->insertNextNode(point);
455 }
456
457 // insert midpoint at top
458 mesh->insertNextNode(Vec3D<T>{0.0, 0.0, cylinder->height});
459
460 double angle = 0;
461 for (unsigned i = 0; i < numPoints; ++i) {
462 // create triangles at base
463 std::array<unsigned, 3> triangle{};
464 triangle[0] = (i + 1) % numPoints + 1;
465 triangle[1] = i + 1;
466 triangle[2] = 0;
467 mesh->insertNextTriangle(triangle);
468
469 // insert points at top
470 // If topRadius is specified, update the first two coordinates of the
471 // points
472 if (cylinder->topRadius) {
473 points[i][0] = cylinder->topRadius * std::cos(angle);
474 points[i][1] = cylinder->topRadius * std::sin(angle);
475 angle += smallAngle;
476 }
477 points[i][2] = cylinder->height;
478 mesh->insertNextNode(points[i]);
479
480 // insert triangles at top
481 triangle[0] = numPoints + 1;
482 triangle[1] = numPoints + i + 2;
483 triangle[2] = (i + 1) % numPoints + 2 + numPoints;
484 mesh->insertNextTriangle(triangle);
485 }
486
487 // insert sidewall triangles
488 for (unsigned i = 0; i < numPoints; ++i) {
489 std::array<unsigned, 3> triangle{};
490 triangle[0] = i + 1;
491 triangle[1] = (i + 1) % numPoints + 1;
492 triangle[2] = i + numPoints + 2;
493 mesh->insertNextTriangle(triangle);
494
495 triangle[0] = (i + 1) % numPoints + 1;
496 triangle[1] = (i + 1) % numPoints + 2 + numPoints;
497 triangle[2] = i + numPoints + 2;
498 mesh->insertNextTriangle(triangle);
499 }
500 }
501
502 // rotate mesh
503 // normalise axis vector
504 T unit =
505 std::sqrt(DotProduct(cylinder->axisDirection, cylinder->axisDirection));
506 Vec3D<T> cylinderAxis;
507 for (int i = 0; i < 3; ++i) {
508 cylinderAxis[i] = cylinder->axisDirection[i] / unit;
509 }
510 // get rotation axis via cross product of (0,0,1) and axis of cylinder
511 Vec3D<T> rotAxis = {-cylinderAxis[1], cylinderAxis[0], 0.0};
512 // angle is acos of dot product
513 T rotationAngle = std::acos(cylinderAxis[2]);
514
515 // rotate mesh
516 TransformMesh<T>(mesh, TransformEnum::ROTATION, rotAxis, rotationAngle)
517 .apply();
518
519 // translate mesh
520 Vec3D<T> translationVector;
521 for (int i = 0; i < 3; ++i) {
522 translationVector[i] = cylinder->origin[i];
523 }
524 TransformMesh<T>(mesh, TransformEnum::TRANSLATION, translationVector)
525 .apply();
526
527 // read mesh from surface
528 FromSurfaceMesh<T, D> mesher(levelSet, mesh);
529 mesher.setRemoveBoundaryTriangles(ignoreBoundaryConditions);
530 mesher.apply();
531 }
532
533 void makeCustom(SmartPointer<PointCloud<T, D>> pointCloud) {
534 // create mesh from point cloud
535 auto mesh = Mesh<T>::New();
536 ConvexHull<T, D>(mesh, pointCloud).apply();
537
538 // read mesh from surface
539 FromSurfaceMesh<T, D> mesher(levelSet, mesh);
540 mesher.setRemoveBoundaryTriangles(ignoreBoundaryConditions);
541 mesher.apply();
542 }
543};
544
545// add all template specialisations for this class
547
548} // namespace viennals
constexpr int D
Definition Epitaxy.cpp:11
double T
Definition Epitaxy.cpp:12
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:127
void apply()
Definition lsMakeGeometry.hpp:144
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:137
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
static auto New(Args &&...args)
Definition lsGeometries.hpp:191
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:37
@ TRANSLATION
Definition lsTransformMesh.hpp:16
@ ROTATION
Definition lsTransformMesh.hpp:17
@ CUSTOM
Definition lsBooleanOperation.hpp:30