0030686: Visualization, SelectMgr_ViewerSelector - sorting issues of transformation...
[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 #include <Standard_Type.hxx>
29
30 // GCC supports shrink function only in C++11 mode
31 #if defined(_BVH_USE_STD_VECTOR_) && defined(_MSC_VER) && !defined(__INTEL_COMPILER)
32   #define _STD_VECTOR_SHRINK
33 #endif
34
35 namespace BVH
36 {
37   //! Tool class for selecting appropriate vector type (Eigen or NCollection).
38   //! \tparam T Numeric data type
39   //! \tparam N Component number
40   template<class T, int N> struct VectorType
41   {
42     // Not implemented
43   };
44
45   template<class T> struct VectorType<T, 1>
46   {
47     typedef T Type;
48   };
49
50   template<class T> struct VectorType<T, 2>
51   {
52     typedef NCollection_Vec2<T> Type;
53   };
54
55   template<class T> struct VectorType<T, 3>
56   {
57     typedef NCollection_Vec3<T> Type;
58   };
59
60   template<class T> struct VectorType<T, 4>
61   {
62     typedef NCollection_Vec4<T> Type;
63   };
64
65   //! Tool class for selecting appropriate matrix type (Eigen or NCollection).
66   //! \tparam T Numeric data type
67   //! \tparam N Matrix dimension
68   template<class T, int N> struct MatrixType
69   {
70     // Not implemented
71   };
72
73   template<class T> struct MatrixType<T, 4>
74   {
75     typedef NCollection_Mat4<T> Type;
76   };
77
78   //! Tool class for selecting type of array of vectors (STD or NCollection vector).
79   //! \tparam T Numeric data type
80   //! \tparam N Component number
81   template<class T, int N = 1> struct ArrayType
82   {
83   #ifndef _BVH_USE_STD_VECTOR_
84     typedef NCollection_Vector<typename VectorType<T, N>::Type> Type;
85   #else
86     typedef std::vector<typename VectorType<T, N>::Type> Type;
87   #endif
88   };
89 }
90
91 //! 2D vector of integers.
92 typedef BVH::VectorType<Standard_Integer, 2>::Type BVH_Vec2i;
93 //! 3D vector of integers.
94 typedef BVH::VectorType<Standard_Integer, 3>::Type BVH_Vec3i;
95 //! 4D vector of integers.
96 typedef BVH::VectorType<Standard_Integer, 4>::Type BVH_Vec4i;
97
98 //! Array of 2D vectors of integers.
99 typedef BVH::ArrayType<Standard_Integer, 2>::Type BVH_Array2i;
100 //! Array of 3D vectors of integers.
101 typedef BVH::ArrayType<Standard_Integer, 3>::Type BVH_Array3i;
102 //! Array of 4D vectors of integers.
103 typedef BVH::ArrayType<Standard_Integer, 4>::Type BVH_Array4i;
104
105 //! 2D vector of single precision reals.
106 typedef BVH::VectorType<Standard_ShortReal, 2>::Type BVH_Vec2f;
107 //! 3D vector of single precision reals.
108 typedef BVH::VectorType<Standard_ShortReal, 3>::Type BVH_Vec3f;
109 //! 4D vector of single precision reals.
110 typedef BVH::VectorType<Standard_ShortReal, 4>::Type BVH_Vec4f;
111
112 //! Array of 2D vectors of single precision reals.
113 typedef BVH::ArrayType<Standard_ShortReal, 2>::Type BVH_Array2f;
114 //! Array of 3D vectors of single precision reals.
115 typedef BVH::ArrayType<Standard_ShortReal, 3>::Type BVH_Array3f;
116 //! Array of 4D vectors of single precision reals.
117 typedef BVH::ArrayType<Standard_ShortReal, 4>::Type BVH_Array4f;
118
119 //! 2D vector of double precision reals.
120 typedef BVH::VectorType<Standard_Real, 2>::Type BVH_Vec2d;
121 //! 3D vector of double precision reals.
122 typedef BVH::VectorType<Standard_Real, 3>::Type BVH_Vec3d;
123 //! 4D vector of double precision reals.
124 typedef BVH::VectorType<Standard_Real, 4>::Type BVH_Vec4d;
125
126 //! Array of 2D vectors of double precision reals.
127 typedef BVH::ArrayType<Standard_Real, 2>::Type BVH_Array2d;
128 //! Array of 3D vectors of double precision reals.
129 typedef BVH::ArrayType<Standard_Real, 3>::Type BVH_Array3d;
130 //! Array of 4D vectors of double precision reals.
131 typedef BVH::ArrayType<Standard_Real, 4>::Type BVH_Array4d;
132
133 //! 4x4 matrix of single precision reals.
134 typedef BVH::MatrixType<Standard_ShortReal, 4>::Type BVH_Mat4f;
135
136 //! 4x4 matrix of double precision reals.
137 typedef BVH::MatrixType<Standard_Real, 4>::Type BVH_Mat4d;
138
139 namespace BVH
140 {
141   //! Tool class for accessing specific vector component (by index).
142   //! \tparam T Numeric data type
143   //! \tparam N Component number
144   template<class T, int N> struct VecComp
145   {
146     // Not implemented
147   };
148
149   template<class T> struct VecComp<T, 2>
150   {
151     typedef typename BVH::VectorType<T, 2>::Type BVH_Vec2t;
152
153     static T Get (const BVH_Vec2t& theVec, const Standard_Integer theAxis)
154     {
155       return theAxis == 0 ? theVec.x() : theVec.y();
156     }
157   };
158
159   template<class T> struct VecComp<T, 3>
160   {
161     typedef typename BVH::VectorType<T, 3>::Type BVH_Vec3t;
162
163     static T Get (const BVH_Vec3t& theVec, const Standard_Integer theAxis)
164     {
165       return theAxis == 0 ? theVec.x() : ( theAxis == 1 ? theVec.y() : theVec.z() );
166     }
167   };
168
169   template<class T> struct VecComp<T, 4>
170   {
171     typedef typename BVH::VectorType<T, 4>::Type BVH_Vec4t;
172
173     static T Get (const BVH_Vec4t& theVec, const Standard_Integer theAxis)
174     {
175       return theAxis == 0 ? theVec.x() :
176         (theAxis == 1 ? theVec.y() : ( theAxis == 2 ? theVec.z() : theVec.w() ));
177     }
178   };
179
180   //! Tool class providing typical operations on the array. It allows
181   //! for interoperability between STD vector and NCollection vector.
182   //! \tparam T Numeric data type
183   //! \tparam N Component number
184   template<class T, int N = 1> struct Array
185   {
186     typedef typename BVH::ArrayType<T, N>::Type BVH_ArrayNt;
187
188     //! Returns a const reference to the element with the given index.
189     static inline const typename BVH::VectorType<T, N>::Type& Value (
190         const BVH_ArrayNt& theArray, const Standard_Integer theIndex)
191     {
192 #ifdef _BVH_USE_STD_VECTOR_
193       return theArray[theIndex];
194 #else
195       return theArray.Value (theIndex);
196 #endif
197     }
198
199     //! Returns a reference to the element with the given index.
200     static inline typename BVH::VectorType<T, N>::Type& ChangeValue (
201       BVH_ArrayNt& theArray, const Standard_Integer theIndex)
202     {
203 #ifdef _BVH_USE_STD_VECTOR_
204       return theArray[theIndex];
205 #else
206       return theArray.ChangeValue (theIndex);
207 #endif
208     }
209
210     //! Adds the new element at the end of the array.
211     static inline void Append (BVH_ArrayNt& theArray,
212       const typename BVH::VectorType<T, N>::Type& theElement)
213     {
214 #ifdef _BVH_USE_STD_VECTOR_
215       theArray.push_back (theElement);
216 #else
217       theArray.Append (theElement);
218 #endif
219     }
220
221     //! Returns the number of elements in the given array.
222     static inline Standard_Integer Size (const BVH_ArrayNt& theArray)
223     {
224 #ifdef _BVH_USE_STD_VECTOR_
225       return static_cast<Standard_Integer> (theArray.size());
226 #else
227       return static_cast<Standard_Integer> (theArray.Size());
228 #endif
229     }
230
231     //! Removes all elements from the given array.
232     static inline void Clear (BVH_ArrayNt& theArray)
233     {
234 #ifdef _BVH_USE_STD_VECTOR_
235       theArray.clear();
236 #else
237       theArray.Clear();
238 #endif
239     }
240
241     //! Requests that the array capacity be at least enough to
242     //! contain given number of elements. This function has no
243     //! effect in case of NCollection based array.
244     static inline void Reserve (BVH_ArrayNt& theArray, const Standard_Integer theCount)
245     {
246 #ifdef _BVH_USE_STD_VECTOR_
247       if (Size (theArray) == theCount)
248       {
249 #ifdef _STD_VECTOR_SHRINK
250
251 #if(defined(_MSC_VER) && (_MSC_VER < 1600))
252         BVH_ArrayNt aTmpArray(theArray);
253         theArray.swap(aTmpArray);
254 #else
255         theArray.shrink_to_fit();
256 #endif
257
258 #endif
259       }
260       else
261       {
262         theArray.reserve (theCount);
263       }
264 #else
265       // do nothing
266 #endif
267     }
268   };
269
270   template<class T>
271   static inline Standard_Integer IntFloor (const T theValue)
272   {
273     const Standard_Integer aRes = static_cast<Standard_Integer> (theValue);
274
275     return aRes - static_cast<Standard_Integer> (aRes > theValue);
276   }
277 }
278
279 #endif // _BVH_Types_Header