private void CreateThumbnail() { string stlHashCode = this.ItemWrapper.FileHashCode.ToString(); ImageBuffer bigRender = new ImageBuffer(); if (!File.Exists(this.ItemWrapper.FileLocation)) { return; } List<MeshGroup> loadedMeshGroups = MeshFileIo.Load(this.ItemWrapper.FileLocation); RenderType renderType = GetRenderType(this.ItemWrapper.FileLocation); switch (renderType) { case RenderType.RAY_TRACE: { ThumbnailTracer tracer = new ThumbnailTracer(loadedMeshGroups, BigRenderSize.x, BigRenderSize.y); tracer.DoTrace(); bigRender = tracer.destImage; } break; case RenderType.PERSPECTIVE: { ThumbnailTracer tracer = new ThumbnailTracer(loadedMeshGroups, BigRenderSize.x, BigRenderSize.y); this.thumbnailImage = new ImageBuffer(this.buildingThumbnailImage); this.thumbnailImage.NewGraphics2D().Clear(new RGBA_Bytes(255, 255, 255, 0)); bigRender = new ImageBuffer(BigRenderSize.x, BigRenderSize.y); foreach (MeshGroup meshGroup in loadedMeshGroups) { double minZ = double.MaxValue; double maxZ = double.MinValue; foreach (Mesh loadedMesh in meshGroup.Meshes) { tracer.GetMinMaxZ(loadedMesh, ref minZ, ref maxZ); } foreach (Mesh loadedMesh in meshGroup.Meshes) { tracer.DrawTo(bigRender.NewGraphics2D(), loadedMesh, RGBA_Bytes.White, minZ, maxZ); } } if (bigRender == null) { bigRender = new ImageBuffer(this.noThumbnailImage); } } break; case RenderType.NONE: case RenderType.ORTHOGROPHIC: this.thumbnailImage = new ImageBuffer(this.buildingThumbnailImage); this.thumbnailImage.NewGraphics2D().Clear(new RGBA_Bytes(255, 255, 255, 0)); bigRender = BuildImageFromMeshGroups(loadedMeshGroups, stlHashCode, BigRenderSize); if (bigRender == null) { bigRender = new ImageBuffer(this.noThumbnailImage); } break; } // and save it to disk string imageFileName = GetImageFileName(stlHashCode); if (partExtension == ".png") { ImageIO.SaveImageData(imageFileName, bigRender); } else { ImageTgaIO.SaveImageData(imageFileName, bigRender); } bigRender.SetRecieveBlender(new BlenderPreMultBGRA()); this.thumbnailImage = ImageBuffer.CreateScaledImage(bigRender, (int)Width, (int)Height); UiThread.RunOnIdle(this.EnsureImageUpdated); OnDoneRendering(); }
private void CreateThumbnail() { string stlHashCode = this.ItemWrapper.FileHashCode.ToString(); ImageBuffer bigRender = new ImageBuffer(); if (!File.Exists(this.ItemWrapper.FileLocation)) { return; } List<MeshGroup> loadedMeshGroups = MeshFileIo.Load(this.ItemWrapper.FileLocation); RenderType renderType = GetRenderType(this.ItemWrapper.FileLocation); switch (renderType) { case RenderType.RAY_TRACE: { ThumbnailTracer tracer = new ThumbnailTracer(loadedMeshGroups, BigRenderSize.x, BigRenderSize.y); tracer.DoTrace(); bigRender = tracer.destImage; } break; case RenderType.PERSPECTIVE: { ThumbnailTracer tracer = new ThumbnailTracer(loadedMeshGroups, BigRenderSize.x, BigRenderSize.y); this.thumbnailImage = new ImageBuffer(this.buildingThumbnailImage); this.thumbnailImage.NewGraphics2D().Clear(new RGBA_Bytes(255, 255, 255, 0)); bigRender = new ImageBuffer(BigRenderSize.x, BigRenderSize.y, 32, new BlenderBGRA()); foreach (MeshGroup meshGroup in loadedMeshGroups) { double minZ = double.MaxValue; double maxZ = double.MinValue; foreach (Mesh loadedMesh in meshGroup.Meshes) { tracer.GetMinMaxZ(loadedMesh, ref minZ, ref maxZ); } foreach (Mesh loadedMesh in meshGroup.Meshes) { tracer.DrawTo(bigRender.NewGraphics2D(), loadedMesh, RGBA_Bytes.White, minZ, maxZ); } } if (bigRender == null) { bigRender = new ImageBuffer(this.noThumbnailImage); } } break; case RenderType.NONE: case RenderType.ORTHOGROPHIC: this.thumbnailImage = new ImageBuffer(this.buildingThumbnailImage); this.thumbnailImage.NewGraphics2D().Clear(new RGBA_Bytes(255, 255, 255, 0)); bigRender = BuildImageFromMeshGroups(loadedMeshGroups, stlHashCode, BigRenderSize); if (bigRender == null) { bigRender = new ImageBuffer(this.noThumbnailImage); } break; } // and save it to disk string imageFileName = GetImageFileName(stlHashCode); if (partExtension == ".png") { ImageIO.SaveImageData(imageFileName, bigRender); } else { ImageTgaIO.SaveImageData(imageFileName, bigRender); } ImageBuffer unScaledImage = new ImageBuffer(bigRender.Width, bigRender.Height, 32, new BlenderBGRA()); unScaledImage.NewGraphics2D().Render(bigRender, 0, 0); // If the source image (the one we downloaded) is more than twice as big as our dest image. while (unScaledImage.Width > Width * 2) { // The image sampler we use is a 2x2 filter so we need to scale by a max of 1/2 if we want to get good results. // So we scale as many times as we need to to get the Image to be the right size. // If this were going to be a non-uniform scale we could do the x and y separatly to get better results. ImageBuffer halfImage = new ImageBuffer(unScaledImage.Width / 2, unScaledImage.Height / 2, 32, new BlenderBGRA()); halfImage.NewGraphics2D().Render(unScaledImage, 0, 0, 0, halfImage.Width / (double)unScaledImage.Width, halfImage.Height / (double)unScaledImage.Height); unScaledImage = halfImage; } this.thumbnailImage = new ImageBuffer((int)Width, (int)Height, 32, new BlenderBGRA()); this.thumbnailImage.NewGraphics2D().Clear(new RGBA_Bytes(255, 255, 255, 0)); this.thumbnailImage.NewGraphics2D().Render(unScaledImage, 0, 0, 0, (double)this.thumbnailImage.Width / unScaledImage.Width, (double)this.thumbnailImage.Height / unScaledImage.Height); UiThread.RunOnIdle(this.EnsureImageUpdated); OnDoneRendering(); }