--- ray/src/common/face.c 2016/09/16 15:09:21 2.13 +++ ray/src/common/face.c 2024/08/18 00:06:10 2.17 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: face.c,v 2.13 2016/09/16 15:09:21 greg Exp $"; +static const char RCSid[] = "$Id: face.c,v 2.17 2024/08/18 00:06:10 greg Exp $"; #endif /* * face.c - routines dealing with polygonal faces. @@ -57,7 +57,7 @@ getface( /* get arguments for a face */ f->va = o->oargs.farg; f->nv = o->oargs.nfargs / 3; /* check for last==first */ - if (dist2(VERTEX(f,0),VERTEX(f,f->nv-1)) <= FTINY*FTINY) + if (f->nv > 3 && dist2(VERTEX(f,0),VERTEX(f,f->nv-1)) <= FTINY*FTINY) f->nv--; /* compute area and normal */ f->norm[0] = f->norm[1] = f->norm[2] = 0.0; @@ -98,7 +98,7 @@ getface( /* get arguments for a face */ if (f->nv > 3 && badvert) objerror(o, WARNING, "non-planar vertex"); /* find axis */ - f->ax = fabs(f->norm[0]) > fabs(f->norm[1]) ? 0 : 1; + f->ax = (fabs(f->norm[1]) > fabs(f->norm[0])); if (fabs(f->norm[2]) > fabs(f->norm[f->ax])) f->ax = 2; @@ -140,14 +140,21 @@ inface( /* determine if point is in face */ ncross = 0; /* positive x axis cross test */ while (n--) { + if (FRELEQ(p0[yi], y) && FRELEQ(p1[yi], y) && + (p0[xi] > x) ^ (p1[xi] > x)) + return(1); /* edge case #1 */ if ((p0[yi] > y) ^ (p1[yi] > y)) { tst = (p0[xi] > x) + (p1[xi] > x); if (tst == 2) ncross++; - else if (tst) - ncross += (p1[yi] > p0[yi]) ^ - ((p0[yi]-y)*(p1[xi]-x) > - (p0[xi]-x)*(p1[yi]-y)); + else if (tst) { + double prodA = (p0[yi]-y)*(p1[xi]-x); + double prodB = (p0[xi]-x)*(p1[yi]-y); + if (FRELEQ(prodA, prodB)) + return(1); /* edge case #2 */ + ncross += (p1[yi] > p0[yi]) ^ (prodA > prodB); + } else if (FRELEQ(p0[xi], x) && FRELEQ(p1[xi], x)) + return(1); /* edge case #3 */ } p0 = p1; p1 += 3;