Skip to content

edge_edge_distance

edge_edge_distance(P1, Q1, P2, Q2)

Computes the distance between two edges (segments) in 3D space.

Parameters:

Name Type Description Default
P1 (3,) numpy array

start point of first edge

required
Q1 (3,) numpy array

end point of first edge

required
P2 (3,) numpy array

start point of second edge

required
Q2 (3,) numpy array

end point of second edge

required

Returns:

Name Type Description
d float

The distance between the two edges.

R1 (3,) numpy array

The closest point on the first edge to the second edge.

R2 (3,) numpy array

The closest point on the second edge to the first edge.

Notes

This function is based on the algorithm from the "Proximity Query Pack" by Eric Larsen and Stefan Gottschalk

Examples:

import numpy as np
from gpytoolbox import edge_edge_distance
P1 = np.array([0.0,0.0,0.0])
P2 = np.array([1.0,0.0,0.0])
Q1 = np.array([0.0,1.0,0.0])
Q2 = np.array([1.0,1.0,0.0])
dist,R1,R2 = gpytoolbox.edge_edge_distance(P1,Q1,P2,Q2)
Source code in src/gpytoolbox/edge_edge_distance.py
  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
def edge_edge_distance(P1,Q1,P2,Q2):
    """
    Computes the distance between two edges (segments) in 3D space.

    Parameters
    ----------
    P1 : (3,) numpy array
        start point of first edge
    Q1 : (3,) numpy array
        end point of first edge
    P2 : (3,) numpy array
        start point of second edge
    Q2 : (3,) numpy array
        end point of second edge

    Returns
    -------
    d : float
        The distance between the two edges.
    R1 : (3,) numpy array
        The closest point on the first edge to the second edge.
    R2 : (3,) numpy array
        The closest point on the second edge to the first edge.

    Notes
    -----
    This function is based on the algorithm from the "Proximity Query Pack" by Eric Larsen and Stefan Gottschalk

    Examples
    --------
    ```python
    import numpy as np
    from gpytoolbox import edge_edge_distance
    P1 = np.array([0.0,0.0,0.0])
    P2 = np.array([1.0,0.0,0.0])
    Q1 = np.array([0.0,1.0,0.0])
    Q2 = np.array([1.0,1.0,0.0])
    dist,R1,R2 = gpytoolbox.edge_edge_distance(P1,Q1,P2,Q2)
    ```
    """


    P = P1
    Q = P2
    A = Q1 - P1
    B = Q2 - P2
        #     VmV(T,Q,P);
    # P -> P1
    # Q -> P2
    T = Q - P
    # A_dot_A = VdotV(A,A);
    # B_dot_B = VdotV(B,B);
    # A_dot_B = VdotV(A,B);
    # A_dot_T = VdotV(A,T);
    # B_dot_T = VdotV(B,T);
    A_dot_A = np.dot(A,A)
    B_dot_B = np.dot(B,B)
    A_dot_B = np.dot(A,B)
    A_dot_T = np.dot(A,T)
    B_dot_T = np.dot(B,T)


    # // t parameterizes ray P,A 
    # // u parameterizes ray Q,B 

    # PQP_REAL t,u;

    # // compute t for the closest point on ray P,A to
    # // ray Q,B

    # PQP_REAL denom = A_dot_A*B_dot_B - A_dot_B*A_dot_B;

    # t = (A_dot_T*B_dot_B - B_dot_T*A_dot_B) / denom;

    # // clamp result so t is on the segment P,A

    # if ((t < 0) || isnan(t)) t = 0; else if (t > 1) t = 1;

    # // find u for point on ray Q,B closest to point at t

    # u = (t*A_dot_B - B_dot_T) / B_dot_B;

    denom = A_dot_A*B_dot_B - A_dot_B*A_dot_B
    if denom == 0:
        t = 0
    else:
        t = (A_dot_T*B_dot_B - B_dot_T*A_dot_B) / denom
    # print("t: ",t)
    if((t < 0) or np.isnan(t)):
        t = 0
    elif(t > 1):
        t = 1
    # print("t: ",t)
    if B_dot_B == 0:
        u = 0
    else:
        u = (t*A_dot_B - B_dot_T) / B_dot_B
    # print("u: ",u)
#     if ((u <= 0) || isnan(u)) {

#     VcV(Y, Q);

#     t = A_dot_T / A_dot_A;

#     if ((t <= 0) || isnan(t)) {
#       VcV(X, P);
#       VmV(VEC, Q, P);
#     }
#     else if (t >= 1) {
#       VpV(X, P, A);
#       VmV(VEC, Q, X);
#     }
#     else {
#       VpVxS(X, P, A, t);
#       VcrossV(TMP, T, A);
#       VcrossV(VEC, A, TMP);
#     }
#   }
    if ((u<=0) or np.isnan(u)):
        Y = Q.copy()
        t = A_dot_T / A_dot_A
        if ((t<=0) or np.isnan(t)):
            X = P.copy()
        elif (t>=1):
            X = P + A
        else:
            X = P + t*A
#   else if (u >= 1) {

#     VpV(Y, Q, B);

#     t = (A_dot_B + A_dot_T) / A_dot_A;

#     if ((t <= 0) || isnan(t)) {
#       VcV(X, P);
#       VmV(VEC, Y, P);
#     }
#     else if (t >= 1) {
#       VpV(X, P, A);
#       VmV(VEC, Y, X);
#     }
#     else {
#       VpVxS(X, P, A, t);
#       VmV(T, Y, P);
#       VcrossV(TMP, T, A);
#       VcrossV(VEC, A, TMP);
#     }
#   }

    elif (u>=1):
        Y = Q + B
        t = (A_dot_T + A_dot_B) / A_dot_A
        if ((t<=0) or np.isnan(t)):
            X = P.copy()
        elif (t>=1):
            X = P + A
        else:
            X = P + t*A
#   else {

#     VpVxS(Y, Q, B, u);

#     if ((t <= 0) || isnan(t)) {
#       VcV(X, P);
#       VcrossV(TMP, T, B);
#       VcrossV(VEC, B, TMP);
#     }
#     else if (t >= 1) {
#       VpV(X, P, A);
#       VmV(T, Q, X);
#       VcrossV(TMP, T, B);
#       VcrossV(VEC, B, TMP);
#     }
#     else {
#       VpVxS(X, P, A, t);
#       VcrossV(VEC, A, B);
#       if (VdotV(VEC, T) < 0) {
#         VxS(VEC, VEC, -1);
#       }
#     }
#   }
    else:
        Y = Q + u*B
        if ((t<=0) or np.isnan(t)):
            X = P.copy()
        elif (t>=1):
            # print("here")
            X = P + A
        else:
            X = P + t*A

    R1 = X
    R2 = Y
    dist = np.linalg.norm(R1-R2)
    return dist, R1, R2