ViennaLS
Loading...
Searching...
No Matches
lsCalculateNormalVectors.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <algorithm>
6
7#include <hrleSparseStarIterator.hpp>
8#include <hrleVectorType.hpp>
9
10#include <lsDomain.hpp>
11
12#include <vcLogger.hpp>
13#include <vcSmartPointer.hpp>
14#include <vcVectorUtil.hpp>
15
16namespace viennals {
17
18using namespace viennacore;
19
26template <class T, int D> class CalculateNormalVectors {
27 SmartPointer<Domain<T, D>> levelSet = nullptr;
28 T maxValue = 0.5;
29
30public:
31 static constexpr char normalVectorsLabel[] = "Normals";
32
34
35 CalculateNormalVectors(SmartPointer<Domain<T, D>> passedLevelSet,
36 T passedMaxValue = 0.5)
37 : levelSet(passedLevelSet), maxValue(passedMaxValue) {}
38
39 void setLevelSet(SmartPointer<Domain<T, D>> passedLevelSet) {
40 levelSet = passedLevelSet;
41 }
42
43 void setMaxValue(const T passedMaxValue) { maxValue = passedMaxValue; }
44
45 void apply() {
46 if (levelSet == nullptr) {
47 Logger::getInstance()
48 .addWarning("No level set was passed to CalculateNormalVectors.")
49 .print();
50 }
51
52 if (levelSet->getLevelSetWidth() < (maxValue * 4) + 1) {
53 Logger::getInstance()
54 .addWarning("CalculateNormalVectors: Level set width must be "
55 "greater than " +
56 std::to_string((maxValue * 4) + 1) + "!")
57 .print();
58 }
59
60 std::vector<std::vector<Vec3D<T>>> normalVectorsVector(
61 levelSet->getNumberOfSegments());
62 double pointsPerSegment =
63 double(2 * levelSet->getDomain().getNumberOfPoints()) /
64 double(levelSet->getLevelSetWidth());
65
66 auto grid = levelSet->getGrid();
67
69#pragma omp parallel num_threads(levelSet->getNumberOfSegments())
70 {
71 int p = 0;
72#ifdef _OPENMP
73 p = omp_get_thread_num();
74#endif
75
76 auto &normalVectors = normalVectorsVector[p];
77 normalVectors.reserve(pointsPerSegment);
78
79 hrleVectorType<hrleIndexType, D> startVector =
80 (p == 0) ? grid.getMinGridPoint()
81 : levelSet->getDomain().getSegmentation()[p - 1];
82
83 hrleVectorType<hrleIndexType, D> endVector =
84 (p != static_cast<int>(levelSet->getNumberOfSegments() - 1))
85 ? levelSet->getDomain().getSegmentation()[p]
86 : grid.incrementIndices(grid.getMaxGridPoint());
87
88 for (hrleConstSparseStarIterator<typename Domain<T, D>::DomainType, 1>
89 neighborIt(levelSet->getDomain(), startVector);
90 neighborIt.getIndices() < endVector; neighborIt.next()) {
91
92 auto &center = neighborIt.getCenter();
93 if (!center.isDefined()) {
94 continue;
95 } else if (std::abs(center.getValue()) > maxValue) {
96 // push an empty vector to keep ordering correct
97 Vec3D<T> tmp = {};
98 normalVectors.push_back(tmp);
99 continue;
100 }
101
102 Vec3D<T> n;
103
104 T denominator = 0;
105 for (int i = 0; i < D; i++) {
106 T pos = neighborIt.getNeighbor(i).getValue() - center.getValue();
107 T neg = center.getValue() - neighborIt.getNeighbor(i + D).getValue();
108 n[i] = (pos + neg) * 0.5;
109 denominator += n[i] * n[i];
110 }
111
112 denominator = std::sqrt(denominator);
113 if (std::abs(denominator) < 1e-12) {
114 std::ostringstream oss;
115 oss << "CalculateNormalVectors: Vector of length 0 at "
116 << neighborIt.getIndices();
117 Logger::getInstance().addWarning(oss.str()).print();
118 for (unsigned i = 0; i < D; ++i)
119 n[i] = 0.;
120 } else {
121 for (unsigned i = 0; i < D; ++i) {
122 n[i] /= denominator;
123 }
124 }
125
126 normalVectors.push_back(n);
127 }
128 }
129
130 // copy all normals
131 unsigned numberOfNormals = 0;
132 for (unsigned i = 0; i < levelSet->getNumberOfSegments(); ++i) {
133 numberOfNormals += normalVectorsVector[i].size();
134 }
135 normalVectorsVector[0].reserve(numberOfNormals);
136
137 for (unsigned i = 1; i < levelSet->getNumberOfSegments(); ++i) {
138 normalVectorsVector[0].insert(normalVectorsVector[0].end(),
139 normalVectorsVector[i].begin(),
140 normalVectorsVector[i].end());
141 }
142
143 // insert into pointData of levelSet
144 auto &pointData = levelSet->getPointData();
145 auto vectorDataPointer = pointData.getVectorData(normalVectorsLabel, true);
146 // if it does not exist, insert new normals vector
147 if (vectorDataPointer == nullptr) {
148 pointData.insertNextVectorData(normalVectorsVector[0],
150 } else {
151 // if it does exist, just swap the old with the new values
152 *vectorDataPointer = std::move(normalVectorsVector[0]);
153 }
154 }
155};
156
157// add all template specialisations for this class
158PRECOMPILE_PRECISION_DIMENSION(CalculateNormalVectors)
159
160} // namespace viennals
This algorithm is used to compute the normal vectors for all points with level set values <= 0....
Definition lsCalculateNormalVectors.hpp:26
CalculateNormalVectors()
Definition lsCalculateNormalVectors.hpp:33
CalculateNormalVectors(SmartPointer< Domain< T, D > > passedLevelSet, T passedMaxValue=0.5)
Definition lsCalculateNormalVectors.hpp:35
static constexpr char normalVectorsLabel[]
Definition lsCalculateNormalVectors.hpp:31
void setLevelSet(SmartPointer< Domain< T, D > > passedLevelSet)
Definition lsCalculateNormalVectors.hpp:39
void apply()
Definition lsCalculateNormalVectors.hpp:45
void setMaxValue(const T passedMaxValue)
Definition lsCalculateNormalVectors.hpp:43
Class containing all information about the level set, including the dimensions of the domain,...
Definition lsDomain.hpp:28
hrleDomain< T, D > DomainType
Definition lsDomain.hpp:33
#define PRECOMPILE_PRECISION_DIMENSION(className)
Definition lsPreCompileMacros.hpp:24
Definition lsAdvect.hpp:46
constexpr int D
Definition pyWrap.cpp:65
double T
Definition pyWrap.cpp:63