ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/triangulate.c
(Generate patch)

Comparing ray/src/common/triangulate.c (file contents):
Revision 2.1 by greg, Thu Jan 23 23:51:41 2014 UTC vs.
Revision 2.4 by greg, Thu Dec 22 18:48:36 2016 UTC

# Line 38 | Line 38 | static int
38   polySnip(const Vert2_list *contour, int u, int v, int w, int n, int *V)
39   {
40    int p;
41 <  double Ax, Ay, Bx, By, Cx, Cy, Px, Py;
41 >  double Ax, Ay, Bx, By, Cx, Cy, Px, Py, cross;
42  
43    Ax = contour->v[V[u]].mX;
44    Ay = contour->v[V[u]].mY;
# Line 49 | Line 49 | polySnip(const Vert2_list *contour, int u, int v, int
49    Cx = contour->v[V[w]].mX;
50    Cy = contour->v[V[w]].mY;
51  
52 <  if ( EPSILON > (((Bx-Ax)*(Cy-Ay)) - ((By-Ay)*(Cx-Ax))) ) return false;
52 >  cross = ((Bx - Ax)*(Cy - Ay)) - ((By - Ay)*(Cx - Ax));
53 >  if (EPSILON > cross) return EPSILON > -cross ? -1 : false; /* Negative if colinear points */
54  
55    for (p=0;p<n;p++)
56    {
57 <    if( (p == u) || (p == v) || (p == w) ) continue;
57 >    if( (p == u) | (p == v) | (p == w) ) continue;
58      Px = contour->v[V[p]].mX;
59      Py = contour->v[V[p]].mY;
60 +    if ((Px == Ax && Py == Ay) || (Px == Bx && Py == By) ||
61 +                (Px == Cx && Py == Cy)) continue; /* Handle donuts */
62      if (insideTriangle(Ax,Ay,Bx,By,Cx,Cy,Px,Py)) return false;
63    }
64  
# Line 123 | Line 126 | polyTriangulate(const Vert2_list *contour, tri_out_t *
126   {
127    /* allocate and initialize list of Vertices in polygon */
128  
129 <  int nv, m, u, v, w, count;
129 >  int nv, m, u, v, w, count, result;
130    int *V;
131  
132    if ( contour->nv < 3 ) return false;
# Line 133 | Line 136 | polyTriangulate(const Vert2_list *contour, tri_out_t *
136  
137    /* we want a counter-clockwise polygon in V */
138  
139 <  if ( 0.0 < Area(contour) )
139 >  if ( 0.0 < polyArea(contour) )
140      for (v=0; v<contour->nv; v++) V[v] = v;
141    else
142      for(v=0; v<contour->nv; v++) V[v] = (contour->nv-1)-v;
# Line 146 | Line 149 | polyTriangulate(const Vert2_list *contour, tri_out_t *
149    for(m=0, v=nv-1; nv>2; )
150    {
151      /* if we loop, it is probably a non-simple polygon */
152 <    if (0 >= (count--))
152 >    if (0 >= count--)
153      {
154 <      /* Triangulate: ERROR - probable bad polygon! */
154 >      /* Triangulate: ERROR - probable bad polygon */
155        return false;
156      }
157  
# Line 157 | Line 160 | polyTriangulate(const Vert2_list *contour, tri_out_t *
160      v = u+1; if (nv <= v) v = 0;     /* new v    */
161      w = v+1; if (nv <= w) w = 0;     /* next     */
162  
163 <    if ( polySnip(contour,u,v,w,nv,V) )
163 >    result = polySnip(contour, u, v, w, nv, V);
164 >    if (result > 0) /* successfully found a triangle */
165      {
166 <      int a,b,c,s,t;
166 >      int a,b,c;
167  
168        /* true names of the vertices */
169        a = V[u]; b = V[v]; c = V[w];
170  
171        /* output Triangle */
172 <      (*cb)(contour, a, b, c);
172 >      if (!(*cb)(contour, a, b, c)) return false;
173  
174        m++;
175 +    }
176 +    if (result) /* successfully found a triangle or three consecutive colinear points */
177 +    {
178 +      int s,t;
179  
180        /* remove v from remaining polygon */
181        for(s=v,t=v+1;t<nv;s++,t++) V[s] = V[t]; nv--;
182  
183 <      /* resest error detection counter */
183 >      /* reset error detection counter */
184        count = 2*nv;
185      }
186    }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines