TORCS  1.3.10
The Open Racing Car Simulator
Loading...
Searching...
No Matches
v4_t.h
Go to the documentation of this file.
1/***************************************************************************
2
3 file : v4_t.h
4 created : Due Apr 5 13:51:00 CET 2005
5 copyright : (C) 2005 by Bernhard Wymann
6 email : berniw@bluewin.ch
7 version : $Id$
8
9 ***************************************************************************/
10
11/***************************************************************************
12 * *
13 * This program is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU General Public License as published by *
15 * the Free Software Foundation; either version 2 of the License, or *
16 * (at your option) any later version. *
17 * *
18 ***************************************************************************/
19
20/*
21 Template for 4d vector of primitive types (float, double). This template is NOT
22 intended to work with classes which allocate memory. Be aware that there are more
23 efficient methods for doing most of the operations (avoiding temp verctors and make
24 better use of registers). Later I will try to improve the performance (SSE,
25 "abuse" of templates to avoid temporaries).
26*/
27
28#ifndef _LINALG_V4_T_H_
29#define _LINALG_V4_T_H_
30
31#include <math.h>
32
33template<class T> class v4t;
34#ifndef _MSC_VER
35template<class T> v4t<T> operator* (const T s, const v4t<T> & src);
36#endif //_MSC_VER
37
38
39template<class T> class v4t {
40 public:
41 // Friends.
42#ifndef _MSC_VER
43 friend v4t<T> operator* <>(const T s, const v4t<T> &src); // Multiply by scalar.
44#endif //_MSC_VER
45
46 // Constructors.
47 v4t() {}
48 v4t(const v4t<T> &src):x(src.x), y(src.y), z(src.z), w(src.w) {}
49 v4t(const T x, const T y, const T z, const T w):x(x), y(y), z(z), w(w) {}
50 v4t(const T s):x(s), y(s), z(s), w(s) {}
51
52 // Operators.
53 v4t<T>& operator=(const v4t<T> &src); // Assignment.
54 v4t<T>& operator+=(const v4t<T> &src); // Addition.
55 v4t<T>& operator-=(const v4t<T> &src); // Subtraction.
56 v4t<T>& operator*=(const T s); // Multiply with scalar.
57 v4t<T>& operator/=(const T s); // Divide by scalar.
58
59 v4t<T> operator-(void) const; // Negation.
60 v4t<T> operator+(const v4t<T> &src) const; // Addition.
61 v4t<T> operator-(const v4t<T> &src) const; // Subtraction.
62
63 v4t<T> operator*(const T s) const; // Multiply with scalar.
64 T operator*(const v4t<T> &src) const; // Dot product.
65
66 v4t<T> operator/(const T s) const; // Divide by scalar.
67 int operator==(const v4t<T> &src) const; // all fields equal?
68 int operator!=(const v4t<T> &src) const; // not all fields equal?
69
70 // Other methods.
71 T len(void) const;
72 void normalize(void);
73 T dist(const v4t<T> &p) const;
74 int approxEquals(const v4t<T> &cmp, T eps); // Approximately equality per component, eps > 0.0. TODO: Test
75
76 // Data.
77 union {
78 struct { T x, y, z, w; };
79 T vec[4];
80 };
81};
82
83
84// Assignment
85template<class T> inline v4t<T>& v4t<T>::operator=(const v4t<T> &src)
86{
87 x = src.x; y = src.y; z = src.z; w = src.w;
88 return *this;
89}
90
91
92// Addition
93template<class T> inline v4t<T>& v4t<T>::operator+=(const v4t<T> &src)
94{
95 x += src.x; y += src.y; z += src.z; w += src.w;
96 return *this;
97}
98
99
100// Subtraction.
101template<class T> inline v4t<T>& v4t<T>::operator-=(const v4t<T> &src)
102{
103 x -= src.x; y -= src.y; z -= src.z; w -= src.w;
104 return *this;
105}
106
107
108// Multiply with scalar.
109template<class T> inline v4t<T>& v4t<T>::operator*=(const T s)
110{
111 x *= s; y *= s; z *= s; w *= s;
112 return *this;
113}
114
115
116// Divide by scalar.
117template<class T> inline v4t<T>& v4t<T>::operator/=(const T s)
118{
119 x /= s; y /= s; z /= s; w /= s;
120 return *this;
121}
122
123
124// Add *this + src (vector addition) */
125template<class T> inline v4t<T> v4t<T>::operator+(const v4t<T> &src) const
126{
127 return v4t(x + src.x, y + src.y, z + src.z, w + src.w);
128}
129
130
131// Negation of *this
132template<class T> inline v4t<T> v4t<T>::operator-(void) const
133{
134 return v4t(-x, -y, -z, -w);
135}
136
137
138// Compute *this - src (vector subtraction).
139template<class T> inline v4t<T> v4t<T>::operator-(const v4t<T> &src) const
140{
141 return v4t(x - src.x, y - src.y, z - src.z, w - src.w);
142}
143
144
145template<class T> inline int v4t<T>::operator==(const v4t<T> &s) const
146{
147 if (this->x == s.x && this->y == s.y && this->z == s.z && this->w == s.w) {
148 return 1;
149 } else {
150 return 0;
151 }
152}
153
154
155template<class T> inline int v4t<T>::operator!=(const v4t<T> &s) const
156{
157 return !(*this == s);
158}
159
160
161// Approximately equality per component.
162template<class T> inline int v4t<T>::approxEquals(const v4t<T> &cmp, T eps)
163{
164 if (fabs(cmp.x - this->x) < eps &&
165 fabs(cmp.y - this->y) < eps &&
166 fabs(cmp.z - this->z) < eps &&
167 fabs(cmp.w - this->w) < eps)
168 {
169 return 1;
170 } else {
171 return 0;
172 }
173}
174
175
176// Dot (scalar) product.
177template<class T> inline T v4t<T>::operator*(const v4t<T> &src) const
178{
179 return src.x*x + src.y*y + src.z*z + src.w*w;
180}
181
182
183// Multiply vector with scalar (v4t*T)
184template<class T> inline v4t<T> v4t<T>::operator*(const T s) const
185{
186 return v4t(s*x, s*y, s*z, s*w);
187}
188
189
190// friend, Multiply scalar with vector (T*v4t).
191template<class T> inline v4t<T> operator*(const T s, const v4t<T> & src)
192{
193 return v4t<T>(s*src.x, s*src.y, s*src.z, s*src.w);
194}
195
196
197template<class T> inline v4t<T> v4t<T>::operator/(const T s) const
198{
199 return v4t(this->x/s, this->y/s, this->z/s, this->w/s);
200}
201
202
203// Compute the length of the vector.
204template<class T> inline T v4t<T>::len(void) const
205{
206 return sqrt(x*x + y*y + z*z + w*w);
207}
208
209
210// Distance between *this and p.
211template<class T> inline T v4t<T>::dist(const v4t<T> &p) const
212{
213 return sqrt((p.x-x)*(p.x-x) + (p.y-y)*(p.y-y) + (p.z-z)*(p.z-z) + (p.w-w)*(p.w-w));
214}
215
216
217// Normalize the vector.
218template<class T> inline void v4t<T>::normalize(void)
219{
220 T l = this->len();
221 x /= l; y /= l; z /= l; w /= l;
222}
223
224
225#endif // _LINALG_V4_T_H_
226
static Point p[4]
Definition Convex.cpp:54
Definition v4_t.h:39
v4t< T > & operator+=(const v4t< T > &src)
Definition v4_t.h:93
T operator*(const v4t< T > &src) const
Definition v4_t.h:177
v4t< T > & operator*=(const T s)
Definition v4_t.h:109
T dist(const v4t< T > &p) const
Definition v4_t.h:211
v4t(const v4t< T > &src)
Definition v4_t.h:48
int operator==(const v4t< T > &src) const
Definition v4_t.h:145
v4t< T > operator/(const T s) const
Definition v4_t.h:197
v4t< T > operator-(const v4t< T > &src) const
Definition v4_t.h:139
v4t(const T s)
Definition v4_t.h:50
float y
Definition v4_t.h:78
friend v4t< T > operator*(const T s, const v4t< T > &src)
Definition v4_t.h:191
int operator!=(const v4t< T > &src) const
Definition v4_t.h:155
v4t< T > & operator=(const v4t< T > &src)
Definition v4_t.h:85
float z
Definition v4_t.h:78
v4t()
Definition v4_t.h:47
int approxEquals(const v4t< T > &cmp, T eps)
Definition v4_t.h:162
v4t< T > & operator/=(const T s)
Definition v4_t.h:117
void normalize(void)
Definition v4_t.h:218
T len(void) const
Definition v4_t.h:204
v4t< T > & operator-=(const v4t< T > &src)
Definition v4_t.h:101
v4t< T > operator*(const T s) const
Definition v4_t.h:184
v4t(const T x, const T y, const T z, const T w)
Definition v4_t.h:49
v4t< T > operator+(const v4t< T > &src) const
Definition v4_t.h:125
float w
Definition v4_t.h:78
float vec[4]
Definition v4_t.h:79
float x
Definition v4_t.h:78
v4t< T > operator-(void) const
Definition v4_t.h:132
v4t< T > operator*(const T s, const v4t< T > &src)
Definition v4_t.h:191