15 #ifndef CORE_VISUALIZATION_PARAVIEW_MAPPED_DATA_ARRAY_H_
16 #define CORE_VISUALIZATION_PARAVIEW_MAPPED_DATA_ARRAY_H_
21 #include <vtkMappedDataArray.h>
22 #include <vtkObjectFactory.h>
38 template <
typename TReturn,
typename TClass,
typename TDataMember>
51 2 * std::max<unsigned long long>(
60 return DataType::kArray;
61 }
else if (std::is_same<T, AgentUid>::value) {
62 return DataType::kAgentUid;
64 return DataType::kSoPointer;
66 return DataType::kDefault;
69 template <
typename TTDataMember = TDataMember>
70 typename std::enable_if<GetDataType<TTDataMember>() == DataType::kDefault,
73 auto* casted_agent =
static_cast<TClass*
>(agent);
74 return reinterpret_cast<TDataMember*
>(
75 reinterpret_cast<char*
>(casted_agent) +
dm_offset_);
78 template <
typename TTDataMember = TDataMember>
79 typename std::enable_if<GetDataType<TTDataMember>() == DataType::kArray,
82 auto* casted_agent =
static_cast<TClass*
>(agent);
83 auto* data =
reinterpret_cast<TDataMember*
>(
84 reinterpret_cast<char*
>(casted_agent) +
dm_offset_)
86 return const_cast<TReturn
>(data);
89 template <
typename TTDataMember = TDataMember>
90 typename std::enable_if<GetDataType<TTDataMember>() == DataType::kAgentUid,
93 auto* casted_agent =
static_cast<TClass*
>(agent);
94 auto* data =
reinterpret_cast<TDataMember*
>(
95 reinterpret_cast<char*
>(casted_agent) +
dm_offset_);
103 template <
typename TTDataMember = TDataMember>
104 typename std::enable_if<GetDataType<TTDataMember>() == DataType::kSoPointer,
107 auto* casted_agent =
static_cast<TClass*
>(agent);
108 auto* data =
reinterpret_cast<TDataMember*
>(
109 reinterpret_cast<char*
>(casted_agent) +
dm_offset_);
110 uint64_t uid = data->GetUidAsUint64();
122 virtual void Update(
const std::vector<Agent*>* agents, uint64_t start,
127 template <
typename TScalar,
typename TClass,
typename TDataMember>
132 vtkMappedDataArrayNewInstanceMacro(
137 uint64_t num_components, uint64_t dm_offset);
138 void Update(
const std::vector<Agent*>* agents, uint64_t start,
142 void PrintSelf(ostream& os, vtkIndent indent)
final;
144 void GetTuples(vtkIdList* pt_ids, vtkAbstractArray* output) final;
145 void GetTuples(vtkIdType p1, vtkIdType p2, vtkAbstractArray* output) final;
149 void LookupValue(vtkVariant value, vtkIdList* ids) final;
152 double*
GetTuple(vtkIdType i) final;
153 void GetTuple(vtkIdType i,
double* tuple) final;
163 int Allocate(vtkIdType sz, vtkIdType ext) final;
164 int Resize(vtkIdType num_tuples) final;
166 void SetTuple(vtkIdType i, vtkIdType j, vtkAbstractArray* source) final;
167 void SetTuple(vtkIdType i, const
float* source) final;
168 void SetTuple(vtkIdType i, const
double* source) final;
169 void InsertTuple(vtkIdType i, vtkIdType j, vtkAbstractArray* source) final;
170 void InsertTuple(vtkIdType i, const
float* source) final;
171 void InsertTuple(vtkIdType i, const
double* source) final;
172 void InsertTuples(vtkIdList* dst_ids, vtkIdList* src_ids,
173 vtkAbstractArray* source) final;
174 void InsertTuples(vtkIdType dst_start, vtkIdType n, vtkIdType src_start,
175 vtkAbstractArray* source) final;
176 vtkIdType
InsertNextTuple(vtkIdType j, vtkAbstractArray* source) final;
179 void DeepCopy(vtkAbstractArray* aa) final;
180 void DeepCopy(vtkDataArray* da) final;
182 vtkAbstractArray* source,
double* weights) final;
183 void InterpolateTuple(vtkIdType i, vtkIdType id1, vtkAbstractArray* source1,
184 vtkIdType id2, vtkAbstractArray* source2,
194 void SetValue(vtkIdType idx, TScalar value) final;
224 vtkIdType
Lookup(const TScalar& val, vtkIdType start_index);
229 template <typename TScalar, typename TClass, typename TDataMember>
231 Param::MappedDataArrayMode mode, const
std::
string& name,
232 uint64_t num_components, uint64_t dm_offset) {
235 this->NumberOfComponents = num_components;
236 this->SetName(name.c_str());
239 template <
typename TScalar,
typename TClass,
typename TDataMember>
241 const std::vector<Agent*>* agents, uint64_t start, uint64_t end) {
246 this->Size = this->NumberOfComponents * (end - start);
247 this->MaxId = this->Size - 1;
249 if (this->Size <= 0) {
258 if (
mode_ != Param::MappedDataArrayMode::kZeroCopy) {
259 if (
data_.capacity() < this->Size) {
260 data_.reserve(this->Size * 1.5);
262 if (
mode_ == Param::MappedDataArrayMode::kCopy) {
263 uint64_t counter = 0;
264 for (uint64_t i = start; i < end; ++i) {
266 for (uint64_t c = 0; c < this->NumberOfComponents; ++c) {
267 data_[counter++] = data[c];
273 if (cap < this->Size) {
275 for (uint64_t i = cap; i <
is_matching_.capacity(); ++i) {
286 template <
typename TScalar,
typename TClass,
typename TDataMember>
293 template <
typename TScalar,
typename TClass,
typename TDataMember>
295 ostream& os, vtkIndent indent) {
298 os << indent <<
"agents_: " << this->
agents_ << std::endl;
299 os << indent <<
"start_: " << this->
start_ << std::endl;
300 os << indent <<
"end_: " << this->
end_ << std::endl;
301 os << indent <<
"temp_array_: " << this->
temp_array_ << std::endl;
302 os << indent <<
"mode_: " << this->
mode_ << std::endl;
303 os << indent <<
"match_value_: " << this->
match_value_ << std::endl;
304 os << indent <<
"is_matching_.size(): " << this->
is_matching_.size()
306 os << indent <<
"data_.size(): " << this->
data_.size() << std::endl;
310 template <
typename TScalar,
typename TClass,
typename TDataMember>
314 this->NumberOfComponents = 1;
318 template <
typename TScalar,
typename TClass,
typename TDataMember>
320 vtkIdList* pt_ids, vtkAbstractArray* output) {
321 vtkDataArray* out_array = vtkDataArray::FastDownCast(output);
323 vtkWarningMacro(<<
"Input is not a vtkDataArray");
327 vtkIdType num_tuples = pt_ids->GetNumberOfIds();
329 out_array->SetNumberOfComponents(this->NumberOfComponents);
330 out_array->SetNumberOfTuples(num_tuples);
332 for (vtkIdType i = 0; i < pt_ids->GetNumberOfIds(); ++i) {
333 out_array->SetTuple(i, this->
GetTuple(pt_ids->GetId(i)));
338 template <
typename TScalar,
typename TClass,
typename TDataMember>
340 vtkIdType p1, vtkIdType p2, vtkAbstractArray* output) {
341 vtkDataArray* da = vtkDataArray::FastDownCast(output);
343 vtkErrorMacro(<<
"Input is not a vtkDataArray");
347 if (da->GetNumberOfComponents() != this->GetNumberOfComponents()) {
348 vtkErrorMacro(<<
"Incorrect number of components in input array.");
352 for (vtkIdType
id = 0; p1 <= p2; ++p1) {
353 da->SetTuple(
id++, this->
GetTuple(p1));
358 template <
typename TScalar,
typename TClass,
typename TDataMember>
364 template <
typename TScalar,
typename TClass,
typename TDataMember>
366 vtkErrorMacro(<<
"Not implemented.");
371 template <
typename TScalar,
typename TClass,
typename TDataMember>
375 TScalar val = vtkVariantCast<TScalar>(value, &valid);
377 return this->
Lookup(val, 0);
383 template <
typename TScalar,
typename TClass,
typename TDataMember>
385 vtkVariant value, vtkIdList* ids) {
387 TScalar val = vtkVariantCast<TScalar>(value, &valid);
391 while ((index = this->
Lookup(val, index)) >= 0) {
392 ids->InsertNextId(index++);
398 template <
typename TScalar,
typename TClass,
typename TDataMember>
405 template <
typename TScalar,
typename TClass,
typename TDataMember>
411 template <
typename TScalar,
typename TClass,
typename TDataMember>
418 template <
typename TScalar,
typename TClass,
typename TDataMember>
421 uint64_t idx = tuple_id * this->NumberOfComponents;
424 case Param::MappedDataArrayMode::kCache: {
425 bool all_matches =
true;
426 for (uint64_t i = idx;
427 i < idx + static_cast<uint64_t>(this->NumberOfComponents); ++i) {
432 for (uint64_t i = idx;
433 i < idx + static_cast<uint64_t>(this->NumberOfComponents); ++i) {
439 case Param::MappedDataArrayMode::kZeroCopy: {
441 for (uint64_t i = 0; i < static_cast<uint64_t>(this->NumberOfComponents);
443 tuple[i] =
static_cast<real_t>(temporary_data[i]);
444 if (
mode_ == Param::MappedDataArrayMode::kCache) {
445 data_[idx + i] = temporary_data[i];
451 case Param::MappedDataArrayMode::kCopy:
453 for (uint64_t i = idx;
454 i < idx + static_cast<uint64_t>(this->NumberOfComponents); ++i) {
461 template <
typename TScalar,
typename TClass,
typename TDataMember>
464 return this->
Lookup(value, 0);
468 template <
typename TScalar,
typename TClass,
typename TDataMember>
470 TScalar value, vtkIdList* ids) {
473 while ((index = this->
Lookup(value, index)) >= 0) {
474 ids->InsertNextId(index++);
479 template <
typename TScalar,
typename TClass,
typename TDataMember>
486 case Param::MappedDataArrayMode::kCache:
490 case Param::MappedDataArrayMode::kZeroCopy: {
491 if (this->NumberOfComponents == 1) {
493 if (
mode_ == Param::MappedDataArrayMode::kCache) {
499 const vtkIdType tuple = idx / this->NumberOfComponents;
500 const vtkIdType comp = idx % this->NumberOfComponents;
502 if (
mode_ == Param::MappedDataArrayMode::kCache) {
503 data_[idx] = data[comp];
508 case Param::MappedDataArrayMode::kCopy:
514 template <
typename TScalar,
typename TClass,
typename TDataMember>
518 case Param::MappedDataArrayMode::kCache:
522 case Param::MappedDataArrayMode::kZeroCopy: {
523 if (this->NumberOfComponents == 1) {
525 if (
mode_ == Param::MappedDataArrayMode::kCache) {
531 const vtkIdType tuple = idx / this->NumberOfComponents;
532 const vtkIdType comp = idx % this->NumberOfComponents;
534 if (
mode_ == Param::MappedDataArrayMode::kCache) {
535 data_[idx] = data[comp];
540 case Param::MappedDataArrayMode::kCopy:
546 template <
typename TScalar,
typename TClass,
typename TDataMember>
548 vtkIdType tuple_id, TScalar* tuple)
const {
550 for (uint64_t i = 0; i < static_cast<uint64_t>(this->NumberOfComponents);
557 template <
typename TScalar,
typename TClass,
typename TDataMember>
560 vtkErrorMacro(
"Read only container.");
565 template <
typename TScalar,
typename TClass,
typename TDataMember>
567 vtkErrorMacro(
"Read only container.");
572 template <
typename TScalar,
typename TClass,
typename TDataMember>
575 vtkErrorMacro(
"Read only container.");
580 template <
typename TScalar,
typename TClass,
typename TDataMember>
582 vtkIdType, vtkIdType, vtkAbstractArray*) {
583 vtkErrorMacro(
"Read only container.");
588 template <
typename TScalar,
typename TClass,
typename TDataMember>
591 vtkErrorMacro(
"Read only container.");
596 template <
typename TScalar,
typename TClass,
typename TDataMember>
599 vtkErrorMacro(
"Read only container.");
604 template <
typename TScalar,
typename TClass,
typename TDataMember>
606 vtkIdType, vtkIdType, vtkAbstractArray*) {
607 vtkErrorMacro(
"Read only container.");
612 template <
typename TScalar,
typename TClass,
typename TDataMember>
615 vtkErrorMacro(
"Read only container.");
620 template <
typename TScalar,
typename TClass,
typename TDataMember>
623 vtkErrorMacro(
"Read only container.");
628 template <
typename TScalar,
typename TClass,
typename TDataMember>
630 vtkIdList*, vtkIdList*, vtkAbstractArray*) {
631 vtkErrorMacro(
"Read only container.");
636 template <
typename TScalar,
typename TClass,
typename TDataMember>
638 vtkIdType, vtkIdType, vtkIdType, vtkAbstractArray*) {
639 vtkErrorMacro(
"Read only container.");
644 template <
typename TScalar,
typename TClass,
typename TDataMember>
646 vtkIdType, vtkAbstractArray*) {
647 vtkErrorMacro(
"Read only container.");
652 template <
typename TScalar,
typename TClass,
typename TDataMember>
655 vtkErrorMacro(
"Read only container.");
660 template <
typename TScalar,
typename TClass,
typename TDataMember>
663 vtkErrorMacro(
"Read only container.");
668 template <
typename TScalar,
typename TClass,
typename TDataMember>
671 vtkErrorMacro(
"Read only container.");
676 template <
typename TScalar,
typename TClass,
typename TDataMember>
678 vtkErrorMacro(
"Read only container.");
683 template <
typename TScalar,
typename TClass,
typename TDataMember>
685 vtkIdType, vtkIdList*, vtkAbstractArray*,
double*) {
686 vtkErrorMacro(
"Read only container.");
691 template <
typename TScalar,
typename TClass,
typename TDataMember>
693 vtkIdType, vtkIdType, vtkAbstractArray*, vtkIdType, vtkAbstractArray*,
695 vtkErrorMacro(
"Read only container.");
700 template <
typename TScalar,
typename TClass,
typename TDataMember>
702 vtkIdType, vtkVariant) {
703 vtkErrorMacro(
"Read only container.");
708 template <
typename TScalar,
typename TClass,
typename TDataMember>
710 vtkIdType, vtkVariant) {
711 vtkErrorMacro(
"Read only container.");
716 template <
typename TScalar,
typename TClass,
typename TDataMember>
718 vtkErrorMacro(
"Read only container.");
723 template <
typename TScalar,
typename TClass,
typename TDataMember>
725 vtkErrorMacro(
"Read only container.");
730 template <
typename TScalar,
typename TClass,
typename TDataMember>
732 vtkErrorMacro(
"Read only container.");
737 template <
typename TScalar,
typename TClass,
typename TDataMember>
739 vtkIdType,
const TScalar*) {
740 vtkErrorMacro(
"Read only container.");
745 template <
typename TScalar,
typename TClass,
typename TDataMember>
747 vtkIdType,
const TScalar*) {
748 vtkErrorMacro(
"Read only container.");
753 template <
typename TScalar,
typename TClass,
typename TDataMember>
756 vtkErrorMacro(
"Read only container.");
761 template <
typename TScalar,
typename TClass,
typename TDataMember>
764 vtkErrorMacro(
"Read only container.");
769 template <
typename TScalar,
typename TClass,
typename TDataMember>
772 vtkErrorMacro(
"Read only container.");
777 template <
typename TScalar,
typename TClass,
typename TDataMember>
780 vtkErrorMacro(
"Read only container.");
785 template <
typename TScalar,
typename TClass,
typename TDataMember>
787 this->
temp_array_ =
new double[this->NumberOfComponents];
791 template <
typename TScalar,
typename TClass,
typename TDataMember>
797 template <
typename TScalar,
typename TClass,
typename TDataMember>
799 const TScalar& val, vtkIdType index) {
800 while (index <= this->MaxId) {
810 #endif // CORE_VISUALIZATION_PARAVIEW_MAPPED_DATA_ARRAY_H_