0026536: Visualization - Ray-tracing engine: improving BVH traverse and fixing textur...
[occt.git] / src / BVH / BVH_Types.hxx
1 // Created on: 2013-12-20
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 2013-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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
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
22 #include <vector>
23
24 #include <NCollection_Mat4.hxx>
25 #include <NCollection_Vec2.hxx>
26 #include <NCollection_Vec3.hxx>
27 #include <NCollection_Vector.hxx>
28
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
34 namespace BVH
35 {
36   //! Tool class for selecting appropriate vector type (Eigen or NCollection).
37   //! \tparam T Numeric data type
38   //! \tparam N Component number
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
64   //! Tool class for selecting appropriate matrix type (Eigen or NCollection).
65   //! \tparam T Numeric data type
66   //! \tparam N Matrix dimension
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   };
76
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
81   {
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   };
88 }
89
90 //! 2D vector of integers.
91 typedef BVH::VectorType<Standard_Integer, 2>::Type BVH_Vec2i;
92 //! 3D vector of integers.
93 typedef BVH::VectorType<Standard_Integer, 3>::Type BVH_Vec3i;
94 //! 4D vector of integers.
95 typedef BVH::VectorType<Standard_Integer, 4>::Type BVH_Vec4i;
96
97 //! Array of 2D vectors of integers.
98 typedef BVH::ArrayType<Standard_Integer, 2>::Type BVH_Array2i;
99 //! Array of 3D vectors of integers.
100 typedef BVH::ArrayType<Standard_Integer, 3>::Type BVH_Array3i;
101 //! Array of 4D vectors of integers.
102 typedef BVH::ArrayType<Standard_Integer, 4>::Type BVH_Array4i;
103
104 //! 2D vector of single precision reals.
105 typedef BVH::VectorType<Standard_ShortReal, 2>::Type BVH_Vec2f;
106 //! 3D vector of single precision reals.
107 typedef BVH::VectorType<Standard_ShortReal, 3>::Type BVH_Vec3f;
108 //! 4D vector of single precision reals.
109 typedef BVH::VectorType<Standard_ShortReal, 4>::Type BVH_Vec4f;
110
111 //! Array of 2D vectors of single precision reals.
112 typedef BVH::ArrayType<Standard_ShortReal, 2>::Type BVH_Array2f;
113 //! Array of 3D vectors of single precision reals.
114 typedef BVH::ArrayType<Standard_ShortReal, 3>::Type BVH_Array3f;
115 //! Array of 4D vectors of single precision reals.
116 typedef BVH::ArrayType<Standard_ShortReal, 4>::Type BVH_Array4f;
117
118 //! 2D vector of double precision reals.
119 typedef BVH::VectorType<Standard_Real, 2>::Type BVH_Vec2d;
120 //! 3D vector of double precision reals.
121 typedef BVH::VectorType<Standard_Real, 3>::Type BVH_Vec3d;
122 //! 4D vector of double precision reals.
123 typedef BVH::VectorType<Standard_Real, 4>::Type BVH_Vec4d;
124
125 //! Array of 2D vectors of double precision reals.
126 typedef BVH::ArrayType<Standard_Real, 2>::Type BVH_Array2d;
127 //! Array of 3D vectors of double precision reals.
128 typedef BVH::ArrayType<Standard_Real, 3>::Type BVH_Array3d;
129 //! Array of 4D vectors of double precision reals.
130 typedef BVH::ArrayType<Standard_Real, 4>::Type BVH_Array4d;
131
132 //! 4x4 matrix of single precision reals.
133 typedef BVH::MatrixType<Standard_ShortReal, 4>::Type BVH_Mat4f;
134
135 //! 4x4 matrix of double precision reals.
136 typedef BVH::MatrixType<Standard_Real, 4>::Type BVH_Mat4d;
137
138 namespace 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
187     //! Returns a const reference to the element with the given index.
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
198     //! Returns a reference to the element with the given index.
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
209     //! Adds the new element at the end of the array.
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
220     //! Returns the number of elements in the given array.
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
230     //! Removes all elements from the given array.
231     static inline void Clear (BVH_ArrayNt& theArray)
232     {
233 #ifdef _BVH_USE_STD_VECTOR_
234       theArray.clear();
235 #else
236       theArray.Clear();
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
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 }
270
271 #endif // _BVH_Types_Header