0032539: Modeling Algorithms - Parallelize BRepExtrema_DistShapeShape algorithm
[occt.git] / src / BRepTest / BRepTest_ExtremaCommands.cxx
CommitLineData
b311480e 1// Created on: 1995-09-08
2// Created by: Modelistation
3// Copyright (c) 1995-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
7fd59977 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 8// This library is free software; you can redistribute it and/or modify it under
9// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 10// by the Free Software Foundation, with special exception defined in the file
11// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12// distribution for complete text of the license and disclaimer of any warranty.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
b311480e 16
7fd59977 17#include <DBRep.hxx>
18#include <BRepTest.hxx>
7fd59977 19#include <BRepExtrema_Poly.hxx>
20#include <BRepExtrema_DistShapeShape.hxx>
558e68ea 21#include <BRepExtrema_ShapeProximity.hxx>
ae9a414a 22#include <BRepExtrema_SelfIntersection.hxx>
7fd59977 23#include <BRepLib_MakeEdge.hxx>
7fd59977 24#include <BRepLib_MakeVertex.hxx>
ae9a414a 25#include <BRepBuilderAPI_MakeEdge.hxx>
82bee162 26#include <Draw_ProgressIndicator.hxx>
558e68ea 27#include <TopoDS_Builder.hxx>
28#include <TopoDS_Compound.hxx>
7fd59977 29#include <Draw.hxx>
d99f0355 30#include <Message.hxx>
558e68ea 31#include <OSD_Timer.hxx>
32#include <TCollection_AsciiString.hxx>
33#include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
f84bf635 34#include <Precision.hxx>
558e68ea 35
7fd59977 36#include <stdio.h>
7fd59977 37
38//=======================================================================
39//function : distance
40//purpose :
41//=======================================================================
42
43static Standard_Integer distance (Draw_Interpretor& di,
44 Standard_Integer n,
45 const char** a)
46{
47 if (n < 3) return 1;
48
49 const char *name1 = a[1];
50 const char *name2 = a[2];
51
52 TopoDS_Shape S1 = DBRep::Get(name1);
53 TopoDS_Shape S2 = DBRep::Get(name2);
54 if (S1.IsNull() || S2.IsNull()) return 1;
55 gp_Pnt P1,P2;
56 Standard_Real D;
57 if (!BRepExtrema_Poly::Distance(S1,S2,P1,P2,D)) return 1;
04232180 58 //std::cout << " distance : " << D << std::endl;
7fd59977 59 di << " distance : " << D << "\n";
60 TopoDS_Edge E = BRepLib_MakeEdge(P1,P2);
61 DBRep::Set("distance",E);
62 return 0;
63}
762b6cec 64
7fd59977 65static Standard_Integer distmini(Draw_Interpretor& di, Standard_Integer n, const char** a)
f84bf635 66{
0f05f211 67 if (n < 4 || n > 6)
68 {
f84bf635 69 return 1;
0f05f211 70 }
7fd59977 71
72 const char *ns1 = (a[2]), *ns2 = (a[3]), *ns0 = (a[1]);
f84bf635 73 TopoDS_Shape S1(DBRep::Get(ns1)), S2(DBRep::Get(ns2));
74
75 Standard_Real aDeflection = Precision::Confusion();
0f05f211 76 Standard_Integer anIndex = 4;
77 if (n >= 5 && a[4][0] != '-')
78 {
f79b19a1 79 aDeflection = Draw::Atof(a[4]);
0f05f211 80 anIndex++;
81 }
82
83 Standard_Boolean isMultiThread = Standard_False;
84 for (Standard_Integer anI = anIndex; anI < n; anI++)
85 {
86 TCollection_AsciiString anArg(a[anI]);
87 anArg.LowerCase();
88 if (anArg == "-parallel")
89 {
90 isMultiThread = Standard_True;
91 }
92 else
93 {
94 di << "Syntax error at '" << anArg << "'";
95 return 1;
96 }
97 }
7fd59977 98
82bee162 99 Handle(Draw_ProgressIndicator) aProgress = new Draw_ProgressIndicator(di, 1);
0f05f211 100 BRepExtrema_DistShapeShape dst;
101 dst.LoadS1(S1);
102 dst.LoadS2(S2);
103 dst.SetDeflection(aDeflection);
104 dst.SetMultiThread(isMultiThread);
105 dst.Perform(aProgress->Start());
7fd59977 106
107 if (dst.IsDone())
9e20ed57 108 {
0797d9d3 109#ifdef OCCT_DEBUG
04232180 110 //dst.Dump(std::cout);
9e20ed57 111 di << "*** Dump of \"BRepExtrema_DistShapeShape\" in DEBUG mode (begin) *****\n";
112 Standard_SStream aSStream;
113 dst.Dump(aSStream);
114 di << aSStream;
115 di << "*** Dump of \"BRepExtrema_DistShapeShape\" in DEBUG mode (end) *****\n";
7fd59977 116#endif
117
9e20ed57 118 di << "\"distmini\" command returns:\n";
119
120 char named[100];
121 Sprintf(named, "%s%s" ,ns0,"_val");
122 char* tempd = named;
123 Draw::Set(tempd,dst.Value());
124 di << named << " ";
125
f84bf635 126 for (Standard_Integer i1 = 1; i1<= dst.NbSolution(); i1++)
9e20ed57 127 {
128 gp_Pnt P1,P2;
129 P1 = (dst.PointOnShape1(i1));
130 P2 = (dst.PointOnShape2(i1));
131 if (dst.Value()<=1.e-9)
132 {
133 TopoDS_Vertex V =BRepLib_MakeVertex(P1);
134 char namev[100];
135 if (i1==1)
136 Sprintf(namev, "%s" ,ns0);
137 else
138 Sprintf(namev, "%s%d" ,ns0,i1);
139 char* tempv = namev;
140 DBRep::Set(tempv,V);
141 di << namev << " ";
7fd59977 142 }
9e20ed57 143 else
144 {
145 char name[100];
146 TopoDS_Edge E = BRepLib_MakeEdge (P1, P2);
147 if (i1==1)
148 {
149 Sprintf(name,"%s",ns0);
150 }
151 else
152 {
153 Sprintf(name,"%s%d",ns0,i1);
154 }
155
156 char* temp = name;
157 DBRep::Set(temp,E);
158 di << name << " " ;
159 }
160 }
161
162 di << "\nOutput is complete.\n";
163
164 }
7fd59977 165
c894a5fd 166 else di << "problem: no distance is found\n";
7fd59977 167 return 0;
168}
169
558e68ea 170//==============================================================================
171//function : ShapeProximity
172//purpose :
173//==============================================================================
174static int ShapeProximity (Draw_Interpretor& theDI, Standard_Integer theNbArgs, const char** theArgs)
175{
176 if (theNbArgs < 3 || theNbArgs > 6)
177 {
d99f0355 178 Message::SendFail() << "Usage: " << theArgs[0] << " Shape1 Shape2 [-tol <value>] [-profile]";
558e68ea 179 return 1;
180 }
181
182 TopoDS_Shape aShape1 = DBRep::Get (theArgs[1]);
183 TopoDS_Shape aShape2 = DBRep::Get (theArgs[2]);
184
185 if (aShape1.IsNull() || aShape2.IsNull())
186 {
d99f0355 187 Message::SendFail() << "Error: Failed to find specified shapes";
558e68ea 188 return 1;
189 }
190
191 BRepExtrema_ShapeProximity aTool;
192
193 Standard_Boolean aProfile = Standard_False;
194
195 for (Standard_Integer anArgIdx = 3; anArgIdx < theNbArgs; ++anArgIdx)
196 {
197 TCollection_AsciiString aFlag (theArgs[anArgIdx]);
198 aFlag.LowerCase();
199
200 if (aFlag == "-tol")
201 {
202 if (++anArgIdx >= theNbArgs)
203 {
d99f0355 204 Message::SendFail() << "Error: wrong syntax at argument '" << aFlag;
558e68ea 205 return 1;
206 }
207
208 const Standard_Real aTolerance = Draw::Atof (theArgs[anArgIdx]);
209 if (aTolerance < 0.0)
210 {
d99f0355 211 Message::SendFail() << "Error: Tolerance value should be non-negative";
558e68ea 212 return 1;
213 }
214 else
215 {
216 aTool.SetTolerance (aTolerance);
217 }
218 }
219
220 if (aFlag == "-profile")
221 {
222 aProfile = Standard_True;
223 }
224 }
225
226 Standard_Real aInitTime = 0.0;
227 Standard_Real aWorkTime = 0.0;
228
229 OSD_Timer aTimer;
230
231 if (aProfile)
232 {
233 aTimer.Start();
234 }
235
236 aTool.LoadShape1 (aShape1);
237 aTool.LoadShape2 (aShape2);
238
239 if (aProfile)
240 {
241 aInitTime = aTimer.ElapsedTime();
242 aTimer.Reset();
243 aTimer.Start();
244 }
245
246 // Perform shape proximity test
247 aTool.Perform();
248
249 if (aProfile)
250 {
251 aWorkTime = aTimer.ElapsedTime();
252 aTimer.Stop();
253 }
254
255 if (!aTool.IsDone())
256 {
d99f0355 257 Message::SendFail() << "Error: Failed to perform proximity test";
558e68ea 258 return 1;
259 }
260
261 if (aProfile)
262 {
ae9a414a 263 theDI << "Number of primitives in shape 1: " << aTool.ElementSet1()->Size() << "\n";
264 theDI << "Number of primitives in shape 2: " << aTool.ElementSet2()->Size() << "\n";
558e68ea 265 theDI << "Building data structures: " << aInitTime << "\n";
266 theDI << "Executing proximity test: " << aWorkTime << "\n";
267 }
268
ae9a414a 269 TopoDS_Builder aCompBuilder;
558e68ea 270
271 TopoDS_Compound aFaceCompound1;
272 aCompBuilder.MakeCompound (aFaceCompound1);
273
ae9a414a 274 for (BRepExtrema_MapOfIntegerPackedMapOfInteger::Iterator anIt1 (aTool.OverlapSubShapes1()); anIt1.More(); anIt1.Next())
558e68ea 275 {
276 TCollection_AsciiString aStr = TCollection_AsciiString (theArgs[1]) + "_" + (anIt1.Key() + 1);
277
278 const TopoDS_Face& aFace = aTool.GetSubShape1 (anIt1.Key());
279 aCompBuilder.Add (aFaceCompound1, aFace);
280 DBRep::Set (aStr.ToCString(), aFace);
281
282 theDI << aStr << " \n";
283 }
284
285 TopoDS_Compound aFaceCompound2;
286 aCompBuilder.MakeCompound (aFaceCompound2);
287
ae9a414a 288 for (BRepExtrema_MapOfIntegerPackedMapOfInteger::Iterator anIt2 (aTool.OverlapSubShapes2()); anIt2.More(); anIt2.Next())
558e68ea 289 {
290 TCollection_AsciiString aStr = TCollection_AsciiString (theArgs[2]) + "_" + (anIt2.Key() + 1);
291
292 const TopoDS_Face& aFace = aTool.GetSubShape2 (anIt2.Key());
293 aCompBuilder.Add (aFaceCompound2, aFace);
294 DBRep::Set (aStr.ToCString(), aFace);
295
296 theDI << aStr << " \n";
297 }
298
299 DBRep::Set ((TCollection_AsciiString (theArgs[1]) + "_" + "overlapped").ToCString(), aFaceCompound1);
300 DBRep::Set ((TCollection_AsciiString (theArgs[2]) + "_" + "overlapped").ToCString(), aFaceCompound2);
301
302 return 0;
303}
304
ae9a414a 305//==============================================================================
306//function : ShapeSelfIntersection
307//purpose :
308//==============================================================================
309static int ShapeSelfIntersection (Draw_Interpretor& theDI, Standard_Integer theNbArgs, const char** theArgs)
310{
311 if (theNbArgs < 2 || theNbArgs > 5)
312 {
d99f0355 313 Message::SendFail() << "Usage: " << theArgs[0] << " Shape [-tol <value>] [-profile]";
ae9a414a 314 return 1;
315 }
316
317 TopoDS_Shape aShape = DBRep::Get (theArgs[1]);
ae9a414a 318 if (aShape.IsNull())
319 {
d99f0355 320 Message::SendFail() << "Error: Failed to find specified shape";
ae9a414a 321 return 1;
322 }
323
324 Standard_Real aTolerance = 0.0;
325 Standard_Boolean aToProfile = Standard_False;
326
327 for (Standard_Integer anArgIdx = 2; anArgIdx < theNbArgs; ++anArgIdx)
328 {
329 TCollection_AsciiString aFlag (theArgs[anArgIdx]);
330 aFlag.LowerCase();
331
332 if (aFlag == "-tol")
333 {
334 if (++anArgIdx >= theNbArgs)
335 {
d99f0355 336 Message::SendFail() << "Error: wrong syntax at argument '" << aFlag;
ae9a414a 337 return 1;
338 }
339
340 const Standard_Real aValue = Draw::Atof (theArgs[anArgIdx]);
341 if (aValue < 0.0)
342 {
d99f0355 343 Message::SendFail() << "Error: Tolerance value should be non-negative";
ae9a414a 344 return 1;
345 }
346 else
347 {
348 aTolerance = aValue;
349 }
350 }
351
352 if (aFlag == "-profile")
353 {
354 aToProfile = Standard_True;
355 }
356 }
357
358 OSD_Timer aTimer;
359
360 Standard_Real aInitTime = 0.0;
361 Standard_Real aWorkTime = 0.0;
362
363 if (aToProfile)
364 {
365 aTimer.Start();
366 }
367
368 BRepExtrema_SelfIntersection aTool (aShape, aTolerance);
369
370 if (aToProfile)
371 {
372 aInitTime = aTimer.ElapsedTime();
373
374 aTimer.Reset();
375 aTimer.Start();
376 }
377
378 // Perform shape self-intersection test
379 aTool.Perform();
380
381 if (!aTool.IsDone())
382 {
d99f0355 383 Message::SendFail() << "Error: Failed to perform proximity test";
ae9a414a 384 return 1;
385 }
386
387 if (aToProfile)
388 {
389 aWorkTime = aTimer.ElapsedTime();
390 aTimer.Stop();
391
392 theDI << "Building data structure (BVH): " << aInitTime << "\n";
393 theDI << "Executing self-intersection test: " << aWorkTime << "\n";
394 }
395
396 // Extract output faces
397 TopoDS_Builder aCompBuilder;
398 TopoDS_Compound aFaceCompound;
399
400 aCompBuilder.MakeCompound (aFaceCompound);
401
402 for (BRepExtrema_MapOfIntegerPackedMapOfInteger::Iterator anIt (aTool.OverlapElements()); anIt.More(); anIt.Next())
403 {
404 TCollection_AsciiString aStr = TCollection_AsciiString (theArgs[1]) + "_" + (anIt.Key() + 1);
405
406 const TopoDS_Face& aFace = aTool.GetSubShape (anIt.Key());
407 aCompBuilder.Add (aFaceCompound, aFace);
408 DBRep::Set (aStr.ToCString(), aFace);
409
410 theDI << aStr << " \n";
411 }
412
413 theDI << "Compound of overlapped sub-faces: " << theArgs[1] << "_overlapped\n";
414 DBRep::Set ((TCollection_AsciiString (theArgs[1]) + "_" + "overlapped").ToCString(), aFaceCompound);
415
416 return 0;
417}
418
7fd59977 419//=======================================================================
420//function : ExtremaCommands
421//purpose :
422//=======================================================================
423
762b6cec 424void BRepTest::ExtremaCommands (Draw_Interpretor& theCommands)
7fd59977 425{
762b6cec 426 static const char* aGroup = "TOPOLOGY Extrema commands";
427 static Standard_Boolean isDone = Standard_False;
428 if (isDone)
429 {
430 return;
431 }
432 isDone = Standard_True;
433
434 theCommands.Add ("dist",
435 "dist Shape1 Shape2",
436 __FILE__,
437 distance,
438 aGroup);
439
440 theCommands.Add ("distmini",
0f05f211 441 "distmini name Shape1 Shape2 [deflection] [-parallel]",
442 "\n\t\t: Searches minimal distance between two shapes."
443 "\n\t\t: The option is:"
444 "\n\t\t: -parallel : calculate distance in multithreaded mode"
762b6cec 445 __FILE__,
446 distmini,
447 aGroup);
558e68ea 448
449 theCommands.Add ("proximity",
450 "proximity Shape1 Shape2 [-tol <value>] [-profile]"
451 "\n\t\t: Searches for pairs of overlapping faces of the given shapes."
452 "\n\t\t: The options are:"
453 "\n\t\t: -tol : non-negative tolerance value used for overlapping"
454 "\n\t\t: test (for zero tolerance, the strict intersection"
455 "\n\t\t: test will be performed)"
456 "\n\t\t: -profile : outputs execution time for main algorithm stages",
457 __FILE__,
458 ShapeProximity,
459 aGroup);
ae9a414a 460
461 theCommands.Add ("selfintersect",
462 "selfintersect Shape [-tol <value>] [-profile]"
463 "\n\t\t: Searches for intersected/overlapped faces in the given shape."
464 "\n\t\t: The algorithm uses shape tessellation (should be computed in"
465 "\n\t\t: advance), and provides approximate results. The options are:"
466 "\n\t\t: -tol : non-negative tolerance value used for overlapping"
467 "\n\t\t: test (for zero tolerance, the strict intersection"
468 "\n\t\t: test will be performed)"
469 "\n\t\t: -profile : outputs execution time for main algorithm stages",
470 __FILE__,
471 ShapeSelfIntersection,
472 aGroup);
7fd59977 473}