-
Notifications
You must be signed in to change notification settings - Fork 2
/
pointVersusManifoldXZ.vfl
122 lines (106 loc) · 4.09 KB
/
pointVersusManifoldXZ.vfl
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
//Line has 2 coodinate pairs. L1:x1x2y1y2 & L2:x3,x4,y3,y4
vector segsegXZ(float x1; float y1;float x2;float y2;float x3;float y3;float x4;float y4) {
float a1, a2, b1, b2, c1, c2; // Coefficients of line eqns.
float r1, r2, r3, r4; // 'Sign' values
float denom, offset; // Intermediate values
float x, y; // Intermediate return values
// Compute a1, b1, c1, where line joining points 1 and 2
// is "a1 x + b1 y + c1 = 0".
a1 = y2 - y1;
b1 = x1 - x2;
c1 = x2 * y1 - x1 * y2;
// Compute r3 and r4.
r3 = a1 * x3 + b1 * y3 + c1;
r4 = a1 * x4 + b1 * y4 + c1;
// Check signs of r3 and r4. If both point 3 and point 4 lie on
// same side of line 1, the line segments do not intersect.
if ( r3 != 0 && r4 != 0 && ((r3 >= 0 && r4 >= 0) || (r3 < 0 && r4 < 0))) {
return({0,-1,0});// no intersection
}
// Compute a2, b2, c2
a2 = y4 - y3;
b2 = x3 - x4;
c2 = x4 * y3 - x3 * y4;
// Compute r1 and r2
r1 = a2 * x1 + b2 * y1 + c2;
r2 = a2 * x2 + b2 * y2 + c2;
// Check signs of r1 and r2. If both point 1 and point 2 lie
// on same side of second line segment, the line segments do
// not intersect.
if (r1 != 0 && r2 != 0 && ((r1 >= 0 && r2 >= 0) || (r1 < 0 && r2 < 0))) {
return({0,-1,0});// no intersection
}
// Line segments intersect: compute intersection point.
denom = a1 * b2 - a2 * b1;
if ( denom == 0 ) {
return({0,-2,0});//true.
}
offset = denom < 0 ? - denom / 2 : denom / 2;
x = b1 * c2 - b2 * c1;
y = a2 * c1 - a1 * c2;
float retx=( x < 0 ? x : x ) / denom;
float rety=( y < 0 ? y : y ) / denom;
return(set(retx,0,rety));
}
void drawAngle(vector position;float scale; float radians; vector color){
//draw a circle with 10 points.
float pi = 3.141592;
int circle = addprim(0,"polyline");
float total = 5;
for(int points=0;points<total;points++){
float fraction = points/total;
int p = addpoint(0, set(position[0]+cos(fraction*2*pi)*scale ,position[1],position[2]+sin(fraction*2*pi)*scale));
int pass = setpointattrib(0, "Cd", p, color/5, "set");
addvertex(0, circle, p);
}
int p = addpoint(0, set(position[0]+1*scale ,position[1],position[2]+0*scale));
int pass = setpointattrib(0, "Cd", p, color/5, "set");
addvertex(0, circle, p);
//draw angle
int direction = addprim(0,"polyline");
int p0 = addpoint(0,position);
int p1 = addpoint(0,set(position[0]+cos(radians)*scale ,position[1],position[2]+sin(radians)*scale));
pass = setpointattrib(0, "Cd", p0, color*2, "set");
pass = setpointattrib(0, "Cd", p1, color*2, "set");
addvertex(0,direction,p0);
addvertex(0,direction,p1);
int reference = addprim(0,"polyline");
int p00 = addpoint(0, set(position[0]+(1*scale),position[1],position[2]+(0*scale)));
int p01 = addpoint(0, set(position[0]+(1.2*scale),position[1],position[2]+(0*scale)));
pass = setpointattrib(0, "Cd", p00, color, "set");
pass = setpointattrib(0, "Cd", p01, color, "set");
addvertex(0,reference,p00);
addvertex(0,reference,p01);
}
int pointCount1 = npoints(1);
int nprim2 = nprimitives(2);
for(int pnt=0;pnt<pointCount1;pnt++){ //for each point in channel 1
int c = -1; //-1 == false.
vector cLoc = point(1,"P",pnt);
float cx = cLoc[0];
float cy = cLoc[2];
for(int collisionPrim=0;collisionPrim<nprim2;collisionPrim++){//for each primitive in channel 2
int CollisionPoints[] = primpoints(2,collisionPrim);
int close2 = primintrinsic(2, "closed", collisionPrim);
for(int primEdge=len(CollisionPoints)-1; primEdge>-close2;primEdge--){
int pointA = CollisionPoints[primEdge];
int pointB = CollisionPoints[primEdge-1];
vector pointALoc = point(2,"P",pointA);
vector pointBLoc = point(2,"P",pointB);
float ax = pointALoc[0];
float ay = pointALoc[2];
float bx = pointBLoc[0];
float by = pointBLoc[2];
if((ay <= cy && cy < by) || (by <= cy && cy < ay)){
if((cx < (bx - ax) * (cy - ay) / (by - ay) + ax)){
c= c*-1;
}
}
}
}
if(c==1){
drawAngle(cLoc,0.04,0.0,hsvtorgb(0,1,2));
}else{
drawAngle(cLoc,0.03,0.0,hsvtorgb(0.333,1,1));
}
}