diff --git a/.travis.yml b/.travis.yml new file mode 100755 index 0000000000..9d537d16ba --- /dev/null +++ b/.travis.yml @@ -0,0 +1,53 @@ +language: cpp + +before_install: + - sudo apt-get update -qq + - sudo apt-get install -y doxygen + - sudo apt-get install -y libboost-dev + - sudo apt-get install -y libboost-filesystem-dev + - sudo apt-get install -y libboost-program-options-dev + - sudo apt-get install -y libboost-python-dev + - sudo apt-get install -y libboost-regex-dev + - sudo apt-get install -y libboost-signals-dev + - sudo apt-get install -y libboost-system-dev + - sudo apt-get install -y libboost-thread-dev + - sudo apt-get install -y libcoin60 + - sudo apt-get install -y libcoin60-dev + - sudo apt-get install -y libeigen3-dev + - sudo apt-get install -y liboce-foundation-dev + - sudo apt-get install -y liboce-foundation1 + - sudo apt-get install -y liboce-modeling-dev + - sudo apt-get install -y liboce-modeling1 + - sudo apt-get install -y liboce-ocaf-dev + - sudo apt-get install -y liboce-ocaf-lite-dev + - sudo apt-get install -y liboce-ocaf-lite1 + - sudo apt-get install -y liboce-ocaf1 + - sudo apt-get install -y liboce-visualization-dev + - sudo apt-get install -y liboce-visualization1 + - sudo apt-get install -y libopencascade-modeling-6.5.0 + - sudo apt-get install -y libpyside-dev + - sudo apt-get install -y libqtcore4 + - sudo apt-get install -y libshiboken-dev + - sudo apt-get install -y libxerces-c-dev + - sudo apt-get install -y libxmu-dev + - sudo apt-get install -y libxmu-headers + - sudo apt-get install -y libxmu6 + - sudo apt-get install -y libxmuu-dev + - sudo apt-get install -y libxmuu1 + - sudo apt-get install -y oce-draw + - sudo apt-get install -y pyside-tools + - sudo apt-get install -y python-dev + - sudo apt-get install -y python-pyside + - sudo apt-get install -y qt4-dev-tools + - sudo apt-get install -y qt4-qmake + - sudo apt-get install -y shiboken + - sudo apt-get install -y swig +#Patch the system - there is a bug related to invalid location of libs on ubuntu 12.04 + - sudo ln -s /usr/lib/x86_64-linux-gnu/ /usr/lib/i386-linux-gnu + +install: + - mkdir build && cd build && cmake ../ + +script: + - make + - PYTHONPATH=$(pwd)/lib/ python -c "import sys, unittest, FreeCAD, TestApp; sys.exit(0 if unittest.TextTestRunner().run(TestApp.All()).wasSuccessful() else 1)" diff --git a/src/Mod/Arch/importIFC.py b/src/Mod/Arch/importIFC.py index ab6d3b11b8..bd7082f940 100644 --- a/src/Mod/Arch/importIFC.py +++ b/src/Mod/Arch/importIFC.py @@ -253,8 +253,11 @@ def explore(filename=None): def open(filename,skip=[],only=[],root=None): "opens an IFC file in a new document" - + docname = os.path.splitext(os.path.basename(filename))[0] + if isinstance(docname,unicode): + import sys #workaround since newDocument currently can't handle unicode filenames + docname = docname.encode(sys.getfilesystemencoding()) doc = FreeCAD.newDocument(docname) doc.Label = docname doc = insert(filename,doc.Name,skip,only,root) @@ -666,10 +669,6 @@ def export(exportList,filename): except: FreeCAD.Console.PrintError("IfcOpenShell was not found on this system. IFC support is disabled\n") return - - if isinstance(filename,unicode): - import sys #workaround since ifcopenshell currently can't handle unicode filenames - filename = filename.encode(sys.getfilesystemencoding()) version = FreeCAD.Version() owner = FreeCAD.ActiveDocument.CreatedBy @@ -690,7 +689,7 @@ def export(exportList,filename): ifctemplate = ifctemplate.replace("$timestamp",str(time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime()))) template = tempfile.mkstemp(suffix=".ifc")[1] of = pyopen(template,"wb") - of.write(ifctemplate) + of.write(ifctemplate.encode("utf8")) of.close() global ifcfile, surfstyles ifcfile = ifcopenshell.open(template) @@ -877,6 +876,11 @@ def export(exportList,filename): ifcfile.createIfcRelAssociatesMaterial(ifcopenshell.guid.compress(uuid.uuid1().hex),history,'MaterialLink','',relobjs,mat) if DEBUG: print "writing ",filename,"..." + + if isinstance(filename,unicode): + import sys #workaround since ifcopenshell currently can't handle unicode filenames + filename = filename.encode(sys.getfilesystemencoding()) + ifcfile.write(filename) diff --git a/src/Mod/Draft/importDXF.py b/src/Mod/Draft/importDXF.py index 481fde4e23..2e2c195e1f 100644 --- a/src/Mod/Draft/importDXF.py +++ b/src/Mod/Draft/importDXF.py @@ -1467,6 +1467,9 @@ def open(filename): readPreferences() if dxfReader: docname = os.path.splitext(os.path.basename(filename))[0] + if isinstance(docname,unicode): + import sys #workaround since newDocument currently can't handle unicode filenames + docname = docname.encode(sys.getfilesystemencoding()) doc = FreeCAD.newDocument(docname) doc.Label = decodeName(docname) processdxf(doc,filename) diff --git a/src/Mod/Path/CMakeLists.txt b/src/Mod/Path/CMakeLists.txt index b06b5c077d..e66a3525fb 100644 --- a/src/Mod/Path/CMakeLists.txt +++ b/src/Mod/Path/CMakeLists.txt @@ -1,4 +1,5 @@ add_subdirectory(App) +add_subdirectory(libarea) if(BUILD_GUI) add_subdirectory(Gui) diff --git a/src/Mod/Path/PathScripts/PathMachine.py b/src/Mod/Path/PathScripts/PathMachine.py index 225601205f..dd39c93d32 100644 --- a/src/Mod/Path/PathScripts/PathMachine.py +++ b/src/Mod/Path/PathScripts/PathMachine.py @@ -44,6 +44,7 @@ class Machine: obj.addProperty("App::PropertyString", "MachineName","Base",translate("Machine Name","Name of the Machine that will use the CNC program")) obj.addProperty("App::PropertyFile", "PostProcessor", "CodeOutput", translate("Post Processor","Select the Post Processor file for this machine")) + obj.setEditorMode("PostProcessor",1) #set to read only obj.addProperty("App::PropertyEnumeration", "MachineUnits","CodeOutput", translate( "Machine Units", "Units that the machine works in, ie Metric or Inch")) obj.MachineUnits=['Metric', 'Inch'] diff --git a/src/Mod/Path/PathScripts/PathProject.py b/src/Mod/Path/PathScripts/PathProject.py index 71b5680e60..5b228a20b3 100644 --- a/src/Mod/Path/PathScripts/PathProject.py +++ b/src/Mod/Path/PathScripts/PathProject.py @@ -43,6 +43,7 @@ class ObjectPathProject: def __init__(self,obj): # obj.addProperty("App::PropertyFile", "PostProcessor", "CodeOutput", translate("PostProcessor","Select the Post Processor file for this project")) obj.addProperty("App::PropertyFile", "OutputFile", "CodeOutput", translate("OutputFile","The NC output file for this project")) + obj.setEditorMode("OutputFile",0) #set to default mode # obj.addProperty("App::PropertyBool","Editor","CodeOutput",translate("Show Editor","Show G-Code in simple editor after posting code")) # obj.addProperty("Path::PropertyTooltable","Tooltable", "Path",translate("PathProject","The tooltable of this feature")) obj.addProperty("App::PropertyString", "Description","Path",translate("PathProject","An optional description for this project")) diff --git a/src/Mod/Path/PathScripts/PathSelection.py b/src/Mod/Path/PathScripts/PathSelection.py index 4427ababe8..4458a56399 100644 --- a/src/Mod/Path/PathScripts/PathSelection.py +++ b/src/Mod/Path/PathScripts/PathSelection.py @@ -27,9 +27,22 @@ import FreeCAD,FreeCADGui import Part from FreeCAD import Vector +def equals(p1,p2): + '''returns True if vertexes have same coordinates within precision amount of digits ''' + precision = 12 #hardcoded + p=precision + u = Vector(p1.X,p1.Y,p1.Z) + v = Vector(p2.X,p2.Y,p2.Z) + vector = (u.sub(v)) + isNull = (round(vector.x,p)==0 and round(vector.y,p)==0 and round(vector.z,p)==0) + return isNull + + def Sort2Edges(edgelist): - '''Sort2Edges(edgelist) simple function to reorder the start and end pts of two edges based on their selection order. Returns the list, the start point, and their common point, => edgelist, vertex, vertex''' + '''Sort2Edges(edgelist) simple function to reorder the start and end pts of two edges + based on their selection order. Returns the list, the start point, + and their common point, => edgelist, vertex, vertex''' if len(edgelist)>=2: vlist = [] e0 = edgelist[0] @@ -39,22 +52,22 @@ def Sort2Edges(edgelist): b0 = e1.Vertexes[0] b1 = e1.Vertexes[1] # comparison routine to order two edges: - if a1.isSame(b0): + if equals(a1,b0): vlist.append((a0.Point.x,a0.Point.y)) vlist.append((a1.Point.x,a1.Point.y)) vlist.append((b1.Point.x,b1.Point.y)) - elif a0.isSame(b0): + if equals(a0,b0): vlist.append((a1.Point.x,a1.Point.y)) vlist.append((a0.Point.x,a0.Point.y)) vlist.append((b1.Point.x,b1.Point.y)) - elif a0.isSame(b1): + if equals(a0,b1): vlist.append((a1.Point.x,a1.Point.y)) vlist.append((a0.Point.x,a0.Point.y)) vlist.append((b0.Point.x,b0.Point.y)) - elif a1.isSame(b1): + if equals(a1,b1): vlist.append((a0.Point.x,a0.Point.y)) vlist.append((a1.Point.x,a1.Point.y)) vlist.append((b0.Point.x,b0.Point.y)) diff --git a/src/Mod/Path/libarea/Arc.cpp b/src/Mod/Path/libarea/Arc.cpp new file mode 100644 index 0000000000..cf76557ee9 --- /dev/null +++ b/src/Mod/Path/libarea/Arc.cpp @@ -0,0 +1,147 @@ +// Arc.cpp + +/*============================== +Copyright (c) 2011-2015 Dan Heeks + +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. +3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. +==============================*/ + + + +#include "Arc.h" +#include "Curve.h" + +void CArc::SetDirWithPoint(const Point& p) +{ + double angs = atan2(m_s.y - m_c.y, m_s.x - m_c.x); + double ange = atan2(m_e.y - m_c.y, m_e.x - m_c.x); + double angp = atan2(p.y - m_c.y, p.x - m_c.x); + if(ange < angs)ange += 6.2831853071795864; + if(angp < angs - 0.0000000000001)angp += 6.2831853071795864; + if(angp > ange + 0.0000000000001)m_dir = false; + else m_dir = true; +} + +double CArc::IncludedAngle()const +{ + double angs = atan2(m_s.y - m_c.y, m_s.x - m_c.x); + double ange = atan2(m_e.y - m_c.y, m_e.x - m_c.x); + if(m_dir) + { + // make sure ange > angs + if(ange < angs)ange += 6.2831853071795864; + } + else + { + // make sure angs > ange + if(angs < ange)angs += 6.2831853071795864; + } + + return fabs(ange - angs); +} + +bool CArc::AlmostALine()const +{ + Point mid_point = MidParam(0.5); + if(Line(m_s, m_e - m_s).Dist(mid_point) <= Point::tolerance) + return true; + + const double max_arc_radius = 1.0 / Point::tolerance; + double radius = m_c.dist(m_s); + if (radius > max_arc_radius) + { + return true; // We don't want to produce an arc whose radius is too large. + } + + return false; +} + +Point CArc::MidParam(double param)const { + /// returns a point which is 0-1 along arc + if(fabs(param) < 0.00000000000001)return m_s; + if(fabs(param - 1.0) < 0.00000000000001)return m_e; + + Point p; + Point v = m_s - m_c; + v.Rotate(param * IncludedAngle()); + p = v + m_c; + + return p; +} + +//segments - number of segments per full revolution! +//d_angle - determines the direction and the ammount of the arc to draw +void CArc::GetSegments(void(*callbackfunc)(const double *p), double pixels_per_mm, bool want_start_point)const +{ + if(m_s == m_e) + return; + + Point Va = m_s - m_c; + Point Vb = m_e - m_c; + + double start_angle = atan2(Va.y, Va.x); + double end_angle = atan2(Vb.y, Vb.x); + + if(m_dir) + { + if(start_angle > end_angle)end_angle += 6.28318530717958; + } + else + { + if(start_angle < end_angle)end_angle -= 6.28318530717958; + } + + double radius = m_c.dist(m_s); + double d_angle = end_angle - start_angle; + int segments = (int)(fabs(pixels_per_mm * radius * d_angle / 6.28318530717958 + 1)); + + double theta = d_angle / (double)segments; + while(theta>1.0){segments*=2;theta = d_angle / (double)segments;} + double tangetial_factor = tan(theta); + double radial_factor = 1 - cos(theta); + + double x = radius * cos(start_angle); + double y = radius * sin(start_angle); + + double pp[3] = {0.0, 0.0, 0.0}; + + for(int i = 0; i < segments + 1; i++) + { + Point p = m_c + Point(x, y); + pp[0] = p.x; + pp[1] = p.y; + (*callbackfunc)(pp); + + double tx = -y; + double ty = x; + + x += tx * tangetial_factor; + y += ty * tangetial_factor; + + double rx = - x; + double ry = - y; + + x += rx * radial_factor; + y += ry * radial_factor; + } +} \ No newline at end of file diff --git a/src/Mod/Path/libarea/Arc.h b/src/Mod/Path/libarea/Arc.h new file mode 100644 index 0000000000..9455a84706 --- /dev/null +++ b/src/Mod/Path/libarea/Arc.h @@ -0,0 +1,48 @@ +// Arc.h +/*============================== +Copyright (c) 2011-2015 Dan Heeks + +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. +3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. +==============================*/ + +#pragma once + +#include "Point.h" + +class CArc{ +public: + Point m_s; + Point m_e; + Point m_c; + bool m_dir; // true - anti-clockwise, false - clockwise + int m_user_data; + + CArc():m_dir(true), m_user_data(0){} + CArc(const Point& s, const Point& e, const Point& c, bool dir, int user_data):m_s(s), m_e(e), m_c(c), m_dir(dir), m_user_data(user_data){} + + void SetDirWithPoint(const Point& p); // set m_dir, such that this point lies between m_s and m_e + double IncludedAngle()const; // always > 0 + bool AlmostALine()const; + Point MidParam(double param)const; + void GetSegments(void(*callbackfunc)(const double *p), double pixels_per_mm, bool want_start_point = true)const; +}; \ No newline at end of file diff --git a/src/Mod/Path/libarea/Area.cpp b/src/Mod/Path/libarea/Area.cpp new file mode 100644 index 0000000000..c0d1e92859 --- /dev/null +++ b/src/Mod/Path/libarea/Area.cpp @@ -0,0 +1,751 @@ +// Area.cpp + +/*============================== +Copyright (c) 2011-2015 Dan Heeks + +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. +3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. +==============================*/ + + +#include "Area.h" +#include "AreaOrderer.h" + +#include + +double CArea::m_accuracy = 0.01; +double CArea::m_units = 1.0; +bool CArea::m_fit_arcs = true; +double CArea::m_single_area_processing_length = 0.0; +double CArea::m_processing_done = 0.0; +bool CArea::m_please_abort = false; +double CArea::m_MakeOffsets_increment = 0.0; +double CArea::m_split_processing_length = 0.0; +bool CArea::m_set_processing_length_in_split = false; +double CArea::m_after_MakeOffsets_length = 0.0; +//static const double PI = 3.1415926535897932; + +void CArea::append(const CCurve& curve) +{ + m_curves.push_back(curve); +} + +void CArea::FitArcs(){ + for(std::list::iterator It = m_curves.begin(); It != m_curves.end(); It++) + { + CCurve& curve = *It; + curve.FitArcs(); + } +} + +Point CArea::NearestPoint(const Point& p)const +{ + double best_dist = 0.0; + Point best_point = Point(0, 0); + for(std::list::const_iterator It = m_curves.begin(); It != m_curves.end(); It++) + { + const CCurve& curve = *It; + Point near_point = curve.NearestPoint(p); + double dist = near_point.dist(p); + if(It == m_curves.begin() || dist < best_dist) + { + best_dist = dist; + best_point = near_point; + } + } + return best_point; +} + +void CArea::GetBox(CBox2D &box) +{ + for(std::list::iterator It = m_curves.begin(); It != m_curves.end(); It++) + { + CCurve& curve = *It; + curve.GetBox(box); + } +} + +void CArea::Reorder() +{ + // curves may have been added with wrong directions + // test all kurves to see which one are outsides and which are insides and + // make sure outsides are anti-clockwise and insides are clockwise + + // returns 0, if the curves are OK + // returns 1, if the curves are overlapping + + CAreaOrderer ao; + for(std::list::iterator It = m_curves.begin(); It != m_curves.end(); It++) + { + CCurve& curve = *It; + ao.Insert(&curve); + if(m_set_processing_length_in_split) + { + CArea::m_processing_done += (m_split_processing_length / m_curves.size()); + } + } + + *this = ao.ResultArea(); +} + +class ZigZag +{ +public: + CCurve zig; + CCurve zag; + ZigZag(const CCurve& Zig, const CCurve& Zag):zig(Zig), zag(Zag){} +}; + +static double stepover_for_pocket = 0.0; +static std::list zigzag_list_for_zigs; +static std::list *curve_list_for_zigs = NULL; +static bool rightward_for_zigs = true; +static double sin_angle_for_zigs = 0.0; +static double cos_angle_for_zigs = 0.0; +static double sin_minus_angle_for_zigs = 0.0; +static double cos_minus_angle_for_zigs = 0.0; +static double one_over_units = 0.0; + +static Point rotated_point(const Point &p) +{ + return Point(p.x * cos_angle_for_zigs - p.y * sin_angle_for_zigs, p.x * sin_angle_for_zigs + p.y * cos_angle_for_zigs); +} + +static Point unrotated_point(const Point &p) +{ + return Point(p.x * cos_minus_angle_for_zigs - p.y * sin_minus_angle_for_zigs, p.x * sin_minus_angle_for_zigs + p.y * cos_minus_angle_for_zigs); +} + +static CVertex rotated_vertex(const CVertex &v) +{ + if(v.m_type) + { + return CVertex(v.m_type, rotated_point(v.m_p), rotated_point(v.m_c)); + } + return CVertex(v.m_type, rotated_point(v.m_p), Point(0, 0)); +} + +static CVertex unrotated_vertex(const CVertex &v) +{ + if(v.m_type) + { + return CVertex(v.m_type, unrotated_point(v.m_p), unrotated_point(v.m_c)); + } + return CVertex(v.m_type, unrotated_point(v.m_p), Point(0, 0)); +} + +static void rotate_area(CArea &a) +{ + for(std::list::iterator It = a.m_curves.begin(); It != a.m_curves.end(); It++) + { + CCurve& curve = *It; + for(std::list::iterator CIt = curve.m_vertices.begin(); CIt != curve.m_vertices.end(); CIt++) + { + CVertex& vt = *CIt; + vt = rotated_vertex(vt); + } + } +} + +void test_y_point(int i, const Point& p, Point& best_p, bool &found, int &best_index, double y, bool left_not_right) +{ + // only consider points at y + if(fabs(p.y - y) < 0.002 * one_over_units) + { + if(found) + { + // equal high point + if(left_not_right) + { + // use the furthest left point + if(p.x < best_p.x) + { + best_p = p; + best_index = i; + } + } + else + { + // use the furthest right point + if(p.x > best_p.x) + { + best_p = p; + best_index = i; + } + } + } + else + { + best_p = p; + best_index = i; + found = true; + } + } +} + +static void make_zig_curve(const CCurve& input_curve, double y0, double y) +{ + CCurve curve(input_curve); + + if(rightward_for_zigs) + { + if(curve.IsClockwise()) + curve.Reverse(); + } + else + { + if(!curve.IsClockwise()) + curve.Reverse(); + } + + // find a high point to start looking from + Point top_left; + int top_left_index; + bool top_left_found = false; + Point top_right; + int top_right_index; + bool top_right_found = false; + Point bottom_left; + int bottom_left_index; + bool bottom_left_found = false; + + int i =0; + for(std::list::const_iterator VIt = curve.m_vertices.begin(); VIt != curve.m_vertices.end(); VIt++, i++) + { + const CVertex& vertex = *VIt; + + test_y_point(i, vertex.m_p, top_right, top_right_found, top_right_index, y, !rightward_for_zigs); + test_y_point(i, vertex.m_p, top_left, top_left_found, top_left_index, y, rightward_for_zigs); + test_y_point(i, vertex.m_p, bottom_left, bottom_left_found, bottom_left_index, y0, rightward_for_zigs); + } + + int start_index = 0; + int end_index = 0; + int zag_end_index = 0; + + if(bottom_left_found)start_index = bottom_left_index; + else if(top_left_found)start_index = top_left_index; + + if(top_right_found) + { + end_index = top_right_index; + zag_end_index = top_left_index; + } + else + { + end_index = bottom_left_index; + zag_end_index = bottom_left_index; + } + if(end_index <= start_index)end_index += (i-1); + if(zag_end_index <= start_index)zag_end_index += (i-1); + + CCurve zig, zag; + + bool zig_started = false; + bool zig_finished = false; + bool zag_finished = false; + + int v_index = 0; + for(int i = 0; i < 2; i++) + { + // process the curve twice because we don't know where it will start + if(zag_finished) + break; + for(std::list::const_iterator VIt = curve.m_vertices.begin(); VIt != curve.m_vertices.end(); VIt++) + { + if(i == 1 && VIt == curve.m_vertices.begin()) + { + continue; + } + + const CVertex& vertex = *VIt; + + if(zig_finished) + { + zag.m_vertices.push_back(unrotated_vertex(vertex)); + if(v_index == zag_end_index) + { + zag_finished = true; + break; + } + } + else if(zig_started) + { + zig.m_vertices.push_back(unrotated_vertex(vertex)); + if(v_index == end_index) + { + zig_finished = true; + if(v_index == zag_end_index) + { + zag_finished = true; + break; + } + zag.m_vertices.push_back(unrotated_vertex(vertex)); + } + } + else + { + if(v_index == start_index) + { + zig.m_vertices.push_back(unrotated_vertex(vertex)); + zig_started = true; + } + } + v_index++; + } + } + + if(zig_finished) + zigzag_list_for_zigs.push_back(ZigZag(zig, zag)); +} + +void make_zig(const CArea &a, double y0, double y) +{ + for(std::list::const_iterator It = a.m_curves.begin(); It != a.m_curves.end(); It++) + { + const CCurve &curve = *It; + make_zig_curve(curve, y0, y); + } +} + +std::list< std::list > reorder_zig_list_list; + +void add_reorder_zig(ZigZag &zigzag) +{ + // look in existing lists + + // see if the zag is part of an existing zig + if(zigzag.zag.m_vertices.size() > 1) + { + const Point& zag_e = zigzag.zag.m_vertices.front().m_p; + bool zag_removed = false; + for(std::list< std::list >::iterator It = reorder_zig_list_list.begin(); It != reorder_zig_list_list.end() && !zag_removed; It++) + { + std::list &zigzag_list = *It; + for(std::list::iterator It2 = zigzag_list.begin(); It2 != zigzag_list.end() && !zag_removed; It2++) + { + const ZigZag& z = *It2; + for(std::list::const_iterator It3 = z.zig.m_vertices.begin(); It3 != z.zig.m_vertices.end() && !zag_removed; It3++) + { + const CVertex &v = *It3; + if((fabs(zag_e.x - v.m_p.x) < (0.002 * one_over_units)) && (fabs(zag_e.y - v.m_p.y) < (0.002 * one_over_units))) + { + // remove zag from zigzag + zigzag.zag.m_vertices.clear(); + zag_removed = true; + } + } + } + } + } + + // see if the zigzag can join the end of an existing list + const Point& zig_s = zigzag.zig.m_vertices.front().m_p; + for(std::list< std::list >::iterator It = reorder_zig_list_list.begin(); It != reorder_zig_list_list.end(); It++) + { + std::list &zigzag_list = *It; + const ZigZag& last_zigzag = zigzag_list.back(); + const Point& e = last_zigzag.zig.m_vertices.back().m_p; + if((fabs(zig_s.x - e.x) < (0.002 * one_over_units)) && (fabs(zig_s.y - e.y) < (0.002 * one_over_units))) + { + zigzag_list.push_back(zigzag); + return; + } + } + + // else add a new list + std::list zigzag_list; + zigzag_list.push_back(zigzag); + reorder_zig_list_list.push_back(zigzag_list); +} + +void reorder_zigs() +{ + for(std::list::iterator It = zigzag_list_for_zigs.begin(); It != zigzag_list_for_zigs.end(); It++) + { + ZigZag &zigzag = *It; + add_reorder_zig(zigzag); + } + + zigzag_list_for_zigs.clear(); + + for(std::list< std::list >::iterator It = reorder_zig_list_list.begin(); It != reorder_zig_list_list.end(); It++) + { + std::list &zigzag_list = *It; + if(zigzag_list.size() == 0)continue; + + curve_list_for_zigs->push_back(CCurve()); + for(std::list::const_iterator It = zigzag_list.begin(); It != zigzag_list.end();) + { + const ZigZag &zigzag = *It; + for(std::list::const_iterator It2 = zigzag.zig.m_vertices.begin(); It2 != zigzag.zig.m_vertices.end(); It2++) + { + if(It2 == zigzag.zig.m_vertices.begin() && It != zigzag_list.begin())continue; // only add the first vertex if doing the first zig + const CVertex &v = *It2; + curve_list_for_zigs->back().m_vertices.push_back(v); + } + + It++; + if(It == zigzag_list.end()) + { + for(std::list::const_iterator It2 = zigzag.zag.m_vertices.begin(); It2 != zigzag.zag.m_vertices.end(); It2++) + { + if(It2 == zigzag.zag.m_vertices.begin())continue; // don't add the first vertex of the zag + const CVertex &v = *It2; + curve_list_for_zigs->back().m_vertices.push_back(v); + } + } + } + } + reorder_zig_list_list.clear(); +} + +static void zigzag(const CArea &input_a) +{ + if(input_a.m_curves.size() == 0) + { + CArea::m_processing_done += CArea::m_single_area_processing_length; + return; + } + + one_over_units = 1 / CArea::m_units; + + CArea a(input_a); + rotate_area(a); + + CBox2D b; + a.GetBox(b); + + double x0 = b.MinX() - 1.0; + double x1 = b.MaxX() + 1.0; + + double height = b.MaxY() - b.MinY(); + int num_steps = int(height / stepover_for_pocket + 1); + double y = b.MinY();// + 0.1 * one_over_units; + Point null_point(0, 0); + rightward_for_zigs = true; + + if(CArea::m_please_abort)return; + + double step_percent_increment = 0.8 * CArea::m_single_area_processing_length / num_steps; + + for(int i = 0; i &curve_list, const CAreaPocketParams ¶ms)const +{ + CArea::m_processing_done = 0.0; + + double save_units = CArea::m_units; + CArea::m_units = 1.0; + std::list areas; + m_split_processing_length = 50.0; // jump to 50 percent after split + m_set_processing_length_in_split = true; + Split(areas); + m_set_processing_length_in_split = false; + CArea::m_processing_done = m_split_processing_length; + CArea::m_units = save_units; + + if(areas.size() == 0)return; + + double single_area_length = 50.0 / areas.size(); + + for(std::list::iterator It = areas.begin(); It != areas.end(); It++) + { + CArea::m_single_area_processing_length = single_area_length; + CArea &ar = *It; + ar.MakePocketToolpath(curve_list, params); + } +} + +void CArea::MakePocketToolpath(std::list &curve_list, const CAreaPocketParams ¶ms)const +{ + double radians_angle = params.zig_angle * PI / 180; + sin_angle_for_zigs = sin(-radians_angle); + cos_angle_for_zigs = cos(-radians_angle); + sin_minus_angle_for_zigs = sin(radians_angle); + cos_minus_angle_for_zigs = cos(radians_angle); + stepover_for_pocket = params.stepover; + + CArea a_offset = *this; + double current_offset = params.tool_radius + params.extra_offset; + + a_offset.Offset(current_offset); + + if(params.mode == ZigZagPocketMode || params.mode == ZigZagThenSingleOffsetPocketMode) + { + curve_list_for_zigs = &curve_list; + zigzag(a_offset); + } + else if(params.mode == SpiralPocketMode) + { + std::list m_areas; + a_offset.Split(m_areas); + if(CArea::m_please_abort)return; + if(m_areas.size() == 0) + { + CArea::m_processing_done += CArea::m_single_area_processing_length; + return; + } + + CArea::m_single_area_processing_length /= m_areas.size(); + + for(std::list::iterator It = m_areas.begin(); It != m_areas.end(); It++) + { + CArea &a2 = *It; + a2.MakeOnePocketCurve(curve_list, params); + } + } + + if(params.mode == SingleOffsetPocketMode || params.mode == ZigZagThenSingleOffsetPocketMode) + { + // add the single offset too + for(std::list::iterator It = a_offset.m_curves.begin(); It != a_offset.m_curves.end(); It++) + { + CCurve& curve = *It; + curve_list.push_back(curve); + } + } +} + +void CArea::Split(std::list &m_areas)const +{ + if(HolesLinked()) + { + for(std::list::const_iterator It = m_curves.begin(); It != m_curves.end(); It++) + { + const CCurve& curve = *It; + m_areas.push_back(CArea()); + m_areas.back().m_curves.push_back(curve); + } + } + else + { + CArea a = *this; + a.Reorder(); + + if(CArea::m_please_abort)return; + + for(std::list::const_iterator It = a.m_curves.begin(); It != a.m_curves.end(); It++) + { + const CCurve& curve = *It; + if(curve.IsClockwise()) + { + if(m_areas.size() > 0) + m_areas.back().m_curves.push_back(curve); + } + else + { + m_areas.push_back(CArea()); + m_areas.back().m_curves.push_back(curve); + } + } + } +} + +double CArea::GetArea(bool always_add)const +{ + // returns the area of the area + double area = 0.0; + for(std::list::const_iterator It = m_curves.begin(); It != m_curves.end(); It++) + { + const CCurve& curve = *It; + double a = curve.GetArea(); + if(always_add)area += fabs(a); + else area += a; + } + return area; +} + +eOverlapType GetOverlapType(const CCurve& c1, const CCurve& c2) +{ + CArea a1; + a1.m_curves.push_back(c1); + CArea a2; + a2.m_curves.push_back(c2); + + return GetOverlapType(a1, a2); +} + +eOverlapType GetOverlapType(const CArea& a1, const CArea& a2) +{ + CArea A1(a1); + + A1.Subtract(a2); + if(A1.m_curves.size() == 0) + { + return eInside; + } + + CArea A2(a2); + A2.Subtract(a1); + if(A2.m_curves.size() == 0) + { + return eOutside; + } + + A1 = a1; + A1.Intersect(a2); + if(A1.m_curves.size() == 0) + { + return eSiblings; + } + + return eCrossing; +} + +bool IsInside(const Point& p, const CCurve& c) +{ + CArea a; + a.m_curves.push_back(c); + return IsInside(p, a); +} + +bool IsInside(const Point& p, const CArea& a) +{ + CArea a2; + CCurve c; + c.m_vertices.push_back(CVertex(Point(p.x - 0.01, p.y - 0.01))); + c.m_vertices.push_back(CVertex(Point(p.x + 0.01, p.y - 0.01))); + c.m_vertices.push_back(CVertex(Point(p.x + 0.01, p.y + 0.01))); + c.m_vertices.push_back(CVertex(Point(p.x - 0.01, p.y + 0.01))); + c.m_vertices.push_back(CVertex(Point(p.x - 0.01, p.y - 0.01))); + a2.m_curves.push_back(c); + a2.Intersect(a); + if(fabs(a2.GetArea()) < 0.0004)return false; + return true; +} + +void CArea::SpanIntersections(const Span& span, std::list &pts)const +{ + // this returns all the intersections of this area with the given span, ordered along the span + + // get all points where this area's curves intersect the span + std::list pts2; + for(std::list::const_iterator It = m_curves.begin(); It != m_curves.end(); It++) + { + const CCurve &c = *It; + c.SpanIntersections(span, pts2); + } + + // order them along the span + std::multimap ordered_points; + for(std::list::iterator It = pts2.begin(); It != pts2.end(); It++) + { + Point &p = *It; + double t; + if(span.On(p, &t)) + { + ordered_points.insert(std::make_pair(t, p)); + } + } + + // add them to the given list of points + for(std::multimap::iterator It = ordered_points.begin(); It != ordered_points.end(); It++) + { + Point p = It->second; + pts.push_back(p); + } +} + +void CArea::CurveIntersections(const CCurve& curve, std::list &pts)const +{ + // this returns all the intersections of this area with the given curve, ordered along the curve + std::list spans; + curve.GetSpans(spans); + for(std::list::iterator It = spans.begin(); It != spans.end(); It++) + { + Span& span = *It; + std::list pts2; + SpanIntersections(span, pts2); + for(std::list::iterator It = pts2.begin(); It != pts2.end(); It++) + { + Point &pt = *It; + if(pts.size() == 0) + { + pts.push_back(pt); + } + else + { + if(pt != pts.back())pts.push_back(pt); + } + } + } +} + +class ThickLine +{ +public: + CArea m_area; + CCurve m_curve; + + ThickLine(const CCurve& curve) + { + m_curve = curve; + m_area.append(curve); + m_area.Thicken(0.001); + } +}; + +void CArea::InsideCurves(const CCurve& curve, std::list &curves_inside)const +{ + //1. find the intersectionpoints between these two curves. + std::list pts; + CurveIntersections(curve, pts); + + //2.seperate curve2 in multiple curves between these intersections. + std::list separate_curves; + curve.ExtractSeparateCurves(pts, separate_curves); + + //3. if the midpoint of a seperate curve lies in a1, then we return it. + for(std::list::iterator It = separate_curves.begin(); It != separate_curves.end(); It++) + { + CCurve &curve = *It; + double length = curve.Perim(); + Point mid_point = curve.PerimToPoint(length * 0.5); + if(IsInside(mid_point, *this))curves_inside.push_back(curve); + } +} diff --git a/src/Mod/Path/libarea/Area.h b/src/Mod/Path/libarea/Area.h new file mode 100644 index 0000000000..c23f8c58f8 --- /dev/null +++ b/src/Mod/Path/libarea/Area.h @@ -0,0 +1,113 @@ +// Area.h +/*============================== +Copyright (c) 2011-2015 Dan Heeks + +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. +3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. +==============================*/ + + +#ifndef AREA_HEADER +#define AREA_HEADER + +#include "Curve.h" + +enum PocketMode +{ + SpiralPocketMode, + ZigZagPocketMode, + SingleOffsetPocketMode, + ZigZagThenSingleOffsetPocketMode, +}; + +struct CAreaPocketParams +{ + double tool_radius; + double extra_offset; + double stepover; + bool from_center; + PocketMode mode; + double zig_angle; + bool only_cut_first_offset; + CAreaPocketParams(double Tool_radius, double Extra_offset, double Stepover, bool From_center, PocketMode Mode, double Zig_angle) + { + tool_radius = Tool_radius; + extra_offset = Extra_offset; + stepover = Stepover; + from_center = From_center; + mode = Mode; + zig_angle = Zig_angle; + } +}; + +class CArea +{ +public: + std::list m_curves; + static double m_accuracy; + static double m_units; // 1.0 for mm, 25.4 for inches. All points are multiplied by this before going to the engine + static bool m_fit_arcs; + static double m_processing_done; // 0.0 to 100.0, set inside MakeOnePocketCurve + static double m_single_area_processing_length; + static double m_after_MakeOffsets_length; + static double m_MakeOffsets_increment; + static double m_split_processing_length; + static bool m_set_processing_length_in_split; + static bool m_please_abort; // the user sets this from another thread, to tell MakeOnePocketCurve to finish with no result. + + void append(const CCurve& curve); + void Subtract(const CArea& a2); + void Intersect(const CArea& a2); + void Union(const CArea& a2); + void Xor(const CArea& a2); + void Offset(double inwards_value); + void Thicken(double value); + void FitArcs(); + unsigned int num_curves(){return m_curves.size();} + Point NearestPoint(const Point& p)const; + void GetBox(CBox2D &box); + void Reorder(); + void MakePocketToolpath(std::list &toolpath, const CAreaPocketParams ¶ms)const; + void SplitAndMakePocketToolpath(std::list &toolpath, const CAreaPocketParams ¶ms)const; + void MakeOnePocketCurve(std::list &curve_list, const CAreaPocketParams ¶ms)const; + static bool HolesLinked(); + void Split(std::list &m_areas)const; + double GetArea(bool always_add = false)const; + void SpanIntersections(const Span& span, std::list &pts)const; + void CurveIntersections(const CCurve& curve, std::list &pts)const; + void InsideCurves(const CCurve& curve, std::list &curves_inside)const; +}; + +enum eOverlapType +{ + eOutside, + eInside, + eSiblings, + eCrossing, +}; + +eOverlapType GetOverlapType(const CCurve& c1, const CCurve& c2); +eOverlapType GetOverlapType(const CArea& a1, const CArea& a2); +bool IsInside(const Point& p, const CCurve& c); +bool IsInside(const Point& p, const CArea& a); + +#endif // #define AREA_HEADER diff --git a/src/Mod/Path/libarea/AreaClipper.cpp b/src/Mod/Path/libarea/AreaClipper.cpp new file mode 100644 index 0000000000..4f662eb98f --- /dev/null +++ b/src/Mod/Path/libarea/AreaClipper.cpp @@ -0,0 +1,504 @@ +// AreaClipper.cpp +// implements CArea methods using Angus Johnson's "Clipper" + +/*============================== +Copyright (c) 2011-2015 Dan Heeks + +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. +3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. +==============================*/ + +#include "Area.h" +#include "clipper.hpp" +using namespace ClipperLib; + +#define TPolygon Path +#define TPolyPolygon Paths + +bool CArea::HolesLinked(){ return false; } + +//static const double PI = 3.1415926535897932; +static double Clipper4Factor = 10000.0; + +class DoubleAreaPoint +{ +public: + double X, Y; + + DoubleAreaPoint(double x, double y){X = x; Y = y;} + DoubleAreaPoint(const IntPoint& p){X = (double)(p.X) / Clipper4Factor; Y = (double)(p.Y) / Clipper4Factor;} + IntPoint int_point(){return IntPoint((long64)(X * Clipper4Factor), (long64)(Y * Clipper4Factor));} +}; + +static std::list pts_for_AddVertex; + +static void AddPoint(const DoubleAreaPoint& p) +{ + pts_for_AddVertex.push_back(p); +} + +static void AddVertex(const CVertex& vertex, const CVertex* prev_vertex) +{ + if(vertex.m_type == 0 || prev_vertex == NULL) + { + AddPoint(DoubleAreaPoint(vertex.m_p.x * CArea::m_units, vertex.m_p.y * CArea::m_units)); + } + else + { + if(vertex.m_p != prev_vertex->m_p) + { + double phi,dphi,dx,dy; + int Segments; + int i; + double ang1,ang2,phit; + + dx = (prev_vertex->m_p.x - vertex.m_c.x) * CArea::m_units; + dy = (prev_vertex->m_p.y - vertex.m_c.y) * CArea::m_units; + + ang1=atan2(dy,dx); + if (ang1<0) ang1+=2.0*PI; + dx = (vertex.m_p.x - vertex.m_c.x) * CArea::m_units; + dy = (vertex.m_p.y - vertex.m_c.y) * CArea::m_units; + ang2=atan2(dy,dx); + if (ang2<0) ang2+=2.0*PI; + + if (vertex.m_type == -1) + { //clockwise + if (ang2 > ang1) + phit=2.0*PI-ang2+ ang1; + else + phit=ang1-ang2; + } + else + { //counter_clockwise + if (ang1 > ang2) + phit=-(2.0*PI-ang1+ ang2); + else + phit=-(ang2-ang1); + } + + //what is the delta phi to get an accurancy of aber + double radius = sqrt(dx*dx + dy*dy); + dphi=2*acos((radius-CArea::m_accuracy)/radius); + + //set the number of segments + if (phit > 0) + Segments=(int)ceil(phit/dphi); + else + Segments=(int)ceil(-phit/dphi); + + if (Segments < 1) + Segments=1; + if (Segments > 100) + Segments=100; + + dphi=phit/(Segments); + + double px = prev_vertex->m_p.x * CArea::m_units; + double py = prev_vertex->m_p.y * CArea::m_units; + + for (i=1; i<=Segments; i++) + { + dx = px - vertex.m_c.x * CArea::m_units; + dy = py - vertex.m_c.y * CArea::m_units; + phi=atan2(dy,dx); + + double nx = vertex.m_c.x * CArea::m_units + radius * cos(phi-dphi); + double ny = vertex.m_c.y * CArea::m_units + radius * sin(phi-dphi); + + AddPoint(DoubleAreaPoint(nx, ny)); + + px = nx; + py = ny; + } + } + } +} + +static bool IsPolygonClockwise(const TPolygon& p) +{ +#if 1 + double area = 0.0; + unsigned int s = p.size(); + for(unsigned int i = 0; i 0.0; +#else + return IsClockwise(p); +#endif +} + +static void MakeLoop(const DoubleAreaPoint &pt0, const DoubleAreaPoint &pt1, const DoubleAreaPoint &pt2, double radius) +{ + Point p0(pt0.X, pt0.Y); + Point p1(pt1.X, pt1.Y); + Point p2(pt2.X, pt2.Y); + Point forward0 = p1 - p0; + Point right0(forward0.y, -forward0.x); + right0.normalize(); + Point forward1 = p2 - p1; + Point right1(forward1.y, -forward1.x); + right1.normalize(); + + int arc_dir = (radius > 0) ? 1 : -1; + + CVertex v0(0, p1 + right0 * radius, Point(0, 0)); + CVertex v1(arc_dir, p1 + right1 * radius, p1); + CVertex v2(0, p2 + right1 * radius, Point(0, 0)); + + double save_units = CArea::m_units; + CArea::m_units = 1.0; + + AddVertex(v1, &v0); + AddVertex(v2, &v1); + + CArea::m_units = save_units; +} + +static void OffsetWithLoops(const TPolyPolygon &pp, TPolyPolygon &pp_new, double inwards_value) +{ + Clipper c; + + bool inwards = (inwards_value > 0); + bool reverse = false; + double radius = -fabs(inwards_value); + + if(inwards) + { + // add a large square on the outside, to be removed later + TPolygon p; + p.push_back(DoubleAreaPoint(-10000.0, -10000.0).int_point()); + p.push_back(DoubleAreaPoint(-10000.0, 10000.0).int_point()); + p.push_back(DoubleAreaPoint(10000.0, 10000.0).int_point()); + p.push_back(DoubleAreaPoint(10000.0, -10000.0).int_point()); + c.AddPath(p, ptSubject, true); + } + else + { + reverse = true; + } + + for(unsigned int i = 0; i < pp.size(); i++) + { + const TPolygon& p = pp[i]; + + pts_for_AddVertex.clear(); + + if(p.size() > 2) + { + if(reverse) + { + for(unsigned int j = p.size()-1; j > 1; j--)MakeLoop(p[j], p[j-1], p[j-2], radius); + MakeLoop(p[1], p[0], p[p.size()-1], radius); + MakeLoop(p[0], p[p.size()-1], p[p.size()-2], radius); + } + else + { + MakeLoop(p[p.size()-2], p[p.size()-1], p[0], radius); + MakeLoop(p[p.size()-1], p[0], p[1], radius); + for(unsigned int j = 2; j < p.size(); j++)MakeLoop(p[j-2], p[j-1], p[j], radius); + } + + TPolygon loopy_polygon; + loopy_polygon.reserve(pts_for_AddVertex.size()); + for(std::list::iterator It = pts_for_AddVertex.begin(); It != pts_for_AddVertex.end(); It++) + { + loopy_polygon.push_back(It->int_point()); + } + c.AddPath(loopy_polygon, ptSubject, true); + pts_for_AddVertex.clear(); + } + } + + //c.ForceOrientation(false); + c.Execute(ctUnion, pp_new, pftNonZero, pftNonZero); + + if(inwards) + { + // remove the large square + if(pp_new.size() > 0) + { + pp_new.erase(pp_new.begin()); + } + } + else + { + // reverse all the resulting polygons + TPolyPolygon copy = pp_new; + pp_new.clear(); + pp_new.resize(copy.size()); + for(unsigned int i = 0; i < copy.size(); i++) + { + const TPolygon& p = copy[i]; + TPolygon p_new; + p_new.resize(p.size()); + int size_minus_one = p.size() - 1; + for(unsigned int j = 0; j < p.size(); j++)p_new[j] = p[size_minus_one - j]; + pp_new[i] = p_new; + } + } +} + +static void MakeObround(const Point &pt0, const CVertex &vt1, double radius) +{ + Span span(pt0, vt1); + Point forward0 = span.GetVector(0.0); + Point forward1 = span.GetVector(1.0); + Point right0(forward0.y, -forward0.x); + Point right1(forward1.y, -forward1.x); + right0.normalize(); + right1.normalize(); + + CVertex v0(pt0 + right0 * radius); + CVertex v1(vt1.m_type, vt1.m_p + right1 * radius, vt1.m_c); + CVertex v2(1, vt1.m_p + right1 * -radius, vt1.m_p); + CVertex v3(-vt1.m_type, pt0 + right0 * -radius, vt1.m_c); + CVertex v4(1, pt0 + right0 * radius, pt0); + + double save_units = CArea::m_units; + CArea::m_units = 1.0; + + AddVertex(v0, NULL); + AddVertex(v1, &v0); + AddVertex(v2, &v1); + AddVertex(v3, &v2); + AddVertex(v4, &v3); + + CArea::m_units = save_units; +} + +static void OffsetSpansWithObrounds(const CArea& area, TPolyPolygon &pp_new, double radius) +{ + Clipper c; + + + for(std::list::const_iterator It = area.m_curves.begin(); It != area.m_curves.end(); It++) + { + pts_for_AddVertex.clear(); + const CCurve& curve = *It; + const CVertex* prev_vertex = NULL; + for(std::list::const_iterator It2 = curve.m_vertices.begin(); It2 != curve.m_vertices.end(); It2++) + { + const CVertex& vertex = *It2; + if(prev_vertex) + { + MakeObround(prev_vertex->m_p, vertex, radius); + + TPolygon loopy_polygon; + loopy_polygon.reserve(pts_for_AddVertex.size()); + for(std::list::iterator It = pts_for_AddVertex.begin(); It != pts_for_AddVertex.end(); It++) + { + loopy_polygon.push_back(It->int_point()); + } + c.AddPath(loopy_polygon, ptSubject, true); + pts_for_AddVertex.clear(); + } + prev_vertex = &vertex; + } + } + + pp_new.clear(); + c.Execute(ctUnion, pp_new, pftNonZero, pftNonZero); + + // reverse all the resulting polygons + TPolyPolygon copy = pp_new; + pp_new.clear(); + pp_new.resize(copy.size()); + for(unsigned int i = 0; i < copy.size(); i++) + { + const TPolygon& p = copy[i]; + TPolygon p_new; + p_new.resize(p.size()); + int size_minus_one = p.size() - 1; + for(unsigned int j = 0; j < p.size(); j++)p_new[j] = p[size_minus_one - j]; + pp_new[i] = p_new; + } +} + +static void MakePolyPoly( const CArea& area, TPolyPolygon &pp, bool reverse = true ){ + pp.clear(); + + for(std::list::const_iterator It = area.m_curves.begin(); It != area.m_curves.end(); It++) + { + pts_for_AddVertex.clear(); + const CCurve& curve = *It; + const CVertex* prev_vertex = NULL; + for(std::list::const_iterator It2 = curve.m_vertices.begin(); It2 != curve.m_vertices.end(); It2++) + { + const CVertex& vertex = *It2; + if(prev_vertex)AddVertex(vertex, prev_vertex); + prev_vertex = &vertex; + } + + TPolygon p; + p.resize(pts_for_AddVertex.size()); + if(reverse) + { + unsigned int i = pts_for_AddVertex.size() - 1;// clipper wants them the opposite way to CArea + for(std::list::iterator It = pts_for_AddVertex.begin(); It != pts_for_AddVertex.end(); It++, i--) + { + p[i] = It->int_point(); + } + } + else + { + unsigned int i = 0; + for(std::list::iterator It = pts_for_AddVertex.begin(); It != pts_for_AddVertex.end(); It++, i++) + { + p[i] = It->int_point(); + } + } + + pp.push_back(p); + } +} + +static void SetFromResult( CCurve& curve, const TPolygon& p, bool reverse = true ) +{ + for(unsigned int j = 0; j < p.size(); j++) + { + const IntPoint &pt = p[j]; + DoubleAreaPoint dp(pt); + CVertex vertex(0, Point(dp.X / CArea::m_units, dp.Y / CArea::m_units), Point(0.0, 0.0)); + if(reverse)curve.m_vertices.push_front(vertex); + else curve.m_vertices.push_back(vertex); + } + // make a copy of the first point at the end + if(reverse)curve.m_vertices.push_front(curve.m_vertices.back()); + else curve.m_vertices.push_back(curve.m_vertices.front()); + + if(CArea::m_fit_arcs)curve.FitArcs(); +} + +static void SetFromResult( CArea& area, const TPolyPolygon& pp, bool reverse = true ) +{ + // delete existing geometry + area.m_curves.clear(); + + for(unsigned int i = 0; i < pp.size(); i++) + { + const TPolygon& p = pp[i]; + + area.m_curves.push_back(CCurve()); + CCurve &curve = area.m_curves.back(); + SetFromResult(curve, p, reverse); + } +} + +void CArea::Subtract(const CArea& a2) +{ + Clipper c; + TPolyPolygon pp1, pp2; + MakePolyPoly(*this, pp1); + MakePolyPoly(a2, pp2); + c.AddPaths(pp1, ptSubject, true); + c.AddPaths(pp2, ptClip, true); + TPolyPolygon solution; + c.Execute(ctDifference, solution); + SetFromResult(*this, solution); +} + +void CArea::Intersect(const CArea& a2) +{ + Clipper c; + TPolyPolygon pp1, pp2; + MakePolyPoly(*this, pp1); + MakePolyPoly(a2, pp2); + c.AddPaths(pp1, ptSubject, true); + c.AddPaths(pp2, ptClip, true); + TPolyPolygon solution; + c.Execute(ctIntersection, solution); + SetFromResult(*this, solution); +} + +void CArea::Union(const CArea& a2) +{ + Clipper c; + TPolyPolygon pp1, pp2; + MakePolyPoly(*this, pp1); + MakePolyPoly(a2, pp2); + c.AddPaths(pp1, ptSubject, true); + c.AddPaths(pp2, ptClip, true); + TPolyPolygon solution; + c.Execute(ctUnion, solution); + SetFromResult(*this, solution); +} + +void CArea::Xor(const CArea& a2) +{ + Clipper c; + TPolyPolygon pp1, pp2; + MakePolyPoly(*this, pp1); + MakePolyPoly(a2, pp2); + c.AddPaths(pp1, ptSubject, true); + c.AddPaths(pp2, ptClip, true); + TPolyPolygon solution; + c.Execute(ctXor, solution); + SetFromResult(*this, solution); +} + +void CArea::Offset(double inwards_value) +{ + TPolyPolygon pp, pp2; + MakePolyPoly(*this, pp, false); + OffsetWithLoops(pp, pp2, inwards_value * m_units); + SetFromResult(*this, pp2, false); + this->Reorder(); +} + +void CArea::Thicken(double value) +{ + TPolyPolygon pp; + OffsetSpansWithObrounds(*this, pp, value * m_units); + SetFromResult(*this, pp, false); + this->Reorder(); +} + +void UnFitArcs(CCurve &curve) +{ + pts_for_AddVertex.clear(); + const CVertex* prev_vertex = NULL; + for(std::list::const_iterator It2 = curve.m_vertices.begin(); It2 != curve.m_vertices.end(); It2++) + { + const CVertex& vertex = *It2; + AddVertex(vertex, prev_vertex); + prev_vertex = &vertex; + } + + curve.m_vertices.clear(); + + for(std::list::iterator It = pts_for_AddVertex.begin(); It != pts_for_AddVertex.end(); It++) + { + DoubleAreaPoint &pt = *It; + CVertex vertex(0, Point(pt.X / CArea::m_units, pt.Y / CArea::m_units), Point(0.0, 0.0)); + curve.m_vertices.push_back(vertex); + } +} \ No newline at end of file diff --git a/src/Mod/Path/libarea/AreaOrderer.cpp b/src/Mod/Path/libarea/AreaOrderer.cpp new file mode 100644 index 0000000000..c53c38d97c --- /dev/null +++ b/src/Mod/Path/libarea/AreaOrderer.cpp @@ -0,0 +1,178 @@ +// AreaOrderer.cpp + +/*============================== +Copyright (c) 2011-2015 Dan Heeks + +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. +3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. +==============================*/ + + +#include "AreaOrderer.h" +#include "Area.h" + +CAreaOrderer* CInnerCurves::area_orderer = NULL; + +CInnerCurves::CInnerCurves(CInnerCurves* pOuter, const CCurve* curve) +{ + m_pOuter = pOuter; + m_curve = curve; + m_unite_area = NULL; +} + +CInnerCurves::~CInnerCurves() +{ + delete m_unite_area; +} + +void CInnerCurves::Insert(const CCurve* pcurve) +{ + std::list outside_of_these; + std::list crossing_these; + + // check all inner curves + for(std::set::iterator It = m_inner_curves.begin(); It != m_inner_curves.end(); It++) + { + CInnerCurves* c = *It; + + switch(GetOverlapType(*pcurve, *(c->m_curve))) + { + case eOutside: + outside_of_these.push_back(c); + break; + + case eInside: + // insert in this inner curve + c->Insert(pcurve); + return; + + case eSiblings: + break; + + case eCrossing: + crossing_these.push_back(c); + break; + } + } + + // add as a new inner + CInnerCurves* new_item = new CInnerCurves(this, pcurve); + this->m_inner_curves.insert(new_item); + + for(std::list::iterator It = outside_of_these.begin(); It != outside_of_these.end(); It++) + { + // move items + CInnerCurves* c = *It; + c->m_pOuter = new_item; + new_item->m_inner_curves.insert(c); + this->m_inner_curves.erase(c); + } + + for(std::list::iterator It = crossing_these.begin(); It != crossing_these.end(); It++) + { + // unite these + CInnerCurves* c = *It; + new_item->Unite(c); + this->m_inner_curves.erase(c); + } +} + +void CInnerCurves::GetArea(CArea &area, bool outside, bool use_curve)const +{ + if(use_curve && m_curve) + { + area.m_curves.push_back(*m_curve); + outside = !outside; + } + + std::list do_after; + + for(std::set::const_iterator It = m_inner_curves.begin(); It != m_inner_curves.end(); It++) + { + const CInnerCurves* c = *It; + area.m_curves.push_back(*c->m_curve); + if(!outside)area.m_curves.back().Reverse(); + + if(outside)c->GetArea(area, !outside, false); + else do_after.push_back(c); + } + + for(std::list::iterator It = do_after.begin(); It != do_after.end(); It++) + { + const CInnerCurves* c = *It; + c->GetArea(area, !outside, false); + } +} + +void CInnerCurves::Unite(const CInnerCurves* c) +{ + // unite all the curves in c, with this one + CArea* new_area = new CArea(); + new_area->m_curves.push_back(*m_curve); + delete m_unite_area; + m_unite_area = new_area; + + CArea a2; + c->GetArea(a2); + + m_unite_area->Union(a2); + m_unite_area->Reorder(); + for(std::list::iterator It = m_unite_area->m_curves.begin(); It != m_unite_area->m_curves.end(); It++) + { + CCurve &curve = *It; + if(It == m_unite_area->m_curves.begin()) + m_curve = &curve; + else + { + if(curve.IsClockwise())curve.Reverse(); + Insert(&curve); + } + } +} + +CAreaOrderer::CAreaOrderer() +{ + m_top_level = new CInnerCurves(NULL, NULL); +} + +void CAreaOrderer::Insert(CCurve* pcurve) +{ + CInnerCurves::area_orderer = this; + + // make them all anti-clockwise as they come in + if(pcurve->IsClockwise())pcurve->Reverse(); + + m_top_level->Insert(pcurve); +} + +CArea CAreaOrderer::ResultArea()const +{ + CArea a; + + if(m_top_level) + { + m_top_level->GetArea(a); + } + + return a; +} + diff --git a/src/Mod/Path/libarea/AreaOrderer.h b/src/Mod/Path/libarea/AreaOrderer.h new file mode 100644 index 0000000000..13d78db7d2 --- /dev/null +++ b/src/Mod/Path/libarea/AreaOrderer.h @@ -0,0 +1,65 @@ +// AreaOrderer.h + +/*============================== +Copyright (c) 2011-2015 Dan Heeks + +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. +3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. +==============================*/ + + +#pragma once +#include +#include + +class CArea; +class CCurve; + +class CAreaOrderer; + +class CInnerCurves +{ + CInnerCurves* m_pOuter; + const CCurve* m_curve; // always empty if top level + std::set m_inner_curves; + CArea *m_unite_area; // new curves made by uniting are stored here + +public: + static CAreaOrderer* area_orderer; + CInnerCurves(CInnerCurves* pOuter, const CCurve* curve); + ~CInnerCurves(); + + void Insert(const CCurve* pcurve); + void GetArea(CArea &area, bool outside = true, bool use_curve = true)const; + void Unite(const CInnerCurves* c); +}; + +class CAreaOrderer +{ +public: + CInnerCurves* m_top_level; + + CAreaOrderer(); + + void Insert(CCurve* pcurve); + CArea ResultArea()const; +}; \ No newline at end of file diff --git a/src/Mod/Path/libarea/AreaPocket.cpp b/src/Mod/Path/libarea/AreaPocket.cpp new file mode 100644 index 0000000000..399d2eb882 --- /dev/null +++ b/src/Mod/Path/libarea/AreaPocket.cpp @@ -0,0 +1,589 @@ +// AreaPocket.cpp +// implements CArea::MakeOnePocketCurve + +/*============================== +Copyright (c) 2011-2015 Dan Heeks + +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. +3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. +==============================*/ + + +#include "Area.h" + +#include +#include + +static const CAreaPocketParams* pocket_params = NULL; + +class IslandAndOffset +{ +public: + const CCurve* island; + CArea offset; + std::list island_inners; + std::list touching_offsets; + + IslandAndOffset(const CCurve* Island) + { + island = Island; + + offset.m_curves.push_back(*island); + offset.m_curves.back().Reverse(); + + offset.Offset(-pocket_params->stepover); + + + if(offset.m_curves.size() > 1) + { + for(std::list::iterator It = offset.m_curves.begin(); It != offset.m_curves.end(); It++) + { + if(It == offset.m_curves.begin())continue; + island_inners.push_back(*It); + island_inners.back().Reverse(); + } + offset.m_curves.resize(1); + } + } +}; + +class CurveTree +{ + static std::list to_do_list_for_MakeOffsets; + void MakeOffsets2(); + static std::list islands_added; + +public: + Point point_on_parent; + CCurve curve; + std::list inners; + std::list offset_islands; + CurveTree(const CCurve &c) + { + curve = c; + } + ~CurveTree(){} + + void MakeOffsets(); +}; +std::list CurveTree::islands_added; + +class GetCurveItem +{ +public: + CurveTree* curve_tree; + std::list::iterator EndIt; + static std::list to_do_list; + + GetCurveItem(CurveTree* ct, std::list::iterator EIt):curve_tree(ct), EndIt(EIt){} + + void GetCurve(CCurve& output); + CVertex& back(){std::list::iterator It = EndIt; It--; return *It;} +}; + +std::list GetCurveItem::to_do_list; +std::list CurveTree::to_do_list_for_MakeOffsets; + +void GetCurveItem::GetCurve(CCurve& output) +{ + // walk around the curve adding spans to output until we get to an inner's point_on_parent + // then add a line from the inner's point_on_parent to inner's start point, then GetCurve from inner + + // add start point + if(CArea::m_please_abort)return; + output.m_vertices.insert(this->EndIt, CVertex(curve_tree->curve.m_vertices.front())); + + std::list inners_to_visit; + for(std::list::iterator It2 = curve_tree->inners.begin(); It2 != curve_tree->inners.end(); It2++) + { + inners_to_visit.push_back(*It2); + } + + const CVertex* prev_vertex = NULL; + + for(std::list::iterator It = curve_tree->curve.m_vertices.begin(); It != curve_tree->curve.m_vertices.end(); It++) + { + const CVertex& vertex = *It; + if(prev_vertex) + { + Span span(prev_vertex->m_p, vertex); + + // order inners on this span + std::multimap ordered_inners; + for(std::list::iterator It2 = inners_to_visit.begin(); It2 != inners_to_visit.end();) + { + CurveTree *inner = *It2; + double t; + if(span.On(inner->point_on_parent, &t)) + { + ordered_inners.insert(std::make_pair(t, inner)); + It2 = inners_to_visit.erase(It2); + } + else + { + It2++; + } + if(CArea::m_please_abort)return; + } + + if(CArea::m_please_abort)return; + for(std::multimap::iterator It2 = ordered_inners.begin(); It2 != ordered_inners.end(); It2++) + { + CurveTree& inner = *(It2->second); + if(inner.point_on_parent.dist(back().m_p) > 0.01/CArea::m_units) + { + output.m_vertices.insert(this->EndIt, CVertex(vertex.m_type, inner.point_on_parent, vertex.m_c)); + } + if(CArea::m_please_abort)return; + + // vertex add after GetCurve + std::list::iterator VIt = output.m_vertices.insert(this->EndIt, CVertex(inner.point_on_parent)); + + //inner.GetCurve(output); + GetCurveItem::to_do_list.push_back(GetCurveItem(&inner, VIt)); + } + + if(back().m_p != vertex.m_p)output.m_vertices.insert(this->EndIt, vertex); + } + prev_vertex = &vertex; + } + + if(CArea::m_please_abort)return; + for(std::list::iterator It2 = inners_to_visit.begin(); It2 != inners_to_visit.end(); It2++) + { + CurveTree &inner = *(*It2); + if(inner.point_on_parent != back().m_p) + { + output.m_vertices.insert(this->EndIt, CVertex(inner.point_on_parent)); + } + if(CArea::m_please_abort)return; + + // vertex add after GetCurve + std::list::iterator VIt = output.m_vertices.insert(this->EndIt, CVertex(inner.point_on_parent)); + + //inner.GetCurve(output); + GetCurveItem::to_do_list.push_back(GetCurveItem(&inner, VIt)); + + } +} + +class IslandAndOffsetLink +{ +public: + const IslandAndOffset* island_and_offset; + CurveTree* add_to; + IslandAndOffsetLink(const IslandAndOffset* i, CurveTree* a){island_and_offset = i; add_to = a;} +}; + +static Point GetNearestPoint(CurveTree* curve_tree, std::list &islands_added, const CCurve &test_curve, CurveTree** best_curve_tree) +{ + // find nearest point to test_curve, from curve and all the islands in + double best_dist; + Point best_point = curve_tree->curve.NearestPoint(test_curve, &best_dist); + *best_curve_tree = curve_tree; + for(std::list::iterator It = islands_added.begin(); It != islands_added.end(); It++) + { + CurveTree* island = *It; + double dist; + Point p = island->curve.NearestPoint(test_curve, &dist); + if(dist < best_dist) + { + *best_curve_tree = island; + best_point = p; + best_dist = dist; + } + } + + return best_point; +} + +void CurveTree::MakeOffsets2() +{ + // make offsets + + if(CArea::m_please_abort)return; + CArea smaller; + smaller.m_curves.push_back(curve); + smaller.Offset(pocket_params->stepover); + + if(CArea::m_please_abort)return; + + // test islands + for(std::list::iterator It = offset_islands.begin(); It != offset_islands.end();) + { + const IslandAndOffset* island_and_offset = *It; + + if(GetOverlapType(island_and_offset->offset, smaller) == eInside) + It++; // island is still inside + else + { + inners.push_back(new CurveTree(*island_and_offset->island)); + islands_added.push_back(inners.back()); + inners.back()->point_on_parent = curve.NearestPoint(*island_and_offset->island); + if(CArea::m_please_abort)return; + Point island_point = island_and_offset->island->NearestPoint(inners.back()->point_on_parent); + if(CArea::m_please_abort)return; + inners.back()->curve.ChangeStart(island_point); + if(CArea::m_please_abort)return; + + // add the island offset's inner curves + for(std::list::const_iterator It2 = island_and_offset->island_inners.begin(); It2 != island_and_offset->island_inners.end(); It2++) + { + const CCurve& island_inner = *It2; + inners.back()->inners.push_back(new CurveTree(island_inner)); + inners.back()->inners.back()->point_on_parent = inners.back()->curve.NearestPoint(island_inner); + if(CArea::m_please_abort)return; + Point island_point = island_inner.NearestPoint(inners.back()->inners.back()->point_on_parent); + if(CArea::m_please_abort)return; + inners.back()->inners.back()->curve.ChangeStart(island_point); + to_do_list_for_MakeOffsets.push_back(inners.back()->inners.back()); // do it later, in a while loop + if(CArea::m_please_abort)return; + } + + smaller.Subtract(island_and_offset->offset); + + std::set added; + + std::list touching_list; + for(std::list::const_iterator It2 = island_and_offset->touching_offsets.begin(); It2 != island_and_offset->touching_offsets.end(); It2++) + { + const IslandAndOffset* touching = *It2; + touching_list.push_back(IslandAndOffsetLink(touching, inners.back())); + added.insert(touching); + } + + while(touching_list.size() > 0) + { + IslandAndOffsetLink touching = touching_list.front(); + touching_list.pop_front(); + touching.add_to->inners.push_back(new CurveTree(*touching.island_and_offset->island)); + islands_added.push_back(touching.add_to->inners.back()); + touching.add_to->inners.back()->point_on_parent = touching.add_to->curve.NearestPoint(*touching.island_and_offset->island); + Point island_point = touching.island_and_offset->island->NearestPoint(touching.add_to->inners.back()->point_on_parent); + touching.add_to->inners.back()->curve.ChangeStart(island_point); + smaller.Subtract(touching.island_and_offset->offset); + + // add the island offset's inner curves + for(std::list::const_iterator It2 = touching.island_and_offset->island_inners.begin(); It2 != touching.island_and_offset->island_inners.end(); It2++) + { + const CCurve& island_inner = *It2; + touching.add_to->inners.back()->inners.push_back(new CurveTree(island_inner)); + touching.add_to->inners.back()->inners.back()->point_on_parent = touching.add_to->inners.back()->curve.NearestPoint(island_inner); + if(CArea::m_please_abort)return; + Point island_point = island_inner.NearestPoint(touching.add_to->inners.back()->inners.back()->point_on_parent); + if(CArea::m_please_abort)return; + touching.add_to->inners.back()->inners.back()->curve.ChangeStart(island_point); + to_do_list_for_MakeOffsets.push_back(touching.add_to->inners.back()->inners.back()); // do it later, in a while loop + if(CArea::m_please_abort)return; + } + + for(std::list::const_iterator It2 = touching.island_and_offset->touching_offsets.begin(); It2 != touching.island_and_offset->touching_offsets.end(); It2++) + { + if(added.find(*It2)==added.end() && ((*It2) != island_and_offset)) + { + touching_list.push_back(IslandAndOffsetLink(*It2, touching.add_to->inners.back())); + added.insert(*It2); + } + } + } + + if(CArea::m_please_abort)return; + It = offset_islands.erase(It); + + for(std::set::iterator It2 = added.begin(); It2 != added.end(); It2++) + { + const IslandAndOffset* i = *It2; + offset_islands.remove(i); + } + + if(offset_islands.size() == 0)break; + It = offset_islands.begin(); + } + } + + CArea::m_processing_done += CArea::m_MakeOffsets_increment; + if(CArea::m_processing_done > CArea::m_after_MakeOffsets_length)CArea::m_processing_done = CArea::m_after_MakeOffsets_length; + + std::list separate_areas; + smaller.Split(separate_areas); + if(CArea::m_please_abort)return; + for(std::list::iterator It = separate_areas.begin(); It != separate_areas.end(); It++) + { + CArea& separate_area = *It; + CCurve& first_curve = separate_area.m_curves.front(); + + CurveTree* nearest_curve_tree = NULL; + Point near_point = GetNearestPoint(this, islands_added, first_curve, &nearest_curve_tree); + + nearest_curve_tree->inners.push_back(new CurveTree(first_curve)); + + for(std::list::iterator It = offset_islands.begin(); It != offset_islands.end();It++) + { + const IslandAndOffset* island_and_offset = *It; + if(GetOverlapType(island_and_offset->offset, separate_area) == eInside) + nearest_curve_tree->inners.back()->offset_islands.push_back(island_and_offset); + if(CArea::m_please_abort)return; + } + + nearest_curve_tree->inners.back()->point_on_parent = near_point; + + if(CArea::m_please_abort)return; + Point first_curve_point = first_curve.NearestPoint(nearest_curve_tree->inners.back()->point_on_parent); + if(CArea::m_please_abort)return; + nearest_curve_tree->inners.back()->curve.ChangeStart(first_curve_point); + if(CArea::m_please_abort)return; + to_do_list_for_MakeOffsets.push_back(nearest_curve_tree->inners.back()); // do it later, in a while loop + if(CArea::m_please_abort)return; + } +} + +void CurveTree::MakeOffsets() +{ + to_do_list_for_MakeOffsets.push_back(this); + islands_added.clear(); + + while(to_do_list_for_MakeOffsets.size() > 0) + { + CurveTree* curve_tree = to_do_list_for_MakeOffsets.front(); + to_do_list_for_MakeOffsets.pop_front(); + curve_tree->MakeOffsets2(); + } +} + +void recur(std::list &arealist, const CArea& a1, const CAreaPocketParams ¶ms, int level) +{ + //if(level > 3)return; + + // this makes arealist by recursively offsetting a1 inwards + + if(a1.m_curves.size() == 0) + return; + + if(params.from_center) + arealist.push_front(a1); + else + arealist.push_back(a1); + + CArea a_offset = a1; + a_offset.Offset(params.stepover); + + // split curves into new areas + if(CArea::HolesLinked()) + { + for(std::list::iterator It = a_offset.m_curves.begin(); It != a_offset.m_curves.end(); It++) + { + CArea a2; + a2.m_curves.push_back(*It); + recur(arealist, a2, params, level + 1); + } + } + else + { + // split curves into new areas + a_offset.Reorder(); + CArea* a2 = NULL; + + for(std::list::iterator It = a_offset.m_curves.begin(); It != a_offset.m_curves.end(); It++) + { + CCurve& curve = *It; + if(curve.IsClockwise()) + { + if(a2 != NULL) + a2->m_curves.push_back(curve); + } + else + { + if(a2 != NULL) + recur(arealist, *a2, params, level + 1); + else + a2 = new CArea(); + a2->m_curves.push_back(curve); + } + } + + if(a2 != NULL) + recur(arealist, *a2, params, level + 1); + } +} + +static CArea make_obround(const Point& p0, const Point& p1, double radius) +{ + Point dir = p1 - p0; + double d = dir.length(); + dir.normalize(); + Point right(dir.y, -dir.x); + CArea obround; + CCurve c; + if(fabs(radius) < 0.0000001)radius = (radius > 0.0) ? 0.002 : (-0.002); + Point vt0 = p0 + right * radius; + Point vt1 = p1 + right * radius; + Point vt2 = p1 - right * radius; + Point vt3 = p0 - right * radius; + c.append(vt0); + c.append(vt1); + c.append(CVertex(1, vt2, p1)); + c.append(vt3); + c.append(CVertex(1, vt0, p0)); + obround.append(c); + return obround; +} + +static bool feed_possible(const CArea &area_for_feed_possible, const Point& p0, const Point& p1, double tool_radius) +{ + CArea obround = make_obround(p0, p1, tool_radius); + obround.Subtract(area_for_feed_possible); + return obround.m_curves.size() == 0; +} + +void MarkOverlappingOffsetIslands(std::list &offset_islands) +{ + for(std::list::iterator It1 = offset_islands.begin(); It1 != offset_islands.end(); It1++) + { + std::list::iterator It2 = It1; + It2++; + for(;It2 != offset_islands.end(); It2++) + { + IslandAndOffset &o1 = *It1; + IslandAndOffset &o2 = *It2; + + if(GetOverlapType(o1.offset, o2.offset) == eCrossing) + { + o1.touching_offsets.push_back(&o2); + o2.touching_offsets.push_back(&o1); + } + } + } +} + +void CArea::MakeOnePocketCurve(std::list &curve_list, const CAreaPocketParams ¶ms)const +{ + if(CArea::m_please_abort)return; +#if 0 // simple offsets with feed or rapid joins + CArea area_for_feed_possible = *this; + + area_for_feed_possible.Offset(-params.tool_radius - 0.01); + CArea a_offset = *this; + + std::list arealist; + recur(arealist, a_offset, params, 0); + + bool first = true; + + for(std::list::iterator It = arealist.begin(); It != arealist.end(); It++) + { + CArea& area = *It; + for(std::list::iterator It = area.m_curves.begin(); It != area.m_curves.end(); It++) + { + CCurve& curve = *It; + if(!first) + { + // try to join these curves with a feed move, if possible and not too long + CCurve &prev_curve = curve_list.back(); + const Point &prev_p = prev_curve.m_vertices.back().m_p; + const Point &next_p = curve.m_vertices.front().m_p; + + if(feed_possible(area_for_feed_possible, prev_p, next_p, params.tool_radius)) + { + // join curves + prev_curve += curve; + } + else + { + curve_list.push_back(curve); + } + } + else + { + curve_list.push_back(curve); + } + first = false; + } + } +#else + pocket_params = ¶ms; + if(m_curves.size() == 0) + { + CArea::m_processing_done += CArea::m_single_area_processing_length; + return; + } + CurveTree top_level(m_curves.front()); + + std::list offset_islands; + + for(std::list::const_iterator It = m_curves.begin(); It != m_curves.end(); It++) + { + const CCurve& c = *It; + if(It != m_curves.begin()) + { + IslandAndOffset island_and_offset(&c); + offset_islands.push_back(island_and_offset); + top_level.offset_islands.push_back(&(offset_islands.back())); + if(m_please_abort)return; + } + } + + MarkOverlappingOffsetIslands(offset_islands); + + CArea::m_processing_done += CArea::m_single_area_processing_length * 0.1; + + double MakeOffsets_processing_length = CArea::m_single_area_processing_length * 0.8; + CArea::m_after_MakeOffsets_length = CArea::m_processing_done + MakeOffsets_processing_length; + double guess_num_offsets = sqrt(GetArea(true)) * 0.5 / params.stepover; + CArea::m_MakeOffsets_increment = MakeOffsets_processing_length / guess_num_offsets; + + top_level.MakeOffsets(); + if(CArea::m_please_abort)return; + CArea::m_processing_done = CArea::m_after_MakeOffsets_length; + + curve_list.push_back(CCurve()); + CCurve& output = curve_list.back(); + + GetCurveItem::to_do_list.push_back(GetCurveItem(&top_level, output.m_vertices.end())); + + while(GetCurveItem::to_do_list.size() > 0) + { + GetCurveItem item = GetCurveItem::to_do_list.front(); + item.GetCurve(output); + GetCurveItem::to_do_list.pop_front(); + } + + // delete curve_trees non-recursively + std::list CurveTreeDestructList; + for(std::list::iterator It = top_level.inners.begin(); It != top_level.inners.end(); It++) + { + CurveTreeDestructList.push_back(*It); + } + while(CurveTreeDestructList.size() > 0) + { + CurveTree* curve_tree = CurveTreeDestructList.front(); + CurveTreeDestructList.pop_front(); + for(std::list::iterator It = curve_tree->inners.begin(); It != curve_tree->inners.end(); It++) + { + CurveTreeDestructList.push_back(*It); + } + delete curve_tree; + } + + CArea::m_processing_done += CArea::m_single_area_processing_length * 0.1; +#endif +} + diff --git a/src/Mod/Path/libarea/Box2D.h b/src/Mod/Path/libarea/Box2D.h new file mode 100644 index 0000000000..d9c7722a3f --- /dev/null +++ b/src/Mod/Path/libarea/Box2D.h @@ -0,0 +1,94 @@ +// Box2D.h + +/*============================== +Copyright (c) 2011-2015 Dan Heeks + +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. +3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. +==============================*/ + +#pragma once + +#include // for memcpy() prototype +#include // for sqrt() prototype + +class CBox2D{ +public: + Point m_minxy; + Point m_maxxy; + bool m_valid; + + CBox2D():m_valid(false){} + CBox2D(const Point& minxy, const Point& maxxy):m_minxy(minxy), m_maxxy(maxxy), m_valid(true){} + + bool operator==( const CBox2D & rhs ) const + { + if(m_minxy != rhs.m_minxy)return false; + if(m_maxxy != rhs.m_maxxy)return false; + if (m_valid != rhs.m_valid) return(false); + + return(true); + } + + bool operator!=( const CBox2D & rhs ) const { return(! (*this == rhs)); } + + + void Insert(const Point &p){ // insert a point + if(m_valid){ + if(p.x < m_minxy.x)m_minxy.x = p.x; + if(p.y < m_minxy.y)m_minxy.y = p.y; + if(p.x > m_maxxy.x)m_maxxy.x = p.x; + if(p.y > m_maxxy.y)m_maxxy.y = p.y; + } + else + { + m_valid = true; + m_minxy = p; + m_maxxy = p; + } + } + + void Insert(const CBox2D& b){ + if(b.m_valid){ + if(m_valid){ + if(b.m_minxy.x < m_minxy.x)m_minxy.x = b.m_minxy.x; + if(b.m_minxy.y < m_minxy.y)m_minxy.y = b.m_minxy.y; + if(b.m_maxxy.x > m_maxxy.x)m_maxxy.x = b.m_maxxy.x; + if(b.m_maxxy.y > m_maxxy.y)m_maxxy.y = b.m_maxxy.y; + } + else{ + m_valid = b.m_valid; + m_minxy = b.m_minxy; + m_maxxy = b.m_maxxy; + } + } + } + Point Centre() const {return (m_minxy + m_maxxy) * 0.5;} + double Width() const {if(m_valid)return m_maxxy.x - m_minxy.x; else return 0.0;} + double Height() const {if(m_valid)return m_maxxy.y - m_minxy.y; else return 0.0;} + double Radius() const {return sqrt(Width() * Width() + Height() * Height()) /2;} + double MinX() const { return(m_minxy.x); } + double MaxX() const { return(m_maxxy.x); } + double MinY() const { return(m_minxy.y); } + double MaxY() const { return(m_maxxy.y); } +}; + diff --git a/src/Mod/Path/libarea/CMakeLists.txt b/src/Mod/Path/libarea/CMakeLists.txt new file mode 100644 index 0000000000..aad7427093 --- /dev/null +++ b/src/Mod/Path/libarea/CMakeLists.txt @@ -0,0 +1,81 @@ +# Turn compiler warnings on for gcc +if (CMAKE_BUILD_TOOL MATCHES "make") + MESSAGE(STATUS "setting gcc options: -Wall -Werror -Wno-deprecated -pedantic-errors") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") +endif (CMAKE_BUILD_TOOL MATCHES "make") + +include_directories(${PYTHON_INCLUDE_DIRS}) +include_directories(${CMAKE_CURRENT_SOURCE_DIR}) + +find_package( Boost COMPONENTS python REQUIRED) # find BOOST and boost-python +if(Boost_FOUND) + include_directories(${Boost_INCLUDE_DIRS}) + MESSAGE(STATUS "found Boost: " ${Boost_LIB_VERSION}) + MESSAGE(STATUS "boost-incude dirs are: " ${Boost_INCLUDE_DIRS}) + MESSAGE(STATUS "boost-python lib is: " ${Boost_PYTHON_LIBRARY}) + MESSAGE(STATUS "boost_LIBRARY_DIRS is: " ${Boost_LIBRARY_DIRS}) + MESSAGE(STATUS "Boost_LIBRARIES is: " ${Boost_LIBRARIES}) +endif() + +# this defines the source-files for library +set(AREA_SRC_COMMON + Arc.cpp + Area.cpp + AreaOrderer.cpp + AreaPocket.cpp + Circle.cpp + Curve.cpp + kurve/Construction.cpp + kurve/Finite.cpp + kurve/kurve.cpp + kurve/Matrix.cpp + kurve/offset.cpp +) + +set(AREA_SRC_CLIPPER + AreaClipper.cpp + clipper.cpp +) + +# this defines the additional source-files for python module (wrapper to libarea) +set(PYAREA_SRC + PythonStuff.cpp +) + +# this defines the headers +if(DEFINED INCLUDE_INSTALL_DIR) + set(includedir ${INCLUDE_INSTALL_DIR}) +else(DEFINED INCLUDE_INSTALL_DIR) + set(INCLUDE_INSTALL_DIR include) + set(includedir ${CMAKE_INSTALL_PREFIX}/${INCLUDE_INSTALL_DIR}) +endif(DEFINED INCLUDE_INSTALL_DIR) + +file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/kurve/*.h") +file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h") + +# this makes the Python module +add_library( + area + MODULE + ${AREA_SRC_COMMON} + ${AREA_SRC_CLIPPER} + ${PYAREA_SRC} +) + +target_link_libraries(area ${Boost_LIBRARIES}) +set_target_properties(area PROPERTIES PREFIX "") + +# this figures out where to install the Python modules +execute_process( + COMMAND python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())" + OUTPUT_VARIABLE Python_site_packages + OUTPUT_STRIP_TRAILING_WHITESPACE +) + +message(STATUS "area module (for Path Workbench) will be installed to: " ${CMAKE_INSTALL_LIBDIR}) + +# this installs the python library +install( + TARGETS area + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} +) diff --git a/src/Mod/Path/libarea/Circle.cpp b/src/Mod/Path/libarea/Circle.cpp new file mode 100644 index 0000000000..415d5b0283 --- /dev/null +++ b/src/Mod/Path/libarea/Circle.cpp @@ -0,0 +1,103 @@ +// Circle.cpp + +/*============================== +Copyright (c) 2011-2015 Dan Heeks + +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. +3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. +==============================*/ + + +#include "Circle.h" + +Circle::Circle(const Point& p0, const Point& p1, const Point& p2) +{ + // from TangentCircles in http://code.google.com/p/heekscad/source/browse/trunk/src/Geom.cpp + + // set default values, in case this fails + m_radius = 0.0; + m_c = Point(0, 0); + + double x1 = p0.x; + double y1 = p0.y; + double x2 = p1.x; + double y2 = p1.y; + double x3 = p2.x; + double y3 = p2.y; + + double a = 2 * (x1 - x2); + double b = 2 * (y1 - y2); + double d = (x1 * x1 + y1 * y1) - (x2 * x2 + y2 * y2); + + double A = 2 * (x1 - x3); + double B = 2 * (y1 - y3); + double D = (x1 * x1 + y1 * y1) - (x3 * x3 + y3 * y3); + + double aBmbA = (a*B - b*A); // aB - bA + + // x = k + Kr where + double k = (B*d - b*D) / aBmbA; + + // y = l + Lr where + double l = (-A*d + a*D)/ aBmbA; + + double qa = -1; + double qb = 0.0; + double qc = k*k + x1*x1 -2*k*x1 + l*l + y1*y1 - 2*l*y1; + + // solve the quadratic equation, r = (-b +- sqrt(b*b - 4*a*c))/(2 * a) + for(int qs = 0; qs<2; qs++){ + double bb = qb*qb; + double ac4 = 4*qa*qc; + if(ac4 <= bb){ + double r = (-qb + ((qs == 0) ? 1 : -1) * sqrt(bb - ac4))/(2 * qa); + double x = k; + double y = l; + + // set the circle + if(r >= 0.0){ + m_c = Point(x, y); + m_radius = r; + } + } + } +} + +bool Circle::PointIsOn(const Point& p, double accuracy) +{ + double rp = p.dist(m_c); + bool on = fabs(m_radius - rp) < accuracy; + return on; +} + +bool Circle::LineIsOn(const Point& p0, const Point& p1, double accuracy) +{ + // checks the points are on the arc, to the given accuracy, and the mid point of the line. + + if(!PointIsOn(p0, accuracy))return false; + if(!PointIsOn(p1, accuracy))return false; + + Point mid = Point((p0 + p1)/2); + if(!PointIsOn(mid, accuracy))return false; + + return true; +} \ No newline at end of file diff --git a/src/Mod/Path/libarea/Circle.h b/src/Mod/Path/libarea/Circle.h new file mode 100644 index 0000000000..459a5ff318 --- /dev/null +++ b/src/Mod/Path/libarea/Circle.h @@ -0,0 +1,44 @@ +// Circle.h + +/*============================== +Copyright (c) 2011-2015 Dan Heeks + +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. +3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. +==============================*/ + + +#pragma once + +#include "Point.h" + +class Circle{ +public: + Point m_c; + double m_radius; + + Circle(const Point& c, double radius):m_c(c), m_radius(radius){} + Circle(const Point& p0, const Point& p1, const Point& p2); // circle through three points + + bool PointIsOn(const Point& p, double accuracy); + bool LineIsOn(const Point& p0, const Point& p1, double accuracy); +}; \ No newline at end of file diff --git a/src/Mod/Path/libarea/Curve.cpp b/src/Mod/Path/libarea/Curve.cpp new file mode 100644 index 0000000000..3c2740a34b --- /dev/null +++ b/src/Mod/Path/libarea/Curve.cpp @@ -0,0 +1,1294 @@ +// Curve.cpp + +/*============================== +Copyright (c) 2011-2015 Dan Heeks + +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. +3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. +==============================*/ + +#include "Curve.h" +#include "Circle.h" +#include "Arc.h" +#include "Area.h" +#include "kurve/geometry.h" + +const Point operator*(const double &d, const Point &p){ return p * d;} +double Point::tolerance = 0.001; + +//static const double PI = 3.1415926535897932; duplicated in kurve/geometry.h + +double Point::length()const +{ + return sqrt( x*x + y*y ); +} + +double Point::normalize() +{ + double len = length(); + if(fabs(len)> 0.000000000000001) + *this = (*this) / len; + return len; +} + +Line::Line(const Point& P0, const Point& V):p0(P0), v(V) +{ +} + +double Line::Dist(const Point& p)const +{ + Point vn = v; + vn.normalize(); + double d1 = p0 * vn; + double d2 = p * vn; + Point pn = p0 + vn * (d2 - d1); + + return pn.dist(p); +} + +CVertex::CVertex(int type, const Point& p, const Point& c, int user_data):m_type(type), m_p(p), m_c(c), m_user_data(user_data) +{ +} + +CVertex::CVertex(const Point& p, int user_data):m_type(0), m_p(p), m_c(0.0, 0.0), m_user_data(user_data) +{ +} + +void CCurve::append(const CVertex& vertex) +{ + m_vertices.push_back(vertex); +} + +bool CCurve::CheckForArc(const CVertex& prev_vt, std::list& might_be_an_arc, CArc &arc_returned) +{ + // this examines the vertices in might_be_an_arc + // if they do fit an arc, set arc to be the arc that they fit and return true + // returns true, if arc added + if(might_be_an_arc.size() < 2)return false; + + // find middle point + int num = might_be_an_arc.size(); + int i = 0; + const CVertex* mid_vt = NULL; + int mid_i = (num-1)/2; + for(std::list::iterator It = might_be_an_arc.begin(); It != might_be_an_arc.end(); It++, i++) + { + if(i == mid_i) + { + mid_vt = *It; + break; + } + } + + // create a circle to test + Point p0(prev_vt.m_p); + Point p1(mid_vt->m_p); + Point p2(might_be_an_arc.back()->m_p); + Circle c(p0, p1, p2); + + const CVertex* current_vt = &prev_vt; + double accuracy = CArea::m_accuracy * 1.4 / CArea::m_units; + for(std::list::iterator It = might_be_an_arc.begin(); It != might_be_an_arc.end(); It++) + { + const CVertex* vt = *It; + + if(!c.LineIsOn(current_vt->m_p, vt->m_p, accuracy)) + return false; + current_vt = vt; + } + + CArc arc; + arc.m_c = c.m_c; + arc.m_s = prev_vt.m_p; + arc.m_e = might_be_an_arc.back()->m_p; + arc.SetDirWithPoint(might_be_an_arc.front()->m_p); + arc.m_user_data = might_be_an_arc.back()->m_user_data; + + double angs = atan2(arc.m_s.y - arc.m_c.y, arc.m_s.x - arc.m_c.x); + double ange = atan2(arc.m_e.y - arc.m_c.y, arc.m_e.x - arc.m_c.x); + if(arc.m_dir) + { + // make sure ange > angs + if(ange < angs)ange += 6.2831853071795864; + } + else + { + // make sure angs > ange + if(angs < ange)angs += 6.2831853071795864; + } + + if(arc.IncludedAngle() >= 3.15)return false; // We don't want full arcs, so limit to about 180 degrees + + for(std::list::iterator It = might_be_an_arc.begin(); It != might_be_an_arc.end(); It++) + { + const CVertex* vt = *It; + double angp = atan2(vt->m_p.y - arc.m_c.y, vt->m_p.x - arc.m_c.x); + if(arc.m_dir) + { + // make sure angp > angs + if(angp < angs)angp += 6.2831853071795864; + if(angp > ange)return false; + } + else + { + // make sure angp > ange + if(angp < ange)angp += 6.2831853071795864; + if(angp > angs)return false; + } + } + + arc_returned = arc; + return true; +} + +void CCurve::AddArcOrLines(bool check_for_arc, std::list &new_vertices, std::list& might_be_an_arc, CArc &arc, bool &arc_found, bool &arc_added) +{ + if(check_for_arc && CheckForArc(new_vertices.back(), might_be_an_arc, arc)) + { + arc_found = true; + } + else + { + if(arc_found) + { + if(arc.AlmostALine()) + { + new_vertices.push_back(CVertex(arc.m_e, arc.m_user_data)); + } + else + { + new_vertices.push_back(CVertex(arc.m_dir ? 1:-1, arc.m_e, arc.m_c, arc.m_user_data)); + } + + arc_added = true; + arc_found = false; + const CVertex* back_vt = might_be_an_arc.back(); + might_be_an_arc.clear(); + if(check_for_arc)might_be_an_arc.push_back(back_vt); + } + else + { + const CVertex* back_vt = might_be_an_arc.back(); + if(check_for_arc)might_be_an_arc.pop_back(); + for(std::list::iterator It = might_be_an_arc.begin(); It != might_be_an_arc.end(); It++) + { + const CVertex* v = *It; + if(It != might_be_an_arc.begin() || (new_vertices.size() == 0) || (new_vertices.back().m_p != v->m_p)) + { + new_vertices.push_back(*v); + } + } + might_be_an_arc.clear(); + if(check_for_arc)might_be_an_arc.push_back(back_vt); + } + } +} + +void CCurve::FitArcs() +{ + std::list new_vertices; + + std::list might_be_an_arc; + CArc arc; + bool arc_found = false; + bool arc_added = false; + + int i = 0; + for(std::list::iterator It = m_vertices.begin(); It != m_vertices.end(); It++, i++) + { + CVertex& vt = *It; + if(vt.m_type || i == 0) + new_vertices.push_back(vt); + else + { + might_be_an_arc.push_back(&vt); + + if(might_be_an_arc.size() == 1) + { + } + else + { + AddArcOrLines(true, new_vertices, might_be_an_arc, arc, arc_found, arc_added); + } + } + } + + if(might_be_an_arc.size() > 0)AddArcOrLines(false, new_vertices, might_be_an_arc, arc, arc_found, arc_added); + + if(arc_added) + { + m_vertices.clear(); + for(std::list::iterator It = new_vertices.begin(); It != new_vertices.end(); It++)m_vertices.push_back(*It); + for(std::list::iterator It = might_be_an_arc.begin(); It != might_be_an_arc.end(); It++)m_vertices.push_back(*(*It)); + } +} + +void CCurve::UnFitArcs() +{ + std::list new_pts; + + const CVertex* prev_vertex = NULL; + for(std::list::const_iterator It2 = m_vertices.begin(); It2 != m_vertices.end(); It2++) + { + const CVertex& vertex = *It2; + if(vertex.m_type == 0 || prev_vertex == NULL) + { + new_pts.push_back(vertex.m_p * CArea::m_units); + } + else + { + if(vertex.m_p != prev_vertex->m_p) + { + double phi,dphi,dx,dy; + int Segments; + int i; + double ang1,ang2,phit; + + dx = (prev_vertex->m_p.x - vertex.m_c.x) * CArea::m_units; + dy = (prev_vertex->m_p.y - vertex.m_c.y) * CArea::m_units; + + ang1=atan2(dy,dx); + if (ang1<0) ang1+=2.0*PI; + dx = (vertex.m_p.x - vertex.m_c.x) * CArea::m_units; + dy = (vertex.m_p.y - vertex.m_c.y) * CArea::m_units; + ang2=atan2(dy,dx); + if (ang2<0) ang2+=2.0*PI; + + if (vertex.m_type == -1) + { //clockwise + if (ang2 > ang1) + phit=2.0*PI-ang2+ ang1; + else + phit=ang1-ang2; + } + else + { //counter_clockwise + if (ang1 > ang2) + phit=-(2.0*PI-ang1+ ang2); + else + phit=-(ang2-ang1); + } + + //what is the delta phi to get an accurancy of aber + double radius = sqrt(dx*dx + dy*dy); + dphi=2*acos((radius-CArea::m_accuracy)/radius); + + //set the number of segments + if (phit > 0) + Segments=(int)ceil(phit/dphi); + else + Segments=(int)ceil(-phit/dphi); + + if (Segments < 1) + Segments=1; + if (Segments > 100) + Segments=100; + + dphi=phit/(Segments); + + double px = prev_vertex->m_p.x * CArea::m_units; + double py = prev_vertex->m_p.y * CArea::m_units; + + for (i=1; i<=Segments; i++) + { + dx = px - vertex.m_c.x * CArea::m_units; + dy = py - vertex.m_c.y * CArea::m_units; + phi=atan2(dy,dx); + + double nx = vertex.m_c.x * CArea::m_units + radius * cos(phi-dphi); + double ny = vertex.m_c.y * CArea::m_units + radius * sin(phi-dphi); + + new_pts.push_back(Point(nx, ny)); + + px = nx; + py = ny; + } + } + } + prev_vertex = &vertex; + } + + m_vertices.clear(); + + for(std::list::iterator It = new_pts.begin(); It != new_pts.end(); It++) + { + Point &pt = *It; + CVertex vertex(0, pt / CArea::m_units, Point(0.0, 0.0)); + m_vertices.push_back(vertex); + } +} + +Point CCurve::NearestPoint(const Point& p)const +{ + double best_dist = 0.0; + Point best_point = Point(0, 0); + bool best_point_valid = false; + Point prev_p = Point(0, 0); + bool prev_p_valid = false; + bool first_span = true; + for(std::list::const_iterator It = m_vertices.begin(); It != m_vertices.end(); It++) + { + const CVertex& vertex = *It; + if(prev_p_valid) + { + Point near_point = Span(prev_p, vertex, first_span).NearestPoint(p); + first_span = false; + double dist = near_point.dist(p); + if(!best_point_valid || dist < best_dist) + { + best_dist = dist; + best_point = near_point; + best_point_valid = true; + } + } + prev_p = vertex.m_p; + prev_p_valid = true; + } + return best_point; +} + +Point CCurve::NearestPoint(const CCurve& c, double *d)const +{ + double best_dist = 0.0; + Point best_point = Point(0, 0); + bool best_point_valid = false; + Point prev_p = Point(0, 0); + bool prev_p_valid = false; + bool first_span = true; + for(std::list::const_iterator It = c.m_vertices.begin(); It != c.m_vertices.end(); It++) + { + const CVertex& vertex = *It; + if(prev_p_valid) + { + double dist; + Point near_point = NearestPoint(Span(prev_p, vertex, first_span), &dist); + first_span = false; + if(!best_point_valid || dist < best_dist) + { + best_dist = dist; + best_point = near_point; + best_point_valid = true; + } + } + prev_p = vertex.m_p; + prev_p_valid = true; + } + if(d)*d = best_dist; + return best_point; +} + +void CCurve::GetBox(CBox2D &box) +{ + Point prev_p = Point(0, 0); + bool prev_p_valid = false; + for(std::list::iterator It = m_vertices.begin(); It != m_vertices.end(); It++) + { + CVertex& vertex = *It; + if(prev_p_valid) + { + Span(prev_p, vertex).GetBox(box); + } + prev_p = vertex.m_p; + prev_p_valid = true; + } +} + +void CCurve::Reverse() +{ + std::list new_vertices; + + CVertex* prev_v = NULL; + + for(std::list::reverse_iterator It = m_vertices.rbegin(); It != m_vertices.rend(); It++) + { + CVertex &v = *It; + int type = 0; + Point cp(0.0, 0.0); + if(prev_v) + { + type = -prev_v->m_type; + cp = prev_v->m_c; + } + CVertex new_v(type, v.m_p, cp); + new_vertices.push_back(new_v); + prev_v = &v; + } + + m_vertices = new_vertices; +} + +double CCurve::GetArea()const +{ + double area = 0.0; + Point prev_p = Point(0, 0); + bool prev_p_valid = false; + for(std::list::const_iterator It = m_vertices.begin(); It != m_vertices.end(); It++) + { + const CVertex& vertex = *It; + if(prev_p_valid) + { + area += Span(prev_p, vertex).GetArea(); + } + prev_p = vertex.m_p; + prev_p_valid = true; + } + return area; +} + +bool CCurve::IsClosed()const +{ + if(m_vertices.size() == 0)return false; + return m_vertices.front().m_p == m_vertices.back().m_p; +} + +void CCurve::ChangeStart(const Point &p) { + CCurve new_curve; + + bool started = false; + bool finished = false; + int start_span; + bool closed = IsClosed(); + + for(int i = 0; i < (closed ? 2:1); i++) + { + const Point *prev_p = NULL; + + int span_index = 0; + for(std::list::const_iterator VIt = m_vertices.begin(); VIt != m_vertices.end() && !finished; VIt++) + { + const CVertex& vertex = *VIt; + + if(prev_p) + { + Span span(*prev_p, vertex); + if(span.On(p)) + { + if(started) + { + if(p == *prev_p || span_index != start_span) + { + new_curve.m_vertices.push_back(vertex); + } + else + { + if(p == vertex.m_p)new_curve.m_vertices.push_back(vertex); + else + { + CVertex v(vertex); + v.m_p = p; + new_curve.m_vertices.push_back(v); + } + finished = true; + } + } + else + { + new_curve.m_vertices.push_back(CVertex(p)); + started = true; + start_span = span_index; + if(p != vertex.m_p)new_curve.m_vertices.push_back(vertex); + } + } + else + { + if(started) + { + new_curve.m_vertices.push_back(vertex); + } + } + span_index++; + } + prev_p = &(vertex.m_p); + } + } + + if(started) + { + *this = new_curve; + } +} + +void CCurve::Break(const Point &p) { + // inserts a point, if it lies on the curve + const Point *prev_p = NULL; + + for(std::list::iterator VIt = m_vertices.begin(); VIt != m_vertices.end(); VIt++) + { + CVertex& vertex = *VIt; + + if(p == vertex.m_p)break; // point is already on a vertex + + if(prev_p) + { + Span span(*prev_p, vertex); + if(span.On(p)) + { + CVertex v(vertex); + v.m_p = p; + m_vertices.insert(VIt, v); + break; + } + } + prev_p = &(vertex.m_p); + } +} + +void CCurve::ExtractSeparateCurves(const std::list &ordered_points, std::list &separate_curves)const +{ + // returns separate curves for this curve split at points + // the points must be in order along this curve, already, and lie on this curve + const Point *prev_p = NULL; + + if(ordered_points.size() == 0) + { + separate_curves.push_back(*this); + return; + } + + CCurve current_curve; + + std::list::const_iterator PIt = ordered_points.begin(); + Point point = *PIt; + + for(std::list::const_iterator VIt = m_vertices.begin(); VIt != m_vertices.end(); VIt++) + { + const CVertex& vertex = *VIt; + if(prev_p)// not the first vertex + { + Span span(*prev_p, vertex); + while((PIt != ordered_points.end()) && span.On(point)) + { + CVertex v(vertex); + v.m_p = point; + current_curve.m_vertices.push_back(v); + if(current_curve.m_vertices.size() > 1)// don't add single point curves + separate_curves.push_back(current_curve); // add the curve + current_curve = CCurve();// make a new curve + current_curve.m_vertices.push_back(v); // add it's first point + PIt++; + if(PIt != ordered_points.end())point = *PIt; // increment the point + } + + // add the end of span + if(current_curve.m_vertices.back().m_p != vertex.m_p) + current_curve.m_vertices.push_back(vertex); + } + if((current_curve.m_vertices.size() == 0) || (current_curve.m_vertices.back().m_p != vertex.m_p)) + { + // very first vertex, start the current curve + current_curve.m_vertices.push_back(vertex); + } + prev_p = &(vertex.m_p); + } + + // add whatever is left + if(current_curve.m_vertices.size() > 1)// don't add single point curves + separate_curves.push_back(current_curve); // add the curve +} + + + +void CCurve::RemoveTinySpans() { + CCurve new_curve; + + std::list::const_iterator VIt = m_vertices.begin(); + new_curve.m_vertices.push_back(*VIt); + VIt++; + + for(; VIt != m_vertices.end(); VIt++) + { + const CVertex& vertex = *VIt; + + if(vertex.m_type != 0 || new_curve.m_vertices.back().m_p.dist(vertex.m_p) > Point::tolerance) + { + new_curve.m_vertices.push_back(vertex); + } + } + *this = new_curve; +} + +void CCurve::ChangeEnd(const Point &p) { + // changes the end position of the Kurve, doesn't keep closed kurves closed + CCurve new_curve; + + const Point *prev_p = NULL; + + for(std::list::const_iterator VIt = m_vertices.begin(); VIt != m_vertices.end(); VIt++) + { + const CVertex& vertex = *VIt; + + if(prev_p) + { + Span span(*prev_p, vertex); + if(span.On(p)) + { + CVertex v(vertex); + v.m_p = p; + new_curve.m_vertices.push_back(v); + break; + } + else + { + if(p != vertex.m_p)new_curve.m_vertices.push_back(vertex); + } + } + else + { + new_curve.m_vertices.push_back(vertex); + } + prev_p = &(vertex.m_p); + } + + *this = new_curve; +} + +Point CCurve::NearestPoint(const Span& p, double *d)const +{ + double best_dist = 0.0; + Point best_point = Point(0, 0); + bool best_point_valid = false; + Point prev_p = Point(0, 0); + bool prev_p_valid = false; + bool first_span = true; + for(std::list::const_iterator It = m_vertices.begin(); It != m_vertices.end(); It++) + { + const CVertex& vertex = *It; + if(prev_p_valid) + { + double dist; + Point near_point = Span(prev_p, vertex, first_span).NearestPoint(p, &dist); + first_span = false; + if(!best_point_valid || dist < best_dist) + { + best_dist = dist; + best_point = near_point; + best_point_valid = true; + } + } + prev_p = vertex.m_p; + prev_p_valid = true; + } + if(d)*d = best_dist; + return best_point; +} + +static geoff_geometry::Kurve MakeKurve(const CCurve& curve) +{ + geoff_geometry::Kurve k; + for(std::list::const_iterator It = curve.m_vertices.begin(); It != curve.m_vertices.end(); It++) + { + const CVertex& v = *It; + k.Add(geoff_geometry::spVertex(v.m_type, geoff_geometry::Point(v.m_p.x, v.m_p.y), geoff_geometry::Point(v.m_c.x, v.m_c.y))); + } + return k; +} + +static CCurve MakeCCurve(const geoff_geometry::Kurve& k) +{ + CCurve c; + int n = k.nSpans(); + for(int i = 0; i<= n; i++) + { + geoff_geometry::spVertex spv; + k.Get(i, spv); + c.append(CVertex(spv.type, Point(spv.p.x, spv.p.y), Point(spv.pc.x, spv.pc.y))); + } + return c; +} + +static geoff_geometry::Span MakeSpan(const Span& span) +{ + return geoff_geometry::Span(span.m_v.m_type, geoff_geometry::Point(span.m_p.x, span.m_p.y), geoff_geometry::Point(span.m_v.m_p.x, span.m_v.m_p.y), geoff_geometry::Point(span.m_v.m_c.x, span.m_v.m_c.y)); +} + +static Span MakeCSpan(const geoff_geometry::Span &sp) +{ + return Span(Point(sp.p0.x, sp.p0.y), CVertex(sp.dir, Point(sp.p1.x, sp.p1.y), Point(sp.pc.x, sp.pc.y))); +} + +bool CCurve::Offset(double leftwards_value) +{ + // use the kurve code donated by Geoff Hawkesford, to offset the curve as an open curve + // returns true for success, false for failure + bool success = true; + + CCurve save_curve = *this; + + try + { + geoff_geometry::Kurve k = MakeKurve(*this); + geoff_geometry::Kurve kOffset; + int ret = 0; + k.OffsetMethod1(kOffset, fabs(leftwards_value), (leftwards_value > 0) ? 1:-1, 1, ret); + success = (ret == 0); + if(success)*this = MakeCCurve(kOffset); + } + catch(...) + { + success = false; + } + + if(success == false) + { + if(this->IsClosed()) + { + double inwards_offset = leftwards_value; + bool cw = false; + if(this->IsClockwise()) + { + inwards_offset = -inwards_offset; + cw = true; + } + CArea a; + a.append(*this); + a.Offset(inwards_offset); + if(a.m_curves.size() == 1) + { + Span* start_span = NULL; + if(this->m_vertices.size() > 1) + { + std::list::iterator It = m_vertices.begin(); + CVertex &v0 = *It; + It++; + CVertex &v1 = *It; + start_span = new Span(v0.m_p, v1, true); + } + *this = a.m_curves.front(); + if(this->IsClockwise() != cw)this->Reverse(); + if(start_span) + { + Point forward = start_span->GetVector(0.0); + Point left(-forward.y, forward.x); + Point offset_start = start_span->m_p + left * leftwards_value; + this->ChangeStart(this->NearestPoint(offset_start)); + delete start_span; + } + success = true; + } + } + } + + return success; +} + +void CCurve::GetSpans(std::list &spans)const +{ + const Point *prev_p = NULL; + for(std::list::const_iterator It = m_vertices.begin(); It != m_vertices.end(); It++) + { + const CVertex& vertex = *It; + if(prev_p) + { + spans.push_back(Span(*prev_p, vertex)); + } + prev_p = &(vertex.m_p); + } +} + +void CCurve::OffsetForward(double forwards_value, bool refit_arcs) +{ + // for drag-knife compensation + + // replace arcs with lines + UnFitArcs(); + + std::list spans; + GetSpans(spans); + + m_vertices.clear(); + + // shift all the spans + for(std::list::iterator It = spans.begin(); It != spans.end(); It++) + { + Span &span = *It; + Point v = span.GetVector(0.0); + v.normalize(); + Point shift = v * forwards_value; + span.m_p = span.m_p + shift; + span.m_v.m_p = span.m_v.m_p + shift; + } + + // loop through the shifted spans + for(std::list::iterator It = spans.begin(); It != spans.end();) + { + Span &span = *It; + Point v = span.GetVector(0.0); + v.normalize(); + + // add the span + if(It == spans.begin())m_vertices.push_back(span.m_p); + m_vertices.push_back(span.m_v.m_p); + + It++; + if(It != spans.end()) + { + Span &next_span = *It; + Point nv = next_span.GetVector(0.0); + nv.normalize(); + double sin_angle = v ^ nv; + bool sharp_corner = ( fabs(sin_angle) > 0.5 ); // angle > 30 degrees + + if(sharp_corner) + { + // add an arc to the start of the next span + int arc_type = ((sin_angle > 0) ? 1 : (-1)); + Point centre = span.m_v.m_p - v * forwards_value; + m_vertices.push_back(CVertex(arc_type, next_span.m_p, centre)); + } + } + } + + if(refit_arcs) + FitArcs(); // find the arcs again + else + UnFitArcs(); // convert those little arcs added to lines +} + +double CCurve::Perim()const +{ + const Point *prev_p = NULL; + double perim = 0.0; + for(std::list::const_iterator It = m_vertices.begin(); It != m_vertices.end(); It++) + { + const CVertex& vertex = *It; + if(prev_p) + { + Span span(*prev_p, vertex); + perim += span.Length(); + } + prev_p = &(vertex.m_p); + } + + return perim; +} + +Point CCurve::PerimToPoint(double perim)const +{ + if(m_vertices.size() == 0)return Point(0, 0); + + const Point *prev_p = NULL; + double kperim = 0.0; + for(std::list::const_iterator It = m_vertices.begin(); It != m_vertices.end(); It++) + { + const CVertex& vertex = *It; + if(prev_p) + { + Span span(*prev_p, vertex); + double length = span.Length(); + if(perim < kperim + length) + { + Point p = span.MidPerim(perim - kperim); + return p; + } + kperim += length; + } + prev_p = &(vertex.m_p); + } + + return m_vertices.back().m_p; +} + +double CCurve::PointToPerim(const Point& p)const +{ + double best_dist = 0.0; + double perim_at_best_dist = 0.0; + Point best_point = Point(0, 0); + bool best_dist_found = false; + + double perim = 0.0; + + const Point *prev_p = NULL; + bool first_span = true; + for(std::list::const_iterator It = m_vertices.begin(); It != m_vertices.end(); It++) + { + const CVertex& vertex = *It; + if(prev_p) + { + Span span(*prev_p, vertex, first_span); + Point near_point = span.NearestPoint(p); + first_span = false; + double dist = near_point.dist(p); + if(!best_dist_found || dist < best_dist) + { + best_dist = dist; + Span span_to_point(*prev_p, CVertex(span.m_v.m_type, near_point, span.m_v.m_c)); + perim_at_best_dist = perim + span_to_point.Length(); + best_dist_found = true; + } + perim += span.Length(); + } + prev_p = &(vertex.m_p); + } + return perim_at_best_dist; +} + +void CCurve::operator+=(const CCurve& curve) +{ + for(std::list::const_iterator It = curve.m_vertices.begin(); It != curve.m_vertices.end(); It++) + { + const CVertex &vt = *It; + if(It == curve.m_vertices.begin()) + { + if((m_vertices.size() == 0) || (It->m_p != m_vertices.back().m_p)) + { + m_vertices.push_back(CVertex(It->m_p)); + } + } + else + { + m_vertices.push_back(vt); + } + } +} + +void CCurve::CurveIntersections(const CCurve& c, std::list &pts)const +{ + CArea a; + a.append(*this); + a.CurveIntersections(c, pts); +} + +void CCurve::SpanIntersections(const Span& s, std::list &pts)const +{ + std::list spans; + GetSpans(spans); + for(std::list::iterator It = spans.begin(); It != spans.end(); It++) + { + Span& span = *It; + std::list pts2; + span.Intersect(s, pts2); + for(std::list::iterator It = pts2.begin(); It != pts2.end(); It++) + { + Point &pt = *It; + if(pts.size() == 0) + { + pts.push_back(pt); + } + else + { + if(pt != pts.back())pts.push_back(pt); + } + } + } +} + +const Point Span::null_point = Point(0, 0); +const CVertex Span::null_vertex = CVertex(Point(0, 0)); + +Span::Span():m_start_span(false), m_p(null_point), m_v(null_vertex){} + +Point Span::NearestPointNotOnSpan(const Point& p)const +{ + if(m_v.m_type == 0) + { + Point Vs = (m_v.m_p - m_p); + Vs.normalize(); + double dp = (p - m_p) * Vs; + return (Vs * dp) + m_p; + } + else + { + double radius = m_p.dist(m_v.m_c); + double r = p.dist(m_v.m_c); + if(r < Point::tolerance)return m_p; + Point vc = (m_v.m_c - p); + return p + vc * ((r - radius) / r); + } +} + +Point Span::NearestPoint(const Point& p)const +{ + Point np = NearestPointNotOnSpan(p); + double t = Parameter(np); + if(t >= 0.0 && t <= 1.0)return np; + + double d1 = p.dist(this->m_p); + double d2 = p.dist(this->m_v.m_p); + + if(d1 < d2)return this->m_p; + else return m_v.m_p; +} + +Point Span::MidPerim(double d)const { + /// returns a point which is 0-d along span + Point p; + if(m_v.m_type == 0) { + Point vs = m_v.m_p - m_p; + vs.normalize(); + p = vs * d + m_p; + } + else { + Point v = m_p - m_v.m_c; + double radius = v.length(); + v.Rotate(d * m_v.m_type / radius); + p = v + m_v.m_c; + } + return p; +} + +Point Span::MidParam(double param)const { + /// returns a point which is 0-1 along span + if(fabs(param) < 0.00000000000001)return m_p; + if(fabs(param - 1.0) < 0.00000000000001)return m_v.m_p; + + Point p; + if(m_v.m_type == 0) { + Point vs = m_v.m_p - m_p; + p = vs * param + m_p; + } + else { + Point v = m_p - m_v.m_c; + v.Rotate(param * IncludedAngle()); + p = v + m_v.m_c; + } + return p; +} + +Point Span::NearestPointToSpan(const Span& p, double &d)const +{ + Point midpoint = MidParam(0.5); + Point np = p.NearestPoint(m_p); + Point best_point = m_p; + double dist = np.dist(m_p); + if(p.m_start_span)dist -= (CArea::m_accuracy * 2); // give start of curve most priority + Point npm = p.NearestPoint(midpoint); + double dm = npm.dist(midpoint) - CArea::m_accuracy; // lie about midpoint distance to give midpoints priority + if(dm < dist){dist = dm; best_point = midpoint;} + Point np2 = p.NearestPoint(m_v.m_p); + double dp2 = np2.dist(m_v.m_p); + if(dp2 < dist){dist = dp2; best_point = m_v.m_p;} + d = dist; + return best_point; +} + +Point Span::NearestPoint(const Span& p, double *d)const +{ + double best_dist; + Point best_point = this->NearestPointToSpan(p, best_dist); + + // try the other way round too + double best_dist2; + Point best_point2 = p.NearestPointToSpan(*this, best_dist2); + if(best_dist2 < best_dist) + { + best_point = NearestPoint(best_point2); + best_dist = best_dist2; + } + + if(d)*d = best_dist; + return best_point; +} + +static int GetQuadrant(const Point& v){ + // 0 = [+,+], 1 = [-,+], 2 = [-,-], 3 = [+,-] + if(v.x > 0) + { + if(v.y > 0) + return 0; + return 3; + } + if(v.y > 0) + return 1; + return 2; +} + +static Point QuadrantEndPoint(int i) +{ + if(i >3)i-=4; + switch(i) + { + case 0: + return Point(0.0,1.0); + case 1: + return Point(-1.0,0.0); + case 2: + return Point(0.0,-1.0); + default: + return Point(1.0,0.0); + } +} + +void Span::GetBox(CBox2D &box) +{ + box.Insert(m_p); + box.Insert(m_v.m_p); + + if(this->m_v.m_type) + { + // arc, add quadrant points + Point vs = m_p - m_v.m_c; + Point ve = m_v.m_p - m_v.m_c; + int qs = GetQuadrant(vs); + int qe = GetQuadrant(ve); + if(m_v.m_type == -1) + { + // swap qs and qe + int t=qs; + qs = qe; + qe = t; + } + + if(qe 1. - 1.0e-10) return 0; + if(inc_ang < -1. + 1.0e-10) + inc_ang = PI; + else { // dot product, v1 . v2 = cos ang + if(inc_ang > 1.0) inc_ang = 1.0; + inc_ang = acos(inc_ang); // 0 to pi radians + + if(dir * (v0 ^ v1) < 0) inc_ang = 2 * PI - inc_ang ; // cp + } + return dir * inc_ang; +} + +double Span::IncludedAngle()const +{ + if(m_v.m_type) + { + Point vs = ~(m_p - m_v.m_c); + Point ve = ~(m_v.m_p - m_v.m_c); + if(m_v.m_type == -1) + { + vs = -vs; + ve = -ve; + } + vs.normalize(); + ve.normalize(); + + return ::IncludedAngle(vs, ve, m_v.m_type); + } + + return 0.0; +} + +double Span::GetArea()const +{ + if(m_v.m_type) + { + double angle = IncludedAngle(); + double radius = m_p.dist(m_v.m_c); + return ( 0.5 * ((m_v.m_c.x - m_p.x) * (m_v.m_c.y + m_p.y) - (m_v.m_c.x - m_v.m_p.x) * (m_v.m_c.y + m_v.m_p.y) - angle * radius * radius)); + } + + return 0.5 * (m_v.m_p.x - m_p.x) * (m_p.y + m_v.m_p.y); +} + +double Span::Parameter(const Point& p)const +{ + double t; + if(m_v.m_type == 0) { + Point v0 = p - m_p; + Point vs = m_v.m_p - m_p; + double length = vs.length(); + vs.normalize(); + t = vs * v0; + t = t / length; + } + else + { + // true if p lies on arc span sp (p must be on circle of span) + Point vs = ~(m_p - m_v.m_c); + Point v = ~(p - m_v.m_c); + vs.normalize(); + v.normalize(); + if(m_v.m_type == -1){ + vs = -vs; + v = -v; + } + double ang = ::IncludedAngle(vs, v, m_v.m_type); + double angle = IncludedAngle(); + t = ang / angle; + } + return t; +} + +bool Span::On(const Point& p, double* t)const +{ + if(p != NearestPoint(p))return false; + if(t)*t = Parameter(p); + return true; +} + +double Span::Length()const +{ + if(m_v.m_type) { + double radius = m_p.dist(m_v.m_c); + return fabs(IncludedAngle()) * radius; + } + + return m_p.dist(m_v.m_p); +} + +Point Span::GetVector(double fraction)const +{ + /// returns the direction vector at point which is 0-1 along span + if(m_v.m_type == 0){ + Point v(m_p, m_v.m_p); + v.normalize(); + return v; + } + + Point p= MidParam(fraction); + Point v(m_v.m_c, p); + v.normalize(); + if(m_v.m_type == 1) + { + return Point(-v.y, v.x); + } + else + { + return Point(v.y, -v.x); + } +} + +void Span::Intersect(const Span& s, std::list &pts)const +{ + // finds all the intersection points between two spans and puts them in the given list + geoff_geometry::Point pInt1, pInt2; + double t[4]; + int num_int = MakeSpan(*this).Intof(MakeSpan(s), pInt1, pInt2, t); + if(num_int > 0)pts.push_back(Point(pInt1.x, pInt1.y)); + if(num_int > 1)pts.push_back(Point(pInt2.x, pInt2.y)); +} + +void tangential_arc(const Point &p0, const Point &p1, const Point &v0, Point &c, int &dir) +{ + geoff_geometry::Point gp0(p0.x, p0.y); + geoff_geometry::Point gp1(p1.x, p1.y); + geoff_geometry::Vector2d gv0(v0.x, v0.y); + geoff_geometry::Point gc; + geoff_geometry::tangential_arc(gp0, gp1, gv0, gc, dir); + c = Point(gc.x, gc.y); +} diff --git a/src/Mod/Path/libarea/Curve.h b/src/Mod/Path/libarea/Curve.h new file mode 100644 index 0000000000..d66215372e --- /dev/null +++ b/src/Mod/Path/libarea/Curve.h @@ -0,0 +1,130 @@ +// Curve.h + +/*============================== +Copyright (c) 2011-2015 Dan Heeks + +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. +3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. +==============================*/ + + +#pragma once + +#include +#include +#include +#include "Point.h" +#include "Box2D.h" + +class Line{ +public: + Point p0; + Point v; + + // constructors + Line(const Point& P0, const Point& V); + + double Dist(const Point& p)const; +}; + +class CArc; + +class CVertex +{ +public: + int m_type; // 0 - line ( or start point ), 1 - anti-clockwise arc, -1 - clockwise arc + Point m_p; // end point + Point m_c; // centre point in absolute coordinates + int m_user_data; + + CVertex():m_type(0), m_p(Point(0, 0)), m_c(Point(0,0)), m_user_data(0){} + CVertex(int type, const Point& p, const Point& c, int user_data = 0); + CVertex(const Point& p, int user_data = 0); +}; + +class Span +{ + Point NearestPointNotOnSpan(const Point& p)const; + double Parameter(const Point& p)const; + Point NearestPointToSpan(const Span& p, double &d)const; + + static const Point null_point; + static const CVertex null_vertex; + +public: + bool m_start_span; + Point m_p; + CVertex m_v; + Span(); + Span(const Point& p, const CVertex& v, bool start_span = false):m_start_span(start_span), m_p(p), m_v(v){} + Point NearestPoint(const Point& p)const; + Point NearestPoint(const Span& p, double *d = NULL)const; + void GetBox(CBox2D &box); + double IncludedAngle()const; + double GetArea()const; + bool On(const Point& p, double* t = NULL)const; + Point MidPerim(double d)const; + Point MidParam(double param)const; + double Length()const; + Point GetVector(double fraction)const; + void Intersect(const Span& s, std::list &pts)const; // finds all the intersection points between two spans +}; + +class CCurve +{ + // a closed curve, please make sure you add an end point, the same as the start point + +protected: + void AddArcOrLines(bool check_for_arc, std::list &new_vertices, std::list& might_be_an_arc, CArc &arc, bool &arc_found, bool &arc_added); + bool CheckForArc(const CVertex& prev_vt, std::list& might_be_an_arc, CArc &arc); + +public: + std::list m_vertices; + void append(const CVertex& vertex); + + void FitArcs(); + void UnFitArcs(); + Point NearestPoint(const Point& p)const; + Point NearestPoint(const CCurve& p, double *d = NULL)const; + Point NearestPoint(const Span& p, double *d = NULL)const; + void GetBox(CBox2D &box); + void Reverse(); + double GetArea()const; + bool IsClockwise()const{return GetArea()>0;} + bool IsClosed()const; + void ChangeStart(const Point &p); + void ChangeEnd(const Point &p); + bool Offset(double leftwards_value); + void OffsetForward(double forwards_value, bool refit_arcs = true); // for drag-knife compensation + void Break(const Point &p); + void ExtractSeparateCurves(const std::list &ordered_points, std::list &separate_curves)const; + double Perim()const; + Point PerimToPoint(double perim)const; + double PointToPerim(const Point& p)const; + void GetSpans(std::list &spans)const; + void RemoveTinySpans(); + void operator+=(const CCurve& p); + void SpanIntersections(const Span& s, std::list &pts)const; + void CurveIntersections(const CCurve& c, std::list &pts)const; +}; + +void tangential_arc(const Point &p0, const Point &p1, const Point &v0, Point &c, int &dir); diff --git a/src/Mod/Path/libarea/Point.h b/src/Mod/Path/libarea/Point.h new file mode 100644 index 0000000000..81546874d0 --- /dev/null +++ b/src/Mod/Path/libarea/Point.h @@ -0,0 +1,75 @@ +// Point.h + +/*============================== +Copyright (c) 2011-2015 Dan Heeks + +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. +3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. +==============================*/ + + +#pragma once + +#include +#include "kurve/geometry.h" + +class Point{ +public: + // can be a position, or a vector + double x, y; + + Point():x(0.0), y(0.0){} + Point(double X, double Y):x(X), y(Y){} + Point(const double* p):x(p[0]), y(p[1]){} + Point(const Point& p0, const Point& p1):x(p1.x - p0.x), y(p1.y - p0.y){} // vector from p0 to p1 + + static double tolerance; + + const Point operator+(const Point& p)const{return Point(x + p.x, y + p.y);} + const Point operator-(const Point& p)const{return Point(x - p.x, y - p.y);} + const Point operator*(double d)const{return Point(x * d, y * d);} + const Point operator/(double d)const{return Point(x / d, y / d);} + bool operator==(const Point& p)const{return fabs(x-p.x) +#define _DEBUG +#else +#include +#endif + +#ifdef __GNUG__ +#pragma implementation +#endif + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "clipper.hpp" +using namespace ClipperLib; + + +namespace bp = boost::python; + +boost::python::list getVertices(const CCurve& curve) { + boost::python::list vlist; + BOOST_FOREACH(const CVertex& vertex, curve.m_vertices) { + vlist.append(vertex); + } + return vlist; +} + +boost::python::list getCurves(const CArea& area) { + boost::python::list clist; + BOOST_FOREACH(const CCurve& curve, area.m_curves) { + clist.append(curve); + } + return clist; +} + +boost::python::tuple transformed_point(const geoff_geometry::Matrix &matrix, double x, double y, double z) +{ + geoff_geometry::Point3d p(x,y,z); + p = p.Transform(matrix); + + return bp::make_tuple(p.x,p.y,p.z); +} + +static void print_curve(const CCurve& c) +{ + unsigned int nvertices = c.m_vertices.size(); + printf("number of vertices = %d\n", nvertices); + int i = 0; + for(std::list::const_iterator It = c.m_vertices.begin(); It != c.m_vertices.end(); It++, i++) + { + const CVertex& vertex = *It; + printf("vertex %d type = %d, x = %g, y = %g", i+1, vertex.m_type, vertex.m_p.x / CArea::m_units, vertex.m_p.y / CArea::m_units); + if(vertex.m_type)printf(", xc = %g, yc = %g", vertex.m_c.x / CArea::m_units, vertex.m_c.y / CArea::m_units); + printf("\n"); + } +} + +static void print_area(const CArea &a) +{ + for(std::list::const_iterator It = a.m_curves.begin(); It != a.m_curves.end(); It++) + { + const CCurve& curve = *It; + print_curve(curve); + } +} + +static unsigned int num_vertices(const CCurve& curve) +{ + return curve.m_vertices.size(); +} + +static CVertex FirstVertex(const CCurve& curve) +{ + return curve.m_vertices.front(); +} + +static CVertex LastVertex(const CCurve& curve) +{ + return curve.m_vertices.back(); +} + +static void set_units(double units) +{ + CArea::m_units = units; +} + +static double get_units() +{ + return CArea::m_units; +} + +static bool holes_linked() +{ + return CArea::HolesLinked(); +} + + +static void append_point(CCurve& c, const Point& p) +{ + c.m_vertices.push_back(CVertex(p)); +} + +static boost::python::tuple nearest_point_to_curve(CCurve& c1, const CCurve& c2) +{ + double dist; + Point p = c1.NearestPoint(c2, &dist); + + return bp::make_tuple(p, dist); +} + +boost::python::list MakePocketToolpath(const CArea& a, double tool_radius, double extra_offset, double stepover, bool from_center, bool use_zig_zag, double zig_angle) +{ + std::list toolpath; + + CAreaPocketParams params(tool_radius, extra_offset, stepover, from_center, use_zig_zag ? ZigZagPocketMode : SpiralPocketMode, zig_angle); + a.SplitAndMakePocketToolpath(toolpath, params); + + boost::python::list clist; + BOOST_FOREACH(const CCurve& c, toolpath) { + clist.append(c); + } + return clist; +} + +boost::python::list SplitArea(const CArea& a) +{ + std::list areas; + a.Split(areas); + + boost::python::list alist; + BOOST_FOREACH(const CArea& a, areas) { + alist.append(a); + } + return alist; +} + + +boost::python::list getCurveSpans(const CCurve& c) +{ + boost::python::list span_list; + const Point *prev_p = NULL; + + for(std::list::const_iterator VIt = c.m_vertices.begin(); VIt != c.m_vertices.end(); VIt++) + { + const CVertex& vertex = *VIt; + + if(prev_p) + { + span_list.append(Span(*prev_p, vertex)); + } + prev_p = &(vertex.m_p); + } + + return span_list; +} + +Span getFirstCurveSpan(const CCurve& c) +{ + if(c.m_vertices.size() < 2)return Span(); + + std::list::const_iterator VIt = c.m_vertices.begin(); + const Point &p = (*VIt).m_p; + VIt++; + return Span(p, *VIt, true); +} + +Span getLastCurveSpan(const CCurve& c) +{ + if(c.m_vertices.size() < 2)return Span(); + + std::list::const_reverse_iterator VIt = c.m_vertices.rbegin(); + const CVertex &v = (*VIt); + VIt++; + + return Span((*VIt).m_p, v, c.m_vertices.size() == 2); +} + +bp::tuple TangentialArc(const Point &p0, const Point &p1, const Point &v0) +{ + Point c; + int dir; + tangential_arc(p0, p1, v0, c, dir); + + return bp::make_tuple(c, dir); +} + +boost::python::list spanIntersect(const Span& span1, const Span& span2) { + boost::python::list plist; + std::list pts; + span1.Intersect(span2, pts); + BOOST_FOREACH(const Point& p, pts) { + plist.append(p); + } + return plist; +} + +//Matrix(boost::python::list &l){} + +boost::shared_ptr matrix_constructor(const boost::python::list& lst) { + double m[16] = {1,0,0,0,0,1,0,0, 0,0,1,0, 0,0,0,1}; + + boost::python::ssize_t n = boost::python::len(lst); + int j = 0; + for(boost::python::ssize_t i=0;i(elem.attr("__float__")()); + j++; + if(j>=16)break; + } + + return boost::shared_ptr( new geoff_geometry::Matrix(m) ); +} + +boost::python::list InsideCurves(const CArea& a, const CCurve& curve) { + boost::python::list plist; + + std::list curves_inside; + a.InsideCurves(curve, curves_inside); + BOOST_FOREACH(const CCurve& c, curves_inside) { + plist.append(c); + } + return plist; +} + +boost::python::list CurveIntersections(const CCurve& c1, const CCurve& c2) { + boost::python::list plist; + + std::list pts; + c1.CurveIntersections(c2, pts); + BOOST_FOREACH(const Point& p, pts) { + plist.append(p); + } + return plist; +} + +boost::python::list AreaIntersections(const CArea& a, const CCurve& c2) { + boost::python::list plist; + + std::list pts; + a.CurveIntersections(c2, pts); + BOOST_FOREACH(const Point& p, pts) { + plist.append(p); + } + return plist; +} + +double AreaGetArea(const CArea& a) +{ + return a.GetArea(); +} + +BOOST_PYTHON_MODULE(area) { + bp::class_("Point") + .def(bp::init()) + .def(bp::init()) + .def(bp::other() * bp::self) + .def(bp::self * bp::other()) + .def(bp::self / bp::other()) + .def(bp::self * bp::other()) + .def(bp::self - bp::other()) + .def(bp::self + bp::other()) + .def(bp::self ^ bp::other()) + .def(bp::self == bp::other()) + .def(bp::self != bp::other()) + .def(-bp::self) + .def(~bp::self) + .def("dist", &Point::dist) + .def("length", &Point::length) + .def("normalize", &Point::normalize) + .def("Rotate", static_cast< void (Point::*)(double, double) >(&Point::Rotate)) + .def("Rotate", static_cast< void (Point::*)(double) >(&Point::Rotate)) + .def_readwrite("x", &Point::x) + .def_readwrite("y", &Point::y) + .def("Transform", &Point::Transform) + ; + + bp::class_("Vertex") + .def(bp::init()) + .def(bp::init()) + .def(bp::init()) + .def(bp::init()) + .def_readwrite("type", &CVertex::m_type) + .def_readwrite("p", &CVertex::m_p) + .def_readwrite("c", &CVertex::m_c) + .def_readwrite("user_data", &CVertex::m_user_data) + ; + + bp::class_("Span") + .def(bp::init()) + .def(bp::init()) + .def("NearestPoint", static_cast< Point (Span::*)(const Point& p)const >(&Span::NearestPoint)) + .def("NearestPoint", static_cast< Point (Span::*)(const Span& p, double *d)const >(&Span::NearestPoint)) + .def("GetBox", &Span::GetBox) + .def("IncludedAngle", &Span::IncludedAngle) + .def("GetArea", &Span::GetArea) + .def("On", &Span::On) + .def("MidPerim", &Span::MidPerim) + .def("MidParam", &Span::MidParam) + .def("Length", &Span::Length) + .def("GetVector", &Span::GetVector) + .def("Intersect", &spanIntersect) + .def_readwrite("p", &Span::m_p) + .def_readwrite("v", &Span::m_v) + ; + + bp::class_("Curve") + .def(bp::init()) + .def("getVertices", &getVertices) + .def("append",&CCurve::append) + .def("append",&append_point) + .def("text", &print_curve) + .def("NearestPoint", static_cast< Point (CCurve::*)(const Point& p)const >(&CCurve::NearestPoint)) + .def("NearestPoint", &nearest_point_to_curve) + .def("Reverse", &CCurve::Reverse) + .def("getNumVertices", &num_vertices) + .def("FirstVertex", &FirstVertex) + .def("LastVertex", &LastVertex) + .def("GetArea", &CCurve::GetArea) + .def("IsClockwise", &CCurve::IsClockwise) + .def("IsClosed", &CCurve::IsClosed) + .def("ChangeStart",&CCurve::ChangeStart) + .def("ChangeEnd",&CCurve::ChangeEnd) + .def("Offset",&CCurve::Offset) + .def("OffsetForward",&CCurve::OffsetForward) + .def("GetSpans",&getCurveSpans) + .def("GetFirstSpan",&getFirstCurveSpan) + .def("GetLastSpan",&getLastCurveSpan) + .def("Break",&CCurve::Break) + .def("Perim",&CCurve::Perim) + .def("PerimToPoint",&CCurve::PerimToPoint) + .def("PointToPerim",&CCurve::PointToPerim) + .def("FitArcs",&CCurve::FitArcs) + .def("UnFitArcs",&CCurve::UnFitArcs) + .def("Intersections",&CurveIntersections) + ; + + bp::class_("Box") + .def(bp::init()) + .def("MinX", &CBox2D::MinX) + .def("MaxX", &CBox2D::MaxX) + .def("MinY", &CBox2D::MinY) + .def("MaxY", &CBox2D::MaxY) + ; + + bp::class_("Area") + .def(bp::init()) + .def("getCurves", &getCurves) + .def("append",&CArea::append) + .def("Subtract",&CArea::Subtract) + .def("Intersect",&CArea::Intersect) + .def("Union",&CArea::Union) + .def("Offset",&CArea::Offset) + .def("FitArcs",&CArea::FitArcs) + .def("text", &print_area) + .def("num_curves", &CArea::num_curves) + .def("NearestPoint", &CArea::NearestPoint) + .def("GetBox", &CArea::GetBox) + .def("Reorder", &CArea::Reorder) + .def("MakePocketToolpath", &MakePocketToolpath) + .def("Split", &SplitArea) + .def("InsideCurves", &InsideCurves) + .def("Thicken", &CArea::Thicken) + .def("Intersections",&AreaIntersections) + .def("GetArea",&AreaGetArea) + ; + + bp::class_ > ("Matrix") + .def(bp::init()) + .def("__init__", bp::make_constructor(&matrix_constructor)) + .def("TransformedPoint", &transformed_point) + .def("Multiply", &geoff_geometry::Matrix::Multiply) + ; + + bp::def("set_units", set_units); + bp::def("get_units", get_units); + bp::def("holes_linked", holes_linked); + bp::def("TangentialArc", TangentialArc); +} diff --git a/src/Mod/Path/libarea/PythonStuff.h b/src/Mod/Path/libarea/PythonStuff.h new file mode 100644 index 0000000000..f767f16bca --- /dev/null +++ b/src/Mod/Path/libarea/PythonStuff.h @@ -0,0 +1,34 @@ +// PythonStuff.h + +/*============================== +Copyright (c) 2011-2015 Dan Heeks + +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. +3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. +==============================*/ + + +extern void Message(const char*); + +void PythonInit(); +void PythonFinish(); + diff --git a/src/Mod/Path/libarea/clipper.cpp b/src/Mod/Path/libarea/clipper.cpp new file mode 100644 index 0000000000..903d0651dc --- /dev/null +++ b/src/Mod/Path/libarea/clipper.cpp @@ -0,0 +1,4460 @@ +/******************************************************************************* +* * +* Author : Angus Johnson * +* Version : 6.2.0 * +* Date : 2 October 2014 * +* Website : http://www.angusj.com * +* Copyright : Angus Johnson 2010-2014 * +* * +* License: * +* Use, modification & distribution is subject to Boost Software License Ver 1. * +* http://www.boost.org/LICENSE_1_0.txt * +* * +* Attributions: * +* The code in this library is an extension of Bala Vatti's clipping algorithm: * +* "A generic solution to polygon clipping" * +* Communications of the ACM, Vol 35, Issue 7 (July 1992) pp 56-63. * +* http://portal.acm.org/citation.cfm?id=129906 * +* * +* Computer graphics and geometric modeling: implementation and algorithms * +* By Max K. Agoston * +* Springer; 1 edition (January 4, 2005) * +* http://books.google.com/books?q=vatti+clipping+agoston * +* * +* See also: * +* "Polygon Offsetting by Computing Winding Numbers" * +* Paper no. DETC2005-85513 pp. 565-575 * +* ASME 2005 International Design Engineering Technical Conferences * +* and Computers and Information in Engineering Conference (IDETC/CIE2005) * +* September 24-28, 2005 , Long Beach, California, USA * +* http://www.me.berkeley.edu/~mcmains/pubs/DAC05OffsetPolygon.pdf * +* * +*******************************************************************************/ + +/******************************************************************************* +* * +* This is a translation of the Delphi Clipper library and the naming style * +* used has retained a Delphi flavour. * +* * +*******************************************************************************/ + +#include "clipper.hpp" +#include +#include +#include +#include +#include +#include +#include +#include + +namespace ClipperLib { + +static double const pi = 3.141592653589793238; +static double const two_pi = pi *2; +static double const def_arc_tolerance = 0.25; + +enum Direction { dRightToLeft, dLeftToRight }; + +static int const Unassigned = -1; //edge not currently 'owning' a solution +static int const Skip = -2; //edge that would otherwise close a path + +#define HORIZONTAL (-1.0E+40) +#define TOLERANCE (1.0e-20) +#define NEAR_ZERO(val) (((val) > -TOLERANCE) && ((val) < TOLERANCE)) + +struct TEdge { + IntPoint Bot; + IntPoint Curr; + IntPoint Top; + IntPoint Delta; + double Dx; + PolyType PolyTyp; + EdgeSide Side; + int WindDelta; //1 or -1 depending on winding direction + int WindCnt; + int WindCnt2; //winding count of the opposite polytype + int OutIdx; + TEdge *Next; + TEdge *Prev; + TEdge *NextInLML; + TEdge *NextInAEL; + TEdge *PrevInAEL; + TEdge *NextInSEL; + TEdge *PrevInSEL; +}; + +struct IntersectNode { + TEdge *Edge1; + TEdge *Edge2; + IntPoint Pt; +}; + +struct LocalMinimum { + cInt Y; + TEdge *LeftBound; + TEdge *RightBound; +}; + +struct OutPt; + +struct OutRec { + int Idx; + bool IsHole; + bool IsOpen; + OutRec *FirstLeft; //see comments in clipper.pas + PolyNode *PolyNd; + OutPt *Pts; + OutPt *BottomPt; +}; + +struct OutPt { + int Idx; + IntPoint Pt; + OutPt *Next; + OutPt *Prev; +}; + +struct Join { + OutPt *OutPt1; + OutPt *OutPt2; + IntPoint OffPt; +}; + +struct LocMinSorter +{ + inline bool operator()(const LocalMinimum& locMin1, const LocalMinimum& locMin2) + { + return locMin2.Y < locMin1.Y; + } +}; + +//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ + +inline cInt Round(double val) +{ + if ((val < 0)) return static_cast(val - 0.5); + else return static_cast(val + 0.5); +} +//------------------------------------------------------------------------------ + +inline cInt Abs(cInt val) +{ + return val < 0 ? -val : val; +} + +//------------------------------------------------------------------------------ +// PolyTree methods ... +//------------------------------------------------------------------------------ + +void PolyTree::Clear() +{ + for (PolyNodes::size_type i = 0; i < AllNodes.size(); ++i) + delete AllNodes[i]; + AllNodes.resize(0); + Childs.resize(0); +} +//------------------------------------------------------------------------------ + +PolyNode* PolyTree::GetFirst() const +{ + if (!Childs.empty()) + return Childs[0]; + else + return 0; +} +//------------------------------------------------------------------------------ + +int PolyTree::Total() const +{ + return (int)AllNodes.size(); +} + +//------------------------------------------------------------------------------ +// PolyNode methods ... +//------------------------------------------------------------------------------ + +PolyNode::PolyNode(): Childs(), Parent(0), Index(0), m_IsOpen(false) +{ +} +//------------------------------------------------------------------------------ + +int PolyNode::ChildCount() const +{ + return (int)Childs.size(); +} +//------------------------------------------------------------------------------ + +void PolyNode::AddChild(PolyNode& child) +{ + unsigned cnt = (unsigned)Childs.size(); + Childs.push_back(&child); + child.Parent = this; + child.Index = cnt; +} +//------------------------------------------------------------------------------ + +PolyNode* PolyNode::GetNext() const +{ + if (!Childs.empty()) + return Childs[0]; + else + return GetNextSiblingUp(); +} +//------------------------------------------------------------------------------ + +PolyNode* PolyNode::GetNextSiblingUp() const +{ + if (!Parent) //protects against PolyTree.GetNextSiblingUp() + return 0; + else if (Index == Parent->Childs.size() - 1) + return Parent->GetNextSiblingUp(); + else + return Parent->Childs[Index + 1]; +} +//------------------------------------------------------------------------------ + +bool PolyNode::IsHole() const +{ + bool result = true; + PolyNode* node = Parent; + while (node) + { + result = !result; + node = node->Parent; + } + return result; +} +//------------------------------------------------------------------------------ + +bool PolyNode::IsOpen() const +{ + return m_IsOpen; +} +//------------------------------------------------------------------------------ + +#ifndef use_int32 + +//------------------------------------------------------------------------------ +// Int128 class (enables safe math on signed 64bit integers) +// eg Int128 val1((long64)9223372036854775807); //ie 2^63 -1 +// Int128 val2((long64)9223372036854775807); +// Int128 val3 = val1 * val2; +// val3.AsString => "85070591730234615847396907784232501249" (8.5e+37) +//------------------------------------------------------------------------------ + +class Int128 +{ + public: + ulong64 lo; + long64 hi; + + Int128(long64 _lo = 0) + { + lo = (ulong64)_lo; + if (_lo < 0) hi = -1; else hi = 0; + } + + + Int128(const Int128 &val): lo(val.lo), hi(val.hi){} + + Int128(const long64& _hi, const ulong64& _lo): lo(_lo), hi(_hi){} + + Int128& operator = (const long64 &val) + { + lo = (ulong64)val; + if (val < 0) hi = -1; else hi = 0; + return *this; + } + + bool operator == (const Int128 &val) const + {return (hi == val.hi && lo == val.lo);} + + bool operator != (const Int128 &val) const + { return !(*this == val);} + + bool operator > (const Int128 &val) const + { + if (hi != val.hi) + return hi > val.hi; + else + return lo > val.lo; + } + + bool operator < (const Int128 &val) const + { + if (hi != val.hi) + return hi < val.hi; + else + return lo < val.lo; + } + + bool operator >= (const Int128 &val) const + { return !(*this < val);} + + bool operator <= (const Int128 &val) const + { return !(*this > val);} + + Int128& operator += (const Int128 &rhs) + { + hi += rhs.hi; + lo += rhs.lo; + if (lo < rhs.lo) hi++; + return *this; + } + + Int128 operator + (const Int128 &rhs) const + { + Int128 result(*this); + result+= rhs; + return result; + } + + Int128& operator -= (const Int128 &rhs) + { + *this += -rhs; + return *this; + } + + Int128 operator - (const Int128 &rhs) const + { + Int128 result(*this); + result -= rhs; + return result; + } + + Int128 operator-() const //unary negation + { + if (lo == 0) + return Int128(-hi, 0); + else + return Int128(~hi, ~lo + 1); + } + + operator double() const + { + const double shift64 = 18446744073709551616.0; //2^64 + if (hi < 0) + { + if (lo == 0) return (double)hi * shift64; + else return -(double)(~lo + ~hi * shift64); + } + else + return (double)(lo + hi * shift64); + } + +}; +//------------------------------------------------------------------------------ + +Int128 Int128Mul (long64 lhs, long64 rhs) +{ + bool negate = (lhs < 0) != (rhs < 0); + + if (lhs < 0) lhs = -lhs; + ulong64 int1Hi = ulong64(lhs) >> 32; + ulong64 int1Lo = ulong64(lhs & 0xFFFFFFFF); + + if (rhs < 0) rhs = -rhs; + ulong64 int2Hi = ulong64(rhs) >> 32; + ulong64 int2Lo = ulong64(rhs & 0xFFFFFFFF); + + //nb: see comments in clipper.pas + ulong64 a = int1Hi * int2Hi; + ulong64 b = int1Lo * int2Lo; + ulong64 c = int1Hi * int2Lo + int1Lo * int2Hi; + + Int128 tmp; + tmp.hi = long64(a + (c >> 32)); + tmp.lo = long64(c << 32); + tmp.lo += long64(b); + if (tmp.lo < b) tmp.hi++; + if (negate) tmp = -tmp; + return tmp; +}; +#endif + +//------------------------------------------------------------------------------ +// Miscellaneous global functions +//------------------------------------------------------------------------------ + +void Swap(cInt& val1, cInt& val2) +{ + cInt tmp = val1; + val1 = val2; + val2 = tmp; +} +//------------------------------------------------------------------------------ +bool Orientation(const Path &poly) +{ + return Area(poly) >= 0; +} +//------------------------------------------------------------------------------ + +double Area(const Path &poly) +{ + int size = (int)poly.size(); + if (size < 3) return 0; + + double a = 0; + for (int i = 0, j = size -1; i < size; ++i) + { + a += ((double)poly[j].X + poly[i].X) * ((double)poly[j].Y - poly[i].Y); + j = i; + } + return -a * 0.5; +} +//------------------------------------------------------------------------------ + +double Area(const OutRec &outRec) +{ + OutPt *op = outRec.Pts; + if (!op) return 0; + double a = 0; + do { + a += (double)(op->Prev->Pt.X + op->Pt.X) * (double)(op->Prev->Pt.Y - op->Pt.Y); + op = op->Next; + } while (op != outRec.Pts); + return a * 0.5; +} +//------------------------------------------------------------------------------ + +bool PointIsVertex(const IntPoint &Pt, OutPt *pp) +{ + OutPt *pp2 = pp; + do + { + if (pp2->Pt == Pt) return true; + pp2 = pp2->Next; + } + while (pp2 != pp); + return false; +} +//------------------------------------------------------------------------------ + +int PointInPolygon (const IntPoint &pt, const Path &path) +{ + //returns 0 if false, +1 if true, -1 if pt ON polygon boundary + //See "The Point in Polygon Problem for Arbitrary Polygons" by Hormann & Agathos + //http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.88.5498&rep=rep1&type=pdf + int result = 0; + size_t cnt = path.size(); + if (cnt < 3) return 0; + IntPoint ip = path[0]; + for(size_t i = 1; i <= cnt; ++i) + { + IntPoint ipNext = (i == cnt ? path[0] : path[i]); + if (ipNext.Y == pt.Y) + { + if ((ipNext.X == pt.X) || (ip.Y == pt.Y && + ((ipNext.X > pt.X) == (ip.X < pt.X)))) return -1; + } + if ((ip.Y < pt.Y) != (ipNext.Y < pt.Y)) + { + if (ip.X >= pt.X) + { + if (ipNext.X > pt.X) result = 1 - result; + else + { + double d = (double)(ip.X - pt.X) * (ipNext.Y - pt.Y) - + (double)(ipNext.X - pt.X) * (ip.Y - pt.Y); + if (!d) return -1; + if ((d > 0) == (ipNext.Y > ip.Y)) result = 1 - result; + } + } else + { + if (ipNext.X > pt.X) + { + double d = (double)(ip.X - pt.X) * (ipNext.Y - pt.Y) - + (double)(ipNext.X - pt.X) * (ip.Y - pt.Y); + if (!d) return -1; + if ((d > 0) == (ipNext.Y > ip.Y)) result = 1 - result; + } + } + } + ip = ipNext; + } + return result; +} +//------------------------------------------------------------------------------ + +int PointInPolygon (const IntPoint &pt, OutPt *op) +{ + //returns 0 if false, +1 if true, -1 if pt ON polygon boundary + int result = 0; + OutPt* startOp = op; + for(;;) + { + if (op->Next->Pt.Y == pt.Y) + { + if ((op->Next->Pt.X == pt.X) || (op->Pt.Y == pt.Y && + ((op->Next->Pt.X > pt.X) == (op->Pt.X < pt.X)))) return -1; + } + if ((op->Pt.Y < pt.Y) != (op->Next->Pt.Y < pt.Y)) + { + if (op->Pt.X >= pt.X) + { + if (op->Next->Pt.X > pt.X) result = 1 - result; + else + { + double d = (double)(op->Pt.X - pt.X) * (op->Next->Pt.Y - pt.Y) - + (double)(op->Next->Pt.X - pt.X) * (op->Pt.Y - pt.Y); + if (!d) return -1; + if ((d > 0) == (op->Next->Pt.Y > op->Pt.Y)) result = 1 - result; + } + } else + { + if (op->Next->Pt.X > pt.X) + { + double d = (double)(op->Pt.X - pt.X) * (op->Next->Pt.Y - pt.Y) - + (double)(op->Next->Pt.X - pt.X) * (op->Pt.Y - pt.Y); + if (!d) return -1; + if ((d > 0) == (op->Next->Pt.Y > op->Pt.Y)) result = 1 - result; + } + } + } + op = op->Next; + if (startOp == op) break; + } + return result; +} +//------------------------------------------------------------------------------ + +bool Poly2ContainsPoly1(OutPt *OutPt1, OutPt *OutPt2) +{ + OutPt* op = OutPt1; + do + { + //nb: PointInPolygon returns 0 if false, +1 if true, -1 if pt on polygon + int res = PointInPolygon(op->Pt, OutPt2); + if (res >= 0) return res > 0; + op = op->Next; + } + while (op != OutPt1); + return true; +} +//---------------------------------------------------------------------- + +bool SlopesEqual(const TEdge &e1, const TEdge &e2, bool UseFullInt64Range) +{ +#ifndef use_int32 + if (UseFullInt64Range) + return Int128Mul(e1.Delta.Y, e2.Delta.X) == Int128Mul(e1.Delta.X, e2.Delta.Y); + else +#endif + return e1.Delta.Y * e2.Delta.X == e1.Delta.X * e2.Delta.Y; +} +//------------------------------------------------------------------------------ + +bool SlopesEqual(const IntPoint pt1, const IntPoint pt2, + const IntPoint pt3, bool UseFullInt64Range) +{ +#ifndef use_int32 + if (UseFullInt64Range) + return Int128Mul(pt1.Y-pt2.Y, pt2.X-pt3.X) == Int128Mul(pt1.X-pt2.X, pt2.Y-pt3.Y); + else +#endif + return (pt1.Y-pt2.Y)*(pt2.X-pt3.X) == (pt1.X-pt2.X)*(pt2.Y-pt3.Y); +} +//------------------------------------------------------------------------------ + +bool SlopesEqual(const IntPoint pt1, const IntPoint pt2, + const IntPoint pt3, const IntPoint pt4, bool UseFullInt64Range) +{ +#ifndef use_int32 + if (UseFullInt64Range) + return Int128Mul(pt1.Y-pt2.Y, pt3.X-pt4.X) == Int128Mul(pt1.X-pt2.X, pt3.Y-pt4.Y); + else +#endif + return (pt1.Y-pt2.Y)*(pt3.X-pt4.X) == (pt1.X-pt2.X)*(pt3.Y-pt4.Y); +} +//------------------------------------------------------------------------------ + +inline bool IsHorizontal(TEdge &e) +{ + return e.Delta.Y == 0; +} +//------------------------------------------------------------------------------ + +inline double GetDx(const IntPoint pt1, const IntPoint pt2) +{ + return (pt1.Y == pt2.Y) ? + HORIZONTAL : (double)(pt2.X - pt1.X) / (pt2.Y - pt1.Y); +} +//--------------------------------------------------------------------------- + +inline void SetDx(TEdge &e) +{ + e.Delta.X = (e.Top.X - e.Bot.X); + e.Delta.Y = (e.Top.Y - e.Bot.Y); + + if (e.Delta.Y == 0) e.Dx = HORIZONTAL; + else e.Dx = (double)(e.Delta.X) / e.Delta.Y; +} +//--------------------------------------------------------------------------- + +inline void SwapSides(TEdge &Edge1, TEdge &Edge2) +{ + EdgeSide Side = Edge1.Side; + Edge1.Side = Edge2.Side; + Edge2.Side = Side; +} +//------------------------------------------------------------------------------ + +inline void SwapPolyIndexes(TEdge &Edge1, TEdge &Edge2) +{ + int OutIdx = Edge1.OutIdx; + Edge1.OutIdx = Edge2.OutIdx; + Edge2.OutIdx = OutIdx; +} +//------------------------------------------------------------------------------ + +inline cInt TopX(TEdge &edge, const cInt currentY) +{ + return ( currentY == edge.Top.Y ) ? + edge.Top.X : edge.Bot.X + Round(edge.Dx *(currentY - edge.Bot.Y)); +} +//------------------------------------------------------------------------------ + +void IntersectPoint(TEdge &Edge1, TEdge &Edge2, IntPoint &ip) +{ +#ifdef use_xyz + ip.Z = 0; +#endif + + double b1, b2; + if (Edge1.Dx == Edge2.Dx) + { + ip.Y = Edge1.Curr.Y; + ip.X = TopX(Edge1, ip.Y); + return; + } + else if (Edge1.Delta.X == 0) + { + ip.X = Edge1.Bot.X; + if (IsHorizontal(Edge2)) + ip.Y = Edge2.Bot.Y; + else + { + b2 = Edge2.Bot.Y - (Edge2.Bot.X / Edge2.Dx); + ip.Y = Round(ip.X / Edge2.Dx + b2); + } + } + else if (Edge2.Delta.X == 0) + { + ip.X = Edge2.Bot.X; + if (IsHorizontal(Edge1)) + ip.Y = Edge1.Bot.Y; + else + { + b1 = Edge1.Bot.Y - (Edge1.Bot.X / Edge1.Dx); + ip.Y = Round(ip.X / Edge1.Dx + b1); + } + } + else + { + b1 = Edge1.Bot.X - Edge1.Bot.Y * Edge1.Dx; + b2 = Edge2.Bot.X - Edge2.Bot.Y * Edge2.Dx; + double q = (b2-b1) / (Edge1.Dx - Edge2.Dx); + ip.Y = Round(q); + if (std::fabs(Edge1.Dx) < std::fabs(Edge2.Dx)) + ip.X = Round(Edge1.Dx * q + b1); + else + ip.X = Round(Edge2.Dx * q + b2); + } + + if (ip.Y < Edge1.Top.Y || ip.Y < Edge2.Top.Y) + { + if (Edge1.Top.Y > Edge2.Top.Y) + ip.Y = Edge1.Top.Y; + else + ip.Y = Edge2.Top.Y; + if (std::fabs(Edge1.Dx) < std::fabs(Edge2.Dx)) + ip.X = TopX(Edge1, ip.Y); + else + ip.X = TopX(Edge2, ip.Y); + } + //finally, don't allow 'ip' to be BELOW curr.Y (ie bottom of scanbeam) ... + if (ip.Y > Edge1.Curr.Y) + { + ip.Y = Edge1.Curr.Y; + //use the more vertical edge to derive X ... + if (std::fabs(Edge1.Dx) > std::fabs(Edge2.Dx)) + ip.X = TopX(Edge2, ip.Y); else + ip.X = TopX(Edge1, ip.Y); + } +} +//------------------------------------------------------------------------------ + +void ReversePolyPtLinks(OutPt *pp) +{ + if (!pp) return; + OutPt *pp1, *pp2; + pp1 = pp; + do { + pp2 = pp1->Next; + pp1->Next = pp1->Prev; + pp1->Prev = pp2; + pp1 = pp2; + } while( pp1 != pp ); +} +//------------------------------------------------------------------------------ + +void DisposeOutPts(OutPt*& pp) +{ + if (pp == 0) return; + pp->Prev->Next = 0; + while( pp ) + { + OutPt *tmpPp = pp; + pp = pp->Next; + delete tmpPp; + } +} +//------------------------------------------------------------------------------ + +inline void InitEdge(TEdge* e, TEdge* eNext, TEdge* ePrev, const IntPoint& Pt) +{ + std::memset(e, 0, sizeof(TEdge)); + e->Next = eNext; + e->Prev = ePrev; + e->Curr = Pt; + e->OutIdx = Unassigned; +} +//------------------------------------------------------------------------------ + +void InitEdge2(TEdge& e, PolyType Pt) +{ + if (e.Curr.Y >= e.Next->Curr.Y) + { + e.Bot = e.Curr; + e.Top = e.Next->Curr; + } else + { + e.Top = e.Curr; + e.Bot = e.Next->Curr; + } + SetDx(e); + e.PolyTyp = Pt; +} +//------------------------------------------------------------------------------ + +TEdge* RemoveEdge(TEdge* e) +{ + //removes e from double_linked_list (but without removing from memory) + e->Prev->Next = e->Next; + e->Next->Prev = e->Prev; + TEdge* result = e->Next; + e->Prev = 0; //flag as removed (see ClipperBase.Clear) + return result; +} +//------------------------------------------------------------------------------ + +inline void ReverseHorizontal(TEdge &e) +{ + //swap horizontal edges' Top and Bottom x's so they follow the natural + //progression of the bounds - ie so their xbots will align with the + //adjoining lower edge. [Helpful in the ProcessHorizontal() method.] + Swap(e.Top.X, e.Bot.X); +#ifdef use_xyz + Swap(e.Top.Z, e.Bot.Z); +#endif +} +//------------------------------------------------------------------------------ + +void SwapPoints(IntPoint &pt1, IntPoint &pt2) +{ + IntPoint tmp = pt1; + pt1 = pt2; + pt2 = tmp; +} +//------------------------------------------------------------------------------ + +bool GetOverlapSegment(IntPoint pt1a, IntPoint pt1b, IntPoint pt2a, + IntPoint pt2b, IntPoint &pt1, IntPoint &pt2) +{ + //precondition: segments are Collinear. + if (Abs(pt1a.X - pt1b.X) > Abs(pt1a.Y - pt1b.Y)) + { + if (pt1a.X > pt1b.X) SwapPoints(pt1a, pt1b); + if (pt2a.X > pt2b.X) SwapPoints(pt2a, pt2b); + if (pt1a.X > pt2a.X) pt1 = pt1a; else pt1 = pt2a; + if (pt1b.X < pt2b.X) pt2 = pt1b; else pt2 = pt2b; + return pt1.X < pt2.X; + } else + { + if (pt1a.Y < pt1b.Y) SwapPoints(pt1a, pt1b); + if (pt2a.Y < pt2b.Y) SwapPoints(pt2a, pt2b); + if (pt1a.Y < pt2a.Y) pt1 = pt1a; else pt1 = pt2a; + if (pt1b.Y > pt2b.Y) pt2 = pt1b; else pt2 = pt2b; + return pt1.Y > pt2.Y; + } +} +//------------------------------------------------------------------------------ + +bool FirstIsBottomPt(const OutPt* btmPt1, const OutPt* btmPt2) +{ + OutPt *p = btmPt1->Prev; + while ((p->Pt == btmPt1->Pt) && (p != btmPt1)) p = p->Prev; + double dx1p = std::fabs(GetDx(btmPt1->Pt, p->Pt)); + p = btmPt1->Next; + while ((p->Pt == btmPt1->Pt) && (p != btmPt1)) p = p->Next; + double dx1n = std::fabs(GetDx(btmPt1->Pt, p->Pt)); + + p = btmPt2->Prev; + while ((p->Pt == btmPt2->Pt) && (p != btmPt2)) p = p->Prev; + double dx2p = std::fabs(GetDx(btmPt2->Pt, p->Pt)); + p = btmPt2->Next; + while ((p->Pt == btmPt2->Pt) && (p != btmPt2)) p = p->Next; + double dx2n = std::fabs(GetDx(btmPt2->Pt, p->Pt)); + return (dx1p >= dx2p && dx1p >= dx2n) || (dx1n >= dx2p && dx1n >= dx2n); +} +//------------------------------------------------------------------------------ + +OutPt* GetBottomPt(OutPt *pp) +{ + OutPt* dups = 0; + OutPt* p = pp->Next; + while (p != pp) + { + if (p->Pt.Y > pp->Pt.Y) + { + pp = p; + dups = 0; + } + else if (p->Pt.Y == pp->Pt.Y && p->Pt.X <= pp->Pt.X) + { + if (p->Pt.X < pp->Pt.X) + { + dups = 0; + pp = p; + } else + { + if (p->Next != pp && p->Prev != pp) dups = p; + } + } + p = p->Next; + } + if (dups) + { + //there appears to be at least 2 vertices at BottomPt so ... + while (dups != p) + { + if (!FirstIsBottomPt(p, dups)) pp = dups; + dups = dups->Next; + while (dups->Pt != pp->Pt) dups = dups->Next; + } + } + return pp; +} +//------------------------------------------------------------------------------ + +bool Pt2IsBetweenPt1AndPt3(const IntPoint pt1, + const IntPoint pt2, const IntPoint pt3) +{ + if ((pt1 == pt3) || (pt1 == pt2) || (pt3 == pt2)) + return false; + else if (pt1.X != pt3.X) + return (pt2.X > pt1.X) == (pt2.X < pt3.X); + else + return (pt2.Y > pt1.Y) == (pt2.Y < pt3.Y); +} +//------------------------------------------------------------------------------ + +bool HorzSegmentsOverlap(cInt seg1a, cInt seg1b, cInt seg2a, cInt seg2b) +{ + if (seg1a > seg1b) Swap(seg1a, seg1b); + if (seg2a > seg2b) Swap(seg2a, seg2b); + return (seg1a < seg2b) && (seg2a < seg1b); +} + +//------------------------------------------------------------------------------ +// ClipperBase class methods ... +//------------------------------------------------------------------------------ + +ClipperBase::ClipperBase() //constructor +{ + m_CurrentLM = m_MinimaList.begin(); //begin() == end() here + m_UseFullRange = false; +} +//------------------------------------------------------------------------------ + +ClipperBase::~ClipperBase() //destructor +{ + Clear(); +} +//------------------------------------------------------------------------------ + +void RangeTest(const IntPoint& Pt, bool& useFullRange) +{ + if (useFullRange) + { + if (Pt.X > hiRange || Pt.Y > hiRange || -Pt.X > hiRange || -Pt.Y > hiRange) + throw "Coordinate outside allowed range"; + } + else if (Pt.X > loRange|| Pt.Y > loRange || -Pt.X > loRange || -Pt.Y > loRange) + { + useFullRange = true; + RangeTest(Pt, useFullRange); + } +} +//------------------------------------------------------------------------------ + +TEdge* FindNextLocMin(TEdge* E) +{ + for (;;) + { + while (E->Bot != E->Prev->Bot || E->Curr == E->Top) E = E->Next; + if (!IsHorizontal(*E) && !IsHorizontal(*E->Prev)) break; + while (IsHorizontal(*E->Prev)) E = E->Prev; + TEdge* E2 = E; + while (IsHorizontal(*E)) E = E->Next; + if (E->Top.Y == E->Prev->Bot.Y) continue; //ie just an intermediate horz. + if (E2->Prev->Bot.X < E->Bot.X) E = E2; + break; + } + return E; +} +//------------------------------------------------------------------------------ + +TEdge* ClipperBase::ProcessBound(TEdge* E, bool NextIsForward) +{ + TEdge *Result = E; + TEdge *Horz = 0; + + if (E->OutIdx == Skip) + { + //if edges still remain in the current bound beyond the skip edge then + //create another LocMin and call ProcessBound once more + if (NextIsForward) + { + while (E->Top.Y == E->Next->Bot.Y) E = E->Next; + //don't include top horizontals when parsing a bound a second time, + //they will be contained in the opposite bound ... + while (E != Result && IsHorizontal(*E)) E = E->Prev; + } + else + { + while (E->Top.Y == E->Prev->Bot.Y) E = E->Prev; + while (E != Result && IsHorizontal(*E)) E = E->Next; + } + + if (E == Result) + { + if (NextIsForward) Result = E->Next; + else Result = E->Prev; + } + else + { + //there are more edges in the bound beyond result starting with E + if (NextIsForward) + E = Result->Next; + else + E = Result->Prev; + MinimaList::value_type locMin; + locMin.Y = E->Bot.Y; + locMin.LeftBound = 0; + locMin.RightBound = E; + E->WindDelta = 0; + Result = ProcessBound(E, NextIsForward); + m_MinimaList.push_back(locMin); + } + return Result; + } + + TEdge *EStart; + + if (IsHorizontal(*E)) + { + //We need to be careful with open paths because this may not be a + //true local minima (ie E may be following a skip edge). + //Also, consecutive horz. edges may start heading left before going right. + if (NextIsForward) + EStart = E->Prev; + else + EStart = E->Next; + if (EStart->OutIdx != Skip) + { + if (IsHorizontal(*EStart)) //ie an adjoining horizontal skip edge + { + if (EStart->Bot.X != E->Bot.X && EStart->Top.X != E->Bot.X) + ReverseHorizontal(*E); + } + else if (EStart->Bot.X != E->Bot.X) + ReverseHorizontal(*E); + } + } + + EStart = E; + if (NextIsForward) + { + while (Result->Top.Y == Result->Next->Bot.Y && Result->Next->OutIdx != Skip) + Result = Result->Next; + if (IsHorizontal(*Result) && Result->Next->OutIdx != Skip) + { + //nb: at the top of a bound, horizontals are added to the bound + //only when the preceding edge attaches to the horizontal's left vertex + //unless a Skip edge is encountered when that becomes the top divide + Horz = Result; + while (IsHorizontal(*Horz->Prev)) Horz = Horz->Prev; + if (Horz->Prev->Top.X == Result->Next->Top.X) + { + if (!NextIsForward) Result = Horz->Prev; + } + else if (Horz->Prev->Top.X > Result->Next->Top.X) Result = Horz->Prev; + } + while (E != Result) + { + E->NextInLML = E->Next; + if (IsHorizontal(*E) && E != EStart && + E->Bot.X != E->Prev->Top.X) ReverseHorizontal(*E); + E = E->Next; + } + if (IsHorizontal(*E) && E != EStart && E->Bot.X != E->Prev->Top.X) + ReverseHorizontal(*E); + Result = Result->Next; //move to the edge just beyond current bound + } else + { + while (Result->Top.Y == Result->Prev->Bot.Y && Result->Prev->OutIdx != Skip) + Result = Result->Prev; + if (IsHorizontal(*Result) && Result->Prev->OutIdx != Skip) + { + Horz = Result; + while (IsHorizontal(*Horz->Next)) Horz = Horz->Next; + if (Horz->Next->Top.X == Result->Prev->Top.X) + { + if (!NextIsForward) Result = Horz->Next; + } + else if (Horz->Next->Top.X > Result->Prev->Top.X) Result = Horz->Next; + } + + while (E != Result) + { + E->NextInLML = E->Prev; + if (IsHorizontal(*E) && E != EStart && E->Bot.X != E->Next->Top.X) + ReverseHorizontal(*E); + E = E->Prev; + } + if (IsHorizontal(*E) && E != EStart && E->Bot.X != E->Next->Top.X) + ReverseHorizontal(*E); + Result = Result->Prev; //move to the edge just beyond current bound + } + + return Result; +} +//------------------------------------------------------------------------------ + +bool ClipperBase::AddPath(const Path &pg, PolyType PolyTyp, bool Closed) +{ +#ifdef use_lines + if (!Closed && PolyTyp == ptClip) + throw clipperException("AddPath: Open paths must be subject."); +#else + if (!Closed) + throw clipperException("AddPath: Open paths have been disabled."); +#endif + + int highI = (int)pg.size() -1; + if (Closed) while (highI > 0 && (pg[highI] == pg[0])) --highI; + while (highI > 0 && (pg[highI] == pg[highI -1])) --highI; + if ((Closed && highI < 2) || (!Closed && highI < 1)) return false; + + //create a new edge array ... + TEdge *edges = new TEdge [highI +1]; + + bool IsFlat = true; + //1. Basic (first) edge initialization ... + try + { + edges[1].Curr = pg[1]; + RangeTest(pg[0], m_UseFullRange); + RangeTest(pg[highI], m_UseFullRange); + InitEdge(&edges[0], &edges[1], &edges[highI], pg[0]); + InitEdge(&edges[highI], &edges[0], &edges[highI-1], pg[highI]); + for (int i = highI - 1; i >= 1; --i) + { + RangeTest(pg[i], m_UseFullRange); + InitEdge(&edges[i], &edges[i+1], &edges[i-1], pg[i]); + } + } + catch(...) + { + delete [] edges; + throw; //range test fails + } + TEdge *eStart = &edges[0]; + + //2. Remove duplicate vertices, and (when closed) collinear edges ... + TEdge *E = eStart, *eLoopStop = eStart; + for (;;) + { + //nb: allows matching start and end points when not Closed ... + if (E->Curr == E->Next->Curr && (Closed || E->Next != eStart)) + { + if (E == E->Next) break; + if (E == eStart) eStart = E->Next; + E = RemoveEdge(E); + eLoopStop = E; + continue; + } + if (E->Prev == E->Next) + break; //only two vertices + else if (Closed && + SlopesEqual(E->Prev->Curr, E->Curr, E->Next->Curr, m_UseFullRange) && + (!m_PreserveCollinear || + !Pt2IsBetweenPt1AndPt3(E->Prev->Curr, E->Curr, E->Next->Curr))) + { + //Collinear edges are allowed for open paths but in closed paths + //the default is to merge adjacent collinear edges into a single edge. + //However, if the PreserveCollinear property is enabled, only overlapping + //collinear edges (ie spikes) will be removed from closed paths. + if (E == eStart) eStart = E->Next; + E = RemoveEdge(E); + E = E->Prev; + eLoopStop = E; + continue; + } + E = E->Next; + if ((E == eLoopStop) || (!Closed && E->Next == eStart)) break; + } + + if ((!Closed && (E == E->Next)) || (Closed && (E->Prev == E->Next))) + { + delete [] edges; + return false; + } + + if (!Closed) + { + m_HasOpenPaths = true; + eStart->Prev->OutIdx = Skip; + } + + //3. Do second stage of edge initialization ... + E = eStart; + do + { + InitEdge2(*E, PolyTyp); + E = E->Next; + if (IsFlat && E->Curr.Y != eStart->Curr.Y) IsFlat = false; + } + while (E != eStart); + + //4. Finally, add edge bounds to LocalMinima list ... + + //Totally flat paths must be handled differently when adding them + //to LocalMinima list to avoid endless loops etc ... + if (IsFlat) + { + if (Closed) + { + delete [] edges; + return false; + } + E->Prev->OutIdx = Skip; + if (E->Prev->Bot.X < E->Prev->Top.X) ReverseHorizontal(*E->Prev); + MinimaList::value_type locMin; + locMin.Y = E->Bot.Y; + locMin.LeftBound = 0; + locMin.RightBound = E; + locMin.RightBound->Side = esRight; + locMin.RightBound->WindDelta = 0; + while (E->Next->OutIdx != Skip) + { + E->NextInLML = E->Next; + if (E->Bot.X != E->Prev->Top.X) ReverseHorizontal(*E); + E = E->Next; + } + m_MinimaList.push_back(locMin); + m_edges.push_back(edges); + return true; + } + + m_edges.push_back(edges); + bool leftBoundIsForward; + TEdge* EMin = 0; + + //workaround to avoid an endless loop in the while loop below when + //open paths have matching start and end points ... + if (E->Prev->Bot == E->Prev->Top) E = E->Next; + + for (;;) + { + E = FindNextLocMin(E); + if (E == EMin) break; + else if (!EMin) EMin = E; + + //E and E.Prev now share a local minima (left aligned if horizontal). + //Compare their slopes to find which starts which bound ... + MinimaList::value_type locMin; + locMin.Y = E->Bot.Y; + if (E->Dx < E->Prev->Dx) + { + locMin.LeftBound = E->Prev; + locMin.RightBound = E; + leftBoundIsForward = false; //Q.nextInLML = Q.prev + } else + { + locMin.LeftBound = E; + locMin.RightBound = E->Prev; + leftBoundIsForward = true; //Q.nextInLML = Q.next + } + locMin.LeftBound->Side = esLeft; + locMin.RightBound->Side = esRight; + + if (!Closed) locMin.LeftBound->WindDelta = 0; + else if (locMin.LeftBound->Next == locMin.RightBound) + locMin.LeftBound->WindDelta = -1; + else locMin.LeftBound->WindDelta = 1; + locMin.RightBound->WindDelta = -locMin.LeftBound->WindDelta; + + E = ProcessBound(locMin.LeftBound, leftBoundIsForward); + if (E->OutIdx == Skip) E = ProcessBound(E, leftBoundIsForward); + + TEdge* E2 = ProcessBound(locMin.RightBound, !leftBoundIsForward); + if (E2->OutIdx == Skip) E2 = ProcessBound(E2, !leftBoundIsForward); + + if (locMin.LeftBound->OutIdx == Skip) + locMin.LeftBound = 0; + else if (locMin.RightBound->OutIdx == Skip) + locMin.RightBound = 0; + m_MinimaList.push_back(locMin); + if (!leftBoundIsForward) E = E2; + } + return true; +} +//------------------------------------------------------------------------------ + +bool ClipperBase::AddPaths(const Paths &ppg, PolyType PolyTyp, bool Closed) +{ + bool result = false; + for (Paths::size_type i = 0; i < ppg.size(); ++i) + if (AddPath(ppg[i], PolyTyp, Closed)) result = true; + return result; +} +//------------------------------------------------------------------------------ + +void ClipperBase::Clear() +{ + DisposeLocalMinimaList(); + for (EdgeList::size_type i = 0; i < m_edges.size(); ++i) + { + //for each edge array in turn, find the first used edge and + //check for and remove any hiddenPts in each edge in the array. + TEdge* edges = m_edges[i]; + delete [] edges; + } + m_edges.clear(); + m_UseFullRange = false; + m_HasOpenPaths = false; +} +//------------------------------------------------------------------------------ + +void ClipperBase::Reset() +{ + m_CurrentLM = m_MinimaList.begin(); + if (m_CurrentLM == m_MinimaList.end()) return; //ie nothing to process + std::sort(m_MinimaList.begin(), m_MinimaList.end(), LocMinSorter()); + + //reset all edges ... + for (MinimaList::iterator lm = m_MinimaList.begin(); lm != m_MinimaList.end(); ++lm) + { + TEdge* e = lm->LeftBound; + if (e) + { + e->Curr = e->Bot; + e->Side = esLeft; + e->OutIdx = Unassigned; + } + + e = lm->RightBound; + if (e) + { + e->Curr = e->Bot; + e->Side = esRight; + e->OutIdx = Unassigned; + } + } +} +//------------------------------------------------------------------------------ + +void ClipperBase::DisposeLocalMinimaList() +{ + m_MinimaList.clear(); + m_CurrentLM = m_MinimaList.begin(); +} +//------------------------------------------------------------------------------ + +void ClipperBase::PopLocalMinima() +{ + if (m_CurrentLM == m_MinimaList.end()) return; + ++m_CurrentLM; +} +//------------------------------------------------------------------------------ + +IntRect ClipperBase::GetBounds() +{ + IntRect result; + MinimaList::iterator lm = m_MinimaList.begin(); + if (lm == m_MinimaList.end()) + { + result.left = result.top = result.right = result.bottom = 0; + return result; + } + result.left = lm->LeftBound->Bot.X; + result.top = lm->LeftBound->Bot.Y; + result.right = lm->LeftBound->Bot.X; + result.bottom = lm->LeftBound->Bot.Y; + while (lm != m_MinimaList.end()) + { + result.bottom = std::max(result.bottom, lm->LeftBound->Bot.Y); + TEdge* e = lm->LeftBound; + for (;;) { + TEdge* bottomE = e; + while (e->NextInLML) + { + if (e->Bot.X < result.left) result.left = e->Bot.X; + if (e->Bot.X > result.right) result.right = e->Bot.X; + e = e->NextInLML; + } + result.left = std::min(result.left, e->Bot.X); + result.right = std::max(result.right, e->Bot.X); + result.left = std::min(result.left, e->Top.X); + result.right = std::max(result.right, e->Top.X); + result.top = std::min(result.top, e->Top.Y); + if (bottomE == lm->LeftBound) e = lm->RightBound; + else break; + } + ++lm; + } + return result; +} + +//------------------------------------------------------------------------------ +// TClipper methods ... +//------------------------------------------------------------------------------ + +Clipper::Clipper(int initOptions) : ClipperBase() //constructor +{ + m_ActiveEdges = 0; + m_SortedEdges = 0; + m_ExecuteLocked = false; + m_UseFullRange = false; + m_ReverseOutput = ((initOptions & ioReverseSolution) != 0); + m_StrictSimple = ((initOptions & ioStrictlySimple) != 0); + m_PreserveCollinear = ((initOptions & ioPreserveCollinear) != 0); + m_HasOpenPaths = false; +#ifdef use_xyz + m_ZFill = 0; +#endif +} +//------------------------------------------------------------------------------ + +Clipper::~Clipper() //destructor +{ + Clear(); +} +//------------------------------------------------------------------------------ + +#ifdef use_xyz +void Clipper::ZFillFunction(ZFillCallback zFillFunc) +{ + m_ZFill = zFillFunc; +} +//------------------------------------------------------------------------------ +#endif + +void Clipper::Reset() +{ + ClipperBase::Reset(); + m_Scanbeam = ScanbeamList(); + m_ActiveEdges = 0; + m_SortedEdges = 0; + for (MinimaList::iterator lm = m_MinimaList.begin(); lm != m_MinimaList.end(); ++lm) + InsertScanbeam(lm->Y); +} +//------------------------------------------------------------------------------ + +bool Clipper::Execute(ClipType clipType, Paths &solution, + PolyFillType subjFillType, PolyFillType clipFillType) +{ + if( m_ExecuteLocked ) return false; + if (m_HasOpenPaths) + throw clipperException("Error: PolyTree struct is need for open path clipping."); + m_ExecuteLocked = true; + solution.resize(0); + m_SubjFillType = subjFillType; + m_ClipFillType = clipFillType; + m_ClipType = clipType; + m_UsingPolyTree = false; + bool succeeded = ExecuteInternal(); + if (succeeded) BuildResult(solution); + DisposeAllOutRecs(); + m_ExecuteLocked = false; + return succeeded; +} +//------------------------------------------------------------------------------ + +bool Clipper::Execute(ClipType clipType, PolyTree& polytree, + PolyFillType subjFillType, PolyFillType clipFillType) +{ + if( m_ExecuteLocked ) return false; + m_ExecuteLocked = true; + m_SubjFillType = subjFillType; + m_ClipFillType = clipFillType; + m_ClipType = clipType; + m_UsingPolyTree = true; + bool succeeded = ExecuteInternal(); + if (succeeded) BuildResult2(polytree); + DisposeAllOutRecs(); + m_ExecuteLocked = false; + return succeeded; +} +//------------------------------------------------------------------------------ + +void Clipper::FixHoleLinkage(OutRec &outrec) +{ + //skip OutRecs that (a) contain outermost polygons or + //(b) already have the correct owner/child linkage ... + if (!outrec.FirstLeft || + (outrec.IsHole != outrec.FirstLeft->IsHole && + outrec.FirstLeft->Pts)) return; + + OutRec* orfl = outrec.FirstLeft; + while (orfl && ((orfl->IsHole == outrec.IsHole) || !orfl->Pts)) + orfl = orfl->FirstLeft; + outrec.FirstLeft = orfl; +} +//------------------------------------------------------------------------------ + +bool Clipper::ExecuteInternal() +{ + bool succeeded = true; + try { + Reset(); + if (m_CurrentLM == m_MinimaList.end()) return true; + cInt botY = PopScanbeam(); + do { + InsertLocalMinimaIntoAEL(botY); + ClearGhostJoins(); + ProcessHorizontals(false); + if (m_Scanbeam.empty()) break; + cInt topY = PopScanbeam(); + succeeded = ProcessIntersections(botY, topY); + if (!succeeded) break; + ProcessEdgesAtTopOfScanbeam(topY); + botY = topY; + } while (!m_Scanbeam.empty() || m_CurrentLM != m_MinimaList.end()); + } + catch(...) + { + succeeded = false; + } + + if (succeeded) + { + //fix orientations ... + for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); ++i) + { + OutRec *outRec = m_PolyOuts[i]; + if (!outRec->Pts || outRec->IsOpen) continue; + if ((outRec->IsHole ^ m_ReverseOutput) == (Area(*outRec) > 0)) + ReversePolyPtLinks(outRec->Pts); + } + + if (!m_Joins.empty()) JoinCommonEdges(); + + //unfortunately FixupOutPolygon() must be done after JoinCommonEdges() + for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); ++i) + { + OutRec *outRec = m_PolyOuts[i]; + if (outRec->Pts && !outRec->IsOpen) + FixupOutPolygon(*outRec); + } + + if (m_StrictSimple) DoSimplePolygons(); + } + + ClearJoins(); + ClearGhostJoins(); + return succeeded; +} +//------------------------------------------------------------------------------ + +void Clipper::InsertScanbeam(const cInt Y) +{ + //if (!m_Scanbeam.empty() && Y == m_Scanbeam.top()) return;// avoid duplicates. + m_Scanbeam.push(Y); +} +//------------------------------------------------------------------------------ + +cInt Clipper::PopScanbeam() +{ + const cInt Y = m_Scanbeam.top(); + m_Scanbeam.pop(); + while (!m_Scanbeam.empty() && Y == m_Scanbeam.top()) { m_Scanbeam.pop(); } // Pop duplicates. + return Y; +} +//------------------------------------------------------------------------------ + +void Clipper::DisposeAllOutRecs(){ + for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); ++i) + DisposeOutRec(i); + m_PolyOuts.clear(); +} +//------------------------------------------------------------------------------ + +void Clipper::DisposeOutRec(PolyOutList::size_type index) +{ + OutRec *outRec = m_PolyOuts[index]; + if (outRec->Pts) DisposeOutPts(outRec->Pts); + delete outRec; + m_PolyOuts[index] = 0; +} +//------------------------------------------------------------------------------ + +void Clipper::SetWindingCount(TEdge &edge) +{ + TEdge *e = edge.PrevInAEL; + //find the edge of the same polytype that immediately preceeds 'edge' in AEL + while (e && ((e->PolyTyp != edge.PolyTyp) || (e->WindDelta == 0))) e = e->PrevInAEL; + if (!e) + { + edge.WindCnt = (edge.WindDelta == 0 ? 1 : edge.WindDelta); + edge.WindCnt2 = 0; + e = m_ActiveEdges; //ie get ready to calc WindCnt2 + } + else if (edge.WindDelta == 0 && m_ClipType != ctUnion) + { + edge.WindCnt = 1; + edge.WindCnt2 = e->WindCnt2; + e = e->NextInAEL; //ie get ready to calc WindCnt2 + } + else if (IsEvenOddFillType(edge)) + { + //EvenOdd filling ... + if (edge.WindDelta == 0) + { + //are we inside a subj polygon ... + bool Inside = true; + TEdge *e2 = e->PrevInAEL; + while (e2) + { + if (e2->PolyTyp == e->PolyTyp && e2->WindDelta != 0) + Inside = !Inside; + e2 = e2->PrevInAEL; + } + edge.WindCnt = (Inside ? 0 : 1); + } + else + { + edge.WindCnt = edge.WindDelta; + } + edge.WindCnt2 = e->WindCnt2; + e = e->NextInAEL; //ie get ready to calc WindCnt2 + } + else + { + //nonZero, Positive or Negative filling ... + if (e->WindCnt * e->WindDelta < 0) + { + //prev edge is 'decreasing' WindCount (WC) toward zero + //so we're outside the previous polygon ... + if (Abs(e->WindCnt) > 1) + { + //outside prev poly but still inside another. + //when reversing direction of prev poly use the same WC + if (e->WindDelta * edge.WindDelta < 0) edge.WindCnt = e->WindCnt; + //otherwise continue to 'decrease' WC ... + else edge.WindCnt = e->WindCnt + edge.WindDelta; + } + else + //now outside all polys of same polytype so set own WC ... + edge.WindCnt = (edge.WindDelta == 0 ? 1 : edge.WindDelta); + } else + { + //prev edge is 'increasing' WindCount (WC) away from zero + //so we're inside the previous polygon ... + if (edge.WindDelta == 0) + edge.WindCnt = (e->WindCnt < 0 ? e->WindCnt - 1 : e->WindCnt + 1); + //if wind direction is reversing prev then use same WC + else if (e->WindDelta * edge.WindDelta < 0) edge.WindCnt = e->WindCnt; + //otherwise add to WC ... + else edge.WindCnt = e->WindCnt + edge.WindDelta; + } + edge.WindCnt2 = e->WindCnt2; + e = e->NextInAEL; //ie get ready to calc WindCnt2 + } + + //update WindCnt2 ... + if (IsEvenOddAltFillType(edge)) + { + //EvenOdd filling ... + while (e != &edge) + { + if (e->WindDelta != 0) + edge.WindCnt2 = (edge.WindCnt2 == 0 ? 1 : 0); + e = e->NextInAEL; + } + } else + { + //nonZero, Positive or Negative filling ... + while ( e != &edge ) + { + edge.WindCnt2 += e->WindDelta; + e = e->NextInAEL; + } + } +} +//------------------------------------------------------------------------------ + +bool Clipper::IsEvenOddFillType(const TEdge& edge) const +{ + if (edge.PolyTyp == ptSubject) + return m_SubjFillType == pftEvenOdd; else + return m_ClipFillType == pftEvenOdd; +} +//------------------------------------------------------------------------------ + +bool Clipper::IsEvenOddAltFillType(const TEdge& edge) const +{ + if (edge.PolyTyp == ptSubject) + return m_ClipFillType == pftEvenOdd; else + return m_SubjFillType == pftEvenOdd; +} +//------------------------------------------------------------------------------ + +bool Clipper::IsContributing(const TEdge& edge) const +{ + PolyFillType pft, pft2; + if (edge.PolyTyp == ptSubject) + { + pft = m_SubjFillType; + pft2 = m_ClipFillType; + } else + { + pft = m_ClipFillType; + pft2 = m_SubjFillType; + } + + switch(pft) + { + case pftEvenOdd: + //return false if a subj line has been flagged as inside a subj polygon + if (edge.WindDelta == 0 && edge.WindCnt != 1) return false; + break; + case pftNonZero: + if (Abs(edge.WindCnt) != 1) return false; + break; + case pftPositive: + if (edge.WindCnt != 1) return false; + break; + default: //pftNegative + if (edge.WindCnt != -1) return false; + } + + switch(m_ClipType) + { + case ctIntersection: + switch(pft2) + { + case pftEvenOdd: + case pftNonZero: + return (edge.WindCnt2 != 0); + case pftPositive: + return (edge.WindCnt2 > 0); + default: + return (edge.WindCnt2 < 0); + } + break; + case ctUnion: + switch(pft2) + { + case pftEvenOdd: + case pftNonZero: + return (edge.WindCnt2 == 0); + case pftPositive: + return (edge.WindCnt2 <= 0); + default: + return (edge.WindCnt2 >= 0); + } + break; + case ctDifference: + if (edge.PolyTyp == ptSubject) + switch(pft2) + { + case pftEvenOdd: + case pftNonZero: + return (edge.WindCnt2 == 0); + case pftPositive: + return (edge.WindCnt2 <= 0); + default: + return (edge.WindCnt2 >= 0); + } + else + switch(pft2) + { + case pftEvenOdd: + case pftNonZero: + return (edge.WindCnt2 != 0); + case pftPositive: + return (edge.WindCnt2 > 0); + default: + return (edge.WindCnt2 < 0); + } + break; + case ctXor: + if (edge.WindDelta == 0) //XOr always contributing unless open + switch(pft2) + { + case pftEvenOdd: + case pftNonZero: + return (edge.WindCnt2 == 0); + case pftPositive: + return (edge.WindCnt2 <= 0); + default: + return (edge.WindCnt2 >= 0); + } + else + return true; + break; + default: + return true; + } +} +//------------------------------------------------------------------------------ + +OutPt* Clipper::AddLocalMinPoly(TEdge *e1, TEdge *e2, const IntPoint &Pt) +{ + OutPt* result; + TEdge *e, *prevE; + if (IsHorizontal(*e2) || ( e1->Dx > e2->Dx )) + { + result = AddOutPt(e1, Pt); + e2->OutIdx = e1->OutIdx; + e1->Side = esLeft; + e2->Side = esRight; + e = e1; + if (e->PrevInAEL == e2) + prevE = e2->PrevInAEL; + else + prevE = e->PrevInAEL; + } else + { + result = AddOutPt(e2, Pt); + e1->OutIdx = e2->OutIdx; + e1->Side = esRight; + e2->Side = esLeft; + e = e2; + if (e->PrevInAEL == e1) + prevE = e1->PrevInAEL; + else + prevE = e->PrevInAEL; + } + + if (prevE && prevE->OutIdx >= 0 && + (TopX(*prevE, Pt.Y) == TopX(*e, Pt.Y)) && + SlopesEqual(*e, *prevE, m_UseFullRange) && + (e->WindDelta != 0) && (prevE->WindDelta != 0)) + { + OutPt* outPt = AddOutPt(prevE, Pt); + AddJoin(result, outPt, e->Top); + } + return result; +} +//------------------------------------------------------------------------------ + +void Clipper::AddLocalMaxPoly(TEdge *e1, TEdge *e2, const IntPoint &Pt) +{ + AddOutPt( e1, Pt ); + if (e2->WindDelta == 0) AddOutPt(e2, Pt); + if( e1->OutIdx == e2->OutIdx ) + { + e1->OutIdx = Unassigned; + e2->OutIdx = Unassigned; + } + else if (e1->OutIdx < e2->OutIdx) + AppendPolygon(e1, e2); + else + AppendPolygon(e2, e1); +} +//------------------------------------------------------------------------------ + +void Clipper::AddEdgeToSEL(TEdge *edge) +{ + //SEL pointers in PEdge are reused to build a list of horizontal edges. + //However, we don't need to worry about order with horizontal edge processing. + if( !m_SortedEdges ) + { + m_SortedEdges = edge; + edge->PrevInSEL = 0; + edge->NextInSEL = 0; + } + else + { + edge->NextInSEL = m_SortedEdges; + edge->PrevInSEL = 0; + m_SortedEdges->PrevInSEL = edge; + m_SortedEdges = edge; + } +} +//------------------------------------------------------------------------------ + +void Clipper::CopyAELToSEL() +{ + TEdge* e = m_ActiveEdges; + m_SortedEdges = e; + while ( e ) + { + e->PrevInSEL = e->PrevInAEL; + e->NextInSEL = e->NextInAEL; + e = e->NextInAEL; + } +} +//------------------------------------------------------------------------------ + +void Clipper::AddJoin(OutPt *op1, OutPt *op2, const IntPoint OffPt) +{ + Join* j = new Join; + j->OutPt1 = op1; + j->OutPt2 = op2; + j->OffPt = OffPt; + m_Joins.push_back(j); +} +//------------------------------------------------------------------------------ + +void Clipper::ClearJoins() +{ + for (JoinList::size_type i = 0; i < m_Joins.size(); i++) + delete m_Joins[i]; + m_Joins.resize(0); +} +//------------------------------------------------------------------------------ + +void Clipper::ClearGhostJoins() +{ + for (JoinList::size_type i = 0; i < m_GhostJoins.size(); i++) + delete m_GhostJoins[i]; + m_GhostJoins.resize(0); +} +//------------------------------------------------------------------------------ + +void Clipper::AddGhostJoin(OutPt *op, const IntPoint OffPt) +{ + Join* j = new Join; + j->OutPt1 = op; + j->OutPt2 = 0; + j->OffPt = OffPt; + m_GhostJoins.push_back(j); +} +//------------------------------------------------------------------------------ + +void Clipper::InsertLocalMinimaIntoAEL(const cInt botY) +{ + while (m_CurrentLM != m_MinimaList.end() && (m_CurrentLM->Y == botY)) + { + TEdge* lb = m_CurrentLM->LeftBound; + TEdge* rb = m_CurrentLM->RightBound; + PopLocalMinima(); + OutPt *Op1 = 0; + if (!lb) + { + //nb: don't insert LB into either AEL or SEL + InsertEdgeIntoAEL(rb, 0); + SetWindingCount(*rb); + if (IsContributing(*rb)) + Op1 = AddOutPt(rb, rb->Bot); + } + else if (!rb) + { + InsertEdgeIntoAEL(lb, 0); + SetWindingCount(*lb); + if (IsContributing(*lb)) + Op1 = AddOutPt(lb, lb->Bot); + InsertScanbeam(lb->Top.Y); + } + else + { + InsertEdgeIntoAEL(lb, 0); + InsertEdgeIntoAEL(rb, lb); + SetWindingCount( *lb ); + rb->WindCnt = lb->WindCnt; + rb->WindCnt2 = lb->WindCnt2; + if (IsContributing(*lb)) + Op1 = AddLocalMinPoly(lb, rb, lb->Bot); + InsertScanbeam(lb->Top.Y); + } + + if (rb) + { + if(IsHorizontal(*rb)) AddEdgeToSEL(rb); + else InsertScanbeam( rb->Top.Y ); + } + + if (!lb || !rb) continue; + + //if any output polygons share an edge, they'll need joining later ... + if (Op1 && IsHorizontal(*rb) && + m_GhostJoins.size() > 0 && (rb->WindDelta != 0)) + { + for (JoinList::size_type i = 0; i < m_GhostJoins.size(); ++i) + { + Join* jr = m_GhostJoins[i]; + //if the horizontal Rb and a 'ghost' horizontal overlap, then convert + //the 'ghost' join to a real join ready for later ... + if (HorzSegmentsOverlap(jr->OutPt1->Pt.X, jr->OffPt.X, rb->Bot.X, rb->Top.X)) + AddJoin(jr->OutPt1, Op1, jr->OffPt); + } + } + + if (lb->OutIdx >= 0 && lb->PrevInAEL && + lb->PrevInAEL->Curr.X == lb->Bot.X && + lb->PrevInAEL->OutIdx >= 0 && + SlopesEqual(*lb->PrevInAEL, *lb, m_UseFullRange) && + (lb->WindDelta != 0) && (lb->PrevInAEL->WindDelta != 0)) + { + OutPt *Op2 = AddOutPt(lb->PrevInAEL, lb->Bot); + AddJoin(Op1, Op2, lb->Top); + } + + if(lb->NextInAEL != rb) + { + + if (rb->OutIdx >= 0 && rb->PrevInAEL->OutIdx >= 0 && + SlopesEqual(*rb->PrevInAEL, *rb, m_UseFullRange) && + (rb->WindDelta != 0) && (rb->PrevInAEL->WindDelta != 0)) + { + OutPt *Op2 = AddOutPt(rb->PrevInAEL, rb->Bot); + AddJoin(Op1, Op2, rb->Top); + } + + TEdge* e = lb->NextInAEL; + if (e) + { + while( e != rb ) + { + //nb: For calculating winding counts etc, IntersectEdges() assumes + //that param1 will be to the Right of param2 ABOVE the intersection ... + IntersectEdges(rb , e , lb->Curr); //order important here + e = e->NextInAEL; + } + } + } + + } +} +//------------------------------------------------------------------------------ + +void Clipper::DeleteFromAEL(TEdge *e) +{ + TEdge* AelPrev = e->PrevInAEL; + TEdge* AelNext = e->NextInAEL; + if( !AelPrev && !AelNext && (e != m_ActiveEdges) ) return; //already deleted + if( AelPrev ) AelPrev->NextInAEL = AelNext; + else m_ActiveEdges = AelNext; + if( AelNext ) AelNext->PrevInAEL = AelPrev; + e->NextInAEL = 0; + e->PrevInAEL = 0; +} +//------------------------------------------------------------------------------ + +void Clipper::DeleteFromSEL(TEdge *e) +{ + TEdge* SelPrev = e->PrevInSEL; + TEdge* SelNext = e->NextInSEL; + if( !SelPrev && !SelNext && (e != m_SortedEdges) ) return; //already deleted + if( SelPrev ) SelPrev->NextInSEL = SelNext; + else m_SortedEdges = SelNext; + if( SelNext ) SelNext->PrevInSEL = SelPrev; + e->NextInSEL = 0; + e->PrevInSEL = 0; +} +//------------------------------------------------------------------------------ + +#ifdef use_xyz +void Clipper::SetZ(IntPoint& pt, TEdge& e1, TEdge& e2) +{ + if (pt.Z != 0 || !m_ZFill) return; + else if (pt == e1.Bot) pt.Z = e1.Bot.Z; + else if (pt == e1.Top) pt.Z = e1.Top.Z; + else if (pt == e2.Bot) pt.Z = e2.Bot.Z; + else if (pt == e2.Top) pt.Z = e2.Top.Z; + else (*m_ZFill)(e1.Bot, e1.Top, e2.Bot, e2.Top, pt); +} +//------------------------------------------------------------------------------ +#endif + +void Clipper::IntersectEdges(TEdge *e1, TEdge *e2, IntPoint &Pt) +{ + bool e1Contributing = ( e1->OutIdx >= 0 ); + bool e2Contributing = ( e2->OutIdx >= 0 ); + +#ifdef use_xyz + SetZ(Pt, *e1, *e2); +#endif + +#ifdef use_lines + //if either edge is on an OPEN path ... + if (e1->WindDelta == 0 || e2->WindDelta == 0) + { + //ignore subject-subject open path intersections UNLESS they + //are both open paths, AND they are both 'contributing maximas' ... + if (e1->WindDelta == 0 && e2->WindDelta == 0) return; + + //if intersecting a subj line with a subj poly ... + else if (e1->PolyTyp == e2->PolyTyp && + e1->WindDelta != e2->WindDelta && m_ClipType == ctUnion) + { + if (e1->WindDelta == 0) + { + if (e2Contributing) + { + AddOutPt(e1, Pt); + if (e1Contributing) e1->OutIdx = Unassigned; + } + } + else + { + if (e1Contributing) + { + AddOutPt(e2, Pt); + if (e2Contributing) e2->OutIdx = Unassigned; + } + } + } + else if (e1->PolyTyp != e2->PolyTyp) + { + //toggle subj open path OutIdx on/off when Abs(clip.WndCnt) == 1 ... + if ((e1->WindDelta == 0) && abs(e2->WindCnt) == 1 && + (m_ClipType != ctUnion || e2->WindCnt2 == 0)) + { + AddOutPt(e1, Pt); + if (e1Contributing) e1->OutIdx = Unassigned; + } + else if ((e2->WindDelta == 0) && (abs(e1->WindCnt) == 1) && + (m_ClipType != ctUnion || e1->WindCnt2 == 0)) + { + AddOutPt(e2, Pt); + if (e2Contributing) e2->OutIdx = Unassigned; + } + } + return; + } +#endif + + //update winding counts... + //assumes that e1 will be to the Right of e2 ABOVE the intersection + if ( e1->PolyTyp == e2->PolyTyp ) + { + if ( IsEvenOddFillType( *e1) ) + { + int oldE1WindCnt = e1->WindCnt; + e1->WindCnt = e2->WindCnt; + e2->WindCnt = oldE1WindCnt; + } else + { + if (e1->WindCnt + e2->WindDelta == 0 ) e1->WindCnt = -e1->WindCnt; + else e1->WindCnt += e2->WindDelta; + if ( e2->WindCnt - e1->WindDelta == 0 ) e2->WindCnt = -e2->WindCnt; + else e2->WindCnt -= e1->WindDelta; + } + } else + { + if (!IsEvenOddFillType(*e2)) e1->WindCnt2 += e2->WindDelta; + else e1->WindCnt2 = ( e1->WindCnt2 == 0 ) ? 1 : 0; + if (!IsEvenOddFillType(*e1)) e2->WindCnt2 -= e1->WindDelta; + else e2->WindCnt2 = ( e2->WindCnt2 == 0 ) ? 1 : 0; + } + + PolyFillType e1FillType, e2FillType, e1FillType2, e2FillType2; + if (e1->PolyTyp == ptSubject) + { + e1FillType = m_SubjFillType; + e1FillType2 = m_ClipFillType; + } else + { + e1FillType = m_ClipFillType; + e1FillType2 = m_SubjFillType; + } + if (e2->PolyTyp == ptSubject) + { + e2FillType = m_SubjFillType; + e2FillType2 = m_ClipFillType; + } else + { + e2FillType = m_ClipFillType; + e2FillType2 = m_SubjFillType; + } + + cInt e1Wc, e2Wc; + switch (e1FillType) + { + case pftPositive: e1Wc = e1->WindCnt; break; + case pftNegative: e1Wc = -e1->WindCnt; break; + default: e1Wc = Abs(e1->WindCnt); + } + switch(e2FillType) + { + case pftPositive: e2Wc = e2->WindCnt; break; + case pftNegative: e2Wc = -e2->WindCnt; break; + default: e2Wc = Abs(e2->WindCnt); + } + + if ( e1Contributing && e2Contributing ) + { + if ((e1Wc != 0 && e1Wc != 1) || (e2Wc != 0 && e2Wc != 1) || + (e1->PolyTyp != e2->PolyTyp && m_ClipType != ctXor) ) + { + AddLocalMaxPoly(e1, e2, Pt); + } + else + { + AddOutPt(e1, Pt); + AddOutPt(e2, Pt); + SwapSides( *e1 , *e2 ); + SwapPolyIndexes( *e1 , *e2 ); + } + } + else if ( e1Contributing ) + { + if (e2Wc == 0 || e2Wc == 1) + { + AddOutPt(e1, Pt); + SwapSides(*e1, *e2); + SwapPolyIndexes(*e1, *e2); + } + } + else if ( e2Contributing ) + { + if (e1Wc == 0 || e1Wc == 1) + { + AddOutPt(e2, Pt); + SwapSides(*e1, *e2); + SwapPolyIndexes(*e1, *e2); + } + } + else if ( (e1Wc == 0 || e1Wc == 1) && (e2Wc == 0 || e2Wc == 1)) + { + //neither edge is currently contributing ... + + cInt e1Wc2, e2Wc2; + switch (e1FillType2) + { + case pftPositive: e1Wc2 = e1->WindCnt2; break; + case pftNegative : e1Wc2 = -e1->WindCnt2; break; + default: e1Wc2 = Abs(e1->WindCnt2); + } + switch (e2FillType2) + { + case pftPositive: e2Wc2 = e2->WindCnt2; break; + case pftNegative: e2Wc2 = -e2->WindCnt2; break; + default: e2Wc2 = Abs(e2->WindCnt2); + } + + if (e1->PolyTyp != e2->PolyTyp) + { + AddLocalMinPoly(e1, e2, Pt); + } + else if (e1Wc == 1 && e2Wc == 1) + switch( m_ClipType ) { + case ctIntersection: + if (e1Wc2 > 0 && e2Wc2 > 0) + AddLocalMinPoly(e1, e2, Pt); + break; + case ctUnion: + if ( e1Wc2 <= 0 && e2Wc2 <= 0 ) + AddLocalMinPoly(e1, e2, Pt); + break; + case ctDifference: + if (((e1->PolyTyp == ptClip) && (e1Wc2 > 0) && (e2Wc2 > 0)) || + ((e1->PolyTyp == ptSubject) && (e1Wc2 <= 0) && (e2Wc2 <= 0))) + AddLocalMinPoly(e1, e2, Pt); + break; + case ctXor: + AddLocalMinPoly(e1, e2, Pt); + } + else + SwapSides( *e1, *e2 ); + } +} +//------------------------------------------------------------------------------ + +void Clipper::SetHoleState(TEdge *e, OutRec *outrec) +{ + bool IsHole = false; + TEdge *e2 = e->PrevInAEL; + while (e2) + { + if (e2->OutIdx >= 0 && e2->WindDelta != 0) + { + IsHole = !IsHole; + if (! outrec->FirstLeft) + outrec->FirstLeft = m_PolyOuts[e2->OutIdx]; + } + e2 = e2->PrevInAEL; + } + if (IsHole) outrec->IsHole = true; +} +//------------------------------------------------------------------------------ + +OutRec* GetLowermostRec(OutRec *outRec1, OutRec *outRec2) +{ + //work out which polygon fragment has the correct hole state ... + if (!outRec1->BottomPt) + outRec1->BottomPt = GetBottomPt(outRec1->Pts); + if (!outRec2->BottomPt) + outRec2->BottomPt = GetBottomPt(outRec2->Pts); + OutPt *OutPt1 = outRec1->BottomPt; + OutPt *OutPt2 = outRec2->BottomPt; + if (OutPt1->Pt.Y > OutPt2->Pt.Y) return outRec1; + else if (OutPt1->Pt.Y < OutPt2->Pt.Y) return outRec2; + else if (OutPt1->Pt.X < OutPt2->Pt.X) return outRec1; + else if (OutPt1->Pt.X > OutPt2->Pt.X) return outRec2; + else if (OutPt1->Next == OutPt1) return outRec2; + else if (OutPt2->Next == OutPt2) return outRec1; + else if (FirstIsBottomPt(OutPt1, OutPt2)) return outRec1; + else return outRec2; +} +//------------------------------------------------------------------------------ + +bool Param1RightOfParam2(OutRec* outRec1, OutRec* outRec2) +{ + do + { + outRec1 = outRec1->FirstLeft; + if (outRec1 == outRec2) return true; + } while (outRec1); + return false; +} +//------------------------------------------------------------------------------ + +OutRec* Clipper::GetOutRec(int Idx) +{ + OutRec* outrec = m_PolyOuts[Idx]; + while (outrec != m_PolyOuts[outrec->Idx]) + outrec = m_PolyOuts[outrec->Idx]; + return outrec; +} +//------------------------------------------------------------------------------ + +void Clipper::AppendPolygon(TEdge *e1, TEdge *e2) +{ + //get the start and ends of both output polygons ... + OutRec *outRec1 = m_PolyOuts[e1->OutIdx]; + OutRec *outRec2 = m_PolyOuts[e2->OutIdx]; + + OutRec *holeStateRec; + if (Param1RightOfParam2(outRec1, outRec2)) + holeStateRec = outRec2; + else if (Param1RightOfParam2(outRec2, outRec1)) + holeStateRec = outRec1; + else + holeStateRec = GetLowermostRec(outRec1, outRec2); + + //get the start and ends of both output polygons and + //join e2 poly onto e1 poly and delete pointers to e2 ... + + OutPt* p1_lft = outRec1->Pts; + OutPt* p1_rt = p1_lft->Prev; + OutPt* p2_lft = outRec2->Pts; + OutPt* p2_rt = p2_lft->Prev; + + EdgeSide Side; + //join e2 poly onto e1 poly and delete pointers to e2 ... + if( e1->Side == esLeft ) + { + if( e2->Side == esLeft ) + { + //z y x a b c + ReversePolyPtLinks(p2_lft); + p2_lft->Next = p1_lft; + p1_lft->Prev = p2_lft; + p1_rt->Next = p2_rt; + p2_rt->Prev = p1_rt; + outRec1->Pts = p2_rt; + } else + { + //x y z a b c + p2_rt->Next = p1_lft; + p1_lft->Prev = p2_rt; + p2_lft->Prev = p1_rt; + p1_rt->Next = p2_lft; + outRec1->Pts = p2_lft; + } + Side = esLeft; + } else + { + if( e2->Side == esRight ) + { + //a b c z y x + ReversePolyPtLinks(p2_lft); + p1_rt->Next = p2_rt; + p2_rt->Prev = p1_rt; + p2_lft->Next = p1_lft; + p1_lft->Prev = p2_lft; + } else + { + //a b c x y z + p1_rt->Next = p2_lft; + p2_lft->Prev = p1_rt; + p1_lft->Prev = p2_rt; + p2_rt->Next = p1_lft; + } + Side = esRight; + } + + outRec1->BottomPt = 0; + if (holeStateRec == outRec2) + { + if (outRec2->FirstLeft != outRec1) + outRec1->FirstLeft = outRec2->FirstLeft; + outRec1->IsHole = outRec2->IsHole; + } + outRec2->Pts = 0; + outRec2->BottomPt = 0; + outRec2->FirstLeft = outRec1; + + int OKIdx = e1->OutIdx; + int ObsoleteIdx = e2->OutIdx; + + e1->OutIdx = Unassigned; //nb: safe because we only get here via AddLocalMaxPoly + e2->OutIdx = Unassigned; + + TEdge* e = m_ActiveEdges; + while( e ) + { + if( e->OutIdx == ObsoleteIdx ) + { + e->OutIdx = OKIdx; + e->Side = Side; + break; + } + e = e->NextInAEL; + } + + outRec2->Idx = outRec1->Idx; +} +//------------------------------------------------------------------------------ + +OutRec* Clipper::CreateOutRec() +{ + OutRec* result = new OutRec; + result->IsHole = false; + result->IsOpen = false; + result->FirstLeft = 0; + result->Pts = 0; + result->BottomPt = 0; + result->PolyNd = 0; + m_PolyOuts.push_back(result); + result->Idx = (int)m_PolyOuts.size()-1; + return result; +} +//------------------------------------------------------------------------------ + +OutPt* Clipper::AddOutPt(TEdge *e, const IntPoint &pt) +{ + bool ToFront = (e->Side == esLeft); + if( e->OutIdx < 0 ) + { + OutRec *outRec = CreateOutRec(); + outRec->IsOpen = (e->WindDelta == 0); + OutPt* newOp = new OutPt; + outRec->Pts = newOp; + newOp->Idx = outRec->Idx; + newOp->Pt = pt; + newOp->Next = newOp; + newOp->Prev = newOp; + if (!outRec->IsOpen) + SetHoleState(e, outRec); + e->OutIdx = outRec->Idx; + return newOp; + } else + { + OutRec *outRec = m_PolyOuts[e->OutIdx]; + //OutRec.Pts is the 'Left-most' point & OutRec.Pts.Prev is the 'Right-most' + OutPt* op = outRec->Pts; + + if (ToFront && (pt == op->Pt)) return op; + else if (!ToFront && (pt == op->Prev->Pt)) return op->Prev; + + OutPt* newOp = new OutPt; + newOp->Idx = outRec->Idx; + newOp->Pt = pt; + newOp->Next = op; + newOp->Prev = op->Prev; + newOp->Prev->Next = newOp; + op->Prev = newOp; + if (ToFront) outRec->Pts = newOp; + return newOp; + } +} +//------------------------------------------------------------------------------ + +void Clipper::ProcessHorizontals(bool IsTopOfScanbeam) +{ + TEdge* horzEdge = m_SortedEdges; + while(horzEdge) + { + DeleteFromSEL(horzEdge); + ProcessHorizontal(horzEdge, IsTopOfScanbeam); + horzEdge = m_SortedEdges; + } +} +//------------------------------------------------------------------------------ + +inline bool IsMinima(TEdge *e) +{ + return e && (e->Prev->NextInLML != e) && (e->Next->NextInLML != e); +} +//------------------------------------------------------------------------------ + +inline bool IsMaxima(TEdge *e, const cInt Y) +{ + return e && e->Top.Y == Y && !e->NextInLML; +} +//------------------------------------------------------------------------------ + +inline bool IsIntermediate(TEdge *e, const cInt Y) +{ + return e->Top.Y == Y && e->NextInLML; +} +//------------------------------------------------------------------------------ + +TEdge *GetMaximaPair(TEdge *e) +{ + TEdge* result = 0; + if ((e->Next->Top == e->Top) && !e->Next->NextInLML) + result = e->Next; + else if ((e->Prev->Top == e->Top) && !e->Prev->NextInLML) + result = e->Prev; + + if (result && (result->OutIdx == Skip || + //result is false if both NextInAEL & PrevInAEL are nil & not horizontal ... + (result->NextInAEL == result->PrevInAEL && !IsHorizontal(*result)))) + return 0; + return result; +} +//------------------------------------------------------------------------------ + +void Clipper::SwapPositionsInAEL(TEdge *Edge1, TEdge *Edge2) +{ + //check that one or other edge hasn't already been removed from AEL ... + if (Edge1->NextInAEL == Edge1->PrevInAEL || + Edge2->NextInAEL == Edge2->PrevInAEL) return; + + if( Edge1->NextInAEL == Edge2 ) + { + TEdge* Next = Edge2->NextInAEL; + if( Next ) Next->PrevInAEL = Edge1; + TEdge* Prev = Edge1->PrevInAEL; + if( Prev ) Prev->NextInAEL = Edge2; + Edge2->PrevInAEL = Prev; + Edge2->NextInAEL = Edge1; + Edge1->PrevInAEL = Edge2; + Edge1->NextInAEL = Next; + } + else if( Edge2->NextInAEL == Edge1 ) + { + TEdge* Next = Edge1->NextInAEL; + if( Next ) Next->PrevInAEL = Edge2; + TEdge* Prev = Edge2->PrevInAEL; + if( Prev ) Prev->NextInAEL = Edge1; + Edge1->PrevInAEL = Prev; + Edge1->NextInAEL = Edge2; + Edge2->PrevInAEL = Edge1; + Edge2->NextInAEL = Next; + } + else + { + TEdge* Next = Edge1->NextInAEL; + TEdge* Prev = Edge1->PrevInAEL; + Edge1->NextInAEL = Edge2->NextInAEL; + if( Edge1->NextInAEL ) Edge1->NextInAEL->PrevInAEL = Edge1; + Edge1->PrevInAEL = Edge2->PrevInAEL; + if( Edge1->PrevInAEL ) Edge1->PrevInAEL->NextInAEL = Edge1; + Edge2->NextInAEL = Next; + if( Edge2->NextInAEL ) Edge2->NextInAEL->PrevInAEL = Edge2; + Edge2->PrevInAEL = Prev; + if( Edge2->PrevInAEL ) Edge2->PrevInAEL->NextInAEL = Edge2; + } + + if( !Edge1->PrevInAEL ) m_ActiveEdges = Edge1; + else if( !Edge2->PrevInAEL ) m_ActiveEdges = Edge2; +} +//------------------------------------------------------------------------------ + +void Clipper::SwapPositionsInSEL(TEdge *Edge1, TEdge *Edge2) +{ + if( !( Edge1->NextInSEL ) && !( Edge1->PrevInSEL ) ) return; + if( !( Edge2->NextInSEL ) && !( Edge2->PrevInSEL ) ) return; + + if( Edge1->NextInSEL == Edge2 ) + { + TEdge* Next = Edge2->NextInSEL; + if( Next ) Next->PrevInSEL = Edge1; + TEdge* Prev = Edge1->PrevInSEL; + if( Prev ) Prev->NextInSEL = Edge2; + Edge2->PrevInSEL = Prev; + Edge2->NextInSEL = Edge1; + Edge1->PrevInSEL = Edge2; + Edge1->NextInSEL = Next; + } + else if( Edge2->NextInSEL == Edge1 ) + { + TEdge* Next = Edge1->NextInSEL; + if( Next ) Next->PrevInSEL = Edge2; + TEdge* Prev = Edge2->PrevInSEL; + if( Prev ) Prev->NextInSEL = Edge1; + Edge1->PrevInSEL = Prev; + Edge1->NextInSEL = Edge2; + Edge2->PrevInSEL = Edge1; + Edge2->NextInSEL = Next; + } + else + { + TEdge* Next = Edge1->NextInSEL; + TEdge* Prev = Edge1->PrevInSEL; + Edge1->NextInSEL = Edge2->NextInSEL; + if( Edge1->NextInSEL ) Edge1->NextInSEL->PrevInSEL = Edge1; + Edge1->PrevInSEL = Edge2->PrevInSEL; + if( Edge1->PrevInSEL ) Edge1->PrevInSEL->NextInSEL = Edge1; + Edge2->NextInSEL = Next; + if( Edge2->NextInSEL ) Edge2->NextInSEL->PrevInSEL = Edge2; + Edge2->PrevInSEL = Prev; + if( Edge2->PrevInSEL ) Edge2->PrevInSEL->NextInSEL = Edge2; + } + + if( !Edge1->PrevInSEL ) m_SortedEdges = Edge1; + else if( !Edge2->PrevInSEL ) m_SortedEdges = Edge2; +} +//------------------------------------------------------------------------------ + +TEdge* GetNextInAEL(TEdge *e, Direction dir) +{ + return dir == dLeftToRight ? e->NextInAEL : e->PrevInAEL; +} +//------------------------------------------------------------------------------ + +void GetHorzDirection(TEdge& HorzEdge, Direction& Dir, cInt& Left, cInt& Right) +{ + if (HorzEdge.Bot.X < HorzEdge.Top.X) + { + Left = HorzEdge.Bot.X; + Right = HorzEdge.Top.X; + Dir = dLeftToRight; + } else + { + Left = HorzEdge.Top.X; + Right = HorzEdge.Bot.X; + Dir = dRightToLeft; + } +} +//------------------------------------------------------------------------ + +/******************************************************************************* +* Notes: Horizontal edges (HEs) at scanline intersections (ie at the Top or * +* Bottom of a scanbeam) are processed as if layered. The order in which HEs * +* are processed doesn't matter. HEs intersect with other HE Bot.Xs only [#] * +* (or they could intersect with Top.Xs only, ie EITHER Bot.Xs OR Top.Xs), * +* and with other non-horizontal edges [*]. Once these intersections are * +* processed, intermediate HEs then 'promote' the Edge above (NextInLML) into * +* the AEL. These 'promoted' edges may in turn intersect [%] with other HEs. * +*******************************************************************************/ + +void Clipper::ProcessHorizontal(TEdge *horzEdge, bool isTopOfScanbeam) +{ + Direction dir; + cInt horzLeft, horzRight; + + GetHorzDirection(*horzEdge, dir, horzLeft, horzRight); + + TEdge* eLastHorz = horzEdge, *eMaxPair = 0; + while (eLastHorz->NextInLML && IsHorizontal(*eLastHorz->NextInLML)) + eLastHorz = eLastHorz->NextInLML; + if (!eLastHorz->NextInLML) + eMaxPair = GetMaximaPair(eLastHorz); + + for (;;) + { + bool IsLastHorz = (horzEdge == eLastHorz); + TEdge* e = GetNextInAEL(horzEdge, dir); + while(e) + { + //Break if we've got to the end of an intermediate horizontal edge ... + //nb: Smaller Dx's are to the right of larger Dx's ABOVE the horizontal. + if (e->Curr.X == horzEdge->Top.X && horzEdge->NextInLML && + e->Dx < horzEdge->NextInLML->Dx) break; + + TEdge* eNext = GetNextInAEL(e, dir); //saves eNext for later + + if ((dir == dLeftToRight && e->Curr.X <= horzRight) || + (dir == dRightToLeft && e->Curr.X >= horzLeft)) + { + //so far we're still in range of the horizontal Edge but make sure + //we're at the last of consec. horizontals when matching with eMaxPair + if(e == eMaxPair && IsLastHorz) + { + + if (horzEdge->OutIdx >= 0) + { + OutPt* op1 = AddOutPt(horzEdge, horzEdge->Top); + TEdge* eNextHorz = m_SortedEdges; + while (eNextHorz) + { + if (eNextHorz->OutIdx >= 0 && + HorzSegmentsOverlap(horzEdge->Bot.X, + horzEdge->Top.X, eNextHorz->Bot.X, eNextHorz->Top.X)) + { + OutPt* op2 = AddOutPt(eNextHorz, eNextHorz->Bot); + AddJoin(op2, op1, eNextHorz->Top); + } + eNextHorz = eNextHorz->NextInSEL; + } + AddGhostJoin(op1, horzEdge->Bot); + AddLocalMaxPoly(horzEdge, eMaxPair, horzEdge->Top); + } + DeleteFromAEL(horzEdge); + DeleteFromAEL(eMaxPair); + return; + } + else if(dir == dLeftToRight) + { + IntPoint Pt = IntPoint(e->Curr.X, horzEdge->Curr.Y); + IntersectEdges(horzEdge, e, Pt); + } + else + { + IntPoint Pt = IntPoint(e->Curr.X, horzEdge->Curr.Y); + IntersectEdges( e, horzEdge, Pt); + } + SwapPositionsInAEL( horzEdge, e ); + } + else if( (dir == dLeftToRight && e->Curr.X >= horzRight) || + (dir == dRightToLeft && e->Curr.X <= horzLeft) ) break; + e = eNext; + } //end while + + if (horzEdge->NextInLML && IsHorizontal(*horzEdge->NextInLML)) + { + UpdateEdgeIntoAEL(horzEdge); + if (horzEdge->OutIdx >= 0) AddOutPt(horzEdge, horzEdge->Bot); + GetHorzDirection(*horzEdge, dir, horzLeft, horzRight); + } else + break; + } //end for (;;) + + if(horzEdge->NextInLML) + { + if(horzEdge->OutIdx >= 0) + { + OutPt* op1 = AddOutPt( horzEdge, horzEdge->Top); + if (isTopOfScanbeam) AddGhostJoin(op1, horzEdge->Bot); + UpdateEdgeIntoAEL(horzEdge); + if (horzEdge->WindDelta == 0) return; + //nb: HorzEdge is no longer horizontal here + TEdge* ePrev = horzEdge->PrevInAEL; + TEdge* eNext = horzEdge->NextInAEL; + if (ePrev && ePrev->Curr.X == horzEdge->Bot.X && + ePrev->Curr.Y == horzEdge->Bot.Y && ePrev->WindDelta != 0 && + (ePrev->OutIdx >= 0 && ePrev->Curr.Y > ePrev->Top.Y && + SlopesEqual(*horzEdge, *ePrev, m_UseFullRange))) + { + OutPt* op2 = AddOutPt(ePrev, horzEdge->Bot); + AddJoin(op1, op2, horzEdge->Top); + } + else if (eNext && eNext->Curr.X == horzEdge->Bot.X && + eNext->Curr.Y == horzEdge->Bot.Y && eNext->WindDelta != 0 && + eNext->OutIdx >= 0 && eNext->Curr.Y > eNext->Top.Y && + SlopesEqual(*horzEdge, *eNext, m_UseFullRange)) + { + OutPt* op2 = AddOutPt(eNext, horzEdge->Bot); + AddJoin(op1, op2, horzEdge->Top); + } + } + else + UpdateEdgeIntoAEL(horzEdge); + } + else + { + if (horzEdge->OutIdx >= 0) AddOutPt(horzEdge, horzEdge->Top); + DeleteFromAEL(horzEdge); + } +} +//------------------------------------------------------------------------------ + +void Clipper::UpdateEdgeIntoAEL(TEdge *&e) +{ + if( !e->NextInLML ) throw + clipperException("UpdateEdgeIntoAEL: invalid call"); + + e->NextInLML->OutIdx = e->OutIdx; + TEdge* AelPrev = e->PrevInAEL; + TEdge* AelNext = e->NextInAEL; + if (AelPrev) AelPrev->NextInAEL = e->NextInLML; + else m_ActiveEdges = e->NextInLML; + if (AelNext) AelNext->PrevInAEL = e->NextInLML; + e->NextInLML->Side = e->Side; + e->NextInLML->WindDelta = e->WindDelta; + e->NextInLML->WindCnt = e->WindCnt; + e->NextInLML->WindCnt2 = e->WindCnt2; + e = e->NextInLML; + e->Curr = e->Bot; + e->PrevInAEL = AelPrev; + e->NextInAEL = AelNext; + if (!IsHorizontal(*e)) InsertScanbeam(e->Top.Y); +} +//------------------------------------------------------------------------------ + +bool Clipper::ProcessIntersections(const cInt botY, const cInt topY) +{ + if( !m_ActiveEdges ) return true; + try { + BuildIntersectList(botY, topY); + size_t IlSize = m_IntersectList.size(); + if (IlSize == 0) return true; + if (IlSize == 1 || FixupIntersectionOrder()) ProcessIntersectList(); + else return false; + } + catch(...) + { + m_SortedEdges = 0; + DisposeIntersectNodes(); + throw clipperException("ProcessIntersections error"); + } + m_SortedEdges = 0; + return true; +} +//------------------------------------------------------------------------------ + +void Clipper::DisposeIntersectNodes() +{ + for (size_t i = 0; i < m_IntersectList.size(); ++i ) + delete m_IntersectList[i]; + m_IntersectList.clear(); +} +//------------------------------------------------------------------------------ + +void Clipper::BuildIntersectList(const cInt botY, const cInt topY) +{ + if ( !m_ActiveEdges ) return; + + //prepare for sorting ... + TEdge* e = m_ActiveEdges; + m_SortedEdges = e; + while( e ) + { + e->PrevInSEL = e->PrevInAEL; + e->NextInSEL = e->NextInAEL; + e->Curr.X = TopX( *e, topY ); + e = e->NextInAEL; + } + + //bubblesort ... + bool isModified; + do + { + isModified = false; + e = m_SortedEdges; + while( e->NextInSEL ) + { + TEdge *eNext = e->NextInSEL; + IntPoint Pt; + if(e->Curr.X > eNext->Curr.X) + { + IntersectPoint(*e, *eNext, Pt); + IntersectNode * newNode = new IntersectNode; + newNode->Edge1 = e; + newNode->Edge2 = eNext; + newNode->Pt = Pt; + m_IntersectList.push_back(newNode); + + SwapPositionsInSEL(e, eNext); + isModified = true; + } + else + e = eNext; + } + if( e->PrevInSEL ) e->PrevInSEL->NextInSEL = 0; + else break; + } + while ( isModified ); + m_SortedEdges = 0; //important +} +//------------------------------------------------------------------------------ + + +void Clipper::ProcessIntersectList() +{ + for (size_t i = 0; i < m_IntersectList.size(); ++i) + { + IntersectNode* iNode = m_IntersectList[i]; + { + IntersectEdges( iNode->Edge1, iNode->Edge2, iNode->Pt); + SwapPositionsInAEL( iNode->Edge1 , iNode->Edge2 ); + } + delete iNode; + } + m_IntersectList.clear(); +} +//------------------------------------------------------------------------------ + +bool IntersectListSort(IntersectNode* node1, IntersectNode* node2) +{ + return node2->Pt.Y < node1->Pt.Y; +} +//------------------------------------------------------------------------------ + +inline bool EdgesAdjacent(const IntersectNode &inode) +{ + return (inode.Edge1->NextInSEL == inode.Edge2) || + (inode.Edge1->PrevInSEL == inode.Edge2); +} +//------------------------------------------------------------------------------ + +bool Clipper::FixupIntersectionOrder() +{ + //pre-condition: intersections are sorted Bottom-most first. + //Now it's crucial that intersections are made only between adjacent edges, + //so to ensure this the order of intersections may need adjusting ... + CopyAELToSEL(); + std::sort(m_IntersectList.begin(), m_IntersectList.end(), IntersectListSort); + size_t cnt = m_IntersectList.size(); + for (size_t i = 0; i < cnt; ++i) + { + if (!EdgesAdjacent(*m_IntersectList[i])) + { + size_t j = i + 1; + while (j < cnt && !EdgesAdjacent(*m_IntersectList[j])) j++; + if (j == cnt) return false; + std::swap(m_IntersectList[i], m_IntersectList[j]); + } + SwapPositionsInSEL(m_IntersectList[i]->Edge1, m_IntersectList[i]->Edge2); + } + return true; +} +//------------------------------------------------------------------------------ + +void Clipper::DoMaxima(TEdge *e) +{ + TEdge* eMaxPair = GetMaximaPair(e); + if (!eMaxPair) + { + if (e->OutIdx >= 0) + AddOutPt(e, e->Top); + DeleteFromAEL(e); + return; + } + + TEdge* eNext = e->NextInAEL; + while(eNext && eNext != eMaxPair) + { + IntersectEdges(e, eNext, e->Top); + SwapPositionsInAEL(e, eNext); + eNext = e->NextInAEL; + } + + if(e->OutIdx == Unassigned && eMaxPair->OutIdx == Unassigned) + { + DeleteFromAEL(e); + DeleteFromAEL(eMaxPair); + } + else if( e->OutIdx >= 0 && eMaxPair->OutIdx >= 0 ) + { + if (e->OutIdx >= 0) AddLocalMaxPoly(e, eMaxPair, e->Top); + DeleteFromAEL(e); + DeleteFromAEL(eMaxPair); + } +#ifdef use_lines + else if (e->WindDelta == 0) + { + if (e->OutIdx >= 0) + { + AddOutPt(e, e->Top); + e->OutIdx = Unassigned; + } + DeleteFromAEL(e); + + if (eMaxPair->OutIdx >= 0) + { + AddOutPt(eMaxPair, e->Top); + eMaxPair->OutIdx = Unassigned; + } + DeleteFromAEL(eMaxPair); + } +#endif + else throw clipperException("DoMaxima error"); +} +//------------------------------------------------------------------------------ + +void Clipper::ProcessEdgesAtTopOfScanbeam(const cInt topY) +{ + TEdge* e = m_ActiveEdges; + while( e ) + { + //1. process maxima, treating them as if they're 'bent' horizontal edges, + // but exclude maxima with horizontal edges. nb: e can't be a horizontal. + bool IsMaximaEdge = IsMaxima(e, topY); + + if(IsMaximaEdge) + { + TEdge* eMaxPair = GetMaximaPair(e); + IsMaximaEdge = (!eMaxPair || !IsHorizontal(*eMaxPair)); + } + + if(IsMaximaEdge) + { + TEdge* ePrev = e->PrevInAEL; + DoMaxima(e); + if( !ePrev ) e = m_ActiveEdges; + else e = ePrev->NextInAEL; + } + else + { + //2. promote horizontal edges, otherwise update Curr.X and Curr.Y ... + if (IsIntermediate(e, topY) && IsHorizontal(*e->NextInLML)) + { + UpdateEdgeIntoAEL(e); + if (e->OutIdx >= 0) + AddOutPt(e, e->Bot); + AddEdgeToSEL(e); + } + else + { + e->Curr.X = TopX( *e, topY ); + e->Curr.Y = topY; + } + + if (m_StrictSimple) + { + TEdge* ePrev = e->PrevInAEL; + if ((e->OutIdx >= 0) && (e->WindDelta != 0) && ePrev && (ePrev->OutIdx >= 0) && + (ePrev->Curr.X == e->Curr.X) && (ePrev->WindDelta != 0)) + { + IntPoint pt = e->Curr; +#ifdef use_xyz + SetZ(pt, *ePrev, *e); +#endif + OutPt* op = AddOutPt(ePrev, pt); + OutPt* op2 = AddOutPt(e, pt); + AddJoin(op, op2, pt); //StrictlySimple (type-3) join + } + } + + e = e->NextInAEL; + } + } + + //3. Process horizontals at the Top of the scanbeam ... + ProcessHorizontals(true); + + //4. Promote intermediate vertices ... + e = m_ActiveEdges; + while(e) + { + if(IsIntermediate(e, topY)) + { + OutPt* op = 0; + if( e->OutIdx >= 0 ) + op = AddOutPt(e, e->Top); + UpdateEdgeIntoAEL(e); + + //if output polygons share an edge, they'll need joining later ... + TEdge* ePrev = e->PrevInAEL; + TEdge* eNext = e->NextInAEL; + if (ePrev && ePrev->Curr.X == e->Bot.X && + ePrev->Curr.Y == e->Bot.Y && op && + ePrev->OutIdx >= 0 && ePrev->Curr.Y > ePrev->Top.Y && + SlopesEqual(*e, *ePrev, m_UseFullRange) && + (e->WindDelta != 0) && (ePrev->WindDelta != 0)) + { + OutPt* op2 = AddOutPt(ePrev, e->Bot); + AddJoin(op, op2, e->Top); + } + else if (eNext && eNext->Curr.X == e->Bot.X && + eNext->Curr.Y == e->Bot.Y && op && + eNext->OutIdx >= 0 && eNext->Curr.Y > eNext->Top.Y && + SlopesEqual(*e, *eNext, m_UseFullRange) && + (e->WindDelta != 0) && (eNext->WindDelta != 0)) + { + OutPt* op2 = AddOutPt(eNext, e->Bot); + AddJoin(op, op2, e->Top); + } + } + e = e->NextInAEL; + } +} +//------------------------------------------------------------------------------ + +void Clipper::FixupOutPolygon(OutRec &outrec) +{ + //FixupOutPolygon() - removes duplicate points and simplifies consecutive + //parallel edges by removing the middle vertex. + OutPt *lastOK = 0; + outrec.BottomPt = 0; + OutPt *pp = outrec.Pts; + + for (;;) + { + if (pp->Prev == pp || pp->Prev == pp->Next ) + { + DisposeOutPts(pp); + outrec.Pts = 0; + return; + } + + //test for duplicate points and collinear edges ... + if ((pp->Pt == pp->Next->Pt) || (pp->Pt == pp->Prev->Pt) || + (SlopesEqual(pp->Prev->Pt, pp->Pt, pp->Next->Pt, m_UseFullRange) && + (!m_PreserveCollinear || + !Pt2IsBetweenPt1AndPt3(pp->Prev->Pt, pp->Pt, pp->Next->Pt)))) + { + lastOK = 0; + OutPt *tmp = pp; + pp->Prev->Next = pp->Next; + pp->Next->Prev = pp->Prev; + pp = pp->Prev; + delete tmp; + } + else if (pp == lastOK) break; + else + { + if (!lastOK) lastOK = pp; + pp = pp->Next; + } + } + outrec.Pts = pp; +} +//------------------------------------------------------------------------------ + +int PointCount(OutPt *Pts) +{ + if (!Pts) return 0; + int result = 0; + OutPt* p = Pts; + do + { + result++; + p = p->Next; + } + while (p != Pts); + return result; +} +//------------------------------------------------------------------------------ + +void Clipper::BuildResult(Paths &polys) +{ + polys.reserve(m_PolyOuts.size()); + for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); ++i) + { + if (!m_PolyOuts[i]->Pts) continue; + Path pg; + OutPt* p = m_PolyOuts[i]->Pts->Prev; + int cnt = PointCount(p); + if (cnt < 2) continue; + pg.reserve(cnt); + for (int i = 0; i < cnt; ++i) + { + pg.push_back(p->Pt); + p = p->Prev; + } + polys.push_back(pg); + } +} +//------------------------------------------------------------------------------ + +void Clipper::BuildResult2(PolyTree& polytree) +{ + polytree.Clear(); + polytree.AllNodes.reserve(m_PolyOuts.size()); + //add each output polygon/contour to polytree ... + for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); i++) + { + OutRec* outRec = m_PolyOuts[i]; + int cnt = PointCount(outRec->Pts); + if ((outRec->IsOpen && cnt < 2) || (!outRec->IsOpen && cnt < 3)) continue; + FixHoleLinkage(*outRec); + PolyNode* pn = new PolyNode(); + //nb: polytree takes ownership of all the PolyNodes + polytree.AllNodes.push_back(pn); + outRec->PolyNd = pn; + pn->Parent = 0; + pn->Index = 0; + pn->Contour.reserve(cnt); + OutPt *op = outRec->Pts->Prev; + for (int j = 0; j < cnt; j++) + { + pn->Contour.push_back(op->Pt); + op = op->Prev; + } + } + + //fixup PolyNode links etc ... + polytree.Childs.reserve(m_PolyOuts.size()); + for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); i++) + { + OutRec* outRec = m_PolyOuts[i]; + if (!outRec->PolyNd) continue; + if (outRec->IsOpen) + { + outRec->PolyNd->m_IsOpen = true; + polytree.AddChild(*outRec->PolyNd); + } + else if (outRec->FirstLeft && outRec->FirstLeft->PolyNd) + outRec->FirstLeft->PolyNd->AddChild(*outRec->PolyNd); + else + polytree.AddChild(*outRec->PolyNd); + } +} +//------------------------------------------------------------------------------ + +void SwapIntersectNodes(IntersectNode &int1, IntersectNode &int2) +{ + //just swap the contents (because fIntersectNodes is a single-linked-list) + IntersectNode inode = int1; //gets a copy of Int1 + int1.Edge1 = int2.Edge1; + int1.Edge2 = int2.Edge2; + int1.Pt = int2.Pt; + int2.Edge1 = inode.Edge1; + int2.Edge2 = inode.Edge2; + int2.Pt = inode.Pt; +} +//------------------------------------------------------------------------------ + +inline bool E2InsertsBeforeE1(TEdge &e1, TEdge &e2) +{ + if (e2.Curr.X == e1.Curr.X) + { + if (e2.Top.Y > e1.Top.Y) + return e2.Top.X < TopX(e1, e2.Top.Y); + else return e1.Top.X > TopX(e2, e1.Top.Y); + } + else return e2.Curr.X < e1.Curr.X; +} +//------------------------------------------------------------------------------ + +bool GetOverlap(const cInt a1, const cInt a2, const cInt b1, const cInt b2, + cInt& Left, cInt& Right) +{ + if (a1 < a2) + { + if (b1 < b2) {Left = std::max(a1,b1); Right = std::min(a2,b2);} + else {Left = std::max(a1,b2); Right = std::min(a2,b1);} + } + else + { + if (b1 < b2) {Left = std::max(a2,b1); Right = std::min(a1,b2);} + else {Left = std::max(a2,b2); Right = std::min(a1,b1);} + } + return Left < Right; +} +//------------------------------------------------------------------------------ + +inline void UpdateOutPtIdxs(OutRec& outrec) +{ + OutPt* op = outrec.Pts; + do + { + op->Idx = outrec.Idx; + op = op->Prev; + } + while(op != outrec.Pts); +} +//------------------------------------------------------------------------------ + +void Clipper::InsertEdgeIntoAEL(TEdge *edge, TEdge* startEdge) +{ + if(!m_ActiveEdges) + { + edge->PrevInAEL = 0; + edge->NextInAEL = 0; + m_ActiveEdges = edge; + } + else if(!startEdge && E2InsertsBeforeE1(*m_ActiveEdges, *edge)) + { + edge->PrevInAEL = 0; + edge->NextInAEL = m_ActiveEdges; + m_ActiveEdges->PrevInAEL = edge; + m_ActiveEdges = edge; + } + else + { + if(!startEdge) startEdge = m_ActiveEdges; + while(startEdge->NextInAEL && + !E2InsertsBeforeE1(*startEdge->NextInAEL , *edge)) + startEdge = startEdge->NextInAEL; + edge->NextInAEL = startEdge->NextInAEL; + if(startEdge->NextInAEL) startEdge->NextInAEL->PrevInAEL = edge; + edge->PrevInAEL = startEdge; + startEdge->NextInAEL = edge; + } +} +//---------------------------------------------------------------------- + +OutPt* DupOutPt(OutPt* outPt, bool InsertAfter) +{ + OutPt* result = new OutPt; + result->Pt = outPt->Pt; + result->Idx = outPt->Idx; + if (InsertAfter) + { + result->Next = outPt->Next; + result->Prev = outPt; + outPt->Next->Prev = result; + outPt->Next = result; + } + else + { + result->Prev = outPt->Prev; + result->Next = outPt; + outPt->Prev->Next = result; + outPt->Prev = result; + } + return result; +} +//------------------------------------------------------------------------------ + +bool JoinHorz(OutPt* op1, OutPt* op1b, OutPt* op2, OutPt* op2b, + const IntPoint Pt, bool DiscardLeft) +{ + Direction Dir1 = (op1->Pt.X > op1b->Pt.X ? dRightToLeft : dLeftToRight); + Direction Dir2 = (op2->Pt.X > op2b->Pt.X ? dRightToLeft : dLeftToRight); + if (Dir1 == Dir2) return false; + + //When DiscardLeft, we want Op1b to be on the Left of Op1, otherwise we + //want Op1b to be on the Right. (And likewise with Op2 and Op2b.) + //So, to facilitate this while inserting Op1b and Op2b ... + //when DiscardLeft, make sure we're AT or RIGHT of Pt before adding Op1b, + //otherwise make sure we're AT or LEFT of Pt. (Likewise with Op2b.) + if (Dir1 == dLeftToRight) + { + while (op1->Next->Pt.X <= Pt.X && + op1->Next->Pt.X >= op1->Pt.X && op1->Next->Pt.Y == Pt.Y) + op1 = op1->Next; + if (DiscardLeft && (op1->Pt.X != Pt.X)) op1 = op1->Next; + op1b = DupOutPt(op1, !DiscardLeft); + if (op1b->Pt != Pt) + { + op1 = op1b; + op1->Pt = Pt; + op1b = DupOutPt(op1, !DiscardLeft); + } + } + else + { + while (op1->Next->Pt.X >= Pt.X && + op1->Next->Pt.X <= op1->Pt.X && op1->Next->Pt.Y == Pt.Y) + op1 = op1->Next; + if (!DiscardLeft && (op1->Pt.X != Pt.X)) op1 = op1->Next; + op1b = DupOutPt(op1, DiscardLeft); + if (op1b->Pt != Pt) + { + op1 = op1b; + op1->Pt = Pt; + op1b = DupOutPt(op1, DiscardLeft); + } + } + + if (Dir2 == dLeftToRight) + { + while (op2->Next->Pt.X <= Pt.X && + op2->Next->Pt.X >= op2->Pt.X && op2->Next->Pt.Y == Pt.Y) + op2 = op2->Next; + if (DiscardLeft && (op2->Pt.X != Pt.X)) op2 = op2->Next; + op2b = DupOutPt(op2, !DiscardLeft); + if (op2b->Pt != Pt) + { + op2 = op2b; + op2->Pt = Pt; + op2b = DupOutPt(op2, !DiscardLeft); + }; + } else + { + while (op2->Next->Pt.X >= Pt.X && + op2->Next->Pt.X <= op2->Pt.X && op2->Next->Pt.Y == Pt.Y) + op2 = op2->Next; + if (!DiscardLeft && (op2->Pt.X != Pt.X)) op2 = op2->Next; + op2b = DupOutPt(op2, DiscardLeft); + if (op2b->Pt != Pt) + { + op2 = op2b; + op2->Pt = Pt; + op2b = DupOutPt(op2, DiscardLeft); + }; + }; + + if ((Dir1 == dLeftToRight) == DiscardLeft) + { + op1->Prev = op2; + op2->Next = op1; + op1b->Next = op2b; + op2b->Prev = op1b; + } + else + { + op1->Next = op2; + op2->Prev = op1; + op1b->Prev = op2b; + op2b->Next = op1b; + } + return true; +} +//------------------------------------------------------------------------------ + +bool Clipper::JoinPoints(Join *j, OutRec* outRec1, OutRec* outRec2) +{ + OutPt *op1 = j->OutPt1, *op1b; + OutPt *op2 = j->OutPt2, *op2b; + + //There are 3 kinds of joins for output polygons ... + //1. Horizontal joins where Join.OutPt1 & Join.OutPt2 are a vertices anywhere + //along (horizontal) collinear edges (& Join.OffPt is on the same horizontal). + //2. Non-horizontal joins where Join.OutPt1 & Join.OutPt2 are at the same + //location at the Bottom of the overlapping segment (& Join.OffPt is above). + //3. StrictSimple joins where edges touch but are not collinear and where + //Join.OutPt1, Join.OutPt2 & Join.OffPt all share the same point. + bool isHorizontal = (j->OutPt1->Pt.Y == j->OffPt.Y); + + if (isHorizontal && (j->OffPt == j->OutPt1->Pt) && + (j->OffPt == j->OutPt2->Pt)) + { + //Strictly Simple join ... + if (outRec1 != outRec2) return false; + op1b = j->OutPt1->Next; + while (op1b != op1 && (op1b->Pt == j->OffPt)) + op1b = op1b->Next; + bool reverse1 = (op1b->Pt.Y > j->OffPt.Y); + op2b = j->OutPt2->Next; + while (op2b != op2 && (op2b->Pt == j->OffPt)) + op2b = op2b->Next; + bool reverse2 = (op2b->Pt.Y > j->OffPt.Y); + if (reverse1 == reverse2) return false; + if (reverse1) + { + op1b = DupOutPt(op1, false); + op2b = DupOutPt(op2, true); + op1->Prev = op2; + op2->Next = op1; + op1b->Next = op2b; + op2b->Prev = op1b; + j->OutPt1 = op1; + j->OutPt2 = op1b; + return true; + } else + { + op1b = DupOutPt(op1, true); + op2b = DupOutPt(op2, false); + op1->Next = op2; + op2->Prev = op1; + op1b->Prev = op2b; + op2b->Next = op1b; + j->OutPt1 = op1; + j->OutPt2 = op1b; + return true; + } + } + else if (isHorizontal) + { + //treat horizontal joins differently to non-horizontal joins since with + //them we're not yet sure where the overlapping is. OutPt1.Pt & OutPt2.Pt + //may be anywhere along the horizontal edge. + op1b = op1; + while (op1->Prev->Pt.Y == op1->Pt.Y && op1->Prev != op1b && op1->Prev != op2) + op1 = op1->Prev; + while (op1b->Next->Pt.Y == op1b->Pt.Y && op1b->Next != op1 && op1b->Next != op2) + op1b = op1b->Next; + if (op1b->Next == op1 || op1b->Next == op2) return false; //a flat 'polygon' + + op2b = op2; + while (op2->Prev->Pt.Y == op2->Pt.Y && op2->Prev != op2b && op2->Prev != op1b) + op2 = op2->Prev; + while (op2b->Next->Pt.Y == op2b->Pt.Y && op2b->Next != op2 && op2b->Next != op1) + op2b = op2b->Next; + if (op2b->Next == op2 || op2b->Next == op1) return false; //a flat 'polygon' + + cInt Left, Right; + //Op1 --> Op1b & Op2 --> Op2b are the extremites of the horizontal edges + if (!GetOverlap(op1->Pt.X, op1b->Pt.X, op2->Pt.X, op2b->Pt.X, Left, Right)) + return false; + + //DiscardLeftSide: when overlapping edges are joined, a spike will created + //which needs to be cleaned up. However, we don't want Op1 or Op2 caught up + //on the discard Side as either may still be needed for other joins ... + IntPoint Pt; + bool DiscardLeftSide; + if (op1->Pt.X >= Left && op1->Pt.X <= Right) + { + Pt = op1->Pt; DiscardLeftSide = (op1->Pt.X > op1b->Pt.X); + } + else if (op2->Pt.X >= Left&& op2->Pt.X <= Right) + { + Pt = op2->Pt; DiscardLeftSide = (op2->Pt.X > op2b->Pt.X); + } + else if (op1b->Pt.X >= Left && op1b->Pt.X <= Right) + { + Pt = op1b->Pt; DiscardLeftSide = op1b->Pt.X > op1->Pt.X; + } + else + { + Pt = op2b->Pt; DiscardLeftSide = (op2b->Pt.X > op2->Pt.X); + } + j->OutPt1 = op1; j->OutPt2 = op2; + return JoinHorz(op1, op1b, op2, op2b, Pt, DiscardLeftSide); + } else + { + //nb: For non-horizontal joins ... + // 1. Jr.OutPt1.Pt.Y == Jr.OutPt2.Pt.Y + // 2. Jr.OutPt1.Pt > Jr.OffPt.Y + + //make sure the polygons are correctly oriented ... + op1b = op1->Next; + while ((op1b->Pt == op1->Pt) && (op1b != op1)) op1b = op1b->Next; + bool Reverse1 = ((op1b->Pt.Y > op1->Pt.Y) || + !SlopesEqual(op1->Pt, op1b->Pt, j->OffPt, m_UseFullRange)); + if (Reverse1) + { + op1b = op1->Prev; + while ((op1b->Pt == op1->Pt) && (op1b != op1)) op1b = op1b->Prev; + if ((op1b->Pt.Y > op1->Pt.Y) || + !SlopesEqual(op1->Pt, op1b->Pt, j->OffPt, m_UseFullRange)) return false; + }; + op2b = op2->Next; + while ((op2b->Pt == op2->Pt) && (op2b != op2))op2b = op2b->Next; + bool Reverse2 = ((op2b->Pt.Y > op2->Pt.Y) || + !SlopesEqual(op2->Pt, op2b->Pt, j->OffPt, m_UseFullRange)); + if (Reverse2) + { + op2b = op2->Prev; + while ((op2b->Pt == op2->Pt) && (op2b != op2)) op2b = op2b->Prev; + if ((op2b->Pt.Y > op2->Pt.Y) || + !SlopesEqual(op2->Pt, op2b->Pt, j->OffPt, m_UseFullRange)) return false; + } + + if ((op1b == op1) || (op2b == op2) || (op1b == op2b) || + ((outRec1 == outRec2) && (Reverse1 == Reverse2))) return false; + + if (Reverse1) + { + op1b = DupOutPt(op1, false); + op2b = DupOutPt(op2, true); + op1->Prev = op2; + op2->Next = op1; + op1b->Next = op2b; + op2b->Prev = op1b; + j->OutPt1 = op1; + j->OutPt2 = op1b; + return true; + } else + { + op1b = DupOutPt(op1, true); + op2b = DupOutPt(op2, false); + op1->Next = op2; + op2->Prev = op1; + op1b->Prev = op2b; + op2b->Next = op1b; + j->OutPt1 = op1; + j->OutPt2 = op1b; + return true; + } + } +} +//---------------------------------------------------------------------- + +static OutRec* ParseFirstLeft(OutRec* FirstLeft) +{ + while (FirstLeft && !FirstLeft->Pts) + FirstLeft = FirstLeft->FirstLeft; + return FirstLeft; +} +//------------------------------------------------------------------------------ + +void Clipper::FixupFirstLefts1(OutRec* OldOutRec, OutRec* NewOutRec) +{ + //tests if NewOutRec contains the polygon before reassigning FirstLeft + for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); ++i) + { + OutRec* outRec = m_PolyOuts[i]; + if (!outRec->Pts || !outRec->FirstLeft) continue; + OutRec* firstLeft = ParseFirstLeft(outRec->FirstLeft); + if (firstLeft == OldOutRec) + { + if (Poly2ContainsPoly1(outRec->Pts, NewOutRec->Pts)) + outRec->FirstLeft = NewOutRec; + } + } +} +//---------------------------------------------------------------------- + +void Clipper::FixupFirstLefts2(OutRec* OldOutRec, OutRec* NewOutRec) +{ + //reassigns FirstLeft WITHOUT testing if NewOutRec contains the polygon + for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); ++i) + { + OutRec* outRec = m_PolyOuts[i]; + if (outRec->FirstLeft == OldOutRec) outRec->FirstLeft = NewOutRec; + } +} +//---------------------------------------------------------------------- + +void Clipper::JoinCommonEdges() +{ + for (JoinList::size_type i = 0; i < m_Joins.size(); i++) + { + Join* join = m_Joins[i]; + + OutRec *outRec1 = GetOutRec(join->OutPt1->Idx); + OutRec *outRec2 = GetOutRec(join->OutPt2->Idx); + + if (!outRec1->Pts || !outRec2->Pts) continue; + + //get the polygon fragment with the correct hole state (FirstLeft) + //before calling JoinPoints() ... + OutRec *holeStateRec; + if (outRec1 == outRec2) holeStateRec = outRec1; + else if (Param1RightOfParam2(outRec1, outRec2)) holeStateRec = outRec2; + else if (Param1RightOfParam2(outRec2, outRec1)) holeStateRec = outRec1; + else holeStateRec = GetLowermostRec(outRec1, outRec2); + + if (!JoinPoints(join, outRec1, outRec2)) continue; + + if (outRec1 == outRec2) + { + //instead of joining two polygons, we've just created a new one by + //splitting one polygon into two. + outRec1->Pts = join->OutPt1; + outRec1->BottomPt = 0; + outRec2 = CreateOutRec(); + outRec2->Pts = join->OutPt2; + + //update all OutRec2.Pts Idx's ... + UpdateOutPtIdxs(*outRec2); + + //We now need to check every OutRec.FirstLeft pointer. If it points + //to OutRec1 it may need to point to OutRec2 instead ... + if (m_UsingPolyTree) + for (PolyOutList::size_type j = 0; j < m_PolyOuts.size() - 1; j++) + { + OutRec* oRec = m_PolyOuts[j]; + if (!oRec->Pts || ParseFirstLeft(oRec->FirstLeft) != outRec1 || + oRec->IsHole == outRec1->IsHole) continue; + if (Poly2ContainsPoly1(oRec->Pts, join->OutPt2)) + oRec->FirstLeft = outRec2; + } + + if (Poly2ContainsPoly1(outRec2->Pts, outRec1->Pts)) + { + //outRec2 is contained by outRec1 ... + outRec2->IsHole = !outRec1->IsHole; + outRec2->FirstLeft = outRec1; + + //fixup FirstLeft pointers that may need reassigning to OutRec1 + if (m_UsingPolyTree) FixupFirstLefts2(outRec2, outRec1); + + if ((outRec2->IsHole ^ m_ReverseOutput) == (Area(*outRec2) > 0)) + ReversePolyPtLinks(outRec2->Pts); + + } else if (Poly2ContainsPoly1(outRec1->Pts, outRec2->Pts)) + { + //outRec1 is contained by outRec2 ... + outRec2->IsHole = outRec1->IsHole; + outRec1->IsHole = !outRec2->IsHole; + outRec2->FirstLeft = outRec1->FirstLeft; + outRec1->FirstLeft = outRec2; + + //fixup FirstLeft pointers that may need reassigning to OutRec1 + if (m_UsingPolyTree) FixupFirstLefts2(outRec1, outRec2); + + if ((outRec1->IsHole ^ m_ReverseOutput) == (Area(*outRec1) > 0)) + ReversePolyPtLinks(outRec1->Pts); + } + else + { + //the 2 polygons are completely separate ... + outRec2->IsHole = outRec1->IsHole; + outRec2->FirstLeft = outRec1->FirstLeft; + + //fixup FirstLeft pointers that may need reassigning to OutRec2 + if (m_UsingPolyTree) FixupFirstLefts1(outRec1, outRec2); + } + + } else + { + //joined 2 polygons together ... + + outRec2->Pts = 0; + outRec2->BottomPt = 0; + outRec2->Idx = outRec1->Idx; + + outRec1->IsHole = holeStateRec->IsHole; + if (holeStateRec == outRec2) + outRec1->FirstLeft = outRec2->FirstLeft; + outRec2->FirstLeft = outRec1; + + //fixup FirstLeft pointers that may need reassigning to OutRec1 + if (m_UsingPolyTree) FixupFirstLefts2(outRec2, outRec1); + } + } +} + +//------------------------------------------------------------------------------ +// ClipperOffset support functions ... +//------------------------------------------------------------------------------ + +DoublePoint GetUnitNormal(const IntPoint &pt1, const IntPoint &pt2) +{ + if(pt2.X == pt1.X && pt2.Y == pt1.Y) + return DoublePoint(0, 0); + + double Dx = (double)(pt2.X - pt1.X); + double dy = (double)(pt2.Y - pt1.Y); + double f = 1 *1.0/ std::sqrt( Dx*Dx + dy*dy ); + Dx *= f; + dy *= f; + return DoublePoint(dy, -Dx); +} + +//------------------------------------------------------------------------------ +// ClipperOffset class +//------------------------------------------------------------------------------ + +ClipperOffset::ClipperOffset(double miterLimit, double arcTolerance) +{ + this->MiterLimit = miterLimit; + this->ArcTolerance = arcTolerance; + m_lowest.X = -1; +} +//------------------------------------------------------------------------------ + +ClipperOffset::~ClipperOffset() +{ + Clear(); +} +//------------------------------------------------------------------------------ + +void ClipperOffset::Clear() +{ + for (int i = 0; i < m_polyNodes.ChildCount(); ++i) + delete m_polyNodes.Childs[i]; + m_polyNodes.Childs.clear(); + m_lowest.X = -1; +} +//------------------------------------------------------------------------------ + +void ClipperOffset::AddPath(const Path& path, JoinType joinType, EndType endType) +{ + int highI = (int)path.size() - 1; + if (highI < 0) return; + PolyNode* newNode = new PolyNode(); + newNode->m_jointype = joinType; + newNode->m_endtype = endType; + + //strip duplicate points from path and also get index to the lowest point ... + if (endType == etClosedLine || endType == etClosedPolygon) + while (highI > 0 && path[0] == path[highI]) highI--; + newNode->Contour.reserve(highI + 1); + newNode->Contour.push_back(path[0]); + int j = 0, k = 0; + for (int i = 1; i <= highI; i++) + if (newNode->Contour[j] != path[i]) + { + j++; + newNode->Contour.push_back(path[i]); + if (path[i].Y > newNode->Contour[k].Y || + (path[i].Y == newNode->Contour[k].Y && + path[i].X < newNode->Contour[k].X)) k = j; + } + if (endType == etClosedPolygon && j < 2) + { + delete newNode; + return; + } + m_polyNodes.AddChild(*newNode); + + //if this path's lowest pt is lower than all the others then update m_lowest + if (endType != etClosedPolygon) return; + if (m_lowest.X < 0) + m_lowest = IntPoint(m_polyNodes.ChildCount() - 1, k); + else + { + IntPoint ip = m_polyNodes.Childs[(int)m_lowest.X]->Contour[(int)m_lowest.Y]; + if (newNode->Contour[k].Y > ip.Y || + (newNode->Contour[k].Y == ip.Y && + newNode->Contour[k].X < ip.X)) + m_lowest = IntPoint(m_polyNodes.ChildCount() - 1, k); + } +} +//------------------------------------------------------------------------------ + +void ClipperOffset::AddPaths(const Paths& paths, JoinType joinType, EndType endType) +{ + for (Paths::size_type i = 0; i < paths.size(); ++i) + AddPath(paths[i], joinType, endType); +} +//------------------------------------------------------------------------------ + +void ClipperOffset::FixOrientations() +{ + //fixup orientations of all closed paths if the orientation of the + //closed path with the lowermost vertex is wrong ... + if (m_lowest.X >= 0 && + !Orientation(m_polyNodes.Childs[(int)m_lowest.X]->Contour)) + { + for (int i = 0; i < m_polyNodes.ChildCount(); ++i) + { + PolyNode& node = *m_polyNodes.Childs[i]; + if (node.m_endtype == etClosedPolygon || + (node.m_endtype == etClosedLine && Orientation(node.Contour))) + ReversePath(node.Contour); + } + } else + { + for (int i = 0; i < m_polyNodes.ChildCount(); ++i) + { + PolyNode& node = *m_polyNodes.Childs[i]; + if (node.m_endtype == etClosedLine && !Orientation(node.Contour)) + ReversePath(node.Contour); + } + } +} +//------------------------------------------------------------------------------ + +void ClipperOffset::Execute(Paths& solution, double delta) +{ + solution.clear(); + FixOrientations(); + DoOffset(delta); + + //now clean up 'corners' ... + Clipper clpr; + clpr.AddPaths(m_destPolys, ptSubject, true); + if (delta > 0) + { + clpr.Execute(ctUnion, solution, pftPositive, pftPositive); + } + else + { + IntRect r = clpr.GetBounds(); + Path outer(4); + outer[0] = IntPoint(r.left - 10, r.bottom + 10); + outer[1] = IntPoint(r.right + 10, r.bottom + 10); + outer[2] = IntPoint(r.right + 10, r.top - 10); + outer[3] = IntPoint(r.left - 10, r.top - 10); + + clpr.AddPath(outer, ptSubject, true); + clpr.ReverseSolution(true); + clpr.Execute(ctUnion, solution, pftNegative, pftNegative); + if (solution.size() > 0) solution.erase(solution.begin()); + } +} +//------------------------------------------------------------------------------ + +void ClipperOffset::Execute(PolyTree& solution, double delta) +{ + solution.Clear(); + FixOrientations(); + DoOffset(delta); + + //now clean up 'corners' ... + Clipper clpr; + clpr.AddPaths(m_destPolys, ptSubject, true); + if (delta > 0) + { + clpr.Execute(ctUnion, solution, pftPositive, pftPositive); + } + else + { + IntRect r = clpr.GetBounds(); + Path outer(4); + outer[0] = IntPoint(r.left - 10, r.bottom + 10); + outer[1] = IntPoint(r.right + 10, r.bottom + 10); + outer[2] = IntPoint(r.right + 10, r.top - 10); + outer[3] = IntPoint(r.left - 10, r.top - 10); + + clpr.AddPath(outer, ptSubject, true); + clpr.ReverseSolution(true); + clpr.Execute(ctUnion, solution, pftNegative, pftNegative); + //remove the outer PolyNode rectangle ... + if (solution.ChildCount() == 1 && solution.Childs[0]->ChildCount() > 0) + { + PolyNode* outerNode = solution.Childs[0]; + solution.Childs.reserve(outerNode->ChildCount()); + solution.Childs[0] = outerNode->Childs[0]; + for (int i = 1; i < outerNode->ChildCount(); ++i) + solution.AddChild(*outerNode->Childs[i]); + } + else + solution.Clear(); + } +} +//------------------------------------------------------------------------------ + +void ClipperOffset::DoOffset(double delta) +{ + m_destPolys.clear(); + m_delta = delta; + + //if Zero offset, just copy any CLOSED polygons to m_p and return ... + if (NEAR_ZERO(delta)) + { + m_destPolys.reserve(m_polyNodes.ChildCount()); + for (int i = 0; i < m_polyNodes.ChildCount(); i++) + { + PolyNode& node = *m_polyNodes.Childs[i]; + if (node.m_endtype == etClosedPolygon) + m_destPolys.push_back(node.Contour); + } + return; + } + + //see offset_triginometry3.svg in the documentation folder ... + if (MiterLimit > 2) m_miterLim = 2/(MiterLimit * MiterLimit); + else m_miterLim = 0.5; + + double y; + if (ArcTolerance <= 0.0) y = def_arc_tolerance; + else if (ArcTolerance > std::fabs(delta) * def_arc_tolerance) + y = std::fabs(delta) * def_arc_tolerance; + else y = ArcTolerance; + //see offset_triginometry2.svg in the documentation folder ... + double steps = pi / std::acos(1 - y / std::fabs(delta)); + if (steps > std::fabs(delta) * pi) + steps = std::fabs(delta) * pi; //ie excessive precision check + m_sin = std::sin(two_pi / steps); + m_cos = std::cos(two_pi / steps); + m_StepsPerRad = steps / two_pi; + if (delta < 0.0) m_sin = -m_sin; + + m_destPolys.reserve(m_polyNodes.ChildCount() * 2); + for (int i = 0; i < m_polyNodes.ChildCount(); i++) + { + PolyNode& node = *m_polyNodes.Childs[i]; + m_srcPoly = node.Contour; + + int len = (int)m_srcPoly.size(); + if (len == 0 || (delta <= 0 && (len < 3 || node.m_endtype != etClosedPolygon))) + continue; + + m_destPoly.clear(); + if (len == 1) + { + if (node.m_jointype == jtRound) + { + double X = 1.0, Y = 0.0; + for (cInt j = 1; j <= steps; j++) + { + m_destPoly.push_back(IntPoint( + Round(m_srcPoly[0].X + X * delta), + Round(m_srcPoly[0].Y + Y * delta))); + double X2 = X; + X = X * m_cos - m_sin * Y; + Y = X2 * m_sin + Y * m_cos; + } + } + else + { + double X = -1.0, Y = -1.0; + for (int j = 0; j < 4; ++j) + { + m_destPoly.push_back(IntPoint( + Round(m_srcPoly[0].X + X * delta), + Round(m_srcPoly[0].Y + Y * delta))); + if (X < 0) X = 1; + else if (Y < 0) Y = 1; + else X = -1; + } + } + m_destPolys.push_back(m_destPoly); + continue; + } + //build m_normals ... + m_normals.clear(); + m_normals.reserve(len); + for (int j = 0; j < len - 1; ++j) + m_normals.push_back(GetUnitNormal(m_srcPoly[j], m_srcPoly[j + 1])); + if (node.m_endtype == etClosedLine || node.m_endtype == etClosedPolygon) + m_normals.push_back(GetUnitNormal(m_srcPoly[len - 1], m_srcPoly[0])); + else + m_normals.push_back(DoublePoint(m_normals[len - 2])); + + if (node.m_endtype == etClosedPolygon) + { + int k = len - 1; + for (int j = 0; j < len; ++j) + OffsetPoint(j, k, node.m_jointype); + m_destPolys.push_back(m_destPoly); + } + else if (node.m_endtype == etClosedLine) + { + int k = len - 1; + for (int j = 0; j < len; ++j) + OffsetPoint(j, k, node.m_jointype); + m_destPolys.push_back(m_destPoly); + m_destPoly.clear(); + //re-build m_normals ... + DoublePoint n = m_normals[len -1]; + for (int j = len - 1; j > 0; j--) + m_normals[j] = DoublePoint(-m_normals[j - 1].X, -m_normals[j - 1].Y); + m_normals[0] = DoublePoint(-n.X, -n.Y); + k = 0; + for (int j = len - 1; j >= 0; j--) + OffsetPoint(j, k, node.m_jointype); + m_destPolys.push_back(m_destPoly); + } + else + { + int k = 0; + for (int j = 1; j < len - 1; ++j) + OffsetPoint(j, k, node.m_jointype); + + IntPoint pt1; + if (node.m_endtype == etOpenButt) + { + int j = len - 1; + pt1 = IntPoint((cInt)Round(m_srcPoly[j].X + m_normals[j].X * + delta), (cInt)Round(m_srcPoly[j].Y + m_normals[j].Y * delta)); + m_destPoly.push_back(pt1); + pt1 = IntPoint((cInt)Round(m_srcPoly[j].X - m_normals[j].X * + delta), (cInt)Round(m_srcPoly[j].Y - m_normals[j].Y * delta)); + m_destPoly.push_back(pt1); + } + else + { + int j = len - 1; + k = len - 2; + m_sinA = 0; + m_normals[j] = DoublePoint(-m_normals[j].X, -m_normals[j].Y); + if (node.m_endtype == etOpenSquare) + DoSquare(j, k); + else + DoRound(j, k); + } + + //re-build m_normals ... + for (int j = len - 1; j > 0; j--) + m_normals[j] = DoublePoint(-m_normals[j - 1].X, -m_normals[j - 1].Y); + m_normals[0] = DoublePoint(-m_normals[1].X, -m_normals[1].Y); + + k = len - 1; + for (int j = k - 1; j > 0; --j) OffsetPoint(j, k, node.m_jointype); + + if (node.m_endtype == etOpenButt) + { + pt1 = IntPoint((cInt)Round(m_srcPoly[0].X - m_normals[0].X * delta), + (cInt)Round(m_srcPoly[0].Y - m_normals[0].Y * delta)); + m_destPoly.push_back(pt1); + pt1 = IntPoint((cInt)Round(m_srcPoly[0].X + m_normals[0].X * delta), + (cInt)Round(m_srcPoly[0].Y + m_normals[0].Y * delta)); + m_destPoly.push_back(pt1); + } + else + { + k = 1; + m_sinA = 0; + if (node.m_endtype == etOpenSquare) + DoSquare(0, 1); + else + DoRound(0, 1); + } + m_destPolys.push_back(m_destPoly); + } + } +} +//------------------------------------------------------------------------------ + +void ClipperOffset::OffsetPoint(int j, int& k, JoinType jointype) +{ + //cross product ... + m_sinA = (m_normals[k].X * m_normals[j].Y - m_normals[j].X * m_normals[k].Y); + if (std::fabs(m_sinA * m_delta) < 1.0) + { + //dot product ... + double cosA = (m_normals[k].X * m_normals[j].X + m_normals[j].Y * m_normals[k].Y ); + if (cosA > 0) // angle => 0 degrees + { + m_destPoly.push_back(IntPoint(Round(m_srcPoly[j].X + m_normals[k].X * m_delta), + Round(m_srcPoly[j].Y + m_normals[k].Y * m_delta))); + return; + } + //else angle => 180 degrees + } + else if (m_sinA > 1.0) m_sinA = 1.0; + else if (m_sinA < -1.0) m_sinA = -1.0; + + if (m_sinA * m_delta < 0) + { + m_destPoly.push_back(IntPoint(Round(m_srcPoly[j].X + m_normals[k].X * m_delta), + Round(m_srcPoly[j].Y + m_normals[k].Y * m_delta))); + m_destPoly.push_back(m_srcPoly[j]); + m_destPoly.push_back(IntPoint(Round(m_srcPoly[j].X + m_normals[j].X * m_delta), + Round(m_srcPoly[j].Y + m_normals[j].Y * m_delta))); + } + else + switch (jointype) + { + case jtMiter: + { + double r = 1 + (m_normals[j].X * m_normals[k].X + + m_normals[j].Y * m_normals[k].Y); + if (r >= m_miterLim) DoMiter(j, k, r); else DoSquare(j, k); + break; + } + case jtSquare: DoSquare(j, k); break; + case jtRound: DoRound(j, k); break; + } + k = j; +} +//------------------------------------------------------------------------------ + +void ClipperOffset::DoSquare(int j, int k) +{ + double dx = std::tan(std::atan2(m_sinA, + m_normals[k].X * m_normals[j].X + m_normals[k].Y * m_normals[j].Y) / 4); + m_destPoly.push_back(IntPoint( + Round(m_srcPoly[j].X + m_delta * (m_normals[k].X - m_normals[k].Y * dx)), + Round(m_srcPoly[j].Y + m_delta * (m_normals[k].Y + m_normals[k].X * dx)))); + m_destPoly.push_back(IntPoint( + Round(m_srcPoly[j].X + m_delta * (m_normals[j].X + m_normals[j].Y * dx)), + Round(m_srcPoly[j].Y + m_delta * (m_normals[j].Y - m_normals[j].X * dx)))); +} +//------------------------------------------------------------------------------ + +void ClipperOffset::DoMiter(int j, int k, double r) +{ + double q = m_delta / r; + m_destPoly.push_back(IntPoint(Round(m_srcPoly[j].X + (m_normals[k].X + m_normals[j].X) * q), + Round(m_srcPoly[j].Y + (m_normals[k].Y + m_normals[j].Y) * q))); +} +//------------------------------------------------------------------------------ + +void ClipperOffset::DoRound(int j, int k) +{ + double a = std::atan2(m_sinA, + m_normals[k].X * m_normals[j].X + m_normals[k].Y * m_normals[j].Y); + int steps = (int)Round(m_StepsPerRad * std::fabs(a)); + + double X = m_normals[k].X, Y = m_normals[k].Y, X2; + for (int i = 0; i < steps; ++i) + { + m_destPoly.push_back(IntPoint( + Round(m_srcPoly[j].X + X * m_delta), + Round(m_srcPoly[j].Y + Y * m_delta))); + X2 = X; + X = X * m_cos - m_sin * Y; + Y = X2 * m_sin + Y * m_cos; + } + m_destPoly.push_back(IntPoint( + Round(m_srcPoly[j].X + m_normals[j].X * m_delta), + Round(m_srcPoly[j].Y + m_normals[j].Y * m_delta))); +} + +//------------------------------------------------------------------------------ +// Miscellaneous public functions +//------------------------------------------------------------------------------ + +void Clipper::DoSimplePolygons() +{ + PolyOutList::size_type i = 0; + while (i < m_PolyOuts.size()) + { + OutRec* outrec = m_PolyOuts[i++]; + OutPt* op = outrec->Pts; + if (!op || outrec->IsOpen) continue; + do //for each Pt in Polygon until duplicate found do ... + { + OutPt* op2 = op->Next; + while (op2 != outrec->Pts) + { + if ((op->Pt == op2->Pt) && op2->Next != op && op2->Prev != op) + { + //split the polygon into two ... + OutPt* op3 = op->Prev; + OutPt* op4 = op2->Prev; + op->Prev = op4; + op4->Next = op; + op2->Prev = op3; + op3->Next = op2; + + outrec->Pts = op; + OutRec* outrec2 = CreateOutRec(); + outrec2->Pts = op2; + UpdateOutPtIdxs(*outrec2); + if (Poly2ContainsPoly1(outrec2->Pts, outrec->Pts)) + { + //OutRec2 is contained by OutRec1 ... + outrec2->IsHole = !outrec->IsHole; + outrec2->FirstLeft = outrec; + if (m_UsingPolyTree) FixupFirstLefts2(outrec2, outrec); + } + else + if (Poly2ContainsPoly1(outrec->Pts, outrec2->Pts)) + { + //OutRec1 is contained by OutRec2 ... + outrec2->IsHole = outrec->IsHole; + outrec->IsHole = !outrec2->IsHole; + outrec2->FirstLeft = outrec->FirstLeft; + outrec->FirstLeft = outrec2; + if (m_UsingPolyTree) FixupFirstLefts2(outrec, outrec2); + } + else + { + //the 2 polygons are separate ... + outrec2->IsHole = outrec->IsHole; + outrec2->FirstLeft = outrec->FirstLeft; + if (m_UsingPolyTree) FixupFirstLefts1(outrec, outrec2); + } + op2 = op; //ie get ready for the Next iteration + } + op2 = op2->Next; + } + op = op->Next; + } + while (op != outrec->Pts); + } +} +//------------------------------------------------------------------------------ + +void ReversePath(Path& p) +{ + std::reverse(p.begin(), p.end()); +} +//------------------------------------------------------------------------------ + +void ReversePaths(Paths& p) +{ + for (Paths::size_type i = 0; i < p.size(); ++i) + ReversePath(p[i]); +} +//------------------------------------------------------------------------------ + +void SimplifyPolygon(const Path &in_poly, Paths &out_polys, PolyFillType fillType) +{ + Clipper c; + c.StrictlySimple(true); + c.AddPath(in_poly, ptSubject, true); + c.Execute(ctUnion, out_polys, fillType, fillType); +} +//------------------------------------------------------------------------------ + +void SimplifyPolygons(const Paths &in_polys, Paths &out_polys, PolyFillType fillType) +{ + Clipper c; + c.StrictlySimple(true); + c.AddPaths(in_polys, ptSubject, true); + c.Execute(ctUnion, out_polys, fillType, fillType); +} +//------------------------------------------------------------------------------ + +void SimplifyPolygons(Paths &polys, PolyFillType fillType) +{ + SimplifyPolygons(polys, polys, fillType); +} +//------------------------------------------------------------------------------ + +inline double DistanceSqrd(const IntPoint& pt1, const IntPoint& pt2) +{ + double Dx = ((double)pt1.X - pt2.X); + double dy = ((double)pt1.Y - pt2.Y); + return (Dx*Dx + dy*dy); +} +//------------------------------------------------------------------------------ + +double DistanceFromLineSqrd( + const IntPoint& pt, const IntPoint& ln1, const IntPoint& ln2) +{ + //The equation of a line in general form (Ax + By + C = 0) + //given 2 points (x¹,y¹) & (x²,y²) is ... + //(y¹ - y²)x + (x² - x¹)y + (y² - y¹)x¹ - (x² - x¹)y¹ = 0 + //A = (y¹ - y²); B = (x² - x¹); C = (y² - y¹)x¹ - (x² - x¹)y¹ + //perpendicular distance of point (x³,y³) = (Ax³ + By³ + C)/Sqrt(A² + B²) + //see http://en.wikipedia.org/wiki/Perpendicular_distance + double A = double(ln1.Y - ln2.Y); + double B = double(ln2.X - ln1.X); + double C = A * ln1.X + B * ln1.Y; + C = A * pt.X + B * pt.Y - C; + return (C * C) / (A * A + B * B); +} +//--------------------------------------------------------------------------- + +bool SlopesNearCollinear(const IntPoint& pt1, + const IntPoint& pt2, const IntPoint& pt3, double distSqrd) +{ + //this function is more accurate when the point that's geometrically + //between the other 2 points is the one that's tested for distance. + //ie makes it more likely to pick up 'spikes' ... + if (Abs(pt1.X - pt2.X) > Abs(pt1.Y - pt2.Y)) + { + if ((pt1.X > pt2.X) == (pt1.X < pt3.X)) + return DistanceFromLineSqrd(pt1, pt2, pt3) < distSqrd; + else if ((pt2.X > pt1.X) == (pt2.X < pt3.X)) + return DistanceFromLineSqrd(pt2, pt1, pt3) < distSqrd; + else + return DistanceFromLineSqrd(pt3, pt1, pt2) < distSqrd; + } + else + { + if ((pt1.Y > pt2.Y) == (pt1.Y < pt3.Y)) + return DistanceFromLineSqrd(pt1, pt2, pt3) < distSqrd; + else if ((pt2.Y > pt1.Y) == (pt2.Y < pt3.Y)) + return DistanceFromLineSqrd(pt2, pt1, pt3) < distSqrd; + else + return DistanceFromLineSqrd(pt3, pt1, pt2) < distSqrd; + } +} +//------------------------------------------------------------------------------ + +bool PointsAreClose(IntPoint pt1, IntPoint pt2, double distSqrd) +{ + double Dx = (double)pt1.X - pt2.X; + double dy = (double)pt1.Y - pt2.Y; + return ((Dx * Dx) + (dy * dy) <= distSqrd); +} +//------------------------------------------------------------------------------ + +OutPt* ExcludeOp(OutPt* op) +{ + OutPt* result = op->Prev; + result->Next = op->Next; + op->Next->Prev = result; + result->Idx = 0; + return result; +} +//------------------------------------------------------------------------------ + +void CleanPolygon(const Path& in_poly, Path& out_poly, double distance) +{ + //distance = proximity in units/pixels below which vertices + //will be stripped. Default ~= sqrt(2). + + size_t size = in_poly.size(); + + if (size == 0) + { + out_poly.clear(); + return; + } + + OutPt* outPts = new OutPt[size]; + for (size_t i = 0; i < size; ++i) + { + outPts[i].Pt = in_poly[i]; + outPts[i].Next = &outPts[(i + 1) % size]; + outPts[i].Next->Prev = &outPts[i]; + outPts[i].Idx = 0; + } + + double distSqrd = distance * distance; + OutPt* op = &outPts[0]; + while (op->Idx == 0 && op->Next != op->Prev) + { + if (PointsAreClose(op->Pt, op->Prev->Pt, distSqrd)) + { + op = ExcludeOp(op); + size--; + } + else if (PointsAreClose(op->Prev->Pt, op->Next->Pt, distSqrd)) + { + ExcludeOp(op->Next); + op = ExcludeOp(op); + size -= 2; + } + else if (SlopesNearCollinear(op->Prev->Pt, op->Pt, op->Next->Pt, distSqrd)) + { + op = ExcludeOp(op); + size--; + } + else + { + op->Idx = 1; + op = op->Next; + } + } + + if (size < 3) size = 0; + out_poly.resize(size); + for (size_t i = 0; i < size; ++i) + { + out_poly[i] = op->Pt; + op = op->Next; + } + delete [] outPts; +} +//------------------------------------------------------------------------------ + +void CleanPolygon(Path& poly, double distance) +{ + CleanPolygon(poly, poly, distance); +} +//------------------------------------------------------------------------------ + +void CleanPolygons(const Paths& in_polys, Paths& out_polys, double distance) +{ + for (Paths::size_type i = 0; i < in_polys.size(); ++i) + CleanPolygon(in_polys[i], out_polys[i], distance); +} +//------------------------------------------------------------------------------ + +void CleanPolygons(Paths& polys, double distance) +{ + CleanPolygons(polys, polys, distance); +} +//------------------------------------------------------------------------------ + +void Minkowski(const Path& poly, const Path& path, + Paths& solution, bool isSum, bool isClosed) +{ + int delta = (isClosed ? 1 : 0); + size_t polyCnt = poly.size(); + size_t pathCnt = path.size(); + Paths pp; + pp.reserve(pathCnt); + if (isSum) + for (size_t i = 0; i < pathCnt; ++i) + { + Path p; + p.reserve(polyCnt); + for (size_t j = 0; j < poly.size(); ++j) + p.push_back(IntPoint(path[i].X + poly[j].X, path[i].Y + poly[j].Y)); + pp.push_back(p); + } + else + for (size_t i = 0; i < pathCnt; ++i) + { + Path p; + p.reserve(polyCnt); + for (size_t j = 0; j < poly.size(); ++j) + p.push_back(IntPoint(path[i].X - poly[j].X, path[i].Y - poly[j].Y)); + pp.push_back(p); + } + + solution.clear(); + solution.reserve((pathCnt + delta) * (polyCnt + 1)); + for (size_t i = 0; i < pathCnt - 1 + delta; ++i) + for (size_t j = 0; j < polyCnt; ++j) + { + Path quad; + quad.reserve(4); + quad.push_back(pp[i % pathCnt][j % polyCnt]); + quad.push_back(pp[(i + 1) % pathCnt][j % polyCnt]); + quad.push_back(pp[(i + 1) % pathCnt][(j + 1) % polyCnt]); + quad.push_back(pp[i % pathCnt][(j + 1) % polyCnt]); + if (!Orientation(quad)) ReversePath(quad); + solution.push_back(quad); + } +} +//------------------------------------------------------------------------------ + +void MinkowskiSum(const Path& pattern, const Path& path, Paths& solution, bool pathIsClosed) +{ + Minkowski(pattern, path, solution, true, pathIsClosed); + Clipper c; + c.AddPaths(solution, ptSubject, true); + c.Execute(ctUnion, solution, pftNonZero, pftNonZero); +} +//------------------------------------------------------------------------------ + +void TranslatePath(const Path& input, Path& output, IntPoint delta) +{ + //precondition: input != output + output.resize(input.size()); + for (size_t i = 0; i < input.size(); ++i) + output[i] = IntPoint(input[i].X + delta.X, input[i].Y + delta.Y); +} +//------------------------------------------------------------------------------ + +void MinkowskiSum(const Path& pattern, const Paths& paths, Paths& solution, bool pathIsClosed) +{ + Clipper c; + for (size_t i = 0; i < paths.size(); ++i) + { + Paths tmp; + Minkowski(pattern, paths[i], tmp, true, pathIsClosed); + c.AddPaths(tmp, ptSubject, true); + if (pathIsClosed) + { + Path tmp2; + TranslatePath(paths[i], tmp2, pattern[0]); + c.AddPath(tmp2, ptClip, true); + } + } + c.Execute(ctUnion, solution, pftNonZero, pftNonZero); +} +//------------------------------------------------------------------------------ + +void MinkowskiDiff(const Path& poly1, const Path& poly2, Paths& solution) +{ + Minkowski(poly1, poly2, solution, false, true); + Clipper c; + c.AddPaths(solution, ptSubject, true); + c.Execute(ctUnion, solution, pftNonZero, pftNonZero); +} +//------------------------------------------------------------------------------ + +enum NodeType {ntAny, ntOpen, ntClosed}; + +void AddPolyNodeToPaths(const PolyNode& polynode, NodeType nodetype, Paths& paths) +{ + bool match = true; + if (nodetype == ntClosed) match = !polynode.IsOpen(); + else if (nodetype == ntOpen) return; + + if (!polynode.Contour.empty() && match) + paths.push_back(polynode.Contour); + for (int i = 0; i < polynode.ChildCount(); ++i) + AddPolyNodeToPaths(*polynode.Childs[i], nodetype, paths); +} +//------------------------------------------------------------------------------ + +void PolyTreeToPaths(const PolyTree& polytree, Paths& paths) +{ + paths.resize(0); + paths.reserve(polytree.Total()); + AddPolyNodeToPaths(polytree, ntAny, paths); +} +//------------------------------------------------------------------------------ + +void ClosedPathsFromPolyTree(const PolyTree& polytree, Paths& paths) +{ + paths.resize(0); + paths.reserve(polytree.Total()); + AddPolyNodeToPaths(polytree, ntClosed, paths); +} +//------------------------------------------------------------------------------ + +void OpenPathsFromPolyTree(PolyTree& polytree, Paths& paths) +{ + paths.resize(0); + paths.reserve(polytree.Total()); + //Open paths are top level only, so ... + for (int i = 0; i < polytree.ChildCount(); ++i) + if (polytree.Childs[i]->IsOpen()) + paths.push_back(polytree.Childs[i]->Contour); +} +//------------------------------------------------------------------------------ + +std::ostream& operator <<(std::ostream &s, const IntPoint &p) +{ + s << "(" << p.X << "," << p.Y << ")"; + return s; +} +//------------------------------------------------------------------------------ + +std::ostream& operator <<(std::ostream &s, const Path &p) +{ + if (p.empty()) return s; + Path::size_type last = p.size() -1; + for (Path::size_type i = 0; i < last; i++) + s << "(" << p[i].X << "," << p[i].Y << "), "; + s << "(" << p[last].X << "," << p[last].Y << ")\n"; + return s; +} +//------------------------------------------------------------------------------ + +std::ostream& operator <<(std::ostream &s, const Paths &p) +{ + for (Paths::size_type i = 0; i < p.size(); i++) + s << p[i]; + s << "\n"; + return s; +} +//------------------------------------------------------------------------------ + +} //ClipperLib namespace diff --git a/src/Mod/Path/libarea/clipper.hpp b/src/Mod/Path/libarea/clipper.hpp new file mode 100644 index 0000000000..e83e70a959 --- /dev/null +++ b/src/Mod/Path/libarea/clipper.hpp @@ -0,0 +1,395 @@ +/******************************************************************************* +* * +* Author : Angus Johnson * +* Version : 6.2.0 * +* Date : 2 October 2014 * +* Website : http://www.angusj.com * +* Copyright : Angus Johnson 2010-2014 * +* * +* License: * +* Use, modification & distribution is subject to Boost Software License Ver 1. * +* http://www.boost.org/LICENSE_1_0.txt * +* * +* Attributions: * +* The code in this library is an extension of Bala Vatti's clipping algorithm: * +* "A generic solution to polygon clipping" * +* Communications of the ACM, Vol 35, Issue 7 (July 1992) pp 56-63. * +* http://portal.acm.org/citation.cfm?id=129906 * +* * +* Computer graphics and geometric modeling: implementation and algorithms * +* By Max K. Agoston * +* Springer; 1 edition (January 4, 2005) * +* http://books.google.com/books?q=vatti+clipping+agoston * +* * +* See also: * +* "Polygon Offsetting by Computing Winding Numbers" * +* Paper no. DETC2005-85513 pp. 565-575 * +* ASME 2005 International Design Engineering Technical Conferences * +* and Computers and Information in Engineering Conference (IDETC/CIE2005) * +* September 24-28, 2005 , Long Beach, California, USA * +* http://www.me.berkeley.edu/~mcmains/pubs/DAC05OffsetPolygon.pdf * +* * +*******************************************************************************/ + +#ifndef clipper_hpp +#define clipper_hpp + +#define CLIPPER_VERSION "6.2.0" + +//use_int32: When enabled 32bit ints are used instead of 64bit ints. This +//improve performance but coordinate values are limited to the range +/- 46340 +//#define use_int32 + +//use_xyz: adds a Z member to IntPoint. Adds a minor cost to perfomance. +//#define use_xyz + +//use_lines: Enables line clipping. Adds a very minor cost to performance. +//#define use_lines + +//use_deprecated: Enables temporary support for the obsolete functions +//#define use_deprecated + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace ClipperLib { + +enum ClipType { ctIntersection, ctUnion, ctDifference, ctXor }; +enum PolyType { ptSubject, ptClip }; +//By far the most widely used winding rules for polygon filling are +//EvenOdd & NonZero (GDI, GDI+, XLib, OpenGL, Cairo, AGG, Quartz, SVG, Gr32) +//Others rules include Positive, Negative and ABS_GTR_EQ_TWO (only in OpenGL) +//see http://glprogramming.com/red/chapter11.html +enum PolyFillType { pftEvenOdd, pftNonZero, pftPositive, pftNegative }; + +#ifdef use_int32 + typedef int cInt; + static cInt const loRange = 0x7FFF; + static cInt const hiRange = 0x7FFF; +#else + typedef signed long long cInt; + static cInt const loRange = 0x3FFFFFFF; + static cInt const hiRange = 0x3FFFFFFFFFFFFFFFLL; + typedef signed long long long64; //used by Int128 class + typedef unsigned long long ulong64; + +#endif + +struct IntPoint { + cInt X; + cInt Y; +#ifdef use_xyz + cInt Z; + IntPoint(cInt x = 0, cInt y = 0, cInt z = 0): X(x), Y(y), Z(z) {}; +#else + IntPoint(cInt x = 0, cInt y = 0): X(x), Y(y) {}; +#endif + + friend inline bool operator== (const IntPoint& a, const IntPoint& b) + { + return a.X == b.X && a.Y == b.Y; + } + friend inline bool operator!= (const IntPoint& a, const IntPoint& b) + { + return a.X != b.X || a.Y != b.Y; + } +}; +//------------------------------------------------------------------------------ + +typedef std::vector< IntPoint > Path; +typedef std::vector< Path > Paths; + +inline Path& operator <<(Path& poly, const IntPoint& p) {poly.push_back(p); return poly;} +inline Paths& operator <<(Paths& polys, const Path& p) {polys.push_back(p); return polys;} + +std::ostream& operator <<(std::ostream &s, const IntPoint &p); +std::ostream& operator <<(std::ostream &s, const Path &p); +std::ostream& operator <<(std::ostream &s, const Paths &p); + +struct DoublePoint +{ + double X; + double Y; + DoublePoint(double x = 0, double y = 0) : X(x), Y(y) {} + DoublePoint(IntPoint ip) : X((double)ip.X), Y((double)ip.Y) {} +}; +//------------------------------------------------------------------------------ + +#ifdef use_xyz +typedef void (*ZFillCallback)(IntPoint& e1bot, IntPoint& e1top, IntPoint& e2bot, IntPoint& e2top, IntPoint& pt); +#endif + +enum InitOptions {ioReverseSolution = 1, ioStrictlySimple = 2, ioPreserveCollinear = 4}; +enum JoinType {jtSquare, jtRound, jtMiter}; +enum EndType {etClosedPolygon, etClosedLine, etOpenButt, etOpenSquare, etOpenRound}; + +class PolyNode; +typedef std::vector< PolyNode* > PolyNodes; + +class PolyNode +{ +public: + PolyNode(); + virtual ~PolyNode(){}; + Path Contour; + PolyNodes Childs; + PolyNode* Parent; + PolyNode* GetNext() const; + bool IsHole() const; + bool IsOpen() const; + int ChildCount() const; +private: + unsigned Index; //node index in Parent.Childs + bool m_IsOpen; + JoinType m_jointype; + EndType m_endtype; + PolyNode* GetNextSiblingUp() const; + void AddChild(PolyNode& child); + friend class Clipper; //to access Index + friend class ClipperOffset; +}; + +class PolyTree: public PolyNode +{ +public: + ~PolyTree(){Clear();}; + PolyNode* GetFirst() const; + void Clear(); + int Total() const; +private: + PolyNodes AllNodes; + friend class Clipper; //to access AllNodes +}; + +bool Orientation(const Path &poly); +double Area(const Path &poly); +int PointInPolygon(const IntPoint &pt, const Path &path); + +void SimplifyPolygon(const Path &in_poly, Paths &out_polys, PolyFillType fillType = pftEvenOdd); +void SimplifyPolygons(const Paths &in_polys, Paths &out_polys, PolyFillType fillType = pftEvenOdd); +void SimplifyPolygons(Paths &polys, PolyFillType fillType = pftEvenOdd); + +void CleanPolygon(const Path& in_poly, Path& out_poly, double distance = 1.415); +void CleanPolygon(Path& poly, double distance = 1.415); +void CleanPolygons(const Paths& in_polys, Paths& out_polys, double distance = 1.415); +void CleanPolygons(Paths& polys, double distance = 1.415); + +void MinkowskiSum(const Path& pattern, const Path& path, Paths& solution, bool pathIsClosed); +void MinkowskiSum(const Path& pattern, const Paths& paths, Paths& solution, bool pathIsClosed); +void MinkowskiDiff(const Path& poly1, const Path& poly2, Paths& solution); + +void PolyTreeToPaths(const PolyTree& polytree, Paths& paths); +void ClosedPathsFromPolyTree(const PolyTree& polytree, Paths& paths); +void OpenPathsFromPolyTree(PolyTree& polytree, Paths& paths); + +void ReversePath(Path& p); +void ReversePaths(Paths& p); + +struct IntRect { cInt left; cInt top; cInt right; cInt bottom; }; + +//enums that are used internally ... +enum EdgeSide { esLeft = 1, esRight = 2}; + +//forward declarations (for stuff used internally) ... +struct TEdge; +struct IntersectNode; +struct LocalMinimum; +struct Scanbeam; +struct OutPt; +struct OutRec; +struct Join; + +typedef std::vector < OutRec* > PolyOutList; +typedef std::vector < TEdge* > EdgeList; +typedef std::vector < Join* > JoinList; +typedef std::vector < IntersectNode* > IntersectList; + +//------------------------------------------------------------------------------ + +//ClipperBase is the ancestor to the Clipper class. It should not be +//instantiated directly. This class simply abstracts the conversion of sets of +//polygon coordinates into edge objects that are stored in a LocalMinima list. +class ClipperBase +{ +public: + ClipperBase(); + virtual ~ClipperBase(); + bool AddPath(const Path &pg, PolyType PolyTyp, bool Closed); + bool AddPaths(const Paths &ppg, PolyType PolyTyp, bool Closed); + virtual void Clear(); + IntRect GetBounds(); + bool PreserveCollinear() {return m_PreserveCollinear;}; + void PreserveCollinear(bool value) {m_PreserveCollinear = value;}; +protected: + void DisposeLocalMinimaList(); + TEdge* AddBoundsToLML(TEdge *e, bool IsClosed); + void PopLocalMinima(); + virtual void Reset(); + TEdge* ProcessBound(TEdge* E, bool IsClockwise); + void DoMinimaLML(TEdge* E1, TEdge* E2, bool IsClosed); + TEdge* DescendToMin(TEdge *&E); + void AscendToMax(TEdge *&E, bool Appending, bool IsClosed); + + typedef std::vector MinimaList; + MinimaList::iterator m_CurrentLM; + MinimaList m_MinimaList; + + bool m_UseFullRange; + EdgeList m_edges; + bool m_PreserveCollinear; + bool m_HasOpenPaths; +}; +//------------------------------------------------------------------------------ + +class Clipper : public virtual ClipperBase +{ +public: + Clipper(int initOptions = 0); + ~Clipper(); + bool Execute(ClipType clipType, + Paths &solution, + PolyFillType subjFillType = pftEvenOdd, + PolyFillType clipFillType = pftEvenOdd); + bool Execute(ClipType clipType, + PolyTree &polytree, + PolyFillType subjFillType = pftEvenOdd, + PolyFillType clipFillType = pftEvenOdd); + bool ReverseSolution() {return m_ReverseOutput;}; + void ReverseSolution(bool value) {m_ReverseOutput = value;}; + bool StrictlySimple() {return m_StrictSimple;}; + void StrictlySimple(bool value) {m_StrictSimple = value;}; + //set the callback function for z value filling on intersections (otherwise Z is 0) +#ifdef use_xyz + void ZFillFunction(ZFillCallback zFillFunc); +#endif +protected: + void Reset(); + virtual bool ExecuteInternal(); +private: + PolyOutList m_PolyOuts; + JoinList m_Joins; + JoinList m_GhostJoins; + IntersectList m_IntersectList; + ClipType m_ClipType; + typedef std::priority_queue ScanbeamList; + ScanbeamList m_Scanbeam; + TEdge *m_ActiveEdges; + TEdge *m_SortedEdges; + bool m_ExecuteLocked; + PolyFillType m_ClipFillType; + PolyFillType m_SubjFillType; + bool m_ReverseOutput; + bool m_UsingPolyTree; + bool m_StrictSimple; +#ifdef use_xyz + ZFillCallback m_ZFill; //custom callback +#endif + void SetWindingCount(TEdge& edge); + bool IsEvenOddFillType(const TEdge& edge) const; + bool IsEvenOddAltFillType(const TEdge& edge) const; + void InsertScanbeam(const cInt Y); + cInt PopScanbeam(); + void InsertLocalMinimaIntoAEL(const cInt botY); + void InsertEdgeIntoAEL(TEdge *edge, TEdge* startEdge); + void AddEdgeToSEL(TEdge *edge); + void CopyAELToSEL(); + void DeleteFromSEL(TEdge *e); + void DeleteFromAEL(TEdge *e); + void UpdateEdgeIntoAEL(TEdge *&e); + void SwapPositionsInSEL(TEdge *edge1, TEdge *edge2); + bool IsContributing(const TEdge& edge) const; + bool IsTopHorz(const cInt XPos); + void SwapPositionsInAEL(TEdge *edge1, TEdge *edge2); + void DoMaxima(TEdge *e); + void ProcessHorizontals(bool IsTopOfScanbeam); + void ProcessHorizontal(TEdge *horzEdge, bool isTopOfScanbeam); + void AddLocalMaxPoly(TEdge *e1, TEdge *e2, const IntPoint &pt); + OutPt* AddLocalMinPoly(TEdge *e1, TEdge *e2, const IntPoint &pt); + OutRec* GetOutRec(int idx); + void AppendPolygon(TEdge *e1, TEdge *e2); + void IntersectEdges(TEdge *e1, TEdge *e2, IntPoint &pt); + OutRec* CreateOutRec(); + OutPt* AddOutPt(TEdge *e, const IntPoint &pt); + void DisposeAllOutRecs(); + void DisposeOutRec(PolyOutList::size_type index); + bool ProcessIntersections(const cInt botY, const cInt topY); + void BuildIntersectList(const cInt botY, const cInt topY); + void ProcessIntersectList(); + void ProcessEdgesAtTopOfScanbeam(const cInt topY); + void BuildResult(Paths& polys); + void BuildResult2(PolyTree& polytree); + void SetHoleState(TEdge *e, OutRec *outrec); + void DisposeIntersectNodes(); + bool FixupIntersectionOrder(); + void FixupOutPolygon(OutRec &outrec); + bool IsHole(TEdge *e); + bool FindOwnerFromSplitRecs(OutRec &outRec, OutRec *&currOrfl); + void FixHoleLinkage(OutRec &outrec); + void AddJoin(OutPt *op1, OutPt *op2, const IntPoint offPt); + void ClearJoins(); + void ClearGhostJoins(); + void AddGhostJoin(OutPt *op, const IntPoint offPt); + bool JoinPoints(Join *j, OutRec* outRec1, OutRec* outRec2); + void JoinCommonEdges(); + void DoSimplePolygons(); + void FixupFirstLefts1(OutRec* OldOutRec, OutRec* NewOutRec); + void FixupFirstLefts2(OutRec* OldOutRec, OutRec* NewOutRec); +#ifdef use_xyz + void SetZ(IntPoint& pt, TEdge& e1, TEdge& e2); +#endif +}; +//------------------------------------------------------------------------------ + +class ClipperOffset +{ +public: + ClipperOffset(double miterLimit = 2.0, double roundPrecision = 0.25); + ~ClipperOffset(); + void AddPath(const Path& path, JoinType joinType, EndType endType); + void AddPaths(const Paths& paths, JoinType joinType, EndType endType); + void Execute(Paths& solution, double delta); + void Execute(PolyTree& solution, double delta); + void Clear(); + double MiterLimit; + double ArcTolerance; +private: + Paths m_destPolys; + Path m_srcPoly; + Path m_destPoly; + std::vector m_normals; + double m_delta, m_sinA, m_sin, m_cos; + double m_miterLim, m_StepsPerRad; + IntPoint m_lowest; + PolyNode m_polyNodes; + + void FixOrientations(); + void DoOffset(double delta); + void OffsetPoint(int j, int& k, JoinType jointype); + void DoSquare(int j, int k); + void DoMiter(int j, int k, double r); + void DoRound(int j, int k); +}; +//------------------------------------------------------------------------------ + +class clipperException : public std::exception +{ + public: + clipperException(const char* description): m_descr(description) {} + virtual ~clipperException() throw() {} + virtual const char* what() const throw() {return m_descr.c_str();} + private: + std::string m_descr; +}; +//------------------------------------------------------------------------------ + +} //ClipperLib namespace + +#endif //clipper_hpp + + diff --git a/src/Mod/Path/libarea/kurve/Construction.cpp b/src/Mod/Path/libarea/kurve/Construction.cpp new file mode 100644 index 0000000000..7d387ee612 --- /dev/null +++ b/src/Mod/Path/libarea/kurve/Construction.cpp @@ -0,0 +1,868 @@ +// *************************************************************************************************************************************** +// Point, CLine & Circle classes part of geometry.lib +// *************************************************************************************************************************************** + +/*============================== +Copyright (c) 2006 g.j.hawkesford + +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. +3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. +==============================*/ + + + +#include "geometry.h" +using namespace geoff_geometry; + +namespace geoff_geometry { + int UNITS = MM; + double TOLERANCE = 1.0e-06; + double TOLERANCE_SQ = TOLERANCE * TOLERANCE; + double TIGHT_TOLERANCE = 1.0e-09; + double UNIT_VECTOR_TOLERANCE = 1.0e-10; + double RESOLUTION = 1.0e-06; + + // dummy functions + const wchar_t* getMessage(const wchar_t* original, int messageGroup, int stringID){return original;} + const wchar_t* getMessage(const wchar_t* original){return original;} + void FAILURE(const wchar_t* str){throw(str);} + void FAILURE(const std::wstring& str){throw(str);} + + void set_Tolerances(int mode) { + UNIT_VECTOR_TOLERANCE = 1.0e-10; + switch (UNITS = mode) + { + case MM: + geoff_geometry::TOLERANCE = 1.0e-03; // Peps + RESOLUTION = 1.0e-03; + TIGHT_TOLERANCE = 1.0e-06; + break; + case INCHES: + TOLERANCE = 1.0e-04; // Peps + RESOLUTION = 1.0e-04; + TIGHT_TOLERANCE = 1.0e-7; + break; + case METRES: + TOLERANCE = 1.0e-06; // p4c...SW + RESOLUTION = 1.0e-06; + TIGHT_TOLERANCE = 1.0e-09; + break; + default: + FAILURE(L"INVALID UNITS"); + } + TOLERANCE_SQ = TOLERANCE * TOLERANCE; + } + + double mm(double value) { + switch(UNITS) { + default: + return value; + case METRES: + return value * .001; + case INCHES: + return value / 25.4; + } + } + + // ostream operators = non-member overload + // ********************************************************************************************************* + wostream& operator << (wostream& op, Point& p){ + // for debug - print point to file + if(p.ok == false) + op << L" ok=\"false\""; + else + op << L" x=\"" << p.x << L"\" y=\"" << p.y << L"\""; + return op; + } + + wostream& operator <<(wostream& op, CLine& cl){ + // for debug - print cline to file + if(cl.ok == false) + op << L"(CLine UNSET)"; + else + op << L"sp=" << cl.p << L" v=" << cl.v; + return op; + } + + wostream& operator <<(wostream& op, Plane& pl){ + // for debug - print plane to file stream + if(pl.ok == false) + op << L"(Plane UNSET)"; + else + op << L"d=" << pl.d << L" normal=" << pl.normal; + return op; + } + + ostream& operator << (ostream& op, Point3d& p){ + // for debug - print point to file +// if(p.ok == false) +// op << "ok=\"false\""; +// else + op << "x=\"" << p.x << "\" y=\"" << p.y << "\" z=" << p.z << "\""; + return op; + + } + + wostream& operator <<(wostream& op, Vector2d& v){ + // for debug - print vector to file + op << L"(" << v.getx() << L", " << v.gety() << L")"; + return op; + } + + wostream& operator <<(wostream& op, Vector3d& v){ + // for debug - print vector to file + op << L"(" << v.getx() << L", " << v.gety() << L"," << v.getz() << L")"; + return op; + } + + wostream& operator <<(wostream& op, Circle& c){ + // for debug - print circle to file + if(c.ok == false) + op << L"ok=\"false\""; + else + op << L" x=\"" << c.pc.x << L"\" y=\"" << c.pc.y << L"\" radius=\"" << c.radius << L"\""; + return op; + } + + wostream& operator <<(wostream& op, Span& sp){ + // for debug - print span to file stream + op << L"p0 = " << sp.p0 << L" p1=" << sp.p1; + if(sp.dir) { + op << L" pc=" << sp.pc << L" dir=" << ((sp.dir == CW)?L"CW" : L"ACW") << L" radius=" << sp.radius; + } + return op; + } + + + + // *************************************************************************************************************************************** + // point classes + // *************************************************************************************************************************************** + Point::Point( const Point3d& p ) { // copy constructor Point p1(p2); + x = p.x; + y = p.y; +// ok = p.ok; + ok = true; + } + + Point::Point(const Vector2d& v) + { + x = v.getx(); y = v.gety(); + } + + Point3d::Point3d(const Vector3d& v) { + x = v.getx(); y = v.gety(); z = v.getz();// ok = true; + } + + bool Point3d::operator==(const Point3d &p)const{ + // p1 == p2 (uses TOLERANCE) + if(FNE(this->x, p.x, TOLERANCE) || FNE(this->y, p.y, TOLERANCE) || FNE(this->z, p.z, TOLERANCE)) return false; + return true; + } + + Point Point::Transform(const Matrix& m) { + // transform Point + Point ret; + m.Transform2d(&x, &ret.x); + ret.ok = true; + return ret; + } + Point3d Point3d::Transform(const Matrix& m) { + // transform Point + Point3d ret; + m.Transform(&x, &ret.x); +// ret.ok = true; + return ret; + } + + Point Point::operator+(const Vector2d &v)const{ + return Point(x + v.getx(), y + v.gety()); + } + + Point3d Point3d::operator+(const Vector3d &v)const{ + return Point3d(x + v.getx(), y + v.gety(), z + v.getz()); + } + + bool Point::operator==(const Point &p) const{ + // p1 == p2 (uses TOLERANCE) + if(FNE(this->x, p.x, TOLERANCE) || FNE(this->y, p.y, TOLERANCE)) return false; + return true; + } + + + + double Point::Dist(const Point& p)const{ // distance between 2 points + return Vector2d(*this, p).magnitude(); + } + + double Point::DistSq(const Point& p)const{ // distance squared between 2 points + return Vector2d(*this, p).magnitudesqd(); + } + + double Point3d::Dist(const Point3d& p)const { // distance between 2 points + return Vector3d(*this, p).magnitude(); + } + + double Point3d::DistSq(const Point3d& p)const { // distance squared + return (this->x - p.x) * (this->x - p.x) + (this->y - p.y) * (this->y - p.y) + (this->z - p.z) * (this->z - p.z); + } + + Point Point::Mid(const Point& p1, double factor)const{ + // Mid + return geoff_geometry::Mid(*this, p1, factor); + } + + Point3d Point3d::Mid(const Point3d& p, double factor)const{ + // Mid + return Vector3d(*this, p) * factor + *this; + } + + Point Mid(const Point& p0, const Point& p1, double factor){ + // mid or partway between 2 points + return Vector2d(p0, p1) * factor + p0; + } + Point Rel(const Point& p, double x0, double y0) { + // Relative point + return (p.ok)?Point(p.x + x0, p.y + y0) : INVALID_POINT; + } + + Point Polar(const Point& p, double angle, double r) { + // polar from this point + angle *= DegreesToRadians; + return (p.ok)?Point(p.x + r * cos(angle), p.y + r * sin(angle)) : INVALID_POINT; + } + + // *************************************************************************************************************************************** + // clines + // *************************************************************************************************************************************** + //const CLine horiz(Point(0, 0), 1, 0); // define global horizontal line + + double CLine::c() { + // returns c for ax + by + c = 0 format (peps format where needed) + return (v.getx() * p.y - v.gety() * p.x); + } + void CLine::Normalise() { + // normalise the cline vector + ok = v.normalise() >= TOLERANCE; + } + + CLine::CLine(const Span& sp){ + p = sp.p0; + v = sp.vs; + ok = sp.returnSpanProperties && !sp.NullSpan; + } + + CLine Normal(const CLine& s) { + // returns normal to this line + return CLine(s.p, ~s.v, false); + } + const CLine CLine::operator ~(void){ + return CLine(this->p, ~v, false); + } + CLine Normal(const CLine& s, const Point& p) { + // returns normal to this line thro' p + return CLine(p, ~s.v, false); + } + + CLine CLine::Transform(Matrix& m) { + + Point p0 = this->p; + Point p1(p0.x + v.getx(), p0.y + v.gety()); + return CLine(p0.Transform(m), p1.Transform(m)); + } + + + double CLine::Dist(const Point& p0)const { + // distance between cline & point >0 cw about point <0 acw about point + return this->v ^ Vector2d(p0, this->p); + } + + double Point::Dist(const CLine& cl)const { + // distance between cline & point >0 cw about point <0 acw about point + return cl.v ^ Vector2d(*this, cl.p); + } + + Point CLine::Intof(const CLine& s) { + // Intof 2 Clines + return geoff_geometry::Intof(*this, s); + } + + Point CLine::Intof(int NF, const Circle& c) { + // Intof Cline & Circleconst + return geoff_geometry::Intof(NF, *this, c); + } + Point CLine::Intof(int NF, const Circle& c, Point& otherInters) { + // Intof Cline & Circle & other intersection + return geoff_geometry::Intof(NF, *this, c, otherInters); + } + + Point Intof(const CLine& s0, const CLine& s1) { + // inters of 2 clines (parameterise lines x = x0 + t * dx) + double cp = s1.v ^ s0.v; + if(fabs (cp) > 1.0e-6) { + double t = (s1.v ^ Vector2d(s0.p, s1.p)) / cp; + return s0.v * t + s0.p; + } + return INVALID_POINT; + } + Point XonCLine(CLine& s, double xval) { + // return point given X on a line + return Intof(s, CLine(Point(xval,0),0,1,false)); + } + Point YonCLine(CLine& s, double yval) { + // return point given Y on a line + return Intof(s, CLine(Point(0,yval),1,0,false)); + } + Point Along(const CLine& s, double d) { + // distance along line + return Point(s.p.x + d * s.v.getx(), s.p.y + d * s.v.gety(), s.ok); + } + + Point Along(const CLine& s, double d, Point& p) { + // distance along line from point + return Point(p.x + d * s.v.getx(), p.y + d * s.v.gety(), p.ok); + } + Point Around(const Circle& c, double d, const Point& p) { + // distance around circle from point + CLine radial(c.pc, p); + if(radial.ok) { + if(fabs(c.radius) > TOLERANCE ) { + double a = sin(- d / c.radius); + double b = cos(- d / c.radius); + return Point(c.pc.x - c.radius * (radial.v.gety() * a - radial.v.getx() * b), c.pc.y + c.radius * (radial.v.gety() * b + radial.v.getx() * a)); + } + } + return INVALID_POINT; + } + CLine AtAngle(double angle, const Point& p0, const CLine& s) { + // cline at angle [to a cline] thro' a point + angle *= DegreesToRadians; + Vector2d v(cos(angle), sin(angle)); + return CLine(p0, v.getx() * s.v.getx() - v.gety() * s.v.gety(), v.gety() * s.v.getx() + v.getx() * s.v.gety()); + } + CLine Parallel(int side, const CLine& s0, double distance) { + // parallel to line by distance + Vector2d v = ~s0.v; + return CLine(v * ((double)side * distance) + s0.p, s0.v.getx(), s0.v.gety()); + } + + CLine Parallel(const CLine& s0, Point& p) { + // parallel to line through point + return CLine(p, s0.v.getx(), s0.v.gety()); + } + + CLine CLine::Bisector(const CLine& s) { + // bisector of 2 clines + return CLine (this->Intof(s), this->v.getx() + s.v.getx(), this->v.gety() + s.v.gety()); + } + + + + + // *************************************************************************************************************************************** + // circle methods + // *************************************************************************************************************************************** + + Circle::Circle(const Point& p, double rad, bool okay){ + // Circle + pc = p; + radius = rad; + ok = pc.ok; + } + + Circle::Circle( const Point& p, const Point& pc0){ + if((ok = (p.ok && pc0.ok))) { + pc = pc0; + radius = p.Dist(pc0); + } + } + + Circle::Circle( const Span& sp){ + pc = sp.pc; + radius = sp.radius; + ok = sp.returnSpanProperties; + } + + bool Circle::operator==(const Circle &c)const{ + // c1 == c2 (uses TOLERANCE) + return FEQ(this->radius, c.radius, TOLERANCE) && (this->pc == c.pc); + } + + Circle Circle::Transform(Matrix& m) { // transform + Point p0 = this->pc; + double scale; + if(m.GetScale(scale) == false) FAILURE(getMessage(L"Differential Scale not allowed for this method", GEOMETRY_ERROR_MESSAGES, MES_DIFFSCALE)); + return Circle(p0.Transform(m), radius * scale); + } + + Point Circle::Intof(int LR, const Circle& c1) { + // intof 2 circles + return geoff_geometry::Intof(LR, *this, c1); + } + Point Circle::Intof(int LR, const Circle& c1, Point& otherInters) { + // intof 2 circles, (returns the other intersection) + return geoff_geometry::Intof(LR, *this, c1, otherInters); + } + int Circle::Intof(const Circle& c1, Point& leftInters, Point& rightInters) { + // intof 2 circles, (returns the other intersection) + return geoff_geometry::Intof(*this, c1, leftInters, rightInters); + } + + CLine Circle::Tanto(int AT, double angle, const CLine& s0) const{ + // cline tanto circle at angle to optional cline + return geoff_geometry::Tanto(AT, *this, angle, s0); + } + + CLine Tanto(int AT, const Circle& c, const Point& p) { + // CLine tangent to a circle through a point + Vector2d v(p, c.pc); + double d = v.magnitude(); + CLine s(p, ~v, false); // initialise cline + + if ( d < TOLERANCE || d < fabs(c.radius) - TOLERANCE) // point inside circle ? + return INVALID_CLINE; + else { + if(d > fabs(c.radius) + TOLERANCE) { // point outside circle + v.Rotate(sqrt((d - c.radius) * (d + c.radius)), - AT * c.radius); + s.v = v; + } + } + s.Normalise(); + return s; + } + + CLine Tanto(int AT0, const Circle& c0, int AT1, const Circle& c) { + // cline tanto 2 circles + CLine s; + Circle c1 = c; + c1.radius -= (double) (AT0 * AT1) * c0.radius; + s = Tanto(AT1, c1, c0.pc); + s.p.x += (double) AT0 * c0.radius * s.v.gety(); + s.p.y -= (double) AT0 * c0.radius * s.v.getx(); + return s; + } + + CLine Tanto(int AT, const Circle& c, double angle, const CLine& s0) { + // cline at an angle [to a cline] tanto a circle + CLine s = AtAngle(angle, c.pc, s0); + s.p.x += (double) AT * c.radius * s.v.gety(); + s.p.y -= (double) AT * c.radius * s.v.getx(); + // s.p += ~s.v * (AT * c.radius); + s.ok = true; + return s; + } + Point AtAngle(const Circle& c, double angle) { + // Point at an angle on circle + angle *= DegreesToRadians; + return Point(c.pc.x + c.radius * cos(angle), c.pc.y + c.radius * sin(angle)); + } + + Point On(const CLine& s, const Point& p) { + // returns point that is nearest to s from p + double t = s.v * Vector2d(s.p, p); + return s.v * t + s.p; + } + + Point On(const Circle& c, const Point& p) { + // returns point that is nearest to c from p + double r = p.Dist(c.pc); + if(r < TOLERANCE) FAILURE(getMessage(L",Point on Circle centre - On(Circle& c, Point& p)", GEOMETRY_ERROR_MESSAGES, MES_POINTONCENTRE)); + return(Mid(p, c.pc, (r - c.radius) / r)); + } + + + Point Intof( int NF, const CLine& s, const Circle& c) { + // inters of cline & circle eg. p1 = Intof(NEARINT, s1, c1); + Point otherInters; + return Intof(NF, s, c, otherInters); + } + + Point Intof( int NF, const CLine& s, const Circle& c, Point& otherInters) { + // inters of cline & circle eg. p1 = Intof(NEARINT, s1, c1); + // otherInters returns the other intersection +#if 1 + // solving x = x0 + dx * t x = y0 + dy * t + // x = xc + R * cos(a) y = yc + R * sin(a) for t + // gives :- t� (dx� + dy�) + 2t(dx*dx0 + dy*dy0) + (x0-xc)� + (y0-yc)� - R� = 0 + int nRoots; + double t, tFar, tNear, tOther; + Vector2d v0(c.pc, s.p); + if((nRoots = quadratic(1, 2 * (v0 * s.v), v0.magnitudesqd() - c.radius * c.radius, tFar, tNear)) != 0) { + if(nRoots == 2 && NF == NEARINT) { + t = tNear; + tOther = tFar; + } else { + t = tFar; + tOther = (nRoots == 2)?tNear : tFar; + } + otherInters = s.v * tOther + s.p; + return s.v * t + s.p; + } + return INVALID_POINT; + } +#else + // geometric solution - this is similar to the peps method, and it may offer better tolerancing than above?? + Point intof; + CLine normal = Normal(s, c.pc); + intof = s.Intof(normal); + double d = intof.Dist(c.pc); + + if(fabs(d - c.radius) < TOLERANCE) return intof; // tangent (near enough for non-large radius I suppose?) + + if(d > c.radius + TOLERANCE) return INVALID_POINT; // no intersection + + double q = (c.radius - d) * (c.radius + d); + if(q < 0) return intof; // line inside tolerance + + return Along(s, -(double)NF * sqrt(q), intof); // 2 intersections (return near/far case) + } +#endif + Point Intof( int intMode, const Circle& c0, const Circle& c1) { + // inters of 2 circles eg. p1 = Intof(LEFTINT, c1, c2) + Point otherInters; + return Intof(intMode, c0, c1, otherInters); + } + + Point Intof( int intMode, const Circle& c0, const Circle& c1, Point& otherInters) { + // inters of 2 circles eg. p1 = Intof(LEFTINT, c1, c2);u + Point pLeft, pRight; + switch(Intof(c0, c1, pLeft, pRight)) { + default: + return INVALID_POINT; + case 1: + otherInters = pLeft; + return pLeft; + case 2: + if(intMode == LEFTINT) { + otherInters = pRight; + return pLeft; + }else { + otherInters = pLeft; + return pRight; + } + } + } + + int Intof(const Circle& c0, const Circle& c1, Point& pLeft, Point& pRight) { + // inters of 2 circles + // returns the number of intersctions + Vector2d v(c0.pc, c1.pc); + double d = v.normalise(); + if(d < TOLERANCE) return 0; // co-incident circles + + double sum = fabs(c0.radius) + fabs(c1.radius); + double diff = fabs(fabs(c0.radius) - fabs(c1.radius)); + if(d > sum + TOLERANCE || d < diff - TOLERANCE) return 0; + + // dist from centre of this circle to mid intersection + double d0 = 0.5 * (d + (c0.radius + c1.radius) * (c0.radius - c1.radius) / d); + if(d0 - c0.radius > TOLERANCE) return 0; // circles don't intersect + + double h = (c0.radius - d0) * (c0.radius + d0); // half distance between intersects squared + if(h < 0) d0 = c0.radius; // tangent + pLeft = v * d0 + c0.pc; // mid-point of intersects + if(h < TOLERANCE_SQ) return 1; // tangent + h = sqrt(h); + + v = ~v; // calculate 2 intersects + pRight = v * h + pLeft; + v = -v; + pLeft = v * h + pLeft; + return 2; + } + + Circle Tanto(int NF, CLine& s0, Point& p, double rad) { + // circle tanto a CLine thro' a point + double d = s0.Dist(p); + if(fabs(d) > rad + TOLERANCE) return INVALID_CIRCLE; // point too far from line + CLine s0offset = Parallel(RIGHTINT, s0, rad); + + return Circle(Intof(NF, s0offset, Circle(p, rad)), rad); + } + + Circle Tanto(int AT1, CLine& s1, int AT2, CLine& s2, double rad) { + // circle tanto 2 clines with radius + CLine Offs1 = Parallel(AT1, s1, rad); + CLine Offs2 = Parallel(AT2, s2, rad); + Point pc = Intof(Offs1, Offs2); + return (pc.ok)? Circle(pc, rad) : INVALID_CIRCLE; + } + Circle Tanto(int AT1, CLine s1, int AT2, CLine s2, int AT3, CLine s3) { + // circle tanto 3 CLines + double s1c = s1.c(), s2c = s2.c(), s3c = s3.c(); + double d = s1.v.gety() * (AT2 * s3.v.getx() - AT3 * s2.v.getx()) + + s2.v.gety() * (AT3 * s1.v.getx() - AT1 * s3.v.getx()) + + s3.v.gety() * (AT1 * s2.v.getx() - AT2 * s1.v.getx()); + if(fabs(d) < UNIT_VECTOR_TOLERANCE) return INVALID_CIRCLE; + double radius = (s1.v.gety() * (s2.v.getx() * s3c - s3.v.getx() * s2c) + + s2.v.gety() * (s3.v.getx() * s1c - s1.v.getx() * s3c) + + s3.v.gety() * (s1.v.getx() * s2c - s2.v.getx() * s1c)) / d ; + if(radius < TOLERANCE) return INVALID_CIRCLE; + + CLine Offs1 = Parallel(AT1, s1, radius); + CLine Offs2 = Parallel(AT2, s2, radius); + + Point p = Intof(Offs1, Offs2); + if(!p.ok) { + CLine Offs3 = Parallel(AT3, s3, radius); // s1 & s2 parallel + p = Intof(Offs1, Offs3); + if(!p.ok) return INVALID_CIRCLE; // 3 parallel lines + } + return Circle(p, radius); + } + Circle Thro(int LR, const Point& p0, const Point& p1, double rad) { + // circle thro' 2 points, given radius and side + CLine thro(p0, p1); + if(thro.ok) { + double d = 0.5 * p0.Dist(p1); + Point pm = Mid(p0, p1); + + if(d > rad + TOLERANCE) return INVALID_CIRCLE; + else if(d > rad - TOLERANCE) { + // within tolerance of centre of 2 points + return Circle(pm, d); + } + else { + // 2 solutions + return Circle(Along(Normal(thro, pm), (double)LR * sqrt((rad + d) * (rad - d)), pm), rad); + } + } + return INVALID_CIRCLE; + } + + Circle Thro(const Point& p0, const Point& p1) { + // circle thro 2 points (diametric) + return Circle(p0.Mid(p1), .5*p0.Dist(p1)); + } + Circle Thro(const Point& p0, const Point& p1, const Point& p2) { + // circle thro 3 points + CLine s0(p0, p1); + if(!s0.ok) return Thro(p1,p2); // p0 & p1 coincident + + CLine s1(p0, p2); + if(!s1.ok) return Thro(p0, p1); // p0 & p2 coincident + + CLine s2(p2, p1); + if(!s2.ok) return Thro(p0, p2); // p1 & p2 coincident + + Point p = Intof(Normal(s0, Mid(p0, p1)), Normal(s1, Mid(p0, p2))); + return (p.ok)? Circle(p, p0.Dist(p), true) : INVALID_CIRCLE; + } + Circle Tanto(int NF, int AT0, const CLine& s0, int AT1, const Circle &c1, double rad) { + // circle tanto cline & circle with radius + CLine Offs0 = Parallel(AT0, s0, rad); + Circle c2 = c1; + c2.radius += AT1 * rad; + Point pc = Intof(NF, Offs0, c2); + return (pc.ok)? Circle(pc, rad) : INVALID_CIRCLE; + } + + Circle Tanto( int LR, int AT0, const Circle& c0, const Point& p, double rad) { + // circle tanto circle & thro' a point + Circle c2 = c0; + c2.radius += AT0 * rad; + Circle c1(p, rad); + Point pc = Intof(LR, c2, c1); + return (pc.ok)? Circle(pc, rad) : INVALID_CIRCLE; + } + Circle Tanto(int LR, int AT0, const Circle& c0, int AT1, const Circle& c1, double rad) { + // circle tanto 2 circles + Circle c2 = c0; + Circle c3 = c1; + c2.radius += AT0 * rad; + c3.radius += AT1 * rad; + Point pc = Intof(LR, c2, c3); + return (pc.ok)? Circle(pc, rad) : INVALID_CIRCLE; + } + + Circle Parallel(int side, const Circle& c0, double distance) { + // parallel to circle by distance + return Circle(c0.pc, c0.radius + (double) side * distance); + } + + // distance + double atn360(double dy, double dx) { + // angle 0 to 2pi + double ang = atan2(dy, dx); + return ((ang < 0)? 2 * PI + ang : ang); + } + + double Dist(const Point& p0, const Circle& c, const Point& p1) { + // clockwise distance around c from p0 to p1 + double a0 = atn360(p0.y - c.pc.y, p0.x - c.pc.x); + double a1 = atn360(p1.y - c.pc.y ,p1.x - c.pc.x); + if ( a1 > a0 ) a1 -= 2 * PI ; + return (a0 - a1) * c.radius; + } + double Dist(const CLine& s, const Circle& c) { + // distance between line and circle + return fabs(s.Dist(c.pc)) - c.radius; + } + double Dist(const Circle& c0, const Circle& c1) { + // distance between 2 circles + return c0.pc.Dist(c1.pc) - c0.radius - c1.radius; + } + double Dist(const Circle& c, const Point& p) { + // distance between circle and point + return p.Dist(On(c, p)); + } + + double IncludedAngle(const Vector2d& v0, const Vector2d& v1, int dir) { + // returns the absolute included angle between 2 vectors in the direction of dir ( 1=acw -1=cw) + double inc_ang = v0 * v1; + if(inc_ang > 1. - UNIT_VECTOR_TOLERANCE) return 0; + if(inc_ang < -1. + UNIT_VECTOR_TOLERANCE) + inc_ang = PI; + else { // dot product, v1 . v2 = cos ang + if(inc_ang > 1.0) inc_ang = 1.0; + inc_ang = acos(inc_ang); // 0 to pi radians + + if(dir * (v0 ^ v1) < 0) inc_ang = 2 * PI - inc_ang ; // cp + } + return dir * inc_ang; + } + + double IncludedAngle(const Vector3d& v0, const Vector3d& v1, const Vector3d& normal, int dir) { + // returns the absolute included angle between 2 vectors in the direction of dir ( 1=acw -1=cw) about normal + double inc_ang = v0 * v1; + + if(inc_ang >= -NEARLY_ONE) { // dot product, v1 . v2 = cos ang + inc_ang = acos(inc_ang); // 0 to pi radians + + if(dir * (normal * (v0 ^ v1)) < 0) inc_ang = 2 * PI - inc_ang ; // cp + } + else + inc_ang = PI; // semi-cicle + + return dir * inc_ang; + } + + int corner(const Vector2d& v0, const Vector2d& v1, double cpTol) { + // returns corner + // 0 (TANGENT) = tangent + // 1 (LEFT) = left turn + // -1 (RIGHT) = right turn + double cp = v0 ^ v1; + if(fabs(cp) < cpTol) return TANGENT; + + return (cp > 0)?GEOFF_LEFT : GEOFF_RIGHT; + } + + int quadratic(double a, double b, double c, double& x0, double& x1) { + // solves quadratic equation ax² + bx + c = 0 + // returns number of real roots +// double epsilon = 1.0e-6; + double epsilon = (geoff_geometry::UNITS == METRES)?1.0e-09 : 1.0e-06; + double epsilonsq = epsilon * epsilon; + if(fabs(a) < epsilon) { + if(fabs(b) < epsilon) return 0; // invalid + x0 = - c / b; + return 1; + } + b /= a; + c /= a; + double s = b * b - 4 * c; + if(s < -epsilon) return 0; // imaginary roots + x0 = - 0.5 * b; + if(s > epsilonsq) { + s = 0.5 * sqrt(s); + x1 = x0 - s; + x0 += s; + return 2; + } + return 1; + } + + Plane::Plane(const Point3d& p0, const Point3d& p1, const Point3d& p2) { + // constructor plane from 3 points + normal = Vector3d(p0, p1) ^ Vector3d(p0, p2); + normal.normalise(); + ok = (normal != NULL_VECTOR); + d = -(normal * Vector3d(p0)); + } + + Plane::Plane(const Point3d& p0, const Vector3d& v, bool normalise) { + // constructor plane from point & vector + normal = v; + if(normalise == true) normal.normalise(); + d = -(normal * Vector3d(p0)); + } + + Plane::Plane(double dist, const Vector3d& n) { + normal = n; + double mag = normal.normalise(); + if((ok = (normal != NULL_VECTOR))) d = dist / mag; + } + + double Plane::Dist(const Point3d& p)const{ + // returns signed distance to plane from point p + return (normal * Vector3d(p)) + d; +} + + Point3d Plane::Near(const Point3d& p)const { + // returns near point to p on the plane + return - normal * Dist(p) + p; + } + + bool Plane::Intof(const Line& l, Point3d& intof, double& t) const{ + // intersection between plane and line + // input this plane, line + // output intof + // method returns true for valid intersection + double den = l.v * this->normal; + if(fabs(den) < UNIT_VECTOR_TOLERANCE) return false; // line is parallel to the plane, return false, even if the line lies on the plane + + t = -(normal * Vector3d(l.p0) + d) / den; + intof = l.v * t + l.p0; + return true; + } + + bool Plane::Intof(const Plane& pl, Line& intof)const { + // intersection of 2 planes + Vector3d d = this->normal ^ pl.normal; + d.normalise(); + intof.ok = false; + if(d == NULL_VECTOR) return false; // parallel planes + + intof.v = d; + intof.length = 1; + + double dot = this->normal * pl.normal; + + double den = dot * dot - 1.; + double a = (this->d - pl.d * dot) / den; + double b = (pl.d - this->d * dot) / den; + intof.p0 = a * this->normal + b * pl.normal; + intof.ok = true; + return true; + } + + bool Plane::Intof(const Plane& pl0, const Plane& pl1, Point3d& intof) const{ + // intersection of 3 planes + Line tmp; + if(Intof(pl0, tmp)) { + double t; + return pl1.Intof(tmp, intof, t); + } + return false; + } +} diff --git a/src/Mod/Path/libarea/kurve/Finite.cpp b/src/Mod/Path/libarea/kurve/Finite.cpp new file mode 100644 index 0000000000..0dee646e2e --- /dev/null +++ b/src/Mod/Path/libarea/kurve/Finite.cpp @@ -0,0 +1,677 @@ +////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// finite intersections +////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +/*============================== +Copyright (c) 2006 g.j.hawkesford + +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. +3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. +==============================*/ + +#include "geometry.h" + + + +#ifndef WIN32 +#define __min(a,b) ((ab)?a:b) +#endif + +namespace geoff_geometry { + int Intof(const Span& sp0, const Span& sp1, Point& p0, Point& p1, double t[4]) + { + // returns the number of intersects (lying within spans sp0, sp1) + if(sp0.box.outside(sp1.box) == true) return 0; + if(!sp0.dir) { + if(!sp1.dir) { + // line line + return LineLineIntof(sp0, sp1, p0, t); + } + else { + // line arc + return LineArcIntof(sp0, sp1, p0, p1, t); + } + } + else { + if(!sp1.dir) { + // arc line + return LineArcIntof(sp1, sp0, p0, p1, t); + } + else { + // arc arc + return ArcArcIntof(sp0, sp1, p0, p1, t); + } + } + } + + int LineLineIntof(const Span& sp0 , const Span& sp1, Point& p, double t[2]) { + // intersection between 2 Line2d + // returns 0 for no intersection in range of either span + // returns 1 for intersction in range of both spans + // t[0] is parameter on sp0, + // t[1] is parameter on sp1 + Vector2d v0(sp0.p0, sp0.p1); + Vector2d v1(sp1.p0, sp1.p1); + Vector2d v2(sp0.p0, sp1.p0); + + double cp = v1 ^ v0; + + if(fabs(cp) < UNIT_VECTOR_TOLERANCE ) { + p = INVALID_POINT; + return 0; // parallel or degenerate lines + } + + t[0] = (v1 ^ v2) / cp; + p = v0 * t[0] + sp0.p0; + p.ok = true; + double toler = geoff_geometry::TOLERANCE / sp0.length; // calc a parametric tolerance + + t[1] = (v0 ^ v2) / cp; + if(t[0] < -toler || t[0] > 1 + toler) return 0; // intersection on first? + toler = geoff_geometry::TOLERANCE / sp1.length; // calc a parametric tolerance + if(t[1] < -toler || t[1] > 1 + toler) return 0; // intersection on second? + return 1; + } + + int LineArcIntof(const Span& line, const Span& arc, Point& p0, Point& p1, double t[4]) { + // inters of line arc + // solving x = x0 + dx * t x = y0 + dy * t + // x = xc + R * cos(a) y = yc + R * sin(a) for t + // gives :- t² (dx² + dy²) + 2t(dx*dx0 + dy*dy0) + (x0-xc)² + (y0-yc)² - R² = 0 + int nRoots; + Vector2d v0(arc.pc, line.p0); + Vector2d v1(line.p0, line.p1); + double s = v1.magnitudesqd(); + + p0.ok = p1.ok = false; + if((nRoots = quadratic(s, 2 * (v0 * v1), v0.magnitudesqd() - arc.radius * arc.radius, t[0], t[1])) != 0) { + double toler = geoff_geometry::TOLERANCE / sqrt(s); // calc a parametric tolerance + if(t[0] > -toler && t[0] < 1 + toler) { + p0 = v1 * t[0] + line.p0; + p0.ok = arc.OnSpan(p0, &t[2]); + } + if(nRoots == 2) { + if(t[1] > -toler && t[1] < 1 + toler) { + p1 = v1 * t[1] + line.p0; + p1.ok = arc.OnSpan(p1, &t[3]); + } + } + if(!p0.ok && p1.ok) { + p0 = p1; + p1.ok = false; + } + nRoots = (int)p0.ok + (int)p1.ok; + } + return nRoots; + } + + int ArcArcIntof(const Span& arc0, const Span& arc1, Point& pLeft, Point& pRight, double t[4]) { + // Intof 2 arcs + int numInts = Intof(Circle(arc0.pc, arc0.radius), Circle(arc1.pc, arc1.radius), pLeft, pRight); + + if(numInts == 0) { + pLeft = arc0.p1; + pLeft.ok = false; + return 0; + } + int nLeft = arc0.OnSpan(pLeft) && arc1.OnSpan(pLeft); + int nRight = (numInts == 2)?arc0.OnSpan(pRight) && arc1.OnSpan(pRight) : 0; + if(nLeft == 0 && nRight) pLeft = pRight; + return nLeft + nRight; + } + + bool Span::OnSpan(const Point& p)const { + double t; + return OnSpan(p, &t); + } + + bool Span::OnSpan(const Point& p, double* t)const { + // FAST OnSpan test - assumes that p lies ON the unbounded span +#if _DEBUG + if(this->returnSpanProperties == false) { + FAILURE(L"OnSpan - properties no set, incorrect calling code"); + } +#endif +#if 0 + if(NullSpan) { + *t = 0.0; + return (p == p0); + } + + if(p == p0) { + *t = 0.0; + return true; + } + + if(p == p1) { + *t = 1.0; + return true; + } +#endif + bool ret; +// if(p == this->p0 || p == this->p1) return true; + + if(dir == LINEAR) { +#if 1 +#if _DEBUG + // check p is on line + CLine cl(*this); + double d = fabs(cl.Dist(p)); + if( d > geoff_geometry::TOLERANCE) { + FAILURE(L"OnSpan - point not on linear span, incorrect calling code"); + } +#endif +#endif + Vector2d v0(p0, p); + *t = vs * v0; +// ret = (*t > - geoff_geometry::TOLERANCE && *t < length + geoff_geometry::TOLERANCE); + + *t = *t / length; + ret = (*t >= 0 && *t <= 1.0 ); + + } + else { + // true if p lies on arc span sp (p must be on circle of span) +#if 1 +#if _DEBUG + // check that p lies on the arc + double d = p.Dist(pc); + if(FNE(d, radius, geoff_geometry::TOLERANCE)) { + FAILURE(L"OnSpan - point not on circular span, incorrect calling code"); + } + +#endif +#endif +#if 0 // alt method (faster, but doesn't provide t) + Vector2d v0(p0, p); + Vector2d v1(p0, p1); + + // check angle to point from start + double cp; + ret = ((cp = (dir * (v0 ^ v1))) > 0); + *t = 0.0;// incorrect !!! +#else + Vector2d v = ~Vector2d(pc, p); + v.normalise(); + if(dir == CW) v = -v; + + double ang = IncludedAngle(vs, v, dir); + *t = ang / angle; + ret = (*t >= 0 && *t <= 1.0); +#endif + } + + return ret; + } + + Line::Line(const Point3d& p, const Vector3d& v0, bool boxed){ + // constructor from point & vector + p0 = p; + v = v0; + length = v.magnitude(); + if(boxed) minmax(); + ok = (length > geoff_geometry::TOLERANCE); + } + + Line::Line(const Point3d& p, const Point3d& p1){ + // constructor from 2 points + p0 = p; + v = Vector3d(p, p1); + length = v.magnitude(); + minmax(); + ok = (length > geoff_geometry::TOLERANCE); + } + + Line::Line(const Span& sp){ + // contructor from linear span + p0 = sp.p0; + v = sp.vs * sp.length; + length = sp.length; + // box = sp.box; + box.min = Point3d(sp.box.min); + box.max = Point3d(sp.box.max); + ok = !sp.NullSpan; + } + + void Line::minmax() { + MinMax(this->p0, box.min, box.max); + MinMax(this->v + this->p0, box.min, box.max); + } + + bool Line::atZ(double z, Point3d& p)const { + // returns p at z on line + if(FEQZ(this->v.getz())) return false; + double t = (z - this->p0.z) / this->v.getz(); + p = Point3d(this->p0.x + t * this->v.getx(), this->p0.y + t * this->v.gety(), z); + return true; + } + + + bool Line::Shortest(const Line& l2, Line& lshort, double& t1, double& t2)const { + /* + Calculate the line segment PaPb that is the shortest route between + two lines P1P2 and P3P4. Calculate also the values of mua and mub where + Pa = P1 + t1 (P2 - P1) + Pb = P3 + t2 (P4 - P3) + Return FALSE if no solution exists. P Bourke method. + Input this 1st line + Input l2 2nd line + Output lshort shortest line between lines (if lshort.ok == false, the line intersect at a point lshort.p0) + Output t1 parameter at intersection on 1st Line + Output t2 parameter at intersection on 2nd Line + + */ + Vector3d v13(l2.p0, this->p0); + if(this->ok == false || l2.ok == false) return false; + + double d1343 = v13 * l2.v; // dot products + double d4321 = l2.v * this->v; + double d1321 = v13 * this->v; + double d4343 = l2.v * l2.v; + double d2121 = this->v * this->v; + + double denom = d2121 * d4343 - d4321 * d4321; + if(fabs(denom) < 1.0e-09) return false; + double numer = d1343 * d4321 - d1321 * d4343; + + t1 = numer / denom; + t2 = (d1343 + d4321 * t1) / d4343; + + lshort = Line(t1* this->v + this->p0, t2 * l2.v + l2.p0); + t1 *= this->length; + t2 *= l2.length; // parameter in line length for tolerance checking + return true; + } + + int Intof(const Line& l0, const Line& l1, Point3d& intof) + { + /* intersection of 2 vectors + returns 0 for intercept but not within either vector + returns 1 for intercept on both vectors + + note that this routine always returns 0 for parallel vectors + method: + x = x0 + dx0 * t0 for l0 + ... + ... + x = x1 + dx1 * t1 for l1 + ... + ... + + x0 + dx0 * t0 = x1 + dx1 * t1 + dx0 * t0 - dx1 * t1 + x0 - x1 = 0 + + setup 3 x 3 determinent for + a0 t0 + b0 t1 + c0 = 0 + a1 t0 + b1 t1 + c1 = 0 + a2 t0 + b2 t1 + c2 = 0 + + from above a = l0.v + b = -l1.v + c = Vector3d(l1, l0) + */ + // Vector3d a = l0.v; + if(l0.box.outside(l1.box) == true) return 0; + Vector3d b = -l1.v; + Vector3d c = Vector3d(l1.p0, l0.p0); + Vector3d det = l0.v ^ b; + Vector3d t = b ^ c; + + // choose largest determinant & corresponding parameter for accuracy + double t0 = t.getx(); + double d = det.getx(); + + if(fabs(det.getz()) > fabs(det.gety())) { + if(fabs(det.getz()) > fabs(det.getx())) { + t0 = t.getz(); + d = det.getz(); + } + } + else { + if(fabs(det.gety()) > fabs(det.getx())) { + t0 = t.gety(); + d = det.gety(); + } + } + + if(fabs(d) < 1.0e-06) return 0; + + t0 /= d; + intof = l0.v * t0 + l0.p0; + + Point3d other; + double t1; + if(Dist(l1, intof, other, t1) > geoff_geometry::TOLERANCE) return 0; + + t0 *= l0.length; + if( t0 < -geoff_geometry::TOLERANCE || t0 > l0.length + geoff_geometry::TOLERANCE || t1 < -geoff_geometry::TOLERANCE || t1 > l1.length + geoff_geometry::TOLERANCE ) return 0; + return 1; + } + + + double Dist(const Line& l, const Point3d& p, Point3d& pnear, double& t){ + // returns the distance of a point from a line and the near point on the extended line and the parameter of the near point (0-length) in range + pnear = Near(l, p, t ); + return p.Dist(pnear); + } + + Point3d Near(const Line& l, const Point3d& p, double& t){ + // returns the near point from a line on the extended line and the parameter of the near point (0-length) in range + t = (Vector3d(l.p0, p) * l.v) / l.length; // t parametised 0 - line length + return l.v * (t / l.length) + l.p0; + } + + Point3d Line::Near(const Point3d& p, double& t)const{ + // returns the near point from a line on the extended line and the parameter of the near point (0-length) in range + t = (Vector3d(this->p0, p) * this->v) / this->length; // t parametised 0 - line length + return this->v * (t / this->length) + this->p0; + } + + double DistSq(const Point3d *p, const Vector3d *vl, const Point3d *pf) { + /// returns the distance squared of pf from the line given by p,vl + /// vl must be normalised + Vector3d v(*p, *pf); + Vector3d vcp = *vl ^ v; + double d = vcp.magnitudeSq(); // l * sina + return d; + } + + double Dist(const Point3d *p, const Vector3d *vl, const Point3d *pf) { + /// returns the distance of pf from the line given by p,vl + /// vl must be normalised + Vector3d v(*p, *pf); + Vector3d vcp = *vl ^ v; + double d = vcp.magnitude(); // l * sina + return d; +#if 0 + // slower method requires 2 sqrts + Vector3d v(*p, *pf); + double magv = v.normalise(); + Vector3d cp = *vl ^ v; + double d = magv * cp.magnitude(); + return d; // l * sina +#endif + } + + double Dist(const Span& sp, const Point& p , Point& pnear ) { + // returns distance of p from span, pnear is the nearpoint on the span (or endpoint) + if(!sp.dir) { + double d, t; + Point3d unused_pnear; + d = Dist(Line(sp), Point3d(p), unused_pnear, t); + if(t < -geoff_geometry::TOLERANCE) { + pnear = sp.p0; // nearpoint + d = pnear.Dist(p); + } + else if(t > sp.length + geoff_geometry::TOLERANCE) { + pnear = sp.p1; + d = pnear.Dist(p); + } + return d; + } + else { + // put pnear on the circle + double radiusp; + Vector2d v(sp.pc, p); + if((radiusp = v.magnitude()) < geoff_geometry::TOLERANCE) { + // point specified on circle centre - use first point as near point + pnear = sp.p0; // nearpoint + return sp.radius; + } + else { + pnear = v * (sp.radius / radiusp) + sp.pc; + + // check if projected point is on the arc + if(sp.OnSpan(pnear)) return fabs(radiusp - sp.radius); + // double h1 = pnear.x - sp.p0.x ; + // double v1 = pnear.y - sp.p0.y ; + // double h2 = sp.p1.x - pnear.x ; + // double v2 = sp.p1.y - pnear.y ; + // if ( sp.dir * ( h1 * v2 - h2 * v1 ) >= 0 )return fabs(radiusp - sp.radius); + + // point not on arc so calc nearest end-point + double ndist = p.Dist(sp.p0); + double dist = p.Dist(sp.p1); + if(ndist >= dist) { + // sp.p1 is near point + pnear = sp.p1; + return dist; + } + + // sp.p0 is near point + pnear = sp.p0; // nearpoint + return ndist ; + } + } + } + + bool OnSpan(const Span& sp, const Point& p) { + Point nullPoint; + return OnSpan(sp, p, false, nullPoint, nullPoint); + } + + bool OnSpan(const Span& sp, const Point& p, bool nearPoints, Point& pNear, Point& pOnSpan) { + // function returns true if pNear == pOnSpan + // returns pNear & pOnSpan if nearPoints true + // pNear (nearest on unbound span) + // pOnSpan (nearest on finite span) + if(sp.dir) { + // arc + if(fabs(p.Dist(sp.pc) - sp.radius) > geoff_geometry::TOLERANCE) { + if(!nearPoints) return false; + } + + pNear = On(Circle(sp.pc, sp.radius), p); + + if(sp.OnSpan(pNear)) { + if(nearPoints) pOnSpan = pNear; + return true; // near point is on arc - already calculated + } + + // point not on arc return the nearest end-point + if(nearPoints) pOnSpan = (p.Dist(sp.p0) >= p.Dist(sp.p1)) ?sp.p1 : sp.p0; + return false; + } + else { + // straight + if(fabs(CLine(sp.p0, sp.vs).Dist(p)) > geoff_geometry::TOLERANCE) { + if(!nearPoints) return false; + } + Vector2d v(sp.p0, p); + double t = v * sp.vs; + if(nearPoints) pNear = sp.vs * t + sp.p0; + bool onSpan = (t > - geoff_geometry::TOLERANCE && t < sp.length + geoff_geometry::TOLERANCE); + if(! onSpan) { + if(nearPoints) pOnSpan = (p.Dist(sp.p0) >= p.Dist(sp.p1))?sp.p1 : sp.p0; + } + else { + if(nearPoints) pOnSpan = pNear; + } + return onSpan; + } + } + + // Triangle3d Constructors + Triangle3d::Triangle3d(const Point3d& p1, const Point3d& p2, const Point3d& p3) { + vert1 = p1; + vert2 = p2; + vert3 = p3; + v0 = Vector3d(vert1, vert2); + v1 = Vector3d(vert1, vert3); + ok = true; + + // set box + box.min.x = __min(__min(vert1.x, vert2.x), vert3.x); + box.min.y = __min(__min(vert1.y, vert2.y), vert3.y); + box.min.z = __min(__min(vert1.z, vert2.z), vert3.z); + + box.max.x = __max(__max(vert1.x, vert2.x), vert3.x); + box.max.y = __max(__max(vert1.y, vert2.y), vert3.y); + box.max.z = __max(__max(vert1.z, vert2.z), vert3.z); + } + + // Triangle3d methods + bool Triangle3d::Intof(const Line& l, Point3d& intof)const { + // returns intersection triangle to line in intof + // funtion returns true for intersection, false for no intersection + // method based on Möller & Trumbore(1997) (Barycentric coordinates) + // based on incorrect Pseudo code from "Geometric Tools for Computer Graphics" p.487 + if(box.outside(l.box) == true) return false; + + Vector3d line(l.v); + line.normalise(); + + Vector3d p = line ^ v1; // cross product + double tmp = p * v0; // dot product + + if(FEQZ(tmp)) return false; + + tmp = 1 / tmp; + Vector3d s(vert1, l.p0); + + double u = tmp * (s * p); // barycentric coordinate + if(u < 0 || u > 1) return false; // not inside triangle + + Vector3d q = s ^ v0; + double v = tmp * (line * q); // barycentric coordinate + if(v < 0 || v > 1) return false; // not inside triangle + + if( u + v > 1) return false; // not inside triangle + + double t = tmp * (v1 * q); + intof = line * t + l.p0; + return true; + } + + + // box class + bool Box::outside(const Box& b)const { + // returns true if this box is outside b + if(b.ok == false || this->ok == false) return false; // no box set + if(this->max.x < b.min.x) return true; + if(this->max.y < b.min.y) return true; + if(this->min.x > b.max.x) return true; + if(this->min.y > b.max.y) return true; + return false; + } + + void Box::combine(const Box& b) { + if(b.max.x > this->max.x) this->max.x = b.max.x; + if(b.max.y > this->max.y) this->max.y = b.max.y; + if(b.min.x < this->min.x) this->min.x = b.min.x; + if(b.min.y < this->min.y) this->min.y = b.min.y; + } + + void Box3d::combine(const Box3d& b) { + if(b.max.x > this->max.x) this->max.x = b.max.x; + if(b.max.y > this->max.y) this->max.y = b.max.y; + if(b.max.z > this->max.z) this->max.z = b.max.z; + if(b.min.x < this->min.x) this->min.x = b.min.x; + if(b.min.y < this->min.y) this->min.y = b.min.y; + if(b.min.z < this->min.z) this->min.z = b.min.z; + } + + bool Box3d::outside(const Box3d& b) const{ + // returns true if this box is outside b + if(b.ok == false || this->ok == false) return false; // no box set + if(this->max.x < b.min.x) return true; + if(this->max.y < b.min.y) return true; + if(this->max.z < b.min.z) return true; + if(this->min.x > b.max.x) return true; + if(this->min.y > b.max.y) return true; + if(this->min.z > b.max.z) return true; + return false; + } +#if 0 + Span3d IsPtsSpan3d(const double* a, int n, double tolerance, double* deviation) { + // returns a span3d if all points are within tolerance + int np = n / 3; // number of points + if(np < 2) return Span3d(); // Invalid span3d + Point3d sp = Point3d(&a[0]); + Point3d ep = Point3d(&a[n-3]); + Line line = IsPtsLine(a, n, tolerance, deviation); + if(line.ok) return Span3d(sp, ep); // it's a line + + *deviation = 0; // cumulative deviation + Point3d mp = Point3d(&a[np / 2 * 3]); // mid point + Plane plane(sp, mp, ep); + if(plane.ok) { + // plane of the arc is ok + // calculate centre point + Vector3d vs(mp, sp); + vs.normalise(); + Vector3d ve(mp, ep); + ve.normalise(); + Vector3d rs = vs ^ plane.normal; + Vector3d re = ve ^ plane.normal; + + Line rsl(sp.Mid(mp), rs, false); + Line rel(ep.Mid(mp), re, false); + + Point3d pc; + Intof(rsl, rel, pc); + double radius = pc.Dist(sp); + + // check other points on circle + for(int i = 2; i < np - 1; i++) { + Point3d p(&a[i*3]); + double dp = fabs(plane.Dist(p)); + double dr = fabs(p.Dist(pc) - radius); +double tolerance = 10.0 * 1.0e-6; + if(dp > tolerance || dr > tolerance) { + return Span3d(); + } + + } + return Span3d(CW, plane.normal, sp, ep, pc); + + } + return Span3d(); + } +#endif + + Line IsPtsLine(const double* a, int n, double tolerance, double* deviation) { + // returns a Line if all points are within tolerance + // deviation is returned as the sum of all deviations of interior points to line(sp,ep) + int np = n / 3; // number of points + *deviation = 0; // cumulative deviation + if(np < 2) return Line(); // Invalid line + + Point3d sp(&a[0]); + Point3d ep(&a[n-3]); + Line line(sp, ep); // line start - end + + if(line.ok) { + for(int j = 1; j < np - 1; j++) { + Point3d mp(&a[j * 3]); + double t, d=0; + if((d = mp.Dist(line.Near(mp, t))) > tolerance) { + line.ok = false; + return line; + } + *deviation = *deviation + d; + } + } + return line; + } +} diff --git a/src/Mod/Path/libarea/kurve/License.txt b/src/Mod/Path/libarea/kurve/License.txt new file mode 100644 index 0000000000..db9a107960 --- /dev/null +++ b/src/Mod/Path/libarea/kurve/License.txt @@ -0,0 +1,24 @@ +Copyright (c) <2015>, +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * 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. + * Neither the name of the nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + +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 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. diff --git a/src/Mod/Path/libarea/kurve/Matrix.cpp b/src/Mod/Path/libarea/kurve/Matrix.cpp new file mode 100644 index 0000000000..ed58afb9ab --- /dev/null +++ b/src/Mod/Path/libarea/kurve/Matrix.cpp @@ -0,0 +1,648 @@ +//////////////////////////////////////////////////////////////////////////////////////////////// +// 3d geometry classes - implements some 3d stuff +// +// +//////////////////////////////////////////////////////////////////////////////////////////////// + +/*============================== +Copyright (c) 2006 g.j.hawkesford + +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. +3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. +==============================*/ + + +#include "geometry.h" +using namespace geoff_geometry; + +#ifdef PEPSDLL + #include "vdm.h" + #include "pepsdll.h" + #include "realds.h" +#endif +//////////////////////////////////////////////////////////////////////////////////////////////// +// matrix +//////////////////////////////////////////////////////////////////////////////////////////////// +namespace geoff_geometry { + + Matrix::Matrix(){ + Unit(); + } + Matrix::Matrix(double m[16]) { + memcpy(e, m, sizeof(e)); + this->IsUnit(); + this->IsMirrored(); + } + + Matrix::Matrix( const Matrix& m) + { + *this = m; + } + + bool Matrix::operator==(const Matrix &m)const{ + // m1 == m2 + if(this->m_unit != m.m_unit || this->m_mirrored != m.m_mirrored) return false; + for(int i = 0; i < 16; i++) + if(FEQ(this->e[i], m.e[i], TIGHT_TOLERANCE) == false) return false; + return true; + } + +#if 0 + const Matrix& Matrix::operator=( Matrix &m) { + for(int i = 0; i < 16; i++) e[i] = m.e[i]; + m_unit = m.m_unit; + m_mirrored = m.m_mirrored; + return *this; + } +#endif + void Matrix::Unit() + { + // homogenous matrix - set as unit matrix + memset(e, 0, sizeof(e)); + e[0] = e[5] = e[10] = e[15] = 1; + m_unit = true; + m_mirrored = false; + } + + void Matrix::Get(double* p) const + { + // copy the matrix + memcpy(p, e, sizeof(e)); + } + void Matrix::Put(double* p) + { + // assign the matrix + memcpy(e, p, sizeof(e)); + m_unit = false; // don't know + m_mirrored = -1; // don't know + + } + void Matrix::Translate(double x, double y, double z) + { + // translation + e[3] += x; + e[7] += y; + e[11] += z; + m_unit = false; + } + + void Matrix::Rotate(double angle, Vector3d *rotAxis) { + /// Rotation about rotAxis with angle + Rotate(sin(angle), cos(angle), rotAxis); + } + + void Matrix::Rotate(double sinang, double cosang, Vector3d *rotAxis) { + /// Rotation about rotAxis with cp & dp + Matrix rotate; + double oneminusc = 1.0 - cosang; + + rotate.e[0] = rotAxis->getx() * rotAxis->getx() * oneminusc + cosang; + rotate.e[1] = rotAxis->getx() * rotAxis->gety() * oneminusc - rotAxis->getz() * sinang; + rotate.e[2] = rotAxis->getx() * rotAxis->getz() * oneminusc + rotAxis->gety() * sinang; + + rotate.e[4] = rotAxis->getx() * rotAxis->gety() * oneminusc + rotAxis->getz() * sinang; + rotate.e[5] = rotAxis->gety() * rotAxis->gety() * oneminusc + cosang; + rotate.e[6] = rotAxis->gety() * rotAxis->getz() * oneminusc - rotAxis->getx() * sinang; + + rotate.e[8] = rotAxis->getx() * rotAxis->getz() * oneminusc - rotAxis->gety() * sinang; + rotate.e[9] = rotAxis->gety() * rotAxis->getz() * oneminusc + rotAxis->getx() * sinang; + rotate.e[10] = rotAxis->getz() * rotAxis->getz() * oneminusc + cosang; + Multiply(rotate); // concatinate rotation with this matrix + m_unit = false; + m_mirrored = -1; // don't know + } + + + void Matrix::Rotate(double angle, int Axis) + { // Rotation (Axis 1 = x , 2 = y , 3 = z + Rotate(sin(angle), cos(angle), Axis); + } + + void Matrix::Rotate(double sinang, double cosang, int Axis) + { // Rotation (Axis 1 = x , 2 = y , 3 = z + Matrix rotate; + rotate.Unit(); + + switch(Axis) + { + case 1: + // about x axis + rotate.e[5] = rotate.e[10] = cosang; + rotate.e[6] = -sinang; + rotate.e[9] = sinang; + break; + case 2: + // about y axis + rotate.e[0] = rotate.e[10] = cosang; + rotate.e[2] = sinang; + rotate.e[8] = -sinang; + break; + case 3: + // about z axis + rotate.e[0] = rotate.e[5] = cosang; + rotate.e[1] = -sinang; + rotate.e[4] = sinang; + break; + } + Multiply(rotate); // concatinate rotation with this matrix + m_unit = false; + m_mirrored = -1; // don't know + } + + void Matrix::Scale(double scale) + { + // add a scale + Scale(scale, scale, scale); + } + + void Matrix::Scale(double scalex, double scaley, double scalez) + { + // add a scale + Matrix temp; + temp.Unit(); + + temp.e[0] = scalex; + temp.e[5] = scaley; + temp.e[10] = scalez; + Multiply(temp); + m_unit = false; + m_mirrored = -1; // don't know + } + void Matrix::Multiply(Matrix& m) + { + // multiply this by give matrix - concatinate + int i, k, l; + Matrix ret; + + for (i = 0; i < 16; i++) + { + l = i - (k = (i % 4)); + ret.e[i] = m.e[l] * e[k] + m.e[l+1] * e[k+4] + m.e[l+2] * e[k+8] + m.e[l+3] * e[k+12]; + } + + *this = ret; + this->IsUnit(); + } + + void Matrix::Transform(double p0[3], double p1[3]) const + { + // transform p0 thro' this matrix + if(m_unit) + memcpy(p1, p0, 3 * sizeof(double)); + else { + p1[0] = p0[0] * e[0] + p0[1] * e[1] + p0[2] * e[2] + e[3]; + p1[1] = p0[0] * e[4] + p0[1] * e[5] + p0[2] * e[6] + e[7]; + p1[2] = p0[0] * e[8] + p0[1] * e[9] + p0[2] * e[10] + e[11]; + } + } + void Matrix::Transform2d(double p0[2], double p1[2]) const + { + // transform p0 thro' this matrix (2d only) + if(m_unit) + memcpy(p1, p0, 2 * sizeof(double)); + else { + p1[0] = p0[0] * e[0] + p0[1] * e[1] + e[3]; + p1[1] = p0[0] * e[4] + p0[1] * e[5] + e[7]; + } + } + + void Matrix::Transform(double p0[3]) const + { + double p1[3]; + if(!m_unit) { + Transform(p0, p1); + memcpy(p0, p1, 3 * sizeof(double)); + } + } + + int Matrix::IsMirrored() + { + // returns true if matrix has a mirror + if(m_unit) + m_mirrored = false; + else if(m_mirrored == -1) { + + m_mirrored = ((e[0] * (e[5] * e[10] - e[6] * e[9]) + - e[1] * (e[4] * e[10] - e[6] * e[8]) + + e[2] * (e[4] * e[9] - e[5] * e[8])) < 0); + } + return m_mirrored; + } + int Matrix::IsUnit() { + // returns true if unit matrix + for(int i = 0; i < 16; i++) { + if(i == 0 || i == 5 || i == 10 || i == 15) { + if(e[i] != 1) return m_unit = false; + } + else { + if(e[i] != 0) return m_unit = false; + } + } + m_mirrored = false; + return m_unit = true; + } + + void Matrix::GetTranslate(double& x, double& y, double& z) const + { + // return translation + x = e[3]; + y = e[7]; + z = e[11]; + } + void Matrix::GetScale(double& sx, double& sy, double& sz) const + { + // return the scale + if(m_unit) { + sx = sy = sz = 1; + } + else { + sx = sqrt(e[0] * e[0] + e[1] * e[1] + e[2] * e[2]); + sy = sqrt(e[4] * e[4] + e[5] * e[5] + e[6] * e[6]); + sz = sqrt(e[8] * e[8] + e[9] * e[9] + e[10] * e[10]); + } + } + bool Matrix::GetScale(double& sx) const + { + // return a uniform scale (false if differential) + double sy, sz; + if(m_unit) { + sx = 1; + return true; + } + GetScale(sx, sy, sz); + return (fabs(fabs(sx) - fabs(sy)) < 0.000001)?true : false; + } + void Matrix::GetRotation(double& ax, double& ay, double& az) const + { + // return the rotations + if(m_unit) { + ax = ay = az = 0; + return; + } + double a; /* cos(bx) */ + double b; /* sin(bx) */ + double c; /* cos(by) */ + double d; /* sin(by) */ + double ee; /* cos(bz) */ + double f; /* sin(bz) */ + double sx, sy, sz; + GetScale(sx, sy, sz); + if(this->m_mirrored == -1) FAILURE(L"Don't know mirror - use IsMirrored method on object"); + if(this->m_mirrored) sx = -sx; + + // solve for d and decide case and solve for a, b, c, e and f + d = - e[8] / sz; + if((c = (1 - d) * (1 + d)) > 0.001) + { + // case 1 + c = sqrt( c ); + a = e[10] / sz / c; + b = e[9] / sz / c; + ee = e[0] / sx / c; + f = e[4] / sy / c; + } + else + { + // case 2 + double coef; + double p, q; + + d = ( d < 0 ) ? -1 : 1 ; + c = 0 ; + p = d * e[5] / sy - e[2] / sx; + q = d * e[6] / sy + e[1] / sx; + if((coef = sqrt( p * p + q * q )) > 0.001) { + a = q / coef; + b = p / coef; + ee = b; + f = -d * b; + } + else + { + /* dependent pairs */ + a = e[5] / sy; + b = -e[6] / sy; + ee = 1 ; + f = 0 ; + } + } + + // solve and return ax, ay and az + ax = atan2( b, a ); + ay = atan2( d, c ); + az = atan2( f, ee ); + } + + Matrix Matrix::Inverse() + { + // matrix inversion routine + + // a is input matrix destroyed & replaced by inverse + // method used is gauss-jordan (ref ibm applications) + + double hold , biga ; + int i , j , k , nk , kk , ij , iz ; + int ki , ji , jp , jk , kj , jq , jr , ik; + + int n = 4; // 4 x 4 matrix only + Matrix a = *this; + int l[4], m[4]; + + if(a.m_unit) return a; // unit matrix + + // search for largest element + nk = - n ; + for ( k = 0 ; k < n ; k++ ) { + nk += n ; + l [ k ] = m [ k ] = k ; + kk = nk + k ; + biga = a.e[ kk ] ; + + for ( j = k ; j < n ; j++ ) { + iz = n * j ; + for ( i = k ; i < n ; i++ ) { + ij = iz + i ; + if ( fabs ( biga ) < fabs ( a.e[ ij ] ) ) { + biga = a.e[ ij ] ; + l[ k ] = i ; + m[ k ] = j ; + } + } + } + + + // interchange rows + j = l[ k ] ; + if ( j > k ) { + ki = k - n ; + + for ( i = 0 ; i < n ; i++ ) { + ki += n ; + hold = - a.e[ ki ] ; + ji = ki - k + j ; + a.e[ ki ] = a.e[ ji ] ; + a.e[ ji ] = hold ; + } + } + + // interchange columns + i = m[ k ] ; + if ( i > k ) { + jp = n * i ; + for ( j = 0 ; j < n ; j++ ) { + jk = nk + j ; + ji = jp + j ; + hold = - a.e[ jk ] ; + a.e[ jk ] = a.e[ ji ] ; + a.e[ ji ] = hold ; + } + } + + // divide columns by minus pivot (value of pivot element is contained in biga) + if ( fabs ( biga ) < 1.0e-10 )FAILURE(getMessage(L"Singular Matrix - Inversion failure",GEOMETRY_ERROR_MESSAGES, -1)); // singular matrix + + for ( i = 0 ; i < n ; i++ ) { + if ( i != k ) { + ik = nk + i ; + a.e[ ik ] = - a.e[ ik ] /biga ; + } + } + + // reduce matrix + for ( i = 0 ; i < n ; i++ ) { + ik = nk + i ; + hold = a.e[ ik ] ; + ij = i - n ; + + for ( j = 0 ; j < n ; j++ ) { + ij = ij + n ; + if ( i != k && j != k ) { + kj = ij - i + k ; + a.e[ ij ] = hold * a.e[ kj ] + a.e[ ij ] ; + } + } + } + + // divide row by pivot + kj = k - n ; + for ( j = 0 ; j < n ; j++ ) { + kj = kj + n ; + if ( j != k ) a.e[ kj] = a.e[ kj ] /biga ; + } + + // replace pivot by reciprocal + a.e[ kk ] = 1 / biga ; + } + + // final row and column interchange + k = n - 1 ; + + while ( k > 0 ) { + i = l[ --k ] ; + if ( i > k ) { + jq = n * k ; + jr = n * i ; + + for ( j = 0 ; j < n ; j++ ) { + jk = jq + j ; + hold = a.e[jk] ; + ji = jr + j ; + a.e[jk] = - a.e[ji] ; + a.e[ji] = hold ; + } + } + + j = m[ k ] ; + if ( j > k ) { + ki = k - n ; + + for ( i = 1 ; i <= n ; i ++ ) { + ki = ki + n ; + hold = a.e[ ki ] ; + ji = ki - k + j ; + a.e[ ki ] = - a.e[ ji ] ; + a.e[ ji ] = hold ; + } + } + } + + return a; + } + +#ifdef PEPSDLL + void Matrix::ToPeps(int id) + { + int set = PepsVdmMake(id, VDM_MATRIX_TYPE , VDM_LOCAL); + if(set < 0) FAILURE(L"Failed to create Matrix VDM"); + struct kgm_header pepsm; + + Get(pepsm.matrix); + pepsm.off_rad = 0; + pepsm.off_dir = pepsm.origin_id = 0; + + PepsVdmWriteTmx(set , &pepsm ); + + PepsVdmClose(set); + + } + + void Matrix::FromPeps(int id) + { + // if(id) { + int set = PepsVdmOpen(id, VDM_MATRIX_TYPE , VDM_READ_ONLY | VDM_LOCAL); + if(set < 0) FAILURE(L"Failed to open Matrix VDM"); + + struct kgm_header pepsm; + PepsVdmReadTmx(set , &pepsm); + memcpy(e, pepsm.matrix, sizeof(pepsm.matrix)); + m_unit = true; + for(int i = 0; i < 16; i++) { + // copy over matrix and check for unit matrix + if(i == 0 || i == 5 || i == 10 || i == 15) { + if((e[i] = pepsm.matrix[i]) != 1) m_unit = false; + } + else { + if((e[i] = pepsm.matrix[i]) != 0) m_unit = false; + } + } + PepsVdmClose(set); + m_mirrored = IsMirrored(); + // } + } +#endif + + Matrix UnitMatrix; // a global unit matrix + + + // vector + Vector2d::Vector2d(const Vector3d &v){ + if(FEQZ(v.getz())) FAILURE(L"Converting Vector3d to Vector2d illegal"); + dx = v.getx(); + dy = v.gety(); + } + + bool Vector2d::operator==(const Vector2d &v)const { + return FEQ(dx, v.getx(), 1.0e-06) && FEQ(dy, v.gety(), 1.0e-06); + } + + void Vector2d::Transform(const Matrix& m) { + // transform vector + if(m.m_unit == false) { + double dxt = dx * m.e[0] + dy * m.e[1]; + double dyt = dx * m.e[4] + dy * m.e[5]; + dx = dxt; + dy = dyt; + } + this->normalise(); + } + + void Vector3d::Transform(const Matrix& m) { + // transform vector + if(m.m_unit == false) { + double dxt = dx * m.e[0] + dy * m.e[1] + dz * m.e[2]; + double dyt = dx * m.e[4] + dy * m.e[5] + dz * m.e[6]; + double dzt = dx * m.e[8] + dy * m.e[9] + dz * m.e[10]; + dx = dxt; + dy = dyt; + dz = dzt; + } + this->normalise(); + } + + void Vector3d::arbitrary_axes(Vector3d& x, Vector3d& y){ + // arbitrary axis algorithm - acad method of generating an arbitrary but + // consistant set of axes from a single normal ( z ) + // arbitrary x & y axes + + if ( ( fabs ( this->getx() ) < 1.0/64.0 ) && (fabs(this->gety()) < 1.0/64.0)) + x = Y_VECTOR ^ *this; + else + x = Z_VECTOR ^ *this; + + y = *this ^ x; + } + + int Vector3d::setCartesianAxes(Vector3d& b, Vector3d& c) { +#define a *this + // computes a RH triad of Axes (Cartesian) starting from a (normalised) + // if a & b are perpendicular then c = a ^ b + // if a & c are perpendicular then b = c ^ a + // if neither are perpendicular to a, then return arbitrary axes from a + + // calling sequence for RH cartesian + // x y z + // y z x + // z x y + if(a == NULL_VECTOR) FAILURE(L"SetAxes given a NULL Vector"); + double epsilon = 1.0e-09; + bool bNull = (b == NULL_VECTOR); + bool cNull = (c == NULL_VECTOR); + bool abPerp = !bNull; + if(abPerp) abPerp = (fabs(a * b) < epsilon); + + bool acPerp = !cNull; + if(acPerp) acPerp = (fabs(a * c) < epsilon); + + if(abPerp) { + c = a ^ b; + return 1; + } + + if(acPerp) { + b = c ^ a; + return 1; + } + + arbitrary_axes(b, c); + b.normalise(); + c.normalise(); + return 2; +} + + + void Plane::Mirrored(Matrix* tmMirrored) { + // calculates a mirror transformation that mirrors 2d about plane + + Point3d p1 = this->Near(Point3d(0.,0.,0.)); + if(tmMirrored->m_unit == false) tmMirrored->Unit(); + + double nx = this->normal.getx(); + double ny = this->normal.gety(); + double nz = this->normal.getz(); + + // the translation + tmMirrored->e[ 3] = -2. * nx * this->d; + tmMirrored->e[ 7] = -2. * ny * this->d; + tmMirrored->e[11] = -2. * nz * this->d; + + // the rest + tmMirrored->e[ 0] = 1. - 2. * nx * nx; + tmMirrored->e[ 5] = 1. - 2. * ny * ny; + tmMirrored->e[10] = 1. - 2. * nz * nz; + tmMirrored->e[ 1] = tmMirrored->e[ 4] = -2. * nx * ny; + tmMirrored->e[ 2] = tmMirrored->e[ 8] = -2. * nz * nx; + tmMirrored->e[ 6] = tmMirrored->e[ 9] = -2. * ny * nz; + + tmMirrored->m_unit = false; + tmMirrored->m_mirrored = true; + } +} diff --git a/src/Mod/Path/libarea/kurve/README b/src/Mod/Path/libarea/kurve/README new file mode 100644 index 0000000000..2e90f26eaf --- /dev/null +++ b/src/Mod/Path/libarea/kurve/README @@ -0,0 +1,79 @@ +libarea is a CAM-related software for profile and pocketing operation. + +This project provides library and associated python-modules to compute profile and pocket operations. + +Written by Dan Heeks + +libarea originally used kbool http://boolean.klaasholwerda.nl/bool.html written by Klaas Holwerda for calculating intersections. Since kbool uses a GPL license and HeeksCNC has a New BSD license, Dan rewrote some of the code to use the more liberally licensed (Boost license, which is compatible with LGPLv2) clipper library,written by Angus Johnson, to replace kbool. Clipper can be found here: http://sourceforge.net/p/polyclipping/code/HEAD/tree/trunk/. The clipper library is include with libarea for convenience. + +libarea is used by HeeksCNC and FreeCAD's Path workbench. + +As more evidence of the legitimate New BSD licensing of libarea, I am including an email exchange between Dan Heeks, Geoff Hawkesford, and myself. In this exchange of emails, Geoff cleared up his use of the New BSD license for the code in the 'kurve' directory. See below: + +Subject: question about kurve license +Dan Falck +1/19/12 + +to Dan Heeks: +Hi Dan, + +Do you know if g.j.hawkesford wanted his kurve code released under the +'New BSD' or original BSD license? I can't find the 'COPYING' file +that is mentioned at the beginning of all the files in /kurve . + +I want to get everything in line with 'New BSD' , Boost, and LGPL +licenses before I go too far in my FreeCAD CAM module. I plan on using +clipper instead of bool for the area code. + +Thanks, +Dan + +Dan Heeks +1/20/12 + +to me +I can't find his email. But I am sure that he has no idea that there +is a difference between BSD and "New BSD". I will ask him when I see +him next Wednesday also. + +Dan Heeks +1/30/12 + +to Geoff, me +Geoff, + +My friend, Dan Falck, from Portland Oregon, is asking about your geometry code that I released in my HeeksCNC project. +It looks like we put "BSD license", but it is more normal to put "New BSD license". +I think the difference is the removal of a clause about having to include an acknowledgement of the original source in all advertising material. +I presume you don't mind if we change it from "BSD" to "New BSD"? +http://en.wikipedia.org/wiki/BSD_licenses + + +Geoff Hawkesford +1/31/12 + +to Dan, me +That’s ok for me. +Geoff + + +From: Dan Heeks +Sent: Monday, January 30, 2012 9:34 PM +To: Geoff Hawkesford +Cc: Dan Falck +Subject: Re: question about kurve license + + +Dan Heeks +1/31/12 + +to Geoff, me +Thanks. + + +Dan Falck +1/31/12 + +to Geoff, Dan +Thank you so much. + diff --git a/src/Mod/Path/libarea/kurve/geometry.h b/src/Mod/Path/libarea/kurve/geometry.h new file mode 100644 index 0000000000..fd8f0f00bf --- /dev/null +++ b/src/Mod/Path/libarea/kurve/geometry.h @@ -0,0 +1,1021 @@ +///////////////////////////////////////////////////////////////////////////////////////// +// geometry.lib header +// modified with 2d & 3d vector methods 2006 +///////////////////////////////////////////////////////////////////////////////////////// + +/*============================== +Copyright (c) 2006 g.j.hawkesford + +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. +3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. +==============================*/ + +#pragma once +#ifdef WIN32 +#pragma warning( disable : 4996 ) +#ifndef WINVER + #define WINVER 0x501 +#endif +#endif + +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + + +namespace geoff_geometry { + + // offset methods + enum OFFSET_METHODS { + NO_ELIMINATION = 0, + BASIC_OFFSET, + ROLLINGBALL_OFFSET // unfinished + }; + + enum SPAN_IDS { + UNMARKED = 0xe0000000, + ROLL_AROUND, + INTERSECTION, + FULL_CIRCLE_KURVE + }; + + + class Vector2d; + class Vector3d; + class Point; + class Point3d; + class CLine; + class Circle; + class Span; + class Kurve; + class Line; + + + enum UNITS_TYPE{ + MM = 0, + METRES, + INCHES + }; + + extern int UNITS; // may be enum UNITS_TYPE (MM METRES or INCHES) + extern double TOLERANCE; // CAD Geometry resolution (inexact, eg. from import) + extern double TOLERANCE_SQ; // tolerance squared for faster coding. + extern double TIGHT_TOLERANCE; + extern double UNIT_VECTOR_TOLERANCE; + extern double SMALL_ANGLE; // small angle tangency test eg isConvex + extern double SIN_SMALL_ANGLE; + extern double COS_SMALL_ANGLE; + extern double RESOLUTION; // CNC resolution + + void set_Tolerances(int mode); + double mm(double value); // convert to current units from mm + +inline bool FEQ(double a, double b, double tolerance = TOLERANCE) {return fabs(a - b) <= tolerance;} +inline bool FNE(double a, double b, double tolerance = TOLERANCE) {return fabs(a - b) > tolerance;} + +inline bool FEQZ(double a, double tolerance = TIGHT_TOLERANCE) {return fabs(a) <= tolerance;} +inline bool FNEZ(double a, double tolerance = TIGHT_TOLERANCE) {return fabs(a) > tolerance;} + +#define PI 3.1415926535897932384626433832795e0 +#define DegreesToRadians (PI / 180.0e0) +#define RadiansToDegrees (180.0e0 / PI) +#define NEARLY_ONE 0.99999999999e0 +#define CPTANGENTTOL 1.0e-04 // normalised vector crossproduct tolerance sin A so A = .0057deg + +#define TANTO -1 +#define ANTITANTO 1 + +#define TANGENT 0 + +#define NEARINT 1 +#define FARINT -1 + +#define LEFTINT 1 +#define RIGHTINT -1 + +#define CFILLET 0 // corner fillet +#define CHAMFER 1 // chamfer + +#define GEOFF_LEFT 1 +#define NONE 0 +#define GEOFF_RIGHT -1 + + +#define LINEAR 0 // linear +#define ACW 1 // anti-clockwise +#define CW -1 // clockwise + + const wchar_t* getMessage(const wchar_t* original, int messageGroup, int stringID); + const wchar_t* getMessage(const wchar_t* original); // dummy + void FAILURE(const wchar_t* str); + void FAILURE(const std::wstring& str); + + enum MESSAGE_GROUPS { + GENERAL_MESSAGES, + GEOMETRY_ERROR_MESSAGES, + PARAMSPMP + }; + + enum GENERAL_MESSAGES { + MES_TITLE = 0, + MES_UNFINISHEDCODING, + MES_ERRORFILENAME, + MES_LOGFILE, + MES_LOGFILE1, + MES_P4CMENU, + MES_P4CMENUHINT + }; + + enum GEOMETRY_ERROR_MESSAGES{ // For geometry.lib + MES_DIFFSCALE = 1000, + MES_POINTONCENTRE, + MES_INVALIDARC, + MES_LOFTUNEQUALSPANCOUNT, + MES_EQUALSPANCOUNTFAILED, + MES_CANNOTTRIMSPAN, + MES_INDEXOUTOFRANGE, + MES_BAD_VERTEX_NUMBER, + MES_BAD_REF_OFFSET, + MES_BAD_SEC_OFFSET, + MES_ROLLINGBALL4AXIS_ERROR, + MES_INPUT_EQUALSPANCOUNT, + MES_INVALIDPLANE + }; + + // homogenous 4 x 4 Matrix class + class Matrix{ + protected: + public: + double e[16]; + bool m_unit; // true if unit matrix + int m_mirrored; // 1 if mirrored, 0 if not and -1 if unknown + + public: + // constructors etc... + Matrix(); // create a unit matrix + Matrix(double m[16]); // from an array + Matrix(const Matrix& m); // copy constructor + + ~Matrix(){}; + + //operators + bool operator==(const Matrix &m)const; + bool operator!=(const Matrix &m)const { return !(*this == m);} + + // methods + void Unit(); // unit matrix + void Get(double* p) const; // get the matrix into p + void Put(double*p); // put p[16] into matrix + void Translate(double x, double y, double z=0); // Translation + + void Rotate(double sinang, double cosang, Vector3d *rotAxis); // Rotation about rotAxis + void Rotate(double angle, Vector3d *rotAxis); // Rotation about rotAxis + + void Rotate(double sinang, double cosang, int Axis); // Rotation with cp & dp + void Rotate(double angle, int Axis); // Rotation with angle + + void Scale(double scale); // Scale + void Scale(double scalex, double scaley, double scalez); + + void Multiply(Matrix& m); // Multiply 2 Matrices + // void Transform(Point& p); + void Transform(double p0[3]) const; // Transform p0 thro' this matrix + void Transform(double p0[3], double p1[3]) const; // Transform p0 to p1 thro' this matrix + void Transform2d(double p0[2], double p1[2]) const; // Transform p0 to p1 thro' this matrix + + int IsMirrored(); // true if matrix has a mirror transformation + int IsUnit(); // true if matrix is unit matrix + void GetTranslate(double& x, double& y, double& z) const; // get translation from matrix + void GetScale(double& sx, double& sy, double& sz) const; // get scale from matrix + bool GetScale(double& sx) const; // get scale from matrix (true if uniform scale) + void GetRotation(double& ax, double& ay, double& az) const; // get rotation from matrix + + Matrix Inverse(); // inverts this matrix + }; + + extern Matrix UnitMatrix; // a Unit Matrix + + + // 2d Point class + class Point { + friend wostream& operator << (wostream& op, Point& p); + + public: + bool ok; // true if this point is defined correctly + double x; // x value + double y; // y value + + // constructors etc... + inline Point(){ ok=false;}; // Point p1 + inline Point( double xord, double yord, bool okay = true) { // Point p1(10,30); + x = xord; y = yord; ok = okay;} + inline Point( const Point& p ) { // copy constructor Point p1(p2); + x = p.x; y = p.y; ok = p.ok;} + Point( const Point3d& p ); // copy constructor Point p1(p2); + Point(const Vector2d& v); + + // operators + bool operator==(const Point &p)const; + bool operator!=(const Point &p)const { return !(*this == p);} + inline Point operator+(const Point &p)const{return Point(x + p.x, y + p.y);} // p0 = p1 + p2; + inline Point operator+=(const Point &p){return Point(x += p.x, y += p.y);} // p0 += p1; + Point operator+(const Vector2d &v)const; // p1 = p0 + v0; + + // destructor + //~Point(){}; + + // methods + Point Transform(const Matrix& m); // transform point + double Dist(const Point& p)const; // distance between 2 points + double DistSq(const Point& p)const; // distance squared + double Dist(const CLine& cl)const; // distance p to cl + Point Mid(const Point& p, double factor=.5)const; // mid point + void get(double xyz[2]) {xyz[0] = x; xyz[1] = y;} // return to array + }; + + +#define INVALID_POINT Point(9.9999999e50, 0, false) +#define INVALID_POINT3D Point3d(9.9999999e50, 0, 0, false) +#define INVALID_CLINE CLine(INVALID_POINT, 1, 0, false) +#define INVALID_CIRCLE Circle(INVALID_POINT, 0, false) + + // 3d point class + class Point3d { + friend wostream& operator <<(wostream& op, Point3d& p); + public: +// bool ok; // true if this point is defined correctly + double x; // x value + double y; // y value + double z; // z value + + // constructors + inline Point3d(){};// {z=0; /*ok=false;*/}; // Point p1 + inline Point3d(const double* xyz) {x = xyz[0], y = xyz[1]; z = xyz[2];} + inline Point3d( double xord, double yord, double zord = 0/*, bool okay = true*/) { // Point p1(10,30.5); + x = xord; y = yord; z = zord;/* ok = okay;*/} + inline Point3d( const Point3d& p ) { // copy constructor Point p1(p2); + x = p.x; y = p.y; z = p.z;/* ok = p.ok;*/} + inline Point3d( const Point& p ) { // copy constructor Point p1(p2); + x = p.x; y = p.y; z = 0; /*ok = p.ok;*/} + inline Point3d( const Point& p, double zord ) { // copy constructor Point p1(p2, z); + x = p.x; y = p.y; z = zord;/* ok = p.ok;*/} + Point3d(const Vector3d& v); + + // destructor +// ~Point3d(); + + // operators + bool operator==(const Point3d &p)const; + bool operator!=(const Point3d &p)const { return !(*this == p);} + Point3d operator+(const Vector3d &v)const; // p1 = p0 + v0; + + + // methods +#ifdef PEPSDLL + void ToPeps(int id, bool draw = true); // copy Point to Peps +#endif + Point3d Transform(const Matrix& m); + double Dist(const Point3d& p)const; // distance between 2 points + double DistSq(const Point3d& p)const; // distance squared between 2 points + Point3d Mid(const Point3d& p, double factor = 0.5)const; // midpoint + void get(double xyz[3]) {xyz[0] = x; xyz[1] = y; xyz[2] = z;} + double* getBuffer(){return &this->x;}; // returns ptr to data + const double* getBuffer()const{return &this->x;}; // returns ptr to data + + }; + + // 2d vector class + class Vector2d{ + friend wostream& operator <<(wostream& op, Vector2d& v); + private: + double dx, dy; + public: + + // constructors + Vector2d() {}; + inline Vector2d(const Vector2d &v) { dx = v.dx; dy = v.dy;} + Vector2d(const Vector3d &v); // careful + inline Vector2d(double x, double y) {dx = x, dy = y;} + inline Vector2d(const Point& p0, const Point& p1) {dx = p1.x - p0.x; dy = p1.y - p0.y;} + inline Vector2d(const Point *p0, const Point *p1) {dx = p1->x - p0->x; dy = p1->y - p0->y;} + inline Vector2d(const Point& p) { dx = p.x; dy = p.y;} // from 0,0 to p + inline Vector2d(double angle) {dx = cos(angle *= DegreesToRadians); dy = sin(angle);} // constructs a vector from an angle (0° - 360°) + + + // operators + inline const Vector2d& operator=(const Vector2d &v){dx = v.dx; dy = v.dy; return *this;} // v1 = v2; + inline Vector2d operator+(const Vector2d &v)const{return Vector2d(dx + v.dx, dy + v.dy);} // v2 = v0 + v1; + inline Point operator+(const Point &p)const{return Point(this->dx + p.x, this->dy + p.y);} // p1 = v0 + p0; + inline Vector2d operator+(const double d){ return Vector2d(dx + d, dy + d); }; + + inline const Vector2d& operator+=(const Vector2d &v){dx += v.dx; dy += v.dy; return *this;} // v1 += v0; + inline Vector2d operator-(const Vector2d &v)const{return Vector2d( dx - v.dx, dy - v.dy);} // v2 = v0 - v1; + inline const Vector2d& operator-=(const Vector2d &v){dx -= v.dx; dy -= v.dy; return *this;} // v1 -= v0; + inline Vector2d operator-(const double d){ return Vector2d(dx - d, dy - d); }; + + inline const Vector2d operator-(void)const{return Vector2d(-dx, -dy);} // v1 = -v0; (unary minus) + + inline const double operator*(const Vector2d &v)const{return (dx * v.dx + dy * v.dy);} // dot product m0.m1.cos a = v0 * v1 + inline Vector2d operator*(double c)const{return Vector2d(dx*c, dy*c);} // scalar product + inline const Vector2d& operator*=(double c){dx *= c; dy *= c; return *this;} // scalar product + inline Vector2d operator*(int c)const{return Vector2d(dx*(double)c, dy*(double)c);} // scalar product + + inline const double operator^(const Vector2d &v)const{return (dx * v.dy - dy * v.dx);} // cross product m0.m1.sin a = v0 ^ v1 + inline Vector2d operator~(void)const{return Vector2d(-dy, dx);} // perp to left + + bool operator==(const Vector2d &v)const; // v1 == v2 + inline bool operator!=(const Vector2d &v)const { return !(*this == v);} // v1 != v2 + + + + // methods + void get(double xyz[2]) {xyz[0] = dx; xyz[1] = dy;} // return to array + inline double getx()const{return dx;} + inline double gety()const{return dy;} + inline void putx(double x){dx = x;} + inline void puty(double y){dy = y;} + double normalise() + {double m = magnitude(); if(m < TIGHT_TOLERANCE) {dx=dy=0; return 0;} dx/=m; dy/=m; return m;} // normalise & returns magnitude + inline double magnitudesqd(void)const{return(dx * dx + dy * dy);} // magnitude squared + inline double magnitude(void)const{return(sqrt(magnitudesqd()));} // magnitude + void Rotate(double cosa, double sina){ // rotate vector by angle + double temp = -dy * sina + dx * cosa; + dy = dx * sina + cosa * dy; + dx = temp; + } + inline void Rotate(double angle) { if(FEQZ(angle) == true) return; Rotate(cos(angle), sin(angle));} + void Transform( const Matrix& m); // transform vector + + // destructor + //~Vector2d(){} + + }; + + + // 3d vector class + class Vector3d{ + friend wostream& operator <<(wostream& op, Vector3d& v); + private: + double dx, dy, dz; + public: + + // constructors + Vector3d() {}; + Vector3d(const Vector3d &v) { dx = v.dx; dy = v.dy; dz = v.dz;} + Vector3d(double x, double y, double z = 0) {dx = x, dy = y; dz = z;} + Vector3d(const double* x) {dx = x[0], dy = x[1]; dz = x[2];} + Vector3d(const double* x0, const double* x1) {dx = x1[0] - x0[0], dy = x1[1] - x0[1]; dz = x1[2] - x0[2];} + Vector3d(const Point3d& p0, const Point3d& p1) {dx = p1.x - p0.x; dy = p1.y - p0.y; dz = p1.z - p0.z;} + Vector3d(const Point3d& p) { dx = p.x; dy = p.y; dz = p.z;} // from 0,0,0 to p + Vector3d(const Vector2d& v) {dx = v.getx(); dy = v.gety(); dz = 0;} + + // operators + bool operator==(const Vector3d &v)const { return(FEQ(dx, v.dx, UNIT_VECTOR_TOLERANCE) && FEQ(dy, v.dy, UNIT_VECTOR_TOLERANCE) && FEQ(dz, v.dz, UNIT_VECTOR_TOLERANCE)); } // v1 == v2 (unit only!) + bool operator!=(const Vector3d &v)const { return (!(*this == v)); } // v1 != v2 + const Vector3d& operator=(const Vector3d &v){dx = v.dx; dy = v.dy; dz = v.dz;return *this;} // v1 = v2; + // const Vector3d& operator=(const Vector2d &v){dx = v.getx(); dy = v.gety(); dz = 0.0;return *this;} // v1 = v2; + inline Point3d operator+(const Point3d &p)const{return Point3d(dx + p.x, dy + p.y, dz + p.z);} // p1 = v0 + p0; + Vector3d operator+(const Vector3d &v)const{return Vector3d(dx + v.dx, dy + v.dy, dz + v.dz);} // v2 = v0 + v1; + const Vector3d& operator+=(const Vector3d &v){dx += v.dx; dy += v.dy; dz += v.dz; return *this;} // v1 += v0; + Vector3d operator-(const Vector3d &v)const{return Vector3d( dx - v.dx, dy - v.dy, dz - v.dz);} // v2 = v0 - v1; + const Vector3d& operator-=(const Vector3d &v){ + dx -= v.dx; dy -= v.dy; dz -= v.dz; return *this;} // v1 -= v0; + + const Vector3d operator-(void)const{return Vector3d(-dx, -dy, -dz);} // v1 = -v0; (unary minus) + + const double operator*(const Vector3d &v)const{return (dx * v.dx + dy * v.dy + dz * v.dz);} // dot product m0 m1 cos a = v0 * v1 + + const Vector3d& operator*=(double c){dx *= c; dy *= c; dz *= c; return *this;} // scalar products + friend const Vector3d operator*(const Vector3d &v, double c){return Vector3d(v.dx*c, v.dy*c, v.dz*c);} + friend const Vector3d operator*(double c, const Vector3d &v){return Vector3d(v.dx*c, v.dy*c, v.dz*c);} + friend const Vector3d operator/(const Vector3d &v, double c){return Vector3d(v.dx/c, v.dy/c, v.dz/c);} + + const Vector3d operator^(const Vector3d &v)const{ + return Vector3d(dy * v.dz - dz * v.dy, dz * v.dx - dx * v.dz, dx * v.dy - dy * v.dx);} // cross product vector + + // = the vector perp to the plane of the 2 vectors + // the z component magnitude is m0.m1.sin a + // methods + inline void get(double xyz[3])const {xyz[0] = dx; xyz[1] = dy; xyz[2] = dz;} // return to array + inline double getx()const{return dx;} + inline double gety()const{return dy;} + inline double getz()const{return dz;} + inline void putx(double x){dx = x;} + inline void puty(double y){dy = y;} + inline void putz(double z){dz = z;} + double normalise(){double m = magnitude(); if(m < 1.0e-09) {dx=dy=dz=0; return 0;} dx/=m; dy/=m; dz/=m; // normalise & returns magnitude + return m;} + inline double magnitude(void)const{return(sqrt(dx * dx + dy * dy + dz * dz));} // magnitude + inline double magnitudeSq(void)const{return(dx * dx + dy * dy + dz * dz);} // magnitude squared + void Transform( const Matrix& m); // transform vector + void arbitrary_axes(Vector3d& x, Vector3d& y); + int setCartesianAxes(Vector3d& b, Vector3d& c); + double* getBuffer(){return &this->dx;}; // returns ptr to data + const double* getBuffer()const{return &this->dx;}; // returns ptr to data + + // destructor + //~Vector3d(){} + + }; + +#define ORIGIN Point3d(0,0,0) +#define NULL_VECTOR Vector3d(0,0,0) +#define Z_VECTOR Vector3d(0,0,1) +#define Y_VECTOR Vector3d(0,1,0) +#define X_VECTOR Vector3d(1,0,0) + + // 2D cline x = x0 + t * dx; y = y0 + t * dy + class CLine{ + friend wostream& operator <<(wostream& op, CLine& cl); + public: + bool ok; + Point p; + Vector2d v; + + // constructors + inline CLine() {ok = false;}; + inline CLine(const Point& p0, double dx, double dy, bool normalise = true){ p = p0; v = Vector2d(dx, dy); if(normalise) Normalise();}; + inline CLine(const Point& p0, const Vector2d& v0, bool normalise = true) {p = p0; v = v0; if(normalise) Normalise();}; + inline CLine( const CLine& s ) {p = s.p; v = s.v;}; // copy constructor CLine s1(s2); + inline CLine(const Point& p0, const Point& p1) {p = p0; v = Vector2d(p0, p1); Normalise();}; + CLine(const Span& sp); + + // operators + const CLine operator~(void);// perp to left + const CLine operator=(const Point& p0){p.x=p0.x; p.y=p0.y; return *this;}; // s = p; + + // methods + double c(); // returns c + void Normalise(); // normalise dx,dy +#ifdef PEPSDLL + void ToPeps(int id, bool draw = true); // to Peps + void DelPeps(int id); // delete Peps CLine +#endif + CLine Transform(Matrix& m); // transform a CLine + Point Intof(const CLine& s); // intersection of 2 clines + Point Intof(int NF, const Circle& c); // intersection of cline & circle + Point Intof(int NF, const Circle& c, Point& otherInters); double Dist(const Point& p1)const; // ditto & other intersection + CLine Bisector(const CLine& s); // Bisector of 2 Clines + + // destructor +// ~CLine(); + }; + +#define HORIZ_CLINE CLine(geoff_geometry::Point(0,0), 1.0, 0.0, true) + + + // 2D circle + class Circle{ + friend wostream& operator <<(wostream& op, Circle& c); + public: + bool ok; + Point pc; + double radius; + + // constructors etc... + inline Circle() {ok = false;}; + Circle( const Point& p, double r, bool okay = true); // Circle c1(Point(10,30), 20); + Circle( const Point& p, const Point& pc); // Circle c1(p[222], p[223]); + Circle( const Circle& c ){*this = c;} // copy constructor Circle c1(c2); + Circle( const Span& sp); // constructor + + // methods +#ifdef PEPSDLL + void ToPeps(int id, bool draw = true); // to Peps + void DelPeps(int id); // delete Peps Circle +#endif + bool operator==(const Circle &c)const; // c == cc + bool operator!=(const Circle &c)const { return !(*this == c);} + Circle Transform(Matrix& m); // transform a Circle + Point Intof(int LR, const Circle& c1); // intof 2 circles + Point Intof(int LR, const Circle& c1, Point& otherInters); // intof 2 circles, (returns the other intersection) + int Intof(const Circle& c1, Point& leftInters, Point& rightInters); // intof 2 circles (returns number of intersections & left/right inters) + CLine Tanto(int AT, double angle, const CLine& s0)const; // a cline tanto this circle at angle + // ~Circle(); // destructor + }; + + // 2d box class + class Box{ + public: + Point min; + Point max; + bool ok; + + Box() { min.x = min.y = 1.0e61; max.x = max.y = -1.0e61; ok = false;}; + Box(Point& pmin, Point& pmax) { min = pmin; max = pmax; ok = true;}; + + bool outside(const Box& b)const; // returns true if box is outside box + void combine(const Box& b); // combines this with b + }; + + // 3d box class + class Box3d{ + public: + Point3d min; + Point3d max; + bool ok; + + Box3d() { min.x = min.y = min.z = 1.0e61; max.x = max.y = max.z = -1.0e61; ok = false;}; + Box3d(const Point3d& pmin, const Point3d& pmax) { min = pmin; max = pmax; ok = true;}; + + bool outside(const Box3d& b)const; // returns true if box is outside box + void combine(const Box3d& b); // combines this with b + }; + + inline void MinMax(const Point& p, Point& pmin, Point& pmax) { + if(p.x > pmax.x) pmax.x = p.x; + if(p.y > pmax.y) pmax.y = p.y; + if(p.x < pmin.x) pmin.x = p.x; + if(p.y < pmin.y) pmin.y = p.y; + }; + + inline void MinMax(const Point3d& p, Point3d& pmin, Point3d& pmax) { + if(p.x > pmax.x) pmax.x = p.x; + if(p.y > pmax.y) pmax.y = p.y; + if(p.z > pmax.z) pmax.z = p.z; + if(p.x < pmin.x) pmin.x = p.x; + if(p.y < pmin.y) pmin.y = p.y; + if(p.z < pmin.z) pmin.z = p.z; + }; + + + + + // 2D line arc span + class Span{ + friend wostream& operator <<(wostream& op, Span& span); + public: + Point p0; // start + Point p1; // end + Point pc; // centre + int dir; // arc direction (CW or ACW or 0 for straight) + int ID; // ID (for offset in wire - stores spanID etc. from original kurve) + bool ok; + + bool returnSpanProperties; // set if properties below are set + Vector2d vs; // direction at start or for straight + Vector2d ve; // direction at span end + + double length; // span length + double radius; // arc radius + double angle; // included arc angle ( now arc is parameterised start -> start + angle + + Box box; // span box + + bool NullSpan; // true if small span + + // methods + void SetProperties(bool returnProperties); // set span properties + Span Offset(double offset); // offset span method + int Split(double tolerance); // returns number of splits + void SplitMatrix(int num_vectors, Matrix* matrix); // returns incremental matrix from split + void minmax(Box& box, bool start = true); // minmax of span + void minmax(Point& pmin, Point& pmax, bool start = true); // minmax of span + int Intof(const Span& sp, Point& pInt1, Point& pInt2, double t[4])const; + void Transform(const Matrix& m, bool setprops = true); + Point Near(const Point& p)const; // returns the near point to span from p (on or off) + Point NearOn(const Point& p)const; // returns the near point to span from p (on span) + Point Mid()const; // midpoint of a span + Point MidPerim(double d)const; // interior point of Span (param 0 - d) + Point MidParam(double param)const; // interior point of Span (param 0 - 1) + bool OnSpan(const Point& p)const; // tests if p is on sp *** FAST TEST p MUST LIE on unbounded span + bool OnSpan(const Point& p, double* t)const; // tests if p is on sp *** FAST TEST p MUST LIE on unbounded span + bool JoinSeparateSpans(Span& sp); + Span BlendTwoSpans(Span& sp2, double radius, double maxt); // Blends 2 Spans + bool isJoinable(const Span& sp)const; // is this & sp joinable to 1 span? + Vector2d GetVector(double fraction)const; // the direction along the span, 0.0 for start, 1.0 for end + + // constructor + Span() {ID = 0; ok = false;}; + Span(int spandir, const Point& pn, const Point& pf, const Point& c) { dir = spandir; p0 = pn, p1 = pf, pc = c; ID = 0; SetProperties(true); ok = p0.ok;}; + + // operators + // bool operator==(const Span &sp)const; + // bool operator!=(const Span &sp)const { return !(*this == sp);} + }; + + // general + double atn360(double dx, double dy); // angle 0 to 2pi + + // distance functions + //double Dist(double px, double py, double p1x, double p1y); // diatance between 2 points (2d) + //double Dist(Point& p0, Point& p1); // distance between 2 points (3d) + //double Dist(CLine& s, Point& p1); // distance between cline & point + + double Dist(const Point3d *p, const Vector3d *vl, const Point3d *pf); // distance from line (p, vl) and pf + double DistSq(const Point3d *p, const Vector3d *vl, const Point3d *pf); // distance squared from line (p, vl) and pf + double Dist(const Circle& c, const Point& p); // distance between c & p + double Dist(const Point& p0, const Circle& c, const Point& p1); // clockwise distance around c from p0 to p1 + double Dist(const CLine& s, const Circle& c); // distance between line and circle + double Dist(const Circle& c0, const Circle& c1); // distance between 2 circles + double IncludedAngle(const Vector2d& v0, const Vector2d& v1, int dir = 1); // angle between 2 vectors + double IncludedAngle(const Vector3d& v0, const Vector3d& v1, const Vector3d& normal, int dir = 1); + inline double IncludedAngle(const CLine& s0, const CLine& s1, int dir = 1) { // angle between 2 Clines + return IncludedAngle(s0.v, s1.v, dir); + } + + + // point definitions + Point Mid(const Point& p0, const Point& p1, double factor = 0.5); //// midpoint + Point Mid(const Span& sp); //// midpoint of a span + Point Rel(const Point& p, double x, double y); // relative point + Point Polar(const Point& p, double angle, double r); // polar from this point + Point AtAngle(const Circle& c, double angle); // Point at angle on a circle + Point XonCLine(const CLine& s, double xval); // returns point that has X on this line + Point YonCLine(const CLine& s, double yval); // returns point that has Y on this line + Point Intof(const CLine& s0, const CLine& s1); //// intof 2 clines + Point Intof(int NF, const CLine& s, const Circle& c); //// intof of circle & a cline + Point Intof(int NF, const CLine& s, const Circle& c, Point& otherInters); //// intof of circle & a cline (returns the other intersection) + Point Intof(int LR, const Circle& c0, const Circle& c1); //// intof 2 circles + Point Intof(int LR, const Circle& c0, const Circle& c1, Point& otherInters); //// intof 2 circles, (returns the other intersection) + int Intof(const Circle& c0, const Circle& c1, Point& pLeft, Point& pRight); //// ditto + Point Along(const CLine& s, double d); // distance along Cline + Point Along(const CLine& s, double d, const Point& p); // distance along Cline from point + Point Around(const Circle& c, double d, const Point& p); // distance around a circle from point + Point On(const CLine& s, const Point& p); // returns a point on s nearest to p + Point On(const Circle& c, const Point& p); // returns a point on c nearest to p + + // cline definitons + + CLine AtAngle(double angle, const Point& p, const CLine& s = HORIZ_CLINE); // cline at angle to line thro' point + CLine Tanto(int AT, const Circle& c, double angle, const CLine& s0 = HORIZ_CLINE);//// cline tanto circle at angle to optional cline + CLine Tanto(int AT, const Circle& c, const Point& p); // cline tanto circle thro' a point + CLine Tanto(int AT0, const Circle& c0, int AT1, const Circle& c1); // cline tanto 2 circles + CLine Normal(const CLine& s); // noirmal to cline + CLine Normal(const CLine& s, const Point& p); // normal to cline thro' p + CLine Parallel(int LR, const CLine& s, double distance); // parallel to cline by distance + CLine Parallel(const CLine& cl, const Point& p); // parallel to cline thro' a point + + + // circle definitions + Circle Thro(const Point& p0, const Point& p1); // circle thro 2 points (diametric) + Circle Thro(const Point& p0, const Point& p1, const Point& p2); // circle thro 3 points + Circle Tanto(int NF, const CLine& s0, const Point& p, double rad); // circle tanto a CLine thro' a point with radius + Circle Thro(int LR, const Point& p0, const Point& p1, double rad); // circle thro' 2 points with radius + Circle Tanto(int AT1, const CLine& s1, int AT2, const CLine& s2, double rad); // circle tanto 2 clines with radius + Circle Tanto(int AT1, const CLine& s1, int AT2, const CLine& s2, int AT3, const CLine& s3); // circle tanto 3 clines + Circle Tanto(int LR, int AT, const Circle& c, const Point& p, double rad); // circle tanto circle & thro' a point + Circle Tanto(int NF, int AT0, const CLine& s0, int AT1, const Circle& c1, double rad);// circle tanto cline & circle with radius + Circle Tanto(int LR, int AT0, const Circle& c0, int AT1, const Circle& c1, double rad);// circle tanto 2 circles with radius + Circle Tanto(int LR, int AT1 , const Circle& c1 , int AT2 , const Circle& c2, int AT3 , const Circle c3); // tanto 3 circles + int apolloniusProblem(int AT1 , const Circle& c1 , int AT2 , const Circle& c2, int AT3 , const Circle& c3, Circle& Solution1, Circle& Solution2); + int apolloniusProblem(int AT1 , const Circle& c1 , int AT2 , const Circle& c2, int AT3 , const CLine& cl3, Circle& Solution1, Circle& Solution2); + int apolloniusProblem(int AT1 , const Circle& c1 , int AT2 , const CLine& cl2, int AT3 , const CLine& cl3, Circle& Solution1, Circle& Solution2); + + // Circle Tanto(int AT0, int NF, int AT1, CLine s1, int AT2, CLine s2); // circle tanto circle, and 2 clines + Circle Parallel(int LR, const Circle& c, double distance); // parallel to circle by a distance + + + // misc + inline double Radians(double degrees) {return degrees * PI / 180;} + inline double Degrees(double radians) { return radians * 180 / PI;} + int quadratic(double a, double b, double c, double& x0, double& x1); // solve quadratic + + int corner(const Vector2d& v0, const Vector2d& v1, double cpTol = CPTANGENTTOL); // corner (TANGENT, LEFT, RIGHT) + inline int corner(const Span& span, const Span& next, double cpTol = CPTANGENTTOL) { + return corner((Vector2d)span.ve, (Vector2d)next.vs, cpTol);} + + Line IsPtsLine(const double* a, int n, double tolerance, double* deviation); +// Span3d IsPtsSpan3d(const double* a, int n, double tolerance, double* deviation); + + class Plane { + friend wostream& operator <<(wostream& op, Plane& pl); + + public: // ax + by + cz + d = 0 + bool ok; + double d; // distance of plane to origin + Vector3d normal; // normal to plane a = n.dx, b = n.dy, c = n.dz + // constructors + Plane(){ok = false;}; + Plane(double dist, const Vector3d& n); + Plane(const Point3d& p0, const Point3d& p1, const Point3d& p2); + Plane(const Point3d& p0, const Vector3d& n, bool normalise = true); + + // methods + double Dist(const Point3d& p)const; // signed distance of point to plane + bool Intof(const Line& l, Point3d& intof, double& t)const; // intersection of plane & line (0 >= t <= 1 if intersect within line) + bool Intof(const Plane& pl, Line& intof)const; // intersection of 2 planes + bool Intof(const Plane& pl0, const Plane& pl1, Point3d& intof)const; // intersection of 3 planes + Point3d Near(const Point3d& p)const; // returns near point to p on the plane + void Mirrored(Matrix* m); // returns a matrix for a mirror about this + }; + + + + +#define SPANSTORAGE 32 // lessens number of object pointers + + class spVertex { + friend wostream& operator <<(wostream& op, spVertex& sp); + + public: + int type; + int spanid; + Point p; + Point pc; + spVertex(){}; + spVertex(int t, const Point& point, const Point& centre): type(t), spanid(0), p(point), pc(centre){}; + + bool operator==(spVertex &spv){ + // vertex == spvertex (vertex check - doesn't check spannid!) + if(this->type != spv.type) return false; + if(this->p != spv.p) return false; + if(this->type != LINEAR) { + if(this->pc != spv.pc) return false; + } + return true; + } + + bool operator!=(spVertex &spv){ return !(*this == spv);} + + }; + + + class SpanDataObject { + // holds everything needed for Post-Processing/Simulation + public: + int method; // holds method type + + SpanDataObject(int meth){method = meth;}; + SpanDataObject(const SpanDataObject* obj){method = obj->method;}; + }; + + class SpanVertex{ + public: + int type[SPANSTORAGE]; // LINEAR CW or ACW // 0 straight (cw = -1 (T) acw = 1 (A) ) + int spanid[SPANSTORAGE]; // identification (eg wire offset span info) + const SpanDataObject* index[SPANSTORAGE]; // other - pointer to + double x[SPANSTORAGE], y[SPANSTORAGE]; // vertex + double xc[SPANSTORAGE], yc[SPANSTORAGE]; // centre of arc + public: + // methods + void Add(int offset, int type, const Point& p0, const Point& pc, int ID = UNMARKED); + const SpanDataObject* GetIndex(int offset)const; + void AddSpanID(int offset, int ID); + SpanVertex(); + ~SpanVertex(); + const SpanVertex& operator= (const SpanVertex& spv ); + + void Add(int offset, const SpanDataObject* Index ); + const SpanDataObject* Get(int offset); + int Get(int offset, Point& pe, Point& pc); + int GetSpanID(int offset); + }; + + + + + +#ifdef WIN32 +#pragma warning(disable:4522) +#endif + + class Kurve : public Matrix{ + friend wofstream& operator << (wofstream& op, Kurve& k); + friend wifstream& operator >> (wifstream& op, Kurve& k); + + protected: + vector m_spans; + bool m_started; + int m_nVertices; // number of vertices in Kurve + bool m_isReversed; // true if get spans reversed + + public: + // for comparing kurves + struct spanCompare { + int dir; // LINEAR, CW or ACW + double length; // length of the span + double cp; // cross-product to next span (sina) + double dp; + }; + // constructors etc... + Kurve() { + m_started = false; + m_nVertices = 0; + m_isReversed = false; + }; + Kurve(const Kurve& k0); + const Kurve& operator= (const Kurve& k ); + const Kurve& operator=(const Matrix &m); + + bool operator==(const Kurve &k)const; // k == kk (vertex check) + bool operator!=(const Kurve &k)const { return !(*this == k);} + + + // destructor + ~Kurve(); + + // methods + inline int nSpans( )const {return (m_nVertices)? m_nVertices - 1 : 0;} // returns the number of spans + bool Closed()const; // returns true if kurve is closed + inline bool Started()const {return m_started;}; + void FullCircle(int dir, const Point& c, double radius); // make a full circle + void Start(); // start a new kurve + void Start(const Point& p); // start a new kurve with start point + bool Add(const spVertex& spv, bool AddNullSpans = true); // add a vertex + void Get(int vertex, spVertex& spv) const; // get a vertex + bool Add(const Span& sp, bool AddNullSpans = true); // add a span + bool Add(int type, const Point& p0, const Point& pc, bool AddNullSpans = true); // a span + void AddSpanID(int ID); + bool Add(const Point& p0, bool AddNullSpans = true); // linear + void Add(); // add a null span + void Add(const Kurve* k, bool AddNullSpans = true); // a kurve + void StoreAllSpans(std::vector& kSpans)const; // store all kurve spans in array, normally when fast access is reqd + void Clear(); // remove all the spans + + void Replace(int vertexnumber, const spVertex& spv); + void Replace(int vertexnumber, int type, const Point& p, const Point& pc, int ID = UNMARKED); + int GetSpanID(int spanVertexNumber) const; // for spanID (wire offset) + int Get(int spanVertexNumber, Point& p, Point& pc) const; + void Get(std::vector *all, bool ignoreNullSpans) const; // get all spans to vector + int Get(int spanVertexNumber, Point3d& p, Point3d& pc) const + { Point p2d, pc2d; int d = Get(spanVertexNumber, p2d, pc2d); p = p2d; pc = pc2d; return d;} + int Get(int spannumber, Span& sp, bool returnSpanProperties = false, bool transform = false) const; +// int Get(int spannumber, Span3d& sp, bool returnSpanProperties = false, bool transform = false) const; + void Get(Point &ps,Point &pe) const; // returns the start- and endpoint of the kurve + const SpanDataObject* GetIndex(int vertexNumber)const; + inline double GetLength()const{ return Perim();}; // returns the length of a kurve + + void minmax(Point& pmin, Point& pmax); // minmax of span + void minmax(Box& b); + + Point NearToVertex(const Point& p, int& nearSpanNumber)const; + Point NearToVertex(const Point& p)const { int nearSpanNumber; return NearToVertex(p, nearSpanNumber);}; + Point Near(const Point& p, int& nearSpanNumber)const; + Point Near(const Point& p) const{ int nearSpanNumber; return Near(p, nearSpanNumber);}; + double Perim()const; // perimeter of kurve + double Area()const; // area of closed kurve + void Reverse(); // reverse kurve direction - obsolete + bool Reverse(bool isReversed) { // reverse kurve direction - later better method + bool tmp = m_isReversed; + m_isReversed = isReversed; + return tmp; + }; + int Reduce(double tolerance); // reduce spans which are in tolerance + + int Offset(vector &OffsetKurves, double offset, int direction, int method, int& ret)const; // offset methods + int OffsetMethod1(Kurve& kOffset, double off, int direction, int method, int& ret)const; + int OffsetISOMethod(Kurve& kOffset, double off, int direction, bool BlendAll)const; // special offset (ISO radius - no span elimination) + int Intof(const Span& sp, vector& p)const; // intof span + int Intof(const Kurve&k, vector& p)const; // intof kurve + bool Compare(const Kurve* k, Matrix* m, bool bAllowMirror = true)const; // compare 2 Kurves + void ChangeStart(const Point *pNewStart, int startSpanno); // change the Kurve's startpoint + void ChangeEnd(const Point *pNewEnd, int endSpanno); // change the Kurve's endpoint + + private: + bool compareKurves(const std::vector &first, const std::vector &second, int &nOffset/*, Kurve *k, Matrix *m*/)const; + bool calculateMatrix(const Kurve *k, Matrix *m, int nOffset, bool bMirror = false)const; + public: + + + void AddIndex(int vertexNumber, const SpanDataObject* data); + bool Split(double MaximumRadius, double reslution); // split arcs larger than MaximumRadius to resoultion + int IntExtWire( Kurve& kSec, double Ref, double Sec, double height, Kurve* kOut); // interpolate / extrapolate a mid height kurve (wire) + void SetZ(double z) { e[11] = z; if(fabs(z) > 1.0e-6) m_unit = false;} // assigns kurve to fixed height (wire) + + void Part(int startVertex, int EndVertex, Kurve *part); + Kurve Part(int fromSpanno, const Point& fromPt, int toSpanno, const Point& toPt); // make a Part Kurve + int Break(double atParam, const Kurve *secInput, Kurve *refOut, Kurve *secOut);// break kurve perimeter parameterisation with synchronised Kurve (wire) + void Part(double fromParam, double toParam, const Kurve *secInput, Kurve *refOut, Kurve *secOut);// part kurve perimeter parameterisation with synchronised Kurve (wire) + Kurve Part(double fromParam, double toParam); // part kurve perimeter parameterisation + void AddSections(const Kurve* k, bool endOfSection); // special add kurves for rollingball + void AddEllipse(int dir, const Point& pStart, const Point& pEnd, const Point& pCentre, const Vector2d& majorAxis, double majorRadius, double minorRadius, double tolerance); +// void Kurve::AddEllipse(int dir, Plane *plEllipse, Vector3d *cylAxis, Point3d *cylCentre, double cylradius, Point3d *pStart, Point3d *pEnd, double tolerance); /// elliptical curve - biarc in tolerance + + void Spiral(const Point& centre, double startAngle, double startRadius, double radiusRisePerRevolution, double endRadius); +#ifdef PARASOLID + int ToPKcurve(PK_CURVE_t *curves, PK_INTERVAL_t *ranges, int start_spanno, int n_spans); // Convert to PK Curve + + PK_BODY_t ToPKwire(); // Convert to PK Wire Body + PK_BODY_t ToPKwire(int start_spanno, int n_spans); + + PK_BODY_t ToPKsheet( ); // Convert to PK Sheet Body + PK_BODY_t ToPKextrudedBody(PK_VECTOR1_t path, bool solidbody = true); + // Convert to PK Body (open kurve >> sheet) + PK_BODY_t ToPKlofted_sheet_body(Kurve &sec); // Convert 2 kurves to lofted sheet body + PK_BODY_t ToPKlofted_thickened_body(Kurve &sec, double thickness); +#endif + }; +#ifdef WIN32 +#pragma warning(default:4522) +#endif + + void tangential_arc(const Point &p0, const Point &p1, const Vector2d &v0, Point &c, int &dir); + + int EqualiseSpanCount(Kurve& k1, Kurve& k2, Kurve& k1equal, Kurve& k2equal, bool equalise_same_span_count); // span count equalisation + void EqualiseSpanCountAfterOffset(Kurve& k1, Kurve&k2, Kurve& k1Out, Kurve& k2Out);// span equalisation after offset + void EqualiseSpanCountAfterOffsetFromRollAround(Kurve& k1, Kurve&k2, Kurve& k1Out, Kurve& k2Out/*, double offset, int arc_direction*/);// span equalisation after offset + + Point IntofIso(Span& one, Span& two, Span& three); // for iso blend radiuses - calc intersection + + inline double CPTOL(double offset, double maxOffset) { + // this returns a suitable tolerance for a cross product + // the cp for normalised vectors is the sin of the included angle between the vectors + // + // this function takes the machine resolution from RESOLUTION + + offset = fabs(offset); + + if(offset <= RESOLUTION) offset = maxOffset; // no known offset so guess one from the application + + return RESOLUTION / offset; + } + + + + // finite Span routines + int Intof(const Span& sp0 , const Span& sp1, Point& p0, Point& p1, double t[4]); + int LineLineIntof(const Span& L0 , const Span& L1, Point& p, double t[2]); + int LineArcIntof(const Span& line, const Span& arc, Point& p0, Point& p1, double t[4]); + int ArcArcIntof(const Span& arc0, const Span& arc1, Point& pLeft, Point& pRight, double t[4]); + + bool OnSpan(const Span& sp, const Point& p); + bool OnSpan(const Span& sp, const Point& p, bool nearPoints, Point& pNear, Point& pOnSpan); // function returns true if pNear == pOnSpan + // pNear (nearest on unbound span) + // pOnSpan (nearest on finite span) + + + int Intof(const Line& v0, const Line& v1, Point3d& intof); // intof 2 lines + double Dist(const Line& l, const Point3d& p, Point3d& pnear, double& t); // distance from a point to a line + Point3d Near(const Line& l, const Point3d& p, double& t ); // near point to a line & t in 0-length range + double Dist(const Span& sp, const Point& p , Point& pnear ); // distance from p to sp, nearpoint returned as pnear + +// Kurve splineUsingBiarc(CLine& cl0, CLine& cl1, std::vector); + + int biarc(CLine& cl0, CLine& cl1, Span* sp0, Span* sp1 ); + + // 3d line segment + class Line{ + public: + Point3d p0; // start + Vector3d v; // vector (not normalised) + double length; // line length + Box3d box; + bool ok; + + // constructors + Line() {ok = false;}; + Line(const Point3d& p0, const Vector3d& v0, bool boxed = true); + Line(const Point3d& p0, const Point3d& p1); + Line(const Span& sp); + + // methods + void minmax(); + Point3d Near(const Point3d& p, double& t)const; // near point to line from point (0 >= t <= 1) in range + int Intof(const Line& l, Point3d& intof)const {return geoff_geometry::Intof(*this, l, intof);}; // intof 2 lines + bool atZ(double z, Point3d& p)const; // returns p at z on line + bool Shortest(const Line& l2, Line& lshort, double& t1, double& t2)const; // calculate shortest line between this & l2 + }; + + +class Triangle3d { + Point3d vert1; // first vertex + Point3d vert2; // second vertex + Point3d vert3; // third vertex + Vector3d v0; // vector from vert1 to vert2 + Vector3d v1; // vector from vert1 to vert3 + bool ok; + + Box3d box; // box around triangle + +public: + // constructor + Triangle3d(){ ok = false;}; + Triangle3d(const Point3d& vert1, const Point3d& vert2, const Point3d& vert3); + + // methods + bool Intof(const Line& l, Point3d& intof)const; // returns intersection triangle to line +}; + + + +} // End namespace geoff_geometry + + + + diff --git a/src/Mod/Path/libarea/kurve/kurve.cpp b/src/Mod/Path/libarea/kurve/kurve.cpp new file mode 100644 index 0000000000..39f8d3f1cc --- /dev/null +++ b/src/Mod/Path/libarea/kurve/kurve.cpp @@ -0,0 +1,1534 @@ +//////////////////////////////////////////////////////////////////////////////////////////////// +// kurve +//////////////////////////////////////////////////////////////////////////////////////////////// + +/*============================== +Copyright (c) 2006 g.j.hawkesford + +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. +3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. +==============================*/ + +#include "geometry.h" +using namespace geoff_geometry; + +#ifdef PEPSPOST + #include "postoutput.h" +#endif + +namespace geoff_geometry { + + SpanVertex::SpanVertex() { + for(int i = 0; i < SPANSTORAGE; i++) index[i] = NULL; + } + + SpanVertex::~SpanVertex() { +#ifndef PEPSDLL + // don't know what peps did about this? + for(int i = 0; i < SPANSTORAGE; i++) { + if(index[i] != NULL) { + delete index[i]; + } + } +#endif + } + + const SpanVertex& SpanVertex::operator= (const SpanVertex& spv ){ + /// + + memcpy(x, spv.x, SPANSTORAGE * sizeof(double)); + memcpy(y, spv.y, SPANSTORAGE * sizeof(double)); + memcpy(xc, spv.xc, SPANSTORAGE * sizeof(double)); + memcpy(yc, spv.yc, SPANSTORAGE * sizeof(double)); + + for(unsigned int i = 0; i < SPANSTORAGE; i++) { + type[i] = spv.type[i]; + spanid[i] = spv.spanid[i]; + index[i] = spv.index[i]; +#ifndef PEPSDLL + if(index[i] != NULL) { + SpanDataObject* obj = new SpanDataObject(index[i]); + index[i] = obj; + } +#endif + } + return *this; + } + + + void SpanVertex::Add(int offset, int spantype, const Point& p, const Point& pc, int ID) + { + type[offset] = spantype; +// index[offset] = NULL; + x[offset] = p.x; + y[offset] = p.y; + xc[offset] = pc.x; + yc[offset] = pc.y; + spanid[offset] = ID; + } + void SpanVertex::AddSpanID(int offset, int ID) + { + spanid[offset] = ID; + } + +#if PEPSDLL + void SpanVertex::Add(int offset, WireExtraData* Index ) + { + index[offset] = Index; + } + WireExtraData* SpanVertex::Get(int offset) + { + return index[offset]; + } +#else + void SpanVertex::Add(int offset, const SpanDataObject* Index ) { + index[offset] = Index; + } + + const SpanDataObject* SpanVertex::GetIndex(int offset) const{ + return index[offset]; + } +#endif + + int SpanVertex::Get(int offset, Point& pe, Point& pc) + { + pe = Point(x[offset], y[offset]); + pc = Point(xc[offset], yc[offset]); + + return type[offset]; + } + int SpanVertex::GetSpanID(int offset) + { + return spanid[offset]; + } + + Span Span::Offset(double offset) + { + Span Offsp = *this; + if(FNEZ(offset) && !NullSpan) { + if ( !dir ) { + // straight + Offsp.p0.x -= offset * vs.gety(); + Offsp.p0.y += offset * vs.getx(); + + Offsp.p1.x -= offset * vs.gety(); + Offsp.p1.y += offset * vs.getx(); + } + else { + // circular span + // double coffset = (double) dir * offset; + Offsp.p0.x -= vs.gety() * offset; + Offsp.p0.y += vs.getx() * offset; + + Offsp.p1.x -= ve.gety() * offset; + Offsp.p1.y += ve.getx() * offset; + +// Offsp.radius -= dir * offset; + } + Offsp.SetProperties(true); + } + return Offsp; + } + + bool Span::JoinSeparateSpans(Span& sp) { + // this method joins this span to sp where they are separated normally by an offset from original + // + // parameters:- + // Input sp near span + // Output this->p1 and sp.p0 assigned to the spans intersection + Point inters; + int turnLeft = ((this->ve ^ sp.vs) > 0)? 1 : -1; + if(!this->dir) { + CLine one(*this); + if(!sp.dir) { + // line line + CLine two(sp); + inters = one.Intof(two); + } + else { + // line arc + Circle two(sp); + inters = one.Intof(-turnLeft * sp.dir, two); + } + } + else { + Circle one(*this); + if(!sp.dir) { + // arc line + CLine two(sp); + inters = two.Intof(turnLeft * this->dir, one); + } + else { + // arc arc + Circle two(sp); + inters = one.Intof(-turnLeft * this->dir * sp.dir, two); + } + } + if(inters.ok) { + this->p1 = sp.p0 = inters; + this->SetProperties(true); + sp.SetProperties(true); + } + return inters.ok; + } + static int Split(double tolerance, double angle, double radius, int dir); + int Span::Split(double tolerance) { + // returns the number of divisions required to keep in tolerance + if(returnSpanProperties == false) this->SetProperties(true); + return geoff_geometry::Split(tolerance, angle, radius, dir); + } +#if 0 + int Span3d::Split(double tolerance) { + // returns the number of divisions required to keep in tolerance + if(returnSpanProperties == false) this->SetProperties(true); + return geoff_geometry::Split(tolerance, angle, radius, dir); + } +#endif + static int Split(double tolerance, double angle, double radius, int dir) { + if(dir == LINEAR) return 0; // straight span + double cosa = 1 - tolerance / radius; + if(cosa > NEARLY_ONE) cosa = NEARLY_ONE; + cosa = 2 * cosa * cosa - 1 ; /* double angle */ + double sina = sqrt(1 - cosa * cosa) * dir; + double tempang = atan2(sina, cosa); + int num_vectors = (int)(fabs (angle / tempang )) + 1 ; + return num_vectors; + } + + void Span::SplitMatrix(int num_vectors, Matrix* matrix) { + // returns the incremental matrix + matrix->Unit(); + if(dir) { + // arc span + double incang = angle / (double) num_vectors ; + + matrix->Translate(-pc.x, -pc.y, 0); + matrix->Rotate(incang, 3); + matrix->Translate(pc.x, pc.y, 0); + } else { + // linear span + matrix->Translate(length / num_vectors * vs.getx(), length / num_vectors * vs.gety(), 0); + } + } + +#if 0 + void Span3d::SplitMatrix(int num_vectors, Matrix* matrix) { + // returns the incremental matrix + matrix->Unit(); + if(dir) { + // arc span + if(normal.getz() <= NEARLY_ONE) FAILURE(getMessage(L"Unfinished coding - contact the Company", GENERAL_MESSAGES, MES_UNFINISHEDCODING)); + double incang = angle / (double) num_vectors ; + + matrix->Translate(-pc.x, -pc.y, -pc.z); + matrix->Rotate(incang, 3); + matrix->Translate(pc.x, pc.y, pc.z); + } else { + // linear span + double d = length / num_vectors; + matrix->Translate(d * vs.getx(), d * vs.gety(), d * vs.getz()); + } + } +#endif + + void Span::minmax(Box& box, bool start) { + minmax(box.min, box.max, start); + } +#if 0 + void Span3d::minmax(Box3d& box, bool start) { + minmax(box.min, box.max, start); + } +#endif + void Span::minmax(Point& min, Point& max, bool start) { + // box a span (min/max) + if(start) { + MinMax(p0, min, max); + } + MinMax(p1, min, max); + + if(dir) { + // check the quadrant points + double dx1 = p1.x - p0.x; + double dy1 = p1.y - p0.y; + + double dx = pc.x - p0.x; + double dy = pc.y - p0.y; + + double dx0 = dx + radius; // 0deg + + if( dir * (dx0 * dy1 - dx1 * dy) > 0) { + if(pc.x + radius > max.x) max.x = pc.x + radius; + } + dx0 = dx - radius; // 180deg + if( dir * (dx0 * dy1 - dx1 * dy) > 0) { + if(pc.x - radius < min.x) min.x = pc.x - radius; + } + double dy0 = dy + radius; // 90deg + if( dir * (dx * dy1 - dx1 * dy0) > 0) { + if(pc.y + radius > max.y) max.y = pc.y + radius; + } + + dy0 = dy - radius; // 270deg + if( dir * (dx * dy1 - dx1 * dy0) > 0) { + if(pc.y - radius < min.y) min.y = pc.y - radius; + } + } + } + +#if 0 + void Span3d::minmax(Point3d& min, Point3d& max, bool start) { + // box a span (min/max) + if(start) { + MinMax(p0, min, max); + } + MinMax(p1, min, max); + + if(dir) { + // check the quadrant points ... MUST RECODE THIS FOR 3D sometime + double dx1 = p1.x - p0.x; + double dy1 = p1.y - p0.y; + + double dx = pc.x - p0.x; + double dy = pc.y - p0.y; + + double dx0 = dx + radius; // 0deg + + if( dir * (dx0 * dy1 - dx1 * dy) > 0) { + if(pc.x + radius > max.x) max.x = pc.x + radius; + } + dx0 = dx - radius; // 180deg + if( dir * (dx0 * dy1 - dx1 * dy) > 0) { + if(pc.x - radius < min.x) min.x = pc.x - radius; + } + double dy0 = dy + radius; // 90deg + if( dir * (dx * dy1 - dx1 * dy0) > 0) { + if(pc.y + radius > max.y) max.y = pc.y + radius; + } + + dy0 = dy - radius; // 270deg + if( dir * (dx * dy1 - dx1 * dy0) > 0) { + if(pc.y - radius < min.y) min.y = pc.y - radius; + } + } + } +#endif + + int Span::Intof(const Span& sp, Point& pInt1, Point& pInt2, double t[4])const { + // Intof 2 spans + return geoff_geometry::Intof(*this, sp, pInt1, pInt2, t); + } + + Point Span::Near(const Point& p)const{ + // returns the near point to span from p + if(this->dir == LINEAR) { + double t; + t = (Vector2d(this->p0, p) * this->vs); // t parametised 0 - line length + return this->vs * t + this->p0; + } else { + double r = p.Dist(this->pc); + if(r < geoff_geometry::TOLERANCE) return (p.Dist(this->p0) < p.Dist(this->p1))?this->p0 : this->p1; + return(p.Mid(this->pc, (r - this->radius) / r)); + } + } + Point Span::NearOn(const Point& p)const{ + // returns the near point to span from p - returned point is always on the span + Point pn; + pn = Near(p); + if(this->OnSpan(pn) == true) return pn; + + // return nearest endpoint + return (pn.Dist(p0) < pn.Dist(p1))?p0 : p1; + } + + void Span::Transform(const Matrix& m, bool setprops) { + p0 = p0.Transform(m); + p1 = p1.Transform(m); + if(dir != LINEAR) { + pc = pc.Transform(m); + if(m.m_mirrored == -1) FAILURE(L"Don't know mirror - use IsMirrored method on object"); + if(m.m_mirrored) dir = -dir; + } + if(setprops == true) SetProperties(true); + } + +#if 0 + void Span3d::Transform(const Matrix& m, bool setprops) { + p0 = p0.Transform(m); + p1 = p1.Transform(m); + if(dir != LINEAR) { + pc = pc.Transform(m); + normal.Transform(m); + if(m.m_mirrored == -1) FAILURE(L"Don't know mirror - use IsMirrored method on object"); + if(m.m_mirrored) dir = -dir; + } + if(setprops == true) SetProperties(true); + } +#endif + + Point Span::Mid()const { + // midpoint of a span + + return geoff_geometry::Mid(*this); + + } + + + Point Span::MidPerim(double d)const { + /// returns a point which is 0-d along span + Point p; + if(this->dir == LINEAR) { + p = this->vs * d + this->p0; + } + else { + Vector2d v(pc, p0); + v.Rotate(d * dir / this->radius); + p = v + pc; + } + return p; + } + + Point Span::MidParam(double param)const { + /// returns a point which is 0-1 along span + if(fabs(param) < 0.00000000000001)return p0; + if(fabs(param - 1.0) < 0.00000000000001)return p1; + return MidPerim(param * this->length); + } + + Vector2d Span::GetVector(double fraction)const { + /// returns the direction vector at point which is 0-1 along span + if(dir == 0){ + Vector2d v(p0, p1); + v.normalise(); + return v; + } + + Point p= MidParam(fraction); + Vector2d v(pc, p); + v.normalise(); + if(dir == ACW) + { + return Vector2d(-v.gety(), v.getx()); + } + else + { + return Vector2d(v.gety(), -v.getx()); + } + } + + Kurve::Kurve(const Kurve& k) :Matrix(){ + /// copy constructor + this->m_nVertices = k.m_nVertices; + memcpy(this->e, k.e, 16 * sizeof(double)); + this->m_unit = k.m_unit; + this->m_mirrored = k.m_mirrored; + this->m_isReversed = k.m_isReversed; + this->m_started = k.m_started; + for(unsigned int i = 0; i < k.m_spans.size(); i++) { + SpanVertex* spv = new SpanVertex; + *spv = *k.m_spans[i]; + this->m_spans.push_back(spv); + } + } + + const Kurve& Kurve::operator=( const Kurve &k) { + memcpy(e, k.e, 16 * sizeof(double)); + m_unit = k.m_unit; + m_mirrored = k.m_mirrored; + m_isReversed = k.m_isReversed; + + this->Clear(); + + if(k.m_nVertices) m_started = true; +// m_nVertices = 0; + +// spVertex spv; +// for(int i = 0; i < k.m_nVertices; i++) { +// k.Get(i, spv); +// Add(spv); +// } + for(unsigned int i = 0; i < k.m_spans.size(); i++) { + SpanVertex* spv = new SpanVertex; + *spv = *k.m_spans[i]; + this->m_spans.push_back(spv); + } + m_nVertices = k.m_nVertices; + return *this; + } + +#if 0 + + Kurve::Kurve(Kurve& k) :Matrix(){ +*this = k; +return; + *this = Matrix(k); + + Point p, pc; + m_nVertices = 0; + + for(int i = 0; i < k.m_nVertices; i++) { + int spantype = k.Get(i, p, pc); + int spanid = k.GetSpanID(i); + if(Add(spantype, p, pc)) this->AddSpanID(spanid); + } + if(k.m_nVertices) m_started = true; + } + + const Kurve& Kurve::operator=( Kurve &k) + { + *this = Matrix(k); + + Point p, pc; + this->Clear(); + m_isReversed = k.m_isReversed; + + for(int i = 0; i < k.m_nVertices; i++) { + int spantype = k.Get(i, p, pc); + int spanid = k.GetSpanID(i); + if(Add(spantype, p, pc)) this->AddSpanID(spanid); + } + if(k.m_nVertices) m_started = true; + return *this; + } +#endif + + const Kurve& Kurve::operator=(const Matrix &m) + { +// *this = Matrix(m); +// return *this; + + for(int i = 0; i < 16; i++) e[i] = m.e[i]; + m_unit = m.m_unit; + m_mirrored = m.m_mirrored; + return *this; + } + + Kurve::~Kurve() + { + this->Clear(); + } + + + + bool Kurve::Closed()const + { + // returns true if kurve closed + if(m_nVertices > 1) { + Point ps, pe, pc; + Get(0, ps, pc); + Get(m_nVertices - 1, pe, pc); + return (ps == pe); + } + else + return false; + } + + void Kurve::FullCircle(int dir, const Point& c, double radius) { + /// make a full circle Kurve (2 spans) + /// mark the first span for later + this->Clear(); + Point ps = c; + ps.x = c.x + radius; + this->Start(ps); + this->AddSpanID(FULL_CIRCLE_KURVE); + ps.x = c.x - radius; + this->Add(dir, ps, c, true); + ps.x = c.x + radius; + this->Add(dir, ps, c, true); + } + + void Kurve::Start() + { + if(m_started) this->Clear(); + m_started = true; + } + + + void Kurve::Start(const Point& p) + { + Start(); + Add(0, p, Point(0,0)); + } + + bool Kurve::Add(const Span& sp, bool AddNullSpans) { + // add a span, including ID + if(this->m_started == false) this->Start(sp.p0); + if(this->Add(sp.dir, sp.p1, sp.pc, AddNullSpans)) { + this->AddSpanID(sp.ID); + return true; + } + return false; + } + + bool Kurve::Add(const spVertex& spv, bool AddNullSpans) { + if(Add(spv.type, spv.p, spv.pc, AddNullSpans)) { + AddSpanID(spv.spanid); + return true; + } + return false; + } + + bool Kurve::Add(int span_type, const Point& p0, const Point& pc, bool AddNullSpans) + { + // add a span (cw = -1 (T) acw = 1 (A) ) +#ifdef _DEBUG + if(this == NULL) FAILURE(L"Kurve::Add - No Kurve Object"); +#endif + + if(!m_started) { + Start(p0); + return true; + } + + if(m_nVertices) { + // see if a null span would result by the addition of this span + // double xl, yl, cxl, cyl; + Point pv, pcc; + Get(m_nVertices - 1, pv, pcc); + if(pv.Dist(p0) < geoff_geometry::TOLERANCE) { + if(!AddNullSpans)return false; + span_type = LINEAR; // linear span + } + } + + SpanVertex* p; + if(m_nVertices % SPANSTORAGE == 0) { + p = new SpanVertex; + m_spans.push_back(p); + } + else + p = (SpanVertex*) m_spans[m_nVertices / SPANSTORAGE]; + + p->Add(m_nVertices % SPANSTORAGE, span_type, p0, pc); + m_nVertices++; + return true; + } + void Kurve::AddSpanID(int ID) + { + // add a extra data - must be called after Add + int vertex = this->m_nVertices - 1; + SpanVertex* p = (SpanVertex*) m_spans[vertex / SPANSTORAGE]; + p->AddSpanID(vertex % SPANSTORAGE, ID); + } + + void Kurve::Add() { + // null span + if(m_nVertices == 0) FAILURE(L"Invalid attempt to add null span - no start"); + Point p, pc; + Get(m_nVertices - 1, p, pc); + Add(p, true); + } + + bool Kurve::Add(const Point& p0, bool AddNullSpans) { + return Add(0, p0, Point(0,0), AddNullSpans); + } + + + void Kurve::Add(const Kurve* k, bool AddNullSpans) { + Span sp; + Matrix m; + if(this->m_unit == false) { + m = *k; + Matrix im = this->Inverse(); + m.Multiply(im); + m.IsUnit(); + } + for(int i = 1; i <= k->nSpans(); i++) { + k->Get(i, sp, false, this->m_unit); + #ifndef PEPSDLL + const SpanDataObject* obj = k->GetIndex(i-1); + #endif + if(this->m_unit == false) sp.Transform(m); + + if(i == 1) { + // check if this is the same as last point in kurve + bool AddFirstVertex = true; + if(nSpans()) { + Span spLast; + Get(nSpans(), spLast, false, false); + if(spLast.p1.Dist(sp.p0) <= geoff_geometry::TOLERANCE) AddFirstVertex = false; + } + if(AddFirstVertex) { + Add(sp.p0, AddNullSpans); + #ifndef PEPSDLL + if(obj != NULL) { + SpanDataObject* objnew = new SpanDataObject(obj); + AddIndex(nSpans() - 1, objnew); + } + #endif + } + } + + Add(sp.dir, sp.p1, sp.pc, AddNullSpans); + #ifndef PEPSDLL + if(obj != NULL) { + SpanDataObject* objnew = new SpanDataObject(obj); + AddIndex(nSpans() - 1, objnew); + } + #endif + } + } + + void Kurve::Replace(int vertexnumber, const spVertex& spv) { + // replace a span + Replace(vertexnumber, spv.type, spv.p, spv.pc, spv.spanid); + } + + + void Kurve::Replace(int vertexnumber, int type, const Point& p0, const Point& pc, int ID) { + // replace a span +#ifdef _DEBUG + if(this == NULL || vertexnumber > m_nVertices) FAILURE(getMessage(L"Kurve::Replace - vertexNumber out of range", GEOMETRY_ERROR_MESSAGES, MES_BAD_VERTEX_NUMBER)); +#endif + SpanVertex* p = (SpanVertex*) m_spans[vertexnumber / SPANSTORAGE]; + p->Add(vertexnumber % SPANSTORAGE, type, p0, pc, ID); + } + +#ifdef PEPSDLL + void Kurve::ModifyIndex(int vertexnumber, WireExtraData* i) { + // replace an index +#ifdef _DEBUG + if(this == NULL || vertexnumber > m_nVertices) FAILURE(getMessage(L"Kurve::ModifyIndex - vertexNumber out of range", GEOMETRY_ERROR_MESSAGES, MES_BAD_VERTEX_NUMBER)); +#endif + SpanVertex* p = (SpanVertex*) m_spans[vertexnumber / SPANSTORAGE]; + p->Add(vertexnumber % SPANSTORAGE, i); + } +#else + void Kurve::AddIndex(int vertexNumber, const SpanDataObject* data) { + if(this == NULL || vertexNumber > m_nVertices - 1) FAILURE(L"Kurve::AddIndex - vertexNumber out of range"); + SpanVertex* p = (SpanVertex*) m_spans[vertexNumber / SPANSTORAGE]; + p->Add(vertexNumber % SPANSTORAGE, data); + } + + const SpanDataObject* Kurve::GetIndex(int vertexNumber)const { + if(this == NULL || vertexNumber > m_nVertices - 1) FAILURE(L"Kurve::GetIndex - vertexNumber out of range"); + SpanVertex* p = (SpanVertex*) m_spans[vertexNumber / SPANSTORAGE]; + return p->GetIndex(vertexNumber % SPANSTORAGE); + } + + +#endif + void Kurve::Get(std::vector *all, bool igNoreNullSpans)const { + /// put all spans to vector + for(int i = 1; i <= nSpans(); i++) { + Span sp; + Get(i, sp, true); + if(igNoreNullSpans == true && sp.NullSpan == true) continue; + all->push_back(sp); + } + } + + void Kurve::Get(int vertexnumber, spVertex& spv) const { + spv.type = Get(vertexnumber, spv.p, spv.pc); + spv.spanid = GetSpanID(vertexnumber); + } + + int Kurve::Get(int vertexnumber, Point& pe, Point& pc) const { + // returns spantype with end / centre by reference + if(vertexnumber < 0 || vertexnumber >= m_nVertices) FAILURE(getMessage(L"Kurve::Get - vertexNumber out of range", GEOMETRY_ERROR_MESSAGES, MES_BAD_VERTEX_NUMBER)); + if(m_isReversed == true) { + int revVertexnumber = m_nVertices - 1 - vertexnumber; + SpanVertex* p = (SpanVertex*)m_spans[revVertexnumber / SPANSTORAGE]; + int offset = revVertexnumber % SPANSTORAGE; + pe = Point(p->x[offset], p->y[offset]); + if(vertexnumber > 0) { + revVertexnumber++; + offset = revVertexnumber % SPANSTORAGE; + p = (SpanVertex*)m_spans[revVertexnumber / SPANSTORAGE]; + pc = Point(p->xc[offset], p->yc[offset]); + return -p->type[offset]; + } + else return LINEAR; + } + else { + SpanVertex* p = (SpanVertex*)m_spans[vertexnumber / SPANSTORAGE]; + return p->Get(vertexnumber % SPANSTORAGE, pe, pc); + } + } + int Kurve::GetSpanID(int vertexnumber) const { + // for spanID (wire offset) + if(vertexnumber < 0 || vertexnumber >= m_nVertices) FAILURE(getMessage(L"Kurve::Get - vertexNumber out of range", GEOMETRY_ERROR_MESSAGES, MES_BAD_VERTEX_NUMBER)); + if(m_isReversed == true) vertexnumber = m_nVertices - 1 - vertexnumber; + SpanVertex* p = (SpanVertex*)m_spans[vertexnumber / SPANSTORAGE]; + return p->GetSpanID(vertexnumber % SPANSTORAGE); + } + int Kurve::Get(int spannumber, Span& sp, bool returnSpanProperties, bool transform) const { + // returns span data and optional properties - the function returns as the span type + if(spannumber < 1 || spannumber > m_nVertices) FAILURE(getMessage(L"Kurve::Get - vertexNumber out of range", GEOMETRY_ERROR_MESSAGES, MES_BAD_VERTEX_NUMBER)); + if(m_nVertices < 2) return -99; + + int spanVertexNumber = spannumber - 1; + if(m_isReversed) spanVertexNumber = m_nVertices - 1 - spanVertexNumber; + SpanVertex* p = (SpanVertex*)m_spans[spanVertexNumber / SPANSTORAGE]; + sp.p0.x = p->x[spanVertexNumber % SPANSTORAGE]; + sp.p0.y = p->y[spanVertexNumber % SPANSTORAGE]; + sp.p0.ok = 1; + + sp.dir = Get(spannumber, sp.p1, sp.pc); + sp.ID = GetSpanID(spannumber); + + if(transform && !m_unit) { + const Matrix *m = this; + sp.Transform(*m, false); + } + + sp.SetProperties(returnSpanProperties); + + return sp.dir; + } + +#if 0 + int Kurve::Get(int spannumber, Span3d& sp, bool returnSpanProperties, bool transform) const { + // returns span data and optional properties - the function returns as the span type + if(spannumber < 1 || spannumber > m_nVertices) FAILURE(getMessage(L"Kurve::Get - vertexNumber out of range", GEOMETRY_ERROR_MESSAGES, MES_BAD_VERTEX_NUMBER)); + if(m_nVertices < 2) return -99; + + int spanVertexNumber = spannumber - 1; + SpanVertex* p = (SpanVertex*)m_spans[spanVertexNumber / SPANSTORAGE]; + sp.p0.x = p->x[spanVertexNumber % SPANSTORAGE]; + sp.p0.y = p->y[spanVertexNumber % SPANSTORAGE]; + sp.p0.z = 0; +// sp.p0.ok = 1; + + sp.dir = Get(spannumber, sp.p1, sp.pc); + + if(transform && !m_unit) { + const Matrix *m = this; + sp.Transform(*m, false); + } + + sp.SetProperties(returnSpanProperties); + + return sp.dir; + } +#endif + + void Kurve::Get(Point &ps,Point &pe) const + { + // returns the start- and endpoint of the kurve + Span sp; + Get(1,sp,true,true); + ps = sp.p0; + Get(m_nVertices-1,sp,true,true); + pe = sp.p1; + } + + void Span::SetProperties(bool returnProperties) { + if((returnSpanProperties = returnProperties)) { + // return span properties + if(dir) { + // arc properties + vs = ~Vector2d(pc, p0); // tangent at start ( perp to radial vector) + ve = ~Vector2d(pc, p1); // tangent at end ( perp to radial vector) + if(dir == CW) { + vs = -vs; // reverse directions for CW arc + ve = -ve; + } + + radius = vs.normalise(); + double radCheck = ve.normalise(); +// if(FNE(radius, radCheck, geoff_geometry::TOLERANCE * 0.5)){ + if(FNE(radius, radCheck, geoff_geometry::TOLERANCE)){ + FAILURE(getMessage(L"Invalid Geometry - Radii mismatch - SetProperties", GEOMETRY_ERROR_MESSAGES, MES_INVALIDARC)); + } + + length = 0.0; + angle = 0.0; + if(radius > geoff_geometry::TOLERANCE) { + if((NullSpan = (p0.Dist(p1)) <= geoff_geometry::TOLERANCE)) { + dir = LINEAR; + } + else { + // arc length & included angle + length = fabs(angle = IncludedAngle(vs, ve, dir)) * radius; + } + } + else + NullSpan = true; + } + else { + // straight properties + vs = Vector2d(p0, p1); + + length = vs.normalise(); + NullSpan = (length <= geoff_geometry::TOLERANCE); + ve = vs; + } + minmax(box, true); + } + } + +#if 0 + void Span3d::SetProperties(bool returnProperties) { + if(returnSpanProperties = returnProperties) { + // return span properties + if(dir) { + // arc properties + vs = normal ^ Vector3d(pc, p0);// tangent at start ( perp to radial vector) + ve = normal ^ Vector3d(pc, p1);// tangent at end ( perp to radial vector) + if(dir == CW) { + vs = -vs; // reverse directions for CW arc + ve = -ve; + } + + radius = vs.normalise(); + double radCheck = ve.normalise(); + if(FNE(radius, radCheck, geoff_geometry::TOLERANCE * 0.5)) FAILURE(getMessage(L"Invalid Geometry - Radii mismatch - SetProperties", GEOMETRY_ERROR_MESSAGES, MES_INVALIDARC)); + if(radius > geoff_geometry::TOLERANCE) { + if(NullSpan = (p0.Dist(p1) <= geoff_geometry::TOLERANCE)) { + length = 0.0; + angle = 0.0; + dir = LINEAR; + } + else { + // arc length & included angle + length = fabs(angle = IncludedAngle(vs, ve, normal, dir)) * radius; + } + } + else + NullSpan = true; + } + else { + // straight properties + vs = Vector3d(p0, p1); + + length = vs.normalise(); + NullSpan = (length <= geoff_geometry::TOLERANCE); + ve = vs; + } + minmax(box, true); + } + } +#endif + + + Point Mid(const Span& span) { + // mid point of a span + if(span.dir) { + CLine chord(span.p0, span.p1); + if(chord.ok) { + CLine bisector(Mid(span.p0, span.p1), ~chord.v, false); + return Intof((span.dir == CW) ?FARINT : NEARINT, bisector, Circle(span)); + } + else + return span.p0; + } + else + return Mid(span.p0, span.p1); + } + + Point Kurve::Near(const Point& p, int& nearSpanNumber)const { + // finds the nearest span on kurve to the the given point, nearSpanNumber is the spannumber + double minDist = 1.0e100; + Point pNear, pn; + + nearSpanNumber = 0; + for(int i = 1; i <= nSpans(); i++) { + Span sp; + Get(i, sp, true, true); + pNear = sp.NearOn(p); + double d = pNear.Dist(p); + if(minDist > d) { + nearSpanNumber = i; + pn = pNear; + minDist = d; + if(minDist < geoff_geometry::TOLERANCE) break; // p must be on the span + } + } + return pn; + } + + + Point Kurve::NearToVertex(const Point& p, int& nearSpanNumber)const { + // finds the nearest span endpoint on kurve to the the given point, nearSpanNumber is the spannumber + double minDistSquared = 1.0e100; + Point pn; + + Matrix inv_mat = *this; + inv_mat.Inverse(); + + Point tp = p; + if(!m_unit) tp = tp.Transform(inv_mat); // Inverse transform point (rather than transform each vertex!) + + nearSpanNumber = 0; + + for(int i = 0; i < m_nVertices; i++) { + Point ps, pc; + Get(i, ps, pc); + double DistSquared = Vector2d(ps, tp).magnitudesqd(); + if(DistSquared < minDistSquared) { + minDistSquared = DistSquared; + nearSpanNumber = i; + pn = ps; + } + } + return pn.Transform(*this); + } + + void Kurve::ChangeStart(const Point *pNewStart, int startSpanno) { + // changes the start position of the Kurve + if(startSpanno == 1) { + Span spFirst; + this->Get(1, spFirst, false, true); + if(spFirst.p0 == *pNewStart) return; + } + else if(startSpanno == this->nSpans()) { + Span spLast; + this->Get(this->nSpans(), spLast, false, true); + if(spLast.p1 == *pNewStart) return; + } + Kurve temp; + + bool wrapped = false; + int nSpans = 0; + int spanno = startSpanno; + Span sp; + while(1) { + this->Get(spanno, sp, false, true); + if(spanno == startSpanno && wrapped == false) { + temp.Start(*pNewStart); + temp.Add(sp.dir, sp.p1, sp.pc, true); + } + else { + if(nSpans == this->nSpans() && this->Closed() == true) { + sp.p1 = *pNewStart; + } + temp.Add(sp, true); + } + + spanno++; + nSpans++; + if(nSpans > this->nSpans()) break; + if(spanno > this->nSpans()) { + if(this->Closed() == false) break; + spanno = 1; + wrapped = true; + } + } + + *this = temp; + } + + + void Kurve::ChangeEnd(const Point *pNewEnd, int endSpanno) { + // changes the end position of the Kurve, doesn't keep closed kurves closed + if(endSpanno == 1) { + Span spFirst; + this->Get(1, spFirst, false, true); + if(spFirst.p0 == *pNewEnd) return; + } + else if(endSpanno == this->nSpans()) { + Span spLast; + this->Get(this->nSpans(), spLast, false, true); + if(spLast.p1 == *pNewEnd) return; + } + Kurve temp; + + int spanno = 1; + Span sp; + while(1) { + this->Get(spanno, sp, false, true); + if(spanno == 1) { + temp.Start(sp.p0); + } + + if(spanno == endSpanno)sp.p1 = *pNewEnd; + + temp.Add(sp.dir, sp.p1, sp.pc, true); + if(spanno == endSpanno)break; + + spanno++; + } + + *this = temp; + } + + void Kurve::minmax(Point& min, Point& max) { + // boxes kurve + double xscale = 1.0; + min = Point(1.0e61, 1.0e61); + max = Point(-1.0e61, -1.0e61); + + if(!GetScale(xscale)) FAILURE(getMessage(L"Differential Scale not allowed for this method", GEOMETRY_ERROR_MESSAGES, MES_DIFFSCALE)); // differential scale + Span sp; + for(int i = 1; i < m_nVertices; i++) { + Get(i, sp, true, true); + if(i == 1) MinMax(sp.p0, min, max); + sp.minmax(min, max, false); + } + } + + void Kurve::minmax(Box& b) { + minmax(b.min, b.max); + } + + void Kurve::StoreAllSpans(std::vector& kSpans)const { // store all kurve spans in array, normally when fast access is reqd + Span span; + for(int i = 1; i <= this->nSpans(); i++) { + this->Get(i, span, true, false); + kSpans.push_back(span); + } + } + + void Kurve::Clear() + { + for(vector::iterator It = m_spans.begin(); It != m_spans.end(); It++) + { + SpanVertex* spv = *It; + delete spv; + } + m_spans.clear(); + m_started = false; + m_nVertices = 0; + m_isReversed = false; + } + + bool Kurve::operator==(const Kurve &k)const{ + // k = kk (vertex check) + if(nSpans() != k.nSpans()) return false; + spVertex thisvertex, vertex; + for(int i = 0; i <= nSpans(); i++) { + this->Get(i, thisvertex); + k.Get(i, vertex); + if(thisvertex != vertex) return false; + } + return true; + } + + double Kurve::Perim() const{ + // returns perimeter of kurve + double perim = 0; + Span sp; + double xscale = 1.0; + if(!GetScale(xscale)) FAILURE(getMessage(L"Differential Scale not allowed for this method", GEOMETRY_ERROR_MESSAGES, MES_DIFFSCALE)); // differential scale + + if(m_nVertices > 1) { + for(int i = 1; i < m_nVertices; i++) + perim += (Get(i, sp, true))? fabs(sp.angle) * sp.radius : sp.length; + } + return perim * xscale; + } + double Kurve::Area() const{ + // returns Area of kurve (+ve clockwise , -ve anti-clockwise sense) + double xscale = 1.0; + double area = 0; + Span sp; + + if(Closed()) { + if(!GetScale(xscale)) FAILURE(getMessage(L"Differential Scale not allowed for this method", GEOMETRY_ERROR_MESSAGES, MES_DIFFSCALE)); // differential scale + for(int i = 1; i < m_nVertices; i++) { + if(Get(i, sp, true)) + area += ( 0.5 * ((sp.pc.x - sp.p0.x) * (sp.pc.y + sp.p0.y) - (sp.pc.x - sp.p1.x) * (sp.pc.y + sp.p1.y) - sp.angle * sp.radius * sp.radius)); + else + area += 0.5 * (sp.p1.x - sp.p0.x) * (sp.p0.y + sp.p1.y); + } + } + return area * xscale * xscale; + } + + static void bubblesort(vector&p, vector& d); + + int Kurve::Intof(const Span& spin, vector& p)const { + // returns a vector (array) of intersection points + int totalPoints = 0; + for(int i = 1; i <= nSpans(); i++) { + Span sp; + Get(i, sp, true, true); + + Point pInt1, pInt2; + double t[4]; + int numint = sp.Intof(spin, pInt1, pInt2, t); + if(numint) p.push_back(pInt1); + if(numint == 2) p.push_back(pInt2); + totalPoints += numint; + } + if(totalPoints) { + // sort intersects along span + vector d; + Span temp(spin); + + for(int i = 0; i < (int)p.size(); i++) { + temp.p1 = p[i]; + temp.SetProperties(true); + + d.push_back(temp.length); + } + bubblesort(p, d); + } + return totalPoints; + } + + static void bubblesort(vector&p, vector& d) { + + for(int pass = 1; pass < (int)p.size() ; pass++) { + for(int j = 0; j < (int)p.size() - 1; j++) { + if(d[j] > d[j+1] ) { + // swap + Point temp = p[j]; + p[j] = p[j+1]; + p[j+1] = temp; + double dtemp = d[j]; + d[j] = d[j+1]; + d[j+1] = dtemp; + } + } + } + } + + int Kurve::Intof(const Kurve&k, vector& p)const { + vector all; + + int totalPoints = 0; + for(int i = 1; i <= nSpans(); i++) { + Span sp; + Get(i, sp, true, true); + vector p0; + totalPoints += k.Intof(sp, p0); + + for(int j = 0; j < (int)p0.size(); j++) all.push_back(p0[j]); + } + //FILE* d; + //d = fopen("\\temp\\test.txt", "w"); + // for(int l = 0; l < all.size(); l++) all[l].print(d, "all","\n"); + + + for(int i = 0; i < (int)all.size(); i++) { + if(i == 0) + p.push_back(all[0]); + else + if(all[i-1].Dist(all[i]) > geoff_geometry::TOLERANCE) p.push_back(all[i]); + } + + //fclose(d); + return (int)p.size(); + } + + bool Kurve::Split(double MaximumRadius, double resolution) { + + Span sp; + bool changed = false; + Kurve ko; + + Get(0, sp.p0, sp.pc); + ko.Start(sp.p0); + + for(int i = 1 ; i < m_nVertices; i++) { + sp.dir = Get(i, sp.p1, sp.pc); + if(sp.dir) { + sp.SetProperties(true); + if(sp.radius >= MaximumRadius) { + // split this arc + int nSplits = sp.Split(resolution); + if(nSplits > 1) { + Matrix m; + sp.SplitMatrix(nSplits, &m); + for(int j = 1; j < nSplits; j++) { + sp.p0 = sp.p0.Transform(m); + ko.Add(sp.p0); + } + + sp.dir = LINEAR; + changed = true; + } + } + } + + ko.Add(sp.dir, sp.p1, sp.pc); + + sp.p0 = sp.p1; + + } + // copy kurve + if(changed) *this = ko; + return changed; + } + + void Kurve::Reverse() { + // reverse the direction of a kurve + int nSwaps = (m_nVertices - 1) / 2; + if(nSwaps == 0) return; + Point p0, pc0; // near + Point pend, pcend; // far + + int i = 0, j = m_nVertices - 1; + int dir0 = Get(i, p0, pc0); + int spanID0 = GetSpanID(i); + int dirend = Get(j, pend, pcend); + int spanIDend = GetSpanID(j); + + while(i <= nSwaps) { + Point p1, pc1; + int dir1 = Get(i+1, p1, pc1); + int spanID1 = GetSpanID(i+1); + Point pendp, pcendp; // far previous + int direndp = Get(j-1, pendp, pcendp); + int spanIDendp = GetSpanID(j-1); + // end point + Replace(i, dir0, pend, pc0, spanID0); + Replace(j, dirend, p0, pcend, spanIDend); + + dir0 = dir1; + p0 = p1; + pc0 = pc1; + dirend = direndp; + pend = pendp; + pcend = pcendp; + spanID0 = spanID1; + spanIDend = spanIDendp; + + i++; + j--; + } + + // now circle data - but it should be easy to modify centre data in the loop above (for another day) + i = 0; + j = m_nVertices - 1; + dir0 = Get(i, p0, pc0); + dirend = Get(j, pend, pcend); + + while(i < nSwaps) { + Point p1, pc1; + Point pendp, pcendp; // far previous + + int dir1 = Get(i+1, p1, pc1); + int direndp = Get(j-1, pendp, pcendp); + + Replace(i+1, -dirend, p1, pcend); + Replace(j, -dir1, pend, pc1); + dir0 = dir1; + p0 = p1; + pc0 = pc1; + dirend = direndp; + pend = pendp; + pcend = pcendp; + i++; + j--; + } + } + + int Kurve::Reduce(double tolerance) { + // remove spans that lie within tolerance + // returns the number of spans removed + if(nSpans() <= 2) return 0; // too few spans for this method + Kurve kReduced; + kReduced = Matrix(*this); + +#if 0 + for(int i = 1; i <= this->nSpans(); i++) { + Span sp; + this->Get(i, sp, true); + + for(int j = i+1; j <= this->nSpans(); j++) { + Span spnext; + this->Get(j, spnext, true); + + } + } + return m_nVertices - kReduced.m_nVertices; + +#else + int dir1, dir2; + Point p0, p1, p2, pc0, pc1, pc2; + int vertex = 0; + int dir0 = Get(vertex++, p0, pc0); // first vertex + kReduced.Start(p0); + int lvertex = vertex++; + + while(vertex < m_nVertices) { + while(vertex < m_nVertices) { + int savelvertex = lvertex; + int addvertex = vertex - 1; + dir2 = Get(vertex++, p2, pc2); + CLine cl(p0, p2); + if(cl.ok) { + bool outoftol = false; + while(lvertex < vertex - 1) { // interior loop, p1 after p0 up to vertex before p2 + dir1 = Get(lvertex++, p1, pc1); + + if(dir1 || fabs(cl.Dist(p1)) > tolerance) { + outoftol = true; + break; + } + } + if(outoftol) { + dir0 = Get(addvertex, p0, pc0); + kReduced.Add(dir0, p0, pc0); + lvertex = addvertex + 1; + } + else { + lvertex = savelvertex; + } + } + } + } + kReduced.Add(dir2, p2, pc2); + + if(m_nVertices != kReduced.m_nVertices) *this = kReduced; + return m_nVertices - kReduced.m_nVertices; +#endif + } + +void Kurve::Part(int startVertex, int EndVertex, Kurve *part) { + // make a part kurve + spVertex spv; + for(int i = startVertex; i <= EndVertex; i++) { + Get(i, spv); + part->Add(spv, true); + } + return; +} + + +Kurve Kurve::Part(int fromSpanno, const Point& fromPt, int toSpanno, const Point& toPt) { + // make a Part Kurve + // if spanno are known containing from/to Points then this is used, otherwise set = 0 + Kurve kPart; + Span span; + Point ps,pe; + int iStartSpanNr,iEndSpanNr,i; + + // get start point and start spannumber + if(fromSpanno == 0) + ps = Near(fromPt,iStartSpanNr); + else + { + Get(fromSpanno,span,true,true); + ps = span.p0; + iStartSpanNr = fromSpanno; + } + // get end point and end spannumber + if(toSpanno == 0) + pe = Near(toPt,iEndSpanNr); + else + { + Get(toSpanno,span,true,true); + pe = span.p1; + iEndSpanNr = toSpanno; + } + + kPart.Start(ps); + Get(iStartSpanNr,span,true,true); + + if(iStartSpanNr == iEndSpanNr) + { + kPart.Add(span.dir,pe,span.pc); + return kPart; + } + + if(iStartSpanNr < iEndSpanNr) + { + for(i=iStartSpanNr;i iEndSpanNr) + { + for(i=iStartSpanNr;i<=nSpans();i++) + { + Get(i,span,true,true); + kPart.Add(span.dir,span.p1,span.pc); + } + if(Closed() == false) + { + Get(1,span,true,true); + kPart.Add(0,span.p0,Point(0.0,0.0)); // Add new span from kend to kstart + } + for(i=1;iPerim(); + double fromPerim = fromParam * perimTotal; + double toPerim = toParam * perimTotal; + double perim = 0.; + double perimLast = 0.; + for(int i = 1; i <= this->nSpans(); i++) { + Span sp; + this->Get(i, sp, true, true); + perim += sp.length; + if(fromPerim <= perim && k.m_started == false) { + // start + if(FEQ(fromPerim, perim) == true) + k.Start(sp.p0); + else { + double d = fromPerim - perimLast; + k.Start(sp.MidPerim(d)); + } + } + + if(perim >= toPerim) { + // end + if(FEQ(toPerim, perim) == true) + k.Add(sp); + else { + double d = toPerim - perimLast; + sp.p1 = sp.MidPerim(d); + k.Add(sp); + } + break; + } + if(k.m_started == true) k.Add(sp); + perimLast = perim; + } + return k; + } + + void tangential_arc(const Point &p0, const Point &p1, const Vector2d &v0, Point &c, int &dir) + { + // sets dir to 0, if a line is needed, else to 1 or -1 for acw or cw arc and sets c + dir = 0; + + if(p0.Dist(p1) > 0.0000000001 && v0.magnitude() > 0.0000000001){ + Vector2d v1(p0, p1); + Point halfway(p0 + Point(v1 * 0.5)); + Plane pl1(halfway, v1); + Plane pl2(p0, v0); + Line plane_line; + if(pl1.Intof(pl2, plane_line)) + { + Line l1(halfway, v1); + double t1, t2; + Line lshort; + plane_line.Shortest(l1, lshort, t1, t2); + c = lshort.p0; + Vector3d cross = Vector3d(v0) ^ Vector3d(v1); + dir = (cross.getz() > 0) ? 1:-1; + } + } + } + +} diff --git a/src/Mod/Path/libarea/kurve/offset.cpp b/src/Mod/Path/libarea/kurve/offset.cpp new file mode 100644 index 0000000000..2a016fcc5c --- /dev/null +++ b/src/Mod/Path/libarea/kurve/offset.cpp @@ -0,0 +1,399 @@ +//////////////////////////////////////////////////////////////////////////////////////////////// +// 2d geometry classes - implements 2d kurve offset for use in dll +// +//////////////////////////////////////////////////////////////////////////////////////////////// + +/*============================== +Copyright (c) 2003 g.j.hawkesford + +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. +3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. +==============================*/ + +#include "geometry.h" +using namespace geoff_geometry; + +namespace geoff_geometry { + static Kurve eliminateLoops(const Kurve& k , const Kurve& originalk, double offset, int& ret); + static bool DoesIntersInterfere(const Point& pInt, const Kurve& k, double offset); + + int Kurve::Offset(vector&OffsetKurves, double offset, int direction, int method, int& ret)const { + + switch(method) { + case NO_ELIMINATION: + case BASIC_OFFSET: + { + Kurve* ko = new Kurve; + int n = OffsetMethod1(*ko, offset, direction, method, ret); + OffsetKurves.push_back(ko); + return n; + } + + default: + FAILURE(L"Requested Offsetting Method not available"); + } + return 0; + } + + int Kurve::OffsetMethod1(Kurve& kOffset, double off, int direction, int method, int& ret)const + { + // offset kurve with simple span elimination + // direction 1 = left, -1 = right + + // ret = 0 - kurve offset ok + // = 1 - kurve has differential scale (not allowed) + // = 2 - offset failed + // = 3 - offset too large + if(this == &kOffset) FAILURE(L"Illegal Call - 'this' must not be kOffset"); + double offset = (direction == GEOFF_LEFT)?off : -off; + + if(fabs(offset) < geoff_geometry::TOLERANCE || m_nVertices < 2) { + kOffset = *this; + ret = 0; + return 1; + } + + Span curSpan, curSpanOff; // current & offset spans + Span prevSpanOff; // previous offset span + Point p0, p1; // Offset span intersections + + // offset Kurve + kOffset = Matrix(*this); + + if(m_mirrored) offset = -offset; + int RollDir = ( off < 0 ) ? direction : - direction; // Roll arc direction + + double scalex; + if(!GetScale(scalex)) { + ret = 1; + return 0; // differential scale + } + offset /= scalex; + + bool bClosed = Closed(); + int nspans = nSpans(); + if(bClosed) { + Get(nspans, curSpan, true); // assign previus span for closed + + prevSpanOff = curSpan.Offset(offset); + nspans++; // read first again + } + + for(int spannumber = 1; spannumber <= nspans; spannumber++) { + if(spannumber > nSpans()) + Get(1, curSpan, true); // closed kurve - read first span again + else + Get(spannumber, curSpan, true); + + if(!curSpan.NullSpan) { + int numint = 0; + curSpanOff = curSpan.Offset(offset); + curSpanOff.ID = 0; + if(!kOffset.m_started) { + kOffset.Start(curSpanOff.p0); + kOffset.AddSpanID(0); + } + + if(spannumber > 1) { + // see if tangent + double d = curSpanOff.p0.Dist(prevSpanOff.p1); + if((d > geoff_geometry::TOLERANCE) && (curSpanOff.NullSpan == false && prevSpanOff.NullSpan == false)) { + // see if offset spans intersect + + double cp = prevSpanOff.ve ^ curSpanOff.vs; + bool inters = (cp > 0 && direction == GEOFF_LEFT) || (cp < 0 && direction == GEOFF_RIGHT); + + if(inters) { + double t[4]; + numint = prevSpanOff.Intof(curSpanOff, p0, p1, t); + } + + if(numint == 1) { + // intersection - modify previous endpoint + kOffset.Replace(kOffset.m_nVertices-1, prevSpanOff.dir, p0, prevSpanOff.pc, prevSpanOff.ID); + } + else { + // 0 or 2 intersections, add roll around (remove -ve loops in elimination function) + if(kOffset.Add(RollDir, curSpanOff.p0, curSpan.p0, false)) kOffset.AddSpanID(ROLL_AROUND); + } + } + } + + // add span + if(spannumber < m_nVertices) { + curSpanOff.ID = spannumber; + kOffset.Add(curSpanOff, false); + } + else if(numint == 1) // or replace the closed first span + kOffset.Replace(0, 0, p0, Point(0, 0), 0); + + } + if(!curSpanOff.NullSpan)prevSpanOff = curSpanOff; + } // end of main pre-offsetting loop + + +#ifdef _DEBUG +//testDraw->AddKurve("", &kOffset, 0, GREEN); +// outXML oxml(L"c:\\temp\\eliminateLoops.xml"); +// oxml.startElement(L"eliminateLoops"); +// oxml.Write(kOffset, L"kOffset"); +// oxml.endElement(); +#endif + // eliminate loops + if(method == NO_ELIMINATION) { + ret = 0; + return 1; + } + kOffset = eliminateLoops(kOffset, *this, offset, ret); + + if(ret == 0 && bClosed) { + // check for inverted offsets of closed kurves + if(kOffset.Closed()) { + double a = Area(); + int dir = (a < 0); + double ao = kOffset.Area(); + int dirOffset = ao < 0; + + if(dir != dirOffset) + ret = 3; + else { + // check area change compatible with offset direction - catastrophic failure + bool bigger = (a > 0 && offset > 0) || (a < 0 && offset < 0); + if(bigger && fabs(ao) < fabs(a)) ret = 2; + } + } + else + ret = 2; // started closed but now open?? + } + return (ret == 0)?1 : 0; + } + + + static Kurve eliminateLoops(const Kurve& k , const Kurve& originalk, double offset, int& ret) { + // a simple loop elimination routine based on first offset ideas in Peps + // this needs extensive work for future + // start point musn't disappear & only one valid offset is determined + // + // ret = 0 for ok + // ret = 2 for impossible geometry + + Span sp0, sp1; + Point pInt, pIntOther; + + Kurve ko; // eliminated output + ko = Matrix(k); + int kinVertex = 0; + + while(kinVertex <= k.nSpans()) { + bool clipped = false ; // not in a clipped section (assumption with this simple method) + + sp0.dir = k.Get(kinVertex, sp0.p0, sp0.pc); + sp0.ID = k.GetSpanID(kinVertex++); + if (kinVertex == 1) { + ko.Start(sp0.p0); // start point mustn't dissappear for this simple method + ko.AddSpanID(sp0.ID); + } + if (kinVertex <= k.nSpans()) { // any more? + int ksaveVertex = kinVertex ; + sp0.dir = k.Get(kinVertex, sp0.p1, sp0.pc); // first span + sp0.ID = k.GetSpanID(kinVertex++); + + sp0.SetProperties(true); + + int ksaveVertex1 = kinVertex; // mark position AA + if (kinVertex <= k.nSpans()) { // get the next but one span + sp1.dir = k.Get(kinVertex, sp1.p0, sp1.pc); + sp1.ID = k.GetSpanID(kinVertex++); + int ksaveVertex2 = kinVertex; // mark position BB + + int fwdCount = 0; + while(kinVertex <= k.nSpans()) { + sp1.dir = k.Get(kinVertex, sp1.p1, sp1.pc); // check span + sp1.ID = k.GetSpanID(kinVertex++); + sp1.SetProperties(true); + + double t[4]; + int numint = sp0.Intof(sp1, pInt, pIntOther, t); // find span intersections + if(numint && sp0.p0.Dist(pInt) < geoff_geometry::TOLERANCE ) numint=0; // check that intersection is not at the start of the check span + if(numint ) { + + if(numint == 2) { + // choose first intercept on sp0 + Span spd = sp0; + spd.p1 = pInt; + spd.SetProperties(true); + double dd = spd.length; + + spd.p1 = pIntOther; + spd.SetProperties(true); + if(dd > spd.length) pInt = pIntOther; + numint = 1; + + } + ksaveVertex = ksaveVertex1 ; + + clipped = true ; // in a clipped section + if(DoesIntersInterfere(pInt, originalk, offset) == false) { + sp0.p1 = pInt; // ok so truncate this span to the intersection + clipped = false; // end of clipped section + break; + } + // no valid intersection found so carry on + } + sp1.p0 = sp1.p1 ; // next + ksaveVertex1 = ksaveVertex2 ; // pos AA = BB + ksaveVertex2 = kinVertex; // mark + + if((kinVertex > k.nSpans() || fwdCount++ > 25) && clipped == false) break; + } + } + + if(clipped) { + ret = 2; // still in a clipped section - error + + return ko; + } + + ko.Add(sp0, false); + + kinVertex = ksaveVertex; + } + } + ret = 0; + + return ko; // no more spans - seems ok + } + + + static bool DoesIntersInterfere(const Point& pInt, const Kurve& k, double offset) { + // check that intersections don't interfere with the original kurve + Span sp; + Point dummy; + int kCheckVertex = 0; + k.Get(kCheckVertex++, sp.p0, sp.pc); + + offset = fabs(offset) - geoff_geometry::TOLERANCE; + while(kCheckVertex <= k.nSpans()) { + sp.dir = k.Get(kCheckVertex++, sp.p1, sp.pc); + sp.SetProperties(true); + // check for interference + if(Dist(sp, pInt, dummy) < offset) return true; + sp.p0 = sp.p1; + } + return false; // intersection is ok + } +} + + +static struct iso { + Span sp; + Span off; + } isodata; +static void isoRadius(Span& before, Span& blend, Span& after, double radius); + +int Kurve::OffsetISOMethod(Kurve& kOut, double off, int direction, bool BlendAll)const { + // produces a special offset Kurve - observing so-called ISO radii + // eg line/arc/line tangent - keep arc radius constant + // this method also considers arc/arc/arc etc. + // interior radius must be smallest of triplet for above. + + // parameters:- + // Output kOut resulting kurve + // Input off offset amount + // Input direction offset direction (LEFT or RIGHT) + // Input BlendAall if false only consider ISO radius for LINE/ARC/LINE + // if true consider all blended radii (ARC/ARC/ARC etc.) + double offset = (direction == GEOFF_LEFT)?off : -off; + if(FEQZ(off) || nSpans() < 1) { + kOut = *this; + return 1; + } + double cptol = 1.0e-05; + std::vector spans; + for(int i = 0; i < nSpans(); i++) { // store all spans and offsets + Get(i+1, isodata.sp, true, true); + isodata.off = isodata.sp.Offset(offset); + spans.push_back(isodata); + } + + for(int i = 0; i < nSpans() - 1; i++) // calculate intersections for none tangent spans + if(fabs(spans[i].off.ve ^ spans[i+1].off.vs) > cptol) spans[i].off.JoinSeparateSpans(spans[i+1].off); + + for(int i = 1; i < nSpans() - 1; i++) { // deal with isoradii + if(spans[i].off.dir) { + if(BlendAll) { // interior radius should be smaller than neighbours + if(spans[i-1].sp.dir) + if(spans[i-1].sp.radius < spans[i].sp.radius) continue; + if(spans[i+1].sp.dir) + if(spans[i+1].sp.radius < spans[i].sp.radius) continue; + } + else { + if((spans[i-1].off.dir || spans[i+1].off.dir)) continue; // linear neighbours only + } + + if((fabs(spans[i-1].sp.ve ^ spans[i].sp.vs) < cptol) && (fabs(spans[i].sp.ve ^ spans[i+1].sp.vs) < cptol)) { + // isoradius - calculate the new offset radius and modify neighbouring spans + isoRadius(spans[i-1].off, spans[i].off, spans[i+1].off, spans[i].sp.radius); + } + } + } + + kOut.Start(spans[0].off.p0); // start point + for(int i = 0; i < nSpans(); i++) + kOut.Add(spans[i].off.dir, spans[i].off.p1, spans[i].off.pc); // output all spans + return 1; + } + +static void isoRadius(Span& before, Span& blend, Span& after, double radius) { + // calculate the new offset radius and modify neighbouring spans + int direction = ((before.ve ^ after.vs) > 0)? 1 : -1; // offset direction + Span beforeOff = before.Offset(direction * radius); + Span afterOff = after.Offset(direction * radius); + int turnLeft = ((before.ve ^ after.vs) > 0)? 1 : -1; + if(before.dir == LINEAR) { + CLine b(beforeOff); + if(after.dir == LINEAR) { + CLine a(afterOff); + blend.pc = b.Intof(a); + } + else { + Circle a(afterOff); + b.Intof(turnLeft * after.dir, a, blend.pc); + } + } + else { + Circle b(beforeOff); + + if(after.dir == LINEAR) { + CLine a(afterOff); + a.Intof(-turnLeft * before.dir, b, blend.pc); + } + else { + // arc arc + Circle a(afterOff); + int leftright = ((Vector2d(b.pc, blend.pc) ^ Vector2d(b.pc, a.pc)) < 0)? 1 : -1; + b.Intof(leftright, a, blend.pc); + } + } + before.p1 = blend.p0 = before.Near(blend.pc); + after.p0 = blend.p1 = after.Near(blend.pc); +} diff --git a/src/Mod/Path/libarea/kurve/test.py b/src/Mod/Path/libarea/kurve/test.py new file mode 100644 index 0000000000..a179cd710f --- /dev/null +++ b/src/Mod/Path/libarea/kurve/test.py @@ -0,0 +1,9 @@ +import area + +p = area.Point(0,0) + +m = area.Matrix([1,0,0,12, 0,1,0,0, 0,0,1,0, 0,0,0,1]) + +p.Transform(m) + +print p.x, p.y \ No newline at end of file