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>
34 #include <TopTools_IndexedMapOfShape.hxx>
36 #include <Poly_Triangulation.hxx>
38 #include <GProp_GProps.hxx>
39 #include <BRepGProp.hxx>
40 #include <DrawTrSurf.hxx>
41 #include <BRep_Tool.hxx>
43 #include <MeshTest_CheckTopology.hxx>
44 #include <TColgp_Array1OfPnt2d.hxx>
45 #include <Poly_Polygon3D.hxx>
46 #include <Poly_Polygon2D.hxx>
47 #include <Standard.hxx>
48 #include <TopExp_Explorer.hxx>
49 #include <TColStd_Array1OfInteger.hxx>
50 #include <Poly_PolygonOnTriangulation.hxx>
51 #include <TopoDS_Face.hxx>
52 #include <BRepMesh_Edge.hxx>
53 #include <NCollection_Map.hxx>
55 static Standard_Integer mpnames (Draw_Interpretor& , Standard_Integer , const char** );
56 static Standard_Integer mpsetdefaultname (Draw_Interpretor& , Standard_Integer , const char** );
57 static Standard_Integer mpgetdefaultname (Draw_Interpretor& , Standard_Integer , const char** );
58 static Standard_Integer mpsetfunctionname (Draw_Interpretor& , Standard_Integer , const char** );
59 static Standard_Integer mpgetfunctionname (Draw_Interpretor& , Standard_Integer , const char** );
60 static Standard_Integer mperror (Draw_Interpretor& , Standard_Integer , const char** );
61 static Standard_Integer mpincmesh (Draw_Interpretor& , Standard_Integer , const char** );
62 static Standard_Integer mpparallel (Draw_Interpretor& , Standard_Integer , const char** );
63 static Standard_Integer triarea (Draw_Interpretor& , Standard_Integer , const char** );
64 static Standard_Integer tricheck (Draw_Interpretor& , Standard_Integer , const char** );
66 //=======================================================================
67 //function : PluginCommands
69 //=======================================================================
70 void MeshTest::PluginCommands(Draw_Interpretor& theCommands)
72 static Standard_Boolean done = Standard_False;
78 const char* g = "Mesh Commands";
80 theCommands.Add("mpnames" , "use mpnames" , __FILE__, mpnames , g);
81 theCommands.Add("mpsetdefaultname" , "use mpsetdefaultname" , __FILE__, mpsetdefaultname , g);
82 theCommands.Add("mpgetdefaultname" , "use mpgetdefaultname" , __FILE__, mpgetdefaultname , g);
83 theCommands.Add("mpsetfunctionname", "use mpsetfunctionname", __FILE__, mpsetfunctionname , g);
84 theCommands.Add("mpgetfunctionname", "use mpgetfunctionname", __FILE__, mpgetfunctionname , g);
85 theCommands.Add("mperror" , "use mperror" , __FILE__, mperror , g);
86 theCommands.Add("mpincmesh" , "use mpincmesh" , __FILE__, mpincmesh , g);
87 theCommands.Add("mpparallel" , "mpparallel [toTurnOn] : show / set multi-threading flag for incremental mesh",
88 __FILE__, mpparallel, g);
89 theCommands.Add("triarea","shape [eps] (computes triangles and surface area)",__FILE__, triarea, g);
90 theCommands.Add("tricheck", "shape (checks triangulation of shape)", __FILE__, tricheck, g);
94 //=======================================================================
97 //=======================================================================
98 static Standard_Integer mpnames (Draw_Interpretor& , Standard_Integer n, const char** )
100 Standard_Integer aNb;
101 TColStd_MapIteratorOfMapOfAsciiString aIt;
104 printf(" use mpnames\n");
108 const TColStd_MapOfAsciiString& aMN=BRepMesh_DiscretFactory::Get().Names();
111 printf(" *no names found\n");
115 printf(" *available names:\n");
117 for (; aIt.More(); aIt.Next()) {
118 const TCollection_AsciiString& aName=aIt.Key();
119 printf(" %s\n", aName.ToCString());
124 //=======================================================================
125 //function : mpsetdefaultname
127 //=======================================================================
128 static Standard_Integer mpsetdefaultname (Draw_Interpretor& , Standard_Integer n, const char**a )
130 TCollection_AsciiString aName;
133 printf(" use mpsetdefaultname name\n");
139 if (BRepMesh_DiscretFactory::Get().SetDefaultName (aName))
146 //=======================================================================
147 //function : mpgetdefaultname
149 //=======================================================================
150 static Standard_Integer mpgetdefaultname (Draw_Interpretor& , Standard_Integer n, const char** )
153 printf(" use mpgetdefaultname\n");
157 const TCollection_AsciiString& aName=BRepMesh_DiscretFactory::Get().DefaultName();
158 printf(" *default name: %s\n", aName.ToCString());
162 //=======================================================================
163 //function : mpsetfunctionname
165 //=======================================================================
166 static Standard_Integer mpsetfunctionname (Draw_Interpretor& , Standard_Integer n, const char**a )
168 TCollection_AsciiString aName;
171 printf(" use mpsetfunctionname name\n");
177 if (BRepMesh_DiscretFactory::Get().SetFunctionName (aName))
184 //=======================================================================
185 //function : mpgetdefaultname
187 //=======================================================================
188 static Standard_Integer mpgetfunctionname (Draw_Interpretor& , Standard_Integer n, const char** )
191 printf(" use mpgetfunctionname\n");
195 const TCollection_AsciiString& aName=BRepMesh_DiscretFactory::Get().FunctionName();
196 printf(" *function name: %s\n", aName.ToCString());
200 //=======================================================================
203 //=======================================================================
204 static Standard_Integer mperror (Draw_Interpretor& , Standard_Integer n, const char** )
206 BRepMesh_FactoryError aErr;
209 printf(" use mperror\n");
213 aErr=BRepMesh_DiscretFactory::Get().ErrorStatus();
214 printf(" *ErrorStatus: %d\n", (int)aErr);
219 //=======================================================================
220 //function :mpincmesh
222 //=======================================================================
223 static Standard_Integer mpincmesh (Draw_Interpretor& , Standard_Integer n, const char** a)
225 Standard_Real aDeflection, aAngle;
229 printf(" use mpincmesh s deflection [angle]\n");
235 printf(" null shapes is not allowed here\n");
239 aDeflection=Draw::Atof(a[2]);
242 aAngle=Draw::Atof(a[3]);
245 Handle(BRepMesh_DiscretRoot) aMeshAlgo = BRepMesh_DiscretFactory::Get().Discret (aS,
249 BRepMesh_FactoryError aErr = BRepMesh_DiscretFactory::Get().ErrorStatus();
250 if (aErr != BRepMesh_FE_NOERROR)
252 printf(" *Factory::Get().ErrorStatus()=%d\n", (int)aErr);
255 if (aMeshAlgo.IsNull())
257 printf(" *Can not create the algo\n");
261 aMeshAlgo->Perform();
262 if (!aMeshAlgo->IsDone())
264 printf(" *Not done\n");
270 //#######################################################################
271 static Standard_Integer triarea (Draw_Interpretor& di, int n, const char ** a)
276 TopoDS_Shape shape = DBRep::Get(a[1]);
277 if (shape.IsNull()) return 1;
278 Standard_Real anEps = -1.;
280 anEps = Draw::Atof(a[2]);
282 TopTools_IndexedMapOfShape aMapF;
283 TopExp::MapShapes (shape, TopAbs_FACE, aMapF);
285 // detect if a shape has triangulation
286 Standard_Boolean hasPoly = Standard_False;
288 for (i=1; i <= aMapF.Extent(); i++) {
289 const TopoDS_Face& aFace = TopoDS::Face(aMapF(i));
290 TopLoc_Location aLoc;
291 Handle(Poly_Triangulation) aPoly = BRep_Tool::Triangulation(aFace,aLoc);
292 if (!aPoly.IsNull()) {
293 hasPoly = Standard_True;
298 // compute area by triangles
301 for (i=1; i <= aMapF.Extent(); i++) {
302 const TopoDS_Face& aFace = TopoDS::Face(aMapF(i));
303 TopLoc_Location aLoc;
304 Handle(Poly_Triangulation) aPoly = BRep_Tool::Triangulation(aFace,aLoc);
305 if (aPoly.IsNull()) {
306 cout << "face "<<i<<" has no triangulation"<<endl;
309 const Poly_Array1OfTriangle& triangles = aPoly->Triangles();
310 const TColgp_Array1OfPnt& nodes = aPoly->Nodes();
311 for (int j=triangles.Lower(); j <= triangles.Upper(); j++) {
312 const Poly_Triangle& tri = triangles(j);
314 tri.Get (n1, n2, n3);
315 const gp_Pnt& p1 = nodes(n1);
316 const gp_Pnt& p2 = nodes(n2);
317 const gp_Pnt& p3 = nodes(n3);
320 double ar = v1.CrossMagnitude(v2);
327 // compute area by geometry
330 BRepGProp::SurfaceProperties(shape, props);
332 BRepGProp::SurfaceProperties(shape, props, anEps);
333 double aGeomArea = props.Mass();
335 di << aTriArea << " " << aGeomArea << "\n";
339 //#######################################################################
340 Standard_Boolean IsEqual(const BRepMesh_Edge& theFirst, const BRepMesh_Edge& theSecond)
342 return theFirst.IsEqual(theSecond);
345 static Standard_Integer tricheck (Draw_Interpretor& di, int n, const char ** a)
349 TopoDS_Shape shape = DBRep::Get(a[1]);
350 if (shape.IsNull()) return 1;
352 TopTools_IndexedMapOfShape aMapF;
353 TopExp::MapShapes (shape, TopAbs_FACE, aMapF);
354 Standard_CString name = ".";
357 MeshTest_CheckTopology aCheck(shape);
360 // dump info on free links inside the triangulation
361 Standard_Integer nbFree = 0;
362 Standard_Integer nbFac = aCheck.NbFacesWithFL(), i, k;
364 for (k=1; k <= nbFac; k++) {
365 Standard_Integer nbEdge = aCheck.NbFreeLinks(k);
366 Standard_Integer iF = aCheck.GetFaceNumWithFL(k);
368 di << "free links of face " << iF << "\n";
370 const TopoDS_Shape& aShape = aMapF.FindKey(iF);
371 const TopoDS_Face& aFace = TopoDS::Face(aShape);
372 TopLoc_Location aLoc;
373 Handle(Poly_Triangulation) aT = BRep_Tool::Triangulation(aFace, aLoc);
374 const TColgp_Array1OfPnt& aPoints = aT->Nodes();
375 const TColgp_Array1OfPnt2d& aPoints2d = aT->UVNodes();
376 const gp_Trsf& trsf = aLoc.Transformation();
378 TColgp_Array1OfPnt pnts(1,2);
379 TColgp_Array1OfPnt2d pnts2d(1,2);
380 for (i=1; i <= nbEdge; i++) {
381 Standard_Integer n1, n2;
382 aCheck.GetFreeLink(k, i, n1, n2);
383 di << "{" << n1 << " " << n2 << "} ";
384 pnts(1) = aPoints(n1).Transformed(trsf);
385 pnts(2) = aPoints(n2).Transformed(trsf);
386 Handle(Poly_Polygon3D) poly = new Poly_Polygon3D (pnts);
387 DrawTrSurf::Set (name, poly);
388 DrawTrSurf::Set (name, pnts(1));
389 DrawTrSurf::Set (name, pnts(2));
390 pnts2d(1) = aPoints2d(n1);
391 pnts2d(2) = aPoints2d(n2);
392 Handle(Poly_Polygon2D) poly2d = new Poly_Polygon2D (pnts2d);
393 DrawTrSurf::Set (name, poly2d);
394 DrawTrSurf::Set (name, pnts2d(1));
395 DrawTrSurf::Set (name, pnts2d(2));
401 // dump info on cross face errors
402 Standard_Integer nbErr = aCheck.NbCrossFaceErrors();
404 di << "cross face errors: {face1, node1, face2, node2, distance}" << "\n";
405 for (i=1; i <= nbErr; i++) {
406 Standard_Integer iF1, n1, iF2, n2;
408 aCheck.GetCrossFaceError(i, iF1, n1, iF2, n2, aVal);
409 di << "{" << iF1 << " " << n1 << " " << iF2 << " " << n2 << " " << aVal << "} ";
414 // dump info on edges
415 Standard_Integer nbAsync = aCheck.NbAsyncEdges();
417 di << "async edges:" << "\n";
418 for (i=1; i <= nbAsync; i++) {
419 Standard_Integer ie = aCheck.GetAsyncEdgeNum(i);
425 // dump info on free nodes
426 Standard_Integer nbFreeNodes = aCheck.NbFreeNodes();
427 if (nbFreeNodes > 0) {
428 di << "free nodes (in pairs: face / node): " << "\n";
429 for (i=1; i <= nbFreeNodes; i++) {
430 Standard_Integer iface, inode;
431 aCheck.GetFreeNodeNum(i, iface, inode);
432 di << "{" << iface << " " << inode << "} ";
437 // output errors summary to DRAW
438 if ( nbFree > 0 || nbErr > 0 || nbAsync > 0 || nbFreeNodes > 0)
439 di << "Free_links " << nbFree
440 << " Cross_face_errors " << nbErr
441 << " Async_edges " << nbAsync
442 << " Free_nodes " << nbFreeNodes << "\n";
445 Standard_Integer aFaceId = 1;
446 TopExp_Explorer aFaceExp(shape, TopAbs_FACE);
447 for ( ; aFaceExp.More(); aFaceExp.Next(), ++aFaceId)
449 const TopoDS_Shape& aShape = aFaceExp.Current();
450 const TopoDS_Face& aFace = TopoDS::Face(aShape);
452 TopLoc_Location aLoc;
453 Handle(Poly_Triangulation) aT = BRep_Tool::Triangulation(aFace, aLoc);
455 // Iterate boundary edges
456 NCollection_Map<BRepMesh_Edge> aBoundaryEdgeMap;
457 TopExp_Explorer anExp(aShape, TopAbs_EDGE);
458 for ( ; anExp.More(); anExp.Next() )
460 TopLoc_Location anEdgeLoc;
461 const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
462 Handle(Poly_PolygonOnTriangulation) aPoly = BRep_Tool::PolygonOnTriangulation(anEdge, aT, aLoc);
468 const TColStd_Array1OfInteger& anIndices = aPoly->Nodes();
469 Standard_Integer aLower = anIndices.Lower();
470 Standard_Integer anUpper = anIndices.Upper();
472 Standard_Integer aPrevNode = -1;
473 for (Standard_Integer i = aLower; i <= anUpper; ++i)
475 Standard_Integer aNodeIdx = anIndices.Value(i);
478 BRepMesh_Edge aLink(aPrevNode, aNodeIdx, BRepMesh_Frontier);
479 aBoundaryEdgeMap.Add(aLink);
481 aPrevNode = aNodeIdx;
485 if (aBoundaryEdgeMap.Size() == 0)
490 const Poly_Array1OfTriangle& aTris = aT->Triangles();
491 NCollection_Map<BRepMesh_Edge> aFreeEdgeMap;
492 Standard_Integer aTriNum = aTris.Length();
493 for ( Standard_Integer aTriIndx = 1; aTriIndx <= aTriNum; aTriIndx++ )
495 const Poly_Triangle& aTri = aTris(aTriIndx);
496 Standard_Integer aTriNodes[3] = { aTri.Value(1), aTri.Value(2), aTri.Value(3)};
498 for (Standard_Integer i = 1; i <= 3; ++i)
500 Standard_Integer aLastId = aTriNodes[i % 3];
501 Standard_Integer aFirstId = aTriNodes[i - 1];
503 BRepMesh_Edge aLink(aFirstId, aLastId, BRepMesh_Free);
504 if (!aBoundaryEdgeMap.Contains(aLink))
506 if (!aFreeEdgeMap.Add(aLink))
508 aFreeEdgeMap.Remove(aLink);
514 if (aFreeEdgeMap.Size() != 0)
516 di << "Not connected mesh inside face " << aFaceId << "\n";
522 //=======================================================================
523 //function : mpparallel
525 //=======================================================================
526 static int mpparallel (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
530 Standard_Boolean isParallelOn = Draw::Atoi (argv[1]) == 1;
531 BRepMesh_IncrementalMesh::SetParallelDefault (isParallelOn);
533 std::cout << "Incremental Mesh, multi-threading "
534 << (BRepMesh_IncrementalMesh::IsParallelDefault() ? "ON\n" : "OFF\n");