-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinalg.h
More file actions
569 lines (555 loc) · 22.3 KB
/
Copy pathlinalg.h
File metadata and controls
569 lines (555 loc) · 22.3 KB
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
#ifndef _Aloschil_Linalg_h
#define _Aloschil_Linalg_h
///////////////////////////////////////////////////////////////////////////////
/// @file Matrix.h
/// @brief This file contains definition of Matrix class
///
/// Related Files: The implementation is in LinAlg.cpp
/// Other related files are: types.h
///
/// Maintained by: NSTU
///
/// Copyright c 2007 aloschil. All right reserved.
///
/// CONFIDENTIALLY AND LIMITED USE
///
/// This software, including any software of third parties embodied herein,
/// contains information and concepts wich are confidential to Alexander
/// Loschilov and such third parties. This software is licensed for use
/// solely in accordance with the terms and conditions of the applicable
/// license agreement with Alexander Loschilov or his authorized distributor.
///////////////////////////////////////////////////////////////////////////////
#include <vector>
#include <list>
#include "matrix.h"
namespace Aloschil
{
extern const double deg;
///////////////////////////////////////////////////////////////////////////////
/// @class NoCrossPoint
/// @brief Unable to get cross point of some elements
///////////////////////////////////////////////////////////////////////////////
class NoCrossPoint : public MatrixError{};
////////////////////////////////////////////////////////////////////
/// @ingroup Linear algebra
/// @enum SectionsPositionalRelationship
/// @brief Characterizes positional relationship of two sections
///
/// These characteristics are directional.
///
/// <A NAME="SectionsPositionalRelationship">
/// <IMG src=images/SectionsPositionalRelationship_enum.jpg ALIGN=left>
/// </A>
///
/// On image (1) CD is TOUCHING, AB is TOUCHED.<br>
/// On image (2) CD is TOUCHED, AB is TOUCHING.<br>
/// On image (3) AB and CD are both CROSSING_OR_CROSSED.<br>
/// On image (4) AB and CD are both COLLINEAR.<br>
/// On image (5) AB and CD are both ADJACENT.<br>
/// On image (6) AB and CD are both NONINTERSECTING.
///
////////////////////////////////////////////////////////////////////
enum SectionsPositionalRelationship
{
COLLINEAR,
CROSSING_OR_CROSSED,
TOUCHED,
TOUCHING,
ADJACENT,
NONINTERSECTING
};
//////////////////////////////////////////////////////////////////////////////
/// @ingroup Linear algebra
///
/// @brief Computes angle in between 2D column vectors represented as Matrixes
///
/// The value returned is always between -M_PI and M_PI, including M_PI.
/// Returns 0 if one of vectors is zero-vector.
///
/// Example 1:
/// <PRE>
/// Matrix v;
/// v.setSize(2,1);
/// v[0]=1;
/// v[1]=3;
///
/// Matrix omega;
/// omega.setSize(2,1);
/// omega[0]=-1;
/// omega[1]=-0.000001;
///
/// printf("ang(v,omega) = %f\n",ang(v,omega)/deg);
/// // ang(v,omega) = 108.435006
/// </PRE>
///
/// @param v � input � Vector to count angle from.
/// @param omega � input � Vector to count angle to.
///
/// @returns � Angle in rand between vectors
///
//////////////////////////////////////////////////////////////////////////////
double ang(const Matrix &v,const Matrix &omega);
//////////////////////////////////////////////////////////////////////////////
/// @ingroup Linear algebra
///
/// @brief Calculates polar angle for 2D vector in Cartesian coordinate system
///
/// Calculates the angle (in radians) from the x-axis to a line containing the
/// origin (0,0) and the point (x,y). The value returned is always between
/// -M_PI and M_PI, including M_PI.
///
/// Example:
/// <PRE>
/// Matrix p;
/// p.setSize(2,1);
/// p[0]=-1;
/// p[1]=-0.000001;
///
/// printf("atan(p) = %f\n",atan(p)*deg);
/// // atan(p) = -179.999943
/// </PRE>
///
/// @param p � input � Vector to calculate polar angle of.
///
/// @returns � Returns the angle (in radians).
///
//////////////////////////////////////////////////////////////////////////////
double atan(const Matrix &p);
//////////////////////////////////////////////////////////////////////////////
/// @ingroup Linear algebra
///
/// @brief Calculates parameters (t - <A HREF="#tParameterImage">see image</A>) of cross point.
///
/// <A NAME="tParameterImage">
/// <IMG src=images/getCrossingParameters.jpg ALIGN=left>
/// </A>
///
/// Example 1:
/// <IMG src=images/getCrossingParameters_example_1.jpg ALIGN=left>
/// <PRE>
/// Matrix A = Vector(1,2);
/// Matrix B = Vector(5,4);
/// Matrix C = Vector(5,1);
/// Matrix D = Vector(2,4);
///
/// Matrix T = getCrossingParameters(A,B,C,D);
/// T.print();
/// </PRE>
/// Output 1:<B><I><br>| 0.5<br> | 0.667</I></B>
///
/// <br>Example 2:
/// <IMG src=images/getCrossingParameters_example_2.jpg ALIGN=left>
/// <PRE>
/// Matrix A = Vector(1,2);
/// Matrix B = Vector(5,4);
/// Matrix C = Vector(5,1);
/// Matrix D = Vector(4,2);
///
/// Matrix T = getCrossingParameters(A,B,C,D);
/// T.print();
/// </PRE>
/// Output 2:<B><I><br>| 0.5<br> | 2</I></B>
///
/// @param A � input � First point of first line segment.
/// @param B � input � Second point of first line segment.
/// @param C � input � First point of second line segment.
/// @param D � input � Second point of second line segment.
/// @warning � (B-A) and (C-D) vectors are to be canted otherwise
/// ErrorMath exception is thrown.
///
/// @returns � 2D column vector with t-parameters as coordinates.
///
//////////////////////////////////////////////////////////////////////////////
Matrix getCrossingParameters(const Matrix &A,const Matrix &B,
const Matrix &C,const Matrix &D);
//////////////////////////////////////////////////////////////////////////////
/// @ingroup Linear algebra
///
/// @brief Calculates cross point (<A HREF="#getCrosspoint">red one on image</A>) of two lines passing throught two points
///
/// <A NAME="getCrosspoint">
/// <IMG src=images/getCrosspoint.jpg ALIGN=left>
/// </A>
///
/// @param A � input � The first point the first line passing through.
/// @param B � input � The second point the first line passing through.
/// @param C � input � The first point the second line passing through.
/// @param D � input � The second point the second line passing through.
/// @warning � (B-A) and (C-D) vectors are to be canted otherwise
/// ErrorMath exception is thrown.
///
/// @returns � Crosspoint as 2D column vector.
///
//////////////////////////////////////////////////////////////////////////////
Matrix getCrossPoint( const Matrix &A,const Matrix &B,
const Matrix &C,const Matrix &D);
//////////////////////////////////////////////////////////////////////////////
/// @ingroup Linear algebra
///
/// @brief Calculates cross point (<A HREF="#getSectionsCrosspoint">red one on image</A>) of two lines passing throught two points
///
/// <A NAME="getSectionsCrosspoint">
/// <IMG src=images/getSectionsCrosspoint.jpg ALIGN=left>
/// </A>
///
/// @param A � input � The first point the first section.
/// @param B � input � The second point the first section.
/// @param C � input � The first point the second section.
/// @param D � input � The second point the second section.
/// @warning � (B-A) and (C-D) vectors are to be canted otherwise
/// ErrorMath exception is thrown.
/// If there is no cross point NoCrossPoint exception is thrown.
///
/// @returns � Crosspoint as 2D column vector.
///
//////////////////////////////////////////////////////////////////////////////
Matrix getSectionsCrossPoint( const Matrix &A,const Matrix &B,
const Matrix &C,const Matrix &D);
//////////////////////////////////////////////////////////////////////////////
/// @ingroup Linear algebra
///
/// @brief Calculates cross point (<A HREF="#getCrosspoint">red one on image</A>) of two lines passing throught two points
///
/// <A NAME="getSectionsCrosspoint">
/// <IMG src=images/getSectionsCrosspoint.jpg ALIGN=left>
/// </A>
///
/// @param A � input � The first point the first section.
/// @param B � input � The second point the first section.
/// @param C � input � The first point the second section.
/// @param D � input � The second point the second section.
/// @warning � (B-A) and (C-D) vectors are to be canted otherwise
/// ErrorMath exception is thrown.
/// If there is no cross point NoCrossPoint exception is thrown.
///
/// @returns � Crosspoint as 2D column vector.
///
//////////////////////////////////////////////////////////////////////////////
double getDistance(const Matrix &A, const Matrix &B);
//////////////////////////////////////////////////////////////////////////////
/// @ingroup Linear algebra
///
/// @brief Classifies section CD with respect to AB
///
/// @see SectionsPositionalRelationship for more information.
///
/// @param A � input � The first point the first section.
/// @param B � input � The second point the first section.
/// @param C � input � The first point the second section.
/// @param D � input � The second point the second section.
/// @param rangeOfEquivalence � input � Distance between points where they
/// are considered to be equal.
/// @param rangeOfCollinearity � input � Range of angle within which lines
/// are considered to be collinear.
///
/// @returns � Value that stands for class.
///
//////////////////////////////////////////////////////////////////////////////
SectionsPositionalRelationship getPositionalRelationship(
const Matrix &A,const Matrix &B,
const Matrix &C,const Matrix &D,
double rangeOfEquivalence,
double rangeOfCollinearity);
//////////////////////////////////////////////////////////////////////////////
/// @ingroup Linear algebra
///
/// @brief Classifies section CD with respect to AB
///
/// @see SectionsPositionalRelationship for more information.
///
/// @param A � input � The first point the first section.
/// @param B � input � The second point the first section.
/// @param C � input � The first point the second section.
/// @param D � input � The second point the second section.
/// @param rangeOfEquivalence � input � Distance between points where they
/// are considered to be equal.
/// @param rangeOfCollinearity � input � Range of angle within which lines
/// are considered to be collinear.
///
/// @returns � Value that stands for class.
///
//////////////////////////////////////////////////////////////////////////////
std::vector<Matrix> getRandomPolygon( const Matrix &T,
double a, double b,
double teta);
//////////////////////////////////////////////////////////////////////////////
/// @ingroup Linear algebra
///
/// @brief Classifies section CD with respect to AB
///
/// @see SectionsPositionalRelationship for more information.
///
/// @param A � input � The first point the first section.
/// @param B � input � The second point the first section.
/// @param C � input � The first point the second section.
/// @param D � input � The second point the second section.
/// @param rangeOfEquivalence � input � Distance between points where they
/// are considered to be equal.
/// @param rangeOfCollinearity � input � Range of angle within which lines
/// are considered to be collinear.
///
/// @returns � Value that stands for class.
///
//////////////////////////////////////////////////////////////////////////////
std::vector<Matrix> getRound( const Matrix &a,
const Matrix &b,
const Matrix &c,
double r,
double rho,
int numberOfSegments,
double rangeOfCollinearity=EPSILON);
//////////////////////////////////////////////////////////////////////////////
/// @ingroup Linear algebra
///
/// @brief This function generates set of points representing an arc
///
///
///
/// @param o � input � The first point the first section.
/// @param r � input � The second point the first section.
/// @param alfa � input � The first point the second section.
/// @param redoubledPhi � input � The second point the second section.
/// @param numberOfSegments � input � Distance between points where they
/// are considered to be equal.
///
/// @returns � Value that stands for class.
///
//////////////////////////////////////////////////////////////////////////////
std::vector<Matrix> getArc( const Matrix &o,
double r,
double alfa,
double redoubledPhi,
int numberOfSegments);
//////////////////////////////////////////////////////////////////////////////
/// @ingroup Linear algebra
///
/// @brief Used to rotate vector on 90 degrees counterclockwise.
///
/// @param vectorToRotate � input � 2D column vector.
///
/// @returns � 2D column vector rotated 90 degrees counterclockwise.
///
//////////////////////////////////////////////////////////////////////////////
Matrix Lrot(const Matrix &vectorToRotate);
//////////////////////////////////////////////////////////////////////////////
/// @ingroup Linear algebra
///
/// @brief Used to rotate vector on 90 degrees clockwise.
///
/// @param vectorToRotate � input � 2D column vector.
///
/// @returns � 2D column vector rotated 90 degrees clockwise.
///
//////////////////////////////////////////////////////////////////////////////
Matrix Rrot(const Matrix &vectorToRotate);
//////////////////////////////////////////////////////////////////////////////
/// @ingroup Linear algebra
///
/// @brief Used to get normalized vector.
///
/// @param vectorToNormalize � input � multidimensional column vector.
///
/// @returns � Normalized column vector with the same dimensionality as
/// for the input one.
///
//////////////////////////////////////////////////////////////////////////////
Matrix normalize(const Matrix &vectorToNormalize);
//////////////////////////////////////////////////////////////////////////////
/// @ingroup Basic functions
///
/// @brief Used to get the sign of the parameter passed.
///
/// Example:
/// <PRE>
/// double a = -3423;
/// double b = 3423;
/// double c = 0;
///
/// printf("Value returned by sing(a) is %f.\r\n",sign(a));
/// // Value returned by sing(a) is -1.000000.
/// printf("Value returned by sing(b) is %f.\r\n",sign(b));
/// // Value returned by sing(b) is 1.000000.
/// printf("Value returned by sing(c) is %f.\r\n",sign(c));
/// // Value returned by sing(c) is 0.000000.
/// </PRE>
///
/// @param x � input � The variable to get the sign of.
///
/// @returns � Returns 0 if x = 0, 1 if x > 0, and 1 otherwise.
///
//////////////////////////////////////////////////////////////////////////////
double sign(double x);
//////////////////////////////////////////////////////////////////////////////
/// @ingroup Linear algebra
///
/// @brief Compiles list of closed circuits from list of segments
///
/// Very strict constraints are imposed on segments circuits composed from.
/// It's to be no multiple segments.
/// It's to be no segments with deadlock vertexes.
/// It's to be only one paar on equivalent points.
///
/// @param listOfSegments - input - list of segments to extract circuits from
/// @param rangeOfEquivalence � input � Distance between points where they
/// are considered to be equal.
///
/// @returns - Returns list of closed circuits if all the conditions specified
/// above are met and empty list otherwise.
///
//////////////////////////////////////////////////////////////////////////////
std::vector<std::vector<Matrix> > getClosedCircuits(
std::vector<std::vector<Matrix> > listOfSegments,
double rangeOfEquivalence=EPSILON);
//////////////////////////////////////////////////////////////////////////////
/// @ingroup Linear algebra
///
/// @brief Calculates cross product of two three-element column vectors
///
/// Direction of the cross-product is orthogonal to u and v in the direction
/// determined by the right hand rule
///
/// @param u - input - first three-element column vector to calculate cross
/// product of
/// @param v � input � second three-element column vector to calculate cross
/// product of
///
/// @returns - Returns the vector cross product of u and v
///
//////////////////////////////////////////////////////////////////////////////
Matrix getCrossProduct(const Matrix &u, const Matrix&v);
//////////////////////////////////////////////////////////////////////////////
/// @ingroup Linear algebra
///
/// @brief This function binds segments to closed circuit
///
/// @param Ls - input - list of segments
///
/// @returns - list of polygons
///
//////////////////////////////////////////////////////////////////////////////
std::list<std::list<Matrix> > cont(std::list<std::list<Matrix> > Ls,double epsilon=1e-2);
//////////////////////////////////////////////////////////////////////////////
/// @ingroup Linear algebra
///
/// @brief Minimizes polygon
///
/// @param Ls - input - polygon to minimize represented as list of 2D column Vectors
///
/// @returns - Minimized polygon represented as list of 2D column Vectors
///
//////////////////////////////////////////////////////////////////////////////
std::list<Matrix> min_poly(std::list<Matrix> P,double epsilon=1e-2);
//////////////////////////////////////////////////////////////////////////////
/// @ingroup Debug
///
/// @brief Prints list of matrices
///
/// The only reason of creation is debugin while writing primary functions
///
/// @param fileName - input - name of the file to save list in
/// @param listToPrint - input - self-explanatory
///
/// @returns - Nothing
///
//////////////////////////////////////////////////////////////////////////////
void printList(const char *fileName,std::list<Matrix> listToPrint);
//////////////////////////////////////////////////////////////////////////////
/// @ingroup Debug
///
/// @brief Prints list of lists of matrices
///
/// The only reason of creation is debugin while writing primary functions
///
/// @param fileName - input - name of the file to save list in
/// @param listToPrint - input - self-explanatory
///
/// @returns - Minimized polygon represented as list of 2D column Vectors
///
//////////////////////////////////////////////////////////////////////////////
void printListOfLists(const char*,std::list<std::list<Matrix> >listToPrint);
//////////////////////////////////////////////////////////////////////////////
/// @ingroup Linear algebra
///
/// @brief This function returns matrix of rotation on X
///
/// @param Ls - input - list of segments
///
/// @returns - list of polygons
///
//////////////////////////////////////////////////////////////////////////////
Matrix rotateX(double angle);
//////////////////////////////////////////////////////////////////////////////
/// @ingroup Linear algebra
///
/// @brief This function binds segments to closed circuit
///
/// @param Ls - input - list of segments
///
/// @returns - list of polygons
///
//////////////////////////////////////////////////////////////////////////////
Matrix rotateY(double angle);
//////////////////////////////////////////////////////////////////////////////
/// @ingroup Linear algebra
///
/// @brief This function binds segments to closed circuit
///
/// @param Ls - input - list of segments
///
/// @returns - list of polygons
///
//////////////////////////////////////////////////////////////////////////////
Matrix rotateZ(double angle);
//////////////////////////////////////////////////////////////////////////////
/// @ingroup Linear algebra
///
/// @brief This function binds segments to closed circuit
///
/// @param Ls - input - list of segments
///
/// @returns - list of polygons
///
//////////////////////////////////////////////////////////////////////////////
Matrix scale(double x,double y,double z);
//////////////////////////////////////////////////////////////////////////////
/// @ingroup Linear algebra
///
/// @brief This function binds segments to closed circuit
///
/// @param Ls - input - list of segments
///
/// @returns - list of polygons
///
//////////////////////////////////////////////////////////////////////////////
Matrix translate(double x,double y,double z);
//////////////////////////////////////////////////////////////////////////////
/// @ingroup Linear algebra
///
/// @brief This function tests weather point is within a polygon (conv2_test)
///
/// @param point - input - Point to test
/// @param polygon - input - Polygon to test
///
/// @returns - 1 if point is within a polygon
/// -1 if point is outside of a polygon
/// 0 if point is on the border of a polygon
///
//////////////////////////////////////////////////////////////////////////////
int point_is_in_polygon(const Matrix &point, const std::vector<Matrix> &polygon);
//////////////////////////////////////////////////////////////////////////////
/// @ingroup Linear algebra
///
/// @brief This function tests position of point relating to line
///
/// @param A - input - The first point of the line
/// @param B - input - The second point of the line
/// @param P - input - A point
///
/// @returns - > 0 if point is at the right side of line
/// < 0 if point is at the left side of line
/// 0 if point is on the line
///
//////////////////////////////////////////////////////////////////////////////
double line_to_point_test(const Matrix &A, const Matrix &B, const Matrix&P);
};
#endif // _Aloschil_Linalg_h