1 // Created on: 1995-09-08
2 // Created by: Modelistation
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
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
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.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 // modified by mps (juillet 96) : ajout de la commande distmini
20 #include <BRepTest.hxx>
21 #include <BRepExtrema_Poly.hxx>
22 #include <BRepExtrema_DistShapeShape.hxx>
23 #include <BRepExtrema_ShapeProximity.hxx>
24 #include <BRepExtrema_SelfIntersection.hxx>
25 #include <BRepLib_MakeEdge.hxx>
26 #include <BRepLib_MakeVertex.hxx>
27 #include <BRepBuilderAPI_MakeEdge.hxx>
28 #include <TopoDS_Builder.hxx>
29 #include <TopoDS_Compound.hxx>
31 #include <OSD_Timer.hxx>
32 #include <TCollection_AsciiString.hxx>
33 #include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
34 #include <Precision.hxx>
41 //=======================================================================
44 //=======================================================================
46 static Standard_Integer distance (Draw_Interpretor& di,
52 const char *name1 = a[1];
53 const char *name2 = a[2];
55 TopoDS_Shape S1 = DBRep::Get(name1);
56 TopoDS_Shape S2 = DBRep::Get(name2);
57 if (S1.IsNull() || S2.IsNull()) return 1;
60 if (!BRepExtrema_Poly::Distance(S1,S2,P1,P2,D)) return 1;
61 //std::cout << " distance : " << D << std::endl;
62 di << " distance : " << D << "\n";
63 TopoDS_Edge E = BRepLib_MakeEdge(P1,P2);
64 DBRep::Set("distance",E);
68 static Standard_Integer distmini(Draw_Interpretor& di, Standard_Integer n, const char** a)
70 if (n != 4 && n != 5 )
73 const char *ns1 = (a[2]), *ns2 = (a[3]), *ns0 = (a[1]);
74 TopoDS_Shape S1(DBRep::Get(ns1)), S2(DBRep::Get(ns2));
76 Standard_Real aDeflection = Precision::Confusion();
78 aDeflection = Draw::Atof(a[4]);
80 BRepExtrema_DistShapeShape dst(S1 ,S2, aDeflection);
85 //dst.Dump(std::cout);
86 di << "*** Dump of \"BRepExtrema_DistShapeShape\" in DEBUG mode (begin) *****\n";
87 Standard_SStream aSStream;
90 di << "*** Dump of \"BRepExtrema_DistShapeShape\" in DEBUG mode (end) *****\n";
93 di << "\"distmini\" command returns:\n";
96 Sprintf(named, "%s%s" ,ns0,"_val");
98 Draw::Set(tempd,dst.Value());
101 for (Standard_Integer i1 = 1; i1<= dst.NbSolution(); i1++)
104 P1 = (dst.PointOnShape1(i1));
105 P2 = (dst.PointOnShape2(i1));
106 if (dst.Value()<=1.e-9)
108 TopoDS_Vertex V =BRepLib_MakeVertex(P1);
111 Sprintf(namev, "%s" ,ns0);
113 Sprintf(namev, "%s%d" ,ns0,i1);
121 TopoDS_Edge E = BRepLib_MakeEdge (P1, P2);
124 Sprintf(name,"%s",ns0);
128 Sprintf(name,"%s%d",ns0,i1);
137 di << "\nOutput is complete.\n";
141 else di << "problem: no distance is found\n";
145 //==============================================================================
146 //function : ShapeProximity
148 //==============================================================================
149 static int ShapeProximity (Draw_Interpretor& theDI, Standard_Integer theNbArgs, const char** theArgs)
151 if (theNbArgs < 3 || theNbArgs > 6)
153 std::cout << "Usage: " << theArgs[0] <<
154 " Shape1 Shape2 [-tol <value>] [-profile]" << std::endl;
159 TopoDS_Shape aShape1 = DBRep::Get (theArgs[1]);
160 TopoDS_Shape aShape2 = DBRep::Get (theArgs[2]);
162 if (aShape1.IsNull() || aShape2.IsNull())
164 std::cout << "Error: Failed to find specified shapes" << std::endl;
168 BRepExtrema_ShapeProximity aTool;
170 Standard_Boolean aProfile = Standard_False;
172 for (Standard_Integer anArgIdx = 3; anArgIdx < theNbArgs; ++anArgIdx)
174 TCollection_AsciiString aFlag (theArgs[anArgIdx]);
179 if (++anArgIdx >= theNbArgs)
181 std::cout << "Error: wrong syntax at argument '" << aFlag << std::endl;
185 const Standard_Real aTolerance = Draw::Atof (theArgs[anArgIdx]);
186 if (aTolerance < 0.0)
188 std::cout << "Error: Tolerance value should be non-negative" << std::endl;
193 aTool.SetTolerance (aTolerance);
197 if (aFlag == "-profile")
199 aProfile = Standard_True;
203 Standard_Real aInitTime = 0.0;
204 Standard_Real aWorkTime = 0.0;
213 aTool.LoadShape1 (aShape1);
214 aTool.LoadShape2 (aShape2);
218 aInitTime = aTimer.ElapsedTime();
223 // Perform shape proximity test
228 aWorkTime = aTimer.ElapsedTime();
234 std::cout << "Error: Failed to perform proximity test" << std::endl;
240 theDI << "Number of primitives in shape 1: " << aTool.ElementSet1()->Size() << "\n";
241 theDI << "Number of primitives in shape 2: " << aTool.ElementSet2()->Size() << "\n";
242 theDI << "Building data structures: " << aInitTime << "\n";
243 theDI << "Executing proximity test: " << aWorkTime << "\n";
246 TopoDS_Builder aCompBuilder;
248 TopoDS_Compound aFaceCompound1;
249 aCompBuilder.MakeCompound (aFaceCompound1);
251 for (BRepExtrema_MapOfIntegerPackedMapOfInteger::Iterator anIt1 (aTool.OverlapSubShapes1()); anIt1.More(); anIt1.Next())
253 TCollection_AsciiString aStr = TCollection_AsciiString (theArgs[1]) + "_" + (anIt1.Key() + 1);
255 const TopoDS_Face& aFace = aTool.GetSubShape1 (anIt1.Key());
256 aCompBuilder.Add (aFaceCompound1, aFace);
257 DBRep::Set (aStr.ToCString(), aFace);
259 theDI << aStr << " \n";
262 TopoDS_Compound aFaceCompound2;
263 aCompBuilder.MakeCompound (aFaceCompound2);
265 for (BRepExtrema_MapOfIntegerPackedMapOfInteger::Iterator anIt2 (aTool.OverlapSubShapes2()); anIt2.More(); anIt2.Next())
267 TCollection_AsciiString aStr = TCollection_AsciiString (theArgs[2]) + "_" + (anIt2.Key() + 1);
269 const TopoDS_Face& aFace = aTool.GetSubShape2 (anIt2.Key());
270 aCompBuilder.Add (aFaceCompound2, aFace);
271 DBRep::Set (aStr.ToCString(), aFace);
273 theDI << aStr << " \n";
276 DBRep::Set ((TCollection_AsciiString (theArgs[1]) + "_" + "overlapped").ToCString(), aFaceCompound1);
277 DBRep::Set ((TCollection_AsciiString (theArgs[2]) + "_" + "overlapped").ToCString(), aFaceCompound2);
282 //==============================================================================
283 //function : ShapeSelfIntersection
285 //==============================================================================
286 static int ShapeSelfIntersection (Draw_Interpretor& theDI, Standard_Integer theNbArgs, const char** theArgs)
288 if (theNbArgs < 2 || theNbArgs > 5)
290 std::cout << "Usage: " << theArgs[0] <<
291 " Shape [-tol <value>] [-profile]" << std::endl;
296 TopoDS_Shape aShape = DBRep::Get (theArgs[1]);
300 std::cout << "Error: Failed to find specified shape" << std::endl;
304 Standard_Real aTolerance = 0.0;
305 Standard_Boolean aToProfile = Standard_False;
307 for (Standard_Integer anArgIdx = 2; anArgIdx < theNbArgs; ++anArgIdx)
309 TCollection_AsciiString aFlag (theArgs[anArgIdx]);
314 if (++anArgIdx >= theNbArgs)
316 std::cout << "Error: wrong syntax at argument '" << aFlag << std::endl;
320 const Standard_Real aValue = Draw::Atof (theArgs[anArgIdx]);
323 std::cout << "Error: Tolerance value should be non-negative" << std::endl;
332 if (aFlag == "-profile")
334 aToProfile = Standard_True;
340 Standard_Real aInitTime = 0.0;
341 Standard_Real aWorkTime = 0.0;
348 BRepExtrema_SelfIntersection aTool (aShape, aTolerance);
352 aInitTime = aTimer.ElapsedTime();
358 // Perform shape self-intersection test
363 std::cout << "Error: Failed to perform proximity test" << std::endl;
369 aWorkTime = aTimer.ElapsedTime();
372 theDI << "Building data structure (BVH): " << aInitTime << "\n";
373 theDI << "Executing self-intersection test: " << aWorkTime << "\n";
376 // Extract output faces
377 TopoDS_Builder aCompBuilder;
378 TopoDS_Compound aFaceCompound;
380 aCompBuilder.MakeCompound (aFaceCompound);
382 for (BRepExtrema_MapOfIntegerPackedMapOfInteger::Iterator anIt (aTool.OverlapElements()); anIt.More(); anIt.Next())
384 TCollection_AsciiString aStr = TCollection_AsciiString (theArgs[1]) + "_" + (anIt.Key() + 1);
386 const TopoDS_Face& aFace = aTool.GetSubShape (anIt.Key());
387 aCompBuilder.Add (aFaceCompound, aFace);
388 DBRep::Set (aStr.ToCString(), aFace);
390 theDI << aStr << " \n";
393 theDI << "Compound of overlapped sub-faces: " << theArgs[1] << "_overlapped\n";
394 DBRep::Set ((TCollection_AsciiString (theArgs[1]) + "_" + "overlapped").ToCString(), aFaceCompound);
399 //=======================================================================
400 //function : ExtremaCommands
402 //=======================================================================
404 void BRepTest::ExtremaCommands (Draw_Interpretor& theCommands)
406 static const char* aGroup = "TOPOLOGY Extrema commands";
407 static Standard_Boolean isDone = Standard_False;
412 isDone = Standard_True;
414 theCommands.Add ("dist",
415 "dist Shape1 Shape2",
420 theCommands.Add ("distmini",
421 "distmini name Shape1 Shape2 [deflection]",
426 theCommands.Add ("proximity",
427 "proximity Shape1 Shape2 [-tol <value>] [-profile]"
428 "\n\t\t: Searches for pairs of overlapping faces of the given shapes."
429 "\n\t\t: The options are:"
430 "\n\t\t: -tol : non-negative tolerance value used for overlapping"
431 "\n\t\t: test (for zero tolerance, the strict intersection"
432 "\n\t\t: test will be performed)"
433 "\n\t\t: -profile : outputs execution time for main algorithm stages",
438 theCommands.Add ("selfintersect",
439 "selfintersect Shape [-tol <value>] [-profile]"
440 "\n\t\t: Searches for intersected/overlapped faces in the given shape."
441 "\n\t\t: The algorithm uses shape tessellation (should be computed in"
442 "\n\t\t: advance), and provides approximate results. The options are:"
443 "\n\t\t: -tol : non-negative tolerance value used for overlapping"
444 "\n\t\t: test (for zero tolerance, the strict intersection"
445 "\n\t\t: test will be performed)"
446 "\n\t\t: -profile : outputs execution time for main algorithm stages",
448 ShapeSelfIntersection,