X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.g3d%2Fsrc%2Forg%2Fsimantics%2Fg3d%2Fmath%2FMathTools.java;h=b9941c8b94a774605ed91fd55752150fe3aab88d;hb=5fcc8f4cf9d511d50cdf47b0f8152ea0f287199a;hp=d761b0ec8350450e8f45829fc8446ffb823bff01;hpb=eb67906ff85a83b2f71b823110e5c3d12da8bfc2;p=simantics%2F3d.git diff --git a/org.simantics.g3d/src/org/simantics/g3d/math/MathTools.java b/org.simantics.g3d/src/org/simantics/g3d/math/MathTools.java index d761b0ec..b9941c8b 100644 --- a/org.simantics.g3d/src/org/simantics/g3d/math/MathTools.java +++ b/org.simantics.g3d/src/org/simantics/g3d/math/MathTools.java @@ -839,10 +839,19 @@ public class MathTools { result.y = axis.y; result.z = axis.z; } else { + // Project vectors to Axis plane Vector3d p1 = projectToPlane(original, axis); Vector3d p2 = projectToPlane(rotated, axis); - double angle = p1.angle(p2); - result.set(axis,angle); + // Create vectors where z-axis is plane normal + Quat4d q = getQuat(createRotation(axis, Z_AXIS)); + Vector3d t1 = new Vector3d(); + Vector3d t2 = new Vector3d(); + rotate(q, p1, t1); + rotate(q, p2, t2); + // Calculate angles on z-axis plane. + double a1 = Math.atan2(t1.y, t1.x); + double a2 = Math.atan2(t2.y, t2.x); + result.set(axis,a2-a1); } return true; @@ -1001,4 +1010,25 @@ public class MathTools { mat.m23 = -(f+n)/(f-n); return mat; } + + /** + * Round the number to a given number of decimals, if the rounded result contains at least three + * zero trailing decimal places within the rounded result. + */ + public static double round(double value, int nsig) { + if (Math.abs(value) < NEAR_ZERO) + return 0.0; + + int decimals = (int) Math.round(Math.log10(value)); + int shift = nsig - decimals; + double multiplier = Math.pow(10.0, shift); + long roundedValue = Math.round(value * multiplier); + if (roundedValue % 1000 == 0) { + // Rounding results in at least three zeroes at the end + return roundedValue / multiplier; + } else { + // Number was not close to a shorter decimal representation, return it as is + return value; + } + } }