Polygon.py 57.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# -*- coding: utf-8 -*-
"""
$Id$

Copyright 2010 Lars Kruse <devel@sumpfralle.de>

This file is part of PyCAM.

PyCAM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

PyCAM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with PyCAM.  If not, see <http://www.gnu.org/licenses/>.
"""

from pycam.Geometry.Line import Line
24
from pycam.Geometry.Point import Point, Vector
25
from pycam.Geometry.Plane import Plane
26
from pycam.Geometry import TransformableContainer, IDGenerator, get_bisector
27
from pycam.Geometry.utils import number, epsilon
28
import pycam.Utils.log
29 30 31
# import later to avoid circular imports
#from pycam.Geometry.Model import ContourModel

32 33 34 35 36 37
try:
    import OpenGL.GL as GL
    GL_enabled = True
except ImportError:
    GL_enabled = False

38

39 40 41
LINE_WIDTH_INNER = 0.7
LINE_WIDTH_OUTER = 1.3

42 43
log = pycam.Utils.log.get_logger()

44

45
class PolygonInTree(IDGenerator):
46 47 48 49 50 51
    """ This class is a wrapper around Polygon objects that is used for sorting.
    """

    next_id = 0

    def __init__(self, polygon):
52
        super(PolygonInTree, self).__init__()
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
        self.start = polygon.get_points()[0]
        self.end = polygon.get_points()[-1]
        self.polygon = polygon
        self.area = polygon.get_area()
        self.children = []

    def __eq__(self, other):
        return self.id == other.id

    def __cmp__(self, other):
        return cmp(self.area, other.area)

    def insert_if_child(self, other):
        if self.polygon.is_polygon_inside(other.polygon):
            self.children.append(other)

    def remove_child(self, other):
        try:
            self.children.remove(other)
        except ValueError:
            pass

    def get_cost(self, other):
        return other.start.sub(self.end).norm


class PolygonPositionSorter(object):
    """ sort PolygonInTree objects for a minimized way length.
    The sorter takes care that no polygons are processed before their children
    (inside polygons).
    """

    def __init__(self, polygons):
        self.polygons = []
        for poly in polygons:
            self._append(poly)
        self.optimize_order()
        self.branches = []
        for poly in self.polygons:
            self.branches.append([poly])

    def _append(self, poly):
        if self.polygons:
            min_cost = poly.get_cost(self.polygons[0])
            min_index = -1
            for index in range(len(self.polygons)):
                prev_item = self.polygons[index]
                cost = prev_item.get_cost(poly)
                try:
                    next_item = self.polygons[index + 1]
                except IndexError:
                    pass
                else:
                    cost += poly.get_cost(next_item)
                    cost -= prev_item.get_cost(next_item)
                if cost < min_cost:
                    min_cost = cost
                    min_index = index
            self.polygons.insert(min_index + 1, poly)
        else:
            self.polygons.append(poly)

    def append(self, poly):
        min_cost = None
        min_branch = None
        for branch_index in range(len(self.branches) - 1, -1, -1):
            this_branch = self.branches[branch_index]
            cost = this_branch[-1].get_cost(poly)
            try:
                next_branch = self.branches[branch_index + 1]
            except IndexError:
                pass
            else:
                cost += poly.get_cost(next_branch[0])
                cost -= this_branch[-1].get_cost(next_branch[0])
            if (min_cost is None) or (cost < min_cost):
                min_cost = cost
                min_branch = this_branch
            for child in poly.children:
                if child in this_branch:
                    break
            else:
                continue
            break
        if min_branch:
            min_branch.append(poly)


    def optimize_order(self):
        """ re-insert all items until their order stabilizes """
        finished = False
        counter_left = len(self.polygons)
        while not finished and (counter_left > 0):
            finished = True
            for index in range(len(self.polygons)):
                item = self.polygons.pop(index)
                self._append(item)
                if self.polygons[index] != item:
                    finished = False
            counter_left -= 1

    def get_polygons(self):
        result = []
        for branch in self.branches:
            result.extend(branch)
        return result


class PolygonSorter(object):
    """ sort Plygon instances according to the following rules:
    * inner polygons first (with no inside polygons)
    * inner polygons with inside polygons that are already processed
    * outer polygons (with no polygons inside that are not yet processed)
    * remaining outer polygons
    The order of polygons is slightly optimized (minimizing the way length).
    """

    def __init__(self, polygons, callback=None):
        self.polygons = []
        self.sorter = None
        self.callback = callback
        for poly in polygons:
            self._append(poly)
        self.optimize_order()

    def _append(self, polygon):
        new_item = PolygonInTree(polygon)
        for item in self.polygons:
            item.insert_if_child(new_item)
            new_item.insert_if_child(item)
        self.polygons.append(new_item)

    def optimize_order(self):
        self.polygons.sort()
        remaining_polygons = list(self.polygons)
        done_polygons = []
        while remaining_polygons:
            if self.callback:
                self.callback()
            usable_polys = []
            for poly in remaining_polygons:
                for child in poly.children:
                    if not child in done_polygons:
                        break
                else:
                    usable_polys.append(poly)
            for poly in usable_polys:
                remaining_polygons.remove(poly)
            if self.sorter is None:
                self.sorter = PolygonPositionSorter(usable_polys)
            else:
                for poly in usable_polys:
                    self.sorter.append(poly)
            done_polygons.extend(usable_polys)

    def get_polygons(self):
209 210 211 212
        if not self.sorter:
            return []
        else:
            return [poly.polygon for poly in self.sorter.get_polygons()]
213 214


215 216 217 218 219
class Polygon(TransformableContainer):

    def __init__(self, plane=None):
        super(Polygon, self).__init__()
        if plane is None:
220
            # the default plane points upwards along the z axis
221
            plane = Plane(Point(0, 0, 0), Vector(0, 0, 1))
222
        self.plane = plane
223
        self._points = []
224
        self.is_closed = False
225 226 227 228 229 230
        self.maxx = None
        self.minx = None
        self.maxy = None
        self.miny = None
        self.maxz = None
        self.minz = None
231
        self._lines_cache = None
232
        self._area_cache = None
233
        self._cached_offset_polygons = {}
234

235
    def copy(self):
236 237 238 239
        result = self.__class__(plane=self.plane.copy())
        for line in self.get_lines():
            result.append(line.copy())
        return result
240

241 242 243
    def append(self, line):
        if not self.is_connectable(line):
            raise ValueError("This line does not fit to the polygon")
244
        elif line.len < epsilon:
245 246 247 248 249 250 251 252 253
            raise ValueError("A line with zero length may not be part of a " \
                    + "polygon")
        else:
            if not self._points:
                self._points.append(line.p1)
                self._update_limits(line.p1)
                self._points.append(line.p2)
                self._update_limits(line.p2)
            elif self._points[-1] == line.p1:
254
                # the new Line can be added to the end of the polygon
sumpfralle's avatar
sumpfralle committed
255 256
                if line.dir == self._points[-1].sub(
                        self._points[-2]).normalized():
257 258 259 260
                    # Remove the last point, if the previous point combination
                    # is in line with the new Line. This avoids unnecessary
                    # points on straight lines.
                    self._points.pop(-1)
261 262 263 264
                if line.p2 != self._points[0]:
                    self._points.append(line.p2)
                    self._update_limits(line.p2)
                else:
265
                    self.is_closed = True
sumpfralle's avatar
sumpfralle committed
266 267
                # take care that the line_cache is flushed
                self.reset_cache()
268
            else:
269
                # the new Line can be added to the beginning of the polygon
270
                if (len(self._points) > 1) \
sumpfralle's avatar
sumpfralle committed
271 272
                        and (line.dir == self._points[1].sub(
                            self._points[0]).normalized()):
273 274
                    # Avoid points on straight lines - see above.
                    self._points.pop(0)
275 276 277 278
                if line.p1 != self._points[-1]:
                    self._points.insert(0, line.p1)
                    self._update_limits(line.p1)
                else:
279
                    self.is_closed = True
sumpfralle's avatar
sumpfralle committed
280 281
                # take care that the line_cache is flushed
                self.reset_cache()
282 283 284 285 286

    def __len__(self):
        return len(self._points)

    def __str__(self):
287
        if self.is_closed:
288 289 290 291 292
            status = "closed"
        else:
            status = "open"
        return "Polygon (%s) %s" % (status, [point for point in self._points])

293 294 295 296
    def reverse_direction(self):
        self._points.reverse()
        self.reset_cache()

297 298 299
    def get_reversed(self):
        result = Polygon(plane=self.plane)
        result._points = self._points[:]
300
        result.is_closed = self.is_closed
301 302 303
        result.reverse_direction()
        return result

304
    def is_connectable(self, line_or_point):
305
        if self.is_closed:
306 307 308 309
            return False
        elif not self._points:
            # empty polygons can be connected with any line
            return True
310 311 312 313 314 315 316 317 318
        if hasattr(line_or_point, "get_length_line"):
            # it is a line
            line = line_or_point
            if line.p1 == self._points[-1]:
                return True
            elif line.p2 == self._points[0]:
                return True
            else:
                return False
319
        else:
320 321 322 323 324 325
            # it is a point
            point = line_or_point
            if (point == self._points[-1]) or (point == self._points[0]):
                return True
            else:
                return False
326 327 328 329

    def next(self):
        for point in self._points:
            yield point
330
        yield self.plane
331 332

    def get_children_count(self):
333
        return len(self._points) + self.plane.get_children_count()
334 335 336 337 338 339 340 341 342

    def get_area(self):
        """ calculate the area covered by a line group
        Currently this works only for line groups in an xy-plane.
        Returns zero for empty line groups or for open line groups.
        Returns negative values for inner hole.
        """
        if not self._points:
            return 0
343
        if not self.is_closed:
344
            return 0
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
        if self._area_cache is None:
            # calculate the area for the first time
            value = [0, 0, 0]
            # taken from: http://www.wikihow.com/Calculate-the-Area-of-a-Polygon
            # and: http://softsurfer.com/Archive/algorithm_0101/algorithm_0101.htm#3D%20Polygons
            for index in range(len(self._points)):
                p1 = self._points[index]
                p2 = self._points[(index + 1) % len(self._points)]
                value[0] += p1.y * p2.z - p1.z * p2.y
                value[1] += p1.z * p2.x - p1.x * p2.z
                value[2] += p1.x * p2.y - p1.y * p2.x
            result = self.plane.n.x * value[0] + self.plane.n.y * value[1] \
                    + self.plane.n.z * value[2]
            self._area_cache = result / 2
        return self._area_cache
360

361 362 363 364 365 366 367 368 369 370 371 372
    def get_barycenter(self):
        area = self.get_area()
        if not area:
            return None
        # see: http://stackoverflow.com/questions/2355931/compute-the-centroid-of-a-3d-planar-polygon/2360507
        # first: calculate cx and y
        cxy, cxz, cyx, cyz, czx, czy = (0, 0, 0, 0, 0, 0)
        for index in range(len(self._points)):
            p1 = self._points[index]
            p2 = self._points[(index + 1) % len(self._points)]
            cxy += (p1.x + p2.x) * (p1.x * p2.y - p1.y * p2.x)
            cxz += (p1.x + p2.x) * (p1.x * p2.z - p1.z * p2.x)
373
            cyx += (p1.y + p2.y) * (p1.x * p2.y - p1.y * p2.x)
374 375
            cyz += (p1.y + p2.y) * (p1.y * p2.z - p1.z * p2.y)
            czx += (p1.z + p2.z) * (p1.z * p2.x - p1.x * p2.z)
376
            czy += (p1.z + p2.z) * (p1.y * p2.z - p1.z * p2.y)
sumpfralle's avatar
sumpfralle committed
377
        if abs(self.maxz - self.minz) < epsilon:
378
            return Point(cxy / (6 * area), cyx / (6 * area), self.minz)
sumpfralle's avatar
sumpfralle committed
379
        elif abs(self.maxy - self.miny) < epsilon:
380
            return Point(cxz / (6 * area), self.miny, czx / (6 * area))
sumpfralle's avatar
sumpfralle committed
381
        elif abs(self.maxx - self.minx) < epsilon:
382 383 384
            return Point(self.minx, cyz / (6 * area), czy / (6 * area))
        else:
            # calculate area of xy projection
sumpfralle's avatar
sumpfralle committed
385 386 387 388 389 390 391 392 393 394 395 396 397
            poly_xy = self.get_plane_projection(Plane(Point(0, 0, 0),
                    Point(0, 0, 1)))
            poly_xz = self.get_plane_projection(Plane(Point(0, 0, 0),
                    Point(0, 1, 0)))
            poly_yz = self.get_plane_projection(Plane(Point(0, 0, 0),
                    Point(1, 0, 0)))
            if (poly_xy is None) or (poly_xz is None) or (poly_yz is None):
                log.warn("Invalid polygon projection for barycenter: %s" \
                        % str(self))
                return None
            area_xy = poly_xy.get_area()
            area_xz = poly_xz.get_area()
            area_yz = poly_yz.get_area()
398 399 400
            if 0 in (area_xy, area_xz, area_yz):
                log.info("Failed assumtion: zero-sized projected area - " + \
                        "%s / %s / %s" % (area_xy, area_xz, area_yz))
sumpfralle's avatar
sumpfralle committed
401
                return None
402 403 404 405 406 407 408 409 410 411 412 413
            if abs(cxy / area_xy - cxz / area_xz) > epsilon:
                log.info("Failed assumption: barycenter xy/xz - %s / %s" % \
                        (cxy / area_xy, cxz / area_xz))
            if abs(cyx / area_xy - cyz / area_yz) > epsilon:
                log.info("Failed assumption: barycenter yx/yz - %s / %s" % \
                        (cyx / area_xy, cyz / area_yz))
            if abs(czx / area_xz - czy / area_yz) > epsilon:
                log.info("Failed assumption: barycenter zx/zy - %s / %s" % \
                        (czx / area_xz, cyz / area_yz))
            return Point(cxy / (6 * area_xy), cyx / (6 * area_xy),
                    czx / (6 * area_xz))

414 415 416 417 418 419 420
    def get_length(self):
        """ add the length of all lines within the polygon
        """
        return sum(self.get_lengths())

    def get_middle_of_line(self, index):
        if (index >= len(self._points)) \
421
                or (not self.is_closed and index == len(self._points) - 1):
422 423
            return None
        else:
sumpfralle's avatar
sumpfralle committed
424 425
            return self._points[index].add(self._points[(index + 1) % \
                    len(self._points)]).div(2)
426 427 428 429 430

    def get_lengths(self):
        result = []
        for index in range(len(self._points) - 1):
            result.append(self._points[index + 1].sub(
431
                    self._points[index]).norm)
432
        if self.is_closed:
433
            result.append(self._points[0].sub(self._points[-1]).norm)
434 435
        return result

436 437 438 439 440 441 442 443 444 445 446 447 448
    def get_max_inside_distance(self):
        """ calculate the maximum distance between two points of the polygon
        """
        if len(self._points) < 2:
            return None
        distance = self._points[1].sub(self._points[0]).norm
        for p1 in self._points:
            for p2 in self._points:
                if p1 is p2:
                    continue
                distance = max(distance, p2.sub(p1).norm)
        return distance

449 450 451
    def is_outer(self):
        return self.get_area() > 0

452
    def is_polygon_inside(self, polygon):
453 454 455 456 457 458
        if not self.is_closed:
            return False
        if (self.minx > polygon.maxx) or (self.maxx < polygon.minx) or \
                (self.miny > polygon.maxy) or (self.maxy < polygon.miny) or \
                (self.minz > polygon.maxz) or (self.maxz < polygon.minz):
            return False
459
        for point in polygon._points:
460 461 462
            if not self.is_point_inside(point):
                return False
        return True
463

464 465
    def is_point_on_outline(self, p):
        for line in self.get_lines():
466
            if line.is_point_inside(p):
467 468 469
                return True
        return False

470 471
    def is_point_inside(self, p):
        """ Test if a given point is inside of the polygon.
472
        The result is True if the point is on a line (or very close to it).
473
        """
474 475
        if not self.is_closed:
            return False
476
        # First: check if the point is within the boundary of the polygon.
477 478
        if not p.is_inside(self.minx, self.maxx, self.miny, self.maxy,
                self.minz, self.maxz):
479 480 481 482 483 484
            # the point is outside the rectangle boundary
            return False
        # see http://www.alienryderflex.com/polygon/
        # Count the number of intersections of a ray along the x axis through
        # all polygon lines.
        # Odd number -> point is inside
485 486
        intersection_count_left = 0
        intersection_count_right = 0
487 488 489 490 491 492 493 494 495 496 497
        for index in range(len(self._points)):
            p1 = self._points[index]
            p2 = self._points[(index + 1) % len(self._points)]
            # Only count intersections with lines that are partly below
            # the y level of the point. This solves the problem of intersections
            # through shared vertices or lines that go along the y level of the
            # point.
            if ((p1.y < p.y) and (p.y <= p2.y)) \
                    or ((p2.y < p.y) and (p.y <= p1.y)):
                part_y = (p.y - p1.y) / (p2.y - p1.y)
                intersection_x = p1.x + part_y * (p2.x - p1.x)
498
                if intersection_x < p.x + epsilon:
499 500 501 502 503
                    # count intersections to the left
                    intersection_count_left += 1
                if intersection_x > p.x - epsilon:
                    # count intersections to the right
                    intersection_count_right += 1
504
        # odd intersection count -> inside
505 506 507 508 509 510 511 512 513 514
        left_odd = intersection_count_left % 2 == 1
        right_odd = intersection_count_right % 2 == 1
        if left_odd and right_odd:
            # clear decision: we are inside
            return True
        elif not left_odd and not right_odd:
            # clear decision: we are outside
            return False
        else:
            # it seems like we are on the line -> inside
515
            log.debug("polygon.is_point_inside: unclear decision")
516
            return True
517

518 519 520
    def get_points(self):
        return self._points[:]

521 522 523 524
    def get_lines(self):
        """ Caching is necessary to avoid constant recalculation due to
        the "to_OpenGL" method.
        """
525
        if self._lines_cache is None:
526 527
            # recalculate the line cache
            lines = []
528 529
            for index in range(len(self._points) - 1):
                lines.append(Line(self._points[index], self._points[index + 1]))
sumpfralle's avatar
sumpfralle committed
530 531
            # Connect the last point with the first only if the polygon is
            # closed.
532
            if self.is_closed:
533
                lines.append(Line(self._points[-1], self._points[0]))
534 535 536
            self._lines_cache = lines
        return self._lines_cache[:]

537
    def to_OpenGL(self, **kwords):
538 539
        if not GL_enabled:
            return
540
        GL.glDisable(GL.GL_LIGHTING)
541 542 543 544 545
        if self.is_closed:
            is_outer = self.is_outer()
            if not is_outer:
                color = GL.glGetFloatv(GL.GL_CURRENT_COLOR)
                GL.glColor(color[0], color[1], color[2], color[3] / 2)
546 547 548
                GL.glLineWidth(LINE_WIDTH_INNER)
            else:
                GL.glLineWidth(LINE_WIDTH_OUTER)
549 550 551 552 553 554
            GL.glBegin(GL.GL_LINE_LOOP)
            for point in self._points:
                GL.glVertex3f(point.x, point.y, point.z)
            GL.glEnd()
            if not is_outer:
                GL.glColor(*color)
555 556
            # reset line width
            GL.glLineWidth(1.0)
557 558 559 560
        else:
            for line in self.get_lines():
                line.to_OpenGL(**kwords)

561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
    def _update_limits(self, point):
        if self.minx is None:
            self.minx = point.x
            self.maxx = point.x
            self.miny = point.y
            self.maxy = point.y
            self.minz = point.z
            self.maxz = point.z
        else:
            self.minx = min(self.minx, point.x)
            self.maxx = max(self.maxx, point.x)
            self.miny = min(self.miny, point.y)
            self.maxy = max(self.maxy, point.y)
            self.minz = min(self.minz, point.z)
            self.maxz = max(self.maxz, point.z)
576
        self._lines_cache = None
577
        self._area_cache = None
578 579

    def reset_cache(self):
580
        self._cached_offset_polygons = {}
581
        self._lines_cache = None
582
        self._area_cache = None
583 584 585 586 587 588 589 590 591 592
        self.minx, self.miny, self.minz = None, None, None
        self.maxx, self.maxy, self.maxz = None, None, None
        # update the limit for each line
        for point in self._points:
            self._update_limits(point)

    def get_bisector(self, index):
        p1 = self._points[index - 1]
        p2 = self._points[index]
        p3 = self._points[(index + 1) % len(self._points)]
593
        return get_bisector(p1, p2, p3, self.plane.n)
594

595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
    def get_offset_polygons_validated(self, offset):
        def get_shifted_vertex(index, offset):
            p1 = self._points[index]
            p2 = self._points[(index + 1) % len(self._points)]
            cross_offset = p2.sub(p1).cross(self.plane.n).normalized()
            bisector_normalized = self.get_bisector(index)
            factor = cross_offset.dot(bisector_normalized)
            if factor != 0:
                bisector_sized = bisector_normalized.mul(offset / factor)
                return p1.add(bisector_sized)
            else:
                return p2
        if offset * 2 >= self.get_max_inside_distance():
            # no polygons will be left
            return []
        points = []
        for index in range(len(self._points)):
            points.append(get_shifted_vertex(index, offset))
613
        max_dist = 1000 * epsilon
614 615
        def test_point_near(p, others):
            for o in others:
616
                if p.sub(o).norm < max_dist:
617 618
                    return True
            return False
619 620
        reverse_lines = []
        shifted_lines = []
621 622 623 624 625
        for index in range(len(points)):
            next_index = (index + 1) % len(points)
            p1 = points[index]
            p2 = points[next_index]
            diff = p2.sub(p1)
626 627 628 629 630 631
            old_dir = self._points[next_index].sub(
                    self._points[index]).normalized()
            if diff.normalized() != old_dir:
                # the direction turned around
                if diff.norm > max_dist:
                    # the offset was too big
632 633
                    return None
                else:
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
                    reverse_lines.append(index)
                shifted_lines.append((True, Line(p1, p2)))
            else:
                shifted_lines.append((False, Line(p1, p2)))
        # look for reversed lines
        index = 0
        while index < len(shifted_lines):
            line_reverse, line = shifted_lines[index]
            if line_reverse:
                prev_index = (index - 1) % len(shifted_lines)
                next_index = (index + 1) % len(shifted_lines)
                prev_reverse, prev_line = shifted_lines[prev_index]
                while prev_reverse and (prev_index != next_index):
                    prev_index = (prev_index - 1) % len(shifted_lines)
                    prev_reverse, prev_line = shifted_lines[prev_index]
                if prev_index == next_index:
                    # no lines are left
                    print "out 1"
                    return []
                next_reverse, next_line = shifted_lines[next_index]
                while next_reverse and (prev_index != next_index):
                    next_index = (next_index + 1) % len(shifted_lines)
                    next_reverse, next_line = shifted_lines[next_index]
                if prev_index == next_index:
                    # no lines are left
                    print "out 2"
                    return []
                if prev_line.p2.sub(next_line.p1).norm > max_dist:
                    cp, dist = prev_line.get_intersection(next_line)
                else:
                    cp = prev_line.p2
                if cp:
                    shifted_lines[prev_index] = (False, Line(prev_line.p1, cp))
                    shifted_lines[next_index] = (False, Line(cp, next_line.p2))
                else:
sumpfralle's avatar
sumpfralle committed
669 670 671 672 673 674
                    cp, dist = prev_line.get_intersection(next_line,
                            infinite_lines=True)
                    raise BaseException("Expected intersection not found: " + \
                            "%s - %s - %s(%d) / %s(%d)" % \
                            (cp, shifted_lines[prev_index+1:next_index],
                                prev_line, prev_index, next_line, next_index))
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
                if index > next_index:
                    # we wrapped around the end of the list
                    break
                else:
                    index = next_index + 1
            else:
                index += 1
        non_reversed = [line for reverse, line in shifted_lines
                if not reverse and line.len > 0]
        # split the list of lines into groups (based on intersections)
        split_points = []
        index = 0
        while index < len(non_reversed):
            other_index = 0
            while other_index < len(non_reversed):
                other_line = non_reversed[other_index]
                if (other_index == index) \
                        or (other_index == ((index - 1) % len(non_reversed))) \
                        or (other_index == ((index + 1) % len(non_reversed))):
                    # skip neighbours
                    other_index += 1
                    continue
                line = non_reversed[index]
                cp, dist = line.get_intersection(other_line)
                if cp:
                    if not test_point_near(cp,
                            (line.p1, line.p2, other_line.p1, other_line.p2)):
                        # the collision is not close to an end of the line
                        return None
                    elif (cp == line.p1) or (cp == line.p2):
                        # maybe we have been here before
                        if not cp in split_points:
707
                            split_points.append(cp)
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
                    elif (cp.sub(line.p1).norm < max_dist) \
                            or (cp.sub(line.p2).norm < max_dist):
                        if cp.sub(line.p1).norm < cp.sub(line.p2).norm:
                            non_reversed[index] = Line(cp, line.p2)
                        else:
                            non_reversed[index] = Line(line.p1, cp)
                        non_reversed.pop(other_index)
                        non_reversed.insert(other_index,
                                Line(other_line.p1, cp))
                        non_reversed.insert(other_index + 1,
                                Line(cp, other_line.p2))
                        split_points.append(cp)
                        if other_index < index:
                            index += 1
                        # skip the second part of this line
723
                        other_index += 1
724 725 726 727 728
                    else:
                        # the split of 'other_line' will be handled later
                        pass
                other_index += 1
            index += 1
729 730
        groups = [[]]
        current_group = 0
731 732 733 734 735 736
        split_here = False
        for line in non_reversed:
            if line.p1 in split_points:
                split_here = True
            if split_here:
                split_here = False
737 738 739 740 741 742
                # check if any preceeding group fits to the point
                for index, group in enumerate(groups):
                    if not group:
                        continue
                    if index == current_group:
                        continue
743 744 745
                    if group[0].p1 == group[-1].p2:
                        # the group is already closed
                        continue
746 747 748 749 750 751 752 753 754
                    if line.p1 == group[-1].p2:
                        current_group = index
                        groups[current_group].append(line)
                        break
                else:
                    current_group = len(groups)
                    groups.append([line])
            else:
                groups[current_group].append(line)
755 756
            if line.p2 in split_points:
                split_here = True
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
        def is_joinable(g1, g2):
            if g1 and g2 and (g1[0].p1 != g1[-1].p2):
                if g1[0].p1 == g2[-1].p2:
                    return g2 + g1
                if g2[0].p1 == g1[-1].p2:
                    return g1 + g2
            return None
        # try to combine open groups
        for index1, group1 in enumerate(groups):
            if not group1:
                continue
            for index2, group2 in enumerate(groups):
                if not group2:
                    continue
                if index2 <= index1:
                    continue
                if (group1[-1].p2 == group2[0].p1) \
                        and (group1[0].p1 == group2[-1].p2):
                    group1.extend(group2)
                    groups[index2] = []
                    break
778
        result_polygons = []
779 780 781
        print "********** GROUPS **************"
        for a in groups:
            print a
782 783 784 785
        for group in groups:
            if len(group) <= 2:
                continue
            poly = Polygon(self.plane)
786
            #print "**************************************"
787
            for line in group:
788 789 790 791 792 793 794 795 796 797 798
                try:
                    poly.append(line)
                except ValueError:
                    print "NON_REVERSED"
                    for a in non_reversed:
                        print a
                    print groups
                    print split_points
                    print poly
                    print line
                    raise
799
            if self.is_closed and ((not poly.is_closed) \
800 801
                    or (self.is_outer() != poly.is_outer())):
                continue
802
            elif (not self.is_closed) and (poly.get_area() != 0):
803
                continue
804
            else:
805
                result_polygons.append(poly)
806 807
        return result_polygons

808
    def get_offset_polygons_incremental(self, offset, depth=20):
809 810
        if offset == 0:
            return [self]
811 812 813 814 815 816 817 818 819 820 821 822
        if self._cached_offset_polygons.has_key(offset):
            return self._cached_offset_polygons[offset]
        def is_better_offset(previous_offset, alternative_offset):
            return ((offset < alternative_offset < 0) \
                    or (0 < alternative_offset < offset)) \
                    and (abs(alternative_offset) > abs(previous_offset))
        # check the cache for a good starting point
        best_offset = 0
        best_offset_polygons = [self]
        for cached_offset in self._cached_offset_polygons:
            if is_better_offset(best_offset, cached_offset):
                best_offset = cached_offset
sumpfralle's avatar
sumpfralle committed
823 824
                best_offset_polygons = \
                        self._cached_offset_polygons[cached_offset]
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
        remaining_offset = offset - best_offset
        result_polygons = []
        for poly in best_offset_polygons:
            result = poly.get_offset_polygons_validated(remaining_offset)
            if not result is None:
                result_polygons.extend(result)
            else:
                lower = number(0)
                upper = remaining_offset
                loop_limit = 90
                while (loop_limit > 0):
                    middle = (upper + lower) / 2
                    result = poly.get_offset_polygons_validated(middle)
                    if result is None:
                        upper = middle
                    else:
                        if depth > 0:
                            # the original polygon was splitted or modified
                            print "Next level: %s" % str(middle)
                            shifted_sub_polygons = []
                            for sub_poly in result:
                                shifted_sub_polygons.extend(
                                        sub_poly.get_offset_polygons(
                                            remaining_offset - middle,
                                            depth=depth-1))
                            result_polygons.extend(shifted_sub_polygons)
                            break
                        else:
                            print "Maximum recursion level reached"
                            break
                    loop_limit -= 1
856
                else:
857 858 859 860
                    # no split event happened -> no valid shifted polygon
                    pass
        self._cached_offset_polygons[offset] = result_polygons
        return result_polygons
861

862
    def get_offset_polygons(self, offset, callback=None):
863 864 865
        def get_shifted_vertex(index, offset):
            p1 = self._points[index]
            p2 = self._points[(index + 1) % len(self._points)]
866
            cross_offset = p2.sub(p1).cross(self.plane.n).normalized()
867 868
            bisector_normalized = self.get_bisector(index)
            factor = cross_offset.dot(bisector_normalized)
sumpfralle's avatar
sumpfralle committed
869 870 871 872 873
            if factor != 0:
                bisector_sized = bisector_normalized.mul(offset / factor)
                return p1.add(bisector_sized)
            else:
                return p2
874 875 876 877 878 879
        def simplify_polygon_intersections(lines):
            new_group = lines[:]
            # remove all non-adjacent intersecting lines (this splits the group)
            if len(new_group) > 0:
                group_starts = []
                index1 = 0
880
                fallout3 = 0
881 882
                while index1 < len(new_group):
                    index2 = 0
883 884
                    fallout2 = len(new_group)
                    fallout3 += 1
885
                    while index2 < len(new_group):
886
                        fallout2 -= 1
887 888
                        index_distance = min(abs(index2 - index1), \
                                abs(len(new_group) - (index2 - index1))) 
889 890 891 892 893
                        if fallout3 > 10000:
                            print "FALLOUT3"
                            print index_distance, index2, index1, len(new_group), len(group_starts)
                            import sys
                            sys.exit(1)
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
                        # skip neighbours
                        if index_distance > 1:
                            line1 = new_group[index1]
                            line2 = new_group[index2]
                            intersection, factor = line1.get_intersection(line2)
                            if intersection and (intersection != line1.p1) \
                                    and (intersection != line1.p2):
                                del new_group[index1]
                                new_group.insert(index1,
                                        Line(line1.p1, intersection))
                                new_group.insert(index1 + 1,
                                        Line(intersection, line1.p2))
                                # Shift all items in "group_starts" by one if
                                # they reference a line whose index changed.
                                for i in range(len(group_starts)):
                                    if group_starts[i] > index1:
                                        group_starts[i] += 1
                                if not index1 + 1 in group_starts:
                                    group_starts.append(index1 + 1)
                                # don't update index2 -> maybe there are other hits
                            elif intersection and (intersection == line1.p1):
                                if not index1 in group_starts:
                                    group_starts.append(index1)
                                index2 += 1
                            else:
                                index2 += 1
                        else:
                            index2 += 1
                    index1 += 1
                # The lines intersect each other
                # We need to split the group.
                if len(group_starts) > 0:
                    group_starts.sort()
                    groups = []
                    last_start = 0
                    for group_start in group_starts:
                        groups.append(new_group[last_start:group_start])
                        last_start = group_start
                    # Add the remaining lines to the first group or as a new
                    # group.
                    if groups[0][0].p1 == new_group[-1].p2:
                        groups[0] = new_group[last_start:] + groups[0]
                    else:
                        groups.append(new_group[last_start:])
                    # try to find open groups that can be combined
                    combined_groups = []
                    for index, current_group in enumerate(groups):
                        # Check if the group is not closed: try to add it to
                        # other non-closed groups.
                        if current_group[0].p1 == current_group[-1].p2:
                            # a closed group
                            combined_groups.append(current_group)
                        else:
                            # the current group is open
                            for other_group in groups[index + 1:]:
                                if other_group[0].p1 != other_group[-1].p2:
                                    # This group is also open - a candidate
                                    # for merging?
                                    if other_group[0].p1 == current_group[-1].p2:
                                        current_group.reverse()
                                        for line in current_group:
                                            other_group.insert(0, line)
                                        break
                                    if other_group[-1].p2 == current_group[0].p1:
                                        other_group.extend(current_group)
                                        break
                            else:
                                # not suitable open group found
                                combined_groups.append(current_group)
                    return combined_groups
                else:
                    # just return one group without intersections
                    return [new_group]
            else:
                return None
969
        offset = number(offset)
970 971 972 973 974 975
        if offset == 0:
            return [self]
        if offset * 2 >= self.get_max_inside_distance():
            # This offset will not create a valid offset polygon.
            # Sadly there is currently no other way to detect a complete flip of
            # something like a circle.
976
            log.debug("Skipping offset polygon: polygon is too small")
977 978 979 980 981 982 983 984 985
            return []
        points = []
        for index in range(len(self._points)):
            points.append(get_shifted_vertex(index, offset))
        new_lines = []
        for index in range(len(points)):
            p1 = points[index]
            p2 = points[(index + 1) % len(points)]
            new_lines.append(Line(p1, p2))
986 987
        if callback and callback():
            return None
988 989
        cleaned_line_groups = simplify_polygon_intersections(new_lines)
        if cleaned_line_groups is None:
990 991
            log.debug("Skipping offset polygon: intersections could not be " \
                    + "simplified")
992 993
            return None
        else:
994 995 996
            if not cleaned_line_groups:
                log.debug("Skipping offset polygon: no polygons left after " \
                        + "intersection simplification")
997 998 999 1000
            # remove all groups with a toggled direction
            self_is_outer = self.is_outer()
            groups = []
            for lines in cleaned_line_groups:
1001 1002
                if callback and callback():
                    return None
1003
                group = Polygon(self.plane)
1004 1005 1006 1007 1008 1009
                for line in lines:
                    group.append(line)
                if group.is_outer() != self_is_outer:
                    # We ignore groups that changed the direction. These
                    # parts of the original group are flipped due to the
                    # offset.
sumpfralle's avatar
sumpfralle committed
1010 1011
                    log.debug("Ignoring reversed polygon: %s / %s" % \
                            (self.get_area(), group.get_area()))
1012 1013 1014 1015 1016 1017
                    continue
                # Remove polygons that should be inside the original,
                # but due to float inaccuracies they are not.
                if ((self.is_outer() and (offset < 0)) \
                        or (not self.is_outer() and (offset > 0))) \
                        and (not self.is_polygon_inside(group)):
sumpfralle's avatar
sumpfralle committed
1018 1019
                    log.debug("Ignoring inaccurate polygon: %s / %s" \
                            % (self.get_area(), group.get_area()))
1020 1021
                    continue
                groups.append(group)
1022 1023
            if not groups:
                log.debug("Skipping offset polygon: toggled polygon removed")
1024 1025 1026 1027 1028
            # remove all polygons that are within other polygons
            result = []
            for group in groups:
                inside = False
                for group_test in groups:
1029 1030
                    if callback and callback():
                        return None
1031 1032 1033 1034 1035 1036
                    if group_test is group:
                        continue
                    if group_test.is_polygon_inside(group):
                        inside = True
                if not inside:
                    result.append(group)
1037 1038 1039
            if not result:
                log.debug("Skipping offset polygon: polygon is inside of " \
                        + "another one")
1040 1041 1042
            return result

    def get_offset_polygons_old(self, offset):
1043 1044 1045 1046
        def get_parallel_line(line, offset):
            if offset == 0:
                return Line(line.p1, line.p2)
            else:
1047
                cross_offset = line.dir.cross(self.plane.n).normalized().mul(offset)
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
                # Prolong the line at the beginning and at the end - to allow
                # overlaps. Use factor "2" to take care for star-like structure
                # where a complete convex triangle would get cropped (two lines
                # get lost instead of just one). Use the "abs" value to
                # compensate negative offsets.
                in_line = line.dir.mul(2 * abs(offset))
                return Line(line.p1.add(cross_offset).sub(in_line),
                        line.p2.add(cross_offset).add(in_line))
        def do_lines_intersection(l1, l2):
            """ calculate the new intersection between two neighbouring lines
            """
            # TODO: use Line.get_intersection instead of the code below
            if l1.p2 == l2.p1:
                # intersection is already fine
                return
            if (l1.p1 is None) or (l2.p1 is None):
                # one line was already marked as obsolete
                return
            x1, x2, x3, x4 = l2.p1, l2.p2, l1.p1, l1.p2
            a = x2.sub(x1)
            b = x4.sub(x3)
            c = x3.sub(x1)
            # see http://mathworld.wolfram.com/Line-LineIntersection.html (24)
            try:
                factor = c.cross(b).dot(a.cross(b)) / a.cross(b).normsq
            except ZeroDivisionError:
                l2.p1 = None
                return
            if not (0 <= factor < 1):
                # The intersection is always supposed to be within p1 and p2.
                l2.p1 = None
            else:
                intersection = x1.add(a.mul(factor))
                if Line(l1.p1, intersection).dir != l1.dir:
                    # Remove lines that would change their direction due to the
                    # new intersection. These are usually lines that become
                    # obsolete due to a more favourable intersection of the two
                    # neighbouring lines. This appears at small corners.
                    l1.p1 = None
                elif Line(intersection, l2.p2).dir != l2.dir:
                    # see comment above
                    l2.p1 = None
                elif l1.p1 == intersection:
                    # remove invalid lines (zero length)
                    l1.p1 = None
                elif l2.p2 == intersection:
                    # remove invalid lines (zero length)
                    l2.p1 = None
                else:
                    # shorten both lines according to the new intersection
                    l1.p2 = intersection
                    l2.p1 = intersection
        def simplify_polygon_intersections(lines):
            finished = False
            new_group = lines[:]
            while not finished:
                if len(new_group) > 1:
                    # Calculate new intersections for each pair of adjacent
                    # lines.
                    for index in range(len(new_group)):
1108
                        if (index == 0) and (not self.is_closed):
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
                            # skip the first line if the group is not closed
                            continue
                        # this also works for index==0 (closed groups)
                        l1 = new_group[index - 1]
                        l2 = new_group[index]
                        do_lines_intersection(l1, l2)
                # Remove all lines that were marked as obsolete during
                # intersection calculation.
                clean_group = [line for line in new_group
                        if not line.p1 is None]
                finished = len(new_group) == len(clean_group)
1120
                if (len(clean_group) == 1) and self.is_closed:
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
                    new_group = []
                    finished = True
                else:
                    new_group = clean_group
            # remove all non-adjacent intersecting lines (this splits the group)
            if len(new_group) > 0:
                group_starts = []
                index1 = 0
                while index1 < len(new_group):
                    index2 = 0
                    while index2 < len(new_group):
                        index_distance = min(abs(index2 - index1), \
                                abs(len(new_group) - (index2 - index1))) 
                        # skip neighbours
                        if index_distance > 1:
                            line1 = new_group[index1]
                            line2 = new_group[index2]
                            intersection, factor = line1.get_intersection(line2)
                            if intersection and (intersection != line1.p1) \
                                    and (intersection != line1.p2):
                                del new_group[index1]
                                new_group.insert(index1,
                                        Line(line1.p1, intersection))
                                new_group.insert(index1 + 1,
                                        Line(intersection, line1.p2))
                                # Shift all items in "group_starts" by one if
                                # they reference a line whose index changed.
                                for i in range(len(group_starts)):
                                    if group_starts[i] > index1:
                                        group_starts[i] += 1
                                if not index1 + 1 in group_starts:
                                    group_starts.append(index1 + 1)
                                # don't update index2 -> maybe there are other hits
                            elif intersection and (intersection == line1.p1):
                                if not index1 in group_starts:
                                    group_starts.append(index1)
                                index2 += 1
                            else:
                                index2 += 1
                        else:
                            index2 += 1
                    index1 += 1
                # The lines intersect each other
                # We need to split the group.
                if len(group_starts) > 0:
                    group_starts.sort()
                    groups = []
                    last_start = 0
                    for group_start in group_starts:
                        groups.append(new_group[last_start:group_start])
                        last_start = group_start
                    # Add the remaining lines to the first group or as a new
                    # group.
                    if groups[0][0].p1 == new_group[-1].p2:
                        groups[0] = new_group[last_start:] + groups[0]
                    else:
                        groups.append(new_group[last_start:])
                    # try to find open groups that can be combined
                    combined_groups = []
                    for index, current_group in enumerate(groups):
                        # Check if the group is not closed: try to add it to
                        # other non-closed groups.
                        if current_group[0].p1 == current_group[-1].p2:
                            # a closed group
                            combined_groups.append(current_group)
                        else:
                            # the current group is open
                            for other_group in groups[index + 1:]:
                                if other_group[0].p1 != other_group[-1].p2:
                                    # This group is also open - a candidate
                                    # for merging?
                                    if other_group[0].p1 == current_group[-1].p2:
                                        current_group.reverse()
                                        for line in current_group:
                                            other_group.insert(0, line)
                                        break
                                    if other_group[-1].p2 == current_group[0].p1:
                                        other_group.extend(current_group)
                                        break
                            else:
                                # not suitable open group found
                                combined_groups.append(current_group)
                    return combined_groups
                else:
                    # just return one group without intersections
                    return [new_group]
            else:
                return None
        new_lines = []
        for line in self.get_lines():
            new_lines.append(get_parallel_line(line, offset))
        cleaned_line_groups = simplify_polygon_intersections(new_lines)
        if cleaned_line_groups is None:
            return None
        else:
            # remove all groups with a toggled direction
            self_is_outer = self.is_outer()
            groups = []
            for lines in cleaned_line_groups:
