9a33e0353321e199e90800db2b0a2aac81027abf
[occt.git] / src / BVH / BVH_Triangulation.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_Triangulation_Header
17 #define _BVH_Triangulation_Header
18
19 #include <BVH_PrimitiveSet.hxx>
20
21 //! Triangulation as an example of BVH primitive set.
22 //! \tparam T Numeric data type
23 //! \tparam N Vector dimension
24 template<class T, int N>
25 class BVH_Triangulation : public BVH_PrimitiveSet<T, N>
26 {
27 public:
28
29   typedef typename BVH::VectorType<T, N>::Type BVH_VecNt;
30
31 public:
32
33   //! Creates empty triangulation.
34   BVH_Triangulation() {}
35
36   //! Releases resources of triangulation.
37   virtual ~BVH_Triangulation() {}
38
39 public:
40
41   //! Array of vertex coordinates.
42   typename BVH::ArrayType<T, N>::Type Vertices;
43
44   //! Array of indices of triangle vertices.
45   BVH_Array4i Elements;
46
47 public:
48
49   //! Returns total number of triangles.
50   virtual Standard_Integer Size() const Standard_OVERRIDE
51   {
52     return BVH::Array<Standard_Integer, 4>::Size (Elements);
53   }
54
55   //! Returns AABB of entire set of objects.
56   using BVH_PrimitiveSet<T, N>::Box;
57
58   //! Returns AABB of the given triangle.
59   virtual BVH_Box<T, N> Box (const Standard_Integer theIndex) const Standard_OVERRIDE
60   {
61     const BVH_Vec4i& anIndex = BVH::Array<Standard_Integer, 4>::Value (Elements, theIndex);
62
63     const BVH_VecNt& aPoint0 = BVH::Array<T, N>::Value (Vertices, anIndex.x());
64     const BVH_VecNt& aPoint1 = BVH::Array<T, N>::Value (Vertices, anIndex.y());
65     const BVH_VecNt& aPoint2 = BVH::Array<T, N>::Value (Vertices, anIndex.z());
66
67     BVH_VecNt aMinPoint (aPoint0), aMaxPoint (aPoint0);
68
69     BVH::BoxMinMax<T, N>::CwiseMin (aMinPoint, aPoint1);
70     BVH::BoxMinMax<T, N>::CwiseMin (aMinPoint, aPoint2);
71     BVH::BoxMinMax<T, N>::CwiseMax (aMaxPoint, aPoint1);
72     BVH::BoxMinMax<T, N>::CwiseMax (aMaxPoint, aPoint2);
73     return BVH_Box<T, N> (aMinPoint, aMaxPoint);
74   }
75
76   //! Returns centroid position along the given axis.
77   virtual T Center (const Standard_Integer theIndex,
78                     const Standard_Integer theAxis) const Standard_OVERRIDE
79   {
80     const BVH_Vec4i& anIndex = BVH::Array<Standard_Integer, 4>::Value (Elements, theIndex);
81
82     const BVH_VecNt& aPoint0 = BVH::Array<T, N>::Value (Vertices, anIndex.x());
83     const BVH_VecNt& aPoint1 = BVH::Array<T, N>::Value (Vertices, anIndex.y());
84     const BVH_VecNt& aPoint2 = BVH::Array<T, N>::Value (Vertices, anIndex.z());
85     return (BVH::VecComp<T, N>::Get (aPoint0, theAxis) +
86             BVH::VecComp<T, N>::Get (aPoint1, theAxis) +
87             BVH::VecComp<T, N>::Get (aPoint2, theAxis)) * static_cast<T> (1.0 / 3.0);
88   }
89
90   //! Performs transposing the two given triangles in the set.
91   virtual void Swap (const Standard_Integer theIndex1,
92                      const Standard_Integer theIndex2) Standard_OVERRIDE
93   {
94     BVH_Vec4i& anIndices1 = BVH::Array<Standard_Integer, 4>::ChangeValue (Elements, theIndex1);
95     BVH_Vec4i& anIndices2 = BVH::Array<Standard_Integer, 4>::ChangeValue (Elements, theIndex2);
96     std::swap (anIndices1, anIndices2);
97   }
98
99 };
100
101 #endif // _BVH_Triangulation_Header