Skip to content

Commit

Permalink
update examples to latest codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
tomvanmele committed Nov 6, 2023
1 parent 229d9ee commit 8aeaeee
Show file tree
Hide file tree
Showing 35 changed files with 265 additions and 284 deletions.
16 changes: 8 additions & 8 deletions docs/examples/brep_explorer.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# type: ignore

from compas.geometry import Box
from compas.colors import Color
from compas.geometry import Brep
from compas.brep import Brep
from compas_view2.app import App

box = Box.from_width_height_depth(1, 1, 1)
A = Brep.from_box(box)
A.sew()
A.fix()

vertex = A.vertices[0]

Expand All @@ -21,7 +21,7 @@

viewer = App()

viewer.add(A, linewidth=1, linecolor=Color(0.2, 0.2, 0.2))
viewer.add(A, linewidth=2, linecolor=Color(0.2, 0.2, 0.2))

viewer.add(vertex.point, pointcolor=Color.red(), pointsize=20)

Expand All @@ -31,9 +31,9 @@
for edge in edges:
viewer.add(edge.to_line(), linewidth=5, linecolor=Color(0.2, 0.2, 0.2))

for face in faces:
brep = Brep()
brep.native_brep = face.occ_face
viewer.add(brep, show_lines=False, opacity=0.5)
# for face in faces:
# brep = Brep.from_brepfaces()
# brep.occ_shape = face.occ_face
# viewer.add(brep.to_viewmesh()[0], show_lines=False, opacity=0.5)

viewer.show()
37 changes: 14 additions & 23 deletions docs/examples/brep_from_booleans.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
from compas.geometry import Point, Vector, Plane
# type: ignore

from compas.geometry import Frame
from compas.geometry import Box, Cylinder
from compas.geometry import Brep
from compas_view2.app import App
from compas_view2.objects import Collection

R = 1.4
P = Point(0, 0, 0)
X = Vector(1, 0, 0)
Y = Vector(0, 1, 0)
Z = Vector(0, 0, 1)
YZ = Plane(P, X)
ZX = Plane(P, Y)
XY = Plane(P, Z)

box = Box.from_width_height_depth(2 * R, 2 * R, 2 * R)
cx = Cylinder((YZ, 0.7 * R), 4 * R)
cy = Cylinder((ZX, 0.7 * R), 4 * R)
cz = Cylinder((XY, 0.7 * R), 4 * R)

A = Brep.from_box(box)
B1 = Brep.from_cylinder(cx)
B2 = Brep.from_cylinder(cy)
B3 = Brep.from_cylinder(cz)

C = A - (B1 + B2 + B3)
YZ = Frame.worldYZ()
ZX = Frame.worldZX()
XY = Frame.worldXY()

box = Box(2 * R).to_brep()
cx = Cylinder(0.7 * R, 4 * R, frame=YZ).to_brep()
cy = Cylinder(0.7 * R, 4 * R, frame=ZX).to_brep()
cz = Cylinder(0.7 * R, 4 * R, frame=XY).to_brep()

result = box - (cx + cy + cz)

# ==============================================================================
# Visualisation
Expand All @@ -34,6 +25,6 @@
viewer.view.camera.rx = -75
viewer.view.camera.distance = 7

viewer.add(C)
viewer.add(result, linewidth=2)

viewer.run()
28 changes: 12 additions & 16 deletions docs/examples/brep_from_mesh.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
# type: ignore

import compas
from compas.geometry import Vector, Translation, Scale
from compas.geometry import Polyline
from compas.datastructures import Mesh
from compas_occ.brep import BRep
from compas.brep import Brep
from compas_view2.app import App


mesh = Mesh.from_obj(compas.get("tubemesh.obj"))
mesh: Mesh = Mesh.from_obj(compas.get("tubemesh.obj"))

# move the mesh closer to the origin
# place it on the XY plane
# and scale it down

centroid = mesh.centroid()
zmin = min(mesh.vertices_attribute("z"))
Expand All @@ -19,22 +24,13 @@

mesh.transform(S * T)

brep = BRep.from_mesh(mesh)
brep.check()
# convert to a brep

lines = []
curves = []
brep = Brep.from_mesh(mesh)

for edge in brep.edges:
if edge.is_line:
lines.append(edge.to_line())
elif edge.is_bspline:
curves.append(Polyline(edge.curve.locus()))
# visualize

viewer = App(viewmode="ghosted")
viewer.add(brep, show_lines=False)

for curve in curves:
viewer.add(curve, linewidth=2)
viewer.add(brep, linewidth=2)

viewer.run()
31 changes: 23 additions & 8 deletions docs/examples/brep_overlap.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# type: ignore

from compas.geometry import Translation
from compas.geometry import Box
from compas.geometry import Brep
from compas.colors import Color
from compas.brep import Brep
from compas_view2.app import App
from compas_view2.objects import Collection

box = Box.from_width_height_depth(1, 1, 1)
A = Brep.from_box(box)
Expand All @@ -16,12 +18,25 @@

viewer = App()

viewer.add(Collection(items=[A, B]), show_lines=False, opacity=0.5)

brep = Brep.from_native(FA[0].occ_face)
viewer.add(brep, show_lines=False, facecolor=Color.red().lightened(50))

brep = Brep.from_native(FB[0].occ_face)
viewer.add(brep, show_lines=False, facecolor=Color.blue().lightened(50))
viewer.add(A, opacity=0.5, linewidth=3)
viewer.add(B, opacity=0.5, linewidth=3)

for face in FA[:1]:
brep = Brep.from_brepfaces([face])
viewer.add(
brep,
facecolor=Color.red().lightened(50),
linewidth=3,
linecolor=Color.red(),
)

for face in FB[:1]:
brep = Brep.from_brepfaces([face])
viewer.add(
brep,
facecolor=Color.blue().lightened(50),
linewidth=3,
linecolor=Color.blue(),
)

viewer.show()
49 changes: 30 additions & 19 deletions docs/examples/brepface_with_hole.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from compas.geometry import Point, Vector, Plane, Circle
from compas_occ.brep import BRepEdge, BRepLoop, BRepFace, BRep
from compas_occ.geometry import OCCNurbsCurve, OCCNurbsSurface
# type: ignore

from compas.geometry import Point, Vector, Frame, Circle
from compas_occ.brep import OCCBrepEdge, OCCBrepLoop, OCCBrepFace
from compas.brep import Brep
from compas.geometry import NurbsCurve, NurbsSurface
from compas_view2.app import App

points = [
Expand All @@ -10,27 +13,35 @@
[Point(0, 3, 0), Point(1, 3, 0), Point(2, 3, 0), Point(3, 3, 0)],
]

surface = OCCNurbsSurface.from_points(points=points)
surface = NurbsSurface.from_points(points=points)

circle = Circle(
0.5,
frame=Frame(
Point(1.5, 1.5, 1.5),
Vector(1, 0, 0),
Vector(0, 1, 0),
),
)

circle = Circle(Plane(Point(1.5, 1.5, 1.5), Vector(0, 0, 1)), 0.5)
curve = OCCNurbsCurve.from_circle(circle).projected(surface).embedded(surface)
# projected is still 3D
# embedded is 2D
# and the 2D curve should keep track of the embedding surface
curve = NurbsCurve.from_circle(circle)

edge = BRepEdge.from_curve(curve, surface)
loop = BRepLoop.from_edges([edge])
edge = OCCBrepEdge.from_curve(curve=curve, surface=surface)
loop = OCCBrepLoop.from_edges([edge])

face = BRepFace.from_surface(surface)
# perhaps this should be:
# face = OCCBrepFace()
# face.set_surface(surface)
# face.add_boundary(loop) => if the loop edges are not embedded in the surface, they should be
# face.add_hole(loop) => if the loop edges ...
face = OCCBrepFace.from_surface(surface)
face.add_loop(loop)

brep = BRep.from_faces([face])
mesh = brep.to_tesselation()
brep = Brep.from_brepfaces([face])

viewer = App()
viewer.add(mesh, show_lines=False)
for edge in brep.edges:
if edge.is_line:
viewer.add(edge.to_line(), linewidth=2)
elif edge.is_circle:
viewer.add(edge.curve.to_polyline(), linewidth=2)
elif edge.is_bspline:
viewer.add(edge.curve.to_polyline(), linewidth=2)
viewer.add(brep, linewidth=2)
viewer.show()
34 changes: 14 additions & 20 deletions docs/examples/brepface_with_holes.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
from compas.geometry import Plane, Circle
from compas_occ.brep import BRepEdge, BRepLoop, BRepFace, BRep
from compas_view2.app import App
# type: ignore

