| 1 | /* | 
|---|
| 2 | * SplitMeshForRifts.c: | 
|---|
| 3 | */ | 
|---|
| 4 | #include "./trimesh.h" | 
|---|
| 5 |  | 
|---|
| 6 | #include "../Alloc/alloc.h" | 
|---|
| 7 |  | 
|---|
| 8 | int SplitMeshForRifts(int* pnel,double** pindex,int* pnods,double** px,double** py,int* pnsegs,double** psegments,double** psegmentmarkerlist){ | 
|---|
| 9 |  | 
|---|
| 10 | /*Some notes on dimensions: | 
|---|
| 11 | index  of size nelx3 | 
|---|
| 12 | x and y of size nodsx1 | 
|---|
| 13 | segments of size nsegsx3*/ | 
|---|
| 14 |  | 
|---|
| 15 | /*Error management: */ | 
|---|
| 16 | int noerr=1; | 
|---|
| 17 |  | 
|---|
| 18 | int i,j,k,l; | 
|---|
| 19 | int grid; | 
|---|
| 20 | int el; | 
|---|
| 21 |  | 
|---|
| 22 | int  nriftsegs; | 
|---|
| 23 | int* riftsegments=NULL; | 
|---|
| 24 | int* flags=NULL; | 
|---|
| 25 |  | 
|---|
| 26 | int  NumGridElementListOnOneSideOfRift; | 
|---|
| 27 | int* GridElementListOnOneSideOfRift=NULL; | 
|---|
| 28 |  | 
|---|
| 29 | /*Input: */ | 
|---|
| 30 | int     nel; | 
|---|
| 31 | double* index=NULL; | 
|---|
| 32 | int     nods; | 
|---|
| 33 | double* x=NULL; | 
|---|
| 34 | double* y=NULL; | 
|---|
| 35 | double* segments=NULL; | 
|---|
| 36 | double*    segmentmarkerlist=NULL; | 
|---|
| 37 | int     nsegs; | 
|---|
| 38 |  | 
|---|
| 39 | /*Recover input: */ | 
|---|
| 40 | nel=*pnel; | 
|---|
| 41 | index=*pindex; | 
|---|
| 42 | nods=*pnods; | 
|---|
| 43 | x=*px; | 
|---|
| 44 | y=*py; | 
|---|
| 45 | nsegs=*pnsegs; | 
|---|
| 46 | segments=*psegments; | 
|---|
| 47 | segmentmarkerlist=*psegmentmarkerlist; | 
|---|
| 48 |  | 
|---|
| 49 |  | 
|---|
| 50 | /*Establish list of segments that belong to a rift: */ | 
|---|
| 51 | RiftSegmentsFromSegments(&nriftsegs,&riftsegments,nel,index,nsegs,segments); /*riftsegments of size nriftsegsx4 (4 for first element on segment,second element, | 
|---|
| 52 | first grid and second sgrid)*/ | 
|---|
| 53 |  | 
|---|
| 54 | /*Go through all grids of the rift segments, and start splitting the mesh: */ | 
|---|
| 55 | flags=(int*)xcalloc(nods,sizeof(int)); //to make sure we don't split the same grids twice! | 
|---|
| 56 | for (i=0;i<nriftsegs;i++){ | 
|---|
| 57 | for (j=0;j<2;j++){ | 
|---|
| 58 |  | 
|---|
| 59 | grid=*(riftsegments+4*i+j+2); | 
|---|
| 60 | if(flags[grid-1]){ | 
|---|
| 61 | /*This grid was already split, skip:*/ | 
|---|
| 62 | continue; | 
|---|
| 63 | } | 
|---|
| 64 | else{ | 
|---|
| 65 | flags[grid-1]=1; | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 | if(IsGridOnRift(riftsegments,nriftsegs,grid)){ | 
|---|
| 69 |  | 
|---|
| 70 | DetermineGridElementListOnOneSideOfRift(&NumGridElementListOnOneSideOfRift,&GridElementListOnOneSideOfRift,i,nriftsegs,riftsegments,grid,index,nel); | 
|---|
| 71 |  | 
|---|
| 72 | /*Summary: we have for grid, a list of elements (GridElementListOnOneSideOfRift, of size NumGridElementListOnOneSideOfRift) that all contain grid | 
|---|
| 73 | *and that are on the same side of the rift. For all these elements, we clone grid into another grid, and we swap all instances of grid in the triangulation | 
|---|
| 74 | *for those elements, to the new grid.*/ | 
|---|
| 75 |  | 
|---|
| 76 | //augment number of grids | 
|---|
| 77 | nods=nods+1; | 
|---|
| 78 | //create new grid | 
|---|
| 79 | x=(double*)xrealloc(x,nods*sizeof(double)); | 
|---|
| 80 | y=(double*)xrealloc(y,nods*sizeof(double)); | 
|---|
| 81 | x[nods-1]=x[grid-1]; //matlab indexing | 
|---|
| 82 | y[nods-1]=y[grid-1]; //matlab indexing | 
|---|
| 83 |  | 
|---|
| 84 | //change elements owning this grid | 
|---|
| 85 | for (k=0;k<NumGridElementListOnOneSideOfRift;k++){ | 
|---|
| 86 | el=GridElementListOnOneSideOfRift[k]; | 
|---|
| 87 | for (l=0;l<3;l++){ | 
|---|
| 88 | if (*(index+3*el+l)==grid)*(index+3*el+l)=nods; //again, matlab indexing. | 
|---|
| 89 | } | 
|---|
| 90 | } | 
|---|
| 91 | }// if(IsGridOnRift(riftsegments,nriftsegs,grid)) | 
|---|
| 92 | } //for(j=0;j<2;j++) | 
|---|
| 93 | } //for (i=0;i<nriftsegs;i++) | 
|---|
| 94 |  | 
|---|
| 95 | /*update segments: they got modified completely by adding new grids.*/ | 
|---|
| 96 | UpdateSegments(&segments,&segmentmarkerlist, &nsegs,index,x,y,riftsegments,nriftsegs); | 
|---|
| 97 |  | 
|---|
| 98 | /*Assign output pointers: */ | 
|---|
| 99 | *pnel=nel; | 
|---|
| 100 | *pindex=index; | 
|---|
| 101 | *pnods=nods; | 
|---|
| 102 | *px=x; | 
|---|
| 103 | *py=y; | 
|---|
| 104 | *pnsegs=nsegs; | 
|---|
| 105 | *psegments=segments; | 
|---|
| 106 | *psegmentmarkerlist=segmentmarkerlist; | 
|---|
| 107 | return noerr; | 
|---|
| 108 | } | 
|---|