abe767ad244363c06fb59d230c2179af97c5901d
[occt.git] / src / BVH / BVH_Properties.lxx
1 // Created on: 2013-12-20
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 2013 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
8 // under the terms of the GNU Lesser General Public 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 // =======================================================================
17 // function : BVH_Transform
18 // purpose  :
19 // =======================================================================
20 template<class T, int N>
21 BVH_Transform<T, N>::BVH_Transform()
22 {
23   //
24 }
25
26 // =======================================================================
27 // function : BVH_Transform
28 // purpose  :
29 // =======================================================================
30 template<class T, int N>
31 BVH_Transform<T, N>::BVH_Transform (const BVH_MatNt& theTransform)
32 : myTransform (theTransform)
33 {
34   //
35 }
36
37 // =======================================================================
38 // function : ~BVH_Transform
39 // purpose  :
40 // =======================================================================
41 template<class T, int N>
42 BVH_Transform<T, N>::~BVH_Transform()
43 {
44   //
45 }
46
47 // =======================================================================
48 // function : Transform
49 // purpose  :
50 // =======================================================================
51 template<class T, int N>
52 const typename BVH_Transform<T, N>::BVH_MatNt& BVH_Transform<T, N>::Transform() const
53 {
54   return myTransform;
55 }
56
57 namespace BVHTools
58 {
59   template<class T, int N> struct MatrixOp
60   {
61     // Not implemented
62   };
63
64   template<class T> struct MatrixOp<T, 4>
65   {
66     typedef typename BVHTools::MatrixType<T, 4>::Type BVH_Mat4t;
67
68     static void Inverse (const BVH_Mat4t& theIn,
69                          BVH_Mat4t&       theOut)
70     {
71       theIn.Inverted (theOut);
72     }
73
74     typedef typename BVHTools::VectorType<T, 4>::Type BVH_Vec4t;
75
76     static BVH_Vec4t Multiply (const BVH_Mat4t& theMat,
77                                const BVH_Vec4t& theVec)
78     {
79       BVH_Vec4t aOut = theMat * theVec;
80       return aOut * static_cast<T> (1.0 / aOut.w());
81     }
82   };
83 }
84
85 // =======================================================================
86 // function : SetTransform
87 // purpose  :
88 // =======================================================================
89 template<class T, int N>
90 void BVH_Transform<T, N>::SetTransform (const BVH_MatNt& theTransform)
91 {
92   myTransform = theTransform;
93   BVHTools::MatrixOp<T, N>::Inverse (myTransform, myTransformInversed);
94 }
95
96 // =======================================================================
97 // function : Inversed
98 // purpose  :
99 // =======================================================================
100 template<class T, int N>
101 const typename BVH_Transform<T, N>::BVH_MatNt& BVH_Transform<T, N>::Inversed() const
102 {
103   return myTransformInversed;
104 }
105
106 namespace BVHTools
107 {
108   template<class T, int N>
109   struct UnitVector
110   {
111     // Not implemented
112   };
113
114   template<class T>
115   struct UnitVector<T, 2>
116   {
117     typedef typename BVHTools::VectorType<T, 2>::Type BVH_Vec2t;
118
119     static BVH_Vec2t DX()
120     {
121       return BVH_Vec2t (static_cast<T> (1.0),
122                         static_cast<T> (0.0));
123     }
124
125     static BVH_Vec2t DY()
126     {
127       return BVH_Vec2t (static_cast<T> (0.0),
128                         static_cast<T> (1.0));
129     }
130
131     static BVH_Vec2t DZ()
132     {
133       return BVH_Vec2t (static_cast<T> (0.0),
134                         static_cast<T> (0.0));
135     }
136   };
137
138   template<class T>
139   struct UnitVector<T, 3>
140   {
141     typedef typename BVHTools::VectorType<T, 3>::Type BVH_Vec3t;
142
143     static BVH_Vec3t DX()
144     {
145       return BVH_Vec3t (static_cast<T> (1.0),
146                         static_cast<T> (0.0),
147                         static_cast<T> (0.0));
148     }
149
150     static BVH_Vec3t DY()
151     {
152       return BVH_Vec3t (static_cast<T> (0.0),
153                         static_cast<T> (1.0),
154                         static_cast<T> (0.0));
155     }
156
157     static BVH_Vec3t DZ()
158     {
159       return BVH_Vec3t (static_cast<T> (0.0),
160                         static_cast<T> (0.0),
161                         static_cast<T> (1.0));
162     }
163   };
164
165   template<class T>
166   struct UnitVector<T, 4>
167   {
168     typedef typename BVHTools::VectorType<T, 4>::Type BVH_Vec4t;
169
170     static BVH_Vec4t DX()
171     {
172       return BVH_Vec4t (static_cast<T> (1.0),
173                         static_cast<T> (0.0),
174                         static_cast<T> (0.0),
175                         static_cast<T> (0.0));
176     }
177
178     static BVH_Vec4t DY()
179     {
180       return BVH_Vec4t (static_cast<T> (0.0),
181                         static_cast<T> (1.0),
182                         static_cast<T> (0.0),
183                         static_cast<T> (0.0));
184     }
185
186     static BVH_Vec4t DZ()
187     {
188       return BVH_Vec4t (static_cast<T> (0.0),
189                         static_cast<T> (0.0),
190                         static_cast<T> (1.0),
191                         static_cast<T> (0.0));
192     }
193   };
194 }
195
196 // =======================================================================
197 // function : Apply
198 // purpose  :
199 // =======================================================================
200 template<class T, int N>
201 BVH_Box<T, N> BVH_Transform<T, N>::Apply (const BVH_Box<T, N>& theBox) const
202 {
203   typename BVH_Box<T, N>::BVH_VecNt aSize = theBox.Size();
204
205   BVH_Box<T, N> aBox;
206   for (Standard_Integer aX = 0; aX <= 1; ++aX)
207   {
208     for (Standard_Integer aY = 0; aY <= 1; ++aY)
209     {
210       for (Standard_Integer aZ = 0; aZ <= 1; ++aZ)
211       {
212         typename BVH_Box<T, N>::BVH_VecNt aCorner = theBox.CornerMin() +
213           BVHTools::UnitVector<T, N>::DX() * aSize * static_cast<T> (aX) +
214           BVHTools::UnitVector<T, N>::DY() * aSize * static_cast<T> (aY) +
215           BVHTools::UnitVector<T, N>::DZ() * aSize * static_cast<T> (aZ);
216
217         aBox.Add (BVHTools::MatrixOp<T, N>::Multiply (myTransform, aCorner));
218       }
219     }
220   }
221
222   return aBox;
223 }