Esempio n. 1
0
  List_ptr ME_Regions_F1(MEdge_ptr e) {
    MEdge_Adj_F1 *adj; 
    int i, j, nr, nf;
    List_ptr eregs;
    MFace_ptr eface;
    MRegion_ptr freg;
 
    adj = (MEdge_Adj_F1 *) e->adj;
    nf = List_Num_Entries(adj->efaces);
    eregs = List_New(nf);
   
    nr = 0;
    for (i = 0; i < nf; i++) {
      eface = List_Entry(adj->efaces,i);

      for (j = 0; j < 2; j++) {
	freg = MF_Region(eface,j);
	if (freg) {
          int inlist;
          inlist = List_Contains(eregs,freg);
          if (!inlist) {
            List_Add(eregs,freg);
            nr++;
          }
	}
      }
    }

    if (nr) 
      return eregs;
    else {
      List_Delete(eregs);
      return 0;
    }
  }
Esempio n. 2
0
MVertex_ptr ME_Collapse(MEdge_ptr e, MVertex_ptr vkeep_in, int topoflag,
                        List_ptr *deleted_entities) {
  MVertex_ptr vdel, vkeep, ev00, ev01, ev10, ev11, vert;
  MEdge_ptr edge, edge2, oldedges[3], nuedges[2];
  MFace_ptr face, face2, rface1, rface2;
  MRegion_ptr reg, reg2;
  List_ptr vedges, efaces, eregs, fedges, rfaces, fverts1, fverts2, vfaces;
  int idx1, idx2, idx3, dir, status, nfe, nrf, allfound, degenerate;
  int i, j, nfe2, nfv1, nfv2;

  status = 1;

  if (vkeep_in == NULL) {
    vdel = ME_Vertex(e,0);
    vkeep = ME_Vertex(e,1);
  }
  else {
    vkeep = vkeep_in;
    vdel = ME_OppVertex(e,vkeep);
  }

  int dimkeep, dimdel;
    
  dimkeep = MV_GEntDim(vkeep); /* Model entity dim of vertex to keep */
  dimdel = MV_GEntDim(vdel);   /* Model entity dim  of vertex to delete */
  
  if (topoflag == 1) {
    if (dimkeep == dimdel) {
      
      if (MV_GEntID(vkeep) != MV_GEntID(vdel))
        status = 0;                 /* cannot allow since it will cause 
                                       a dimensional reduction in mesh */
    }
    else if (dimdel < dimkeep) {
      
      if (vkeep_in == NULL) {
        /* If no preference was indicated on which vertex to retain,
	   we can collapse in the other direction */
        MVertex_ptr vtemp = vdel;
	vdel = vkeep;
	vkeep = vtemp;
      }
      else
        status = 0; /* can't reverse order or vertices and boundary of
                       mesh will get messed up if we go through as is */
    }
  }
  else if (vkeep_in == NULL) { 

    /* If no preference was indicated for the kept vertex and
       topological conformity with the underlying geometric model was
       not requested, we prefer to keep an external boundary vertex
       over an interior vertex or interior boundary vertex. This is
       because it is more likely that the external boundary vertex
       would have a boundary condition applied to it. If a preference
       was indicated, we just have to respect that. */

    int vdel_external = 0;

    /* Check if any edges connected to vdel have only one connected face */

    vedges = MV_Edges(vdel);    

    idx1 = 0;
    while ((edge = (MEdge_ptr) List_Next_Entry(vedges,&idx1))) {
      List_ptr efaces = ME_Faces(edge);
      int nef = List_Num_Entries(efaces);
      List_Delete(efaces);
      if (nef < 2) {
        vdel_external = 1;
        break;
      }
    }                                          
    
    List_Delete(vedges);

    /* check if any face connected to vdel has only one region
       connected to it */

    if (!vdel_external) {
      vfaces = MV_Faces(vdel);

      idx1 = 0;
      while ((face = (MFace_ptr) List_Next_Entry(vfaces,&idx1))) {
        List_ptr fregs = MF_Regions(face);
        int nfr = fregs ? List_Num_Entries(fregs) : 0;
        if (fregs) List_Delete(fregs);

        if (nfr == 1) {
          vdel_external = 0;
          break;
        }
      }

      List_Delete(vfaces);
    }

    if (vdel_external) {
      /* swap the vertices in the hope that vkeep is not also on an
         external boundary. Since we have to go through with the
         collapse anyway, there is no use of doing a detailed check
         for whether vkeep is also on an external boundary */

      MVertex_ptr vtemp = vdel;
      vdel = vkeep;
      vkeep = vtemp;
    }
    
  }


  if (status == 0)
    return NULL;   /* Cannot collapse due to constraints of topological
		   conformity with geometric model */

  *deleted_entities = List_New(10);

  /* Need to collect this in advance because the info gets messed up later */

  efaces = ME_Faces(e);
  eregs = ME_Regions(e);


  /* Replace vdel with vkeep in all edges connected to vdel */

  vedges = MV_Edges(vdel);
  idx1 = 0;
  while ((edge = List_Next_Entry(vedges,&idx1))) {
    ME_Replace_Vertex(edge,vdel,vkeep);
  }
  List_Delete(vedges);

  /* Remove edge 'e' from all faces connected to e */
  /* This part of the code is using some reliance on the internal
     implementation of MF_Edges. While unlikely, it _might_ break if
     the innards of MF_Edges are changed */

  
  idx1 = 0;
  while ((face = List_Next_Entry(efaces,&idx1))) {

    fedges = MF_Edges(face,1,0);
    nfe = List_Num_Entries(fedges);

    /* Find the edge before and after e in the face */

    oldedges[0] = oldedges[2] = NULL;
    for (i = 0; i < nfe; i++) {
      edge = List_Entry(fedges,i);
      if (edge == e) continue;

      dir = MF_EdgeDir_i(face,i);

      if (ME_Vertex(edge,dir) == vkeep)
	oldedges[0] = edge;
      else if (ME_Vertex(edge,!dir) == vkeep)
	oldedges[2] = edge;
    }
    oldedges[1] = e;

    nuedges[0] = oldedges[0];
    nuedges[1] = oldedges[2];


    /* Replace oldedges[0], oldedges[1] (=e), oldedges[2] with 
       oldedges[0], oldedges[2] since e is degenerate */

    MF_Replace_Edges(face,3,oldedges,2,nuedges);

    List_Delete(fedges);
  }



  /* Delete topologically degenerate regions */
  /* Defined as two faces of the regions having the same vertices */

  if (eregs) {
    idx1 = 0;
    while ((reg = List_Next_Entry(eregs,&idx1))) {

      rfaces = MR_Faces(reg);
      nrf = List_Num_Entries(rfaces);

      if (nrf == 4) {      
        List_ptr rverts = MR_Vertices(reg);
        if (List_Num_Entries(rverts) == 4) {
          MR_Delete(reg,0);    /* This is a tet - it will become degenerate */
        }
        List_Delete(rverts);
      }
      else {

	degenerate = 0;

	for (i = 0; i < nrf; i++) {

	  rface1 = List_Entry(rfaces,i);
	
	  fverts1 = MF_Vertices(rface1,1,0);
	  nfv1 = List_Num_Entries(fverts1);
		
	  for (j = i+1; j < nrf; j++) {
	  
	    rface2 = List_Entry(rfaces,j);

	    fverts2 = MF_Vertices(rface2,1,0);
	    nfv2 = List_Num_Entries(fverts2);
	  
	    if (nfv1 != nfv2) {
	      List_Delete(fverts2);
	      continue;             /* can't be exactly coincident */
	    }

	    allfound = 1;
	    idx2 = 0;
	    while ((vert = List_Next_Entry(fverts2,&idx2))) {
	      if (!List_Contains(fverts1,vert)) {
		allfound = 0;
		break;
	      }
	    }
	  
	    List_Delete(fverts2);
	  
	    if (allfound) {
	      degenerate = 1;
	      break;
	    }
	  
	  } /* for (j = i+1 ... */

	  List_Delete(fverts1);

	  if (degenerate) break;

	} /* for (i = 0; i < nrf;.... */

	if (degenerate) {
          List_Add(*deleted_entities,reg);
	  MR_Delete(reg,0);
        }

      } /* if (nrf == 4) .. else ... */

      List_Delete(rfaces);

    } /* while ((reg = ...)) */
  }


  /* Delete topologically degenerate faces */

  if (efaces) {
    idx1 = 0;
    while ((face = List_Next_Entry(efaces,&idx1))) {

      fedges = MF_Edges(face,1,0);

      if (List_Num_Entries(fedges) == 2) {

	/* Disconnect the regions from the face before deleting */

	List_ptr fregs = MF_Regions(face);
        if (fregs) {
          idx2 = 0;
          while ((reg = List_Next_Entry(fregs,&idx2)))
            MR_Rem_Face(reg,face);
          
          List_Delete(fregs);
        }

        List_Add(*deleted_entities,face);
	MF_Delete(face,0);
      }

      List_Delete(fedges);
    }
    List_Delete(efaces);
  }



  /* Now merge edges which have the same end vertices */
  /* Prefer to preserve edges on external boundaries over internal edges */

  vedges = MV_Edges(vkeep);
  idx1 = 0; 
  while ((edge = List_Next_Entry(vedges,&idx1))) {
    if (edge == e) continue;
    
    ev00 = ME_Vertex(edge,0);
    ev01 = ME_Vertex(edge,1);

    idx2 = 0;
    while ((edge2 = List_Next_Entry(vedges,&idx2))) {
      if (edge == e || edge == edge2) continue;

      ev10 = ME_Vertex(edge2,0);
      ev11 = ME_Vertex(edge2,1);

      if ((ev00 == ev10 && ev01 == ev11) ||
	  (ev00 == ev11 && ev10 == ev01)) {

        int external_edge, external_edge2;
        int edim = 4;
	    
        external_edge = 0;
        edim = ME_GEntDim(edge);
        if (edim == 1 || edim == 2 || edim == 4) { /* check if external edge */
          
          efaces = ME_Faces(edge);
          int nef = List_Num_Entries(efaces);
          if (nef == 1) {
            external_edge = 1;
          }
          else {
            idx3 = 0;
            while ((face = List_Next_Entry(efaces,&idx2))) {
              List_ptr fregs = MF_Regions(face);
              int nfr = fregs ? List_Num_Entries(fregs) : 0;
              if (fregs) List_Delete(fregs);
              if (nfr == 1) {
                external_edge = 1;
                break;
              }
            }
          }
          List_Delete(efaces);
          
        }
    
        external_edge2 = 0;
        edim = ME_GEntDim(edge2);
        if (edim == 1 || edim == 2 || edim == 4) { /* check if external edge */

          efaces = ME_Faces(edge2);
          int nef = List_Num_Entries(efaces);
          if (nef == 1) {
            external_edge2 = 1;
          }
          else {
            idx3 = 0;
            while ((face = List_Next_Entry(efaces,&idx2))) {
              List_ptr fregs = MF_Regions(face);
              int nfr = fregs ? List_Num_Entries(fregs) : 0;
              if (fregs) List_Delete(fregs);
              if (nfr == 1) {
                external_edge2 = 1;
                break;
              }
            }
          }
          List_Delete(efaces);
          
        }

        /* If edge2 is not external or both edges are external, go
           ahead and merge (edge2 will be deleted subject to
           topological checks if topoflag is 1) */

        if (!external_edge2 || (external_edge && external_edge2)) {
          MEs_Merge(edge,edge2,topoflag);	
          List_Rem(vedges,edge2);	
          List_Add(*deleted_entities,edge2);
          break;
        }
      }
    }
  }
  List_Delete(vedges);


  /* Merge faces with the same set of edges */
      
  vfaces = MV_Faces(vkeep);

  if (vfaces) {
    idx1 = 0;
    while ((face = List_Next_Entry(vfaces,&idx1))) {
    
      fedges = MF_Edges(face,1,0);
      nfe = List_Num_Entries(fedges);
    
      idx2 = 0;
      while ((face2 = List_Next_Entry(vfaces,&idx2))) {
	List_ptr fedges2;

	if (face2 == face) continue;

	fedges2 = MF_Edges(face2,1,0);
	nfe2 = List_Num_Entries(fedges2);

	if (nfe != nfe2) {
	  List_Delete(fedges2);
	  continue;
	}

	allfound = 1;

	for (i = 0; i < nfe2; i++) {
	  edge = List_Entry(fedges2,i);
	  if (!List_Contains(fedges,edge)) {
	    allfound = 0;
	    break;
	  }
	}
	List_Delete(fedges2);

	if (allfound) {

          List_ptr fregs = MF_Regions(face);
          int external_face = fregs ? (List_Num_Entries(fregs) == 1) : 0;
          if (fregs) List_Delete(fregs);

          List_ptr fregs2 = MF_Regions(face2);
          int external_face2 = fregs2 ? (List_Num_Entries(fregs2) == 1) : 0;
          if (fregs2) List_Delete(fregs2);

          /* Proceed with merge (which will delete face2) only if face2 is
             not an external face or both face and face2 are external */

          if (!external_face2 || (external_face && external_face2)) {
            MFs_Merge(face,face2,topoflag);	
            List_Rem(vfaces,face2);
            List_Add(*deleted_entities,face2);
            break;
          }

        }
	
      } /* while (face2 = List_Next_Entry(vfaces,... */

      List_Delete(fedges);

    } /* while (face = List_Next_Entry(vfaces,... */
    List_Delete(vfaces);
  }

  /* Now actually delete the collapse edge and the to-be-merged vertex */

  ME_Delete(e,0);
  List_Add(*deleted_entities,e);

  MV_Delete(vdel,0);
  List_Add(*deleted_entities,vdel);

  if (eregs) {
    idx1 = 0;
    while ((reg = List_Next_Entry(eregs,&idx1)))
      MR_Update_ElementType(reg);
    
    List_Delete(eregs);
  }

  return vkeep;
}
Esempio n. 3
0
  List_ptr ME_Faces_R1R2(MEdge_ptr e) {
    int idx, idx1, idx2, found;
    MFace_ptr face, lstface;
    MRegion_ptr reg;
    List_ptr rfaces, vfaces1, efaces, vregs0, vregs1, cmnregs=NULL;

    efaces = List_New(0);

    vregs0 = MV_Regions(e->vertex[0]);
    vregs1 = MV_Regions(e->vertex[1]);
    if (vregs0 || vregs1) {
      if (vregs0 && vregs1) {
	cmnregs = List_New(0);
	
	idx = 0;
	while ((reg = List_Next_Entry(vregs0,&idx))) {
	  if (List_Contains(vregs1,reg))
	    List_Add(cmnregs,reg);
	}
      }
      if (vregs0)
	List_Delete(vregs0);
      if (vregs1)
	List_Delete(vregs1);
      
      if (!List_Num_Entries(cmnregs)) {
	List_Delete(cmnregs);
	return NULL;
      }

      idx = 0;
      while ((reg = List_Next_Entry(cmnregs,&idx))) {
	rfaces = MR_Faces(reg);

	idx1 = 0;
	while ((face = List_Next_Entry(rfaces,&idx1))) {
	  if (MF_UsesEntity(face,(MEntity_ptr) e,MEDGE)) {
	    
	    idx2 = 0; found = 0;
	    while ((lstface = List_Next_Entry(efaces,&idx2))) {
	      if (MFs_AreSame(face,lstface)) {
		found = 1;
		break;
	      }
	    }

	    if (!found)
	      List_Add(efaces,face);
	  }
	}
	
	List_Delete(rfaces);
      }
      List_Delete(cmnregs);
    }
    else { /* Must be only faces are connected to edge */

      vfaces1 = MV_Faces(e->vertex[0]);

      if (vfaces1) {
	idx = 0;
	while ((face = List_Next_Entry(vfaces1,&idx))) {
	  if (MF_UsesEntity(face,e->vertex[1],MVERTEX))
	    List_Add(efaces,face);
	}
      }
      
    }

    if (List_Num_Entries(efaces))
      return efaces;
    else {
      List_Delete(efaces);
      return NULL;
    }
  }
