From 83012a66aa980c69a5ad1e1c3deea00d96924e47 Mon Sep 17 00:00:00 2001 From: megacz Date: Sat, 28 Feb 2009 15:10:33 -0800 Subject: [PATCH] refactor MarchingCubes to use only a single static method (never instantiated) --- src/edu/berkeley/qfat/voxel/MarchingCubes.java | 189 +++++++++++++----------- 1 file changed, 106 insertions(+), 83 deletions(-) diff --git a/src/edu/berkeley/qfat/voxel/MarchingCubes.java b/src/edu/berkeley/qfat/voxel/MarchingCubes.java index cccbecf..bda9531 100644 --- a/src/edu/berkeley/qfat/voxel/MarchingCubes.java +++ b/src/edu/berkeley/qfat/voxel/MarchingCubes.java @@ -23,68 +23,39 @@ import edu.berkeley.qfat.geom.*; // ////////////////////////////////////////////////////////////////////////////////////// -public abstract class MarchingCubes { - - private int iEdgeFlags; +public class MarchingCubes { private static class GLvector { public double fX; public double fY; - public double fZ; - }; - - GLvector sSourcePoint[] = new GLvector[3]; - - public abstract double fSample(double x, double y, double z); - - //vGetNormal() finds the gradient of the scalar field at a point - //This gradient can be used as a very accurate vertx normal for lighting calculations - void vGetNormal(GLvector rfNormal, double fX, double fY, double fZ) { - rfNormal.fX = fSample(fX-0.01, fY, fZ) - fSample(fX+0.01, fY, fZ); - rfNormal.fY = fSample(fX, fY-0.01, fZ) - fSample(fX, fY+0.01, fZ); - rfNormal.fZ = fSample(fX, fY, fZ-0.01) - fSample(fX, fY, fZ+0.01); - vNormalizeVector(rfNormal, rfNormal); - } - - void vNormalizeVector(GLvector rfVectorResult, GLvector rfVectorSource) { - double fOldLength; - double fScale; - - fOldLength = Math.sqrt( (rfVectorSource.fX * rfVectorSource.fX) + - (rfVectorSource.fY * rfVectorSource.fY) + - (rfVectorSource.fZ * rfVectorSource.fZ) ); - - if(fOldLength == 0.0) { - rfVectorResult.fX = rfVectorSource.fX; - rfVectorResult.fY = rfVectorSource.fY; - rfVectorResult.fZ = rfVectorSource.fZ; - } else { - fScale = 1.0/fOldLength; - rfVectorResult.fX = rfVectorSource.fX*fScale; - rfVectorResult.fY = rfVectorSource.fY*fScale; - rfVectorResult.fZ = rfVectorSource.fZ*fScale; + public double fZ; + public String toString() { + return "("+fX+","+fY+","+fZ+")"; } - } - - // fGetOffset finds the approximate point of intersection of the surface - // between two points with the values fValue1 and fValue2 - double fGetOffset(double fValue1, double fValue2, double fValueDesired) { - double fDelta = fValue2 - fValue1; - if(fDelta == 0.0) return 0.5; - return (fValueDesired - fValue1)/fDelta; - } + }; - // vMarchingCubes iterates over the entire dataset, calling vMarchCube on each cube - void vMarchingCubes(Mesh mesh, double targetValue, int iDataSetSize, int fStepSize) { + /** march iterates over the entire dataset, calling vMarchCube on each cube */ + public static void march(SampledField sampledField, double targetValue, int iDataSetSize, double fStepSize, Mesh mesh) { int iX, iY, iZ; - for(iX = 0; iX < iDataSetSize; iX++) + int initialTriangles = mesh.numTriangles(); + for(iX = 0; iX < iDataSetSize; iX++) { + System.out.print("\r"); + for(int i=0; i<78; i++) System.out.print(' '); + System.out.print("\r"); + System.out.print(Math.ceil((iX/((double)iDataSetSize))*100) + "% marched, " + + (mesh.numTriangles()-initialTriangles) + " triangles"); for(iY = 0; iY < iDataSetSize; iY++) for(iZ = 0; iZ < iDataSetSize; iZ++) - vMarchCube(mesh, targetValue, iX*fStepSize, iY*fStepSize, iZ*fStepSize, fStepSize); + march(sampledField, mesh, targetValue, iX*fStepSize, iY*fStepSize, iZ*fStepSize, fStepSize); + } + System.out.print("\r"); + for(int i=0; i<78; i++) System.out.print(' '); + System.out.print("\r"); + System.out.println("done marching."); } - //vMarchCube performs the Marching Cubes algorithm on a single cube - void vMarchCube(Mesh mesh, double targetValue, double fX, double fY, double fZ, double fScale) { + /** performs the Marching Cubes algorithm on a single cube */ + static void march(SampledField sampledField, Mesh mesh, double targetValue, double fX, double fY, double fZ, double fScale) { int iCorner, iVertex, iVertexTest, iEdge, iTriangle, iFlagIndex, iEdgeFlags; double fOffset; GLvector sColor; @@ -92,39 +63,42 @@ public abstract class MarchingCubes { GLvector asEdgeVertex[] = new GLvector[12]; GLvector asEdgeNorm[] = new GLvector[12]; - //Make a local copy of the values at the cube's corners + for(int i=0; i