Salome HOME
Lot 2: change bathy associated to natural object propagated to all cases without...
[modules/hydro.git] / src / HYDROData / HYDROData_Transform.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_Transform.h>
20
21 #include <math.h>
22
23 #include <Bnd_Box.hxx>
24 #include <gp_Vec.hxx>
25
26 #include <TopoDS_Shape.hxx>
27 #include <TopoDS_Iterator.hxx>
28
29 #include <BRepBndLib.hxx>
30
31 #include <TopTools_ListIteratorOfListOfShape.hxx>
32
33 #include <TopTools_ListOfShape.hxx>
34 #include <TopTools_MapOfShape.hxx>
35
36 #include <TopTools_ListOfShape.hxx>
37
38 #include <BOPAlgo_BuilderShape.hxx>
39 #include <BOPAlgo_PaveFiller.hxx>
40
41 //#define DEB_SPLIT_TO_ZONES_CHECK_PARTITION 1
42 #ifdef DEB_SPLIT_TO_ZONES_CHECK_PARTITION
43 #include <BRepTools.hxx>
44 #include <TCollection_AsciiString.hxx>
45 static TCollection_AsciiString fileNameAfter("AfterTranslation");
46 #endif
47
48 void HYDROData_Transform::SetFileName(const TCollection_AsciiString& theNameAfter)
49 {
50 #ifdef DEB_SPLIT_TO_ZONES_CHECK_PARTITION
51   fileNameAfter = theNameAfter;
52 #endif
53 }
54 //=======================================================================
55 //function : HYDROData_Transform
56 //purpose  : 
57 //=======================================================================
58 HYDROData_Transform::HYDROData_Transform()
59 :
60   BOPAlgo_BuilderShape()
61 {
62   myToTransform=Standard_False;
63   myTreshold=10000.;
64   myTransform1=NULL;
65   myTransform2=NULL;
66   myBuilder=NULL;
67   myErrorStatus=0;
68   myWarningStatus=0;
69 }
70 //=======================================================================
71 //function : ~
72 //purpose  : 
73 //=======================================================================
74 HYDROData_Transform::~HYDROData_Transform()
75 {
76   Clear();
77 }
78 //=======================================================================
79 //function : Clear
80 //purpose  : 
81 //=======================================================================
82 void HYDROData_Transform::Clear()
83 {
84   myTreshold=10000.;
85   //
86   if (myTransform1) {
87     delete myTransform1;
88     myTransform1=NULL;
89   }
90   if (myTransform2) {
91     delete myTransform2;
92     myTransform2=NULL;
93   }
94   if (myBuilder) {
95     delete myBuilder;
96     myBuilder=NULL;
97   }
98   //
99   BOPAlgo_BuilderShape::PrepareHistory();
100 }
101 //=======================================================================
102 //function : SetArgument
103 //purpose  : 
104 //=======================================================================
105 void HYDROData_Transform::SetArgument(const TopoDS_Shape& theShape)
106 {
107   myArgument=theShape;
108 }
109 //=======================================================================
110 //function : AddArgument
111 //purpose  : 
112 //=======================================================================
113 const TopoDS_Shape& HYDROData_Transform::Argument()const
114 {
115   return myArgument;
116 }
117 //=======================================================================
118 //function : SetTreshold
119 //purpose  : 
120 //=======================================================================
121 void HYDROData_Transform::SetTreshold(const Standard_Real theTreshold)
122 {
123   myTreshold=theTreshold;
124 }
125 //=======================================================================
126 //function : Treshold
127 //purpose  : 
128 //=======================================================================
129 Standard_Real HYDROData_Transform::Treshold()const 
130 {
131   return myTreshold;
132 }
133 //=======================================================================
134 //function : SetToTransform
135 //purpose  : 
136 //=======================================================================
137 void HYDROData_Transform::SetToTransform(const Standard_Boolean theFlag)
138 {
139   myToTransform=theFlag;
140 }
141 //=======================================================================
142 //function : ToTransform
143 //purpose  : 
144 //=======================================================================
145 Standard_Boolean HYDROData_Transform::ToTransform()const
146 {
147   return myToTransform;
148 }
149 //=======================================================================
150 //function : CheckData
151 //purpose  : 
152 //=======================================================================
153 void HYDROData_Transform::CheckData()
154 {
155   if(myArgument.IsNull()) {
156     myErrorStatus=10;
157   }
158 }
159 //=======================================================================
160 //function : Prepare
161 //purpose  : 
162 //=======================================================================
163 void HYDROData_Transform::Prepare()
164 {
165   TopoDS_Shape aSx;
166   //
167   myErrorStatus=0;
168   myWarningStatus=0;
169   myShape=aSx;
170 }
171 //=======================================================================
172 //function : Perform
173 //purpose  : 
174 //=======================================================================
175 void HYDROData_Transform::Perform()
176 {
177   Prepare();
178   //
179   CheckData();
180   if(myErrorStatus) {
181     return;
182   }
183   //
184   Clear();
185   if(myErrorStatus) {
186     return;
187   }
188   //
189   ComputeTrsf();
190   if(myErrorStatus) {
191     return;
192   }
193   //
194   Transform1();
195   if(myErrorStatus) {
196     return;
197   }
198   //
199   BuildFuse();
200   if(myErrorStatus) {
201     return;
202   }
203   //
204   Transform2();
205 }
206 //=======================================================================
207 //function : Detect
208 //purpose  : 
209 //=======================================================================
210 void HYDROData_Transform::Detect()
211 {
212   Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
213   Standard_Real aXc, aYc, aZc;
214   Bnd_Box aBox; 
215   gp_Vec aVec;
216   //
217   myErrorStatus=0;
218   //
219   BRepBndLib::Add(myArgument, aBox);
220   //
221   aBox.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
222   //
223   aXc=0.5*(aXmin+aXmax);
224   aYc=0.5*(aYmin+aYmax);
225   aZc=0.5*(aZmin+aZmax);
226   //printf(" aVc : { %lf %lf %lf }\n", aXc, aYc, aZc);
227   //
228   myToTransform=Standard_False;
229   if (fabs(aXc)>myTreshold || 
230       fabs(aYc)>myTreshold || 
231       fabs(aZc)>myTreshold) {
232     myToTransform=!myToTransform;
233   }  
234   //
235   aVec.SetCoord(-aXc, -aYc, -aZc);
236   myTrsf1.SetTranslation(aVec);
237   //
238   aVec.SetCoord(aXc, aYc, aZc);
239   myTrsf2.SetTranslation(aVec);
240 }
241 //=======================================================================
242 //function : ComputeTrsf
243 //purpose  : 
244 //=======================================================================
245 void HYDROData_Transform::ComputeTrsf()
246 {
247   if (!myToTransform) {
248     gp_Trsf aTrsf;
249     //
250     myTrsf1=aTrsf;
251     myTrsf2=aTrsf;
252   }
253 }
254 //=======================================================================
255 //function : Transform1
256 //purpose  : 
257 //=======================================================================
258 void HYDROData_Transform::Transform1()
259
260   myErrorStatus=0;
261   //
262   if (myToTransform) {//pkv ft
263     Standard_Boolean bIsDone;
264     //
265     myErrorStatus=0;
266     //
267     myTransform1=new BRepBuilderAPI_Transform(myTrsf1);
268     //
269     myTransform1->Perform(myArgument);
270     bIsDone=myTransform1->IsDone();
271     if (!bIsDone) {
272       myErrorStatus=11;
273     }
274   }
275 }
276 //=======================================================================
277 //function : BuildFuse
278 //purpose  : 
279 //=======================================================================
280 void HYDROData_Transform::BuildFuse()
281 {
282   Standard_Integer iErr;
283   TopoDS_Iterator aIt;
284   TopTools_ListOfShape aLC;
285   BOPAlgo_PaveFiller aPF;
286   //
287   myErrorStatus=0;
288   //
289    //modified by NIZNHY-PKV Wed Jan 15 13:24:48 2014f
290   const TopoDS_Shape& aS1=
291     (myToTransform)? myTransform1->Shape() : myArgument;
292   //const TopoDS_Shape& aS1=()Transform1->Shape();
293   //modified by NIZNHY-PKV Wed Jan 15 13:24:52 2014t
294
295 #ifdef DEB_SPLIT_TO_ZONES_CHECK_PARTITION
296   TCollection_AsciiString aNameBefore = fileNameAfter + ".brep";
297   BRepTools::Write(aS1, aNameBefore.ToCString());
298 #endif
299   //
300   aIt.Initialize(aS1);
301   for (; aIt.More(); aIt.Next()) {
302     const TopoDS_Shape& aS1x=aIt.Value();
303     aLC.Append(aS1x);
304   }
305   //
306   aPF.SetArguments(aLC);
307   //
308   aPF.Perform();
309   if (aPF.HasErrors()) 
310   {
311     myErrorStatus=20;
312     return;
313   }
314   //
315   myBuilder=new BOPAlgo_Builder;
316   //
317   aIt.Initialize(aS1);
318   for (; aIt.More(); aIt.Next()) {
319     const TopoDS_Shape& aS1x=aIt.Value();
320     myBuilder->AddArgument(aS1x);
321   }
322   //
323   myBuilder->PerformWithFiller(aPF);
324   if (myBuilder->HasErrors())
325   {
326     myErrorStatus=21;
327     return;
328   }
329 }
330 //=======================================================================
331 //function : Transform2
332 //purpose  : 
333 //=======================================================================
334 void HYDROData_Transform::Transform2()
335 {
336   myErrorStatus=0;
337   //
338   const TopoDS_Shape& aR1=myBuilder->Shape();
339   //
340   myShape=aR1; //pkv ft
341   if (myToTransform) { //pkv ft
342     Standard_Boolean bIsDone;
343     //
344     myTransform2=new BRepBuilderAPI_Transform(myTrsf2);
345     //
346     myTransform2->Perform(aR1);
347     bIsDone=myTransform2->IsDone();
348     if (!bIsDone) {
349       myErrorStatus=12;
350       return;
351     }
352     //
353     const TopoDS_Shape& aR2=myTransform2->Shape();
354     //
355     myShape=aR2;
356   }
357 }
358 //
359 //=======================================================================
360 //function : Modified
361 //purpose  : 
362 //=======================================================================
363 const TopTools_ListOfShape& HYDROData_Transform::Modified
364   (const TopoDS_Shape& aS)
365 {
366   TopTools_ListOfShape* pHistShapes;
367   TopTools_ListIteratorOfListOfShape aItLS;
368   //
369   pHistShapes=(TopTools_ListOfShape*)&myHistShapes;
370   pHistShapes->Clear();
371   //
372   if (myToTransform) { //pkv ft
373     const TopTools_ListOfShape& aLS1=myTransform1->Modified(aS);
374     const TopoDS_Shape& aS1=aLS1.First();
375     //
376     const TopTools_ListOfShape& aLS1B=myBuilder->Modified(aS1);
377     //
378     if (aLS1B.IsEmpty()) { //pkv ft
379       const TopTools_ListOfShape& aLS2B=myTransform2->Modified(aS1);
380       const TopoDS_Shape& aS2B=aLS2B.First();
381       pHistShapes->Append(aS2B);
382     }
383     else {
384       aItLS.Initialize(aLS1B);
385       for (; aItLS.More(); aItLS.Next()) {
386         const TopoDS_Shape& aS1B=aItLS.Value();
387         const TopTools_ListOfShape& aLS2B=myTransform2->Modified(aS1B);
388         const TopoDS_Shape& aS2B=aLS2B.First();
389         pHistShapes->Append(aS2B);
390       }
391     }
392   }
393   else {
394     const TopTools_ListOfShape& aLS1B=myBuilder->Modified(aS);
395     aItLS.Initialize(aLS1B);
396     for (; aItLS.More(); aItLS.Next()) {
397       const TopoDS_Shape& aS1B=aItLS.Value();
398       pHistShapes->Append(aS1B);
399     }
400   }
401   //
402   return myHistShapes;
403 }
404 //=======================================================================
405 //function : Generated
406 //purpose  : 
407 //=======================================================================
408 const TopTools_ListOfShape& HYDROData_Transform::Generated
409   (const TopoDS_Shape& aS)
410 {
411   TopTools_ListOfShape* pHistShapes;
412   TopTools_ListIteratorOfListOfShape aItLS;
413   //
414   pHistShapes=(TopTools_ListOfShape*)&myHistShapes;
415   pHistShapes->Clear();
416   //
417   if (myToTransform) { //pkv ft
418     const TopTools_ListOfShape& aLS1=myTransform1->Modified(aS);
419     const TopoDS_Shape& aS1=aLS1.First();
420     //
421     const TopTools_ListOfShape& aLS1B=myBuilder->Generated(aS1);
422     if (aLS1B.IsEmpty()) { //pkv ft
423       const TopTools_ListOfShape& aLS2B=myTransform2->Modified(aS1);
424       const TopoDS_Shape& aS2B=aLS2B.First();
425       pHistShapes->Append(aS2B);
426     }
427     else {
428       aItLS.Initialize(aLS1B);
429       for (; aItLS.More(); aItLS.Next()) {
430         const TopoDS_Shape& aS1B=aItLS.Value();
431         const TopTools_ListOfShape& aLS2B=myTransform2->Modified(aS1B);
432         const TopoDS_Shape& aS2B=aLS2B.First();
433         pHistShapes->Append(aS2B);
434       }
435     }
436   }
437   else {
438     const TopTools_ListOfShape& aLS1B=myBuilder->Generated(aS);
439     aItLS.Initialize(aLS1B);
440     for (; aItLS.More(); aItLS.Next()) {
441       const TopoDS_Shape& aS1B=aItLS.Value();
442       pHistShapes->Append(aS1B);
443     }
444   }
445   //
446   return myHistShapes;
447 }
448 //=======================================================================
449 //function : IsDeleted
450 //purpose  : 
451 //=======================================================================
452 Standard_Boolean HYDROData_Transform::IsDeleted(const TopoDS_Shape& aS)
453 {
454   Standard_Boolean bIsDeleted;
455   //
456   if (myToTransform) { //pkv ft
457     const TopTools_ListOfShape& aLS1=myTransform1->Modified(aS);
458     const TopoDS_Shape& aS1=aLS1.First();
459     //
460     bIsDeleted=myBuilder->IsDeleted(aS1);
461   }
462   else {
463     bIsDeleted=myBuilder->IsDeleted(aS);
464   }
465   //
466   return bIsDeleted;
467 }
468 //=======================================================================
469 //function : HasDeleted
470 //purpose  : 
471 //=======================================================================
472 Standard_Boolean HYDROData_Transform::HasDeleted()
473 {
474   return myBuilder->HasDeleted();
475 }
476 //=======================================================================
477 //function : HasGenerated
478 //purpose  : 
479 //=======================================================================
480 Standard_Boolean HYDROData_Transform::HasGenerated()
481 {
482   return myBuilder->HasGenerated();
483 }
484 //=======================================================================
485 //function : HasModified
486 //purpose  : 
487 //=======================================================================
488 Standard_Boolean HYDROData_Transform::HasModified()
489 {
490   return myBuilder->HasModified();
491 }