JNR
laPoint_novec.h
1 /*
2 
3 Jump'n'Run Engine
4 http://www.atanaslaskov.com/jnr/
5 
6 BSD LICENSE
7 Copyright (c) 2007-2013, Atanas Laskov
8 All rights reserved.
9 
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12 1. Redistributions of source code must retain the above copyright notice,
13 this list of conditions and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright notice,
15 this list of conditions and the following disclaimer in the documentation
16 and/or other materials provided with the distribution.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 */
29 
31 
32 
40 
41 class laPoint2 : public laLoadableObj
42 {
43 protected:
44  /*__declspec( align( 16 ) )*/ double _v[2];
45  friend class laPoint3;
46 
47 public:
48  // Indexing
49  //
50  inline double& operator[] (unsigned short n) { /*ASSERT(n<2, "%d out of bounds", n);*/ return _v[n]; }
51  inline const double& operator() (unsigned short n) const{ /*ASSERT(n<2, "%d out of bounds", n);*/ return _v[n]; }
52 
53  inline const double& x() const{ return _v[0]; }
54  inline const double& y() const{ return _v[1]; }
55  inline void x(const double &ax) { _v[0] = ax; }
56  inline void y(const double &ay) { _v[1] = ay; }
57 
58  inline double* data() { return _v; } // NOTE: This could be operator double*(), but that's harder to debug
59  inline const double* cdata() const{ return _v; }
60 
61  // Constructors
62  //
63  laPoint2() { _v[0]=_v[1]=0; /* don't waste time = 0*/ };
64  laPoint2( const double &x, const double &y ) { _v[0] = x; _v[1] = y; }
65  laPoint2( const laPoint2 &pt ) { _v[0] = pt(0); _v[1] = pt(1); }
66  laPoint2( const double *v ) { _v[0] = v[0]; _v[1] = v[1]; }
67 
68  virtual void load(class laFileParser *fp);
69 
70  // Basic vertex operations
71  //
72  inline const laPoint2& operator*=(const double &m) { _v[0] *=m; _v[1] *=m; return *this; };
73  inline const laPoint2& operator/=(const double &m) { _v[0] /=m; _v[1] /=m; return *this; };
74  inline const laPoint2& operator+=(const laPoint2 &pt) { _v[0] += pt(0); _v[1] += pt(1); return *this; };
75  inline const laPoint2& operator-=(const laPoint2 &pt) { _v[0] -= pt(0); _v[1] -= pt(1); return *this; };
76  inline const laPoint2& operator*=(const laPoint2 &pt) { _v[0] *=pt(0); _v[1]*=pt(1); return *this; };
77  inline const laPoint2& operator/=(const laPoint2 &pt) { _v[0] /=pt(0); _v[1]/=pt(1); return *this; };
78 
79  // Dot product of two vectors
80  //
81  inline double dot(const laPoint2 &p) const{
82  return x()*p.x() + y()*p.y();
83  }
84 
85  // Cross product of two vectors
86  // NOTE: This is a scalar in 2D
87  //
88  inline double cross(const laPoint2 &p) const{
89  return x()*p.y() - y()*p.x();
90  }
91 
92  // Vector lenght
93  //
94  inline double lenght() const{
95  return sqrt( _v[0]*_v[0] + _v[1]*_v[1] );
96  };
97 
98  inline double lenght_sq() const{
99  return ( _v[0]*_v[0] + _v[1]*_v[1] );
100  };
101 
102  // Normalize the vector
103  //
104  inline void normalize(){
105  double len = lenght();
106  ASSERT(len!=0, "Division by zero.");
107 
108  /*if(len!=0)*/ (*this) /= len;
109  }
110 
111  // Draw (NOTE: for debugging only)
112  void draw(class laRenderer *r);
113 };
114 
115 
116 // More operators (2d)
117 //
118 inline const laPoint2 operator+(const laPoint2 &pt1, const laPoint2 &pt2)
119  { return laPoint2( pt1(0)+pt2(0), pt1(1)+pt2(1) ); };
120 
121 inline const laPoint2 operator-(const laPoint2 &pt1, const laPoint2 &pt2)
122  { return laPoint2( pt1(0)-pt2(0), pt1(1)-pt2(1) ); };
123 
124 inline const laPoint2 operator*(const laPoint2 &pt, const double &a)
125  { return laPoint2( pt(0)*a, pt(1)*a ); };
126 
127 inline const laPoint2 operator/(const laPoint2 &pt, const double &a)
128  { return laPoint2( pt(0)/a, pt(1)/a ); };
129 
130 inline const laPoint2 operator*(const double &a, const laPoint2 &pt)
131  { return laPoint2( a*pt(0), a*pt(1) ); };
132 
133 inline const laPoint2 operator/(const double &a, const laPoint2 &pt)
134  { return laPoint2( a/pt(0), a/pt(1) ); };
135 
136 
137 class laPoint3 : public laLoadableObj
138 {
139 protected:
140  /*__declspec( align( 16 ) )*/ double _v[3];
141 
142 public:
143  // Indexing
144  //
145  inline double& operator[] (unsigned short n) { /*ASSERT(n<3, "%d out of bounds", n);*/ return _v[n]; }
146  inline const double& operator() (unsigned short n) const{ /*ASSERT(n<3, "%d out of bounds", n);*/ return _v[n]; }
147 
148  inline const double& x() const{ return _v[0]; }
149  inline const double& y() const{ return _v[1]; }
150  inline const double& z() const{ return _v[2]; }
151  inline void x(const double &ax) { _v[0] = ax; }
152  inline void y(const double &ay) { _v[1] = ay; }
153  inline void z(const double &az) { _v[2] = az; }
154 
155  inline double* data() { return _v; } // NOTE: This could be operator double*(), but that's harder to debug
156  inline const double* cdata() const{ return _v; }
157 
158  // Constructors
159  //
160  laPoint3() { _v[0]=_v[1]=_v[2]=0; /* TODO: don't waste time = 0*/ };
161  laPoint3( const double &x, const double &y, const double &z = 0) { _v[0] = x; _v[1] = y; _v[2] = z; }
162  laPoint3( const laPoint3 &pt ) { _v[0]=pt(0); _v[1]=pt(1); _v[2]=pt(2); };
163  laPoint3( const double *v ) { _v[0]=v[0]; _v[1]=v[1]; _v[2]=v[2]; }
164 
165  inline operator const laPoint2() const { return laPoint2( _v ); }
166  laPoint3( const laPoint2 &pt ) { _v[0]=pt(0); _v[1]=pt(1); _v[2]=0; }
167 
168  virtual void load(class laFileParser *fp);
169 
170  // Basic vertex operations
171  //
172  inline const laPoint3& operator*=(const double &m) { _v[0]*=m; _v[1]*=m; _v[2]*=m; return *this; };
173  inline const laPoint3& operator/=(const double &m) { _v[0]/=m; _v[1]/=m; _v[2]/=m; return *this; };
174  inline const laPoint3& operator+=(const laPoint3 &pt) { _v[0]+=pt(0); _v[1]+=pt(1); _v[2]+=pt(2); return *this; };
175  inline const laPoint3& operator-=(const laPoint3 &pt) { _v[0]-=pt(0); _v[1]-=pt(1); _v[2]-=pt(2); return *this; };
176  inline const laPoint3& operator*=(const laPoint3 &pt) { _v[0]*=pt(0); _v[1]*=pt(1); _v[2]*=pt(2); return *this; };
177  inline const laPoint3& operator/=(const laPoint3 &pt) { _v[0]/=pt(0); _v[1]/=pt(1); _v[2]*=pt(2); return *this; };
178 
179  // Dot product of two vectors
180  //
181  inline double dot(const laPoint3 &p) const{
182  return _v[0]*p(0) + _v[1]*p(1) + _v[2]*p(2);
183  }
184 
185  // Dot product of two vectors
186  //
187  inline laPoint3 cross(const laPoint3 &p) const
188  {
189  return laPoint3( _v[1] * p(2) - _v[2] * p(1),
190  _v[2] * p(0) - _v[0] * p(2),
191  _v[0] * p(1) - _v[1] * p(0) );
192  }
193 
194  // Lenght of the vector
195  //
196  inline double lenght() const{
197  return sqrt( _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] );
198  }
199 
200  inline double lenght_sq() const{
201  return ( _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] );
202  }
203 
204  // Normalize the vector
205  //
206  inline void normalize(){
207  double len = lenght();
208  ASSERT(len!=0, "Division by zero.");
209 
210  (*this) /= len;
211  }
212 
213  // Angles
214  //
215  inline double angle_cos(const laPoint3 &p) const{
216  return dot(p) / sqrt( lenght_sq() * p.lenght_sq() );
217  }
218 
219  inline double angle(const laPoint3 &p) const{
220  return acos( angle_cos(p) ); //TODO: this sohould be atan2
221  }
222 
223  // Draw (NOTE: for debugging onl_v[1])
224  void draw(class laRenderer *r);
225 };
226 
227 // More operators (3d)
228 //
229 inline const laPoint3 operator+(const laPoint3 &pt1, const laPoint3 &pt2)
230  { return laPoint3( pt1(0)+pt2(0), pt1(1)+pt2(1), pt1(2)+pt2(2) ); };
231 
232 inline const laPoint3 operator-(const laPoint3 &pt1, const laPoint3 &pt2)
233  { return laPoint3( pt1(0)-pt2(0), pt1(1)-pt2(1), pt1(2)-pt2(2) ); };
234 
235 inline const laPoint3 operator*(const laPoint3 &pt, const double &a)
236  { return laPoint3( pt(0)*a, pt(1)*a, pt(2)*a ); };
237 
238 inline const laPoint3 operator/(const laPoint3 &pt, const double &a)
239  { return laPoint3( pt(0)/a, pt(1)/a, pt(2)/a ); };
240 
241 inline const laPoint3 operator*(const double &a, const laPoint3 &pt)
242  { return laPoint3( a*pt(0), a*pt(1), a*pt(2) ); };
243 
244 inline const laPoint3 operator/(const double &a, const laPoint3 &pt)
245  { return laPoint3( a/pt(0), a/pt(1), a/pt(2) ); };
246 
2D Point
Definition: laPoint_novec.h:41
Interface for loadable objects.
Definition: laLoadableObj.h:43
Virtual interface for the Engine graphics renderer.
Definition: laRenderer.h:98
File Parser.
Definition: laFileParser.h:41