From 89eed589e2755803c788441a0b9e3fddbbd49f3e Mon Sep 17 00:00:00 2001 From: bwent Date: Mon, 20 Jul 2015 10:59:55 -0700 Subject: [PATCH] Add Vec3 function to convert vec3 to/from euler angles --- examples/edit.js | 47 +-------------------- libraries/script-engine/src/Vec3.cpp | 62 ++++++++++++++++++++++++++++ libraries/script-engine/src/Vec3.h | 2 + 3 files changed, 66 insertions(+), 45 deletions(-) diff --git a/examples/edit.js b/examples/edit.js index 544f3c94b6..edf60110c4 100644 --- a/examples/edit.js +++ b/examples/edit.js @@ -1211,49 +1211,6 @@ PropertiesTool = function(opts) { webView.setVisible(visible); }; - vecToPolar = function(direction) { - var x = direction.x; - var y = direction.y; - var z = direction.z; - var pitch, yaw; - pitch = -Math.asin(y); - var c = Math.cos(-pitch); - if (Math.abs(pitch) > (Math.PI / 2.0 - epsilon)) { - //handle gymbal lock - if (pitch > 0) { - pitch = Math.PI / 2.0; - } else { - pitch = -Math.PI / 2.0; - } - yaw = 0.0; - } else { - if (z < 0) { - if(x > 0 && x < 1) { - yaw = Math.PI - Math.asin(x / c); - } else { - yaw = -Math.asin(x / c) - Math.PI; - } - } else { - yaw = Math.asin(x / c); - } - } - return { - x: pitch * RADIANS_TO_DEGREES, - y: yaw * RADIANS_TO_DEGREES, - z: 0.0 //discard roll component - }; - }; - - polarToVec = function(orientation) { - var pitch = orientation.x * DEGREES_TO_RADIANS; - var yaw = orientation.y * DEGREES_TO_RADIANS; - return { - x: Math.cos(pitch) * Math.sin(yaw), - y: Math.sin(-pitch), - z: Math.cos(pitch) * Math.cos(yaw) - }; - } - selectionManager.addEventListener(function() { data = { type: 'update', @@ -1267,7 +1224,7 @@ PropertiesTool = function(opts) { entity.properties.rotation = Quat.safeEulerAngles(entity.properties.rotation); } if (entity.properties.keyLightDirection !== undefined) { - entity.properties.keyLightDirection = vecToPolar(entity.properties.keyLightDirection); + entity.properties.keyLightDirection = Vec3.toPolar(entity.properties.keyLightDirection); } selections.push(entity); } @@ -1297,7 +1254,7 @@ PropertiesTool = function(opts) { data.properties.rotation = Quat.fromPitchYawRollDegrees(rotation.x, rotation.y, rotation.z); } if (data.properties.keyLightDirection !== undefined) { - data.properties.keyLightDirection = polarToVec(data.properties.keyLightDirection); + data.properties.keyLightDirection = Vec3.fromPolar(data.properties.keyLightDirection.x, data.properties.keyLightDirection.y); } Entities.editEntity(selectionManager.selections[0], data.properties); if (data.properties.name != undefined) { diff --git a/libraries/script-engine/src/Vec3.cpp b/libraries/script-engine/src/Vec3.cpp index 49a2ad2cbe..2bb28180d9 100644 --- a/libraries/script-engine/src/Vec3.cpp +++ b/libraries/script-engine/src/Vec3.cpp @@ -14,6 +14,7 @@ #include #include "ScriptEngineLogging.h" +#include "NumericalConstants.h" #include "Vec3.h" glm::vec3 Vec3::reflect(const glm::vec3& v1, const glm::vec3& v2) { @@ -73,3 +74,64 @@ void Vec3::print(const QString& lable, const glm::vec3& v) { bool Vec3::equal(const glm::vec3& v1, const glm::vec3& v2) { return v1 == v2; } + +glm::vec3 Vec3::toPolar(const glm::vec3& v) { + glm::vec3 u = normalize(v); + float pitch, yaw, temp; + pitch = glm::asin(-u.y); + temp = glm::cos(pitch); + if (glm::abs(pitch) >= (PI - EPSILON)) { + yaw = 0.0; + if (pitch > 0) { + pitch = PI_OVER_TWO; + } else { + pitch = -PI_OVER_TWO; + } + } else { + if (u.z < 0.0) { + if (u.x > 0.0 && u.x < 1.0) { + yaw = PI - glm::asin(u.x / temp); + } else { + yaw = -PI - glm::asin(u.x / temp); + } + } else { + yaw = glm::asin(u.x / temp); + } + } + + // Round small values to 0 + if (glm::abs(pitch) < EPSILON) { + pitch = 0.0; + } + if (glm::abs(yaw) < EPSILON) { + yaw = 0.0; + } + + // Neglect roll component + return glm::vec3(glm::degrees(pitch), glm::degrees(yaw), 0.0); +} + +glm::vec3 Vec3::fromPolar(float pitch, float yaw) { + pitch = glm::radians(pitch); + yaw = glm::radians(yaw); + + float x = glm::cos(pitch) * glm::sin(yaw); + float y = glm::sin(-pitch); + float z = glm::cos(pitch) * glm::cos(yaw); + + // Round small values to 0 + if (glm::abs(x) < EPSILON) { + x = 0.0; + } + if (glm::abs(y) < EPSILON) { + y = 0.0; + } + if (glm::abs(z) < EPSILON) { + z = 0.0; + } + + return glm::vec3(x, y, z); +} + + + diff --git a/libraries/script-engine/src/Vec3.h b/libraries/script-engine/src/Vec3.h index 2a2e145e8e..198d65004c 100644 --- a/libraries/script-engine/src/Vec3.h +++ b/libraries/script-engine/src/Vec3.h @@ -40,6 +40,8 @@ public slots: glm::vec3 mix(const glm::vec3& v1, const glm::vec3& v2, float m); void print(const QString& lable, const glm::vec3& v); bool equal(const glm::vec3& v1, const glm::vec3& v2); + glm::vec3 toPolar(const glm::vec3& v); + glm::vec3 fromPolar(float pitch, float yaw); };