1 // Created on: 2008-04-11
2 // Created by: Peter KURNEV
3 // Copyright (c) 2008-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
17 #include <Bnd_Box.hxx>
18 #include <BRep_Tool.hxx>
19 #include <BRepGProp.hxx>
20 #include <BRepMesh_DiscretFactory.hxx>
21 #include <BRepMesh_DiscretRoot.hxx>
22 #include <BRepMesh_Edge.hxx>
23 #include <BRepMesh_FactoryError.hxx>
24 #include <BRepMesh_IncrementalMesh.hxx>
27 #include <Draw_Interpretor.hxx>
28 #include <DrawTrSurf.hxx>
30 #include <GProp_GProps.hxx>
31 #include <MeshTest.hxx>
32 #include <MeshTest_CheckTopology.hxx>
33 #include <NCollection_Map.hxx>
34 #include <Poly_Polygon2D.hxx>
35 #include <Poly_Polygon3D.hxx>
36 #include <Poly_PolygonOnTriangulation.hxx>
37 #include <Poly_Triangulation.hxx>
38 #include <Standard.hxx>
39 #include <TColgp_Array1OfPnt2d.hxx>
40 #include <TCollection_AsciiString.hxx>
41 #include <TColStd_Array1OfInteger.hxx>
42 #include <TColStd_MapIteratorOfMapOfAsciiString.hxx>
43 #include <TColStd_MapOfAsciiString.hxx>
45 #include <TopExp_Explorer.hxx>
47 #include <TopoDS_Face.hxx>
48 #include <TopTools_IndexedMapOfShape.hxx>
49 #include <Geom_BSplineCurve.hxx>
50 #include <Geom2d_BSplineCurve.hxx>
52 static Standard_Integer mpnames (Draw_Interpretor& , Standard_Integer , const char** );
53 static Standard_Integer mpsetdefaultname (Draw_Interpretor& , Standard_Integer , const char** );
54 static Standard_Integer mpgetdefaultname (Draw_Interpretor& , Standard_Integer , const char** );
55 static Standard_Integer mpsetfunctionname (Draw_Interpretor& , Standard_Integer , const char** );
56 static Standard_Integer mpgetfunctionname (Draw_Interpretor& , Standard_Integer , const char** );
57 static Standard_Integer mperror (Draw_Interpretor& , Standard_Integer , const char** );
58 static Standard_Integer mpincmesh (Draw_Interpretor& , Standard_Integer , const char** );
59 static Standard_Integer mpparallel (Draw_Interpretor& , Standard_Integer , const char** );
60 static Standard_Integer triarea (Draw_Interpretor& , Standard_Integer , const char** );
61 static Standard_Integer tricheck (Draw_Interpretor& , Standard_Integer , const char** );
63 //=======================================================================
64 //function : PluginCommands
66 //=======================================================================
67 void MeshTest::PluginCommands(Draw_Interpretor& theCommands)
69 static Standard_Boolean done = Standard_False;
75 const char* g = "Mesh Commands";
77 theCommands.Add("mpnames" , "use mpnames" , __FILE__, mpnames , g);
78 theCommands.Add("mpsetdefaultname" , "use mpsetdefaultname" , __FILE__, mpsetdefaultname , g);
79 theCommands.Add("mpgetdefaultname" , "use mpgetdefaultname" , __FILE__, mpgetdefaultname , g);
80 theCommands.Add("mpsetfunctionname", "use mpsetfunctionname", __FILE__, mpsetfunctionname , g);
81 theCommands.Add("mpgetfunctionname", "use mpgetfunctionname", __FILE__, mpgetfunctionname , g);
82 theCommands.Add("mperror" , "use mperror" , __FILE__, mperror , g);
83 theCommands.Add("mpincmesh" , "use mpincmesh" , __FILE__, mpincmesh , g);
84 theCommands.Add("mpparallel" , "mpparallel [toTurnOn] : show / set multi-threading flag for incremental mesh",
85 __FILE__, mpparallel, g);
86 theCommands.Add("triarea","shape [eps] (computes triangles and surface area)",__FILE__, triarea, g);
87 theCommands.Add("tricheck", "shape [-small] (checks triangulation of shape);\n"
88 "\"-small\"-option allows finding triangles with small area", __FILE__, tricheck, g);
91 //=======================================================================
94 //=======================================================================
95 static Standard_Integer mpnames (Draw_Interpretor& , Standard_Integer n, const char** )
98 TColStd_MapIteratorOfMapOfAsciiString aIt;
101 printf(" use mpnames\n");
105 const TColStd_MapOfAsciiString& aMN=BRepMesh_DiscretFactory::Get().Names();
108 printf(" *no names found\n");
112 printf(" *available names:\n");
114 for (; aIt.More(); aIt.Next()) {
115 const TCollection_AsciiString& aName=aIt.Key();
116 printf(" %s\n", aName.ToCString());
121 //=======================================================================
122 //function : mpsetdefaultname
124 //=======================================================================
125 static Standard_Integer mpsetdefaultname (Draw_Interpretor& , Standard_Integer n, const char**a )
127 TCollection_AsciiString aName;
130 printf(" use mpsetdefaultname name\n");
136 if (BRepMesh_DiscretFactory::Get().SetDefaultName (aName))
143 //=======================================================================
144 //function : mpgetdefaultname
146 //=======================================================================
147 static Standard_Integer mpgetdefaultname (Draw_Interpretor& , Standard_Integer n, const char** )
150 printf(" use mpgetdefaultname\n");
154 const TCollection_AsciiString& aName=BRepMesh_DiscretFactory::Get().DefaultName();
155 printf(" *default name: %s\n", aName.ToCString());
159 //=======================================================================
160 //function : mpsetfunctionname
162 //=======================================================================
163 static Standard_Integer mpsetfunctionname (Draw_Interpretor& , Standard_Integer n, const char**a )
165 TCollection_AsciiString aName;
168 printf(" use mpsetfunctionname name\n");
174 if (BRepMesh_DiscretFactory::Get().SetFunctionName (aName))
181 //=======================================================================
182 //function : mpgetdefaultname
184 //=======================================================================
185 static Standard_Integer mpgetfunctionname (Draw_Interpretor& , Standard_Integer n, const char** )
188 printf(" use mpgetfunctionname\n");
192 const TCollection_AsciiString& aName=BRepMesh_DiscretFactory::Get().FunctionName();
193 printf(" *function name: %s\n", aName.ToCString());
197 //=======================================================================
200 //=======================================================================
201 static Standard_Integer mperror (Draw_Interpretor& , Standard_Integer n, const char** )
203 BRepMesh_FactoryError aErr;
206 printf(" use mperror\n");
210 aErr=BRepMesh_DiscretFactory::Get().ErrorStatus();
211 printf(" *ErrorStatus: %d\n", (int)aErr);
216 //=======================================================================
217 //function :mpincmesh
219 //=======================================================================
220 static Standard_Integer mpincmesh (Draw_Interpretor& , Standard_Integer n, const char** a)
222 Standard_Real aDeflection, aAngle;
226 printf(" use mpincmesh s deflection [angle]\n");
232 printf(" null shapes is not allowed here\n");
236 aDeflection=Draw::Atof(a[2]);
239 aAngle=Draw::Atof(a[3]);
242 Handle(BRepMesh_DiscretRoot) aMeshAlgo = BRepMesh_DiscretFactory::Get().Discret (aS,
246 BRepMesh_FactoryError aErr = BRepMesh_DiscretFactory::Get().ErrorStatus();
247 if (aErr != BRepMesh_FE_NOERROR)
249 printf(" *Factory::Get().ErrorStatus()=%d\n", (int)aErr);
252 if (aMeshAlgo.IsNull())
254 printf(" *Can not create the algo\n");
258 aMeshAlgo->Perform();
259 if (!aMeshAlgo->IsDone())
261 printf(" *Not done\n");
267 //#######################################################################
268 static Standard_Integer triarea (Draw_Interpretor& di, int n, const char ** a)
273 TopoDS_Shape shape = DBRep::Get(a[1]);
274 if (shape.IsNull()) return 1;
275 Standard_Real anEps = -1.;
277 anEps = Draw::Atof(a[2]);
279 TopTools_IndexedMapOfShape aMapF;
280 TopExp::MapShapes (shape, TopAbs_FACE, aMapF);
282 // detect if a shape has triangulation
283 Standard_Boolean hasPoly = Standard_False;
285 for (i=1; i <= aMapF.Extent(); i++) {
286 const TopoDS_Face& aFace = TopoDS::Face(aMapF(i));
287 TopLoc_Location aLoc;
288 Handle(Poly_Triangulation) aPoly = BRep_Tool::Triangulation(aFace,aLoc);
289 if (!aPoly.IsNull()) {
290 hasPoly = Standard_True;
295 // compute area by triangles
298 for (i=1; i <= aMapF.Extent(); i++) {
299 const TopoDS_Face& aFace = TopoDS::Face(aMapF(i));
300 TopLoc_Location aLoc;
301 Handle(Poly_Triangulation) aPoly = BRep_Tool::Triangulation(aFace,aLoc);
302 if (aPoly.IsNull()) {
303 std::cout << "face "<<i<<" has no triangulation"<<std::endl;
306 for (int j = 1; j <= aPoly->NbTriangles(); j++)
308 const Poly_Triangle tri = aPoly->Triangle (j);
310 tri.Get (n1, n2, n3);
311 const gp_Pnt p1 = aPoly->Node (n1);
312 const gp_Pnt p2 = aPoly->Node (n2);
313 const gp_Pnt p3 = aPoly->Node (n3);
316 double ar = v1.CrossMagnitude(v2);
323 // compute area by geometry
326 BRepGProp::SurfaceProperties(shape, props);
328 BRepGProp::SurfaceProperties(shape, props, anEps);
329 double aGeomArea = props.Mass();
331 di << aTriArea << " " << aGeomArea << "\n";
335 //#######################################################################
336 Standard_Boolean IsEqual(const BRepMesh_Edge& theFirst, const BRepMesh_Edge& theSecond)
338 return theFirst.IsEqual(theSecond);
341 static Standard_Integer tricheck (Draw_Interpretor& di, int n, const char ** a)
345 TopoDS_Shape shape = DBRep::Get(a[1]);
346 if (shape.IsNull()) return 1;
348 const Standard_Boolean isToFindSmallTriangles = (n >= 3) ? (strcmp(a[2], "-small") == 0) : Standard_False;
350 TopTools_IndexedMapOfShape aMapF;
351 TopExp::MapShapes (shape, TopAbs_FACE, aMapF);
352 const Standard_CString name = ".";
355 MeshTest_CheckTopology aCheck(shape);
358 // dump info on free links inside the triangulation
359 Standard_Integer nbFree = 0;
360 Standard_Integer nbFac = aCheck.NbFacesWithFL(), i, k;
362 for (k=1; k <= nbFac; k++) {
363 Standard_Integer nbEdge = aCheck.NbFreeLinks(k);
364 Standard_Integer iF = aCheck.GetFaceNumWithFL(k);
366 di << "free links of face " << iF << "\n";
368 const TopoDS_Shape& aShape = aMapF.FindKey(iF);
369 const TopoDS_Face& aFace = TopoDS::Face(aShape);
370 TopLoc_Location aLoc;
371 Handle(Poly_Triangulation) aT = BRep_Tool::Triangulation(aFace, aLoc);
372 const gp_Trsf& trsf = aLoc.Transformation();
374 TColgp_Array1OfPnt pnts(1,2);
375 TColgp_Array1OfPnt2d pnts2d(1,2);
376 for (i=1; i <= nbEdge; i++) {
377 Standard_Integer n1, n2;
378 aCheck.GetFreeLink(k, i, n1, n2);
379 di << "{" << n1 << " " << n2 << "} ";
380 pnts (1) = aT->Node (n1).Transformed (trsf);
381 pnts (2) = aT->Node (n2).Transformed (trsf);
382 Handle(Poly_Polygon3D) poly = new Poly_Polygon3D (pnts);
383 DrawTrSurf::Set (name, poly);
384 DrawTrSurf::Set (name, pnts(1));
385 DrawTrSurf::Set (name, pnts(2));
386 if (aT->HasUVNodes())
388 pnts2d (1) = aT->UVNode (n1);
389 pnts2d (2) = aT->UVNode (n2);
390 Handle(Poly_Polygon2D) poly2d = new Poly_Polygon2D (pnts2d);
391 DrawTrSurf::Set (name, poly2d);
392 DrawTrSurf::Set (name, pnts2d(1));
393 DrawTrSurf::Set (name, pnts2d(2));
400 // dump info on cross face errors
401 Standard_Integer nbErr = aCheck.NbCrossFaceErrors();
403 di << "cross face errors: {face1, node1, face2, node2, distance}\n";
404 for (i=1; i <= nbErr; i++) {
405 Standard_Integer iF1, n1, iF2, n2;
407 aCheck.GetCrossFaceError(i, iF1, n1, iF2, n2, aVal);
408 di << "{" << iF1 << " " << n1 << " " << iF2 << " " << n2 << " " << aVal << "} ";
413 // dump info on edges
414 Standard_Integer nbAsync = aCheck.NbAsyncEdges();
416 di << "async edges:\n";
417 for (i=1; i <= nbAsync; i++) {
418 Standard_Integer ie = aCheck.GetAsyncEdgeNum(i);
424 // dump info on free nodes
425 Standard_Integer nbFreeNodes = aCheck.NbFreeNodes();
426 if (nbFreeNodes > 0) {
427 di << "free nodes (in pairs: face / node): \n";
428 for (i=1; i <= nbFreeNodes; i++) {
429 Standard_Integer iface, inode;
430 aCheck.GetFreeNodeNum(i, iface, inode);
432 const TopoDS_Face& aFace = TopoDS::Face(aMapF.FindKey(iface));
433 TopLoc_Location aLoc;
434 Handle(Poly_Triangulation) aT = BRep_Tool::Triangulation(aFace, aLoc);
435 const gp_Trsf& trsf = aLoc.Transformation();
436 DrawTrSurf::Set (name, aT->Node (inode).Transformed (trsf));
437 if (aT->HasUVNodes())
439 DrawTrSurf::Set (name, aT->UVNode (inode));
442 di << "{" << iface << " " << inode << "} ";
447 const Standard_Integer aNbSmallTriangles = isToFindSmallTriangles? aCheck.NbSmallTriangles() : 0;
448 if (aNbSmallTriangles > 0)
450 di << "triangles with null area (in pairs: face / triangle): \n";
451 for (i = 1; i <= aNbSmallTriangles; i++)
453 Standard_Integer aFaceId = 0, aTriID = 0;
454 aCheck.GetSmallTriangle(i, aFaceId, aTriID);
456 const TopoDS_Face& aFace = TopoDS::Face(aMapF.FindKey(aFaceId));
457 TopLoc_Location aLoc;
458 const gp_Trsf& aTrsf = aLoc.Transformation();
459 const Handle(Poly_Triangulation) aT = BRep_Tool::Triangulation(aFace, aLoc);
460 const Poly_Triangle &aTri = aT->Triangle(aTriID);
461 Standard_Integer aN1, aN2, aN3;
462 aTri.Get(aN1, aN2, aN3);
464 TColgp_Array1OfPnt aPoles(1, 4);
465 aPoles (1) = aPoles (4) = aT->Node (aN1).Transformed (aTrsf);
466 aPoles (2) = aT->Node (aN2).Transformed (aTrsf);
467 aPoles (3) = aT->Node (aN3).Transformed (aTrsf);
469 TColStd_Array1OfInteger aMults(1, 4);
470 aMults(1) = aMults(4) = 2;
471 aMults(2) = aMults(3) = 1;
473 TColStd_Array1OfReal aKnots(1, 4);
479 Handle(Geom_BSplineCurve) aBS = new Geom_BSplineCurve(aPoles, aKnots, aMults, 1);
481 DrawTrSurf::Set(name, aBS);
483 if (aT->HasUVNodes())
485 TColgp_Array1OfPnt2d aPoles2d(1, 4);
486 aPoles2d (1) = aPoles2d (4) = aT->UVNode (aN1);
487 aPoles2d (2) = aT->UVNode (aN2);
488 aPoles2d (3) = aT->UVNode (aN3);
490 Handle(Geom2d_BSplineCurve) aBS2d = new Geom2d_BSplineCurve(aPoles2d, aKnots, aMults, 1);
492 DrawTrSurf::Set(name, aBS2d);
495 di << "{" << aFaceId << " " << aTriID << "} ";
501 // output errors summary to DRAW
506 (aNbSmallTriangles > 0))
508 di << "Free_links " << nbFree
509 << " Cross_face_errors " << nbErr
510 << " Async_edges " << nbAsync
511 << " Free_nodes " << nbFreeNodes
512 << " Small triangles " << aNbSmallTriangles << "\n";
515 Standard_Integer aFaceId = 1;
516 TopExp_Explorer aFaceExp(shape, TopAbs_FACE);
517 for ( ; aFaceExp.More(); aFaceExp.Next(), ++aFaceId)
519 const TopoDS_Shape& aShape = aFaceExp.Current();
520 const TopoDS_Face& aFace = TopoDS::Face(aShape);
522 TopLoc_Location aLoc;
523 Handle(Poly_Triangulation) aT = BRep_Tool::Triangulation(aFace, aLoc);
525 // Iterate boundary edges
526 NCollection_Map<BRepMesh_Edge> aBoundaryEdgeMap;
527 TopExp_Explorer anExp(aShape, TopAbs_EDGE);
528 for ( ; anExp.More(); anExp.Next() )
530 TopLoc_Location anEdgeLoc;
531 const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
532 Handle(Poly_PolygonOnTriangulation) aPoly = BRep_Tool::PolygonOnTriangulation(anEdge, aT, aLoc);
538 const TColStd_Array1OfInteger& anIndices = aPoly->Nodes();
539 Standard_Integer aLower = anIndices.Lower();
540 Standard_Integer anUpper = anIndices.Upper();
542 Standard_Integer aPrevNode = -1;
543 for (Standard_Integer j = aLower; j <= anUpper; ++j)
545 Standard_Integer aNodeIdx = anIndices.Value(j);
548 BRepMesh_Edge aLink(aPrevNode, aNodeIdx, BRepMesh_Frontier);
549 aBoundaryEdgeMap.Add(aLink);
551 aPrevNode = aNodeIdx;
555 if (aBoundaryEdgeMap.Size() == 0)
560 NCollection_Map<BRepMesh_Edge> aFreeEdgeMap;
561 const Standard_Integer aTriNum = aT->NbTriangles();
562 for ( Standard_Integer aTriIndx = 1; aTriIndx <= aTriNum; aTriIndx++ )
564 const Poly_Triangle aTri = aT->Triangle (aTriIndx);
565 Standard_Integer aTriNodes[3] = { aTri.Value(1), aTri.Value(2), aTri.Value(3)};
567 for (Standard_Integer j = 1; j <= 3; ++j)
569 Standard_Integer aLastId = aTriNodes[j % 3];
570 Standard_Integer aFirstId = aTriNodes[j - 1];
572 BRepMesh_Edge aLink(aFirstId, aLastId, BRepMesh_Free);
573 if (!aBoundaryEdgeMap.Contains(aLink))
575 if (!aFreeEdgeMap.Add(aLink))
577 aFreeEdgeMap.Remove(aLink);
583 if (aFreeEdgeMap.Size() != 0)
585 di << "Not connected mesh inside face " << aFaceId << "\n";
587 const gp_Trsf& trsf = aLoc.Transformation();
589 TColgp_Array1OfPnt pnts(1,2);
590 TColgp_Array1OfPnt2d pnts2d(1,2);
591 NCollection_Map<BRepMesh_Edge>::Iterator aMapIt(aFreeEdgeMap);
592 for (; aMapIt.More(); aMapIt.Next())
594 const BRepMesh_Edge& aLink = aMapIt.Key();
595 di << "{" << aLink.FirstNode() << " " << aLink.LastNode() << "} ";
596 pnts (1) = aT->Node (aLink.FirstNode()).Transformed (trsf);
597 pnts (2) = aT->Node (aLink.LastNode()).Transformed (trsf);
598 Handle(Poly_Polygon3D) poly = new Poly_Polygon3D (pnts);
599 DrawTrSurf::Set (name, poly);
600 DrawTrSurf::Set (name, pnts(1));
601 DrawTrSurf::Set (name, pnts(2));
602 if (aT->HasUVNodes())
604 pnts2d (1) = aT->UVNode (aLink.FirstNode());
605 pnts2d (2) = aT->UVNode (aLink.LastNode());
606 Handle(Poly_Polygon2D) poly2d = new Poly_Polygon2D (pnts2d);
607 DrawTrSurf::Set (name, poly2d);
608 DrawTrSurf::Set (name, pnts2d(1));
609 DrawTrSurf::Set (name, pnts2d(2));
618 //=======================================================================
619 //function : mpparallel
621 //=======================================================================
622 static int mpparallel (Draw_Interpretor& /*di*/, Standard_Integer argc, const char** argv)
626 Standard_Boolean isParallelOn = Draw::Atoi (argv[1]) == 1;
627 BRepMesh_IncrementalMesh::SetParallelDefault (isParallelOn);
629 std::cout << "Incremental Mesh, multi-threading "
630 << (BRepMesh_IncrementalMesh::IsParallelDefault() ? "ON\n" : "OFF\n");