1+ import numpy as np
2+
3+ import PointPointDistance as PP
4+ import PointLineDistance as PL
5+
6+ def val (p , e0 , e1 ):
7+ e = e1 - e0
8+ ratio = np .dot (e , p - e0 ) / np .dot (e , e )
9+ if ratio < 0 : # point(p)-point(e0) expression
10+ return PP .val (p , e0 )
11+ elif ratio > 1 : # point(p)-point(e1) expression
12+ return PP .val (p , e1 )
13+ else : # point(p)-line(e0e1) expression
14+ return PL .val (p , e0 , e1 )
15+
16+ def grad (p , e0 , e1 ):
17+ e = e1 - e0
18+ ratio = np .dot (e , p - e0 ) / np .dot (e , e )
19+ if ratio < 0 : # point(p)-point(e0) expression
20+ g_PP = PP .grad (p , e0 )
21+ return np .reshape ([g_PP [0 :2 ], g_PP [2 :4 ], np .array ([0.0 , 0.0 ])], (1 , 6 ))[0 ]
22+ elif ratio > 1 : # point(p)-point(e1) expression
23+ g_PP = PP .grad (p , e1 )
24+ return np .reshape ([g_PP [0 :2 ], np .array ([0.0 , 0.0 ]), g_PP [2 :4 ]], (1 , 6 ))[0 ]
25+ else : # point(p)-line(e0e1) expression
26+ return PL .grad (p , e0 , e1 )
27+
28+ def hess (p , e0 , e1 ):
29+ e = e1 - e0
30+ ratio = np .dot (e , p - e0 ) / np .dot (e , e )
31+ if ratio < 0 : # point(p)-point(e0) expression
32+ H_PP = PP .hess (p , e0 )
33+ return np .array ([np .reshape ([H_PP [0 , 0 :2 ], H_PP [0 , 2 :4 ], np .array ([0.0 , 0.0 ])], (1 , 6 ))[0 ], \
34+ np .reshape ([H_PP [1 , 0 :2 ], H_PP [1 , 2 :4 ], np .array ([0.0 , 0.0 ])], (1 , 6 ))[0 ], \
35+ np .reshape ([H_PP [2 , 0 :2 ], H_PP [2 , 2 :4 ], np .array ([0.0 , 0.0 ])], (1 , 6 ))[0 ], \
36+ np .reshape ([H_PP [3 , 0 :2 ], H_PP [3 , 2 :4 ], np .array ([0.0 , 0.0 ])], (1 , 6 ))[0 ], \
37+ np .array ([0.0 ] * 6 ), \
38+ np .array ([0.0 ] * 6 )])
39+ elif ratio > 1 : # point(p)-point(e1) expression
40+ H_PP = PP .hess (p , e1 )
41+ return np .array ([np .reshape ([H_PP [0 , 0 :2 ], np .array ([0.0 , 0.0 ]), H_PP [0 , 2 :4 ]], (1 , 6 ))[0 ], \
42+ np .reshape ([H_PP [1 , 0 :2 ], np .array ([0.0 , 0.0 ]), H_PP [1 , 2 :4 ]], (1 , 6 ))[0 ], \
43+ np .array ([0.0 ] * 6 ), \
44+ np .array ([0.0 ] * 6 ), \
45+ np .reshape ([H_PP [2 , 0 :2 ], np .array ([0.0 , 0.0 ]), H_PP [2 , 2 :4 ]], (1 , 6 ))[0 ], \
46+ np .reshape ([H_PP [3 , 0 :2 ], np .array ([0.0 , 0.0 ]), H_PP [3 , 2 :4 ]], (1 , 6 ))[0 ]])
47+ else : # point(p)-line(e0e1) expression
48+ return PL .hess (p , e0 , e1 )
0 commit comments