52 #include <visp/vpConfig.h>
54 #ifndef DOXYGEN_SHOULD_SKIP_THIS
55 #include <visp/vpMy.h>
56 #include <visp/vpArit.h>
57 #include <visp/vpBound.h>
58 #include <visp/vpView.h>
64 void open_clipping (
void);
65 void close_clipping (
void);
66 static Index clipping (Byte mask, Index vni, Index *pi, Index *po);
67 static Index clipping_Face (Face *fi, Face *fo);
68 static Index clipping_Face (Face *fi, Face *fo);
69 static void inter (Byte mask, Index v0, Index v1);
70 static void point_4D_3D (Point4f *p4,
int size, Byte *cp, Point3f *p3);
71 void set_Point4f_code (Point4f *p4,
int size, Byte *cp);
72 Byte where_is_Point4f (Point4f *p4);
107 static Point4f *point4f;
108 static Index point4f_nbr;
111 static Index *poly0, *poly1;
113 static Index *poly_tmp;
121 void open_clipping (
void)
123 static char proc_name[] =
"open_clipping";
126 malloc_huge_Bound (&clip);
129 if ((code = (Byte *) malloc (POINT_NBR *
sizeof (Byte))) == NULL
130 || (point4f = (Point4f *) malloc (POINT_NBR *
sizeof (Point4f))) == NULL
132 || (poly0 = (Index *) malloc (VERTEX_NBR *
sizeof (Index))) == NULL
133 || (poly1 = (Index *) malloc (VERTEX_NBR *
sizeof (Index))) == NULL) {
138 || (poly_tmp = (Index *) malloc (VERTEX_NBR *
sizeof (Index))) == NULL){
149 void close_clipping (
void)
151 free_huge_Bound (&clip);
152 free ((
char *) code);
153 free ((
char *) point4f);
155 free ((
char *) poly0);
156 free ((
char *) poly1);
158 free ((
char *) poly_tmp);
175 clipping (Byte mask, Index vni, Index *pi, Index *po)
185 Index vs = pi[vni-1];
187 Byte ins = code[vs] & mask;
192 inp = code[vp] & mask;
194 if (ins == IS_INSIDE) {
195 if (inp == IS_INSIDE) {
199 inter (mask, vs, vp);
200 *po++ = point4f_nbr++;
204 if (inp == IS_INSIDE) {
205 inter (mask, vs, vp);
206 *po++ = point4f_nbr++;
231 clipping_Face (Face *fi, Face *fo)
233 Index *flip = poly_tmp;
234 Index *flop = fo->vertex.ptr;
235 Index vn = fi->vertex.nbr;
237 if ((vn = clipping (IS_ABOVE, vn, fi->vertex.ptr, flip)) != 0)
238 if ((vn = clipping (IS_BELOW, vn, flip, flop)) != 0)
239 if ((vn = clipping (IS_RIGHT, vn, flop, flip)) != 0)
240 if ((vn = clipping (IS_LEFT, vn, flip, flop)) != 0)
241 if ((vn = clipping (IS_BACK, vn, flop, flip)) != 0)
242 if ((vn = clipping (IS_FRONT, vn, flip, flop)) != 0) {
246 fo->is_polygonal = fi->is_polygonal;
247 fo->is_visible = fi->is_visible;
249 fo->normal = fi->normal;
270 *clipping_Bound (Bound *bp, Matrix m)
272 Face *fi = bp->face.ptr;
273 Face *fend = fi + bp->face.nbr;
274 Face *fo = clip.face.ptr;
278 point4f_nbr = bp->point.nbr;
279 point_3D_4D (bp->point.ptr, (
int) point4f_nbr, m, point4f);
280 set_Point4f_code (point4f, (
int) point4f_nbr, code);
282 if (! (clip.is_polygonal = bp->is_polygonal))
285 memmove (clip.normal.ptr, bp->normal.ptr,
286 bp->normal.nbr * sizeof (Vector));
288 for (; fi < fend; fi++) {
289 if (clipping_Face (fi, fo) != 0) {
296 fo->vertex.ptr = (fo-1)->vertex.ptr+(fo-1)->vertex.nbr;
300 if (fo == clip.face.ptr)
305 point_4D_3D (point4f, (
int) point4f_nbr, code, clip.point.ptr);
306 clip.type = bp->type;
307 clip.face.nbr = (Index)( fo - clip.face.ptr );
308 clip.point.nbr = point4f_nbr;
310 if (! bp->is_polygonal)
311 clip.normal.nbr = point4f_nbr;
325 inter (Byte mask, Index v0, Index v1)
327 Point4f *p = point4f + point4f_nbr;
328 Point4f *p0 = point4f + v0;
329 Point4f *p1 = point4f + v1;
338 t = (p0->w - p0->y) - (p1->w - p1->y);
340 t = (std::fabs(t) <= std::numeric_limits<double>::epsilon()) ? (
float)1.0 : (p0->w - p0->y) / t;
341 PAR_COORD3(*p,t,*p0,*p1);
347 t = (p0->w + p0->y) - (p1->w + p1->y);
349 t = (std::fabs(t) <= std::numeric_limits<double>::epsilon()) ? (
float)1.0 : (p0->w + p0->y) / t;
350 PAR_COORD3(*p,t,*p0,*p1);
356 t = (p0->w - p0->x) - (p1->w - p1->x);
358 t = (std::fabs(t) <= std::numeric_limits<double>::epsilon()) ? (
float)1.0 : (p0->w - p0->x) / t;
359 PAR_COORD3(*p,t,*p0,*p1);
365 t = (p0->w + p0->x) - (p1->w + p1->x);
367 t = (std::fabs(t) <= std::numeric_limits<double>::epsilon()) ? (
float)1.0 : (p0->w + p0->x) / t;
368 PAR_COORD3(*p,t,*p0,*p1);
374 t = (p0->w - p0->z) - (p1->w - p1->z);
376 t = (std::fabs(t) <= std::numeric_limits<double>::epsilon()) ? (
float)1.0 : (p0->w - p0->z) / t;
377 PAR_COORD3(*p,t,*p0,*p1);
385 t = (std::fabs(t) <= std::numeric_limits<double>::epsilon()) ? (
float)1.0 : p0->z / t;
386 p->x = (p1->x - p0->x) * t + p0->x;
387 p->y = (p1->y - p0->y) * t + p0->y;
388 p->w = (p1->w - p0->w) * t + p0->w;
389 p->z = (
float)M_EPSILON;
394 p->w += (float)M_EPSILON;
395 code[point4f_nbr] = where_is_Point4f (p);
397 if (! clip.is_polygonal) {
398 Vector *n0 = clip.normal.ptr + v0;
399 Vector *n1 = clip.normal.ptr + v1;
400 Vector *n = clip.normal.ptr + point4f_nbr;
403 (n1->x - n0->x) * t + n0->x,
404 (n1->y - n0->y) * t + n0->y,
405 (n1->z - n0->z) * t + n0->z);
421 point_4D_3D (Point4f *p4,
int size, Byte *cp, Point3f *p3)
423 Point4f *pend = p4 + size;
426 for (; p4 < pend; p4++, p3++) {
427 if (*cp++ == IS_INSIDE) {
450 set_Point4f_code (Point4f *p4,
int size, Byte *cp)
452 Point4f *pend = p4 + size;
455 for (; p4 < pend; p4++, *cp++ = b) {
457 if ( p4->w < p4->y) b |= IS_ABOVE;
458 else if (- p4->w > p4->y) b |= IS_BELOW;
459 if ( p4->w < p4->x) b |= IS_RIGHT;
460 else if (- p4->w > p4->x) b |= IS_LEFT;
461 if ( p4->w < p4->z) b |= IS_BACK;
462 else if ( -0.9 > p4->z) b |= IS_FRONT;
476 where_is_Point4f (Point4f *p4)
480 if ( p4->w < p4->y) b |= IS_ABOVE;
481 else if (- p4->w > p4->y) b |= IS_BELOW;
482 if ( p4->w < p4->x) b |= IS_RIGHT;
483 else if (- p4->w > p4->x) b |= IS_LEFT;
484 if ( p4->w < p4->z) b |= IS_BACK;
485 else if ( -0.9 > p4->z) b |= IS_FRONT;