private static void SaveAsPng(List <SurfacePoint> points, string filename, bool monochrome) { var boundingBox = new BoundingBox(); boundingBox.Set(points); Console.WriteLine("normalization. min:{0}|{1} max:{2}|{3}", boundingBox.MinZ, boundingBox.NormalizeZ(boundingBox.MinZ), boundingBox.MaxZ, boundingBox.NormalizeZ(boundingBox.MaxZ)); var width = (int)(boundingBox.Width / STRIDE) + 1; var height = (int)(boundingBox.Height / STRIDE) + 1; using (Bitmap b = new Bitmap(width, height)) { points.ForEach(point => { var x = (int)((point.GridX - boundingBox.MinX) / STRIDE); var y = (int)((height - 1) - ((point.GridY - boundingBox.MinY) / STRIDE)); // y coordinate is flipped. var normalized = boundingBox.NormalizeZ(point.Height); if (normalized < 0f || normalized > 1f) { Console.WriteLine("ERROR"); } var colorIndex = (int)Math.Floor(255d * normalized); if (monochrome) { b.SetPixel(x, y, Color.FromArgb(255, colorIndex, colorIndex, colorIndex)); } else { var color = ColorPallete.Viridis.Value[colorIndex]; b.SetPixel(x, y, Color.FromArgb(colorIndex, color[0], color[1], color[2])); } }); b.Save(filename + PNG, ImageFormat.Png); } }
public static void SaveAsObj(List <SurfacePoint> points, string filename, string textureFilename) { var boundingBox = new BoundingBox(); boundingBox.Set(points); var mesh = GetMesh(points, boundingBox); using (StreamWriter writer = new StreamWriter(File.Open(filename + MTL, FileMode.Create))) { writer.WriteLine("newmtl volume"); writer.WriteLine("Ns 10.0000"); writer.WriteLine("Ni 1.5000"); writer.WriteLine("d 1.0000"); writer.WriteLine("Tr 0.0000"); writer.WriteLine("Tf 1.0000 1.0000 1.0000"); writer.WriteLine("illum 2"); writer.WriteLine("Ka 0.0000 0.0000 0.0000"); writer.WriteLine("Kd 0.0 0.0 0.0"); writer.WriteLine("Ks 0.0000 0.0000 0.0000"); writer.WriteLine("Ke 0.0000 0.0000 0.0000"); writer.WriteLine("map_Ka {0}", textureFilename + PNG); writer.WriteLine("map_Kd {0}", textureFilename + PNG); } using (StreamWriter writer = new StreamWriter(File.Open(filename + OBJ, FileMode.Create))) { writer.WriteLine("o volume"); writer.WriteLine("mtllib superficie.mtl"); writer.WriteLine("# vertices (x,y,z)"); foreach (var t in mesh) { writer.WriteLine("v {0} {1} {2}", t.a.x, t.a.y, t.a.z); writer.WriteLine("v {0} {1} {2}", t.b.x, t.b.y, t.b.z); writer.WriteLine("v {0} {1} {2}", t.c.x, t.c.y, t.c.z); } writer.WriteLine(); writer.WriteLine("# normals (i,j,k)"); foreach (var t in mesh) { writer.WriteLine("vn {0} {1} {2}", t.a.i, t.a.j, t.a.k); writer.WriteLine("vn {0} {1} {2}", t.b.i, t.b.j, t.b.k); writer.WriteLine("vn {0} {1} {2}", t.c.i, t.c.j, t.c.k); } writer.WriteLine(); writer.WriteLine("# texture coordinates (u,v)"); foreach (var t in mesh) { writer.WriteLine("vt {0} {1}", t.a.u, t.a.v); writer.WriteLine("vt {0} {1}", t.b.u, t.b.v); writer.WriteLine("vt {0} {1}", t.c.u, t.c.v); } writer.WriteLine(); writer.WriteLine("# faces"); writer.WriteLine("g volume"); writer.WriteLine("usemtl volume"); int index = 1; // indices starts with 1 foreach (var t in mesh) { writer.WriteLine("f {0}/{0}/{0} {1}/{1}/{1} {2}/{2}/{2}", index, index + 1, index + 2); index += 3; } } }