openscenegraph
Vec4f
Go to the documentation of this file.
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14#ifndef OSG_VEC4F
15#define OSG_VEC4F 1
16
17#include <osg/Vec3f>
18
19namespace osg {
20
27class Vec4f
28{
29 public:
30
32 typedef float value_type;
33
35 enum { num_components = 4 };
36
39
40 // Methods are defined here so that they are implicitly inlined
41
43 Vec4f() { _v[0]=0.0f; _v[1]=0.0f; _v[2]=0.0f; _v[3]=0.0f;}
44
46 {
47 _v[0]=x;
48 _v[1]=y;
49 _v[2]=z;
50 _v[3]=w;
51 }
52
54 {
55 _v[0]=v3[0];
56 _v[1]=v3[1];
57 _v[2]=v3[2];
58 _v[3]=w;
59 }
60
61 inline bool operator == (const Vec4f& v) const { return _v[0]==v._v[0] && _v[1]==v._v[1] && _v[2]==v._v[2] && _v[3]==v._v[3]; }
62
63 inline bool operator != (const Vec4f& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1] || _v[2]!=v._v[2] || _v[3]!=v._v[3]; }
64
65 inline bool operator < (const Vec4f& v) const
66 {
67 if (_v[0]<v._v[0]) return true;
68 else if (_v[0]>v._v[0]) return false;
69 else if (_v[1]<v._v[1]) return true;
70 else if (_v[1]>v._v[1]) return false;
71 else if (_v[2]<v._v[2]) return true;
72 else if (_v[2]>v._v[2]) return false;
73 else return (_v[3]<v._v[3]);
74 }
75
76 inline value_type* ptr() { return _v; }
77 inline const value_type* ptr() const { return _v; }
78
80 {
81 _v[0]=x; _v[1]=y; _v[2]=z; _v[3]=w;
82 }
83
84 inline value_type& operator [] (unsigned int i) { return _v[i]; }
85 inline value_type operator [] (unsigned int i) const { return _v[i]; }
86
87 inline value_type& x() { return _v[0]; }
88 inline value_type& y() { return _v[1]; }
89 inline value_type& z() { return _v[2]; }
90 inline value_type& w() { return _v[3]; }
91
92 inline value_type x() const { return _v[0]; }
93 inline value_type y() const { return _v[1]; }
94 inline value_type z() const { return _v[2]; }
95 inline value_type w() const { return _v[3]; }
96
97 inline value_type& r() { return _v[0]; }
98 inline value_type& g() { return _v[1]; }
99 inline value_type& b() { return _v[2]; }
100 inline value_type& a() { return _v[3]; }
101
102 inline value_type r() const { return _v[0]; }
103 inline value_type g() const { return _v[1]; }
104 inline value_type b() const { return _v[2]; }
105 inline value_type a() const { return _v[3]; }
106
107 inline unsigned int asABGR() const
108 {
109 return (unsigned int)clampTo((_v[0]*255.0f),0.0f,255.0f)<<24 |
110 (unsigned int)clampTo((_v[1]*255.0f),0.0f,255.0f)<<16 |
111 (unsigned int)clampTo((_v[2]*255.0f),0.0f,255.0f)<<8 |
112 (unsigned int)clampTo((_v[3]*255.0f),0.0f,255.0f);
113 }
114
115 inline unsigned int asRGBA() const
116 {
117 return (unsigned int)clampTo((_v[3]*255.0f),0.0f,255.0f)<<24 |
118 (unsigned int)clampTo((_v[2]*255.0f),0.0f,255.0f)<<16 |
119 (unsigned int)clampTo((_v[1]*255.0f),0.0f,255.0f)<<8 |
120 (unsigned int)clampTo((_v[0]*255.0f),0.0f,255.0f);
121 }
122
124 inline bool valid() const { return !isNaN(); }
126 inline bool isNaN() const { return osg::isNaN(_v[0]) || osg::isNaN(_v[1]) || osg::isNaN(_v[2]) || osg::isNaN(_v[3]); }
127
129 inline value_type operator * (const Vec4f& rhs) const
130 {
131 return _v[0]*rhs._v[0]+
132 _v[1]*rhs._v[1]+
133 _v[2]*rhs._v[2]+
134 _v[3]*rhs._v[3] ;
135 }
136
139 {
140 return Vec4f(_v[0]*rhs, _v[1]*rhs, _v[2]*rhs, _v[3]*rhs);
141 }
142
145 {
146 _v[0]*=rhs;
147 _v[1]*=rhs;
148 _v[2]*=rhs;
149 _v[3]*=rhs;
150 return *this;
151 }
152
155 {
156 return Vec4f(_v[0]/rhs, _v[1]/rhs, _v[2]/rhs, _v[3]/rhs);
157 }
158
161 {
162 _v[0]/=rhs;
163 _v[1]/=rhs;
164 _v[2]/=rhs;
165 _v[3]/=rhs;
166 return *this;
167 }
168
170 inline Vec4f operator + (const Vec4f& rhs) const
171 {
172 return Vec4f(_v[0]+rhs._v[0], _v[1]+rhs._v[1],
173 _v[2]+rhs._v[2], _v[3]+rhs._v[3]);
174 }
175
179 inline Vec4f& operator += (const Vec4f& rhs)
180 {
181 _v[0] += rhs._v[0];
182 _v[1] += rhs._v[1];
183 _v[2] += rhs._v[2];
184 _v[3] += rhs._v[3];
185 return *this;
186 }
187
189 inline Vec4f operator - (const Vec4f& rhs) const
190 {
191 return Vec4f(_v[0]-rhs._v[0], _v[1]-rhs._v[1],
192 _v[2]-rhs._v[2], _v[3]-rhs._v[3] );
193 }
194
196 inline Vec4f& operator -= (const Vec4f& rhs)
197 {
198 _v[0]-=rhs._v[0];
199 _v[1]-=rhs._v[1];
200 _v[2]-=rhs._v[2];
201 _v[3]-=rhs._v[3];
202 return *this;
203 }
204
206 inline const Vec4f operator - () const
207 {
208 return Vec4f (-_v[0], -_v[1], -_v[2], -_v[3]);
209 }
210
212 inline value_type length() const
213 {
214 return sqrtf( _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] + _v[3]*_v[3]);
215 }
216
218 inline value_type length2() const
219 {
220 return _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] + _v[3]*_v[3];
221 }
222
227 {
229 if (norm>0.0f)
230 {
231 value_type inv = 1.0f/norm;
232 _v[0] *= inv;
233 _v[1] *= inv;
234 _v[2] *= inv;
235 _v[3] *= inv;
236 }
237 return( norm );
238 }
239
240}; // end of class Vec4f
241
243inline Vec4f::value_type operator * (const Vec3f& lhs,const Vec4f& rhs)
244{
245 return lhs[0]*rhs[0]+lhs[1]*rhs[1]+lhs[2]*rhs[2]+rhs[3];
246}
247
249inline Vec4f::value_type operator * (const Vec4f& lhs,const Vec3f& rhs)
250{
251 return lhs[0]*rhs[0]+lhs[1]*rhs[1]+lhs[2]*rhs[2]+lhs[3];
252}
253
255inline Vec4f componentMultiply(const Vec4f& lhs, const Vec4f& rhs)
256{
257 return Vec4f(lhs[0]*rhs[0], lhs[1]*rhs[1], lhs[2]*rhs[2], lhs[3]*rhs[3]);
258}
259
261inline Vec4f componentDivide(const Vec4f& lhs, const Vec4f& rhs)
262{
263 return Vec4f(lhs[0]/rhs[0], lhs[1]/rhs[1], lhs[2]/rhs[2], lhs[3]/rhs[3]);
264}
265
266} // end of namespace osg
267
268#endif
269
Definition Vec3f:29
Definition Vec4f:28
void set(value_type x, value_type y, value_type z, value_type w)
Definition Vec4f:79
Vec4f & operator/=(value_type rhs)
Definition Vec4f:160
value_type g() const
Definition Vec4f:103
Vec4f operator+(const Vec4f &rhs) const
Definition Vec4f:170
unsigned int asABGR() const
Definition Vec4f:107
Vec4f operator/(value_type rhs) const
Definition Vec4f:154
bool isNaN() const
Definition Vec4f:126
Vec4f & operator-=(const Vec4f &rhs)
Definition Vec4f:196
Vec4f & operator+=(const Vec4f &rhs)
Definition Vec4f:179
bool operator==(const Vec4f &v) const
Definition Vec4f:61
value_type _v[4]
Definition Vec4f:38
bool operator<(const Vec4f &v) const
Definition Vec4f:65
value_type normalize()
Definition Vec4f:226
value_type & r()
Definition Vec4f:97
const Vec4f operator-() const
Definition Vec4f:206
value_type x() const
Definition Vec4f:92
value_type y() const
Definition Vec4f:93
Vec4f & operator*=(value_type rhs)
Definition Vec4f:144
value_type r() const
Definition Vec4f:102
value_type & y()
Definition Vec4f:88
bool valid() const
Definition Vec4f:124
bool operator!=(const Vec4f &v) const
Definition Vec4f:63
value_type & g()
Definition Vec4f:98
value_type length() const
Definition Vec4f:212
@ num_components
Definition Vec4f:35
value_type & a()
Definition Vec4f:100
value_type & x()
Definition Vec4f:87
Vec4f()
Definition Vec4f:43
Vec4f(value_type x, value_type y, value_type z, value_type w)
Definition Vec4f:45
unsigned int asRGBA() const
Definition Vec4f:115
value_type & operator[](unsigned int i)
Definition Vec4f:84
value_type w() const
Definition Vec4f:95
value_type operator*(const Vec4f &rhs) const
Definition Vec4f:129
value_type & z()
Definition Vec4f:89
value_type * ptr()
Definition Vec4f:76
value_type b() const
Definition Vec4f:104
Vec4f(const Vec3f &v3, value_type w)
Definition Vec4f:53
value_type & w()
Definition Vec4f:90
value_type a() const
Definition Vec4f:105
float value_type
Definition Vec4f:32
value_type & b()
Definition Vec4f:99
const value_type * ptr() const
Definition Vec4f:77
value_type length2() const
Definition Vec4f:218
value_type z() const
Definition Vec4f:94
author: Julien Valentin 2017 (mp3butcher@hotmail.com)
Definition AlphaFunc:19
Vec3f operator*(const Vec3f &v, const Matrixd &m)
Definition Matrixd:793
Vec2d componentDivide(const Vec2d &lhs, const Vec2d &rhs)
Definition Vec2d:187
bool isNaN(float v)
Definition Math:133
T clampTo(T v, T minimum, T maximum)
Definition Math:88
Vec2d componentMultiply(const Vec2d &lhs, const Vec2d &rhs)
Definition Vec2d:181