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