Salome HOME
refs #1813 ... lot 14 combined commit : parts : 1 - 16
[modules/hydro.git] / src / HYDROData / HYDROData_BoundaryPolygonTools.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include <HYDROData_Tool.h>
20 #include <HYDROData_BoundaryPolygonTools.h>
21
22 #include <TopExp_Explorer.hxx>
23 #include <TopoDS.hxx>
24 #include <TopoDS_Face.hxx>
25 #include <TopoDS_Shape.hxx>
26 #include <TopoDS_Wire.hxx>
27
28
29 #include <BRep_Builder.hxx>
30 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
31 #include <TopExp.hxx>
32 #include <NCollection_List.hxx>
33 #include <TopTools_ListIteratorOfListOfShape.hxx>
34
35 #include <TopExp_Explorer.hxx>
36
37 #include <NCollection_IndexedDataMap.hxx>
38 #include <BOPAlgo_BOP.hxx>
39 #include <BOPAlgo_Section.hxx>
40
41
42 #include <Basics_OCCTVersion.hxx>
43
44 static bool FindInterBySection(const TopoDS_Face& IncF, TopoDS_Shape poly)
45 {
46   if (IncF.IsNull())
47     return false;
48   //make cmp from edges of Inc_sh
49   TopTools_IndexedMapOfShape IncToolsE;
50   TopExp::MapShapes(IncF, TopAbs_EDGE, IncToolsE);
51   BRep_Builder BB;
52   TopoDS_Compound aToolCmp;
53   BB.MakeCompound(aToolCmp);
54   for (int i=1; i<= IncToolsE.Extent();i++)
55     BB.Add(aToolCmp, IncToolsE(i));
56   //
57   //perform bsection - poly vs aToolCmp
58   BOPAlgo_Section aBOP;
59   aBOP.AddArgument(poly);
60   aBOP.AddArgument(aToolCmp);
61   aBOP.SetRunParallel(true);
62
63   aBOP.Perform();
64   //aBOP.GetReport());
65
66 //#if OCC_VERSION_LARGE > 0x07020000
67 //  if (aBOP.HasErrors())
68 //    return false;
69 //#endif
70
71   const TopoDS_Shape& aRes=aBOP.Shape();
72   if (aRes.IsNull())
73     return false;
74
75   bool IsInter = false;
76   TopTools_IndexedMapOfShape aShVE;
77   TopExp::MapShapes(aRes, TopAbs_VERTEX, aShVE);
78   if (aShVE.IsEmpty())
79   {
80     TopExp::MapShapes(aRes, TopAbs_EDGE, aShVE);
81     if (!aShVE.IsEmpty())
82       IsInter = true;
83   }
84   else
85     IsInter = true;
86   return IsInter;
87 }
88
89 bool HYDROData_BoundaryPolygonTools::CutTool( const TopTools_SequenceOfShape& CutTools, 
90                                               NCollection_IndexedDataMap<TopoDS_Shape, TopoDS_Shape>& ObjToRes )
91 {
92   TopTools_IndexedMapOfShape EdgesTools;
93   for (int i =1; i<=CutTools.Length();i++)
94   {
95     const TopoDS_Shape& CSH = CutTools(i);
96     TopExp::MapShapes(CSH, TopAbs_EDGE, EdgesTools);
97   }
98   //
99   BRep_Builder BB;
100   TopoDS_Compound aToolCmp;
101   BB.MakeCompound(aToolCmp);
102   for (int i=1; i<= EdgesTools.Extent();i++)
103     BB.Add(aToolCmp, EdgesTools(i));
104   //
105   for (int i = 1; i <= ObjToRes.Extent(); i++ )
106   {
107     const TopoDS_Shape& Obj = ObjToRes.FindKey(i);
108     Standard_Real aTol;
109     if (!Obj.IsNull()) 
110     {
111       aTol = Precision::Confusion();
112       BOPAlgo_BOP aBOP;
113       //
114       aBOP.AddArgument(Obj);
115       aBOP.AddTool(aToolCmp);
116       aBOP.SetOperation(BOPAlgo_CUT);
117       aBOP.SetRunParallel(true);
118
119       aBOP.Perform();
120
121 #if OCC_VERSION_LARGE > 0x07020000
122   if (aBOP.HasErrors())
123     return false;
124 #endif
125
126       const TopoDS_Shape& aRes=aBOP.Shape();
127       ObjToRes.ChangeFromKey(Obj) = aRes;
128     }
129   }
130   return true;
131 }
132
133
134 bool HYDROData_BoundaryPolygonTools::IncludeTool( const TopTools_SequenceOfShape& IncludeTools, 
135   TopoDS_Shape Obj)
136 {
137   TopExp_Explorer exp(Obj, TopAbs_VERTEX);
138   if (!exp.More())
139     return false;
140   TopoDS_Vertex aV = TopoDS::Vertex(exp.Current());
141   gp_Pnt aP = BRep_Tool::Pnt(aV);
142   for (int i=1; i<=IncludeTools.Size(); i++)
143   {
144     TopoDS_Face IncF = TopoDS::Face(IncludeTools(i));
145     bool IsInter = FindInterBySection(IncF, Obj);
146     if (IsInter)
147       return false;
148     //
149     gp_XY aP2d(aP.X(), aP.Y());
150     TopAbs_State aState = HYDROData_Tool::ComputePointState(aP2d, IncF);
151     if (aState != TopAbs_IN)
152       return false;
153   }
154   return true;
155 }
156
157 bool HYDROData_BoundaryPolygonTools::SelectionTool( const TopTools_SequenceOfShape& SelectionTool, 
158                                     TopoDS_Shape Obj)
159 {
160   for (int i=1; i<=SelectionTool.Size(); i++)
161   {
162     TopoDS_Face IncF = TopoDS::Face(SelectionTool(i));
163     bool IsInter = FindInterBySection(IncF, Obj);
164     if (!IsInter)
165       return false;
166   }
167   return true;
168 }
169
170