Salome HOME
Merge branch 'BR_H2018_3' into BR_2018_V8_5
[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 #include <TopTools_MapOfShape.hxx>
41
42
43 #include <Basics_OCCTVersion.hxx>
44
45 static bool FindInterBySection(const TopoDS_Face& IncF, TopoDS_Shape poly)
46 {
47   if (IncF.IsNull())
48     return false;
49   //make cmp from edges of Inc_sh
50   TopTools_IndexedMapOfShape IncToolsE;
51   TopExp::MapShapes(IncF, TopAbs_EDGE, IncToolsE);
52   BRep_Builder BB;
53   TopoDS_Compound aToolCmp;
54   BB.MakeCompound(aToolCmp);
55   for (int i=1; i<= IncToolsE.Extent();i++)
56     BB.Add(aToolCmp, IncToolsE(i));
57   //
58   //perform bsection - poly vs aToolCmp
59   BOPAlgo_Section aBOP;
60   aBOP.AddArgument(poly);
61   aBOP.AddArgument(aToolCmp);
62   aBOP.SetRunParallel(true);
63
64   aBOP.Perform();
65   //aBOP.GetReport());
66
67 //#if OCC_VERSION_LARGE > 0x07020000
68 //  if (aBOP.HasErrors())
69 //    return false;
70 //#endif
71
72   const TopoDS_Shape& aRes=aBOP.Shape();
73   if (aRes.IsNull())
74     return false;
75
76   bool IsInter = false;
77   TopTools_IndexedMapOfShape aShVE;
78   TopExp::MapShapes(aRes, TopAbs_VERTEX, aShVE);
79   if (aShVE.IsEmpty())
80   {
81     TopExp::MapShapes(aRes, TopAbs_EDGE, aShVE);
82     if (!aShVE.IsEmpty())
83       IsInter = true;
84   }
85   else
86     IsInter = true;
87   return IsInter;
88 }
89
90 bool HYDROData_BoundaryPolygonTools::CutTool( const TopTools_SequenceOfShape& CutTools, 
91                                               NCollection_IndexedDataMap<TopoDS_Shape, TopoDS_Shape, TopTools_ShapeMapHasher>& ObjToRes,
92                                               NCollection_IndexedDataMap<TopoDS_Shape, TopTools_MapOfShape, TopTools_ShapeMapHasher>& BPFaceToCutEdges)
93 {
94   BOPAlgo_Builder anAlgo;
95   TopTools_MapOfShape cuttedEdges; //all new edges (Obj) after cut
96
97   for (int i=1; i<= CutTools.Length();i++)
98     anAlgo.AddArgument(CutTools(i));
99
100   for (int i = 1; i <= ObjToRes.Extent(); i++ )
101   {
102     const TopoDS_Shape& Obj = ObjToRes.FindKey(i);
103     if (!Obj.IsNull())
104       anAlgo.AddArgument(Obj);
105   }
106
107   anAlgo.Perform(); 
108 #if OCC_VERSION_LARGE > 0x07020000
109   if (anAlgo.HasErrors())
110     return false;
111 #endif
112   TopoDS_Shape aRes = anAlgo.Shape();  
113   
114   BRep_Builder BB;
115   for (int i = 1; i <= ObjToRes.Extent(); i++ )
116   {
117     const TopoDS_Shape& Obj = ObjToRes.FindKey(i);
118     Standard_Real aTol;
119     if (!Obj.IsNull()) 
120     {
121       TopTools_ListOfShape ls = anAlgo.Modified(Obj);
122       TopoDS_Shape aRes;
123       if (ls.IsEmpty())
124       {
125         aRes = Obj; //no changes
126         cuttedEdges.Add(aRes);
127       }
128       else if (ls.Extent() == 1)
129       {
130         aRes = ls.First();
131         cuttedEdges.Add(aRes);
132       }
133       else
134       {
135         TopoDS_Compound cmp;
136         BB.MakeCompound(cmp);
137         TopTools_ListIteratorOfListOfShape aItLS(ls);
138         for (; aItLS.More(); aItLS.Next()) 
139         {
140           const TopoDS_Shape& val = aItLS.Value();
141           BB.Add(cmp, val);
142           cuttedEdges.Add(val);
143         }
144         aRes = cmp;
145       }  
146       ObjToRes.ChangeFromKey(Obj) = aRes;
147     }
148   }
149
150   //determine the source face 
151   for (int i = 1; i <= CutTools.Length(); i++ )
152   {
153     TopoDS_Shape F = CutTools(i);
154     TopTools_ListOfShape lsF = anAlgo.Modified(F);
155     if (lsF.IsEmpty())
156       lsF.Append(F);
157     TopoDS_Compound cmpF;
158     BB.MakeCompound(cmpF);
159     TopTools_ListIteratorOfListOfShape aItLS(lsF);
160     for (; aItLS.More(); aItLS.Next()) 
161       BB.Add(cmpF, aItLS.Value());
162     //cmpF is a compound if connected faces => modified of boudnary polygon F
163     TopExp_Explorer expE(cmpF, TopAbs_EDGE);
164     TopTools_MapOfShape cmpFEdges;
165     for (;expE.More();expE.Next())
166       cmpFEdges.Add(expE.Current());
167     cmpFEdges.Intersect(cuttedEdges);
168     BPFaceToCutEdges.Add(F, cmpFEdges);
169   }
170
171   return true;
172 }
173
174 /* OLD WAY
175 bool HYDROData_BoundaryPolygonTools::CutTool( const TopTools_SequenceOfShape& CutTools, 
176                                               NCollection_IndexedDataMap<TopoDS_Shape, TopoDS_Shape>& ObjToRes )
177 {
178   TopTools_IndexedMapOfShape EdgesTools;
179   for (int i =1; i<=CutTools.Length();i++)
180   {
181     const TopoDS_Shape& CSH = CutTools(i);
182     TopExp::MapShapes(CSH, TopAbs_EDGE, EdgesTools);
183   }
184   //
185   BRep_Builder BB;
186   TopoDS_Compound aToolCmp;
187   BB.MakeCompound(aToolCmp);
188   for (int i=1; i<= EdgesTools.Extent();i++)
189     BB.Add(aToolCmp, EdgesTools(i));
190
191   TopoDS_Compound aObjCmp;
192   BB.MakeCompound(aObjCmp);
193   for (int i = 1; i <= ObjToRes.Extent(); i++ )
194   {
195     const TopoDS_Shape& Obj = ObjToRes.FindKey(i);
196     if (!Obj.IsNull())
197       BB.Add(aObjCmp, Obj);
198   }
199
200   Standard_Real aTol;
201
202   aTol = Precision::Confusion();
203   BOPAlgo_BOP aBOP;
204   //
205   aBOP.AddArgument(aObjCmp);
206   aBOP.AddTool(aToolCmp);
207   aBOP.SetOperation(BOPAlgo_FUSE);
208   aBOP.SetRunParallel(true);
209
210   aBOP.Perform();
211
212 #if OCC_VERSION_LARGE > 0x07020000
213   if (aBOP.HasErrors())
214     return false;
215 #endif
216
217   for (int i = 1; i <= ObjToRes.Extent(); i++ )
218   {
219     const TopoDS_Shape& Obj = ObjToRes.FindKey(i);
220     Standard_Real aTol;
221     if (!Obj.IsNull()) 
222     {
223       TopTools_ListOfShape ls = aBOP.Modified(Obj);
224       TopoDS_Shape aRes;
225       if (ls.IsEmpty())
226         aRes = Obj; //no fusing of toolcmp with Obj
227       else if (ls.Extent() == 1)
228         aRes = ls.First();
229       else
230       {
231         BRep_Builder BB;
232         TopoDS_Compound cmp;
233         BB.MakeCompound(cmp);
234         TopTools_ListIteratorOfListOfShape aItLS(ls);
235         for (; aItLS.More(); aItLS.Next()) 
236           BB.Add(cmp, aItLS.Value());
237         aRes = cmp;
238       }  
239       //const TopoDS_Shape& aRes=aBOP.Shape();
240       ObjToRes.ChangeFromKey(Obj) = aRes;
241     }
242   }
243
244   return true;
245 }*/
246
247
248 bool HYDROData_BoundaryPolygonTools::IncludeTool( const TopTools_SequenceOfShape& IncludeTools, 
249   TopoDS_Shape Obj)
250 {
251   TopExp_Explorer exp(Obj, TopAbs_VERTEX);
252   if (!exp.More())
253     return false;
254   TopoDS_Vertex aV = TopoDS::Vertex(exp.Current());
255   gp_Pnt aP = BRep_Tool::Pnt(aV);
256   for (int i=1; i<=IncludeTools.Size(); i++)
257   {
258     TopoDS_Face IncF = TopoDS::Face(IncludeTools(i));
259     bool IsInter = FindInterBySection(IncF, Obj);
260     if (IsInter)
261       return false;
262     //
263     gp_XY aP2d(aP.X(), aP.Y());
264     TopAbs_State aState = HYDROData_Tool::ComputePointState(aP2d, IncF);
265     if (aState != TopAbs_IN)
266       return false;
267   }
268   return true;
269 }
270
271 bool HYDROData_BoundaryPolygonTools::SelectionTool( const TopTools_SequenceOfShape& SelectionTool, 
272                                     TopoDS_Shape Obj)
273 {
274   for (int i=1; i<=SelectionTool.Size(); i++)
275   {
276     TopoDS_Face IncF = TopoDS::Face(SelectionTool(i));
277     bool IsInter = FindInterBySection(IncF, Obj);
278     if (!IsInter)
279       return false;
280   }
281   return true;
282 }
283
284