private void Splitcolortriangles(ColorTriangle triangle) { if (backgroundWorker.CancellationPending) { throw new OperationCanceledException(); } //split triangle in 4 triangles: Point3D mid1 = Average(triangle.vertices[0], triangle.vertices[1]); Point3D mid2 = Average(triangle.vertices[1], triangle.vertices[2]); Point3D mid3 = Average(triangle.vertices[0], triangle.vertices[2]); Point3D tmid1 = Average(triangle.texturecoords[0], triangle.texturecoords[1]); Point3D tmid2 = Average(triangle.texturecoords[1], triangle.texturecoords[2]); Point3D tmid3 = Average(triangle.texturecoords[0], triangle.texturecoords[2]); List <ColorTriangle> triangles = new List <ColorTriangle> { new ColorTriangle(new List <Point3D> { mid1, mid2, mid3 }, new List <Point3D> { tmid1, tmid2, tmid3 }), new ColorTriangle(new List <Point3D> { mid1, mid3, triangle.vertices[0] }, new List <Point3D> { tmid1, tmid3, triangle.texturecoords[0] }), new ColorTriangle(new List <Point3D> { mid1, mid2, triangle.vertices[1] }, new List <Point3D> { tmid1, tmid2, triangle.texturecoords[1] }), new ColorTriangle(new List <Point3D> { mid2, mid3, triangle.vertices[2] }, new List <Point3D> { tmid2, tmid3, triangle.texturecoords[2] }) }; Point3D flooredpoint1 = new Point3D((int)Math.Floor(mid1.X), (int)Math.Floor(mid1.Y), (int)Math.Floor(mid1.Z)); if (!coloredpoints.ContainsKey(flooredpoint1)) { coloredpoints.Add(flooredpoint1, tmid1); } Point3D flooredpoint2 = new Point3D((int)Math.Floor(mid2.X), (int)Math.Floor(mid2.Y), (int)Math.Floor(mid2.Z)); if (!coloredpoints.ContainsKey(flooredpoint2)) { coloredpoints.Add(flooredpoint2, tmid2); } Point3D flooredpoint3 = new Point3D((int)Math.Floor(mid3.X), (int)Math.Floor(mid3.Y), (int)Math.Floor(mid3.Z)); if (!coloredpoints.ContainsKey(flooredpoint3)) { coloredpoints.Add(flooredpoint3, tmid3); } // coloredpoints.Add(new Tuple<Point3D,Point3D>( new Point3D((int)Math.Floor(mid1.X), (int)Math.Floor(mid1.Y), (int)Math.Floor(mid1.Z)),tmid1)); // coloredpoints.Add(new Tuple<Point3D,Point3D>( new Point3D((int)Math.Floor(mid2.X), (int)Math.Floor(mid2.Y), (int)Math.Floor(mid2.Z)),tmid2)); // coloredpoints.Add(new Tuple<Point3D,Point3D>( new Point3D((int)Math.Floor(mid3.X), (int)Math.Floor(mid3.Y), (int)Math.Floor(mid3.Z)),tmid3)); //triangles.Add(new Triangle(new List<Point3D> { mid1, mid2, mid3 })); //triangles.Add(new Triangle(new List<Point3D> { mid1, mid3, triangle.vertices[0] })); //triangles.Add(new Triangle(new List<Point3D> { mid1, mid2, triangle.vertices[1] })); //triangles.Add(new Triangle(new List<Point3D> { mid2, mid3, triangle.vertices[2] })); //check for each triangle if bounds are smaller than 1x1x1, if not, split each again: foreach (ColorTriangle splittriangle in triangles) { // foreach(Point3D point in splittriangle.vertices) // pointlist.Add(new Point3D((int)Math.Floor(point.X), (int)Math.Floor(point.Y), (int)Math.Floor(point.Z))); if (!splittriangle.BoundsSmallerThan(bounds)) { Splitcolortriangles(splittriangle); } } //return splittriangles; }
private void Convertobjwtexture(object sender, DoWorkEventArgs e) { try { Scene scene = new AssimpImporter().ImportFile(file, PostProcessSteps.Triangulate); coloredpoints = new Dictionary <Point3D, Point3D>(); //List<Triangle> splittriangles = new List<Triangle>(); //pointlist = new HashSet<Point3D>(); //pointlist.ToDictionary<> int progress = 0; int total = 0; foreach (Mesh m in scene.Meshes) { foreach (Face face in m.Faces) { total++; } } foreach (Mesh m in scene.Meshes) { var texturecoords = m.GetTextureCoords(0); foreach (Face face in m.Faces) { IList <Point3D> vertices = new List <Point3D>(); IList <Point3D> texturepoints = new List <Point3D>(); foreach (uint i in face.Indices) { double x = m.Vertices[i].X * scale; double y = m.Vertices[i].Y * scale; double z = m.Vertices[i].Z * scale; Point3D point; Assimp.Vector3D texturep = texturecoords[i]; Point3D texturepoint = new Point3D(texturep.X, 1 - texturep.Y, texturep.Z); if (flipyz) { if (flipxz) { point = new Point3D(y, z, flipz * x); } else { point = new Point3D(x, z, flipz * y); } } else { if (flipxz) { point = new Point3D(z, y, flipz * x); } else { point = new Point3D(x, y, flipz * z); } } vertices.Add(point); texturepoints.Add(texturepoint); Point3D flooredpoint = new Point3D((int)Math.Floor(point.X), (int)Math.Floor(point.Y), (int)Math.Floor(point.Z)); if (!coloredpoints.ContainsKey(flooredpoint)) { coloredpoints.Add(flooredpoint, texturepoint); } } if (vertices.Count == 3) { ColorTriangle triangle = new ColorTriangle(vertices, texturepoints); if (!triangle.BoundsSmallerThan(bounds)) { Splitcolortriangles(triangle); } } else { } progress++; backgroundWorker.ReportProgress((progress * 100) / total); if (backgroundWorker.CancellationPending) { e.Cancel = true; } } } } catch (Exception ex) { if (!(ex is OperationCanceledException)) { System.Windows.MessageBox.Show(ex.Message, "Something went wrong converting the obj+texture"); } else { e.Cancel = true; } } try { if (backgroundWorker.CancellationPending) { throw new OperationCanceledException(); } dynamic blueprint = new JObject(); try { Bitmap texturemap = new Bitmap(this.texture); HashSet <Tuple <Point3D, string> > pointswithcolor = new HashSet <Tuple <Point3D, string> >(); int width = texturemap.Width; int height = texturemap.Height; foreach (var pair in coloredpoints) { int x = (int)(pair.Value.X * width); int y = (int)(pair.Value.Y * height); if (pair.Value.Y > 1) { } while (y < 0) { y++; } while (x < 0) { x++; } if (Math.Abs(x) > width / 2) { } while (x >= width) { x -= width; } while (y >= height) { y -= height; } System.Drawing.Color color = texturemap.GetPixel(x, y); string c = color.Name.Substring(2); pointswithcolor.Add(new Tuple <Point3D, string>(pair.Key, c)); } coloredpoints.Clear(); blueprint = BlueprintOptimizer.CreateBlueprintFromPointsAndColor(pointswithcolor); } catch (Exception bpex) { System.Windows.MessageBox.Show(bpex.Message, "Something went wrong building the blueprint"); } int amountgenerated = blueprint.bodies[0].childs.Count; string message = "converted obj to blueprint with " + amountgenerated + " blocks !"; //MessageBox.Show(message+"\n\nOptimized to: "+count+" shapes"); Random r = new Random(); string blueprintpath = Database.User_ + "\\Blueprints\\Generatedblueprintobj-" + r.Next() + r.Next(); dynamic description = new JObject(); description.description = "generated obj blueprint with " + amountgenerated + " block"; description.name = "generated blueprint" + r.Next(); description.type = "Blueprint"; description.version = 0; new Task(() => { System.Windows.MessageBox.Show(message + "\nPLEASE WAIT for the rendering to complete"); }).Start(); if (BP.Blueprintpath == null) {//no blueprint exists, initialize new one new BP(blueprintpath, blueprint, description); } else {//overwrite current blueprint BP.setblueprint(blueprint); BP.Description.description += message; } } catch (Exception exc) { System.Windows.MessageBox.Show(exc.Message, "error"); } }