Exemple #1
0
 def test_closest_distance_2(self):
     l1 = svgcuts.Line(svgcuts.Point(1, 2), svgcuts.Point(1, 3))
     l2 = svgcuts.Line(svgcuts.Point(3, 3.5), svgcuts.Point(5, 1.5))
     self.assertAlmostEqual(l1.closest_distance(l2),
                            math.sqrt(4 + math.pow(0.5, 2)))
     self.assertAlmostEqual(l2.closest_distance(l1),
                            math.sqrt(4 + math.pow(0.5, 2)))
Exemple #2
0
 def test_crosses3(self):
     p1 = svgcuts.Point(0, 0)
     p2 = svgcuts.Point(2, 3)
     p3 = svgcuts.Point(1, 1)
     p4 = svgcuts.Point(3, 4)
     l1 = svgcuts.Line(p1, p2)
     l2 = svgcuts.Line(p3, p4)
     self.assertFalse(l1.intersects(l2))
     self.assertFalse(l2.intersects(l1))
Exemple #3
0
 def test_crosses2(self):
     p1 = svgcuts.Point(1, 0)
     p2 = svgcuts.Point(1, 2)
     p3 = svgcuts.Point(0, 1)
     p4 = svgcuts.Point(2, 1)
     l1 = svgcuts.Line(p1, p2)
     l2 = svgcuts.Line(p3, p4)
     self.assertTrue(l1.intersects(l2))
     self.assertTrue(l2.intersects(l1))
Exemple #4
0
 def test_aligned_hor_nonintersect(self):
     p1 = svgcuts.Point(0, 0)
     p2 = svgcuts.Point(1, 0)
     p3 = svgcuts.Point(2, 0)
     p4 = svgcuts.Point(3, 0)
     l1 = svgcuts.Line(p1, p2)
     l2 = svgcuts.Line(p3, p4)
     self.assertFalse(l1.intersects(l2))
     self.assertFalse(l2.intersects(l1))
Exemple #5
0
 def test_slope_offset(self):
     s, i = svgcuts.Line(svgcuts.Point(1, 1), svgcuts.Point(2,
                                                            3)).slope_offset
     self.assertAlmostEqual(s, 2.0)
     self.assertAlmostEqual(i, -1.0)
     s, i = svgcuts.Line(svgcuts.Point(1, 1), svgcuts.Point(3,
                                                            4)).slope_offset
     self.assertAlmostEqual(s, 1.5)
     self.assertAlmostEqual(i, -0.5)
Exemple #6
0
 def test_aligned_vert_nonintersect(self):
     p1 = svgcuts.Point(0, 0)
     p2 = svgcuts.Point(0, 1)
     p3 = svgcuts.Point(0, 2)
     p4 = svgcuts.Point(0, 3)
     l1 = svgcuts.Line(p1, p2)
     l2 = svgcuts.Line(p3, p4)
     self.assertFalse(l1.intersects(l2))
     self.assertFalse(l2.intersects(l1))
 def project_inner(self, l, o):
     # TODO determine mechanic to use for edge hiding; add a trait/property to triangles for visibility of the edges (not just a bool)
     if isinstance(o, list):
         for o_ in o:
             self.project_inner(l, o_)
     elif isinstance(o, stl.Solid):
         for t in o.triangles:
             self.project_inner(l, t)
     elif isinstance(o, stl.Triangle):
         p1 = self.project_point(o.p1)
         p2 = self.project_point(o.p2)
         p3 = self.project_point(o.p3)
         l.add_line(svgcuts.Line(p1, p2, unit='in'))
         l.add_line(svgcuts.Line(p1, p3, unit='in'))
         l.add_line(svgcuts.Line(p2, p3, unit='in'))
Exemple #8
0
    def test_crosses_intersection_point2(self):
        p1 = svgcuts.Point(0, 0)
        p2 = svgcuts.Point(3, 3)
        p3 = svgcuts.Point(1, 0)
        p4 = svgcuts.Point(1, 3)
        l1 = svgcuts.Line(p1, p2)
        l2 = svgcuts.Line(p3, p4)

        for d in [False, True]:
            if d:
                l1, l2 = l2, l1

            pinter = l1.intersects(l2, return_intersection_point=True)
            self.assertTrue(bool(pinter))
            self.assertAlmostEqual(pinter.x, 1.0)
            self.assertAlmostEqual(pinter.y, 1.0)
