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>
71 #include <GEOMAlgo_SurfaceTools.hxx>
72 #include <GEOMAlgo_StateCollector.hxx>
74 #include <GEOMAlgo_PassKey.hxx>
75 #include <GEOMAlgo_DataMapOfPassKeyInteger.hxx>
76 #include <GEOMAlgo_DataMapIteratorOfDataMapOfPassKeyInteger.hxx>
78 //=======================================================================
79 //function : GEOMAlgo_FinderShapeOn1
81 //=======================================================================
82 GEOMAlgo_FinderShapeOn1::GEOMAlgo_FinderShapeOn1()
87 myShapeType=TopAbs_VERTEX;
88 myState=GEOMAlgo_ST_UNKNOWN;
92 //=======================================================================
95 //=======================================================================
96 GEOMAlgo_FinderShapeOn1::~GEOMAlgo_FinderShapeOn1()
99 //=======================================================================
100 //function : SetSurface
102 //=======================================================================
103 void GEOMAlgo_FinderShapeOn1::SetSurface(const Handle(Geom_Surface)& aS)
107 //=======================================================================
110 //=======================================================================
111 const Handle(Geom_Surface)& GEOMAlgo_FinderShapeOn1::Surface() const
115 //=======================================================================
116 //function : SetShapeType
118 //=======================================================================
119 void GEOMAlgo_FinderShapeOn1::SetShapeType(const TopAbs_ShapeEnum aType)
123 //=======================================================================
124 //function : ShapeType
126 //=======================================================================
127 TopAbs_ShapeEnum GEOMAlgo_FinderShapeOn1::ShapeType()const
131 //=======================================================================
132 //function : SetState
134 //=======================================================================
135 void GEOMAlgo_FinderShapeOn1::SetState(const GEOMAlgo_State aState)
139 //=======================================================================
142 //=======================================================================
143 GEOMAlgo_State GEOMAlgo_FinderShapeOn1::State() const
147 //=======================================================================
148 //function : SetNbPntsMin
150 //=======================================================================
151 void GEOMAlgo_FinderShapeOn1::SetNbPntsMin(const Standard_Integer aNb)
155 //=======================================================================
156 //function : NbPntsMin
158 //=======================================================================
159 Standard_Integer GEOMAlgo_FinderShapeOn1::NbPntsMin()const
163 //=======================================================================
164 //function : SetNbPntsMax
166 //=======================================================================
167 void GEOMAlgo_FinderShapeOn1::SetNbPntsMax(const Standard_Integer aNb)
171 //=======================================================================
172 //function : NbPntsMax
174 //=======================================================================
175 Standard_Integer GEOMAlgo_FinderShapeOn1::NbPntsMax()const
179 //=======================================================================
182 //=======================================================================
183 const GEOMAlgo_IndexedDataMapOfShapeState& GEOMAlgo_FinderShapeOn1::MSS() const
187 //=======================================================================
190 //=======================================================================
191 const TopTools_ListOfShape& GEOMAlgo_FinderShapeOn1::Shapes() const
193 Standard_Integer i, aNb;
194 TopTools_ListOfShape* pL;
196 pL=(TopTools_ListOfShape*) &myLS;
200 for (i=1; i<=aNb; ++i) {
201 const TopoDS_Shape& aS=myMSS.FindKey(i);
202 if (aS.ShapeType()==myShapeType) {
208 //=======================================================================
211 //=======================================================================
212 void GEOMAlgo_FinderShapeOn1::Perform()
229 if (myShapeType==TopAbs_VERTEX) {
238 if (myShapeType==TopAbs_EDGE) {
247 if (myShapeType==TopAbs_FACE) {
255 //=======================================================================
256 //function : ProcessVertices
258 //=======================================================================
259 void GEOMAlgo_FinderShapeOn1::ProcessVertices()
263 Standard_Boolean bIsConformState;
264 Standard_Integer i, aNb;
266 TopTools_IndexedMapOfShape aM;
269 TopExp::MapShapes(myShape, TopAbs_VERTEX, aM);
271 for (i=1; i<=aNb; ++i) {
272 const TopoDS_Vertex& aV=TopoDS::Vertex(aM(i));
273 aP=BRep_Tool::Pnt(aV);
275 aSt = GetPointState( aP );
276 bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
278 if (myShapeType==TopAbs_VERTEX){
279 if (bIsConformState) {
283 else if (bIsConformState || aSt==TopAbs_ON) {
288 //=======================================================================
289 //function : ProcessEdges
291 //=======================================================================
292 void GEOMAlgo_FinderShapeOn1::ProcessEdges()
296 Standard_Boolean bIsConformState, bIsToBreak;
297 Standard_Integer i, aNb, iCnt;
299 TopTools_IndexedMapOfShape aM;
300 TopExp_Explorer aExp;
301 GEOMAlgo_ListIteratorOfListOfPnt aIt;
302 GeomAbs_SurfaceType aType1;
304 aType1=myGAS.GetType();
306 TopExp::MapShapes(myShape, TopAbs_EDGE, aM);
308 for (i=1; i<=aNb; ++i) {
309 GEOMAlgo_ListOfPnt aLP;
310 GEOMAlgo_StateCollector aSC;
312 const TopoDS_Edge& aE=TopoDS::Edge(aM(i));
314 aExp.Init(aE, TopAbs_VERTEX);
315 for (; aExp.More(); aExp.Next()) {
316 const TopoDS_Shape& aV=aExp.Current();
318 bIsConformState=myMSS.Contains(aV);
319 if (!bIsConformState) {
320 break;// vertex has non-conformed state
323 aSt=myMSS.FindFromKey(aV);
324 aSC.AppendState(aSt);
328 if (!bIsConformState) {
329 continue; // vertex has non-conformed state,skip edge
332 if (BRep_Tool::Degenerated(aE)) {
337 if (myState==GEOMAlgo_ST_ON && aType1==GeomAbs_Sphere) {
338 Standard_Real aT1, aT2;
339 Handle(Geom_Curve) aC;
340 GeomAdaptor_Curve aGAC;
341 GeomAbs_CurveType aType2;
343 aC=BRep_Tool::Curve(aE, aT1, aT2);
346 aType2=aGAC.GetType();
347 if (aType2==GeomAbs_Line) {
352 InnerPoints(aE, aLP);
357 bIsConformState=Standard_True;
359 for (iCnt=0; aIt.More(); aIt.Next(), ++iCnt) {
361 if (iCnt > myNbPntsMax) {
366 const gp_Pnt& aP=aIt.Value();
367 aSt = GetPointState( aP );
368 bIsToBreak=aSC.AppendState(aSt);
376 bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
377 if (myShapeType==TopAbs_EDGE) {
378 if (bIsConformState) {
382 else if (bIsConformState || aSt==TopAbs_ON) {
385 } // for (i=1; i<=aNb; ++i) next edge
387 //=======================================================================
388 //function : ProcessFaces
390 //=======================================================================
391 void GEOMAlgo_FinderShapeOn1::ProcessFaces()
395 Standard_Boolean bIsConformState, bIsToBreak;
396 Standard_Integer i, aNbF, iCnt;
398 TopTools_IndexedMapOfShape aM;
399 TopExp_Explorer aExp;
400 GEOMAlgo_ListIteratorOfListOfPnt aIt;
401 GeomAbs_SurfaceType aType1, aType2;
403 aType1=myGAS.GetType();
405 TopExp::MapShapes(myShape, TopAbs_FACE, aM);
407 for (i=1; i<=aNbF; ++i) {
408 GEOMAlgo_StateCollector aSC;
409 GEOMAlgo_ListOfPnt aLP;
411 const TopoDS_Face& aF=TopoDS::Face(aM(i));
413 if (myState==GEOMAlgo_ST_ON) {
414 Handle(Geom_Surface) aS;
415 GeomAdaptor_Surface aGAS;
417 aS=BRep_Tool::Surface(aF);
419 aType2=aGAS.GetType();
420 if (aType2!=aType1) {
425 aExp.Init(aF, TopAbs_EDGE);
426 for (; aExp.More(); aExp.Next()) {
427 const TopoDS_Shape& aE=aExp.Current();
428 bIsConformState=myMSS.Contains(aE);
429 if (!bIsConformState) {
430 break;// edge has non-conformed state
433 aSt=myMSS.FindFromKey(aE);
434 aSC.AppendState(aSt);
438 if (!bIsConformState) {
439 continue; // edge has non-conformed state,skip face
442 InnerPoints(aF, aLP);
447 bIsConformState=Standard_True;
449 for (iCnt=0; aIt.More(); aIt.Next(), ++iCnt) {
451 if (iCnt > myNbPntsMax) {
456 const gp_Pnt& aP=aIt.Value();
457 aSt = GetPointState( aP );
458 bIsToBreak=aSC.AppendState(aSt);
466 bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
467 if (myShapeType==TopAbs_FACE) {
468 if (bIsConformState) {
472 else if (bIsConformState || aSt==TopAbs_ON) {
475 }// for (i=1; i<=aNb; ++i) next face
477 //=======================================================================
478 //function : ProcessSolids
480 //=======================================================================
481 void GEOMAlgo_FinderShapeOn1::ProcessSolids()
485 Standard_Boolean bIsConformState;
486 Standard_Integer i, aNbS, j, aNbF;
487 TopTools_IndexedMapOfShape aM, aMF;
490 TopExp::MapShapes(myShape, TopAbs_SOLID, aM);
492 for (i=1; i<=aNbS; ++i) {
493 GEOMAlgo_StateCollector aSC;
495 const TopoDS_Shape& aSd=aM(i);
497 TopExp::MapShapes(aSd, TopAbs_FACE, aMF);
499 for (j=1; j<=aNbF; ++j) {
500 const TopoDS_Shape& aF=aMF(j);
501 bIsConformState=myMSS.Contains(aF);
502 if (!bIsConformState) {
503 break;// face has non-conformed state
506 aSt=myMSS.FindFromKey(aF);
507 aSC.AppendState(aSt);
511 if (!bIsConformState) {
512 continue; // face has non-conformed state,skip solid
517 bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
518 if (bIsConformState) {
524 //=======================================================================
525 //function : InnerPoints
527 //=======================================================================
528 void GEOMAlgo_FinderShapeOn1::InnerPoints(const TopoDS_Face& aF,
529 GEOMAlgo_ListOfPnt& aLP)
533 Standard_Integer j, j1, j2, k, n[4], aNbLinks, aNx, aNb, iCnt;//, aNbMax, *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();
584 pIds=(Standard_Integer*)aPK.Key();
585 for (k=1; k<3; ++k) {
586 aNx=*(pIds+aNbMax-k);
590 aNx=(Standard_Integer)aPK.Id(1);
592 aNx=(Standard_Integer)aPK.Id(2);
598 // inner nodes=all_nodes - boundary_nodes
601 for (j=j1; j<=j2; ++j) {
602 if (!aMBN.Contains(j)) {
603 aP=aNodes(j).Transformed(aTrsf);
610 if (!aNb && myNbPntsMin) {
611 // try to fill it yourself
612 Standard_Boolean bIsDone;
613 Standard_Integer aN1, aN2;
614 Handle(Geom_Surface) aS;
615 GeomAdaptor_Surface aGAS;
616 GeomAbs_SurfaceType aType;
618 aS=BRep_Tool::Surface(aF);
620 aType=aGAS.GetType();
621 if (aType==GeomAbs_Plane || aType==GeomAbs_Cylinder) {
623 aNbLinks=aMPKI.Extent();
624 aIt.Initialize(aMPKI);
625 for (; aIt.More(); aIt.Next()) {
628 // take the first having occured inner link
630 const GEOMAlgo_PassKey& aPK=aIt.Key();
634 pIds=(Standard_Integer*)aPK.Key();
635 aN1=*(pIds+aNbMax-1);
636 aN2=*(pIds+aNbMax-2);
639 aN1=(Standard_Integer)aPK.Id(1);
640 aN2=(Standard_Integer)aPK.Id(2);
642 aP1=aNodes(aN1).Transformed(aTrsf);
643 aP2=aNodes(aN2).Transformed(aTrsf);
645 if (aType==GeomAbs_Cylinder) {
646 Standard_Real aTolSM;
649 aTolSM=1.523e-6;//~1.-cos(0.1 deg)
650 aCyl=aGAS.Cylinder();
651 if (!GEOMAlgo_SurfaceTools::IsCoaxial(aP1, aP2, aCyl, aTolSM)) {
656 BRepLib_MakeEdge aBME(aP1, aP2);
657 bIsDone=aBME.IsDone();
659 myErrorStatus=30; //can not obtain the line fron the link
663 const TopoDS_Shape& aSx=aBME.Shape();
664 const TopoDS_Edge& aE=TopoDS::Edge(aSx);
666 InnerPoints(aE, myNbPntsMin, aLP);
669 }// for (; aIt.More(); aIt.Next())
670 }// if (aType==GeomAbs_Plane || aType==GeomAbs_Cylinder)
671 }// if (!aNb && myNbPntsMin) {
673 //=======================================================================
674 //function : InnerPoints
676 //=======================================================================
677 void GEOMAlgo_FinderShapeOn1::InnerPoints(const TopoDS_Edge& aE,
678 GEOMAlgo_ListOfPnt& aLP)
682 Standard_Integer j, aNbNodes, aIndex, aNb;
683 Handle(Poly_PolygonOnTriangulation) aPTE;
684 Handle(Poly_Triangulation) aTRE;
685 TopLoc_Location aLoc;
689 BRep_Tool::PolygonOnTriangulation(aE, aPTE, aTRE, aLoc);
690 if (aTRE.IsNull() || aPTE.IsNull()) {
691 myErrorStatus=20; // no triangulation found
695 const gp_Trsf& aTrsf=aLoc.Transformation();
696 const TColgp_Array1OfPnt& aNodes=aTRE->Nodes();
698 aNbNodes=aPTE->NbNodes();
699 const TColStd_Array1OfInteger& aInds=aPTE->Nodes();
700 for (j=2; j<aNbNodes; ++j) {
702 aP=aNodes(aIndex).Transformed(aTrsf);
707 if (!aNb && myNbPntsMin) {
708 // try to fill it yourself
709 InnerPoints(aE, myNbPntsMin, aLP);
714 //=======================================================================
715 //function : InnerPoints
717 //=======================================================================
718 void GEOMAlgo_FinderShapeOn1::InnerPoints(const TopoDS_Edge& aE,
719 const Standard_Integer aNbPntsMin,
720 GEOMAlgo_ListOfPnt& aLP)
722 // try to fill it yourself
723 Standard_Boolean bInf1, bInf2;
724 Standard_Integer j, aNbT;
725 Standard_Real dT, aT, aT1, aT2;
727 Handle(Geom_Curve) aC3D;
729 aC3D=BRep_Tool::Curve(aE, aT1, aT2);
734 bInf1=Precision::IsNegativeInfinite(aT1);
735 bInf2=Precision::IsPositiveInfinite(aT2);
736 if (bInf1 || bInf2) {
742 for (j=1; j<=aNbPntsMin; ++j) {
749 //=======================================================================
750 //function : CheckData
752 //=======================================================================
753 void GEOMAlgo_FinderShapeOn1::CheckData()
757 if(mySurface.IsNull()) {
758 myErrorStatus=10; // mySurface=NULL
762 if (myShape.IsNull()) {
763 myErrorStatus=11; // myShape=NULL
767 if (!(myShapeType==TopAbs_VERTEX ||
768 myShapeType==TopAbs_EDGE ||
769 myShapeType==TopAbs_FACE ||
770 myShapeType==TopAbs_SOLID)) {
771 myErrorStatus=12; // unallowed subshape type
775 if (myState==GEOMAlgo_ST_UNKNOWN ||
776 myState==GEOMAlgo_ST_INOUT) {
777 myErrorStatus=13; // unallowed state type
781 GeomAbs_SurfaceType aType;
783 myGAS.Load(mySurface);
784 aType=myGAS.GetType();
785 if (!(aType==GeomAbs_Plane ||
786 aType==GeomAbs_Cylinder ||
787 aType==GeomAbs_Sphere)) {
788 myErrorStatus=14; // unallowed surface type
792 //=======================================================================
793 //function : GetPointState
795 //=======================================================================
797 TopAbs_State GEOMAlgo_FinderShapeOn1::GetPointState(const gp_Pnt& aP)
800 GEOMAlgo_SurfaceTools::GetState(aP, myGAS, myTolerance, aSt);
808 // 10 -mySurface=NULL
810 // 12 -unallowed type of subshapes
811 // 13 -unallowed state
812 // 14 -unallowed surface type
813 // 15 -unallowed surface type
814 // 20- no triangulation found
815 // 30- can not obtain the line from the link
820 #include <OSD_Chronometer.hxx>
821 #include <Standard_Static.hxx>
823 Standard_STATIC(OSD_Chronometer, x_S_Chrono);
824 static void x_StartChrono();
825 static void x_StopChrono();
826 static Standard_Boolean x_IsToShow();
828 //=======================================================================
829 //function : x_StartChrono
831 //=======================================================================
835 x_S_Chrono().Reset();
836 x_S_Chrono().Start();
839 //=======================================================================
840 //function : x_StopChrono
842 //=======================================================================
846 Standard_Real Chrono;
848 x_S_Chrono().Show(Chrono);
849 printf(" Tps: %lf\n", Chrono);
850 //cout << "Tps: " << Chrono << endl;
853 //=======================================================================
854 //function : x_IsToShow
856 //=======================================================================
857 Standard_Boolean x_IsToShow()
859 Standard_Boolean bFlag=Standard_False;
861 char *xr=getenv ("STDCHRONO");
863 if (!strcmp (xr, "yes")) {