ViennaLS
Loading...
Searching...
No Matches
lsDomain.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <limits>
6
7#include <hrleDomain.hpp>
8#include <hrleFillDomainWithSignedDistance.hpp>
9#include <hrleVectorType.hpp>
10
11#include <lsPointData.hpp>
12
13#include <vcLogger.hpp>
14#include <vcSmartPointer.hpp>
15
16#define LS_DOMAIN_SERIALIZATION_VERSION 0
17
18namespace viennals {
19
20using namespace viennacore;
21
22// Boundary condition alias for easier access
23template <int D>
24using BoundaryConditionEnum = typename hrleGrid<D>::boundaryType;
25
28template <class T, int D> class Domain {
29public:
30 // TYPEDEFS
31 typedef T ValueType;
32 typedef hrleGrid<D> GridType;
33 typedef hrleDomain<T, D> DomainType;
35 typedef typename std::vector<std::pair<hrleVectorType<hrleIndexType, D>, T>>
37 typedef typename std::vector<std::array<T, D>> NormalVectorType;
39 typedef typename std::vector<bool> VoidPointMarkersType;
40
41private:
42 // PRIVATE MEMBER VARIABLES
43 GridType grid;
44 DomainType domain;
45 int levelSetWidth = 1;
46 PointDataType pointData;
47 VoidPointMarkersType voidPointMarkers;
48
49public:
50 // STATIC CONSTANTS
51 static constexpr int dimensions = D;
52
53 // PUBLIC MEMBER VARIABLES
54 static constexpr T POS_VALUE = std::numeric_limits<T>::max();
55 static constexpr T NEG_VALUE = std::numeric_limits<T>::lowest();
56
58 Domain(hrleCoordType gridDelta = 1.0) {
59 hrleIndexType gridMin[D], gridMax[D];
60 BoundaryType boundaryCons[D];
61 for (unsigned i = 0; i < D; ++i) {
62 gridMin[i] = 0;
63 gridMax[i] = 0;
64 boundaryCons[i] = BoundaryType::INFINITE_BOUNDARY;
65 }
66
67 grid = GridType(gridMin, gridMax, gridDelta, boundaryCons);
68 domain.deepCopy(grid, DomainType(grid, T(POS_VALUE)));
69 }
70
71 Domain(hrleCoordType *bounds, BoundaryType *boundaryConditions,
72 hrleCoordType gridDelta = 1.0) {
73 hrleIndexType gridMin[D], gridMax[D];
74 for (unsigned i = 0; i < D; ++i) {
75 gridMin[i] = std::floor(bounds[2 * i] / gridDelta);
76 gridMax[i] = std::ceil(bounds[2 * i + 1] / gridDelta);
77 }
78
79 grid = GridType(gridMin, gridMax, gridDelta, boundaryConditions);
80 domain.deepCopy(grid, DomainType(grid, T(POS_VALUE)));
81 }
82
83 Domain(std::vector<hrleCoordType> bounds,
84 std::vector<unsigned> boundaryConditions,
85 hrleCoordType gridDelta = 1.0) {
86 BoundaryType boundaryCons[D];
87 for (unsigned i = 0; i < D; ++i) {
88 boundaryCons[i] = static_cast<BoundaryType>(boundaryConditions[i]);
89 }
90 auto newDomain =
91 SmartPointer<Domain<T, D>>::New(bounds.data(), boundaryCons, gridDelta);
92 this->deepCopy(newDomain);
93 }
94
97 Domain(PointValueVectorType pointData, hrleCoordType *bounds,
98 BoundaryType *boundaryConditions, hrleCoordType gridDelta = 1.0) {
99 auto newDomain =
100 SmartPointer<Domain<T, D>>::New(bounds, boundaryConditions, gridDelta);
101 this->deepCopy(newDomain);
102 hrleFillDomainWithSignedDistance(domain, pointData, T(NEG_VALUE),
103 T(POS_VALUE));
104 }
105
106 Domain(GridType passedGrid) : grid(passedGrid) {
107 domain.deepCopy(grid, DomainType(grid, T(POS_VALUE)));
108 }
109
110 Domain(SmartPointer<Domain> passedDomain) { deepCopy(passedDomain); }
111
114 void finalize(int newWidth) { levelSetWidth = newWidth; }
115
118 void finalize() {}
119
121 void deepCopy(const SmartPointer<Domain<T, D>> passedDomain) {
122 grid = passedDomain->grid;
123 domain.deepCopy(grid, passedDomain->domain);
124 levelSetWidth = passedDomain->levelSetWidth;
125 pointData = passedDomain->pointData;
126 }
127
132 void insertPoints(PointValueVectorType pointData, bool sort = true) {
133 hrleFillDomainWithSignedDistance(domain, pointData, T(NEG_VALUE),
134 T(POS_VALUE), sort);
135 }
136
138 const GridType &getGrid() const { return grid; }
139
141 GridType &getGrid() { return grid; }
142
144 DomainType &getDomain() { return domain; }
145
146 const DomainType &getDomain() const { return domain; }
147
150 unsigned getNumberOfSegments() const { return domain.getNumberOfSegments(); }
151
153 unsigned getNumberOfPoints() const { return domain.getNumberOfPoints(); }
154
155 int getLevelSetWidth() const { return levelSetWidth; }
156
157 void setLevelSetWidth(int width) { levelSetWidth = width; }
158
159 // clear all additional data
160 void clearMetaData() { pointData.clear(); }
161
163 PointDataType &getPointData() { return pointData; }
164
165 const PointDataType &getPointData() const { return pointData; }
166
168 VoidPointMarkersType &getVoidPointMarkers() { return voidPointMarkers; }
169
171 return voidPointMarkers;
172 }
173
175 void print(std::ostream &out = std::cout) {
176 out << "Grid pointer: " << &grid << std::endl;
177 out << "lsDomain: " << &domain << std::endl;
178 out << "DomainSegments: " << std::endl;
179 for (unsigned i = 0; i < getNumberOfSegments(); ++i) {
180 out << &(domain.getDomainSegment(i)) << std::endl;
181 }
182 domain.print(out);
183 }
184
186 std::ostream &serialize(std::ostream &stream) {
187 // Save header to identify Domain
188 stream << "lsDomain";
189
190 // now write format version number
191 char formatVersion = LS_DOMAIN_SERIALIZATION_VERSION;
192 stream.write(&formatVersion, 1);
193
194 // serialize grid
195 grid.serialize(stream);
196
197 // serialize hrleDomain which saves LS values
198 domain.serialize(stream);
199
200 // serialize Domain members
201 // level set width as 32bit uint
202 const uint32_t width = levelSetWidth;
203 stream.write(reinterpret_cast<const char *>(&width), sizeof(uint32_t));
204
205 // serialize pointData if there is any point data associated with this
206 // Domain mark whether there is point data or not (1/0)
207 char hasPointData = (pointData.empty()) ? 0 : 1;
208 stream.write(&hasPointData, 1);
209 if (hasPointData == 1) {
210 pointData.serialize(stream);
211 }
212
213 return stream;
214 }
215
217 std::istream &deserialize(std::istream &stream) {
218 // Check identifier
219 char identifier[8];
220 stream.read(identifier, 8);
221 if (std::string(identifier).compare(0, 8, "lsDomain")) {
222 Logger::getInstance()
223 .addWarning(
224 "Reading Domain from stream failed. Header could not be found.")
225 .print();
226 return stream;
227 }
228
229 // check format version for compatibility
230 char formatVersion;
231 stream.read(&formatVersion, 1);
232 if (formatVersion > LS_DOMAIN_SERIALIZATION_VERSION) {
233 Logger::getInstance()
234 .addWarning(
235 "Reading Domain of version " + std::to_string(formatVersion) +
236 " with reader of version " +
237 std::to_string(LS_DOMAIN_SERIALIZATION_VERSION) + " failed.")
238 .print();
239 return stream;
240 }
241
242 // read in the grid
243 grid.deserialize(stream);
244
245 // read in the hrleDoamin
246 // grid pointer in hrleDomain is implicitly set
247 // to the correct grid already since they were
248 // initialized together
249 domain.deserialize(stream);
250
251 // read in the level set width
252 uint32_t width;
253 stream.read(reinterpret_cast<char *>(&width), sizeof(uint32_t));
254 levelSetWidth = width;
255
256 // check wether there is point data to read
257 char hasPointData;
258 stream.read(&hasPointData, 1);
259 if (hasPointData == 1) {
260 pointData.clear();
261 pointData.deserialize(stream);
262 }
263
264 return stream;
265 }
266};
267
268// add all template specialisations for this class
270
271} // namespace viennals
Class containing all information about the level set, including the dimensions of the domain,...
Definition lsDomain.hpp:28
Domain(PointValueVectorType pointData, hrleCoordType *bounds, BoundaryType *boundaryConditions, hrleCoordType gridDelta=1.0)
initialise Domain with domain size "bounds", filled with point/value pairs in pointData
Definition lsDomain.hpp:97
static constexpr T NEG_VALUE
Definition lsDomain.hpp:55
T ValueType
Definition lsDomain.hpp:31
void clearMetaData()
Definition lsDomain.hpp:160
hrleGrid< D > GridType
Definition lsDomain.hpp:32
void deepCopy(const SmartPointer< Domain< T, D > > passedDomain)
copy all values of "passedDomain" to this Domain
Definition lsDomain.hpp:121
unsigned getNumberOfSegments() const
returns the number of segments, the levelset is split into. This is useful for algorithm parallelisat...
Definition lsDomain.hpp:150
void finalize()
this function finalizes the levelset, so it is ready for use by other algorithms
Definition lsDomain.hpp:118
void print(std::ostream &out=std::cout)
prints basic information and all memebers of the levelset structure
Definition lsDomain.hpp:175
Domain(hrleCoordType gridDelta=1.0)
initalise an empty infinite Domain
Definition lsDomain.hpp:58
std::vector< bool > VoidPointMarkersType
Definition lsDomain.hpp:39
PointData< T > PointDataType
Definition lsDomain.hpp:38
Domain(std::vector< hrleCoordType > bounds, std::vector< unsigned > boundaryConditions, hrleCoordType gridDelta=1.0)
Definition lsDomain.hpp:83
std::vector< std::array< T, D > > NormalVectorType
Definition lsDomain.hpp:37
void finalize(int newWidth)
this function sets a new levelset width and finalizes the levelset, so it is ready for use by other a...
Definition lsDomain.hpp:114
Domain(SmartPointer< Domain > passedDomain)
Definition lsDomain.hpp:110
static constexpr T POS_VALUE
Definition lsDomain.hpp:54
const DomainType & getDomain() const
Definition lsDomain.hpp:146
VoidPointMarkersType & getVoidPointMarkers()
get reference to the voidPoints markers for all points
Definition lsDomain.hpp:168
const VoidPointMarkersType & getVoidPointMarkers() const
Definition lsDomain.hpp:170
BoundaryConditionEnum< D > BoundaryType
Definition lsDomain.hpp:34
const PointDataType & getPointData() const
Definition lsDomain.hpp:165
PointDataType & getPointData()
get reference to point data saved in the level set
Definition lsDomain.hpp:163
hrleDomain< T, D > DomainType
Definition lsDomain.hpp:33
Domain(hrleCoordType *bounds, BoundaryType *boundaryConditions, hrleCoordType gridDelta=1.0)
Definition lsDomain.hpp:71
DomainType & getDomain()
get const reference to the underlying hrleDomain data structure
Definition lsDomain.hpp:144
Domain(GridType passedGrid)
Definition lsDomain.hpp:106
int getLevelSetWidth() const
Definition lsDomain.hpp:155
std::istream & deserialize(std::istream &stream)
Deserialize Domain from binary stream.
Definition lsDomain.hpp:217
GridType & getGrid()
get mutable reference to the grid on which the level set is defined
Definition lsDomain.hpp:141
std::vector< std::pair< hrleVectorType< hrleIndexType, D >, T > > PointValueVectorType
Definition lsDomain.hpp:36
const GridType & getGrid() const
get reference to the grid on which the levelset is defined
Definition lsDomain.hpp:138
unsigned getNumberOfPoints() const
returns the number of defined points
Definition lsDomain.hpp:153
std::ostream & serialize(std::ostream &stream)
Serializes the Domain into a binary stream.
Definition lsDomain.hpp:186
void setLevelSetWidth(int width)
Definition lsDomain.hpp:157
void insertPoints(PointValueVectorType pointData, bool sort=true)
re-initalise Domain with the point/value pairs in pointData This is similar to lsFromMesh with the di...
Definition lsDomain.hpp:132
static constexpr int dimensions
Definition lsDomain.hpp:51
This class holds data associated with points in space.
Definition lsPointData.hpp:20
void clear()
Delete all data stored in this object.
Definition lsPointData.hpp:272
std::ostream & serialize(std::ostream &stream)
Serialize PointData into a binary stream.
Definition lsPointData.hpp:283
bool empty()
Return whether this object is empty.
Definition lsPointData.hpp:280
std::istream & deserialize(std::istream &stream)
Deserialize PointData from a binary stream.
Definition lsPointData.hpp:346
#define LS_DOMAIN_SERIALIZATION_VERSION
Definition lsDomain.hpp:16
#define PRECOMPILE_PRECISION_DIMENSION(className)
Definition lsPreCompileMacros.hpp:24
Definition lsAdvect.hpp:46
typename hrleGrid< D >::boundaryType BoundaryConditionEnum
Definition lsDomain.hpp:24
constexpr int D
Definition pyWrap.cpp:65
double T
Definition pyWrap.cpp:63