0024639: Parallelization FillDS part of BO
[occt.git] / src / BOPTest / BOPTest_PartitionCommands.cxx
1 // Created by: Peter KURNEV
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and / or modify it
7 // under the terms of the GNU Lesser General Public version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #include <BOPTest.ixx>
16
17 #include <stdio.h>
18 #include <string.h>
19
20 #include <NCollection_IncAllocator.hxx>
21
22 #include <DBRep.hxx>
23 #include <Draw.hxx>
24 #include <Draw_Color.hxx>
25 #include <DrawTrSurf.hxx>
26
27 #include <TopoDS_Shape.hxx>
28 //
29 #include <BOPAlgo_Builder.hxx>
30 #include <BOPAlgo_PaveFiller.hxx>
31 #include <BOPAlgo_Operation.hxx>
32 #include <BOPAlgo_BOP.hxx>
33 //
34 #include <BOPTest_DrawableShape.hxx>
35 #include <BOPTest_Objects.hxx>
36
37 //
38 #ifdef HAVE_TBB
39 #include <BOPCol_TBB.hxx>
40 //=======================================================================
41 //class : BOPTime_Chronometer
42 //purpose  : 
43 //=======================================================================
44 class BOPTime_Chronometer {
45  public:
46   BOPTime_Chronometer() {
47   }
48   //
49   ~BOPTime_Chronometer() {
50   }
51   //
52   void Start() {
53     myT0 = tick_count::now();
54   }
55   //
56   void Stop() {
57     myTime=(tick_count::now() - myT0).seconds();
58   }
59   //
60   double Time() const{
61     return myTime;
62   };
63   //
64  protected:
65   tick_count myT0;
66   double myTime;
67 };
68 ////////////////////////////////////////////////////////////////////////
69 #else
70 #include <OSD_Chronometer.hxx>
71 //=======================================================================
72 //class    : BOPTime_Chronometer
73 //purpose  : 
74 //=======================================================================
75 class BOPTime_Chronometer {
76  public:
77   BOPTime_Chronometer() {
78   }
79   //
80   ~BOPTime_Chronometer() {
81   }
82   //
83   void Start() {
84     myChronometer.Reset();
85     myChronometer.Start();
86   }
87   //
88   void Stop() {
89     myChronometer.Stop();
90     myChronometer.Show(myTime);
91   }
92   //
93   double Time() const{
94     return myTime;
95   };
96   //
97  protected:
98   OSD_Chronometer myChronometer;
99   double myTime;
100 };
101 #endif
102
103 static Standard_Integer bfillds  (Draw_Interpretor&, Standard_Integer, const char**); 
104 static Standard_Integer bbuild   (Draw_Interpretor&, Standard_Integer, const char**);
105 static Standard_Integer bbop     (Draw_Interpretor&, Standard_Integer, const char**);
106 static Standard_Integer bclear   (Draw_Interpretor&, Standard_Integer, const char**);
107
108 //=======================================================================
109 //function : PartitionCommands
110 //purpose  : 
111 //=======================================================================
112 void BOPTest::PartitionCommands(Draw_Interpretor& theCommands)
113 {
114   static Standard_Boolean done = Standard_False;
115   if (done) return;
116   done = Standard_True;
117   // Chapter's name
118   const char* g = "Partition commands";
119   // Commands  
120   theCommands.Add("bfillds"  , "use bfillds [-s -t]" , __FILE__, bfillds, g);
121   theCommands.Add("bbuild"   , "use bbuild r [-s -t]", __FILE__, bbuild, g);
122   theCommands.Add("bbop"     , "use bbop r op"       , __FILE__, bbop, g);
123   theCommands.Add("bclear"   , "use bclear"          , __FILE__, bclear, g);
124 }
125
126 //=======================================================================
127 //function : bclear
128 //purpose  : 
129 //=======================================================================
130 Standard_Integer bclear(Draw_Interpretor& di, 
131                         Standard_Integer n, 
132                         const char** ) 
133 {
134   if (n!=1) {
135     di << " use bclear\n";
136     return 0;
137   }
138   //
139   BOPTest_Objects::Clear(); 
140   return 0;
141 }
142 //=======================================================================
143 //function : bfillds
144 //purpose  : 
145 //=======================================================================
146 Standard_Integer bfillds(Draw_Interpretor& di, 
147                          Standard_Integer n, 
148                          const char** a) 
149
150   if (n>3) {
151     di << " use bfillds [-s -t]\n";
152     return 0;
153   }
154   //
155   char buf[32];
156   Standard_Boolean bRunParallel, bShowTime;
157   Standard_Integer i, aNbS, iErr;
158   BOPCol_ListIteratorOfListOfShape aIt;
159   BOPCol_ListOfShape aLC;
160   BOPTime_Chronometer aChrono;
161   
162   BOPCol_ListOfShape& aLS=BOPTest_Objects::Shapes();
163   aNbS=aLS.Extent();
164   if (!aNbS) {
165     di << " no objects to process\n";
166     return 0;
167   }
168   //
169   bShowTime=Standard_False;
170   bRunParallel=Standard_True;
171   for (i=1; i<n; ++i) {
172     if (!strcmp(a[i], "-s")) {
173       bRunParallel=Standard_False;
174     }
175     else if (!strcmp(a[i], "-t")) {
176       bShowTime=Standard_True;
177     }
178   }
179   //
180   BOPCol_ListOfShape& aLT=BOPTest_Objects::Tools();
181   //
182   aIt.Initialize(aLS);
183   for (; aIt.More(); aIt.Next()) {
184     const TopoDS_Shape& aS=aIt.Value();
185     aLC.Append(aS);
186   }
187   //
188   aIt.Initialize(aLT);
189   for (; aIt.More(); aIt.Next()) {
190     const TopoDS_Shape& aS=aIt.Value();
191      aLC.Append(aS);
192   }
193   //
194   BOPAlgo_PaveFiller& aPF=BOPTest_Objects::PaveFiller();
195   //
196   aPF.SetArguments(aLC);
197   aPF.SetRunParallel(bRunParallel);
198   //
199   aChrono.Start();
200   //
201   aPF.Perform();
202   iErr=aPF.ErrorStatus();
203   if (iErr) {
204     Sprintf(buf, " error: %d\n",  iErr);
205     di << buf;
206     return 0;
207   }
208   //
209   aChrono.Stop();
210   //
211   if (bShowTime) {
212     Standard_Real aTime;
213     //
214     aTime=aChrono.Time();
215     Sprintf(buf, "  Tps: %7.2lf\n", aTime);
216     di << buf;
217   }
218   //
219   return 0;
220 }
221 //=======================================================================
222 //function : bbuild
223 //purpose  : 
224 //=======================================================================
225 Standard_Integer bbuild(Draw_Interpretor& di,
226                         Standard_Integer n, 
227                         const char** a) 
228
229   if (n<2) {
230     di << " use bbuild r [-s -t]\n";
231     return 0;
232   }
233   //
234   BOPDS_PDS pDS=BOPTest_Objects::PDS();
235   if (!pDS) {
236     di << " prepare PaveFiller first\n";
237     return 0;
238   }
239   //
240   char buf[128];
241   Standard_Boolean bRunParallel, bShowTime;
242   Standard_Integer i, iErr;
243   
244   BOPTime_Chronometer aChrono;
245   BOPCol_ListIteratorOfListOfShape aIt;
246   //
247   BOPAlgo_PaveFiller& aPF=BOPTest_Objects::PaveFiller();
248   //
249   BOPAlgo_Builder& aBuilder=BOPTest_Objects::Builder();
250   aBuilder.Clear();
251   //
252   BOPCol_ListOfShape& aLSObj=BOPTest_Objects::Shapes();
253   aIt.Initialize(aLSObj);
254   for (; aIt.More(); aIt.Next()) {
255     const TopoDS_Shape& aS=aIt.Value();
256     aBuilder.AddArgument(aS);
257   }
258   //
259   BOPCol_ListOfShape& aLSTool=BOPTest_Objects::Tools();
260   aIt.Initialize(aLSTool);
261   for (; aIt.More(); aIt.Next()) {
262     const TopoDS_Shape& aS=aIt.Value();
263     aBuilder.AddArgument(aS);
264   }
265   //
266   bShowTime=Standard_False;
267   bRunParallel=Standard_True;
268   for (i=2; i<n; ++i) {
269     if (!strcmp(a[i], "-s")) {
270       bRunParallel=Standard_False;
271     }
272     else if (!strcmp(a[i], "-t")) {
273       bShowTime=Standard_True;
274     }
275   }
276   aBuilder.SetRunParallel(bRunParallel);
277   //
278   //
279   aChrono.Start();
280   //
281   aBuilder.PerformWithFiller(aPF); 
282   iErr=aBuilder.ErrorStatus();
283   if (iErr) {
284     Sprintf(buf, " error: %d\n",  iErr);
285     di << buf;
286     return 0;
287   }
288   //
289   aChrono.Stop();
290   //
291   if (bShowTime) {
292     Standard_Real aTime;
293     //
294     aTime=aChrono.Time();
295     Sprintf(buf, "  Tps: %7.2lf\n", aTime);
296     di << buf;
297   }
298   //
299   const TopoDS_Shape& aR=aBuilder.Shape();
300   if (aR.IsNull()) {
301     di << " null shape\n";
302     return 0;
303   }
304   //
305   DBRep::Set(a[1], aR);
306   return 0;
307 }
308
309 //=======================================================================
310 //function : bbop
311 //purpose  : 
312 //=======================================================================
313 Standard_Integer bbop(Draw_Interpretor& di, 
314                       Standard_Integer n, 
315                       const char** a) 
316
317   if (n!=3) {
318     di << " use bbop r op\n";
319     return 0;
320   }
321   //
322   BOPDS_PDS pDS=BOPTest_Objects::PDS();
323   if (!pDS) {
324     di << " prepare PaveFiller first\n";
325     return 0;
326   }
327   //
328   char buf[32];
329   Standard_Integer iErr, iOp;
330   BOPAlgo_Operation aOp;
331   BOPCol_ListIteratorOfListOfShape aIt;
332   //
333   iOp=Draw::Atoi(a[2]);
334   if (iOp<0 || iOp>4) {
335     di << " invalid operation type\n";
336   }
337   aOp=(BOPAlgo_Operation)iOp;
338   //
339   BOPAlgo_PaveFiller& aPF=BOPTest_Objects::PaveFiller();
340   //
341   BOPAlgo_BOP& aBOP=BOPTest_Objects::BOP();
342   aBOP.Clear();
343   //
344   BOPCol_ListOfShape& aLSObj=BOPTest_Objects::Shapes();
345   aIt.Initialize(aLSObj);
346   for (; aIt.More(); aIt.Next()) {
347     const TopoDS_Shape& aS=aIt.Value();
348     aBOP.AddArgument(aS);
349   }
350   //
351   BOPCol_ListOfShape& aLSTools=BOPTest_Objects::Tools();
352   aIt.Initialize(aLSTools);
353   for (; aIt.More(); aIt.Next()) {
354     const TopoDS_Shape& aS=aIt.Value();
355     aBOP.AddTool(aS);
356   }
357   //
358   aBOP.SetOperation(aOp);
359   //
360   aBOP.PerformWithFiller(aPF);
361   iErr=aBOP.ErrorStatus();
362   if (iErr) {
363     Sprintf(buf, " error: %d\n",  iErr);
364     di << buf;
365     return 0;
366   }
367   //
368   const TopoDS_Shape& aR=aBOP.Shape();
369   if (aR.IsNull()) {
370     di << " null shape\n";
371     return 0;
372   }
373   //
374   DBRep::Set(a[1], aR);
375   return 0;
376 }
377