0028789: Visualization, TKV3d - extend API for accessing and assigning BVH builders
[occt.git] / src / BVH / BVH_Properties.hxx
CommitLineData
3c4e78f2 1// Created on: 2013-12-20
2// Created by: Denis BOGOLEPOV
d5f74e42 3// Copyright (c) 2013-2014 OPEN CASCADE SAS
3c4e78f2 4//
5// This file is part of Open CASCADE Technology software library.
6//
d5f74e42 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
3c4e78f2 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_Properties_Header
17#define _BVH_Properties_Header
18
19#include <BVH_Box.hxx>
20
21#include <Standard_Macro.hxx>
22
23//! Abstract properties of geometric object.
f5b72419 24class BVH_Properties : public Standard_Transient
3c4e78f2 25{
f5b72419 26 DEFINE_STANDARD_RTTIEXT(BVH_Properties, Standard_Transient)
3c4e78f2 27public:
28
29 //! Releases resources of object properties.
30 Standard_EXPORT virtual ~BVH_Properties() = 0;
31
32};
33
34//! Stores transform properties of geometric object.
35template<class T, int N>
36class BVH_Transform : public BVH_Properties
37{
38public:
39
40 //! Type of transformation matrix.
3a7a7013 41 typedef typename BVH::MatrixType<T, N>::Type BVH_MatNt;
3c4e78f2 42
43public:
44
45 //! Creates new identity transformation.
e28f12b3 46 BVH_Transform() {}
3c4e78f2 47
48 //! Creates new transformation with specified matrix.
e28f12b3 49 BVH_Transform (const BVH_MatNt& theTransform) : myTransform (theTransform) {}
3c4e78f2 50
51 //! Releases resources of transformation properties.
e28f12b3 52 virtual ~BVH_Transform() {}
3c4e78f2 53
54 //! Returns transformation matrix.
e28f12b3 55 const BVH_MatNt& Transform() const { return myTransform; }
3c4e78f2 56
57 //! Sets new transformation matrix.
58 void SetTransform (const BVH_MatNt& theTransform);
59
60 //! Returns inversed transformation matrix.
e28f12b3 61 const BVH_MatNt& Inversed() const { return myTransformInversed; }
3c4e78f2 62
63 //! Applies transformation matrix to bounding box.
64 BVH_Box<T, N> Apply (const BVH_Box<T, N>& theBox) const;
65
66protected:
67
68 BVH_MatNt myTransform; //!< Transformation matrix
69 BVH_MatNt myTransformInversed; //!< Inversed transformation matrix
70
71};
72
e28f12b3 73namespace BVH
74{
75 template<class T, int N> struct MatrixOp
76 {
77 // Not implemented
78 };
79
80 template<class T> struct MatrixOp<T, 4>
81 {
82 typedef typename BVH::MatrixType<T, 4>::Type BVH_Mat4t;
83
84 static void Inverse (const BVH_Mat4t& theIn,
85 BVH_Mat4t& theOut)
86 {
87 theIn.Inverted (theOut);
88 }
89
90 typedef typename BVH::VectorType<T, 4>::Type BVH_Vec4t;
91
92 static BVH_Vec4t Multiply (const BVH_Mat4t& theMat,
93 const BVH_Vec4t& theVec)
94 {
95 BVH_Vec4t aOut = theMat * theVec;
96 return aOut * static_cast<T> (1.0 / aOut.w());
97 }
98 };
99
100 template<class T, int N>
101 struct UnitVector
102 {
103 // Not implemented
104 };
105
106 template<class T>
107 struct UnitVector<T, 2>
108 {
109 typedef typename BVH::VectorType<T, 2>::Type BVH_Vec2t;
110 static BVH_Vec2t DX() { return BVH_Vec2t (static_cast<T> (1.0), static_cast<T> (0.0)); }
111 static BVH_Vec2t DY() { return BVH_Vec2t (static_cast<T> (0.0), static_cast<T> (1.0)); }
112 static BVH_Vec2t DZ() { return BVH_Vec2t (static_cast<T> (0.0), static_cast<T> (0.0)); }
113 };
114
115 template<class T>
116 struct UnitVector<T, 3>
117 {
118 typedef typename BVH::VectorType<T, 3>::Type BVH_Vec3t;
119 static BVH_Vec3t DX() { return BVH_Vec3t (static_cast<T> (1.0), static_cast<T> (0.0), static_cast<T> (0.0)); }
120 static BVH_Vec3t DY() { return BVH_Vec3t (static_cast<T> (0.0), static_cast<T> (1.0), static_cast<T> (0.0)); }
121 static BVH_Vec3t DZ() { return BVH_Vec3t (static_cast<T> (0.0), static_cast<T> (0.0), static_cast<T> (1.0)); }
122 };
123
124 template<class T>
125 struct UnitVector<T, 4>
126 {
127 typedef typename BVH::VectorType<T, 4>::Type BVH_Vec4t;
128 static BVH_Vec4t DX() { return BVH_Vec4t (static_cast<T> (1.0), static_cast<T> (0.0), static_cast<T> (0.0), static_cast<T> (0.0)); }
129 static BVH_Vec4t DY() { return BVH_Vec4t (static_cast<T> (0.0), static_cast<T> (1.0), static_cast<T> (0.0), static_cast<T> (0.0)); }
130 static BVH_Vec4t DZ() { return BVH_Vec4t (static_cast<T> (0.0), static_cast<T> (0.0), static_cast<T> (1.0), static_cast<T> (0.0)); }
131 };
132}
133
134// =======================================================================
135// function : SetTransform
136// purpose :
137// =======================================================================
138template<class T, int N>
139void BVH_Transform<T, N>::SetTransform (const BVH_MatNt& theTransform)
140{
141 myTransform = theTransform;
142 BVH::MatrixOp<T, N>::Inverse (myTransform, myTransformInversed);
143}
144
145// =======================================================================
146// function : Apply
147// purpose :
148// =======================================================================
149template<class T, int N>
150BVH_Box<T, N> BVH_Transform<T, N>::Apply (const BVH_Box<T, N>& theBox) const
151{
152 typename BVH_Box<T, N>::BVH_VecNt aSize = theBox.Size();
153
154 BVH_Box<T, N> aBox;
155 for (Standard_Integer aX = 0; aX <= 1; ++aX)
156 {
157 for (Standard_Integer aY = 0; aY <= 1; ++aY)
158 {
159 for (Standard_Integer aZ = 0; aZ <= 1; ++aZ)
160 {
161 typename BVH_Box<T, N>::BVH_VecNt aCorner = theBox.CornerMin() +
162 BVH::UnitVector<T, N>::DX() * aSize * static_cast<T> (aX) +
163 BVH::UnitVector<T, N>::DY() * aSize * static_cast<T> (aY) +
164 BVH::UnitVector<T, N>::DZ() * aSize * static_cast<T> (aZ);
165
166 aBox.Add (BVH::MatrixOp<T, N>::Multiply (myTransform, aCorner));
167 }
168 }
169 }
170
171 return aBox;
172}
3c4e78f2 173
174#endif // _BVH_Properties_Header