1 // Copyright (C) 2007-2016 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, or (at your option) any later version.
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_FinderShapeOn2.cxx
24 // Created: Fri Mar 4 10:31:06 2005
25 // Author: Peter KURNEV
28 #include <GEOMAlgo_FinderShapeOn2.hxx>
29 #include <GEOMAlgo_DataMapIteratorOfDataMapOfPassKeyInteger.hxx>
30 #include <GEOMAlgo_DataMapOfPassKeyInteger.hxx>
31 #include <GEOMAlgo_ListIteratorOfListOfPnt.hxx>
32 #include <GEOMAlgo_PassKey.hxx>
33 #include <GEOMAlgo_StateCollector.hxx>
34 #include <GEOMAlgo_SurfaceTools.hxx>
36 #include <Bnd_Box.hxx>
37 #include <BRep_Tool.hxx>
38 #include <BRepBndLib.hxx>
39 #include <BRepMesh_IncrementalMesh.hxx>
40 #include <BRepTools.hxx>
41 #include <Geom_Curve.hxx>
42 #include <Geom_Surface.hxx>
43 #include <Geom2d_Line.hxx>
44 #include <Geom2dAdaptor_Curve.hxx>
45 #include <Geom2dHatch_Hatcher.hxx>
46 #include <gp_Dir2d.hxx>
47 #include <gp_Pnt2d.hxx>
49 #include <gp_Trsf.hxx>
50 #include <HatchGen_Domain.hxx>
51 #include <IntTools_Tools.hxx>
52 #include <Poly_Array1OfTriangle.hxx>
53 #include <Poly_Polygon3D.hxx>
54 #include <Poly_PolygonOnTriangulation.hxx>
55 #include <Poly_Triangle.hxx>
56 #include <Poly_Triangulation.hxx>
57 #include <Precision.hxx>
58 #include <TColgp_Array1OfPnt.hxx>
59 #include <TColStd_Array1OfInteger.hxx>
60 #include <TColStd_MapOfInteger.hxx>
61 #include <TopAbs_State.hxx>
63 #include <TopExp_Explorer.hxx>
64 #include <TopLoc_Location.hxx>
66 #include <TopoDS_Edge.hxx>
67 #include <TopoDS_Face.hxx>
68 #include <TopoDS_Shape.hxx>
69 #include <TopoDS_Vertex.hxx>
70 #include <TopTools_IndexedMapOfShape.hxx>
71 #include <IntTools_Context.hxx>
73 //=======================================================================
76 //=======================================================================
77 GEOMAlgo_FinderShapeOn2::GEOMAlgo_FinderShapeOn2()
82 myShapeType=TopAbs_VERTEX;
83 myState=GEOMAlgo_ST_UNKNOWN;
87 //=======================================================================
90 //=======================================================================
91 GEOMAlgo_FinderShapeOn2::~GEOMAlgo_FinderShapeOn2()
94 //=======================================================================
97 //=======================================================================
98 void GEOMAlgo_FinderShapeOn2::SetClsf(const Handle(GEOMAlgo_Clsf)& aClsf)
102 //=======================================================================
105 //=======================================================================
106 const Handle(GEOMAlgo_Clsf)& GEOMAlgo_FinderShapeOn2::Clsf() const
110 //=======================================================================
111 //function : SetShapeType
113 //=======================================================================
114 void GEOMAlgo_FinderShapeOn2::SetShapeType(const TopAbs_ShapeEnum aType)
118 //=======================================================================
119 //function : ShapeType
121 //=======================================================================
122 TopAbs_ShapeEnum GEOMAlgo_FinderShapeOn2::ShapeType()const
126 //=======================================================================
127 //function : SetState
129 //=======================================================================
130 void GEOMAlgo_FinderShapeOn2::SetState(const GEOMAlgo_State aState)
134 //=======================================================================
137 //=======================================================================
138 GEOMAlgo_State GEOMAlgo_FinderShapeOn2::State() const
142 //=======================================================================
143 //function : SetNbPntsMin
145 //=======================================================================
146 void GEOMAlgo_FinderShapeOn2::SetNbPntsMin(const Standard_Integer aNb)
150 //=======================================================================
151 //function : NbPntsMin
153 //=======================================================================
154 Standard_Integer GEOMAlgo_FinderShapeOn2::NbPntsMin()const
158 //=======================================================================
159 //function : SetNbPntsMax
161 //=======================================================================
162 void GEOMAlgo_FinderShapeOn2::SetNbPntsMax(const Standard_Integer aNb)
166 //=======================================================================
167 //function : NbPntsMax
169 //=======================================================================
170 Standard_Integer GEOMAlgo_FinderShapeOn2::NbPntsMax()const
174 //=======================================================================
177 //=======================================================================
178 const GEOMAlgo_IndexedDataMapOfShapeState& GEOMAlgo_FinderShapeOn2::MSS() const
182 //=======================================================================
185 //=======================================================================
186 const TopTools_ListOfShape& GEOMAlgo_FinderShapeOn2::Shapes() const
188 Standard_Integer i, aNb;
189 TopTools_ListOfShape* pL;
191 pL=(TopTools_ListOfShape*) &myLS;
195 for (i=1; i<=aNb; ++i) {
196 const TopoDS_Shape& aS=myMSS.FindKey(i);
197 if (aS.ShapeType()==myShapeType) {
203 //=======================================================================
206 //=======================================================================
207 void GEOMAlgo_FinderShapeOn2::Perform()
219 // Initialize the context
220 GEOMAlgo_ShapeAlgo::Perform();
222 myClsf->SetTolerance(myTolerance);
229 if (myShapeType==TopAbs_VERTEX) {
238 if (myShapeType==TopAbs_EDGE) {
247 if (myShapeType==TopAbs_FACE) {
255 //=======================================================================
256 //function : CheckData
258 //=======================================================================
259 void GEOMAlgo_FinderShapeOn2::CheckData()
261 Standard_Integer iErr;
265 if(myClsf.IsNull()) {
266 myErrorStatus=10; // myClsf=NULL
271 iErr=myClsf->ErrorStatus();
273 myErrorStatus=41; // invalid data for classifier
277 if (myShape.IsNull()) {
278 myErrorStatus=11; // myShape=NULL
282 if (!(myShapeType==TopAbs_VERTEX ||
283 myShapeType==TopAbs_EDGE ||
284 myShapeType==TopAbs_FACE ||
285 myShapeType==TopAbs_SOLID)) {
286 myErrorStatus=12; // unallowed sub-shape type
290 if (myState==GEOMAlgo_ST_UNKNOWN ||
291 myState==GEOMAlgo_ST_INOUT) {
292 myErrorStatus=13; // unallowed state type
296 //=======================================================================
297 //function : ProcessVertices
299 //=======================================================================
300 void GEOMAlgo_FinderShapeOn2::ProcessVertices()
304 Standard_Boolean bIsConformState;
305 Standard_Integer i, aNb, iErr;
307 TopTools_IndexedMapOfShape aM;
310 TopExp::MapShapes(myShape, TopAbs_VERTEX, aM);
312 for (i=1; i<=aNb; ++i) {
313 const TopoDS_Vertex& aV=TopoDS::Vertex(aM(i));
314 aP=BRep_Tool::Pnt(aV);
318 iErr=myClsf->ErrorStatus();
320 myErrorStatus=40; // point can not be classified
325 bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
327 if (myShapeType==TopAbs_VERTEX){
328 if (bIsConformState) {
332 else if (bIsConformState || aSt==TopAbs_ON) {
337 //=======================================================================
338 //function : ProcessEdges
340 //=======================================================================
341 void GEOMAlgo_FinderShapeOn2::ProcessEdges()
345 Standard_Boolean bIsConformState, bIsToBreak;
346 Standard_Integer i, aNb, iCnt, iErr;
348 TopTools_IndexedMapOfShape aM;
349 TopExp_Explorer aExp;
350 GEOMAlgo_ListIteratorOfListOfPnt aIt;
352 TopExp::MapShapes(myShape, TopAbs_EDGE, aM);
354 for (i=1; i<=aNb; ++i) {
355 GEOMAlgo_ListOfPnt aLP;
356 GEOMAlgo_StateCollector aSC;
358 const TopoDS_Edge& aE=TopoDS::Edge(aM(i));
360 bIsConformState=Standard_False;
362 aExp.Init(aE, TopAbs_VERTEX);
363 for (; aExp.More(); aExp.Next()) {
364 const TopoDS_Shape& aV=aExp.Current();
366 bIsConformState=myMSS.Contains(aV);
367 if (!bIsConformState) {
368 break;// vertex has non-conformed state
371 aSt=myMSS.FindFromKey(aV);
372 aSC.AppendState(aSt);
376 if (!bIsConformState) {
377 continue; // vertex has non-conformed state,skip edge
380 if (BRep_Tool::Degenerated(aE)) {
385 if (myState==GEOMAlgo_ST_ON) {
386 Standard_Boolean bCanBeON;
387 Standard_Real aT1, aT2;
388 Handle(Geom_Curve) aC;
390 aC=BRep_Tool::Curve(aE, aT1, aT2);
391 bCanBeON=myClsf->CanBeON(aC);
397 InnerPoints(aE, aLP);
402 bIsConformState=Standard_True;
404 for (iCnt=0; aIt.More(); aIt.Next(), ++iCnt) {
406 if (iCnt > myNbPntsMax) {
411 const gp_Pnt& aP=aIt.Value();
415 iErr=myClsf->ErrorStatus();
417 myErrorStatus=40; // point can not be classified
423 bIsToBreak=aSC.AppendState(aSt);
431 bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
432 if (myShapeType==TopAbs_EDGE) {
433 if (bIsConformState) {
437 else if (bIsConformState || aSt==TopAbs_ON) {
440 } // for (i=1; i<=aNb; ++i) next edge
442 //=======================================================================
443 //function : ProcessFaces
445 //=======================================================================
446 void GEOMAlgo_FinderShapeOn2::ProcessFaces()
450 Standard_Boolean bIsConformState, bIsToBreak, bCanBeON;
451 Standard_Integer i, aNbF, iCnt, iErr;
453 TopTools_IndexedMapOfShape aM;
454 TopExp_Explorer aExp;
455 GEOMAlgo_ListIteratorOfListOfPnt aIt;
457 TopExp::MapShapes(myShape, TopAbs_FACE, aM);
459 for (i=1; i<=aNbF; ++i) {
460 GEOMAlgo_StateCollector aSC;
461 GEOMAlgo_ListOfPnt aLP;
463 const TopoDS_Face& aF=TopoDS::Face(aM(i));
465 if (myState==GEOMAlgo_ST_ON) {
466 Handle(Geom_Surface) aS;
468 aS=BRep_Tool::Surface(aF);
469 bCanBeON=myClsf->CanBeON(aS);
476 bIsConformState=Standard_False;
478 aExp.Init(aF, TopAbs_EDGE);
479 for (; aExp.More(); aExp.Next()) {
480 const TopoDS_Shape& aE=aExp.Current();
481 bIsConformState=myMSS.Contains(aE);
482 if (!bIsConformState) {
483 break;// edge has non-conformed state
486 aSt=myMSS.FindFromKey(aE);
487 aSC.AppendState(aSt);
491 if (!bIsConformState) {
492 continue; // edge has non-conformed state,skip face
495 InnerPoints(aF, aLP);
500 bIsConformState=Standard_True;
502 for (iCnt=0; aIt.More(); aIt.Next(), ++iCnt) {
504 if (iCnt > myNbPntsMax) {
509 const gp_Pnt& aP=aIt.Value();
513 iErr=myClsf->ErrorStatus();
515 myErrorStatus=40; // point can not be classified
521 bIsToBreak=aSC.AppendState(aSt);
529 bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
530 if (myShapeType==TopAbs_FACE) {
531 if (bIsConformState) {
535 else if (bIsConformState || aSt==TopAbs_ON) {
538 }// for (i=1; i<=aNb; ++i) next face
540 //=======================================================================
541 //function : ProcessSolids
543 //=======================================================================
544 void GEOMAlgo_FinderShapeOn2::ProcessSolids()
548 Standard_Boolean bIsConformState;
549 Standard_Integer i, aNbS, j, aNbF;
550 TopTools_IndexedMapOfShape aM, aMF;
553 TopExp::MapShapes(myShape, TopAbs_SOLID, aM);
555 for (i=1; i<=aNbS; ++i) {
556 GEOMAlgo_StateCollector aSC;
558 const TopoDS_Shape& aSd=aM(i);
560 TopExp::MapShapes(aSd, TopAbs_FACE, aMF);
562 bIsConformState=Standard_False;
565 for (j=1; j<=aNbF; ++j) {
566 const TopoDS_Shape& aF=aMF(j);
567 bIsConformState=myMSS.Contains(aF);
568 if (!bIsConformState) {
569 break;// face has non-conformed state
572 aSt=myMSS.FindFromKey(aF);
573 aSC.AppendState(aSt);
577 if (!bIsConformState) {
578 continue; // face has non-conformed state,skip solid
583 bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
584 if (bIsConformState) {
590 //=======================================================================
591 //function : InnerPoints
593 //=======================================================================
594 void GEOMAlgo_FinderShapeOn2::InnerPoints(const TopoDS_Face& aF,
595 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;
609 aTRF=BRep_Tool::Triangulation(aF, aLoc);
611 if (!BuildTriangulation(aF)) {
612 myWarningStatus=20; // no triangulation found
615 aTRF=BRep_Tool::Triangulation(aF, aLoc);
618 const gp_Trsf& aTrsf=aLoc.Transformation();
619 const Poly_Array1OfTriangle& aTrs=aTRF->Triangles();
620 const TColgp_Array1OfPnt& aNodes=aTRF->Nodes();
622 // map link/nbtriangles
625 for (j=j1; j<=j2; ++j) {
626 const Poly_Triangle& aTr=aTrs(j);
627 aTr.Get(n[0], n[1], n[2]);
629 for (k=0; k<3; ++k) {
630 GEOMAlgo_PassKey aPK;
632 aPK.SetIds(n[k], n[k+1]);
633 if (aMPKI.IsBound(aPK)) {
634 Standard_Integer& iCntX=aMPKI.ChangeFind(aPK);
643 // boundary nodes aMBN
644 aNbLinks=aMPKI.Extent();
645 aIt.Initialize(aMPKI);
646 for (; aIt.More(); aIt.Next()) {
649 const GEOMAlgo_PassKey& aPK=aIt.Key();
650 aNx=(Standard_Integer)aPK.Id(1);
652 aNx=(Standard_Integer)aPK.Id(2);
658 // inner nodes=all_nodes - boundary_nodes
661 for (j=j1; j<=j2; ++j) {
662 if (!aMBN.Contains(j)) {
663 aP=aNodes(j).Transformed(aTrsf);
670 //modified by NIZNHY-PKV Mon Sep 24 08:42:32 2012f
671 if (!aNb && myNbPntsMin) { // A
672 Standard_Boolean bIsDone, bHasFirstPoint, bHasSecondPoint;
673 Standard_Integer i, aNb, aIx, /*iErr, */aNbDomains;
674 Standard_Real aUMin, aUMax, aVMin, aVMax, dU, aUx, aVx, aV1, aV2;
676 gp_Dir2d aD2D (0., 1.);
678 Handle(Geom2d_Line) aL2D;
679 Handle(Geom_Surface) aS;
683 aFF.Orientation (TopAbs_FORWARD);
685 Geom2dHatch_Hatcher& aHatcher=myContext->Hatcher(aFF);
687 aS=BRep_Tool::Surface(aFF);
688 BRepTools::UVBounds(aFF, aUMin, aUMax, aVMin, aVMax);
691 dU=(aUMax-aUMin)/aNb;
692 for (i=1; i<aNb; ++i) {
694 aP2D.SetCoord(aUx, 0.);
695 aL2D=new Geom2d_Line (aP2D, aD2D);
696 Geom2dAdaptor_Curve aHCur(aL2D);
698 aHatcher.ClrHatchings();
699 aIx=aHatcher.AddHatching(aHCur);
702 bIsDone=aHatcher.TrimDone(aIx);
708 aHatcher.ComputeDomains(aIx);
709 bIsDone=aHatcher.IsDone(aIx);
714 aNbDomains=aHatcher.NbDomains(aIx);
715 for (j=1; j<=aNbDomains; ++j) {
716 const HatchGen_Domain& aDomain=aHatcher.Domain (aIx, j) ;
718 bHasFirstPoint=aDomain.HasFirstPoint();
719 bHasSecondPoint=aDomain.HasSecondPoint();
720 if (!bHasFirstPoint || !bHasSecondPoint) {
724 aV1=aDomain.FirstPoint().Parameter();
725 aV2=aDomain.SecondPoint().Parameter();
726 aVx=IntTools_Tools::IntermediatePoint(aV1, aV2);
728 aS->D0(aUx, aVx, aPx);
732 }// for (i=1; i<aNb; ++i) {
733 }// if (!aNb && myNbPntsMin) {
735 //=======================================================================
736 //function : InnerPoints
738 //=======================================================================
739 void GEOMAlgo_FinderShapeOn2::InnerPoints(const TopoDS_Edge& aE,
740 GEOMAlgo_ListOfPnt& aLP)
744 Standard_Integer j, aNbNodes, aIndex, aNb;
745 Handle(Poly_PolygonOnTriangulation) aPTE;
746 Handle(Poly_Triangulation) aTRE;
747 TopLoc_Location aLoc;
751 BRep_Tool::PolygonOnTriangulation(aE, aPTE, aTRE, aLoc);
752 if (aTRE.IsNull() || aPTE.IsNull()) {
753 Handle(Poly_Polygon3D) aPE = BRep_Tool::Polygon3D(aE, aLoc);
755 if (!BuildTriangulation(aE)) {
756 myErrorStatus=20; // no triangulation found
759 aPE = BRep_Tool::Polygon3D(aE, aLoc);
761 const gp_Trsf& aTrsf=aLoc.Transformation();
762 const TColgp_Array1OfPnt& aNodes=aPE->Nodes();
764 aNbNodes=aPE->NbNodes();
765 Standard_Integer low = aNodes.Lower(), up = aNodes.Upper();
766 for (j=low+1; j<up; ++j) {
767 aP=aNodes(j).Transformed(aTrsf);
772 const gp_Trsf& aTrsf=aLoc.Transformation();
773 const TColgp_Array1OfPnt& aNodes=aTRE->Nodes();
775 aNbNodes=aPTE->NbNodes();
776 const TColStd_Array1OfInteger& aInds=aPTE->Nodes();
777 for (j=2; j<aNbNodes; ++j) {
779 aP=aNodes(aIndex).Transformed(aTrsf);
785 if (!aNb && myNbPntsMin) {
786 // try to fill it yourself
787 InnerPoints(aE, myNbPntsMin, aLP);
791 //=======================================================================
792 //function : InnerPoints
794 //=======================================================================
795 void GEOMAlgo_FinderShapeOn2::InnerPoints(const TopoDS_Edge& aE,
796 const Standard_Integer aNbPntsMin,
797 GEOMAlgo_ListOfPnt& aLP)
799 // try to fill it yourself
800 Standard_Boolean bInf1, bInf2;
801 Standard_Integer j, aNbT;
802 Standard_Real dT, aT, aT1, aT2;
804 Handle(Geom_Curve) aC3D;
806 aC3D=BRep_Tool::Curve(aE, aT1, aT2);
811 bInf1=Precision::IsNegativeInfinite(aT1);
812 bInf2=Precision::IsPositiveInfinite(aT2);
813 if (bInf1 || bInf2) {
819 for (j=1; j<=aNbPntsMin; ++j) {
826 //=======================================================================
827 //function : BuildTriangulation
829 //=======================================================================
831 GEOMAlgo_FinderShapeOn2::BuildTriangulation (const TopoDS_Shape& theShape)
833 // calculate deflection
834 Standard_Real aDeviationCoefficient = 0.001;
837 BRepBndLib::Add(theShape, B);
838 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
839 B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
841 Standard_Real dx = aXmax - aXmin, dy = aYmax - aYmin, dz = aZmax - aZmin;
842 Standard_Real aDeflection = Max(Max(dx, dy), dz) * aDeviationCoefficient * 4;
843 Standard_Real aHLRAngle = 0.349066;
845 // build triangulation
846 BRepMesh_IncrementalMesh Inc (theShape, aDeflection, Standard_False, aHLRAngle);
848 // check triangulation
849 bool isTriangulation = true;
851 TopExp_Explorer exp (theShape, TopAbs_FACE);
854 TopLoc_Location aTopLoc;
855 Handle(Poly_Triangulation) aTRF;
856 aTRF = BRep_Tool::Triangulation(TopoDS::Face(exp.Current()), aTopLoc);
858 isTriangulation = false;
861 else // no faces, try edges
863 TopExp_Explorer expe (theShape, TopAbs_EDGE);
865 isTriangulation = false;
868 TopLoc_Location aLoc;
869 Handle(Poly_Polygon3D) aPE = BRep_Tool::Polygon3D(TopoDS::Edge(expe.Current()), aLoc);
871 isTriangulation = false;
876 return isTriangulation;
884 // 12 -unallowed type of sub-shapes
885 // 13 -unallowed state
886 // 15 -unallowed surface type
887 // 20- no triangulation found
888 // 30- can not obtain the line from the link
889 // 40- point can not be classified
890 // 41- invalid data for classifier
891 // 42- can not compute hatching