JNR
laPoint_vec.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 #include <Eigen/Array>
42 #include <Eigen/Core>
43 #include <Eigen/Geometry>
44 USING_PART_OF_NAMESPACE_EIGEN
45 
46 //#pragma pack( show )
47 class laPoint2 :public laLoadableObj
48 {
49 protected:
50  Vector2d _v;
51 
52  friend inline const laPoint2 operator+(const laPoint2 &pt1, const laPoint2 &pt2);
53  friend inline const laPoint2 operator-(const laPoint2 &pt1, const laPoint2 &pt2);
54 
55  friend inline const laPoint2 operator*(const laPoint2 &pt, const double &a);
56  friend inline const laPoint2 operator/(const laPoint2 &pt, const double &a);
57  friend inline const laPoint2 operator*(const double &a, const laPoint2 &pt);
58  friend inline const laPoint2 operator/(const double &a, const laPoint2 &pt);
59 
60 public:
61  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
62 
63  inline double x() const{ return _v(0); }
64  inline double y() const{ return _v(1); }
65  inline void x(const double &x) { _v[0] = x; }
66  inline void y(const double &y) { _v[1] = y; }
67 
68  laPoint2() { _v.setZero(); /* TODO: don't waste time = 0*/ };
69  laPoint2( const double &ax, const double &ay ) { _v[0] = ax; _v[1] = ay; }
70  laPoint2( const laPoint2 &pt ) { _v = pt._v; }
71  laPoint2( const Vector2d &pt ) { _v = _v; }
72 
73  inline double* data() { return _v.data(); } // NOTE: This could be operator double*(), but that's harder to debug
74 
75  virtual void load(class laFileParser *fp);
76 
77  // Basic vertex operations
78  //
79  inline const laPoint2& operator*=(const double &m) { _v *= m; return *this; };
80  inline const laPoint2& operator/=(const double &m) { _v /= m; return *this; };
81  inline const laPoint2& operator+=(const laPoint2 &pt) { _v += pt._v; return *this; };
82  inline const laPoint2& operator-=(const laPoint2 &pt) { _v -= pt._v; return *this; };
83 
84  // Dot product of two vectors
85  //
86  inline double dot(const laPoint2 &p) const{
87  //return _v.dot( p._v );
88  return x()*p.x() + y()*p.y();
89  }
90 
91  // Cross product of two vectors
92  // NOTE: This is a scalar in 2D
93  //
94  inline double cross(const laPoint2 &p) const{
95  //return _v.cwise() * p._v;
96  return x()*p.y() - y()*p.x();
97  }
98 
99  // Vector lenght
100  //
101  inline double lenght() const{
102  return sqrt( x()*x() + y()*y() );
103  };
104 
105  inline double lenght_sq() const{
106  return ( x()*x() + y()*y() );
107  };
108 
109  // Normalize the vector
110  //
111  inline void normalize(){
112  //double len = lenght();
113  //if(len!=0) (*this) /= len;
114  return _v.normalize();
115  }
116 
117  // Draw (NOTE: for debugging only)
118  void draw(class laRenderer *r);
119 };
120 
121 // More operators (2d)
122 //
123 inline const laPoint2 operator+(const laPoint2 &pt1, const laPoint2 &pt2)
124  { return laPoint2( pt1._v + pt2._v ); };
125 
126 inline const laPoint2 operator-(const laPoint2 &pt1, const laPoint2 &pt2)
127  { return laPoint2( pt1._v - pt2._v ); };
128 
129 inline const laPoint2 operator*(const laPoint2 &pt, const double &a)
130  { return laPoint2( pt._v * a ); };
131 
132 inline const laPoint2 operator/(const laPoint2 &pt, const double &a)
133  { return laPoint2( pt._v / a ); };
134 
135 inline const laPoint2 operator*(const double &a, const laPoint2 &pt)
136  { return laPoint2( a * pt._v ); };
137 
138 inline const laPoint2 operator/(const double &a, const laPoint2 &pt)
139  { return laPoint2( a / pt._v ); };
140 
141 
142 class laPoint3 : public laLoadableObj
143 {
144 protected:
145  /*__declspec( align( 16 ) )*/ double _v[3];
146 
147 public:
148  // Indexing
149  //
150  inline double& operator[] (unsigned short n) { /*ASSERT(n<3, "%d out of bounds", n);*/ return _v[n]; }
151  inline const double& operator() (unsigned short n) const{ /*ASSERT(n<3, "%d out of bounds", n);*/ return _v[n]; }
152 
153  inline const double& x() const{ return _v[0]; }
154  inline const double& y() const{ return _v[1]; }
155  inline const double& z() const{ return _v[2]; }
156  inline void x(const double &ax) { _v[0] = ax; }
157  inline void y(const double &ay) { _v[1] = ay; }
158  inline void z(const double &az) { _v[2] = az; }
159 
160  inline double* data() { return _v; } // NOTE: This could be operator double*(), but that's harder to debug
161 
162  // Constructors
163  //
164  laPoint3() { _v[0]=_v[1]=_v[2]=0; /* TODO: don't waste time = 0*/ };
165  laPoint3( const double &x, const double &y, const double &z = 0) { _v[0] = x; _v[1] = y; _v[2] = z; }
166  laPoint3( const laPoint3 &pt ) { _v[0]=pt(0); _v[1]=pt(1); _v[2]=pt(2); };
167  laPoint3( const double *v ) { _v[0]=v[0]; _v[1]=v[1]; _v[2]=v[2]; }
168 
169  inline operator const laPoint2() const { return laPoint2( _v ); }
170  laPoint3( const laPoint2 &pt ) { _v[0]=pt(0); _v[1]=pt(1); _v[2] = 0; }
171 
172  virtual void load(class laFileParser *fp);
173 
174  // Basic vertex operations
175  //
176  inline const laPoint3& operator*=(const double &m) { _v[0]*=m; _v[1]*=m; _v[2]*=m; return *this; };
177  inline const laPoint3& operator/=(const double &m) { _v[0]/=m; _v[1]/=m; _v[2]/=m; return *this; };
178  inline const laPoint3& operator+=(const laPoint3 &pt) { _v[0]+=pt(0); _v[1]+=pt(1); _v[2]+=pt(2); return *this; };
179  inline const laPoint3& operator-=(const laPoint3 &pt) { _v[0]-=pt(0); _v[1]-=pt(1); _v[2]-=pt(2); return *this; };
180  inline const laPoint3& operator*=(const laPoint3 &pt) { _v[0]*=pt(0); _v[1]*=pt(1); _v[2]*=pt(2); return *this; };
181  inline const laPoint3& operator/=(const laPoint3 &pt) { _v[0]/=pt(0); _v[1]/=pt(1); _v[2]*=pt(2); return *this; };
182 
183  // Dot product of two vectors
184  //
185  inline double dot(const laPoint3 &p) const{
186  return _v[0]*p(0) + _v[1]*p(1) + _v[2]*p(2);
187  }
188 
189  // Dot product of two vectors
190  //
191  inline laPoint3 cross(const laPoint3 &p) const{
192  laPoint3 ptCross;
193 
194  ptCross._v[0] = _v[1] * p(2) - _v[2] * p(1);
195  ptCross._v[1] = _v[2] * p(0) - _v[0] * p(2);
196  ptCross._v[2] = _v[0] * p(1) - _v[1] * p(0);
197 
198  return ptCross;
199  }
200 
201  // Lenght of the vector
202  //
203  inline double lenght() const{
204  return sqrt(_v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2]);
205  }
206 
207  inline double lenght_sq() const{
208  return (_v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2]);
209  }
210 
211  // Normalize the vector
212  //
213  inline void normalize(){
214  double len = lenght();
215  ASSERT(len!=0, "Attempted division by zero.");
216 
217  (*this) /= len;
218  }
219 
220  // Angles
221  //
222  inline double angle_cos(laPoint3 &p) const{
223  return dot(p) / sqrt( lenght_sq() * p.lenght_sq() );
224  }
225 
226  inline double angle(laPoint3 &p) const{
227  return acos( angle_cos(p) ); //TODO: this sohould be atan2
228  }
229 
230  // Draw (NOTE: for debugging onl_v[1])
231  void draw(class laRenderer *r);
232 };
233 
234 // More operators (3d)
235 //
236 inline const laPoint3 operator+(const laPoint3 &pt1, const laPoint3 &pt2)
237  { return laPoint3( pt1(0)+pt2(0), pt1(1)+pt2(1), pt1(2)+pt2(2) ); };
238 
239 inline const laPoint3 operator-(const laPoint3 &pt1, const laPoint3 &pt2)
240  { return laPoint3( pt1(0)-pt2(0), pt1(1)-pt2(1), pt1(2)-pt2(2) ); };
241 
242 inline const laPoint3 operator*(const laPoint3 &pt, const double &a)
243  { return laPoint3( pt(0)*a, pt(1)*a, pt(2)*a ); };
244 
245 inline const laPoint3 operator/(const laPoint3 &pt, const double &a)
246  { return laPoint3( pt(0)/a, pt(1)/a, pt(2)/a ); };
247 
248 inline const laPoint3 operator*(const double &a, const laPoint3 &pt)
249  { return laPoint3( a*pt(0), a*pt(1), a*pt(2) ); };
250 
251 inline const laPoint3 operator/(const double &a, const laPoint3 &pt)
252  { return laPoint3( a/pt(0), a/pt(1), a/pt(2) ); };
253 
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