Created
January 22, 2016 09:36
-
-
Save awesomebytes/2c6eac12857e0e156ce6 to your computer and use it in GitHub Desktop.
Get a orientation Quaternion from two 3d vectors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def normalize(v): | |
| norm = np.linalg.norm(v) | |
| if norm == 0: | |
| return v | |
| return v / norm | |
| # http://stackoverflow.com/questions/17044296/quaternion-rotation-without-euler-angles | |
| def quaternion_from_vectors(v0, v1): | |
| if type(v0) == Point(): | |
| v0 = [v0.x, v0.y, v0.z] | |
| if type(v1) == Point(): | |
| v1 = [v1.x, v1.y, v1.z] | |
| v0 = normalize(v0) | |
| v1 = normalize(v1) | |
| # inline QuaternionT<T> QuaternionT<T>::CreateFromVectors(const Vector3<T>& v0, const Vector3<T>& v1) | |
| # { | |
| # | |
| # Vector3<T> c = v0.Cross(v1); | |
| c = np.cross(v0, v1) | |
| # T d = v0.Dot(v1); | |
| d = np.dot(v0, v1) | |
| # T s = std::sqrt((1 + d) * 2); | |
| try: | |
| s = math.sqrt((1.0 + d) * 2) | |
| except ValueError: | |
| s = 0.0 | |
| if s == 0.0: | |
| # print "s == 0.0, we cant compute" | |
| return [0.0, 0.0, 0.0, 1.0] | |
| # | |
| # QuaternionT<T> q; | |
| q = [0.0, 0.0, 0.0, 0.0] | |
| # q.x = c.x / s; | |
| q[0] = c[0] / s | |
| # q.y = c.y / s; | |
| q[1] = c[1] / s | |
| # q.z = c.z / s; | |
| q[2] = c[2] / s | |
| # q.w = s / 2.0f; | |
| q[3] = s / 2.0 | |
| # return q; | |
| return q | |
| # } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment