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 <limits>
32 #include <iostream>
33 #include <sstream>
34 
35 #include <math/math_util.h>
36 
37 #ifdef WX_COMPATIBILITY
38  #include <wx/gdicmn.h>
39 #endif
40 
45 template <class T>
47 {
50  typedef T extended_type;
51 };
52 
53 template <>
54 struct VECTOR2_TRAITS<int>
55 {
56  typedef int64_t extended_type;
57 };
58 
59 // Forward declarations for template friends
60 template <class T>
61 class VECTOR2;
62 template <class T>
63 std::ostream& operator<<( std::ostream& aStream, const VECTOR2<T>& aVector );
64 
73 template <class T = int>
74 class VECTOR2
75 {
76 public:
78  typedef T coord_type;
79 
80  static constexpr extended_type ECOORD_MAX = std::numeric_limits<extended_type>::max();
81  static constexpr extended_type ECOORD_MIN = std::numeric_limits<extended_type>::min();
82 
83  T x, y;
84 
85  // Constructors
86 
88  VECTOR2();
89 
90 #ifdef WX_COMPATIBILITY
91  VECTOR2( const wxPoint& aPoint );
93 
95  VECTOR2( const wxSize& aSize );
96 #endif
97 
99  VECTOR2( T x, T y );
100 
103  template <typename CastingType>
105  {
106  x = (T) aVec.x;
107  y = (T) aVec.y;
108  }
109 
112  template <typename CastedType>
114  {
115  return VECTOR2<CastedType>( (CastedType) x, (CastedType) y );
116  }
117 
123  explicit operator wxPoint() const
124  {
125  return wxPoint( x, y );
126  }
127 
129  // virtual ~VECTOR2();
130 
137  T EuclideanNorm() const;
138 
145  extended_type SquaredEuclideanNorm() const;
146 
147 
153  VECTOR2<T> Perpendicular() const;
154 
161  VECTOR2<T> Resize( T aNewLength ) const;
162 
168  double Angle() const;
169 
176  VECTOR2<T> Rotate( double aAngle ) const;
177 
183  const std::string Format() const;
184 
189  extended_type Cross( const VECTOR2<T>& aVector ) const;
190 
195  extended_type Dot( const VECTOR2<T>& aVector ) const;
196 
197 
198  // Operators
199 
201  VECTOR2<T>& operator=( const VECTOR2<T>& aVector );
202 
204  VECTOR2<T> operator+( const VECTOR2<T>& aVector ) const;
205 
207  VECTOR2<T> operator+( const T& aScalar ) const;
208 
210  VECTOR2<T>& operator+=( const VECTOR2<T>& aVector );
211 
213  VECTOR2<T>& operator+=( const T& aScalar );
214 
216  VECTOR2<T> operator-( const VECTOR2<T>& aVector ) const;
217 
219  VECTOR2<T> operator-( const T& aScalar ) const;
220 
222  VECTOR2<T>& operator-=( const VECTOR2<T>& aVector );
223 
225  VECTOR2<T>& operator-=( const T& aScalar );
226 
228  VECTOR2<T> operator-();
229 
231  extended_type operator*( const VECTOR2<T>& aVector ) const;
232 
234  VECTOR2<T> operator*( const T& aFactor ) const;
235 
237  VECTOR2<T> operator/( const T& aFactor ) const;
238 
240  bool operator==( const VECTOR2<T>& aVector ) const;
241 
243  bool operator!=( const VECTOR2<T>& aVector ) const;
244 
246  bool operator<( const VECTOR2<T>& aVector ) const;
247  bool operator<=( const VECTOR2<T>& aVector ) const;
248 
250  bool operator>( const VECTOR2<T>& aVector ) const;
251  bool operator>=( const VECTOR2<T>& aVector ) const;
252 };
253 
254 
255 // ----------------------
256 // --- Implementation ---
257 // ----------------------
258 
259 template <class T>
261 {
262  x = y = 0.0;
263 }
264 
265 
266 #ifdef WX_COMPATIBILITY
267 template <class T>
268 VECTOR2<T>::VECTOR2( wxPoint const& aPoint )
269 {
270  x = T( aPoint.x );
271  y = T( aPoint.y );
272 }
273 
274 
275 template <class T>
276 VECTOR2<T>::VECTOR2( wxSize const& aSize )
277 {
278  x = T( aSize.x );
279  y = T( aSize.y );
280 }
281 #endif
282 
283 template <class T>
284 VECTOR2<T>::VECTOR2( T aX, T aY )
285 {
286  x = aX;
287  y = aY;
288 }
289 
290 
291 template <class T>
293 {
294  return sqrt( (extended_type) x * x + (extended_type) y * y );
295 }
296 
297 
298 template <class T>
300 {
301  return (extended_type) x * x + (extended_type) y * y;
302 }
303 
304 
305 template <class T>
306 double VECTOR2<T>::Angle() const
307 {
308  return atan2( (double) y, (double) x );
309 }
310 
311 
312 template <class T>
314 {
315  VECTOR2<T> perpendicular( -y, x );
316  return perpendicular;
317 }
318 
319 
320 template <class T>
322 {
323  x = aVector.x;
324  y = aVector.y;
325  return *this;
326 }
327 
328 
329 template <class T>
331 {
332  x += aVector.x;
333  y += aVector.y;
334  return *this;
335 }
336 
337 
338 template <class T>
339 VECTOR2<T>& VECTOR2<T>::operator+=( const T& aScalar )
340 {
341  x += aScalar;
342  y += aScalar;
343  return *this;
344 }
345 
346 
347 template <class T>
349 {
350  x -= aVector.x;
351  y -= aVector.y;
352  return *this;
353 }
354 
355 
356 template <class T>
357 VECTOR2<T>& VECTOR2<T>::operator-=( const T& aScalar )
358 {
359  x -= aScalar;
360  y -= aScalar;
361  return *this;
362 }
363 
364 
369 template <class T>
370 VECTOR2<T> VECTOR2<T>::Rotate( double aAngle ) const
371 {
372  // Avoid 0 radian rotation, case very frequently found
373  if( aAngle == 0.0 )
374  return VECTOR2<T> ( T( x ), T( y ) );
375 
376  double sa = sin( aAngle );
377  double ca = cos( aAngle );
378 
379  return VECTOR2<T> ( T( (double) x * ca - (double) y * sa ),
380  T( (double) x * sa + (double) y * ca ) );
381 }
382 
383 
384 template <class T>
385 VECTOR2<T> VECTOR2<T>::Resize( T aNewLength ) const
386 {
387  if( x == 0 && y == 0 )
388  return VECTOR2<T> ( 0, 0 );
389 
390  extended_type l_sq_current = (extended_type) x * x + (extended_type) y * y;
391  extended_type l_sq_new = (extended_type) aNewLength * aNewLength;
392 
393  return VECTOR2<T> (
394  ( x < 0 ? -1 : 1 ) * sqrt( rescale( l_sq_new, (extended_type) x * x, l_sq_current ) ),
395  ( y < 0 ? -1 : 1 ) * sqrt( rescale( l_sq_new, (extended_type) y * y, l_sq_current ) ) ) * sign( aNewLength );
396 }
397 
398 
399 template <class T>
400 const std::string VECTOR2<T>::Format() const
401 {
402  std::stringstream ss;
403 
404  ss << "( xy " << x << " " << y << " )";
405 
406  return ss.str();
407 }
408 
409 
410 template <class T>
412 {
413  return VECTOR2<T> ( x + aVector.x, y + aVector.y );
414 }
415 
416 
417 template <class T>
418 VECTOR2<T> VECTOR2<T>::operator+( const T& aScalar ) const
419 {
420  return VECTOR2<T> ( x + aScalar, y + aScalar );
421 }
422 
423 
424 template <class T>
426 {
427  return VECTOR2<T> ( x - aVector.x, y - aVector.y );
428 }
429 
430 
431 template <class T>
432 VECTOR2<T> VECTOR2<T>::operator-( const T& aScalar ) const
433 {
434  return VECTOR2<T> ( x - aScalar, y - aScalar );
435 }
436 
437 
438 template <class T>
440 {
441  return VECTOR2<T> ( -x, -y );
442 }
443 
444 
445 template <class T>
447 {
448  return (extended_type)aVector.x * x + (extended_type)aVector.y * y;
449 }
450 
451 
452 template <class T>
453 VECTOR2<T> VECTOR2<T>::operator*( const T& aFactor ) const
454 {
455  VECTOR2<T> vector( x * aFactor, y * aFactor );
456  return vector;
457 }
458 
459 
460 template <class T>
461 VECTOR2<T> VECTOR2<T>::operator/( const T& aFactor ) const
462 {
463  VECTOR2<T> vector( x / aFactor, y / aFactor );
464  return vector;
465 }
466 
467 
468 template <class T>
469 VECTOR2<T> operator*( const T& aFactor, const VECTOR2<T>& aVector )
470 {
471  VECTOR2<T> vector( aVector.x * aFactor, aVector.y * aFactor );
472  return vector;
473 }
474 
475 
476 template <class T>
478 {
479  return (extended_type) x * (extended_type) aVector.y -
480  (extended_type) y * (extended_type) aVector.x;
481 }
482 
483 
484 template <class T>
485 typename VECTOR2<T>::extended_type VECTOR2<T>::Dot( const VECTOR2<T>& aVector ) const
486 {
487  return (extended_type) x * (extended_type) aVector.x +
488  (extended_type) y * (extended_type) aVector.y;
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>=( const VECTOR2<T>& aVector ) const
515 {
516  return ( *this * *this ) >= ( aVector * aVector );
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 bool VECTOR2<T>::operator!=( VECTOR2<T> const& aVector ) const
529 {
530  return ( aVector.x != x ) || ( aVector.y != y );
531 }
532 
533 
534 template <class T>
535 const VECTOR2<T> LexicographicalMax( const VECTOR2<T>& aA, const VECTOR2<T>& aB )
536 {
537  if( aA.x > aB.x )
538  return aA;
539  else if( aA.x == aB.x && aA.y > aB.y )
540  return aA;
541 
542  return aB;
543 }
544 
545 
546 template <class T>
547 const VECTOR2<T> LexicographicalMin( const VECTOR2<T>& aA, const VECTOR2<T>& aB )
548 {
549  if( aA.x < aB.x )
550  return aA;
551  else if( aA.x == aB.x && aA.y < aB.y )
552  return aA;
553 
554  return aB;
555 }
556 
557 
558 template <class T>
559 const int LexicographicalCompare( const VECTOR2<T>& aA, const VECTOR2<T>& aB )
560 {
561  if( aA.x < aB.x )
562  return -1;
563  else if( aA.x > aB.x )
564  return 1;
565  else // aA.x == aB.x
566  {
567  if( aA.y < aB.y )
568  return -1;
569  else if( aA.y > aB.y )
570  return 1;
571  else
572  return 0;
573  }
574 }
575 
576 
577 template <class T>
578 std::ostream& operator<<( std::ostream& aStream, const VECTOR2<T>& aVector )
579 {
580  aStream << "[ " << aVector.x << " | " << aVector.y << " ]";
581  return aStream;
582 }
583 
584 
585 /* Default specializations */
589 
590 /* Compatibility typedefs */
591 // FIXME should be removed to avoid multiple typedefs for the same type
593 typedef DPOINT DSIZE;
594 
595 #endif // VECTOR2D_H_
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:112
VECTOR2_TRAITS< T >::extended_type extended_type
Definition: vector2d.h:77
Class VECTOR2_TRAITS traits class for VECTOR2.
Definition: vector2d.h:46
VECTOR2< T > operator+(const VECTOR2< T > &aVector) const
Vector addition operator.
Definition: vector2d.h:411
int64_t extended_type
Definition: vector2d.h:56
extended_type Dot(const VECTOR2< T > &aVector) const
Function Dot() computes dot product of self with aVector.
Definition: vector2d.h:485
bool operator<=(const VECTOR2< T > &aVector) const
Definition: vector2d.h:500
bool operator>(const VECTOR2< T > &aVector) const
Greater than operator.
Definition: vector2d.h:507
VECTOR2< T > Resize(T aNewLength) const
Function Resize returns a vector of the same direction, but length specified in aNewLength.
Definition: vector2d.h:385
VECTOR2< CastedType > operator()() const
Casts a vector to another specialized subclass.
Definition: vector2d.h:113
DPOINT DSIZE
Definition: vector2d.h:593
Class VECTOR2 defines a general 2D-vector/point.
Definition: vector2d.h:61
VECTOR2< T > Rotate(double aAngle) const
Function Rotate rotates the vector by a given angle.
Definition: vector2d.h:370
const VECTOR2< T > LexicographicalMin(const VECTOR2< T > &aA, const VECTOR2< T > &aB)
Definition: vector2d.h:547
VECTOR2< T > operator/(const T &aFactor) const
Division with a factor.
Definition: vector2d.h:461
bool operator==(const PART_LIB &aLibrary, const wxString &aName)
Case insensitive library name comparison.
VECTOR2< int > VECTOR2I
Definition: vector2d.h:587
VECTOR2< T > & operator=(const VECTOR2< T > &aVector)
Assignment operator.
Definition: vector2d.h:321
T extended_type
extended range/precision types used by operations involving multiple multiplications to prevent overf...
Definition: vector2d.h:50
double Angle() const
Function Angle computes the angle of the vector.
Definition: vector2d.h:306
T coord_type
Definition: vector2d.h:78
T EuclideanNorm() const
Destructor.
Definition: vector2d.h:292
VECTOR2< double > VECTOR2D
Definition: vector2d.h:586
VECTOR2()
Construct a 2D-vector with x, y = 0.
Definition: vector2d.h:260
const int LexicographicalCompare(const VECTOR2< T > &aA, const VECTOR2< T > &aB)
Definition: vector2d.h:559
VECTOR2< unsigned int > VECTOR2U
Definition: vector2d.h:588
bool operator>=(const VECTOR2< T > &aVector) const
Definition: vector2d.h:514
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:477
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:299
GAL_LAYER_ID operator+(const GAL_LAYER_ID &a, int b)
Used for via types.
VECTOR2< T > & operator-=(const VECTOR2< T > &aVector)
Compound assignment operator.
Definition: vector2d.h:348
bool operator<(const VECTOR2< T > &aVector) const
Smaller than operator.
Definition: vector2d.h:493
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
const VECTOR2< T > LexicographicalMax(const VECTOR2< T > &aA, const VECTOR2< T > &aB)
Definition: vector2d.h:535
#define max(a, b)
Definition: auxiliary.h:86
extended_type operator*(const VECTOR2< T > &aVector) const
Scalar product operator.
Definition: vector2d.h:446
bool operator==(const VECTOR2< T > &aVector) const
Equality operator.
Definition: vector2d.h:521
VECTOR2< T > operator-()
Negate Vector operator.
Definition: vector2d.h:439
VECTOR2< T > Perpendicular() const
Function Perpendicular computes the perpendicular vector.
Definition: vector2d.h:313
VECTOR2(const VECTOR2< CastingType > &aVec)
Initializes a vector from another specialization.
Definition: vector2d.h:104
bool operator!=(const VECTOR2< T > &aVector) const
Not equality operator.
Definition: vector2d.h:528
VECTOR2< double > DPOINT
Definition: vector2d.h:592
VECTOR2< T > & operator+=(const VECTOR2< T > &aVector)
Compound assignment operator.
Definition: vector2d.h:330
bool operator!=(const PART_LIB &aLibrary, const wxString &aName)
const std::string Format() const
Function Format returns the vector formatted as a string.
Definition: vector2d.h:400
#define min(a, b)
Definition: auxiliary.h:85
VECTOR2< T > operator*(const T &aFactor, const VECTOR2< T > &aVector)
Definition: vector2d.h:469
int sign(T val)
Definition: math_util.h:44