TORCS  1.3.10
The Open Racing Car Simulator
Loading...
Searching...
No Matches
v2_t.h
Go to the documentation of this file.
1/***************************************************************************
2
3 file : v2_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 2d 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
29#ifndef _LINALG_V2_T_H_
30#define _LINALG_V2_T_H_
31
32#include <math.h>
33#include <tmath/v3_t.h>
34
35template<class T> class v2t;
36#ifndef _MSC_VER
37template<class T> v2t<T> operator*(const T s, const v2t<T> & src);
38#endif // _MSC_VER
39
40template<class T> class v2t {
41 public:
42 // Friends.
43#ifndef _MSC_VER
44 friend v2t<T> operator*<>(const T s, const v2t<T> &src); // Multiply by scalar.
45#endif // _MSC_VER
46
47 // Constructors.
48 v2t() {}
49 v2t(const v2t<T> &src):x(src.x), y(src.y) {}
50 v2t(const T x, const T y):x(x), y(y) {}
51 v2t(const T s):x(s), y(s) {}
52
53 // Operators.
54 v2t<T>& operator=(const v2t<T> &src); // Assignment.
55 v2t<T>& operator=(const v3t<T> &src); // Assignment from v3t<T>
56 v2t<T>& operator+=(const v2t<T> &src); // Addition.
57 v2t<T>& operator-=(const v2t<T> &src); // Subtraction.
58 v2t<T>& operator*=(const T s); // Multiply with scalar.
59 v2t<T>& operator/=(const T s); // Divide by scalar.
60
61 v2t<T> operator-(void) const; // Negation.
62 v2t<T> operator+(const v2t<T> &src) const; // Addition.
63 v2t<T> operator-(const v2t<T> &src) const; // Subtraction.
64
65 v2t<T> operator*(const T s) const; // Multiply with scalar.
66 T operator*(const v2t<T> &src) const; // Dot product.
67 v2t<T> operator/(const T s) const; // Divide by scalar.
68 int operator==(const v2t<T> &src) const; // all fields equal?
69 int operator!=(const v2t<T> &src) const; // not all fields equal?
70
71
72 // Other methods.
73 T len(void) const;
74 void normalize(void);
75 T dist(const v2t<T> &p) const;
76 T cosalpha(const v2t<T> &p2, const v2t<T> &center) const;
77 v2t<T> rotate(const v2t<T> &c, T arc) const;
78 T fakeCrossProduct(const v2t<T>* b) const; // result.z := this X b
79 int approxEquals(const v2t<T> &cmp, T eps); // Approximately equality per component, eps > 0.0. TODO: Test
80
81 // Data.
82 union {
83 struct { T x, y; };
84 T vec[2];
85 };
86};
87
88
89// Assignment
90template<class T> inline v2t<T>& v2t<T>::operator=(const v2t<T> &src)
91{
92 x = src.x; y = src.y;
93 return *this;
94}
95
96
97// Assignment
98template<class T> inline v2t<T>& v2t<T>::operator=(const v3t<T> &src)
99{
100 x = src.x; y = src.y;
101 return *this;
102}
103
104
105// Addition
106template<class T> inline v2t<T>& v2t<T>::operator+=(const v2t<T> &src)
107{
108 x += src.x; y += src.y;
109 return *this;
110}
111
112
113// Subtraction.
114template<class T> inline v2t<T>& v2t<T>::operator-=(const v2t<T> &src)
115{
116 x -= src.x; y -= src.y;
117 return *this;
118}
119
120
121// Multiply with scalar.
122template<class T> inline v2t<T>& v2t<T>::operator*=(const T s)
123{
124 x *= s; y *= s;
125 return *this;
126}
127
128
129// Divide by scalar.
130template<class T> inline v2t<T>& v2t<T>::operator/=(const T s)
131{
132 x /= s; y /= s;
133 return *this;
134}
135
136
137// Add *this + src (vector addition) */
138template<class T> inline v2t<T> v2t<T>::operator+(const v2t<T> &src) const
139{
140 return v2t(x + src.x, y + src.y);
141}
142
143
144// Negation of *this
145template<class T> inline v2t<T> v2t<T>::operator-(void) const
146{
147 return v2t(-x, -y);
148}
149
150
151// Compute *this - src (vector subtraction).
152template<class T> inline v2t<T> v2t<T>::operator-(const v2t<T> &src) const
153{
154 return v2t(x - src.x, y - src.y);
155}
156
157
158template<class T> inline int v2t<T>::operator==(const v2t<T> &s) const
159{
160 if (this->x == s.x && this->y == s.y) {
161 return 1;
162 } else {
163 return 0;
164 }
165}
166
167
168template<class T> inline int v2t<T>::operator!=(const v2t<T> &s) const
169{
170 return !(*this == s);
171}
172
173
174// Approximately equality per component.
175template<class T> inline int v2t<T>::approxEquals(const v2t<T> &cmp, T eps)
176{
177 if (fabs(cmp.x - this->x) < eps &&
178 fabs(cmp.y - this->y) < eps)
179 {
180 return 1;
181 } else {
182 return 0;
183 }
184}
185
186
187// Dot (scalar) product.
188template<class T> inline T v2t<T>::operator*(const v2t<T> &src) const
189{
190 return src.x*x + src.y*y;
191}
192
193
194// Multiply vector with scalar (v2t*T)
195template<class T> inline v2t<T> v2t<T>::operator*(const T s) const
196{
197 return v2t(s*x, s*y);
198}
199
200
201// Multiply scalar with vector (T*v2t).
202template<class T> inline v2t<T> operator*(const T s, const v2t<T> & src)
203{
204 return v2t<T>(s*src.x, s*src.y);
205}
206
207
208template<class T> inline v2t<T> v2t<T>::operator/(const T s) const
209{
210 return v2t(this->x/s, this->y/s);
211}
212
213
214// Compute cosine of the angle between vectors *this-c and p2-c.
215template<class T> inline T v2t<T>::cosalpha(const v2t<T> &p2, const v2t<T> &c) const
216{
217 v2t l1 = *this-c;
218 v2t l2 = p2 - c;
219 return (l1*l2)/(l1.len()*l2.len());
220}
221
222
223// Rotate vector arc radians around center c.
224template<class T> inline v2t<T> v2t<T>::rotate(const v2t<T> &c, T arc) const
225{
226 v2t d = *this-c;
227 T sina = sin(arc), cosa = cos(arc);
228 return c + v2t(d.x*cosa-d.y*sina, d.x*sina+d.y*cosa);
229}
230
231
232// Compute the length of the vector.
233template<class T> inline T v2t<T>::len(void) const
234{
235 return sqrt(x*x+y*y);
236}
237
238
239// Distance between *this and p.
240template<class T> inline T v2t<T>::dist(const v2t<T> &p) const
241{
242 return sqrt((p.x-x)*(p.x-x)+(p.y-y)*(p.y-y));
243}
244
245
246// Normalize the vector.
247template<class T> inline void v2t<T>::normalize(void)
248{
249 T l = this->len();
250 x /= l; y /= l;
251}
252
253
254// "Cross Product" with z=0, good to check the order of the vectors.
255template<class T> inline T v2t<T>::fakeCrossProduct(const v2t<T>* b) const
256{
257 return this->x*b->y - this->y*b->x;
258}
259
260
261#endif // _LINALG_V2_T_H_
262
static Point p[4]
Definition Convex.cpp:54
Definition v2_t.h:40
int approxEquals(const v2t< T > &cmp, T eps)
Definition v2_t.h:175
int operator!=(const v2t< T > &src) const
Definition v2_t.h:168
T fakeCrossProduct(const v2t< T > *b) const
Definition v2_t.h:255
v2t(const T x, const T y)
Definition v2_t.h:50
v2t(const v2t< T > &src)
Definition v2_t.h:49
v2t< T > & operator=(const v3t< T > &src)
Definition v2_t.h:98
friend v2t< T > operator*(const T s, const v2t< T > &src)
Definition v2_t.h:202
v2t< T > operator*(const T s) const
Definition v2_t.h:195
float vec[2]
Definition v2_t.h:84
void normalize(void)
Definition v2_t.h:247
v2t< T > operator-(const v2t< T > &src) const
Definition v2_t.h:152
v2t< T > operator-(void) const
Definition v2_t.h:145
float y
Definition v2_t.h:83
v2t< T > operator/(const T s) const
Definition v2_t.h:208
v2t< T > & operator*=(const T s)
Definition v2_t.h:122
v2t(const T s)
Definition v2_t.h:51
int operator==(const v2t< T > &src) const
Definition v2_t.h:158
v2t< T > & operator/=(const T s)
Definition v2_t.h:130
float x
Definition v2_t.h:83
v2t< T > rotate(const v2t< T > &c, T arc) const
Definition v2_t.h:224
T len(void) const
Definition v2_t.h:233
T cosalpha(const v2t< T > &p2, const v2t< T > &center) const
Definition v2_t.h:215
T dist(const v2t< T > &p) const
Definition v2_t.h:240
v2t< T > & operator-=(const v2t< T > &src)
Definition v2_t.h:114
v2t< T > & operator=(const v2t< T > &src)
Definition v2_t.h:90
v2t< T > operator+(const v2t< T > &src) const
Definition v2_t.h:138
v2t< T > & operator+=(const v2t< T > &src)
Definition v2_t.h:106
T operator*(const v2t< T > &src) const
Definition v2_t.h:188
v2t()
Definition v2_t.h:48
Definition v3_t.h:40
T y
Definition v3_t.h:82
T x
Definition v3_t.h:82
v2t< T > operator*(const T s, const v2t< T > &src)
Definition v2_t.h:202