Esempio n. 1
0
//
// Enforce delaunay on triangle t. Assume that p1 & p2 are the
// endpoints to the edge that is being checked for delaunay
//
void enforceDelaunay(TRIANGLE *t, R_POINT *p1, R_POINT *p2, R_POINT *p3,
		     double e, TIN_TILE *tt){
  assert(t);

  // Since we have tiles we cannot garauntee global delaunay. We must
  // not enforce delaunay on boundary edges, that is edges that are on
  // the same boundary together.
  if(!(edgeOnBoundary(p1,p2,tt))){
    
    // Find the triangle on the other side of edge p1p2
    TRIANGLE *tn = whichTri(t,p1,p2,tt);  
    
    // If tn is NULL then we are done, otherwise we need to check delaunay
    if(tn != NULL){
      
      // Find point across from edge p1p2 in tn
      R_POINT *d = findThirdPoint(tn->p1,tn->p2,tn->p3,p1,p2);
      
      if(CircumCircle(d->x,d->y,p1->x,p1->y,p2->x,p2->y,p3->x,p3->y)){
	edgeSwap(t,tn,p1,p3,p2,d,e,tt);
      }
    }
  }
}
Esempio n. 2
0
/*
   Triangulation subroutine
   Takes as input NV vertices in array pxyz
   Returned is a list of ntri triangular faces in the array v
   These triangles are arranged in a consistent clockwise order.
   The triangle array 'v' should be malloced to 3 * nv
   The vertex array pxyz must be big enough to hold 3 more points
   The vertex array must be sorted in increasing x values say

   qsort(p,nv,sizeof(XYZ),XYZCompare);
      :
   int XYZCompare(void *v1,void *v2)
   {
      XYZ *p1,*p2;
      p1 = v1;
      p2 = v2;
      if (p1->x < p2->x)
         return(-1);
      else if (p1->x > p2->x)
         return(1);
      else
         return(0);
   }
*/
int Triangulate(int nv,XYZ *pxyz,ITRIANGLE *v,int *ntri, vector<IEDGE> & edges)
{
   bool *complete = NULL;
   //IEDGE *edges = NULL;
   int nedge = 0;
   int trimax,emax = 200;
   int status = 0;

   int inside;
   int i,j,k;
   double xp,yp,x1,y1,x2,y2,x3,y3,xc,yc,r;
   double xmin,xmax,ymin,ymax,xmid,ymid;
   double dx,dy,dmax;

   /* Allocate memory for the completeness list, flag for each triangle */
   trimax = 4 * nv;
   if ((complete = (bool*)malloc(trimax*sizeof(bool))) == NULL) {
      status = 1;
      goto skip;
   }

   edges.resize(emax);

   /* Allocate memory for the edge list */
   /*if ((edges = (IEDGE*)malloc(emax*(long)sizeof(IEDGE))) == NULL) {
      status = 2;
      goto skip;
   }*/

   /*
      Find the maximum and minimum vertex bounds.
      This is to allow calculation of the bounding triangle
   */
   xmin = pxyz[0].x;
   ymin = pxyz[0].y;
   xmax = xmin;
   ymax = ymin;
   for (i=1;i<nv;i++) {
      if (pxyz[i].x < xmin) xmin = pxyz[i].x;
      if (pxyz[i].x > xmax) xmax = pxyz[i].x;
      if (pxyz[i].y < ymin) ymin = pxyz[i].y;
      if (pxyz[i].y > ymax) ymax = pxyz[i].y;
   }
   dx = xmax - xmin;
   dy = ymax - ymin;
   dmax = (dx > dy) ? dx : dy;
   xmid = (xmax + xmin) / 2.0;
   ymid = (ymax + ymin) / 2.0;

   /*
      Set up the supertriangle
      This is a triangle which encompasses all the sample points.
      The supertriangle coordinates are added to the end of the
      vertex list. The supertriangle is the first triangle in
      the triangle list.
   */
   pxyz[nv+0].x = xmid - 20 * dmax;
   pxyz[nv+0].y = ymid - dmax;
   pxyz[nv+0].z = 0.0;
   pxyz[nv+1].x = xmid;
   pxyz[nv+1].y = ymid + 20 * dmax;
   pxyz[nv+1].z = 0.0;
   pxyz[nv+2].x = xmid + 20 * dmax;
   pxyz[nv+2].y = ymid - dmax;
   pxyz[nv+2].z = 0.0;
   v[0].p1 = nv;
   v[0].p2 = nv+1;
   v[0].p3 = nv+2;
   complete[0] = false;
   *ntri = 1;

   /*
      Include each point one at a time into the existing mesh
   */
   for (i=0;i<nv;i++) {

      xp = pxyz[i].x;
      yp = pxyz[i].y;
      nedge = 0;

      /*
         Set up the edge buffer.
         If the point (xp,yp) lies inside the circumcircle then the
         three edges of that triangle are added to the edge buffer
         and that triangle is removed.
      */
      for (j=0;j<(*ntri);j++) {
         if (complete[j])
            continue;
         x1 = pxyz[v[j].p1].x;
         y1 = pxyz[v[j].p1].y;
         x2 = pxyz[v[j].p2].x;
         y2 = pxyz[v[j].p2].y;
         x3 = pxyz[v[j].p3].x;
         y3 = pxyz[v[j].p3].y;
         inside = CircumCircle(xp,yp,x1,y1,x2,y2,x3,y3,&xc,&yc,&r);
         if (xc < xp && ((xp-xc)*(xp-xc)) > r)
				complete[j] = true;
         if (inside) {
            /* Check that we haven't exceeded the edge list size */
            if (nedge+3 >= emax) {
               emax += 100;
               /*if ((edges = (IEDGE*)realloc(edges,emax*(long)sizeof(IEDGE))) == NULL) {
                  status = 3;
                  goto skip;
               }*/
               edges.resize(emax);
            }
            edges[nedge+0].p1 = v[j].p1;
            edges[nedge+0].p2 = v[j].p2;
            edges[nedge+1].p1 = v[j].p2;
            edges[nedge+1].p2 = v[j].p3;
            edges[nedge+2].p1 = v[j].p3;
            edges[nedge+2].p2 = v[j].p1;
            nedge += 3;
            v[j] = v[(*ntri)-1];
            complete[j] = complete[(*ntri)-1];
            (*ntri)--;
            j--;
         }
      }

      /*
         Tag multiple edges
         Note: if all triangles are specified anticlockwise then all
               interior edges are opposite pointing in direction.
      */
      for (j=0;j<nedge-1;j++) {
         for (k=j+1;k<nedge;k++) {
            if ((edges[j].p1 == edges[k].p2) && (edges[j].p2 == edges[k].p1)) {
               edges[j].p1 = -1;
               edges[j].p2 = -1;
               edges[k].p1 = -1;
               edges[k].p2 = -1;
            }
            /* Shouldn't need the following, see note above */
            if ((edges[j].p1 == edges[k].p1) && (edges[j].p2 == edges[k].p2)) {
               edges[j].p1 = -1;
               edges[j].p2 = -1;
               edges[k].p1 = -1;
               edges[k].p2 = -1;
            }
         }
      }

      /*
         Form new triangles for the current point
         Skipping over any tagged edges.
         All edges are arranged in clockwise order.
      */
      for (j=0;j<nedge;j++) {
         if (edges[j].p1 < 0 || edges[j].p2 < 0)
            continue;
         if ((*ntri) >= trimax) {
            status = 4;
            goto skip;
         }
         v[*ntri].p1 = edges[j].p1;
         v[*ntri].p2 = edges[j].p2;
         v[*ntri].p3 = i;
         complete[*ntri] = false;
         (*ntri)++;
      }
   }

   /*
      Remove triangles with supertriangle vertices
      These are triangles which have a vertex number greater than nv
   */
   for (i=0;i<(*ntri);i++) {
      if (v[i].p1 >= nv || v[i].p2 >= nv || v[i].p3 >= nv) {
         v[i] = v[(*ntri)-1];
         (*ntri)--;
         i--;
      }
   }

skip:
   //free(edges);
   free(complete);
   return(status);
}
	void Triangulate(std::vector<float2>& pxyz, std::vector<int3>& triangles) {
		int nv = (int)pxyz.size();
		std::vector<int2> edges;
		bool inside;
		int i, j, k;
		float xp, yp, x1, y1, x2, y2, x3, y3, xc, yc, r;
		float xmin, xmax, ymin, ymax, xmid, ymid;
		float dx, dy, dmax;
		std::vector<bool> complete;
		/*
		Find the maximum and minimum vertex bounds.
		This is to allow calculation of the bounding triangle
		*/
		xmin = pxyz[0].x;
		ymin = pxyz[0].y;
		xmax = xmin;
		ymax = ymin;
		for (i = 1; i < nv; i++) {
			if (pxyz[i].x < xmin) xmin = pxyz[i].x;
			if (pxyz[i].x > xmax) xmax = pxyz[i].x;
			if (pxyz[i].y < ymin) ymin = pxyz[i].y;
			if (pxyz[i].y > ymax) ymax = pxyz[i].y;
		}
		dx = xmax - xmin;
		dy = ymax - ymin;
		dmax = (dx > dy) ? dx : dy;
		xmid = (xmax + xmin) *0.5f;
		ymid = (ymax + ymin) *0.5f;
		float scale = 20.0f;
		triangles.clear();
		pxyz.push_back(float2(xmid - scale * dmax, ymid - dmax));
		pxyz.push_back(float2(xmid, ymid + scale * dmax));
		pxyz.push_back(float2(xmid + scale * dmax, ymid - dmax));
		triangles.push_back(int3( nv, nv + 1, nv + 2 ));
		complete.push_back(false);
		for (i = 0; i < (int)pxyz.size(); i++) {
			xp = pxyz[i].x;
			yp = pxyz[i].y;
			edges.clear();
			for (j = 0; j < (int)triangles.size(); j++) {
				if (complete[j])
					continue;
				x1 = pxyz[triangles[j].x].x;
				y1 = pxyz[triangles[j].x].y;
				x2 = pxyz[triangles[j].y].x;
				y2 = pxyz[triangles[j].y].y;
				x3 = pxyz[triangles[j].z].x;
				y3 = pxyz[triangles[j].z].y;
				inside = CircumCircle(xp, yp, x1, y1, x2, y2, x3, y3, xc, yc, r);
				if (xc + r < xp)complete[j] = true;
				if (inside) {
					edges.push_back(int2( triangles[j].x, triangles[j].y ));
					edges.push_back(int2(triangles[j].y, triangles[j].z ));
					edges.push_back(int2(triangles[j].z, triangles[j].x ));
					triangles.erase(triangles.begin() + j);
					complete.erase(complete.begin() + j);
					j--;
				}
			}
			/*
			Tag multiple edges
			Note: if all triangles are specified anticlockwise then all
			interior edges are opposite pointing in direction.
			*/
			for (j = 0; j < (int)edges.size() - 1; j++) {
				for (k = j + 1; k < (int)edges.size(); k++) {
					if ((edges[j].x == edges[k].y) && (edges[j].y == edges[k].x)) {
						edges[j] = int2(-1, -1 );
						edges[k] = int2(-1, -1 );
					}
					/* Shouldn't need the following, see note above */
					if ((edges[j].x == edges[k].x) && (edges[j].y == edges[k].y)) {
						edges[j] = int2(-1, -1 );
						edges[k] = int2(-1, -1 );
					}
				}
			}
			/*
			Form new triangles for the current point
			Skipping over any tagged edges.
			All edges are arranged in clockwise order.
			*/
			for (j = 0; j < (int)edges.size(); j++) {
				if (edges[j].x < 0 || edges[j].y < 0)
					continue;
				triangles.push_back(int3(edges[j].x, edges[j].y, i ));
				complete.push_back(false);
			}
		}
	}
