Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plane splitPolygon running into loop / StackOverflowError #53

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.miho</groupId>
<artifactId>JCSG</artifactId>
<version>master-SNAPSHOT</version>

<dependencies>

<dependency>
<groupId>eu.mihosoft.vvecmath</groupId>
<artifactId>vvecmath</artifactId>
<version>0.3.8</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.1</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

</dependencies>

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<!-- or whatever version you use -->
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
6 changes: 6 additions & 0 deletions src/main/java/eu/mihosoft/jcsg/Plane.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ public void splitPolygon(
types.add(type);
}

// fix for stackoverflow
if (this.normal.equals(polygon._csg_plane.normal) && polygonType!=SPANNING && polygonType!=COPLANAR) {
System.out.println("our plane is not spanning and not coplannar but it should be " + this.normal.dot(polygon._csg_plane.normal));
polygonType=COPLANAR;
}

//System.out.println("> switching");
// Put the polygon in the correct list, splitting it when necessary.
switch (polygonType) {
Expand Down
69 changes: 69 additions & 0 deletions src/test/java/eu/mihosoft/jcsg/AppleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package eu.mihosoft.jcsg;

import java.io.IOException;
import java.nio.file.Paths;

import org.junit.After;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import eu.mihosoft.jcsg.CSG;
import eu.mihosoft.jcsg.Cube;
import eu.mihosoft.jcsg.STL;
import eu.mihosoft.vvecmath.Transform;
import eu.mihosoft.vvecmath.Vector3d;

public class AppleTest {

private static final Logger LOGGER = LoggerFactory.getLogger(AppleTest.class);

String originalFilename = "src/test/resources/Apple.stl";

@After
public void tearDown() throws Exception {}

@Test
public void test() throws IOException {
CSG apple = STL.file(Paths.get(originalFilename)).optimization(CSG.OptType.POLYGON_BOUND);

// center model around zero point

double xDim = apple.getBounds().getMax().getX()-apple.getBounds().getMin().getX();
double yDim = apple.getBounds().getMax().getY()-apple.getBounds().getMin().getY();
double zDim = apple.getBounds().getMax().getZ()-apple.getBounds().getMin().getZ();

Vector3d moveToCenterVector = Vector3d.xyz(xDim/2-apple.getBounds().getMax().getX(), yDim/2-apple.getBounds().getMax().getY(), zDim/2-apple.getBounds().getMax().getZ());

LOGGER.info("center=" + apple.getBounds().getCenter().getX() + ":"
+ apple.getBounds().getCenter().getY() + ":"
+ apple.getBounds().getCenter().getZ());

LOGGER.info("max=" + apple.getBounds().getMax().getX() + ":"
+ apple.getBounds().getMax().getY() + ":"
+ apple.getBounds().getMax().getZ());

LOGGER.info("min=" + apple.getBounds().getMin().getX() + ":"
+ apple.getBounds().getMin().getY() + ":"
+ apple.getBounds().getMin().getZ());

apple = apple
.transformed(Transform.unity()
.translate(moveToCenterVector)
);

Vector3d max = apple.getBounds().getMax();
Vector3d min = apple.getBounds().getMin();

double cubeMaxDimension = Math.max(Math.max(Math.max(max.x(), Math.abs(min.x())),Math.max(max.y(), Math.abs(min.y()))),Math.max(max.z(), Math.abs(min.z()))) ;
cubeMaxDimension+=1;

CSG cube = new Cube(cubeMaxDimension*2).toCSG().
transformed(Transform.unity().
translateZ(cubeMaxDimension));

CSG diff = apple.difference(cube);

}

}
Binary file added src/test/resources/Apple.stl
Binary file not shown.