1220
                group = Polygon(self.plane)
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
                for line in lines:
                    group.append(line)
                if group.is_outer() == self_is_outer:
                    # We ignore groups that changed the direction. These
                    # parts of the original group are flipped due to the
                    # offset.
                    groups.append(group)
            return groups

    def get_cropped_polygons(self, minx, maxx, miny, maxy, minz, maxz):
        """ crop a line group according to a 3d bounding box

        The result is a list of Polygons, since the bounding box can possibly
        break the original line group into several non-connected pieces.
        """
        new_groups = []
        for line in self.get_lines():
            new_line = None
1239
            if line.is_completely_inside(minx, maxx, miny, maxy, minz, maxz):
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
                new_line = line
            else:
                cropped_line = line.get_cropped_line(minx, maxx, miny, maxy,
                        minz, maxz)
                if not cropped_line is None:
                    new_line = cropped_line
            # add the new line to one of the line groups
            if not new_line is None:
                # try to find a suitable line group
                for new_group in new_groups:
                    try:
                        new_group.append(new_line)
                        break
                    except ValueError:
                        # the line did not fit to this group (segment is broken)
                        pass
                else:
                    # no suitable group was found - we create a new one
1258
                    new_group = Polygon(self.plane)
1259 1260 1261 1262 1263 1264 1265
                    new_group.append(new_line)
                    new_groups.append(new_group)
        if len(new_groups) > 0:
            return new_groups
        else:
            return None

1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
    def get_plane_projection(self, plane):
        if plane == self.plane:
            return self
        elif plane.n.dot(self.plane.n) == 0:
            log.warn("Polygon projection onto plane: orthogonal projection " \
                    + "is not possible")
            return None
        else:
            result = Polygon(plane)
            for line in self.get_lines():
1276 1277
                p1 = plane.get_point_projection(line.p1)
                p2 = plane.get_point_projection(line.p2)
1278
                result.append(Line(p1, p2))
1279 1280 1281
            # check if the projection would revert the direction of the polygon
            if plane.n.dot(self.plane.n) < 0:
                result.reverse_direction()
1282 1283
            return result

1284 1285 1286 1287 1288 1289 1290 1291 1292
    def is_overlap(self, other):
        for line1 in self.get_lines():
            for line2 in other.get_lines():
                cp, dist = line1.get_intersection(line2)
                if not cp is None:
                    return True
        return False

    def union(self, other):
1293 1294 1295
        """ This "union" of two polygons only works for polygons without
        shared edges. TODO: fix the issues of shared edges!
        """
1296 1297 1298 1299
        # don't import earlier to avoid circular imports
        from pycam.Geometry.Model import ContourModel
        # check if one of the polygons is completely inside of the other
        if self.is_polygon_inside(other):
1300
            return [self]
1301
        if other.is_polygon_inside(self):
1302
            return [other]
1303 1304 1305
        # check if there is any overlap at all
        if not self.is_overlap(other):
            # no changes
1306
            return [self, other]
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
        contour = ContourModel(self.plane)
        def get_outside_lines(poly1, poly2):
            result = []
            for line in poly1.get_lines():
                collisions = []
                for o_line in poly2.get_lines():
                    cp, dist = o_line.get_intersection(line)
                    if (not cp is None) and (0 < dist < 1):
                        collisions.append((cp, dist))
                # sort the collisions according to the distance
                collisions.append((line.p1, 0))
                collisions.append((line.p2, 1))
                collisions.sort(key=lambda (cp, dist): dist)
                for index in range(len(collisions) - 1):
                    p1 = collisions[index][0]
                    p2 = collisions[index + 1][0]
                    if p1.sub(p2).norm < epsilon:
                        # ignore zero-length lines
                        continue
                    # Use the middle between p1 and p2 to check the
                    # inner/outer state.
                    p_middle = p1.add(p2).div(2)
                    p_inside = poly2.is_point_inside(p_middle) \
                            and not poly2.is_point_on_outline(p_middle)
                    if not p_inside:
                        result.append(Line(p1, p2))
            return result
        outside_lines = []
        outside_lines.extend(get_outside_lines(self, other))
        outside_lines.extend(get_outside_lines(other, self))
        for line in outside_lines:
            contour.append(line)
        # fix potential overlapping at the beginning and end of each polygon
        result = []
        for poly in contour.get_polygons():
1342
            if not poly.is_closed:
1343 1344 1345 1346
                lines = poly.get_lines()
                line1 = lines[-1]
                line2 = lines[0]
                if (line1.dir == line2.dir) \
1347
                        and (line1.is_point_inside(line2.p1)):
1348 1349
                    # remove the last point and define the polygon as closed
                    poly._points.pop(-1)
1350
                    poly.is_closed = True
1351 1352 1353
            result.append(poly)
        return result

1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
    def split_line(self, line):
        outer = []
        inner = []
        # project the line onto the polygon's plane
        proj_line = self.plane.get_line_projection(line)
        intersections = []
        for pline in self.get_lines():
            cp, d = proj_line.get_intersection(pline)
            if cp:
                intersections.append((cp, d))
        # sort the intersections
        intersections.sort(key=lambda (cp, d): d)
        intersections.insert(0, (proj_line.p1, 0))
        intersections.append((proj_line.p2, 1))
        get_original_point = lambda d: line.p1.add(line.vector.mul(d))
        for index in range(len(intersections) - 1):
            p1, d1 = intersections[index]
            p2, d2 = intersections[index + 1]
            if p1 != p2:
                middle = p1.add(p2).div(2)
                new_line = Line(get_original_point(d1), get_original_point(d2))
                if self.is_point_inside(middle):
                    inner.append(new_line)
                else:
                    outer.append(new_line)
        return (inner, outer)