Skip to content

Commit 4491a88

Browse files
committed
example that demonstrates how to compute the minimal distance between 2 circles and 2 cubes
1 parent 64fa604 commit 4491a88

1 file changed

Lines changed: 96 additions & 0 deletions

File tree

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/usr/bin/env python
2+
3+
##Copyright 2009-2015 Jelle Ferina (jelleferinga@gmail.com)
4+
##
5+
##This file is part of pythonOCC.
6+
##
7+
##pythonOCC is free software: you can redistribute it and/or modify
8+
##it under the terms of the GNU Lesser General Public License as published by
9+
##the Free Software Foundation, either version 3 of the License, or
10+
##(at your option) any later version.
11+
##
12+
##pythonOCC is distributed in the hope that it will be useful,
13+
##but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
##GNU Lesser General Public License for more details.
16+
##
17+
##You should have received a copy of the GNU Lesser General Public License
18+
##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.
19+
20+
from __future__ import print_function
21+
22+
from OCC.BRepExtrema import BRepExtrema_DistShapeShape
23+
from OCC.BRepPrimAPI import BRepPrimAPI_MakeBox
24+
from OCC.Display.SimpleGui import init_display
25+
from OCC.gp import gp_Pnt, gp_Ax2, gp_Circ
26+
27+
from core_geometry_utils import make_edge, make_vertex
28+
29+
display, start_display, add_menu, add_function_to_menu = init_display()
30+
31+
32+
def compute_minimal_distance_between_cubes():
33+
""" compute the minimal distance between 2 cubes
34+
35+
the line between the 2 points is rendered in cyan
36+
37+
"""
38+
b1 = BRepPrimAPI_MakeBox(gp_Pnt(100, 0, 0), 10., 10., 10.).Shape()
39+
b2 = BRepPrimAPI_MakeBox(gp_Pnt(45, 45, 45), 10., 10., 10.).Shape()
40+
display.DisplayShape([b1, b2])
41+
42+
dss = BRepExtrema_DistShapeShape()
43+
dss.LoadS1(b1)
44+
dss.LoadS2(b2)
45+
dss.Perform()
46+
47+
assert dss.IsDone()
48+
49+
edg = make_edge(dss.PointOnShape1(1), dss.PointOnShape2(1))
50+
display.DisplayColoredShape([edg], color="CYAN")
51+
52+
53+
def compute_minimal_distance_between_circles():
54+
""" compute the minimal distance between 2 circles
55+
56+
here the minimal distance overlaps the intersection of the circles
57+
the points are rendered to indicate the locations
58+
59+
"""
60+
# required for precise rendering of the circles
61+
display.Context.SetDeviationCoefficient(0.0001)
62+
L = gp_Pnt(4, 10, 0)
63+
M = gp_Pnt(10, 16, 0)
64+
65+
Laxis = gp_Ax2()
66+
Maxis = gp_Ax2()
67+
Laxis.SetLocation(L)
68+
Maxis.SetLocation(M)
69+
70+
r1 = 12.0
71+
r2 = 15.0
72+
Lcircle = gp_Circ(Laxis, r1)
73+
Mcircle = gp_Circ(Maxis, r2)
74+
75+
l_circle, m_circle = make_edge(Lcircle), make_edge(Mcircle)
76+
display.DisplayShape((l_circle, m_circle))
77+
78+
# compute the minimal distance between 2 circles
79+
# the minimal distance here matches the intersection of the circles
80+
dss = BRepExtrema_DistShapeShape(l_circle, m_circle)
81+
82+
print("intersection parameters on l_circle:",
83+
[dss.ParOnEdgeS1(i) for i in range(1, dss.NbSolution() + 1)])
84+
print("intersection parameters on m_circle:",
85+
[dss.ParOnEdgeS2(i) for i in range(1, dss.NbSolution() + 1)])
86+
87+
for i in range(1, dss.NbSolution() + 1):
88+
pnt = dss.PointOnShape1(i)
89+
display.DisplayShape(make_vertex(pnt))
90+
91+
92+
if __name__ == "__main__":
93+
compute_minimal_distance_between_cubes()
94+
compute_minimal_distance_between_circles()
95+
display.FitAll()
96+
start_display()

0 commit comments

Comments
 (0)