forked from andrewkirillov/AForge.NET
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathPointsCloud.cs
608 lines (534 loc) · 25.1 KB
/
PointsCloud.cs
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
// AForge Math Library
// AForge.NET framework
// http://www.aforgenet.com/framework/
//
// Copyright © AForge.NET, 2007-2011
// contacts@aforgenet.com
//
namespace AForge.Math.Geometry
{
using System;
using System.Collections.Generic;
using AForge;
/// <summary>
/// Set of tools for processing collection of points in 2D space.
/// </summary>
///
/// <remarks><para>The static class contains set of routines, which provide different
/// operations with collection of points in 2D space. For example, finding the
/// furthest point from a specified point or line.</para>
///
/// <para>Sample usage:</para>
/// <code>
/// // create points' list
/// List<IntPoint> points = new List<IntPoint>( );
/// points.Add( new IntPoint( 10, 10 ) );
/// points.Add( new IntPoint( 20, 15 ) );
/// points.Add( new IntPoint( 15, 30 ) );
/// points.Add( new IntPoint( 40, 12 ) );
/// points.Add( new IntPoint( 30, 20 ) );
/// // get furthest point from the specified point
/// IntPoint p1 = PointsCloud.GetFurthestPoint( points, new IntPoint( 15, 15 ) );
/// Console.WriteLine( p1.X + ", " + p1.Y );
/// // get furthest point from line
/// IntPoint p2 = PointsCloud.GetFurthestPointFromLine( points,
/// new IntPoint( 50, 0 ), new IntPoint( 0, 50 ) );
/// Console.WriteLine( p2.X + ", " + p2.Y );
/// </code>
/// </remarks>
///
public static class PointsCloud
{
/// <summary>
/// Shift cloud by adding specified value to all points in the collection.
/// </summary>
///
/// <param name="cloud">Collection of points to shift their coordinates.</param>
/// <param name="shift">Point to shift by.</param>
///
public static void Shift( IList<IntPoint> cloud, IntPoint shift )
{
for ( int i = 0, n = cloud.Count; i < n; i++ )
{
cloud[i] = cloud[i] + shift;
}
}
/// <summary>
/// Get bounding rectangle of the specified list of points.
/// </summary>
///
/// <param name="cloud">Collection of points to get bounding rectangle for.</param>
/// <param name="minXY">Point comprised of smallest X and Y coordinates.</param>
/// <param name="maxXY">Point comprised of biggest X and Y coordinates.</param>
///
public static void GetBoundingRectangle( IEnumerable<IntPoint> cloud, out IntPoint minXY, out IntPoint maxXY )
{
int minX = int.MaxValue;
int maxX = int.MinValue;
int minY = int.MaxValue;
int maxY = int.MinValue;
foreach ( IntPoint pt in cloud )
{
int x = pt.X;
int y = pt.Y;
// check X coordinate
if ( x < minX )
minX = x;
if ( x > maxX )
maxX = x;
// check Y coordinate
if ( y < minY )
minY = y;
if ( y > maxY )
maxY = y;
}
if ( minX > maxX ) // if no point appeared to set either minX or maxX
throw new ArgumentException( "List of points can not be empty." );
minXY = new IntPoint( minX, minY );
maxXY = new IntPoint( maxX, maxY );
}
/// <summary>
/// Get center of gravity for the specified list of points.
/// </summary>
///
/// <param name="cloud">List of points to calculate center of gravity for.</param>
///
/// <returns>Returns center of gravity (mean X-Y values) for the specified list of points.</returns>
///
public static Point GetCenterOfGravity( IEnumerable<IntPoint> cloud )
{
int numberOfPoints = 0;
float xSum = 0, ySum = 0;
foreach ( IntPoint pt in cloud )
{
xSum += pt.X;
ySum += pt.Y;
numberOfPoints++;
}
xSum /= numberOfPoints;
ySum /= numberOfPoints;
return new Point( xSum, ySum );
}
/// <summary>
/// Find furthest point from the specified point.
/// </summary>
///
/// <param name="cloud">Collection of points to search furthest point in.</param>
/// <param name="referencePoint">The point to search furthest point from.</param>
///
/// <returns>Returns a point, which is the furthest away from the <paramref name="referencePoint"/>.</returns>
///
public static IntPoint GetFurthestPoint( IEnumerable<IntPoint> cloud, IntPoint referencePoint )
{
IntPoint furthestPoint = referencePoint;
float maxDistance = -1;
int rx = referencePoint.X;
int ry = referencePoint.Y;
foreach ( IntPoint point in cloud )
{
int dx = rx - point.X;
int dy = ry - point.Y;
// we are not calculating square root for finding "real" distance,
// since it is really not important for finding furthest point
float distance = dx * dx + dy * dy;
if ( distance > maxDistance )
{
maxDistance = distance;
furthestPoint = point;
}
}
return furthestPoint;
}
/// <summary>
/// Find two furthest points from the specified line.
/// </summary>
///
/// <param name="cloud">Collection of points to search furthest points in.</param>
/// <param name="linePoint1">First point forming the line.</param>
/// <param name="linePoint2">Second point forming the line.</param>
/// <param name="furthestPoint1">First found furthest point.</param>
/// <param name="furthestPoint2">Second found furthest point (which is on the
/// opposite side from the line compared to the <paramref name="furthestPoint1"/>);</param>
///
/// <remarks><para>The method finds two furthest points from the specified line,
/// where one point is on one side from the line and the second point is on
/// another side from the line.</para></remarks>
///
public static void GetFurthestPointsFromLine( IEnumerable<IntPoint> cloud, IntPoint linePoint1, IntPoint linePoint2,
out IntPoint furthestPoint1, out IntPoint furthestPoint2 )
{
float d1, d2;
GetFurthestPointsFromLine( cloud, linePoint1, linePoint2,
out furthestPoint1, out d1, out furthestPoint2, out d2 );
}
/// <summary>
/// Find two furthest points from the specified line.
/// </summary>
///
/// <param name="cloud">Collection of points to search furthest points in.</param>
/// <param name="linePoint1">First point forming the line.</param>
/// <param name="linePoint2">Second point forming the line.</param>
/// <param name="furthestPoint1">First found furthest point.</param>
/// <param name="distance1">Distance between the first found point and the given line.</param>
/// <param name="furthestPoint2">Second found furthest point (which is on the
/// opposite side from the line compared to the <paramref name="furthestPoint1"/>);</param>
/// <param name="distance2">Distance between the second found point and the given line.</param>
///
/// <remarks><para>The method finds two furthest points from the specified line,
/// where one point is on one side from the line and the second point is on
/// another side from the line.</para></remarks>
///
public static void GetFurthestPointsFromLine( IEnumerable<IntPoint> cloud, IntPoint linePoint1, IntPoint linePoint2,
out IntPoint furthestPoint1, out float distance1, out IntPoint furthestPoint2, out float distance2 )
{
furthestPoint1 = linePoint1;
distance1 = 0;
furthestPoint2 = linePoint2;
distance2 = 0;
if ( linePoint2.X != linePoint1.X )
{
// line's equation y(x) = k * x + b
float k = (float) ( linePoint2.Y - linePoint1.Y ) / ( linePoint2.X - linePoint1.X );
float b = linePoint1.Y - k * linePoint1.X;
float div = (float) Math.Sqrt( k * k + 1 );
float distance = 0;
foreach ( IntPoint point in cloud )
{
distance = ( k * point.X + b - point.Y ) / div;
if ( distance > distance1 )
{
distance1 = distance;
furthestPoint1 = point;
}
if ( distance < distance2 )
{
distance2 = distance;
furthestPoint2 = point;
}
}
}
else
{
int lineX = linePoint1.X;
float distance = 0;
foreach ( IntPoint point in cloud )
{
distance = lineX - point.X;
if ( distance > distance1 )
{
distance1 = distance;
furthestPoint1 = point;
}
if ( distance < distance2 )
{
distance2 = distance;
furthestPoint2 = point;
}
}
}
distance2 = -distance2;
}
/// <summary>
/// Find the furthest point from the specified line.
/// </summary>
///
/// <param name="cloud">Collection of points to search furthest point in.</param>
/// <param name="linePoint1">First point forming the line.</param>
/// <param name="linePoint2">Second point forming the line.</param>
///
/// <returns>Returns a point, which is the furthest away from the
/// specified line.</returns>
///
/// <remarks><para>The method finds the furthest point from the specified line.
/// Unlike the <see cref="GetFurthestPointsFromLine( IEnumerable{IntPoint}, IntPoint, IntPoint, out IntPoint, out IntPoint )"/>
/// method, this method find only one point, which is the furthest away from the line
/// regardless of side from the line.</para></remarks>
///
public static IntPoint GetFurthestPointFromLine( IEnumerable<IntPoint> cloud, IntPoint linePoint1, IntPoint linePoint2 )
{
float d;
return GetFurthestPointFromLine( cloud, linePoint1, linePoint2, out d );
}
/// <summary>
/// Find the furthest point from the specified line.
/// </summary>
///
/// <param name="cloud">Collection of points to search furthest points in.</param>
/// <param name="linePoint1">First point forming the line.</param>
/// <param name="linePoint2">Second point forming the line.</param>
/// <param name="distance">Distance between the furthest found point and the given line.</param>
///
/// <returns>Returns a point, which is the furthest away from the
/// specified line.</returns>
///
/// <remarks><para>The method finds the furthest point from the specified line.
/// Unlike the <see cref="GetFurthestPointsFromLine( IEnumerable{IntPoint}, IntPoint, IntPoint, out IntPoint, out float, out IntPoint, out float )"/>
/// method, this method find only one point, which is the furthest away from the line
/// regardless of side from the line.</para></remarks>
///
public static IntPoint GetFurthestPointFromLine( IEnumerable<IntPoint> cloud, IntPoint linePoint1, IntPoint linePoint2, out float distance )
{
IntPoint furthestPoint = linePoint1;
distance = 0;
if ( linePoint2.X != linePoint1.X )
{
// line's equation y(x) = k * x + b
float k = (float) ( linePoint2.Y - linePoint1.Y ) / ( linePoint2.X - linePoint1.X );
float b = linePoint1.Y - k * linePoint1.X;
float div = (float) Math.Sqrt( k * k + 1 );
float pointDistance = 0;
foreach ( IntPoint point in cloud )
{
pointDistance = Math.Abs( ( k * point.X + b - point.Y ) / div );
if ( pointDistance > distance )
{
distance = pointDistance;
furthestPoint = point;
}
}
}
else
{
int lineX = linePoint1.X;
float pointDistance = 0;
foreach ( IntPoint point in cloud )
{
distance = Math.Abs( lineX - point.X );
if ( pointDistance > distance )
{
distance = pointDistance;
furthestPoint = point;
}
}
}
return furthestPoint;
}
/// <summary>
/// Relative distortion limit allowed for quadrilaterals, [0.0, 0.25].
/// </summary>
///
/// <remarks><para>The value of this property is used to calculate distortion limit used by
/// <see cref="FindQuadrilateralCorners"/>, when processing potential corners and making decision
/// if the provided points form a quadrilateral or a triangle. The distortion limit is
/// calculated as:
/// <code lang="none">
/// distrtionLimit = RelativeDistortionLimit * ( W * H ) / 2,
/// </code>
/// where <b>W</b> and <b>H</b> are width and height of the "points cloud" passed to the
/// <see cref="FindQuadrilateralCorners"/>.
/// </para>
///
/// <para>To explain the idea behind distortion limit, let’s suppose that quadrilateral finder routine found
/// the next candidates for corners:<br />
/// <img src="img/math/potential_corners.png" width="151" height="128" /><br />
/// As we can see on the above picture, the shape there potentially can be a triangle, but not quadrilateral
/// (suppose that points list comes from a hand drawn picture or acquired from camera, so some
/// inaccuracy may exist). It may happen that the <b>D</b> point is just a distortion (noise, etc).
/// So the <see cref="FindQuadrilateralCorners"/> check what is the distance between a potential corner
/// (D in this case) and a line connecting two adjacent points (AB in this case). If the distance is smaller
/// then the distortion limit, then the point may be rejected, so the shape turns into triangle.
/// </para>
///
/// <para>An exception is the case when both <b>C</b> and <b>D</b> points are very close to the <b>AB</b> line,
/// so both their distances are less than distortion limit. In this case both points will be accepted as corners -
/// the shape is just a flat quadrilateral.</para>
///
/// <para>Default value is set to <b>0.1</b>.</para>
/// </remarks>
///
public static float QuadrilateralRelativeDistortionLimit
{
get { return quadrilateralRelativeDistortionLimit; }
set { quadrilateralRelativeDistortionLimit = Math.Max( 0.0f, Math.Min( 0.25f, value ) ); }
}
private static float quadrilateralRelativeDistortionLimit = 0.1f;
/// <summary>
/// Find corners of quadrilateral or triangular area, which contains the specified collection of points.
/// </summary>
///
/// <param name="cloud">Collection of points to search quadrilateral for.</param>
///
/// <returns>Returns a list of 3 or 4 points, which are corners of the quadrilateral or
/// triangular area filled by specified collection of point. The first point in the list
/// is the point with lowest X coordinate (and with lowest Y if there are several points
/// with the same X value). The corners are provided in counter clockwise order
/// (<a href="http://en.wikipedia.org/wiki/Cartesian_coordinate_system">Cartesian
/// coordinate system</a>).</returns>
///
/// <remarks><para>The method makes an assumption that the specified collection of points
/// form some sort of quadrilateral/triangular area. With this assumption it tries to find corners
/// of the area.</para>
///
/// <para><note>The method does not search for <b>bounding</b> quadrilateral/triangular area,
/// where all specified points are <b>inside</b> of the found quadrilateral/triangle. Some of the
/// specified points potentially may be outside of the found quadrilateral/triangle, since the
/// method takes corners only from the specified collection of points, but does not calculate such
/// to form true bounding quadrilateral/triangle.</note></para>
///
/// <para>See <see cref="QuadrilateralRelativeDistortionLimit"/> property for additional information.</para>
/// </remarks>
///
public static List<IntPoint> FindQuadrilateralCorners( IEnumerable<IntPoint> cloud )
{
// quadrilateral's corners
List<IntPoint> corners = new List<IntPoint>( );
// get bounding rectangle of the points list
IntPoint minXY, maxXY;
PointsCloud.GetBoundingRectangle( cloud, out minXY, out maxXY );
// get cloud's size
IntPoint cloudSize = maxXY - minXY;
// calculate center point
IntPoint center = minXY + cloudSize / 2;
// acceptable deviation limit
float distortionLimit = quadrilateralRelativeDistortionLimit * ( cloudSize.X + cloudSize.Y ) / 2;
// get the furthest point from (0,0)
IntPoint point1 = PointsCloud.GetFurthestPoint( cloud, center );
// get the furthest point from the first point
IntPoint point2 = PointsCloud.GetFurthestPoint( cloud, point1 );
corners.Add( point1 );
corners.Add( point2 );
// get two furthest points from line
IntPoint point3, point4;
float distance3, distance4;
PointsCloud.GetFurthestPointsFromLine( cloud, point1, point2,
out point3, out distance3, out point4, out distance4 );
// ideally points 1 and 2 form a diagonal of the
// quadrilateral area, and points 3 and 4 form another diagonal
// but if one of the points (3 or 4) is very close to the line
// connecting points 1 and 2, then it is one the same line ...
// which means corner was not found.
// in this case we deal with a trapezoid or triangle, where
// (1-2) line is one of it sides.
// another interesting case is when both points (3) and (4) are
// very close the (1-2) line. in this case we may have just a flat
// quadrilateral.
if (
( ( distance3 >= distortionLimit ) && ( distance4 >= distortionLimit ) ) ||
( ( distance3 < distortionLimit ) && ( distance3 != 0 ) &&
( distance4 < distortionLimit ) && ( distance4 != 0 ) ) )
{
// don't add one of the corners, if the point is already in the corners list
// (this may happen when both #3 and #4 points are very close to the line
// connecting #1 and #2)
if ( !corners.Contains( point3 ) )
{
corners.Add( point3 );
}
if ( !corners.Contains( point4 ) )
{
corners.Add( point4 );
}
}
else
{
// it seems that we deal with kind of trapezoid,
// where point 1 and 2 are on the same edge
IntPoint tempPoint = ( distance3 > distance4 ) ? point3 : point4;
// try to find 3rd point
PointsCloud.GetFurthestPointsFromLine( cloud, point1, tempPoint,
out point3, out distance3, out point4, out distance4 );
bool thirdPointIsFound = false;
if ( ( distance3 >= distortionLimit ) && ( distance4 >= distortionLimit ) )
{
if ( point4.DistanceTo( point2 ) > point3.DistanceTo( point2 ) )
point3 = point4;
thirdPointIsFound = true;
}
else
{
PointsCloud.GetFurthestPointsFromLine( cloud, point2, tempPoint,
out point3, out distance3, out point4, out distance4 );
if ( ( distance3 >= distortionLimit ) && ( distance4 >= distortionLimit ) )
{
if ( point4.DistanceTo( point1 ) > point3.DistanceTo( point1 ) )
point3 = point4;
thirdPointIsFound = true;
}
}
if ( !thirdPointIsFound )
{
// failed to find 3rd edge point, which is away enough from the temp point.
// this means that the clound looks more like triangle
corners.Add( tempPoint );
}
else
{
corners.Add( point3 );
// try to find 4th point
float tempDistance;
PointsCloud.GetFurthestPointsFromLine( cloud, point1, point3,
out tempPoint, out tempDistance, out point4, out distance4 );
if ( ( distance4 >= distortionLimit ) && ( tempDistance >= distortionLimit ) )
{
if ( tempPoint.DistanceTo( point2 ) > point4.DistanceTo( point2 ) )
point4 = tempPoint;
}
else
{
PointsCloud.GetFurthestPointsFromLine( cloud, point2, point3,
out tempPoint, out tempDistance, out point4, out distance4 );
if ( ( tempPoint.DistanceTo( point1 ) > point4.DistanceTo( point1 ) ) &&
( tempPoint != point2 ) && ( tempPoint != point3 ) )
{
point4 = tempPoint;
}
}
if ( ( point4 != point1 ) && ( point4 != point2 ) && ( point4 != point3 ) )
corners.Add( point4 );
}
}
// put the point with lowest X as the first
for ( int i = 1, n = corners.Count; i < n; i++ )
{
if ( ( corners[i].X < corners[0].X ) ||
( ( corners[i].X == corners[0].X ) && ( corners[i].Y < corners[0].Y ) ) )
{
IntPoint temp = corners[i];
corners[i] = corners[0];
corners[0] = temp;
}
}
// sort other points in counter clockwise order
float k1 = ( corners[1].X != corners[0].X ) ?
( (float) ( corners[1].Y - corners[0].Y ) / ( corners[1].X - corners[0].X ) ) :
( ( corners[1].Y > corners[0].Y ) ? float.PositiveInfinity : float.NegativeInfinity );
float k2 = ( corners[2].X != corners[0].X ) ?
( (float) ( corners[2].Y - corners[0].Y ) / ( corners[2].X - corners[0].X ) ) :
( ( corners[2].Y > corners[0].Y ) ? float.PositiveInfinity : float.NegativeInfinity );
if ( k2 < k1 )
{
IntPoint temp = corners[1];
corners[1] = corners[2];
corners[2] = temp;
float tk = k1;
k1 = k2;
k2 = tk;
}
if ( corners.Count == 4 )
{
float k3 = ( corners[3].X != corners[0].X ) ?
( (float) ( corners[3].Y - corners[0].Y ) / ( corners[3].X - corners[0].X ) ) :
( ( corners[3].Y > corners[0].Y ) ? float.PositiveInfinity : float.NegativeInfinity );
if ( k3 < k1 )
{
IntPoint temp = corners[1];
corners[1] = corners[3];
corners[3] = temp;
float tk = k1;
k1 = k3;
k3 = tk;
}
if ( k3 < k2 )
{
IntPoint temp = corners[2];
corners[2] = corners[3];
corners[3] = temp;
float tk = k2;
k2 = k3;
k3 = tk;
}
}
return corners;
}
}
}