public override Vector4 getEdgeData(Point3 cube, int edgeId) { var offsetPos = cube - offset; if (offsetPos.X < 0 || offsetPos.Y < 0 || offsetPos.Z < 0 || offsetPos.X >= b.Dimensions.X || offsetPos.Y >= b.Dimensions.Y || offsetPos.Z >= b.Dimensions.Z) { return(a.getEdgeData(cube, edgeId)); } if (!a.HasEdgeData(cube, edgeId)) { return(b.getEdgeData(offsetPos, edgeId)); } if (!b.HasEdgeData(offsetPos, edgeId)) { return(a.getEdgeData(cube, edgeId)); } var da = a.getEdgeData(cube, edgeId).W; var db = b.getEdgeData(offsetPos, edgeId).W; float diff; if (GetSign(cube)) { diff = da - db; // 0 is not air, so if diff > 0 then b is closer } else { diff = db - da; // 0 is air, so if diff > 0 then b is closer } return(diff > 0 ? b.getEdgeData(offsetPos, edgeId) : a.getEdgeData(cube, edgeId)); }
private static void buildTriangleIndices(List <int> indices, AbstractHermiteGrid grid, Dictionary <Point3, int> vIndex, List <DCVoxelMaterial> triangleMaterials) { // Possible quads var offsets = new[] { Point3.UnitX(), Point3.UnitY(), Point3.UnitZ(), }; var rights = new[] { Point3.UnitY(), Point3.UnitZ(), Point3.UnitX(), }; var ups = new[] { Point3.UnitZ(), Point3.UnitX(), Point3.UnitY(), }; var unitEdges = offsets.Select(o => grid.GetEdgeId(new Point3(), o)).ToArray(); //TODO: should be unit test for (int i = 0; i < 3; i++) { Debug.Assert(grid.GetEdgeOffsets(unitEdges[i])[0] == new Point3()); Debug.Assert(grid.GetEdgeOffsets(unitEdges[i])[1] == offsets[i]); } grid.ForEachCube(o => { if (!vIndex.ContainsKey(o)) { return; // No sign changes so no relevant edges here } for (int i = 0; i < 3; i++) { var edgeId = unitEdges[i]; if (!grid.HasEdgeData(o, edgeId)) { continue; } // Generate quad var right = rights[i]; var up = ups[i]; DCVoxelMaterial mat; var signs = grid.GetEdgeSigns(o, edgeId); // Face towards air by swapping right and up if (signs[1]) { var swap = right; right = up; up = swap; mat = grid.GetMaterial(o + offsets[i]); } else { mat = grid.GetMaterial(o); } // build quad faces var a = o - right; var b = o - up; var ab = o - right - up; if (!new[] { a, b, ab }.All(vIndex.ContainsKey)) { continue; // This should never happen unless on the side of the field, maybe add a check for this? } indices.AddRange(new[] { vIndex[o], vIndex[a], vIndex[ab] }); indices.AddRange(new[] { vIndex[o], vIndex[ab], vIndex[b] }); triangleMaterials.Add(mat); triangleMaterials.Add(mat); } }); }
public static HermiteDataGrid CopyGrid(AbstractHermiteGrid grid) { var ret = new HermiteDataGrid(); Point3 storageSize = grid.Dimensions + new Point3(1, 1, 1); ret.cells = new Array3D <Vertex>(storageSize); for (int x = 0; x < storageSize.X; x++) { for (int y = 0; y < storageSize.Y; y++) { for (int z = 0; z < storageSize.Z; z++) { var p = new Point3(x, y, z); ret.cells[p] = new Vertex() { Sign = grid.GetSign(p), Material = grid.GetMaterial(p) /*EdgeData = ret.dirs.Select(dir => * { * var edgeId = grid.GetEdgeId(p, p + dir); * return grid.HasEdgeData(p, edgeId) ? grid.getEdgeData(p, edgeId) : new Vector4(); * }).ToArray()*/ }; } } } //ret.ForEachGridPoint(p => // { // ret.cells[p] = new Vertex() // { // Sign = grid.GetSign(p), // /*EdgeData = ret.dirs.Select(dir => // { // var edgeId = grid.GetEdgeId(p, p + dir); // return grid.HasEdgeData(p, edgeId) ? grid.getEdgeData(p, edgeId) : new Vector4(); // }).ToArray()*/ // }; // }); var dirs = ret.dirs; var dirEdges = dirs.Select(i => ret.GetEdgeId(new Point3(), i)).ToArray(); ret.ForEachGridPoint(p => { var gridPointP = ret.cells.GetFast(p.X, p.Y, p.Z); gridPointP.EdgeData = new Vector4[3]; for (int i = 0; i < 3; i++) { var dir = dirs[i]; var edgeId = dirEdges[i]; Point3 endPoint = p + dir; // Optimization: direclty read from already constructed data if (!ret.cells.InArray(endPoint)) { continue; } if (gridPointP.Sign == ret.cells.GetFast(endPoint.X, endPoint.Y, endPoint.Z).Sign) { continue; } if (!grid.HasEdgeData(p, edgeId)) { // This can normally not happen, since we check if there is a sign difference by looking at the already evaluated density points. // If this would be true there is some problem with the manual determining of the existence of an edge. //throw new InvalidOperationException(); continue; } gridPointP.EdgeData[i] = grid.getEdgeData(p, edgeId); } /*val.EdgeData = ret.dirs.Select( dir => * { * * } ).ToArray();*/ ret.cells[p] = gridPointP; }); return(ret); }