KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 <[email protected]>
6 * Copyright (C) 2012-2021 KiCad Developers, see AUTHORS.txt for contributors.
7 * Copyright (C) 2013 CERN
8 * @author Tomasz Wlostowski <[email protected]>
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#include <type_traits>
35
36#include <math/util.h>
37
41template <class T>
43{
46 typedef T extended_type;
47};
48
49template <>
50struct VECTOR2_TRAITS<int>
51{
52 typedef int64_t extended_type;
53};
54
55// Forward declarations for template friends
56template <class T>
57class VECTOR2;
58template <class T>
59std::ostream& operator<<( std::ostream& aStream, const VECTOR2<T>& aVector );
60
68template <class T = int>
70{
71public:
73 typedef T coord_type;
74
75 static constexpr extended_type ECOORD_MAX = std::numeric_limits<extended_type>::max();
76 static constexpr extended_type ECOORD_MIN = std::numeric_limits<extended_type>::min();
77
78 T x, y;
79
82
84 VECTOR2( T x, T y );
85
87 template <typename CastingType>
89 {
90 if( std::is_same<T, int>::value )
91 {
92 CastingType minI = static_cast<CastingType>( std::numeric_limits<int>::min() );
93 CastingType maxI = static_cast<CastingType>( std::numeric_limits<int>::max() );
94
95 x = static_cast<int>( Clamp( minI, aVec.x, maxI ) );
96 y = static_cast<int>( Clamp( minI, aVec.y, maxI ) );
97 }
98 else
99 {
100 x = static_cast<T>( aVec.x );
101 y = static_cast<T>( aVec.y );
102 }
103 }
104
106 VECTOR2( const VECTOR2<T>& aVec )
107 {
108 x = aVec.x;
109 y = aVec.y;
110 }
111
113 template <typename CastedType>
115 {
116 if( std::is_same<CastedType, int>::value )
117 {
118 T minI = static_cast<T>( std::numeric_limits<int>::min() );
119 T maxI = static_cast<T>( std::numeric_limits<int>::max() );
120
121 return VECTOR2<int>( static_cast<int>( Clamp( minI, x, maxI ) ),
122 static_cast<int>( Clamp( minI, y, maxI ) ) );
123 }
124 else
125 {
126 return VECTOR2<CastedType>( static_cast<CastedType>( x ), static_cast<CastedType>( y ) );
127 }
128 }
129
130 // virtual ~VECTOR2();
131
139 T EuclideanNorm() const;
140
149
150
157
164 VECTOR2<T> Resize( T aNewLength ) const;
165
171 const std::string Format() const;
172
176 extended_type Cross( const VECTOR2<T>& aVector ) const;
177
181 extended_type Dot( const VECTOR2<T>& aVector ) const;
182
183
184 // Operators
185
187 VECTOR2<T>& operator=( const VECTOR2<T>& aVector );
188
190 VECTOR2<T> operator+( const VECTOR2<T>& aVector ) const;
191
193 VECTOR2<T> operator+( const T& aScalar ) const;
194
196 VECTOR2<T>& operator+=( const VECTOR2<T>& aVector );
197
199 VECTOR2<T>& operator*=( const VECTOR2<T>& aVector );
200
201 VECTOR2<T>& operator*=( const T& aScalar );
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/( double 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
245
246// ----------------------
247// --- Implementation ---
248// ----------------------
249
250template <class T>
252{
253}
254
255
256template <class T>
258{
259 x = aX;
260 y = aY;
261}
262
263
264template <class T>
266{
267 return static_cast<T>( sqrt( (extended_type) x * x + (extended_type) y * y ) );
268}
269
270
271template <class T>
273{
274 return (extended_type) x * x + (extended_type) y * y;
275}
276
277
278template <class T>
280{
281 VECTOR2<T> perpendicular( -y, x );
282 return perpendicular;
283}
284
285
286template <class T>
288{
289 x = aVector.x;
290 y = aVector.y;
291 return *this;
292}
293
294
295template <class T>
297{
298 x += aVector.x;
299 y += aVector.y;
300 return *this;
301}
302
303
304template <class T>
306{
307 x *= aVector.x;
308 y *= aVector.y;
309 return *this;
310}
311
312
313template <class T>
315{
316 x *= aScalar;
317 y *= aScalar;
318 return *this;
319}
320
321
322template <class T>
324{
325 x += aScalar;
326 y += aScalar;
327 return *this;
328}
329
330
331template <class T>
333{
334 x -= aVector.x;
335 y -= aVector.y;
336 return *this;
337}
338
339
340template <class T>
342{
343 x -= aScalar;
344 y -= aScalar;
345 return *this;
346}
347
348
349template <class T>
350VECTOR2<T> VECTOR2<T>::Resize( T aNewLength ) const
351{
352 if( x == 0 && y == 0 )
353 return VECTOR2<T> ( 0, 0 );
354
355 extended_type x_sq = (extended_type) x * x;
356 extended_type y_sq = (extended_type) y * y;
357 extended_type l_sq = x_sq + y_sq;
358 extended_type newLength_sq = (extended_type) aNewLength * aNewLength;
359 double newX = std::sqrt( rescale( newLength_sq, x_sq, l_sq ) );
360 double newY = std::sqrt( rescale( newLength_sq, y_sq, l_sq ) );
361
362 if( std::is_integral<T>::value )
363 {
364 return VECTOR2<T>( static_cast<T>( x < 0 ? -KiROUND( newX ) : KiROUND( newX ) ),
365 static_cast<T>( y < 0 ? -KiROUND( newY ) : KiROUND( newY ) ) )
366 * sign( aNewLength );
367 }
368 else
369 {
370 return VECTOR2<T>( static_cast<T>( x < 0 ? -newX : newX ),
371 static_cast<T>( y < 0 ? -newY : newY ) )
372 * sign( aNewLength );
373 }
374}
375
376
377template <class T>
378const std::string VECTOR2<T>::Format() const
379{
380 std::stringstream ss;
381
382 ss << "( xy " << x << " " << y << " )";
383
384 return ss.str();
385}
386
387
388template <class T>
390{
391 return VECTOR2<T> ( x + aVector.x, y + aVector.y );
392}
393
394
395template <class T>
396VECTOR2<T> VECTOR2<T>::operator+( const T& aScalar ) const
397{
398 return VECTOR2<T> ( x + aScalar, y + aScalar );
399}
400
401
402template <class T>
404{
405 return VECTOR2<T> ( x - aVector.x, y - aVector.y );
406}
407
408
409template <class T>
410VECTOR2<T> VECTOR2<T>::operator-( const T& aScalar ) const
411{
412 return VECTOR2<T> ( x - aScalar, y - aScalar );
413}
414
415
416template <class T>
418{
419 return VECTOR2<T> ( -x, -y );
420}
421
422
423template <class T>
425{
426 return (extended_type)aVector.x * x + (extended_type)aVector.y * y;
427}
428
429
430template <class T>
431VECTOR2<T> VECTOR2<T>::operator*( const T& aFactor ) const
432{
433 VECTOR2<T> vector( x * aFactor, y * aFactor );
434 return vector;
435}
436
437
438template <class T>
439VECTOR2<T> VECTOR2<T>::operator/( double aFactor ) const
440{
441 if( std::is_integral<T>::value )
442 return VECTOR2<T>( KiROUND( x / aFactor ), KiROUND( y / aFactor ) );
443 else
444 return VECTOR2<T>( static_cast<T>( x / aFactor ), static_cast<T>( y / aFactor ) );
445}
446
447
448template <class T>
449VECTOR2<T> operator*( const T& aFactor, const VECTOR2<T>& aVector )
450{
451 VECTOR2<T> vector( aVector.x * aFactor, aVector.y * aFactor );
452 return vector;
453}
454
455
456template <class T>
458{
459 return (extended_type) x * (extended_type) aVector.y -
460 (extended_type) y * (extended_type) aVector.x;
461}
462
463
464template <class T>
466{
467 return (extended_type) x * (extended_type) aVector.x +
468 (extended_type) y * (extended_type) aVector.y;
469}
470
471
472template <class T>
473bool VECTOR2<T>::operator<( const VECTOR2<T>& aVector ) const
474{
475 return ( *this * *this ) < ( aVector * aVector );
476}
477
478
479template <class T>
480bool VECTOR2<T>::operator<=( const VECTOR2<T>& aVector ) const
481{
482 return ( *this * *this ) <= ( aVector * aVector );
483}
484
485
486template <class T>
487bool VECTOR2<T>::operator>( const VECTOR2<T>& aVector ) const
488{
489 return ( *this * *this ) > ( aVector * aVector );
490}
491
492
493template <class T>
494bool VECTOR2<T>::operator>=( const VECTOR2<T>& aVector ) const
495{
496 return ( *this * *this ) >= ( aVector * aVector );
497}
498
499
500template <class T>
501bool VECTOR2<T>::operator==( VECTOR2<T> const& aVector ) const
502{
503 return ( aVector.x == x ) && ( aVector.y == y );
504}
505
506
507template <class T>
508bool VECTOR2<T>::operator!=( VECTOR2<T> const& aVector ) const
509{
510 return ( aVector.x != x ) || ( aVector.y != y );
511}
512
513
514template <class T>
516{
517 if( aA.x > aB.x )
518 return aA;
519 else if( aA.x == aB.x && aA.y > aB.y )
520 return aA;
521
522 return aB;
523}
524
525
526template <class T>
528{
529 if( aA.x < aB.x )
530 return aA;
531 else if( aA.x == aB.x && aA.y < aB.y )
532 return aA;
533
534 return aB;
535}
536
537
538template <class T>
540{
541 if( aA.x < aB.x )
542 return -1;
543 else if( aA.x > aB.x )
544 return 1;
545 else // aA.x == aB.x
546 {
547 if( aA.y < aB.y )
548 return -1;
549 else if( aA.y > aB.y )
550 return 1;
551 else
552 return 0;
553 }
554}
555
556
565template <class T>
566typename std::enable_if<!std::numeric_limits<T>::is_integer, bool>::type
567equals( VECTOR2<T> const& aFirst, VECTOR2<T> const& aSecond,
568 T aEpsilon = std::numeric_limits<T>::epsilon() )
569{
570 if( !equals( aFirst.x, aSecond.x, aEpsilon ) )
571 {
572 return false;
573 }
574
575 return equals( aFirst.y, aSecond.y, aEpsilon );
576}
577
578
579template <class T>
580std::ostream& operator<<( std::ostream& aStream, const VECTOR2<T>& aVector )
581{
582 aStream << "[ " << aVector.x << " | " << aVector.y << " ]";
583 return aStream;
584}
585
586/* Default specializations */
589
590/* KiROUND specialization for vectors */
591inline VECTOR2I KiROUND( const VECTOR2D& vec )
592{
593 return VECTOR2I( KiROUND( vec.x ), KiROUND( vec.y ) );
594}
595
596/* STL specializations */
597namespace std
598{
599 // Required to enable correct use in std::map/unordered_map
600 // DO NOT USE hash tables with VECTOR2 elements. It is inefficient
601 // and degenerates to a linear search. Use the std::map/std::set
602 // trees instead that utilize the less operator below
603 // This function is purposely deleted after substantial testing
604 template <>
605 struct hash<VECTOR2I>
606 {
607 size_t operator()( const VECTOR2I& k ) const = delete;
608 };
609
610 // Required to enable use of std::hash with maps.
611 template <>
612 struct less<VECTOR2I>
613 {
614 bool operator()( const VECTOR2I& aA, const VECTOR2I& aB ) const;
615 };
616}
617
618#endif // VECTOR2D_H_
Define a general 2D-vector/point.
Definition: vector2d.h:70
VECTOR2< T > operator-(const T &aScalar) const
Scalar subtraction operator.
Definition: vector2d.h:410
VECTOR2< T > & operator+=(const T &aScalar)
Compound assignment operator.
Definition: vector2d.h:323
VECTOR2(const VECTOR2< CastingType > &aVec)
Initializes a vector from another specialization. Beware of rounding issues.
Definition: vector2d.h:88
VECTOR2< T > & operator*=(const T &aScalar)
Definition: vector2d.h:314
extended_type SquaredEuclideanNorm() const
Compute the squared euclidean norm of the vector, which is defined as (x ** 2 + y ** 2).
Definition: vector2d.h:272
VECTOR2< T > & operator*=(const VECTOR2< T > &aVector)
Compound assignment operator.
Definition: vector2d.h:305
const std::string Format() const
Return the vector formatted as a string.
Definition: vector2d.h:378
VECTOR2()
Construct a 2D-vector with x, y = 0.
Definition: vector2d.h:251
VECTOR2< T > operator/(double aFactor) const
Division with a factor.
Definition: vector2d.h:439
VECTOR2(T x, T y)
Construct a vector with given components x, y.
Definition: vector2d.h:257
bool operator>=(const VECTOR2< T > &aVector) const
Definition: vector2d.h:494
static constexpr extended_type ECOORD_MAX
Definition: vector2d.h:75
VECTOR2< T > & operator+=(const VECTOR2< T > &aVector)
Compound assignment operator.
Definition: vector2d.h:296
VECTOR2(const VECTOR2< T > &aVec)
Copy a vector.
Definition: vector2d.h:106
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition: vector2d.h:265
VECTOR2_TRAITS< T >::extended_type extended_type
Definition: vector2d.h:72
bool operator==(const VECTOR2< T > &aVector) const
Equality operator.
Definition: vector2d.h:501
VECTOR2< T > Perpendicular() const
Compute the perpendicular vector.
Definition: vector2d.h:279
bool operator<=(const VECTOR2< T > &aVector) const
Definition: vector2d.h:480
VECTOR2< T > & operator=(const VECTOR2< T > &aVector)
Assignment operator.
Definition: vector2d.h:287
VECTOR2< T > & operator-=(const T &aScalar)
Compound assignment operator.
Definition: vector2d.h:341
bool operator!=(const VECTOR2< T > &aVector) const
Not equality operator.
Definition: vector2d.h:508
static constexpr extended_type ECOORD_MIN
Definition: vector2d.h:76
extended_type Cross(const VECTOR2< T > &aVector) const
Compute cross product of self with aVector.
Definition: vector2d.h:457
VECTOR2< CastedType > operator()() const
Cast a vector to another specialized subclass. Beware of rounding issues.
Definition: vector2d.h:114
bool operator>(const VECTOR2< T > &aVector) const
Greater than operator.
Definition: vector2d.h:487
extended_type Dot(const VECTOR2< T > &aVector) const
Compute dot product of self with aVector.
Definition: vector2d.h:465
extended_type operator*(const VECTOR2< T > &aVector) const
Scalar product operator.
Definition: vector2d.h:424
VECTOR2< T > operator+(const VECTOR2< T > &aVector) const
Vector addition operator.
Definition: vector2d.h:389
VECTOR2< T > operator-(const VECTOR2< T > &aVector) const
Vector subtraction operator.
Definition: vector2d.h:403
VECTOR2< T > operator*(const T &aFactor) const
Multiplication with a factor.
Definition: vector2d.h:431
VECTOR2< T > operator+(const T &aScalar) const
Scalar addition operator.
Definition: vector2d.h:396
VECTOR2< T > & operator-=(const VECTOR2< T > &aVector)
Compound assignment operator.
Definition: vector2d.h:332
bool operator<(const VECTOR2< T > &aVector) const
Smaller than operator.
Definition: vector2d.h:473
VECTOR2< T > Resize(T aNewLength) const
Return a vector of the same direction, but length specified in aNewLength.
Definition: vector2d.h:350
VECTOR2< T > operator-()
Negate Vector operator.
Definition: vector2d.h:417
T coord_type
Definition: vector2d.h:73
STL namespace.
int64_t extended_type
Definition: vector2d.h:52
Traits class for VECTOR2.
Definition: vector2d.h:43
T extended_type
extended range/precision types used by operations involving multiple multiplications to prevent overf...
Definition: vector2d.h:46
size_t operator()(const VECTOR2I &k) const =delete
int sign(T val)
Definition: util.h:124
T rescale(T aNumerator, T aValue, T aDenominator)
Scale a number (value) by rational (numerator/denominator).
Definition: util.h:118
constexpr T Clamp(const T &lower, const T &value, const T &upper)
Limit value within the range lower <= value <= upper.
Definition: util.h:64
VECTOR2< double > VECTOR2D
Definition: vector2d.h:587
std::ostream & operator<<(std::ostream &aStream, const VECTOR2< T > &aVector)
Definition: vector2d.h:580
const VECTOR2< T > LexicographicalMax(const VECTOR2< T > &aA, const VECTOR2< T > &aB)
Definition: vector2d.h:515
std::enable_if<!std::numeric_limits< T >::is_integer, bool >::type equals(VECTOR2< T > const &aFirst, VECTOR2< T > const &aSecond, T aEpsilon=std::numeric_limits< T >::epsilon())
Template to compare two VECTOR2<T> values for equality within a required epsilon.
Definition: vector2d.h:567
VECTOR2I KiROUND(const VECTOR2D &vec)
Definition: vector2d.h:591
const VECTOR2< T > LexicographicalMin(const VECTOR2< T > &aA, const VECTOR2< T > &aB)
Definition: vector2d.h:527
int LexicographicalCompare(const VECTOR2< T > &aA, const VECTOR2< T > &aB)
Definition: vector2d.h:539
VECTOR2< int > VECTOR2I
Definition: vector2d.h:588
VECTOR2< T > operator*(const T &aFactor, const VECTOR2< T > &aVector)
Definition: vector2d.h:449