Skip to content

Commit 6c8eb78

Browse files
committed
add Pyvista transformation reference
1 parent b61c3e5 commit 6c8eb78

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

src/plotly_3d_primitives/shapes.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,3 +277,84 @@ def rectangular_grid(
277277
tri_1 = [anchor_node, anchor_node + mod_ro, anchor_node + mod_ro + 1]
278278
tri_2 = [anchor_node, anchor_node + 1, anchor_node + mod_ro + 1]
279279

280+
# From Pyvista
281+
def translate(surf, center=(0.0, 0.0, 0.0), direction=(1.0, 0.0, 0.0)):
282+
"""Translate and orient a mesh to a new center and direction.
283+
284+
By default, the input mesh is considered centered at the origin
285+
and facing in the x direction.
286+
287+
Parameters
288+
----------
289+
surf : pyvista.core.pointset.PolyData
290+
Mesh to be translated and oriented.
291+
center : tuple, optional, default: (0.0, 0.0, 0.0)
292+
Center point to which the mesh should be translated.
293+
direction : tuple, optional, default: (1.0, 0.0, 0.0)
294+
Direction vector along which the mesh should be oriented.
295+
296+
"""
297+
normx = np.array(direction) / np.linalg.norm(direction)
298+
normy_temp = [0.0, 1.0, 0.0]
299+
300+
# Adjust normy if collinear with normx since cross-product will
301+
# be zero otherwise
302+
if np.allclose(normx, [0, 1, 0]):
303+
normy_temp = [-1.0, 0.0, 0.0]
304+
elif np.allclose(normx, [0, -1, 0]):
305+
normy_temp = [1.0, 0.0, 0.0]
306+
307+
normz = np.cross(normx, normy_temp)
308+
normz /= np.linalg.norm(normz)
309+
normy = np.cross(normz, normx)
310+
311+
trans = np.zeros((4, 4))
312+
trans[:3, 0] = normx
313+
trans[:3, 1] = normy
314+
trans[:3, 2] = normz
315+
trans[3, 3] = 1
316+
317+
surf.transform(trans)
318+
if not np.allclose(center, [0.0, 0.0, 0.0]):
319+
surf.points += np.array(center, dtype=surf.points.dtype)
320+
321+
# Modified - Reference: https://www.euclideanspace.com/maths/geometry/affine/matrix4x4/index.htm
322+
def transform(surf, center=(0.0, 0.0, 0.0), direction=(0.0, 0.0, 1.0)):
323+
"""Translate and orient a mesh to a new center and direction.
324+
325+
By default, the input mesh is considered centered at the origin
326+
and facing in the z direction.
327+
328+
Parameters
329+
----------
330+
surf : pyvista.core.pointset.PolyData
331+
Mesh to be translated and oriented.
332+
center : tuple, optional, default: (0.0, 0.0, 0.0)
333+
Center point to which the mesh should be translated.
334+
direction : tuple, optional, default: (0.0, 0.0, 1.0)
335+
Direction vector along which the mesh should be oriented.
336+
337+
"""
338+
normz = np.array(direction) / np.linalg.norm(direction)
339+
normy_temp = [0.0, 1.0, 0.0]
340+
341+
# Adjust normy if collinear with normx since cross-product will
342+
# be zero otherwise
343+
if np.allclose(normz, [0, 1, 0]):
344+
normy_temp = [0.0, 0.0, -1.0]
345+
elif np.allclose(normz, [0, -1, 0]):
346+
normy_temp = [0.0, 0.0, 1.0]
347+
348+
normx = np.cross(normz, normy_temp)
349+
normx /= np.linalg.norm(normx)
350+
normy = np.cross(normx, normz)
351+
352+
trans = np.zeros((4, 4))
353+
trans[:3, 0] = normx
354+
trans[:3, 1] = normy
355+
trans[:3, 2] = normz
356+
trans[3, 3] = 1
357+
358+
# surf.transform(trans)
359+
# if not np.allclose(center, [0.0, 0.0, 0.0]):
360+
# surf.points += np.array(center, dtype=surf.points.dtype)

0 commit comments

Comments
 (0)