diff --git a/MatterControl.csproj b/MatterControl.csproj index 4be3a8b21..867ec171f 100644 --- a/MatterControl.csproj +++ b/MatterControl.csproj @@ -181,6 +181,7 @@ + @@ -382,6 +383,7 @@ + packages\Mindscape.Raygun4Net.5.0.0\lib\net40\Mindscape.Raygun4Net.dll True diff --git a/PartPreviewWindow/View3D/DebugBvh.cs b/PartPreviewWindow/View3D/DebugBvh.cs new file mode 100644 index 000000000..a8a8613f6 --- /dev/null +++ b/PartPreviewWindow/View3D/DebugBvh.cs @@ -0,0 +1,148 @@ +/* +Copyright (c) 2014, Lars Brubaker +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +The views and conclusions contained in the software and documentation are those +of the authors and should not be interpreted as representing official policies, +either expressed or implied, of the FreeBSD Project. +*/ + +using MatterHackers.VectorMath; +using System; +using System.Text; +using MatterHackers.RayTracer; +using MatterHackers.PolygonMesh; +using MatterHackers.RenderOpenGl; +using MatterHackers.Agg; +using System.Collections.Generic; +using MatterHackers.RayTracer.Traceable; + +namespace MatterHackers.MatterControl.PartPreviewWindow +{ + public class DebugBvh + { + int startRenderLevel; + int endRenderLevel; + Stack transform = new Stack(); + + public static void Render(IPrimitive bvhToRender, Matrix4X4 startingTransform, int startRenderLevel = 0, int endRenderLevel = int.MaxValue) + { + DebugBvh visitor = new DebugBvh(startRenderLevel, endRenderLevel); + visitor.transform.Push(startingTransform); + visitor.RenderRecursive((dynamic)bvhToRender); + } + + public DebugBvh(int startRenderLevel = 0, int endRenderLevel = int.MaxValue) + { + this.startRenderLevel = startRenderLevel; + this.endRenderLevel = endRenderLevel; + } + + Mesh lineMesh = PlatonicSolids.CreateCube(1, 1, 1); + private void RenderLine(Matrix4X4 transform, Vector3 start, Vector3 end, RGBA_Bytes color, bool zBuffered = true) + { + Vector3 lineCenter = (start + end) / 2; + + Vector3 delta = start - end; + Matrix4X4 rotateTransform = Matrix4X4.CreateRotation(new Quaternion(Vector3.UnitX + new Vector3(.0001, -.00001, .00002), delta.GetNormal())); + Matrix4X4 scaleTransform = Matrix4X4.CreateScale((end - start).Length, 1, 1); + Matrix4X4 lineTransform = scaleTransform * rotateTransform * Matrix4X4.CreateTranslation(lineCenter) * transform; + + if (zBuffered) + { + RenderMeshToGl.Render(lineMesh, RGBA_Bytes.Black, lineTransform, RenderTypes.Shaded); + //drawEvent.graphics2D.Line(cornerPositionScreen, cornerPositionCcwScreen, RGBA_Bytes.Gray); + } + else + { + // render on top of everything very lightly + RenderMeshToGl.Render(lineMesh, new RGBA_Bytes(RGBA_Bytes.Black, 5), lineTransform, RenderTypes.Shaded); + } + } + + private void RenderBounds(AxisAlignedBoundingBox aabb) + { + RGBA_Bytes color = RGBA_Bytes.Red; + + // the bottom + RenderLine(transform.Peek(), aabb.GetBottomCorner(0), aabb.GetBottomCorner(1), color); + RenderLine(transform.Peek(), aabb.GetBottomCorner(1), aabb.GetBottomCorner(2), color); + RenderLine(transform.Peek(), aabb.GetBottomCorner(2), aabb.GetBottomCorner(3), color); + RenderLine(transform.Peek(), aabb.GetBottomCorner(3), aabb.GetBottomCorner(0), color); + + // the top + RenderLine(transform.Peek(), aabb.GetTopCorner(0), aabb.GetTopCorner(1), color); + RenderLine(transform.Peek(), aabb.GetTopCorner(1), aabb.GetTopCorner(2), color); + RenderLine(transform.Peek(), aabb.GetTopCorner(2), aabb.GetTopCorner(3), color); + RenderLine(transform.Peek(), aabb.GetTopCorner(3), aabb.GetTopCorner(0), color); + + // the sides + RenderLine(transform.Peek(), + new Vector3(aabb.minXYZ.x, aabb.minXYZ.y, aabb.minXYZ.z), + new Vector3(aabb.minXYZ.x, aabb.minXYZ.y, aabb.maxXYZ.z), + color); + RenderLine(transform.Peek(), + new Vector3(aabb.maxXYZ.x, aabb.minXYZ.y, aabb.minXYZ.z), + new Vector3(aabb.maxXYZ.x, aabb.minXYZ.y, aabb.maxXYZ.z), + color); + RenderLine(transform.Peek(), + new Vector3(aabb.minXYZ.x, aabb.maxXYZ.y, aabb.minXYZ.z), + new Vector3(aabb.minXYZ.x, aabb.maxXYZ.y, aabb.maxXYZ.z), + color); + RenderLine(transform.Peek(), + new Vector3(aabb.maxXYZ.x, aabb.maxXYZ.y, aabb.minXYZ.z), + new Vector3(aabb.maxXYZ.x, aabb.maxXYZ.y, aabb.maxXYZ.z), + color); + } + + #region Visitor Pattern Functions + + public void RenderRecursive(object objectToProcess, int level = 0) + { + throw new Exception("You must write the specialized function for this type."); + } + + public void RenderRecursive(UnboundCollection objectToProcess, int level = 0) + { + RenderBounds(objectToProcess.GetAxisAlignedBoundingBox()); + foreach (var child in objectToProcess.Items) + { + RenderRecursive((dynamic)child, level + 1); + } + } + + public void RenderRecursive(MeshFaceTraceable objectToProcess, int level = 0) + { + RenderBounds(objectToProcess.GetAxisAlignedBoundingBox()); + } + + public void RenderRecursive(Transform objectToProcess, int level = 0) + { + RenderBounds(objectToProcess.GetAxisAlignedBoundingBox()); + transform.Push(objectToProcess.Transform); + RenderRecursive((dynamic)objectToProcess.Child, level + 1); + } + + #endregion Visitor Patern Functions + } +} \ No newline at end of file diff --git a/PartPreviewWindow/View3D/View3DWidget.cs b/PartPreviewWindow/View3D/View3DWidget.cs index d9ca2f20a..265ebe08a 100644 --- a/PartPreviewWindow/View3D/View3DWidget.cs +++ b/PartPreviewWindow/View3D/View3DWidget.cs @@ -456,6 +456,16 @@ namespace MatterHackers.MatterControl.PartPreviewWindow DrawBefore += CreateBooleanTestGeometry; DrawAfter += RemoveBooleanTestGeometry; #endif + meshViewerWidget.TrackballTumbleWidget.DrawGlContent += trackballTumbleWidget_DrawGlContent; + + } + + private void trackballTumbleWidget_DrawGlContent(object sender, EventArgs e) + { + if(allObjects != null) + { + //DebugBvh.Render(allObjects, Matrix4X4.Identity); + } } public override void OnKeyDown(KeyEventArgs keyEvent) @@ -840,7 +850,6 @@ namespace MatterHackers.MatterControl.PartPreviewWindow hasDrawn = true; base.OnDraw(graphics2D); - DrawStuffForSelectedPart(graphics2D); } private ViewControls3DButtons? activeButtonBeforeMouseOverride = null; @@ -1539,31 +1548,6 @@ namespace MatterHackers.MatterControl.PartPreviewWindow } } - private void DrawStuffForSelectedPart(Graphics2D graphics2D) - { - if (SelectedMeshGroup != null) - { - AxisAlignedBoundingBox selectedBounds = SelectedMeshGroup.GetAxisAlignedBoundingBox(SelectedMeshGroupTransform); - Vector3 boundsCenter = selectedBounds.Center; - Vector3 centerTop = new Vector3(boundsCenter.x, boundsCenter.y, selectedBounds.maxXYZ.z); - - Vector2 centerTopScreenPosition = meshViewerWidget.TrackballTumbleWidget.GetScreenPosition(centerTop); - centerTopScreenPosition = meshViewerWidget.TransformToParentSpace(this, centerTopScreenPosition); - //graphics2D.Circle(screenPosition.x, screenPosition.y, 5, RGBA_Bytes.Cyan); - - PathStorage zArrow = new PathStorage(); - zArrow.MoveTo(-6, -2); - zArrow.curve3(0, -4); - zArrow.LineTo(6, -2); - zArrow.LineTo(0, 12); - zArrow.LineTo(-6, -2); - - VertexSourceApplyTransform translate = new VertexSourceApplyTransform(zArrow, Affine.NewTranslation(centerTopScreenPosition)); - - //graphics2D.Render(translate, RGBA_Bytes.Black); - } - } - private void ExitEditingAndSaveIfRequired(bool response) { if (response == true) @@ -1614,7 +1598,8 @@ namespace MatterHackers.MatterControl.PartPreviewWindow } } - private bool FindMeshGroupHitPosition(Vector2 screenPosition, out int meshHitIndex, ref IntersectInfo info) + IPrimitive allObjects; + private bool FindMeshGroupHitPosition(Vector2 screenPosition, out int meshHitIndex, ref IntersectInfo info) { meshHitIndex = 0; if (MeshGroupExtraData.Count == 0 || MeshGroupExtraData[0].meshTraceableData == null) @@ -1630,7 +1615,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow mesheTraceables.Add(new Transform(traceData, MeshGroupTransforms[i])); } } - IPrimitive allObjects = BoundingVolumeHierarchy.CreateNewHierachy(mesheTraceables); + allObjects = BoundingVolumeHierarchy.CreateNewHierachy(mesheTraceables, 0); Vector2 meshViewerWidgetScreenPosition = meshViewerWidget.TransformFromParentSpace(this, screenPosition); Ray ray = meshViewerWidget.TrackballTumbleWidget.GetRayFromScreen(meshViewerWidgetScreenPosition); diff --git a/Submodules/agg-sharp b/Submodules/agg-sharp index 140b5c421..9148b51ae 160000 --- a/Submodules/agg-sharp +++ b/Submodules/agg-sharp @@ -1 +1 @@ -Subproject commit 140b5c421da17ceef7538c0ac03e5e87346bf1e3 +Subproject commit 9148b51ae473995e727e5d980c458e6da6bbfb96