Skip to content
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
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ test = ["pytest"]

[project.urls]
Home = "https://github.com/structuralpython/plotly_3d_primitives"

[dependency-groups]
dev = [
"ipykernel>=6.29.5",
"nbformat>=5.10.4",
]
126 changes: 5 additions & 121 deletions src/plotly_3d_primitives/shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def cone(
def sphere(
radius=0.5,
center=(0.0, 0.0, 0.0),
direction=(0.0, 0.0, 1.0),
direction=(1.0, 0.0, 0.0),
theta_resolution=30,
phi_resolution=30,
start_theta=0.0,
Expand All @@ -124,7 +124,7 @@ def sphere(
color="#555",
opacity=0.5,
) -> go.Mesh3d:
anchor_x, anchor_y, anchor_z = center
anchor_x, anchor_y, anchor_z = (0., 0., 0.)
phi = np.linspace(start_phi, 2 * np.radians(end_phi), phi_resolution + 1)
theta = np.linspace(start_theta, np.radians(end_theta), theta_resolution + 1)

Expand All @@ -134,9 +134,9 @@ def sphere(
z_array = np.ravel(anchor_z + np.cos(phi) * radius_zy)
y_array = np.ravel(anchor_y + np.sin(phi) * radius_zy)
x_array = np.ravel(anchor_x + radius * np.cos(theta))

x_array, y_array, z_array = apply_transformations(x_array, y_array, z_array, center, direction)

# print(center, direction)
# print(y_array)
return go.Mesh3d(
x=x_array, y=y_array, z=z_array, alphahull=0, color=color, opacity=opacity
)
Expand Down Expand Up @@ -264,10 +264,6 @@ def rectangle(
y_array = [y0, y0, y1, y1, y0, y0, y1, y1]
z_array = [z0, z0, z0, z0, z1, z1, z1, z1]

# i_array = [0, 1]
# j_array = [1, 2]
# k_array = [3, 3]

x_array, y_array, z_array = apply_transformations(x_array, y_array, z_array, center, normal)

mesh = go.Mesh3d(
Expand Down Expand Up @@ -330,6 +326,7 @@ def transform_points(
collection (which is originally oriented toward (1, 0, 0)).
"""
transform_matrix = reorient(direction=new_direction)
# print(transform_matrix)
oriented_point_matrix = transform_matrix @ point_matrix
oriented_point_matrix_3 = oriented_point_matrix[0:3]
if not np.allclose(new_center, [0.0, 0.0, 0.0]):
Expand All @@ -341,119 +338,6 @@ def transform_points(
return translated_point_matrix.T


# def rectangular_grid(
# center=(0.0, 0.0, 0.0),
# b=1.0,
# d=1.0,
# normal=(0.0, 0.0, 1.0),
# rows=1,
# cols=1,
# color: str = "#aaa",
# ) -> go.Mesh3d:
# """
# Returns a grid like:
# ... ... ...
# | . | . | . |
# | 3 | 4 | 5 | ...
# | 0 | 1 | 2 | ...

# Where 0, 1, 2, 3, 4, ... etc. are the "indexes" of the grid
# rectangles. They are numbered from the bottom-left left-to-right,
# down-to-up until the bth rectangle which has an index of (m * n - 1)
# where m is rows and n is columns.

# b: total width of grid
# d: total depth (height) of grid

# color: str | dict[Callable, str] will color the rectangles either all
# one color (str) or conditionally color them based on whether the
# index value of each rectangle returns True in the dict callable key
# (the color in the value will be applied if True; the first matching
# condition applies).

# """
# # nodes
# center = np.array(center)
# normal = np.array(normal)

# # Direction cosines
# x_dir = (1, 0, 0)
# y_dir = (0, 1, 0)
# z_dir = (0, 0, 1)
# assumed_normal = z_dir
# norm = np.linalg.norm(normal)
# cos_alpha = np.dot(x_dir, normal) / norm
# cos_beta = np.dot(y_dir, normal) / norm
# cos_gamma = np.dot(z_dir, normal) / norm

# pitch_rot = np.array(
# [[cos_beta, 0, 1 - cos_beta], [0, 1, 0], [-1 - cos_beta, 0, cos_beta]]
# )

# yaw_rot = np.array(
# [[cos_gamma, -1 - cos_gamma, 0], [1 - cos_gamma, cos_gamma, 0], [0, 0, 1]]
# )

# # The projection of the normal on each plane
# xy_proj = np.array([normal[0], normal[1], 0])
# yz_proj = np.array([0, normal[1], normal[2]])
# xz_proj = np.array([normal[0], 0, normal[2]])

# xy_perp = np.array([-normal[1], normal[0], 0])
# yz_perp = np.array([0, normal[2], -normal[1]])
# xz_perp = np.array([normal[2], 0, -normal[0]])

# # Go "down" to the mid-point of the bottom edge of the rectangle
# bot_mid_point = (
# center
# - (
# yz_perp / np.linalg.norm(yz_perp) if np.sum(yz_perp) != 0 else np.zeros(3)
# + xz_perp / np.linalg.norm(xz_perp) if np.sum(xz_perp) != 0 else np.zeros(3)
# ) / 2**0.5 * d/2
# )
# # Then go "left" to the bottom-left corner of the rectangle for the "A" point
# A_point = bot_mid_point - (xy_perp / np.linalg.norm(xy_perp) if np.sum(xy_perp) != 0 else np.zeros(3)) * b/2

# # Go "up" to the mid-poitn of the top edge of the rectangle
# top_mid_point = (
# center
# + (
# yz_perp / np.linalg.norm(yz_perp) if np.sum(yz_perp) != 0 else np.zeros(3)
# + xz_perp / np.linalg.norm(xz_perp) if np.sum(xz_perp) != 0 else np.zeros(3)
# ) / 2**0.5 * d/2
# )
# print(bot_mid_point, top_mid_point)
# # Then go "right" to the top-right corner of the rectangle for the "B" point
# B_point = top_mid_point + (xy_perp / np.linalg.norm(xy_perp) if np.sum(xy_perp) != 0 else np.zeros(3)) * b/2
# hypot_length = np.sqrt((b)**2 + (d)**2)

# print(hypot_length)
# print(np.linalg.norm(B_point - A_point))

# Plane equation
"normal[0] * x + normal[1] * y + normal[2] * z = np.dot(center, normal)"

# Distance from center to "min" point
"sqrt((b/2 - center[0])**2 + (d/2 - center[1])**2 + (0 - center[2])**2)"

# A is "min" point, B is "max" point
"tan(alpha) = d / b"

# Assumption, the bottom edge of the rect will be parallel with the xy plane
# Therefore vector of the bottom edge will be the -ve reciprocal of the xy projection of the vector normal [1, 2, 3] => [1, 2, 0]
# So the bottom edge will be [-2, 1, 0]

# triangles
for j in range(rows):
mod_co = cols + 1
for i in range(cols):
mod_ro = rows + 1
rect_index = i + j * mod_co
anchor_node = rect_index + j

tri_1 = [anchor_node, anchor_node + mod_ro, anchor_node + mod_ro + 1]
tri_2 = [anchor_node, anchor_node + 1, anchor_node + mod_ro + 1]


# From Pyvista
def reorient(direction=(1.0, 0.0, 0.0)):
Expand Down
Loading