Esempio n. 4
0
  double MFs_DihedralAngle(MFace_ptr face1, MFace_ptr face2, MEdge_ptr edge) {
    int i, fnd, nfe1, fedir, nfv1, nfv2;
    double fxyz[MAXPV2][3], vec1[3], vec2[3];
    double normal1[3], normal2[3], dp, mid[3];
    MVertex_ptr fv, ev0, ev1;
    List_ptr fedges1, fedges2, fverts1, fverts2;
    

    if (!edge) {
      fedges1 = MF_Edges(face1,1,0);      
      nfe1 = List_Num_Entries(fedges1);

      fedges2 = MF_Edges(face2,1,0);

      for (i = 0, fnd = 0; i < nfe1 && !fnd; i++) {
	edge = List_Entry(fedges1,i);
	if (List_Contains(fedges2,edge)) 
	  fnd = 1;	
      }

      List_Delete(fedges1);
      List_Delete(fedges2);

      if (!fnd) {
	MSTK_Report("MFs_DihedralAngle","Faces do not share common edge",MSTK_ERROR);
	return 0.0;
      }
    }


    /* For non-convex faces, picking the three points from which to
       calculate the normal is an issue. We will pick the two points
       of the common edge and the geometric center of the face.

       THIS MAY NOT WORK IF THE GEOMETRIC CENTER IS OUTSIDE THE FACE

       Eventually we will have to find a point in the face such that
       all triangles formed by connecting the point and each of the
       edges have consistent normals */


    ev0 = ME_Vertex(edge,0); 
    ev1 = ME_Vertex(edge,1); 

    /* Normal of face 1 */

    fedir = MF_EdgeDir(face1,edge);

    fverts1 = MF_Vertices(face1,fedir,ev0);
    nfv1 = List_Num_Entries(fverts1);

    for (i = 0; i < nfv1; i++) {
      fv = List_Entry(fverts1,i); 
      MV_Coords(fv,fxyz[i]);
    }
    List_Delete(fverts1);

    MSTK_VDiff3(fxyz[1],fxyz[0],vec1);

    if (nfv1 == 3) { /* Triangles - always convex */
      MSTK_VDiff3(fxyz[2],fxyz[0],vec2);
    }
    else { /* Others - can be non-convex */

      /* use geometric center as third point */
      
      mid[0] = mid[1] = mid[2] = 0.0;
      for (i = 0; i < nfv1; i++) {
	mid[0] += fxyz[i][0];
	mid[1] += fxyz[i][1];
	mid[2] += fxyz[i][2];
      }
      mid[0] /= nfv1; mid[1] /= nfv1; mid[2] /= nfv1;

      MSTK_VDiff3(mid,fxyz[0],vec2);
    }
    
    MSTK_VCross3(vec1,vec2,normal1);
    MSTK_VNormalize3(normal1);
    

    /* Normal of face 2 */

    fedir = MF_EdgeDir(face2,edge);

    fverts2 = MF_Vertices(face2,!fedir,ev1);
    nfv2 = List_Num_Entries(fverts2);

    for (i = 0; i < nfv2; i++) {
      fv = List_Entry(fverts2,i); 
      MV_Coords(fv,fxyz[i]);
    }
    List_Delete(fverts2);

    MSTK_VDiff3(fxyz[1],fxyz[0],vec1);

    if (nfv1 == 3) { /* Triangles - always convex */
      MSTK_VDiff3(fxyz[2],fxyz[0],vec2);
    }
    else { /* Others - can be non-convex */

      /* use geometric center as third point */
      
      mid[0] = mid[1] = mid[2] = 0.0;
      for (i = 0; i < nfv2; i++) {
	mid[0] += fxyz[i][0];
	mid[1] += fxyz[i][1];
	mid[2] += fxyz[i][2];
      }
      mid[0] /= nfv2; mid[1] /= nfv2; mid[2] /= nfv2;

      MSTK_VDiff3(mid,fxyz[0],vec2);
    }
    
    MSTK_VCross3(vec1,vec2,normal2);
    MSTK_VNormalize3(normal2);

    /* We have to negate the second normal. Otherwise we will get a
       dihedral angle of 0 for two faces in a plane when it should
       be 180 */

    MSTK_VNeg3(normal2);
    

    /* Angle between normals */
    dp = MSTK_VDot3(normal1,normal2);
    
    return dp;
  }
