/
distance.py
925 lines (723 loc) · 23.5 KB
/
distance.py
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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
from __future__ import print_function
from __future__ import absolute_import
from __future__ import division
from math import fabs
from math import sqrt
from compas.utilities import pairwise
from compas.geometry._core import add_vectors
from compas.geometry._core import add_vectors_xy
from compas.geometry._core import subtract_vectors
from compas.geometry._core import subtract_vectors_xy
from compas.geometry._core import scale_vector
from compas.geometry._core import normalize_vector
from compas.geometry._core import length_vector
from compas.geometry._core import length_vector_xy
from compas.geometry._core import length_vector_sqrd
from compas.geometry._core import length_vector_sqrd_xy
from compas.geometry._core import cross_vectors
from compas.geometry._core import cross_vectors_xy
from compas.geometry._core import dot_vectors
from compas.geometry._core import vector_component
from compas.geometry._core import vector_component_xy
__all__ = [
'distance_point_point',
'distance_point_point_xy',
'distance_point_point_sqrd',
'distance_point_point_sqrd_xy',
'distance_point_line',
'distance_point_line_xy',
'distance_point_line_sqrd',
'distance_point_line_sqrd_xy',
'distance_point_plane',
'distance_point_plane_signed',
'distance_line_line',
'closest_point_in_cloud',
'closest_point_in_cloud_xy',
'closest_point_on_line',
'closest_point_on_line_xy',
'closest_point_on_segment',
'closest_point_on_segment_xy',
'closest_point_on_polyline',
'closest_point_on_polyline_xy',
'closest_point_on_plane',
'closest_line_to_point',
]
def distance_point_point(a, b):
"""Compute the distance bewteen a and b.
Parameters
----------
a : sequence of float
XYZ coordinates of point a.
b : sequence of float
XYZ coordinates of point b.
Returns
-------
float
Distance bewteen a and b.
Examples
--------
>>> distance_point_point([0.0, 0.0, 0.0], [2.0, 0.0, 0.0])
2.0
See Also
--------
distance_point_point_xy
"""
ab = subtract_vectors(b, a)
return length_vector(ab)
def distance_point_point_xy(a, b):
"""Compute the distance between points a and b, assuming they lie in the XY plane.
Parameters
----------
a : sequence of float
XY(Z) coordinates of a 2D or 3D point (Z will be ignored).
b : sequence of float
XY(Z) coordinates of a 2D or 3D point (Z will be ignored).
Returns
-------
float
Distance between a and b in the XY-plane.
Examples
--------
>>> distance_point_point_xy([0.0, 0.0], [2.0, 0.0])
2.0
>>> distance_point_point_xy([0.0, 0.0, 0.0], [2.0, 0.0, 0.0])
2.0
>>> distance_point_point_xy([0.0, 0.0, 1.0], [2.0, 0.0, 1.0])
2.0
"""
ab = subtract_vectors_xy(b, a)
return length_vector_xy(ab)
def distance_point_point_sqrd(a, b):
"""Compute the squared distance bewteen points a and b.
Parameters
----------
a : sequence of float
XYZ coordinates of point a.
b : sequence of float
XYZ coordinates of point b.
Returns
-------
d2 : float
Squared distance bewteen a and b.
Examples
--------
>>> distance_point_point_sqrd([0.0, 0.0, 0.0], [2.0, 0.0, 0.0])
4.0
See Also
--------
distance_point_point_sqrd_xy
"""
ab = subtract_vectors(b, a)
return length_vector_sqrd(ab)
def distance_point_point_sqrd_xy(a, b):
"""Compute the squared distance between points a and b lying in the XY plane.
Parameters
----------
a : sequence of float
XY(Z) coordinates of the first point.
b : sequence of float)
XY(Z) coordinates of the second point.
Returns
-------
float
Squared distance between a and b in the XY-plane.
Examples
--------
>>> distance([0.0, 0.0], [2.0, 0.0])
4.0
>>> distance([0.0, 0.0, 0.0], [2.0, 0.0, 0.0])
4.0
>>> distance([0.0, 0.0, 1.0], [2.0, 0.0, 1.0])
4.0
"""
ab = subtract_vectors_xy(b, a)
return length_vector_sqrd_xy(ab)
def distance_point_line(point, line):
"""Compute the distance between a point and a line.
Parameters
----------
point : list, tuple
Point location.
line : list, tuple
Line defined by two points.
Returns
-------
float
The distance between the point and the line.
Notes
-----
This implementation computes the *right angle distance* from a point P to a
line defined by points A and B as twice the area of the triangle ABP divided
by the length of AB [1]_.
References
----------
.. [1] Wikipedia. *Distance from a point to a line*.
Available at: https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line
Examples
--------
>>>
"""
a, b = line
ab = subtract_vectors(b, a)
pa = subtract_vectors(a, point)
pb = subtract_vectors(b, point)
length = length_vector(cross_vectors(pa, pb))
length_ab = length_vector(ab)
return length / length_ab
def distance_point_line_xy(point, line):
"""Compute the distance between a point and a line, assuming they lie in the XY-plane.
Parameters
----------
point : sequence of float
XY(Z) coordinates of the point.
line : list, tuple
Line defined by two points.
Returns
-------
float
The distance between the point and the line.
Notes
-----
This implementation computes the orthogonal distance from a point P to a
line defined by points A and B as twice the area of the triangle ABP divided
by the length of AB [1]_.
References
----------
.. [1] Wikipedia. *Distance from a point to a line*.
Available at: https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line.
"""
a, b = line
ab = subtract_vectors_xy(b, a)
pa = subtract_vectors_xy(a, point)
pb = subtract_vectors_xy(b, point)
length = fabs(cross_vectors_xy(pa, pb)[2])
length_ab = length_vector_xy(ab)
return length / length_ab
def distance_point_line_sqrd(point, line):
"""Compute the squared distance between a point and a line.
Parameters
----------
point : sequence of float
XYZ coordinates of the point.
line : list, tuple
Line defined by two points.
Returns
-------
float
The squared distance between the point and the line.
Notes
-----
For more info, see [1]_.
References
----------
.. [1] Wikipedia. *Distance from a point to a line*.
Available at: https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line.
"""
a, b = line
ab = subtract_vectors(b, a)
pa = subtract_vectors(a, point)
pb = subtract_vectors(b, point)
length = length_vector_sqrd(cross_vectors(pa, pb))
length_ab = length_vector_sqrd(ab)
return length / length_ab
def distance_point_line_sqrd_xy(point, line):
"""Compute the squared distance between a point and a line lying in the XY-plane.
Parameters
----------
point : sequence of float
XY(Z) coordinates of a 2D or 3D point (Z will be ignored).
line : list, tuple
Line defined by two points.
Returns
-------
float
The squared distance between the point and the line.
Notes
-----
This implementation computes the orthogonal squared distance from a point P to a
line defined by points A and B as twice the area of the triangle ABP divided
by the length of AB [1]_.
References
----------
.. [1] Wikipedia. *Distance from a point to a line*.
Available at: https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line.
"""
a, b = line
ab = subtract_vectors_xy(b, a)
pa = subtract_vectors_xy(a, point)
pb = subtract_vectors_xy(b, point)
length = cross_vectors_xy(pa, pb)[2]**2
length_ab = length_vector_sqrd_xy(ab)
return length / length_ab
def distance_point_plane(point, plane):
r"""Compute the distance from a point to a plane defined by origin point and normal.
Parameters
----------
point : list
Point coordinates.
plane : tuple
A point and a vector defining a plane.
Returns
-------
float
Distance between point and plane.
Notes
-----
The distance from a point to a plane can be computed from the coefficients
of the equation of the plane and the coordinates of the point [1]_.
The equation of a plane is
.. math::
Ax + By + Cz + D = 0
where
.. math::
:nowrap:
\begin{align}
D &= - Ax_0 - Bx_0 - Cz_0 \\
Q &= (x_0, y_0, z_0) \\
N &= (A, B, C)
\end{align}
with :math:`Q` a point on the plane, and :math:`N` the normal vector at
that point. The distance of any point :math:`P` to a plane is the
absolute value of the dot product of the vector from :math:`Q` to :math:`P`
and the normal at :math:`Q`.
References
----------
.. [1] Nykamp, D. *Distance from point to plane*.
Available at: http://mathinsight.org/distance_point_plane.
Examples
--------
>>>
"""
return fabs(distance_point_plane_signed(point, plane))
def distance_point_plane_signed(point, plane):
r"""Compute the signed distance from a point to a plane defined by origin point and normal.
Parameters
----------
point : list
Point coordinates.
plane : tuple
A point and a vector defining a plane.
Returns
-------
float
Distance between point and plane.
Notes
-----
The distance from a point to a plane can be computed from the coefficients
of the equation of the plane and the coordinates of the point [1]_.
The equation of a plane is
.. math::
Ax + By + Cz + D = 0
where
.. math::
:nowrap:
\begin{align}
D &= - Ax_0 - Bx_0 - Cz_0 \\
Q &= (x_0, y_0, z_0) \\
N &= (A, B, C)
\end{align}
with :math:`Q` a point on the plane, and :math:`N` the normal vector at
that point. The distance of any point :math:`P` to a plane is the
value of the dot product of the vector from :math:`Q` to :math:`P`
and the normal at :math:`Q`.
References
----------
.. [1] Nykamp, D. *Distance from point to plane*.
Available at: http://mathinsight.org/distance_point_plane.
Examples
--------
>>>
"""
base, normal = plane
vector = subtract_vectors(point, base)
return dot_vectors(vector, normal)
def distance_line_line(l1, l2, tol=0.0):
r"""Compute the shortest distance between two lines.
Parameters
----------
l1 : tuple
Two points defining a line.
l2 : tuple
Two points defining a line.
Returns
-------
float
The distance between the two lines.
Notes
-----
The distance is the absolute value of the dot product of a unit vector that
is perpendicular to the two lines, and the vector between two points on the lines ([1]_, [2]_).
If each of the lines is defined by two points (:math:`l_1 = (\mathbf{x_1}, \mathbf{x_2})`,
:math:`l_2 = (\mathbf{x_3}, \mathbf{x_4})`), then the unit vector that is
perpendicular to both lines is...
References
----------
.. [1] Weisstein, E.W. *Line-line Distance*.
Available at: http://mathworld.wolfram.com/Line-LineDistance.html.
.. [2] Wikipedia. *Skew lines Distance*.
Available at: https://en.wikipedia.org/wiki/Skew_lines#Distance.
Examples
--------
>>>
"""
a, b = l1
c, d = l2
ab = subtract_vectors(b, a)
cd = subtract_vectors(d, c)
ac = subtract_vectors(c, a)
n = cross_vectors(ab, cd)
length = length_vector(n)
if length <= tol:
return distance_point_point(closest_point_on_line(l1[0], l2), l1[0])
n = scale_vector(n, 1.0 / length)
return fabs(dot_vectors(n, ac))
# ==============================================================================
# closest
# ==============================================================================
def sort_points(point, cloud):
"""Sorts points of a pointcloud based on their distance from a given point.
Parameters
----------
point : tuple
The XYZ coordinates of the base point.
cloud : sequence
A sequence locations in three-dimensional space.
Returns
-------
list
A list containing the points of the cloud sorted by their squared distance to the base points.
Each item in the list contains the squared distance to the base point, the XYZ coordinates
of the point in the cloud, and the index of the point in the original cloud.
Notes
-----
Check kdTree class for an optimized implementation (MR).
Examples
--------
>>> sort_points()
"""
minsq = [distance_point_point_sqrd(p, point) for p in cloud]
return sorted(zip(minsq, cloud, range(len(cloud))), key=lambda x: x[0])
def sort_points_xy(point, cloud):
"""Sorts points of a pointcloud based on their distance from a given point,
assuming all points lie in the XY plane.
Parameters
----------
point : sequence of float
XY(Z) coordinates of a point.
cloud : sequence
A list of points represented by their XY(Z) coordinates.
Returns
-------
list
A list containing the points of the cloud sorted by their squared distance to the base points.
Each item in the list contains the squared distance to the base point, the XYZ coordinates
of the point in the cloud, and the index of the point in the original cloud.
Notes
-----
Check kdTree class for an optimized implementation (MR).
Examples
--------
>>>
"""
minsq = [distance_point_point_sqrd_xy(p, point) for p in cloud]
return sorted(zip(minsq, cloud, range(len(cloud))), key=lambda x: x[0])
def closest_point_in_cloud(point, cloud):
"""Calculates the closest point in a pointcloud.
Parameters
----------
point : tuple
XYZ coordinates of the base point.
cloud : sequence
A sequence locations in three-dimensional space.
Returns
-------
tuple
The distance to the closest point.
XYZ coordinates of the closest point.
The index of the closest point in the original list.
Notes
-----
Check kdTree class for an optimized implementation (MR).
Examples
--------
>>>
"""
data = sort_points(point, cloud)
d, xyz, index = data[0]
return sqrt(d), xyz, index
def closest_points_in_cloud_numpy(points, cloud, threshold=10**7, distances=True, num_nbrs=1):
"""Find the closest points in a point cloud to a set of sample points.
Parameters
----------
points : array, list
The sample points (n,).
cloud : array, list
The cloud points to compare to (n,).
threshold : float
Points are checked within this distance.
distances : bool
Return distance matrix.
Returns
-------
list
Indices of the closest points in the cloud per point in points.
array
Distances between points and closest points in cloud (n x n).
Notes
-----
Items in cloud further from items in points than threshold return zero
distance and will affect the indices returned if not set suitably high.
Examples
--------
>>> a = np.random.rand(4, 3)
>>> b = np.random.rand(4, 3)
>>> indices, distances = closest_points(a, b, distances=True)
[1, 2, 0, 3]
array([[ 1.03821946, 0.66226402, 0.67964346, 0.98877891],
[ 0.4650432 , 0.54484186, 0.36158995, 0.60385484],
[ 0.19562088, 0.73240154, 0.50235761, 0.51439644],
[ 0.84680233, 0.85390316, 0.72154983, 0.50432293]])
"""
from numpy import asarray
from numpy import argmin
from numpy import argpartition
from scipy.spatial import distance_matrix
points = asarray(points).reshape((-1, 3))
cloud = asarray(cloud).reshape((-1, 3))
d_matrix = distance_matrix(points, cloud, threshold=threshold)
if num_nbrs == 1:
indices = argmin(d_matrix, axis=1)
else:
indices = argpartition(d_matrix, num_nbrs, axis=1)
if distances:
return indices, d_matrix
return indices
def closest_point_in_cloud_xy(point, cloud):
"""Calculates the closest point in a list of points in the XY-plane.
Parameters
----------
point : sequence of float
XY(Z) coordinates of a the base point.
cloud : sequence
A list of points forming the cloud, with each point represented by its XY(Z) coordinates.
Returns
-------
tuple
The distance to the closest point.
The XYZ coordinates of the closest point (with Z = 0).
The index of the closest point in the cloud.
Notes
-----
Check kdTree class for an optimized implementation (MR).
"""
data = sort_points_xy(point, cloud)
d, xyz, index = data[0]
return sqrt(d), xyz, index
def closest_point_on_line(point, line):
"""Computes closest point on line to a given point.
Parameters
----------
point : sequence of float
XYZ coordinates.
line : tuple
Two points defining the line.
Returns
-------
list
XYZ coordinates of closest point.
Examples
--------
>>>
See Also
--------
:func:`basic.transformations.project_point_line`
"""
a, b = line
ab = subtract_vectors(b, a)
ap = subtract_vectors(point, a)
c = vector_component(ap, ab)
return add_vectors(a, c)
def closest_point_on_line_xy(point, line):
"""Compute closest point on line (continuous) to a given point lying in the XY-plane.
Parameters
----------
point : sequence of float
XY(Z) coordinates of a point.
line : tuple
Two XY(Z) points defining a line.
Returns
-------
list
XYZ coordinates of closest point (Z = 0.0).
"""
a, b = line
ab = subtract_vectors_xy(b, a)
ap = subtract_vectors_xy(point, a)
c = vector_component_xy(ap, ab)
return add_vectors_xy(a, c)
def closest_point_on_segment(point, segment):
"""Computes closest point on line segment (p1, p2) to test point.
Parameters
----------
point : sequence of float
XYZ coordinates.
saegment : tuple
Two points defining the segment.
Returns
-------
list
XYZ coordinates of closest point.
Examples
--------
>>>
"""
a, b = segment
p = closest_point_on_line(point, segment)
d = distance_point_point_sqrd(a, b)
d1 = distance_point_point_sqrd(a, p)
d2 = distance_point_point_sqrd(b, p)
if d1 > d or d2 > d:
if d1 < d2:
return a
return b
return p
def closest_point_on_segment_xy(point, segment):
"""Compute closest point on a line segment to a given point lying in the XY-plane.
Parameters
----------
point : sequence of float
XY(Z) coordinates of a point.
segment : tuple
Two 2D or 3D points defining the line segment (Z components will be ignored).
Returns
-------
list
XYZ coordinates of closest point (Z = 0.0).
"""
a, b = segment
p = closest_point_on_line_xy(point, segment)
d = distance_point_point_sqrd_xy(a, b)
d1 = distance_point_point_sqrd_xy(a, p)
d2 = distance_point_point_sqrd_xy(b, p)
if d1 > d or d2 > d:
if d1 < d2:
return a
return b
return p
def closest_point_on_polyline(point, polyline):
"""Find the closest point on a polyline to a given point.
Parameters
----------
point : list
XYZ coordinates of a 2D or 3D point (Z will be ignored).
polygon : sequence
A sequence of XYZ coordinates representing the locations of the corners of a polygon.
The vertices are assumed to be in order. The polygon is assumed to be closed:
the first and last vertex in the sequence should not be the same.
Returns
-------
list
XYZ coordinates of closest point.
"""
cloud = []
for segment in pairwise(polyline):
cloud.append(closest_point_on_segment(point, segment))
return closest_point_in_cloud(point, cloud)[1]
def closest_point_on_polyline_xy(point, polyline):
"""Compute closest point on a polyline to a given point,
assuming they both lie in the XY-plane.
Parameters
----------
point : sequence of float
XY(Z) coordinates of a 2D or 3D point (Z will be ignored).
polygon : sequence
A sequence of XY(Z) coordinates of 2D or 3D points
(Z will be ignored) representing the locations of the corners of a polygon.
The vertices are assumed to be in order. The polygon is assumed to be closed:
the first and last vertex in the sequence should not be the same.
Returns
-------
list
XYZ coordinates of closest point (Z = 0.0).
"""
cloud = []
for segment in pairwise(polyline):
cloud.append(closest_point_on_segment_xy(point, segment))
return closest_point_in_cloud_xy(point, cloud)[1]
def closest_point_on_polygon_xy(point, polygon):
"""Compute closest point on a polygon to a given point lying in the XY-plane.
Parameters
----------
point : sequence of float
XY(Z) coordinates of a 2D or 3D point (Z will be ignored).
polygon : sequence
A sequence of XY(Z) coordinates of 2D or 3D points
(Z will be ignored) representing the locations of the corners of a polygon.
The vertices are assumed to be in order. The polygon is assumed to be closed:
the first and last vertex in the sequence should not be the same.
Returns
-------
list
XYZ coordinates of closest point (Z = 0.0).
"""
points = []
for i in range(len(polygon)):
segment = polygon[i - 1], polygon[i]
points.append(closest_point_on_segment_xy(point, segment))
return closest_point_in_cloud_xy(point, points)[1]
def closest_point_on_plane(point, plane):
"""Compute closest point on a plane to a given point.
Parameters
----------
point : sequenceof float
XYZ coordinates of point.
plane : tuple
The base point and normal defining the plane.
Returns
-------
list
XYZ coordinates of the closest point.
Notes
-----
For more info, see [1]_.
References
----------
.. [1] Wikipedia. *Distance from a point to a plane*.
Available at: https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_plane
Examples
--------
>>> plane = ([0.0, 0.0, 0.0], [0.0, 0.0, 1.0])
>>> point = [1.0, 2.0, 3.0]
>>> closest_point_on_plane(point, plane)
"""
base, normal = plane
x, y, z = base
a, b, c = normalize_vector(normal)
x1, y1, z1 = point
d = a * x + b * y + c * z
k = (a * x1 + b * y1 + c * z1 - d) / (a**2 + b**2 + c**2)
return [x1 - k * a,
y1 - k * b,
z1 - k * c]
def closest_line_to_point(point, lines):
"""Compute closest line to a point from a list of lines.
Parameters
----------
point : sequenceof float
XYZ coordinates of point.
lines : sequence of lines (tuples of points).
The lines to be checked for distance.
Returns
-------
tuple
the closest line.
"""
cloud = []
for segment in lines:
cloud.append(closest_point_on_segment(point, segment))
return lines[closest_point_in_cloud(point, cloud)[2]]
# ==============================================================================
# Main
# ==============================================================================
if __name__ == "__main__":
pass