Skip to content

Commit

Permalink
Merge pull request #54 from breiler/polygon-colors
Browse files Browse the repository at this point in the history
Update the storage when setting the color to CSG
  • Loading branch information
madhephaestus authored Aug 10, 2024
2 parents 208ee06 + 90a7374 commit af7d29c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/main/java/eu/mihosoft/vrl/v3d/CSG.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/eu/mihosoft/vrl/v3d/PropertyStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@

import javafx.scene.paint.Color;

// TODO: Auto-generated Javadoc
/**
* A simple property storage.
*
* @author Michael Hoffer <[email protected]>
*/
public class PropertyStorage {

public static final String PROPERTY_MATERIAL_COLOR = "material:color";

/** The map. */
private final Map<String, Object> map = new HashMap<>();

Expand Down Expand Up @@ -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());
}
Expand Down
54 changes: 54 additions & 0 deletions src/test/java/eu/mihosoft/vrl/v3d/CSGTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
});
}
}

0 comments on commit af7d29c

Please sign in to comment.