MCPcopy Create free account
hub / github.com/PyMesh/PyMesh / fromData

Method fromData

python/pymesh/misc/quaternion.py:43–76  ·  view source on GitHub ↗

Create the rotation to rotate v1 to v2 Args: v1 (``numpy.ndarray``): From vector. Normalization not necessary. v2 (``numpy.ndarray``): To vector. Normalization not necessary. Returns: The following values are returned. * ``quat`` (:

(cls, v1, v2)

Source from the content-addressed store, hash-verified

41
42 @classmethod
43 def fromData(cls, v1, v2):
44 """ Create the rotation to rotate v1 to v2
45
46 Args:
47 v1 (``numpy.ndarray``): From vector. Normalization not necessary.
48 v2 (``numpy.ndarray``): To vector. Normalization not necessary.
49
50 Returns:
51 The following values are returned.
52
53 * ``quat`` (:class:`Quaternion`): Corresponding quaternion that
54 rotates ``v1`` to ``v2``.
55 """
56 eps = 1e-12
57 v1 /= norm(v1)
58 v2 /= norm(v2)
59 c = np.dot(v1, v2)
60 if c < -1.0 + eps:
61 # v1 is parallel and opposite of v2
62 u,s,v = svd(np.array([v1, v2]))
63 axis = v[2,:]
64 else:
65 axis = np.cross(v1, v2)
66 l = norm(axis)
67 if l > 0.0:
68 axis /= norm(axis)
69 else:
70 # Parallel vectors.
71 axis = v1
72
73 w_sq = 0.5 * (1.0 + c)
74 l = sqrt(1.0 - w_sq) * axis
75 quat = np.array([sqrt(w_sq), l[0], l[1], l[2]])
76 return cls(quat)
77
78 def norm(self):
79 """ Quaternion norm.

Callers 10

slice_meshFunction · 0.80
generate_cylinderFunction · 0.80
generate_tubeFunction · 0.80
test_face_edge_touchMethod · 0.80
test_face_edge_touchMethod · 0.80
add_baseFunction · 0.80

Calls

no outgoing calls