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