PageRenderTime 66ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/FREEFEM/freefem/lib/adapt/mshopt.cxx

#
C++ | 208 lines | 146 code | 7 blank | 55 comment | 22 complexity | ce418325bce3c65a135fae5b4bfcc82a MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. /*
  2. * ADAPT2D : a software for automatic mesh adaptation in 2D
  3. *
  4. * AUTHOR : Manuel J. Castro Diaz(e-mail:castro@gamba.cie.uma.es)
  5. * ADAPTED FOR FREEFEM : Prud'homme Christophe (e-mail:prudhomm@ann.jussieu.fr)
  6. *
  7. * this code is public domain
  8. *
  9. * You may copy freely these files and use it for
  10. * teaching or research. These or part of these may
  11. * not be sold or used for a commercial purpose without
  12. * our consent
  13. *
  14. * Any problems should be reported to the AUTHOR
  15. * at the following address : castro@gamba.cie.uma.es
  16. */
  17. #include <mshopt.hxx>
  18. int mshopt (Triangulo_T1* tr, const int& a) {
  19. int err=0,cont=0,err0;
  20. Pila_trar* pila;
  21. Pila_trar_link* aux,*cab;
  22. Triangulo_T1 t0;
  23. Triangulo_T1* t1,*t2;
  24. Triangulo_T1* t1v2,*t1v3,*t2v2,*t2v3;
  25. Arista_T1* a1,*a2,*a3,*b2,*b3;
  26. Vertice_T1* so1,*so2,*so3,*so4;
  27. Metrica mm1,mm2,m1i,m2i,m3i,m4i;
  28. R2 c1,c2,c3,c4;
  29. Scalar area1,area2;
  30. Scalar crit1,crit2,critp,crit;
  31. int ip1[3];
  32. int a11,a12,a13,a21,a22,a23,aa;
  33. int reft;
  34. ip1[0]=1;ip1[1]=2;ip1[2]=0;
  35. pila =new Pila_trar;
  36. aux=new Pila_trar_link;
  37. if (pila==NIL || aux==NIL) ERROR();
  38. aux->set(tr,a);
  39. pila->in(aux);
  40. cab=pila->cabeza();
  41. while (cab!=NIL && cont<128) {
  42. // Saco un elemento de la pila y lo destruyo.
  43. cont++;
  44. aux=pila->out();
  45. t1=aux->t;
  46. a11=aux->a;
  47. if (a11<0 || a11>2){
  48. cout<<"a11:"<<a11<<endl;
  49. cerr<<"Error in mshopt."<<endl;
  50. exit(1);
  51. }
  52. delete aux;
  53. a1=t1->a[a11];
  54. t2=t1->t[a11]; // Saco el triangulo vecino de t1 por la
  55. // la arista numero a.
  56. if (t2) {
  57. // Busco cual es la arista a11 en t2 y lo meto en a21.
  58. // cout<<"mshopt 1. arista"<<endl;
  59. a21=t2->arista(t1->a[a11]);
  60. if (a21<0 || a21>2){
  61. cerr<<"Error in mshopt."<<endl;
  62. cout<<"a21:"<<a21<<endl;
  63. cout<<"a11:"<<a11<<endl;
  64. cout<<"cont:"<<cont<<endl;
  65. cout<<"t1:"<<endl<<*t1<<endl;
  66. cout<<*(t1->s[0])<<endl<<*(t1->s[1])<<endl<<*(t1->s[2])<<endl;
  67. cout<<*(t1->a[0])<<endl<<*(t1->a[1])<<endl<<*(t1->a[2])<<endl;
  68. cout<<*(t1->t[0])<<endl<<*(t1->t[1])<<endl<<*(t1->t[2])<<endl;
  69. cout<<"t2:"<<endl<<*t2<<endl;
  70. cout<<*(t2->s[0])<<endl<<*(t2->s[1])<<endl<<*(t2->s[2])<<endl;
  71. cout<<*(t2->a[0])<<endl<<*(t2->a[1])<<endl<<*(t2->a[2])<<endl;
  72. cout<<*(t2->t[0])<<endl<<*(t2->t[1])<<endl<<*(t2->t[2])<<endl;
  73. exit (1);
  74. }
  75. if (a1->interseccion()==FALSE) {
  76. a12=ip1[a11];
  77. a13=ip1[a12];
  78. a22=ip1[a21];
  79. a23=ip1[a22];
  80. so1=t1->s[a13];
  81. so2=t1->s[a11];
  82. so3=t1->s[a12];
  83. so4=t2->s[a23];
  84. /* Criterio de optim. del cuadrilatero
  85. x so1 x so1
  86. / \ /|\
  87. / \ / | \
  88. / \ / | \
  89. t1v3 a3/ \ a2 t1v2 / | \
  90. / \ t1v3 a3/ | \
  91. / t1 \ / | \
  92. / \ / | a1 \ a2 t1v2
  93. / \ / | \
  94. so2 x-----------------x so3 so2 x t1 | t2 x so3
  95. \ a1 / \ | /
  96. \ / \ | /
  97. \ t2 / \ | /
  98. t2v2 b2\ / b3 t2v3 t2v2 b2\ | / b3 t2v3
  99. \ / \ | /
  100. \ / \ | /
  101. \ / \ | /
  102. \ / \|/
  103. x x
  104. so4 so4
  105. */
  106. c1=so1->c;
  107. c2=so2->c;
  108. c3=so3->c;
  109. c4=so4->c;
  110. t0.set(so2,so4,so1,NIL,NIL,NIL,NIL,NIL,NIL,0);
  111. area1=t0.area2D();
  112. t0.set(so3,so1,so4,NIL,NIL,NIL,NIL,NIL,NIL,0);
  113. area2=t0.area2D();
  114. if (area1>0 && area2>0) {
  115. m1i=so1->mtr.inv(err0);
  116. m2i=so2->mtr.inv(err0);
  117. m3i=so3->mtr.inv(err0);
  118. m4i=so4->mtr.inv(err0);
  119. //mm1=(so1->mtr+so2->mtr+so3->mtr)/3.0;
  120. //mm2=(so2->mtr+so3->mtr+so4->mtr)/3.0;
  121. mm1=((m1i+m2i+m3i)/3.0).inv(err0);
  122. mm2=((m2i+m3i+m4i)/3.0).inv(err0);
  123. if (t1->krit==-999.0) t1->krit=criter(c1,c2,c3,mm1);
  124. if (t2->krit==-999.0) t2->krit=criter(c2,c3,c4,mm2);
  125. crit=MIN(t1->krit,t2->krit);
  126. /*
  127. if (crit<=0) {
  128. cout<<"Error. criterio negativo o cero"<<endl;
  129. cout<<"t1:"<<t1->krit<<endl<<"t2:"<<t2->krit<<endl;
  130. cout<<*t1<<endl<<normal1<<endl<<*t2<<endl<<normal2<<endl;
  131. //exit (1);
  132. }
  133. */
  134. //mm1=(so1->mtr+so4->mtr+so3->mtr)/3.0;
  135. //mm2=(so1->mtr+so4->mtr+so2->mtr)/3.0;
  136. mm1=((m1i+m4i+m3i)/3.0).inv(err0);
  137. mm2=((m1i+m4i+m2i)/3.0).inv(err0);
  138. crit1=criter(c1,c4,c3,mm1);
  139. crit2=criter(c1,c4,c2,mm2);
  140. critp=MIN(crit1,crit2);
  141. if (critp<0) {
  142. cout<<"Error. Criterio negativo o cero"<<endl;
  143. cout<<"crit1:"<<crit1<<endl<<"crit2:"<<crit2<<endl;
  144. exit (1);
  145. }
  146. if (crit<critp) {
  147. t1->krit=crit1;
  148. t2->krit=crit2;
  149. a1=t1->a[a11];
  150. a2=t1->a[a12];
  151. a3=t1->a[a13];
  152. b2=t2->a[a22];
  153. b3=t2->a[a23];
  154. t1v2=t1->t[a12];
  155. t1v3=t1->t[a13];
  156. t2v2=t2->t[a22];
  157. t2v3=t2->t[a23];
  158. so2->t=t1;
  159. so3->t=t2;
  160. a1->s[0]=so1;
  161. a1->s[1]=so4;
  162. a2->tr=t2;
  163. b2->tr=t1;
  164. reft=t1->ref;
  165. t1->set(so2,so4,so1,t2v2,t2,t1v3,b2,a1,a3,reft);
  166. reft=t2->ref;
  167. t2->set(so3,so1,so4,t1v2,t1,t2v3,a2,a1,b3,reft);
  168. if (t1v2) {
  169. aa=t1v2->arista(a2);
  170. t1v2->t[aa]=t2;
  171. }
  172. if (t2v2) {
  173. aa=t2v2->arista(b2);
  174. t2v2->t[aa]=t1;
  175. }
  176. aux=new Pila_trar_link;
  177. if (aux==NIL) ERROR();
  178. aux->set(t1,0);
  179. pila->in(aux);
  180. aux=new Pila_trar_link;
  181. if (aux==NIL) ERROR();
  182. aux->set(t2,0);
  183. pila->in(aux);
  184. aux=new Pila_trar_link;
  185. if (aux==NIL) ERROR();
  186. aux->set(t1,2);
  187. pila->in(aux);
  188. aux=new Pila_trar_link;
  189. if (aux==NIL) ERROR();
  190. aux->set(t2,2);
  191. pila->in(aux);
  192. }
  193. }
  194. }
  195. }
  196. cab=pila->cabeza();
  197. }
  198. delete pila;
  199. return err;
  200. }