-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoly.c
322 lines (246 loc) · 5.82 KB
/
poly.c
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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "common.h"
#include "geom.h"
#include "sphere.h"
#include "poly.h"
#include "input.h"
int validPolyg(polyg P) /* revisa si son coplanares los puntos */
{
vec a,b,c;
int i;
assert(P.nvertices > 2);
a = points2vec(P.vertices[0],P.vertices[1]);
b = points2vec(P.vertices[0],P.vertices[2]);
for(i=3;i<P.nvertices;++i)
{
c = points2vec(P.vertices[0],P.vertices[i]);
if(fabs(tripleProduct(a,b,c)) > EPS) return 0;
}
return 1;
}
vec polygNormal(polyg P)
{
vec a,b;
assert(validPolyg(P));
a = points2vec(P.vertices[0],P.vertices[1]);
b = points2vec(P.vertices[0],P.vertices[2]);
return crossProduct(a,b);
}
int pointInPolyg(point a, polyg P)
{
vec o;
seg ray;
int i;
int c;
assert(validPolyg(P));
if(!coplanar(P.vertices[0],P.vertices[1],P.vertices[2],a))
return 0; /* no coplanares */
for(i=1;i<P.nvertices;++i)
if(pointInSeg(a,points2seg(P.vertices[i],P.vertices[i-1])))
return 1;
if(pointInSeg(a,points2seg(P.vertices[0],P.vertices[P.nvertices-1])))
return 1;
newray:
o = orthoRand(polygNormal(P));
ray = extend(points2seg(a,vecSum(a,o)));
c = 0;
for(i=0;i<P.nvertices;++i)
if(pointInSeg(P.vertices[i],ray))
goto newray;
for(i=1;i<P.nvertices;++i)
if(segsIntersect(ray,points2seg(P.vertices[i],P.vertices[i-1])))
c=!c;
if(segsIntersect(ray, points2seg(P.vertices[0],P.vertices[P.nvertices-1])))
c=!c;
return c;
}
int segPlaneIntersect(seg s, point a, point b, point c, point* r,vec normal)
{
CORD_TYPE d1,d2;
if(dotProduct(points2vec(a,s.p),normal) /* si estan del mismo lado */
* dotProduct(points2vec(a,s.q),normal) > 0)
return 0;
d1 = distPoint2Plane(s.p,a,b,c);
d2 = distPoint2Plane(s.q,a,b,c);
if(r != NULL)
{
*r = vecLinComb(d2,s.p,d1,s.q); /* corte de s con el plano */
*r = vecScale(1/(d1+d2),*r);
}
return 1;
}
int segPolygCut(seg s, polyg P, point* r, vec* normal)
{
vec tn;
point tp;
assert(validPolyg(P));
tn = polygNormal(P);
if(normal != NULL) *normal = tn;
if(!segPlaneIntersect(s,P.vertices[0],P.vertices[1],P.vertices[2],&tp,tn))
return 0;
if(r != NULL) *r = tp;
return pointInPolyg(tp,P);
}
int pointInPolyh(point p, void* data)
{
int i,j;
int c;
seg ray;
vec o;
polyh H;
H = *(polyh*)data;
#ifndef DONT_USE_BOUND
if(!pointInSphere(p,&(H.bound)))
return 0;
#endif
for(i=0;i<H.nfaces;++i)
if(pointInPolyg(p,H.faces[i]))
return 1;
newray:
o = orthoRand(orthoRand(p));
ray.p = p;
ray.q = vecSum(p,o);
ray = extend(ray);
c = 0;
for(i=0;i<H.nfaces;++i)
for(j=0;j<H.faces[i].nvertices;++j)
if(segsIntersect(ray,points2seg(H.faces[i].vertices[j],H.faces[i].vertices[(j+1)%H.faces[i].nvertices])))
goto newray;
for(i=0;i<H.nfaces;++i)
if(segPolygCut(ray,H.faces[i],NULL,NULL))
c = !c;
return c;
}
int segPolyhCut(seg s, void* data, point* in, vec* normal)
{
point r_;
vec normal_;
CORD_TYPE dist_ = 0;
polyh H;
char found = 0;
int i;
H = *(polyh*)data;
#ifndef DONT_USE_BOUND
/* if(!segSphereCut(s,&(H.bound),NULL,NULL))
return 0; */
/* Esto esta mal! Puede cortar el poliedro por mas que no
* corte a la esfera. Cuando esto pasa, ambos puntos son
* interiores a la esfera, o sea que podría arreglarse
* checkeando que al menos un punto sea exterior, pero
* la ganancia en performance no se justifica. Se deberia
* usar una esfera inscripta ademas de la circunscripta o
* algun otro método... */
#endif
for(i=0;i<H.nfaces;++i)
if(segPolygCut(s,H.faces[i],&r_,&normal_))
if((!found || (pointDist2(s.p,r_) < dist_)))
{
if(in != NULL) *in = r_;
if(normal != NULL) *normal = normal_;
dist_ = pointDist2(s.p,r_);
found = 1;
}
return found;
}
int segPolyhCutAll(seg s, void* data, point in[], vec normal[])
{
CORD_TYPE* dist;
CORD_TYPE tdist;
vec tnormal;
point tin;
polyh H;
int i;
int j;
int count = 0;
H = *(polyh*)data;
#ifndef DONT_USE_BOUND
/*
if(!segSphereCut(s,&(H.bound),NULL,NULL))
return 0;
*/
/* esto tambien esta mal (ver segPolyhCut) */
#endif
dist = (CORD_TYPE*)malloc(H.nfaces * sizeof(CORD_TYPE));
assert(dist != NULL);
for(i=0;i<H.nfaces;++i)
if(segPolygCut(s,H.faces[i],&tin,&tnormal))
{
tdist = vecModulus2(points2vec(s.p,tin));
for(j=count-1;j>=0 && dist[j]>tdist;--j)
{
dist[j+1] = dist[j];
if(in != NULL) in[j+1] = in[j];
if(normal != NULL) normal[j+1] = normal[j];
}
dist[j+1] = tdist;
if(in != NULL) in[j+1] = tin;
if(normal != NULL) normal[j+1] = tnormal;
count++;
}
for(i=1;i<count;++i)
assert(dist[i]>=dist[i-1]); /* ordenado */
free(dist);
return count;
}
polyg readPolyg()
{
polyg P;
int i;
P.nvertices = readInt();
P.vertices = (point*)malloc(P.nvertices * sizeof(point));
for(i=0;i<P.nvertices;++i)
{
P.vertices[i].x = readDouble();
P.vertices[i].y = readDouble();
P.vertices[i].z = readDouble();
}
return P;
}
void* readPolyh()
{
polyh* H;
int i;
int nvert;
#ifndef DONT_USE_BOUND
int j;
CORD_TYPE rad;
#endif
H = (polyh*) malloc(sizeof(polyh));
H->nfaces = readInt();
H->faces = (polyg*)malloc(H->nfaces * sizeof(polyg));
nvert = 0;
for(i=0;i<H->nfaces;++i)
{
H->faces[i] = readPolyg();
}
#ifndef DONT_USE_BOUND
H->bound.center = coords2vec(0,0,0);
for(i=0;i<H->nfaces;++i)
{
for(j=0;j< H->faces[i].nvertices;++j)
H->bound.center = vecSum(H->bound.center,H->faces[i].vertices[j]);
nvert += j;
}
H->bound.center = vecScale(1.0/nvert,H->bound.center);
rad = 0;
for(i=0;i<H->nfaces;++i)
for(j=0;j< H->faces[i].nvertices;++j)
if(pointDist(H->bound.center,H->faces[i].vertices[j]) > rad)
rad = pointDist(H->bound.center,H->faces[i].vertices[j]);
H->bound.radius = rad;
#endif
return H;
}
void freePolyg(polyg P)
{
free(P.vertices);
}
void freePolyh(void* data)
{
polyh* H = data;
int i;
for(i=0;i< H->nfaces;++i)
freePolyg(H->faces[i]);
}