0024057: Eliminate compiler warning C4100 in MSVC++ with warning level 4
[occt.git] / src / MeshTest / MeshTest.cxx
1 // Created on: 1993-09-22
2 // Created by: Didier PIFFAULT
3 // Copyright (c) 1993-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
5 //
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
10 //
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 //
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
20
21
22 #include <Standard_Stream.hxx>
23
24 #include <stdio.h>
25
26 #include <MeshTest.ixx>
27
28 #include <MeshTest_DrawableMesh.hxx>
29 #include <TopAbs_ShapeEnum.hxx>
30 #include <TopoDS.hxx>
31 #include <TopoDS_Edge.hxx>
32 #include <TopoDS_Face.hxx>
33 #include <TopoDS_Shape.hxx>
34 #include <TopoDS_Compound.hxx>
35 #include <TopExp_Explorer.hxx>
36 #include <TopTools_ListIteratorOfListOfShape.hxx>
37 #include <DBRep.hxx>
38 #include <BRepTest.hxx>
39 #include <GeometryTest.hxx>
40 #include <BRep_Tool.hxx>
41 #include <BRep_Builder.hxx>
42 #include <Draw_MarkerShape.hxx>
43 #include <Draw_Appli.hxx>
44 #include <Draw.hxx>
45 #include <DrawTrSurf.hxx>
46 #include <BRepMesh_Triangle.hxx>
47 #include <BRepMesh_DataStructureOfDelaun.hxx>
48 #include <BRepMesh_Delaun.hxx>
49 #include <BRepMesh_FastDiscret.hxx>
50 #include <BRepMesh_Array1OfVertexOfDelaun.hxx>
51 #include <BRepMesh_Vertex.hxx>
52 #include <BRepMesh_Edge.hxx>
53 #include <BRepMesh_IncrementalMesh.hxx>
54 #include <TColStd_ListIteratorOfListOfInteger.hxx>
55 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
56 #include <Bnd_Box.hxx>
57 #include <Precision.hxx>
58 #include <Draw_Interpretor.hxx>
59 #include <IntPoly_PlaneSection.hxx>
60 #include <IntPoly_ShapeSection.hxx>
61 #include <Geom_Plane.hxx>
62 #include <Geom_Surface.hxx>
63 #include <Draw_Marker3D.hxx>
64 #include <Draw_Segment2D.hxx>
65
66 #include <GCPnts_UniformAbscissa.hxx>
67 #include <GeomAdaptor_Curve.hxx>
68 #include <Geom_Curve.hxx>
69 #include <Extrema_LocateExtPC.hxx>
70
71 #include <TopLoc_Location.hxx>
72 #include <gp_Trsf.hxx>
73 #include <Poly_Triangulation.hxx>
74 #include <Poly_Connect.hxx>
75 #include <TColgp_Array1OfPnt2d.hxx>
76 #include <TColStd_HArray1OfInteger.hxx>
77 #include <TopExp_Explorer.hxx>
78 #include <gp_Pln.hxx>
79
80 #include <PLib.hxx>
81 #include <AppCont_ContMatrices.hxx>
82 #include <math_Vector.hxx>
83 #include <math_Matrix.hxx>
84 #include <math.hxx>
85
86 #include <CSLib_DerivativeStatus.hxx>
87 #include <CSLib.hxx>
88 #include <BRepAdaptor_Surface.hxx>
89 #include <Bnd_Box.hxx>
90 #include <BRepBndLib.hxx>
91
92
93 //epa Memory leaks test
94 #include <BRepBuilderAPI_MakePolygon.hxx>
95 #include <TopoDS_Wire.hxx>
96 #include <BRepBuilderAPI_MakeFace.hxx>
97 #include <BRepTools.hxx>
98
99 //OAN: for triepoints
100 #include <BRepBuilderAPI_MakeVertex.hxx>
101 #include <Poly_PolygonOnTriangulation.hxx>
102 #include <TopTools_MapIteratorOfMapOfShape.hxx>
103
104 #ifdef WNT
105 Standard_IMPORT Draw_Viewer dout;
106 #endif
107
108 #define MAX2(X, Y)      (  Abs(X) > Abs(Y)? Abs(X) : Abs(Y) )
109 #define MAX3(X, Y, Z)   ( MAX2 ( MAX2(X,Y) , Z) )
110
111
112
113 #define ONETHIRD 0.333333333333333333333333333333333333333333333333333333333333
114 #define TWOTHIRD 0.666666666666666666666666666666666666666666666666666666666666
115
116 #ifdef DEB_MESH_CHRONO
117 #include <OSD_Chronometer.hxx>
118 Standard_Integer D0Control, D0Internal, D0Unif, D0Edges, NbControls;
119 OSD_Chronometer chTotal, chInternal, chControl, chUnif, chAddPoint;
120 OSD_Chronometer chEdges, chMaillEdges, chEtuInter, chLastControl, chStock;
121 OSD_Chronometer chAdd11, chAdd12, chAdd2, chUpdate, chPointValid;
122 OSD_Chronometer chIsos, chPointsOnIsos;
123 #endif
124
125
126
127 //=======================================================================
128 //function : shapesection
129 //purpose  : 
130 //=======================================================================
131
132 static Standard_Integer shapesection(Draw_Interpretor&, Standard_Integer nbarg, const char** argv)
133 {
134   if (nbarg < 4) return 1;
135
136   TopoDS_Shape S1 = DBRep::Get(argv[2]);
137   TopoDS_Shape S2 = DBRep::Get(argv[3]);
138   if (S1.IsNull() || S2.IsNull()) return 1;
139
140   IntPoly_ShapeSection SECTION(S1,S2);
141   //  char name[100];
142   BRep_Builder B;
143   TopoDS_Compound C;
144   B.MakeCompound(C);
145   for (Standard_Integer i = 1 ; i <= SECTION.NbEdges() ; i++) {
146     TopoDS_Shape E = SECTION.Edge(i);
147     if (!E.IsNull()) B.Add(C,E);
148   }
149
150   DBRep::Set(argv[1],C);
151   return 0;
152 }
153
154 //=======================================================================
155 //function : planesection
156 //purpose  : 
157 //=======================================================================
158
159 static Standard_Integer planesection(Draw_Interpretor&, Standard_Integer nbarg, const char** argv)
160 {
161   if (nbarg < 4) return 1;
162
163   TopoDS_Shape S = DBRep::Get(argv[2]);
164   if (S.IsNull()) return 1;
165   Handle(Geom_Surface) Surf = DrawTrSurf::GetSurface(argv[3]);
166
167   Handle(Geom_Plane) pl = Handle(Geom_Plane)::DownCast(Surf);
168   if (!pl.IsNull()) {
169     IntPoly_PlaneSection SECTION(S,pl->Pln());
170     //    char name[100];
171     BRep_Builder B;
172     TopoDS_Compound C;
173     B.MakeCompound(C);
174     for (Standard_Integer i = 1 ; i <= SECTION.NbEdges() ; i++) {
175       TopoDS_Shape E = SECTION.Edge(i);
176       if (!E.IsNull()) B.Add(C,E);
177     }
178
179     DBRep::Set(argv[1],C);
180     return 0;
181   }
182   else return  1;
183 }
184
185 //=======================================================================
186 //function : incrementalmesh
187 //purpose  : 
188 //=======================================================================
189
190 static Standard_Integer incrementalmesh(Draw_Interpretor& di, Standard_Integer nbarg, const char** argv)
191 {
192   if (nbarg < 3) {
193     di << " use incmesh shape deflection [inParallel (0/1) : 0 by default]\n";
194     return 0;
195   }
196
197   TopoDS_Shape aShape = DBRep::Get(argv[1]);
198   if (aShape.IsNull()) {
199     di << " null shapes is not allowed here\n";
200     return 0;
201   }
202   Standard_Real aDeflection = Draw::Atof(argv[2]);
203
204   Standard_Boolean isInParallel = Standard_False;
205   if (nbarg == 4) {
206         isInParallel = Draw::Atoi(argv[3]) == 1;
207   }
208   di << "Incremental Mesh, multi-threading "
209     << (isInParallel ? "ON\n" : "OFF\n");
210   
211   BRepMesh_IncrementalMesh MESH(aShape, aDeflection, Standard_False, 0.5, isInParallel);
212   Standard_Integer statusFlags = MESH.GetStatusFlags();  
213
214   di << "Meshing statuses: ";
215
216   if( !statusFlags )
217   {
218     di << "NoError";
219   }
220   else
221   {
222     Standard_Integer i;
223     for( i = 0; i < 4; i++ )
224     {
225       if( (statusFlags >> i) & (Standard_Integer)1 )
226       {
227         switch(i+1)
228         {
229           case 1:
230             di << "OpenWire ";
231             break;
232           case 2:
233             di << "SelfIntersectingWire ";
234             break;
235           case 3:
236             di << "Failure ";
237             break;
238           case 4:
239             di << "ReMesh ";
240             break;
241         }
242       }
243     }
244   }
245
246   return 0;
247 }
248
249 //=======================================================================
250 //function : MemLeakTest
251 //purpose  : 
252 //=======================================================================
253
254 static Standard_Integer MemLeakTest(Draw_Interpretor&, Standard_Integer /*nbarg*/, const char** /*argv*/)
255 {
256   for(int i=0;i<10000;i++)
257   {
258     BRepBuilderAPI_MakePolygon w(gp_Pnt(0,0,0),gp_Pnt(0,100,0),gp_Pnt(20,100,0),gp_Pnt(20,0,0));
259     w.Close();     
260     TopoDS_Wire wireShape( w.Wire());
261     BRepBuilderAPI_MakeFace faceBuilder(wireShape);          
262     TopoDS_Face f( faceBuilder.Face());
263     BRepMesh_IncrementalMesh im(f,1);
264     BRepTools::Clean(f);      
265   }
266   return 0;
267 }
268
269 //=======================================================================
270 //function : fastdiscret
271 //purpose  : 
272 //=======================================================================
273
274 static Standard_Integer fastdiscret(Draw_Interpretor& di, Standard_Integer nbarg, const char** argv)
275 {
276   if (nbarg < 3) return 1;
277
278   TopoDS_Shape S = DBRep::Get(argv[1]);
279   if (S.IsNull()) return 1;
280
281   const Standard_Real d = Draw::Atof(argv[2]);
282
283   Standard_Boolean WithShare = Standard_True;
284   if (nbarg > 3) WithShare = Draw::Atoi(argv[3]);
285
286   Bnd_Box B;
287   BRepBndLib::Add(S,B);
288   BRepMesh_FastDiscret MESH(d,0.5,B,WithShare,Standard_True,Standard_False,Standard_True);
289
290   //Standard_Integer NbIterations = MESH.NbIterations();
291   //if (nbarg > 4) NbIterations = Draw::Atoi(argv[4]);
292   //MESH.NbIterations() = NbIterations;
293
294   di<<"Starting FastDiscret with :"<<"\n";
295   di<<"  Deflection="<<d<<"\n";
296   di<<"  Angle="<<0.5<<"\n";
297   di<<"  SharedMode="<< (Standard_Integer) WithShare<<"\n";
298   //di<<"  NbIterations="<<NbIterations<<"\n";
299
300   Handle(Poly_Triangulation) T;
301   BRep_Builder aBuilder;
302   TopExp_Explorer ex;
303
304   // Clear existing triangulations
305   for (ex.Init(S, TopAbs_FACE); ex.More(); ex.Next())
306     aBuilder.UpdateFace(TopoDS::Face(ex.Current()),T);
307
308   MESH.Perform(S);
309
310   TopoDS_Compound aCompGood, aCompFailed, aCompViolating;
311
312   TopLoc_Location L;
313   Standard_Integer nbtriangles = 0, nbnodes = 0, nbfailed = 0, nbviolating = 0;
314   Standard_Real maxdef = 0.0;
315   for (ex.Init(S, TopAbs_FACE); ex.More(); ex.Next())
316   {
317     T = BRep_Tool::Triangulation(TopoDS::Face(ex.Current()),L);
318     if (T.IsNull())
319     {
320       nbfailed++;
321       if (aCompFailed.IsNull())
322         aBuilder.MakeCompound(aCompFailed);
323       aBuilder.Add(aCompFailed,ex.Current());
324     }
325     else
326     {
327       nbtriangles += T->NbTriangles();
328       nbnodes += T->NbNodes();
329       if (T->Deflection() > maxdef) maxdef = T->Deflection();
330       if (T->Deflection() > d)
331       {
332         nbviolating++;
333         if (aCompViolating.IsNull())
334           aBuilder.MakeCompound(aCompViolating);
335         aBuilder.Add(aCompViolating,ex.Current());
336       }
337       else
338       {
339         if (aCompGood.IsNull())
340           aBuilder.MakeCompound(aCompGood);
341         aBuilder.Add(aCompGood,ex.Current());
342       }
343     }
344   }
345
346   if (!aCompGood.IsNull())
347   {
348     char name[256];
349     strcpy(name,argv[1]);
350     strcat(name,"_good");
351     DBRep::Set(name,aCompGood);
352   }
353   if (!aCompFailed.IsNull())
354   {
355     char name[256];
356     strcpy(name,argv[1]);
357     strcat(name,"_failed");
358     DBRep::Set(name,aCompFailed);
359   }
360   if (!aCompViolating.IsNull())
361   {
362     char name[256];
363     strcpy(name,argv[1]);
364     strcat(name,"_violating");
365     DBRep::Set(name,aCompViolating);
366   }
367
368   di<<"FastDiscret completed with :"<<"\n";
369   di<<"  MaxDeflection="<<maxdef<<"\n";
370   di<<"  NbNodes="<<nbnodes<<"\n";
371   di<<"  NbTriangles="<<nbtriangles<<"\n";
372   di<<"  NbFailed="<<nbfailed<<"\n";
373   di<<"  NbViolating="<<nbviolating<<"\n";
374
375   return 0;
376 }
377
378
379 //=======================================================================
380 //function : triangule
381 //purpose  : 
382 //=======================================================================
383
384
385 class BRepMesh_Couple
386 {
387 public:
388   BRepMesh_Couple() { myI1 = myI2 = 0; }
389   BRepMesh_Couple(const Standard_Integer I1,
390     const Standard_Integer I2)
391   { myI1 = I1; myI2 = I2; }
392
393   Standard_Integer myI1;
394   Standard_Integer myI2;
395 };
396
397 inline Standard_Boolean IsEqual(const BRepMesh_Couple& one,
398                                 const BRepMesh_Couple& other)
399 {
400   if (one.myI1 == other.myI1 &&
401     one.myI2 == other.myI2) return Standard_True;
402   else return Standard_False;
403 }
404
405 inline Standard_Integer HashCode(const BRepMesh_Couple& one,
406                                  const Standard_Integer Upper)
407 {
408   return ::HashCode((one.myI1+one.myI2), Upper);
409 }
410
411 typedef NCollection_Map<BRepMesh_Couple> BRepMesh_MapOfCouple;
412
413
414 static void AddLink(BRepMesh_MapOfCouple& aMap, 
415                     Standard_Integer v1,
416                     Standard_Integer v2)
417 {
418   Standard_Integer i1 = v1;
419   Standard_Integer i2 = v2;
420   if(i1 > i2) {
421     i1 = v2;
422     i2 = v1;
423   }
424   aMap.Add(BRepMesh_Couple(i1,i2));
425 }
426
427 static void MeshStats(const TopoDS_Shape& theSape,
428                       Standard_Integer& theNbTri,
429                       Standard_Integer& theNbEdges,
430                       Standard_Integer& theNbNodes)
431 {
432   theNbTri = 0;
433   theNbEdges = 0;
434   theNbNodes = 0;
435
436   Handle(Poly_Triangulation) T;
437   TopLoc_Location L;
438
439   for ( TopExp_Explorer ex(theSape, TopAbs_FACE); ex.More(); ex.Next()) {
440     TopoDS_Face F = TopoDS::Face(ex.Current());
441     T = BRep_Tool::Triangulation(F, L);
442     if (!T.IsNull()) {
443       theNbTri += T->NbTriangles();
444       theNbNodes += T->NbNodes();
445
446       BRepMesh_MapOfCouple aMap;
447       //count number of links
448       Poly_Array1OfTriangle& Trian = T->ChangeTriangles();
449       for(Standard_Integer i = 1; i<=Trian.Length();i++) {
450         Standard_Integer v1, v2, v3;
451         Trian(i).Get(v1,v2,v3);
452
453         AddLink(aMap, v1, v2);
454         AddLink(aMap, v2, v3);
455         AddLink(aMap, v3, v1);
456       }
457
458       theNbEdges+=aMap.Extent();
459     }
460   }
461 }
462
463 static Standard_Integer triangule(Draw_Interpretor& di, Standard_Integer nbarg, const char** argv)
464 {
465   if (nbarg < 4) return 1;
466
467   Standard_Boolean save = Standard_False;
468
469   const char *id1 = argv[2];
470   TopoDS_Shape S = DBRep::Get(id1);
471   if (S.IsNull()) return 1;
472   di << argv[1] << " ";
473   Standard_Real Deflect=Draw::Atof(argv[3]);
474   if (Deflect<=0.) {
475     di << " Donner la fleche !" << "\n";
476     return 1;
477   }
478
479   if (nbarg >4) {
480     save = (Draw::Atoi(argv[4])==1);
481   }
482
483   Standard_Boolean partage=Standard_True;
484   if (nbarg>5) {
485     partage=Draw::Atoi(argv[5])==1;
486   }
487
488   Handle(MeshTest_DrawableMesh) DM =
489     new MeshTest_DrawableMesh(S,Deflect,partage, save);
490
491   Draw::Set(argv[1],DM);
492
493   Standard_Integer nbn, nbl, nbe;
494   MeshStats(S, nbe, nbl, nbn);
495
496   di<<"(Resultat ("<<nbe<<" mailles) ("<<nbl<<" aretes) ("<<nbn<<" sommets))"<<"\n";
497
498   // passe de verification du maillage.
499   /*Standard_Integer nbc;
500   for (Standard_Integer iLi=1; iLi<= DM->Mesh()->NbEdges(); iLi++) {
501   const BRepMesh_Edge& ed=DM->Mesh()->Edge(iLi);
502   if (ed.Movability()!=BRepMesh_Deleted) {
503   nbc=struc->ElemConnectedTo(iLi).Extent();
504   if (nbc != 1 && nbc != 2) di <<"ERROR MAILLAGE Edge no "<< iLi<<"\n";
505   }
506   }*/
507
508   Bnd_Box bobo;
509
510   for (Standard_Integer lepnt=1; lepnt<DM->Mesh()->NbPoint3d(); lepnt++) {
511     bobo.Add(DM->Mesh()->Point3d(lepnt));
512   }
513   Standard_Real x,y,z,X,Y,Z;
514   bobo.Get(x,y,z,X,Y,Z);
515   Standard_Real delta=Max(X-x,Max(Y-y,Z-z));
516   if (delta>0) delta=Deflect/delta;
517   di << " Fleche de " << delta << " fois la taille de l''objet." << "\n";
518
519   return 0;
520 }
521
522 //=======================================================================
523 //function : addshape
524 //purpose  : 
525 //=======================================================================
526
527 Standard_Integer addshape(Draw_Interpretor&, Standard_Integer n, const char** a)
528 {
529   if (n < 3) return 1;
530   Handle(MeshTest_DrawableMesh) D =
531     Handle(MeshTest_DrawableMesh)::DownCast(Draw::Get(a[1]));
532   if (D.IsNull()) return 1;
533   TopoDS_Shape S = DBRep::Get(a[2]);
534   if (S.IsNull()) return 1;
535
536   D->Add(S);
537   Draw::Repaint();
538
539   return 0;
540 }
541
542
543 //=======================================================================
544 //function : smooth
545 //purpose  : 
546 //=======================================================================
547
548 /*Standard_Integer smooth(Draw_Interpretor&, Standard_Integer n, const char** a)
549 {
550 if (n < 2) return 1;
551 Handle(MeshTest_DrawableMesh) D =
552 Handle(MeshTest_DrawableMesh)::DownCast(Draw::Get(a[1]));
553 if (D.IsNull()) return 1;
554 Handle(BRepMesh_DataStructureOfDelaun) struc=
555 D->Mesh()->Result();
556 BRepMesh_Array1OfVertexOfDelaun toto(1,1);
557 BRepMesh_Delaun trial(struc, 
558 toto,
559 Standard_True);
560 trial.SmoothMesh(0.1);
561 Draw::Repaint();
562 return 0;
563 }
564 */
565
566 //=======================================================================
567 //function : edges
568 //purpose  : 
569 //=======================================================================
570
571 /*static Standard_Integer edges (Draw_Interpretor&, Standard_Integer n, const char** a)
572 {
573 if (n < 3) return 1;
574
575 Handle(MeshTest_DrawableMesh) D =
576 Handle(MeshTest_DrawableMesh)::DownCast(Draw::Get(a[1]));
577 if (D.IsNull()) return 1;
578 TopoDS_Shape S = DBRep::Get(a[2]);
579 if (S.IsNull()) return 1;
580
581 TopExp_Explorer ex;
582 TColStd_SequenceOfInteger& eseq = D->Edges();
583 Handle(BRepMesh_FastDiscret) M = D->Mesh();
584 Handle(BRepMesh_DataStructureOfDelaun) DS = M->Result();
585 Standard_Integer e1, e2, e3, iTri;
586 Standard_Boolean o1, o2, o3;
587
588 // the faces
589 for (ex.Init(S,TopAbs_FACE);ex.More();ex.Next()) {
590 const BRepMesh_MapOfInteger& elems = DS->ElemOfDomain();
591 BRepMesh_MapOfInteger::Iterator it;
592 for (it.Initialize(elems); it.More(); it.Next()) {
593 iTri = it.Key();
594 const BRepMesh_Triangle& triang = M->Triangle(iTri);
595 if (triang.Movability()!=BRepMesh_Deleted) {
596 triang.Edges(e1, e2, e3, o1, o2, o3);
597 eseq.Append(e1);
598 eseq.Append(e2);
599 eseq.Append(e3);
600 }
601 }
602 }
603
604 // the edges
605 //for (ex.Init(S,TopAbs_EDGE,TopAbs_FACE);ex.More();ex.Next()) {
606 //}
607
608 Draw::Repaint();
609 return 0;
610 }
611 */
612
613 //=======================================================================
614 //function : vertices
615 //purpose  : 
616 //=======================================================================
617
618 static Standard_Integer vertices (Draw_Interpretor&, Standard_Integer n, const char** a)
619 {
620   if (n < 3) return 1;
621
622   Handle(MeshTest_DrawableMesh) D =
623     Handle(MeshTest_DrawableMesh)::DownCast(Draw::Get(a[1]));
624   if (D.IsNull()) return 1;
625   TopoDS_Shape S = DBRep::Get(a[2]);
626   if (S.IsNull()) return 1;
627
628   TopExp_Explorer ex;
629   TColStd_SequenceOfInteger& vseq = D->Vertices();
630   Handle(BRepMesh_FastDiscret) M = D->Mesh();
631
632   // the faces
633   for (ex.Init(S,TopAbs_FACE);ex.More();ex.Next()) {
634     BRepMesh_MapOfInteger vtx;
635     M->VerticesOfDomain(vtx);
636     for (BRepMesh_MapOfInteger::Iterator it(vtx); it.More(); it.Next())
637       vseq.Append(it.Key());
638   }
639
640
641   // the edges
642   //for (ex.Init(S,TopAbs_EDGE,TopAbs_FACE);ex.More();ex.Next()) {
643   //}
644
645   Draw::Repaint();
646   return 0;
647 }
648
649 //=======================================================================
650 //function : medge
651 //purpose  : 
652 //=======================================================================
653
654 static Standard_Integer medge (Draw_Interpretor&, Standard_Integer n, const char** a)
655 {
656   if (n < 3) return 1;
657
658   Handle(MeshTest_DrawableMesh) D =
659     Handle(MeshTest_DrawableMesh)::DownCast(Draw::Get(a[1]));
660   if (D.IsNull()) return 1;
661
662   Standard_Integer i,j,e;
663   TColStd_SequenceOfInteger& eseq = D->Edges();
664   for (i = 2; i < n; i++) {
665     e = Draw::Atoi(a[i]);
666     if (e > 0)
667       eseq.Append(e);
668     else if (e < 0) {
669       e = -e;
670       j = 1; 
671       while (j <= eseq.Length()) {
672         if (eseq(j) == e) 
673           eseq.Remove(j);
674         else
675           j++;
676       }
677     }
678     else
679       eseq.Clear();
680   }
681
682   Draw::Repaint();
683   return 0;
684 }
685
686
687 //=======================================================================
688 //function : mvertex
689 //purpose  : 
690 //=======================================================================
691
692 static Standard_Integer mvertex (Draw_Interpretor&, Standard_Integer n, const char** a)
693 {
694   if (n < 3) return 1;
695
696   Handle(MeshTest_DrawableMesh) D =
697     Handle(MeshTest_DrawableMesh)::DownCast(Draw::Get(a[1]));
698   if (D.IsNull()) return 1;
699
700   Standard_Integer i,j,v;
701   TColStd_SequenceOfInteger& vseq = D->Vertices();
702   for (i = 2; i < n; i++) {
703     v = Draw::Atoi(a[i]);
704     if (v > 0)
705       vseq.Append(v);
706     else if (v < 0) {
707       v = -v;
708       j = 1;
709       while (j <= vseq.Length()) {
710         if (vseq(j) == v)
711           vseq.Remove(v);
712         else
713           j++;
714       }
715     }
716     else
717       vseq.Clear();
718   }
719   Draw::Repaint();
720   return 0;
721 }
722
723
724 //=======================================================================
725 //function : triangle
726 //purpose  : 
727 //=======================================================================
728
729 static Standard_Integer triangle (Draw_Interpretor&, Standard_Integer n, const char** a)
730 {
731   if (n < 3) return 1;
732
733   Handle(MeshTest_DrawableMesh) D =
734     Handle(MeshTest_DrawableMesh)::DownCast(Draw::Get(a[1]));
735   if (D.IsNull()) return 1;
736
737   Standard_Integer i,j,v;
738   TColStd_SequenceOfInteger& tseq = D->Triangles();
739   for (i = 2; i < n; i++) {
740     v = Draw::Atoi(a[i]);
741     if (v > 0)
742       tseq.Append(v);
743     else if (v < 0) {
744       v = -v;
745       j = 1;
746       while (j <= tseq.Length()) {
747         if (tseq(j) == v)
748           tseq.Remove(v);
749         else
750           j++;
751       }
752     }
753     else
754       tseq.Clear();
755   }
756   Draw::Repaint();
757   return 0;
758 }
759
760
761 //=======================================================================
762 //function : printdegree
763 //purpose  : 
764 //=======================================================================
765
766 static void printdegree(BRepMesh_DegreeOfFreedom dof, Draw_Interpretor& di)
767 {
768   switch (dof) {
769 case BRepMesh_InVolume :
770   di << "InVolume";
771   break;
772 case BRepMesh_OnSurface :
773   di << "OnSurface";
774   break;
775 case BRepMesh_OnCurve :
776   di << "OnCurve";
777   break;
778 case BRepMesh_Fixed :
779   di << "Fixed";
780   break;
781 case BRepMesh_Frontier :
782   di << "Frontier";
783   break;
784 case BRepMesh_Deleted :
785   di << "Deleted";
786   break;
787 case BRepMesh_Free :
788   di << "Free";
789   break;
790   }
791 }
792
793 //=======================================================================
794 //function : dumpvertex
795 //purpose  : 
796 //=======================================================================
797
798 /*
799 Standard_Integer dumpvertex(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
800 {
801 if (argc < 2) return 1;
802
803 Handle(MeshTest_DrawableMesh) D =
804 Handle(MeshTest_DrawableMesh)::DownCast(Draw::Get(argv[1]));
805 if (D.IsNull()) return 1;
806
807 Handle(BRepMesh_DataStructureOfDelaun) struc = D->Mesh()->Result();
808
809 Standard_Integer in=1;
810 if (argc>=3) {
811 in=Draw::Atoi(argv[2]);
812 in=Max(1,in);
813 }
814 Standard_Integer nbn=in;
815 if (argc>=4) {
816 nbn=Draw::Atoi(argv[3]);
817 nbn=Min(nbn,struc->NbNodes());
818 }
819
820 for (; in<=nbn; in++) {
821 BRepMesh_Vertex nod=struc->GetNode(in);
822 di<<"(node "<<in<<" (uv "<<nod.Coord().X()
823 <<" "<<nod.Coord().Y()<<") (3d "
824 <<nod.Location3d()<<") ";
825 printdegree(nod.Movability(), di);
826 di<<" (edgeconex";
827 BRepMesh_ListOfInteger::Iterator tati(struc->LinkNeighboursOf(in));
828 for (; tati.More(); tati.Next()) di<<" "<<tati.Value();
829 di << "))\n";
830 }
831 di <<"\n";
832 return 0;
833 }
834
835 //=======================================================================
836 //function : dumpedge
837 //purpose  : 
838 //=======================================================================
839
840 Standard_Integer dumpedge(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
841 {
842 if (argc < 2) return 1;
843
844 Handle(MeshTest_DrawableMesh) D =
845 Handle(MeshTest_DrawableMesh)::DownCast(Draw::Get(argv[1]));
846 if (D.IsNull()) return 1;
847
848 Handle(BRepMesh_DataStructureOfDelaun) struc=D->Mesh()->Result();
849 Standard_Integer il=1;
850 if (argc>=3) {
851 il=Draw::Atoi(argv[2]);
852 il=Max(1, il);
853 }
854 Standard_Integer nbl=il;
855 if (argc>=4) {
856 nbl=Draw::Atoi(argv[3]);
857 nbl=Min(nbl, struc->NbLinks());
858 }
859
860 for (; il<=nbl; il++) {
861 BRepMesh_Edge edg=struc->GetLink(il);
862 di << "(edge "<<il<<" ("<<edg.FirstNode()<<" "<<edg.LastNode()
863 <<" ";
864 printdegree(edg.Movability(), di);
865 di<<") (triconex";
866 const BRepMesh_PairOfIndex& pair = struc->ElemConnectedTo(il);
867 for (Standard_Integer j = 1, jn = pair.Extent(); j <= jn; j++)
868 di<<" "<<pair.Index(j);
869 di << "))\n";
870 }
871 di <<"\n";
872 return 0;
873 }
874
875 //=======================================================================
876 //function : dumptriangle
877 //purpose  : 
878 //=======================================================================
879
880 Standard_Integer dumptriangle(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
881 {
882 if (argc < 2) return 1;
883
884 Handle(MeshTest_DrawableMesh) D =
885 Handle(MeshTest_DrawableMesh)::DownCast(Draw::Get(argv[1]));
886 if (D.IsNull()) return 1;
887
888 Handle(BRepMesh_DataStructureOfDelaun) struc=D->Mesh()->Result();
889 Standard_Integer ie=1;
890 if (argc>=3) {
891 ie=Draw::Atoi(argv[2]);
892 ie=Max(1, ie);
893 }
894 Standard_Integer nbe=ie;
895 if (argc>=4) {
896 nbe=Draw::Atoi(argv[3]);
897 nbe=Min(nbe, struc->NbElements());
898 }
899
900 Standard_Integer e1, e2, e3;
901 Standard_Boolean o1, o2, o3;
902
903 for (; ie<=nbe; ie++) {
904 BRepMesh_Triangle tri=struc->GetElement(ie);
905 tri.Edges(e1, e2, e3, o1, o2, o3); 
906 if (o1) e1=-e1;
907 if (o2) e2=-e2;
908 if (o3) e3=-e3;
909 di<<" (maille "<<ie<<" (links "<<e1<<" "
910 <<e2<<" "<<e3<<")";
911 printdegree(tri.Movability(), di);
912 di<<")\n";
913 }
914 di << "\n";
915 return 0;
916 }
917 */
918
919 //=======================================================================
920 //function : trianglesinfo
921 //purpose  : 
922 //=======================================================================
923 static Standard_Integer trianglesinfo(Draw_Interpretor& di, Standard_Integer n, const char** a)
924 {
925   if (n != 2) return 1;
926   TopoDS_Shape S = DBRep::Get(a[1]);
927   if (S.IsNull()) return 1;
928   TopExp_Explorer ex;
929   Handle(Poly_Triangulation) T;
930   TopLoc_Location L;
931
932   Standard_Real MaxDeflection = 0.0;
933   Standard_Integer nbtriangles = 0, nbnodes = 0;
934   for (ex.Init(S, TopAbs_FACE); ex.More(); ex.Next()) {
935     TopoDS_Face F = TopoDS::Face(ex.Current());
936     T = BRep_Tool::Triangulation(F, L);
937     if (!T.IsNull()) {
938       nbtriangles += T->NbTriangles();
939       nbnodes += T->NbNodes();
940       if (T->Deflection() > MaxDeflection)
941         MaxDeflection = T->Deflection();
942     }
943   }
944
945   di<<"\n";
946   di<<"This shape contains " <<nbtriangles<<" triangles."<<"\n";
947   di<<"                    " <<nbnodes    <<" nodes."<<"\n";
948   di<<"Maximal deflection " <<MaxDeflection<<"\n";
949   di<<"\n";
950 #ifdef DEB_MESH_CHRONO
951   Standard_Real tot, addp, unif, contr, inter;
952   Standard_Real edges, mailledges, etuinter, lastcontrol, stock;
953   Standard_Real add11, add12, add2, upda, pointvalid;
954   Standard_Real isos, pointsisos;
955   chTotal.Show(tot); chAddPoint.Show(addp); chUnif.Show(unif); 
956   chControl.Show(contr); chInternal.Show(inter);
957   chEdges.Show(edges); chMaillEdges.Show(mailledges);
958   chEtuInter.Show(etuinter); chLastControl.Show(lastcontrol); 
959   chStock.Show(stock);
960   chAdd11.Show(add11); chAdd12.Show(add12); chAdd2.Show(add2); chUpdate.Show(upda);
961   chPointValid.Show(pointvalid); chIsos.Show(isos); chPointsOnIsos.Show(pointsisos);
962
963   if (tot > 0.00001) {
964     di <<"temps total de maillage:     "<<tot        <<" seconds"<< "\n";
965     di <<"dont: "<< "\n";
966     di <<"discretisation des edges:    "<<edges      <<" seconds---> "<< 100*edges/tot      <<" %"<<"\n";
967     di <<"maillage des edges:          "<<mailledges <<" seconds---> "<< 100*mailledges/tot <<" %"<<"\n";
968     di <<"controle et points internes: "<<etuinter   <<" seconds---> "<< 100*etuinter/tot   <<" %"<<"\n";
969     di <<"derniers controles:          "<<lastcontrol<<" seconds---> "<< 100*lastcontrol/tot<<" %"<<"\n";
970     di <<"stockage dans la S.D.        "<<stock      <<" seconds---> "<< 100*stock/tot      <<" %"<<"\n";
971     di << "\n";
972     di <<"et plus precisement: "<<"\n";
973     di <<"Add 11ere partie :           "<<add11     <<" seconds---> "<<100*add11/tot      <<" %"<<"\n";
974     di <<"Add 12ere partie :           "<<add12     <<" seconds---> "<<100*add12/tot      <<" %"<<"\n";
975     di <<"Add 2eme partie :            "<<add2      <<" seconds---> "<<100*add2/tot       <<" %"<<"\n";
976     di <<"Update :                     "<<upda      <<" seconds---> "<<100*upda/tot       <<" %"<<"\n";
977     di <<"AddPoint :                   "<<addp      <<" seconds---> "<<100*addp/tot       <<" %"<<"\n";
978     di <<"UniformDeflection            "<<unif      <<" seconds---> "<<100*unif/tot       <<" %"<<"\n";
979     di <<"Controle :                   "<<contr     <<" seconds---> "<<100*contr/tot      <<" %"<<"\n";
980     di <<"Points Internes:             "<<inter     <<" seconds---> "<<100*inter/tot      <<" %"<<"\n";
981     di <<"calcul des isos et du, dv:   "<<isos      <<" seconds---> "<<100*isos/tot       <<" %"<<"\n";
982     di <<"calcul des points sur isos:  "<<pointsisos<<" seconds---> "<<100*pointsisos/tot <<" %"<<"\n";
983     di <<"IsPointValid:                "<<pointvalid<<" seconds---> "<<100*pointvalid/tot <<" %"<<"\n";
984     di << "\n";
985
986
987     di <<"nombre d'appels de controle apres points internes          : "<< NbControls << "\n";
988     di <<"nombre de points sur restrictions                          : "<< D0Edges    << "\n";
989     di <<"nombre de points calcules par UniformDeflection            : "<< D0Unif     << "\n";
990     di <<"nombre de points calcules dans InternalVertices            : "<< D0Internal << "\n";
991     di <<"nombre de points calcules dans Control                     : "<< D0Control  << "\n";
992     if (nbnodes-D0Edges != 0) { 
993       Standard_Real ratio = (Standard_Real)(D0Internal+D0Control)/ (Standard_Real)(nbnodes-D0Edges);
994       di <<"---> Ratio: (D0Internal+D0Control) / (nbNodes-nbOnEdges)   : "<< ratio      << "\n";
995     }
996
997     di << "\n";
998
999     chTotal.Reset(); chAddPoint.Reset(); chUnif.Reset(); 
1000     chControl.Reset(); chInternal.Reset();
1001     chEdges.Reset(); chMaillEdges.Reset();
1002     chEtuInter.Reset(); chLastControl.Reset(); 
1003     chStock.Reset();
1004     chAdd11.Reset(); chAdd12.Reset(); chAdd2.Reset(); chUpdate.Reset();
1005     chPointValid.Reset(); chIsos.Reset(); chPointsOnIsos.Reset();
1006
1007   }
1008 #endif
1009   return 0;
1010 }
1011
1012 //=======================================================================
1013 //function : veriftriangles
1014 //purpose  : 
1015 //=======================================================================
1016
1017 static Standard_Integer veriftriangles(Draw_Interpretor& di, Standard_Integer n, const char** a)
1018 {
1019   if (n < 2) return 1;
1020   Standard_Boolean quiet = 1;
1021   if (n == 3) quiet = 0;
1022   TopoDS_Shape Sh = DBRep::Get(a[1]);
1023   if (Sh.IsNull()) return 1;
1024   TopExp_Explorer ex;
1025   Handle(Poly_Triangulation) T;
1026   TopLoc_Location L;
1027   Standard_Integer i, n1, n2, n3;
1028   gp_Pnt2d mitri, v1, v2, v3, mi2d1, mi2d2, mi2d3;
1029   gp_XYZ vecEd1, vecEd2, vecEd3;
1030   //  Standard_Real dipo, dm, dv, d1, d2, d3, defle;
1031   Standard_Real dipo, dv, d1, d2, d3, defle;
1032   Handle(Geom_Surface) S;
1033   Standard_Integer nbface = 0;
1034   gp_Pnt PP;
1035
1036   for (ex.Init(Sh, TopAbs_FACE); ex.More(); ex.Next()) {
1037     TopoDS_Face F = TopoDS::Face(ex.Current());
1038     nbface++;
1039     T = BRep_Tool::Triangulation(F, L);
1040     Standard_Real deflemax = 0, deflemin = 1.e100;
1041     if (!T.IsNull()) {
1042       Standard_Real defstock = T->Deflection();
1043       const Poly_Array1OfTriangle& triangles  = T->Triangles();
1044       const TColgp_Array1OfPnt2d&  Nodes2d    = T->UVNodes();
1045       const TColgp_Array1OfPnt&    Nodes      = T->Nodes();
1046
1047       S = BRep_Tool::Surface(F, L);
1048
1049       for(i = 1; i <= triangles.Length(); i++) {
1050         if (F.Orientation() == TopAbs_REVERSED) 
1051           triangles(i).Get(n1,n3,n2);
1052         else 
1053           triangles(i).Get(n1,n2,n3);
1054
1055         const gp_XY& xy1 = Nodes2d(n1).XY();
1056         const gp_XY& xy2 = Nodes2d(n2).XY();
1057         const gp_XY& xy3 = Nodes2d(n3).XY();
1058
1059         mi2d1.SetCoord((xy2.X()+xy3.X())*0.5, 
1060           (xy2.Y()+xy3.Y())*0.5);
1061         mi2d2.SetCoord((xy1.X()+xy3.X())*0.5, 
1062           (xy1.Y()+xy3.Y())*0.5);
1063         mi2d3.SetCoord((xy1.X()+xy2.X())*0.5, 
1064           (xy1.Y()+xy2.Y())*0.5);
1065
1066         gp_XYZ p1 = Nodes(n1).Transformed(L.Transformation()).XYZ();
1067         gp_XYZ p2 = Nodes(n2).Transformed(L.Transformation()).XYZ();
1068         gp_XYZ p3 = Nodes(n3).Transformed(L.Transformation()).XYZ();
1069
1070         vecEd1=p2-p1;
1071         vecEd2=p3-p2;
1072         vecEd3=p1-p3;
1073         d1=vecEd1.SquareModulus();
1074         d2=vecEd2.SquareModulus();
1075         d3=vecEd3.SquareModulus();
1076
1077         if (d1!=0. && d2!=0. && d3!=0.) {
1078           gp_XYZ equa(vecEd1^vecEd2);
1079           dv=equa.Modulus();
1080           if (dv>0.) {
1081             equa.SetCoord(equa.X()/dv, equa.Y()/dv, equa.Z()/dv);
1082             dipo=equa*p1;
1083
1084
1085             mitri.SetCoord(ONETHIRD*(xy1.X()+xy2.X()+xy3.X()),
1086               ONETHIRD*(xy1.Y()+xy2.Y()+xy3.Y()));
1087             v1.SetCoord(ONETHIRD*mi2d1.X()+TWOTHIRD*xy1.X(), 
1088               ONETHIRD*mi2d1.Y()+TWOTHIRD*xy1.Y());
1089             v2.SetCoord(ONETHIRD*mi2d2.X()+TWOTHIRD*xy2.X(), 
1090               ONETHIRD*mi2d2.Y()+TWOTHIRD*xy2.Y());
1091             v3.SetCoord(ONETHIRD*mi2d3.X()+TWOTHIRD*xy3.X(), 
1092               ONETHIRD*mi2d3.Y()+TWOTHIRD*xy3.Y());
1093
1094             S->D0(mi2d1.X(), mi2d1.Y(), PP);
1095             PP = PP.Transformed(L.Transformation());
1096             defle = Abs((equa*PP.XYZ())-dipo);
1097             deflemax = Max(deflemax, defle);
1098             deflemin = Min(deflemin, defle);
1099
1100             S->D0(mi2d2.X(), mi2d2.Y(), PP);
1101             PP = PP.Transformed(L.Transformation());
1102             defle = Abs((equa*PP.XYZ())-dipo);
1103             deflemax = Max(deflemax, defle);
1104             deflemin = Min(deflemin, defle);
1105
1106             S->D0(mi2d3.X(), mi2d3.Y(), PP);
1107             PP = PP.Transformed(L.Transformation());
1108             defle = Abs((equa*PP.XYZ())-dipo);
1109             deflemax = Max(deflemax, defle);
1110             deflemin = Min(deflemin, defle);
1111
1112             S->D0(v1.X(), v1.Y(), PP);
1113             PP = PP.Transformed(L.Transformation());
1114             defle = Abs((equa*PP.XYZ())-dipo);
1115             deflemax = Max(deflemax, defle);
1116             deflemin = Min(deflemin, defle);
1117
1118             S->D0(v2.X(), v2.Y(), PP);
1119             PP = PP.Transformed(L.Transformation());
1120             defle = Abs((equa*PP.XYZ())-dipo);
1121             deflemax = Max(deflemax, defle);
1122             deflemin = Min(deflemin, defle);
1123
1124             S->D0(v3.X(), v3.Y(), PP);
1125             PP = PP.Transformed(L.Transformation());
1126             defle = Abs((equa*PP.XYZ())-dipo);
1127             deflemax = Max(deflemax, defle);
1128             deflemin = Min(deflemin, defle);
1129
1130             S->D0(mitri.X(), mitri.Y(), PP);
1131             PP = PP.Transformed(L.Transformation());
1132             defle = Abs((equa*PP.XYZ())-dipo);
1133             deflemax = Max(deflemax, defle);
1134             deflemin = Min(deflemin, defle);
1135
1136             if (defle > defstock) {
1137               di <<"face "<< nbface <<" deflection = " << defle <<" pour "<<defstock <<" stockee."<<"\n";
1138             }
1139           }
1140         }
1141       }
1142       if (!quiet) {
1143         di <<"face "<< nbface<<", deflemin = "<< deflemin<<", deflemax = "<<deflemax<<"\n";
1144       }
1145
1146     }
1147   }
1148
1149
1150   return 0;
1151 }
1152
1153
1154
1155
1156 //=======================================================================
1157 //function : tri2d
1158 //purpose  : 
1159 //=======================================================================
1160
1161 Standard_Integer tri2d(Draw_Interpretor&, Standard_Integer n, const char** a)
1162 {
1163
1164   if (n != 2) return 1;
1165   TopoDS_Shape aLocalShape = DBRep::Get(a[1]);
1166   TopoDS_Face F = TopoDS::Face(aLocalShape);
1167   //  TopoDS_Face F = TopoDS::Face(DBRep::Get(a[1]));
1168   if (F.IsNull()) return 1;
1169   Handle(Poly_Triangulation) T;
1170   TopLoc_Location L;
1171
1172   T = BRep_Tool::Triangulation(F, L);
1173   if (!T.IsNull()) {
1174 #ifdef DEB
1175     gp_Trsf tr  = L.Transformation();
1176 #else
1177     L.Transformation();
1178 #endif
1179
1180     // Build the connect tool
1181     Poly_Connect pc(T);
1182
1183     Standard_Integer i,j, nFree, nInternal, nbTriangles = T->NbTriangles();
1184     Standard_Integer t[3];
1185
1186     // count the free edges
1187     nFree = 0;
1188     for (i = 1; i <= nbTriangles; i++) {
1189       pc.Triangles(i,t[0],t[1],t[2]);
1190       for (j = 0; j < 3; j++)
1191         if (t[j] == 0) nFree++;
1192     }
1193
1194     // allocate the arrays
1195     TColStd_Array1OfInteger Free(1,2*nFree);
1196     nInternal = (3*nbTriangles - nFree) / 2;
1197     TColStd_Array1OfInteger Internal(0,2*nInternal);
1198
1199     Standard_Integer fr = 1, in = 1;
1200     const Poly_Array1OfTriangle& triangles = T->Triangles();
1201     Standard_Integer nodes[3];
1202     for (i = 1; i <= nbTriangles; i++) {
1203       pc.Triangles(i,t[0],t[1],t[2]);
1204       triangles(i).Get(nodes[0],nodes[1],nodes[2]);
1205       for (j = 0; j < 3; j++) {
1206         Standard_Integer k = (j+1) % 3;
1207         if (t[j] == 0) {
1208           Free(fr)   = nodes[j];
1209           Free(fr+1) = nodes[k];
1210           fr += 2;
1211         }
1212         // internal edge if this triangle has a lower index than the adjacent
1213         else if (i < t[j]) {
1214           Internal(in)   = nodes[j];
1215           Internal(in+1) = nodes[k];
1216           in += 2;
1217         }
1218       }
1219     }
1220
1221     // Display the edges
1222     if (T->HasUVNodes()) {
1223       const TColgp_Array1OfPnt2d& Nodes2d = T->UVNodes();
1224
1225       Handle(Draw_Segment2D) Seg;
1226
1227       // free edges
1228       Standard_Integer nn;
1229       nn = Free.Length() / 2;
1230       for (i = 1; i <= nn; i++) {
1231         Seg = new Draw_Segment2D(Nodes2d(Free(2*i-1)),
1232           Nodes2d(Free(2*i)),
1233           Draw_rouge);
1234         dout << Seg;
1235       }
1236
1237       // internal edges
1238
1239       nn = nInternal;
1240       for (i = 1; i <= nn; i++) {
1241         Seg = new Draw_Segment2D(Nodes2d(Internal(2*i-1)),
1242           Nodes2d(Internal(2*i)),
1243           Draw_bleu);
1244         dout << Seg;
1245       }
1246     }
1247     dout.Flush();
1248   }
1249
1250   return 0;
1251 }
1252
1253
1254
1255
1256 //=======================================================================
1257 //function : wavefront
1258 //purpose  : 
1259 //=======================================================================
1260
1261 static Standard_Integer wavefront(Draw_Interpretor&, Standard_Integer nbarg, const char** argv)
1262 {
1263   if (nbarg < 2) return 1;
1264
1265   TopoDS_Shape S = DBRep::Get(argv[1]);
1266   if (S.IsNull()) return 1;
1267
1268   // creation du maillage s'il n'existe pas.
1269
1270   Bnd_Box B;
1271   Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
1272   BRepBndLib::Add(S, B);
1273   B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
1274   Standard_Real aDeflection = 
1275     MAX3( aXmax-aXmin , aYmax-aYmin , aZmax-aZmin) * 0.004;
1276
1277   BRepMesh_IncrementalMesh(S, aDeflection);
1278
1279
1280   TopLoc_Location L;
1281   TopExp_Explorer ex;
1282
1283   Standard_Integer i, nbface = 0;
1284   Standard_Boolean OK = Standard_True;
1285   gp_Vec D1U,D1V;
1286   gp_Vec D2U,D2V,D2UV;
1287   gp_Dir Nor;
1288   gp_Pnt P;
1289   Standard_Real U, V;
1290   CSLib_DerivativeStatus Status;
1291   CSLib_NormalStatus NStat;
1292   Standard_Real x, y, z;
1293   Standard_Integer n1, n2, n3;
1294   Standard_Integer k1, k2, k3;
1295
1296   char ffile[100];
1297
1298   if (nbarg == 3) {
1299     strcpy(ffile, argv[2]);
1300     strcat(ffile, ".obj");
1301   }
1302   else  strcpy(ffile, "wave.obj");
1303   FILE* outfile = fopen(ffile, "w");
1304
1305
1306   fprintf(outfile, "%s  %s\n%s %s\n\n", "# CASCADE   ","MATRA DATAVISION", "#", ffile);
1307
1308   Standard_Integer nbNodes, totalnodes = 0, nbpolygons = 0;
1309   for (ex.Init(S, TopAbs_FACE); ex.More(); ex.Next()) {
1310     nbface++;
1311     TopoDS_Face F = TopoDS::Face(ex.Current());
1312     Handle(Poly_Triangulation) Tr = BRep_Tool::Triangulation(F, L);
1313
1314     if (!Tr.IsNull()) {
1315       nbNodes = Tr->NbNodes();
1316       const TColgp_Array1OfPnt& Nodes = Tr->Nodes();
1317
1318       // les noeuds.
1319       for (i = 1; i <= nbNodes; i++) {
1320         gp_Pnt Pnt = Nodes(i).Transformed(L.Transformation());
1321         x = Pnt.X();
1322         y = Pnt.Y();
1323         z = Pnt.Z();
1324         fprintf(outfile, "%s      %f  %f  %f\n", "v", x, y, z);
1325       }
1326
1327       fprintf(outfile, "\n%s    %d\n\n", "# number of vertex", nbNodes);
1328
1329
1330       // les normales.
1331
1332       if (Tr->HasUVNodes()) {
1333         const TColgp_Array1OfPnt2d& UVNodes = Tr->UVNodes();
1334         BRepAdaptor_Surface BS(F, Standard_False);
1335
1336         for (i = 1; i <= nbNodes; i++) {
1337           U = UVNodes(i).X();
1338           V = UVNodes(i).Y();
1339
1340           BS.D1(U,V,P,D1U,D1V);
1341           CSLib::Normal(D1U,D1V,Precision::Angular(),Status,Nor);
1342           if (Status != CSLib_Done) {
1343             BS.D2(U,V,P,D1U,D1V,D2U,D2V,D2UV);
1344             CSLib::Normal(D1U,D1V,D2U,D2V,D2UV,Precision::Angular(),OK,NStat,Nor);
1345           }
1346           if (F.Orientation() == TopAbs_REVERSED) Nor.Reverse();
1347
1348           fprintf(outfile, "%s      %f  %f  %f\n", "vn", Nor.X(), Nor.Y(), Nor.Z());
1349         }
1350
1351         fprintf(outfile, "\n%s    %d\n\n", "# number of vertex normals", nbNodes);
1352       }
1353
1354       fprintf(outfile, "%s    %d\n", "s", nbface);
1355
1356       // les triangles.
1357       Standard_Integer nbTriangles = Tr->NbTriangles();
1358       const Poly_Array1OfTriangle& triangles = Tr->Triangles();
1359
1360
1361       for (i = 1; i <= nbTriangles; i++) {
1362         if (F.Orientation()  == TopAbs_REVERSED)
1363           triangles(i).Get(n1, n3, n2);
1364         else 
1365           triangles(i).Get(n1, n2, n3);
1366         k1 = n1+totalnodes;
1367         k2 = n2+totalnodes;
1368         k3 = n3+totalnodes;
1369         fprintf(outfile, "%s %d%s%d %d%s%d %d%s%d\n", "fo", k1,"//", k1, k2,"//", k2, k3,"//", k3);
1370       }
1371       nbpolygons += nbTriangles;
1372       totalnodes += nbNodes;
1373
1374       fprintf(outfile, "\n%s    %d\n", "# number of smooth groups", nbface);
1375       fprintf(outfile, "\n%s    %d\n", "# number of polygons", nbpolygons);
1376
1377     }
1378   }
1379
1380   fclose(outfile);
1381
1382   return 0;
1383 }
1384
1385
1386 //=======================================================================
1387 //function : onetriangulation
1388 //purpose  : 
1389 //=======================================================================
1390
1391 Standard_Integer onetriangulation(Draw_Interpretor&, Standard_Integer /*nbarg*/, const char** /*argv*/)
1392 {
1393
1394   /*
1395
1396   if (nbarg < 2) return 1;
1397
1398   TopoDS_Shape S = DBRep::Get(argv[1]);
1399   if (S.IsNull()) return 1;
1400
1401   Handle(Poly_Triangulation) TFinale;
1402   char name[100];
1403   Standard_Integer nbshell = 0;
1404
1405   TopExp_Explorer ex, exs, ex2;
1406
1407   for (ex.Init(S, TopAbs_SHELL); ex.More(); ex.Next()) {
1408   nbshell++;
1409   TopoDS_Shell Sh = TopoDS::Shell(ex.Current());
1410
1411   for (exs.Init(Sh, TopAbs_Face); exs.More(); exs.Next()) {
1412   TopoDS_Face F = TopoDS::Face(exs.Current());
1413   Handle(Poly_Triangulation) T = BRep_Tool::Triangulation(F, L);
1414
1415   for (ex2.Init(F, TopAbs_EDGE); ex2.More(); ex2.Next()) {
1416   TopoDS_Edge edge = TopoDS::Edge(ex2.Current());
1417   const TColgp_Array1OfPnt& Nodes = T->Nodes();
1418   const Poly_Array1OfTriangle& triangles = T->Triangles();
1419
1420   if (mapedges.IsBound(edge)) {
1421   const TColStd_ListOfTransient& L = edges.Find(edge);
1422   const Handle(Poly_PolygonOnTriangulation)& P = 
1423   *(Handle(Poly_PolygonOnTriangulation)*)&(L.First());
1424   const TColStd_Array1OfInteger& NOD = P->Nodes();
1425
1426   }
1427   }
1428   }
1429
1430   Sprintf(name, "%s_%i", "tr", nbshell);
1431   DrawTrSurf::Set(name, TFinale);
1432
1433   }
1434
1435   */
1436   return 0;
1437 }
1438
1439
1440 #if 0
1441
1442 //=======================================================================
1443 //function : vb
1444 //purpose  : 
1445 //=======================================================================
1446
1447 Standard_Integer vb(Draw_Interpretor& di, Standard_Integer nbarg, const char** argv)
1448 {
1449   Standard_Integer NbPoints = 1, Deg = 1;
1450
1451   for (Deg = 1; Deg <= 25; Deg++) {
1452     for (NbPoints = 1; NbPoints <= 24; NbPoints++) {
1453
1454       math_Vector GaussP(1, NbPoints), GaussW(1, NbPoints);
1455       math_Vector TheWeights(1, NbPoints), VBParam(1, NbPoints);
1456       math_Matrix VB(1, Deg+1, 1, NbPoints);
1457
1458       math::GaussPoints(NbPoints, GaussP);
1459
1460       Standard_Integer i, j, classe = Deg+1, cl1 = Deg;
1461
1462       // calcul et mise en ordre des parametres et des poids:
1463       for (i = 1; i <= NbPoints; i++) {
1464         if (i <=  (NbPoints+1)/2) {
1465           VBParam(NbPoints-i+1)  = 0.5*(1 + GaussP(i));
1466         }
1467         else {
1468           VBParam(i-(NbPoints+1)/2)  = 0.5*(1 + GaussP(i));
1469         }
1470       }
1471
1472
1473       // Calcul du VB (Valeur des fonctions de Bernstein):
1474       for (i = 1; i <= classe; i++) {
1475         for (j = 1; j <= NbPoints; j++) {
1476           VB(i,j)=PLib::Binomial(cl1,i-1)*Pow((1-VBParam(j)),classe-i)*Pow(VBParam(j),i-1);
1477         }
1478       }
1479
1480
1481       for (i = 1; i <= classe; i++) {
1482         for (j = 1; j <= NbPoints; j++) {
1483           di<< VB(i, j) << ", ";
1484         }
1485       }
1486       di << "\n" << "\n";
1487     }
1488   }
1489   return 0;
1490 }  
1491 //=======================================================================
1492 //function : extrema
1493 //purpose  : 
1494 //=======================================================================
1495
1496 Standard_Integer extrema(Draw_Interpretor& di, Standard_Integer nbarg, const char** argv)
1497 {
1498
1499
1500   Handle(Geom_Curve) C = DrawTrSurf::GetCurve(argv[1]);
1501
1502   Standard_Real X, Y, Z, U0;
1503   X = Draw::Atof(argv[2]);
1504   Y = Draw::Atof(argv[3]);
1505   Z = Draw::Atof(argv[4]);
1506   U0 = Draw::Atof(argv[5]);
1507
1508   gp_Pnt P(X, Y, Z);
1509   GeomAdaptor_Curve GC(C);
1510   Standard_Real tol = 1.e-09;
1511   Extrema_LocateExtPC ext(P, GC, U0, tol);
1512
1513   if (ext.IsDone()) {
1514     gp_Pnt P1 = ext.Point().Value();
1515     di <<"distance =  "<<ext.Value() << "\n";
1516     di <<"point =     "<<P1.X()<<" "<<P1.Y()<<" "<< P1.Z()<< "\n";
1517     di <<"parametre = "<<ext.Point().Parameter()<<"\n";
1518   }
1519
1520   return 0;
1521 }
1522
1523 #endif
1524
1525
1526 //=======================================================================
1527 //function : triedgepoints
1528 //purpose  : 
1529 //=======================================================================
1530
1531 Standard_Integer triedgepoints(Draw_Interpretor& di, Standard_Integer nbarg, const char** argv)
1532 {
1533   if( nbarg < 2 )
1534     return 1;
1535
1536   for( Standard_Integer i = 1; i < nbarg; i++ )
1537   {
1538     TopoDS_Shape aShape = DBRep::Get(argv[i]);
1539     if ( aShape.IsNull() )
1540       continue;
1541
1542     Handle(Poly_PolygonOnTriangulation) aPoly;
1543     Handle(Poly_Triangulation)          aT;
1544     TopLoc_Location                     aLoc;
1545     TopTools_MapOfShape                 anEdgeMap;
1546     TopTools_MapIteratorOfMapOfShape    it;
1547     
1548     if( aShape.ShapeType() == TopAbs_EDGE )
1549     {
1550       anEdgeMap.Add( aShape );
1551     }
1552     else
1553     {
1554       TopExp_Explorer ex(aShape, TopAbs_EDGE);
1555       for(; ex.More(); ex.Next() )
1556         anEdgeMap.Add( ex.Current() );
1557     }
1558
1559     if ( anEdgeMap.Extent() == 0 )
1560       continue;
1561
1562     char newname[1024];
1563     strcpy(newname,argv[i]);
1564     char* p = newname;
1565     while (*p != '\0') p++;
1566     *p = '_';
1567     p++;
1568
1569     Standard_Integer nbEdge = 1;
1570     for(it.Initialize(anEdgeMap); it.More(); it.Next())
1571     {
1572       BRep_Tool::PolygonOnTriangulation(TopoDS::Edge(it.Key()), aPoly, aT, aLoc);
1573       if ( aT.IsNull() || aPoly.IsNull() )
1574         continue;
1575       
1576       const TColgp_Array1OfPnt&      Nodes   = aT->Nodes();
1577       const TColStd_Array1OfInteger& Indices = aPoly->Nodes();
1578       const Standard_Integer         nbnodes = Indices.Length();
1579
1580       for( Standard_Integer j = 1; j <= nbnodes; j++ )
1581       {
1582         gp_Pnt P3d = Nodes(Indices(j));
1583         if( !aLoc.IsIdentity() )
1584           P3d.Transform(aLoc.Transformation());
1585
1586         if( anEdgeMap.Extent() > 1 )
1587           Sprintf(p,"%d_%d",nbEdge,j);
1588         else
1589           Sprintf(p,"%d",j);
1590               DBRep::Set( newname, BRepBuilderAPI_MakeVertex(P3d) );
1591               di.AppendElement(newname);
1592       }
1593       nbEdge++;
1594     }
1595   }
1596   return 0;
1597 }
1598
1599 //=======================================================================
1600 void  MeshTest::Commands(Draw_Interpretor& theCommands)
1601 //=======================================================================
1602 {
1603   Draw::Commands(theCommands);
1604   BRepTest::AllCommands(theCommands);
1605   GeometryTest::AllCommands(theCommands);
1606   MeshTest::PluginCommands(theCommands);
1607   const char* g;
1608
1609   g = "Mesh Commands";
1610
1611   theCommands.Add("shpsec","shpsec result shape shape",__FILE__, shapesection, g);
1612   theCommands.Add("plnsec","plnsec result shape plane",__FILE__, planesection, g);
1613   theCommands.Add("incmesh","incmesh shape deflection [inParallel (0/1) : 0 by default]",__FILE__, incrementalmesh, g);
1614   theCommands.Add("MemLeakTest","MemLeakTest",__FILE__, MemLeakTest, g);
1615   theCommands.Add("fastdiscret","fastdiscret shape deflection [shared [nbiter]]",__FILE__, fastdiscret, g);
1616   theCommands.Add("mesh","mesh result Shape deflection [save partage]",__FILE__, triangule, g);
1617   theCommands.Add("addshape","addshape meshname Shape [deflection]",__FILE__, addshape, g);
1618   //theCommands.Add("smooth","smooth meshname",__FILE__, smooth, g);
1619   //theCommands.Add("edges","edges mesh shape, highlight the edges",__FILE__,edges, g);
1620   theCommands.Add("vertices","vertices mesh shape, highlight the vertices",__FILE__,vertices, g);
1621   theCommands.Add("medge","medge mesh [-]index (0 to clear all)",__FILE__,medge, g);
1622   theCommands.Add("mvertex","mvertex mesh [-]index (0 to clear all)",__FILE__,mvertex, g);
1623   theCommands.Add("triangle","triangle mesh [-]index (0 to clear all)",__FILE__,triangle, g);
1624   //theCommands.Add("dumpvertex","dumpvertex mesh [index]",__FILE__,dumpvertex, g);
1625   //theCommands.Add("dumpedge","dumpedge mesh [index]",__FILE__,dumpedge, g);
1626   //theCommands.Add("dumptriangle","dumptriangle mesh [index]",__FILE__,dumptriangle, g);
1627
1628   theCommands.Add("tri2d", "tri2d facename",__FILE__, tri2d, g);
1629   theCommands.Add("trinfo","trinfo name, print triangles information on objects",__FILE__,trianglesinfo,g);
1630   theCommands.Add("veriftriangles","veriftriangles name, verif triangles",__FILE__,veriftriangles,g);
1631   theCommands.Add("wavefront","wavefront name",__FILE__, wavefront, g);
1632   theCommands.Add("onetriangulation","onetriangulation name",__FILE__, onetriangulation, g);
1633   theCommands.Add("triepoints", "triepoints shape1 [shape2 ...]",__FILE__, triedgepoints, g);
1634
1635 #if 0
1636   theCommands.Add("extrema","extrema ",__FILE__, extrema, g);
1637   theCommands.Add("vb","vb ",__FILE__, vb, g);
1638 #endif
1639 }