Commit | Line | Data |
---|---|---|
5e27df78 | 1 | // Created by: Kirill GAVRILOV |
973c2be1 | 2 | // Copyright (c) 2014 OPEN CASCADE SAS |
5e27df78 | 3 | // |
973c2be1 | 4 | // This file is part of Open CASCADE Technology software library. |
5e27df78 | 5 | // |
973c2be1 | 6 | // This library is free software; you can redistribute it and / or modify it |
7 | // under the terms of the GNU Lesser General Public version 2.1 as published | |
8 | // by the Free Software Foundation, with special exception defined in the file | |
9 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT | |
10 | // distribution for complete text of the license and disclaimer of any warranty. | |
5e27df78 | 11 | // |
973c2be1 | 12 | // Alternatively, this file may be used under the terms of Open CASCADE |
13 | // commercial license or contractual agreement. | |
5e27df78 | 14 | |
15 | #ifndef _NCollection_Vec3_H__ | |
16 | #define _NCollection_Vec3_H__ | |
17 | ||
18 | #include <cstring> | |
19 | #include <cmath> | |
20 | #include <NCollection_Vec2.hxx> | |
21 | ||
22 | //! Auxiliary macros to define couple of similar access components as vector methods | |
23 | #define NCOLLECTION_VEC_COMPONENTS_3D(theX, theY, theZ) \ | |
bf75be98 | 24 | const NCollection_Vec3<Element_t> theX##theY##theZ() const { return NCollection_Vec3<Element_t>(theX(), theY(), theZ()); } \ |
25 | const NCollection_Vec3<Element_t> theX##theZ##theY() const { return NCollection_Vec3<Element_t>(theX(), theZ(), theY()); } \ | |
26 | const NCollection_Vec3<Element_t> theY##theX##theZ() const { return NCollection_Vec3<Element_t>(theY(), theX(), theZ()); } \ | |
27 | const NCollection_Vec3<Element_t> theY##theZ##theX() const { return NCollection_Vec3<Element_t>(theY(), theZ(), theX()); } \ | |
28 | const NCollection_Vec3<Element_t> theZ##theY##theX() const { return NCollection_Vec3<Element_t>(theZ(), theY(), theX()); } \ | |
29 | const NCollection_Vec3<Element_t> theZ##theX##theY() const { return NCollection_Vec3<Element_t>(theZ(), theX(), theY()); } | |
5e27df78 | 30 | |
31 | //! Generic 3-components vector. | |
32 | //! To be used as RGB color pixel or XYZ 3D-point. | |
33 | //! The main target for this class - to handle raw low-level arrays (from/to graphic driver etc.). | |
34 | template<typename Element_t> | |
35 | class NCollection_Vec3 | |
36 | { | |
37 | ||
38 | public: | |
39 | ||
40 | //! Returns the number of components. | |
41 | static int Length() | |
42 | { | |
43 | return 3; | |
44 | } | |
45 | ||
46 | //! Empty constructor. Construct the zero vector. | |
47 | NCollection_Vec3() | |
48 | { | |
49 | std::memset (this, 0, sizeof(NCollection_Vec3)); | |
50 | } | |
51 | ||
52 | //! Initialize ALL components of vector within specified value. | |
53 | explicit NCollection_Vec3 (Element_t theValue) | |
54 | { | |
55 | v[0] = v[1] = v[2] = theValue; | |
56 | } | |
57 | ||
58 | //! Per-component constructor. | |
59 | explicit NCollection_Vec3 (const Element_t theX, | |
60 | const Element_t theY, | |
61 | const Element_t theZ) | |
62 | { | |
63 | v[0] = theX; | |
64 | v[1] = theY; | |
65 | v[2] = theZ; | |
66 | } | |
67 | ||
68 | //! Constructor from 2-components vector. | |
69 | explicit NCollection_Vec3 (const NCollection_Vec2<Element_t>& theVec2) | |
70 | { | |
71 | v[0] = theVec2[0]; | |
72 | v[1] = theVec2[1]; | |
73 | v[2] = Element_t(0); | |
74 | } | |
75 | ||
76 | //! Copy constructor. | |
77 | NCollection_Vec3 (const NCollection_Vec3& theVec3) | |
78 | { | |
79 | std::memcpy (this, &theVec3, sizeof(NCollection_Vec3)); | |
80 | } | |
81 | ||
82 | //! Assignment operator. | |
83 | const NCollection_Vec3& operator= (const NCollection_Vec3& theVec3) | |
84 | { | |
85 | std::memcpy (this, &theVec3, sizeof(NCollection_Vec3)); | |
86 | return *this; | |
87 | } | |
88 | ||
89 | //! Alias to 1st component as X coordinate in XYZ. | |
90 | Element_t x() const { return v[0]; } | |
91 | ||
92 | //! Alias to 1st component as RED channel in RGB. | |
93 | Element_t r() const { return v[0]; } | |
94 | ||
95 | //! Alias to 2nd component as Y coordinate in XYZ. | |
96 | Element_t y() const { return v[1]; } | |
97 | ||
98 | //! Alias to 2nd component as GREEN channel in RGB. | |
99 | Element_t g() const { return v[1]; } | |
100 | ||
101 | //! Alias to 3rd component as Z coordinate in XYZ. | |
102 | Element_t z() const { return v[2]; } | |
103 | ||
104 | //! Alias to 3rd component as BLUE channel in RGB. | |
105 | Element_t b() const { return v[2]; } | |
106 | ||
107 | //! @return 2 components by their names in specified order (in GLSL-style) | |
5640d653 DB |
108 | NCOLLECTION_VEC_COMPONENTS_2D(x, y) |
109 | NCOLLECTION_VEC_COMPONENTS_2D(x, z) | |
110 | NCOLLECTION_VEC_COMPONENTS_2D(y, z) | |
5e27df78 | 111 | |
112 | //! @return 3 components by their names in specified order (in GLSL-style) | |
5640d653 | 113 | NCOLLECTION_VEC_COMPONENTS_3D(x, y, z) |
5e27df78 | 114 | |
115 | //! Alias to 1st component as X coordinate in XYZ. | |
116 | Element_t& x() { return v[0]; } | |
117 | ||
118 | //! Alias to 1st component as RED channel in RGB. | |
119 | Element_t& r() { return v[0]; } | |
120 | ||
121 | //! Alias to 2nd component as Y coordinate in XYZ. | |
122 | Element_t& y() { return v[1]; } | |
123 | ||
124 | //! Alias to 2nd component as GREEN channel in RGB. | |
125 | Element_t& g() { return v[1]; } | |
126 | ||
127 | //! Alias to 3rd component as Z coordinate in XYZ. | |
128 | Element_t& z() { return v[2]; } | |
129 | ||
130 | //! Alias to 3rd component as BLUE channel in RGB. | |
131 | Element_t& b() { return v[2]; } | |
132 | ||
133 | //! @return XY-components modifiable vector | |
134 | NCollection_Vec2<Element_t>& xy() | |
135 | { | |
136 | return *((NCollection_Vec2<Element_t>* )&v[0]); | |
137 | } | |
138 | ||
139 | //! @return YZ-components modifiable vector | |
140 | NCollection_Vec2<Element_t>& yz() | |
141 | { | |
142 | return *((NCollection_Vec2<Element_t>* )&v[1]); | |
143 | } | |
144 | ||
145 | //! Raw access to the data (for OpenGL exchange). | |
938d4544 | 146 | const Element_t* GetData() const { return v; } |
147 | Element_t* ChangeData() { return v; } | |
148 | operator const Element_t*() const { return v; } | |
149 | operator Element_t*() { return v; } | |
5e27df78 | 150 | |
151 | //! Compute per-component summary. | |
152 | NCollection_Vec3& operator+= (const NCollection_Vec3& theAdd) | |
153 | { | |
154 | v[0] += theAdd.v[0]; | |
155 | v[1] += theAdd.v[1]; | |
156 | v[2] += theAdd.v[2]; | |
157 | return *this; | |
158 | } | |
159 | ||
160 | //! Compute per-component summary. | |
161 | friend NCollection_Vec3 operator+ (const NCollection_Vec3& theLeft, | |
162 | const NCollection_Vec3& theRight) | |
163 | { | |
164 | NCollection_Vec3 aSumm = NCollection_Vec3 (theLeft); | |
165 | return aSumm += theRight; | |
166 | } | |
167 | ||
168 | //! Unary -. | |
169 | NCollection_Vec3 operator-() const | |
170 | { | |
171 | return NCollection_Vec3 (-x(), -y(), -z()); | |
172 | } | |
173 | ||
174 | //! Compute per-component subtraction. | |
175 | NCollection_Vec3& operator-= (const NCollection_Vec3& theDec) | |
176 | { | |
177 | v[0] -= theDec.v[0]; | |
178 | v[1] -= theDec.v[1]; | |
179 | v[2] -= theDec.v[2]; | |
180 | return *this; | |
181 | } | |
182 | ||
183 | //! Compute per-component subtraction. | |
184 | friend NCollection_Vec3 operator- (const NCollection_Vec3& theLeft, | |
185 | const NCollection_Vec3& theRight) | |
186 | { | |
187 | NCollection_Vec3 aSumm = NCollection_Vec3 (theLeft); | |
188 | return aSumm -= theRight; | |
189 | } | |
190 | ||
191 | //! Compute per-component multiplication by scale factor. | |
192 | void Multiply (const Element_t theFactor) | |
193 | { | |
194 | v[0] *= theFactor; | |
195 | v[1] *= theFactor; | |
196 | v[2] *= theFactor; | |
197 | } | |
198 | ||
199 | //! Compute per-component multiplication. | |
200 | NCollection_Vec3& operator*= (const NCollection_Vec3& theRight) | |
201 | { | |
202 | v[0] *= theRight.v[0]; | |
203 | v[1] *= theRight.v[1]; | |
204 | v[2] *= theRight.v[2]; | |
205 | return *this; | |
206 | } | |
207 | ||
208 | //! Compute per-component multiplication. | |
209 | friend NCollection_Vec3 operator* (const NCollection_Vec3& theLeft, | |
210 | const NCollection_Vec3& theRight) | |
211 | { | |
212 | NCollection_Vec3 aResult = NCollection_Vec3 (theLeft); | |
213 | return aResult *= theRight; | |
214 | } | |
215 | ||
216 | //! Compute per-component multiplication by scale factor. | |
217 | NCollection_Vec3& operator*= (const Element_t theFactor) | |
218 | { | |
219 | Multiply (theFactor); | |
220 | return *this; | |
221 | } | |
222 | ||
223 | //! Compute per-component multiplication by scale factor. | |
224 | NCollection_Vec3 operator* (const Element_t theFactor) const | |
225 | { | |
226 | return Multiplied (theFactor); | |
227 | } | |
228 | ||
229 | //! Compute per-component multiplication by scale factor. | |
230 | NCollection_Vec3 Multiplied (const Element_t theFactor) const | |
231 | { | |
232 | NCollection_Vec3 aCopyVec3 (*this); | |
233 | aCopyVec3 *= theFactor; | |
234 | return aCopyVec3; | |
235 | } | |
236 | ||
237 | //! Compute per-component division by scale factor. | |
238 | NCollection_Vec3& operator/= (const Element_t theInvFactor) | |
239 | { | |
240 | v[0] /= theInvFactor; | |
241 | v[1] /= theInvFactor; | |
242 | v[2] /= theInvFactor; | |
243 | return *this; | |
244 | } | |
245 | ||
246 | //! Compute per-component division by scale factor. | |
247 | NCollection_Vec3 operator/ (const Element_t theInvFactor) | |
248 | { | |
249 | NCollection_Vec3 aResult (this); | |
250 | return aResult /= theInvFactor; | |
251 | } | |
252 | ||
253 | //! Computes the dot product. | |
254 | Element_t Dot (const NCollection_Vec3& theOther) const | |
255 | { | |
256 | return x() * theOther.x() + y() * theOther.y() + z() * theOther.z(); | |
257 | } | |
258 | ||
259 | //! Computes the vector modulus (magnitude, length). | |
260 | Element_t Modulus() const | |
261 | { | |
262 | return std::sqrt (x() * x() + y() * y() + z() * z()); | |
263 | } | |
264 | ||
265 | //! Computes the square of vector modulus (magnitude, length). | |
266 | //! This method may be used for performance tricks. | |
267 | Element_t SquareModulus() const | |
268 | { | |
269 | return x() * x() + y() * y() + z() * z(); | |
270 | } | |
271 | ||
272 | //! Normalize the vector. | |
273 | void Normalize() | |
274 | { | |
275 | Element_t aModulus = Modulus(); | |
276 | if (aModulus != Element_t(0)) // just avoid divide by zero | |
277 | { | |
278 | x() = x() / aModulus; | |
279 | y() = y() / aModulus; | |
280 | z() = z() / aModulus; | |
281 | } | |
282 | } | |
283 | ||
284 | //! Normalize the vector. | |
285 | NCollection_Vec3 Normalized() const | |
286 | { | |
287 | NCollection_Vec3 aCopy (*this); | |
288 | aCopy.Normalize(); | |
289 | return aCopy; | |
290 | } | |
291 | ||
292 | //! Computes the cross product. | |
293 | static NCollection_Vec3 Cross (const NCollection_Vec3& theVec1, | |
294 | const NCollection_Vec3& theVec2) | |
295 | { | |
296 | return NCollection_Vec3(theVec1.y() * theVec2.z() - theVec1.z() * theVec2.y(), | |
297 | theVec1.z() * theVec2.x() - theVec1.x() * theVec2.z(), | |
298 | theVec1.x() * theVec2.y() - theVec1.y() * theVec2.x()); | |
299 | } | |
300 | ||
301 | //! Compute linear interpolation between to vectors. | |
302 | //! @param theT - interpolation coefficient 0..1; | |
303 | //! @return interpolation result. | |
304 | static NCollection_Vec3 GetLERP (const NCollection_Vec3& theFrom, | |
305 | const NCollection_Vec3& theTo, | |
306 | const Element_t theT) | |
307 | { | |
308 | return theFrom * (Element_t(1) - theT) + theTo * theT; | |
309 | } | |
310 | ||
311 | //! Constuct DX unit vector. | |
312 | static NCollection_Vec3 DX() | |
313 | { | |
314 | return NCollection_Vec3 (Element_t(1), Element_t(0), Element_t(0)); | |
315 | } | |
316 | ||
317 | //! Constuct DY unit vector. | |
318 | static NCollection_Vec3 DY() | |
319 | { | |
320 | return NCollection_Vec3 (Element_t(0), Element_t(1), Element_t(0)); | |
321 | } | |
322 | ||
323 | //! Constuct DZ unit vector. | |
324 | static NCollection_Vec3 DZ() | |
325 | { | |
326 | return NCollection_Vec3 (Element_t(0), Element_t(0), Element_t(1)); | |
327 | } | |
328 | ||
329 | private: | |
330 | ||
331 | Element_t v[3]; //!< define the vector as array to avoid structure alignment issues | |
332 | ||
333 | }; | |
334 | ||
335 | //! Optimized concretization for float type. | |
336 | template<> inline NCollection_Vec3<float>& NCollection_Vec3<float>::operator/= (const float theInvFactor) | |
337 | { | |
338 | Multiply (1.0f / theInvFactor); | |
339 | return *this; | |
340 | } | |
341 | ||
342 | //! Optimized concretization for double type. | |
343 | template<> inline NCollection_Vec3<double>& NCollection_Vec3<double>::operator/= (const double theInvFactor) | |
344 | { | |
345 | Multiply (1.0 / theInvFactor); | |
346 | return *this; | |
347 | } | |
348 | ||
349 | #endif // _NCollection_Vec3_H__ |