1 // Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File: GEOMAlgo_FinderShapeOn1.cxx
23 // Created: Fri Mar 4 10:31:06 2005
24 // Author: Peter KURNEV
26 #include <GEOMAlgo_FinderShapeOn1.hxx>
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>
44 #include <Poly_Polygon3D.hxx>
46 #include <Geom_Curve.hxx>
47 #include <Geom_Surface.hxx>
48 #include <GeomAdaptor_Surface.hxx>
49 #include <GeomAbs_SurfaceType.hxx>
50 #include <GeomAdaptor_Curve.hxx>
51 #include <GeomAbs_CurveType.hxx>
53 #include <TopAbs_State.hxx>
55 #include <TopLoc_Location.hxx>
57 #include <TopoDS_Shape.hxx>
58 #include <TopoDS_Vertex.hxx>
59 #include <TopoDS_Face.hxx>
60 #include <TopoDS_Edge.hxx>
63 #include <TopExp_Explorer.hxx>
65 #include <TopTools_IndexedMapOfShape.hxx>
67 #include <BRep_Tool.hxx>
68 #include <BRepLib_MakeEdge.hxx>
70 #include <GEOMAlgo_ListIteratorOfListOfPnt.hxx>
72 #include <GEOMAlgo_SurfaceTools.hxx>
73 #include <GEOMAlgo_StateCollector.hxx>
74 #include <GEOMAlgo_FinderShapeOn.hxx>
76 #include <GEOMAlgo_PassKey.hxx>
77 #include <GEOMAlgo_DataMapOfPassKeyInteger.hxx>
78 #include <GEOMAlgo_DataMapIteratorOfDataMapOfPassKeyInteger.hxx>
80 //=======================================================================
81 //function : GEOMAlgo_FinderShapeOn1
83 //=======================================================================
84 GEOMAlgo_FinderShapeOn1::GEOMAlgo_FinderShapeOn1()
89 myShapeType=TopAbs_VERTEX;
90 myState=GEOMAlgo_ST_UNKNOWN;
94 //=======================================================================
97 //=======================================================================
98 GEOMAlgo_FinderShapeOn1::~GEOMAlgo_FinderShapeOn1()
101 //=======================================================================
102 //function : SetSurface
104 //=======================================================================
105 void GEOMAlgo_FinderShapeOn1::SetSurface(const Handle(Geom_Surface)& aS)
109 //=======================================================================
112 //=======================================================================
113 const Handle(Geom_Surface)& GEOMAlgo_FinderShapeOn1::Surface() const
117 //=======================================================================
118 //function : SetShapeType
120 //=======================================================================
121 void GEOMAlgo_FinderShapeOn1::SetShapeType(const TopAbs_ShapeEnum aType)
125 //=======================================================================
126 //function : ShapeType
128 //=======================================================================
129 TopAbs_ShapeEnum GEOMAlgo_FinderShapeOn1::ShapeType()const
133 //=======================================================================
134 //function : SetState
136 //=======================================================================
137 void GEOMAlgo_FinderShapeOn1::SetState(const GEOMAlgo_State aState)
141 //=======================================================================
144 //=======================================================================
145 GEOMAlgo_State GEOMAlgo_FinderShapeOn1::State() const
149 //=======================================================================
150 //function : SetNbPntsMin
152 //=======================================================================
153 void GEOMAlgo_FinderShapeOn1::SetNbPntsMin(const Standard_Integer aNb)
157 //=======================================================================
158 //function : NbPntsMin
160 //=======================================================================
161 Standard_Integer GEOMAlgo_FinderShapeOn1::NbPntsMin()const
165 //=======================================================================
166 //function : SetNbPntsMax
168 //=======================================================================
169 void GEOMAlgo_FinderShapeOn1::SetNbPntsMax(const Standard_Integer aNb)
173 //=======================================================================
174 //function : NbPntsMax
176 //=======================================================================
177 Standard_Integer GEOMAlgo_FinderShapeOn1::NbPntsMax()const
181 //=======================================================================
184 //=======================================================================
185 const GEOMAlgo_IndexedDataMapOfShapeState& GEOMAlgo_FinderShapeOn1::MSS() const
189 //=======================================================================
192 //=======================================================================
193 const TopTools_ListOfShape& GEOMAlgo_FinderShapeOn1::Shapes() const
195 Standard_Integer i, aNb;
196 TopTools_ListOfShape* pL;
198 pL=(TopTools_ListOfShape*) &myLS;
202 for (i=1; i<=aNb; ++i) {
203 const TopoDS_Shape& aS=myMSS.FindKey(i);
204 if (aS.ShapeType()==myShapeType) {
210 //=======================================================================
213 //=======================================================================
214 void GEOMAlgo_FinderShapeOn1::Perform()
226 // Initialize the context
227 GEOMAlgo_ShapeAlgo::Perform();
234 if (myShapeType==TopAbs_VERTEX) {
243 if (myShapeType==TopAbs_EDGE) {
252 if (myShapeType==TopAbs_FACE) {
260 //=======================================================================
261 //function : ProcessVertices
263 //=======================================================================
264 void GEOMAlgo_FinderShapeOn1::ProcessVertices()
268 Standard_Boolean bIsConformState;
269 Standard_Integer i, aNb;
271 TopTools_IndexedMapOfShape aM;
274 TopExp::MapShapes(myShape, TopAbs_VERTEX, aM);
276 for (i=1; i<=aNb; ++i) {
277 const TopoDS_Vertex& aV=TopoDS::Vertex(aM(i));
278 aP=BRep_Tool::Pnt(aV);
280 aSt = GetPointState( aP );
281 bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
283 if (myShapeType==TopAbs_VERTEX){
284 if (bIsConformState) {
288 else if (bIsConformState || aSt==TopAbs_ON) {
293 //=======================================================================
294 //function : ProcessEdges
296 //=======================================================================
297 void GEOMAlgo_FinderShapeOn1::ProcessEdges()
301 Standard_Boolean bIsConformState, bIsToBreak;
302 Standard_Integer i, aNb, iCnt;
304 TopTools_IndexedMapOfShape aM;
305 TopExp_Explorer aExp;
306 GEOMAlgo_ListIteratorOfListOfPnt aIt;
307 GeomAbs_SurfaceType aType1;
309 aType1=myGAS.GetType();
311 TopExp::MapShapes(myShape, TopAbs_EDGE, aM);
313 for (i=1; i<=aNb; ++i) {
314 GEOMAlgo_ListOfPnt aLP;
315 GEOMAlgo_StateCollector aSC;
317 const TopoDS_Edge& aE=TopoDS::Edge(aM(i));
319 aExp.Init(aE, TopAbs_VERTEX);
320 for (; aExp.More(); aExp.Next()) {
321 const TopoDS_Shape& aV=aExp.Current();
323 bIsConformState=myMSS.Contains(aV);
324 if (!bIsConformState) {
325 break;// vertex has non-conformed state
328 aSt=myMSS.FindFromKey(aV);
329 aSC.AppendState(aSt);
333 if (!bIsConformState) {
334 continue; // vertex has non-conformed state,skip edge
337 if (BRep_Tool::Degenerated(aE)) {
342 if (myState==GEOMAlgo_ST_ON && aType1==GeomAbs_Sphere) {
343 Standard_Real aT1, aT2;
344 Handle(Geom_Curve) aC;
345 GeomAdaptor_Curve aGAC;
346 GeomAbs_CurveType aType2;
348 aC=BRep_Tool::Curve(aE, aT1, aT2);
351 aType2=aGAC.GetType();
352 if (aType2==GeomAbs_Line) {
357 InnerPoints(aE, aLP);
362 bIsConformState=Standard_True;
364 for (iCnt=0; aIt.More(); aIt.Next(), ++iCnt) {
366 if (iCnt > myNbPntsMax) {
371 const gp_Pnt& aP=aIt.Value();
372 aSt = GetPointState( aP );
373 bIsToBreak=aSC.AppendState(aSt);
381 bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
382 if (myShapeType==TopAbs_EDGE) {
383 if (bIsConformState) {
387 else if (bIsConformState || aSt==TopAbs_ON) {
390 } // for (i=1; i<=aNb; ++i) next edge
392 //=======================================================================
393 //function : ProcessFaces
395 //=======================================================================
396 void GEOMAlgo_FinderShapeOn1::ProcessFaces()
400 Standard_Boolean bIsConformState, bIsToBreak;
401 Standard_Integer i, aNbF, iCnt;
403 TopTools_IndexedMapOfShape aM;
404 TopExp_Explorer aExp;
405 GEOMAlgo_ListIteratorOfListOfPnt aIt;
406 GeomAbs_SurfaceType aType1, aType2;
408 aType1=myGAS.GetType();
410 TopExp::MapShapes(myShape, TopAbs_FACE, aM);
412 for (i=1; i<=aNbF; ++i) {
413 GEOMAlgo_StateCollector aSC;
414 GEOMAlgo_ListOfPnt aLP;
416 const TopoDS_Face& aF=TopoDS::Face(aM(i));
418 if (myState==GEOMAlgo_ST_ON) {
419 Handle(Geom_Surface) aS;
420 GeomAdaptor_Surface aGAS;
422 aS=BRep_Tool::Surface(aF);
424 aType2=aGAS.GetType();
425 if (aType2!=aType1) {
430 aExp.Init(aF, TopAbs_EDGE);
431 for (; aExp.More(); aExp.Next()) {
432 const TopoDS_Shape& aE=aExp.Current();
433 bIsConformState=myMSS.Contains(aE);
434 if (!bIsConformState) {
435 break;// edge has non-conformed state
438 aSt=myMSS.FindFromKey(aE);
439 aSC.AppendState(aSt);
443 if (!bIsConformState) {
444 continue; // edge has non-conformed state,skip face
447 InnerPoints(aF, aLP);
452 bIsConformState=Standard_True;
454 for (iCnt=0; aIt.More(); aIt.Next(), ++iCnt) {
456 if (iCnt > myNbPntsMax) {
461 const gp_Pnt& aP=aIt.Value();
462 aSt = GetPointState( aP );
463 bIsToBreak=aSC.AppendState(aSt);
471 bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
472 if (myShapeType==TopAbs_FACE) {
473 if (bIsConformState) {
477 else if (bIsConformState || aSt==TopAbs_ON) {
480 }// for (i=1; i<=aNb; ++i) next face
482 //=======================================================================
483 //function : ProcessSolids
485 //=======================================================================
486 void GEOMAlgo_FinderShapeOn1::ProcessSolids()
490 Standard_Boolean bIsConformState;
491 Standard_Integer i, aNbS, j, aNbF;
492 TopTools_IndexedMapOfShape aM, aMF;
495 TopExp::MapShapes(myShape, TopAbs_SOLID, aM);
497 for (i=1; i<=aNbS; ++i) {
498 GEOMAlgo_StateCollector aSC;
500 const TopoDS_Shape& aSd=aM(i);
502 TopExp::MapShapes(aSd, TopAbs_FACE, aMF);
504 for (j=1; j<=aNbF; ++j) {
505 const TopoDS_Shape& aF=aMF(j);
506 bIsConformState=myMSS.Contains(aF);
507 if (!bIsConformState) {
508 break;// face has non-conformed state
511 aSt=myMSS.FindFromKey(aF);
512 aSC.AppendState(aSt);
516 if (!bIsConformState) {
517 continue; // face has non-conformed state,skip solid
522 bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
523 if (bIsConformState) {
529 //=======================================================================
530 //function : InnerPoints
532 //=======================================================================
533 void GEOMAlgo_FinderShapeOn1::InnerPoints(const TopoDS_Face& aF,
534 GEOMAlgo_ListOfPnt& aLP)
538 Standard_Integer j, j1, j2, k, n[4], aNbLinks, aNx, aNb, iCnt;//, aNbMax, *pIds;
539 TopLoc_Location aLoc;
540 Handle(Poly_Triangulation) aTRF;
541 TColStd_MapOfInteger aMBN;
542 GEOMAlgo_DataMapOfPassKeyInteger aMPKI;
543 GEOMAlgo_DataMapIteratorOfDataMapOfPassKeyInteger aIt;
548 aTRF=BRep_Tool::Triangulation(aF, aLoc);
550 if (!GEOMAlgo_FinderShapeOn::BuildTriangulation(aF)) {
551 myWarningStatus=20; // no triangulation found
554 aTRF=BRep_Tool::Triangulation(aF, aLoc);
557 const gp_Trsf& aTrsf=aLoc.Transformation();
558 const Poly_Array1OfTriangle& aTrs=aTRF->Triangles();
559 const TColgp_Array1OfPnt& aNodes=aTRF->Nodes();
561 // map link/nbtriangles
564 for (j=j1; j<=j2; ++j) {
565 const Poly_Triangle& aTr=aTrs(j);
566 aTr.Get(n[0], n[1], n[2]);
568 for (k=0; k<3; ++k) {
569 GEOMAlgo_PassKey aPK;
571 aPK.SetIds(n[k], n[k+1]);
572 if (aMPKI.IsBound(aPK)) {
573 Standard_Integer& iCntX=aMPKI.ChangeFind(aPK);
582 // boundary nodes aMBN
583 aNbLinks=aMPKI.Extent();
584 aIt.Initialize(aMPKI);
585 for (; aIt.More(); aIt.Next()) {
588 const GEOMAlgo_PassKey& aPK=aIt.Key();
590 aNx=(Standard_Integer)aPK.Id(1);
592 aNx=(Standard_Integer)aPK.Id(2);
597 // inner nodes=all_nodes - boundary_nodes
600 for (j=j1; j<=j2; ++j) {
601 if (!aMBN.Contains(j)) {
602 aP=aNodes(j).Transformed(aTrsf);
609 if (!aNb && myNbPntsMin) {
610 // try to fill it yourself
611 Standard_Boolean bIsDone;
612 Standard_Integer aN1, aN2;
613 Handle(Geom_Surface) aS;
614 GeomAdaptor_Surface aGAS;
615 GeomAbs_SurfaceType aType;
617 aS=BRep_Tool::Surface(aF);
619 aType=aGAS.GetType();
620 if (aType==GeomAbs_Plane || aType==GeomAbs_Cylinder) {
622 aNbLinks=aMPKI.Extent();
623 aIt.Initialize(aMPKI);
624 for (; aIt.More(); aIt.Next()) {
627 // take the first having occured inner link
629 const GEOMAlgo_PassKey& aPK=aIt.Key();
631 aN1=(Standard_Integer)aPK.Id(1);
632 aN2=(Standard_Integer)aPK.Id(2);
634 aP1=aNodes(aN1).Transformed(aTrsf);
635 aP2=aNodes(aN2).Transformed(aTrsf);
637 if (aType==GeomAbs_Cylinder) {
638 Standard_Real aTolSM;
641 aTolSM=1.523e-6;//~1.-cos(0.1 deg)
642 aCyl=aGAS.Cylinder();
643 if (!GEOMAlgo_SurfaceTools::IsCoaxial(aP1, aP2, aCyl, aTolSM)) {
648 BRepLib_MakeEdge aBME(aP1, aP2);
649 bIsDone=aBME.IsDone();
651 myErrorStatus=30; //can not obtain the line fron the link
655 const TopoDS_Shape& aSx=aBME.Shape();
656 const TopoDS_Edge& aE=TopoDS::Edge(aSx);
658 InnerPoints(aE, myNbPntsMin, aLP);
661 }// for (; aIt.More(); aIt.Next())
662 }// if (aType==GeomAbs_Plane || aType==GeomAbs_Cylinder)
663 }// if (!aNb && myNbPntsMin) {
665 //modified by NIZNHY-PKV Thu Jan 26 09:56:20 2012f
666 //=======================================================================
667 //function : InnerPoints
669 //=======================================================================
670 void GEOMAlgo_FinderShapeOn1::InnerPoints(const TopoDS_Edge& aE,
671 GEOMAlgo_ListOfPnt& aLP)
673 Standard_Integer aNbPntsMin;
679 InnerPoints(aE, aNbPntsMin, aLP);
681 //modified by NIZNHY-PKV Thu Jan 26 09:56:32 2012t
682 //=======================================================================
683 //function : InnerPoints
685 //=======================================================================
686 void GEOMAlgo_FinderShapeOn1::InnerPoints(const TopoDS_Edge& aE,
687 const Standard_Integer aNbPntsMin,
688 GEOMAlgo_ListOfPnt& aLP)
690 Standard_Boolean bInf1, bInf2;
691 Standard_Integer j, aNbT;
692 Standard_Real dT, aT, aT1, aT2;
694 Handle(Geom_Curve) aC3D;
696 aC3D=BRep_Tool::Curve(aE, aT1, aT2);
701 bInf1=Precision::IsNegativeInfinite(aT1);
702 bInf2=Precision::IsPositiveInfinite(aT2);
703 if (bInf1 || bInf2) {
707 //modified by NIZNHY-PKV Thu Jan 26 09:51:20 2012f
711 for (j=1; j<=aNbPntsMin; ++j) {
719 for (j=1; j<aNbT; ++j) {
724 //modified by NIZNHY-PKV Thu Jan 26 09:51:24 2012t
727 //=======================================================================
728 //function : CheckData
730 //=======================================================================
731 void GEOMAlgo_FinderShapeOn1::CheckData()
735 if(mySurface.IsNull()) {
736 myErrorStatus=10; // mySurface=NULL
740 if (myShape.IsNull()) {
741 myErrorStatus=11; // myShape=NULL
745 if (!(myShapeType==TopAbs_VERTEX ||
746 myShapeType==TopAbs_EDGE ||
747 myShapeType==TopAbs_FACE ||
748 myShapeType==TopAbs_SOLID)) {
749 myErrorStatus=12; // unallowed subshape type
753 if (myState==GEOMAlgo_ST_UNKNOWN ||
754 myState==GEOMAlgo_ST_INOUT) {
755 myErrorStatus=13; // unallowed state type
759 GeomAbs_SurfaceType aType;
761 myGAS.Load(mySurface);
762 aType=myGAS.GetType();
763 if (!(aType==GeomAbs_Plane ||
764 aType==GeomAbs_Cylinder ||
765 aType==GeomAbs_Sphere)) {
766 myErrorStatus=14; // unallowed surface type
770 //=======================================================================
771 //function : GetPointState
773 //=======================================================================
775 TopAbs_State GEOMAlgo_FinderShapeOn1::GetPointState(const gp_Pnt& aP)
778 GEOMAlgo_SurfaceTools::GetState(aP, myGAS, myTolerance, aSt);
786 // 10 -mySurface=NULL
788 // 12 -unallowed type of subshapes
789 // 13 -unallowed state
790 // 14 -unallowed surface type
791 // 15 -unallowed surface type
792 // 20- no triangulation found
793 // 30- can not obtain the line from the link
795 //modified by NIZNHY-PKV Thu Jan 26 10:01:14 2012f
797 //=======================================================================
798 //function : InnerPoints
800 //=======================================================================
801 void GEOMAlgo_FinderShapeOn1::InnerPoints(const TopoDS_Edge& aE,
802 GEOMAlgo_ListOfPnt& aLP)
806 Standard_Integer j, aNbNodes, aIndex, aNb;
807 Handle(Poly_PolygonOnTriangulation) aPTE;
808 Handle(Poly_Triangulation) aTRE;
809 TopLoc_Location aLoc;
813 BRep_Tool::PolygonOnTriangulation(aE, aPTE, aTRE, aLoc);
814 if (aTRE.IsNull() || aPTE.IsNull()) {
815 Handle(Poly_Polygon3D) aPE = BRep_Tool::Polygon3D(aE, aLoc);
817 if (!GEOMAlgo_FinderShapeOn::BuildTriangulation(aE)) {
818 myErrorStatus=20; // no triangulation found
821 aPE = BRep_Tool::Polygon3D(aE, aLoc);
823 const gp_Trsf& aTrsf=aLoc.Transformation();
824 const TColgp_Array1OfPnt& aNodes=aPE->Nodes();
826 aNbNodes=aPE->NbNodes();
827 Standard_Integer low = aNodes.Lower(), up = aNodes.Upper();
828 for (j=low+1; j<up; ++j) {
829 aP=aNodes(j).Transformed(aTrsf);
834 const gp_Trsf& aTrsf=aLoc.Transformation();
835 const TColgp_Array1OfPnt& aNodes=aTRE->Nodes();
837 aNbNodes=aPTE->NbNodes();
838 const TColStd_Array1OfInteger& aInds=aPTE->Nodes();
839 for (j=2; j<aNbNodes; ++j) {
841 aP=aNodes(aIndex).Transformed(aTrsf);
847 if (!aNb && myNbPntsMin) {
848 // try to fill it yourself
849 InnerPoints(aE, myNbPntsMin, aLP);
854 //modified by NIZNHY-PKV Thu Jan 26 10:01:17 2012t