Skip to content

Commit

Permalink
Merge pull request #59 from breiler/copy-color-on-difference
Browse files Browse the repository at this point in the history
Copy the color of the polygons on difference operations
  • Loading branch information
madhephaestus authored Aug 17, 2024
2 parents 61c0dca + bd2fa17 commit 830f0ae
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 33 deletions.
32 changes: 11 additions & 21 deletions src/main/java/eu/mihosoft/vrl/v3d/CSG.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -708,19 +709,11 @@ public static CSG fromPolygons(PropertyStorage storage, Polygon... polygons) {
@Override
public CSG clone() {
CSG csg = new CSG();

csg.setOptType(this.getOptType());

Stream<Polygon> polygonStream;

// if (getPolygons().size() > 200) {
// polygonStream = getPolygons().parallelStream();
// } else {
// polygonStream =getPolygons().stream();
// }
polygonStream=getPolygons().stream();
csg.setPolygons(polygonStream.map((Polygon p) -> p!=null?p.clone():null).filter(p-> p!=null).collect(Collectors.toList()));

csg.setPolygons(polygons.stream()
.filter(Objects::nonNull)
.map(Polygon::clone)
.collect(Collectors.toList()));
return csg.historySync(this);
}

Expand Down Expand Up @@ -1213,15 +1206,13 @@ public CSG difference(CSG csg) {
* @return the csg
*/
private CSG _differenceCSGBoundsOpt(CSG csg) {
CSG b = csg;

CSG a1 = this._differenceNoOpt(csg.getBounds().toCSG());
CSG a2 = this.intersect(csg.getBounds().toCSG());
CSG BACK = a2._differenceNoOpt(b)._unionIntersectOpt(a1).optimization(getOptType());
CSG a1 = this._differenceNoOpt(csg.getBounds().toCSG().setColor(csg.getColor()));
CSG a2 = this.intersect(csg.getBounds().toCSG().setColor(csg.getColor()));
CSG result = a2._differenceNoOpt(csg)._unionIntersectOpt(a1).optimization(getOptType());
if (getName().length() != 0 && csg.getName().length() != 0) {
BACK.setName( name);
result.setName( name);
}
return BACK;
return result;
}

/**
Expand Down Expand Up @@ -1937,7 +1928,7 @@ public double getTotalZ() {
*
* @return the optType
*/
private OptType getOptType() {
protected OptType getOptType() {
return optType != null ? optType : defaultOptType;
}

Expand Down Expand Up @@ -2208,7 +2199,6 @@ public CSG historySync(CSG dyingCSG) {
this.setParameter(vals, dyingCSG.getMapOfparametrics().get(param));
}
}
//this.setColor(dyingCSG.getColor());
if (getName().length() == 0)
setName(dyingCSG.getName());
return this;
Expand Down
15 changes: 4 additions & 11 deletions src/main/java/eu/mihosoft/vrl/v3d/Plane.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import java.util.ArrayList;
import java.util.List;

// TODO: Auto-generated Javadoc
/**
* Represents a plane in 3D space.
*
Expand Down Expand Up @@ -170,7 +169,6 @@ public void splitPolygon(
for (int i = 0; i < polygon.vertices.size(); i++) {
double t = this.normal.dot(polygon.vertices.get(i).pos) - this.dist;
int type = (t < negEpsilon) ? BACK : (t > posEpsilon) ? FRONT : COPLANAR;
//polygonType |= type;
if(type==BACK)
somePointsInBack=true;
if(type==FRONT)
Expand All @@ -184,23 +182,18 @@ else if(somePointsInBack) {
}else if(somePointsInfront)
polygonType=FRONT;

//System.out.println("> switching");
// Put the polygon in the correct list, splitting it when necessary.
switch (polygonType) {
case COPLANAR:
//System.out.println(" -> coplanar");
(this.normal.dot(polygon.plane.normal) > 0 ? coplanarFront : coplanarBack).add(polygon);
break;
case FRONT:
//System.out.println(" -> front");
front.add(polygon);
break;
case BACK:
//System.out.println(" -> back");
back.add(polygon);
break;
case SPANNING:
//System.out.println(" -> spanning");
List<Vertex> f = new ArrayList<>();
List<Vertex> b = new ArrayList<>();
for (int i = 0; i < polygon.vertices.size(); i++) {
Expand All @@ -224,13 +217,13 @@ else if(somePointsInBack) {
}
}
if (f.size() >= 3) {
front.add(new Polygon(f, polygon.getStorage()));
}else {
front.add(new Polygon(f, polygon.getStorage()).setColor(polygon.getColor()));
} else {
System.out.println("Front Clip Fault!");
}
if (b.size() >= 3) {
back.add(new Polygon(b, polygon.getStorage()));
}else {
back.add(new Polygon(b, polygon.getStorage()).setColor(polygon.getColor()));
} else {
System.out.println("Back Clip Fault!");
}
break;
Expand Down
72 changes: 71 additions & 1 deletion src/test/java/eu/mihosoft/vrl/v3d/CSGTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import javafx.scene.paint.Color;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.junit.Test;

public class CSGTest {
Expand Down Expand Up @@ -85,11 +86,80 @@ public void setColor_OnUnionedCSGShouldChangeColorsOfAllPolygons() {
.transformed(new Transform().translate(10, 0, 0));

CSG union = cube1.union(cube2);
assertEquals("Expected the new object to inherit the color from the latest unioned object", CSG.getDefaultColor(), union.getColor());
assertEquals("Expected the new object to get the default color", CSG.getDefaultColor(), union.getColor());

union.setColor(Color.BLUE);
union.getPolygons().forEach(polygon -> {
assertEquals("Expected the cube polygons to be another color", Color.BLUE, polygon.getColor());
});
}

@Test
public void setColor_OnDifferenceWithOptTypeBoundsShouldChangeColorsOfIntersectingPolygons() {
CSG cube1 = new Cube(10).toCSG()
.move(0, 0, 0)
.setColor(Color.BLUE);

CSG cube2 = new Cube(10).toCSG()
.move(9, 0, 0)
.setColor(Color.RED);

assertEquals(CSG.OptType.CSG_BOUND, cube1.getOptType());

CSG difference = cube1.difference(cube2);
assertEquals("Unexpected number of faces", 6, difference.getPolygons().size());
difference.getPolygons().forEach(polygon -> {
Vector3d center = polygon.getBounds().getCenter();
if (center.equals(Vector3d.xyz(-5, 0, 0))) {
assertEquals("Expected the left face to have another color", Color.BLUE, polygon.getColor());
} else if (center.equals(Vector3d.xyz(-0.5, -5, 0))) {
assertEquals("Expected the front face to have another color", Color.BLUE, polygon.getColor());
} else if (center.equals(Vector3d.xyz(-0.5, 5, 0))) {
assertEquals("Expected the back face to have another color", Color.BLUE, polygon.getColor());
} else if (center.equals(Vector3d.xyz(-0.5, 0, -5))) {
assertEquals("Expected the bottom face to have another color", Color.BLUE, polygon.getColor());
} else if (center.equals(Vector3d.xyz(-0.5, 0, 5))) {
assertEquals("Expected the top face to have another color", Color.BLUE, polygon.getColor());
} else if (center.equals(Vector3d.xyz(4, 0, 0))) {
assertEquals("Expected the right face to have another color", Color.RED, polygon.getColor());
} else {
fail("Unknown face with center" + center);
}
});
}

@Test
public void setColor_OnDifferenceWithOptTypePolygonShouldChangeColorsOfIntersectingPolygons() {
CSG cube1 = new Cube(10).toCSG()
.move(0, 0, 0)
.setColor(Color.BLUE)
.setOptType(CSG.OptType.POLYGON_BOUND);

CSG cube2 = new Cube(10).toCSG()
.move(9, 0, 0)
.setColor(Color.RED);

assertEquals(CSG.OptType.POLYGON_BOUND, cube1.getOptType());

CSG difference = cube1.difference(cube2);
assertEquals("Unexpected number of faces", 6, difference.getPolygons().size());
difference.getPolygons().forEach(polygon -> {
Vector3d center = polygon.getBounds().getCenter();
if (center.equals(Vector3d.xyz(-5, 0, 0))) {
assertEquals("Expected the left face to have another color", Color.BLUE, polygon.getColor());
} else if (center.equals(Vector3d.xyz(-0.5, -5, 0))) {
assertEquals("Expected the front face to have another color", Color.BLUE, polygon.getColor());
} else if (center.equals(Vector3d.xyz(-0.5, 5, 0))) {
assertEquals("Expected the back face to have another color", Color.BLUE, polygon.getColor());
} else if (center.equals(Vector3d.xyz(-0.5, 0, -5))) {
assertEquals("Expected the bottom face to have another color", Color.BLUE, polygon.getColor());
} else if (center.equals(Vector3d.xyz(-0.5, 0, 5))) {
assertEquals("Expected the top face to have another color", Color.BLUE, polygon.getColor());
} else if (center.equals(Vector3d.xyz(4, 0, 0))) {
assertEquals("Expected the right face to have another color", Color.RED, polygon.getColor());
} else {
fail("Unknown face with center" + center);
}
});
}
}

0 comments on commit 830f0ae

Please sign in to comment.