43#include <visp3/core/vpConfig.h>
44#include <visp3/core/vpException.h>
46#ifndef DOXYGEN_SHOULD_SKIP_THIS
47#include "vpClipping.h"
56static void inter(Byte mask, Index v0, Index v1);
57static void point_4D_3D(Point4f *p4,
int size, Byte *cp, Point3f *p3);
92static Point4f *point4f;
93static Index point4f_nbr;
96static Index *poly0, *poly1;
98static Index *poly_tmp;
105void 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";
133void close_clipping(
void)
135 free_huge_Bound(&clip);
137 free((
char *)point4f);
142 free((
char *)poly_tmp);
157static 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) {
180 *po++ = point4f_nbr++;
184 if (inp == IS_INSIDE) {
186 *po++ = point4f_nbr++;
209static Index clipping_Face(Face *fi, Face *fo)
211 Index *flip = poly_tmp;
212 Index *flop = fo->vertex.ptr;
213 Index vn = fi->vertex.nbr;
215 if ((vn = clipping(IS_ABOVE, vn, fi->vertex.ptr, flip)) != 0)
216 if ((vn = clipping(IS_BELOW, vn, flip, flop)) != 0)
217 if ((vn = clipping(IS_RIGHT, vn, flop, flip)) != 0)
218 if ((vn = clipping(IS_LEFT, vn, flip, flop)) != 0)
219 if ((vn = clipping(IS_BACK, vn, flop, flip)) != 0)
220 if ((vn = clipping(IS_FRONT, vn, flip, flop)) != 0) {
224 fo->is_polygonal = fi->is_polygonal;
225 fo->is_visible = fi->is_visible;
227 fo->normal = fi->normal;
246Bound *clipping_Bound(Bound *bp, Matrix m)
248 Face *fi = bp->face.ptr;
249 Face *fend = fi + bp->face.nbr;
250 Face *fo = clip.face.ptr;
254 point4f_nbr = bp->point.nbr;
255 point_3D_4D(bp->point.ptr,
static_cast<int>(point4f_nbr), m, point4f);
256 set_Point4f_code(point4f,
static_cast<int>(point4f_nbr), code);
258 if (!(clip.is_polygonal = bp->is_polygonal))
261 memmove(clip.normal.ptr, bp->normal.ptr, bp->normal.nbr *
sizeof(Vector));
263 for (; fi < fend; fi++) {
264 if (clipping_Face(fi, fo) != 0) {
271 fo->vertex.ptr = (fo - 1)->vertex.ptr + (fo - 1)->vertex.nbr;
275 if (fo == clip.face.ptr)
280 point_4D_3D(point4f,
static_cast<int>(point4f_nbr), code, clip.point.ptr);
281 clip.type = bp->type;
282 clip.face.nbr = (Index)(fo - clip.face.ptr);
283 clip.point.nbr = point4f_nbr;
285 if (!bp->is_polygonal)
286 clip.normal.nbr = point4f_nbr;
299static void inter(Byte mask, Index v0, Index v1)
301 Point4f *
p = point4f + point4f_nbr;
302 Point4f *p0 = point4f + v0;
303 Point4f *p1 = point4f + v1;
312 t = (p0->w - p0->y) - (p1->w - p1->y);
314 t = (std::fabs(t) <= std::numeric_limits<double>::epsilon()) ? 1.0f : (p0->
w - p0->
y) /
t;
315 PAR_COORD3(*p, t, *p0, *p1);
321 t = (p0->w + p0->y) - (p1->w + p1->y);
323 t = (std::fabs(t) <= std::numeric_limits<double>::epsilon()) ? 1.0f : (p0->
w + p0->
y) /
t;
324 PAR_COORD3(*p, t, *p0, *p1);
330 t = (p0->w - p0->x) - (p1->w - p1->x);
332 t = (std::fabs(t) <= std::numeric_limits<double>::epsilon()) ? 1.0f : (p0->
w - p0->
x) /
t;
333 PAR_COORD3(*p, t, *p0, *p1);
339 t = (p0->w + p0->x) - (p1->w + p1->x);
341 t = (std::fabs(t) <= std::numeric_limits<double>::epsilon()) ? 1.0f : (p0->
w + p0->
x) /
t;
342 PAR_COORD3(*p, t, *p0, *p1);
348 t = (p0->w - p0->z) - (p1->w - p1->z);
350 t = (std::fabs(t) <= std::numeric_limits<double>::epsilon()) ? 1.0f : (p0->
w - p0->
z) /
t;
351 PAR_COORD3(*p, t, *p0, *p1);
359 t = (std::fabs(t) <= std::numeric_limits<double>::epsilon()) ? 1.0f : p0->
z /
t;
360 p->x = (p1->x - p0->x) * t + p0->x;
361 p->y = (p1->y - p0->y) * t + p0->y;
362 p->w = (p1->w - p0->w) * t + p0->w;
363 p->z =
static_cast<float>(M_EPSILON);
368 p->w +=
static_cast<float>(M_EPSILON);
369 code[point4f_nbr] = where_is_Point4f(p);
371 if (!clip.is_polygonal) {
372 Vector *n0 = clip.normal.ptr + v0;
373 Vector *n1 = clip.normal.ptr + v1;
374 Vector *n = clip.normal.ptr + point4f_nbr;
376 SET_COORD3(*n, (n1->x - n0->x) * t + n0->x, (n1->y - n0->y) * t + n0->y, (n1->z - n0->z) * t + n0->z);
391static void point_4D_3D(Point4f *p4,
int size, Byte *cp, Point3f *p3)
393 Point4f *pend = p4 + size;
396 for (; p4 < pend; p4++, p3++) {
397 if (*cp++ == IS_INSIDE) {
418void set_Point4f_code(Point4f *p4,
int size, Byte *cp)
420 Point4f *pend = p4 + size;
423 for (; p4 < pend; p4++, *cp++ = b) {
427 else if (-p4->w > p4->y)
431 else if (-p4->w > p4->x)
435 else if (-0.9 > p4->z)
446Byte where_is_Point4f(Point4f *p4)
452 else if (-p4->w > p4->y)
456 else if (-p4->w > p4->x)
460 else if (-0.9 > p4->z)
error that can be emitted by ViSP classes.