1 /*
  2  * Copyright �� 2008 Danny Baumann
  3  *
  4  * Permission to use, copy, modify, distribute, and sell this software
  5  * and its documentation for any purpose is hereby granted without
  6  * fee, provided that the above copyright notice appear in all copies
  7  * and that both that copyright notice and this permission notice
  8  * appear in supporting documentation, and that the name of
  9  * Dennis Kasprzyk not be used in advertising or publicity pertaining to
 10  * distribution of the software without specific, written prior permission.
 11  * Dennis Kasprzyk makes no representations about the suitability of this
 12  * software for any purpose. It is provided "as is" without express or
 13  * implied warranty.
 14  *
 15  * DENNIS KASPRZYK DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 16  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
 17  * NO EVENT SHALL DENNIS KASPRZYK BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 18  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
 19  * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 20  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 21  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 22  *
 23  * Authors: Danny Baumann <maniac@compiz-fusion.org>
 24  */
 25 
 26 /*
 27  * Copyright (C) 1999-2005  Brian Paul   All Rights Reserved.
 28  *
 29  * Permission is hereby granted, free of charge, to any person obtaining a
 30  * copy of this software and associated documentation files (the "Software"),
 31  * to deal in the Software without restriction, including without limitation
 32  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 33  * and/or sell copies of the Software, and to permit persons to whom the
 34  * Software is furnished to do so, subject to the following conditions:
 35  *
 36  * The above copyright notice and this permission notice shall be included
 37  * in all copies or substantial portions of the Software.
 38  *
 39  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 40  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 41  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 42  * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 43  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 44  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 45  *
 46  * From Mesa 3-D graphics library.
 47  */
 48 
 49 #include <string.h>
 50 #include <math.h>
 51 #include <opengl/vector.h>
 52 
 53 GLVector::GLVector ()
 54 {
 55     memset (v, 0, sizeof (v));
 56 }
 57 
 58 GLVector::GLVector (float x,
 59 			float y,
 60 			float z,
 61 			float w)
 62 {
 63     v[0] = x;
 64     v[1] = y;
 65     v[2] = z;
 66     v[3] = w;
 67 }
 68 
 69 float&
 70 GLVector::operator[] (int item)
 71 {
 72     return v[item];
 73 }
 74 
 75 float&
 76 GLVector::operator[] (VectorCoordsEnum coord)
 77 {
 78     int item = (int) coord;
 79     return v[item];
 80 }
 81 
CID 10887 - PW.USELESS_TYPE_QUALIFIER_ON_RETURN_TYPE
type qualifier on return type is meaningless
 82 const float
 83 GLVector::operator[] (int item) const
 84 {
 85     return v[item];
 86 }
 87 
CID 10887 - PW.USELESS_TYPE_QUALIFIER_ON_RETURN_TYPE
type qualifier on return type is meaningless
 88 const float
 89 GLVector::operator[] (VectorCoordsEnum coord) const
 90 {
 91     int item = (int) coord;
 92     return v[item];
 93 }
 94 
 95 GLVector&
 96 GLVector::operator+= (const GLVector& rhs)
 97 {
 98     int i;
 99 
100     for (i = 0; i < 4; i++)
101 	v[i] += rhs[i];
102 
103     return *this;
104 }
105 
106 GLVector
107 operator+ (const GLVector& lhs,
108 	   const GLVector& rhs)
109 {
110     GLVector result;
111     int        i;
112 
113     for (i = 0; i < 4; i++)
114 	result[i] = lhs[i] + rhs[i];
115 
116     return result;
117 }
118 
119 GLVector&
120 GLVector::operator-= (const GLVector& rhs)
121 {
122     int i;
123 
124     for (i = 0; i < 4; i++)
125 	v[i] -= rhs[i];
126 
127     return *this;
128 }
129 
130 GLVector
131 operator- (const GLVector& lhs,
132 	   const GLVector& rhs)
133 {
134     GLVector result;
135     int        i;
136 
137     for (i = 0; i < 4; i++)
138 	result[i] = lhs[i] - rhs[i];
139 
140     return result;
141 }
142 
143 GLVector
144 operator- (const GLVector& vector)
145 {
146     GLVector result;
147     int        i;
148 
149     for (i = 0; i < 4; i++)
150 	result[i] = -vector[i];
151 
152     return result;
153 }
154 
155 GLVector&
156 GLVector::operator*= (const float k)
157 {
158     int i;
159 
160     for (i = 0; i < 4; i++)
161 	v[i] *= k;
162 
163     return *this;
164 }
165 
166 float
167 operator* (const GLVector& lhs,
168 	   const GLVector& rhs)
169 {
170     float result = 0;
171     int   i;
172 
173     for (i = 0; i < 4; i++)
174 	result += lhs[i] * rhs[i];
175 
176     return result;
177 }
178 
179 GLVector
180 operator* (const float       k,
181 	   const GLVector& vector)
182 {
183     GLVector result;
184     int        i;
185 
186     for (i = 0; i < 4; i++)
187 	result[i] = k * vector[i];
188 
189     return result;
190 }
191 
192 GLVector
193 operator* (const GLVector& vector,
194 	   const float       k)
195 {
196     return k * vector;
197 }
198 
199 GLVector&
200 GLVector::operator/= (const float k)
201 {
202     int i;
203 
204     for (i = 0; i < 4; i++)
205 	v[i] /= k;
206 
207     return *this;
208 }
209 
210 GLVector
211 operator/ (const GLVector& vector,
212 	   const float       k)
213 {
214     GLVector result;
215     int        i;
216 
217     for (i = 0; i < 4; i++)
218 	result[i] = vector[i] / k;
219 
220     return result;
221 }
222 
223 GLVector&
224 GLVector::operator^= (const GLVector& vector)
225 {
226     *this = *this ^ vector;
227     return *this;
228 }
229 
230 GLVector
231 operator^ (const GLVector& lhs,
232 	   const GLVector& rhs)
233 {
234     GLVector result;
235 
236     result[0] = lhs[1] * rhs[2] - lhs[2] * rhs[1];
237     result[1] = lhs[2] * rhs[0] - lhs[0] * rhs[2];
238     result[2] = lhs[0] * rhs[1] - lhs[1] * rhs[0];
239     result[3] = 0.0f;
240 
241     return result;
242 }
243 
244 float
245 GLVector::norm ()
246 {
247     if (v[3] != 0.0)
248 	return 1.0;
249     return sqrt ((v[0] * v[0]) + (v[1] * v[1]) + (v[2] * v[2]));
250 }
251 
252 GLVector &
253 GLVector::normalize ()
254 {
255     float normal = norm ();
256 
257     /* Vector is not homogenous */
258     if (normal == 1.0)
259 	return *this;
260 
261     for (unsigned int i = 0; i < 3; i++)
262 	v[i] /= normal;
263     return *this;
264 }
265 
266 GLVector &
267 GLVector::homogenize ()
268 {
269     if (v[3] ==0)
270 	return *this;
271 
272     for (unsigned int i = 0; i < 4; i++)
273 	v[i] /= v[3];
274 
275     return *this;
276 }