Esempio n. 5
0
  int MESH_CheckTopo(Mesh_ptr mesh) {
    int valid = 1;
    char mesg[256], funcname[32] = "MESH_CheckTopo";
    int idx1, idx2, idx3, idx4;
    MVertex_ptr mv;
    MEdge_ptr me, ve, fe, re;
    MFace_ptr mf, vf, ef, rf;
    MRegion_ptr mr, vr, er, fr;
    int found, done;
    int dir;
    int i, j, k;
    int nfe;
    int vid, eid, fid, rid;
    int gvid, geid, gfid, grid;
    int gvdim, gedim, gfdim, grdim;
    int maxiter = 1000;
    List_ptr vedges, vfaces, vregions;
    List_ptr efaces;
    List_ptr fverts, fedges, fregs, fregs1;
    List_ptr rverts, redges, rfaces;



    /*****************************************************************/
    /* Vertices                                                      */
    /*****************************************************************/    

    
    /* Check that edges connected to vertices reference the vertices */
    /* Also check that the classification of the vertex is consistent
       with respect to the edge */

    int first_unknown_classfn = 1;
    idx1 = 0;
    while ((mv = MESH_Next_Vertex(mesh,&idx1))) {

#ifdef MSTK_HAVE_MPI
      if (MV_PType(mv) == PGHOST) continue;
#endif      

      vid = MV_ID(mv);
      gvdim = MV_GEntDim(mv);
      gvid  = MV_GEntID(mv);

      if (gvdim == 4 && first_unknown_classfn) {
        sprintf(mesg, "Vertex %-d - classification unknown\n", vid);
        MSTK_Report(funcname, mesg, MSTK_WARN);
        first_unknown_classfn = 0;
      }
      vedges = MV_Edges(mv);
      if (!vedges) {
        sprintf(mesg,"Vertex %-d does not have any connected edges\n",vid);
        MSTK_Report(funcname,mesg,MSTK_WARN);
        continue;
      }

      idx2 = 0;
      while ((ve = List_Next_Entry(vedges,&idx2))) {
	
	eid = ME_ID(ve);
	     
	if (ME_Vertex(ve,0) != mv && ME_Vertex(ve,1) != mv) {
	  sprintf(mesg,"Vertex %-d connected to edge %-d but edge does not use vertex",vid,eid);
	  MSTK_Report(funcname,mesg,MSTK_ERROR);
	  valid = 0;
	}

      }


      if (gvdim == 1) {

	/* If vertex is classified on a model edge, then it should be
	   connected to two and only two edges that are classified on
	   the same model edge */

	int ne = 0;
	idx2 = 0;
	while ((ve = List_Next_Entry(vedges,&idx2))) {	  
	  gedim = ME_GEntDim(ve);
	  geid  = ME_GEntID(ve);	  
	  if (gedim == 1 && geid == gvid) ne++;
	}
	  
	if (ne != 2) {
	  sprintf(mesg,"Vertex %-d classified on model edge %-d but it is not \n connected to two edges classified on this model edge",vid,gvid);
	  MSTK_Report(funcname,mesg,MSTK_WARN);
	}
      }

      List_Delete(vedges);



      if (gvdim == 2) {
	MEdge_ptr e0, ecur, enxt;
	MFace_ptr fcur;
        int flipped = 0;

	/* If vertex is classified on a model face, then we should be
	   able to find a ring of faces classified on that model
	   face */

	vfaces = MV_Faces(mv);

	found = 0;
	idx2 = 0;
	while ((vf = List_Next_Entry(vfaces,&idx2))) {
	  if (MF_GEntDim(vf) == 2) {
	    found = 1;
	    break;
	  }
	}
        List_Delete(vfaces);

	if (!found) {
	  sprintf(mesg,"Vertex %-d classified on model face %-d but could not \n find connected face classified on this model face",vid,gvid);
	  MSTK_Report(funcname,mesg,MSTK_WARN);
	  valid = 0;
	}

	fcur = vf;

	fedges = MF_Edges(fcur,1,mv);
	nfe = List_Num_Entries(fedges);
	e0 = List_Entry(fedges,0);
	ecur = e0;
	enxt = List_Entry(fedges,nfe-1);
	List_Delete(fedges);
	
	done = 0; i = 0;
	while (!done) {
	  ecur = enxt;
	  efaces = ME_Faces(ecur);
	  found = 0;
	  idx3 = 0;
	  while ((ef = List_Next_Entry(efaces,&idx3))) {
	    if (ef != fcur && MF_GEntDim(ef) == 2 && MF_GEntID(ef) == gvid) {
	      fcur = ef;
	      found = 1;
	      break;
	    }
	  }
	  List_Delete(efaces);

	  if (!found) {
	    sprintf(mesg,"Could not find next boundary face connected to vertex %-d",vid);
	    MSTK_Report(funcname,mesg,MSTK_WARN);
	    valid = 0;
	    break;
	  }
	   
	  fedges = MF_Edges(fcur,1,mv);
	  nfe = List_Num_Entries(fedges);

          if (List_Entry(fedges,0) == ecur)
            enxt = List_Entry(fedges,nfe-1);
          else if (List_Entry(fedges,nfe-1) == ecur) {
            enxt = List_Entry(fedges,0);
            flipped = 1;
          }
          else {
            sprintf(mesg,"Could not find next edge while traversing around vertex %-d on model face %-d",vid,gvid);
            MSTK_Report(funcname,mesg,MSTK_ERROR);
          }

	  List_Delete(fedges);

	  if (enxt == e0)
	    done = 1;

	  if (++i > maxiter)
	    break;
	}

	if (!done) {
	  sprintf(mesg,"Vertex %-d classified on model face %-d but could not  find ring of faces classified on this model face",vid,gvid);
	  MSTK_Report(funcname,mesg,MSTK_WARN);
	}

        if (done && flipped) {
          List_ptr fregs = MF_Regions(fcur);
          if (List_Num_Entries(fregs) < 2) {
            sprintf(mesg,"Inconsistent orientations of boundary faces around vertex %-d",vid);
            MSTK_Report(funcname,mesg,MSTK_WARN);
          }
          if (fregs) List_Delete(fregs);
        }
      }


    } /* while ((mv = MESH_Next_Vertex(mesh,&idx1))) */




    /*****************************************************************/
    /* Edges                                                      */
    /*****************************************************************/    

    first_unknown_classfn = 1;
    idx1 = 0;
    while ((me = MESH_Next_Edge(mesh,&idx1))) {

#ifdef MSTK_HAVE_MPI
      if (ME_PType(me) == PGHOST) continue;
#endif

      eid = ME_ID(me);
      gedim = ME_GEntDim(me);
      geid = ME_GEntID(me);

      if (gedim == 4 && first_unknown_classfn) {
        sprintf(mesg, "Edge %-d - unknown classification", eid);
        MSTK_Report(funcname, mesg, MSTK_WARN);
        first_unknown_classfn = 0;
      }

      if (ME_Vertex(me,0) == ME_Vertex(me,1)) {
        sprintf(mesg,"Edge %-d has repeated vertices",eid);
        MSTK_Report(funcname,mesg,MSTK_ERROR);
      }

      for (i = 0; i < 2; i++) {
	MVertex_ptr ev = ME_Vertex(me,i);

	vid = MV_ID(ev);
	gvid = MV_GEntID(ev);
	gvdim = MV_GEntDim(ev);

        if (gvdim != 4 && gvdim != 4) {  /* vertex and edge classifn is known */
          if (gedim < gvdim) {
            sprintf(mesg,"Edge %-d classified on lower dimensional entity than  connected vertex %-d",eid,vid);
            MSTK_Report(funcname,mesg,MSTK_WARN);
            valid = 0;
          }
          else if (gedim == gvdim && geid != gvid) {
            sprintf(mesg,"Edge %-d and its vertex %-d classified on different entities of the same dimension",eid,vid);
            MSTK_Report(funcname,mesg,MSTK_WARN);
            valid = 0;
          }
        }

	vedges = MV_Edges(ev);
	if (!List_Contains(vedges,me)) {
	  sprintf(mesg,"Edge %-d sees vertex %-d but not vice versa",eid,vid);
	  MSTK_Report(funcname,mesg,MSTK_ERROR);
	  valid = 0;
	}
	List_Delete(vedges);

	if (gedim == 2) {
	  MFace_ptr ebf[2], fcur, fnxt;
	  MRegion_ptr rcur;
	  int nf, nfr;
	  List_ptr eregs;

	  /* Edge is classified on model face - it should be connected
	     to two and only two faces also classified on this model
	     face */

	  ebf[0] = ebf[1] = NULL;
	  nf = 0;
	  efaces = ME_Faces(me);
	  idx2 = 0;
	  while ((ef = List_Next_Entry(efaces,&idx2))) {
	    fid = MF_ID(ef);

	    if (MF_GEntDim(ef) == 2) {
	      nf++;
	      if (gedim == 2 && MF_GEntID(ef) != geid) {
		sprintf(mesg,"Face %-d connected to edge %-d classified on different model face",fid,eid);
		MSTK_Report(funcname,mesg,MSTK_WARN);
		valid = 0;
	      }

	      if (ebf[0] == NULL)
		ebf[0] = ef;
	      else
		ebf[1] = ef;
	    }
	  }
	  List_Delete(efaces);

	  if (nf != 2) {
	    sprintf(mesg,"Boundary edge %-d is not connected to exactly two\n  faces classified on the boundary",eid);
	    MSTK_Report(funcname,mesg,MSTK_ERROR);
	    valid = 0;
	  }


	  eregs = ME_Regions(me);
	  if (!eregs) 
	    continue;
	  else
	    List_Delete(eregs);

	  /* Can we go from f0 to f1 in one or two dirs? */

	  fcur = ebf[0];
          fnxt = NULL;
	  fregs = MF_Regions(fcur);
	  if (!fregs) {
	    fid = MF_ID(fcur);
	    sprintf(mesg,"Edge %-d connected to regions but face %-d is not",eid,fid);
	    MSTK_Report(funcname,mesg,MSTK_ERROR);
	    valid = 0;
	  }

	  nfr = List_Num_Entries(fregs);
	  for (i = 0; i < nfr; i++) {
	    rcur = List_Entry(fregs,i);
	  
	    rfaces = MR_Faces(rcur);
	    idx3 = 0;
	    found = 0;
	    while ((rf = List_Next_Entry(rfaces,&idx3))) {
	      if (rf != fcur && MF_UsesEntity(rf,me,1)) {
		found = 1;
		fnxt = rf;
		break;
	      }
	    }
	    List_Delete(rfaces);
	    
	    if (!found) {
	      rid = MR_ID(rcur);
	      sprintf(mesg,"Could not find second face in region %-d using edge %-d",rid,eid);
	    }
	    
	    
	    done = 0; j = 0;
	    while (!done) {
	      fcur = fnxt;
	      fid = MF_ID(fcur);
	      
	      if (fnxt == ebf[1]) {
		done = 1;
		break;
	      }
	      
	      fregs1 = MF_Regions(fcur);
	      idx3 = 0;
	      while ((fr = List_Next_Entry(fregs1,&idx3))) {
		if (fr != rcur) {
		  rcur = fr;
		  found = 1;
		  break;
		}
	      }
	      List_Delete(fregs1);

	      if (!found) {
		sprintf(mesg,"Could not find next region around edge %-d",eid);
		MSTK_Report(funcname,mesg,MSTK_ERROR);
		valid = 0;
		break;
	      }
	      
	      
	      rfaces = MR_Faces(rcur);
	      idx3 = 0;
	      found = 0;
	      while ((rf = List_Next_Entry(rfaces,&idx3))) {
		if (rf != fcur && MF_UsesEntity(rf,me,1)) {
		  found = 1;
		  fnxt = rf;
		  break;
		}
	      }
	      List_Delete(rfaces);
	      
	      if (!found) {
		rid = MR_ID(rcur);
		sprintf(mesg,"Could not find second face in region %-d using edge %-d",rid,eid);
	      }
	      
	      if (++j > maxiter)
		break;
	    } /* while (!done) */

	    if (!done) {
	      sprintf(mesg,"Could not traverse around edge %-d from face %-d to face %-d",eid,MF_ID(ebf[0]),MF_ID(ebf[1]));
	      MSTK_Report(funcname,mesg,MSTK_ERROR);
	      valid = 0;
	    }
	  } /* for (i = 0; i < nfr; i++) */
          List_Delete(fregs);

	} /* if (geid == 2) */

      } /* for (i = 0; i < 2; i++) */

    } /* while ((me = MESH_Next_Edge(mesh,&idx1))) */



    /*****************************************************************/
    /* Faces                                                      */
    /*****************************************************************/    

    first_unknown_classfn = 1;
    idx1 = 0;
    while ((mf = MESH_Next_Face(mesh,&idx1))) {

#ifdef MSTK_HAVE_MPI
      if (MF_PType(mf) == PGHOST) continue;
#endif

      fid = MF_ID(mf);
      gfid = MF_GEntID(mf);
      gfdim = MF_GEntDim(mf);

      if (gfdim == 4 && first_unknown_classfn) {
        sprintf(mesg, "Face %-d - unknown classification", fid);
        MSTK_Report(funcname, mesg, MSTK_WARN);
        first_unknown_classfn = 0;
      }

      fedges = MF_Edges(mf,1,0);

      if (List_Num_Entries(fedges) < 3) {
        sprintf(mesg,"Face %-d has less than 3 edges",fid);
        MSTK_Report(funcname,mesg,MSTK_ERROR);
      }

      idx2 = 0;
      while ((fe = List_Next_Entry(fedges,&idx2))) {
	eid = ME_ID(fe);
	geid = ME_GEntID(fe);
	gedim = ME_GEntDim(fe);

        if (gedim != 4 && gfdim != 4) {  /* Edge, Face classfn is known */
          if (gfdim < gedim) {
            sprintf(mesg,"Face %-d classified on lower order entity than edge %-d",fid,ME_ID(fe));
            MSTK_Report(funcname,mesg,MSTK_WARN);
            valid = 0;
          }
          else if (gedim == gfdim && geid != gfid) {
            sprintf(mesg,"Face %-d and edge %-d classified on different\n entities of the same dimension",fid,eid);
            MSTK_Report(funcname,mesg,MSTK_WARN);
          }
        }

	efaces = ME_Faces(fe);
	if (!List_Contains(efaces,mf)) {
	  sprintf(mesg,"Face %-d refers to edge %-d but not vice versa",fid,ME_ID(fe));
	  MSTK_Report(funcname,mesg,MSTK_ERROR);
	  valid = 0;
	}
	List_Delete(efaces);
      }
      List_Delete(fedges);
      

      fregs = MF_Regions(mf);

      if (gfdim == 3) {
	if (!fregs || List_Num_Entries(fregs) != 2) {
	  sprintf(mesg,"Interior face %-d does not have two connected regions",fid);
	  MSTK_Report(funcname,mesg,MSTK_ERROR);
	  valid = 0;
	}
      }


      if (fregs) {
	if (List_Num_Entries(fregs) == 2) {
	  if (MR_FaceDir(List_Entry(fregs,0),mf) == MR_FaceDir(List_Entry(fregs,1),mf)) {
	    sprintf(mesg,"Both regions using face %-d in the same sense",fid);
	    MSTK_Report(funcname,mesg,MSTK_ERROR);
	    valid = 0;
	  }
	}
	List_Delete(fregs);      
      }


    } /* while ((mf = MESH_Next_Face(mesh,&idx1))) */



    /*****************************************************************/
    /* Regions                                                      */
    /*****************************************************************/    

    idx1 = 0;
    while ((mr = MESH_Next_Region(mesh,&idx1))) {
      
#ifdef MSTK_HAVE_MPI
      if (MR_PType(mr) == PGHOST) continue;
#endif

      rid = MR_ID(mr);
      grid = MR_GEntID(mr);

      rfaces = MR_Faces(mr);
      int nrf = List_Num_Entries(rfaces);

      if (nrf < 4) {
        sprintf(mesg,"Region %-d has less than 4 faces",rid);
        MSTK_Report(funcname,mesg,MSTK_ERROR);
      }

      /* Check that face to region and region to face links are consistent
         with each other */
      int *rfdirs = (int *) malloc(nrf*sizeof(int));

      i = 0;
      idx2 = 0;
      while ((rf = List_Next_Entry(rfaces,&idx2))) {
	rfdirs[i] = MR_FaceDir_i(mr,i);
	if (mr != MF_Region(rf,!rfdirs[i])) {
	  sprintf(mesg,"Region %-d to face %-d dir inconsistent with \n face to region dir",rid,MF_ID(rf));
	  MSTK_Report(funcname,mesg,MSTK_ERROR);
	  valid = 0;
	}

        i++;
      }


      /* Check that faces of a region have consistent orientation in
         the region with respect to each other */
      
      for (i = 0; i < nrf; i++) {
        MFace_ptr rf, rf2;

        rf = List_Entry(rfaces,i);

        fedges = MF_Edges(rf,1,0);
        nfe = List_Num_Entries(fedges);
        
        for (j = 0; j < nfe; j++) {
          fe = List_Entry(fedges,j);
          int fedir = MF_EdgeDir_i(rf,j);
          
          /* Find adjacent face in the region */
          found = 0;
          for (k = 0; k < nrf; k++) {
            rf2 = List_Entry(rfaces,k);
            if (rf != rf2 && MF_UsesEntity(rf2,fe,MEDGE)) {
              found = 1;
              break;
            }
          }

          if (!found) {
            sprintf(mesg,"Cannot find another face in region %-d sharing edge %-d (ID = %-d) of face with ID = %-d",MR_ID(mr),j,ME_ID(fe),MF_ID(rf));
            MSTK_Report(funcname,mesg,MSTK_ERROR);
            valid = 0;
          }
          
          int fedir_adj = MF_EdgeDir(rf2,fe);
          
          /* If the two faces use the edge in opposite directions then
             the region should use the faces in the same direction and
             vice versa */

          if (((fedir_adj == fedir) && (rfdirs[i] == rfdirs[k])) ||
              ((fedir_adj != fedir) && (rfdirs[i] != rfdirs[k]))) {
            sprintf(mesg,"Region %-d faces are inconsistently oriented",MR_ID(mr));
            MSTK_Report(funcname,mesg,MSTK_ERROR);
            valid = 0;
          }
        }
        List_Delete(fedges);
           
      }
      List_Delete(rfaces);
      free(rfdirs);
    }

    return valid;

  } /* int MESH_CheckTopo */
