VTK  9.3.0
Object.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
3 #pragma once
4 
5 #include "../RTWrapper.h"
6 
7 #include <VisRTX.h>
8 
9 #include <cstring>
10 #include <cassert>
11 #include <iostream>
12 #include <map>
13 #include <string>
14 #include <vector>
15 
16 namespace RTW
17 {
18 VTK_ABI_NAMESPACE_BEGIN
19  class Data;
20 
21  class Object
22  {
23  public:
25  : dataType(type)
26  {
27  this->AddRef();
28  }
29 
30  virtual ~Object()
31  {
32  // Release all objects
33  for (auto it : this->objectMap.map)
34  if (it.second)
35  it.second->Release();
36  }
37 
38  virtual void Commit() = 0;
39 
40  public:
41  void AddRef()
42  {
43  ++this->refCount;
44  }
45 
46  void Release()
47  {
48  assert(refCount > 0);
49  if (--refCount == 0)
50  {
51  delete this;
52  }
53  }
54 
55  private:
56  int64_t refCount = 0;
57 
58  public:
59  inline void SetString(const std::string& id, const std::string& s)
60  {
61  this->stringMap.Set(id, s);
62  }
63 
64  inline const std::string GetString(const std::vector<std::string>& ids, const std::string& defaultValue = "", bool* found = nullptr) const
65  {
66  return this->stringMap.Get(ids, defaultValue, found);
67  }
68 
69  inline bool GetString(const std::vector<std::string>& ids, std::string* result, const std::string& defaultValue = nullptr)
70  {
71  bool found;
72  *result = this->GetString(ids, defaultValue, &found);
73  return found;
74  }
75 
76  inline void SetBool(const std::string& id, bool b)
77  {
78  this->boolMap.Set(id, b);
79  }
80 
81  virtual void SetObject(const std::string& id, Object* object)
82  {
83  // Check if already exists and release
84  Object* current = this->objectMap.Get({ id }, nullptr);
85  if (current)
86  current->Release();
87 
88  // Set new object and add reference
89  if (object)
90  {
91  this->objectMap.Set(id, object);
92  object->AddRef();
93  }
94  else
95  {
96  this->objectMap.Remove(id);
97  }
98  }
99 
100  template<typename T = Object>
101  inline T* GetObject(const std::vector<std::string>& ids, T* defaultValue = nullptr, bool* found = nullptr) const
102  {
103  return reinterpret_cast<T*>(this->objectMap.Get(ids, reinterpret_cast<Object*>(defaultValue), found));
104  }
105 
106  template<typename T = Object>
107  inline bool GetObject(const std::vector<std::string>& ids, T** result, T* defaultValue = nullptr)
108  {
109  bool found;
110  *result = this->GetObject<T>(ids, defaultValue, &found);
111  return found;
112  }
113 
114  inline void SetInt(const std::string& id, int32_t x)
115  {
116  this->int1Map.Set(id, x);
117  }
118 
119  inline int32_t GetInt(const std::vector<std::string>& ids, int32_t defaultValue = 0, bool* found = nullptr) const
120  {
121  return this->int1Map.Get(ids, defaultValue, found);
122  }
123 
124  inline bool GetInt(const std::vector<std::string>& ids, int32_t* result, int32_t defaultValue = 0)
125  {
126  bool found;
127  *result = this->GetInt(ids, defaultValue, &found);
128  return found;
129  }
130 
131  inline void SetFloat(const std::string& id, float x)
132  {
133  this->float1Map.Set(id, x);
134  }
135 
136  inline float GetFloat(const std::vector<std::string>& ids, float defaultValue = 0.0f, bool* found = nullptr) const
137  {
138  return this->float1Map.Get(ids, defaultValue, found);
139  }
140 
141  inline bool GetFloat(const std::vector<std::string>& ids, float* result, float defaultValue = 0.0f)
142  {
143  bool found;
144  *result = this->GetFloat(ids, defaultValue, &found);
145  return found;
146  }
147 
148  inline void SetVec2i(const std::string& id, int32_t x, int32_t y)
149  {
150  this->int2Map.Set(id, VisRTX::Vec2i(x, y));
151  }
152 
153  inline VisRTX::Vec2i GetVec2i(const std::vector<std::string>& ids, const VisRTX::Vec2i& defaultValue = VisRTX::Vec2i(), bool* found = nullptr) const
154  {
155  return this->int2Map.Get(ids, defaultValue, found);
156  }
157 
158  inline bool GetVec2i(const std::vector<std::string>& ids, VisRTX::Vec2i* result, const VisRTX::Vec2i& defaultValue = VisRTX::Vec2i())
159  {
160  bool found;
161  *result = this->GetVec2i(ids, defaultValue, &found);
162  return found;
163  }
164 
165  inline void SetVec2f(const std::string& id, float x, float y)
166  {
167  this->float2Map.Set(id, VisRTX::Vec2f(x, y));
168  }
169 
170  inline VisRTX::Vec2f GetVec2f(const std::vector<std::string>& ids, const VisRTX::Vec2f& defaultValue = VisRTX::Vec2f(), bool* found = nullptr) const
171  {
172  return this->float2Map.Get(ids, defaultValue, found);
173  }
174 
175  inline bool GetVec2f(const std::vector<std::string>& ids, VisRTX::Vec2f* result, const VisRTX::Vec2f& defaultValue = VisRTX::Vec2f())
176  {
177  bool found;
178  *result = this->GetVec2f(ids, defaultValue, &found);
179  return found;
180  }
181 
182  inline void SetVec3i(const std::string& id, int32_t x, int32_t y, int32_t z)
183  {
184  this->int3Map.Set(id, VisRTX::Vec3i(x, y, z));
185  }
186 
187  inline VisRTX::Vec3i GetVec3i(const std::vector<std::string>& ids, const VisRTX::Vec3i& defaultValue = VisRTX::Vec3i(), bool* found = nullptr) const
188  {
189  return this->int3Map.Get(ids, defaultValue, found);
190  }
191 
192  inline bool GetVec3i(const std::vector<std::string>& ids, VisRTX::Vec3i* result, const VisRTX::Vec3i& defaultValue = VisRTX::Vec3i())
193  {
194  bool found;
195  *result = this->GetVec3i(ids, defaultValue, &found);
196  return found;
197  }
198 
199  inline void SetVec3f(const std::string& id, float x, float y, float z)
200  {
201  this->float3Map.Set(id, VisRTX::Vec3f(x, y, z));
202  }
203 
204  inline VisRTX::Vec3f GetVec3f(const std::vector<std::string>& ids, const VisRTX::Vec3f& defaultValue = VisRTX::Vec3f(), bool* found = nullptr) const
205  {
206  return this->float3Map.Get(ids, defaultValue, found);
207  }
208 
209  inline bool GetVec3f(const std::vector<std::string>& ids, VisRTX::Vec3f* result, const VisRTX::Vec3f& defaultValue = VisRTX::Vec3f())
210  {
211  bool found;
212  *result = this->GetVec3f(ids, defaultValue, &found);
213  return found;
214  }
215 
216  inline void SetVec4f(const std::string& id, float x, float y, float z, float w)
217  {
218  this->float4Map.Set(id, VisRTX::Vec4f(x, y, z, w));
219  }
220 
221  inline VisRTX::Vec4f GetVec4f(const std::vector<std::string>& ids, const VisRTX::Vec4f& defaultValue = VisRTX::Vec4f(), bool* found = nullptr) const
222  {
223  return this->float4Map.Get(ids, defaultValue, found);
224  }
225 
226  inline bool GetVec4f(const std::vector<std::string>& ids, VisRTX::Vec4f* result, const VisRTX::Vec4f& defaultValue = VisRTX::Vec4f())
227  {
228  bool found;
229  *result = this->GetVec4f(ids, defaultValue, &found);
230  return found;
231  }
232 
233  virtual void RemoveParam(const std::string& id)
234  {
235  this->stringMap.Remove(id);
236  this->objectMap.Remove(id);
237  this->int1Map.Remove(id);
238  this->float1Map.Remove(id);
239  this->float2Map.Remove(id);
240  this->int2Map.Remove(id);
241  this->int3Map.Remove(id);
242  this->float3Map.Remove(id);
243  this->float4Map.Remove(id);
244  }
245 
246  public:
247  void PrintAllParameters() const
248  {
249  for (auto it : this->stringMap.map)
250  std::cout << "String: \"" << it.first << "\" -> \"" << it.second << "\"" << std::endl;
251 
252  for (auto it : this->objectMap.map)
253  std::cout << "Object/Data: \"" << it.first << "\"" << std::endl;
254 
255  for (auto it : this->int1Map.map)
256  std::cout << "int1: \"" << it.first << "\" -> " << it.second << std::endl;
257 
258  for (auto it : this->float1Map.map)
259  std::cout << "float1: \"" << it.first << "\" -> " << it.second << std::endl;
260 
261  for (auto it : this->int2Map.map)
262  std::cout << "int2: \"" << it.first << "\" -> (" << it.second.x << ", " << it.second.y << ")" << std::endl;
263 
264  for (auto it : this->float2Map.map)
265  std::cout << "float2: \"" << it.first << "\" -> (" << it.second.x << ", " << it.second.y << ")" << std::endl;
266 
267  for (auto it : this->int3Map.map)
268  std::cout << "int3: \"" << it.first << "\" -> (" << it.second.x << ", " << it.second.y << ", " << it.second.z << ")" << std::endl;
269 
270  for (auto it : this->float3Map.map)
271  std::cout << "float3: \"" << it.first << "\" -> (" << it.second.x << ", " << it.second.y << ", " << it.second.z << ")" << std::endl;
272 
273  for (auto it : this->float4Map.map)
274  std::cout << "float4: \"" << it.first << "\" -> (" << it.second.x << ", " << it.second.y << ", " << it.second.z << ", " << it.second.w << ")" << std::endl;
275  }
276 
277  std::set<std::string> GetAllParameters() const
278  {
279  std::set<std::string> result;
280  for (auto it : this->stringMap.map)
281  result.insert("string " + it.first);
282 
283  for (auto it : this->objectMap.map)
284  result.insert("object " + it.first);
285 
286  for (auto it : this->int1Map.map)
287  result.insert("int1 " + it.first);
288 
289  for (auto it : this->float1Map.map)
290  result.insert("float1 " + it.first);
291 
292  for (auto it : this->int2Map.map)
293  result.insert("int2 " + it.first);
294 
295  for (auto it : this->float2Map.map)
296  result.insert("float2 " + it.first);
297 
298  for (auto it : this->int3Map.map)
299  result.insert("int3 " + it.first);
300 
301  for (auto it : this->float3Map.map)
302  result.insert("float3 " + it.first);
303 
304  for (auto it : this->float4Map.map)
305  result.insert("float4 " + it.first);
306  return result;
307  }
308 
310  {
311  return dataType;
312  }
313 
314  private:
315  template<typename T>
316  class ParameterMap
317  {
318  public:
319  inline void Set(const std::string& id, const T& value)
320  {
321  this->map[id] = value;
322  }
323 
324  inline T Get(const std::vector<std::string>& ids, const T& defaultValueValue, bool* found = nullptr) const
325  {
326  for (const std::string& id : ids)
327  {
328  auto it = this->map.find(id);
329  if (it != this->map.end())
330  {
331  if (found)
332  *found = true;
333  return (*it).second;
334  }
335  }
336 
337  if (found)
338  *found = false;
339  return defaultValueValue;
340  }
341 
342  inline void Remove(const std::string& id)
343  {
344  auto it = this->map.find(id);
345  if (it != this->map.end())
346  this->map.erase(it);
347  }
348 
349  public:
350  std::map<std::string, T> map;
351  };
352 
353  private:
354  ParameterMap<std::string> stringMap;
355  ParameterMap<bool> boolMap;
356  ParameterMap<Object*> objectMap;
357 
358  ParameterMap<int32_t> int1Map;
359  ParameterMap<float> float1Map;
360  ParameterMap<VisRTX::Vec2f> float2Map;
361  ParameterMap<VisRTX::Vec2i> int2Map;
362  ParameterMap<VisRTX::Vec3i> int3Map;
363  ParameterMap<VisRTX::Vec3f> float3Map;
364  ParameterMap<VisRTX::Vec4f> float4Map;
365 
366  RTWDataType dataType;
367  };
368 VTK_ABI_NAMESPACE_END
369 }
RTWDataType
Definition: Types.h:124
@ RTW_OBJECT
Definition: Types.h:135
bool GetVec3i(const std::vector< std::string > &ids, VisRTX::Vec3i *result, const VisRTX::Vec3i &defaultValue=VisRTX::Vec3i())
Definition: Object.h:192
void SetVec4f(const std::string &id, float x, float y, float z, float w)
Definition: Object.h:216
VisRTX::Vec2i GetVec2i(const std::vector< std::string > &ids, const VisRTX::Vec2i &defaultValue=VisRTX::Vec2i(), bool *found=nullptr) const
Definition: Object.h:153
bool GetString(const std::vector< std::string > &ids, std::string *result, const std::string &defaultValue=nullptr)
Definition: Object.h:69
bool GetVec2i(const std::vector< std::string > &ids, VisRTX::Vec2i *result, const VisRTX::Vec2i &defaultValue=VisRTX::Vec2i())
Definition: Object.h:158
RTWDataType GetDataType() const
Definition: Object.h:309
int32_t GetInt(const std::vector< std::string > &ids, int32_t defaultValue=0, bool *found=nullptr) const
Definition: Object.h:119
void SetVec3i(const std::string &id, int32_t x, int32_t y, int32_t z)
Definition: Object.h:182
void SetInt(const std::string &id, int32_t x)
Definition: Object.h:114
const std::string GetString(const std::vector< std::string > &ids, const std::string &defaultValue="", bool *found=nullptr) const
Definition: Object.h:64
void SetVec3f(const std::string &id, float x, float y, float z)
Definition: Object.h:199
void SetVec2f(const std::string &id, float x, float y)
Definition: Object.h:165
bool GetInt(const std::vector< std::string > &ids, int32_t *result, int32_t defaultValue=0)
Definition: Object.h:124
bool GetVec2f(const std::vector< std::string > &ids, VisRTX::Vec2f *result, const VisRTX::Vec2f &defaultValue=VisRTX::Vec2f())
Definition: Object.h:175
void Release()
Definition: Object.h:46
VisRTX::Vec2f GetVec2f(const std::vector< std::string > &ids, const VisRTX::Vec2f &defaultValue=VisRTX::Vec2f(), bool *found=nullptr) const
Definition: Object.h:170
void SetBool(const std::string &id, bool b)
Definition: Object.h:76
virtual void SetObject(const std::string &id, Object *object)
Definition: Object.h:81
std::set< std::string > GetAllParameters() const
Definition: Object.h:277
void SetVec2i(const std::string &id, int32_t x, int32_t y)
Definition: Object.h:148
bool GetObject(const std::vector< std::string > &ids, T **result, T *defaultValue=nullptr)
Definition: Object.h:107
void SetFloat(const std::string &id, float x)
Definition: Object.h:131
T * GetObject(const std::vector< std::string > &ids, T *defaultValue=nullptr, bool *found=nullptr) const
Definition: Object.h:101
void AddRef()
Definition: Object.h:41
VisRTX::Vec3i GetVec3i(const std::vector< std::string > &ids, const VisRTX::Vec3i &defaultValue=VisRTX::Vec3i(), bool *found=nullptr) const
Definition: Object.h:187
bool GetFloat(const std::vector< std::string > &ids, float *result, float defaultValue=0.0f)
Definition: Object.h:141
Object(RTWDataType type=RTW_OBJECT)
Definition: Object.h:24
void PrintAllParameters() const
Definition: Object.h:247
bool GetVec4f(const std::vector< std::string > &ids, VisRTX::Vec4f *result, const VisRTX::Vec4f &defaultValue=VisRTX::Vec4f())
Definition: Object.h:226
virtual void RemoveParam(const std::string &id)
Definition: Object.h:233
virtual void Commit()=0
float GetFloat(const std::vector< std::string > &ids, float defaultValue=0.0f, bool *found=nullptr) const
Definition: Object.h:136
void SetString(const std::string &id, const std::string &s)
Definition: Object.h:59
VisRTX::Vec4f GetVec4f(const std::vector< std::string > &ids, const VisRTX::Vec4f &defaultValue=VisRTX::Vec4f(), bool *found=nullptr) const
Definition: Object.h:221
virtual ~Object()
Definition: Object.h:30
bool GetVec3f(const std::vector< std::string > &ids, VisRTX::Vec3f *result, const VisRTX::Vec3f &defaultValue=VisRTX::Vec3f())
Definition: Object.h:209
VisRTX::Vec3f GetVec3f(const std::vector< std::string > &ids, const VisRTX::Vec3f &defaultValue=VisRTX::Vec3f(), bool *found=nullptr) const
Definition: Object.h:204
Definition: Backend.h:8
@ value
Definition: vtkX3D.h:220
@ type
Definition: vtkX3D.h:516
@ string
Definition: vtkX3D.h:490