Exemple #9
0
    def test_slices_layer(self):
        p1 = svgcuts.Point(0, 0)
        p2 = svgcuts.Point(3, 3)
        p3 = svgcuts.Point(1, 0)
        p4 = svgcuts.Point(1, 3)
        l1 = svgcuts.Line(p1, p2)
        l2 = svgcuts.Line(p3, p4)

        layer = svgcuts.Layer(3, 3, unit='in')
        layer.add_line(l1)
        layer.add_line(l2)

        self.assertEquals(len(layer.lines), 2)

        new_layer = layer.slice_lines()

        self.assertEquals(len(new_layer.lines), 4)
Exemple #10
0
 def test_basic_render(self):
     l = svgcuts.Layer(4, 6, unit='in')
     l.add_line(svgcuts.Line(svgcuts.Point(1, 1), svgcuts.Point(2, 2)))
     svg = l.render()
     #print svg
     self.assertTrue('height="6.000000in"' in svg)
     for d in ['x', 'y']:
         for n in [1, 2]:
             self.assertTrue('%s%d=\'%d.00' % (d, n, n) in svg)
     self.assertTrue('<line' in svg)
Exemple #11
0
 def test_incidental_angle_without_common_points(self):
     l1 = svgcuts.Line(svgcuts.Point(0, 2), svgcuts.Point(2, 4))
     l2 = svgcuts.Line(svgcuts.Point(4, 5), svgcuts.Point(5, 5))
     l3a = svgcuts.Line(svgcuts.Point(3, 3.999), svgcuts.Point(4, 3.999))
     l3b = svgcuts.Line(svgcuts.Point(3, 4), svgcuts.Point(4, 4))
     l3c = svgcuts.Line(svgcuts.Point(3, 4.001), svgcuts.Point(4, 4.001))
     l4 = svgcuts.Line(svgcuts.Point(3, 3), svgcuts.Point(4, 3))
     l5 = svgcuts.Line(svgcuts.Point(1, 2), svgcuts.Point(3, 4))
     self.assertAlmostEqual(l1.incident_angle(l2), math.pi * 3.0 / 4.0)
     # TODO add ones for l3*
     self.assertAlmostEqual(l1.incident_angle(l4), math.pi * 1.0 / 4.0)
     # incident angle should be pi, even if they never touch..?
     self.assertAlmostEqual(l1.incident_angle(l5), math.pi)
Exemple #12
0
    def test_incidental_angle_with_common_points(self):
        l1 = svgcuts.Line(svgcuts.Point(1, 1), svgcuts.Point(2, 2))
        l2 = svgcuts.Line(svgcuts.Point(1, 1), svgcuts.Point(0, 2))
        self.assertAlmostEqual(l1.incident_angle(l2), math.pi / 2.0)

        l1 = svgcuts.Line(svgcuts.Point(1, 1), svgcuts.Point(2, 2))
        self.assertAlmostEqual(l1.incident_angle(l1), 0.0)

        l1 = svgcuts.Line(svgcuts.Point(2, 2), svgcuts.Point(1, 1))
        l2 = svgcuts.Line(svgcuts.Point(2, 2), svgcuts.Point(3, 2))
        self.assertAlmostEqual(l1.incident_angle(l2), math.pi * 3.0 / 4.0)
        l1 = svgcuts.Line(svgcuts.Point(1, 1), svgcuts.Point(2, 2))
        self.assertAlmostEqual(l1.incident_angle(l2), math.pi * 3.0 / 4.0)
    def project(self, o):
        # TODO don't default the size and units of the layer.
        layer = svgcuts.Layer(8.0, 6.0, unit="in")

        self.project_inner(layer, o)

        # TODO move this to example? or parameterize it somehow. Either way, this is important for getting your bearings on how the
        # coordinate system is laid out.

        if False:
            n = 2
            r = 0.1
            m = .8
            for x in range(-n, n + 1):
                for y in range(-n, n + 1):
                    for z in range(-n, n + 1):
                        if x == 0 and y == 0 and z == 0:
                            continue

                        c = r * m
                        _c = math.pow(
                            math.pow(x, 2.0) + math.pow(y, 2.0) +
                            math.pow(z, 2.0), 0.5)
                        xm = x / _c * c
                        ym = y / _c * c
                        zm = z / _c * c
                        p1 = stl.Point(x * m, y * m, z * m)
                        p2 = stl.Point(x * m + xm, y * m + ym, z * m + zm)

                        p1 = self.project_point(p1)
                        p2 = self.project_point(p2)

                        # axes colors
                        kw = {}
                        if y == 0 and z == 0:
                            kw['color'] = 'red'
                        elif x == 0 and z == 0:
                            kw['color'] = 'green'
                        elif x == 0 and y == 0:
                            kw['color'] = 'blue'

                        layer.add_line(svgcuts.Line(p1, p2, **kw))
        return layer
