root/juggler/tags/1.0.5/Math/vjVec3.h

Revision 7539, 6.3 kB (checked in by anonymous, 7 years ago)

This commit was manufactured by cvs2svn to create tag
'RELENG_1_0_5_RELEASE'.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
2  *
3  * VR Juggler is (C) Copyright 1998, 1999, 2000 by Iowa State University
4  *
5  * Original Authors:
6  *   Allen Bierbaum, Christopher Just,
7  *   Patrick Hartling, Kevin Meinert,
8  *   Carolina Cruz-Neira, Albert Baker
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public
21  * License along with this library; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 02111-1307, USA.
24  *
25  * -----------------------------------------------------------------
26  * File:          $RCSfile$
27  * Date modified: $Date$
28  * Version:       $Revision$
29  * -----------------------------------------------------------------
30  *
31  *************** <auto-copyright.pl END do not edit this line> ***************/
32
33
34 #ifndef _VJ_VEC3_
35 #define _VJ_VEC3_
36 //#pragma once
37
38 #include <vjConfig.h>
39 #include <math.h>
40
41 class vjMatrix;
42
43 //----------------------------------------------------------------
44 //: Vector of length 3
45 // The class is a representation of a vector with 3 float values.
46 // It defines several operators that can be used on the vectors.
47 //-----------------------------------------------------------------
48 //!PUBLIC_API:
49 class vjVec3
50 {
51 public:
52    // Constructor
53    //! ARGS: x,y,z - the values of the vector
54    vjVec3(float _x, float _y, float _z)
55    { set(_x, _y, _z);}
56
57    //: Default constructor
58    vjVec3() { set(0.0f,0.0f,0.0f); }
59
60 public:
61    //: Set value of Vector
62    void set(float _x, float _y, float _z) {
63       vec[0] = _x;
64       vec[1] = _y;
65       vec[2] = _z;
66    }
67
68 public:
69    //: Are we equal
70    //! RETURNS: true - Values in _v equal our values
71    bool equal(const vjVec3&  _v) const {
72       return (vec[0] == _v[0] &&
73               vec[1] == _v[1] &&
74               vec[2] == _v[2]);
75    }
76
77    //: Compute dot product
78    //! RETURNS: Dot product of this and _v
79    float dot(const vjVec3&  _v) const {
80       return (vec[0] * _v[0] +
81               vec[1] * _v[1] +
82               vec[2] * _v[2]);
83    }
84
85    //: Normalize the vector
86    void normalize()
87    {
88       float len = length();
89       float len_inv = 1.0f/len;
90       vec[0] *= len_inv;
91       vec[1] *= len_inv;
92       vec[2] *= len_inv;
93    }
94
95    //: Is the vector normalized
96    bool isNormalized() const
97    {
98       float len = lengthSquared();
99       float diff = len-1.0f;
100       return((diff < 0.0001f) && (diff > -0.0001f));
101    }
102
103    //: Return the length squared
104    //! NOTE: Since this does not use sqrt it is faster
105    //+ than length().  It can be used to optimize some
106    //+ routines
107    float lengthSquared() const
108    {
109       return ((vec[0]*vec[0])+
110              (vec[1]*vec[1])+
111              (vec[2]*vec[2]));
112    }
113
114    //: Return vector's length
115    float length() const
116    {
117       return sqrtf(lengthSquared());
118    }
119
120
121    //: Calculate the cross product of me X _v
122    vjVec3 cross(const vjVec3&  _v) const
123    {
124       return vjVec3( ((vec[1]*_v.vec[2]) - (vec[2]*_v.vec[1])),
125                      ((vec[2]*_v.vec[0]) - (vec[0]*_v.vec[2])),
126                      ((vec[0]*_v.vec[1]) - (vec[1]*_v.vec[0])));
127    }
128
129    //-----------------------------------------------------
130    //: Transform the vec by the full 4x4 matrix.
131    // vec = (Mat)(_v)
132    // initial w = 1;  At end scale by W to convert back
133    //-----------------------------------------------------
134    void xformFull(const vjMatrix& _m, const vjVec3& _v);
135
136    //-------------------------------------------------------
137    //: Transform as vector.
138    // w initial = 0.0  ==> Translation doesn't effect vector
139    //--------------------------------------------------------
140    void xformVec(const vjMatrix& _m, const vjVec3& _v);
141
142 public:
143    /// Operators
144    float&  operator [](int i) { return vec[i];}
145    const float&  operator [](int i) const { return vec[i];}
146
147    int operator ==(const vjVec3& _v) const {
148       return (vec[0] == _v[0] &&
149               vec[1] == _v[1] &&
150               vec[2] == _v[2]);
151    }
152
153    int operator !=(const vjVec3& _v) const {
154       return !(*this == _v);
155    }
156
157 public:
158    // vjVec3 operators, return by value could be slow
159    vjVec3 operator -() const {
160       return vjVec3(-vec[0], -vec[1], -vec[2]);
161    }
162
163    vjVec3 operator +(const vjVec3& _v) const {
164       return vjVec3(vec[0]+_v[0], vec[1]+_v[1], vec[2]+_v[2]);
165    }
166
167    vjVec3 operator -(const vjVec3& _v) const {
168       return vjVec3(vec[0]-_v[0], vec[1]-_v[1], vec[2]-_v[2]);
169    }
170
171    friend vjVec3 operator *(float _s, const vjVec3&);
172    friend vjVec3 operator *(const vjVec3& _v, float _s);
173    friend vjVec3 operator /(const vjVec3& _v, float _s);
174 //    friend inline vjVec3 operator *(const vjVec3& _v, const vjMatrix& _m);
175
176    //: linear interpolate from vector a to vector b
177    //!PRE:  n is a number between [0..1]
178    //!POST: "this" is set to the interpolated vector
179    void lerp( float n, const vjVec3& a, const vjVec3& b )
180    {
181       vjVec3 offset = b - a;
182       (*this) = a + (offset * n);
183    }   
184 public:
185    //: Assignment Operators
186    vjVec3&  operator =(const vjVec3& _v)
187    {
188       if(&_v == this)
189          return *this;
190       vec[0] = _v[0];
191       vec[1] = _v[1];
192       vec[2] = _v[2];
193       return *this;
194    }
195
196    //: Multiply all elements by a constant
197    vjVec3& operator *=(float _s) {
198       vec[0] *= _s;
199       vec[1] *= _s;
200       vec[2] *= _s;
201       return *this;
202    }
203
204    //: Divide all elements by a constant
205    vjVec3& operator /=(float _s) {
206       _s = 1.0/_s;
207       return *this *= _s;
208    }
209
210    vjVec3& operator +=(const vjVec3& _v) {
211       vec[0] += _v[0];
212       vec[1] += _v[1];
213       vec[2] += _v[2];
214       return *this;
215    }
216
217    vjVec3& operator -=(const vjVec3& _v) {
218       vec[0] -= _v[0];
219       vec[1] -= _v[1];
220       vec[2] -= _v[2];
221       return *this;
222    }
223
224 public:
225    float vec[3];
226 };
227
228
229 vjVec3 operator *(float _s, const vjVec3& _v);
230 vjVec3 operator *(const vjVec3& _v, float _s);
231 vjVec3 operator /(const vjVec3& _v, float _s);
232 std::ostream& operator<<(std::ostream& out, vjVec3& _v);
233
234 #endif
Note: See TracBrowser for help on using the browser.