forked from miho/JCSG
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from breiler/polygon-colors
Update the storage when setting the color to CSG
- Loading branch information
Showing
3 changed files
with
64 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<>(); | ||
|
||
|
@@ -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()); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
} | ||
} |