From 0f9380a89784bac851428fb76d3acee0cd05d2a1 Mon Sep 17 00:00:00 2001 From: Lars Brubaker Date: Mon, 31 Jul 2017 14:20:58 -0700 Subject: [PATCH 1/2] Preserving correct colors while traversing an IObject3D tree --- PartPreviewWindow/View3D/MeshViewerWidget.cs | 10 +++++++--- Submodules/agg-sharp | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/PartPreviewWindow/View3D/MeshViewerWidget.cs b/PartPreviewWindow/View3D/MeshViewerWidget.cs index d4cc189f4..e0c1e0454 100644 --- a/PartPreviewWindow/View3D/MeshViewerWidget.cs +++ b/PartPreviewWindow/View3D/MeshViewerWidget.cs @@ -604,6 +604,7 @@ namespace MatterHackers.MeshVisualizer public bool IsActive { get; set; } = true; + RGBA_Bytes lastDrawColor; private void DrawObject(IObject3D object3D, Matrix4X4 transform, bool parentSelected) { foreach(MeshAndTransform meshAndTransform in object3D.VisibleMeshes(transform)) @@ -611,7 +612,7 @@ namespace MatterHackers.MeshVisualizer bool isSelected = parentSelected || Scene.HasSelection && (object3D == Scene.SelectedItem || Scene.SelectedItem.Children.Contains(object3D)); - RGBA_Bytes drawColor = object3D.Color; + RGBA_Bytes drawColor = meshAndTransform.Color; if(object3D.OutputType == PrintOutputTypes.Support) { drawColor = new RGBA_Bytes(RGBA_Bytes.Yellow, 120); @@ -623,8 +624,11 @@ namespace MatterHackers.MeshVisualizer if (drawColor.Alpha0To1 == 0) { - int extruderIndex = Math.Max(0, object3D.ExtruderIndex); - drawColor = isSelected ? GetSelectedExtruderColor(extruderIndex) : GetExtruderColor(extruderIndex); + drawColor = lastDrawColor; + } + else + { + lastDrawColor = drawColor; } GLHelper.Render(meshAndTransform.MeshData, drawColor, meshAndTransform.Matrix, RenderType); diff --git a/Submodules/agg-sharp b/Submodules/agg-sharp index d7e8252e9..7fe837f4c 160000 --- a/Submodules/agg-sharp +++ b/Submodules/agg-sharp @@ -1 +1 @@ -Subproject commit d7e8252e9602d9d46536377c6dab84f758c63331 +Subproject commit 7fe837f4c4755cc7c60eadf27c9cf71a3ef9277e From 7bd46801c025211da26c9db5945a9c3edbd4bf6f Mon Sep 17 00:00:00 2001 From: Lars Brubaker Date: Tue, 1 Aug 2017 11:51:25 -0700 Subject: [PATCH 2/2] Made thumbnail renderer able to use color data --- .../ContentProviders/MeshContentProvider.cs | 14 --- PartPreviewWindow/CreateDiscreteMeshes.cs | 111 ------------------ PartPreviewWindow/View3D/MeshViewerWidget.cs | 8 +- PartPreviewWindow/View3D/View3DWidget.cs | 4 +- Queue/OptionsMenu/PartsSheetCreator.cs | 25 ++-- Submodules/agg-sharp | 2 +- 6 files changed, 14 insertions(+), 150 deletions(-) diff --git a/Library/ContentProviders/MeshContentProvider.cs b/Library/ContentProviders/MeshContentProvider.cs index a1511a9b3..56d8e2a06 100644 --- a/Library/ContentProviders/MeshContentProvider.cs +++ b/Library/ContentProviders/MeshContentProvider.cs @@ -136,20 +136,6 @@ namespace MatterHackers.MatterControl if (object3D != null) { - if (object3D.Mesh == null && object3D.HasChildren) - { - object3D = new Object3D() - { - ItemType = Object3DTypes.Model, - Mesh = MeshFileIo.DoMerge(object3D.ToMeshGroupList(), new MeshOutputSettings()) - }; - } - - if (object3D.Mesh == null) - { - return; - } - bool RenderOrthographic = UserSettings.Instance.get(UserSettingsKey.ThumbnailRenderingMode) == "orthographic"; var thumbnail = ThumbnailEngine.Generate( diff --git a/PartPreviewWindow/CreateDiscreteMeshes.cs b/PartPreviewWindow/CreateDiscreteMeshes.cs index 52897cc05..7d564081f 100644 --- a/PartPreviewWindow/CreateDiscreteMeshes.cs +++ b/PartPreviewWindow/CreateDiscreteMeshes.cs @@ -128,117 +128,6 @@ namespace MatterHackers.MatterControl return discreetVolumes; } - public static Mesh[] SplitIntoMeshesOnOrthographicZ(Mesh meshToSplit, Vector3 buildVolume, Action reportProgress) - { - // check if the part is bigger than the build plate (if it is we need to use that as our size) - AxisAlignedBoundingBox partBounds = meshToSplit.GetAxisAlignedBoundingBox(); - - buildVolume.x = Math.Max(buildVolume.x, partBounds.XSize + 2); - buildVolume.y = Math.Max(buildVolume.y, partBounds.YSize + 2); - buildVolume.z = Math.Max(buildVolume.z, partBounds.ZSize + 2); - - // Find all the separate objects that are on the plate - // Create a 2D image the size of the printer bed at some scale with the parts draw on it top down - - double scaleFactor = 5; - ImageBuffer partPlate = new ImageBuffer((int)(buildVolume.x * scaleFactor), (int)(buildVolume.y * scaleFactor)); - Vector2 renderOffset = new Vector2(buildVolume.x / 2, buildVolume.y / 2) - new Vector2(partBounds.Center.x, partBounds.Center.y); - - PolygonMesh.Rendering.OrthographicZProjection.DrawTo(partPlate.NewGraphics2D(), meshToSplit, renderOffset, scaleFactor, RGBA_Bytes.White); - - reportProgress?.Invoke(.2, ""); - - //ImageIO.SaveImageData("test part plate 0.png", partPlate); - // expand the bounds a bit so that we can collect all the vertices and polygons within each bound - Dilate.DoDilate3x3Binary(partPlate, 1); - //ImageIO.SaveImageData("test part plate 1.png", partPlate); - - // trace all the bounds of the objects on the plate - PolyTree polyTreeForPlate = FindDistictObjectBounds(partPlate); - if (polyTreeForPlate == null) - { - Mesh[] singleMesh = new Mesh[1]; - singleMesh[0] = meshToSplit; - return singleMesh; - } - - // get all the discrete areas that are polygons so we can search them - Polygons discreteAreas = new Polygons(); - GetAreasRecursive(polyTreeForPlate, discreteAreas); - if (discreteAreas.Count == 0) - { - return null; - } - else if (discreteAreas.Count == 1) - { - Mesh[] singleMesh = new Mesh[1]; - singleMesh[0] = meshToSplit; - return singleMesh; - } - - Graphics2D graphics2D = partPlate.NewGraphics2D(); - graphics2D.Clear(RGBA_Bytes.Black); - Random rand = new Random(); - foreach (Polygon polygon in discreteAreas) - { - graphics2D.Render(PlatingHelper.PolygonToPathStorage(polygon), new RGBA_Bytes(rand.Next(128, 255), rand.Next(128, 255), rand.Next(128, 255))); - } - - reportProgress?.Invoke(.5, ""); - - //ImageIO.SaveImageData("test part plate 2.png", partPlate); - - // add each of the separate bounds polygons to new meshes - Mesh[] discreteMeshes = new Mesh[discreteAreas.Count]; - for (int i = 0; i < discreteAreas.Count; i++) - { - discreteMeshes[i] = new Mesh(); - } - - foreach (Face face in meshToSplit.Faces) - { - bool faceDone = false; - // figure out which area one or more of the vertices are in add the face to the right new mesh - foreach (FaceEdge faceEdge in face.FaceEdges()) - { - Vector2 position = new Vector2(faceEdge.FirstVertex.Position.x, faceEdge.FirstVertex.Position.y); - position += renderOffset; - position *= scaleFactor; - - for (int areaIndex = discreteAreas.Count - 1; areaIndex >= 0; areaIndex--) - { - if (PointInPolygon(discreteAreas[areaIndex], new IntPoint((int)position.x, (int)position.y))) - { - var faceVertices = new List(); - foreach (FaceEdge faceEdgeToAdd in face.FaceEdges()) - { - var newVertex = discreteMeshes[areaIndex].CreateVertex(faceEdgeToAdd.FirstVertex.Position); - faceVertices.Add(newVertex); - } - - discreteMeshes[areaIndex].CreateFace(faceVertices.ToArray()); - faceDone = true; - break; - } - } - - if (faceDone) - { - break; - } - } - } - - reportProgress?.Invoke(.8, ""); - - for (int i = 0; i < discreteMeshes.Count(); i++) - { - Mesh mesh = discreteMeshes[i]; - } - - return discreteMeshes; - } - public static bool PointInPolygon(Polygon polygon, IntPoint testPosition) { int numPoints = polygon.Count; diff --git a/PartPreviewWindow/View3D/MeshViewerWidget.cs b/PartPreviewWindow/View3D/MeshViewerWidget.cs index e0c1e0454..1bcb5d8de 100644 --- a/PartPreviewWindow/View3D/MeshViewerWidget.cs +++ b/PartPreviewWindow/View3D/MeshViewerWidget.cs @@ -242,7 +242,7 @@ namespace MatterHackers.MeshVisualizer renderType = value; foreach(var renderTransfrom in Scene.VisibleMeshes(Matrix4X4.Identity)) { - renderTransfrom.MeshData.MarkAsChanged(); + renderTransfrom.Mesh.MarkAsChanged(); } } } @@ -607,12 +607,12 @@ namespace MatterHackers.MeshVisualizer RGBA_Bytes lastDrawColor; private void DrawObject(IObject3D object3D, Matrix4X4 transform, bool parentSelected) { - foreach(MeshAndTransform meshAndTransform in object3D.VisibleMeshes(transform)) + foreach(MeshRenderData renderData in object3D.VisibleMeshes(transform)) { bool isSelected = parentSelected || Scene.HasSelection && (object3D == Scene.SelectedItem || Scene.SelectedItem.Children.Contains(object3D)); - RGBA_Bytes drawColor = meshAndTransform.Color; + RGBA_Bytes drawColor = renderData.Color; if(object3D.OutputType == PrintOutputTypes.Support) { drawColor = new RGBA_Bytes(RGBA_Bytes.Yellow, 120); @@ -631,7 +631,7 @@ namespace MatterHackers.MeshVisualizer lastDrawColor = drawColor; } - GLHelper.Render(meshAndTransform.MeshData, drawColor, meshAndTransform.Matrix, RenderType); + GLHelper.Render(renderData.Mesh, drawColor, renderData.Matrix, RenderType); } } diff --git a/PartPreviewWindow/View3D/View3DWidget.cs b/PartPreviewWindow/View3D/View3DWidget.cs index e1c807245..c5858444c 100644 --- a/PartPreviewWindow/View3D/View3DWidget.cs +++ b/PartPreviewWindow/View3D/View3DWidget.cs @@ -2366,9 +2366,9 @@ namespace MatterHackers.MatterControl.PartPreviewWindow UserSettings.Instance.set("defaultRenderSetting", meshViewerWidget.RenderType.ToString()); foreach (var meshAndTransform in scene.VisibleMeshes(Matrix4X4.Identity)) { - meshAndTransform.MeshData.MarkAsChanged(); + meshAndTransform.Mesh.MarkAsChanged(); // change the color to be the right thing - GLMeshTrianglePlugin glMeshPlugin = GLMeshTrianglePlugin.Get(meshAndTransform.MeshData, (faceEdge) => + GLMeshTrianglePlugin glMeshPlugin = GLMeshTrianglePlugin.Get(meshAndTransform.Mesh, (faceEdge) => { Vector3 normal = faceEdge.ContainingFace.normal; normal = Vector3.TransformVector(normal, meshAndTransform.Matrix).GetNormal(); diff --git a/Queue/OptionsMenu/PartsSheetCreator.cs b/Queue/OptionsMenu/PartsSheetCreator.cs index 36962b882..3bd12e1ca 100644 --- a/Queue/OptionsMenu/PartsSheetCreator.cs +++ b/Queue/OptionsMenu/PartsSheetCreator.cs @@ -136,22 +136,14 @@ namespace MatterHackers.MatterControl await contentResult.MeshLoaded; - List loadedMeshGroups = contentResult.Object3D?.ToMeshGroupList(); + var loadedMeshGroups = contentResult.Object3D.VisibleMeshes(Matrix4X4.Identity).ToList(); if (loadedMeshGroups?.Count > 0) { - bool firstMeshGroup = true; - AxisAlignedBoundingBox aabb = null; - foreach (MeshGroup meshGroup in loadedMeshGroups) + AxisAlignedBoundingBox aabb = loadedMeshGroups[0].Mesh.GetAxisAlignedBoundingBox(loadedMeshGroups[0].Matrix); + + for (int i = 1; i < loadedMeshGroups.Count; i++) { - if (firstMeshGroup) - { - aabb = meshGroup.GetAxisAlignedBoundingBox(); - firstMeshGroup = false; - } - else - { - aabb = AxisAlignedBoundingBox.Union(aabb, meshGroup.GetAxisAlignedBoundingBox()); - } + aabb = AxisAlignedBoundingBox.Union(aabb, loadedMeshGroups[i].Mesh.GetAxisAlignedBoundingBox(loadedMeshGroups[i].Matrix)); } RectangleDouble bounds2D = new RectangleDouble(aabb.minXYZ.x, aabb.minXYZ.y, aabb.maxXYZ.x, aabb.maxXYZ.y); @@ -176,12 +168,9 @@ namespace MatterHackers.MatterControl Stroke rectOutline = new Stroke(rect, strokeWidth); partGraphics2D.Render(rectOutline, RGBA_Bytes.DarkGray); - foreach (MeshGroup meshGroup in loadedMeshGroups) + foreach (var meshGroup in loadedMeshGroups) { - foreach (Mesh loadedMesh in meshGroup.Meshes) - { - PolygonMesh.Rendering.OrthographicZProjection.DrawTo(partGraphics2D, loadedMesh, new Vector2(-bounds2D.Left + PartMarginMM, -bounds2D.Bottom + textSpaceMM + PartMarginMM), PixelPerMM, RGBA_Bytes.Black); - } + PolygonMesh.Rendering.OrthographicZProjection.DrawTo(partGraphics2D, meshGroup.Mesh, meshGroup.Matrix, new Vector2(-bounds2D.Left + PartMarginMM, -bounds2D.Bottom + textSpaceMM + PartMarginMM), PixelPerMM, RGBA_Bytes.Black); } partGraphics2D.Render(typeFacePrinter, RGBA_Bytes.Black); diff --git a/Submodules/agg-sharp b/Submodules/agg-sharp index 7fe837f4c..8527327fc 160000 --- a/Submodules/agg-sharp +++ b/Submodules/agg-sharp @@ -1 +1 @@ -Subproject commit 7fe837f4c4755cc7c60eadf27c9cf71a3ef9277e +Subproject commit 8527327fcdd703f5eb2836b54ebff8559e625e07