0030655: Modeling Data - Provide interfaces for selection of the elements from BVH...
[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   //! Creates empty triangulation.
37   BVH_Triangulation (const opencascade::handle<BVH_Builder<T, N> >& theBuilder)
38   : BVH_PrimitiveSet<T, N> (theBuilder)
39   {
40     //
41   }
42
43   //! Releases resources of triangulation.
44   virtual ~BVH_Triangulation() {}
45
46 public:
47
48   //! Array of vertex coordinates.
49   typename BVH::ArrayType<T, N>::Type Vertices;
50
51   //! Array of indices of triangle vertices.
52   BVH_Array4i Elements;
53
54 public:
55
56   //! Returns total number of triangles.
57   virtual Standard_Integer Size() const Standard_OVERRIDE
58   {
59     return BVH::Array<Standard_Integer, 4>::Size (Elements);
60   }
61
62   //! Returns AABB of entire set of objects.
63   using BVH_PrimitiveSet<T, N>::Box;
64
65   //! Returns AABB of the given triangle.
66   virtual BVH_Box<T, N> Box (const Standard_Integer theIndex) const Standard_OVERRIDE
67   {
68     const BVH_Vec4i& anIndex = BVH::Array<Standard_Integer, 4>::Value (Elements, theIndex);
69
70     const BVH_VecNt& aPoint0 = BVH::Array<T, N>::Value (Vertices, anIndex.x());
71     const BVH_VecNt& aPoint1 = BVH::Array<T, N>::Value (Vertices, anIndex.y());
72     const BVH_VecNt& aPoint2 = BVH::Array<T, N>::Value (Vertices, anIndex.z());
73
74     BVH_VecNt aMinPoint (aPoint0), aMaxPoint (aPoint0);
75
76     BVH::BoxMinMax<T, N>::CwiseMin (aMinPoint, aPoint1);
77     BVH::BoxMinMax<T, N>::CwiseMin (aMinPoint, aPoint2);
78     BVH::BoxMinMax<T, N>::CwiseMax (aMaxPoint, aPoint1);
79     BVH::BoxMinMax<T, N>::CwiseMax (aMaxPoint, aPoint2);
80     return BVH_Box<T, N> (aMinPoint, aMaxPoint);
81   }
82
83   //! Returns centroid position along the given axis.
84   virtual T Center (const Standard_Integer theIndex,
85                     const Standard_Integer theAxis) const Standard_OVERRIDE
86   {
87     const BVH_Vec4i& anIndex = BVH::Array<Standard_Integer, 4>::Value (Elements, theIndex);
88
89     const BVH_VecNt& aPoint0 = BVH::Array<T, N>::Value (Vertices, anIndex.x());
90     const BVH_VecNt& aPoint1 = BVH::Array<T, N>::Value (Vertices, anIndex.y());
91     const BVH_VecNt& aPoint2 = BVH::Array<T, N>::Value (Vertices, anIndex.z());
92     return (BVH::VecComp<T, N>::Get (aPoint0, theAxis) +
93             BVH::VecComp<T, N>::Get (aPoint1, theAxis) +
94             BVH::VecComp<T, N>::Get (aPoint2, theAxis)) * static_cast<T> (1.0 / 3.0);
95   }
96
97   //! Performs transposing the two given triangles in the set.
98   virtual void Swap (const Standard_Integer theIndex1,
99                      const Standard_Integer theIndex2) Standard_OVERRIDE
100   {
101     BVH_Vec4i& anIndices1 = BVH::Array<Standard_Integer, 4>::ChangeValue (Elements, theIndex1);
102     BVH_Vec4i& anIndices2 = BVH::Array<Standard_Integer, 4>::ChangeValue (Elements, theIndex2);
103     std::swap (anIndices1, anIndices2);
104   }
105
106 };
107
108 #endif // _BVH_Triangulation_Header