Blue Brain BioExplorer
OSPRayUtils.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2024, EPFL/Blue Brain Project
3  *
4  * The Blue Brain BioExplorer is a tool for scientists to extract and analyse
5  * scientific data from visualization
6  *
7  * This file is part of Blue Brain BioExplorer <https://github.com/BlueBrain/BioExplorer>
8  *
9  * This library is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU Lesser General Public License version 3.0 as published
11  * by the Free Software Foundation.
12  *
13  * This library is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 
23 #include "OSPRayUtils.h"
24 
29 
30 namespace core
31 {
32 namespace engine
33 {
34 namespace ospray
35 {
36 void toOSPRayProperties(const PropertyMap& object, OSPObject ospObject)
37 {
38  try
39  {
40  for (const auto& prop : object.getProperties())
41  {
42  switch (prop->type)
43  {
45  osphelper::set(ospObject, prop->name.c_str(), static_cast<float>(prop->get<double>()));
46  break;
48  osphelper::set(ospObject, prop->name.c_str(), prop->get<int32_t>());
49  break;
51  osphelper::set(ospObject, prop->name.c_str(), static_cast<int32_t>(prop->get<bool>()));
52  break;
54  osphelper::set(ospObject, prop->name.c_str(), prop->get<std::string>());
55  break;
57  osphelper::set(ospObject, prop->name.c_str(), Vector2f(toGlmVec(prop->get<std::array<double, 2>>())));
58  break;
60  osphelper::set(ospObject, prop->name.c_str(), toGlmVec(prop->get<std::array<int32_t, 2>>()));
61  break;
63  osphelper::set(ospObject, prop->name.c_str(), Vector3f(toGlmVec(prop->get<std::array<double, 3>>())));
64  break;
66  osphelper::set(ospObject, prop->name.c_str(), toGlmVec(prop->get<std::array<int32_t, 3>>()));
67  break;
69  osphelper::set(ospObject, prop->name.c_str(), Vector4f(toGlmVec(prop->get<std::array<double, 4>>())));
70  break;
71  }
72  }
73  }
74  catch (const std::exception& e)
75  {
76  CORE_ERROR("Failed to apply properties for ospObject");
77  }
78 }
79 
80 void toOSPRayProperties(const PropertyObject& object, OSPObject ospObject)
81 {
82  toOSPRayProperties(object.getPropertyMap(), ospObject);
83 }
84 
85 template <typename T, std::size_t N>
86 auto _toStdArray(const ospcommon::vec_t<T, N>& input)
87 {
88  std::array<T, N> array;
89  array.fill(0);
90  std::copy(&input[0], &input[N - 1], array.begin());
91  return array;
92 }
93 
94 void fromOSPRayProperties(PropertyMap& object, ::ospray::ManagedObject& ospObject)
95 {
96  for (const auto& prop : object.getProperties())
97  {
98  switch (prop->type)
99  {
101  prop->set(double(ospObject.getParam1f(prop->name.c_str(), prop->get<double>())));
102  break;
103  case Property::Type::Int:
104  prop->set(ospObject.getParam1i(prop->name.c_str(), prop->get<int32_t>()));
105  break;
107  // FIXME(jonask): When supported by OSPRay use bool
108  // bool's are stored as int within ospray
109  prop->set(static_cast<bool>(ospObject.getParam(prop->name.c_str(), static_cast<int>(prop->get<bool>()))));
110  break;
112  prop->set(ospObject.getParamString(prop->name.c_str(), prop->get<std::string>()));
113  break;
115  prop->set(_toStdArray<double, 2>(
116  ospObject.getParam<::ospcommon::vec2f>(prop->name.c_str(), ::ospcommon::vec2f())));
117  break;
119  prop->set(_toStdArray<int32_t, 2>(
120  ospObject.getParam<::ospcommon::vec2i>(prop->name.c_str(), ::ospcommon::vec2i())));
121  break;
123  prop->set(_toStdArray<double, 3>(
124  ospObject.getParam<::ospcommon::vec3f>(prop->name.c_str(), ::ospcommon::vec3f())));
125  break;
127  prop->set(_toStdArray<int32_t, 3>(
128  ospObject.getParam<::ospcommon::vec3i>(prop->name.c_str(), ::ospcommon::vec3i())));
129  break;
131  prop->set(_toStdArray<double, 4>(
132  ospObject.getParam<::ospcommon::vec4f>(prop->name.c_str(), ::ospcommon::vec4f())));
133  break;
134  }
135  }
136 }
137 
138 ::ospcommon::affine3f transformationToAffine3f(const Transformation& transformation)
139 {
140  // https://stackoverflow.com/a/18436193
141  const auto& quat = transformation.getRotation();
142  const float x = atan2(2 * (quat.w * quat.x + quat.y * quat.z), 1 - 2 * (quat.x * quat.x + quat.y * quat.y));
143  const float y = asin(2 * (quat.w * quat.y - quat.z * quat.x));
144  const float z = atan2(2 * (quat.w * quat.z + quat.x * quat.y), 1 - 2 * (quat.y * quat.y + quat.z * quat.z));
145 
146  ospcommon::affine3f rot{::ospcommon::one};
147  rot = ::ospcommon::affine3f::rotate({1, 0, 0}, x) * rot;
148  rot = ::ospcommon::affine3f::rotate({0, 1, 0}, y) * rot;
149  rot = ::ospcommon::affine3f::rotate({0, 0, 1}, z) * rot;
150 
151  const auto& rotationCenter = transformation.getRotationCenter();
152  const auto& translation = transformation.getTranslation();
153  const auto& scale = transformation.getScale();
154 
155  return ::ospcommon::affine3f::scale({float(scale.x), float(scale.y), float(scale.z)}) *
156  ::ospcommon::affine3f::translate({float(translation.x), float(translation.y), float(translation.z)}) *
157  ::ospcommon::affine3f::translate(
158  {float(rotationCenter.x), float(rotationCenter.y), float(rotationCenter.z)}) *
159  rot *
160  ::ospcommon::affine3f::translate(
161  {float(-rotationCenter.x), float(-rotationCenter.y), float(-rotationCenter.z)});
162 }
163 
164 void addInstance(OSPModel rootModel, OSPModel modelToAdd, const Transformation& transform)
165 {
166  auto affine = transformationToAffine3f(transform);
167  OSPGeometry instance = ospNewInstance(modelToAdd, (osp::affine3f&)affine);
168  ospCommit(instance);
169  ospAddGeometry(rootModel, instance);
170  ospRelease(instance);
171 }
172 
173 void addInstance(OSPModel rootModel, OSPModel modelToAdd, const ::ospcommon::affine3f& affine)
174 {
175  OSPGeometry instance = ospNewInstance(modelToAdd, (osp::affine3f&)affine);
176  ospCommit(instance);
177  ospAddGeometry(rootModel, instance);
178  ospRelease(instance);
179 }
180 
181 namespace osphelper
182 {
183 void set(OSPObject obj, const char* id, const char* s)
184 {
185  ospSetString(obj, id, s);
186 }
187 void set(OSPObject obj, const char* id, const std::string& s)
188 {
189  ospSetString(obj, id, s.c_str());
190 }
191 void set(OSPObject obj, const char* id, float v)
192 {
193  ospSet1f(obj, id, v);
194 }
195 void set(OSPObject obj, const char* id, bool v)
196 {
197  ospSet1b(obj, id, v);
198 }
199 void set(OSPObject obj, const char* id, int32_t v)
200 {
201  ospSet1i(obj, id, v);
202 }
203 void set(OSPObject obj, const char* id, const Vector2f& v)
204 {
205  ospSet2fv(obj, id, glm::value_ptr(v));
206 }
207 void set(OSPObject obj, const char* id, const Vector2i& v)
208 {
209  ospSet2iv(obj, id, glm::value_ptr(v));
210 }
211 void set(OSPObject obj, const char* id, const Vector3f& v)
212 {
213  ospSet3fv(obj, id, glm::value_ptr(v));
214 }
215 void set(OSPObject obj, const char* id, const Vector3i& v)
216 {
217  ospSet3iv(obj, id, glm::value_ptr(v));
218 }
219 void set(OSPObject obj, const char* id, const Vector4f& v)
220 {
221  ospSet4fv(obj, id, glm::value_ptr(v));
222 }
223 } // namespace osphelper
224 } // namespace ospray
225 } // namespace engine
226 } // namespace core
Defines the translation, rotation and scale parameters to be applied to a scene asset.
const Vector3d & getTranslation() const
const Vector3d & getRotationCenter() const
const Vector3d & getScale() const
const Quaterniond & getRotation() const
void set(OSPObject obj, const char *id, const char *s)
void toOSPRayProperties(const PropertyMap &object, OSPObject ospObject)
Definition: OSPRayUtils.cpp:36
void fromOSPRayProperties(PropertyMap &object, ::ospray::ManagedObject &ospObject)
Definition: OSPRayUtils.cpp:94
void addInstance(OSPModel rootModel, OSPModel modelToAdd, const Transformation &transform)
::ospcommon::affine3f transformationToAffine3f(const Transformation &transformation)
auto _toStdArray(const ospcommon::vec_t< T, N > &input)
Definition: OSPRayUtils.cpp:86
glm::vec3 Vector3f
Definition: MathTypes.h:137
glm::vec2 Vector2f
Definition: MathTypes.h:136
glm::vec< 2, int32_t > Vector2i
Definition: MathTypes.h:130
glm::vec4 Vector4f
Definition: MathTypes.h:138
glm::vec< 3, int32_t > Vector3i
Definition: MathTypes.h:131
glm::vec< M, T > toGlmVec(const std::array< T, M > &input)
Definition: Utils.h:37
#define CORE_ERROR(__msg)
Definition: Logs.h:31