plane = Plane.worldXY()
from compas.geometry import Frame, Plane, Circle
from compas_occ.brep import OCCBrepEdge, OCCBrepLoop, OCCBrepFace
from compas.brep import Brep
from compas_view2.app import App

circle1 = Circle(Plane([2, 2, 0], [0, 0, 1]), 1.0)
circle2 = Circle(Plane([-2, -2, 0], [0, 0, 1]), 2.0)
circle3 = Circle(Plane([2, -2, 0], [0, 0, 1]), 0.5)
circle1 = Circle(1.0, frame=Frame([2, 2, 0]))
circle2 = Circle(2.0, frame=Frame([-2, -2, 0]))
circle3 = Circle(0.5, frame=Frame([2, -2, 0]))

loop1 = BRepLoop.from_edges([BRepEdge.from_circle(circle1)])
loop2 = BRepLoop.from_edges([BRepEdge.from_circle(circle2)])
loop3 = BRepLoop.from_edges([BRepEdge.from_circle(circle3)])
loop1 = OCCBrepLoop.from_edges([OCCBrepEdge.from_circle(circle1)])
loop2 = OCCBrepLoop.from_edges([OCCBrepEdge.from_circle(circle2)])
loop3 = OCCBrepLoop.from_edges([OCCBrepEdge.from_circle(circle3)])

face = BRepFace.from_plane(plane, udomain=(-5, 5), vdomain=(-5, 5))
face = OCCBrepFace.from_plane(Plane.worldXY(), udomain=(-5, 5), vdomain=(-5, 5))
face.add_loops([loop1, loop2, loop3], reverse=True)

brep = BRep.from_faces([face])
brep = Brep.from_brepfaces([face])

viewer = App()
viewer.add(brep, show_lines=False)

for edge in brep.edges:
if edge.is_line:
viewer.add(edge.to_line(), linewidth=2)
elif edge.is_circle:
viewer.add(edge.curve.to_polyline(), linewidth=2)

viewer.add(brep, linewidth=2)
viewer.show()
20 changes: 9 additions & 11 deletions docs/examples/curve_closest_parameters_curve.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
# type: ignore

from compas.geometry import Point
from compas.geometry import Polyline
from compas_occ.geometry import OCCNurbsCurve
from compas.geometry import NurbsCurve
from compas_view2.app import App


points0 = [Point(0, 0, 0), Point(3, 6, 0), Point(6, -3, 3), Point(10, 0, 0)]
curve0 = OCCNurbsCurve.from_points(points0)
curve0 = NurbsCurve.from_points(points0)

points1 = [Point(6, -3, 0), Point(3, 1, 0), Point(6, 6, 3), Point(3, 12, 0)]
curve1 = OCCNurbsCurve.from_points(points1)
curve1 = NurbsCurve.from_points(points1)

# this doesn't make much sense from an API pov
# should be like with intersections
parameters, distance = curve0.closest_parameters_curve(curve1, return_distance=True)

points = curve0.closest_points_curve(curve1, return_distance=False)

# ==============================================================================
# Visualisation
# ==============================================================================

view = App()

view.add(Polyline(curve0.locus()), linewidth=3)
view.add(Polyline(curve1.locus()), linewidth=3)
view.add(curve0.to_polyline(), linewidth=3)
view.add(curve1.to_polyline(), linewidth=3)

view.add(points[0], pointcolor=(1, 0, 0))
view.add(points[1], pointcolor=(1, 0, 0))
Expand Down
11 changes: 5 additions & 6 deletions docs/examples/curve_closest_point.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
# type: ignore

from compas.geometry import Point
from compas.geometry import Polyline
from compas_occ.geometry import OCCNurbsCurve
from compas.geometry import NurbsCurve
from compas_view2.app import App


points = [Point(0, 0, 0), Point(3, 0, 2), Point(6, 0, -3), Point(8, 0, 0)]
curve = OCCNurbsCurve.from_interpolation(points)
curve = NurbsCurve.from_interpolation(points)

point = Point(2, -1, 0)
closest, t = curve.closest_point(point, return_parameter=True)

print(curve.point_at(t) == closest)

view = App()

view.add(Polyline(curve.locus()), linewidth=3)
view.add(curve.to_polyline(), linewidth=3)
view.add(point, pointcolor=(0, 0, 1))
view.add(closest, pointcolor=(1, 0, 0))

Expand Down
Loading

0 comments on commit 8aeaeee

Please sign in to comment.