PeriDyno 1.2.1
Loading...
Searching...
No Matches
Field.h
Go to the documentation of this file.
1
16#pragma once
17#include <iostream>
18#include <stdlib.h>
19#include <sstream>
20#include "FBase.h"
21
22#include "Array/Array.h"
23#include "Array/Array2D.h"
24#include "Array/Array3D.h"
25#include "Array/ArrayList.h"
26
27namespace dyno {
32 template<typename T>
33 class FVar : public FBase
34 {
35 public:
36 typedef T VarType;
37 typedef T DataType;
39
40 FVar() : FBase("", "") {}
41 FVar(std::string name, std::string description, FieldTypeEnum fieldType, OBase* parent)
42 : FBase(name, description, fieldType, parent) {}
43
44 FVar(T value, std::string name, std::string description, FieldTypeEnum fieldType, OBase* parent);
45 ~FVar() override;
46
47 const std::string getTemplateName() override { return std::string(typeid(VarType).name()); }
48 const std::string getClassName() override { return "FVar"; }
49
50 uint size() override { return 1; }
51
57 void setValue(T val, bool notify = true);
59
60 inline std::string serialize() override { return "Unknown"; }
61 inline bool deserialize(const std::string& str) override { return false; }
62
63 bool isEmpty() override {
64 return this->constDataPtr() == nullptr;
65 }
66
67 bool connect(FieldType* dst)
68 {
69 this->connectField(dst);
70 return true;
71 }
72
73 bool connect(FBase* dst) override {
74 FieldType* derived = dynamic_cast<FieldType*>(dst);
75 if (derived == nullptr) return false;
76 return this->connect(derived);
77 }
78
80 auto dataPtr = this->constDataPtr();
81 assert(dataPtr != nullptr);
82 return *dataPtr;
83 }
84
85 std::shared_ptr<DataType>& constDataPtr()
86 {
87 FBase* topField = this->getTopField();
88 FieldType* derived = dynamic_cast<FieldType*>(topField);
89 return derived->m_data;
90 }
91
92 private:
93 std::shared_ptr<DataType>& getDataPtr()
94 {
95 FBase* topField = this->getTopField();
96 FieldType* derived = dynamic_cast<FieldType*>(topField);
97 return derived->m_data;
98 }
99
100 std::shared_ptr<DataType> m_data = nullptr;
101 };
102
103 template<typename T>
104 FVar<T>::FVar(T value, std::string name, std::string description, FieldTypeEnum fieldType, OBase* parent)
105 : FBase(name, description, fieldType, parent)
106 {
107 this->setValue(value);
108 }
109
110 template<typename T>
112 {
113 };
114
115 template<typename T>
116 void FVar<T>::setValue(T val, bool notify)
117 {
118 std::shared_ptr<T>& data = this->getDataPtr();
119 if (data == nullptr)
120 {
121 data = std::make_shared<T>(val);
122 }
123 else
124 {
125 *data = val;
126 }
127
128 if(notify && isActive())
129 this->update();
130
131 this->tick();
132 }
133
134
135 template<typename T>
137 {
138 std::shared_ptr<T>& data = this->constDataPtr();
139
140 return *data;
141 }
142
143
144 template<typename T>
146
147 template<typename T>
149
153 template<typename T, DeviceType deviceType>
154 class FArray : public FBase
155 {
156 public:
157 typedef T VarType;
160
162
163 ~FArray() override;
164
165 inline uint size() override {
166 auto ref = this->constDataPtr();
167 return ref == nullptr ? 0 : ref->size();
168 }
169
170 void resize(uint num);
171 void reset();
172
173 void clear();
174
175 void assign(const T& val);
176 void assign(const std::vector<T>& vals);
177#ifndef NO_BACKEND
178 void assign(const DArray<T>& vals);
179#endif
180 void assign(const CArray<T>& vals);
181
182 bool isEmpty() override {
183 return this->size() == 0;
184 }
185 };
186
187 template<typename T, DeviceType deviceType>
189 {
190 if (m_data.use_count() == 1)
191 {
192 m_data->clear();
193 }
194 }
195
196 template<typename T, DeviceType deviceType>
198 {
199 std::shared_ptr<Array<T, deviceType>>& data = this->getDataPtr();
200 if (data == nullptr) {
201 data = std::make_shared<Array<T, deviceType>>();
202 }
203
204 data->resize(num);
205 }
206
207 template<typename T, DeviceType deviceType>
209 {
210 std::shared_ptr<Array<T, deviceType>>& data = this->getDataPtr();
211 if (data == nullptr)
212 {
213 data = std::make_shared<Array<T, deviceType>>();
214 }
215
216 data->assign(val);
217 }
218
219 template<typename T, DeviceType deviceType>
220 void FArray<T, deviceType>::assign(const std::vector<T>& vals)
221 {
222 std::shared_ptr<Array<T, deviceType>>& data = this->getDataPtr();
223 if (data == nullptr)
224 {
225 data = std::make_shared<Array<T, deviceType>>();
226 }
227
228 data->assign(vals);
229 }
230
231 template<typename T, DeviceType deviceType>
233 {
234 std::shared_ptr<Array<T, deviceType>>& data = this->getDataPtr();
235 if (data == nullptr)
236 {
237 data = std::make_shared<Array<T, deviceType>>();
238 }
239
240 data->assign(vals);
241 }
242
243#ifndef NO_BACKEND
244 template<typename T, DeviceType deviceType>
246 {
247 std::shared_ptr<Array<T, deviceType>>& data = this->getDataPtr();
248 if (data == nullptr)
249 {
250 data = std::make_shared<Array<T, deviceType>>();
251 }
252
253 data->assign(vals);
254 }
255#endif
256
257 template<typename T, DeviceType deviceType>
259 {
260 std::shared_ptr<Array<T, deviceType>>& data = this->getDataPtr();
261 if (data == nullptr)
262 {
263 data = std::make_shared<Array<T, deviceType>>();
264 }
265
266 data->reset();
267 }
268
269 template<typename T, DeviceType deviceType>
271 {
272 std::shared_ptr<Array<T, deviceType>>& data = this->getDataPtr();
273 if (data == nullptr)
274 {
275 data = std::make_shared<Array<T, deviceType>>();
276 }
277
278 data->clear();
279 }
280
281 template<typename T>
283
284 template<typename T>
286
290 template<typename T, DeviceType deviceType>
291 class FArray2D : public FBase
292 {
293 public:
294 typedef T VarType;
297
299
300 ~FArray2D() override;
301
302 inline uint size() override {
303 auto ref = this->constDataPtr();
304 return ref == nullptr ? 0 : ref->size();
305 }
306
307 void resize(uint nx, uint ny);
308
309 void reset();
310
311 void clear();
312
313 void assign(const CArray2D<T>& vals);
314 void assign(const DArray2D<T>& vals);
315
316 bool isEmpty() override {
317 return this->constDataPtr() == nullptr;
318 }
319 };
320
321 template<typename T, DeviceType deviceType>
323 {
324 if (m_data.use_count() == 1)
325 {
326 m_data->clear();
327 }
328 }
329
330 template<typename T, DeviceType deviceType>
332 {
333 std::shared_ptr<Array2D<T, deviceType>>& data = this->getDataPtr();
334 if (data == nullptr)
335 {
336 data = std::make_shared<Array2D<T, deviceType>>();
337 }
338
339 data->assign(vals);
340 }
341
342 template<typename T, DeviceType deviceType>
344 {
345 std::shared_ptr<Array2D<T, deviceType>>& data = this->getDataPtr();
346 if (data == nullptr)
347 {
348 data = std::make_shared<Array2D<T, deviceType>>();
349 }
350
351 data->assign(vals);
352 }
353
354 template<typename T, DeviceType deviceType>
356 {
357 std::shared_ptr<Array2D<T, deviceType>>& data = this->getDataPtr();
358 if (data == nullptr)
359 {
360 data = std::make_shared<Array2D<T, deviceType>>();
361 }
362
363 data->reset();
364 }
365
366 template<typename T, DeviceType deviceType>
368 {
369 std::shared_ptr<Array2D<T, deviceType>>& data = this->getDataPtr();
370 if (data == nullptr)
371 {
372 data = std::make_shared<Array2D<T, deviceType>>();
373 }
374
375 data->clear();
376 }
377
378 template<typename T, DeviceType deviceType>
380 {
381 std::shared_ptr<Array2D<T, deviceType>>& data = this->getDataPtr();
382
383 if (data == nullptr) {
384 data = std::make_shared<Array2D<T, deviceType>>();
385 }
386
387 data->resize(nx, ny);
388 }
389
393 template<typename T, DeviceType deviceType>
394 class FArray3D : public FBase
395 {
396 public:
397 typedef T VarType;
400
402
403 ~FArray3D() override;
404
405 inline uint size() override {
406 auto ref = this->constDataPtr();
407 return ref == nullptr ? 0 : ref->size();
408 }
409
410 void resize(const uint nx, const uint ny, const uint nz);
411
412 void reset();
413
414 void clear();
415
416 void assign(const CArray3D<T>& vals);
417 void assign(const DArray3D<T>& vals);
418
419 bool isEmpty() override {
420 return this->constDataPtr() == nullptr;
421 }
422 };
423
424 template<typename T, DeviceType deviceType>
426 {
427 if (m_data.use_count() == 1)
428 {
429 m_data->clear();
430 }
431 }
432
433 template<typename T, DeviceType deviceType>
435 {
436 std::shared_ptr<Array3D<T, deviceType>>& data = this->getDataPtr();
437
438 if (data == nullptr)
439 data = std::make_shared<Array3D<T, deviceType>>();
440
441 data->assign(vals);
442 }
443
444 template<typename T, DeviceType deviceType>
446 {
447 std::shared_ptr<Array3D<T, deviceType>>& data = this->getDataPtr();
448
449 if (data == nullptr)
450 data = std::make_shared<Array3D<T, deviceType>>();
451
452 data->assign(vals);
453 }
454
455 template<typename T, DeviceType deviceType>
457 {
458 std::shared_ptr<Array3D<T, deviceType>>& data = this->getDataPtr();
459
460 if (data == nullptr)
461 data = std::make_shared<Array3D<T, deviceType>>();
462
463 data->reset();
464 }
465
466 template<typename T, DeviceType deviceType>
468 {
469 std::shared_ptr<FArray3D<T, deviceType>>& data = this->getDataPtr();
470 if (data == nullptr)
471 {
472 data = std::make_shared<FArray3D<T, deviceType>>();
473 }
474
475 data->clear();
476 }
477
478 template<typename T, DeviceType deviceType>
479 void FArray3D<T, deviceType>::resize(const uint nx, const uint ny, const uint nz)
480 {
481 std::shared_ptr<Array3D<T, deviceType>>& data = this->getDataPtr();
482
483 if (data == nullptr)
484 data = std::make_shared<Array3D<T, deviceType>>();
485
486 data->resize(nx, ny, nz);
487 }
488
489#ifdef CUDA_BACKEND
493 template<typename T, DeviceType deviceType>
494 class FArrayList : public FBase
495 {
496 public:
497 typedef T VarType;
499 typedef FArrayList<T, deviceType> FieldType;
500
501 DEFINE_FIELD_FUNC(FieldType, DataType, FArrayList);
502
503 ~FArrayList() override;
504
505 inline uint size() override {
506 auto ref = this->constDataPtr();
507 return ref == nullptr ? 0 : ref->size();
508 }
509
510 void resize(uint num);
511
512 void resize(const Array<int, deviceType>& arr);
513
514 void clear();
515
516 void assign(const ArrayList<T, DeviceType::CPU>& src);
517 void assign(const ArrayList<T, DeviceType::GPU>& src);
518
519 bool isEmpty() override {
520 return this->constDataPtr() == nullptr;
521 }
522 };
523
524 template<typename T, DeviceType deviceType>
525 FArrayList<T, deviceType>::~FArrayList()
526 {
527 if (m_data.use_count() == 1)
528 {
529 m_data->clear();
530 }
531 }
532
533 template<typename T, DeviceType deviceType>
534 void FArrayList<T, deviceType>::assign(const ArrayList<T, DeviceType::CPU>& src)
535 {
536 std::shared_ptr<ArrayList<T, deviceType>>& data = this->getDataPtr();
537
538 if (data == nullptr)
539 data = std::make_shared<ArrayList<T, deviceType>>();
540
541 data->assign(src);
542 }
543
544 template<typename T, DeviceType deviceType>
545 void FArrayList<T, deviceType>::assign(const ArrayList<T, DeviceType::GPU>& src)
546 {
547 std::shared_ptr<ArrayList<T, deviceType>>& data = this->getDataPtr();
548
549 if (data == nullptr)
550 data = std::make_shared<ArrayList<T, deviceType>>();
551
552 data->assign(src);
553 }
554
555 template<typename T, DeviceType deviceType>
556 void FArrayList<T, deviceType>::resize(uint num)
557 {
558 std::shared_ptr<ArrayList<T, deviceType>>& data = this->getDataPtr();
559
560 if (data == nullptr)
561 data = std::make_shared<ArrayList<T, deviceType>>();
562
563 data->resize(num);
564 }
565
566 template<typename T, DeviceType deviceType>
567 void FArrayList<T, deviceType>::resize(const Array<int, deviceType>& arr)
568 {
569 std::shared_ptr<ArrayList<T, deviceType>>& data = this->getDataPtr();
570
571 if (data == nullptr)
572 data = std::make_shared<ArrayList<T, deviceType>>();
573
574 data->resize(arr);
575 }
576
577 template<typename T, DeviceType deviceType>
578 void FArrayList<T, deviceType>::clear()
579 {
580 std::shared_ptr<ArrayList<T, deviceType>>& data = this->getDataPtr();
581 if (data == nullptr)
582 {
583 data = std::make_shared<ArrayList<T, deviceType>>();
584 }
585
586 data->clear();
587 }
588#endif
589
590#ifdef VK_BACKEND
591// /**
592// * Define field for Array
593// */
594// template<typename T, DeviceType deviceType>
595// class FArray : public FBase
596// {
597// public:
598// typedef T VarType;
599// typedef Array<T, deviceType> DataType;
600// typedef FArray<T, deviceType> FieldType;
601//
602// DEFINE_FIELD_FUNC(FieldType, DataType, FArray);
603//
604// ~FArray() override;
605//
606// inline uint size() override {
607// auto ref = this->getDataPtr();
608// return ref == nullptr ? 0 : ref->size();
609// }
610//
611// void resize(uint num);
612// void reset();
613// void clear();
614//
615// bool isEmpty() override {
616// return this->size() == 0;
617// }
618// };
619//
620// template<typename T, DeviceType deviceType>
621// FArray<T, deviceType>::~FArray()
622// {
623// if (m_data.use_count() == 1)
624// {
625// m_data->clear();
626// }
627// }
628//
629// template<typename T, DeviceType deviceType>
630// void FArray<T, deviceType>::resize(uint num)
631// {
632// std::shared_ptr<DataType>& data = this->getDataPtr();
633// if (data == nullptr) {
634// data = std::make_shared<DataType>();
635// }
636//
637// data->resize(num);
638// }
639//
640// template<typename T, DeviceType deviceType>
641// void FArray<T, deviceType>::reset()
642// {
643// std::shared_ptr<DataType>& data = this->getDataPtr();
644// if (data == nullptr)
645// {
646// data = std::make_shared<DataType>();
647// }
648//
649// data->reset();
650// }
651//
652// template<typename T, DeviceType deviceType>
653// void FArray<T, deviceType>::clear()
654// {
655// std::shared_ptr<DataType>& data = this->getDataPtr();
656// if (data == nullptr) {
657// data = std::make_shared<DataType>();
658// }
659//
660// data->clear();
661// }
662
663
667 template<typename T, DeviceType deviceType>
668 class FArrayList : public FBase
669 {
670 public:
671 typedef T VarType;
672 typedef ArrayList<T, deviceType> DataType;
673 typedef FArrayList<T, deviceType> FieldType;
674
675 DEFINE_FIELD_FUNC(FieldType, DataType, FArrayList);
676
677 inline uint size() override {
678 auto ref = this->constDataPtr();
679 return ref == nullptr ? 0 : ref->size();
680 }
681
682// void resize(uint num);
683//
684// void resize(const Array<int, deviceType>& arr);
685//
686// void assign(const ArrayList<T, DeviceType::CPU>& src);
687// void assign(const ArrayList<T, DeviceType::GPU>& src);
688
689 bool isEmpty() override {
690 return this->constDataPtr() == nullptr;
691 }
692 };
693
694// template<typename T, DeviceType deviceType>
695// void FArrayList<T, deviceType>::assign(const ArrayList<T, DeviceType::CPU>& src)
696// {
697// std::shared_ptr<ArrayList<T, deviceType>>& data = this->getDataPtr();
698//
699// if (data == nullptr)
700// data = std::make_shared<ArrayList<T, deviceType>>();
701//
702// data->assign(src);
703// }
704//
705// template<typename T, DeviceType deviceType>
706// void FArrayList<T, deviceType>::assign(const ArrayList<T, DeviceType::GPU>& src)
707// {
708// std::shared_ptr<ArrayList<T, deviceType>>& data = this->getDataPtr();
709//
710// if (data == nullptr)
711// data = std::make_shared<ArrayList<T, deviceType>>();
712//
713// data->assign(src);
714// }
715//
716// template<typename T, DeviceType deviceType>
717// void FArrayList<T, deviceType>::resize(uint num)
718// {
719// std::shared_ptr<ArrayList<T, deviceType>>& data = this->getDataPtr();
720//
721// if (data == nullptr)
722// data = std::make_shared<ArrayList<T, deviceType>>();
723//
724// data->resize(num);
725//
726// this->tick();
727// }
728//
729// template<typename T, DeviceType deviceType>
730// void FArrayList<T, deviceType>::resize(const Array<int, deviceType>& arr)
731// {
732// std::shared_ptr<ArrayList<T, deviceType>>& data = this->getDataPtr();
733//
734// if (data == nullptr)
735// data = std::make_shared<ArrayList<T, deviceType>>();
736//
737// data->resize(arr);
738// }
739
740#endif
741}
742
743#include "FSerialization.inl"
#define DEFINE_FIELD_FUNC(DerivedField, Data, FieldName)
Definition FBase.h:209
unsigned int uint
Definition VkReduce.h:5
assert(queueCount >=1)
This class is designed to be elegant, so it can be directly passed to GPU as parameters.
Definition Array.h:27
Array2D< T, deviceType > DataType
Definition Field.h:295
bool isEmpty() override
Definition Field.h:316
uint size() override
Definition Field.h:302
void reset()
Definition Field.h:355
void assign(const CArray2D< T > &vals)
Definition Field.h:343
~FArray2D() override
Definition Field.h:322
FArray2D< T, deviceType > FieldType
Definition Field.h:296
DEFINE_FIELD_FUNC(FieldType, DataType, FArray2D)
void clear()
Definition Field.h:367
void resize(uint nx, uint ny)
Definition Field.h:379
Array3D< T, deviceType > DataType
Definition Field.h:398
FArray3D< T, deviceType > FieldType
Definition Field.h:399
void reset()
Definition Field.h:456
void clear()
Definition Field.h:467
~FArray3D() override
Definition Field.h:425
void resize(const uint nx, const uint ny, const uint nz)
Definition Field.h:479
void assign(const CArray3D< T > &vals)
Definition Field.h:445
bool isEmpty() override
Definition Field.h:419
uint size() override
Definition Field.h:405
DEFINE_FIELD_FUNC(FieldType, DataType, FArray3D)
void resize(uint num)
Definition Field.h:197
void assign(const std::vector< T > &vals)
Definition Field.h:220
void reset()
Definition Field.h:258
bool isEmpty() override
Definition Field.h:182
void clear()
Definition Field.h:270
uint size() override
Definition Field.h:165
FArray< T, deviceType > FieldType
Definition Field.h:159
~FArray() override
Definition Field.h:188
void assign(const CArray< T > &vals)
Definition Field.h:232
void assign(const T &val)
Definition Field.h:208
DEFINE_FIELD_FUNC(FieldType, DataType, FArray)
void assign(const DArray< T > &vals)
Definition Field.h:245
Array< T, deviceType > DataType
Definition Field.h:158
OBase * parent()
Definition FBase.cpp:41
void tick()
Definition FBase.cpp:340
FBase * getTopField()
Definition FBase.cpp:264
bool isActive()
A variable to control the visibility of the field.
Definition FBase.cpp:330
virtual void update()
Definition FBase.cpp:269
bool connectField(FBase *dst)
Definition FBase.cpp:230
bool connect(FBase *dst) override
Definition Field.h:73
const std::string getTemplateName() override
Definition Field.h:47
std::shared_ptr< DataType > & getDataPtr()
Definition Field.h:93
std::shared_ptr< DataType > m_data
Definition Field.h:100
bool isEmpty() override
Definition Field.h:63
std::string serialize() override
Definition Field.h:60
DataType getData()
Definition Field.h:79
const std::string getClassName() override
Definition Field.h:48
FVar< T > FieldType
Definition Field.h:38
T DataType
Definition Field.h:37
~FVar() override
Definition Field.h:111
uint size() override
Definition Field.h:50
bool connect(FieldType *dst)
Definition Field.h:67
T VarType
Definition Field.h:36
bool deserialize(const std::string &str) override
Definition Field.h:61
T getValue()
Definition Field.h:136
FVar(std::string name, std::string description, FieldTypeEnum fieldType, OBase *parent)
Definition Field.h:41
FVar(T value, std::string name, std::string description, FieldTypeEnum fieldType, OBase *parent)
Definition Field.h:104
std::shared_ptr< DataType > & constDataPtr()
Definition Field.h:85
void setValue(T val, bool notify=true)
set the value
Definition Field.h:116
#define T(t)
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
Array< T, DeviceType::GPU > DArray
Definition Array.inl:89
FArray< T, DeviceType::GPU > DeviceArrayField
Definition Field.h:285
Array2D< T, DeviceType::CPU > CArray2D
Definition Array2D.h:131
Array2D< T, DeviceType::GPU > DArray2D
Definition Array2D.inl:90
Array3D< T, DeviceType::GPU > DArray3D
Definition Array3D.inl:90
Array< T, DeviceType::CPU > CArray
Definition Array.h:151
FieldTypeEnum
Definition FBase.h:30
unsigned int uint
Definition VkProgram.h:14
FArray< T, DeviceType::CPU > HostArrayField
Definition Field.h:282
FVar< T > HostVarField
Definition Field.h:145
Array3D< T, DeviceType::CPU > CArray3D
Definition Array3D.h:136
FVar< T > DeviceVarField
Definition Field.h:148