ViennaLS
Loading...
Searching...
No Matches
lsExpand.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <hrleSparseStarIterator.hpp>
6#include <lsDomain.hpp>
7
8#include <vcVectorType.hpp>
9
10namespace viennals {
11
12using namespace viennacore;
13
17template <class T, int D> class Expand {
18 SmartPointer<Domain<T, D>> levelSet = nullptr;
19 int width = 0;
20 bool updatePointData = true;
21
22public:
23 Expand() = default;
24
25 Expand(SmartPointer<Domain<T, D>> passedlsDomain)
26 : levelSet(passedlsDomain) {}
27
28 Expand(SmartPointer<Domain<T, D>> passedlsDomain, int passedWidth)
29 : levelSet(passedlsDomain), width(passedWidth) {}
30
31 void setLevelSet(SmartPointer<Domain<T, D>> passedlsDomain) {
32 levelSet = passedlsDomain;
33 }
34
37 void setWidth(int passedWidth) { width = passedWidth; }
38
41 void setUpdatePointData(bool update) { updatePointData = update; }
42
44 void apply() {
45 if (levelSet == nullptr) {
46 Logger::getInstance()
47 .addError("No level set passed to Expand. Not expanding.")
48 .print();
49 return;
50 }
51
52 if (width <= levelSet->getLevelSetWidth())
53 return;
54
55 if (levelSet->getNumberOfPoints() == 0)
56 return;
57
58 const T totalLimit = width * 0.5;
59 const int startWidth = levelSet->getLevelSetWidth();
60 const int numberOfRequiredCycles = width - startWidth;
61
62 for (int currentCycle = 0; currentCycle < numberOfRequiredCycles;
63 ++currentCycle) {
64
65 const int allocationFactor =
66 1 + 1.0 / static_cast<double>(startWidth + currentCycle);
67 const T limit = (startWidth + currentCycle + 1) * T(0.5);
68
69 auto &grid = levelSet->getGrid();
70 auto newlsDomain = SmartPointer<Domain<T, D>>::New(grid);
71 auto &newDomain = newlsDomain->getDomain();
72 auto &domain = levelSet->getDomain();
73
74 newDomain.initialize(domain.getNewSegmentation(),
75 domain.getAllocation() * allocationFactor);
76
77 const bool updateData = updatePointData;
78 // save how data should be transferred to new level set
79 // list of indices into the old pointData vector
80 std::vector<std::vector<unsigned>> newDataSourceIds;
81 if (updateData)
82 newDataSourceIds.resize(newDomain.getNumberOfSegments());
83
84#pragma omp parallel for
85 for (unsigned p = 0; p < newDomain.getNumberOfSegments(); ++p) {
86
87 auto &domainSegment = newDomain.getDomainSegment(p);
88
89 viennahrle::Index<D> const startVector =
90 (p == 0) ? grid.getMinGridPoint()
91 : newDomain.getSegmentation()[p - 1];
92
93 viennahrle::Index<D> const endVector =
94 (p != static_cast<int>(newDomain.getNumberOfSegments() - 1))
95 ? newDomain.getSegmentation()[p]
96 : grid.incrementIndices(grid.getMaxGridPoint());
97
98 for (viennahrle::ConstSparseStarIterator<
99 typename Domain<T, D>::DomainType, 1>
100 neighborIt(domain, startVector);
101 neighborIt.getIndices() < endVector; neighborIt.next()) {
102
103 auto &centerIt = neighborIt.getCenter();
104 if (std::abs(centerIt.getValue()) <= totalLimit) {
105 domainSegment.insertNextDefinedPoint(neighborIt.getIndices(),
106 centerIt.getValue());
107 if (updateData)
108 newDataSourceIds[p].push_back(centerIt.getPointId());
109 } else {
110 if (centerIt.getValue() > -std::numeric_limits<T>::epsilon()) {
111 T distance = Domain<T, D>::POS_VALUE;
112 int neighbor = -1;
113 for (int i = 0; i < 2 * D; i++) {
114 T newValue = neighborIt.getNeighbor(i).getValue() + T(1);
115 if (distance > newValue) {
116 distance = newValue;
117 neighbor = i;
118 }
119 }
120 if (distance <= limit) {
121 domainSegment.insertNextDefinedPoint(neighborIt.getIndices(),
122 distance);
123 if (updateData)
124 newDataSourceIds[p].push_back(
125 neighborIt.getNeighbor(neighbor).getPointId());
126 } else {
127 // TODO: use insertNextUndefinedRunType
128 domainSegment.insertNextUndefinedPoint(neighborIt.getIndices(),
130 }
131 } else {
132 T distance = Domain<T, D>::NEG_VALUE;
133 int neighbor = -1;
134 for (int i = 0; i < 2 * D; i++) {
135 T newValue = neighborIt.getNeighbor(i).getValue() - T(1);
136 if (distance < newValue) {
137 distance = newValue;
138 neighbor = i;
139 }
140 }
141 if (distance >= -limit) {
142 domainSegment.insertNextDefinedPoint(neighborIt.getIndices(),
143 distance);
144 if (updateData)
145 newDataSourceIds[p].push_back(
146 neighborIt.getNeighbor(neighbor).getPointId());
147 } else {
148 // TODO: use insertNextUndefinedRunType
149 domainSegment.insertNextUndefinedPoint(neighborIt.getIndices(),
151 }
152 }
153 }
154 }
155 }
156
157 // now copy old data into new level set
158 if (updateData) {
159 newlsDomain->getPointData().translateFromMultiData(
160 levelSet->getPointData(), newDataSourceIds);
161 }
162
163 newDomain.finalize();
164 levelSet->deepCopy(newlsDomain);
165 }
166 levelSet->getDomain().segment();
167 levelSet->finalize(width);
168 }
169};
170
171// add all template specialisations for this class
173
174} // namespace viennals
constexpr int D
Definition Epitaxy.cpp:12
double T
Definition Epitaxy.cpp:13
Class containing all information about the level set, including the dimensions of the domain,...
Definition lsDomain.hpp:27
viennahrle::Domain< T, D > DomainType
Definition lsDomain.hpp:32
static constexpr T NEG_VALUE
Definition lsDomain.hpp:52
static constexpr T POS_VALUE
Definition lsDomain.hpp:51
Expand(SmartPointer< Domain< T, D > > passedlsDomain, int passedWidth)
Definition lsExpand.hpp:28
void setUpdatePointData(bool update)
Set whether to update the point data stored in the LS during this algorithm. Defaults to true.
Definition lsExpand.hpp:41
void apply()
Apply the expansion to the specified width.
Definition lsExpand.hpp:44
Expand()=default
Expand(SmartPointer< Domain< T, D > > passedlsDomain)
Definition lsExpand.hpp:25
void setWidth(int passedWidth)
Set how far the level set should be extended. Points with value width*0.5 will be added by this algor...
Definition lsExpand.hpp:37
void setLevelSet(SmartPointer< Domain< T, D > > passedlsDomain)
Definition lsExpand.hpp:31
#define PRECOMPILE_PRECISION_DIMENSION(className)
Definition lsPreCompileMacros.hpp:24
Definition lsAdvect.hpp:41