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
9#include <lsDomain.hpp>
10#include <lsExpand.hpp>
11
12#include <vcLogger.hpp>
13#include <vcSmartPointer.hpp>
14#include <vcVectorType.hpp>
15
16namespace viennals {
17
18using namespace viennacore;
19
35template <class T, int D> class CalculateNormalVectors {
36 SmartPointer<Domain<T, D>> levelSet = nullptr;
37 T maxValue = 0.5;
38
39 // Constants for calculation
40 static constexpr T DEFAULT_MAX_VALUE = 0.5;
41 static constexpr T EPSILON = 1e-12;
42 static constexpr T FINITE_DIFF_FACTOR = 0.5;
43
44public:
45 static constexpr char normalVectorsLabel[] = "Normals";
46
48
49 CalculateNormalVectors(SmartPointer<Domain<T, D>> passedLevelSet,
50 T passedMaxValue = DEFAULT_MAX_VALUE)
51 : levelSet(passedLevelSet), maxValue(passedMaxValue) {}
52
53 void setLevelSet(SmartPointer<Domain<T, D>> passedLevelSet) {
54 levelSet = passedLevelSet;
55 }
56
57 void setMaxValue(const T passedMaxValue) {
58 if (passedMaxValue <= 0) {
59 VIENNACORE_LOG_WARNING(
60 "CalculateNormalVectors: maxValue should be positive. "
61 "Using default value " +
62 std::to_string(DEFAULT_MAX_VALUE) + ".");
63 maxValue = DEFAULT_MAX_VALUE;
64 } else {
65 maxValue = passedMaxValue;
66 }
67 }
68
69 SmartPointer<Domain<T, D>> getLevelSet() const { return levelSet; }
70
71 T getMaxValue() const { return maxValue; }
72
74 bool hasNormalVectors() const {
75 if (levelSet == nullptr)
76 return false;
77 auto &pointData = levelSet->getPointData();
78 return pointData.getVectorData(normalVectorsLabel) != nullptr;
79 }
80
81 void apply() {
82 if (levelSet == nullptr) {
83 VIENNACORE_LOG_ERROR(
84 "No level set was passed to CalculateNormalVectors.");
85 return;
86 }
87
88 if (levelSet->getLevelSetWidth() < (maxValue * 4) + 1) {
89 VIENNACORE_LOG_WARNING("CalculateNormalVectors: Level set width must be "
90 "greater than " +
91 std::to_string((maxValue * 4) + 1) +
92 ". Expanding level set to " +
93 std::to_string((maxValue * 4) + 1) + ".");
94 Expand<T, D>(levelSet, (maxValue * 4) + 1).apply();
95 }
96
97 std::vector<std::vector<Vec3D<T>>> normalVectorsVector(
98 levelSet->getNumberOfSegments());
99
100 // Estimate memory requirements per thread to improve cache performance
101 double pointsPerSegment =
102 double(2 * levelSet->getDomain().getNumberOfPoints()) /
103 double(levelSet->getLevelSetWidth());
104
105 auto grid = levelSet->getGrid();
106
107 // Calculate Normalvectors
108#pragma omp parallel num_threads(levelSet->getNumberOfSegments())
109 {
110 int p = 0;
111#ifdef _OPENMP
112 p = omp_get_thread_num();
113#endif
114
115 auto &normalVectors = normalVectorsVector[p];
116 normalVectors.reserve(pointsPerSegment);
117
118 viennahrle::Index<D> const startVector =
119 (p == 0) ? grid.getMinGridPoint()
120 : levelSet->getDomain().getSegmentation()[p - 1];
121
122 viennahrle::Index<D> const endVector =
123 (p != static_cast<int>(levelSet->getNumberOfSegments() - 1))
124 ? levelSet->getDomain().getSegmentation()[p]
125 : grid.incrementIndices(grid.getMaxGridPoint());
126
127 for (viennahrle::ConstSparseStarIterator<
128 typename Domain<T, D>::DomainType, 1>
129 neighborIt(levelSet->getDomain(), startVector);
130 neighborIt.getIndices() < endVector; neighborIt.next()) {
131
132 auto &center = neighborIt.getCenter();
133 if (!center.isDefined()) {
134 continue;
135 } else if (std::abs(center.getValue()) > maxValue) {
136 // push an empty vector to keep ordering correct
137 Vec3D<T> tmp{};
138 normalVectors.push_back(tmp);
139 continue;
140 }
141
142 Vec3D<T> n{};
143
144 T denominator = 0;
145 for (int i = 0; i < D; i++) {
146 T pos = neighborIt.getNeighbor(i).getValue() - center.getValue();
147 T neg = center.getValue() - neighborIt.getNeighbor(i + D).getValue();
148 n[i] = (pos + neg) * FINITE_DIFF_FACTOR;
149 denominator += n[i] * n[i];
150 }
151
152 denominator = std::sqrt(denominator);
153 if (std::abs(denominator) < EPSILON) {
154 VIENNACORE_LOG_WARNING(
155 "CalculateNormalVectors: Vector of length 0 at " +
156 neighborIt.getIndices().to_string());
157 for (unsigned i = 0; i < D; ++i)
158 n[i] = 0.;
159 } else {
160 for (unsigned i = 0; i < D; ++i) {
161 n[i] /= denominator;
162 }
163 }
164
165 normalVectors.push_back(n);
166 }
167 }
168
169 // copy all normals
170 unsigned numberOfNormals = 0;
171 for (unsigned i = 0; i < levelSet->getNumberOfSegments(); ++i) {
172 numberOfNormals += normalVectorsVector[i].size();
173 }
174 normalVectorsVector[0].reserve(numberOfNormals);
175
176 for (unsigned i = 1; i < levelSet->getNumberOfSegments(); ++i) {
177 normalVectorsVector[0].insert(normalVectorsVector[0].end(),
178 normalVectorsVector[i].begin(),
179 normalVectorsVector[i].end());
180 }
181
182 // insert into pointData of levelSet
183 auto &pointData = levelSet->getPointData();
184 auto vectorDataPointer = pointData.getVectorData(normalVectorsLabel, true);
185 // if it does not exist, insert new normals vector
186 if (vectorDataPointer == nullptr) {
187 pointData.insertNextVectorData(normalVectorsVector[0],
189 } else {
190 // if it does exist, just swap the old with the new values
191 *vectorDataPointer = std::move(normalVectorsVector[0]);
192 }
193 }
194};
195
196// add all template specialisations for this class
197PRECOMPILE_PRECISION_DIMENSION(CalculateNormalVectors)
198
199} // namespace viennals
constexpr int D
Definition Epitaxy.cpp:11
double T
Definition Epitaxy.cpp:12
static constexpr char normalVectorsLabel[]
Definition lsCalculateNormalVectors.hpp:45
SmartPointer< Domain< T, D > > getLevelSet() const
Definition lsCalculateNormalVectors.hpp:69
void setLevelSet(SmartPointer< Domain< T, D > > passedLevelSet)
Definition lsCalculateNormalVectors.hpp:53
void apply()
Definition lsCalculateNormalVectors.hpp:81
T getMaxValue() const
Definition lsCalculateNormalVectors.hpp:71
CalculateNormalVectors(SmartPointer< Domain< T, D > > passedLevelSet, T passedMaxValue=DEFAULT_MAX_VALUE)
Definition lsCalculateNormalVectors.hpp:49
void setMaxValue(const T passedMaxValue)
Definition lsCalculateNormalVectors.hpp:57
bool hasNormalVectors() const
Check if normal vectors are already calculated for the level set.
Definition lsCalculateNormalVectors.hpp:74
Class containing all information about the level set, including the dimensions of the domain,...
Definition lsDomain.hpp:28
viennahrle::Domain< T, D > DomainType
Definition lsDomain.hpp:33
Expands the levelSet to the specified number of layers. The largest value in the levelset is thus wid...
Definition lsExpand.hpp:17
void apply()
Apply the expansion to the specified width.
Definition lsExpand.hpp:44
#define PRECOMPILE_PRECISION_DIMENSION(className)
Definition lsPreCompileMacros.hpp:24
Definition lsAdvect.hpp:41