VTK  9.1.0
Geometry.h
Go to the documentation of this file.
1#pragma once
2
3#include "../Types.h"
4#include "Material.h"
5
6#include <VisRTX.h>
7#include <cassert>
8
9namespace RTW
10{
11 class Geometry : public Object
12 {
13 friend class World;
14
15 public:
16 Geometry(const std::string& type)
18 {
19 VisRTX::Context* rtx = VisRTX_GetContext();
20
21 if (type == "mesh")
22 this->geometry = rtx->CreateTriangleGeometry();
23 else if (type == "sphere")
24 this->geometry = rtx->CreateSphereGeometry();
25 else if (type == "curve")
26 this->geometry = rtx->CreateCylinderGeometry();
27 else if (type == "isosurfaces")
28 ; // not implemented
29 else
30 {
31 assert(false);
32 }
33 }
34
36 {
37 if(this->geometry)
38 this->geometry->Release();
39 if(this->material)
40 this->material->Release();
41 }
42
43 void Commit() override
44 {
45 if (!this->geometry)
46 return;
47
48 /*
49 * Triangles
50 */
51 if (this->geometry->GetType() == VisRTX::GeometryType::TRIANGLES)
52 {
53 VisRTX::TriangleGeometry* tri = dynamic_cast<VisRTX::TriangleGeometry*>(this->geometry);
54
55 Data* vertex = this->GetObject<Data>({ "vertex.position", "position", "vertex" });
56 Data* index = this->GetObject<Data>({ "index" });
57 if (vertex && index)
58 {
59 uint32_t numTriangles = static_cast<uint32_t>(index->GetNumElements());
60 VisRTX::Vec3ui* triangles = reinterpret_cast<VisRTX::Vec3ui*>(index->GetData());
61 assert(index->GetElementDataType() == RTW_VEC3UI);
62
63 uint32_t numVertices = static_cast<uint32_t>(vertex->GetNumElements());
64 VisRTX::Vec3f* vertices = reinterpret_cast<VisRTX::Vec3f*>(vertex->GetData());
65 assert(vertex->GetElementDataType() == RTW_VEC3F);
66
67 VisRTX::Vec3f* normals = nullptr;
68 Data* normal = this->GetObject<Data>({ "vertex.normal" });
69 if (normal)
70 {
71 normals = reinterpret_cast<VisRTX::Vec3f*>(normal->GetData());
72 assert(normal->GetElementDataType() == RTW_VEC3F);
73 }
74
75
76 tri->SetTriangles(numTriangles, triangles, numVertices, vertices, normals);
77
78
79 Data* color = this->GetObject<Data>({ "vertex.color" });
80 if (color)
81 {
82 VisRTX::Vec4f* colors = reinterpret_cast<VisRTX::Vec4f*>(color->GetData());
83 assert(color->GetElementDataType() == RTW_VEC4F);
84 tri->SetColors(colors);
85 }
86 else
87 {
88 tri->SetColors(nullptr);
89 }
90
91
92 Data* texcoord = GetObject<Data>({ "vertex.texcoord" });
93 if (texcoord)
94 {
95 VisRTX::Vec2f* texcoords = reinterpret_cast<VisRTX::Vec2f*>(texcoord->GetData());
96 assert(texcoord->GetElementDataType() == RTW_VEC2F);
97 tri->SetTexCoords(texcoords);
98 }
99 else
100 {
101 tri->SetTexCoords(nullptr);
102 }
103
104 Data* materialList = GetObject<Data>({ "material" });
105 if (materialList)
106 {
107 assert(materialList->GetElementDataType() == RTW_MATERIAL);
108
109 std::vector<VisRTX::Material*> triangleMaterials;
110 triangleMaterials.resize(numTriangles);
111
112 Material** materials = reinterpret_cast<Material**>(materialList->GetData());
113
114 for (uint32_t i = 0; i < numTriangles; ++i)
115 {
116 Material* materialHandle = materials[i];
117 if (materialHandle)
118 triangleMaterials[i] = materialHandle->material;
119 }
120
121 tri->SetMaterials(triangleMaterials.data());
122 }
123 else
124 {
125 tri->SetMaterials(nullptr);
126 }
127 }
128 else
129 {
130 tri->SetTriangles(0, nullptr, 0, nullptr, nullptr);
131 assert(false);
132 }
133 }
134
135 /*
136 * Spheres
137 */
138 else if (this->geometry->GetType() == VisRTX::GeometryType::SPHERES)
139 {
140 VisRTX::SphereGeometry* sphere = dynamic_cast<VisRTX::SphereGeometry*>(this->geometry);
141
142 Data* spheres = GetObject<Data>({ "sphere.position" });
143 if (spheres)
144 {
145 VisRTX::Vec4f* colors = nullptr;
146 Data* color = GetObject<Data>({ "color" });
147 if (color)
148 {
149 colors = reinterpret_cast<VisRTX::Vec4f*>(color->GetData());
150 assert(color->GetElementDataType() == RTW_VEC4F);
151 sphere->SetColors(reinterpret_cast<VisRTX::Vec4f *>(color->GetData()));
152 }
153 else
154 {
155 sphere->SetColors(nullptr);
156 }
157
158 Data *radii = GetObject<Data>({"sphere.radius"});
159 if (radii)
160 {
161 uint32_t numSpheres = spheres->GetNumElements();
162 sphere->SetSpheres(numSpheres,
163 reinterpret_cast<VisRTX::Vec3f *>(spheres->GetData()),
164 reinterpret_cast<float *>(radii->GetData()));
165 }
166 else
167 {
168 uint32_t numSpheres = spheres->GetNumElements();
169 sphere->SetSpheres(numSpheres,
170 reinterpret_cast<VisRTX::Vec3f *>(spheres->GetData()),
171 nullptr);
172 }
173
174
175 Data* texcoord = GetObject<Data>({ "sphere.texcoord" });
176 if (texcoord)
177 {
178 VisRTX::Vec2f* texcoords = reinterpret_cast<VisRTX::Vec2f*>(texcoord->GetData());
179 assert(texcoord->GetElementDataType() == RTW_VEC2F);
180 sphere->SetTexCoords(texcoords);
181 }
182 else
183 {
184 sphere->SetTexCoords(nullptr);
185 }
186
187 Data* materialList = GetObject<Data>({ "material" });
188 if (materialList)
189 {
190 assert(materialList->GetElementDataType() == RTW_MATERIAL);
191
192 uint32_t numSpheres = spheres->GetNumElements();
193 std::vector<VisRTX::Material*> sphereMaterials;
194 sphereMaterials.resize(numSpheres);
195
196 Material** materials = reinterpret_cast<Material**>(materialList->GetData());
197
198 for (uint32_t i = 0; i < numSpheres; ++i)
199 {
200 Material* materialHandle = materials[i];
201 if (materialHandle)
202 sphereMaterials[i] = materialHandle->material;
203 }
204
205 sphere->SetMaterials(sphereMaterials.data());
206 }
207 else
208 {
209 sphere->SetMaterials(nullptr);
210 }
211 }
212 else
213 {
214 assert(false);
215 }
216
217 float radius;
218 if (this->GetFloat({ "radius" }, &radius))
219 sphere->SetRadius(radius);
220 }
221
222 /*
223 * Cylinders
224 */
225 else if (this->geometry->GetType() == VisRTX::GeometryType::CYLINDERS)
226 {
227 VisRTX::CylinderGeometry* cyl = dynamic_cast<VisRTX::CylinderGeometry*>(this->geometry);
228
229 //Non-scale-array variant
230 Data* cylinders = GetObject<Data>({ "vertex.position" });
231 if (cylinders)
232 {
233 VisRTX::Vec4f* colors = nullptr;
234 Data* color = GetObject<Data>({ "color" });
235 if (color)
236 {
237 colors = reinterpret_cast<VisRTX::Vec4f*>(color->GetData());
238 assert(color->GetElementDataType() == RTW_VEC4F);
239 }
240
241 int32_t bytesPerCylinder = this->GetInt({ "bytes_per_cylinder" }, 24, nullptr);
242 int32_t offsetVertex0 = this->GetInt({ "offset_v0" }, 0, nullptr);
243 int32_t offsetVertex1 = this->GetInt({ "offset_v1" }, 12, nullptr);
244 int32_t offsetRadius = this->GetInt({ "offset_radius" }, -1, nullptr);
245
246 uint32_t numCylinders = cylinders->GetNumElements() * cylinders->GetElementSize() / bytesPerCylinder;
247 cyl->SetCylindersAndColors(numCylinders, cylinders->GetData(), bytesPerCylinder, offsetVertex0, offsetVertex1, offsetRadius, colors);
248
249
250 Data* texcoord = GetObject<Data>({ "vertex.texcoord" });
251 if (texcoord)
252 {
253 VisRTX::Vec2f* texcoords = reinterpret_cast<VisRTX::Vec2f*>(texcoord->GetData());
254 assert(texcoord->GetElementDataType() == RTW_VEC2F);
255 cyl->SetTexCoords(texcoords);
256 }
257 else
258 {
259 cyl->SetTexCoords(nullptr);
260 }
261
262 Data* materialList = GetObject<Data>({ "material" });
263 if (materialList)
264 {
265 assert(materialList->GetElementDataType() == RTW_MATERIAL);
266
267 std::vector<VisRTX::Material*> cylinderMaterials;
268 cylinderMaterials.resize(numCylinders);
269
270 Material** materials = reinterpret_cast<Material**>(materialList->GetData());
271
272 for (uint32_t i = 0; i < numCylinders; ++i)
273 {
274 Material* materialHandle = materials[i];
275 if (materialHandle)
276 cylinderMaterials[i] = materialHandle->material;
277 }
278
279 cyl->SetMaterials(cylinderMaterials.data());
280 }
281 else
282 {
283 cyl->SetMaterials(nullptr);
284 }
285 }
286 else if(cylinders = GetObject<Data>({"vertex.position_radius"}))
287 {
288 //Scale-array variant
289 VisRTX::Vec4f* colors = nullptr;
290 Data* color = GetObject<Data>({ "color" });
291 if (color)
292 {
293 colors = reinterpret_cast<VisRTX::Vec4f*>(color->GetData());
294 assert(color->GetElementDataType() == RTW_VEC4F);
295 }
296
297 int32_t bytesPerCylinder = this->GetInt({ "bytes_per_cylinder" }, 64, nullptr);
298 int32_t offsetVertex0 = this->GetInt({ "offset_v0" }, 0, nullptr);
299 int32_t offsetVertex1 = this->GetInt({ "offset_v1" }, 32, nullptr);
300 int32_t offsetRadius = this->GetInt({ "offset_radius" }, 12, nullptr);
301
302 uint32_t numCylinders = cylinders->GetNumElements() * cylinders->GetElementSize() / bytesPerCylinder;
303 cyl->SetCylindersAndColors(numCylinders, cylinders->GetData(), bytesPerCylinder, offsetVertex0, offsetVertex1, offsetRadius, colors);
304
305
306 Data* texcoord = GetObject<Data>({ "vertex.texcoord" });
307 if (texcoord)
308 {
309 VisRTX::Vec2f* texcoords = reinterpret_cast<VisRTX::Vec2f*>(texcoord->GetData());
310 assert(texcoord->GetElementDataType() == RTW_VEC2F);
311 cyl->SetTexCoords(texcoords);
312 }
313 else
314 {
315 cyl->SetTexCoords(nullptr);
316 }
317
318 Data* materialList = GetObject<Data>({ "material" });
319 if (materialList)
320 {
321 assert(materialList->GetElementDataType() == RTW_MATERIAL);
322
323 std::vector<VisRTX::Material*> cylinderMaterials;
324 cylinderMaterials.resize(numCylinders);
325
326 Material** materials = reinterpret_cast<Material**>(materialList->GetData());
327
328 for (uint32_t i = 0; i < numCylinders; ++i)
329 {
330 Material* materialHandle = materials[i];
331 if (materialHandle)
332 cylinderMaterials[i] = materialHandle->material;
333 }
334
335 cyl->SetMaterials(cylinderMaterials.data());
336 }
337 else
338 {
339 cyl->SetMaterials(nullptr);
340 }
341 }
342 else
343 {
344 assert(false);
345 }
346
347 float radius;
348 if (this->GetFloat({ "radius" }, &radius))
349 cyl->SetRadius(radius);
350 }
351
352 else
353 {
354 assert(false);
355 }
356
357
358 // Set a default material if none is set
359 if (!this->material)
360 {
361 VisRTX::Context* rtx = VisRTX_GetContext();
362 this->geometry->SetMaterial(rtx->CreateBasicMaterial());
363 }
364 }
365
366 void SetMaterial(Material* material)
367 {
368 if (!this->geometry)
369 return;
370
371 // Release current material
372 if (this->material)
373 this->material->Release();
374
375 if (material)
376 {
377 this->geometry->SetMaterial(material->material);
378 this->material = material;
379 this->material->AddRef();
380 }
381 else
382 {
383 this->geometry->SetMaterial(nullptr);
384 this->material = nullptr;
385 }
386 }
387
388 private:
389 VisRTX::Geometry* geometry = nullptr;
390 Material* material = nullptr;
391 };
392}
@ RTW_VEC3F
Definition: Types.h:182
@ RTW_MATERIAL
Definition: Types.h:144
@ RTW_VEC4F
Definition: Types.h:182
@ RTW_VEC3UI
Definition: Types.h:173
@ RTW_VEC2F
Definition: Types.h:182
@ RTW_GEOMETRY
Definition: Types.h:139
Definition: Data.h:10
size_t GetNumElements() const
Definition: Data.h:116
static size_t GetElementSize(RTWDataType type)
Definition: Data.h:12
RTWDataType GetElementDataType() const
Definition: Data.h:136
void * GetData() const
Definition: Data.h:146
Geometry(const std::string &type)
Definition: Geometry.h:16
void SetMaterial(Material *material)
Definition: Geometry.h:366
void Commit() override
Definition: Geometry.h:43
int32_t GetInt(const std::vector< std::string > &ids, int32_t defaultValue=0, bool *found=nullptr) const
Definition: Object.h:116
void Release()
Definition: Object.h:43
void AddRef()
Definition: Object.h:38
float GetFloat(const std::vector< std::string > &ids, float defaultValue=0.0f, bool *found=nullptr) const
Definition: Object.h:133
Definition: Backend.h:6
std::pair< boost::graph_traits< vtkGraph * >::vertex_iterator, boost::graph_traits< vtkGraph * >::vertex_iterator > vertices(vtkGraph *g)