45 #include <visp3/core/vpConfig.h> 46 #include <visp3/core/vpException.h> 48 #ifndef DOXYGEN_SHOULD_SKIP_THIS 49 #include "vpClipping.h" 57 static void inter(Byte mask, Index v0, Index v1);
58 static void point_4D_3D(Point4f *p4,
int size, Byte *cp, Point3f *p3);
93 static Point4f *point4f;
94 static Index point4f_nbr;
97 static Index *poly0, *poly1;
99 static Index *poly_tmp;
106 void open_clipping(
void)
109 malloc_huge_Bound(&clip);
112 if ((code = (Byte *)malloc(POINT_NBR *
sizeof(Byte))) == NULL ||
113 (point4f = (Point4f *)malloc(POINT_NBR *
sizeof(Point4f))) == NULL
115 || (poly0 = (Index *)malloc(VERTEX_NBR *
sizeof(Index))) == NULL ||
116 (poly1 = (Index *)malloc(VERTEX_NBR *
sizeof(Index))) == NULL) {
117 static char proc_name[] =
"open_clipping";
122 || (poly_tmp = (Index *)malloc(VERTEX_NBR *
sizeof(Index))) == NULL) {
123 static char proc_name[] =
"open_clipping";
134 void close_clipping(
void)
136 free_huge_Bound(&clip);
138 free((
char *)point4f);
143 free((
char *)poly_tmp);
158 static Index clipping(Byte mask, Index vni, Index *pi, Index *po)
168 Index vs = pi[vni - 1];
169 Byte ins = code[vs] & mask;
173 Byte inp = code[vp] & mask;
175 if (ins == IS_INSIDE) {
176 if (inp == IS_INSIDE) {
180 *po++ = point4f_nbr++;
183 if (inp == IS_INSIDE) {
185 *po++ = point4f_nbr++;
207 static Index clipping_Face(Face *fi, Face *fo)
209 Index *flip = poly_tmp;
210 Index *flop = fo->vertex.ptr;
211 Index vn = fi->vertex.nbr;
213 if ((vn = clipping(IS_ABOVE, vn, fi->vertex.ptr, flip)) != 0)
214 if ((vn = clipping(IS_BELOW, vn, flip, flop)) != 0)
215 if ((vn = clipping(IS_RIGHT, vn, flop, flip)) != 0)
216 if ((vn = clipping(IS_LEFT, vn, flip, flop)) != 0)
217 if ((vn = clipping(IS_BACK, vn, flop, flip)) != 0)
218 if ((vn = clipping(IS_FRONT, vn, flip, flop)) != 0) {
222 fo->is_polygonal = fi->is_polygonal;
223 fo->is_visible = fi->is_visible;
225 fo->normal = fi->normal;
244 Bound *clipping_Bound(Bound *bp, Matrix m)
246 Face *fi = bp->face.ptr;
247 Face *fend = fi + bp->face.nbr;
248 Face *fo = clip.face.ptr;
252 point4f_nbr = bp->point.nbr;
253 point_3D_4D(bp->point.ptr, (
int)point4f_nbr, m, point4f);
254 set_Point4f_code(point4f, (
int)point4f_nbr, code);
256 if (!(clip.is_polygonal = bp->is_polygonal))
259 memmove(clip.normal.ptr, bp->normal.ptr, bp->normal.nbr *
sizeof(Vector));
261 for (; fi < fend; fi++) {
262 if (clipping_Face(fi, fo) != 0) {
269 fo->vertex.ptr = (fo - 1)->vertex.ptr + (fo - 1)->vertex.nbr;
273 if (fo == clip.face.ptr)
278 point_4D_3D(point4f, (
int)point4f_nbr, code, clip.point.ptr);
279 clip.type = bp->type;
280 clip.face.nbr = (Index)(fo - clip.face.ptr);
281 clip.point.nbr = point4f_nbr;
283 if (!bp->is_polygonal)
284 clip.normal.nbr = point4f_nbr;
297 static void inter(Byte mask, Index v0, Index v1)
299 Point4f *p = point4f + point4f_nbr;
300 Point4f *p0 = point4f + v0;
301 Point4f *p1 = point4f + v1;
310 t = (p0->w - p0->y) - (p1->w - p1->y);
312 t = (std::fabs(t) <= std::numeric_limits<double>::epsilon()) ? (
float)1.0 : (p0->w - p0->y) / t;
313 PAR_COORD3(*p, t, *p0, *p1);
319 t = (p0->w + p0->y) - (p1->w + p1->y);
321 t = (std::fabs(t) <= std::numeric_limits<double>::epsilon()) ? (
float)1.0 : (p0->w + p0->y) / t;
322 PAR_COORD3(*p, t, *p0, *p1);
328 t = (p0->w - p0->x) - (p1->w - p1->x);
330 t = (std::fabs(t) <= std::numeric_limits<double>::epsilon()) ? (
float)1.0 : (p0->w - p0->x) / t;
331 PAR_COORD3(*p, t, *p0, *p1);
337 t = (p0->w + p0->x) - (p1->w + p1->x);
339 t = (std::fabs(t) <= std::numeric_limits<double>::epsilon()) ? (
float)1.0 : (p0->w + p0->x) / t;
340 PAR_COORD3(*p, t, *p0, *p1);
346 t = (p0->w - p0->z) - (p1->w - p1->z);
348 t = (std::fabs(t) <= std::numeric_limits<double>::epsilon()) ? (
float)1.0 : (p0->w - p0->z) / t;
349 PAR_COORD3(*p, t, *p0, *p1);
357 t = (std::fabs(t) <= std::numeric_limits<double>::epsilon()) ? (
float)1.0 : p0->z / t;
358 p->x = (p1->x - p0->x) * t + p0->x;
359 p->y = (p1->y - p0->y) * t + p0->y;
360 p->w = (p1->w - p0->w) * t + p0->w;
361 p->z = (
float)M_EPSILON;
366 p->w += (float)M_EPSILON;
367 code[point4f_nbr] = where_is_Point4f(p);
369 if (!clip.is_polygonal) {
370 Vector *n0 = clip.normal.ptr + v0;
371 Vector *n1 = clip.normal.ptr + v1;
372 Vector *n = clip.normal.ptr + point4f_nbr;
374 SET_COORD3(*n, (n1->x - n0->x) * t + n0->x, (n1->y - n0->y) * t + n0->y, (n1->z - n0->z) * t + n0->z);
389 static void point_4D_3D(Point4f *p4,
int size, Byte *cp, Point3f *p3)
391 Point4f *pend = p4 + size;
394 for (; p4 < pend; p4++, p3++) {
395 if (*cp++ == IS_INSIDE) {
415 void set_Point4f_code(Point4f *p4,
int size, Byte *cp)
417 Point4f *pend = p4 + size;
420 for (; p4 < pend; p4++, *cp++ = b) {
424 else if (-p4->w > p4->y)
428 else if (-p4->w > p4->x)
432 else if (-0.9 > p4->z)
443 Byte where_is_Point4f(Point4f *p4)
449 else if (-p4->w > p4->y)
453 else if (-p4->w > p4->x)
457 else if (-0.9 > p4->z)
error that can be emited by ViSP classes.