Exemplo n.º 1
0
        public static ImageBuffer Generate(IObject3D loadedItem, RenderType renderType, int width, int height, bool allowMultiThreading = true)
        {
            // Skip generation for invalid items
            if ((loadedItem.Children.Count == 0 && loadedItem.Mesh == null) ||
                !loadedItem.VisibleMeshes().Any())
            {
                // check again in case it got loaded
                if (!loadedItem.VisibleMeshes().Any())
                {
                    return(null);
                }
            }

            switch (renderType)
            {
            case RenderType.RAY_TRACE:
            {
                var tracer = new ThumbnailTracer(loadedItem, width, height)
                {
                    MultiThreaded = allowMultiThreading
                };
                tracer.TraceScene();

                tracer.destImage?.SetRecieveBlender(new BlenderPreMultBGRA());

                return(tracer.destImage);
            }

            case RenderType.PERSPECTIVE:
            {
                var tracer = new ThumbnailTracer(loadedItem, width, height);

                var thumbnail  = new ImageBuffer(width, height);
                var graphics2D = thumbnail.NewGraphics2D();

                foreach (IObject3D item in loadedItem.Children)
                {
                    double minZ = double.MaxValue;
                    double maxZ = double.MinValue;

                    tracer.GetMinMaxZ(item.Mesh, ref minZ, ref maxZ);

                    tracer.RenderPerspective(graphics2D, item.Mesh, Color.White, minZ, maxZ);
                }

                thumbnail.SetRecieveBlender(new BlenderPreMultBGRA());
                return(thumbnail);
            }

            case RenderType.NONE:
            case RenderType.ORTHOGROPHIC:
            default:
            {
                var thumbnail = BuildImageFromMeshGroups(loadedItem, width, height);

                // Force to all white and return
                return(thumbnail.AllWhite());
            }
            }
        }
		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();
		}