root/juggler/branches/1.0/Math/vjVec4.h

Revision 8789, 4.6 kB (checked in by patrickh, 7 years ago)

Copyright update.

  • 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, 2001, 2002
4  *   by Iowa State University
5  *
6  * Original Authors:
7  *   Allen Bierbaum, Christopher Just,
8  *   Patrick Hartling, Kevin Meinert,
9  *   Carolina Cruz-Neira, Albert Baker
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Library General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Library General Public License for more details.
20  *
21  * You should have received a copy of the GNU Library General Public
22  * License along with this library; if not, write to the
23  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24  * Boston, MA 02111-1307, USA.
25  *
26  * -----------------------------------------------------------------
27  * File:          $RCSfile$
28  * Date modified: $Date$
29  * Version:       $Revision$
30  * -----------------------------------------------------------------
31  *
32  *************** <auto-copyright.pl END do not edit this line> ***************/
33
34
35 #ifndef _VJ_VEC4_
36 #define _VJ_VEC4_
37 //#pragma once
38
39 #include <vjConfig.h>
40 #include <math.h>
41
42 #include <Math/vjMatrix.h>
43 class vjMatrix;
44
45 // --- Define access reps --- //
46 #define VJ_X 0
47 #define VJ_Y 1
48 #define VJ_Z 2
49 #define VJ_W 3
50
51 //: Vector of length 4
52 //!PUBLIC_API:
53 class vjVec4
54 {
55 public:
56    // Constructor
57    vjVec4(float _x, float _y, float _z, float _w)
58    { set(_x, _y, _z, _w);}
59    vjVec4() { set(0.0f,0.0f,0.0f,0.0f);}
60
61 public:
62    //: Set value of Vector
63    void set(float _x, float _y, float _z, float _w) {
64       vec[0] = _x;
65       vec[1] = _y;
66       vec[2] = _z;
67       vec[3] = _w;
68    }
69
70 public:
71    //: Are we equal
72    bool equal(const vjVec4&  _v) const {
73       return (vec[0] == _v[0] &&
74               vec[1] == _v[1] &&
75               vec[2] == _v[2] &&
76               vec[3] == _v[3]);
77    }
78
79    float dot(const vjVec4&  _v) const {
80       return (vec[0] * _v[0] +
81               vec[1] * _v[1] +
82               vec[2] * _v[2]);
83    }
84
85
86    void normalize()
87    {
88       float len = length();
89       vec[0] = vec[0] / len;
90       vec[1] = vec[1] / len;
91       vec[2] = vec[2] / len;
92       vec[3] = vec[3] / len;
93    }
94
95    float length() const
96    {
97       return sqrtf((vec[0]*vec[0])+
98                    (vec[1]*vec[1])+
99                    (vec[2]*vec[2])+
100                    (vec[3]*vec[3]));
101    }
102
103
104    //: Xform the vector
105    // Set vec = (Mat)(Vec)
106    void xform(const vjMatrix& _m, vjVec4 _v);
107
108 public:
109    /// Operators
110    float&  operator [](int i) { return vec[i];}
111    const float&  operator [](int i) const { return vec[i];}
112
113    int operator ==(const vjVec4& _v) const {
114       return (vec[0] == _v[0] &&
115               vec[1] == _v[1] &&
116               vec[2] == _v[2] &&
117               vec[3] == _v[3]);
118    }
119
120    int operator !=(const vjVec4& _v) const {
121       return !(*this == _v);
122    }
123
124 public:
125    // vjVec4 operators, return by value could be slow
126    vjVec4 operator -() const {
127       return vjVec4(-vec[0], -vec[1], -vec[2], -vec[3]);
128    }
129
130    vjVec4 operator +(const vjVec4& _v) const {
131       return vjVec4(vec[0]+_v[0], vec[1]+_v[1], vec[2]+_v[2], vec[3]+_v[3]);
132    }
133
134    vjVec4 operator -(const vjVec4& _v) const {
135       return vjVec4(vec[0]-_v[0], vec[1]-_v[1], vec[2]-_v[2], vec[3]-_v[3]);
136    }
137
138    friend inline vjVec4 operator *(float _s, const vjVec4&);
139    friend inline vjVec4 operator *(const vjVec4& _v, float _s);
140    friend inline vjVec4 operator /(const vjVec4& _v, float _s);
141
142 public:
143    //: Assignment Operators
144    vjVec4&  operator =(const vjVec4& _v) {
145       vec[0] = _v[0]; vec[1] = _v[1];
146       vec[2] = _v[2]; vec[3] = _v[3];
147       return *this;
148    }
149
150    vjVec4& operator *=(float _s) {
151       vec[0] *= _s; vec[1] *= _s;
152       vec[2] *= _s; vec[3] *= _s;
153       return *this;
154    }
155
156    vjVec4& operator /=(float _s) {
157       _s = 1.0/_s;
158       return *this *= _s;
159    }
160
161    vjVec4& operator +=(const vjVec4& _v) {
162       vec[0] += _v[0]; vec[1] += _v[1];
163       vec[2] += _v[2]; vec[3] += _v[3];
164       return *this;
165    }
166
167    vjVec4& operator -=(const vjVec4& _v) {
168       vec[0] -= _v[0]; vec[1] -= _v[1];
169       vec[2] -= _v[2]; vec[3] -= _v[3];
170       return *this;
171    }
172
173 public:
174    float vec[4];        //: The vector data
175 };
176
177 inline vjVec4 operator *(float _s, const vjVec4& _v);
178 inline vjVec4 operator *(const vjVec4& _v, float _s);
179 inline vjVec4 operator /(const vjVec4& _v, float _s);
180 std::ostream& operator<<(std::ostream& out, vjVec4& _v);
181
182 #endif
Note: See TracBrowser for help on using the browser.