Exemple #14
0
 def test_angle(self):
     self.assertAlmostEqual(
         svgcuts.Line(svgcuts.Point(0, 0), svgcuts.Point(1, 1)).angle,
         math.pi / 4.0)
     self.assertAlmostEqual(
         svgcuts.Line(svgcuts.Point(0, 0), svgcuts.Point(0, 1)).angle,
         math.pi / 2.0)
     self.assertAlmostEqual(
         svgcuts.Line(svgcuts.Point(0, 0), svgcuts.Point(0, -1)).angle,
         math.pi / -2.0)
     self.assertAlmostEqual(
         svgcuts.Line(svgcuts.Point(0, 0.5), svgcuts.Point(1, 1)).angle,
         0.46364760900080609)
     self.assertAlmostEqual(
         svgcuts.Line(svgcuts.Point(0, 0), svgcuts.Point(-1, 1)).angle,
         math.pi * 3.0 / 4.0)
     self.assertAlmostEqual(
         svgcuts.Line(svgcuts.Point(0, 0), svgcuts.Point(-1, -1)).angle,
         math.pi * -3.0 / 4.0)
     self.assertAlmostEqual(
         svgcuts.Line(svgcuts.Point(0, 0), svgcuts.Point(1, 0)).angle, 0.0)
     self.assertAlmostEqual(
         svgcuts.Line(svgcuts.Point(0, 0), svgcuts.Point(1, -1)).angle,
         math.pi * -1.0 / 4.0)
Exemple #15
0
 def test_linelen(self):
     p1 = svgcuts.Point(0, 0)
     p2 = svgcuts.Point(2, 3)
     l = svgcuts.Line(p1, p2)
     self.assertAlmostEqual(l.length, 3.60555127546398929312)
Exemple #16
0
 def test_closest_distance_4(self):
     l1 = svgcuts.Line(svgcuts.Point(0, 1), svgcuts.Point(1, 2))
     l2 = svgcuts.Line(svgcuts.Point(1, 4), svgcuts.Point(3, 2))
     self.assertAlmostEqual(l2.closest_distance(l1), math.sqrt(2))
Exemple #17
0
 def test_closest_distance_1(self):
     l1 = svgcuts.Line(svgcuts.Point(0, 2), svgcuts.Point(2, 4))
     l2 = svgcuts.Line(svgcuts.Point(4, 5), svgcuts.Point(5, 5))
     self.assertAlmostEqual(l1.closest_distance(l2), math.sqrt(5))
     self.assertAlmostEqual(l2.closest_distance(l1), math.sqrt(5))
Exemple #18
0
 def test_closest_distance_3(self):
     l1 = svgcuts.Line(svgcuts.Point(1, 1), svgcuts.Point(1, 3))
     l2 = svgcuts.Line(svgcuts.Point(4, 3), svgcuts.Point(2, 1.5))
     self.assertAlmostEqual(l1.closest_distance(l2), 1.0)
     self.assertAlmostEqual(l2.closest_distance(l1), 1.0)
Exemple #19
0
b = svgcuts.Layer(24, 12, unit=unit)

# make pattern

xd = pad * 2 + edge * cols
yd = pad * 2 + edge * rows

ps = [
    svgcuts.Point(pad, pad),
    svgcuts.Point(pad, pad + yd),
    svgcuts.Point(pad + xd, pad + yd),
    svgcuts.Point(pad + xd, pad)
]

for n in range(4):
    pattern.add_line(svgcuts.Line(ps[n], ps[(n + 1) % 4], unit=unit))

# make squares

# position an identity corner (upper left corner of a square)


def xy2p(x, y, xadg=0.0, yadg=0.0):
    _x = pad + x * edge + xadg
    _y = pad + y * edge + yadg
    return svgcuts.Point(_x, _y)


for x in range(cols):
    for y in range(rows):
        if y == 0: