// find intersection between 2 segments. if they do intersect return new segments after // splitting originals on the intersection point. public List<Segment2D> Intersect(Segment2D seg, Path2D path) { // quickly eliminate non adjacent lines if ((seg.pmax.y <= pmin.y) || (seg.pmin.y >= pmax.y) || (seg.pmax.x <= pmin.x) || (seg.pmin.x >= pmax.x)) return null; // special case 1: sigments have same endpoints, nothing to do if ((Object.ReferenceEquals(p1, seg.p1) && Object.ReferenceEquals(p2, seg.p2)) || (Object.ReferenceEquals(p2, seg.p1) && Object.ReferenceEquals(p1, seg.p2))) return null; double x21 = p2.x - p1.x; double x43 = seg.p2.x - seg.p1.x; double y21 = p2.y - p1.y; double y43 = seg.p2.y - seg.p1.y; double c = x21 * y43 - y21 * x43; List<Segment2D> segments; // if lines are parrallel, nothing to do if (Math.Abs(c) <= Path2D.DEpsilon) return null; // lines are not parallel. if both lines share any edge, nothing to do if (Object.ReferenceEquals(p1, seg.p1) || Object.ReferenceEquals(p2, seg.p2) || Object.ReferenceEquals(p2, seg.p1) || Object.ReferenceEquals(p1, seg.p2)) return null; double x31 = seg.p1.x - p1.x; double y31 = seg.p1.y - p1.y; double u = (y43 * x31 - x43 * y31) / c; double v = (y21 * x31 - x21 * y31) / c; // if u or v are outside the 0-1 range, no intersection if ((u < -Path2D.Epsilon) || (u > 1 + Path2D.Epsilon) || (v < -Path2D.Epsilon) || (v > 1 + Path2D.Epsilon)) return null; // get intersection point Point2D ip = path.GetCreatePoint(p1.x + u * x21, p1.y + u * y21); segments = new List<Segment2D>(); // if ip is not on edge of lines, split them if (!Object.ReferenceEquals(p1, ip) && !Object.ReferenceEquals(p2, ip)) segments.AddRange(SplitAt(ip)); else segments.Add(this); if (!Object.ReferenceEquals(seg.p1, ip) && !Object.ReferenceEquals(seg.p2, ip)) segments.AddRange(seg.SplitAt(ip)); else segments.Add(seg); return segments; }
/// <summary> /// This will be called when we're exporting /// </summary> /// <param name="scenename"></param> /// <param name="layer"></param> /// <param name="numslices"></param> /// <param name="bmp"></param> /// <param name="lstPoly"></param> private void LayerSliced(string scenename, int layer, int numslices, Bitmap bmp, List<PolyLine3d> lstintersections) { string path; try { // if (m_buildparms.exportimages) { // get the model name String modelname = scenename; // strip off the file extension path = SliceFile.GetSliceFilePath(modelname); String imname = Path.GetFileNameWithoutExtension(modelname) + String.Format("{0:0000}", layer) + ".png"; String imagename = path + UVDLPApp.m_pathsep + imname; //if (m_sf.m_config.m_exportopt.ToUpper().Contains("ZIP")) { // create a memory stream for this to save into MemoryStream ms = new MemoryStream(); bmp.Save(ms, ImageFormat.Png); ms.Seek(0, SeekOrigin.Begin); // seek back to beginning if (!m_cancel) // if we're not in the process of cancelling { SceneFile.Instance().AddSlice(UVDLPApp.Instance().SceneFileName,ms, imname); } } if (m_sf.m_config.exportpng) { //imagename bmp.Save(imagename); } if (lstintersections != null) { StreamWriter sw; imname = Path.GetFileNameWithoutExtension(modelname) + String.Format("{0:0000}", layer) + ".svg"; imagename = path + UVDLPApp.m_pathsep + imname; if (m_sf.m_config.exportsvg < 3) { Path2D vectorPath = new Path2D(lstintersections); sw = vectorPath.GenerateSVG(UVDLPApp.Instance().m_printerinfo.m_PlatXSize, UVDLPApp.Instance().m_printerinfo.m_PlatYSize, m_sf.m_config.exportsvg == 2); } else { Slice sl = new Slice(); sl.m_segments = lstintersections; sl.Optimize(); sw = GenerateSVG(sl.m_opsegs, m_sf.m_config.exportsvg == 4); } if (!m_cancel) { SceneFile.Instance().AddVectorSlice(UVDLPApp.Instance().SceneFileName,(MemoryStream)sw.BaseStream, imname); } } RaiseSliceEvent(eSliceEvent.eLayerSliced, layer, numslices); } } catch (Exception ex) { string s = ex.StackTrace; DebugLogger.Instance().LogError(ex.Message); } }
// find intersection between 2 segments. if they do intersect return new segments after // splitting originals on the intersection point. public List <Segment2D> Intersect(Segment2D seg, Path2D path) { // quickly eliminate non adjacent lines if ((seg.pmax.y <= pmin.y) || (seg.pmin.y >= pmax.y) || (seg.pmax.x <= pmin.x) || (seg.pmin.x >= pmax.x)) { return(null); } // special case 1: sigments have same endpoints, nothing to do if ((Object.ReferenceEquals(p1, seg.p1) && Object.ReferenceEquals(p2, seg.p2)) || (Object.ReferenceEquals(p2, seg.p1) && Object.ReferenceEquals(p1, seg.p2))) { return(null); } double x21 = p2.x - p1.x; double x43 = seg.p2.x - seg.p1.x; double y21 = p2.y - p1.y; double y43 = seg.p2.y - seg.p1.y; double c = x21 * y43 - y21 * x43; List <Segment2D> segments; // if lines are parrallel, nothing to do if (Math.Abs(c) <= Path2D.DEpsilon) { return(null); } // lines are not parallel. if both lines share any edge, nothing to do if (Object.ReferenceEquals(p1, seg.p1) || Object.ReferenceEquals(p2, seg.p2) || Object.ReferenceEquals(p2, seg.p1) || Object.ReferenceEquals(p1, seg.p2)) { return(null); } double x31 = seg.p1.x - p1.x; double y31 = seg.p1.y - p1.y; double u = (y43 * x31 - x43 * y31) / c; double v = (y21 * x31 - x21 * y31) / c; // if u or v are outside the 0-1 range, no intersection if ((u < -Path2D.Epsilon) || (u > 1 + Path2D.Epsilon) || (v < -Path2D.Epsilon) || (v > 1 + Path2D.Epsilon)) { return(null); } // get intersection point Point2D ip = path.GetCreatePoint(p1.x + u * x21, p1.y + u * y21); segments = new List <Segment2D>(); // if ip is not on edge of lines, split them if (!Object.ReferenceEquals(p1, ip) && !Object.ReferenceEquals(p2, ip)) { segments.AddRange(SplitAt(ip)); } else { segments.Add(this); } if (!Object.ReferenceEquals(seg.p1, ip) && !Object.ReferenceEquals(seg.p2, ip)) { segments.AddRange(seg.SplitAt(ip)); } else { segments.Add(seg); } return(segments); }