From 90a73744f36219a6998df8f06441aade59e9b3a5 Mon Sep 17 00:00:00 2001 From: Joacim Breiler Date: Fri, 9 Aug 2024 13:05:20 +0200 Subject: [PATCH] Update the storage when setting the color to CSG --- src/main/java/eu/mihosoft/vrl/v3d/CSG.java | 8 ++- .../eu/mihosoft/vrl/v3d/PropertyStorage.java | 7 +-- .../java/eu/mihosoft/vrl/v3d/CSGTest.java | 54 +++++++++++++++++++ 3 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 src/test/java/eu/mihosoft/vrl/v3d/CSGTest.java diff --git a/src/main/java/eu/mihosoft/vrl/v3d/CSG.java b/src/main/java/eu/mihosoft/vrl/v3d/CSG.java index 0504da04..903b0d0d 100644 --- a/src/main/java/eu/mihosoft/vrl/v3d/CSG.java +++ b/src/main/java/eu/mihosoft/vrl/v3d/CSG.java @@ -207,6 +207,10 @@ public Color getColor() { */ public CSG setColor(Color color) { this.color = color; + getStorage().set(PropertyStorage.PROPERTY_MATERIAL_COLOR, color.getRed() + + " " + color.getGreen() + + " " + color.getBlue()); + if (current != null) { PhongMaterial m = new PhongMaterial(getColor()); current.setMaterial(m); @@ -612,7 +616,7 @@ public CSG scaleToMeasurmentZ(Number measurment) { /** * Scaley. * - * @param scaleValue the scale value + * @param measurment the scale value * @return the csg */ public CSG scaleToMeasurmentY(Number measurment) { @@ -624,7 +628,7 @@ public CSG scaleToMeasurmentY(Number measurment) { /** * Scalex. * - * @param scaleValue the scale value + * @param measurment the scale value * @return the csg */ public CSG scaleToMeasurmentX(Number measurment) { diff --git a/src/main/java/eu/mihosoft/vrl/v3d/PropertyStorage.java b/src/main/java/eu/mihosoft/vrl/v3d/PropertyStorage.java index f707db48..5759d76a 100644 --- a/src/main/java/eu/mihosoft/vrl/v3d/PropertyStorage.java +++ b/src/main/java/eu/mihosoft/vrl/v3d/PropertyStorage.java @@ -41,7 +41,6 @@ import javafx.scene.paint.Color; -// TODO: Auto-generated Javadoc /** * A simple property storage. * @@ -49,6 +48,8 @@ */ public class PropertyStorage { + public static final String PROPERTY_MATERIAL_COLOR = "material:color"; + /** The map. */ private final Map map = new HashMap<>(); @@ -121,8 +122,8 @@ public boolean contains(String key) { static void randomColor(PropertyStorage storage) { Color c = colors[(int) (Math.random() * colors.length)]; - storage.set("material:color", - "" + c.getRed() + storage.set(PROPERTY_MATERIAL_COLOR, + c.getRed() + " " + c.getGreen() + " " + c.getBlue()); } diff --git a/src/test/java/eu/mihosoft/vrl/v3d/CSGTest.java b/src/test/java/eu/mihosoft/vrl/v3d/CSGTest.java new file mode 100644 index 00000000..2ad6d376 --- /dev/null +++ b/src/test/java/eu/mihosoft/vrl/v3d/CSGTest.java @@ -0,0 +1,54 @@ +package eu.mihosoft.vrl.v3d; + +import javafx.scene.paint.Color; +import static org.junit.Assert.assertEquals; +import org.junit.Test; + +public class CSGTest { + + public static final String BLUE_COLOR_AS_STRING = Color.BLUE.getRed() + " " + Color.BLUE.getGreen() + " " + Color.BLUE.getBlue(); + public static final String RED_COLOR_AS_STRING = Color.RED.getRed() + " " + Color.RED.getGreen() + " " + Color.RED.getBlue(); + + private static String getColorAsString(Polygon polygon) { + return polygon.getStorage().getValue(PropertyStorage.PROPERTY_MATERIAL_COLOR) + .map(Object::toString) + .orElseThrow(() -> new RuntimeException("Missing property " + PropertyStorage.PROPERTY_MATERIAL_COLOR)); + } + + @Test + public void setColor_ShouldSetColorToAllPolygons() { + CSG cube = new Cube() + .toCSG() + .setColor(Color.BLUE); + assertEquals(Color.BLUE, cube.getColor()); + + String colorAsString = Color.BLUE.getRed() + " " + Color.BLUE.getGreen() + " " + Color.BLUE.getBlue(); + cube.getPolygons().forEach(polygon -> { + String polygonColorAsString = getColorAsString(polygon); + assertEquals("Expected the polygon to get the same color as the CSG", colorAsString, polygonColorAsString); + }); + } + + @Test + public void setColor_OnUnionCSGShouldRetainColorsOnPolygons() { + CSG cube1 = new Cube(10).toCSG() + .setColor(Color.BLUE); + + CSG cube2 = new Cube(10).toCSG() + .setColor(Color.RED) + .transformed(new Transform().translate(10, 0, 0)); + + CSG union = cube1.union(cube2); + assertEquals(Color.RED, union.getColor()); + + union.getPolygons().forEach(polygon -> { + String polygonColorAsString = getColorAsString(polygon); + boolean isLeftCube = polygon.getPoints().stream().allMatch(p -> p.x <= 5); + if (isLeftCube) { + assertEquals("Expected the left cube polygons to be blue", BLUE_COLOR_AS_STRING, polygonColorAsString); + } else { + assertEquals("Expected the right cube polygons to be red", RED_COLOR_AS_STRING, polygonColorAsString); + } + }); + } +}