VTK  9.1.0
Data.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "../Types.h"
4 
5 #include <cstring>
6 
7 namespace RTW
8 {
9  class Data : public Object
10  {
11  public:
12  static size_t GetElementSize(RTWDataType type)
13  {
14  switch (type)
15  {
16  case RTW_UCHAR:
17  return 1;
18  case RTW_VEC2UC:
19  case RTW_SHORT:
20  case RTW_USHORT:
21  return 2;
22  case RTW_VEC3UC:
23  return 3;
24  case RTW_VEC4UC:
25  case RTW_INT:
26  case RTW_UINT:
27  case RTW_FLOAT:
28  return 4;
29  case RTW_VEC2I:
30  case RTW_VEC2UI:
31  case RTW_VEC2F:
32  case RTW_DOUBLE:
33  return 8;
34  case RTW_VEC3I:
35  case RTW_VEC3UI:
36  case RTW_VEC3F:
37  return 12;
38  case RTW_VEC4I:
39  case RTW_VEC4UI:
40  case RTW_VEC4F:
41  return 16;
42 
43  default:
44  if(type >= RTW_OBJECT && type <= RTW_WORLD)
45  return sizeof(Object *);
46  else return 0;
47  }
48  }
49 
50  public:
51 
52  Data(const void *source, RTWDataType type, size_t width, bool shared = false)
53  : Data(source, type, width, 1, 1, shared) {};
54 
55  Data(const void *source, RTWDataType type, size_t width, size_t height, bool shared = false)
56  : Data(source, type, width, height, 1, shared) {};
57 
58  Data(const void *source, RTWDataType type, size_t width, size_t height, size_t depth, bool shared = false)
59  : Object(RTW_DATA)
60  {
61  this->width = width;
62  this->height = height;
63  this->depth = depth;
64  this->type = type;
65  this->elementSize = GetElementSize(type);
66  this->shared = shared;
67 
68  if (this->shared)
69  {
70  this->data = reinterpret_cast<uint8_t*>(const_cast<void*>(source));
71  }
72  else
73  {
74  size_t size = GetNumElements() * this->elementSize;
75  this->data = new uint8_t[size];
76  memcpy(this->data, source, size);
77  }
78 
79  // Increase references
80  if(type >= RTW_OBJECT && type <= RTW_WORLD)
81  {
82  for (size_t i = 0; i < GetNumElements(); ++i)
83  {
84  Object* obj = reinterpret_cast<Object**>(this->data)[i];
85  if (obj)
86  obj->AddRef();
87  }
88  }
89 
90  this->dirty = true;
91  }
92 
94  {
95  // Release references
96  if(type >= RTW_OBJECT && type <= RTW_WORLD)
97  {
98  for (size_t i = 0; i < GetNumElements(); ++i)
99  {
100  Object* obj = reinterpret_cast<Object**>(this->data)[i];
101  if (obj)
102  obj->Release();
103  }
104  }
105 
106  if (!this->shared)
107  delete[] this->data;
108  }
109 
110  void Commit() override
111  {
112  // Committing data marks it as dirty (useful for shared memory)
113  this->dirty = true;
114  }
115 
116  size_t GetNumElements() const
117  {
118  return this->width * this->height * this->depth;
119  }
120 
121  size_t GetWidth() const
122  {
123  return this->width;
124  }
125 
126  size_t GetHeight() const
127  {
128  return this->height;
129  }
130 
131  size_t GetDepth() const
132  {
133  return this->depth;
134  }
135 
137  {
138  return this->type;
139  }
140 
141  size_t GetElementSize() const
142  {
143  return GetElementSize(this->type);
144  }
145 
146  void* GetData() const
147  {
148  return reinterpret_cast<void*>(this->data);
149  }
150 
151  bool IsShared() const
152  {
153  return this->shared;
154  }
155 
156  bool CheckDirty()
157  {
158  bool d = this->dirty;
159  this->dirty = false;
160  return d;
161  }
162 
163  private:
164  size_t width = 0, height = 1, depth = 1;
165  RTWDataType type;
166  size_t elementSize = 0;
167  uint8_t* data = nullptr;
168  bool shared = false;
169  bool dirty = true;
170  };
171 }
RTWDataType
Definition: Types.h:120
@ RTW_VEC3F
Definition: Types.h:182
@ RTW_FLOAT
Definition: Types.h:182
@ RTW_UINT
Definition: Types.h:173
@ RTW_VEC4F
Definition: Types.h:182
@ RTW_VEC2I
Definition: Types.h:170
@ RTW_WORLD
Definition: Types.h:150
@ RTW_INT
Definition: Types.h:170
@ RTW_USHORT
Definition: Types.h:167
@ RTW_VEC2UC
Definition: Types.h:159
@ RTW_VEC3I
Definition: Types.h:170
@ RTW_VEC3UI
Definition: Types.h:173
@ RTW_VEC2F
Definition: Types.h:182
@ RTW_VEC4UC
Definition: Types.h:159
@ RTW_VEC3UC
Definition: Types.h:159
@ RTW_UCHAR
Definition: Types.h:159
@ RTW_SHORT
Definition: Types.h:164
@ RTW_VEC4I
Definition: Types.h:170
@ RTW_VEC2UI
Definition: Types.h:173
@ RTW_DOUBLE
Definition: Types.h:185
@ RTW_OBJECT
Definition: Types.h:131
@ RTW_VEC4UI
Definition: Types.h:173
@ RTW_DATA
Definition: Types.h:134
Definition: Data.h:10
size_t GetHeight() const
Definition: Data.h:126
size_t GetNumElements() const
Definition: Data.h:116
bool IsShared() const
Definition: Data.h:151
static size_t GetElementSize(RTWDataType type)
Definition: Data.h:12
void * GetData() const
Definition: Data.h:146
size_t GetDepth() const
Definition: Data.h:131
Data(const void *source, RTWDataType type, size_t width, bool shared=false)
Definition: Data.h:52
RTWDataType GetElementDataType() const
Definition: Data.h:136
void Commit() override
Definition: Data.h:110
Data(const void *source, RTWDataType type, size_t width, size_t height, bool shared=false)
Definition: Data.h:55
bool CheckDirty()
Definition: Data.h:156
size_t GetElementSize() const
Definition: Data.h:141
~Data()
Definition: Data.h:93
Data(const void *source, RTWDataType type, size_t width, size_t height, size_t depth, bool shared=false)
Definition: Data.h:58
size_t GetWidth() const
Definition: Data.h:121
void Release()
Definition: Object.h:43
void AddRef()
Definition: Object.h:38
Definition: Backend.h:6
@ size
Definition: vtkX3D.h:259
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)