44 #include <visp3/core/vpConfig.h>
46 #ifndef DOXYGEN_SHOULD_SKIP_THIS
47 #include "vpClipping.h"
55 static void inter (Byte mask, Index v0, Index v1);
56 static void point_4D_3D (Point4f *p4,
int size, Byte *cp, Point3f *p3);
91 static Point4f *point4f;
92 static Index point4f_nbr;
95 static Index *poly0, *poly1;
97 static Index *poly_tmp;
105 void open_clipping (
void)
108 malloc_huge_Bound (&clip);
111 if ((code = (Byte *) malloc (POINT_NBR *
sizeof (Byte))) == NULL
112 || (point4f = (Point4f *) malloc (POINT_NBR *
sizeof (Point4f))) == NULL
114 || (poly0 = (Index *) malloc (VERTEX_NBR *
sizeof (Index))) == NULL
115 || (poly1 = (Index *) malloc (VERTEX_NBR *
sizeof (Index))) == NULL) {
116 static char proc_name[] =
"open_clipping";
121 || (poly_tmp = (Index *) malloc (VERTEX_NBR *
sizeof (Index))) == NULL){
122 static char proc_name[] =
"open_clipping";
133 void close_clipping (
void)
135 free_huge_Bound (&clip);
136 free ((
char *) code);
137 free ((
char *) point4f);
139 free ((
char *) poly0);
140 free ((
char *) poly1);
142 free ((
char *) poly_tmp);
159 clipping (Byte mask, Index vni, Index *pi, Index *po)
169 Index vs = pi[vni-1];
171 Byte ins = code[vs] & mask;
175 Byte inp = code[vp] & mask;
177 if (ins == IS_INSIDE) {
178 if (inp == IS_INSIDE) {
182 inter (mask, vs, vp);
183 *po++ = point4f_nbr++;
187 if (inp == IS_INSIDE) {
188 inter (mask, vs, vp);
189 *po++ = point4f_nbr++;
214 clipping_Face (Face *fi, Face *fo)
216 Index *flip = poly_tmp;
217 Index *flop = fo->vertex.ptr;
218 Index vn = fi->vertex.nbr;
220 if ((vn = clipping (IS_ABOVE, vn, fi->vertex.ptr, flip)) != 0)
221 if ((vn = clipping (IS_BELOW, vn, flip, flop)) != 0)
222 if ((vn = clipping (IS_RIGHT, vn, flop, flip)) != 0)
223 if ((vn = clipping (IS_LEFT, vn, flip, flop)) != 0)
224 if ((vn = clipping (IS_BACK, vn, flop, flip)) != 0)
225 if ((vn = clipping (IS_FRONT, vn, flip, flop)) != 0) {
229 fo->is_polygonal = fi->is_polygonal;
230 fo->is_visible = fi->is_visible;
232 fo->normal = fi->normal;
252 Bound *clipping_Bound (Bound *bp, Matrix m)
254 Face *fi = bp->face.ptr;
255 Face *fend = fi + bp->face.nbr;
256 Face *fo = clip.face.ptr;
260 point4f_nbr = bp->point.nbr;
261 point_3D_4D (bp->point.ptr, (
int) point4f_nbr, m, point4f);
262 set_Point4f_code (point4f, (
int) point4f_nbr, code);
264 if (! (clip.is_polygonal = bp->is_polygonal))
267 memmove (clip.normal.ptr, bp->normal.ptr,
268 bp->normal.nbr * sizeof (Vector));
270 for (; fi < fend; fi++) {
271 if (clipping_Face (fi, fo) != 0) {
278 fo->vertex.ptr = (fo-1)->vertex.ptr+(fo-1)->vertex.nbr;
282 if (fo == clip.face.ptr)
287 point_4D_3D (point4f, (
int) point4f_nbr, code, clip.point.ptr);
288 clip.type = bp->type;
289 clip.face.nbr = (Index)( fo - clip.face.ptr );
290 clip.point.nbr = point4f_nbr;
292 if (! bp->is_polygonal)
293 clip.normal.nbr = point4f_nbr;
307 inter (Byte mask, Index v0, Index v1)
309 Point4f *p = point4f + point4f_nbr;
310 Point4f *p0 = point4f + v0;
311 Point4f *p1 = point4f + v1;
320 t = (p0->w - p0->y) - (p1->w - p1->y);
322 t = (std::fabs(t) <= std::numeric_limits<double>::epsilon()) ? (
float)1.0 : (p0->w - p0->y) / t;
323 PAR_COORD3(*p,t,*p0,*p1);
329 t = (p0->w + p0->y) - (p1->w + p1->y);
331 t = (std::fabs(t) <= std::numeric_limits<double>::epsilon()) ? (
float)1.0 : (p0->w + p0->y) / t;
332 PAR_COORD3(*p,t,*p0,*p1);
338 t = (p0->w - p0->x) - (p1->w - p1->x);
340 t = (std::fabs(t) <= std::numeric_limits<double>::epsilon()) ? (
float)1.0 : (p0->w - p0->x) / t;
341 PAR_COORD3(*p,t,*p0,*p1);
347 t = (p0->w + p0->x) - (p1->w + p1->x);
349 t = (std::fabs(t) <= std::numeric_limits<double>::epsilon()) ? (
float)1.0 : (p0->w + p0->x) / t;
350 PAR_COORD3(*p,t,*p0,*p1);
356 t = (p0->w - p0->z) - (p1->w - p1->z);
358 t = (std::fabs(t) <= std::numeric_limits<double>::epsilon()) ? (
float)1.0 : (p0->w - p0->z) / t;
359 PAR_COORD3(*p,t,*p0,*p1);
367 t = (std::fabs(t) <= std::numeric_limits<double>::epsilon()) ? (
float)1.0 : p0->z / t;
368 p->x = (p1->x - p0->x) * t + p0->x;
369 p->y = (p1->y - p0->y) * t + p0->y;
370 p->w = (p1->w - p0->w) * t + p0->w;
371 p->z = (
float)M_EPSILON;
376 p->w += (float)M_EPSILON;
377 code[point4f_nbr] = where_is_Point4f (p);
379 if (! clip.is_polygonal) {
380 Vector *n0 = clip.normal.ptr + v0;
381 Vector *n1 = clip.normal.ptr + v1;
382 Vector *n = clip.normal.ptr + point4f_nbr;
385 (n1->x - n0->x) * t + n0->x,
386 (n1->y - n0->y) * t + n0->y,
387 (n1->z - n0->z) * t + n0->z);
403 point_4D_3D (Point4f *p4,
int size, Byte *cp, Point3f *p3)
405 Point4f *pend = p4 + size;
408 for (; p4 < pend; p4++, p3++) {
409 if (*cp++ == IS_INSIDE) {
432 set_Point4f_code (Point4f *p4,
int size, Byte *cp)
434 Point4f *pend = p4 + size;
437 for (; p4 < pend; p4++, *cp++ = b) {
439 if ( p4->w < p4->y) b |= IS_ABOVE;
440 else if (- p4->w > p4->y) b |= IS_BELOW;
441 if ( p4->w < p4->x) b |= IS_RIGHT;
442 else if (- p4->w > p4->x) b |= IS_LEFT;
443 if ( p4->w < p4->z) b |= IS_BACK;
444 else if ( -0.9 > p4->z) b |= IS_FRONT;
458 where_is_Point4f (Point4f *p4)
462 if ( p4->w < p4->y) b |= IS_ABOVE;
463 else if (- p4->w > p4->y) b |= IS_BELOW;
464 if ( p4->w < p4->x) b |= IS_RIGHT;
465 else if (- p4->w > p4->x) b |= IS_LEFT;
466 if ( p4->w < p4->z) b |= IS_BACK;
467 else if ( -0.9 > p4->z) b |= IS_FRONT;