1 // File: MeshTest_PluginCommands.cxx
2 // Created: Fri Apr 11 15:41:24 2008
3 // Author: Peter KURNEV
6 #include <MeshTest.ixx>
7 #include <Draw_Interpretor.hxx>
8 #include <TColStd_MapOfAsciiString.hxx>
9 #include <BRepMesh_DiscretFactory.hxx>
10 #include <TCollection_AsciiString.hxx>
11 #include <TColStd_MapIteratorOfMapOfAsciiString.hxx>
12 #include <BRepMesh_FactoryError.hxx>
13 #include <BRepMesh_DiscretRoot.hxx>
14 #include <Bnd_Box.hxx>
15 #include <BRepMesh_PDiscretRoot.hxx>
17 #include <TopTools_IndexedMapOfShape.hxx>
19 #include <Poly_Triangulation.hxx>
21 #include <GProp_GProps.hxx>
22 #include <BRepGProp.hxx>
23 #include <DrawTrSurf.hxx>
24 #include <BRep_Tool.hxx>
26 #include <MeshTest_CheckTopology.hxx>
27 #include <TColgp_Array1OfPnt2d.hxx>
28 #include <Poly_Polygon3D.hxx>
29 #include <Poly_Polygon2D.hxx>
31 static Standard_Integer mpnames (Draw_Interpretor& , Standard_Integer , const char** );
32 static Standard_Integer mpsetdefaultname (Draw_Interpretor& , Standard_Integer , const char** );
33 static Standard_Integer mpgetdefaultname (Draw_Interpretor& , Standard_Integer , const char** );
34 static Standard_Integer mpsetfunctionname (Draw_Interpretor& , Standard_Integer , const char** );
35 static Standard_Integer mpgetfunctionname (Draw_Interpretor& , Standard_Integer , const char** );
36 static Standard_Integer mperror (Draw_Interpretor& , Standard_Integer , const char** );
37 static Standard_Integer mpincmesh (Draw_Interpretor& , Standard_Integer , const char** );
38 static Standard_Integer triarea (Draw_Interpretor& , Standard_Integer , const char** );
39 static Standard_Integer tricheck (Draw_Interpretor& , Standard_Integer , const char** );
41 //=======================================================================
42 //function : PluginCommands
44 //=======================================================================
45 void MeshTest::PluginCommands(Draw_Interpretor& theCommands)
47 static Standard_Boolean done = Standard_False;
53 const char* g = "Mesh Commands";
55 theCommands.Add("mpnames" , "use mpnames" , __FILE__, mpnames , g);
56 theCommands.Add("mpsetdefaultname" , "use mpsetdefaultname" , __FILE__, mpsetdefaultname , g);
57 theCommands.Add("mpgetdefaultname" , "use mpgetdefaultname" , __FILE__, mpgetdefaultname , g);
58 theCommands.Add("mpsetfunctionname", "use mpsetfunctionname", __FILE__, mpsetfunctionname , g);
59 theCommands.Add("mpgetfunctionname", "use mpgetfunctionname", __FILE__, mpgetfunctionname , g);
60 theCommands.Add("mperror" , "use mperror" , __FILE__, mperror , g);
61 theCommands.Add("mpincmesh" , "use mpincmesh" , __FILE__, mpincmesh , g);
62 theCommands.Add("triarea","shape [eps] (computes triangles and surface area)",__FILE__, triarea, g);
63 theCommands.Add("tricheck", "shape (checks triangulation of shape)", __FILE__, tricheck, g);
67 //=======================================================================
70 //=======================================================================
71 static Standard_Integer mpnames (Draw_Interpretor& , Standard_Integer n, const char** )
74 TColStd_MapIteratorOfMapOfAsciiString aIt;
77 printf(" use mpnames\n");
81 const TColStd_MapOfAsciiString& aMN=BRepMesh_DiscretFactory::Get().Names();
84 printf(" *no names found\n");
88 printf(" *available names:\n");
90 for (; aIt.More(); aIt.Next()) {
91 const TCollection_AsciiString& aName=aIt.Key();
92 printf(" %s\n", aName.ToCString());
97 //=======================================================================
98 //function : mpsetdefaultname
100 //=======================================================================
101 static Standard_Integer mpsetdefaultname (Draw_Interpretor& , Standard_Integer n, const char**a )
103 TCollection_AsciiString aName;
106 printf(" use mpsetdefaultname name\n");
112 BRepMesh_DiscretFactory::Get().SetDefaultName(aName);
117 //=======================================================================
118 //function : mpgetdefaultname
120 //=======================================================================
121 static Standard_Integer mpgetdefaultname (Draw_Interpretor& , Standard_Integer n, const char** )
124 printf(" use mpgetdefaultname\n");
128 const TCollection_AsciiString& aName=BRepMesh_DiscretFactory::Get().DefaultName();
129 printf(" *default name: %s\n", aName.ToCString());
133 //=======================================================================
134 //function : mpsetfunctionname
136 //=======================================================================
137 static Standard_Integer mpsetfunctionname (Draw_Interpretor& , Standard_Integer n, const char**a )
139 TCollection_AsciiString aName;
142 printf(" use mpsetfunctionname name\n");
148 BRepMesh_DiscretFactory::Get().SetFunctionName(aName);
153 //=======================================================================
154 //function : mpgetdefaultname
156 //=======================================================================
157 static Standard_Integer mpgetfunctionname (Draw_Interpretor& , Standard_Integer n, const char** )
160 printf(" use mpgetfunctionname\n");
164 const TCollection_AsciiString& aName=BRepMesh_DiscretFactory::Get().FunctionName();
165 printf(" *function name: %s\n", aName.ToCString());
169 //=======================================================================
172 //=======================================================================
173 static Standard_Integer mperror (Draw_Interpretor& , Standard_Integer n, const char** )
175 BRepMesh_FactoryError aErr;
178 printf(" use mperror\n");
182 aErr=BRepMesh_DiscretFactory::Get().ErrorStatus();
183 printf(" *ErrorStatus: %d\n", (int)aErr);
188 //=======================================================================
189 //function :mpincmesh
191 //=======================================================================
192 static Standard_Integer mpincmesh (Draw_Interpretor& , Standard_Integer n, const char** a)
194 Standard_Boolean bIsDone;
195 Standard_Real aDeflection, aAngle;
197 BRepMesh_FactoryError aErr;
198 BRepMesh_PDiscretRoot pAlgo;
201 printf(" use mpincmesh s deflection [angle]\n");
207 printf(" null shapes is not allowed here\n");
211 aDeflection=atof(a[2]);
217 pAlgo=BRepMesh_DiscretFactory::Get().Discret(aS,
221 aErr=BRepMesh_DiscretFactory::Get().ErrorStatus();
222 if (aErr!=BRepMesh_FE_NOERROR) {
223 printf(" *Factory::Get().ErrorStatus()=%d\n", (int)aErr);
227 printf(" *Can not create the algo\n");
232 bIsDone=pAlgo->IsDone();
234 printf(" *Not done\n");
240 //#######################################################################
241 static Standard_Integer triarea (Draw_Interpretor& di, int n, const char ** a)
246 TopoDS_Shape shape = DBRep::Get(a[1]);
247 if (shape.IsNull()) return 1;
248 Standard_Real anEps = -1.;
252 TopTools_IndexedMapOfShape aMapF;
253 TopExp::MapShapes (shape, TopAbs_FACE, aMapF);
255 // detect if a shape has triangulation
256 Standard_Boolean hasPoly = Standard_False;
258 for (i=1; i <= aMapF.Extent(); i++) {
259 const TopoDS_Face& aFace = TopoDS::Face(aMapF(i));
260 TopLoc_Location aLoc;
261 Handle(Poly_Triangulation) aPoly = BRep_Tool::Triangulation(aFace,aLoc);
262 if (!aPoly.IsNull()) {
263 hasPoly = Standard_True;
268 // compute area by triangles
271 for (i=1; i <= aMapF.Extent(); i++) {
272 const TopoDS_Face& aFace = TopoDS::Face(aMapF(i));
273 TopLoc_Location aLoc;
274 Handle(Poly_Triangulation) aPoly = BRep_Tool::Triangulation(aFace,aLoc);
275 if (aPoly.IsNull()) {
276 cout << "face "<<i<<" has no triangulation"<<endl;
279 const Poly_Array1OfTriangle& triangles = aPoly->Triangles();
280 const TColgp_Array1OfPnt& nodes = aPoly->Nodes();
281 for (int j=triangles.Lower(); j <= triangles.Upper(); j++) {
282 const Poly_Triangle& tri = triangles(j);
284 tri.Get (n1, n2, n3);
285 const gp_Pnt& p1 = nodes(n1);
286 const gp_Pnt& p2 = nodes(n2);
287 const gp_Pnt& p3 = nodes(n3);
290 double ar = v1.CrossMagnitude(v2);
297 // compute area by geometry
300 BRepGProp::SurfaceProperties(shape, props);
302 BRepGProp::SurfaceProperties(shape, props, anEps);
303 double aGeomArea = props.Mass();
305 di << aTriArea << " " << aGeomArea << "\n";
309 //#######################################################################
310 static Standard_Integer tricheck (Draw_Interpretor& di, int n, const char ** a)
314 TopoDS_Shape shape = DBRep::Get(a[1]);
315 if (shape.IsNull()) return 1;
317 TopTools_IndexedMapOfShape aMapF;
318 TopExp::MapShapes (shape, TopAbs_FACE, aMapF);
319 Standard_CString name = ".";
322 MeshTest_CheckTopology aCheck(shape);
325 // dump info on free links inside the triangulation
326 Standard_Integer nbFree = 0;
327 Standard_Integer nbFac = aCheck.NbFacesWithFL(), i, k;
329 for (k=1; k <= nbFac; k++) {
330 Standard_Integer nbEdge = aCheck.NbFreeLinks(k);
331 Standard_Integer iF = aCheck.GetFaceNumWithFL(k);
333 di << "free links of face " << iF << "\n";
334 const TopoDS_Face& aFace = TopoDS::Face(aMapF.FindKey(iF));
335 TopLoc_Location aLoc;
336 Handle(Poly_Triangulation) aT = BRep_Tool::Triangulation(aFace, aLoc);
337 const TColgp_Array1OfPnt& aPoints = aT->Nodes();
338 const TColgp_Array1OfPnt2d& aPoints2d = aT->UVNodes();
339 const gp_Trsf& trsf = aLoc.Transformation();
340 TColgp_Array1OfPnt pnts(1,2);
341 TColgp_Array1OfPnt2d pnts2d(1,2);
342 for (i=1; i <= nbEdge; i++) {
343 Standard_Integer n1, n2;
344 aCheck.GetFreeLink(k, i, n1, n2);
345 di << "{" << n1 << " " << n2 << "} ";
346 pnts(1) = aPoints(n1).Transformed(trsf);
347 pnts(2) = aPoints(n2).Transformed(trsf);
348 Handle(Poly_Polygon3D) poly = new Poly_Polygon3D (pnts);
349 DrawTrSurf::Set (name, poly);
350 DrawTrSurf::Set (name, pnts(1));
351 DrawTrSurf::Set (name, pnts(2));
352 pnts2d(1) = aPoints2d(n1);
353 pnts2d(2) = aPoints2d(n2);
354 Handle(Poly_Polygon2D) poly2d = new Poly_Polygon2D (pnts2d);
355 DrawTrSurf::Set (name, poly2d);
356 DrawTrSurf::Set (name, pnts2d(1));
357 DrawTrSurf::Set (name, pnts2d(2));
363 // dump info on cross face errors
364 Standard_Integer nbErr = aCheck.NbCrossFaceErrors();
366 di << "cross face errors: {face1, node1, face2, node2, distance}" << "\n";
367 for (i=1; i <= nbErr; i++) {
368 Standard_Integer iF1, n1, iF2, n2;
370 aCheck.GetCrossFaceError(i, iF1, n1, iF2, n2, aVal);
371 di << "{" << iF1 << " " << n1 << " " << iF2 << " " << n2 << " " << aVal << "} ";
376 // dump info on edges
377 Standard_Integer nbAsync = aCheck.NbAsyncEdges();
379 di << "async edges:" << "\n";
380 for (i=1; i <= nbAsync; i++) {
381 Standard_Integer ie = aCheck.GetAsyncEdgeNum(i);
387 // dump info on free nodes
388 Standard_Integer nbFreeNodes = aCheck.NbFreeNodes();
389 if (nbFreeNodes > 0) {
390 di << "free nodes (in pairs: face / node): " << "\n";
391 for (i=1; i <= nbFreeNodes; i++) {
392 Standard_Integer iface, inode;
393 aCheck.GetFreeNodeNum(i, iface, inode);
394 di << "{" << iface << " " << inode << "} ";
399 // output errors summary to DRAW
400 if ( nbFree > 0 || nbErr > 0 || nbAsync > 0 || nbFreeNodes > 0 )
401 di << "Free_links " << nbFree
402 << " Cross_face_errors " << nbErr
403 << " Async_edges " << nbAsync
404 << " Free_nodes " << nbFreeNodes << "\n";