@@ -86,6 +86,13 @@ HOST_DEVICE_INLINE int get_intersection_points(
86
86
vec2[i] = pts2[(i + 1 ) % 4 ] - pts2[i];
87
87
}
88
88
89
+ // When computing the intersection area, it doesn't hurt if we have
90
+ // more (duplicated/approximate) intersections/vertices than needed,
91
+ // while it can cause drastic difference if we miss an intersection/vertex.
92
+ // Therefore, we add an epsilon to relax the comparisons between
93
+ // the float point numbers that decide the intersection points.
94
+ double EPS = 1e-5 ;
95
+
89
96
// Line test - test all line combos for intersection
90
97
int num = 0 ; // number of intersections
91
98
for (int i = 0 ; i < 4 ; i++) {
@@ -103,7 +110,7 @@ HOST_DEVICE_INLINE int get_intersection_points(
103
110
T t1 = cross_2d<T>(vec2[j], vec12) / det;
104
111
T t2 = cross_2d<T>(vec1[i], vec12) / det;
105
112
106
- if (t1 >= 0 . 0f && t1 <= 1 .0f && t2 >= 0 . 0f && t2 <= 1 .0f ) {
113
+ if (t1 > -EPS && t1 < 1 .0f + EPS && t2 > -EPS && t2 < 1 .0f + EPS ) {
107
114
intersections[num++] = pts1[i] + vec1[i] * t1;
108
115
}
109
116
}
@@ -125,8 +132,8 @@ HOST_DEVICE_INLINE int get_intersection_points(
125
132
auto APdotAB = dot_2d<T>(AP, AB);
126
133
auto APdotAD = -dot_2d<T>(AP, DA);
127
134
128
- if ((APdotAB >= 0 ) && (APdotAD >= 0 ) && (APdotAB <= ABdotAB) &&
129
- (APdotAD <= ADdotAD)) {
135
+ if ((APdotAB > -EPS ) && (APdotAD > -EPS ) && (APdotAB < ABdotAB + EPS ) &&
136
+ (APdotAD < ADdotAD + EPS )) {
130
137
intersections[num++] = pts1[i];
131
138
}
132
139
}
@@ -144,8 +151,8 @@ HOST_DEVICE_INLINE int get_intersection_points(
144
151
auto APdotAB = dot_2d<T>(AP, AB);
145
152
auto APdotAD = -dot_2d<T>(AP, DA);
146
153
147
- if ((APdotAB >= 0 ) && (APdotAD >= 0 ) && (APdotAB <= ABdotAB) &&
148
- (APdotAD <= ADdotAD)) {
154
+ if ((APdotAB > -EPS ) && (APdotAD > -EPS ) && (APdotAB < ABdotAB + EPS ) &&
155
+ (APdotAD < ADdotAD + EPS )) {
149
156
intersections[num++] = pts2[i];
150
157
}
151
158
}
0 commit comments