0024826: Wrapping of parallelisation algorithms
[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 under
7 // the terms of the GNU Lesser General Public License 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 #include <BOPAlgo_Section.hxx>
34 //
35 #include <BOPTest_DrawableShape.hxx>
36 #include <BOPTest_Objects.hxx>
37
38 #include <OSD_Timer.hxx>
39
40 static Standard_Integer bfillds  (Draw_Interpretor&, Standard_Integer, const char**); 
41 static Standard_Integer bbuild   (Draw_Interpretor&, Standard_Integer, const char**);
42 static Standard_Integer bbop     (Draw_Interpretor&, Standard_Integer, const char**);
43
44 //=======================================================================
45 //function : PartitionCommands
46 //purpose  : 
47 //=======================================================================
48 void BOPTest::PartitionCommands(Draw_Interpretor& theCommands)
49 {
50   static Standard_Boolean done = Standard_False;
51   if (done) return;
52   done = Standard_True;
53   // Chapter's name
54   const char* g = "BOPTest commands";
55   // Commands  
56   theCommands.Add("bfillds", "use bfillds [-t]"  , __FILE__, bfillds, g);
57   theCommands.Add("bbuild" , "use bbuild r [-t]" , __FILE__, bbuild, g);
58   theCommands.Add("bbop"   , "use bbop r op [-t]", __FILE__, bbop, g);
59 }
60 //=======================================================================
61 //function : bfillds
62 //purpose  : 
63 //=======================================================================
64 Standard_Integer bfillds(Draw_Interpretor& di, 
65                          Standard_Integer n, 
66                          const char** a) 
67
68   if (n > 2) {
69     di << " use bfillds [-t]\n";
70     return 0;
71   }
72   //
73   char buf[32];
74   Standard_Boolean bRunParallel, bShowTime;
75   Standard_Integer i, aNbS, iErr;
76   Standard_Real aTol;
77   BOPCol_ListIteratorOfListOfShape aIt;
78   BOPCol_ListOfShape aLC;
79   BOPCol_ListOfShape& aLS=BOPTest_Objects::Shapes();
80   aNbS=aLS.Extent();
81   if (!aNbS) {
82     di << " no objects to process\n";
83     return 0;
84   }
85   //
86   bShowTime = Standard_False;
87   //
88   bRunParallel=BOPTest_Objects::RunParallel();
89   aTol=BOPTest_Objects::FuzzyValue();
90   //
91   for (i=1; i<n; ++i) {
92     if (!strcmp(a[i], "-t")) {
93       bShowTime=Standard_True;
94     }
95     }
96   //
97   BOPCol_ListOfShape& aLT=BOPTest_Objects::Tools();
98   //
99   aIt.Initialize(aLS);
100   for (; aIt.More(); aIt.Next()) {
101     const TopoDS_Shape& aS=aIt.Value();
102     aLC.Append(aS);
103   }
104   //
105   aIt.Initialize(aLT);
106   for (; aIt.More(); aIt.Next()) {
107     const TopoDS_Shape& aS=aIt.Value();
108      aLC.Append(aS);
109   }
110   //
111   BOPAlgo_PaveFiller& aPF=BOPTest_Objects::PaveFiller();
112   //
113   aPF.SetArguments(aLC);
114   aPF.SetRunParallel(bRunParallel);
115   aPF.SetFuzzyValue(aTol);
116   //
117   OSD_Timer aTimer;
118   aTimer.Start();
119   //
120   aPF.Perform();
121   iErr=aPF.ErrorStatus();
122   if (iErr) {
123     Sprintf(buf, " error: %d\n",  iErr);
124     di << buf;
125     return 0;
126   }
127   //
128   aTimer.Stop();
129   aTimer.Show();
130   //
131   if (bShowTime)
132   {
133     Sprintf(buf, "  Tps: %7.2lf\n", aTimer.ElapsedTime());
134     di << buf;
135   }
136   //
137   return 0;
138 }
139 //=======================================================================
140 //function : bbuild
141 //purpose  : 
142 //=======================================================================
143 Standard_Integer bbuild(Draw_Interpretor& di,
144                         Standard_Integer n, 
145                         const char** a) 
146
147   if (n<2) {
148     di << " use bbuild r [-t]\n";
149     return 0;
150   }
151   //
152   BOPDS_PDS pDS=BOPTest_Objects::PDS();
153   if (!pDS) {
154     di << " prepare PaveFiller first\n";
155     return 0;
156   }
157   //
158   char buf[128];
159   Standard_Boolean bRunParallel, bShowTime;
160   Standard_Integer i, iErr;
161
162   BOPCol_ListIteratorOfListOfShape aIt;
163   //
164   BOPAlgo_PaveFiller& aPF=BOPTest_Objects::PaveFiller();
165   //
166   BOPAlgo_Builder& aBuilder=BOPTest_Objects::Builder();
167   aBuilder.Clear();
168   //
169   BOPCol_ListOfShape& aLSObj=BOPTest_Objects::Shapes();
170   aIt.Initialize(aLSObj);
171   for (; aIt.More(); aIt.Next()) {
172     const TopoDS_Shape& aS=aIt.Value();
173     aBuilder.AddArgument(aS);
174   }
175   //
176   BOPCol_ListOfShape& aLSTool=BOPTest_Objects::Tools();
177   aIt.Initialize(aLSTool);
178   for (; aIt.More(); aIt.Next()) {
179     const TopoDS_Shape& aS=aIt.Value();
180     aBuilder.AddArgument(aS);
181   }
182   //
183   bShowTime=Standard_False;
184   bRunParallel=BOPTest_Objects::RunParallel();
185   for (i=2; i<n; ++i) {
186     if (!strcmp(a[i], "-t")) {
187       bShowTime=Standard_True;
188     }
189   }
190   aBuilder.SetRunParallel(bRunParallel);
191   //
192   //
193   OSD_Timer aTimer;
194   aTimer.Start();
195   //
196   aBuilder.PerformWithFiller(aPF); 
197   iErr=aBuilder.ErrorStatus();
198   if (iErr) {
199     Sprintf(buf, " error: %d\n",  iErr);
200     di << buf;
201     return 0;
202   }
203   //
204   aTimer.Stop();
205   aTimer.Show();
206   //
207   if (bShowTime)
208   {
209     Sprintf(buf, "  Tps: %7.2lf\n", aTimer.ElapsedTime());
210     di << buf;
211   }
212   //
213   const TopoDS_Shape& aR=aBuilder.Shape();
214   if (aR.IsNull()) {
215     di << " null shape\n";
216     return 0;
217   }
218   //
219   DBRep::Set(a[1], aR);
220   return 0;
221 }
222 //=======================================================================
223 //function : bbop
224 //purpose  : 
225 //=======================================================================
226 Standard_Integer bbop(Draw_Interpretor& di, 
227                       Standard_Integer n, 
228                       const char** a) 
229
230   if (n<3) {
231     di << " use bbop r op [-t]\n";
232     return 0;
233   }
234   //
235   BOPDS_PDS pDS=BOPTest_Objects::PDS();
236   if (!pDS) {
237     di << " prepare PaveFiller first\n";
238     return 0;
239   }
240   //
241   char buf[32];
242   Standard_Boolean bRunParallel, bShowTime;
243   Standard_Integer iErr, iOp, i;
244   BOPAlgo_Operation aOp;
245   BOPCol_ListIteratorOfListOfShape aIt; 
246   //
247   iOp=Draw::Atoi(a[2]);
248   if (iOp<0 || iOp>4) {
249     di << " invalid operation type\n";
250     return 0;
251   }
252   aOp=(BOPAlgo_Operation)iOp;
253   //
254   bShowTime=Standard_False;
255   bRunParallel=BOPTest_Objects::RunParallel();
256   for (i=3; i<n; ++i) {
257     if (!strcmp(a[i], "-t")) {
258       bShowTime=Standard_True;
259     }
260   }
261   //
262   BOPAlgo_PaveFiller& aPF=BOPTest_Objects::PaveFiller();
263   //
264   BOPAlgo_Builder *pBuilder=NULL;
265   
266   if (aOp!=BOPAlgo_SECTION) { 
267     pBuilder=&BOPTest_Objects::BOP();
268   } 
269   else {
270     pBuilder=&BOPTest_Objects::Section();
271   }
272   //
273   pBuilder->Clear();
274   //
275   BOPCol_ListOfShape& aLSObj=BOPTest_Objects::Shapes();
276   aIt.Initialize(aLSObj);
277   for (; aIt.More(); aIt.Next()) {
278     const TopoDS_Shape& aS=aIt.Value();
279     pBuilder->AddArgument(aS);
280   }
281   //
282   if (aOp!=BOPAlgo_SECTION) {
283     BOPAlgo_BOP *pBOP=(BOPAlgo_BOP *)pBuilder;
284     //
285     BOPCol_ListOfShape& aLSTools=BOPTest_Objects::Tools();
286     aIt.Initialize(aLSTools);
287     for (; aIt.More(); aIt.Next()) {
288       const TopoDS_Shape& aS=aIt.Value();
289       pBOP->AddTool(aS);
290     }
291     //
292     pBOP->SetOperation(aOp);
293   }
294   else {
295     BOPCol_ListOfShape& aLSTools=BOPTest_Objects::Tools();
296     aIt.Initialize(aLSTools);
297     for (; aIt.More(); aIt.Next()) {
298       const TopoDS_Shape& aS=aIt.Value();
299       pBuilder->AddArgument(aS);
300     }
301   }
302   //
303   pBuilder->SetRunParallel(bRunParallel);
304   //
305   OSD_Timer aTimer;
306   aTimer.Start();
307   //
308   pBuilder->PerformWithFiller(aPF);
309   iErr=pBuilder->ErrorStatus();
310   if (iErr) {
311     Sprintf(buf, " error: %d\n",  iErr);
312     di << buf;
313     return 0;
314   }
315   //
316   aTimer.Stop();
317   aTimer.Show();
318   //
319   if (bShowTime) {
320     Sprintf(buf, "  Tps: %7.2lf\n", aTimer.ElapsedTime());
321     di << buf;
322   }
323   //
324   const TopoDS_Shape& aR=pBuilder->Shape();
325   if (aR.IsNull()) {
326     di << " null shape\n";
327     return 0;
328   }
329   //
330   DBRep::Set(a[1], aR);
331   return 0;
332 }
333