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