ViennaLS
Loading...
Searching...
No Matches
lsPointData.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <array>
6#include <cstdint>
7#include <vector>
8
9#include <lsConcepts.hpp>
10
11#include <vcLogger.hpp>
12
13namespace viennals {
14
15using namespace viennacore;
16
18template <class T = double,
20class PointData {
21public:
22 typedef std::vector<T> ScalarDataType;
23 typedef std::vector<std::array<T, 3>> VectorDataType;
24
25private:
26 std::vector<ScalarDataType> scalarData;
27 std::vector<std::string> scalarDataLabels;
28 std::vector<VectorDataType> vectorData;
29 std::vector<std::string> vectorDataLabels;
30
31 template <class VectorType, class ReturnType = std::conditional_t<
32 std::is_const<VectorType>::value,
33 const typename VectorType::value_type *,
34 typename VectorType::value_type *>>
35 ReturnType indexPointerOrNull(VectorType &v, int index) const {
36 if (index >= 0 && index < v.size())
37 return &(v[index]);
38 else
39 Logger::getInstance()
40 .addWarning("PointData: Tried to access out of bounds index! "
41 "Returned nullptr instead.")
42 .print();
43 return nullptr;
44 }
45
46 template <class DataType>
47 void appendTranslateData(DataType &currentData, const DataType &source,
48 const std::vector<unsigned> &indices) {
49 currentData.reserve(currentData.size() + indices.size());
50 for (unsigned i = 0; i < indices.size(); ++i) {
51 currentData.push_back(source[indices[i]]);
52 }
53 }
54
55public:
58 std::string label = "Scalars") {
59 scalarData.push_back(scalars);
60 scalarDataLabels.push_back(label);
61 }
62
65 std::string label = "Scalars") {
66 scalarData.push_back(std::move(scalars));
67 scalarDataLabels.push_back(label);
68 }
69
72 std::string label = "Vectors") {
73 vectorData.push_back(vectors);
74 vectorDataLabels.push_back(label);
75 }
76
79 std::string label = "Vectors") {
80 vectorData.push_back(std::move(vectors));
81 vectorDataLabels.push_back(label);
82 }
83
85 unsigned getScalarDataSize() const { return scalarData.size(); }
86
88 unsigned getVectorDataSize() const { return vectorData.size(); }
89
91 return indexPointerOrNull(scalarData, index);
92 }
93
94 const ScalarDataType *getScalarData(int index) const {
95 return indexPointerOrNull(scalarData, index);
96 }
97
98 ScalarDataType *getScalarData(std::string searchLabel,
99 bool noWarning = false) {
100 if (int i = getScalarDataIndex(searchLabel); i != -1) {
101 return &(scalarData[i]);
102 }
103 if (!noWarning)
104 Logger::getInstance()
105 .addWarning("PointData attempted to access scalar data labeled '" +
106 searchLabel +
107 "', which does not exist. Returning nullptr instead.")
108 .print();
109 return nullptr;
110 }
111
112 const ScalarDataType *getScalarData(std::string searchLabel,
113 bool noWarning = false) const {
114 if (int i = getScalarDataIndex(searchLabel); i != -1) {
115 return &(scalarData[i]);
116 }
117 if (!noWarning)
118 Logger::getInstance()
119 .addWarning("PointData attempted to access scalar data labeled '" +
120 searchLabel +
121 "', which does not exist. Returning nullptr instead.")
122 .print();
123 return nullptr;
124 }
125
126 int getScalarDataIndex(std::string searchLabel) const {
127 for (int i = 0; i < scalarDataLabels.size(); ++i) {
128 if (scalarDataLabels[i] == searchLabel) {
129 return i;
130 }
131 }
132 return -1;
133 }
134
135 std::string getScalarDataLabel(int index) const {
136 return (index >= 0 && index < scalarDataLabels.size())
137 ? scalarDataLabels[index]
138 : "";
139 }
140
141 void setScalarDataLabel(int index, std::string newLabel) {
142 scalarDataLabels[index] = newLabel;
143 }
144
146 void eraseScalarData(int index) {
147 scalarData.erase(scalarData.begin() + index);
148 scalarDataLabels.erase(scalarDataLabels.begin() + index);
149 }
150
152 return indexPointerOrNull(vectorData, index);
153 }
154
155 const VectorDataType *getVectorData(int index) const {
156 return indexPointerOrNull(vectorData, index);
157 }
158
159 VectorDataType *getVectorData(std::string searchLabel,
160 bool noWarning = false) {
161 if (int i = getVectorDataIndex(searchLabel); i != -1) {
162 return &(vectorData[i]);
163 }
164 if (!noWarning)
165 Logger::getInstance()
166 .addWarning("PointData attempted to access vector data labeled '" +
167 searchLabel +
168 "', which does not exist. Returning nullptr instead.")
169 .print();
170 return nullptr;
171 }
172
173 const VectorDataType *getVectorData(std::string searchLabel,
174 bool noWarning = false) const {
175 if (int i = getVectorDataIndex(searchLabel); i != -1) {
176 return &(vectorData[i]);
177 }
178 if (!noWarning)
179 Logger::getInstance()
180 .addWarning("PointData attempted to access vector data labeled '" +
181 searchLabel +
182 "', which does not exist. Returning nullptr instead.")
183 .print();
184 return nullptr;
185 }
186
187 int getVectorDataIndex(std::string searchLabel) const {
188 for (int i = 0; i < vectorDataLabels.size(); ++i) {
189 if (vectorDataLabels[i] == searchLabel) {
190 return i;
191 }
192 }
193 return -1;
194 }
195
196 std::string getVectorDataLabel(int index) const {
197 return (index >= 0 && index < vectorDataLabels.size())
198 ? vectorDataLabels[index]
199 : "";
200 }
201
202 void setVectorDataLabel(int index, std::string newLabel) {
203 vectorDataLabels[index] = newLabel;
204 }
205
207 void eraseVectorData(int index) {
208 vectorData.erase(vectorData.begin() + index);
209 vectorDataLabels.erase(vectorDataLabels.begin() + index);
210 }
211
213 void append(const PointData &passedData) {
214 scalarData.insert(scalarData.end(), passedData.scalarData.begin(),
215 passedData.scalarData.end());
216 scalarDataLabels.insert(scalarDataLabels.end(),
217 passedData.scalarDataLabels.begin(),
218 passedData.scalarDataLabels.end());
219 vectorData.insert(vectorData.end(), passedData.vectorData.begin(),
220 passedData.vectorData.end());
221 vectorDataLabels.insert(vectorDataLabels.end(),
222 passedData.vectorDataLabels.begin(),
223 passedData.vectorDataLabels.end());
224 }
225
230 void translateFromData(const PointData &source,
231 const std::vector<unsigned> &indices) {
232 // scalars
233 for (unsigned j = 0; j < source.getScalarDataSize(); ++j) {
235 auto currentData = --scalarData.end();
236 appendTranslateData(*currentData, source.scalarData[j], indices);
237 }
238
239 // vectors
240 for (unsigned j = 0; j < source.getVectorDataSize(); ++j) {
242 auto currentData = --vectorData.end();
243 appendTranslateData(*currentData, source.vectorData[j], indices);
244 }
245 }
246
250 const PointData &source,
251 const std::vector<std::vector<unsigned>> &indicesVector) {
252 // scalars
253 for (unsigned j = 0; j < source.getScalarDataSize(); ++j) {
255 auto currentData = --scalarData.end();
256 for (const auto &i : indicesVector) {
257 appendTranslateData(*currentData, source.scalarData[j], i);
258 }
259 }
260
261 // vectors
262 for (unsigned j = 0; j < source.getVectorDataSize(); ++j) {
264 auto currentData = --vectorData.end();
265 for (const auto &i : indicesVector) {
266 appendTranslateData(*currentData, source.vectorData[j], i);
267 }
268 }
269 }
270
272 void clear() {
273 scalarData.clear();
274 scalarDataLabels.clear();
275 vectorData.clear();
276 vectorDataLabels.clear();
277 }
278
280 bool empty() { return scalarData.empty() && vectorData.empty(); }
281
283 std::ostream &serialize(std::ostream &stream) {
284 // HEADER
285 // identifier: "PointData"
286 // 4 byte: number of scalar data sets
287 // 4 byte: number of vector data sets
288 stream << "lsPointData";
289 uint32_t numberOfScalarData = scalarData.size();
290 uint32_t numberOfVectorData = vectorData.size();
291 stream.write(reinterpret_cast<const char *>(&numberOfScalarData),
292 sizeof(uint32_t));
293 stream.write(reinterpret_cast<const char *>(&numberOfVectorData),
294 sizeof(uint32_t));
295
296 // Scalar Data
297 {
298 auto labelIt = scalarDataLabels.begin();
299 // iterate over all scalar data sets
300 for (auto data : scalarData) {
301 // write name of scalar data and size of set
302 uint32_t sizeOfName = labelIt->length();
303 stream.write(reinterpret_cast<char *>(&sizeOfName), sizeof(uint32_t));
304 stream << *labelIt;
305 uint32_t numberOfValues = data.size();
306 stream.write(reinterpret_cast<const char *>(&numberOfValues),
307 sizeof(uint32_t));
308 // iterate over scalars in data set
309 for (auto value : data) {
310 stream.write(reinterpret_cast<const char *>(&value),
311 sizeof(typename ScalarDataType::value_type));
312 }
313 ++labelIt;
314 }
315 }
316
317 // Vector Data
318 {
319 auto labelIt = vectorDataLabels.begin();
320 // iterate over all vector data sets
321 for (auto data : vectorData) {
322 // write name of vector data and size of set
323 uint32_t sizeOfName = labelIt->length();
324 stream.write(reinterpret_cast<char *>(&sizeOfName), sizeof(uint32_t));
325 stream << *labelIt;
326 uint32_t numberOfVectors = data.size();
327 stream.write(reinterpret_cast<const char *>(&numberOfVectors),
328 sizeof(uint32_t));
329 // iterate over vectors in data set
330 for (auto vector : data) {
331 // over values in vector
332 for (auto value : vector) {
333 stream.write(
334 reinterpret_cast<const char *>(&value),
335 sizeof(typename VectorDataType::value_type::value_type));
336 }
337 }
338 ++labelIt;
339 }
340 }
341
342 return stream;
343 }
344
346 std::istream &deserialize(std::istream &stream) {
347 char identifier[11];
348 stream.read(identifier, 11);
349 if (std::string(identifier).compare(0, 11, "lsPointData")) {
350 Logger::getInstance()
351 .addWarning("Reading PointData from stream failed. Header could "
352 "not be found.")
353 .print();
354 return stream;
355 }
356
357 // read number of data sets
358 unsigned numberOfScalarData = 0;
359 unsigned numberOfVectorData = 0;
360 stream.read(reinterpret_cast<char *>(&numberOfScalarData),
361 sizeof(uint32_t));
362 stream.read(reinterpret_cast<char *>(&numberOfVectorData),
363 sizeof(uint32_t));
364
365 // read scalar data
366 for (unsigned i = 0; i < numberOfScalarData; ++i) {
367 uint32_t sizeOfName;
368 stream.read(reinterpret_cast<char *>(&sizeOfName), sizeof(uint32_t));
369 std::vector<char> dataLabel(sizeOfName);
370 stream.read(dataLabel.data(), sizeOfName);
371 uint32_t numberOfValues;
372 stream.read(reinterpret_cast<char *>(&numberOfValues), sizeof(uint32_t));
373 ScalarDataType scalarData;
374 scalarData.resize(numberOfValues);
375 // read all scalar values into the data
376 for (auto &value : scalarData) {
377 stream.read(reinterpret_cast<char *>(&value),
378 sizeof(typename ScalarDataType::value_type));
379 }
380 // now add this scalar data to current PointData
381 insertNextScalarData(scalarData,
382 std::string(dataLabel.begin(), dataLabel.end()));
383 }
384
385 // read vector data
386 for (unsigned i = 0; i < numberOfVectorData; ++i) {
387 uint32_t sizeOfName;
388 stream.read(reinterpret_cast<char *>(&sizeOfName), sizeof(uint32_t));
389 std::vector<char> dataLabel(sizeOfName);
390 stream.read(dataLabel.data(), sizeOfName);
391 uint32_t numberOfValues;
392 stream.read(reinterpret_cast<char *>(&numberOfValues), sizeof(uint32_t));
393 VectorDataType vectorData;
394 vectorData.resize(numberOfValues);
395 // read all vector values into the vector data
396 for (auto &vector : vectorData) {
397 for (auto &value : vector) {
398 stream.read(reinterpret_cast<char *>(&value),
399 sizeof(typename VectorDataType::value_type::value_type));
400 }
401 }
402 // now add this scalar data to current PointData
403 insertNextVectorData(vectorData,
404 std::string(dataLabel.begin(), dataLabel.end()));
405 }
406
407 return stream;
408 }
409};
410
411// add all template specialisations for this class
413
414} // namespace viennals
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
void setScalarDataLabel(int index, std::string newLabel)
Definition lsPointData.hpp:141
void insertNextScalarData(const ScalarDataType &scalars, std::string label="Scalars")
insert new scalar data array
Definition lsPointData.hpp:57
void eraseScalarData(int index)
Delete the scalar data at index.
Definition lsPointData.hpp:146
void translateFromData(const PointData &source, const std::vector< unsigned > &indices)
Add data in the passed source pointData into this data according to the indices passed....
Definition lsPointData.hpp:230
const VectorDataType * getVectorData(std::string searchLabel, bool noWarning=false) const
Definition lsPointData.hpp:173
void setVectorDataLabel(int index, std::string newLabel)
Definition lsPointData.hpp:202
std::vector< T > ScalarDataType
Definition lsPointData.hpp:22
void translateFromMultiData(const PointData &source, const std::vector< std::vector< unsigned > > &indicesVector)
Same as translateFromData, but the indices are given as a vector, as is the case when collecting indi...
Definition lsPointData.hpp:249
VectorDataType * getVectorData(std::string searchLabel, bool noWarning=false)
Definition lsPointData.hpp:159
ScalarDataType * getScalarData(std::string searchLabel, bool noWarning=false)
Definition lsPointData.hpp:98
const ScalarDataType * getScalarData(std::string searchLabel, bool noWarning=false) const
Definition lsPointData.hpp:112
std::ostream & serialize(std::ostream &stream)
Serialize PointData into a binary stream.
Definition lsPointData.hpp:283
void append(const PointData &passedData)
Append the passed PointData to this one.
Definition lsPointData.hpp:213
std::vector< std::array< T, 3 > > VectorDataType
Definition lsPointData.hpp:23
void insertNextScalarData(ScalarDataType &&scalars, std::string label="Scalars")
insert new scalar data array
Definition lsPointData.hpp:64
unsigned getScalarDataSize() const
get the number of different scalar data arrays saved
Definition lsPointData.hpp:85
VectorDataType * getVectorData(int index)
Definition lsPointData.hpp:151
std::string getScalarDataLabel(int index) const
Definition lsPointData.hpp:135
std::string getVectorDataLabel(int index) const
Definition lsPointData.hpp:196
const VectorDataType * getVectorData(int index) const
Definition lsPointData.hpp:155
int getVectorDataIndex(std::string searchLabel) const
Definition lsPointData.hpp:187
ScalarDataType * getScalarData(int index)
Definition lsPointData.hpp:90
const ScalarDataType * getScalarData(int index) const
Definition lsPointData.hpp:94
unsigned getVectorDataSize() const
get the number of different vector data arrays saved
Definition lsPointData.hpp:88
void insertNextVectorData(VectorDataType &&vectors, std::string label="Vectors")
insert new vector data array
Definition lsPointData.hpp:78
int getScalarDataIndex(std::string searchLabel) const
Definition lsPointData.hpp:126
void insertNextVectorData(const VectorDataType &vectors, std::string label="Vectors")
insert new vector data array
Definition lsPointData.hpp:71
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
void eraseVectorData(int index)
Delete the vector data at index.
Definition lsPointData.hpp:207
#define PRECOMPILE_PRECISION(className)
Definition lsPreCompileMacros.hpp:30
constexpr AssignType assignable
Definition lsConcepts.hpp:10
std::enable_if_t< std::is_floating_point< T >::value, AssignType > IsFloatingPoint
Definition lsConcepts.hpp:23
Definition lsAdvect.hpp:46
double T
Definition pyWrap.cpp:63