Skip to content

Commit

Permalink
Attempting to move color information into the polygons in a way that
Browse files Browse the repository at this point in the history
does not lose color information through CSG ops

#56
  • Loading branch information
madhephaestus committed Aug 11, 2024
1 parent af7d29c commit 0b338c9
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 30 deletions.
21 changes: 12 additions & 9 deletions src/main/java/eu/mihosoft/vrl/v3d/CSG.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public CSG prepForManufacturing() {
if (ret == null)
return null;
ret.setName(getName());
ret.setColor(getColor());
//ret.setColor(getColor());
ret.slicePlanes = slicePlanes;
ret.mapOfparametrics = mapOfparametrics;
ret.exportFormats = exportFormats;
Expand All @@ -207,15 +207,16 @@ public Color getColor() {
*/
public CSG setColor(Color color) {
this.color = color;
getStorage().set(PropertyStorage.PROPERTY_MATERIAL_COLOR, color.getRed()
+ " " + color.getGreen()
+ " " + color.getBlue());
for(Polygon p:polygons)
p.setColor(color);
return this;
}

public void setMeshColor(Color color) {
if (current != null) {
PhongMaterial m = new PhongMaterial(getColor());
PhongMaterial m = new PhongMaterial(color);
current.setMaterial(m);
}
return this;
}

/**
Expand Down Expand Up @@ -2207,7 +2208,7 @@ public CSG historySync(CSG dyingCSG) {
this.setParameter(vals, dyingCSG.getMapOfparametrics().get(param));
}
}
this.setColor(dyingCSG.getColor());
//this.setColor(dyingCSG.getColor());
if (getName().length() == 0)
setName(dyingCSG.getName());
return this;
Expand Down Expand Up @@ -2327,7 +2328,8 @@ public CSG setParameterNewValue(String key, double newValue) {
IParametric function = getMapOfparametrics().get(key);
if (function != null)
return function.change(this, key, new Long((long) (newValue * 1000))).setManipulator(this.getManipulator())
.setColor(this.getColor());
//.setColor(this.getColor())
;
return this;
}

Expand All @@ -2342,7 +2344,8 @@ public CSG regenerate() {
return this;
CSG regenerate2 = regenerate.regenerate(this);
if (regenerate2 != null)
return regenerate2.setManipulator(this.getManipulator()).setColor(this.getColor());
return regenerate2.setManipulator(this.getManipulator())//.setColor(this.getColor())
;
return this;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/eu/mihosoft/vrl/v3d/CSGtoJavafx.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static MeshContainer meshFromPolygon(List<Polygon> poly) {

mesh.getPoints().addAll((float) firstVertex.pos.x, (float) firstVertex.pos.y,
(float) firstVertex.pos.z);

mesh.getTexCoords().addAll(0); // texture (not covered)
mesh.getTexCoords().addAll(0);

Expand Down
11 changes: 10 additions & 1 deletion src/main/java/eu/mihosoft/vrl/v3d/Polygon.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.Collections;
import java.util.List;
import eu.mihosoft.vrl.v3d.ext.org.poly2tri.PolygonUtil;
import javafx.scene.paint.Color;

// TODO: Auto-generated Javadoc
/**
Expand Down Expand Up @@ -219,7 +220,7 @@ public Polygon clone() {
this.vertices.forEach((vertex) -> {
newVertices.add(vertex.clone());
});
return new Polygon(newVertices, getStorage(),true);
return new Polygon(newVertices, getStorage(),true).setColor(color);
}

/**
Expand Down Expand Up @@ -675,6 +676,7 @@ public boolean isValid() {

private boolean valid = true;
private boolean degenerate=false;
private Color color;


public void setDegenerate(boolean degenerate) {
Expand Down Expand Up @@ -728,5 +730,12 @@ public ArrayList<Edge> edges() {
return e;
}

public Polygon setColor(Color color) {
this.color = color;
return this;
}
public Color getColor() {
return this.color;
}

}
15 changes: 0 additions & 15 deletions src/main/java/eu/mihosoft/vrl/v3d/PropertyStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@
*/
public class PropertyStorage {

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

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

Expand All @@ -62,7 +60,6 @@ public class PropertyStorage {
* Constructor. Creates a new property storage.
*/
public PropertyStorage() {
randomColor(this);
}

/**
Expand Down Expand Up @@ -114,19 +111,7 @@ public boolean contains(String key) {
return map.containsKey(key);
}

/**
* Random color.
*
* @param storage the storage
*/
static void randomColor(PropertyStorage storage) {
Color c = colors[(int) (Math.random() * colors.length)];

storage.set(PROPERTY_MATERIAL_COLOR,
c.getRed()
+ " " + c.getGreen()
+ " " + c.getBlue());
}
public Set<String> getKeys(){
return map.keySet();
}
Expand Down
7 changes: 3 additions & 4 deletions src/test/java/eu/mihosoft/vrl/v3d/CSGTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ public class CSGTest {
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));
Color c=polygon.getColor();
return c.getRed() + " " + c.getGreen() + " " + c.getBlue();
}

@Test
Expand All @@ -39,7 +38,7 @@ public void setColor_OnUnionCSGShouldRetainColorsOnPolygons() {
.transformed(new Transform().translate(10, 0, 0));

CSG union = cube1.union(cube2);
assertEquals(Color.RED, union.getColor());
assertEquals(CSG.getDefaultColor(), union.getColor());

union.getPolygons().forEach(polygon -> {
String polygonColorAsString = getColorAsString(polygon);
Expand Down

0 comments on commit 0b338c9

Please sign in to comment.