1 // Created on: 2008-04-11
2 // Created by: Peter KURNEV
3 // Copyright (c) 2008-2012 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
21 #include <MeshTest.ixx>
22 #include <Draw_Interpretor.hxx>
23 #include <TColStd_MapOfAsciiString.hxx>
24 #include <BRepMesh_DiscretFactory.hxx>
25 #include <TCollection_AsciiString.hxx>
26 #include <TColStd_MapIteratorOfMapOfAsciiString.hxx>
27 #include <BRepMesh_FactoryError.hxx>
28 #include <BRepMesh_DiscretRoot.hxx>
29 #include <BRepMesh_IncrementalMesh.hxx>
30 #include <Bnd_Box.hxx>
31 #include <BRepMesh_PDiscretRoot.hxx>
33 #include <TopTools_IndexedMapOfShape.hxx>
35 #include <Poly_Triangulation.hxx>
37 #include <GProp_GProps.hxx>
38 #include <BRepGProp.hxx>
39 #include <DrawTrSurf.hxx>
40 #include <BRep_Tool.hxx>
42 #include <MeshTest_CheckTopology.hxx>
43 #include <TColgp_Array1OfPnt2d.hxx>
44 #include <Poly_Polygon3D.hxx>
45 #include <Poly_Polygon2D.hxx>
46 #include <Standard.hxx>
48 static Standard_Integer mpnames (Draw_Interpretor& , Standard_Integer , const char** );
49 static Standard_Integer mpsetdefaultname (Draw_Interpretor& , Standard_Integer , const char** );
50 static Standard_Integer mpgetdefaultname (Draw_Interpretor& , Standard_Integer , const char** );
51 static Standard_Integer mpsetfunctionname (Draw_Interpretor& , Standard_Integer , const char** );
52 static Standard_Integer mpgetfunctionname (Draw_Interpretor& , Standard_Integer , const char** );
53 static Standard_Integer mperror (Draw_Interpretor& , Standard_Integer , const char** );
54 static Standard_Integer mpincmesh (Draw_Interpretor& , Standard_Integer , const char** );
55 static Standard_Integer mpparallel (Draw_Interpretor& , Standard_Integer , const char** );
56 static Standard_Integer triarea (Draw_Interpretor& , Standard_Integer , const char** );
57 static Standard_Integer tricheck (Draw_Interpretor& , Standard_Integer , const char** );
59 //=======================================================================
60 //function : PluginCommands
62 //=======================================================================
63 void MeshTest::PluginCommands(Draw_Interpretor& theCommands)
65 static Standard_Boolean done = Standard_False;
71 const char* g = "Mesh Commands";
73 theCommands.Add("mpnames" , "use mpnames" , __FILE__, mpnames , g);
74 theCommands.Add("mpsetdefaultname" , "use mpsetdefaultname" , __FILE__, mpsetdefaultname , g);
75 theCommands.Add("mpgetdefaultname" , "use mpgetdefaultname" , __FILE__, mpgetdefaultname , g);
76 theCommands.Add("mpsetfunctionname", "use mpsetfunctionname", __FILE__, mpsetfunctionname , g);
77 theCommands.Add("mpgetfunctionname", "use mpgetfunctionname", __FILE__, mpgetfunctionname , g);
78 theCommands.Add("mperror" , "use mperror" , __FILE__, mperror , g);
79 theCommands.Add("mpincmesh" , "use mpincmesh" , __FILE__, mpincmesh , g);
80 theCommands.Add("mpparallel" , "mpparallel [toTurnOn] : show / set multi-threading flag for incremental mesh",
81 __FILE__, mpparallel, g);
82 theCommands.Add("triarea","shape [eps] (computes triangles and surface area)",__FILE__, triarea, g);
83 theCommands.Add("tricheck", "shape (checks triangulation of shape)", __FILE__, tricheck, g);
87 //=======================================================================
90 //=======================================================================
91 static Standard_Integer mpnames (Draw_Interpretor& , Standard_Integer n, const char** )
94 TColStd_MapIteratorOfMapOfAsciiString aIt;
97 printf(" use mpnames\n");
101 const TColStd_MapOfAsciiString& aMN=BRepMesh_DiscretFactory::Get().Names();
104 printf(" *no names found\n");
108 printf(" *available names:\n");
110 for (; aIt.More(); aIt.Next()) {
111 const TCollection_AsciiString& aName=aIt.Key();
112 printf(" %s\n", aName.ToCString());
117 //=======================================================================
118 //function : mpsetdefaultname
120 //=======================================================================
121 static Standard_Integer mpsetdefaultname (Draw_Interpretor& , Standard_Integer n, const char**a )
123 TCollection_AsciiString aName;
126 printf(" use mpsetdefaultname name\n");
132 if (BRepMesh_DiscretFactory::Get().SetDefaultName (aName))
139 //=======================================================================
140 //function : mpgetdefaultname
142 //=======================================================================
143 static Standard_Integer mpgetdefaultname (Draw_Interpretor& , Standard_Integer n, const char** )
146 printf(" use mpgetdefaultname\n");
150 const TCollection_AsciiString& aName=BRepMesh_DiscretFactory::Get().DefaultName();
151 printf(" *default name: %s\n", aName.ToCString());
155 //=======================================================================
156 //function : mpsetfunctionname
158 //=======================================================================
159 static Standard_Integer mpsetfunctionname (Draw_Interpretor& , Standard_Integer n, const char**a )
161 TCollection_AsciiString aName;
164 printf(" use mpsetfunctionname name\n");
170 if (BRepMesh_DiscretFactory::Get().SetFunctionName (aName))
177 //=======================================================================
178 //function : mpgetdefaultname
180 //=======================================================================
181 static Standard_Integer mpgetfunctionname (Draw_Interpretor& , Standard_Integer n, const char** )
184 printf(" use mpgetfunctionname\n");
188 const TCollection_AsciiString& aName=BRepMesh_DiscretFactory::Get().FunctionName();
189 printf(" *function name: %s\n", aName.ToCString());
193 //=======================================================================
196 //=======================================================================
197 static Standard_Integer mperror (Draw_Interpretor& , Standard_Integer n, const char** )
199 BRepMesh_FactoryError aErr;
202 printf(" use mperror\n");
206 aErr=BRepMesh_DiscretFactory::Get().ErrorStatus();
207 printf(" *ErrorStatus: %d\n", (int)aErr);
212 //=======================================================================
213 //function :mpincmesh
215 //=======================================================================
216 static Standard_Integer mpincmesh (Draw_Interpretor& , Standard_Integer n, const char** a)
218 Standard_Real aDeflection, aAngle;
222 printf(" use mpincmesh s deflection [angle]\n");
228 printf(" null shapes is not allowed here\n");
232 aDeflection=atof(a[2]);
238 Handle(BRepMesh_DiscretRoot) aMeshAlgo = BRepMesh_DiscretFactory::Get().Discret (aS,
242 BRepMesh_FactoryError aErr = BRepMesh_DiscretFactory::Get().ErrorStatus();
243 if (aErr != BRepMesh_FE_NOERROR)
245 printf(" *Factory::Get().ErrorStatus()=%d\n", (int)aErr);
248 if (aMeshAlgo.IsNull())
250 printf(" *Can not create the algo\n");
254 aMeshAlgo->Perform();
255 if (!aMeshAlgo->IsDone())
257 printf(" *Not done\n");
263 //#######################################################################
264 static Standard_Integer triarea (Draw_Interpretor& di, int n, const char ** a)
269 TopoDS_Shape shape = DBRep::Get(a[1]);
270 if (shape.IsNull()) return 1;
271 Standard_Real anEps = -1.;
275 TopTools_IndexedMapOfShape aMapF;
276 TopExp::MapShapes (shape, TopAbs_FACE, aMapF);
278 // detect if a shape has triangulation
279 Standard_Boolean hasPoly = Standard_False;
281 for (i=1; i <= aMapF.Extent(); i++) {
282 const TopoDS_Face& aFace = TopoDS::Face(aMapF(i));
283 TopLoc_Location aLoc;
284 Handle(Poly_Triangulation) aPoly = BRep_Tool::Triangulation(aFace,aLoc);
285 if (!aPoly.IsNull()) {
286 hasPoly = Standard_True;
291 // compute area by triangles
294 for (i=1; i <= aMapF.Extent(); i++) {
295 const TopoDS_Face& aFace = TopoDS::Face(aMapF(i));
296 TopLoc_Location aLoc;
297 Handle(Poly_Triangulation) aPoly = BRep_Tool::Triangulation(aFace,aLoc);
298 if (aPoly.IsNull()) {
299 cout << "face "<<i<<" has no triangulation"<<endl;
302 const Poly_Array1OfTriangle& triangles = aPoly->Triangles();
303 const TColgp_Array1OfPnt& nodes = aPoly->Nodes();
304 for (int j=triangles.Lower(); j <= triangles.Upper(); j++) {
305 const Poly_Triangle& tri = triangles(j);
307 tri.Get (n1, n2, n3);
308 const gp_Pnt& p1 = nodes(n1);
309 const gp_Pnt& p2 = nodes(n2);
310 const gp_Pnt& p3 = nodes(n3);
313 double ar = v1.CrossMagnitude(v2);
320 // compute area by geometry
323 BRepGProp::SurfaceProperties(shape, props);
325 BRepGProp::SurfaceProperties(shape, props, anEps);
326 double aGeomArea = props.Mass();
328 di << aTriArea << " " << aGeomArea << "\n";
332 //#######################################################################
333 static Standard_Integer tricheck (Draw_Interpretor& di, int n, const char ** a)
337 TopoDS_Shape shape = DBRep::Get(a[1]);
338 if (shape.IsNull()) return 1;
340 TopTools_IndexedMapOfShape aMapF;
341 TopExp::MapShapes (shape, TopAbs_FACE, aMapF);
342 Standard_CString name = ".";
345 MeshTest_CheckTopology aCheck(shape);
348 // dump info on free links inside the triangulation
349 Standard_Integer nbFree = 0;
350 Standard_Integer nbFac = aCheck.NbFacesWithFL(), i, k;
352 for (k=1; k <= nbFac; k++) {
353 Standard_Integer nbEdge = aCheck.NbFreeLinks(k);
354 Standard_Integer iF = aCheck.GetFaceNumWithFL(k);
356 di << "free links of face " << iF << "\n";
357 const TopoDS_Face& aFace = TopoDS::Face(aMapF.FindKey(iF));
358 TopLoc_Location aLoc;
359 Handle(Poly_Triangulation) aT = BRep_Tool::Triangulation(aFace, aLoc);
360 const TColgp_Array1OfPnt& aPoints = aT->Nodes();
361 const TColgp_Array1OfPnt2d& aPoints2d = aT->UVNodes();
362 const gp_Trsf& trsf = aLoc.Transformation();
363 TColgp_Array1OfPnt pnts(1,2);
364 TColgp_Array1OfPnt2d pnts2d(1,2);
365 for (i=1; i <= nbEdge; i++) {
366 Standard_Integer n1, n2;
367 aCheck.GetFreeLink(k, i, n1, n2);
368 di << "{" << n1 << " " << n2 << "} ";
369 pnts(1) = aPoints(n1).Transformed(trsf);
370 pnts(2) = aPoints(n2).Transformed(trsf);
371 Handle(Poly_Polygon3D) poly = new Poly_Polygon3D (pnts);
372 DrawTrSurf::Set (name, poly);
373 DrawTrSurf::Set (name, pnts(1));
374 DrawTrSurf::Set (name, pnts(2));
375 pnts2d(1) = aPoints2d(n1);
376 pnts2d(2) = aPoints2d(n2);
377 Handle(Poly_Polygon2D) poly2d = new Poly_Polygon2D (pnts2d);
378 DrawTrSurf::Set (name, poly2d);
379 DrawTrSurf::Set (name, pnts2d(1));
380 DrawTrSurf::Set (name, pnts2d(2));
386 // dump info on cross face errors
387 Standard_Integer nbErr = aCheck.NbCrossFaceErrors();
389 di << "cross face errors: {face1, node1, face2, node2, distance}" << "\n";
390 for (i=1; i <= nbErr; i++) {
391 Standard_Integer iF1, n1, iF2, n2;
393 aCheck.GetCrossFaceError(i, iF1, n1, iF2, n2, aVal);
394 di << "{" << iF1 << " " << n1 << " " << iF2 << " " << n2 << " " << aVal << "} ";
399 // dump info on edges
400 Standard_Integer nbAsync = aCheck.NbAsyncEdges();
402 di << "async edges:" << "\n";
403 for (i=1; i <= nbAsync; i++) {
404 Standard_Integer ie = aCheck.GetAsyncEdgeNum(i);
410 // dump info on free nodes
411 Standard_Integer nbFreeNodes = aCheck.NbFreeNodes();
412 if (nbFreeNodes > 0) {
413 di << "free nodes (in pairs: face / node): " << "\n";
414 for (i=1; i <= nbFreeNodes; i++) {
415 Standard_Integer iface, inode;
416 aCheck.GetFreeNodeNum(i, iface, inode);
417 di << "{" << iface << " " << inode << "} ";
422 // output errors summary to DRAW
423 if ( nbFree > 0 || nbErr > 0 || nbAsync > 0 || nbFreeNodes > 0 )
424 di << "Free_links " << nbFree
425 << " Cross_face_errors " << nbErr
426 << " Async_edges " << nbAsync
427 << " Free_nodes " << nbFreeNodes << "\n";
431 //=======================================================================
432 //function : mpparallel
434 //=======================================================================
435 static int mpparallel (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
439 Standard_Boolean isParallelOn = atoi (argv[1]) == 1;
440 BRepMesh_IncrementalMesh::SetParallelDefault (isParallelOn);
442 Standard::SetReentrant(Standard_True);
444 std::cout << "Incremental Mesh, multi-threading "
445 << (BRepMesh_IncrementalMesh::IsParallelDefault() ? "ON\n" : "OFF\n");