Skip to content

Commit

Permalink
Add segments arg for spline helpers (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
brentyi authored Nov 6, 2023
1 parent a5b646c commit 565685e
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 1 deletion.
4 changes: 3 additions & 1 deletion examples/18_splines.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@

def main() -> None:
server = viser.ViserServer()
for i in range(50):
for i in range(10):
positions = onp.random.normal(size=(30, 3)) * 3.0
server.add_spline_catmull_rom(
f"/catmull_{i}",
positions,
tension=0.5,
line_width=3.0,
color=onp.random.uniform(size=3),
segments=100,
)

control_points = onp.random.normal(size=(30 * 2 - 2, 3)) * 3.0
Expand All @@ -29,6 +30,7 @@ def main() -> None:
control_points,
line_width=3.0,
color=onp.random.uniform(size=3),
segments=100,
)

while True:
Expand Down
4 changes: 4 additions & 0 deletions src/viser/_message_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ def add_spline_catmull_rom(
closed: bool = False,
line_width: float = 1,
color: RgbTupleOrArray = (20, 20, 20),
segments: Optional[int] = None,
) -> None:
"""Add spline using Catmull-Rom interpolation."""
if isinstance(positions, onp.ndarray):
Expand All @@ -273,6 +274,7 @@ def add_spline_catmull_rom(
closed,
line_width,
_encode_rgb(color),
segments=segments,
)
)

Expand All @@ -283,6 +285,7 @@ def add_spline_cubic_bezier(
control_points: Tuple[Tuple[float, float, float], ...] | onp.ndarray,
line_width: float = 1,
color: RgbTupleOrArray = (20, 20, 20),
segments: Optional[int] = None,
) -> None:
"""Add spline using Cubic Bezier interpolation."""

Expand All @@ -303,6 +306,7 @@ def add_spline_cubic_bezier(
control_points,
line_width,
_encode_rgb(color),
segments=segments,
)
)

Expand Down
2 changes: 2 additions & 0 deletions src/viser/_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@ class CatmullRomSplineMessage(Message):
closed: bool
line_width: float
color: int
segments: Optional[int]


@dataclasses.dataclass
Expand All @@ -550,6 +551,7 @@ class CubicBezierSplineMessage(Message):
control_points: Tuple[Tuple[float, float, float], ...]
line_width: float
color: int
segments: Optional[int]


@dataclasses.dataclass
Expand Down
4 changes: 4 additions & 0 deletions src/viser/client/src/WebsocketInterface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,8 @@ function useMessageHandler() {
tension={message.tension}
lineWidth={message.line_width}
color={message.color}
// Sketchy cast needed due to https://github.com/pmndrs/drei/issues/1476.
segments={(message.segments ?? undefined) as undefined}
></CatmullRomLine>
</group>
);
Expand All @@ -762,6 +764,8 @@ function useMessageHandler() {
midB={message.control_points[2 * i + 1]}
lineWidth={message.line_width}
color={message.color}
// Sketchy cast needed due to https://github.com/pmndrs/drei/issues/1476.
segments={(message.segments ?? undefined) as undefined}
></CubicBezierLine>
))}
</group>
Expand Down
2 changes: 2 additions & 0 deletions src/viser/client/src/WebsocketMessages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,7 @@ export interface CatmullRomSplineMessage {
closed: boolean;
line_width: number;
color: number;
segments: number | null;
}
/** Message from server->client carrying Cubic Bezier spline information.
*
Expand All @@ -651,6 +652,7 @@ export interface CubicBezierSplineMessage {
control_points: [number, number, number][];
line_width: number;
color: number;
segments: number | null;
}
/** Message from server->client requesting a render of the current viewport.
*
Expand Down

0 comments on commit 565685e

Please sign in to comment.