Test bugs vis bug24130 should be OK when OpenCL is present, corrected
[occt.git] / src / NCollection / NCollection_Vec4.hxx
CommitLineData
5e27df78 1// Created by: Kirill GAVRILOV
2// Copyright (c) 2012 OPEN CASCADE SAS
3//
4// The content of this file is subject to the Open CASCADE Technology Public
5// License Version 6.5 (the "License"). You may not use the content of this file
6// except in compliance with the License. Please obtain a copy of the License
7// at http://www.opencascade.org and read it completely before using this file.
8//
9// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
10// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
11//
12// The Original Code and all software distributed under the License is
13// distributed on an "AS IS" basis, without warranty of any kind, and the
14// Initial Developer hereby disclaims all such warranties, including without
15// limitation, any warranties of merchantability, fitness for a particular
16// purpose or non-infringement. Please see the License for the specific terms
17// and conditions governing the rights and limitations under the License.
18
19#ifndef _NCollection_Vec4_H__
20#define _NCollection_Vec4_H__
21
22#include <NCollection_Vec3.hxx>
23
24//! Generic 4-components vector.
25//! To be used as RGBA color vector or XYZW 3D-point with special W-component
26//! for operations with projection / model view matrices.
27//! Use this class for 3D-points carefully because declared W-component may
28//! results in incorrect results if used without matrices.
29template<typename Element_t>
30class NCollection_Vec4
31{
32
33public:
34
35 //! Returns the number of components.
36 static size_t Length()
37 {
38 return 4;
39 }
40
41 //! Empty constructor. Construct the zero vector.
42 NCollection_Vec4()
43 {
44 std::memset (this, 0, sizeof(NCollection_Vec4));
45 }
46
47 //! Initialize ALL components of vector within specified value.
48 explicit NCollection_Vec4 (const Element_t theValue)
49 {
50 v[0] = v[1] = v[2] = v[3] = theValue;
51 }
52
53 //! Per-component constructor.
54 explicit NCollection_Vec4 (const Element_t theX,
55 const Element_t theY,
56 const Element_t theZ,
57 const Element_t theW)
58 {
59 v[0] = theX;
60 v[1] = theY;
61 v[2] = theZ;
62 v[3] = theW;
63 }
64
65 //! Constructor from 2-components vector.
66 explicit NCollection_Vec4 (const NCollection_Vec2<Element_t>& theVec2)
67 {
68 v[0] = theVec2[0];
69 v[1] = theVec2[1];
70 v[2] = v[3] = Element_t (0);
71 }
72
73 //! Constructor from 3-components vector.
74 explicit NCollection_Vec4(const NCollection_Vec3<Element_t>& theVec3)
75 {
76 std::memcpy (this, &theVec3, sizeof(NCollection_Vec3<Element_t>));
77 v[3] = Element_t (0);
78 }
79
80 //! Constructor from 3-components vector + alpha value.
81 explicit NCollection_Vec4(const NCollection_Vec3<Element_t>& theVec3,
82 const Element_t theAlpha) {
83 std::memcpy (this, &theVec3, sizeof(NCollection_Vec3<Element_t>));
84 v[3] = theAlpha;
85 }
86
87 //! Copy constructor.
88 NCollection_Vec4 (const NCollection_Vec4& theVec4)
89 {
90 std::memcpy (this, &theVec4, sizeof(NCollection_Vec4));
91 }
92
93 //! Assignment operator.
94 const NCollection_Vec4& operator= (const NCollection_Vec4& theVec4)
95 {
96 std::memcpy (this, &theVec4, sizeof(NCollection_Vec4));
97 return *this;
98 }
99
100 //! Alias to 1st component as X coordinate in XYZW.
101 Element_t x() const { return v[0]; }
102
103 //! Alias to 1st component as RED channel in RGBA.
104 Element_t r() const { return v[0]; }
105
106 //! Alias to 2nd component as Y coordinate in XYZW.
107 Element_t y() const { return v[1]; }
108
109 //! Alias to 2nd component as GREEN channel in RGBA.
110 Element_t g() const { return v[1]; }
111
112 //! Alias to 3rd component as Z coordinate in XYZW.
113 Element_t z() const { return v[2]; }
114
115 //! Alias to 3rd component as BLUE channel in RGBA.
116 Element_t b() const { return v[2]; }
117
118 //! Alias to 4th component as W coordinate in XYZW.
119 Element_t w() const { return v[3]; }
120
121 //! Alias to 4th component as ALPHA channel in RGBA.
122 Element_t a() const { return v[3]; }
123
124 //! @return 2 of XYZW components in specified order as vector in GLSL-style
125 NCOLLECTION_VEC_COMPONENTS_2D(x, y);
126 NCOLLECTION_VEC_COMPONENTS_2D(x, z);
127 NCOLLECTION_VEC_COMPONENTS_2D(x, w);
128 NCOLLECTION_VEC_COMPONENTS_2D(y, z);
129 NCOLLECTION_VEC_COMPONENTS_2D(y, w);
130 NCOLLECTION_VEC_COMPONENTS_2D(z, w);
131
132 //! @return 3 of XYZW components in specified order as vector in GLSL-style
133 NCOLLECTION_VEC_COMPONENTS_3D(x, y, z);
134 NCOLLECTION_VEC_COMPONENTS_3D(x, y, w);
135 NCOLLECTION_VEC_COMPONENTS_3D(x, z, w);
136 NCOLLECTION_VEC_COMPONENTS_3D(y, z, w);
137
138 //! @return RGB components as vector
139 NCOLLECTION_VEC_COMPONENTS_3D(r, g, b);
140
141 //! Alias to 1st component as X coordinate in XYZW.
142 Element_t& x() { return v[0]; }
143
144 //! Alias to 1st component as RED channel in RGBA.
145 Element_t& r() { return v[0]; }
146
147 //! Alias to 2nd component as Y coordinate in XYZW.
148 Element_t& y() { return v[1]; }
149
150 //! Alias to 2nd component as GREEN channel in RGBA.
151 Element_t& g() { return v[1]; } // Green color
152
153 //! Alias to 3rd component as Z coordinate in XYZW.
154 Element_t& z() { return v[2]; }
155
156 //! Alias to 3rd component as BLUE channel in RGBA.
157 Element_t& b() { return v[2]; }
158
159 //! Alias to 4th component as W coordinate in XYZW.
160 Element_t& w() { return v[3]; }
161
162 //! Alias to 4th component as ALPHA channel in RGBA.
163 Element_t& a() { return v[3]; }
164
165 //! @return XY-components modifiable vector
166 NCollection_Vec2<Element_t>& xy()
167 {
168 return *((NCollection_Vec2<Element_t>* )&v[0]);
169 }
170
171 //! @return YZ-components modifiable vector
172 NCollection_Vec2<Element_t>& yz()
173 {
174 return *((NCollection_Vec2<Element_t>* )&v[1]);
175 }
176
177 //! @return YZ-components modifiable vector
178 NCollection_Vec2<Element_t>& zw()
179 {
180 return *((NCollection_Vec2<Element_t>* )&v[2]);
181 }
182
183 //! @return XYZ-components modifiable vector
184 NCollection_Vec3<Element_t>& xyz()
185 {
186 return *((NCollection_Vec3<Element_t>* )&v[0]);
187 }
188
189 //! @return YZW-components modifiable vector
190 NCollection_Vec3<Element_t>& yzw()
191 {
192 return *((NCollection_Vec3<Element_t>* )&v[1]);
193 }
194
195 //! Raw access to the data (for OpenGL exchange).
196 const Element_t* GetData() const { return v; }
197 operator const Element_t*() const { return v; }
198 operator Element_t*() { return v; }
199
200 //! Compute per-component summary.
201 NCollection_Vec4& operator+= (const NCollection_Vec4& theAdd)
202 {
203 v[0] += theAdd.v[0];
204 v[1] += theAdd.v[1];
205 v[2] += theAdd.v[2];
206 v[3] += theAdd.v[3];
207 return *this;
208 }
209
210 //! Compute per-component summary.
211 friend NCollection_Vec4 operator+ (const NCollection_Vec4& theLeft,
212 const NCollection_Vec4& theRight)
213 {
214 NCollection_Vec4 aSumm = NCollection_Vec4 (theLeft);
215 return aSumm += theRight;
216 }
217
218 //! Compute per-component subtraction.
219 NCollection_Vec4& operator-= (const NCollection_Vec4& theDec)
220 {
221 v[0] -= theDec.v[0];
222 v[1] -= theDec.v[1];
223 v[2] -= theDec.v[2];
224 v[3] -= theDec.v[3];
225 return *this;
226 }
227
228 //! Compute per-component subtraction.
229 friend NCollection_Vec4 operator- (const NCollection_Vec4& theLeft,
230 const NCollection_Vec4& theRight)
231 {
232 NCollection_Vec4 aSumm = NCollection_Vec4 (theLeft);
233 return aSumm -= theRight;
234 }
235
236 //! Compute per-component multiplication.
237 NCollection_Vec4& operator*= (const NCollection_Vec4& theRight)
238 {
239 v[0] *= theRight.v[0];
240 v[1] *= theRight.v[1];
241 v[2] *= theRight.v[2];
242 v[3] *= theRight.v[3];
243 return *this;
244 }
245
246 //! Compute per-component multiplication.
247 friend NCollection_Vec4 operator* (const NCollection_Vec4& theLeft,
248 const NCollection_Vec4& theRight)
249 {
250 NCollection_Vec4 aResult = NCollection_Vec4 (theLeft);
251 return aResult *= theRight;
252 }
253
254 //! Compute per-component multiplication.
255 void Multiply (const Element_t theFactor)
256 {
257 v[0] *= theFactor;
258 v[1] *= theFactor;
259 v[2] *= theFactor;
260 v[3] *= theFactor;
261 }
262
263 //! Compute per-component multiplication.
264 NCollection_Vec4& operator*=(const Element_t theFactor)
265 {
266 Multiply (theFactor);
267 return *this;
268 }
269
270 //! Compute per-component multiplication.
271 NCollection_Vec4 operator* (const Element_t theFactor) const
272 {
273 return Multiplied (theFactor);
274 }
275
276 //! Compute per-component multiplication.
277 NCollection_Vec4 Multiplied (const Element_t theFactor) const
278 {
279 NCollection_Vec4 aCopyVec4 (*this);
280 aCopyVec4 *= theFactor;
281 return aCopyVec4;
282 }
283
284 //! Compute per-component division by scale factor.
285 NCollection_Vec4& operator/= (const Element_t theInvFactor)
286 {
287 v[0] /= theInvFactor;
288 v[1] /= theInvFactor;
289 v[2] /= theInvFactor;
290 v[3] /= theInvFactor;
291 return *this;
292 }
293
294 //! Compute per-component division by scale factor.
295 NCollection_Vec4 operator/ (const Element_t theInvFactor)
296 {
297 NCollection_Vec4 aResult(this);
298 return aResult /= theInvFactor;
299 }
300
301private:
302
303 Element_t v[4]; //!< define the vector as array to avoid structure alignment issues
304
305};
306
307//! Optimized concretization for float type.
308template<> inline NCollection_Vec4<float>& NCollection_Vec4<float>::operator/= (const float theInvFactor)
309{
310 Multiply (1.0f / theInvFactor);
311 return *this;
312}
313
314//! Optimized concretization for double type.
315template<> inline NCollection_Vec4<double>& NCollection_Vec4<double>::operator/= (const double theInvFactor)
316{
317 Multiply (1.0 / theInvFactor);
318 return *this;
319}
320
321#endif // _NCollection_Vec4_H__