Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Face.make_bezier_surface() #552

Merged
merged 3 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/build123d/topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
from OCP.GCPnts import GCPnts_AbscissaPoint
from OCP.Geom import (
Geom_BezierCurve,
Geom_BezierSurface,
Geom_ConicalSurface,
Geom_CylindricalSurface,
Geom_Plane,
Expand Down Expand Up @@ -5689,6 +5690,53 @@

return cls(BRepBuilderAPI_MakeFace(spline_geom, Precision.Confusion_s()).Face())

@classmethod
def make_bezier_surface(
cls,
points: list[list[VectorLike]],
weights: list[list[float]] = None,

Check warning on line 5697 in src/build123d/topology.py

View check run for this annotation

Codecov / codecov/patch

src/build123d/topology.py#L5697

Added line #L5697 was not covered by tests
) -> Face:
"""make_bezier_surface

Construct a Bézier surface from the provided 2d array of points.

Args:
points (list[list[VectorLike]]): a 2D list of control points
weights (list[list[float]], optional): control point weights. Defaults to None.

Raises:
ValueError: Too few control points
ValueError: Too many control points
ValueError: A weight is required for each control point

Returns:
Face: a potentially non-planar face
"""
if len(points) < 2 or len(points[0]) < 2:
raise ValueError(
"At least two control points must be provided (start, end)"
)
if len(points) > 25 or len(points[0]) > 25:
raise ValueError("The maximum number of control points is 25")
if weights and (len(points) != len(weights) or len(points[0]) != len(weights[0])):
raise ValueError("A weight must be provided for each control point")

points_ = TColgp_HArray2OfPnt(1, len(points), 1, len(points[0]))
for i, row in enumerate(points):
for j, point in enumerate(row):

Check warning on line 5726 in src/build123d/topology.py

View check run for this annotation

Codecov / codecov/patch

src/build123d/topology.py#L5726

Added line #L5726 was not covered by tests
points_.SetValue(i + 1, j + 1, Vector(point).to_pnt())

if weights:
weights_ = TColgp_HArray2OfPnt(1, len(weights), 1, len(weights[0]))

Check warning on line 5730 in src/build123d/topology.py

View check run for this annotation

Codecov / codecov/patch

src/build123d/topology.py#L5730

Added line #L5730 was not covered by tests
for i, row in enumerate(weights):
for j, weight in enumerate(row):

Check warning on line 5732 in src/build123d/topology.py

View check run for this annotation

Codecov / codecov/patch

src/build123d/topology.py#L5732

Added line #L5732 was not covered by tests
weights_.SetValue(i + 1, j + 1, float(weight))
bezier = Geom_BezierSurface(points_, weights_)
else:
bezier = Geom_BezierSurface(points_)

return cls(BRepBuilderAPI_MakeFace(bezier, Precision.Confusion_s()).Face())

@classmethod
def make_surface(
cls,
Expand Down
12 changes: 12 additions & 0 deletions tests/test_direct_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,18 @@ def test_surface_from_array_of_points(self):
self.assertVectorAlmostEquals(bbox.min, (0, 0, -1), 3)
self.assertVectorAlmostEquals(bbox.max, (10, 10, 2), 2)

def test_bezier_surface(self):
points = [ [ (x, y,
2 if x == 0 and y == 0 else
1 if x == 0 or y == 0 else 0
) for x in range(-1,2)
] for y in range(-1, 2)
]
surface = Face.make_bezier_surface(points)
bbox = surface.bounding_box()
self.assertVectorAlmostEquals(bbox.min, (-1, -1, 0), 3)
self.assertVectorAlmostEquals(bbox.max, (+1, +1, +1), 1)

def test_thicken(self):
pnts = [
[
Expand Down
Loading