1 // Copyright (C) 2007-2010 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
23 // File: GEOMAlgo_FinderShapeOn1.cxx
24 // Created: Fri Mar 4 10:31:06 2005
25 // Author: Peter KURNEV
28 #include <GEOMAlgo_FinderShapeOn2.ixx>
32 #include <Precision.hxx>
33 #include <TColStd_Array1OfInteger.hxx>
34 #include <TColStd_MapOfInteger.hxx>
36 #include <gp_Trsf.hxx>
37 #include <gp_Cylinder.hxx>
40 #include <TColgp_Array1OfPnt.hxx>
42 #include <Poly_Array1OfTriangle.hxx>
43 #include <Poly_Triangle.hxx>
44 #include <Poly_PolygonOnTriangulation.hxx>
45 #include <Poly_Triangulation.hxx>
46 #include <Poly_Polygon3D.hxx>
48 #include <Geom_Curve.hxx>
49 #include <Geom_Surface.hxx>
50 #include <GeomAdaptor_Surface.hxx>
51 #include <GeomAbs_SurfaceType.hxx>
52 #include <GeomAdaptor_Curve.hxx>
53 #include <GeomAbs_CurveType.hxx>
55 #include <TopAbs_State.hxx>
57 #include <TopLoc_Location.hxx>
59 #include <TopoDS_Shape.hxx>
60 #include <TopoDS_Vertex.hxx>
61 #include <TopoDS_Face.hxx>
62 #include <TopoDS_Edge.hxx>
65 #include <TopExp_Explorer.hxx>
67 #include <TopTools_IndexedMapOfShape.hxx>
69 #include <BRep_Tool.hxx>
70 #include <BRepLib_MakeEdge.hxx>
72 #include <GEOMAlgo_ListIteratorOfListOfPnt.hxx>
74 #include <GEOMAlgo_SurfaceTools.hxx>
75 #include <GEOMAlgo_StateCollector.hxx>
76 #include <GEOMAlgo_FinderShapeOn.hxx>
78 #include <GEOMAlgo_PassKey.hxx>
79 #include <GEOMAlgo_DataMapOfPassKeyInteger.hxx>
80 #include <GEOMAlgo_DataMapIteratorOfDataMapOfPassKeyInteger.hxx>
82 //=======================================================================
83 //function : GEOMAlgo_FinderShapeOn1
85 //=======================================================================
86 GEOMAlgo_FinderShapeOn2::GEOMAlgo_FinderShapeOn2()
91 myShapeType=TopAbs_VERTEX;
92 myState=GEOMAlgo_ST_UNKNOWN;
96 //=======================================================================
99 //=======================================================================
100 GEOMAlgo_FinderShapeOn2::~GEOMAlgo_FinderShapeOn2()
103 //=======================================================================
106 //=======================================================================
107 void GEOMAlgo_FinderShapeOn2::SetClsf(const Handle(GEOMAlgo_Clsf)& aClsf)
111 //=======================================================================
114 //=======================================================================
115 const Handle(GEOMAlgo_Clsf)& GEOMAlgo_FinderShapeOn2::Clsf() const
119 //=======================================================================
120 //function : SetShapeType
122 //=======================================================================
123 void GEOMAlgo_FinderShapeOn2::SetShapeType(const TopAbs_ShapeEnum aType)
127 //=======================================================================
128 //function : ShapeType
130 //=======================================================================
131 TopAbs_ShapeEnum GEOMAlgo_FinderShapeOn2::ShapeType()const
135 //=======================================================================
136 //function : SetState
138 //=======================================================================
139 void GEOMAlgo_FinderShapeOn2::SetState(const GEOMAlgo_State aState)
143 //=======================================================================
146 //=======================================================================
147 GEOMAlgo_State GEOMAlgo_FinderShapeOn2::State() const
151 //=======================================================================
152 //function : SetNbPntsMin
154 //=======================================================================
155 void GEOMAlgo_FinderShapeOn2::SetNbPntsMin(const Standard_Integer aNb)
159 //=======================================================================
160 //function : NbPntsMin
162 //=======================================================================
163 Standard_Integer GEOMAlgo_FinderShapeOn2::NbPntsMin()const
167 //=======================================================================
168 //function : SetNbPntsMax
170 //=======================================================================
171 void GEOMAlgo_FinderShapeOn2::SetNbPntsMax(const Standard_Integer aNb)
175 //=======================================================================
176 //function : NbPntsMax
178 //=======================================================================
179 Standard_Integer GEOMAlgo_FinderShapeOn2::NbPntsMax()const
183 //=======================================================================
186 //=======================================================================
187 const GEOMAlgo_IndexedDataMapOfShapeState& GEOMAlgo_FinderShapeOn2::MSS() const
191 //=======================================================================
194 //=======================================================================
195 const TopTools_ListOfShape& GEOMAlgo_FinderShapeOn2::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_FinderShapeOn2::Perform()
228 myClsf->SetTolerance(myTolerance);
235 if (myShapeType==TopAbs_VERTEX) {
244 if (myShapeType==TopAbs_EDGE) {
253 if (myShapeType==TopAbs_FACE) {
261 //=======================================================================
262 //function : CheckData
264 //=======================================================================
265 void GEOMAlgo_FinderShapeOn2::CheckData()
267 Standard_Integer iErr;
271 if(myClsf.IsNull()) {
272 myErrorStatus=10; // myClsf=NULL
277 iErr=myClsf->ErrorStatus();
279 myErrorStatus=41; // invalid data for classifier
283 if (myShape.IsNull()) {
284 myErrorStatus=11; // myShape=NULL
288 if (!(myShapeType==TopAbs_VERTEX ||
289 myShapeType==TopAbs_EDGE ||
290 myShapeType==TopAbs_FACE ||
291 myShapeType==TopAbs_SOLID)) {
292 myErrorStatus=12; // unallowed subshape type
296 if (myState==GEOMAlgo_ST_UNKNOWN ||
297 myState==GEOMAlgo_ST_INOUT) {
298 myErrorStatus=13; // unallowed state type
302 //=======================================================================
303 //function : ProcessVertices
305 //=======================================================================
306 void GEOMAlgo_FinderShapeOn2::ProcessVertices()
310 Standard_Boolean bIsConformState;
311 Standard_Integer i, aNb, iErr;
313 TopTools_IndexedMapOfShape aM;
316 TopExp::MapShapes(myShape, TopAbs_VERTEX, aM);
318 for (i=1; i<=aNb; ++i) {
319 const TopoDS_Vertex& aV=TopoDS::Vertex(aM(i));
320 aP=BRep_Tool::Pnt(aV);
324 iErr=myClsf->ErrorStatus();
326 myErrorStatus=40; // point can not be classified
331 bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
333 if (myShapeType==TopAbs_VERTEX){
334 if (bIsConformState) {
338 else if (bIsConformState || aSt==TopAbs_ON) {
343 //=======================================================================
344 //function : ProcessEdges
346 //=======================================================================
347 void GEOMAlgo_FinderShapeOn2::ProcessEdges()
351 Standard_Boolean bIsConformState, bIsToBreak;
352 Standard_Integer i, aNb, iCnt, iErr;
354 TopTools_IndexedMapOfShape aM;
355 TopExp_Explorer aExp;
356 GEOMAlgo_ListIteratorOfListOfPnt aIt;
358 TopExp::MapShapes(myShape, TopAbs_EDGE, aM);
360 for (i=1; i<=aNb; ++i) {
361 GEOMAlgo_ListOfPnt aLP;
362 GEOMAlgo_StateCollector aSC;
364 const TopoDS_Edge& aE=TopoDS::Edge(aM(i));
366 aExp.Init(aE, TopAbs_VERTEX);
367 for (; aExp.More(); aExp.Next()) {
368 const TopoDS_Shape& aV=aExp.Current();
370 bIsConformState=myMSS.Contains(aV);
371 if (!bIsConformState) {
372 break;// vertex has non-conformed state
375 aSt=myMSS.FindFromKey(aV);
376 aSC.AppendState(aSt);
380 if (!bIsConformState) {
381 continue; // vertex has non-conformed state,skip edge
384 if (BRep_Tool::Degenerated(aE)) {
389 if (myState==GEOMAlgo_ST_ON) {
390 Standard_Boolean bCanBeON;
391 Standard_Real aT1, aT2;
392 Handle(Geom_Curve) aC;
394 aC=BRep_Tool::Curve(aE, aT1, aT2);
395 bCanBeON=myClsf->CanBeON(aC);
401 InnerPoints(aE, aLP);
406 bIsConformState=Standard_True;
408 for (iCnt=0; aIt.More(); aIt.Next(), ++iCnt) {
410 if (iCnt > myNbPntsMax) {
415 const gp_Pnt& aP=aIt.Value();
419 iErr=myClsf->ErrorStatus();
421 myErrorStatus=40; // point can not be classified
427 bIsToBreak=aSC.AppendState(aSt);
435 bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
436 if (myShapeType==TopAbs_EDGE) {
437 if (bIsConformState) {
441 else if (bIsConformState || aSt==TopAbs_ON) {
444 } // for (i=1; i<=aNb; ++i) next edge
446 //=======================================================================
447 //function : ProcessFaces
449 //=======================================================================
450 void GEOMAlgo_FinderShapeOn2::ProcessFaces()
454 Standard_Boolean bIsConformState, bIsToBreak, bCanBeON;
455 Standard_Integer i, aNbF, iCnt, iErr;
457 TopTools_IndexedMapOfShape aM;
458 TopExp_Explorer aExp;
459 GEOMAlgo_ListIteratorOfListOfPnt aIt;
461 TopExp::MapShapes(myShape, TopAbs_FACE, aM);
463 for (i=1; i<=aNbF; ++i) {
464 GEOMAlgo_StateCollector aSC;
465 GEOMAlgo_ListOfPnt aLP;
467 const TopoDS_Face& aF=TopoDS::Face(aM(i));
469 if (myState==GEOMAlgo_ST_ON) {
470 Handle(Geom_Surface) aS;
472 aS=BRep_Tool::Surface(aF);
473 bCanBeON=myClsf->CanBeON(aS);
479 aExp.Init(aF, TopAbs_EDGE);
480 for (; aExp.More(); aExp.Next()) {
481 const TopoDS_Shape& aE=aExp.Current();
482 bIsConformState=myMSS.Contains(aE);
483 if (!bIsConformState) {
484 break;// edge has non-conformed state
487 aSt=myMSS.FindFromKey(aE);
488 aSC.AppendState(aSt);
492 if (!bIsConformState) {
493 continue; // edge has non-conformed state,skip face
496 InnerPoints(aF, aLP);
501 bIsConformState=Standard_True;
503 for (iCnt=0; aIt.More(); aIt.Next(), ++iCnt) {
505 if (iCnt > myNbPntsMax) {
510 const gp_Pnt& aP=aIt.Value();
514 iErr=myClsf->ErrorStatus();
516 myErrorStatus=40; // point can not be classified
522 bIsToBreak=aSC.AppendState(aSt);
530 bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
531 if (myShapeType==TopAbs_FACE) {
532 if (bIsConformState) {
536 else if (bIsConformState || aSt==TopAbs_ON) {
539 }// for (i=1; i<=aNb; ++i) next face
541 //=======================================================================
542 //function : ProcessSolids
544 //=======================================================================
545 void GEOMAlgo_FinderShapeOn2::ProcessSolids()
549 Standard_Boolean bIsConformState;
550 Standard_Integer i, aNbS, j, aNbF;
551 TopTools_IndexedMapOfShape aM, aMF;
554 TopExp::MapShapes(myShape, TopAbs_SOLID, aM);
556 for (i=1; i<=aNbS; ++i) {
557 GEOMAlgo_StateCollector aSC;
559 const TopoDS_Shape& aSd=aM(i);
561 TopExp::MapShapes(aSd, TopAbs_FACE, aMF);
563 for (j=1; j<=aNbF; ++j) {
564 const TopoDS_Shape& aF=aMF(j);
565 bIsConformState=myMSS.Contains(aF);
566 if (!bIsConformState) {
567 break;// face has non-conformed state
570 aSt=myMSS.FindFromKey(aF);
571 aSC.AppendState(aSt);
575 if (!bIsConformState) {
576 continue; // face has non-conformed state,skip solid
581 bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
582 if (bIsConformState) {
588 //=======================================================================
589 //function : InnerPoints
591 //=======================================================================
592 void GEOMAlgo_FinderShapeOn2::InnerPoints(const TopoDS_Face& aF,
593 GEOMAlgo_ListOfPnt& aLP)
597 Standard_Integer j, j1, j2, k, n[4], aNbLinks, aNx, aNb, iCnt;//, aNbMax, *pIds;
598 TopLoc_Location aLoc;
599 Handle(Poly_Triangulation) aTRF;
600 TColStd_MapOfInteger aMBN;
601 GEOMAlgo_DataMapOfPassKeyInteger aMPKI;
602 GEOMAlgo_DataMapIteratorOfDataMapOfPassKeyInteger aIt;
607 aTRF=BRep_Tool::Triangulation(aF, aLoc);
609 if (!GEOMAlgo_FinderShapeOn::BuildTriangulation(aF)) {
610 myWarningStatus=20; // no triangulation found
613 aTRF=BRep_Tool::Triangulation(aF, aLoc);
616 const gp_Trsf& aTrsf=aLoc.Transformation();
617 const Poly_Array1OfTriangle& aTrs=aTRF->Triangles();
618 const TColgp_Array1OfPnt& aNodes=aTRF->Nodes();
620 // map link/nbtriangles
623 for (j=j1; j<=j2; ++j) {
624 const Poly_Triangle& aTr=aTrs(j);
625 aTr.Get(n[0], n[1], n[2]);
627 for (k=0; k<3; ++k) {
628 GEOMAlgo_PassKey aPK;
630 aPK.SetIds(n[k], n[k+1]);
631 if (aMPKI.IsBound(aPK)) {
632 Standard_Integer& iCntX=aMPKI.ChangeFind(aPK);
641 // boundary nodes aMBN
642 aNbLinks=aMPKI.Extent();
643 aIt.Initialize(aMPKI);
644 for (; aIt.More(); aIt.Next()) {
647 const GEOMAlgo_PassKey& aPK=aIt.Key();
651 pIds=(Standard_Integer*)aPK.Key();
652 for (k=1; k<3; ++k) {
653 aNx=*(pIds+aNbMax-k);
657 aNx=(Standard_Integer)aPK.Id(1);
659 aNx=(Standard_Integer)aPK.Id(2);
665 // inner nodes=all_nodes - boundary_nodes
668 for (j=j1; j<=j2; ++j) {
669 if (!aMBN.Contains(j)) {
670 aP=aNodes(j).Transformed(aTrsf);
677 if (!aNb && myNbPntsMin) {
678 // try to fill it yourself
679 Standard_Boolean bIsDone;
680 Standard_Integer aN1, aN2;
681 Handle(Geom_Surface) aS;
682 GeomAdaptor_Surface aGAS;
683 GeomAbs_SurfaceType aType;
685 aS=BRep_Tool::Surface(aF);
687 aType=aGAS.GetType();
688 if (aType==GeomAbs_Plane || aType==GeomAbs_Cylinder) {
690 aNbLinks=aMPKI.Extent();
691 aIt.Initialize(aMPKI);
692 for (; aIt.More(); aIt.Next()) {
695 // take the first having occured inner link
697 const GEOMAlgo_PassKey& aPK=aIt.Key();
701 pIds=(Standard_Integer*)aPK.Key();
702 aN1=*(pIds+aNbMax-1);
703 aN2=*(pIds+aNbMax-2);
706 aN1=(Standard_Integer)aPK.Id(1);
707 aN2=(Standard_Integer)aPK.Id(2);
709 aP1=aNodes(aN1).Transformed(aTrsf);
710 aP2=aNodes(aN2).Transformed(aTrsf);
712 if (aType==GeomAbs_Cylinder) {
713 Standard_Real aTolSM;
716 aTolSM=1.523e-6;//~1.-cos(0.1 deg)
717 aCyl=aGAS.Cylinder();
718 if (!GEOMAlgo_SurfaceTools::IsCoaxial(aP1, aP2, aCyl, aTolSM)) {
723 BRepLib_MakeEdge aBME(aP1, aP2);
724 bIsDone=aBME.IsDone();
726 myErrorStatus=30; //can not obtain the line fron the link
730 const TopoDS_Shape& aSx=aBME.Shape();
731 const TopoDS_Edge& aE=TopoDS::Edge(aSx);
733 InnerPoints(aE, myNbPntsMin, aLP);
736 }// for (; aIt.More(); aIt.Next())
737 }// if (aType==GeomAbs_Plane || aType==GeomAbs_Cylinder)
738 }// if (!aNb && myNbPntsMin) {
740 //=======================================================================
741 //function : InnerPoints
743 //=======================================================================
744 void GEOMAlgo_FinderShapeOn2::InnerPoints(const TopoDS_Edge& aE,
745 GEOMAlgo_ListOfPnt& aLP)
749 Standard_Integer j, aNbNodes, aIndex, aNb;
750 Handle(Poly_PolygonOnTriangulation) aPTE;
751 Handle(Poly_Triangulation) aTRE;
752 TopLoc_Location aLoc;
756 BRep_Tool::PolygonOnTriangulation(aE, aPTE, aTRE, aLoc);
757 if (aTRE.IsNull() || aPTE.IsNull()) {
758 Handle(Poly_Polygon3D) aPE = BRep_Tool::Polygon3D(aE, aLoc);
760 if (!GEOMAlgo_FinderShapeOn::BuildTriangulation(aE)) {
761 myErrorStatus=20; // no triangulation found
764 aPE = BRep_Tool::Polygon3D(aE, aLoc);
766 const gp_Trsf& aTrsf=aLoc.Transformation();
767 const TColgp_Array1OfPnt& aNodes=aPE->Nodes();
769 aNbNodes=aPE->NbNodes();
770 Standard_Integer low = aNodes.Lower(), up = aNodes.Upper();
771 for (j=low+1; j<up; ++j) {
772 aP=aNodes(j).Transformed(aTrsf);
777 const gp_Trsf& aTrsf=aLoc.Transformation();
778 const TColgp_Array1OfPnt& aNodes=aTRE->Nodes();
780 aNbNodes=aPTE->NbNodes();
781 const TColStd_Array1OfInteger& aInds=aPTE->Nodes();
782 for (j=2; j<aNbNodes; ++j) {
784 aP=aNodes(aIndex).Transformed(aTrsf);
790 if (!aNb && myNbPntsMin) {
791 // try to fill it yourself
792 InnerPoints(aE, myNbPntsMin, aLP);
796 //=======================================================================
797 //function : InnerPoints
799 //=======================================================================
800 void GEOMAlgo_FinderShapeOn2::InnerPoints(const TopoDS_Edge& aE,
801 const Standard_Integer aNbPntsMin,
802 GEOMAlgo_ListOfPnt& aLP)
804 // try to fill it yourself
805 Standard_Boolean bInf1, bInf2;
806 Standard_Integer j, aNbT;
807 Standard_Real dT, aT, aT1, aT2;
809 Handle(Geom_Curve) aC3D;
811 aC3D=BRep_Tool::Curve(aE, aT1, aT2);
816 bInf1=Precision::IsNegativeInfinite(aT1);
817 bInf2=Precision::IsPositiveInfinite(aT2);
818 if (bInf1 || bInf2) {
824 for (j=1; j<=aNbPntsMin; ++j) {
836 // 12 -unallowed type of subshapes
837 // 13 -unallowed state
838 // 15 -unallowed surface type
839 // 20- no triangulation found
840 // 30- can not obtain the line from the link
841 // 40- point can not be classified
842 // 41- invalid data for classifier