From 89eed589e2755803c788441a0b9e3fddbbd49f3e Mon Sep 17 00:00:00 2001 From: bwent Date: Mon, 20 Jul 2015 10:59:55 -0700 Subject: [PATCH 1/5] 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); }; From 30d4b9f6354fecc1b49f360a26d58f6802b348e5 Mon Sep 17 00:00:00 2001 From: bwent Date: Mon, 20 Jul 2015 16:14:56 -0700 Subject: [PATCH 2/5] switch to radians, overload fromPolar, use atan2 --- libraries/script-engine/src/Vec3.cpp | 66 ++++++++++------------------ libraries/script-engine/src/Vec3.h | 1 + 2 files changed, 25 insertions(+), 42 deletions(-) diff --git a/libraries/script-engine/src/Vec3.cpp b/libraries/script-engine/src/Vec3.cpp index 2bb28180d9..71b04eb321 100644 --- a/libraries/script-engine/src/Vec3.cpp +++ b/libraries/script-engine/src/Vec3.cpp @@ -77,61 +77,43 @@ bool Vec3::equal(const glm::vec3& v1, const glm::vec3& 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); - } - } + float azimuth, elevation; - // Round small values to 0 - if (glm::abs(pitch) < EPSILON) { - pitch = 0.0; - } - if (glm::abs(yaw) < EPSILON) { - yaw = 0.0; - } + azimuth = glm::asin(-u.y); + elevation = atan2(v.x, v.z); - // Neglect roll component - return glm::vec3(glm::degrees(pitch), glm::degrees(yaw), 0.0); + // Round off small decimal values + if (glm::abs(azimuth) < EPSILON) { + azimuth = 0.0f; + } + if (glm::abs(elevation) < EPSILON) { + elevation = 0.0f; + } + + return glm::vec3(azimuth, elevation, length(v)); } -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); +glm::vec3 Vec3::fromPolar(const glm::vec3& polar) { + float x = glm::cos(polar.x) * glm::sin(polar.y); + float y = glm::sin(-polar.x); + float z = glm::cos(polar.x) * glm::cos(polar.y); // Round small values to 0 if (glm::abs(x) < EPSILON) { - x = 0.0; + x = 0.0f; } if (glm::abs(y) < EPSILON) { - y = 0.0; + y = 0.0f; } if (glm::abs(z) < EPSILON) { - z = 0.0; + z = 0.0f; } - return glm::vec3(x, y, z); + return multiply(polar.z, glm::vec3(x, y, z)); } - +glm::vec3 Vec3::fromPolar(float azimuth, float elevation) { + glm::vec3 v = glm::vec3(azimuth, elevation, 1.0f); + return fromPolar(v); +} diff --git a/libraries/script-engine/src/Vec3.h b/libraries/script-engine/src/Vec3.h index 198d65004c..2b202b7a2d 100644 --- a/libraries/script-engine/src/Vec3.h +++ b/libraries/script-engine/src/Vec3.h @@ -41,6 +41,7 @@ public slots: 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(const glm::vec3& polar); glm::vec3 fromPolar(float pitch, float yaw); }; From d688e5f915187b33a55f30f5f30678d48b076d51 Mon Sep 17 00:00:00 2001 From: bwent Date: Tue, 21 Jul 2015 10:50:56 -0700 Subject: [PATCH 3/5] fix case where v not normalizable --- libraries/script-engine/src/Vec3.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/libraries/script-engine/src/Vec3.cpp b/libraries/script-engine/src/Vec3.cpp index 71b04eb321..17e7c56276 100644 --- a/libraries/script-engine/src/Vec3.cpp +++ b/libraries/script-engine/src/Vec3.cpp @@ -76,7 +76,12 @@ bool Vec3::equal(const glm::vec3& v1, const glm::vec3& v2) { } glm::vec3 Vec3::toPolar(const glm::vec3& v) { - glm::vec3 u = normalize(v); + float radius = length(v); + if (glm::abs(radius) < EPSILON) { + return glm::vec3(0.0f, 0.0f, 0.0f); + } + glm::vec3 u = v / radius; + float azimuth, elevation; azimuth = glm::asin(-u.y); @@ -109,7 +114,7 @@ glm::vec3 Vec3::fromPolar(const glm::vec3& polar) { z = 0.0f; } - return multiply(polar.z, glm::vec3(x, y, z)); + return polar.z * glm::vec3(x, y, z); } glm::vec3 Vec3::fromPolar(float azimuth, float elevation) { From 2b745395ec4b743d79dc91b84061f76bf8e61f59 Mon Sep 17 00:00:00 2001 From: bwent Date: Tue, 21 Jul 2015 11:38:10 -0700 Subject: [PATCH 4/5] use radius for z-component --- libraries/script-engine/src/Vec3.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/script-engine/src/Vec3.cpp b/libraries/script-engine/src/Vec3.cpp index 17e7c56276..26ef298493 100644 --- a/libraries/script-engine/src/Vec3.cpp +++ b/libraries/script-engine/src/Vec3.cpp @@ -95,7 +95,7 @@ glm::vec3 Vec3::toPolar(const glm::vec3& v) { elevation = 0.0f; } - return glm::vec3(azimuth, elevation, length(v)); + return glm::vec3(azimuth, elevation, radius); } glm::vec3 Vec3::fromPolar(const glm::vec3& polar) { From 5a8106a04935a34d111c9b90ba94e6fc89f107e7 Mon Sep 17 00:00:00 2001 From: bwent Date: Tue, 21 Jul 2015 13:15:24 -0700 Subject: [PATCH 5/5] fixes in edit.js UI --- examples/edit.js | 6 ++++-- libraries/script-engine/src/Vec3.cpp | 21 +++++++++++---------- libraries/script-engine/src/Vec3.h | 2 +- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/examples/edit.js b/examples/edit.js index edf60110c4..b33f47c2b7 100644 --- a/examples/edit.js +++ b/examples/edit.js @@ -1224,7 +1224,8 @@ PropertiesTool = function(opts) { entity.properties.rotation = Quat.safeEulerAngles(entity.properties.rotation); } if (entity.properties.keyLightDirection !== undefined) { - entity.properties.keyLightDirection = Vec3.toPolar(entity.properties.keyLightDirection); + entity.properties.keyLightDirection = Vec3.multiply(RADIANS_TO_DEGREES, Vec3.toPolar(entity.properties.keyLightDirection)); + entity.properties.keyLightDirection.z = 0.0; } selections.push(entity); } @@ -1254,7 +1255,8 @@ PropertiesTool = function(opts) { data.properties.rotation = Quat.fromPitchYawRollDegrees(rotation.x, rotation.y, rotation.z); } if (data.properties.keyLightDirection !== undefined) { - data.properties.keyLightDirection = Vec3.fromPolar(data.properties.keyLightDirection.x, data.properties.keyLightDirection.y); + data.properties.keyLightDirection = Vec3.fromPolar( + data.properties.keyLightDirection.x * DEGREES_TO_RADIANS, data.properties.keyLightDirection.y * DEGREES_TO_RADIANS); } 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 26ef298493..4ed16b2ef0 100644 --- a/libraries/script-engine/src/Vec3.cpp +++ b/libraries/script-engine/src/Vec3.cpp @@ -80,22 +80,23 @@ glm::vec3 Vec3::toPolar(const glm::vec3& v) { if (glm::abs(radius) < EPSILON) { return glm::vec3(0.0f, 0.0f, 0.0f); } + glm::vec3 u = v / radius; - float azimuth, elevation; + float elevation, azimuth; - azimuth = glm::asin(-u.y); - elevation = atan2(v.x, v.z); + elevation = glm::asin(-u.y); + azimuth = atan2(v.x, v.z); // Round off small decimal values - if (glm::abs(azimuth) < EPSILON) { - azimuth = 0.0f; - } if (glm::abs(elevation) < EPSILON) { elevation = 0.0f; } + if (glm::abs(azimuth) < EPSILON) { + azimuth = 0.0f; + } - return glm::vec3(azimuth, elevation, radius); + return glm::vec3(elevation, azimuth, radius); } glm::vec3 Vec3::fromPolar(const glm::vec3& polar) { @@ -113,12 +114,12 @@ glm::vec3 Vec3::fromPolar(const glm::vec3& polar) { if (glm::abs(z) < EPSILON) { z = 0.0f; } - + return polar.z * glm::vec3(x, y, z); } -glm::vec3 Vec3::fromPolar(float azimuth, float elevation) { - glm::vec3 v = glm::vec3(azimuth, elevation, 1.0f); +glm::vec3 Vec3::fromPolar(float elevation, float azimuth) { + glm::vec3 v = glm::vec3(elevation, azimuth, 1.0f); return fromPolar(v); } diff --git a/libraries/script-engine/src/Vec3.h b/libraries/script-engine/src/Vec3.h index 2b202b7a2d..82062ca80d 100644 --- a/libraries/script-engine/src/Vec3.h +++ b/libraries/script-engine/src/Vec3.h @@ -42,7 +42,7 @@ public slots: bool equal(const glm::vec3& v1, const glm::vec3& v2); glm::vec3 toPolar(const glm::vec3& v); glm::vec3 fromPolar(const glm::vec3& polar); - glm::vec3 fromPolar(float pitch, float yaw); + glm::vec3 fromPolar(float elevation, float azimuth); };