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>
28 #include <Standard_Type.hxx>
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
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
45 template<class T> struct VectorType<T, 1>
50 template<class T> struct VectorType<T, 2>
52 typedef NCollection_Vec2<T> Type;
55 template<class T> struct VectorType<T, 3>
57 typedef NCollection_Vec3<T> Type;
60 template<class T> struct VectorType<T, 4>
62 typedef NCollection_Vec4<T> Type;
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
73 template<class T> struct MatrixType<T, 4>
75 typedef NCollection_Mat4<T> Type;
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
83 #ifndef _BVH_USE_STD_VECTOR_
84 typedef NCollection_Vector<typename VectorType<T, N>::Type> Type;
86 typedef std::vector<typename VectorType<T, N>::Type> Type;
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;
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;
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;
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;
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;
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;
133 //! 4x4 matrix of single precision reals.
134 typedef BVH::MatrixType<Standard_ShortReal, 4>::Type BVH_Mat4f;
136 //! 4x4 matrix of double precision reals.
137 typedef BVH::MatrixType<Standard_Real, 4>::Type BVH_Mat4d;
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
149 template<class T> struct VecComp<T, 2>
151 typedef typename BVH::VectorType<T, 2>::Type BVH_Vec2t;
153 static T Get (const BVH_Vec2t& theVec, const Standard_Integer theAxis)
155 return theAxis == 0 ? theVec.x() : theVec.y();
159 template<class T> struct VecComp<T, 3>
161 typedef typename BVH::VectorType<T, 3>::Type BVH_Vec3t;
163 static T Get (const BVH_Vec3t& theVec, const Standard_Integer theAxis)
165 return theAxis == 0 ? theVec.x() : ( theAxis == 1 ? theVec.y() : theVec.z() );
169 template<class T> struct VecComp<T, 4>
171 typedef typename BVH::VectorType<T, 4>::Type BVH_Vec4t;
173 static T Get (const BVH_Vec4t& theVec, const Standard_Integer theAxis)
175 return theAxis == 0 ? theVec.x() :
176 (theAxis == 1 ? theVec.y() : ( theAxis == 2 ? theVec.z() : theVec.w() ));
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
186 typedef typename BVH::ArrayType<T, N>::Type BVH_ArrayNt;
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)
192 #ifdef _BVH_USE_STD_VECTOR_
193 return theArray[theIndex];
195 return theArray.Value (theIndex);
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)
203 #ifdef _BVH_USE_STD_VECTOR_
204 return theArray[theIndex];
206 return theArray.ChangeValue (theIndex);
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)
214 #ifdef _BVH_USE_STD_VECTOR_
215 theArray.push_back (theElement);
217 theArray.Append (theElement);
221 //! Returns the number of elements in the given array.
222 static inline Standard_Integer Size (const BVH_ArrayNt& theArray)
224 #ifdef _BVH_USE_STD_VECTOR_
225 return static_cast<Standard_Integer> (theArray.size());
227 return static_cast<Standard_Integer> (theArray.Size());
231 //! Removes all elements from the given array.
232 static inline void Clear (BVH_ArrayNt& theArray)
234 #ifdef _BVH_USE_STD_VECTOR_
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)
246 #ifdef _BVH_USE_STD_VECTOR_
247 if (Size (theArray) == theCount)
249 #ifdef _STD_VECTOR_SHRINK
251 #if(defined(_MSC_VER) && (_MSC_VER < 1600))
252 BVH_ArrayNt aTmpArray(theArray);
253 theArray.swap(aTmpArray);
255 theArray.shrink_to_fit();
262 theArray.reserve (theCount);
271 static inline Standard_Integer IntFloor (const T theValue)
273 const Standard_Integer aRes = static_cast<Standard_Integer> (theValue);
275 return aRes - static_cast<Standard_Integer> (aRes > theValue);
279 #endif // _BVH_Types_Header