Skip to content

Commit c7b4378

Browse files
committed
feat: basic implementation of cone, prism, box
1 parent 4d7276e commit c7b4378

File tree

3 files changed

+59
-17
lines changed

3 files changed

+59
-17
lines changed

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ license = {file = "LICENSE"}
99
classifiers = ["License :: OSI Approved :: Apache Software License"]
1010
dynamic = ["version", "description"]
1111
dependencies = [
12-
"plotly"
12+
"plotly",
13+
"numpy"
1314
]
1415

1516
[project.optional-dependencies]

src/plotly_3d_primitives/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55

66
__version__ = "0.1.0"
77

8-
from .shapes import *
8+
from .shapes import *

src/plotly_3d_primitives/shapes.py

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
import plotly.graph_objects as go
2+
import numpy as np
23

34

45
def box(
56
b: float,
6-
h: float,
77
d: float,
8-
anchor=(0, 0, 0),
8+
h: float,
9+
align: list[str],
10+
anchor: tuple = (0, 0, 0),
911
color: str = "#aaaaaa",
1012
opacity: float = 0.5,
1113
) -> go.Mesh3d:
12-
13-
anchor_x, anchor_y, anchor_z = anchor
14+
15+
anchor_point = anchor
16+
anchor_arr = np.array(anchor_point)
17+
for align_instr in align:
18+
if align_instr == "center":
19+
anchor_arr = anchor_arr - np.array([b / 2, d / 2, h / 2])
20+
21+
anchor_x, anchor_y, anchor_z = anchor_arr
1422

1523
x0 = anchor_x
1624
x1 = b + anchor_x
17-
25+
1826
y0 = anchor_y
1927
y1 = d + anchor_y
2028

@@ -26,22 +34,55 @@ def box(
2634
z_array = [z0, z0, z0, z0, z1, z1, z1, z1]
2735

2836
mesh = go.Mesh3d(
29-
x=x_array,
30-
y=y_array,
31-
z=z_array,
32-
opacity=opacity,
33-
color=color,
34-
alphahull=0
37+
x=x_array, y=y_array, z=z_array, opacity=opacity, color=color, alphahull=0
3538
)
3639

3740
return mesh
3841

3942

40-
def prism() -> go.Mesh3d:
41-
return go.Mesh3d()
43+
def prism(
44+
n_points: int,
45+
h: float,
46+
align: list[str],
47+
anchor: tuple = (0, 0, 0),
48+
color: str = "#aaaaaa",
49+
opacity: float = 0.5,
50+
) -> go.Mesh3d:
51+
anchor_x, anchor_y, anchor_z = anchor
52+
53+
arr = np.linspace(0, 2 * np.pi, num=n_points, endpoint=False)
54+
x_poly = np.cos(arr) + anchor_x
55+
y_poly = np.sin(arr) + anchor_y
56+
z_poly = np.zeros(n_points) + anchor_z
57+
58+
x_array = np.concat([x_poly, x_poly])
59+
y_array = np.concat([y_poly, y_poly])
60+
z_array = np.concat([z_poly, z_poly + h])
4261

62+
return go.Mesh3d(
63+
x=x_array, y=y_array, z=z_array, alphahull=0, color=color, opacity=opacity
64+
)
4365

44-
def cone() -> go.Mesh3d:
45-
return go.Mesh3d()
4666

67+
def cone(
68+
n_points: int,
69+
h: float,
70+
align: list[str],
71+
anchor: tuple = (0, 0, 0),
72+
color: str = "#aaaaaa",
73+
opacity: float = 0.5,
74+
) -> go.Mesh3d:
75+
anchor_x, anchor_y, anchor_z = anchor
76+
77+
arr = np.linspace(0, 2 * np.pi, num=n_points, endpoint=False)
78+
x_poly = np.cos(arr) + anchor_x
79+
y_poly = np.sin(arr) + anchor_y
80+
z_poly = np.zeros(n_points) + anchor_z
4781

82+
x_array = np.concat([x_poly, np.array([anchor_x])])
83+
y_array = np.concat([y_poly, np.array([anchor_y])])
84+
z_array = np.concat([z_poly, np.array([anchor_z + h])])
85+
86+
return go.Mesh3d(
87+
x=x_array, y=y_array, z=z_array, alphahull=0, color=color, opacity=opacity
88+
)

0 commit comments

Comments
 (0)