diff --git a/infill.py b/infill.py index 5e211fe..70a1d11 100644 --- a/infill.py +++ b/infill.py @@ -57,7 +57,7 @@ class Infill(LineGroup): def __init__(self, trimOutline, pathWidth, angleDegrees, shiftX=0, shiftY=0, - design=None, designType=c.PARTIAL_ROW): + design=None, designType=c.PARTIAL_ROW, testMode = False): LineGroup.__init__(self, None) self.shiftX = shiftX self.shiftY = shiftY @@ -82,9 +82,12 @@ def __init__(self, trimOutline, pathWidth, angleDegrees, shiftX=0, shiftY=0, self.design = LineGroup(design) # print('\nInfill times:') - for i in range(self.designType, c.TRIMMED_FIELD): - startTime = time.time() - self.operations[i](); + if(not testMode): + for i in range(self.designType, c.TRIMMED_FIELD): + startTime = time.time() + self.operations[i](); + + testMode = False # print((self.operations[i].__name__ + # ''.ljust(maxLength - len(self.operations[i].__name__)) + # '%.2f sec' %(time.time()-startTime))) diff --git a/outline.py b/outline.py index 9999f77..aa5b03f 100644 --- a/outline.py +++ b/outline.py @@ -60,12 +60,11 @@ def fromMeshSection(self, m_section): @finishedOutline def addInternalShape(self, inShape): - if(not inShape.outlineFinished): - print('********** Your internal shape was not a finished outline **********') + inShape.finishOutline() if(not self.isInside(inShape.lines[0].start)): - print('********** The internal shape is not inside the main outline. **********') - if(self.doShapesIntersect(inShape)): - print('********** Internal shape is not completely inside main outline. **********') + raise Exception('********** The internal shape is not inside the main outline. **********') + if(self.doOutlinesIntersect(inShape)): + raise Exception('********** Internal shape is not completely inside main outline. **********') for line in inShape: self.append(line) diff --git a/pointtest.py b/pointtest.py deleted file mode 100644 index d0e974b..0000000 --- a/pointtest.py +++ /dev/null @@ -1,34 +0,0 @@ -# -*- coding: utf-8 -*- -""" -Created on Mon Jun 26 13:53:41 2017 - -@author: siddhant -""" - -import unittest -from point import Point -#from point import __lt__ -#from point import __gt__ -#from point import __neg__ -from point import __eq__ - -p1 = Point(1.0, 2.0, 3.0) - -class PointTestCase (unittest.TestCase): - # p1 = Point(1.0, 2.0, 0.0) - - def test_point_negative(self): - self.assertTrue(__neg__(p1) == Point(-1.0, -2.0, 3.0)) - #self.assertTrue(p1.__neg__ == Point(-1.0, -2.0, 3.0)) - - #def test_point_greater(self): - # self.assertFalse(__gt__((7, 8, 9)) == False) - - def test_point_equal(self): - self.assertEqual(__eq__(p1, (1.0, 2.0, 3.0))) - -def main(): - unittest.main() - -if __name__ == '__main__': - main() \ No newline at end of file diff --git a/unittest.py b/unittest1_point_line.py similarity index 91% rename from unittest.py rename to unittest1_point_line.py index 725cffb..d89d50d 100644 --- a/unittest.py +++ b/unittest1_point_line.py @@ -25,8 +25,6 @@ class PointTestCase (unittest.TestCase): - #def test_point_valid(self): - def test_point_equal(self): """ check if point is equal to given point (__eq__) """ self.assertTrue(p1 == Point(1.0, 2.0, 3.0)) @@ -91,6 +89,19 @@ def test_point_rotate(self): """ check if point rotates as expected (rotate) """ self.assertTrue(p1.rotate(0) == Point(1.0, 2.0, 3.0)) + def test_point_normal_vector(self): + """ checks if a normal vector is correctly returned (normalVector) """ + self.assertTrue(p1.normalVector[3] == 1.0) + + def test_point_point(self): + """ checks if sliced correctly to return first three elements (point) """ + self.assertTrue(tuple(p1.point) == (1.0, 2.0, 3.0)) + + def test_point_transform(self): + """ checks if the point is transformed correctly by matrix (transform) """ + mtr = [[1,2,3,1],[4,0,3,6],[2,7,0,4],[0,0,0,1]] + self.assertTrue(p1.transform(mtr) == Point(15,19,20)) + class LineTestCase(unittest.TestCase): def test_length(self): @@ -157,7 +168,7 @@ def test_line_rotate(self): def test_line_bounding_boxes(self): """ checks if bounding boxes of two lines intersect """ self.assertTrue(l1.doBoundingBoxesIntersect(l2) == True) - self.assertFalse(l1.doBoundingBoxesIntersect(l2) != True) + self.assertFalse(l1.doBoundingBoxesIntersect(l2) == False) def test_line_point_to_line_distance(self): """ checks the distance between point and line (pointToLineDist) """ @@ -200,4 +211,4 @@ def main(): unittest.main() if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/unittest2_linegroup.py b/unittest2_linegroup.py new file mode 100644 index 0000000..b6f1a2e --- /dev/null +++ b/unittest2_linegroup.py @@ -0,0 +1,200 @@ +# -*- coding: utf-8 -*- +""" +Created on Wed Jul 5 15:50:23 2017 + +@author: siddhant +""" + +import unittest +from point import Point +from line import Line +from linegroup import LineGroup +import constants as c +import numpy as np + +p1 = Point(1.0, 2.0, 3.0) +p2 = Point(2.0, 4.0, 6.0) +p3 = Point(6.0, 12.0, 18.0) +p4 = Point(8.0, 12.0, 14.0) + +l1 = Line(p1, p2) +l2 = Line(p1, p3) +l4 = Line(Point(0,0), Point(0, 8, 0)) +l5 = Line(Point(4, 0, 0), Point(0, 6, 0)) + +#lg1 = LineGroup() +#lg2 = LineGroup() +#lg2.append(l1) + + +class LineGroupTestCase(unittest.TestCase): + + def test_linegroup_getitem(self): + """ checks if correct line in linegroup is returned (__getitem__) """ + ltest = LineGroup() + ltest.append(l1) + self.assertTrue(ltest[0] == l1) + + def test_linegroup_append(self): + """ checks if correct line is appended to linegroup (append) """ + ltest = LineGroup() + ltest.append(l1) + ltest.append(l2) + self.assertTrue(ltest[1] == l2) + + def test_linegroup_remove(self): + """ checks if line is correctly removed from group (remove) """ + ltest = LineGroup() + ltest.append(l1) + ltest.append(l2) + ltest.remove(l1) + self.assertTrue(ltest[0] == l2) + + def test_linegroup_length(self): + """ checks if linegroup contains expected number of lines (__len__) """ + ltest = LineGroup() + ltest.append(l1) + ltest.append(l2) + ltest.append(l4) + count = 0 + for l in ltest: + count = count + 1 + self.assertEqual(count, 3) + + def test_linegroup_pop(self): + """ checks if correct line returned when linegroup popped (pop) """ + ltest = LineGroup() + ltest.append(l1) + ltest.append(l2) + self.assertTrue(ltest.pop() == l2) + + def test_linegroup_midpoint(self): + """ checks if correct midpoint of linegroup returned (getMidPoint) """ + #print(lg2) + ltest = LineGroup() + ltest.append(l1) + self.assertTrue(ltest.getMidPoint() == Point(1.5, 3.0, 0.0)) + + def test_linegroup_add_linegroup(self): + """ checks if linegroup is correctly added (addLineGroup) """ + ltest = LineGroup() + ltest.append(l1) + + ltest1 = LineGroup() + ltest1.append(l2) + ltest.addLineGroup(ltest1) + self.assertTrue(ltest[1] == l2) + + def test_linegroup_mirror(self): + """ checks if linegroup is correctly mirrored (mirror) """ + ltest = LineGroup() + ltest.append(l1) + + #ltest1 = LineGroup() + #ltest1.append(Line(Point(1,-2,3), Point(2,-4,6))) + + self.assertTrue(ltest.mirror(c.X)[0] == + Line(Point(1,-2,3), Point(2,-4,6))) + + def test_linegroup_sort(self): + """ checks if linegroup is sorted correctly (sort) """ + ltest = LineGroup() + ltest.append(l1) + ltest.append(l4) + ltest.sort() + self.assertTrue(ltest[0] == l4) + + def test_linegroup_fourcorners(self): + """ checks if the four corners correspond to the correct points """ + ltest = LineGroup() + ltest.append(l4) + ltest.append(l5) + self.assertTrue(ltest.fourCorners()[0] == Point(0,0,0)) + self.assertTrue(ltest.fourCorners()[1] == Point(4,0,0)) + self.assertTrue(ltest.fourCorners()[2] == Point(4,8,0)) + self.assertTrue(ltest.fourCorners()[3] == Point(0,8,0)) + + def test_linegroup_scale(self): + """ checks if start and end points of lines are scaled correctly """ + ltest = LineGroup() + ltest.append(l1) + ltest.append(l2) + + self.assertTrue(ltest.scale(2,3)[0] == + Line(Point(2,6,3), Point(4,12,6))) + self.assertTrue(ltest.scale(2,3)[1] == + Line(Point(2,6,3), Point(12,36,18))) + + def test_linegroup_rotate(self): + """ checks if linegroup is rotated correctly """ + ltest = LineGroup() + ltest.append(l1) + ltest.append(l2) + self.assertTrue(ltest.rotate(np.pi/2)[0] == + Line(Point(-2,1,3), Point(-4,2,6))) + self.assertTrue(ltest.rotate(np.pi/2)[1] == + Line(Point(-2,1,3), Point(-12,6,18))) + + def test_linegroup_translate(self): + """ checks if linegroup is translated correctly """ + ltest = LineGroup() + ltest.append(l1) + ltest.append(l2) + self.assertTrue(ltest.translate(2,4)[0] == + Line(Point(3,6,3), Point(4,8,6))) + self.assertTrue(ltest.translate(2,4)[1] == + Line(Point(3,6,3), Point(8,16,18))) + + def test_linegroup_line_outside_boundingbox(self): + """ checks if the given line is outside the bounding box """ + ltest = LineGroup() + ltest.append(l1) + ltest.append(l2) + self.assertTrue(ltest.lineOutsideBoundingBox(l4) == True) + self.assertTrue(ltest.lineOutsideBoundingBox(l5) == False) + + + def test_linegroup_line_add_points(self): + """ checks if line from given points is added to linegroup """ + ltest = LineGroup() + ltest.append(l1) + ltest.append(l2) + p = [Point(1,1,0), Point(4,5,0)] + ltest.addLinesFromPoints(p) + self.assertTrue(ltest[2] == Line(Point(1,1,0), Point(4,5,0))) + + def test_linegroup_update_min_max(self): + """ checks if the max & min x and y values are updated correctly """ + ltest = LineGroup() + ltest.append(l1) + ltest.append(l2) + l = Line(Point(10,20,0), Point(14,-16,0)) + ltest.append(l) + self.assertTrue(ltest.maxY == 20) + self.assertTrue(ltest.maxX == 14) + self.assertTrue(ltest.minY == -16) + self.assertTrue(ltest.minX == 1) + + def test_linegroup_line_add_coordlist(self): + """ checks if points from coordinate list added to linegroup """ + ltest = LineGroup() + ltest.append(l1) + k = [[3,4], [-6,7]] + ltest.addLinesFromCoordinateList(k) + self.assertTrue(ltest[1] == Line(Point(3,4,0), Point(-6,7,0))) + + def test_linegroup_transform(self): + """ checks if lines within group are transfromed correctly """ + ltest = LineGroup() + ltest.append(l1) + ltest.append(l2) + matr = [[0,0,0,1], [1,4,6,8], [0,6,7,9], [3,4,5,6]] + ltest = ltest.transform(matr) + self.assertTrue(ltest[0] == Line(Point(1,35,42),Point(1,62,75))) + self.assertTrue(ltest[1] == Line(Point(1,35,42), Point(1,170,207))) + +def main(): + unittest.main() + +if __name__ == '__main__': + main() diff --git a/unittest3_arc_matrix.py b/unittest3_arc_matrix.py new file mode 100644 index 0000000..1f838a3 --- /dev/null +++ b/unittest3_arc_matrix.py @@ -0,0 +1,76 @@ +# -*- coding: utf-8 -*- +""" +Created on Fri Jul 7 14:33:12 2017 + +@author: siddhant +""" + +import unittest +from point import Point +#from line import Line +#from linegroup import LineGroup +from arc import Arc +import matrixTrans as mt +import constants as c + +class ArcTestCase(unittest.TestCase): + + def test_arc_calc_inner_angle(self): + """ checks if inner angle between given points is correct """ + a = Arc(Point(1,1,0), Point(4,4,0), c.X, Point(1,3,0)) + ang = a.calcIncludedAngle(Point(1,1,0), Point(4,4,0)) + self.assertAlmostEqual(ang, 4.2426, 4) + + def test_arc_to_lines(self): + """ checks if line segments double when function called (arcToLines) """ + a = Arc(Point(1,1,0), Point(4,4,0), c.X, Point(1,3,0)) + len1 = len(a) + + a.arcToLines() + len2 = len(a) + + self.assertTrue(len2 == 2*len1) + +class MatrixTestCase(unittest.TestCase): + + def test_matrix_mirror(self): + """ checks if the matrix is mirrored correctly about given axis """ + self.assertTrue(tuple(mt.mirrorMatrix(c.X)[0]) == (1,0,0,0)) + self.assertTrue(tuple(mt.mirrorMatrix(c.X)[1]) == (0,-1,0,0)) + self.assertTrue(tuple(mt.mirrorMatrix(c.X)[2]) == (0,0,1,0)) + self.assertTrue(tuple(mt.mirrorMatrix(c.X)[3]) == (0,0,0,1)) + + def test_matrix_translate(self): + """ checks if the matrix is translated correctly """ + self.assertTrue(tuple(mt.translateMatrix(2,3)[0]) == (1,0,0,2)) + self.assertTrue(tuple(mt.translateMatrix(2,3)[1]) == (0,1,0,3)) + self.assertTrue(tuple(mt.translateMatrix(2,3)[2]) == (0,0,1,0)) + self.assertTrue(tuple(mt.translateMatrix(2,3)[3]) == (0,0,0,1)) + + def test_matrix_rotate(self): + """ checks if the matrix is rotated correctly """ + self.assertTrue(tuple(mt.rotateMatrix(0)[0]) == (1,0,0,0)) + self.assertTrue(tuple(mt.rotateMatrix(0)[1]) == (0,1,0,0)) + self.assertTrue(tuple(mt.rotateMatrix(0)[2]) == (0,0,1,0)) + self.assertTrue(tuple(mt.rotateMatrix(0)[3]) == (0,0,0,1)) + + def test_matrix_scale(self): + """ checks if the matrix is scaled accordingly """ + self.assertTrue(tuple(mt.scale(5,4)[0]) == (5,0,0,0)) + self.assertTrue(tuple(mt.scale(5,4)[1]) == (0,4,0,0)) + self.assertTrue(tuple(mt.scale(5,4)[2]) == (0,0,1,0)) + self.assertTrue(tuple(mt.scale(5,4)[3]) == (0,0,0,1)) + + def test_matrix_combine_transformations(self): + """ checks if dot product with matrix list is correct """ + ml = [[1,2,3,4],[5,6,7,8]] + self.assertTrue(mt.combineTransformations(ml) == 70) + +def main(): + unittest.main() + +if __name__ == '__main__': + main() + + + diff --git a/unittest4_outline.py b/unittest4_outline.py new file mode 100644 index 0000000..7d291aa --- /dev/null +++ b/unittest4_outline.py @@ -0,0 +1,145 @@ +# -*- coding: utf-8 -*- +""" +Created on Tue Jul 11 14:20:51 2017 + +@author: siddhant +""" + +import unittest +from point import Point +from line import Line +from linegroup import LineGroup +import constants as c +#import numpy as np +#import matrixTrans as mt +from outline import Outline +#from infill import Infill + +pt1 = Point(0.0, 0.0, 0.0) +pt2 = Point(6.0, 0.0, 0.0) +pt3 = Point(6.0, 6.0, 0.0) +pt4 = Point(0.0, 6.0, 0.0) + +ln1 = Line(pt1, pt2) +ln2 = Line(pt2, pt3) +ln3 = Line(pt3, pt4) +ln4 = Line(pt4, pt1) + +pt5 = Point(2.0, 2.0, 0.0) +pt6 = Point(2.0, 4.0, 0.0) +pt7 = Point(4.0, 4.0, 0.0) +pt8 = Point(2.0, 4.0, 0.0) + +ln5 = Line(pt5, pt6) +ln6 = Line(pt6, pt7) +ln7 = Line(pt7, pt8) +ln8 = Line(pt8, pt5) + +pt9 = Point(8.0, 0.0, 0.0) +pt10 = Point(8.0, 6.0, 0.0) + +ln9 = Line(pt1, pt9) +ln10 = Line(pt9, pt10) +ln11 = Line(pt10, pt4) +ln12 = Line(pt10, pt1) + +class OutlineTestCase(unittest.TestCase): + + def test_outline_add_linegroup(self): + """ checks if linegroup correctly added to outline (addLineGroup) """ + o = Outline() + lg = LineGroup() + lg.append(ln1) + o.addLineGroup(lg) + self.assertTrue(o[0] == ln1) + + def test_outline_is_inside(self): + """ checks if the given point is contained within the outline """ + o = Outline() + o.append(ln1) + o.append(ln2) + o.append(ln3) + o.append(ln4) + p_in = Point(2,2) + p_out = Point(8,6,0) + self.assertTrue(o.isInside(p_in) == 1) + self.assertFalse(o.isInside(p_out) == 1) + + def test_outline_offset(self): + """ checks if outline is offset correctly in given direction """ + o = Outline() + o.append(ln1) + o.append(ln2) + o.append(ln3) + o.append(ln4) + o1 = o.offset(2, c.OUTSIDE) + self.assertTrue(o1[0] == + Line(Point(-2.0,0.0,0.0), Point(-2.0, 6.0, 0.0))) + + def test_outline_do_intersect(self): + """ checks if two given outlines intersect (doOutlinesIntersect) """ + o = Outline() + o.append(ln1) + o.append(ln2) + o.append(ln3) + o.append(ln4) + + o1 = Outline() + o1.append(ln5) + o1.append(ln6) + o1.append(ln7) + o1.append(ln8) + + o2 = Outline() + o2.append(ln9) + o2.append(ln10) + o2.append(ln11) + o2.append(ln12) + + self.assertTrue(o.doOutlinesIntersect(o1) == False) + self.assertTrue(o.doOutlinesIntersect(o2) == True) + + def test_outline_add_internal_shape(self): + """ checks if given shape is added to outline (addInternalShape) """ + o = Outline() + o.append(ln1) + o.append(ln2) + o.append(ln3) + o.append(ln4) + + o1 = Outline() + o1.append(ln5) + o1.append(ln6) + o1.append(ln7) + o1.append(ln8) + #print(o1) + + o.addInternalShape(o1) + #print(o) + #print(o1) + self.assertTrue(o[4] == ln8) + #adds lines in strange order + + def test_outline_close_shape(self): + """ checks if the given outline is closed (closeShape) """ + o = Outline() + o.append(ln1) + o.append(ln2) + o.closeShape() + self.assertTrue(o[2] == Line(Point(6.0,6.0,0.0), Point(0.0,0.0,0.0))) + + def test_outline_finish(self): + """ checks if the given outline is finished (finishOutline) """ + o = Outline() + o.append(ln1) + o.append(ln2) + o.append(ln3) + o.append(ln4) + self.assertTrue(o.finishOutline() == None) + +def main(): + unittest.main() + +if __name__ == '__main__': + main() + diff --git a/unittest5_infill.py b/unittest5_infill.py new file mode 100644 index 0000000..4f0673a --- /dev/null +++ b/unittest5_infill.py @@ -0,0 +1,79 @@ +# -*- coding: utf-8 -*- +""" +Created on Mon Jul 24 15:35:34 2017 + +@author: siddhant +""" + +import unittest +from point import Point +from line import Line +from outline import Outline +from infill import Infill + +class InfillTestCase(unittest.TestCase): + + def test_infill_extend_design(self): + + outline1 = Outline() + outline1.append(Line(Point(0,0), Point(4,0))) + outline1.append(Line(Point(4,0), Point(4,4))) + outline1.append(Line(Point(4,4), Point(0,4))) + outline1.append(Line(Point(0,4), Point(0,0))) + + d = [Line(Point(0,0), Point(2,0))] + + i1 = Infill(outline1, 0.5, 0, design = d, testMode = True) + #print(i1.design) + i1.extendDesign() + #print(i1.design[3]) + self.assertTrue(i1.design[1] == Line(Point(2,0), Point(4,0))) + + def test_infill_create_design(self): + """ checks if resulting infill design has expected modified path width """ + outline1 = Outline() + outline1.append(Line(Point(0,0), Point(4,0))) + outline1.append(Line(Point(4,0), Point(4,4))) + outline1.append(Line(Point(4,4), Point(0,4))) + outline1.append(Line(Point(0,4), Point(0,0))) + + d = [Line(Point(0,0), Point(2,0))] + + i1 = Infill(outline1, 0.5, 0, design = d, testMode = True) + i1.createField() + self.assertTrue(i1.design[1] == Line(Point(0,0.5), Point(2,0.5))) + + def test_infill_centre_and_rotate(self): + + outline1 = Outline() + outline1.append(Line(Point(0,0), Point(4,0))) + outline1.append(Line(Point(4,0), Point(4,4))) + outline1.append(Line(Point(4,4), Point(0,4))) + outline1.append(Line(Point(0,4), Point(0,0))) + + d = [Line(Point(0,0), Point(2,0))] + + i1 = Infill(outline1, 0.5, 0, design = d, testMode = True) + i1.centerAndRotateField() + self.assertTrue(i1.design[0] == Line(Point(1,2), Point(3,2))) + + def test_infill_trim(self): + + outline1 = Outline() + outline1.append(Line(Point(0,0), Point(4,0))) + outline1.append(Line(Point(4,0), Point(4,4))) + outline1.append(Line(Point(4,4), Point(0,4))) + outline1.append(Line(Point(0,4), Point(0,0))) + + d = [Line(Point(0,0), Point(2,0))] + + i1 = Infill(outline1, 0.5, 0, design = d, testMode = True) + i1.trimField() + self.assertTrue(i1.design[0] == d[0]) + +def main(): + unittest.main() + +if __name__ == '__main__': + main() +