1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
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.
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.
19/** General purpose float quad. Uses include representation
20 * of color coordinates.
21 * No support yet added for float * Vec4us - is it necessary?
22 * Need to define a non-member non-friend operator* etc.
23 * Vec4us * float is okay
29 /** Data type of vector components.*/
30 typedef unsigned short value_type;
32 /** Number of vector components. */
33 enum { num_components = 4 };
35 /** Vec member variable. */
38 /** Constructor that sets all components of the vector to zero */
39 Vec4us() { _v[0]=0; _v[1]=0; _v[2]=0; _v[3]=0; }
41 Vec4us(value_type x, value_type y, value_type z, value_type w)
49 inline bool operator == (const Vec4us& 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 Vec4us& 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 Vec4us& v) const
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]);
62 inline value_type* ptr() { return _v; }
63 inline const value_type* ptr() const { return _v; }
65 inline void set( value_type x, value_type y, value_type z, value_type w)
67 _v[0]=x; _v[1]=y; _v[2]=z; _v[3]=w;
70 inline value_type& operator [] (unsigned int i) { return _v[i]; }
71 inline value_type operator [] (unsigned int i) const { return _v[i]; }
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]; }
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]; }
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]; }
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]; }
93 /** Multiply by scalar. */
94 inline Vec4us operator * (value_type rhs) const
96 return Vec4us(_v[0]*rhs, _v[1]*rhs, _v[2]*rhs, _v[3]*rhs);
99 /** Unary multiply by scalar. */
100 inline Vec4us& operator *= (value_type rhs)
109 /** Divide by scalar. */
110 inline Vec4us operator / (value_type rhs) const
112 return Vec4us(_v[0]/rhs, _v[1]/rhs, _v[2]/rhs, _v[3]/rhs);
115 /** Unary divide by scalar. */
116 inline Vec4us& operator /= (value_type rhs)
126 /** Binary vector multiply. */
127 inline Vec4us operator * (const Vec4us& rhs) const
129 return Vec4us(_v[0]*rhs._v[0], _v[1]*rhs._v[1],
130 _v[2]*rhs._v[2], _v[3]*rhs._v[3]);
133 /** Binary vector add. */
134 inline Vec4us operator + (const Vec4us& rhs) const
136 return Vec4us(_v[0]+rhs._v[0], _v[1]+rhs._v[1],
137 _v[2]+rhs._v[2], _v[3]+rhs._v[3]);
140 /** Unary vector add. Slightly more efficient because no temporary
141 * intermediate object.
143 inline Vec4us& operator += (const Vec4us& rhs)
152 /** Binary vector subtract. */
153 inline Vec4us operator - (const Vec4us& rhs) const
155 return Vec4us(_v[0]-rhs._v[0], _v[1]-rhs._v[1],
156 _v[2]-rhs._v[2], _v[3]-rhs._v[3] );
159 /** Unary vector subtract. */
160 inline Vec4us& operator -= (const Vec4us& rhs)
169}; // end of class Vec4us
171/** multiply by vector components. */
172inline Vec4us componentMultiply(const Vec4us& lhs, const Vec4us& rhs)
174 return Vec4us(lhs[0]*rhs[0], lhs[1]*rhs[1], lhs[2]*rhs[2], lhs[3]*rhs[3]);
177/** divide rhs components by rhs vector components. */
178inline Vec4us componentDivide(const Vec4us& lhs, const Vec4us& rhs)
180 return Vec4us(lhs[0]/rhs[0], lhs[1]/rhs[1], lhs[2]/rhs[2], lhs[3]/rhs[3]);
183} // end of namespace osg