1 /*
2 Copyright (C) 2005-2007 Feeling Software Inc.
3 Portions of the code are:
4 Copyright (C) 2005-2007 Sony Computer Entertainment America
5
6 MIT License: http://www.opensource.org/licenses/mit-license.php
7 */
8
9 /**
10 @file FMVector4.h
11 This file contains the class for 4 dimensional vectors.
12 */
13
14 #ifndef _FM_VECTOR4_H_
15 #define _FM_VECTOR4_H_
16
17 class FMColor;
18
19 /**
20 A 4 dimensional vector.
21 Not used within FCollada.
22
23 @ingroup FMath
24 */
25 class FCOLLADA_EXPORT
26 ALIGN_STRUCT(16)
27 FMVector4
28 {
29 public:
30 float x; /**< The first coordinate. */
31 float y; /**< The second coordinate. */
32 float z; /**< The third coordinate. */
33 float w; /**< The fourth coordinate. */
34
35 /** Creates an empty 4D vector. */
36 #ifndef _DEBUG
FMVector4()37 inline FMVector4() {}
38 #else
FMVector4()39 inline FMVector4() { x = 123456789.0f; y = 123456789.0f; z = 123456789.0f; w = 123456789.0f; }
40 #endif
41
42 /** Creates the FMVector4 from a list of \c floats.
43 It takes the first 4 \c floats starting from and including \a startIndex
44 (0 indexing) in the array as the 4 coordinates. The first as the first
45 coordinate, the second as the second, and the third as the third, the fourth as w
46 @param source The \c float array.
47 @param startIndex The index of the first element. */
48 inline FMVector4(const float* source, uint32 startIndex = 0) { source = source + startIndex; x = *source++; y = *source++; z = *source++; w = *source; }
49
50 /** Creates the FMVector4 with the coordinates given.
51 The first three coordinates are taken from the FMVector3, where the
52 first one is the x value, the second is that y, and the third is the z.
53 The forth value is the \c float specified.
54 @param v The FMVector3 representing the first three coordinates.
55 @param _w The final coordinate. */
FMVector4(const FMVector3 & v,float _w)56 inline FMVector4(const FMVector3& v, float _w) { x = v.x; y = v.y; z = v.z; w = _w; }
57
58 /** Creates the FMVector4 with the coordinates given.
59 @param v1 The FMVector2 representing the first two coordinates.
60 @param v2 The FMVector2 representing the last two coordinates. */
FMVector4(const FMVector2 & v1,const FMVector2 & v2)61 inline FMVector4(const FMVector2& v1, const FMVector2& v2) { x = v1.x; y = v1.y; z = v2.x; w = v2.y; }
62
63 /** Creates the FMVector4 with the coordinates given.
64 @param _x The first coordinate.
65 @param _y The second coordinate.
66 @param _z The third coordinate.
67 @param _w The forth coordinate. */
FMVector4(float _x,float _y,float _z,float _w)68 inline FMVector4(float _x, float _y, float _z, float _w) { x = _x; y = _y; z = _z; w = _w; }
69
70 /** Creates the 4D vector from a given color value.
71 @param c The color value. */
72 FMVector4(const FMColor& c);
73
74 /** Creates the 4D vector from a HSV color value.
75 HSV stands for hue, saturation and value and is
76 a more humane way to express color values, as opposed to RGB.
77 @param hue The color hue. In the range [0,1].
78 @param saturation The color saturation. In the range [0,1].
79 @param value The color value. In the range [0,1]. */
80 static FMVector4 FromHSVColor(float hue, float saturation, float value);
81
82 /** Retrieves the HSV color value for this RGBA color value.
83 All the color components are expected to be in the [0,1] range:
84 no clamping will be done.
85 @return The equivalent HSV color value. All the components will be
86 in the [0,1] range. */
87 FMVector3 ToHSVColor();
88
89 public:
90 /** Get this FMVector4 as an array of \c floats.
91 @return The \c float array. */
92 inline operator float*() { return &x; }
93
94 /** Get this FMVector4 as an array of \c floats.
95 @return The \c float array. */
96 inline operator const float*() const { return &x; }
97
98 /** Assign this FMVector4 to the given float array.
99 Assigns each coordinate of this FMVector4 to the elements in the \c
100 float array. The first element to the first coordinate, the second to
101 the second, and the third to the third. It returns this FMVector4.
102 @param v The \c float array to assign with.
103 @return This FMVector4. */
104 inline FMVector4& operator =(const float* v) { x = *v; y = *(v + 1); z = *(v + 2); w = *(v+3); return *this; }
105
106 /** Update each component of this FMVector to the minimum of two FMVector4s.
107 Updates each of the four components to be the minimum of the current
108 value and that of the corresponding value of the given FMVector4.
109 @param min The FMVector to take values from. */
ComponentMinimum(const FMVector4 & min)110 inline void ComponentMinimum(const FMVector4& min) { if (x < min.x) x = min.x; if (y < min.y) y = min.y; if (z < min.z) z = min.z; if (w < min.w) w = min.w; }
111
112 /** Update each component of this FMVector to the maximum of two FMVector4s.
113 Updates each of the four components to be the maximum of the current
114 value and that of the corresponding value of the given FMVector4.
115 @param max The FMVector to take values from. */
ComponentMaximum(const FMVector4 & max)116 inline void ComponentMaximum(const FMVector4& max) { if (x > max.x) x = max.x; if (y > max.y) y = max.y; if (z > max.z) z = max.z; if (w > max.w) w = max.w;}
117
118 public:
119 static const FMVector4 Zero; /**< The 4D vector containing all zeroes. */
120 static const FMVector4 One; /**< The 4D vector containing all ones. */
121 static const FMVector4 AlphaOne;
122 };
123
124 /** Scalar multiplication with a FMVector4.
125 @param a The vector.
126 @param b The scalar.
127 @return The FMVector4 representing the resulting vector. */
128 inline FMVector4 operator *(const FMVector4& a, float b) { return FMVector4(a.x * b, a.y * b, a.z * b, a.w * b); }
129
130 /** Scalar multiplication with a FMVector4.
131 @param a The scalar.
132 @param b The vector.
133 @return The FMVector4 representing the resulting vector. */
134 inline FMVector4 operator *(float a, const FMVector4& b) { return FMVector4(a * b.x, a * b.y, a * b.z, a * b.w); }
135
136 /** Vector addition with two FMVector4.
137 @param a The first vector.
138 @param b The second vector.
139 @return The FMVector4 representation of the resulting vector. */
140 inline FMVector4 operator +(const FMVector4& a, const FMVector4& b) { return FMVector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w); }
141
142 /** Vector substraction with two FMVector4.
143 @param a The first vector.
144 @param b The second vector.
145 @return The FMVector4 representation of the resulting vector. */
146 inline FMVector4 operator -(const FMVector4& a, const FMVector4& b) { return FMVector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w); }
147
148 /** Assignment of the addition of two FMVector4.
149 @param b The first vector, which will also be assigned to the result.
150 @param a The second vector.
151 @return The first vector, after it has been assigned new values. */
152 inline FMVector4& operator +=(FMVector4& b, const FMVector4& a) { b.x += a.x; b.y += a.y; b.z += a.z; b.w += a.w; return b; }
153
154 /** Assignment of the subtraction of two FMVector4.
155 @param b The first vector, which will also be assigned to the result.
156 @param a The second vector.
157 @return The first vector, after it has been assigned new values. */
158 inline FMVector4& operator -=(FMVector4& b, const FMVector4& a) { b.x -= a.x; b.y -= a.y; b.z -= a.z; b.w -= a.w; return b; }
159
160 /** Assignment of the scalar multiplication of a FMVector4.
161 @param b The vector, which will also be assigned to the result.
162 @param a The scalar.
163 @return The vector, after it has been assigned new values. */
164 inline FMVector4& operator *=(FMVector4& b, float a) { b.x *= a; b.y *= a; b.z *= a; b.w *= a; return b; }
165
166 /** Assignment of the scalar division of a FMVector4.
167 @param b The vector, which will also be assigned to the result.
168 @param a The scalar.
169 @return The vector, after it has been assigned new values. */
170 inline FMVector4& operator /=(FMVector4& b, float a) { b.x /= a; b.y /= a; b.z /= a; b.w /= a; return b; }
171
172 /** Dot product of two FMVector4.
173 @param a The first vector.
174 @param b The second vector.
175 @return The result of the dot product. */
176 inline float operator *(const FMVector4& a, const FMVector4& b) { return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; }
177
178
179 /** Returns whether two 4D vectors are equivalent.
180 @param p A first vector.
181 @param q A second vector.
182 @return Whether the vectors are equivalent. */
IsEquivalent(const FMVector4 & p,const FMVector4 & q)183 inline bool IsEquivalent(const FMVector4& p, const FMVector4& q) { return IsEquivalent(p.x, q.x) && IsEquivalent(p.y, q.y) && IsEquivalent(p.z, q.z) && IsEquivalent(p.w, q.w); }
184 inline bool operator==(const FMVector4& p, const FMVector4& q) { return IsEquivalent(p.x, q.x) && IsEquivalent(p.y, q.y) && IsEquivalent(p.z, q.z) && IsEquivalent(p.w, q.w); } /**< See above. */
185
186 /** A dynamically-sized array of 4D vectors or points. */
187 typedef fm::vector<FMVector4> FMVector4List;
188
189 #endif // _FM_VECTOR4_H_
190