VTK  9.4.20241118
World.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#include "Geometry.h"
7
8#include <VisRTX.h>
9
10#include <set>
11
12namespace RTW
13{
14VTK_ABI_NAMESPACE_BEGIN
15 class World : public Object
16 {
17 friend class Renderer;
18
19 public:
22 {
23 VisRTX::Context* rtx = VisRTX_GetContext();
24 this->model = rtx->CreateModel();
25 }
26
28 {
29 for (Geometry* geometry : this->geometries)
30 if (geometry)
31 geometry->Release();
32 this->model->Release();
33 }
34
35 void Commit() override
36 {
37 }
38
39 void SetObject(const std::string& id, Object *object) override
40 {
41 Object::SetObject(id, object);
42 if(object && object->GetDataType() == RTW_DATA)
43 {
44 Data *data = reinterpret_cast<Data *>(object);
45 if(data->GetElementDataType() == RTW_INSTANCE)
46 {
47 int numElements = data->GetNumElements();
48 Instance **elemData = reinterpret_cast<Instance **>(data->GetData());
49 while(numElements-- > 0)
50 {
51 Object *elem = *elemData;
52 assert(elem->GetDataType() == RTW_INSTANCE);
53 Group *group = reinterpret_cast<Instance *>(elem)->group;
54 std::set<GeometricModel *> &geoModels = group->geometricModels;
55 for(auto &m : geoModels)
56 AddGeometry(m->geometry);
57 elemData++;
58 }
59 }
60 }
61 else if(object && object->GetDataType() == RTW_INSTANCE)
62 {
63 Group *group = reinterpret_cast<Instance *>(object)->group;
64 std::set<GeometricModel *> &geoModels = group->geometricModels;
65 for(auto &m : geoModels)
66 AddGeometry(m->geometry);
67 }
68 }
69
70 void RemoveParam(const std::string& id) override
71 {
72 Object *obj = GetObject({id});
73 if(obj && obj->GetDataType() == RTW_INSTANCE)
74 {
75 Group *group = reinterpret_cast<Instance *>(obj)->group;
76 std::set<GeometricModel *> geoModels = group->geometricModels;
77 for(auto &m : geoModels)
78 RemoveGeometry(m->geometry);
79 }
81 }
82
83 void AddGeometry(Geometry* geometry)
84 {
85 if (!geometry)
86 return;
87
88 // Check if already added
89 auto it = this->geometries.find(geometry);
90 if (it != this->geometries.end())
91 return;
92
93
94 // Add and increase ref count
95 this->model->AddGeometry(geometry->geometry);
96 this->geometries.insert(geometry);
97 geometry->AddRef();
98 }
99
100 void RemoveGeometry(Geometry* geometry)
101 {
102 if (!geometry)
103 return;
104
105 // Check if already added
106 auto it = this->geometries.find(geometry);
107 if (it != this->geometries.end())
108 {
109 this->model->RemoveGeometry(geometry->geometry);
110 this->geometries.erase(geometry);
111 geometry->Release();
112 }
113 }
114
115
116
117 private:
118 //The OSPRay 'world' object roughly corresponds to a VisRTX 'model' object.
119 VisRTX::Model* model = nullptr;
120 std::set<Geometry*> geometries;
121 };
122VTK_ABI_NAMESPACE_END
123}
@ RTW_WORLD
Definition Types.h:154
@ RTW_INSTANCE
Definition Types.h:146
@ RTW_DATA
Definition Types.h:138
RTWDataType GetDataType() const
Definition Object.h:309
void Release()
Definition Object.h:46
virtual void SetObject(const std::string &id, Object *object)
Definition Object.h:81
void AddRef()
Definition Object.h:41
T * GetObject(const std::vector< std::string > &ids, T *defaultValue=nullptr, bool *found=nullptr) const
Definition Object.h:101
virtual void RemoveParam(const std::string &id)
Definition Object.h:233
World()
Definition World.h:20
void AddGeometry(Geometry *geometry)
Definition World.h:83
~World()
Definition World.h:27
void RemoveParam(const std::string &id) override
Definition World.h:70
void Commit() override
Definition World.h:35
void RemoveGeometry(Geometry *geometry)
Definition World.h:100
void SetObject(const std::string &id, Object *object) override
Definition World.h:39
Definition Backend.h:8