int Triangulate(int nv, XYZ pxyz[], ITRIANGLE v[], int &ntri) {
	int *complete = NULL;
	IEDGE *edges = NULL; 
	IEDGE *p_EdgeTemp;
	int nedge = 0;
	int trimax, emax = 200;
	int status = 0;
	int inside;
	int i, j, k;
	double xp, yp, x1, y1, x2, y2, x3, y3, xc, yc, r;
	double xmin, xmax, ymin, ymax, xmid, ymid;
	double dx, dy, dmax; 

	/* Allocate memory for the completeness list, flag for each triangle */
	trimax = 4 * nv;
	complete = new int[trimax];
	/* Allocate memory for the edge list */
	edges = new IEDGE[emax];
	/*
	Find the maximum and minimum vertex bounds.
	This is to allow calculation of the bounding triangle
	*/
	xmin = pxyz[0].x;
	ymin = pxyz[0].y;
	xmax = xmin;
	ymax = ymin;
	for(i = 1; i < nv; i++){
		if (pxyz[i].x < xmin) xmin = pxyz[i].x;
		if (pxyz[i].x > xmax) xmax = pxyz[i].x;
		if (pxyz[i].y < ymin) ymin = pxyz[i].y;
		if (pxyz[i].y > ymax) ymax = pxyz[i].y;
	}
	dx = xmax - xmin;
	dy = ymax - ymin;
	dmax = (dx > dy) ? dx : dy;
	xmid = (xmax + xmin) / 2.0;
	ymid = (ymax + ymin) / 2.0;
	/*
	Set up the supertriangle
	his is a triangle which encompasses all the sample points.
	The supertriangle coordinates are added to the end of the
	vertex list. The supertriangle is the first triangle in
	the triangle list.
	*/
	pxyz[nv+0].x = xmid - 20 * dmax;
	pxyz[nv+0].y = ymid - dmax;
	pxyz[nv+1].x = xmid;
	pxyz[nv+1].y = ymid + 20 * dmax;
	pxyz[nv+2].x = xmid + 20 * dmax;
	pxyz[nv+2].y = ymid - dmax;
	v[0].p1 = nv;
	v[0].p2 = nv+1;
	v[0].p3 = nv+2;
	complete[0] = false;
	ntri = 1;
	/*
	Include each point one at a time into the existing mesh
	*/
	for(i = 0; i < nv; i++){
		xp = pxyz[i].x;
		yp = pxyz[i].y;
		nedge = 0;
		/*
		Set up the edge buffer.
		If the point (xp,yp) lies inside the circumcircle then the
		three edges of that triangle are added to the edge buffer
		and that triangle is removed.
		*/
		for(j = 0; j < ntri; j++){
			if(complete[j]) continue;
			x1 = pxyz[v[j].p1].x;
			y1 = pxyz[v[j].p1].y;
			x2 = pxyz[v[j].p2].x;
			y2 = pxyz[v[j].p2].y;
			x3 = pxyz[v[j].p3].x;
			y3 = pxyz[v[j].p3].y;
			inside = CircumCircle(xp, yp, x1, y1, x2, y2, x3, y3, xc, yc, r);
			if (xc + r < xp)
				// Suggested
				// if (xc + r + EPSILON < xp)
				complete[j] = true;
			if(inside){
				/* Check that we haven't exceeded the edge list size */
				if(nedge + 3 >= emax){
					emax += 100;
					p_EdgeTemp = new IEDGE[emax];
					for (int i = 0; i < nedge; i++) { // Fix by John Bowman
						p_EdgeTemp[i] = edges[i];   
					}
					delete []edges;
					edges = p_EdgeTemp;
				}
				edges[nedge+0].p1 = v[j].p1;
				edges[nedge+0].p2 = v[j].p2;
				edges[nedge+1].p1 = v[j].p2;
				edges[nedge+1].p2 = v[j].p3;
				edges[nedge+2].p1 = v[j].p3;
				edges[nedge+2].p2 = v[j].p1;
				nedge += 3;
				v[j] = v[ntri-1];
				complete[j] = complete[ntri-1];
				ntri--;
				j--;
			}
		}
		/*
		Tag multiple edges
		Note: if all triangles are specified anticlockwise then all
		interior edges are opposite pointing in direction.
		*/
		for(j = 0; j < nedge - 1; j++){
			for(k = j + 1; k < nedge; k++){
				if((edges[j].p1 == edges[k].p2) && (edges[j].p2 == edges[k].p1)){
					edges[j].p1 = -1;
					edges[j].p2 = -1;
					edges[k].p1 = -1;
					edges[k].p2 = -1;
				}
				/* Shouldn't need the following, see note above */
				if((edges[j].p1 == edges[k].p1) && (edges[j].p2 == edges[k].p2)){
					edges[j].p1 = -1;
					edges[j].p2 = -1;
					edges[k].p1 = -1;
					edges[k].p2 = -1;
				}
			}
		}
		/*
		Form new triangles for the current point
		Skipping over any tagged edges.
		All edges are arranged in clockwise order.
		*/
		for(j = 0; j < nedge; j++) {
			if(edges[j].p1 < 0 || edges[j].p2 < 0) continue;
			v[ntri].p1 = edges[j].p1;
			v[ntri].p2 = edges[j].p2;
			v[ntri].p3 = i;
			complete[ntri] = false;
			ntri++;
		}
	}
	/*
	Remove triangles with supertriangle vertices
	These are triangles which have a vertex number greater than nv
	*/
	for(i = 0; i < ntri; i++) {
		if(v[i].p1 >= nv || v[i].p2 >= nv || v[i].p3 >= nv) {
			v[i] = v[ntri-1];
			ntri--;
			i--;
		}
	}
	delete[] edges;
	delete[] complete;
	return 0;
}