1 // Created on: 2013-12-20
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 2013-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #ifndef _BVH_Types_Header
17 #define _BVH_Types_Header
19 // Use this macro to switch between STL and OCCT vector types
20 #define _BVH_USE_STD_VECTOR_
24 #include <NCollection_Mat4.hxx>
25 #include <NCollection_Vec2.hxx>
26 #include <NCollection_Vec3.hxx>
27 #include <NCollection_Vector.hxx>
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
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
44 template<class T> struct VectorType<T, 1>
49 template<class T> struct VectorType<T, 2>
51 typedef NCollection_Vec2<T> Type;
54 template<class T> struct VectorType<T, 3>
56 typedef NCollection_Vec3<T> Type;
59 template<class T> struct VectorType<T, 4>
61 typedef NCollection_Vec4<T> Type;
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
72 template<class T> struct MatrixType<T, 4>
74 typedef NCollection_Mat4<T> Type;
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
82 #ifndef _BVH_USE_STD_VECTOR_
83 typedef NCollection_Vector<typename VectorType<T, N>::Type> Type;
85 typedef std::vector<typename VectorType<T, N>::Type> Type;
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;
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;
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;
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;
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;
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;
132 //! 4x4 matrix of single precision reals.
133 typedef BVH::MatrixType<Standard_ShortReal, 4>::Type BVH_Mat4f;
135 //! 4x4 matrix of double precision reals.
136 typedef BVH::MatrixType<Standard_Real, 4>::Type BVH_Mat4d;
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
148 template<class T> struct VecComp<T, 2>
150 typedef typename BVH::VectorType<T, 2>::Type BVH_Vec2t;
152 static T Get (const BVH_Vec2t& theVec, const Standard_Integer theAxis)
154 return theAxis == 0 ? theVec.x() : theVec.y();
158 template<class T> struct VecComp<T, 3>
160 typedef typename BVH::VectorType<T, 3>::Type BVH_Vec3t;
162 static T Get (const BVH_Vec3t& theVec, const Standard_Integer theAxis)
164 return theAxis == 0 ? theVec.x() : ( theAxis == 1 ? theVec.y() : theVec.z() );
168 template<class T> struct VecComp<T, 4>
170 typedef typename BVH::VectorType<T, 4>::Type BVH_Vec4t;
172 static T Get (const BVH_Vec4t& theVec, const Standard_Integer theAxis)
174 return theAxis == 0 ? theVec.x() :
175 (theAxis == 1 ? theVec.y() : ( theAxis == 2 ? theVec.z() : theVec.w() ));
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
185 typedef typename BVH::ArrayType<T, N>::Type BVH_ArrayNt;
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)
191 #ifdef _BVH_USE_STD_VECTOR_
192 return theArray[theIndex];
194 return theArray.Value (theIndex);
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)
202 #ifdef _BVH_USE_STD_VECTOR_
203 return theArray[theIndex];
205 return theArray.ChangeValue (theIndex);
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)
213 #ifdef _BVH_USE_STD_VECTOR_
214 theArray.push_back (theElement);
216 theArray.Append (theElement);
220 //! Returns the number of elements in the given array.
221 static inline Standard_Integer Size (const BVH_ArrayNt& theArray)
223 #ifdef _BVH_USE_STD_VECTOR_
224 return static_cast<Standard_Integer> (theArray.size());
226 return static_cast<Standard_Integer> (theArray.Size());
230 //! Removes all elements from the given array.
231 static inline void Clear (BVH_ArrayNt& theArray)
233 #ifdef _BVH_USE_STD_VECTOR_
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)
245 #ifdef _BVH_USE_STD_VECTOR_
246 if (Size (theArray) == theCount)
248 #ifdef _STD_VECTOR_SHRINK
249 theArray.shrink_to_fit();
254 theArray.reserve (theCount);
263 static inline Standard_Integer IntFloor (const T theValue)
265 const Standard_Integer aRes = static_cast<Standard_Integer> (theValue);
267 return aRes - static_cast<Standard_Integer> (aRes > theValue);
271 #endif // _BVH_Types_Header