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