-
Notifications
You must be signed in to change notification settings - Fork 0
/
jeometry-intersect.js
138 lines (101 loc) · 3.87 KB
/
jeometry-intersect.js
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
(function( jeometry, undefined ) {
var get = function(primitive1, primitive2) {
try{
return _functions.get(primitive1.type, primitive2.type)(primitive1, primitive2);
} catch(e) {
return undefined;
}
};
var segments = function(segment1, segment2) {
/*
method using cross products
segments
p + tr, with t element of [0, 1]
q + us, with s element of [0, 1]
intersection
p + tr = q + us
(p + tr) x s = (q + us) x s
(p x s) + (tr x s) = q x s // s x s = 0
t (r x s) = q x s - p x s
t (r x s) = (q - p) x s
t = (q - p) x s / (r x s)
thus
if r x s = 0 => parallel segments
*/
// --- fast bounding box checks
// TODO could be used for intersext of two AABB's
// check x-axis
var d1 = segment1.p2.x - segment1.p1.x;
var d2 = segment2.p2.x - segment2.p1.x;
var upper1 = (d1 < 0) ? segment1.p1.x : segment1.p2.x;
var lower1 = (d1 < 0) ? segment1.p2.x : segment1.p1.x;
var upper2 = (d2 < 0) ? segment2.p1.x : segment2.p2.x;
var lower2 = (d2 < 0) ? segment2.p2.x : segment2.p1.x;
if(upper1 < lower2 || upper2 < lower1)
return undefined;
// check y-ayis
var d1 = segment1.p2.y - segment1.p1.y;
var d2 = segment2.p2.y - segment2.p1.y;
var upper1 = (d1 < 0) ? segment1.p1.y : segment1.p2.y;
var lower1 = (d1 < 0) ? segment1.p2.y : segment1.p1.y;
var upper2 = (d2 < 0) ? segment2.p1.y : segment2.p2.y;
var lower2 = (d2 < 0) ? segment2.p2.y : segment2.p1.y;
if(upper1 < lower2 || upper2 < lower1)
return undefined;
// --- cross product intersection
var p = segment1.p1;
var r = segment1.p2.subtract(segment1.p1);
var q = segment2.p1;
var s = segment2.p2.subtract(segment2.p1);
var r_x_s = r.cross_product(s);
// parallel
if(r_x_s == 0)
return undefined;
var q_p = q.subtract(p);
var q_p_x_s = q_p.cross_product(s);
if(q_p_x_s == 0)
return undefined;
var t = q_p_x_s / r_x_s;
if( t < 0 || t > 1)
return undefined;
var q_p_x_r = q_p.cross_product(r);
if(q_p_x_r == 0)
return undefined;
var u = q_p_x_r / r_x_s;
if( u < 0 || u > 1)
return undefined;
return p.add(r.multiply_scalar(t));
};
var triangle_with_segment = function(triangle, segment) {
/*
easy algorithm, based upon the intersection of segments
can return 0, 1 or 2 intersection points:
0 = undefined
1 = [point]
2 = [point1, point2]
*/
var intersections = new Array();
var i = segments(triangle.side1, segment);
if(i != undefined) intersections.push(i);
i = segments(triangle.side2, segment);
if(i != undefined) intersections.push(i);
i = segments(triangle.side3, segment);
if(i != undefined) intersections.push(i);
if(intersections.length == 0) {
return undefined;
}
return intersections;
};
var _functions = jeometry.utils.create_2d_lookup([
{key1: jeometry.primitives.SEGMENT, key2: jeometry.primitives.SEGMENT, value: segments },
{key1: jeometry.primitives.TRIANGLE, key2: jeometry.primitives.SEGMENT, value: triangle_with_segment },
// reversed parameters
{key1: jeometry.primitives.SEGMENT, key2: jeometry.primitives.TRIANGLE, value: function(i, j) {return triangle_with_segment(j, i);} }
]);
jeometry.intersection = {
_functions:_functions,
get:get,
segments:segments,
triangle_with_segment:triangle_with_segment
}
}( window.jeometry = window.jeometry || {} ));