Skip to content

Instantly share code, notes, and snippets.

@Octogonapus
Last active February 21, 2018 21:12
Show Gist options
  • Save Octogonapus/221961cef019143deaf025f6611497a0 to your computer and use it in GitHub Desktop.
Save Octogonapus/221961cef019143deaf025f6611497a0 to your computer and use it in GitHub Desktop.
Vertex quantization test
@Grab(group='org.codehaus.gpars', module='gpars', version='1.2.1')
import static groovyx.gpars.GParsPool.withPool
CSG getEllipse(double xRadius, double yRadius, double z) {
List<Vector3d> points = [];
for (double i = 0; i < 2 * Math.PI; i += 0.01) {
points.add(new Vector3d((xRadius*yRadius*Math.cos(i)) / Math.sqrt((Math.pow((yRadius*Math.cos(i)), 2) + Math.pow((xRadius*Math.sin(i)), 2))),
(xRadius*yRadius*Math.sin(i)) / Math.sqrt((Math.pow((yRadius*Math.cos(i)), 2) + Math.pow((xRadius*Math.sin(i)), 2))),
0));
}
return Extrude.points(new Vector3d(0, 0, z), points);
}
CSG unopt = getEllipse(8, 4, 5); //new Cube(50, 50, 50).toCSG();
// for (double i = 0; i < 10; i += 0.01) {
// unopt = unopt.union(new Cube(50 + i, 30, 30).toCSG());
// }
CSG opt = unopt.clone();
def arePolysEqual(Polygon poly1, Polygon poly2) {
def verts1 = poly1.vertices;
def verts2 = poly2.vertices;
if (verts1.size != verts2.size)
return 1;
//TODO: Not sure if we need to compare a normals or not
// if (areVector3dEqual(poly1.plane.normal, poly2.plane.normal)) {
// return 1;
// }
boolean foundFirstMatch = false;
int startIndex = 0;
Vertex v = verts1[0];
for (; startIndex < verts2.size; startIndex++) {
if (areVertsEqual(v, verts2[startIndex])) {
foundFirstMatch = true;
break;
}
}
if (!foundFirstMatch) {
return 1;
}
for (int i = 0; i < verts1.size; i++) {
if (!areVertsEqual(verts1[i], verts2[(i + startIndex) % verts2.size])) {
return 1;
}
}
return 0;
}
boolean areVertsEqual(Vertex vert1, Vertex vert2) {
def pos1 = vert1.pos;
def pos2 = vert2.pos;
return areVector3dEqual(pos1, pos2);
}
boolean areVector3dEqual(Vector3d vec1, Vector3d vec2) {
return vec1.x == vec2.x && vec1.y == vec2.y && vec1.z == vec2.z;
}
Polygon quantizeIt(Polygon it) {
double quanAmount = 0.05;
List<Vertex> qVerts = [];
for (Vertex v : it.vertices) {
Vector3d pos = v.pos;
pos.x = Math.round(pos.x / quanAmount) * quanAmount;
pos.y = Math.round(pos.y / quanAmount) * quanAmount;
pos.z = Math.round(pos.z / quanAmount) * quanAmount;
qVerts.add(new Vector3d(pos.x, pos.y, pos.z));
}
return Polygon.fromPoints(qVerts);
}
List<Polygon> polys = opt.getPolygons();
def startTime = System.currentTimeMillis();
List<Polygon> qPolys = withPool(12) {
polys.collectParallel{quantizeIt(it)}
}
def endTime = System.currentTimeMillis();
println "Before dedup: " + qPolys.size()
qPolys.unique {
poly1, poly2 -> arePolysEqual(poly1, poly2)
}
println "After dedup: " + qPolys.size()
println "Took: " + (endTime - startTime);
CSG out = CSG.fromPolygons(qPolys);
out.setName("quantized");
return out;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment