1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File: GEOMAlgo_FinderShapeOn1.cxx
21 // Created: Fri Mar 4 10:31:06 2005
22 // Author: Peter KURNEV
26 #include <GEOMAlgo_FinderShapeOn1.ixx>
30 #include <Precision.hxx>
31 #include <TColStd_Array1OfInteger.hxx>
32 #include <TColStd_MapOfInteger.hxx>
34 #include <gp_Trsf.hxx>
35 #include <gp_Cylinder.hxx>
38 #include <TColgp_Array1OfPnt.hxx>
40 #include <Poly_Array1OfTriangle.hxx>
41 #include <Poly_Triangle.hxx>
42 #include <Poly_PolygonOnTriangulation.hxx>
43 #include <Poly_Triangulation.hxx>
45 #include <Geom_Curve.hxx>
46 #include <Geom_Surface.hxx>
47 #include <GeomAdaptor_Surface.hxx>
48 #include <GeomAbs_SurfaceType.hxx>
49 #include <GeomAdaptor_Curve.hxx>
50 #include <GeomAbs_CurveType.hxx>
52 #include <TopAbs_State.hxx>
54 #include <TopLoc_Location.hxx>
56 #include <TopoDS_Shape.hxx>
57 #include <TopoDS_Vertex.hxx>
58 #include <TopoDS_Face.hxx>
59 #include <TopoDS_Edge.hxx>
62 #include <TopExp_Explorer.hxx>
64 #include <TopTools_IndexedMapOfShape.hxx>
66 #include <BRep_Tool.hxx>
67 #include <BRepLib_MakeEdge.hxx>
69 #include <GEOMAlgo_ListIteratorOfListOfPnt.hxx>
70 #include <GEOMAlgo_PassKey.hxx>
71 #include <GEOMAlgo_DataMapOfPassKeyInteger.hxx>
72 #include <GEOMAlgo_DataMapIteratorOfDataMapOfPassKeyInteger.hxx>
73 #include <GEOMAlgo_SurfaceTools.hxx>
74 #include <GEOMAlgo_StateCollector.hxx>
77 //=======================================================================
78 //function : GEOMAlgo_FinderShapeOn1
80 //=======================================================================
81 GEOMAlgo_FinderShapeOn1::GEOMAlgo_FinderShapeOn1()
86 myShapeType=TopAbs_VERTEX;
87 myState=GEOMAlgo_ST_UNKNOWN;
91 //=======================================================================
94 //=======================================================================
95 GEOMAlgo_FinderShapeOn1::~GEOMAlgo_FinderShapeOn1()
98 //=======================================================================
99 //function : SetSurface
101 //=======================================================================
102 void GEOMAlgo_FinderShapeOn1::SetSurface(const Handle(Geom_Surface)& aS)
106 //=======================================================================
109 //=======================================================================
110 const Handle(Geom_Surface)& GEOMAlgo_FinderShapeOn1::Surface() const
114 //=======================================================================
115 //function : SetShapeType
117 //=======================================================================
118 void GEOMAlgo_FinderShapeOn1::SetShapeType(const TopAbs_ShapeEnum aType)
122 //=======================================================================
123 //function : ShapeType
125 //=======================================================================
126 TopAbs_ShapeEnum GEOMAlgo_FinderShapeOn1::ShapeType()const
130 //=======================================================================
131 //function : SetState
133 //=======================================================================
134 void GEOMAlgo_FinderShapeOn1::SetState(const GEOMAlgo_State aState)
138 //=======================================================================
141 //=======================================================================
142 GEOMAlgo_State GEOMAlgo_FinderShapeOn1::State() const
146 //=======================================================================
147 //function : SetNbPntsMin
149 //=======================================================================
150 void GEOMAlgo_FinderShapeOn1::SetNbPntsMin(const Standard_Integer aNb)
154 //=======================================================================
155 //function : NbPntsMin
157 //=======================================================================
158 Standard_Integer GEOMAlgo_FinderShapeOn1::NbPntsMin()const
162 //=======================================================================
163 //function : SetNbPntsMax
165 //=======================================================================
166 void GEOMAlgo_FinderShapeOn1::SetNbPntsMax(const Standard_Integer aNb)
170 //=======================================================================
171 //function : NbPntsMax
173 //=======================================================================
174 Standard_Integer GEOMAlgo_FinderShapeOn1::NbPntsMax()const
178 //=======================================================================
181 //=======================================================================
182 const GEOMAlgo_IndexedDataMapOfShapeState& GEOMAlgo_FinderShapeOn1::MSS() const
186 //=======================================================================
189 //=======================================================================
190 const TopTools_ListOfShape& GEOMAlgo_FinderShapeOn1::Shapes() const
192 Standard_Integer i, aNb;
193 TopTools_ListOfShape* pL;
195 pL=(TopTools_ListOfShape*) &myLS;
199 for (i=1; i<=aNb; ++i) {
200 const TopoDS_Shape& aS=myMSS.FindKey(i);
201 if (aS.ShapeType()==myShapeType) {
207 //=======================================================================
210 //=======================================================================
211 void GEOMAlgo_FinderShapeOn1::Perform()
228 if (myShapeType==TopAbs_VERTEX) {
237 if (myShapeType==TopAbs_EDGE) {
246 if (myShapeType==TopAbs_FACE) {
254 //=======================================================================
255 //function : ProcessVertices
257 //=======================================================================
258 void GEOMAlgo_FinderShapeOn1::ProcessVertices()
262 Standard_Boolean bIsConformState;
263 Standard_Integer i, aNb;
265 TopTools_IndexedMapOfShape aM;
268 TopExp::MapShapes(myShape, TopAbs_VERTEX, aM);
270 for (i=1; i<=aNb; ++i) {
271 const TopoDS_Vertex& aV=TopoDS::Vertex(aM(i));
272 aP=BRep_Tool::Pnt(aV);
274 aSt = GetPointState( aP );
275 bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
277 if (myShapeType==TopAbs_VERTEX){
278 if (bIsConformState) {
282 else if (bIsConformState || aSt==TopAbs_ON) {
287 //=======================================================================
288 //function : ProcessEdges
290 //=======================================================================
291 void GEOMAlgo_FinderShapeOn1::ProcessEdges()
295 Standard_Boolean bIsConformState, bIsToBreak;
296 Standard_Integer i, aNb, iCnt;
298 TopTools_IndexedMapOfShape aM;
299 TopExp_Explorer aExp;
300 GEOMAlgo_ListIteratorOfListOfPnt aIt;
301 GeomAbs_SurfaceType aType1;
303 aType1=myGAS.GetType();
305 TopExp::MapShapes(myShape, TopAbs_EDGE, aM);
307 for (i=1; i<=aNb; ++i) {
308 GEOMAlgo_ListOfPnt aLP;
309 GEOMAlgo_StateCollector aSC;
311 const TopoDS_Edge& aE=TopoDS::Edge(aM(i));
313 aExp.Init(aE, TopAbs_VERTEX);
314 for (; aExp.More(); aExp.Next()) {
315 const TopoDS_Shape& aV=aExp.Current();
317 bIsConformState=myMSS.Contains(aV);
318 if (!bIsConformState) {
319 break;// vertex has non-conformed state
322 aSt=myMSS.FindFromKey(aV);
323 aSC.AppendState(aSt);
327 if (!bIsConformState) {
328 continue; // vertex has non-conformed state,skip edge
331 if (BRep_Tool::Degenerated(aE)) {
336 if (myState==GEOMAlgo_ST_ON && aType1==GeomAbs_Sphere) {
337 Standard_Real aT1, aT2;
338 Handle(Geom_Curve) aC;
339 GeomAdaptor_Curve aGAC;
340 GeomAbs_CurveType aType2;
342 aC=BRep_Tool::Curve(aE, aT1, aT2);
345 aType2=aGAC.GetType();
346 if (aType2==GeomAbs_Line) {
351 InnerPoints(aE, aLP);
356 bIsConformState=Standard_True;
358 for (iCnt=0; aIt.More(); aIt.Next(), ++iCnt) {
360 if (iCnt > myNbPntsMax) {
365 const gp_Pnt& aP=aIt.Value();
366 aSt = GetPointState( aP );
367 bIsToBreak=aSC.AppendState(aSt);
375 bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
376 if (myShapeType==TopAbs_EDGE) {
377 if (bIsConformState) {
381 else if (bIsConformState || aSt==TopAbs_ON) {
384 } // for (i=1; i<=aNb; ++i) next edge
386 //=======================================================================
387 //function : ProcessFaces
389 //=======================================================================
390 void GEOMAlgo_FinderShapeOn1::ProcessFaces()
394 Standard_Boolean bIsConformState, bIsToBreak;
395 Standard_Integer i, aNbF, iCnt;
397 TopTools_IndexedMapOfShape aM;
398 TopExp_Explorer aExp;
399 GEOMAlgo_ListIteratorOfListOfPnt aIt;
400 GeomAbs_SurfaceType aType1, aType2;
402 aType1=myGAS.GetType();
404 TopExp::MapShapes(myShape, TopAbs_FACE, aM);
406 for (i=1; i<=aNbF; ++i) {
407 GEOMAlgo_StateCollector aSC;
408 GEOMAlgo_ListOfPnt aLP;
410 const TopoDS_Face& aF=TopoDS::Face(aM(i));
412 if (myState==GEOMAlgo_ST_ON) {
413 Handle(Geom_Surface) aS;
414 GeomAdaptor_Surface aGAS;
416 aS=BRep_Tool::Surface(aF);
418 aType2=aGAS.GetType();
419 if (aType2!=aType1) {
424 aExp.Init(aF, TopAbs_EDGE);
425 for (; aExp.More(); aExp.Next()) {
426 const TopoDS_Shape& aE=aExp.Current();
427 bIsConformState=myMSS.Contains(aE);
428 if (!bIsConformState) {
429 break;// edge has non-conformed state
432 aSt=myMSS.FindFromKey(aE);
433 aSC.AppendState(aSt);
437 if (!bIsConformState) {
438 continue; // edge has non-conformed state,skip face
441 InnerPoints(aF, aLP);
446 bIsConformState=Standard_True;
448 for (iCnt=0; aIt.More(); aIt.Next(), ++iCnt) {
450 if (iCnt > myNbPntsMax) {
455 const gp_Pnt& aP=aIt.Value();
456 aSt = GetPointState( aP );
457 bIsToBreak=aSC.AppendState(aSt);
465 bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
466 if (myShapeType==TopAbs_FACE) {
467 if (bIsConformState) {
471 else if (bIsConformState || aSt==TopAbs_ON) {
474 }// for (i=1; i<=aNb; ++i) next face
476 //=======================================================================
477 //function : ProcessSolids
479 //=======================================================================
480 void GEOMAlgo_FinderShapeOn1::ProcessSolids()
484 Standard_Boolean bIsConformState;
485 Standard_Integer i, aNbS, j, aNbF;
486 TopTools_IndexedMapOfShape aM, aMF;
489 TopExp::MapShapes(myShape, TopAbs_SOLID, aM);
491 for (i=1; i<=aNbS; ++i) {
492 GEOMAlgo_StateCollector aSC;
494 const TopoDS_Shape& aSd=aM(i);
496 TopExp::MapShapes(aSd, TopAbs_FACE, aMF);
498 for (j=1; j<=aNbF; ++j) {
499 const TopoDS_Shape& aF=aMF(j);
500 bIsConformState=myMSS.Contains(aF);
501 if (!bIsConformState) {
502 break;// face has non-conformed state
505 aSt=myMSS.FindFromKey(aF);
506 aSC.AppendState(aSt);
510 if (!bIsConformState) {
511 continue; // face has non-conformed state,skip solid
516 bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
517 if (bIsConformState) {
523 //=======================================================================
524 //function : InnerPoints
526 //=======================================================================
527 void GEOMAlgo_FinderShapeOn1::InnerPoints(const TopoDS_Face& aF,
528 GEOMAlgo_ListOfPnt& aLP)
532 Standard_Integer j, j1, j2, k, n[4], aNbLinks, aNx, aNbMax, aNb;
533 Standard_Integer iCnt, *pIds;
534 TopLoc_Location aLoc;
535 Handle(Poly_Triangulation) aTRF;
536 TColStd_MapOfInteger aMBN;
537 GEOMAlgo_DataMapOfPassKeyInteger aMPKI;
538 GEOMAlgo_DataMapIteratorOfDataMapOfPassKeyInteger aIt;
543 aTRF=BRep_Tool::Triangulation(aF, aLoc);
545 myErrorStatus=20; // no triangulation found
549 const gp_Trsf& aTrsf=aLoc.Transformation();
550 const Poly_Array1OfTriangle& aTrs=aTRF->Triangles();
551 const TColgp_Array1OfPnt& aNodes=aTRF->Nodes();
553 // map link/nbtriangles
556 for (j=j1; j<=j2; ++j) {
557 const Poly_Triangle& aTr=aTrs(j);
558 aTr.Get(n[0], n[1], n[2]);
560 for (k=0; k<3; ++k) {
561 GEOMAlgo_PassKey aPK;
563 aPK.SetIds(n[k], n[k+1]);
564 if (aMPKI.IsBound(aPK)) {
565 Standard_Integer& iCntX=aMPKI.ChangeFind(aPK);
574 // boundary nodes aMBN
575 aNbLinks=aMPKI.Extent();
576 aIt.Initialize(aMPKI);
577 for (; aIt.More(); aIt.Next()) {
580 const GEOMAlgo_PassKey& aPK=aIt.Key();
582 pIds=(Standard_Integer*)aPK.Key();
583 for (k=1; k<3; ++k) {
584 aNx=*(pIds+aNbMax-k);
590 // inner nodes=all_nodes - boundary_nodes
593 for (j=j1; j<=j2; ++j) {
594 if (!aMBN.Contains(j)) {
595 aP=aNodes(j).Transformed(aTrsf);
602 if (!aNb && myNbPntsMin) {
603 // try to fill it yourself
604 Standard_Boolean bIsDone;
605 Standard_Integer aN1, aN2;
606 Handle(Geom_Surface) aS;
607 GeomAdaptor_Surface aGAS;
608 GeomAbs_SurfaceType aType;
610 aS=BRep_Tool::Surface(aF);
612 aType=aGAS.GetType();
613 if (aType==GeomAbs_Plane || aType==GeomAbs_Cylinder) {
615 aNbLinks=aMPKI.Extent();
616 aIt.Initialize(aMPKI);
617 for (; aIt.More(); aIt.Next()) {
620 // take the first having occured inner link
622 const GEOMAlgo_PassKey& aPK=aIt.Key();
624 pIds=(Standard_Integer*)aPK.Key();
625 aN1=*(pIds+aNbMax-1);
626 aN2=*(pIds+aNbMax-2);
627 aP1=aNodes(aN1).Transformed(aTrsf);
628 aP2=aNodes(aN2).Transformed(aTrsf);
630 if (aType==GeomAbs_Cylinder) {
631 Standard_Real aTolSM;
634 aTolSM=1.523e-6;//~1.-cos(0.1 deg)
635 aCyl=aGAS.Cylinder();
636 if (!GEOMAlgo_SurfaceTools::IsCoaxial(aP1, aP2, aCyl, aTolSM)) {
641 BRepLib_MakeEdge aBME(aP1, aP2);
642 bIsDone=aBME.IsDone();
644 myErrorStatus=30; //can not obtain the line fron the link
648 const TopoDS_Shape& aSx=aBME.Shape();
649 const TopoDS_Edge& aE=TopoDS::Edge(aSx);
651 InnerPoints(aE, myNbPntsMin, aLP);
654 }// for (; aIt.More(); aIt.Next())
655 }// if (aType==GeomAbs_Plane || aType==GeomAbs_Cylinder)
656 }// if (!aNb && myNbPntsMin) {
658 //=======================================================================
659 //function : InnerPoints
661 //=======================================================================
662 void GEOMAlgo_FinderShapeOn1::InnerPoints(const TopoDS_Edge& aE,
663 GEOMAlgo_ListOfPnt& aLP)
667 Standard_Integer j, aNbNodes, aIndex, aNb;
668 Handle(Poly_PolygonOnTriangulation) aPTE;
669 Handle(Poly_Triangulation) aTRE;
670 TopLoc_Location aLoc;
674 BRep_Tool::PolygonOnTriangulation(aE, aPTE, aTRE, aLoc);
675 if (aTRE.IsNull() || aPTE.IsNull()) {
676 myErrorStatus=20; // no triangulation found
680 const gp_Trsf& aTrsf=aLoc.Transformation();
681 const TColgp_Array1OfPnt& aNodes=aTRE->Nodes();
683 aNbNodes=aPTE->NbNodes();
684 const TColStd_Array1OfInteger& aInds=aPTE->Nodes();
685 for (j=2; j<aNbNodes; ++j) {
687 aP=aNodes(aIndex).Transformed(aTrsf);
692 if (!aNb && myNbPntsMin) {
693 // try to fill it yourself
694 InnerPoints(aE, myNbPntsMin, aLP);
699 //=======================================================================
700 //function : InnerPoints
702 //=======================================================================
703 void GEOMAlgo_FinderShapeOn1::InnerPoints(const TopoDS_Edge& aE,
704 const Standard_Integer aNbPntsMin,
705 GEOMAlgo_ListOfPnt& aLP)
707 // try to fill it yourself
708 Standard_Boolean bInf1, bInf2;
709 Standard_Integer j, aNbT;
710 Standard_Real dT, aT, aT1, aT2;
712 Handle(Geom_Curve) aC3D;
714 aC3D=BRep_Tool::Curve(aE, aT1, aT2);
719 bInf1=Precision::IsNegativeInfinite(aT1);
720 bInf2=Precision::IsPositiveInfinite(aT2);
721 if (bInf1 || bInf2) {
727 for (j=1; j<=aNbPntsMin; ++j) {
734 //=======================================================================
735 //function : CheckData
737 //=======================================================================
738 void GEOMAlgo_FinderShapeOn1::CheckData()
742 if(mySurface.IsNull()) {
743 myErrorStatus=10; // mySurface=NULL
747 if (myShape.IsNull()) {
748 myErrorStatus=11; // myShape=NULL
752 if (!(myShapeType==TopAbs_VERTEX ||
753 myShapeType==TopAbs_EDGE ||
754 myShapeType==TopAbs_FACE ||
755 myShapeType==TopAbs_SOLID)) {
756 myErrorStatus=12; // unallowed subshape type
760 if (myState==GEOMAlgo_ST_UNKNOWN ||
761 myState==GEOMAlgo_ST_INOUT) {
762 myErrorStatus=13; // unallowed state type
766 GeomAbs_SurfaceType aType;
768 myGAS.Load(mySurface);
769 aType=myGAS.GetType();
770 if (!(aType==GeomAbs_Plane ||
771 aType==GeomAbs_Cylinder ||
772 aType==GeomAbs_Sphere)) {
773 myErrorStatus=14; // unallowed surface type
777 //=======================================================================
778 //function : GetPointState
780 //=======================================================================
782 TopAbs_State GEOMAlgo_FinderShapeOn1::GetPointState(const gp_Pnt& aP)
785 GEOMAlgo_SurfaceTools::GetState(aP, myGAS, myTolerance, aSt);
793 // 10 -mySurface=NULL
795 // 12 -unallowed type of subshapes
796 // 13 -unallowed state
797 // 14 -unallowed surface type
798 // 15 -unallowed surface type
799 // 20- no triangulation found
800 // 30- can not obtain the line from the link
805 #include <OSD_Chronometer.hxx>
806 #include <Standard_Static.hxx>
808 Standard_STATIC(OSD_Chronometer, x_S_Chrono);
809 static void x_StartChrono();
810 static void x_StopChrono();
811 static Standard_Boolean x_IsToShow();
813 //=======================================================================
814 //function : x_StartChrono
816 //=======================================================================
820 x_S_Chrono().Reset();
821 x_S_Chrono().Start();
824 //=======================================================================
825 //function : x_StopChrono
827 //=======================================================================
831 Standard_Real Chrono;
833 x_S_Chrono().Show(Chrono);
834 printf(" Tps: %lf\n", Chrono);
835 //cout << "Tps: " << Chrono << endl;
838 //=======================================================================
839 //function : x_IsToShow
841 //=======================================================================
842 Standard_Boolean x_IsToShow()
844 Standard_Boolean bFlag=Standard_False;
846 char *xr=getenv ("STDCHRONO");
848 if (!strcmp (xr, "yes")) {