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.ixx>
28 #include <GEOMAlgo_ListIteratorOfListOfPnt.hxx>
30 #include <GEOMAlgo_SurfaceTools.hxx>
31 #include <GEOMAlgo_StateCollector.hxx>
32 #include <GEOMAlgo_FinderShapeOn.hxx>
34 #include <GEOMAlgo_PassKey.hxx>
35 #include <GEOMAlgo_DataMapOfPassKeyInteger.hxx>
36 #include <GEOMAlgo_DataMapIteratorOfDataMapOfPassKeyInteger.hxx>
38 #include <Basics_OCCTVersion.hxx>
42 #include <Precision.hxx>
43 #include <TColStd_Array1OfInteger.hxx>
44 #include <TColStd_MapOfInteger.hxx>
46 #include <gp_Trsf.hxx>
47 #include <gp_Cylinder.hxx>
50 #include <TColgp_Array1OfPnt.hxx>
52 #include <Poly_Array1OfTriangle.hxx>
53 #include <Poly_Triangle.hxx>
54 #include <Poly_PolygonOnTriangulation.hxx>
55 #include <Poly_Triangulation.hxx>
56 #include <Poly_Polygon3D.hxx>
58 #include <Geom_Curve.hxx>
59 #include <Geom_Surface.hxx>
60 #include <GeomAdaptor_Surface.hxx>
61 #include <GeomAbs_SurfaceType.hxx>
62 #include <GeomAdaptor_Curve.hxx>
63 #include <GeomAbs_CurveType.hxx>
65 #include <TopAbs_State.hxx>
67 #include <TopLoc_Location.hxx>
69 #include <TopoDS_Shape.hxx>
70 #include <TopoDS_Vertex.hxx>
71 #include <TopoDS_Face.hxx>
72 #include <TopoDS_Edge.hxx>
75 #include <TopExp_Explorer.hxx>
77 #include <TopTools_IndexedMapOfShape.hxx>
79 #include <BRep_Tool.hxx>
80 #include <BRepLib_MakeEdge.hxx>
82 //=======================================================================
83 //function : GEOMAlgo_FinderShapeOn1
85 //=======================================================================
86 GEOMAlgo_FinderShapeOn1::GEOMAlgo_FinderShapeOn1()
91 myShapeType=TopAbs_VERTEX;
92 myState=GEOMAlgo_ST_UNKNOWN;
96 //=======================================================================
99 //=======================================================================
100 GEOMAlgo_FinderShapeOn1::~GEOMAlgo_FinderShapeOn1()
103 //=======================================================================
104 //function : SetSurface
106 //=======================================================================
107 void GEOMAlgo_FinderShapeOn1::SetSurface(const Handle(Geom_Surface)& aS)
111 //=======================================================================
114 //=======================================================================
115 const Handle(Geom_Surface)& GEOMAlgo_FinderShapeOn1::Surface() const
119 //=======================================================================
120 //function : SetShapeType
122 //=======================================================================
123 void GEOMAlgo_FinderShapeOn1::SetShapeType(const TopAbs_ShapeEnum aType)
127 //=======================================================================
128 //function : ShapeType
130 //=======================================================================
131 TopAbs_ShapeEnum GEOMAlgo_FinderShapeOn1::ShapeType()const
135 //=======================================================================
136 //function : SetState
138 //=======================================================================
139 void GEOMAlgo_FinderShapeOn1::SetState(const GEOMAlgo_State aState)
143 //=======================================================================
146 //=======================================================================
147 GEOMAlgo_State GEOMAlgo_FinderShapeOn1::State() const
151 //=======================================================================
152 //function : SetNbPntsMin
154 //=======================================================================
155 void GEOMAlgo_FinderShapeOn1::SetNbPntsMin(const Standard_Integer aNb)
159 //=======================================================================
160 //function : NbPntsMin
162 //=======================================================================
163 Standard_Integer GEOMAlgo_FinderShapeOn1::NbPntsMin()const
167 //=======================================================================
168 //function : SetNbPntsMax
170 //=======================================================================
171 void GEOMAlgo_FinderShapeOn1::SetNbPntsMax(const Standard_Integer aNb)
175 //=======================================================================
176 //function : NbPntsMax
178 //=======================================================================
179 Standard_Integer GEOMAlgo_FinderShapeOn1::NbPntsMax()const
183 //=======================================================================
186 //=======================================================================
187 const GEOMAlgo_IndexedDataMapOfShapeState& GEOMAlgo_FinderShapeOn1::MSS() const
191 //=======================================================================
194 //=======================================================================
195 const TopTools_ListOfShape& GEOMAlgo_FinderShapeOn1::Shapes() const
197 Standard_Integer i, aNb;
198 TopTools_ListOfShape* pL;
200 pL=(TopTools_ListOfShape*) &myLS;
204 for (i=1; i<=aNb; ++i) {
205 const TopoDS_Shape& aS=myMSS.FindKey(i);
206 if (aS.ShapeType()==myShapeType) {
212 //=======================================================================
215 //=======================================================================
216 void GEOMAlgo_FinderShapeOn1::Perform()
228 #if OCC_VERSION_LARGE > 0x06050200
229 // Initialize the context
230 GEOMAlgo_ShapeAlgo::Perform();
238 if (myShapeType==TopAbs_VERTEX) {
247 if (myShapeType==TopAbs_EDGE) {
256 if (myShapeType==TopAbs_FACE) {
264 //=======================================================================
265 //function : ProcessVertices
267 //=======================================================================
268 void GEOMAlgo_FinderShapeOn1::ProcessVertices()
272 Standard_Boolean bIsConformState;
273 Standard_Integer i, aNb;
275 TopTools_IndexedMapOfShape aM;
278 TopExp::MapShapes(myShape, TopAbs_VERTEX, aM);
280 for (i=1; i<=aNb; ++i) {
281 const TopoDS_Vertex& aV=TopoDS::Vertex(aM(i));
282 aP=BRep_Tool::Pnt(aV);
284 aSt = GetPointState( aP );
285 bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
287 if (myShapeType==TopAbs_VERTEX){
288 if (bIsConformState) {
292 else if (bIsConformState || aSt==TopAbs_ON) {
297 //=======================================================================
298 //function : ProcessEdges
300 //=======================================================================
301 void GEOMAlgo_FinderShapeOn1::ProcessEdges()
305 Standard_Boolean bIsConformState, bIsToBreak;
306 Standard_Integer i, aNb, iCnt;
308 TopTools_IndexedMapOfShape aM;
309 TopExp_Explorer aExp;
310 GEOMAlgo_ListIteratorOfListOfPnt aIt;
311 GeomAbs_SurfaceType aType1;
313 aType1=myGAS.GetType();
315 TopExp::MapShapes(myShape, TopAbs_EDGE, aM);
317 for (i=1; i<=aNb; ++i) {
318 GEOMAlgo_ListOfPnt aLP;
319 GEOMAlgo_StateCollector aSC;
321 const TopoDS_Edge& aE=TopoDS::Edge(aM(i));
323 aExp.Init(aE, TopAbs_VERTEX);
324 for (; aExp.More(); aExp.Next()) {
325 const TopoDS_Shape& aV=aExp.Current();
327 bIsConformState=myMSS.Contains(aV);
328 if (!bIsConformState) {
329 break;// vertex has non-conformed state
332 aSt=myMSS.FindFromKey(aV);
333 aSC.AppendState(aSt);
337 if (!bIsConformState) {
338 continue; // vertex has non-conformed state,skip edge
341 if (BRep_Tool::Degenerated(aE)) {
346 if (myState==GEOMAlgo_ST_ON && aType1==GeomAbs_Sphere) {
347 Standard_Real aT1, aT2;
348 Handle(Geom_Curve) aC;
349 GeomAdaptor_Curve aGAC;
350 GeomAbs_CurveType aType2;
352 aC=BRep_Tool::Curve(aE, aT1, aT2);
355 aType2=aGAC.GetType();
356 if (aType2==GeomAbs_Line) {
361 InnerPoints(aE, aLP);
366 bIsConformState=Standard_True;
368 for (iCnt=0; aIt.More(); aIt.Next(), ++iCnt) {
370 if (iCnt > myNbPntsMax) {
375 const gp_Pnt& aP=aIt.Value();
376 aSt = GetPointState( aP );
377 bIsToBreak=aSC.AppendState(aSt);
385 bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
386 if (myShapeType==TopAbs_EDGE) {
387 if (bIsConformState) {
391 else if (bIsConformState || aSt==TopAbs_ON) {
394 } // for (i=1; i<=aNb; ++i) next edge
396 //=======================================================================
397 //function : ProcessFaces
399 //=======================================================================
400 void GEOMAlgo_FinderShapeOn1::ProcessFaces()
404 Standard_Boolean bIsConformState, bIsToBreak;
405 Standard_Integer i, aNbF, iCnt;
407 TopTools_IndexedMapOfShape aM;
408 TopExp_Explorer aExp;
409 GEOMAlgo_ListIteratorOfListOfPnt aIt;
410 GeomAbs_SurfaceType aType1, aType2;
412 aType1=myGAS.GetType();
414 TopExp::MapShapes(myShape, TopAbs_FACE, aM);
416 for (i=1; i<=aNbF; ++i) {
417 GEOMAlgo_StateCollector aSC;
418 GEOMAlgo_ListOfPnt aLP;
420 const TopoDS_Face& aF=TopoDS::Face(aM(i));
422 if (myState==GEOMAlgo_ST_ON) {
423 Handle(Geom_Surface) aS;
424 GeomAdaptor_Surface aGAS;
426 aS=BRep_Tool::Surface(aF);
428 aType2=aGAS.GetType();
429 if (aType2!=aType1) {
434 aExp.Init(aF, TopAbs_EDGE);
435 for (; aExp.More(); aExp.Next()) {
436 const TopoDS_Shape& aE=aExp.Current();
437 bIsConformState=myMSS.Contains(aE);
438 if (!bIsConformState) {
439 break;// edge has non-conformed state
442 aSt=myMSS.FindFromKey(aE);
443 aSC.AppendState(aSt);
447 if (!bIsConformState) {
448 continue; // edge has non-conformed state,skip face
451 InnerPoints(aF, aLP);
456 bIsConformState=Standard_True;
458 for (iCnt=0; aIt.More(); aIt.Next(), ++iCnt) {
460 if (iCnt > myNbPntsMax) {
465 const gp_Pnt& aP=aIt.Value();
466 aSt = GetPointState( aP );
467 bIsToBreak=aSC.AppendState(aSt);
475 bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
476 if (myShapeType==TopAbs_FACE) {
477 if (bIsConformState) {
481 else if (bIsConformState || aSt==TopAbs_ON) {
484 }// for (i=1; i<=aNb; ++i) next face
486 //=======================================================================
487 //function : ProcessSolids
489 //=======================================================================
490 void GEOMAlgo_FinderShapeOn1::ProcessSolids()
494 Standard_Boolean bIsConformState;
495 Standard_Integer i, aNbS, j, aNbF;
496 TopTools_IndexedMapOfShape aM, aMF;
499 TopExp::MapShapes(myShape, TopAbs_SOLID, aM);
501 for (i=1; i<=aNbS; ++i) {
502 GEOMAlgo_StateCollector aSC;
504 const TopoDS_Shape& aSd=aM(i);
506 TopExp::MapShapes(aSd, TopAbs_FACE, aMF);
508 for (j=1; j<=aNbF; ++j) {
509 const TopoDS_Shape& aF=aMF(j);
510 bIsConformState=myMSS.Contains(aF);
511 if (!bIsConformState) {
512 break;// face has non-conformed state
515 aSt=myMSS.FindFromKey(aF);
516 aSC.AppendState(aSt);
520 if (!bIsConformState) {
521 continue; // face has non-conformed state,skip solid
526 bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
527 if (bIsConformState) {
533 //=======================================================================
534 //function : InnerPoints
536 //=======================================================================
537 void GEOMAlgo_FinderShapeOn1::InnerPoints(const TopoDS_Face& aF,
538 GEOMAlgo_ListOfPnt& aLP)
542 Standard_Integer j, j1, j2, k, n[4], aNbLinks, aNx, aNb, iCnt;//, aNbMax, *pIds;
543 TopLoc_Location aLoc;
544 Handle(Poly_Triangulation) aTRF;
545 TColStd_MapOfInteger aMBN;
546 GEOMAlgo_DataMapOfPassKeyInteger aMPKI;
547 GEOMAlgo_DataMapIteratorOfDataMapOfPassKeyInteger aIt;
552 aTRF=BRep_Tool::Triangulation(aF, aLoc);
554 if (!GEOMAlgo_FinderShapeOn::BuildTriangulation(aF)) {
555 myWarningStatus=20; // no triangulation found
558 aTRF=BRep_Tool::Triangulation(aF, aLoc);
561 const gp_Trsf& aTrsf=aLoc.Transformation();
562 const Poly_Array1OfTriangle& aTrs=aTRF->Triangles();
563 const TColgp_Array1OfPnt& aNodes=aTRF->Nodes();
565 // map link/nbtriangles
568 for (j=j1; j<=j2; ++j) {
569 const Poly_Triangle& aTr=aTrs(j);
570 aTr.Get(n[0], n[1], n[2]);
572 for (k=0; k<3; ++k) {
573 GEOMAlgo_PassKey aPK;
575 aPK.SetIds(n[k], n[k+1]);
576 if (aMPKI.IsBound(aPK)) {
577 Standard_Integer& iCntX=aMPKI.ChangeFind(aPK);
586 // boundary nodes aMBN
587 aNbLinks=aMPKI.Extent();
588 aIt.Initialize(aMPKI);
589 for (; aIt.More(); aIt.Next()) {
592 const GEOMAlgo_PassKey& aPK=aIt.Key();
594 aNx=(Standard_Integer)aPK.Id(1);
596 aNx=(Standard_Integer)aPK.Id(2);
601 // inner nodes=all_nodes - boundary_nodes
604 for (j=j1; j<=j2; ++j) {
605 if (!aMBN.Contains(j)) {
606 aP=aNodes(j).Transformed(aTrsf);
613 if (!aNb && myNbPntsMin) {
614 // try to fill it yourself
615 Standard_Boolean bIsDone;
616 Standard_Integer aN1, aN2;
617 Handle(Geom_Surface) aS;
618 GeomAdaptor_Surface aGAS;
619 GeomAbs_SurfaceType aType;
621 aS=BRep_Tool::Surface(aF);
623 aType=aGAS.GetType();
624 if (aType==GeomAbs_Plane || aType==GeomAbs_Cylinder) {
626 aNbLinks=aMPKI.Extent();
627 aIt.Initialize(aMPKI);
628 for (; aIt.More(); aIt.Next()) {
631 // take the first having occured inner link
633 const GEOMAlgo_PassKey& aPK=aIt.Key();
635 aN1=(Standard_Integer)aPK.Id(1);
636 aN2=(Standard_Integer)aPK.Id(2);
638 aP1=aNodes(aN1).Transformed(aTrsf);
639 aP2=aNodes(aN2).Transformed(aTrsf);
641 if (aType==GeomAbs_Cylinder) {
642 Standard_Real aTolSM;
645 aTolSM=1.523e-6;//~1.-cos(0.1 deg)
646 aCyl=aGAS.Cylinder();
647 if (!GEOMAlgo_SurfaceTools::IsCoaxial(aP1, aP2, aCyl, aTolSM)) {
652 BRepLib_MakeEdge aBME(aP1, aP2);
653 bIsDone=aBME.IsDone();
655 myErrorStatus=30; //can not obtain the line fron the link
659 const TopoDS_Shape& aSx=aBME.Shape();
660 const TopoDS_Edge& aE=TopoDS::Edge(aSx);
662 InnerPoints(aE, myNbPntsMin, aLP);
665 }// for (; aIt.More(); aIt.Next())
666 }// if (aType==GeomAbs_Plane || aType==GeomAbs_Cylinder)
667 }// if (!aNb && myNbPntsMin) {
669 //modified by NIZNHY-PKV Thu Jan 26 09:56:20 2012f
670 //=======================================================================
671 //function : InnerPoints
673 //=======================================================================
674 void GEOMAlgo_FinderShapeOn1::InnerPoints(const TopoDS_Edge& aE,
675 GEOMAlgo_ListOfPnt& aLP)
677 Standard_Integer aNbPntsMin;
683 InnerPoints(aE, aNbPntsMin, aLP);
685 //modified by NIZNHY-PKV Thu Jan 26 09:56:32 2012t
686 //=======================================================================
687 //function : InnerPoints
689 //=======================================================================
690 void GEOMAlgo_FinderShapeOn1::InnerPoints(const TopoDS_Edge& aE,
691 const Standard_Integer aNbPntsMin,
692 GEOMAlgo_ListOfPnt& aLP)
694 Standard_Boolean bInf1, bInf2;
695 Standard_Integer j, aNbT;
696 Standard_Real dT, aT, aT1, aT2;
698 Handle(Geom_Curve) aC3D;
700 aC3D=BRep_Tool::Curve(aE, aT1, aT2);
705 bInf1=Precision::IsNegativeInfinite(aT1);
706 bInf2=Precision::IsPositiveInfinite(aT2);
707 if (bInf1 || bInf2) {
711 //modified by NIZNHY-PKV Thu Jan 26 09:51:20 2012f
715 for (j=1; j<=aNbPntsMin; ++j) {
723 for (j=1; j<aNbT; ++j) {
728 //modified by NIZNHY-PKV Thu Jan 26 09:51:24 2012t
731 //=======================================================================
732 //function : CheckData
734 //=======================================================================
735 void GEOMAlgo_FinderShapeOn1::CheckData()
739 if(mySurface.IsNull()) {
740 myErrorStatus=10; // mySurface=NULL
744 if (myShape.IsNull()) {
745 myErrorStatus=11; // myShape=NULL
749 if (!(myShapeType==TopAbs_VERTEX ||
750 myShapeType==TopAbs_EDGE ||
751 myShapeType==TopAbs_FACE ||
752 myShapeType==TopAbs_SOLID)) {
753 myErrorStatus=12; // unallowed subshape type
757 if (myState==GEOMAlgo_ST_UNKNOWN ||
758 myState==GEOMAlgo_ST_INOUT) {
759 myErrorStatus=13; // unallowed state type
763 GeomAbs_SurfaceType aType;
765 myGAS.Load(mySurface);
766 aType=myGAS.GetType();
767 if (!(aType==GeomAbs_Plane ||
768 aType==GeomAbs_Cylinder ||
769 aType==GeomAbs_Sphere)) {
770 myErrorStatus=14; // unallowed surface type
774 //=======================================================================
775 //function : GetPointState
777 //=======================================================================
779 TopAbs_State GEOMAlgo_FinderShapeOn1::GetPointState(const gp_Pnt& aP)
782 GEOMAlgo_SurfaceTools::GetState(aP, myGAS, myTolerance, aSt);
790 // 10 -mySurface=NULL
792 // 12 -unallowed type of subshapes
793 // 13 -unallowed state
794 // 14 -unallowed surface type
795 // 15 -unallowed surface type
796 // 20- no triangulation found
797 // 30- can not obtain the line from the link
799 //modified by NIZNHY-PKV Thu Jan 26 10:01:14 2012f
801 //=======================================================================
802 //function : InnerPoints
804 //=======================================================================
805 void GEOMAlgo_FinderShapeOn1::InnerPoints(const TopoDS_Edge& aE,
806 GEOMAlgo_ListOfPnt& aLP)
810 Standard_Integer j, aNbNodes, aIndex, aNb;
811 Handle(Poly_PolygonOnTriangulation) aPTE;
812 Handle(Poly_Triangulation) aTRE;
813 TopLoc_Location aLoc;
817 BRep_Tool::PolygonOnTriangulation(aE, aPTE, aTRE, aLoc);
818 if (aTRE.IsNull() || aPTE.IsNull()) {
819 Handle(Poly_Polygon3D) aPE = BRep_Tool::Polygon3D(aE, aLoc);
821 if (!GEOMAlgo_FinderShapeOn::BuildTriangulation(aE)) {
822 myErrorStatus=20; // no triangulation found
825 aPE = BRep_Tool::Polygon3D(aE, aLoc);
827 const gp_Trsf& aTrsf=aLoc.Transformation();
828 const TColgp_Array1OfPnt& aNodes=aPE->Nodes();
830 aNbNodes=aPE->NbNodes();
831 Standard_Integer low = aNodes.Lower(), up = aNodes.Upper();
832 for (j=low+1; j<up; ++j) {
833 aP=aNodes(j).Transformed(aTrsf);
838 const gp_Trsf& aTrsf=aLoc.Transformation();
839 const TColgp_Array1OfPnt& aNodes=aTRE->Nodes();
841 aNbNodes=aPTE->NbNodes();
842 const TColStd_Array1OfInteger& aInds=aPTE->Nodes();
843 for (j=2; j<aNbNodes; ++j) {
845 aP=aNodes(aIndex).Transformed(aTrsf);
851 if (!aNb && myNbPntsMin) {
852 // try to fill it yourself
853 InnerPoints(aE, myNbPntsMin, aLP);
858 //modified by NIZNHY-PKV Thu Jan 26 10:01:17 2012t