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 triple.
20 * Uses include representation of color coordinates.
21 * No support yet added for float * Vec4b - is it necessary?
22 * Need to define a non-member non-friend operator* etc.
23 * Vec4b * float is okay
29 /** Data type of vector components.*/
30 typedef signed char value_type;
32 /** Number of vector components. */
33 enum { num_components = 4 };
37 /** Constructor that sets all components of the vector to zero */
38 Vec4b() { _v[0]=0; _v[1]=0; _v[2]=0; _v[3]=0; }
40 Vec4b(value_type x, value_type y, value_type z, value_type w)
48 inline bool operator == (const Vec4b& 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 Vec4b& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1] || _v[2]!=v._v[2] || _v[3]!=v._v[3]; }
52 inline bool operator < (const Vec4b& v) const
54 if (_v[0]<v._v[0]) return true;
55 else if (_v[0]>v._v[0]) return false;
56 else if (_v[1]<v._v[1]) return true;
57 else if (_v[1]>v._v[1]) return false;
58 else if (_v[2]<v._v[2]) return true;
59 else if (_v[2]>v._v[2]) return false;
60 else return (_v[3]<v._v[3]);
63 inline value_type* ptr() { return _v; }
64 inline const value_type* ptr() const { return _v; }
66 inline void set( value_type x, value_type y, value_type z, value_type w)
68 _v[0]=x; _v[1]=y; _v[2]=z; _v[3]=w;
71 inline value_type& operator [] (unsigned int i) { return _v[i]; }
72 inline value_type operator [] (unsigned int i) const { return _v[i]; }
74 inline value_type& x() { return _v[0]; }
75 inline value_type& y() { return _v[1]; }
76 inline value_type& z() { return _v[2]; }
77 inline value_type& w() { return _v[3]; }
79 inline value_type x() const { return _v[0]; }
80 inline value_type y() const { return _v[1]; }
81 inline value_type z() const { return _v[2]; }
82 inline value_type w() const { return _v[3]; }
84 inline value_type& r() { return _v[0]; }
85 inline value_type& g() { return _v[1]; }
86 inline value_type& b() { return _v[2]; }
87 inline value_type& a() { return _v[3]; }
89 inline value_type r() const { return _v[0]; }
90 inline value_type g() const { return _v[1]; }
91 inline value_type b() const { return _v[2]; }
92 inline value_type a() const { return _v[3]; }
94 /** Multiply by scalar. */
95 inline Vec4b operator * (float rhs) const
102 /** Unary multiply by scalar. */
103 inline Vec4b& operator *= (float rhs)
105 _v[0]=(value_type)((float)_v[0]*rhs);
106 _v[1]=(value_type)((float)_v[1]*rhs);
107 _v[2]=(value_type)((float)_v[2]*rhs);
108 _v[3]=(value_type)((float)_v[3]*rhs);
112 /** Divide by scalar. */
113 inline Vec4b operator / (float rhs) const
120 /** Unary divide by scalar. */
121 inline Vec4b& operator /= (float rhs)
123 float div = 1.0f/rhs;
128 /** Binary vector add. */
129 inline Vec4b operator + (const Vec4b& rhs) const
131 return Vec4b(_v[0]+rhs._v[0], _v[1]+rhs._v[1],
132 _v[2]+rhs._v[2], _v[3]+rhs._v[3]);
135 /** Unary vector add. Slightly more efficient because no temporary
136 * intermediate object.
138 inline Vec4b& operator += (const Vec4b& rhs)
147 /** Binary vector subtract. */
148 inline Vec4b operator - (const Vec4b& rhs) const
150 return Vec4b(_v[0]-rhs._v[0], _v[1]-rhs._v[1],
151 _v[2]-rhs._v[2], _v[3]-rhs._v[3]);
154 /** Unary vector subtract. */
155 inline Vec4b& operator -= (const Vec4b& rhs)
164}; // end of class Vec4b
168} // end of namespace osg