From 58229861481df73046940359e309a9e3c7bf3b51 Mon Sep 17 00:00:00 2001 From: Kevin harrington Date: Sat, 31 Aug 2024 13:44:27 -0400 Subject: [PATCH] ensure Bounds can be serialized and stored --- build.gradle | 1 + src/main/java/eu/mihosoft/vrl/v3d/Bounds.java | 8 +++++ .../java/eu/mihosoft/vrl/v3d/Vector3d.java | 29 +++++++++++++++++-- .../java/eu/mihosoft/vrl/v3d/GsonTest.java | 22 ++++++++++++++ 4 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 src/test/java/eu/mihosoft/vrl/v3d/GsonTest.java diff --git a/build.gradle b/build.gradle index 51838beb..6ea42862 100644 --- a/build.gradle +++ b/build.gradle @@ -108,6 +108,7 @@ dependencies { // triangulation algorithm implementation group: 'org.locationtech.jts', name: 'jts-core', version: '1.19.0' + implementation 'com.google.code.gson:gson:2.5' } diff --git a/src/main/java/eu/mihosoft/vrl/v3d/Bounds.java b/src/main/java/eu/mihosoft/vrl/v3d/Bounds.java index 40af0e80..55c78109 100644 --- a/src/main/java/eu/mihosoft/vrl/v3d/Bounds.java +++ b/src/main/java/eu/mihosoft/vrl/v3d/Bounds.java @@ -5,6 +5,8 @@ */ package eu.mihosoft.vrl.v3d; +import com.google.gson.annotations.Expose; + // TODO: Auto-generated Javadoc /** * Bounding box for CSGs. @@ -14,21 +16,27 @@ public class Bounds { /** The center. */ + @Expose (serialize = true, deserialize = true) private final Vector3d center; /** The bounds. */ + @Expose (serialize = true, deserialize = true) private final Vector3d bounds; /** The min. */ + @Expose (serialize = true, deserialize = true) private final Vector3d min; /** The max. */ + @Expose (serialize = true, deserialize = true) private final Vector3d max; /** The csg. */ + @Expose (serialize = false, deserialize = false) private CSG csg; /** The cube. */ + @Expose (serialize = false, deserialize = false) private Cube cube; /** diff --git a/src/main/java/eu/mihosoft/vrl/v3d/Vector3d.java b/src/main/java/eu/mihosoft/vrl/v3d/Vector3d.java index 39909f06..7c58137e 100644 --- a/src/main/java/eu/mihosoft/vrl/v3d/Vector3d.java +++ b/src/main/java/eu/mihosoft/vrl/v3d/Vector3d.java @@ -39,14 +39,32 @@ import static java.lang.Math.min; import java.util.Random; +import com.google.gson.annotations.Expose; + // TODO: Auto-generated Javadoc /** * 3D Vector3d. * * @author Michael Hoffer <info@michaelhoffer.de> */ -public class Vector3d extends javax.vecmath.Vector3d{ +public class Vector3d { + /** + * The x coordinate. + */ + @Expose (serialize = true, deserialize = true) + public double x; + + /** + * The y coordinate. + */ + @Expose (serialize = true, deserialize = true) + public double y; + /** + * The z coordinate. + */ + @Expose (serialize = true, deserialize = true) + public double z; /** * @@ -67,7 +85,14 @@ public class Vector3d extends javax.vecmath.Vector3d{ /** The Constant Z_ONE. */ public static final Vector3d Z_ONE = new Vector3d(0, 0, 1); - + /** + * Returns the length of this vector. + * @return the length of this vector + */ + public double length() + { + return Math.sqrt(this.x*this.x + this.y*this.y + this.z*this.z); + } /** * Creates a new vector. * diff --git a/src/test/java/eu/mihosoft/vrl/v3d/GsonTest.java b/src/test/java/eu/mihosoft/vrl/v3d/GsonTest.java new file mode 100644 index 00000000..bdb81e9d --- /dev/null +++ b/src/test/java/eu/mihosoft/vrl/v3d/GsonTest.java @@ -0,0 +1,22 @@ +package eu.mihosoft.vrl.v3d; + +import java.lang.reflect.Type; + +import org.junit.Test; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.reflect.TypeToken; + +public class GsonTest { + @Test + public void runGson() { + Type TT_CaDoodleFile = new TypeToken() { + }.getType(); + Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation() + .create(); + Bounds b = new Cube(100).toCSG().getBounds(); + String json = gson.toJson(b); + System.out.println(json); + } +}