Debian Codegraph @

idp/MeshSurface.idp

raw
1 load "medit"
2 load "mmg"
3 // 2 basic functions to build surface mesh
4 /* Usage:
5 meshS SurfaceHex(N,B,L,orient);
6 -- build the surface mesh of a 3d box
7 where: for example:
8 int[int] N=[nx,ny,nz]; // the number of seg in the 3 direction
9 real [int,int] B=[[xmin,xmax],[ymin,ymax],[zmin,zmax]]; // bounding bax
10 int [int,int] L=[[1,2],[3,4],[5,6]]; // the label of the 6 face left,right, front, back, down, right
11 orient the global orientation of the surface 1 extern (-1 intern)
12
13
14 func meshS Sphere(real R,real h,int L,int orient);
15 -- build a surface mesh of a sphere with 1 mapping (spheriale coordinate)
16 where R is the raduis,
17 h is the mesh size of the shpere
18 L is the label the the sphere
19 orient the global orientation of the surface 1 extern (-1 intern
20
21 func meshS Icosahedron (int orientation)
22 -- build a Icosahedron meshS with given orientation.
23 with a region number go from 1 to 20 corresponding to the 20 faces
24
25 func meshS Sphere20(real R,int N,int orient);
26 -- build a sphere mesh form a Isocaedron with each traingle subdivide by N^2
27 with a region number go from 1 to 20 corresponding to the 20 faces
28 orient the global orientation of the surface 1 extern (-1 intern
29
30 */
31 func meshS SurfaceHex(int[int] & N,real[int,int] &B ,int[int,int] & L,int orientation)
32 {
33 real x0=B(0,0),x1=B(0,1);
34 real y0=B(1,0),y1=B(1,1);
35 real z0=B(2,0),z1=B(2,1);
36
37 int nx=N[0],ny=N[1],nz=N[2];
38
39 mesh Thx = square(ny,nz,[y0+(y1-y0)*x,z0+(z1-z0)*y]);
40 mesh Thy = square(nx,nz,[x0+(x1-x0)*x,z0+(z1-z0)*y]);
41 mesh Thz = square(nx,ny,[x0+(x1-x0)*x,y0+(y1-y0)*y]);
42
43 int[int] refx=[0,L(0,0)],refX=[0,L(0,1)]; // Xmin, Ymax faces labels renumbering
44 int[int] refy=[0,L(1,0)],refY=[0,L(1,1)]; // Ymin, Ymax faces labesl renumbering
45 int[int] refz=[0,L(2,0)],refZ=[0,L(2,1)]; // Zmin, Zmax faces labels renumbering
46
47 meshS Thx0 = movemesh23(Thx,transfo=[x0,x,y],orientation=-orientation,region=refx);
48 meshS Thx1 = movemesh23(Thx,transfo=[x1,x,y],orientation=+orientation,region=refX);
49 meshS Thy0 = movemesh23(Thy,transfo=[x,y0,y],orientation=+orientation,region=refy);
50 meshS Thy1 = movemesh23(Thy,transfo=[x,y1,y],orientation=-orientation,region=refY);
51 meshS Thz0 = movemesh23(Thz,transfo=[x,y,z0],orientation=-orientation,region=refz);
52 meshS Thz1 = movemesh23(Thz,transfo=[x,y,z1],orientation=+orientation,region=refZ);
53 meshS Th= Thx0+Thx1+Thy0+Thy1+Thz0+Thz1;
54 return Th;
55 }
56 func meshS Ellipsoide (real RX,real RY,real RZ,real h,int L,real Ox,real Oy,real Oz,int orientation)
57 {
58 mesh Th=square(10,20,[x*pi-pi/2,2*y*pi]); // $]\frac{-pi}{2},frac{-pi}{2}[\times]0,2\pi[ $
59 // a parametrization of a sphere
60 func f1 =RX*cos(x)*cos(y)+Ox;
61 func f2 =RY*cos(x)*sin(y)+Oy;
62 func f3 =RZ*sin(x)+Oz;
63 // partiel derivative
64 func f1x= -RX*sin(x)*cos(y);
65 func f1y= -RX*cos(x)*sin(y);
66 func f2x= -RY*sin(x)*sin(y);
67 func f2y= +RY*cos(x)*cos(y);
68 func f3x=-RZ*cos(x);
69 func f3y=0;
70 // the metric on the sphere $ M = DF^t DF $
71 func m11=f1x^2+f2x^2+f3x^2;
72 func m21=f1x*f1y+f2x*f2y+f3x*f3y;
73 func m22=f1y^2+f2y^2+f3y^2;
74
75 func perio=[[4,y],[2,y],[1,x],[3,x]]; // to store the periodic condition
76 real hh=h;// hh mesh size on unite sphere
77 real vv= 1/square(hh);
78 Th=adaptmesh(Th,m11*vv,m21*vv,m22*vv,IsMetric=1,periodic=perio);
79 Th=adaptmesh(Th,m11*vv,m21*vv,m22*vv,IsMetric=1,periodic=perio);
80 Th=adaptmesh(Th,m11*vv,m21*vv,m22*vv,IsMetric=1,periodic=perio);
81 Th=adaptmesh(Th,m11*vv,m21*vv,m22*vv,IsMetric=1,periodic=perio);
82 int[int] ref=[0,L];
83 meshS ThS= movemesh23(Th,transfo=[f1,f2,f3],orientation=orientation,reftri=ref);
84 ThS=mmgs(ThS,hmin=h,hmax=h,hgrad=2.);
85 return ThS;
86 }
87
88 func meshS Ellipsoide (real RX,real RY,real RZ,real h,int L,int orientation)
89 {
90 return Ellipsoide (RX,RY,RZ,h,L,0.,0.,0.,orientation);
91 }
92
93 func meshS Sphere(real R,real h,int L,int orientation)
94 {
95
96 return Ellipsoide(R,R,R,h,L,orientation);
97 }
98
99 func meshS Sphere(real R,real h,int L,real Ox,real Oy,real Oz,int orientation)
100 {
101
102 return Ellipsoide(R,R,R,h,L,Ox,Oy,Oz,orientation);
103 }
104
105 func meshS Icosahedron (int orientation,int wplot)
106 {
107
108 //===================================================================================
109 //Angles utiles
110 //===================================================================================
111 real tan3pi10 = sqrt(25.+10.*sqrt(5.))/5.;//3pi/10 angle entre deux aretes du pentagone
112 real sin3pi10 = (sqrt(5)+1)/4;//3pi/10 angle entre deux aretes du pentagone
113 real cos3pi10 = sqrt(10-2*sqrt(5))/4;//3pi/10 angle entre deux aretes du pentagone
114
115 real cosdiedre = sqrt(5)/3; //angle diedre de l'icosaedre -pi/2
116 real sindiedre = 2./3; //angle diedre de l'icosaedre -pi/2
117
118 real cosico = tan3pi10/sqrt(3); //angle entre une face de la pyramide pentagonale par rapport à l'horizontale
119 real sinico = sqrt(1-square(cosico)); //angle entre une face de la pyramide pentagonale par rapport à l'horizontale
120
121 real sin2pi5 = sqrt(10+2*sqrt(5))/4; //2pi/5 angle pour la rotation des aretes du pentagone
122 real cos2pi5 = (sqrt(5)-1)/4; //2pi/5 angle pour la rotation des aretes du pentagone
123
124 real cosicod = cosdiedre*cosico+sindiedre*sinico;//angle diedre -pi/2 - ico
125 real sinicod = sindiedre*cosico-cosdiedre*sinico;//angle diedre -pi/2 - ico
126
127
128 real sinpi3 = sqrt(3)/2; //angle du triangle equilateral
129
130 int n = 1;
131
132 real sinpi5 = cos3pi10;//pi/5 angle de décalage entre deux demi icosaedre
133 real cospi5 = sin3pi10;//pi/5 angle de décalage entre deux demi icosaedre
134
135 real tanpi10 = sqrt(25.-10.*sqrt(5.))/5.;//pi/10
136 real h = 0.5*sqrt(3-square(tanpi10));//hauteur du prisme d'ordre 5;
137
138 //=================================================================================
139 //Construction du triangle equilateral en 2D
140 //=================================================================================
141 border a(t=0,1){x=t; y=0; label =1;};
142 border b(t=1,0.5){x=t; y=sqrt(3)*(1-t); label =2;};
143 border c(t=0.5,0){x=t; y=sqrt(3)*(t); label =3;};
144 mesh Triangle= buildmesh(a(n)+b(n)+c(n)); //traingle equilateral
145 if(wplot>2) plot (cmm="Triangle",Triangle,wait=1);
146
147 func f = 1;
148
149
150 meshS Triangle3 = movemesh23(Triangle,transfo=[x,0,y]);//trianglesup
151 if(wplot>2) plot (cmm="Triangle3",Triangle3,wait=1);
152
153 meshS TriangleS = change(fregion=1,movemeshS(Triangle3,transfo=[x,sinico*y+cosico*z,-cosico*y+sinico*z]));//rotation de -(pi - diedre) par rapport à l'axe des x pour former une face de la pyramide pentagonale
154 if(wplot>2) plot (cmm="TriangleS",TriangleS,wait=1);
155 //medit("face pyramide pentagonale",TriangleS);
156
157 meshS TriangleI = change(fregion=2,movemeshS(TriangleS,transfo=[x,-cosdiedre*y+sindiedre*z,-sindiedre*y-cosdiedre*z],orientation=-1));//triangle inf rotation de l'angle diedre par rapport au triangle sup
158 if(wplot>2) plot (cmm="TriangleI",TriangleI,wait=1);
159
160 meshS Triangles = TriangleI+TriangleS;
161
162 if(wplot>1) plot (cmm="Triangles",Triangles,wait=1);
163 //medit("face pyramide pentagonale + face antiprisme d'ordre 5",Triangles);
164
165 meshS T1 = change(movemeshS(Triangles,transfo=[x-0.5,y-sinpi3*cosico,z],orientation=1),fregion = region);//translation pour que la figure soit sur le bord du pentagone
166 meshS T2 = change(movemeshS(T1,transfo=[cos2pi5*x-sin2pi5*y,sin2pi5*x+cos2pi5*y,z],orientation=1),fregion = region+2);;
167 meshS T3 = change(movemeshS(T2,transfo=[cos2pi5*x-sin2pi5*y,sin2pi5*x+cos2pi5*y,z]),fregion = region+2);;
168 meshS T4 = change(movemeshS(T3,transfo=[cos2pi5*x-sin2pi5*y,sin2pi5*x+cos2pi5*y,z]),fregion = region+2);;
169 meshS T5 = change(movemeshS(T4,transfo=[cos2pi5*x-sin2pi5*y,sin2pi5*x+cos2pi5*y,z]),fregion = region+2);;
170
171 meshS Tdemi= T1+T2+T3+T4+T5;//moitié de l'icosaedre
172 meshS Tdemi0 = movemeshS(Tdemi,transfo=[x,y,z+0.5*h]);//moitié supérieure
173 meshS Tdemi1 = movemeshS(Tdemi0,transfo=[x,y,-z]);//moitié inférieure
174 meshS Tdemi1rot = change(movemeshS(Tdemi1,transfo=[cospi5*x-sinpi5*y,sinpi5*x+cospi5*y,z],orientation=-1),fregion = region+10);//rotation de la moitié inférieure pour les emboiter
175 meshS Ticosaedre = Tdemi0+Tdemi1rot;
176 //Ticosaedre=trunc(Ticosaedre,1,split=5);
177 if(wplot) plot(Ticosaedre,wait=1);
178 //cout << regions(Ticosaedre) << endl;
179 return Ticosaedre;
180 }
181
182 func meshS Icosahedron (int orientation)
183 {
184 return Icosahedron(orientation,0);
185 }
186 func meshS Sphere20 (real R,int N,int orientation,int wplot)
187 {// Isocaedre regulier !!!! Thank G. Vergez ..
188 meshS Ticosaedre = Icosahedron(orientation,wplot);
189 Ticosaedre=trunc(Ticosaedre,1,split=N);
190 if(wplot) plot(cmm="Icosaedre",Ticosaedre,wait=1);
191
192 //=================================================================================
193 //Construction de la sphere 3D
194 //=================================================================================
195 func metric =dist(x,y,z)/R;
196 meshS Th = movemeshS(Ticosaedre,transfo=[x/metric,y/metric,z/metric]);
197 if(wplot) plot (cmm="Th",Th,wait=1);
198 return Th;
199 }
200
201 func meshS Sphere20 (real R,int N,int orientation)
202 {
203 return Sphere20(R,N,orientation,0);
204 }
205
206 /* test:
207 load "tetgen"
208 {
209 real hs = 0.1; // mesh size on sphere
210 int[int] N=[20,20,20];
211 real [int,int] B=[[-1,1],[-1,1],[-1,1]];
212 int [int,int] L=[[1,2],[3,4],[5,6]];
213
214 ////////////////////////////////
215 meshS ThH =
216 (N,B,L,1);
217 meshS ThS =Sphere(0.5,hs,7,1); // "gluing" surface meshs to tolat boundary meshes
218 cout << " xxxx" << ThH.nv << " " << ThS.nv << endl;
219
220 meshS ThHS=ThH+ThS;
221 savemesh(ThHS,"Hex-Sphere.mesh");
222 exec("ffmedit Hex-Sphere.mesh;rm Hex-Sphere.mesh");
223
224 real voltet=(hs^3)/6.;
225 cout << " voltet = " << voltet << endl;
226 real[int] domaine = [0,0,0,1,voltet,0,0,0.7,2,voltet];
227
228 mesh3 Th = tetg(ThHS,switch="pqaAAYYQ",nbofregions=2,regionlist=domaine);
229 medit("Cube-With-Ball",Th);
230 }
231
232 */