1 // File: GEOMAlgo_FinderShapeOn1.cxx
2 // Created: Fri Mar 4 10:31:06 2005
3 // Author: Peter KURNEV
7 #include <GEOMAlgo_FinderShapeOn1.ixx>
11 #include <Precision.hxx>
12 #include <TColStd_Array1OfInteger.hxx>
13 #include <TColStd_MapOfInteger.hxx>
15 #include <gp_Trsf.hxx>
16 #include <gp_Cylinder.hxx>
19 #include <TColgp_Array1OfPnt.hxx>
21 #include <Poly_Array1OfTriangle.hxx>
22 #include <Poly_Triangle.hxx>
23 #include <Poly_PolygonOnTriangulation.hxx>
24 #include <Poly_Triangulation.hxx>
26 #include <Geom_Curve.hxx>
27 #include <Geom_Surface.hxx>
28 #include <GeomAdaptor_Surface.hxx>
29 #include <GeomAbs_SurfaceType.hxx>
30 #include <GeomAdaptor_Curve.hxx>
31 #include <GeomAbs_CurveType.hxx>
33 #include <TopAbs_State.hxx>
35 #include <TopLoc_Location.hxx>
37 #include <TopoDS_Shape.hxx>
38 #include <TopoDS_Vertex.hxx>
39 #include <TopoDS_Face.hxx>
40 #include <TopoDS_Edge.hxx>
43 #include <TopExp_Explorer.hxx>
45 #include <TopTools_IndexedMapOfShape.hxx>
47 #include <BRep_Tool.hxx>
48 #include <BRepLib_MakeEdge.hxx>
50 #include <GEOMAlgo_ListIteratorOfListOfPnt.hxx>
51 #include <GEOMAlgo_PassKey.hxx>
52 #include <GEOMAlgo_DataMapOfPassKeyInteger.hxx>
53 #include <GEOMAlgo_DataMapIteratorOfDataMapOfPassKeyInteger.hxx>
54 #include <GEOMAlgo_SurfaceTools.hxx>
55 #include <GEOMAlgo_StateCollector.hxx>
58 //=======================================================================
59 //function : GEOMAlgo_FinderShapeOn1
61 //=======================================================================
62 GEOMAlgo_FinderShapeOn1::GEOMAlgo_FinderShapeOn1()
67 myShapeType=TopAbs_VERTEX;
68 myState=GEOMAlgo_ST_UNKNOWN;
72 //=======================================================================
75 //=======================================================================
76 GEOMAlgo_FinderShapeOn1::~GEOMAlgo_FinderShapeOn1()
79 //=======================================================================
80 //function : SetSurface
82 //=======================================================================
83 void GEOMAlgo_FinderShapeOn1::SetSurface(const Handle(Geom_Surface)& aS)
87 //=======================================================================
90 //=======================================================================
91 const Handle(Geom_Surface)& GEOMAlgo_FinderShapeOn1::Surface() const
95 //=======================================================================
96 //function : SetShapeType
98 //=======================================================================
99 void GEOMAlgo_FinderShapeOn1::SetShapeType(const TopAbs_ShapeEnum aType)
103 //=======================================================================
104 //function : ShapeType
106 //=======================================================================
107 TopAbs_ShapeEnum GEOMAlgo_FinderShapeOn1::ShapeType()const
111 //=======================================================================
112 //function : SetState
114 //=======================================================================
115 void GEOMAlgo_FinderShapeOn1::SetState(const GEOMAlgo_State aState)
119 //=======================================================================
122 //=======================================================================
123 GEOMAlgo_State GEOMAlgo_FinderShapeOn1::State() const
127 //=======================================================================
128 //function : SetNbPntsMin
130 //=======================================================================
131 void GEOMAlgo_FinderShapeOn1::SetNbPntsMin(const Standard_Integer aNb)
135 //=======================================================================
136 //function : NbPntsMin
138 //=======================================================================
139 Standard_Integer GEOMAlgo_FinderShapeOn1::NbPntsMin()const
143 //=======================================================================
144 //function : SetNbPntsMax
146 //=======================================================================
147 void GEOMAlgo_FinderShapeOn1::SetNbPntsMax(const Standard_Integer aNb)
151 //=======================================================================
152 //function : NbPntsMax
154 //=======================================================================
155 Standard_Integer GEOMAlgo_FinderShapeOn1::NbPntsMax()const
159 //=======================================================================
162 //=======================================================================
163 const GEOMAlgo_IndexedDataMapOfShapeState& GEOMAlgo_FinderShapeOn1::MSS() const
167 //=======================================================================
170 //=======================================================================
171 const TopTools_ListOfShape& GEOMAlgo_FinderShapeOn1::Shapes() const
173 Standard_Integer i, aNb;
174 TopTools_ListOfShape* pL;
176 pL=(TopTools_ListOfShape*) &myLS;
180 for (i=1; i<=aNb; ++i) {
181 const TopoDS_Shape& aS=myMSS.FindKey(i);
182 if (aS.ShapeType()==myShapeType) {
188 //=======================================================================
191 //=======================================================================
192 void GEOMAlgo_FinderShapeOn1::Perform()
209 if (myShapeType==TopAbs_VERTEX) {
218 if (myShapeType==TopAbs_EDGE) {
227 if (myShapeType==TopAbs_FACE) {
235 //=======================================================================
236 //function : ProcessVertices
238 //=======================================================================
239 void GEOMAlgo_FinderShapeOn1::ProcessVertices()
243 Standard_Boolean bIsConformState;
244 Standard_Integer i, aNb;
246 TopTools_IndexedMapOfShape aM;
249 TopExp::MapShapes(myShape, TopAbs_VERTEX, aM);
251 for (i=1; i<=aNb; ++i) {
252 const TopoDS_Vertex& aV=TopoDS::Vertex(aM(i));
253 aP=BRep_Tool::Pnt(aV);
255 GEOMAlgo_SurfaceTools::GetState(aP, myGAS, myTolerance, aSt);
256 bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
258 if (myShapeType==TopAbs_VERTEX){
259 if (bIsConformState) {
263 else if (bIsConformState || aSt==TopAbs_ON) {
268 //=======================================================================
269 //function : ProcessEdges
271 //=======================================================================
272 void GEOMAlgo_FinderShapeOn1::ProcessEdges()
276 Standard_Boolean bIsConformState, bIsToBreak;
277 Standard_Integer i, aNb, iCnt;
279 TopTools_IndexedMapOfShape aM;
280 TopExp_Explorer aExp;
281 GEOMAlgo_ListIteratorOfListOfPnt aIt;
282 GeomAbs_SurfaceType aType1;
284 aType1=myGAS.GetType();
286 TopExp::MapShapes(myShape, TopAbs_EDGE, aM);
288 for (i=1; i<=aNb; ++i) {
289 GEOMAlgo_ListOfPnt aLP;
290 GEOMAlgo_StateCollector aSC;
292 const TopoDS_Edge& aE=TopoDS::Edge(aM(i));
294 aExp.Init(aE, TopAbs_VERTEX);
295 for (; aExp.More(); aExp.Next()) {
296 const TopoDS_Shape& aV=aExp.Current();
298 bIsConformState=myMSS.Contains(aV);
299 if (!bIsConformState) {
300 break;// vertex has non-conformed state
303 aSt=myMSS.FindFromKey(aV);
304 aSC.AppendState(aSt);
308 if (!bIsConformState) {
309 continue; // vertex has non-conformed state,skip edge
312 if (BRep_Tool::Degenerated(aE)) {
317 if (myState==GEOMAlgo_ST_ON && aType1==GeomAbs_Sphere) {
318 Standard_Real aT1, aT2;
319 Handle(Geom_Curve) aC;
320 GeomAdaptor_Curve aGAC;
321 GeomAbs_CurveType aType2;
323 aC=BRep_Tool::Curve(aE, aT1, aT2);
326 aType2=aGAC.GetType();
327 if (aType2==GeomAbs_Line) {
332 InnerPoints(aE, aLP);
337 bIsConformState=Standard_True;
339 for (iCnt=0; aIt.More(); aIt.Next(), ++iCnt) {
341 if (iCnt > myNbPntsMax) {
346 const gp_Pnt& aP=aIt.Value();
347 GEOMAlgo_SurfaceTools::GetState(aP, myGAS, myTolerance, aSt);
348 bIsToBreak=aSC.AppendState(aSt);
356 bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
357 if (myShapeType==TopAbs_EDGE) {
358 if (bIsConformState) {
362 else if (bIsConformState || aSt==TopAbs_ON) {
365 } // for (i=1; i<=aNb; ++i) next edge
367 //=======================================================================
368 //function : ProcessFaces
370 //=======================================================================
371 void GEOMAlgo_FinderShapeOn1::ProcessFaces()
375 Standard_Boolean bIsConformState, bIsToBreak;
376 Standard_Integer i, aNbF, iCnt;
378 TopTools_IndexedMapOfShape aM;
379 TopExp_Explorer aExp;
380 GEOMAlgo_ListIteratorOfListOfPnt aIt;
381 GeomAbs_SurfaceType aType1, aType2;
383 aType1=myGAS.GetType();
385 TopExp::MapShapes(myShape, TopAbs_FACE, aM);
387 for (i=1; i<=aNbF; ++i) {
388 GEOMAlgo_StateCollector aSC;
389 GEOMAlgo_ListOfPnt aLP;
391 const TopoDS_Face& aF=TopoDS::Face(aM(i));
393 if (myState==GEOMAlgo_ST_ON) {
394 Handle(Geom_Surface) aS;
395 GeomAdaptor_Surface aGAS;
397 aS=BRep_Tool::Surface(aF);
399 aType2=aGAS.GetType();
400 if (aType2!=aType1) {
405 aExp.Init(aF, TopAbs_EDGE);
406 for (; aExp.More(); aExp.Next()) {
407 const TopoDS_Shape& aE=aExp.Current();
408 bIsConformState=myMSS.Contains(aE);
409 if (!bIsConformState) {
410 break;// edge has non-conformed state
413 aSt=myMSS.FindFromKey(aE);
414 aSC.AppendState(aSt);
418 if (!bIsConformState) {
419 continue; // edge has non-conformed state,skip face
422 InnerPoints(aF, aLP);
427 bIsConformState=Standard_True;
429 for (iCnt=0; aIt.More(); aIt.Next(), ++iCnt) {
431 if (iCnt > myNbPntsMax) {
436 const gp_Pnt& aP=aIt.Value();
437 GEOMAlgo_SurfaceTools::GetState(aP, myGAS, myTolerance, aSt);
438 bIsToBreak=aSC.AppendState(aSt);
446 bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
447 if (myShapeType==TopAbs_FACE) {
448 if (bIsConformState) {
452 else if (bIsConformState || aSt==TopAbs_ON) {
455 }// for (i=1; i<=aNb; ++i) next face
457 //=======================================================================
458 //function : ProcessSolids
460 //=======================================================================
461 void GEOMAlgo_FinderShapeOn1::ProcessSolids()
465 Standard_Boolean bIsConformState;
466 Standard_Integer i, aNbS, j, aNbF;
467 TopTools_IndexedMapOfShape aM, aMF;
470 TopExp::MapShapes(myShape, TopAbs_SOLID, aM);
472 for (i=1; i<=aNbS; ++i) {
473 GEOMAlgo_StateCollector aSC;
475 const TopoDS_Shape& aSd=aM(i);
477 TopExp::MapShapes(aSd, TopAbs_FACE, aMF);
479 for (j=1; j<=aNbF; ++j) {
480 const TopoDS_Shape& aF=aMF(j);
481 bIsConformState=myMSS.Contains(aF);
482 if (!bIsConformState) {
483 break;// face has non-conformed state
486 aSt=myMSS.FindFromKey(aF);
487 aSC.AppendState(aSt);
491 if (!bIsConformState) {
492 continue; // face has non-conformed state,skip solid
497 bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
498 if (bIsConformState) {
504 //=======================================================================
505 //function : InnerPoints
507 //=======================================================================
508 void GEOMAlgo_FinderShapeOn1::InnerPoints(const TopoDS_Face& aF,
509 GEOMAlgo_ListOfPnt& aLP)
513 Standard_Integer j, j1, j2, k, n[4], aNbLinks, aNx, aNbMax, aNb;
514 Standard_Integer iCnt, *pIds;
515 TopLoc_Location aLoc;
516 Handle(Poly_Triangulation) aTRF;
517 TColStd_MapOfInteger aMBN;
518 GEOMAlgo_DataMapOfPassKeyInteger aMPKI;
519 GEOMAlgo_DataMapIteratorOfDataMapOfPassKeyInteger aIt;
524 aTRF=BRep_Tool::Triangulation(aF, aLoc);
526 myErrorStatus=20; // no triangulation found
530 const gp_Trsf& aTrsf=aLoc.Transformation();
531 const Poly_Array1OfTriangle& aTrs=aTRF->Triangles();
532 const TColgp_Array1OfPnt& aNodes=aTRF->Nodes();
534 // map link/nbtriangles
537 for (j=j1; j<=j2; ++j) {
538 const Poly_Triangle& aTr=aTrs(j);
539 aTr.Get(n[0], n[1], n[2]);
541 for (k=0; k<3; ++k) {
542 GEOMAlgo_PassKey aPK;
544 aPK.SetIds(n[k], n[k+1]);
545 if (aMPKI.IsBound(aPK)) {
546 Standard_Integer& iCntX=aMPKI.ChangeFind(aPK);
555 // boundary nodes aMBN
556 aNbLinks=aMPKI.Extent();
557 aIt.Initialize(aMPKI);
558 for (; aIt.More(); aIt.Next()) {
561 const GEOMAlgo_PassKey& aPK=aIt.Key();
563 pIds=(Standard_Integer*)aPK.Key();
564 for (k=1; k<3; ++k) {
565 aNx=*(pIds+aNbMax-k);
571 // inner nodes=all_nodes - boundary_nodes
574 for (j=j1; j<=j2; ++j) {
575 if (!aMBN.Contains(j)) {
576 aP=aNodes(j).Transformed(aTrsf);
583 if (!aNb && myNbPntsMin) {
584 // try to fill it yourself
585 Standard_Boolean bIsDone;
586 Standard_Integer aN1, aN2;
587 Handle(Geom_Surface) aS;
588 GeomAdaptor_Surface aGAS;
589 GeomAbs_SurfaceType aType;
591 aS=BRep_Tool::Surface(aF);
593 aType=aGAS.GetType();
594 if (aType==GeomAbs_Plane || aType==GeomAbs_Cylinder) {
596 aNbLinks=aMPKI.Extent();
597 aIt.Initialize(aMPKI);
598 for (; aIt.More(); aIt.Next()) {
601 // take the first having occured inner link
603 const GEOMAlgo_PassKey& aPK=aIt.Key();
605 pIds=(Standard_Integer*)aPK.Key();
606 aN1=*(pIds+aNbMax-1);
607 aN2=*(pIds+aNbMax-2);
608 aP1=aNodes(aN1).Transformed(aTrsf);
609 aP2=aNodes(aN2).Transformed(aTrsf);
611 if (aType==GeomAbs_Cylinder) {
612 Standard_Real aTolSM;
615 aTolSM=1.523e-6;//~1.-cos(0.1 deg)
616 aCyl=aGAS.Cylinder();
617 if (!GEOMAlgo_SurfaceTools::IsCoaxial(aP1, aP2, aCyl, aTolSM)) {
622 BRepLib_MakeEdge aBME(aP1, aP2);
623 bIsDone=aBME.IsDone();
625 myErrorStatus=30; //can not obtain the line fron the link
629 const TopoDS_Shape& aSx=aBME.Shape();
630 const TopoDS_Edge& aE=TopoDS::Edge(aSx);
632 InnerPoints(aE, myNbPntsMin, aLP);
635 }// for (; aIt.More(); aIt.Next())
636 }// if (aType==GeomAbs_Plane || aType==GeomAbs_Cylinder)
637 }// if (!aNb && myNbPntsMin) {
639 //=======================================================================
640 //function : InnerPoints
642 //=======================================================================
643 void GEOMAlgo_FinderShapeOn1::InnerPoints(const TopoDS_Edge& aE,
644 GEOMAlgo_ListOfPnt& aLP)
648 Standard_Integer j, aNbNodes, aIndex, aNb;
649 Handle(Poly_PolygonOnTriangulation) aPTE;
650 Handle(Poly_Triangulation) aTRE;
651 TopLoc_Location aLoc;
655 BRep_Tool::PolygonOnTriangulation(aE, aPTE, aTRE, aLoc);
656 if (aTRE.IsNull() || aPTE.IsNull()) {
657 myErrorStatus=20; // no triangulation found
661 const gp_Trsf& aTrsf=aLoc.Transformation();
662 const TColgp_Array1OfPnt& aNodes=aTRE->Nodes();
664 aNbNodes=aPTE->NbNodes();
665 const TColStd_Array1OfInteger& aInds=aPTE->Nodes();
666 for (j=2; j<aNbNodes; ++j) {
668 aP=aNodes(aIndex).Transformed(aTrsf);
673 if (!aNb && myNbPntsMin) {
674 // try to fill it yourself
675 InnerPoints(aE, myNbPntsMin, aLP);
680 //=======================================================================
681 //function : InnerPoints
683 //=======================================================================
684 void GEOMAlgo_FinderShapeOn1::InnerPoints(const TopoDS_Edge& aE,
685 const Standard_Integer aNbPntsMin,
686 GEOMAlgo_ListOfPnt& aLP)
688 // try to fill it yourself
689 Standard_Boolean bInf1, bInf2;
690 Standard_Integer j, aNbT;
691 Standard_Real dT, aT, aT1, aT2;
693 Handle(Geom_Curve) aC3D;
695 aC3D=BRep_Tool::Curve(aE, aT1, aT2);
700 bInf1=Precision::IsNegativeInfinite(aT1);
701 bInf2=Precision::IsPositiveInfinite(aT2);
702 if (bInf1 || bInf2) {
708 for (j=1; j<=aNbPntsMin; ++j) {
715 //=======================================================================
716 //function : CheckData
718 //=======================================================================
719 void GEOMAlgo_FinderShapeOn1::CheckData()
723 if(mySurface.IsNull()) {
724 myErrorStatus=10; // mySurface=NULL
728 if (myShape.IsNull()) {
729 myErrorStatus=11; // myShape=NULL
733 if (!(myShapeType==TopAbs_VERTEX ||
734 myShapeType==TopAbs_EDGE ||
735 myShapeType==TopAbs_FACE ||
736 myShapeType==TopAbs_SOLID)) {
737 myErrorStatus=12; // unallowed subshape type
741 if (myState==GEOMAlgo_ST_UNKNOWN ||
742 myState==GEOMAlgo_ST_INOUT) {
743 myErrorStatus=13; // unallowed state type
747 GeomAbs_SurfaceType aType;
749 myGAS.Load(mySurface);
750 aType=myGAS.GetType();
751 if (!(aType==GeomAbs_Plane ||
752 aType==GeomAbs_Cylinder ||
753 aType==GeomAbs_Sphere)) {
754 myErrorStatus=14; // unallowed surface type
761 // 10 -mySurface=NULL
763 // 12 -unallowed type of subshapes
764 // 13 -unallowed state
765 // 14 -unallowed surface type
766 // 15 -unallowed surface type
767 // 20- no triangulation found
768 // 30- can not obtain the line from the link
773 #include <OSD_Chronometer.hxx>
774 #include <Standard_Static.hxx>
776 Standard_STATIC(OSD_Chronometer, x_S_Chrono);
777 static void x_StartChrono();
778 static void x_StopChrono();
779 static Standard_Boolean x_IsToShow();
781 //=======================================================================
782 //function : x_StartChrono
784 //=======================================================================
788 x_S_Chrono().Reset();
789 x_S_Chrono().Start();
792 //=======================================================================
793 //function : x_StopChrono
795 //=======================================================================
799 Standard_Real Chrono;
801 x_S_Chrono().Show(Chrono);
802 printf(" Tps: %lf\n", Chrono);
803 //cout << "Tps: " << Chrono << endl;
806 //=======================================================================
807 //function : x_IsToShow
809 //=======================================================================
810 Standard_Boolean x_IsToShow()
812 Standard_Boolean bFlag=Standard_False;
814 char *xr=getenv ("STDCHRONO");
816 if (!strcmp (xr, "yes")) {