public bool GenerateHatches(MHatchSettings settings) { HatchLines.Clear(); _totalHatchesJumpDistance = 0; _totalHatchesMarkDistance = 0; var lines = new List <MarkGeometryLine>(); var angleRad = GeometricArithmeticModule.ToRadians(settings.Angle); var size = Extents.Hypotenuse; var howmany = (int)Math.Ceiling(size / settings.Pitch); var yStart = Extents.Centre.Y - (0.5 * size); // generate lines to calculate intersections for hatch if (settings.Style == HatchStyle.RASTER || settings.Style == HatchStyle.RASTER_GRID) { for (int i = 0; i < howmany; i++) { double y = yStart + (i * settings.Pitch); var line = new MarkGeometryLine( new MarkGeometryPoint( -size + Extents.Centre.X, y ), new MarkGeometryPoint( size + Extents.Centre.X, y ) ); // apply angular rotation GeometricArithmeticModule.Rotate(line, 0, 0, angleRad, Extents.Centre.X, Extents.Centre.Y, Extents.Centre.Z); lines.Add(line); } } else if (settings.Style == HatchStyle.SERPENTINE || settings.Style == HatchStyle.SERPENTINE_GRID) { for (int i = 0; i < howmany; i++) { double y = yStart + (i * settings.Pitch); if (i % 2 == 0) { var line = new MarkGeometryLine( new MarkGeometryPoint( -size + Extents.Centre.X, y ), new MarkGeometryPoint( size + Extents.Centre.X, y ) ); // apply angular rotation GeometricArithmeticModule.Rotate(line, 0, 0, angleRad, Extents.Centre.X, Extents.Centre.Y, Extents.Centre.Z); lines.Add(line); } else { var line = new MarkGeometryLine( new MarkGeometryPoint( size + Extents.Centre.X, y ), new MarkGeometryPoint( -size + Extents.Centre.X, y ) ); // apply angular rotation GeometricArithmeticModule.Rotate(line, 0, 0, angleRad, Extents.Centre.X, Extents.Centre.Y, Extents.Centre.Z); lines.Add(line); } } } // duplicate lines if using grid var perpendicularAngleForGridLines = GeometricArithmeticModule.ToRadians(90); if (settings.Style == HatchStyle.RASTER_GRID || settings.Style == HatchStyle.SERPENTINE_GRID) { int startIndex = lines.Count - 1; for (int i = startIndex; i >= 0; i--) { var ln = (MarkGeometryLine)lines[i].Clone(); GeometricArithmeticModule.Rotate(ln, 0, 0, perpendicularAngleForGridLines, Extents.Centre.X, Extents.Centre.Y, Extents.Centre.Z); lines.Add(ln); } } // used to track jumps MarkGeometryPoint lastPoint = null; // generate hatch lines with extension for (int i = 0; i < lines.Count; i++) { List <MarkGeometryPoint> intersections = _contourQuadTree.Intersect(lines[i])?.ToList(); if (intersections == null) { continue; } int startIndex = (settings.Invert) ? 1 : 0; int endIndex = intersections.Count - 1; while (startIndex < endIndex) { var hatch = new MarkGeometryLine( intersections[startIndex], intersections[startIndex + 1] ); HatchLines.Add(hatch); // increase mark and jump distance if (lastPoint != null) { _totalHatchesJumpDistance += GeometricArithmeticModule.ABSMeasure2D( lastPoint, hatch.StartPoint ); } _totalHatchesMarkDistance += hatch.Length; lastPoint = hatch.EndPoint; startIndex += 2; } } return(true); }
private static void TestQuadTree() { var _slicer = new MSTLSlicer(); _slicer.Load(@"C:\MSOLV\STLs\Ice Cream Type 2.stl"); //_slicer.Load(@"C:\MSOLV\STLs\tray.stl"); var slices = _slicer.Slice(); IEnumerable <MVertex> contour = slices[7][0].ToVertices().Concat(slices[5][0].ToVertices()); //IEnumerable<MVertex> contour = slices[5][1].ToVertices(); var reference = ToPath(contour); var intersectingLine = new MarkGeometryLine( reference.Extents.MinimumPoint, reference.Extents.MaximumPoint ); List <MarkGeometryPoint> controlResults = new List <MarkGeometryPoint>(); List <MarkGeometryPoint> quadTreeResults = new List <MarkGeometryPoint>(); var lines = ToLines(contour); var contourQuadTree = new ContourQuadTree(contour); Console.WriteLine( PerformanceHelper.Compare( () => { // setup up controlResults.Clear(); MarkGeometryPoint intersection; for (int i = 0; i < lines.Count; i++) { if (( intersection = GeometricArithmeticModule.CalculateIntersection2D( intersectingLine, lines[i] )) != null ) { controlResults.Add(intersection); } } }, () => { // setup up quadTreeResults.Clear(); quadTreeResults = contourQuadTree.Intersect(intersectingLine).ToList(); }, tagA: "Lines (Control)", tagB: "Quad Tree" ) ); Println("Control", controlResults); Println("Quad Tree Intersections", quadTreeResults); contourQuadTree.SaveImage(@"C:\Users\Chibuike.Okpaluba\Downloads\quad_tree_v2.png"); }