public static void GenerateSimpleMesh(IVolumeScene scene, Vector3 origin, float sceneCubeVolSize, int numCubes, bool adaptive, out int[] triangles, out Vector3[] vertices, out Color[] colours) { // list all cubes / points in volume space PointBind[] points = new PointBind[numCubes * numCubes * numCubes]; float spacing = sceneCubeVolSize / (float)numCubes; // scan for points float startX = origin.X - (sceneCubeVolSize / 2f); float startY = origin.Y - (sceneCubeVolSize / 2f); float startZ = origin.Z - (sceneCubeVolSize / 2f); float x, y, z = startZ; int index = 0; for (int zPointIdx = 0; zPointIdx < numCubes; zPointIdx++) { x = startX; for (int xPointIdx = 0; xPointIdx < numCubes; xPointIdx++) { y = startY; for (int yPointIdx = 0; yPointIdx < numCubes; yPointIdx++) { Vector3 point = new Vector3(x, y, z); float potential = scene.GetPotentialAtPoint(point); //if (potential > 0.1f) points[index] = new PointBind(point, potential); y += spacing; index++; } x += spacing; } z += spacing; } // map cubes index = 0; List<Cube> cubes = new List<Cube>(); Vector3[] cPoints = new Vector3[8]; float[] cPotentials = new float[8]; int[] indices = new int[8]; int layerSz = numCubes * numCubes; for (int zPointIdx = 0; zPointIdx < numCubes - 1; zPointIdx++) { for (int xPointIdx = 0; xPointIdx < numCubes - 1; xPointIdx++) { for (int yPointIdx = 0; yPointIdx < numCubes - 1; yPointIdx++) { // create cube indices[0] = index; indices[1] = index + 1; indices[2] = index + numCubes + 1; indices[3] = index + numCubes; indices[4] = index + layerSz; indices[5] = index + layerSz + 1; indices[6] = index + layerSz + 1 + numCubes; indices[7] = index + layerSz + numCubes; int numPoints = 0; int numVPoints = 0; for (int i = 0; i < 8; i++) { PointBind value = points[indices[i]]; //if (points.TryGetValue(indices[i], out value)) //{ cPoints[i] = value.Position; cPotentials[i] = value.Potential; numPoints++; if (value.Potential >= 0.1f) numVPoints++; //} //else //{ // cPotentials[i] = float.NaN; //} } if (numPoints > 0 && numVPoints > 0) { cubes.Add(new Cube(cPoints, cPotentials, zPointIdx, xPointIdx, yPointIdx)); cPotentials = new float[8]; cPoints = new Vector3[8]; // TODO: Switch to indices // calc normals/directions they can move for each point in cube // based on side-plane matching/analysis } index++; } index++; // push on to next line } index += numCubes; } Cube[] cubeArray = cubes.ToArray(); // modify cubes for adaptive points if (adaptive) ApplyAdaptiveProcess(scene, cubeArray, 1, sceneCubeVolSize); // transform into triangles MarchingCubes.PolygoniseCubes(cubeArray, 0.1f, (sceneCubeVolSize / (float)numCubes) / 2f, out triangles, out vertices); // colourize all vertices colours = new Color[vertices.Length]; for (int vert = 0; vert < vertices.Length; vert++) { colours[vert] = scene.ColourizePoint(vertices[vert]); } }
public static void GenerateSimpleMesh(IVolumeScene scene, Vector3 origin, float sceneCubeVolSize, int numCubes, bool adaptive, out int[] triangles, out Vector3[] vertices, out Color[] colours) { // list all cubes / points in volume space PointBind[] points = new PointBind[numCubes * numCubes * numCubes]; float spacing = sceneCubeVolSize / (float)numCubes; // scan for points float startX = origin.X - (sceneCubeVolSize / 2f); float startY = origin.Y - (sceneCubeVolSize / 2f); float startZ = origin.Z - (sceneCubeVolSize / 2f); float x, y, z = startZ; int index = 0; for (int zPointIdx = 0; zPointIdx < numCubes; zPointIdx++) { x = startX; for (int xPointIdx = 0; xPointIdx < numCubes; xPointIdx++) { y = startY; for (int yPointIdx = 0; yPointIdx < numCubes; yPointIdx++) { Vector3 point = new Vector3(x, y, z); float potential = scene.GetPotentialAtPoint(point); //if (potential > 0.1f) points[index] = new PointBind(point, potential); y += spacing; index++; } x += spacing; } z += spacing; } // map cubes index = 0; List <Cube> cubes = new List <Cube>(); Vector3[] cPoints = new Vector3[8]; float[] cPotentials = new float[8]; int[] indices = new int[8]; int layerSz = numCubes * numCubes; for (int zPointIdx = 0; zPointIdx < numCubes - 1; zPointIdx++) { for (int xPointIdx = 0; xPointIdx < numCubes - 1; xPointIdx++) { for (int yPointIdx = 0; yPointIdx < numCubes - 1; yPointIdx++) { // create cube indices[0] = index; indices[1] = index + 1; indices[2] = index + numCubes + 1; indices[3] = index + numCubes; indices[4] = index + layerSz; indices[5] = index + layerSz + 1; indices[6] = index + layerSz + 1 + numCubes; indices[7] = index + layerSz + numCubes; int numPoints = 0; int numVPoints = 0; for (int i = 0; i < 8; i++) { PointBind value = points[indices[i]]; //if (points.TryGetValue(indices[i], out value)) //{ cPoints[i] = value.Position; cPotentials[i] = value.Potential; numPoints++; if (value.Potential >= 0.1f) { numVPoints++; } //} //else //{ // cPotentials[i] = float.NaN; //} } if (numPoints > 0 && numVPoints > 0) { cubes.Add(new Cube(cPoints, cPotentials, zPointIdx, xPointIdx, yPointIdx)); cPotentials = new float[8]; cPoints = new Vector3[8]; // TODO: Switch to indices // calc normals/directions they can move for each point in cube // based on side-plane matching/analysis } index++; } index++; // push on to next line } index += numCubes; } Cube[] cubeArray = cubes.ToArray(); // modify cubes for adaptive points if (adaptive) { ApplyAdaptiveProcess(scene, cubeArray, 1, sceneCubeVolSize); } // transform into triangles MarchingCubes.PolygoniseCubes(cubeArray, 0.1f, (sceneCubeVolSize / (float)numCubes) / 2f, out triangles, out vertices); // colourize all vertices colours = new Color[vertices.Length]; for (int vert = 0; vert < vertices.Length; vert++) { colours[vert] = scene.ColourizePoint(vertices[vert]); } }