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