Esempio n. 6
0
MVertex_ptr ME_Split_SimplexMesh(MEdge_ptr esplit, double *splitxyz) {
  int i, j, k, rfdir, ntets=0, ntris=0, *fdim, *fid, *rid=NULL, found;
  MVertex_ptr vsplit, ev[2], (*tetverts)[4]=NULL, (*triverts)[3]=NULL, fv;
  MVertex_ptr fvarr[3], rvarr[4];
  MFace_ptr f;
  MRegion_ptr r;
  List_ptr etets, rfaces, etris, fverts;
  Mesh_ptr mesh = ME_Mesh(esplit);

  ev[0] = ME_Vertex(esplit,0);
  ev[1] = ME_Vertex(esplit,1);

  etets = ME_Regions(esplit);
  if (etets) {
    ntets = List_Num_Entries(etets);   
    tetverts = (MVertex_ptr (*)[4]) malloc(ntets*sizeof(MVertex_ptr [4]));
    rid = (int *) malloc(ntets*sizeof(int));
  }

  for (i = 0; i < ntets; i++) {

    r = List_Entry(etets,i);

    rfaces = MR_Faces(r);

    /* Find a tet face that uses ev[0] but not ev[1] */

    found = 0;
    for (j = 0; !found && j < 4; j++) {
      f = List_Entry(rfaces,j);

      fverts = MF_Vertices(f,1,0);
      if (List_Contains(fverts,ev[0]) &&
          !List_Contains(fverts,ev[1])) {

        found = 1;
        
        /* Get the two vertices (a,b) of this face excluding ev[0] in
           such an order that ev[0],a,b,ev[1] will form a valid
           tet. This requires checking whether the face points into or
           out of this tet (look at rfdir) */

        rfdir = MR_FaceDir_i(r,j);
        for (k = 0; k < 3; k++) {
          fv = List_Entry(fverts,k);
          if (fv == ev[0]) {
            tetverts[i][0] = ev[0];
            tetverts[i][1] = rfdir ? List_Entry(fverts,(k+2)%3) : List_Entry(fverts,(k+1)%3);
            tetverts[i][2] = rfdir ? List_Entry(fverts,(k+1)%3) : List_Entry(fverts,(k+2)%3);
            tetverts[i][3] = ev[1];
          }
        }
      }
      List_Delete(fverts);

      if (found) break;
    }

    List_Delete(rfaces);
  }

  /* Now that we finished collecting info about the connected tets we
     can delete them */

  if (etets) {
    for (i = 0; i < ntets; i++)
      MR_Delete(List_Entry(etets,i),0);

    List_Delete(etets);
  }

  /* Now get the triangular face connected to the edge. For each
     triangular face, record the vertex opposite to edge esplit and
     delete the triangular face */

  etris = ME_Faces(esplit);
  if (etris) {
    ntris = List_Num_Entries(etris);
    triverts = (MVertex_ptr (*)[3]) malloc(ntris*sizeof(MVertex_ptr[3]));
    fdim = (int *) malloc(ntris*sizeof(int));
    fid = (int *) malloc(ntris*sizeof(int));
  }
  
  for (i = 0; i < ntris; i++) {
    f = List_Entry(etris,i);

    fverts = MF_Vertices(f,1,0);
    for (j = 0; j < 3; j++) {
      fv = List_Entry(fverts,j);
      if (fv != ev[0] && fv != ev[1]) {        
        triverts[i][0] = fv;
        triverts[i][1] = List_Entry(fverts,(j+1)%3);
        triverts[i][2] = List_Entry(fverts,(j+2)%3);
        fdim[i] = MF_GEntDim(f);
        fid[i] = MF_GEntID(f);
        break;
      }
    }
    List_Delete(fverts);

    MF_Delete(f,0);
  }

  if (etris) List_Delete(etris);

  /* Now split the edge itself */

  vsplit = ME_Split(esplit, splitxyz);

  /* Now for each tri face that we deleted, create two tri faces that
     incorporate the split vertex, one of the split edge vertices and
     opposite vertex */

  for (i = 0; i < ntris; i++) {

    /* First triangle */

    fvarr[0] = triverts[i][0]; 
    fvarr[1] = triverts[i][1];
    fvarr[2] = vsplit;

    f = MF_New(mesh);
    MF_Set_Vertices(f,3,fvarr);
    MF_Set_GEntDim(f,fdim[i]);
    MF_Set_GEntID(f,fid[i]);
    
    /* Second triangle */

    fvarr[0] = triverts[i][0];
    fvarr[1] = vsplit;
    fvarr[2] = triverts[i][2];

    f = MF_New(mesh);
    MF_Set_Vertices(f,3,fvarr);
    MF_Set_GEntDim(f,fdim[i]);
    MF_Set_GEntID(f,fid[i]);
  }
  if (ntris) {
    free(triverts);
    free(fdim);
    free(fid);
  }

  /* Now for each tet that we deleted, create two tets (these will use
     the split faces that are already created */

  for (i = 0; i < ntets; i++) {
    rvarr[0] = vsplit;
    rvarr[1] = tetverts[i][2];
    rvarr[2] = tetverts[i][1];
    rvarr[3] = tetverts[i][0];

    r = MR_New(mesh);
    MR_Set_Vertices(r,4,rvarr,0,NULL);
    MR_Set_GEntID(r,rid[i]);

    rvarr[0] = vsplit;
    rvarr[1] = tetverts[i][1];
    rvarr[2] = tetverts[i][2];
    rvarr[3] = tetverts[i][3];

    r = MR_New(mesh);
    MR_Set_Vertices(r,4,rvarr,0,NULL);
    MR_Set_GEntID(r,rid[i]);
  }

  if (ntets) {
    free(tetverts);
    free(rid);
  }

  return vsplit;
}
Esempio n. 7
0
MFace_ptr MR_Split_with_EdgeLoop(MRegion_ptr rsplit, int nfe, MEdge_ptr *fedges) {
  Mesh_ptr mesh;
  int i, j, idx1, idx2, fedirs[MAXPV2], rfdir_adj, curdir, edir, edir_adj;;
  int gid, mkid, nrf1, nrf2, rfdirs1[MAXPF3], rfdirs2[MAXPF3];
  MEdge_ptr fe;
  MFace_ptr fnew, eface, rfarray1[MAXPF3], rfarray2[MAXPF3], curface;
  MRegion_ptr rnew[2];
  List_ptr felist, efaces;

#ifdef DEBUG
  List_ptr redges;
#endif

  gid = MR_GEntID(rsplit);

#ifdef DEBUG
  /* check to make sure we got meaningful input */
  redges = MR_Edges(rsplit);
  for (i = 0; i < nfe; i++)
    if (!List_Contains(redges,fedges[i]))
      MSTK_Report("MR_Split","Input edges are not part of the region boundary",
                  MSTK_FATAL);
  List_Delete(redges);
#endif

  mesh = MR_Mesh(rsplit);


  /* Fix a set of directions for the edges */

  fedirs[0] = 1;
  for (i = 1; i < nfe; i++) {
    MVertex_ptr vprev, v0, v1;

    vprev = ME_Vertex(fedges[i-1],fedirs[i-1]);
    v0 = ME_Vertex(fedges[i],0);
    v1 = ME_Vertex(fedges[i],1);
    if (vprev == v0)
      fedirs[i] = 1;
    else if (vprev == v1)
      fedirs[i] = 0;
    else
      MSTK_Report("MR_Split","Input edges do not form a loop as listed",
                  MSTK_FATAL);
  }

  /* Create the splitting face */

  fnew = MF_New(mesh);
  MF_Set_GEntDim(fnew,3);
  MF_Set_GEntID(fnew,gid);
  MF_Set_Edges(fnew,nfe,fedges,fedirs);


  /* Collect info for the first region */

  List_ptr processed_faces = List_New(0);

  rfarray1[0] = fnew;
  rfdirs1[0] = 1; 
  nrf1 = 1;
  List_Add(processed_faces,rfarray1[0]);

  i = 0;
  while (i < nrf1) {
    curface = rfarray1[i];
    curdir = rfdirs1[i];
    i++;

    /* Get adjacent faces in region of current face and if they are
       not already in the new region face list (not marked), then add
       them */
 
    felist = MF_Edges(curface,1,0);
    idx1 = 0; j = 0;
    while ((fe = List_Next_Entry(felist,&idx1))) {
      edir = MF_EdgeDir_i(curface,j);      
      j++;

      efaces = ME_Faces(fe);
      if (curface != fnew && List_Contains(efaces,fnew)) {
        /* we have come back to the starting or splitting face - don't
           go across this edge */
        List_Delete(efaces);
        continue;
      }

      /* Add an adjacent unprocessed face of the region to the list of 
       faces for the new region */
      idx2 = 0; 
      while ((eface = List_Next_Entry(efaces,&idx2))) { 
        if (eface == curface) continue;
        if (List_Contains(processed_faces,eface)) continue;
        if (!MR_UsesEntity(rsplit,eface,MFACE)) continue; /* does not belong to region */

        edir_adj = MF_EdgeDir(eface,fe);
        rfdir_adj = MR_FaceDir(rsplit,eface);

        /* add adjacent face based on the check that if two adjacent faces of
           region are used by the region in the same sense, then their common
           edge should be used by the two faces in opposite senses (or the opposite
           of both the conditions should be true) */

        if ((edir != edir_adj && curdir == rfdir_adj) ||
            (edir == edir_adj && curdir != rfdir_adj)) {
          rfarray1[nrf1] = eface;
          rfdirs1[nrf1] = rfdir_adj;
          List_Add(processed_faces,rfarray1[nrf1]);
          nrf1++;
          break;
        }
      }
      List_Delete(efaces);
    }
    List_Delete(felist);
  }


  /* collect info for the second region */

  rfarray2[0] = fnew;
  rfdirs2[0] = !rfdirs1[0];
  nrf2 = 1;
  List_Add(processed_faces,rfarray2[0]);
  
  i = 0;
  while (i < nrf2) {
    curface = rfarray2[i];
    curdir = rfdirs2[i];
    i++;

    /* Get adjacent faces in region of current face and if they are
       not already in the new region face list (not marked), then add
       them */
 
    felist = MF_Edges(curface,1,0);
    idx1 = 0; j = 0;
    while ((fe = List_Next_Entry(felist,&idx1))) {
      edir = MF_EdgeDir_i(curface,j);      
      j++;

      efaces = ME_Faces(fe);
      if (curface != fnew && List_Contains(efaces,fnew)) {
        /* we have come back to the starting or splitting face - don't
           go across this edge */
        List_Delete(efaces);
        continue;
      }

      
      /* Add an adjacent unprocessed face of the region to the list of 
       faces for the new region */
      idx2 = 0; 
      while ((eface = List_Next_Entry(efaces,&idx2))) { 
        if (eface == curface) continue;
        if (List_Contains(processed_faces,eface)) continue;
        if (!MR_UsesEntity(rsplit,eface,MFACE)) continue; /* does not belong to region */

        edir_adj = MF_EdgeDir(eface,fe);
        rfdir_adj = MR_FaceDir(rsplit,eface);

        /* add adjacent face based on the check that if two adjacent faces of
           region are used by the region in the same sense, then their common
           edge should be used by the two faces in opposite senses (or the opposite
           of both the conditions should be true) */

        if ((edir != edir_adj && curdir == rfdir_adj) ||
            (edir == edir_adj && curdir != rfdir_adj)) {
          rfarray2[nrf2] = eface;
          rfdirs2[nrf2] = rfdir_adj;
          List_Add(processed_faces,rfarray2[nrf2]);
          nrf2++;
          break;
        }
      }
      List_Delete(efaces);
    }
    List_Delete(felist);
  }

  /* Delete the original region */

  MR_Delete(rsplit,0);

  /* Make the two new regions */

  rnew[0] = MR_New(mesh);
  MR_Set_GEntDim(rnew[0],3);
  MR_Set_GEntID(rnew[0],gid);
  MR_Set_Faces(rnew[0],nrf1,rfarray1,rfdirs1);

  rnew[1] = MR_New(mesh);
  MR_Set_GEntDim(rnew[1],3);
  MR_Set_GEntID(rnew[1],gid);
  MR_Set_Faces(rnew[1],nrf2,rfarray2,rfdirs2);

  List_Delete(processed_faces);

  return fnew;
}
Esempio n. 8
0
MRegion_ptr MRs_Join(MRegion_ptr r1, MRegion_ptr r2, MFace_ptr f) {
  int i, j, nrf1, nrf2, gdim, gid, *rfdir2;
  MFace_ptr *rf2, fcmn=f;
  Mesh_ptr   mesh;
  List_ptr   rfaces2;


  mesh = MF_Mesh(r1);
  gid = MF_GEntID(r1);

  if (mesh != MF_Mesh(r2)) {
    MSTK_Report("MRs_Join","Regions not from same mesh",MSTK_ERROR);
    return 0;
  }
  else if (gid != MR_GEntID(r2)) {
    MSTK_Report("MRs_Join","Regions not from same geometric entity",MSTK_ERROR);
    return 0;
  }


  rfaces2 = MR_Faces(r2);
  nrf2 = List_Num_Entries(rfaces2);
  
  if (fcmn) {
    if (!MR_UsesEntity(r1,fcmn,MFACE)) {
      MSTK_Report("MRs_Join","Cannot find common face in region",MSTK_ERROR);
      return 0;
    }
  }
  else { /* find the common face */

    List_ptr rfaces1 = MR_Faces(r1);

    int idx = 0;
    MFace_ptr rf;
    while ((rf = List_Next_Entry(rfaces2,&idx))) {
      if (List_Contains(rfaces1,rf)) {
        fcmn = rf;
        break;
      }
    }

    List_Delete(rfaces1);

  }

  rf2 = (MFace_ptr) malloc(nrf2*sizeof(MFace_ptr));
  rfdir2 = (int *) malloc(nrf2*sizeof(int));

  int found;
  for (i = 0, j = 0, found = 0; i < nrf2; i++) {
    MFace_ptr rface = List_Entry(rfaces2,i);
    if (rface == fcmn) 
      found = 1;
    else {
      rf2[j] = rface;
      rfdir2[j] = MR_FaceDir_i(r2,i);
      j++;
    }
  }
  List_Delete(rfaces2);

  if (!found) {
    MSTK_Report("MRs_Join","Cannot find common face in region",MSTK_ERROR);
    return 0;
  }

  MR_Delete(r2,0);

  MR_Replace_Faces(r1,1,&fcmn,nrf2-1,rf2,rfdir2);
  
  MF_Delete(fcmn,0);

  free(rf2); free(rfdir2);

  return r1;
}
Esempio n. 9
0
  List_ptr MR_Vertices_FNR3R4(MRegion_ptr r) {
    int i, j, n, ne, nf, mkr, found, idx;
    int diradj0=0, diropp=0, fdir, fdir0, fdir1, allquad, alltri;
    int nquads, ntris, itri0, itri1, iquad0;
    MFace_ptr face=NULL, face0=NULL, fadj0=NULL, fopp=NULL;
    MFace_ptr quad0=NULL, tri0=NULL, tri1=NULL;
    MEdge_ptr edge, fedge00, upedge;
    MVertex_ptr vert, rv0, rvopp0=NULL;
    List_ptr rvertices, fverts, fedges0, adjfedges;
    MRegion_Adj_FN *adj;

    adj = (MRegion_Adj_FN *) r->adj;

    nf = List_Num_Entries(adj->rfaces);

    switch (r->mrtype) {

    case TET: 

      /* Add vertices of first face to list of region vertices */
      face0 = List_Entry(adj->rfaces,0); /* first face */
      fdir0 = MR_FaceDir_i(r,0);  /* Sense in which face is used in region */
      rvertices = MF_Vertices(face0,!fdir0,0);
      
      face = List_Entry(adj->rfaces,1);
      fverts = MF_Vertices(face,1,0);
      for (i = 0; i < 3; i++) { 
        vert = List_Entry(fverts,i);
        if (!List_Contains(rvertices,vert)) {
          List_Add(rvertices,vert);
          break;
        }
      }
      List_Delete(fverts);
      
      return rvertices;
      break;

    case PRISM:

      tri0 = NULL; itri0 = -1;
      tri1 = NULL; itri1 = -1;

      for (i = 0; i < nf; i++) {
	face = List_Entry(adj->rfaces,i);
	ne = MF_Num_Edges(face);
	if (ne == 3) {
	  if (!tri0) {
	    tri0 = face;
	    itri0 = i;
	  }
	  else {
	    tri1 = face;
	    itri1 = i;
	  }
	}
      }
      
      fdir0 = MR_FaceDir_i(r,itri0); /* Sense in which face is used in region */
      rvertices = MF_Vertices(tri0,!fdir0,0);
      
      /* find the vertical edge between vertex 0 of bottom triangle and
         top triangle */
      
      fdir1 = MR_FaceDir_i(r,itri1);
      fverts = MF_Vertices(tri1,fdir1,0);
      
      rv0 = List_Entry(rvertices,0); /* first vtx of region & bottom face */
      
      for (i = 0; i < 3; i++) {
        vert = List_Entry(fverts,i);
        
        if (MVs_CommonEdge(rv0,vert)) {
          List_Add(rvertices,vert);
          List_Add(rvertices,List_Entry(fverts,(i+1)%3));
          List_Add(rvertices,List_Entry(fverts,(i+2)%3));
          break;
        }
      }
      
      List_Delete(fverts);
      
      return rvertices;
      break;

    case PYRAMID:

      quad0 = NULL; iquad0 = -1;
      tri0 = NULL; itri0 = -1;

      for (i = 0; i < nf; i++) {
	face = List_Entry(adj->rfaces,i);
	ne = MF_Num_Edges(face);
	if (ne == 4) {
	  if (!quad0) {
	    quad0 = face;
	    iquad0 = i;
	  }          
	}
        else if (ne == 3) {
          if (!tri0) {
            tri0 = face;
            itri0 = i;
          }
        }
      }
      

      fdir0 = MR_FaceDir_i(r,iquad0);  /* Sense in which face is used in region */
      rvertices = MF_Vertices(quad0,!fdir0,0);
      
      fverts = MF_Vertices(tri0,1,0);
      
      for (i = 0; i < 3; i++) {
        vert = List_Entry(fverts,i);
        if (!List_Contains(rvertices,vert)) { /* found apex vtx of pyramid */
          List_Add(rvertices,vert);
          break;
        }
      }

      List_Delete(fverts);
	  
      return rvertices;
      break;

    case HEX:

      face0 = List_Entry(adj->rfaces,0); /* face 0 */
      fdir0 = MR_FaceDir_i(r,0); /* dir of face w.r.t. region */
	
      /* Add vertices of first face */
      rvertices = MF_Vertices(face0,!fdir0,0);
	
      /* vertex 0 of region and of face 0 */
      rv0 = List_Entry(rvertices,0);      
	
      /* Edges of face 0 */
      fedges0 = MF_Edges(face0,!fdir0,rv0);
	
      /* edge 0 of face 0 (wrt face dir pointing into the region) */
      fedge00 = List_Entry(fedges0,0); 
	
      /* Get the face adjacent to edge 0 of face 0 and also the
         opposite face, a face that has no edge common face 0 */
	
      fopp = NULL; fadj0 = NULL; 
      found = 0;
      for (i = 1; i < nf; i++) {
        face = List_Entry(adj->rfaces,i);
	  
        /* Check if face uses any edge of face 0 */
	  
        for (j = 0, found = 0; j < 4; j++) {
          edge = List_Entry(fedges0,j);
	    
          if (MF_UsesEntity(face,edge,1)) {
            if (edge == fedge00) {
              /* face uses edge 0 of face 0 (w.r.t. face dir pointing
                 into region) */
              fadj0 = face;
              diradj0 = MR_FaceDir_i(r,i);
            }
            found = 1;
            break;
          }
        }
        if (!found) {
          fopp = face;
          diropp = MR_FaceDir_i(r,i);
        }
        if (fopp && fadj0)
          break;
      }
	
      if (!fopp) {
        MSTK_Report("MR_Vertices_FNR3R4","Could not find opposite face",MSTK_ERROR);
        List_Delete(fedges0);
        List_Delete(rvertices);
        return (void *) NULL;
      }
	
      /* edges of face adjacent to edge 0 of face 0 */
      adjfedges = MF_Edges(fadj0,1,rv0);
	
      /* edge connecting rv0 and vertex on opposite face */
      upedge = diradj0 ? List_Entry(adjfedges,3): List_Entry(adjfedges,0);
	
#ifdef DEBUG
      if (!ME_UsesEntity(upedge,rv0,0))
        MSTK_Report("MR_Vertices_FNR3R4","Cannot find correct vertical edge",
                    MSTK_ERROR);
#endif
	
      if (ME_Vertex(upedge,0) == rv0)
        rvopp0 = ME_Vertex(upedge,1);
      else {
        rvopp0 = ME_Vertex(upedge,0);
      }
	
      List_Delete(fedges0);
      List_Delete(adjfedges);
	
      fverts = MF_Vertices(fopp,diropp,rvopp0);
	
      for (i = 0; i < 4; i++)
        List_Add(rvertices,List_Entry(fverts,i));
      List_Delete(fverts);
	
      return rvertices;
      break;

    default: 

      /* General Polyhedra */
      mkr = MSTK_GetMarker();
      
      /* Add vertices of first face */
      face = List_Entry(adj->rfaces,0); /* first face */
      fdir = MR_FaceDir_i(r,0);    /* Sense in which face is used in region */
      
      rvertices = MF_Vertices(face,!fdir,0); 
      List_Mark(rvertices,mkr);
      
      for (i = 1; i < nf-1; i++) {
        face = List_Entry(adj->rfaces,i);
        fverts = MF_Vertices(face,1,0);
        n = List_Num_Entries(fverts);
        for (j = 0; j < n; j++) {
          vert = List_Entry(fverts,j);
          if (!MEnt_IsMarked(vert,mkr)) {
            List_Add(rvertices,vert);
            MEnt_Mark(vert,mkr);
          }
        }
        List_Delete(fverts);
      }
      List_Unmark(rvertices,mkr);
      MSTK_FreeMarker(mkr);
      
      return rvertices;
    }

    return NULL;
  }
Esempio n. 10
0
 int MF_UsesVertex_R2(MFace_ptr f, MVertex_ptr v) {
   MFace_Adj_R2 *adj;
   adj = (MFace_Adj_R2 *) f->adj;
   return List_Contains(adj->fvertices,v);
 }