Blue Brain BioExplorer
MathTypes.h
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 #pragma once
24 
25 #define GLM_FORCE_CTOR_INIT
26 #include <glm/glm.hpp>
27 #define GLM_ENABLE_EXPERIMENTAL
28 #include <glm/ext.hpp>
29 #include <glm/gtx/io.hpp>
30 #include <vector>
31 
34 
35 namespace core
36 {
37 template <typename T>
38 class Box;
39 }
40 namespace staticjson
41 {
42 class ObjectHandler;
43 template <typename U>
44 void init(core::Box<U>*, ObjectHandler*);
45 } // namespace staticjson
46 
47 namespace core
48 {
49 template <class T>
50 class Box
51 {
52 public:
53  using vec = glm::vec<3, T>;
54 
55  Box() = default;
56 
57  Box(const vec& pMin, const vec& pMax)
58  : _min(glm::min(pMin, pMax))
59  , _max(glm::max(pMin, pMax))
60  {
61  }
62  inline bool operator==(const Box<T>& other) const { return _min == other._min && _max == other._max; }
63 
64  inline void merge(const Box<T>& aabb)
65  {
66  _min = glm::min(_min, aabb.getMin());
67  _max = glm::max(_max, aabb.getMax());
68  }
69 
70  inline void merge(const vec& point)
71  {
72  _min = glm::min(_min, point);
73  _max = glm::max(_max, point);
74  }
75 
76  inline void intersect(const Box<T>& aabb)
77  {
78  _min = glm::max(_min, aabb.getMin());
79  _max = glm::min(_max, aabb.getMax());
80  }
81 
82  inline void reset()
83  {
84  _min = vec(std::numeric_limits<T>::max());
85  _max = vec(-std::numeric_limits<T>::max());
86  }
87 
88  inline bool isEmpty() const { return _min.x >= _max.x || _min.y >= _max.y || _min.z >= _max.x; }
89 
90  inline vec getCenter() const { return (_min + _max) * .5; }
91  inline vec getSize() const { return _max - _min; }
92  inline const vec& getMin() const { return _min; }
93  inline const vec& getMax() const { return _max; }
94 
95 #ifdef __INTEL_COMPILER // Workaround for ICC. Make members public
96 public:
97  vec _min{std::numeric_limits<T>::max()};
98  vec _max{-std::numeric_limits<T>::max()};
99 #else
100 private:
101  vec _min{std::numeric_limits<T>::max()};
102  vec _max{-std::numeric_limits<T>::max()};
103 
104  SERIALIZATION_FRIEND(Box<double>)
105  SERIALIZATION_FRIEND(Box<float>)
106 #endif
107 };
108 
109 template <typename T>
110 inline std::ostream& operator<<(std::ostream& os, const Box<T>& aabb)
111 {
112  return os << aabb.getMin() << " - " << aabb.getMax();
113 }
114 
118 using Boxf = Box<float>;
120 
124 using Matrix4d = glm::mat<4, 4, double>;
125 using Matrix4f = glm::mat4;
126 
130 using Vector2i = glm::vec<2, int32_t>;
131 using Vector3i = glm::vec<3, int32_t>;
132 
133 using Vector2ui = glm::vec<2, uint32_t>;
134 using Vector3ui = glm::vec<3, uint32_t>;
135 
136 using Vector2f = glm::vec2;
137 using Vector3f = glm::vec3;
138 using Vector4f = glm::vec4;
139 typedef std::vector<Vector3f> Vector3fs;
140 typedef std::vector<Vector4f> Vector4fs;
141 
142 using Vector2d = glm::vec<2, double>;
143 using Vector3d = glm::vec<3, double>;
144 using Vector4d = glm::vec<4, double>;
145 typedef std::vector<Vector2d> Vector2ds;
146 
147 // Consts
148 const Vector3d UP_VECTOR = {0.0, 1.0, 0.0};
149 
153 using Quaterniond = glm::tquat<double, glm::highp>;
154 
156 {
157  const Vector3d vector = glm::normalize(v);
158  Vector3d upVector = UP_VECTOR;
159  if (glm::abs(glm::dot(vector, upVector)) > 0.999)
160  // Gimble lock
161  upVector = Vector3d(0.0, 0.0, 1.0);
162  return quatLookAtRH(vector, upVector);
163 }
164 } // namespace core
#define SERIALIZATION_FRIEND(type)
Definition: Macros.h:32
glm::vec< 3, T > vec
Definition: MathTypes.h:53
void intersect(const Box< T > &aabb)
Definition: MathTypes.h:76
Box(const vec &pMin, const vec &pMax)
Definition: MathTypes.h:57
Box()=default
vec getCenter() const
Definition: MathTypes.h:90
const vec & getMin() const
Definition: MathTypes.h:92
vec getSize() const
Definition: MathTypes.h:91
void reset()
Definition: MathTypes.h:82
bool operator==(const Box< T > &other) const
Definition: MathTypes.h:62
void merge(const vec &point)
Definition: MathTypes.h:70
const vec & getMax() const
Definition: MathTypes.h:93
void merge(const Box< T > &aabb)
Definition: MathTypes.h:64
bool isEmpty() const
Definition: MathTypes.h:88
Quaterniond safeQuatlookAt(const Vector3d &v)
Definition: MathTypes.h:155
glm::vec3 Vector3f
Definition: MathTypes.h:137
glm::vec< 3, uint32_t > Vector3ui
Definition: MathTypes.h:134
glm::vec2 Vector2f
Definition: MathTypes.h:136
glm::vec< 2, int32_t > Vector2i
Definition: MathTypes.h:130
glm::mat< 4, 4, double > Matrix4d
Definition: MathTypes.h:124
glm::vec< 3, double > Vector3d
Definition: MathTypes.h:143
glm::vec4 Vector4f
Definition: MathTypes.h:138
std::vector< Vector2d > Vector2ds
Definition: MathTypes.h:145
glm::vec< 3, int32_t > Vector3i
Definition: MathTypes.h:131
glm::vec< 2, double > Vector2d
Definition: MathTypes.h:142
std::vector< Vector4f > Vector4fs
Definition: MathTypes.h:140
glm::tquat< double, glm::highp > Quaterniond
Double quaternion.
Definition: MathTypes.h:153
std::ostream & operator<<(std::ostream &os, const Box< T > &aabb)
Definition: MathTypes.h:110
glm::vec< 4, double > Vector4d
Definition: MathTypes.h:144
glm::mat4 Matrix4f
Definition: MathTypes.h:125
const Vector3d UP_VECTOR
Definition: MathTypes.h:148
glm::vec< 2, uint32_t > Vector2ui
Definition: MathTypes.h:133
std::vector< Vector3f > Vector3fs
Definition: MathTypes.h:139
void init(core::Box< U > *, ObjectHandler *)
@ point
Definition: CommonTypes.h:67
@ vector
Definition: CommonTypes.h:68