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 
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  friend std::ostream & operator<< <T> ( std::ostream & stream, const VECTOR2<T> &vector );
254 };
255 
256 
257 // ----------------------
258 // --- Implementation ---
259 // ----------------------
260 
261 template <class T>
263 {
264  x = y = 0.0;
265 }
266 
267 
268 #ifdef WX_COMPATIBILITY
269 template <class T>
270 VECTOR2<T>::VECTOR2( wxPoint const& aPoint )
271 {
272  x = T( aPoint.x );
273  y = T( aPoint.y );
274 }
275 
276 
277 template <class T>
278 VECTOR2<T>::VECTOR2( wxSize const& aSize )
279 {
280  x = T( aSize.x );
281  y = T( aSize.y );
282 }
283 #endif
284 
285 template <class T>
287 {
288  x = aX;
289  y = aY;
290 }
291 
292 
293 template <class T>
295 {
296  return sqrt( (extended_type) x * x + (extended_type) y * y );
297 }
298 
299 
300 template <class T>
302 {
303  return (extended_type) x * x + (extended_type) y * y;
304 }
305 
306 
307 template <class T>
308 double VECTOR2<T>::Angle() const
309 {
310  return atan2( (double) y, (double) x );
311 }
312 
313 
314 template <class T>
316 {
317  VECTOR2<T> perpendicular( -y, x );
318  return perpendicular;
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 += aVector.x;
335  y += aVector.y;
336  return *this;
337 }
338 
339 
340 template <class T>
342 {
343  x += aScalar;
344  y += aScalar;
345  return *this;
346 }
347 
348 
349 template <class T>
351 {
352  x -= aVector.x;
353  y -= aVector.y;
354  return *this;
355 }
356 
357 
358 template <class T>
360 {
361  x -= aScalar;
362  y -= aScalar;
363  return *this;
364 }
365 
366 
371 template <class T>
372 VECTOR2<T> VECTOR2<T>::Rotate( double aAngle ) const
373 {
374  // Avoid 0 radian rotation, case very frequently found
375  if( aAngle == 0.0 )
376  return VECTOR2<T> ( T( x ), T( y ) );
377 
378  double sa = sin( aAngle );
379  double ca = cos( aAngle );
380 
381  return VECTOR2<T> ( T( (double) x * ca - (double) y * sa ),
382  T( (double) x * sa + (double) y * ca ) );
383 }
384 
385 
386 template <class T>
387 VECTOR2<T> VECTOR2<T>::Resize( T aNewLength ) const
388 {
389  if( x == 0 && y == 0 )
390  return VECTOR2<T> ( 0, 0 );
391 
392  extended_type l_sq_current = (extended_type) x * x + (extended_type) y * y;
393  extended_type l_sq_new = (extended_type) aNewLength * aNewLength;
394 
395  return VECTOR2<T> (
396  ( x < 0 ? -1 : 1 ) * sqrt( rescale( l_sq_new, (extended_type) x * x, l_sq_current ) ),
397  ( y < 0 ? -1 : 1 ) * sqrt( rescale( l_sq_new, (extended_type) y * y, l_sq_current ) ) ) * sign( aNewLength );
398 }
399 
400 
401 template <class T>
402 const std::string VECTOR2<T>::Format() const
403 {
404  std::stringstream ss;
405 
406  ss << "( xy " << x << " " << y << " )";
407 
408  return ss.str();
409 }
410 
411 
412 template <class T>
414 {
415  return VECTOR2<T> ( x + aVector.x, y + aVector.y );
416 }
417 
418 
419 template <class T>
420 VECTOR2<T> VECTOR2<T>::operator+( const T& aScalar ) const
421 {
422  return VECTOR2<T> ( x + aScalar, y + aScalar );
423 }
424 
425 
426 template <class T>
428 {
429  return VECTOR2<T> ( x - aVector.x, y - aVector.y );
430 }
431 
432 
433 template <class T>
434 VECTOR2<T> VECTOR2<T>::operator-( const T& aScalar ) const
435 {
436  return VECTOR2<T> ( x - aScalar, y - aScalar );
437 }
438 
439 
440 template <class T>
442 {
443  return VECTOR2<T> ( -x, -y );
444 }
445 
446 
447 template <class T>
449 {
450  return (extended_type)aVector.x * x + (extended_type)aVector.y * y;
451 }
452 
453 
454 template <class T>
455 VECTOR2<T> VECTOR2<T>::operator*( const T& aFactor ) const
456 {
457  VECTOR2<T> vector( x * aFactor, y * aFactor );
458  return vector;
459 }
460 
461 
462 template <class T>
463 VECTOR2<T> VECTOR2<T>::operator/( const T& aFactor ) const
464 {
465  VECTOR2<T> vector( x / aFactor, y / aFactor );
466  return vector;
467 }
468 
469 
470 template <class T>
471 VECTOR2<T> operator*( const T& aFactor, const VECTOR2<T>& aVector )
472 {
473  VECTOR2<T> vector( aVector.x * aFactor, aVector.y * aFactor );
474  return vector;
475 }
476 
477 
478 template <class T>
480 {
481  return (extended_type) x * (extended_type) aVector.y -
482  (extended_type) y * (extended_type) aVector.x;
483 }
484 
485 
486 template <class T>
487 typename VECTOR2<T>::extended_type VECTOR2<T>::Dot( const VECTOR2<T>& aVector ) const
488 {
489  return (extended_type) x * (extended_type) aVector.x +
490  (extended_type) y * (extended_type) aVector.y;
491 }
492 
493 
494 template <class T>
495 bool VECTOR2<T>::operator<( const VECTOR2<T>& aVector ) const
496 {
497  return ( *this * *this ) < ( aVector * aVector );
498 }
499 
500 
501 template <class T>
502 bool VECTOR2<T>::operator<=( const VECTOR2<T>& aVector ) const
503 {
504  return ( *this * *this ) <= ( aVector * aVector );
505 }
506 
507 
508 template <class T>
509 bool VECTOR2<T>::operator>( const VECTOR2<T>& aVector ) const
510 {
511  return ( *this * *this ) > ( aVector * aVector );
512 }
513 
514 
515 template <class T>
516 bool VECTOR2<T>::operator>=( const VECTOR2<T>& aVector ) const
517 {
518  return ( *this * *this ) >= ( aVector * aVector );
519 }
520 
521 
522 template <class T>
523 bool VECTOR2<T>::operator==( VECTOR2<T> const& aVector ) const
524 {
525  return ( aVector.x == x ) && ( aVector.y == y );
526 }
527 
528 
529 template <class T>
530 bool VECTOR2<T>::operator!=( VECTOR2<T> const& aVector ) const
531 {
532  return ( aVector.x != x ) || ( aVector.y != y );
533 }
534 
535 
536 template <class T>
537 const VECTOR2<T> LexicographicalMax( const VECTOR2<T>& aA, const VECTOR2<T>& aB )
538 {
539  if( aA.x > aB.x )
540  return aA;
541  else if( aA.x == aB.x && aA.y > aB.y )
542  return aA;
543 
544  return aB;
545 }
546 
547 
548 template <class T>
549 const VECTOR2<T> LexicographicalMin( const VECTOR2<T>& aA, const VECTOR2<T>& aB )
550 {
551  if( aA.x < aB.x )
552  return aA;
553  else if( aA.x == aB.x && aA.y < aB.y )
554  return aA;
555 
556  return aB;
557 }
558 
559 
560 template <class T>
561 const int LexicographicalCompare( const VECTOR2<T>& aA, const VECTOR2<T>& aB )
562 {
563  if( aA.x < aB.x )
564  return -1;
565  else if( aA.x > aB.x )
566  return 1;
567  else // aA.x == aB.x
568  {
569  if( aA.y < aB.y )
570  return -1;
571  else if( aA.y > aB.y )
572  return 1;
573  else
574  return 0;
575  }
576 }
577 
578 
579 template <class T>
580 std::ostream& operator<<( std::ostream& aStream, const VECTOR2<T>& aVector )
581 {
582  aStream << "[ " << aVector.x << " | " << aVector.y << " ]";
583  return aStream;
584 }
585 
586 
587 /* Default specializations */
591 
592 /* Compatibility typedefs */
593 // FIXME should be removed to avoid multiple typedefs for the same type
595 typedef DPOINT DSIZE;
596 
597 #endif // VECTOR2D_H_
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:413
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:487
bool operator<=(const VECTOR2< T > &aVector) const
Definition: vector2d.h:502
bool operator>(const VECTOR2< T > &aVector) const
Greater than operator.
Definition: vector2d.h:509
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:387
VECTOR2< CastedType > operator()() const
Casts a vector to another specialized subclass.
Definition: vector2d.h:113
DPOINT DSIZE
Definition: vector2d.h:595
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:372
const VECTOR2< T > LexicographicalMin(const VECTOR2< T > &aA, const VECTOR2< T > &aB)
Definition: vector2d.h:549
VECTOR2< T > operator/(const T &aFactor) const
Division with a factor.
Definition: vector2d.h:463
VECTOR2< int > VECTOR2I
Definition: vector2d.h:589
VECTOR2< T > & operator=(const VECTOR2< T > &aVector)
Assignment operator.
Definition: vector2d.h:323
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:308
static constexpr extended_type ECOORD_MAX
Definition: vector2d.h:80
T coord_type
Definition: vector2d.h:78
T EuclideanNorm() const
Destructor.
Definition: vector2d.h:294
VECTOR2< double > VECTOR2D
Definition: vector2d.h:588
VECTOR2()
Construct a 2D-vector with x, y = 0.
Definition: vector2d.h:262
const int LexicographicalCompare(const VECTOR2< T > &aA, const VECTOR2< T > &aB)
Definition: vector2d.h:561
VECTOR2< unsigned int > VECTOR2U
Definition: vector2d.h:590
bool operator>=(const VECTOR2< T > &aVector) const
Definition: vector2d.h:516
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:479
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:301
VECTOR2< T > & operator-=(const VECTOR2< T > &aVector)
Compound assignment operator.
Definition: vector2d.h:350
bool operator<(const VECTOR2< T > &aVector) const
Smaller than operator.
Definition: vector2d.h:495
const VECTOR2< T > LexicographicalMax(const VECTOR2< T > &aA, const VECTOR2< T > &aB)
Definition: vector2d.h:537
#define max(a, b)
Definition: auxiliary.h:86
extended_type operator*(const VECTOR2< T > &aVector) const
Scalar product operator.
Definition: vector2d.h:448
bool operator==(const VECTOR2< T > &aVector) const
Equality operator.
Definition: vector2d.h:523
static constexpr extended_type ECOORD_MIN
Definition: vector2d.h:81
VECTOR2< T > operator-()
Negate Vector operator.
Definition: vector2d.h:441
VECTOR2< T > Perpendicular() const
Function Perpendicular computes the perpendicular vector.
Definition: vector2d.h:315
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:530
VECTOR2< double > DPOINT
Definition: vector2d.h:594
VECTOR2< T > & operator+=(const VECTOR2< T > &aVector)
Compound assignment operator.
Definition: vector2d.h:332
const std::string Format() const
Function Format returns the vector formatted as a string.
Definition: vector2d.h:402
#define min(a, b)
Definition: auxiliary.h:85
VECTOR2< T > operator*(const T &aFactor, const VECTOR2< T > &aVector)
Definition: vector2d.h:471
int sign(T val)
Definition: math_util.h:44