openscenegraph
Vec4s
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_VEC4S
15#define OSG_VEC4S 1
16
17namespace osg {
18
19/** General purpose float quad. Uses include representation
20 * of color coordinates.
21 * No support yet added for float * Vec4f - is it necessary?
22 * Need to define a non-member non-friend operator* etc.
23 * Vec4f * float is okay
24*/
25class Vec4s
26{
27 public:
28
29 /** Data type of vector components.*/
30 typedef short value_type;
31
32 /** Number of vector components. */
33 enum { num_components = 4 };
34
35 /** Vec member variable. */
36 value_type _v[4];
37
38 /** Constructor that sets all components of the vector to zero */
39 Vec4s() { _v[0]=0; _v[1]=0; _v[2]=0; _v[3]=0; }
40
41 Vec4s(value_type x, value_type y, value_type z, value_type w)
42 {
43 _v[0]=x;
44 _v[1]=y;
45 _v[2]=z;
46 _v[3]=w;
47 }
48
49 inline bool operator == (const Vec4s& v) const { return _v[0]==v._v[0] && _v[1]==v._v[1] && _v[2]==v._v[2] && _v[3]==v._v[3]; }
50 inline bool operator != (const Vec4s& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1] || _v[2]!=v._v[2] || _v[3]!=v._v[3]; }
51 inline bool operator < (const Vec4s& v) const
52 {
53 if (_v[0]<v._v[0]) return true;
54 else if (_v[0]>v._v[0]) return false;
55 else if (_v[1]<v._v[1]) return true;
56 else if (_v[1]>v._v[1]) return false;
57 else if (_v[2]<v._v[2]) return true;
58 else if (_v[2]>v._v[2]) return false;
59 else return (_v[3]<v._v[3]);
60 }
61
62 inline value_type* ptr() { return _v; }
63 inline const value_type* ptr() const { return _v; }
64
65 inline void set( value_type x, value_type y, value_type z, value_type w)
66 {
67 _v[0]=x; _v[1]=y; _v[2]=z; _v[3]=w;
68 }
69
70 inline value_type& operator [] (unsigned int i) { return _v[i]; }
71 inline value_type operator [] (unsigned int i) const { return _v[i]; }
72
73 inline value_type& x() { return _v[0]; }
74 inline value_type& y() { return _v[1]; }
75 inline value_type& z() { return _v[2]; }
76 inline value_type& w() { return _v[3]; }
77
78 inline value_type x() const { return _v[0]; }
79 inline value_type y() const { return _v[1]; }
80 inline value_type z() const { return _v[2]; }
81 inline value_type w() const { return _v[3]; }
82
83 inline value_type& r() { return _v[0]; }
84 inline value_type& g() { return _v[1]; }
85 inline value_type& b() { return _v[2]; }
86 inline value_type& a() { return _v[3]; }
87
88 inline value_type r() const { return _v[0]; }
89 inline value_type g() const { return _v[1]; }
90 inline value_type b() const { return _v[2]; }
91 inline value_type a() const { return _v[3]; }
92
93 /** Multiply by scalar. */
94 inline Vec4s operator * (value_type rhs) const
95 {
96 return Vec4s(_v[0]*rhs, _v[1]*rhs, _v[2]*rhs, _v[3]*rhs);
97 }
98
99 /** Unary multiply by scalar. */
100 inline Vec4s& operator *= (value_type rhs)
101 {
102 _v[0]*=rhs;
103 _v[1]*=rhs;
104 _v[2]*=rhs;
105 _v[3]*=rhs;
106 return *this;
107 }
108
109 /** Divide by scalar. */
110 inline Vec4s operator / (value_type rhs) const
111 {
112 return Vec4s(_v[0]/rhs, _v[1]/rhs, _v[2]/rhs, _v[3]/rhs);
113 }
114
115 /** Unary divide by scalar. */
116 inline Vec4s& operator /= (value_type rhs)
117 {
118 _v[0]/=rhs;
119 _v[1]/=rhs;
120 _v[2]/=rhs;
121 _v[3]/=rhs;
122 return *this;
123 }
124
125
126 /** Binary vector multiply. */
127 inline Vec4s operator * (const Vec4s& rhs) const
128 {
129 return Vec4s(_v[0]*rhs._v[0], _v[1]*rhs._v[1],
130 _v[2]*rhs._v[2], _v[3]*rhs._v[3]);
131 }
132
133 /** Binary vector add. */
134 inline Vec4s operator + (const Vec4s& rhs) const
135 {
136 return Vec4s(_v[0]+rhs._v[0], _v[1]+rhs._v[1],
137 _v[2]+rhs._v[2], _v[3]+rhs._v[3]);
138 }
139
140 /** Unary vector add. Slightly more efficient because no temporary
141 * intermediate object.
142 */
143 inline Vec4s& operator += (const Vec4s& rhs)
144 {
145 _v[0] += rhs._v[0];
146 _v[1] += rhs._v[1];
147 _v[2] += rhs._v[2];
148 _v[3] += rhs._v[3];
149 return *this;
150 }
151
152 /** Binary vector subtract. */
153 inline Vec4s operator - (const Vec4s& rhs) const
154 {
155 return Vec4s(_v[0]-rhs._v[0], _v[1]-rhs._v[1],
156 _v[2]-rhs._v[2], _v[3]-rhs._v[3] );
157 }
158
159 /** Unary vector subtract. */
160 inline Vec4s& operator -= (const Vec4s& rhs)
161 {
162 _v[0]-=rhs._v[0];
163 _v[1]-=rhs._v[1];
164 _v[2]-=rhs._v[2];
165 _v[3]-=rhs._v[3];
166 return *this;
167 }
168
169 /** Negation operator. Returns the negative of the Vec4s. */
170 inline Vec4s operator - () const
171 {
172 return Vec4s (-_v[0], -_v[1], -_v[2], -_v[3]);
173 }
174
175}; // end of class Vec4s
176
177
178/** multiply by vector components. */
179inline Vec4s componentMultiply(const Vec4s& lhs, const Vec4s& rhs)
180{
181 return Vec4s(lhs[0]*rhs[0], lhs[1]*rhs[1], lhs[2]*rhs[2], lhs[3]*rhs[3]);
182}
183
184/** divide rhs components by rhs vector components. */
185inline Vec4s componentDivide(const Vec4s& lhs, const Vec4s& rhs)
186{
187 return Vec4s(lhs[0]/rhs[0], lhs[1]/rhs[1], lhs[2]/rhs[2], lhs[3]/rhs[3]);
188}
189
190} // end of namespace osg
191
192#endif
193