KiCad PCB EDA Suite
vector2d.h
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KICAD, a free EDA CAD application.
3  *
4  * Copyright (C) 2010 Virtenio GmbH, Torsten Hueter, torsten.hueter <at> virtenio.de
5  * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
6  * Copyright (C) 2012 Kicad Developers, see change_log.txt for contributors.
7  * Copyright (C) 2013 CERN
8  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, you may find one here:
22  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
23  * or you may search the http://www.gnu.org website for the version 2 license,
24  * or you may write to the Free Software Foundation, Inc.,
25  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
26  */
27 
28 #ifndef VECTOR2D_H_
29 #define VECTOR2D_H_
30 
31 #include <cmath>
32 #include <climits>
33 #include <iostream>
34 #include <sstream>
35 #include <cmath>
36 
37 #include <math/math_util.h>
38 
39 #ifdef WX_COMPATIBILITY
40  #include <wx/gdicmn.h>
41 #endif
42 
47 template <class T>
49 {
52  typedef T extended_type;
53 };
54 
55 template <>
56 struct VECTOR2_TRAITS<int>
57 {
58  typedef int64_t extended_type;
59  static const extended_type ECOORD_MAX = 0x7fffffffffffffffULL;
60  static const extended_type ECOORD_MIN = 0x8000000000000000ULL;
61 };
62 
63 // Forward declarations for template friends
64 template <class T>
65 class VECTOR2;
66 template <class T>
67 std::ostream& operator<<( std::ostream& aStream, const VECTOR2<T>& aVector );
68 
77 template <class T = int>
78 class VECTOR2 : public VECTOR2_TRAITS<T>
79 {
80 public:
82  typedef T coord_type;
83 
84  T x, y;
85 
86  // Constructors
87 
89  VECTOR2();
90 
91 #ifdef WX_COMPATIBILITY
92  VECTOR2( const wxPoint& aPoint );
94 
96  VECTOR2( const wxSize& aSize );
97 #endif
98 
100  VECTOR2( T x, T y );
101 
104  template <typename CastingType>
106  {
107  x = (T) aVec.x;
108  y = (T) aVec.y;
109  }
110 
113  template <typename CastedType>
115  {
116  return VECTOR2<CastedType>( (CastedType) x, (CastedType) y );
117  }
118 
120  // virtual ~VECTOR2();
121 
128  T EuclideanNorm() const;
129 
136  extended_type SquaredEuclideanNorm() const;
137 
138 
144  VECTOR2<T> Perpendicular() const;
145 
152  VECTOR2<T> Resize( T aNewLength ) const;
153 
159  double Angle() const;
160 
167  VECTOR2<T> Rotate( double aAngle ) const;
168 
174  const std::string Format() const;
175 
180  extended_type Cross( const VECTOR2<T>& aVector ) const;
181 
186  extended_type Dot( const VECTOR2<T>& aVector ) const;
187 
188 
189  // Operators
190 
192  VECTOR2<T>& operator=( const VECTOR2<T>& aVector );
193 
195  VECTOR2<T> operator+( const VECTOR2<T>& aVector ) const;
196 
198  VECTOR2<T> operator+( const T& aScalar ) const;
199 
201  VECTOR2<T>& operator+=( const VECTOR2<T>& aVector );
202 
204  VECTOR2<T>& operator+=( const T& aScalar );
205 
207  VECTOR2<T> operator-( const VECTOR2<T>& aVector ) const;
208 
210  VECTOR2<T> operator-( const T& aScalar ) const;
211 
213  VECTOR2<T>& operator-=( const VECTOR2<T>& aVector );
214 
216  VECTOR2<T>& operator-=( const T& aScalar );
217 
220 
222  extended_type operator*( const VECTOR2<T>& aVector ) const;
223 
225  VECTOR2<T> operator*( const T& aFactor ) const;
226 
228  VECTOR2<T> operator/( const T& aFactor ) const;
229 
231  bool operator==( const VECTOR2<T>& aVector ) const;
232 
234  bool operator!=( const VECTOR2<T>& aVector ) const;
235 
237  bool operator<( const VECTOR2<T>& aVector ) const;
238  bool operator<=( const VECTOR2<T>& aVector ) const;
239 
241  bool operator>( const VECTOR2<T>& aVector ) const;
242  bool operator>=( const VECTOR2<T>& aVector ) const;
243 
244  friend std::ostream & operator<< <T> ( std::ostream & stream, const VECTOR2<T> &vector );
245 };
246 
247 
248 // ----------------------
249 // --- Implementation ---
250 // ----------------------
251 
252 template <class T>
254 {
255  x = y = 0.0;
256 }
257 
258 
259 #ifdef WX_COMPATIBILITY
260 template <class T>
261 VECTOR2<T>::VECTOR2( wxPoint const& aPoint )
262 {
263  x = T( aPoint.x );
264  y = T( aPoint.y );
265 }
266 
267 
268 template <class T>
269 VECTOR2<T>::VECTOR2( wxSize const& aSize )
270 {
271  x = T( aSize.x );
272  y = T( aSize.y );
273 }
274 #endif
275 
276 template <class T>
278 {
279  x = aX;
280  y = aY;
281 }
282 
283 
284 template <class T>
286 {
287  return sqrt( (extended_type) x * x + (extended_type) y * y );
288 }
289 
290 
291 template <class T>
293 {
294  return (extended_type) x * x + (extended_type) y * y;
295 }
296 
297 
298 template <class T>
299 double VECTOR2<T>::Angle() const
300 {
301  return atan2( (double) y, (double) x );
302 }
303 
304 
305 template <class T>
307 {
308  VECTOR2<T> perpendicular( -y, x );
309  return perpendicular;
310 }
311 
312 
313 template <class T>
315 {
316  x = aVector.x;
317  y = aVector.y;
318  return *this;
319 }
320 
321 
322 template <class T>
324 {
325  x += aVector.x;
326  y += aVector.y;
327  return *this;
328 }
329 
330 
331 template <class T>
333 {
334  x += aScalar;
335  y += aScalar;
336  return *this;
337 }
338 
339 
340 template <class T>
342 {
343  x -= aVector.x;
344  y -= aVector.y;
345  return *this;
346 }
347 
348 
349 template <class T>
351 {
352  x -= aScalar;
353  y -= aScalar;
354  return *this;
355 }
356 
357 
362 template <class T>
363 VECTOR2<T> VECTOR2<T>::Rotate( double aAngle ) const
364 {
365  // Avoid 0 radian rotation, case very frequently found
366  if( aAngle == 0.0 )
367  return VECTOR2<T> ( T( x ), T( y ) );
368 
369  double sa = sin( aAngle );
370  double ca = cos( aAngle );
371 
372  return VECTOR2<T> ( T( (double) x * ca - (double) y * sa ),
373  T( (double) x * sa + (double) y * ca ) );
374 }
375 
376 
377 template <class T>
378 VECTOR2<T> VECTOR2<T>::Resize( T aNewLength ) const
379 {
380  if( x == 0 && y == 0 )
381  return VECTOR2<T> ( 0, 0 );
382 
383  extended_type l_sq_current = (extended_type) x * x + (extended_type) y * y;
384  extended_type l_sq_new = (extended_type) aNewLength * aNewLength;
385 
386  return VECTOR2<T> (
387  ( x < 0 ? -1 : 1 ) * sqrt( rescale( l_sq_new, (extended_type) x * x, l_sq_current ) ),
388  ( y < 0 ? -1 : 1 ) * sqrt( rescale( l_sq_new, (extended_type) y * y, l_sq_current ) ) ) * sign( aNewLength );
389 }
390 
391 
392 template <class T>
393 const std::string VECTOR2<T>::Format() const
394 {
395  std::stringstream ss;
396 
397  ss << "( xy " << x << " " << y << " )";
398 
399  return ss.str();
400 }
401 
402 
403 template <class T>
405 {
406  return VECTOR2<T> ( x + aVector.x, y + aVector.y );
407 }
408 
409 
410 template <class T>
411 VECTOR2<T> VECTOR2<T>::operator+( const T& aScalar ) const
412 {
413  return VECTOR2<T> ( x + aScalar, y + aScalar );
414 }
415 
416 
417 template <class T>
419 {
420  return VECTOR2<T> ( x - aVector.x, y - aVector.y );
421 }
422 
423 
424 template <class T>
425 VECTOR2<T> VECTOR2<T>::operator-( const T& aScalar ) const
426 {
427  return VECTOR2<T> ( x - aScalar, y - aScalar );
428 }
429 
430 
431 template <class T>
433 {
434  return VECTOR2<T> ( -x, -y );
435 }
436 
437 
438 template <class T>
440 {
441  return aVector.x * x + aVector.y * y;
442 }
443 
444 
445 template <class T>
446 VECTOR2<T> VECTOR2<T>::operator*( const T& aFactor ) const
447 {
448  VECTOR2<T> vector( x * aFactor, y * aFactor );
449  return vector;
450 }
451 
452 
453 template <class T>
454 VECTOR2<T> VECTOR2<T>::operator/( const T& aFactor ) const
455 {
456  VECTOR2<T> vector( x / aFactor, y / aFactor );
457  return vector;
458 }
459 
460 
461 template <class T>
462 VECTOR2<T> operator*( const T& aFactor, const VECTOR2<T>& aVector )
463 {
464  VECTOR2<T> vector( aVector.x * aFactor, aVector.y * aFactor );
465  return vector;
466 }
467 
468 
469 template <class T>
471 {
472  return (extended_type) x * (extended_type) aVector.y -
473  (extended_type) y * (extended_type) aVector.x;
474 }
475 
476 
477 template <class T>
478 typename VECTOR2<T>::extended_type VECTOR2<T>::Dot( const VECTOR2<T>& aVector ) const
479 {
480  return (extended_type) x * (extended_type) aVector.x +
481  (extended_type) y * (extended_type) aVector.y;
482 }
483 
484 
485 template <class T>
486 bool VECTOR2<T>::operator<( const VECTOR2<T>& aVector ) const
487 {
488  return ( *this * *this ) < ( aVector * aVector );
489 }
490 
491 
492 template <class T>
493 bool VECTOR2<T>::operator<=( const VECTOR2<T>& aVector ) const
494 {
495  return ( *this * *this ) <= ( aVector * aVector );
496 }
497 
498 
499 template <class T>
500 bool VECTOR2<T>::operator>( const VECTOR2<T>& aVector ) const
501 {
502  return ( *this * *this ) > ( aVector * aVector );
503 }
504 
505 
506 template <class T>
507 bool VECTOR2<T>::operator>=( const VECTOR2<T>& aVector ) const
508 {
509  return ( *this * *this ) >= ( aVector * aVector );
510 }
511 
512 
513 template <class T>
514 bool VECTOR2<T>::operator==( VECTOR2<T> const& aVector ) const
515 {
516  return ( aVector.x == x ) && ( aVector.y == y );
517 }
518 
519 
520 template <class T>
521 bool VECTOR2<T>::operator!=( VECTOR2<T> const& aVector ) const
522 {
523  return ( aVector.x != x ) || ( aVector.y != y );
524 }
525 
526 
527 template <class T>
528 const VECTOR2<T> LexicographicalMax( const VECTOR2<T>& aA, const VECTOR2<T>& aB )
529 {
530  if( aA.x > aB.x )
531  return aA;
532  else if( aA.x == aB.x && aA.y > aB.y )
533  return aA;
534 
535  return aB;
536 }
537 
538 
539 template <class T>
540 const VECTOR2<T> LexicographicalMin( const VECTOR2<T>& aA, const VECTOR2<T>& aB )
541 {
542  if( aA.x < aB.x )
543  return aA;
544  else if( aA.x == aB.x && aA.y < aB.y )
545  return aA;
546 
547  return aB;
548 }
549 
550 
551 template <class T>
552 const int LexicographicalCompare( const VECTOR2<T>& aA, const VECTOR2<T>& aB )
553 {
554  if( aA.x < aB.x )
555  return -1;
556  else if( aA.x > aB.x )
557  return 1;
558  else // aA.x == aB.x
559  {
560  if( aA.y < aB.y )
561  return -1;
562  else if( aA.y > aB.y )
563  return 1;
564  else
565  return 0;
566  }
567 }
568 
569 
570 template <class T>
571 std::ostream& operator<<( std::ostream& aStream, const VECTOR2<T>& aVector )
572 {
573  aStream << "[ " << aVector.x << " | " << aVector.y << " ]";
574  return aStream;
575 }
576 
577 
578 /* Default specializations */
582 
583 /* Compatibility typedefs */
584 // FIXME should be removed to avoid multiple typedefs for the same type
586 typedef DPOINT DSIZE;
587 
588 #endif // VECTOR2D_H_
VECTOR2_TRAITS< T >::extended_type extended_type
Definition: vector2d.h:81
Class VECTOR2_TRAITS traits class for VECTOR2.
Definition: vector2d.h:48
VECTOR2< T > operator+(const VECTOR2< T > &aVector) const
Vector addition operator.
Definition: vector2d.h:404
int64_t extended_type
Definition: vector2d.h:58
extended_type Dot(const VECTOR2< T > &aVector) const
Function Dot() computes dot product of self with aVector.
Definition: vector2d.h:478
bool operator<=(const VECTOR2< T > &aVector) const
Definition: vector2d.h:493
bool operator>(const VECTOR2< T > &aVector) const
Greater than operator.
Definition: vector2d.h:500
T
enum T contains all this lexer's tokens.
VECTOR2< T > Resize(T aNewLength) const
Function Resize returns a vector of the same direction, but length specified in aNewLength.
Definition: vector2d.h:378
VECTOR2< CastedType > operator()() const
Casts a vector to another specialized subclass.
Definition: vector2d.h:114
DPOINT DSIZE
Definition: vector2d.h:586
Class VECTOR2 defines a general 2D-vector/point.
Definition: vector2d.h:65
VECTOR2< T > Rotate(double aAngle) const
Function Rotate rotates the vector by a given angle.
Definition: vector2d.h:363
const VECTOR2< T > LexicographicalMin(const VECTOR2< T > &aA, const VECTOR2< T > &aB)
Definition: vector2d.h:540
VECTOR2< T > operator/(const T &aFactor) const
Division with a factor.
Definition: vector2d.h:454
VECTOR2< int > VECTOR2I
Definition: vector2d.h:580
VECTOR2< T > & operator=(const VECTOR2< T > &aVector)
Assignment operator.
Definition: vector2d.h:314
T extended_type
extended range/precision types used by operations involving multiple multiplications to prevent overf...
Definition: vector2d.h:52
double Angle() const
Function Angle computes the angle of the vector.
Definition: vector2d.h:299
T coord_type
Definition: vector2d.h:82
T EuclideanNorm() const
Destructor.
Definition: vector2d.h:285
VECTOR2< double > VECTOR2D
Definition: vector2d.h:579
VECTOR2()
Construct a 2D-vector with x, y = 0.
Definition: vector2d.h:253
const int LexicographicalCompare(const VECTOR2< T > &aA, const VECTOR2< T > &aB)
Definition: vector2d.h:552
VECTOR2< unsigned int > VECTOR2U
Definition: vector2d.h:581
bool operator>=(const VECTOR2< T > &aVector) const
Definition: vector2d.h:507
int rescale(int aNumerator, int aValue, int aDenominator)
Definition: math_util.cpp:32
extended_type Cross(const VECTOR2< T > &aVector) const
Function Cross() computes cross product of self with aVector.
Definition: vector2d.h:470
extended_type SquaredEuclideanNorm() const
Function Squared Euclidean Norm computes the squared euclidean norm of the vector, which is defined as (x ** 2 + y ** 2).
Definition: vector2d.h:292
VECTOR2< T > & operator-=(const VECTOR2< T > &aVector)
Compound assignment operator.
Definition: vector2d.h:341
bool operator<(const VECTOR2< T > &aVector) const
Smaller than operator.
Definition: vector2d.h:486
const VECTOR2< T > LexicographicalMax(const VECTOR2< T > &aA, const VECTOR2< T > &aB)
Definition: vector2d.h:528
extended_type operator*(const VECTOR2< T > &aVector) const
Scalar product operator.
Definition: vector2d.h:439
bool operator==(const VECTOR2< T > &aVector) const
Equality operator.
Definition: vector2d.h:514
VECTOR2< T > operator-()
Negate Vector operator.
Definition: vector2d.h:432
VECTOR2< T > Perpendicular() const
Function Perpendicular computes the perpendicular vector.
Definition: vector2d.h:306
VECTOR2(const VECTOR2< CastingType > &aVec)
Initializes a vector from another specialization.
Definition: vector2d.h:105
bool operator!=(const VECTOR2< T > &aVector) const
Not equality operator.
Definition: vector2d.h:521
VECTOR2< double > DPOINT
Definition: vector2d.h:585
VECTOR2< T > & operator+=(const VECTOR2< T > &aVector)
Compound assignment operator.
Definition: vector2d.h:323
const std::string Format() const
Function Format returns the vector formatted as a string.
Definition: vector2d.h:393
VECTOR2< T > operator*(const T &aFactor, const VECTOR2< T > &aVector)
Definition: vector2d.h:462
int sign(T val)
Definition: math_util.h:44