0028368: TKMath, BVH -- Fix invalid tree height in QBVH
[occt.git] / src / BVH / BVH_Types.hxx
CommitLineData
3c4e78f2 1// Created on: 2013-12-20
2// Created by: Denis BOGOLEPOV
d5f74e42 3// Copyright (c) 2013-2014 OPEN CASCADE SAS
3c4e78f2 4//
5// This file is part of Open CASCADE Technology software library.
6//
d5f74e42 7// This library is free software; you can redistribute it and/or modify it under
8// the terms of the GNU Lesser General Public License version 2.1 as published
3c4e78f2 9// by the Free Software Foundation, with special exception defined in the file
10// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11// distribution for complete text of the license and disclaimer of any warranty.
12//
13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
15
16#ifndef _BVH_Types_Header
17#define _BVH_Types_Header
18
19// Use this macro to switch between STL and OCCT vector types
20#define _BVH_USE_STD_VECTOR_
21
679d3878 22#include <vector>
23
24#include <NCollection_Mat4.hxx>
3c4e78f2 25#include <NCollection_Vec2.hxx>
26#include <NCollection_Vec3.hxx>
3c4e78f2 27#include <NCollection_Vector.hxx>
3c4e78f2 28
65578e1c 29// GCC supports shrink function only in C++11 mode
30#if defined(_BVH_USE_STD_VECTOR_) && defined(_MSC_VER) && !defined(__INTEL_COMPILER)
31 #define _STD_VECTOR_SHRINK
32#endif
33
3a7a7013 34namespace BVH
3c4e78f2 35{
679d3878 36 //! Tool class for selecting appropriate vector type (Eigen or NCollection).
37 //! \tparam T Numeric data type
38 //! \tparam N Component number
3c4e78f2 39 template<class T, int N> struct VectorType
40 {
41 // Not implemented
42 };
43
44 template<class T> struct VectorType<T, 1>
45 {
46 typedef T Type;
47 };
48
49 template<class T> struct VectorType<T, 2>
50 {
51 typedef NCollection_Vec2<T> Type;
52 };
53
54 template<class T> struct VectorType<T, 3>
55 {
56 typedef NCollection_Vec3<T> Type;
57 };
58
59 template<class T> struct VectorType<T, 4>
60 {
61 typedef NCollection_Vec4<T> Type;
62 };
63
679d3878 64 //! Tool class for selecting appropriate matrix type (Eigen or NCollection).
65 //! \tparam T Numeric data type
66 //! \tparam N Matrix dimension
3c4e78f2 67 template<class T, int N> struct MatrixType
68 {
69 // Not implemented
70 };
71
72 template<class T> struct MatrixType<T, 4>
73 {
74 typedef NCollection_Mat4<T> Type;
75 };
0ef61b50 76
679d3878 77 //! Tool class for selecting type of array of vectors (STD or NCollection vector).
78 //! \tparam T Numeric data type
79 //! \tparam N Component number
80 template<class T, int N = 1> struct ArrayType
0ef61b50 81 {
679d3878 82 #ifndef _BVH_USE_STD_VECTOR_
83 typedef NCollection_Vector<typename VectorType<T, N>::Type> Type;
84 #else
85 typedef std::vector<typename VectorType<T, N>::Type> Type;
86 #endif
87 };
3c4e78f2 88}
89
90//! 2D vector of integers.
3a7a7013 91typedef BVH::VectorType<Standard_Integer, 2>::Type BVH_Vec2i;
3c4e78f2 92//! 3D vector of integers.
3a7a7013 93typedef BVH::VectorType<Standard_Integer, 3>::Type BVH_Vec3i;
3c4e78f2 94//! 4D vector of integers.
3a7a7013 95typedef BVH::VectorType<Standard_Integer, 4>::Type BVH_Vec4i;
3c4e78f2 96
97//! Array of 2D vectors of integers.
3a7a7013 98typedef BVH::ArrayType<Standard_Integer, 2>::Type BVH_Array2i;
3c4e78f2 99//! Array of 3D vectors of integers.
3a7a7013 100typedef BVH::ArrayType<Standard_Integer, 3>::Type BVH_Array3i;
3c4e78f2 101//! Array of 4D vectors of integers.
3a7a7013 102typedef BVH::ArrayType<Standard_Integer, 4>::Type BVH_Array4i;
3c4e78f2 103
104//! 2D vector of single precision reals.
3a7a7013 105typedef BVH::VectorType<Standard_ShortReal, 2>::Type BVH_Vec2f;
3c4e78f2 106//! 3D vector of single precision reals.
3a7a7013 107typedef BVH::VectorType<Standard_ShortReal, 3>::Type BVH_Vec3f;
3c4e78f2 108//! 4D vector of single precision reals.
3a7a7013 109typedef BVH::VectorType<Standard_ShortReal, 4>::Type BVH_Vec4f;
3c4e78f2 110
111//! Array of 2D vectors of single precision reals.
3a7a7013 112typedef BVH::ArrayType<Standard_ShortReal, 2>::Type BVH_Array2f;
3c4e78f2 113//! Array of 3D vectors of single precision reals.
3a7a7013 114typedef BVH::ArrayType<Standard_ShortReal, 3>::Type BVH_Array3f;
3c4e78f2 115//! Array of 4D vectors of single precision reals.
3a7a7013 116typedef BVH::ArrayType<Standard_ShortReal, 4>::Type BVH_Array4f;
3c4e78f2 117
118//! 2D vector of double precision reals.
3a7a7013 119typedef BVH::VectorType<Standard_Real, 2>::Type BVH_Vec2d;
3c4e78f2 120//! 3D vector of double precision reals.
3a7a7013 121typedef BVH::VectorType<Standard_Real, 3>::Type BVH_Vec3d;
3c4e78f2 122//! 4D vector of double precision reals.
3a7a7013 123typedef BVH::VectorType<Standard_Real, 4>::Type BVH_Vec4d;
3c4e78f2 124
125//! Array of 2D vectors of double precision reals.
3a7a7013 126typedef BVH::ArrayType<Standard_Real, 2>::Type BVH_Array2d;
3c4e78f2 127//! Array of 3D vectors of double precision reals.
3a7a7013 128typedef BVH::ArrayType<Standard_Real, 3>::Type BVH_Array3d;
3c4e78f2 129//! Array of 4D vectors of double precision reals.
3a7a7013 130typedef BVH::ArrayType<Standard_Real, 4>::Type BVH_Array4d;
3c4e78f2 131
132//! 4x4 matrix of single precision reals.
3a7a7013 133typedef BVH::MatrixType<Standard_ShortReal, 4>::Type BVH_Mat4f;
3c4e78f2 134
135//! 4x4 matrix of double precision reals.
3a7a7013 136typedef BVH::MatrixType<Standard_Real, 4>::Type BVH_Mat4d;
3c4e78f2 137
679d3878 138namespace BVH
139{
140 //! Tool class for accessing specific vector component (by index).
141 //! \tparam T Numeric data type
142 //! \tparam N Component number
143 template<class T, int N> struct VecComp
144 {
145 // Not implemented
146 };
147
148 template<class T> struct VecComp<T, 2>
149 {
150 typedef typename BVH::VectorType<T, 2>::Type BVH_Vec2t;
151
152 static T Get (const BVH_Vec2t& theVec, const Standard_Integer theAxis)
153 {
154 return theAxis == 0 ? theVec.x() : theVec.y();
155 }
156 };
157
158 template<class T> struct VecComp<T, 3>
159 {
160 typedef typename BVH::VectorType<T, 3>::Type BVH_Vec3t;
161
162 static T Get (const BVH_Vec3t& theVec, const Standard_Integer theAxis)
163 {
164 return theAxis == 0 ? theVec.x() : ( theAxis == 1 ? theVec.y() : theVec.z() );
165 }
166 };
167
168 template<class T> struct VecComp<T, 4>
169 {
170 typedef typename BVH::VectorType<T, 4>::Type BVH_Vec4t;
171
172 static T Get (const BVH_Vec4t& theVec, const Standard_Integer theAxis)
173 {
174 return theAxis == 0 ? theVec.x() :
175 (theAxis == 1 ? theVec.y() : ( theAxis == 2 ? theVec.z() : theVec.w() ));
176 }
177 };
178
179 //! Tool class providing typical operations on the array. It allows
180 //! for interoperability between STD vector and NCollection vector.
181 //! \tparam T Numeric data type
182 //! \tparam N Component number
183 template<class T, int N = 1> struct Array
184 {
185 typedef typename BVH::ArrayType<T, N>::Type BVH_ArrayNt;
186
65578e1c 187 //! Returns a const reference to the element with the given index.
679d3878 188 static inline const typename BVH::VectorType<T, N>::Type& Value (
189 const BVH_ArrayNt& theArray, const Standard_Integer theIndex)
190 {
191#ifdef _BVH_USE_STD_VECTOR_
192 return theArray[theIndex];
193#else
194 return theArray.Value (theIndex);
195#endif
196 }
197
65578e1c 198 //! Returns a reference to the element with the given index.
679d3878 199 static inline typename BVH::VectorType<T, N>::Type& ChangeValue (
200 BVH_ArrayNt& theArray, const Standard_Integer theIndex)
201 {
202#ifdef _BVH_USE_STD_VECTOR_
203 return theArray[theIndex];
204#else
205 return theArray.ChangeValue (theIndex);
206#endif
207 }
208
65578e1c 209 //! Adds the new element at the end of the array.
679d3878 210 static inline void Append (BVH_ArrayNt& theArray,
211 const typename BVH::VectorType<T, N>::Type& theElement)
212 {
213#ifdef _BVH_USE_STD_VECTOR_
214 theArray.push_back (theElement);
215#else
216 theArray.Append (theElement);
217#endif
218 }
219
65578e1c 220 //! Returns the number of elements in the given array.
679d3878 221 static inline Standard_Integer Size (const BVH_ArrayNt& theArray)
222 {
223#ifdef _BVH_USE_STD_VECTOR_
224 return static_cast<Standard_Integer> (theArray.size());
225#else
226 return static_cast<Standard_Integer> (theArray.Size());
227#endif
228 }
229
65578e1c 230 //! Removes all elements from the given array.
679d3878 231 static inline void Clear (BVH_ArrayNt& theArray)
232 {
233#ifdef _BVH_USE_STD_VECTOR_
234 theArray.clear();
235#else
236 theArray.Clear();
65578e1c 237#endif
238 }
239
240 //! Requests that the array capacity be at least enough to
241 //! contain given number of elements. This function has no
242 //! effect in case of NCollection based array.
243 static inline void Reserve (BVH_ArrayNt& theArray, const Standard_Integer theCount)
244 {
245#ifdef _BVH_USE_STD_VECTOR_
246 if (Size (theArray) == theCount)
247 {
248#ifdef _STD_VECTOR_SHRINK
249 theArray.shrink_to_fit();
250#endif
251 }
252 else
253 {
254 theArray.reserve (theCount);
255 }
256#else
257 // do nothing
679d3878 258#endif
259 }
260 };
261
262 template<class T>
263 static inline Standard_Integer IntFloor (const T theValue)
264 {
265 const Standard_Integer aRes = static_cast<Standard_Integer> (theValue);
266
267 return aRes - static_cast<Standard_Integer> (aRes > theValue);
268 }
269}
3c4e78f2 270
271#endif // _BVH_Types_Header