PathAccumulator.py 2.05 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>
Copyright 2008 Lode Leroy

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/>.
"""

lode_leroy's avatar
lode_leroy committed
24 25
from pycam.Geometry import *

26 27 28 29 30 31 32 33
def _check_colinearity(p1, p2, p3):
    v1 = p2.sub(p1)
    v2 = p3.sub(p2)
    v1.normalize()
    v2.normalize()
    # compare if the normalized distances between p1-p2 and p2-p3 are equal
    return v1 == v2

lode_leroy's avatar
lode_leroy committed
34 35 36 37 38 39 40 41 42
class PathAccumulator:
    def __init__(self, zigzag=False):
        self.paths = []
        self.curr_path = None
        self.zigzag = zigzag

    def append(self, p):
        if self.curr_path == None:
            self.curr_path = Path()
43 44 45 46 47
        if (len(self.curr_path.points) >= 2) and \
                (_check_colinearity(self.curr_path.points[-2], self.curr_path.points[-1], p)):
            # remove the previous point since it is in line with its
            # predecessor and the new point
            self.curr_path.points.pop()
lode_leroy's avatar
lode_leroy committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
        self.curr_path.append(p)

    def new_direction(self, dir):
        self.scanline = 0

    def end_direction(self):
        pass

    def new_scanline(self):
        self.scanline += 1
        if self.curr_path:
            print "ERROR: curr_path expected to be empty"
            self.curr_path = None

    def end_scanline(self):
        if self.curr_path:
            if self.zigzag and (self.scanline%2 == 0):
                self.curr_path.reverse()
            self.paths.append(self.curr_path)
            self.curr_path = None

    def finish(self):
        pass