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/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 
111  VECTOR2( const VECTOR2<T>& aVec )
112  {
113  x = aVec.x;
114  y = aVec.y;
115  }
116 
119  template <typename CastedType>
121  {
122  return VECTOR2<CastedType>( (CastedType) x, (CastedType) y );
123  }
124 
130  explicit operator wxPoint() const
131  {
132  return wxPoint( x, y );
133  }
134 
136  // virtual ~VECTOR2();
137 
144  T EuclideanNorm() const;
145 
153 
154 
160  VECTOR2<T> Perpendicular() const;
161 
168  VECTOR2<T> Resize( T aNewLength ) const;
169 
175  double Angle() const;
176 
183  VECTOR2<T> Rotate( double aAngle ) const;
184 
190  const std::string Format() const;
191 
196  extended_type Cross( const VECTOR2<T>& aVector ) const;
197 
202  extended_type Dot( const VECTOR2<T>& aVector ) const;
203 
204 
205  // Operators
206 
208  VECTOR2<T>& operator=( const VECTOR2<T>& aVector );
209 
211  VECTOR2<T> operator+( const VECTOR2<T>& aVector ) const;
212 
214  VECTOR2<T> operator+( const T& aScalar ) const;
215 
217  VECTOR2<T>& operator+=( const VECTOR2<T>& aVector );
218 
220  VECTOR2<T>& operator+=( const T& aScalar );
221 
223  VECTOR2<T> operator-( const VECTOR2<T>& aVector ) const;
224 
226  VECTOR2<T> operator-( const T& aScalar ) const;
227 
229  VECTOR2<T>& operator-=( const VECTOR2<T>& aVector );
230 
232  VECTOR2<T>& operator-=( const T& aScalar );
233 
236 
238  extended_type operator*( const VECTOR2<T>& aVector ) const;
239 
241  VECTOR2<T> operator*( const T& aFactor ) const;
242 
244  VECTOR2<T> operator/( const T& aFactor ) const;
245 
247  bool operator==( const VECTOR2<T>& aVector ) const;
248 
250  bool operator!=( const VECTOR2<T>& aVector ) const;
251 
253  bool operator<( const VECTOR2<T>& aVector ) const;
254  bool operator<=( const VECTOR2<T>& aVector ) const;
255 
257  bool operator>( const VECTOR2<T>& aVector ) const;
258  bool operator>=( const VECTOR2<T>& aVector ) const;
259 };
260 
261 
262 // ----------------------
263 // --- Implementation ---
264 // ----------------------
265 
266 template <class T>
268 {
269  x = y = 0.0;
270 }
271 
272 
273 #ifdef WX_COMPATIBILITY
274 template <class T>
275 VECTOR2<T>::VECTOR2( wxPoint const& aPoint )
276 {
277  x = T( aPoint.x );
278  y = T( aPoint.y );
279 }
280 
281 
282 template <class T>
283 VECTOR2<T>::VECTOR2( wxSize const& aSize )
284 {
285  x = T( aSize.x );
286  y = T( aSize.y );
287 }
288 #endif
289 
290 template <class T>
291 VECTOR2<T>::VECTOR2( T aX, T aY )
292 {
293  x = aX;
294  y = aY;
295 }
296 
297 
298 template <class T>
300 {
301  return sqrt( (extended_type) x * x + (extended_type) y * y );
302 }
303 
304 
305 template <class T>
307 {
308  return (extended_type) x * x + (extended_type) y * y;
309 }
310 
311 
312 template <class T>
313 double VECTOR2<T>::Angle() const
314 {
315  return atan2( (double) y, (double) x );
316 }
317 
318 
319 template <class T>
321 {
322  VECTOR2<T> perpendicular( -y, x );
323  return perpendicular;
324 }
325 
326 
327 template <class T>
329 {
330  x = aVector.x;
331  y = aVector.y;
332  return *this;
333 }
334 
335 
336 template <class T>
338 {
339  x += aVector.x;
340  y += aVector.y;
341  return *this;
342 }
343 
344 
345 template <class T>
346 VECTOR2<T>& VECTOR2<T>::operator+=( const T& aScalar )
347 {
348  x += aScalar;
349  y += aScalar;
350  return *this;
351 }
352 
353 
354 template <class T>
356 {
357  x -= aVector.x;
358  y -= aVector.y;
359  return *this;
360 }
361 
362 
363 template <class T>
364 VECTOR2<T>& VECTOR2<T>::operator-=( const T& aScalar )
365 {
366  x -= aScalar;
367  y -= aScalar;
368  return *this;
369 }
370 
371 
376 template <class T>
377 VECTOR2<T> VECTOR2<T>::Rotate( double aAngle ) const
378 {
379  // Avoid 0 radian rotation, case very frequently found
380  if( aAngle == 0.0 )
381  return VECTOR2<T> ( T( x ), T( y ) );
382 
383  double sa = sin( aAngle );
384  double ca = cos( aAngle );
385 
386  return VECTOR2<T> ( T( (double) x * ca - (double) y * sa ),
387  T( (double) x * sa + (double) y * ca ) );
388 }
389 
390 
391 template <class T>
392 VECTOR2<T> VECTOR2<T>::Resize( T aNewLength ) const
393 {
394  if( x == 0 && y == 0 )
395  return VECTOR2<T> ( 0, 0 );
396 
397  extended_type l_sq_current = (extended_type) x * x + (extended_type) y * y;
398  extended_type l_sq_new = (extended_type) aNewLength * aNewLength;
399 
400  return VECTOR2<T> (
401  ( x < 0 ? -1 : 1 ) * sqrt( rescale( l_sq_new, (extended_type) x * x, l_sq_current ) ),
402  ( y < 0 ? -1 : 1 ) * sqrt( rescale( l_sq_new, (extended_type) y * y, l_sq_current ) ) ) * sign( aNewLength );
403 }
404 
405 
406 template <class T>
407 const std::string VECTOR2<T>::Format() const
408 {
409  std::stringstream ss;
410 
411  ss << "( xy " << x << " " << y << " )";
412 
413  return ss.str();
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 - aVector.x, y - aVector.y );
435 }
436 
437 
438 template <class T>
439 VECTOR2<T> VECTOR2<T>::operator-( const T& aScalar ) const
440 {
441  return VECTOR2<T> ( x - aScalar, y - aScalar );
442 }
443 
444 
445 template <class T>
447 {
448  return VECTOR2<T> ( -x, -y );
449 }
450 
451 
452 template <class T>
454 {
455  return (extended_type)aVector.x * x + (extended_type)aVector.y * y;
456 }
457 
458 
459 template <class T>
460 VECTOR2<T> VECTOR2<T>::operator*( const T& aFactor ) const
461 {
462  VECTOR2<T> vector( x * aFactor, y * aFactor );
463  return vector;
464 }
465 
466 
467 template <class T>
468 VECTOR2<T> VECTOR2<T>::operator/( const T& aFactor ) const
469 {
470  VECTOR2<T> vector( x / aFactor, y / aFactor );
471  return vector;
472 }
473 
474 
475 template <class T>
476 VECTOR2<T> operator*( const T& aFactor, const VECTOR2<T>& aVector )
477 {
478  VECTOR2<T> vector( aVector.x * aFactor, aVector.y * aFactor );
479  return vector;
480 }
481 
482 
483 template <class T>
485 {
486  return (extended_type) x * (extended_type) aVector.y -
487  (extended_type) y * (extended_type) aVector.x;
488 }
489 
490 
491 template <class T>
492 typename VECTOR2<T>::extended_type VECTOR2<T>::Dot( const VECTOR2<T>& aVector ) const
493 {
494  return (extended_type) x * (extended_type) aVector.x +
495  (extended_type) y * (extended_type) aVector.y;
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>=( const VECTOR2<T>& aVector ) const
522 {
523  return ( *this * *this ) >= ( aVector * aVector );
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 bool VECTOR2<T>::operator!=( VECTOR2<T> const& aVector ) const
536 {
537  return ( aVector.x != x ) || ( aVector.y != y );
538 }
539 
540 
541 template <class T>
542 const VECTOR2<T> LexicographicalMax( const VECTOR2<T>& aA, const VECTOR2<T>& aB )
543 {
544  if( aA.x > aB.x )
545  return aA;
546  else if( aA.x == aB.x && aA.y > aB.y )
547  return aA;
548 
549  return aB;
550 }
551 
552 
553 template <class T>
554 const VECTOR2<T> LexicographicalMin( const VECTOR2<T>& aA, const VECTOR2<T>& aB )
555 {
556  if( aA.x < aB.x )
557  return aA;
558  else if( aA.x == aB.x && aA.y < aB.y )
559  return aA;
560 
561  return aB;
562 }
563 
564 
565 template <class T>
566 const int LexicographicalCompare( const VECTOR2<T>& aA, const VECTOR2<T>& aB )
567 {
568  if( aA.x < aB.x )
569  return -1;
570  else if( aA.x > aB.x )
571  return 1;
572  else // aA.x == aB.x
573  {
574  if( aA.y < aB.y )
575  return -1;
576  else if( aA.y > aB.y )
577  return 1;
578  else
579  return 0;
580  }
581 }
582 
583 
584 template <class T>
585 std::ostream& operator<<( std::ostream& aStream, const VECTOR2<T>& aVector )
586 {
587  aStream << "[ " << aVector.x << " | " << aVector.y << " ]";
588  return aStream;
589 }
590 
591 
592 /* Default specializations */
596 
597 /* Compatibility typedefs */
598 // FIXME should be removed to avoid multiple typedefs for the same type
600 typedef DPOINT DSIZE;
601 
602 #endif // VECTOR2D_H_
VECTOR2_TRAITS< T >::extended_type extended_type
Definition: vector2d.h:77
extended_type Cross(const VECTOR2< T > &aVector) const
Function Cross() computes cross product of self with aVector.
Definition: vector2d.h:484
int sign(T val)
Definition: util.h:101
VECTOR2_TRAITS traits class for VECTOR2.
Definition: vector2d.h:46
bool operator>=(const VECTOR2< T > &aVector) const
Definition: vector2d.h:521
int64_t extended_type
Definition: vector2d.h:56
VECTOR2< T > operator+(const VECTOR2< T > &aVector) const
Vector addition operator.
Definition: vector2d.h:418
VECTOR2< T > Perpendicular() const
Function Perpendicular computes the perpendicular vector.
Definition: vector2d.h:320
DPOINT DSIZE
Definition: vector2d.h:600
VECTOR2 defines a general 2D-vector/point.
Definition: vector2d.h:61
const VECTOR2< T > LexicographicalMin(const VECTOR2< T > &aA, const VECTOR2< T > &aB)
Definition: vector2d.h:554
extended_type SquaredEuclideanNorm() const
Function Squared Euclidean Norm computes the squared euclidean norm of the vector,...
Definition: vector2d.h:306
VECTOR2< T > operator *(const T &aFactor, const VECTOR2< T > &aVector)
Definition: vector2d.h:476
VECTOR2< int > VECTOR2I
Definition: vector2d.h:594
VECTOR2< CastedType > operator()() const
Casts a vector to another specialized subclass.
Definition: vector2d.h:120
bool operator!=(const VECTOR2< T > &aVector) const
Not equality operator.
Definition: vector2d.h:535
VECTOR2< T > & operator=(const VECTOR2< T > &aVector)
Assignment operator.
Definition: vector2d.h:328
T extended_type
extended range/precision types used by operations involving multiple multiplications to prevent overf...
Definition: vector2d.h:50
static constexpr extended_type ECOORD_MAX
Definition: vector2d.h:80
std::ostream & operator<<(std::ostream &aStream, const VECTOR2< T > &aVector)
Definition: vector2d.h:585
T coord_type
Definition: vector2d.h:78
VECTOR2< double > VECTOR2D
Definition: vector2d.h:593
VECTOR2()
Construct a 2D-vector with x, y = 0.
Definition: vector2d.h:267
const int LexicographicalCompare(const VECTOR2< T > &aA, const VECTOR2< T > &aB)
Definition: vector2d.h:566
const std::string Format() const
Function Format returns the vector formatted as a string.
Definition: vector2d.h:407
bool operator<(const VECTOR2< T > &aVector) const
Smaller than operator.
Definition: vector2d.h:500
VECTOR2< unsigned int > VECTOR2U
Definition: vector2d.h:595
extended_type operator *(const VECTOR2< T > &aVector) const
Scalar product operator.
Definition: vector2d.h:453
bool operator==(const VECTOR2< T > &aVector) const
Equality operator.
Definition: vector2d.h:528
double Angle() const
Function Angle computes the angle of the vector.
Definition: vector2d.h:313
bool operator<=(const VECTOR2< T > &aVector) const
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:392
VECTOR2< T > Rotate(double aAngle) const
Function Rotate rotates the vector by a given angle.
Definition: vector2d.h:377
VECTOR2< T > & operator-=(const VECTOR2< T > &aVector)
Compound assignment operator.
Definition: vector2d.h:355
VECTOR2< T > operator/(const T &aFactor) const
Division with a factor.
Definition: vector2d.h:468
extended_type Dot(const VECTOR2< T > &aVector) const
Function Dot() computes dot product of self with aVector.
Definition: vector2d.h:492
const VECTOR2< T > LexicographicalMax(const VECTOR2< T > &aA, const VECTOR2< T > &aB)
Definition: vector2d.h:542
T rescale(T aNumerator, T aValue, T aDenominator)
Function rescale()
Definition: util.h:95
static constexpr extended_type ECOORD_MIN
Definition: vector2d.h:81
VECTOR2< T > operator-()
Negate Vector operator.
Definition: vector2d.h:446
VECTOR2(const VECTOR2< CastingType > &aVec)
Initializes a vector from another specialization.
Definition: vector2d.h:104
T EuclideanNorm() const
Destructor.
Definition: vector2d.h:299
VECTOR2(const VECTOR2< T > &aVec)
Copy a vector.
Definition: vector2d.h:111
VECTOR2< double > DPOINT
Definition: vector2d.h:599
VECTOR2< T > & operator+=(const VECTOR2< T > &aVector)
Compound assignment operator.
Definition: vector2d.h:337
bool operator>(const VECTOR2< T > &aVector) const
Greater than operator.
Definition: vector2d.h:514