45 #include <visp3/core/vpConfig.h> 47 #ifndef DOXYGEN_SHOULD_SKIP_THIS 48 #include "vpClipping.h" 56 static void inter(Byte mask, Index v0, Index v1);
57 static void point_4D_3D(Point4f *p4,
int size, Byte *cp, Point3f *p3);
92 static Point4f *point4f;
93 static Index point4f_nbr;
96 static Index *poly0, *poly1;
98 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);
137 free((
char *)point4f);
142 free((
char *)poly_tmp);
157 static Index clipping(Byte mask, Index vni, Index *pi, Index *po)
167 Index vs = pi[vni - 1];
168 Byte ins = code[vs] & mask;
172 Byte inp = code[vp] & mask;
174 if (ins == IS_INSIDE) {
175 if (inp == IS_INSIDE) {
179 *po++ = point4f_nbr++;
182 if (inp == IS_INSIDE) {
184 *po++ = point4f_nbr++;
206 static Index clipping_Face(Face *fi, Face *fo)
208 Index *flip = poly_tmp;
209 Index *flop = fo->vertex.ptr;
210 Index vn = fi->vertex.nbr;
212 if ((vn = clipping(IS_ABOVE, vn, fi->vertex.ptr, flip)) != 0)
213 if ((vn = clipping(IS_BELOW, vn, flip, flop)) != 0)
214 if ((vn = clipping(IS_RIGHT, vn, flop, flip)) != 0)
215 if ((vn = clipping(IS_LEFT, vn, flip, flop)) != 0)
216 if ((vn = clipping(IS_BACK, vn, flop, flip)) != 0)
217 if ((vn = clipping(IS_FRONT, vn, flip, flop)) != 0) {
221 fo->is_polygonal = fi->is_polygonal;
222 fo->is_visible = fi->is_visible;
224 fo->normal = fi->normal;
243 Bound *clipping_Bound(Bound *bp, Matrix m)
245 Face *fi = bp->face.ptr;
246 Face *fend = fi + bp->face.nbr;
247 Face *fo = clip.face.ptr;
251 point4f_nbr = bp->point.nbr;
252 point_3D_4D(bp->point.ptr, (
int)point4f_nbr, m, point4f);
253 set_Point4f_code(point4f, (
int)point4f_nbr, code);
255 if (!(clip.is_polygonal = bp->is_polygonal))
258 memmove(clip.normal.ptr, bp->normal.ptr, bp->normal.nbr *
sizeof(Vector));
260 for (; fi < fend; fi++) {
261 if (clipping_Face(fi, fo) != 0) {
268 fo->vertex.ptr = (fo - 1)->vertex.ptr + (fo - 1)->vertex.nbr;
272 if (fo == clip.face.ptr)
277 point_4D_3D(point4f, (
int)point4f_nbr, code, clip.point.ptr);
278 clip.type = bp->type;
279 clip.face.nbr = (Index)(fo - clip.face.ptr);
280 clip.point.nbr = point4f_nbr;
282 if (!bp->is_polygonal)
283 clip.normal.nbr = point4f_nbr;
296 static void inter(Byte mask, Index v0, Index v1)
298 Point4f *p = point4f + point4f_nbr;
299 Point4f *p0 = point4f + v0;
300 Point4f *p1 = point4f + v1;
309 t = (p0->w - p0->y) - (p1->w - p1->y);
311 t = (std::fabs(t) <= std::numeric_limits<double>::epsilon()) ? (
float)1.0 : (p0->w - p0->y) / t;
312 PAR_COORD3(*p, t, *p0, *p1);
318 t = (p0->w + p0->y) - (p1->w + p1->y);
320 t = (std::fabs(t) <= std::numeric_limits<double>::epsilon()) ? (
float)1.0 : (p0->w + p0->y) / t;
321 PAR_COORD3(*p, t, *p0, *p1);
327 t = (p0->w - p0->x) - (p1->w - p1->x);
329 t = (std::fabs(t) <= std::numeric_limits<double>::epsilon()) ? (
float)1.0 : (p0->w - p0->x) / t;
330 PAR_COORD3(*p, t, *p0, *p1);
336 t = (p0->w + p0->x) - (p1->w + p1->x);
338 t = (std::fabs(t) <= std::numeric_limits<double>::epsilon()) ? (
float)1.0 : (p0->w + p0->x) / t;
339 PAR_COORD3(*p, t, *p0, *p1);
345 t = (p0->w - p0->z) - (p1->w - p1->z);
347 t = (std::fabs(t) <= std::numeric_limits<double>::epsilon()) ? (
float)1.0 : (p0->w - p0->z) / t;
348 PAR_COORD3(*p, t, *p0, *p1);
356 t = (std::fabs(t) <= std::numeric_limits<double>::epsilon()) ? (
float)1.0 : p0->z / t;
357 p->x = (p1->x - p0->x) * t + p0->x;
358 p->y = (p1->y - p0->y) * t + p0->y;
359 p->w = (p1->w - p0->w) * t + p0->w;
360 p->z = (
float)M_EPSILON;
365 p->w += (float)M_EPSILON;
366 code[point4f_nbr] = where_is_Point4f(p);
368 if (!clip.is_polygonal) {
369 Vector *n0 = clip.normal.ptr + v0;
370 Vector *n1 = clip.normal.ptr + v1;
371 Vector *n = clip.normal.ptr + point4f_nbr;
373 SET_COORD3(*n, (n1->x - n0->x) * t + n0->x, (n1->y - n0->y) * t + n0->y, (n1->z - n0->z) * t + n0->z);
388 static void point_4D_3D(Point4f *p4,
int size, Byte *cp, Point3f *p3)
390 Point4f *pend = p4 + size;
393 for (; p4 < pend; p4++, p3++) {
394 if (*cp++ == IS_INSIDE) {
414 void set_Point4f_code(Point4f *p4,
int size, Byte *cp)
416 Point4f *pend = p4 + size;
419 for (; p4 < pend; p4++, *cp++ = b) {
423 else if (-p4->w > p4->y)
427 else if (-p4->w > p4->x)
431 else if (-0.9 > p4->z)
442 Byte where_is_Point4f(Point4f *p4)
448 else if (-p4->w > p4->y)
452 else if (-p4->w > p4->x)
456 else if (-0.9 > p4->z)