From 5fcc8f4cf9d511d50cdf47b0f8152ea0f287199a Mon Sep 17 00:00:00 2001 From: Reino Ruusu Date: Thu, 5 Dec 2019 16:21:33 +0200 Subject: [PATCH] Eliminate rounding errors in property tabs. gitlab #64 Change-Id: I8bb9978c99a612d841c41645c0a89ab381557b79 (cherry picked from commit 7679dd7b19b7f8c74bd57df3f5800ac98790cdaf) --- .../src/org/simantics/g3d/math/MathTools.java | 21 +++++++++++++++++++ .../DoubleArrayPropertyManipulator.java | 4 +++- .../DoubleArrayPropertyManipulator2.java | 4 +++- .../property/DoublePropertyManipulator.java | 7 ++++++- .../g3d/property/QuatPropertyManipulator.java | 2 +- .../property/VectorPropertyManipulator.java | 8 ++++--- 6 files changed, 39 insertions(+), 7 deletions(-) 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 aa8b0551..b9941c8b 100644 --- a/org.simantics.g3d/src/org/simantics/g3d/math/MathTools.java +++ b/org.simantics.g3d/src/org/simantics/g3d/math/MathTools.java @@ -1010,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; + } + } } diff --git a/org.simantics.g3d/src/org/simantics/g3d/property/DoubleArrayPropertyManipulator.java b/org.simantics.g3d/src/org/simantics/g3d/property/DoubleArrayPropertyManipulator.java index a9971605..e0148147 100644 --- a/org.simantics.g3d/src/org/simantics/g3d/property/DoubleArrayPropertyManipulator.java +++ b/org.simantics.g3d/src/org/simantics/g3d/property/DoubleArrayPropertyManipulator.java @@ -13,6 +13,8 @@ package org.simantics.g3d.property; import java.util.Arrays; +import org.simantics.g3d.math.MathTools; + public class DoubleArrayPropertyManipulator implements PropertyManipulator { ValueProvider provider; @@ -52,7 +54,7 @@ public class DoubleArrayPropertyManipulator implements PropertyManipulator { try { double[] val = getValue(); if (val == null) return null; - return Arrays.toString(val); + return Double.toString(MathTools.round(val[i], 10)); } catch (Exception e) { return null; } diff --git a/org.simantics.g3d/src/org/simantics/g3d/property/DoubleArrayPropertyManipulator2.java b/org.simantics.g3d/src/org/simantics/g3d/property/DoubleArrayPropertyManipulator2.java index 6b7a2e11..9275bfea 100644 --- a/org.simantics.g3d/src/org/simantics/g3d/property/DoubleArrayPropertyManipulator2.java +++ b/org.simantics.g3d/src/org/simantics/g3d/property/DoubleArrayPropertyManipulator2.java @@ -11,6 +11,8 @@ *******************************************************************************/ package org.simantics.g3d.property; +import org.simantics.g3d.math.MathTools; + public class DoubleArrayPropertyManipulator2 implements PropertyManipulator { ValueProvider provider; @@ -57,7 +59,7 @@ public class DoubleArrayPropertyManipulator2 implements PropertyManipulator { return "New"; if (val.length < i) return null; - return Double.toString(val[i]); + return Double.toString(MathTools.round(val[i], 10)); } catch (Exception e) { return null; } diff --git a/org.simantics.g3d/src/org/simantics/g3d/property/DoublePropertyManipulator.java b/org.simantics.g3d/src/org/simantics/g3d/property/DoublePropertyManipulator.java index 4f7c670f..46f892f3 100644 --- a/org.simantics.g3d/src/org/simantics/g3d/property/DoublePropertyManipulator.java +++ b/org.simantics.g3d/src/org/simantics/g3d/property/DoublePropertyManipulator.java @@ -11,6 +11,8 @@ *******************************************************************************/ package org.simantics.g3d.property; +import org.simantics.g3d.math.MathTools; + public class DoublePropertyManipulator implements PropertyManipulator { ValueProvider provider; @@ -43,6 +45,8 @@ public class DoublePropertyManipulator implements PropertyManipulator { try { Object value = provider.getValue(input); if (value == null) return null; + if (value instanceof Double) + return Double.toString(MathTools.round((Double)value, 10)); return value.toString(); } catch (Exception e) { return null; @@ -70,7 +74,8 @@ public class DoublePropertyManipulator implements PropertyManipulator { editMode = b; if (editMode) { try { - editValue = provider.getValue(input).toString(); + Object value = provider.getValue(input); + editValue = value.toString(); } catch (Exception e) { } diff --git a/org.simantics.g3d/src/org/simantics/g3d/property/QuatPropertyManipulator.java b/org.simantics.g3d/src/org/simantics/g3d/property/QuatPropertyManipulator.java index 2104fbca..8c335992 100644 --- a/org.simantics.g3d/src/org/simantics/g3d/property/QuatPropertyManipulator.java +++ b/org.simantics.g3d/src/org/simantics/g3d/property/QuatPropertyManipulator.java @@ -158,7 +158,7 @@ public class QuatPropertyManipulator implements PropertyManipulator { default: break; } - return Double.toString(d); + return Double.toString(MathTools.round(d, 10)); } catch (Exception e) { return null; } diff --git a/org.simantics.g3d/src/org/simantics/g3d/property/VectorPropertyManipulator.java b/org.simantics.g3d/src/org/simantics/g3d/property/VectorPropertyManipulator.java index e0a80219..47587fbc 100644 --- a/org.simantics.g3d/src/org/simantics/g3d/property/VectorPropertyManipulator.java +++ b/org.simantics.g3d/src/org/simantics/g3d/property/VectorPropertyManipulator.java @@ -13,6 +13,8 @@ package org.simantics.g3d.property; import javax.vecmath.Vector3d; +import org.simantics.g3d.math.MathTools; + public class VectorPropertyManipulator implements PropertyManipulator { ValueProvider provider; @@ -49,11 +51,11 @@ public class VectorPropertyManipulator implements PropertyManipulator { if (v == null) return null; if (i == 0) - return Double.toString(v.x); + return Double.toString(MathTools.round(v.x, 10)); if (i == 1) - return Double.toString(v.y); + return Double.toString(MathTools.round(v.y, 10)); if (i == 2) - return Double.toString(v.z); + return Double.toString(MathTools.round(v.z, 10)); return null; } catch (Exception e) { return null; -- 2.47.1