Esempio n. 1
0
 def __init__(self, start, end, start_tangent, end_tangent, segments):
     self.start = Vector(start)
     self.end = Vector(end)
     self.start_tangent = Vector(
         start_tangent)  # as vector, from start point
     self.end_tangent = Vector(end_tangent)  # as vector, from end point
     self.segments = segments
Esempio n. 2
0
    def __init__(self,
                 pos,
                 center,
                 start,
                 end,
                 dimstyle='angle.deg',
                 layer=None,
                 roundval=None):
        """
        AngularDimension constructor.

        Args:
            pos: location as (x, y) tuple of dimension line, line goes through this point
            center: center point as (x, y) tuple of angle
            start: line from center to start is the first side of the angle
            end: line from center to end is the second side of the angle
            dimstyle: dimstyle name, 'Default' - style is the default value
            layer: dimension line layer, override the default value of dimstyle
            roundval: count of decimal places

        """
        super(AngularDimension, self).__init__(dimstyle, layer, roundval)
        self.dimlinepos = Vector(pos)
        self.center = Vector(center)
        self.start = Vector(start)
        self.end = Vector(end)
Esempio n. 3
0
def test_is_null():
    v = Vector()
    assert v.is_null

    v1 = Vector(23.56678, 56678.56778, 2.56677) * (1.0 / 14.5667)
    v2 = Vector(23.56678, 56678.56778, 2.56677) / 14.5667
    result = v2 - v1
    assert Vector(0, 0, 0).is_null
Esempio n. 4
0
 def transform(self, points):
     rotation = radians(self.rotation)
     for point in points:
         if self.mirrorx:
             point = Vector(point[0], -point[1])
         if self.mirrory:
             point = Vector(-point[0], point[1])
         yield self.start + point.rot_z_rad(rotation)
Esempio n. 5
0
def test_strtag2_vector():
    assert ' 10\n1.0\n 20\n2.0\n 30\n3.0\n' == strtag2(
        DXFTag(10, Vector(1, 2, 3)))
    assert ' 10\n1.0\n 20\n2.0\n 30\n3.0\n' == strtag2(
        DXFTag(10, Vector((1, 2, 3))))
    assert ' 10\n1.0\n 20\n2.0\n 30\n0.0\n' == strtag2(DXFTag(
        10, Vector(1, 2)))
    assert ' 10\n1.0\n 20\n2.0\n 30\n0.0\n' == strtag2(
        DXFTag(10, Vector((1, 2))))
Esempio n. 6
0
def test_bool():
    v = Vector()
    assert bool(v) is False

    v1 = Vector(23.56678, 56678.56778, 2.56677) * (1.0 / 14.5667)
    v2 = Vector(23.56678, 56678.56778, 2.56677) / 14.5667
    result = v2 - v1
    assert bool(result) is False
    # actual precision is abs_tol=1e-9
    assert not Vector(1e-8, 0, 0).is_null
Esempio n. 7
0
def test_strtag2_vector():
    assert ' 10\n1.0\n 20\n2.0\n 30\n3.0\n' == DXFVertex(10,
                                                         Vector(1, 2,
                                                                3)).dxfstr()
    assert ' 10\n1.0\n 20\n2.0\n 30\n3.0\n' == DXFVertex(
        10, Vector((1, 2, 3))).dxfstr()
    assert ' 10\n1.0\n 20\n2.0\n 30\n0.0\n' == DXFVertex(10,
                                                         Vector(1,
                                                                2)).dxfstr()
    assert ' 10\n1.0\n 20\n2.0\n 30\n0.0\n' == DXFVertex(10, Vector(
        (1, 2))).dxfstr()
Esempio n. 8
0
    def scale(self, sx=1, sy=1, sz=1):
        """
        Scale mesh inplace.

        """
        self.vertices = [
            Vector(v[0] * sx, v[1] * sy, v[2] * sz) for v in self.vertices
        ]
        for index, vertex in enumerate(self.vertices):
            self.vertices[index] = Vector(vertex[0] * sx, vertex[1] * sy,
                                          vertex[2] * sz)
Esempio n. 9
0
    def translate(self, x=0, y=0, z=0):
        """
        Translate mesh inplace.

        """
        if isinstance(x, (float, int)):
            t = Vector(x, y, z)
        else:
            t = Vector(x)
        for index, vertex in enumerate(self.vertices):
            self.vertices[index] = t + vertex
Esempio n. 10
0
def test_compare_vectors():
    v1 = Vector(1, 2, 3)

    # compare to tuple
    assert v1 == (1, 2, 3)
    # compare tuple to vector
    assert (1, 2, 3) == v1

    v2 = Vector(2, 3, 4)
    assert v2 > v1
    assert v1 < v2
Esempio n. 11
0
    def point(self, t):
        """
        Returns (point, derivative1, derivative2) at BezierCurve(t)

        Args:
            t: parameter in range [0, 1]

        Returns: (point, derivative1, derivative2)
            point -- Vector(x, y, z)
            derivative1 -- Vector(x, y, z)
            derivative2 -- Vector(x, y, z)

        """
        if t < 0. or t > 1.:
            raise ValueError('parameter t in range [0, 1]')

        if (1.0 - t) < 5e-6:
            t = 1.0
        defpoints = self._defpoints
        npts = len(defpoints)
        npts0 = npts - 1
        point = [0., 0., 0.]
        d1 = [0., 0., 0.]
        d2 = [0., 0., 0.]
        for axis in (0, 1, 2):
            if t == 0.0:
                d1[axis] = npts0 * (defpoints[1][axis] - defpoints[0][axis])
                d2[axis] = npts0 * (npts0 - 1) * (defpoints[0][axis] -
                                                  2. * defpoints[1][axis] +
                                                  defpoints[2][axis])
            for i in range(len(defpoints)):
                tempbasis = bernstein_basis(npts0, i, t)
                point[axis] += tempbasis * defpoints[i][axis]
                if 0.0 < t < 1.0:
                    d1[axis] += ((i - npts0 * t) /
                                 (t *
                                  (1. - t))) * tempbasis * defpoints[i][axis]
                    temp1 = (i - npts0 * t)**2
                    temp2 = temp1 - npts0 * t**2 - i * (1. - 2. * t)
                    d2[axis] += (
                        temp2 / (t**2 *
                                 (1. - t)**2)) * tempbasis * defpoints[i][axis]
                if t == 1.0:
                    d1[axis] = npts0 * (defpoints[npts0][axis] -
                                        defpoints[npts0 - 1][axis])
                    d2[axis] = npts0 * (npts0 -
                                        1) * (defpoints[npts0][axis] -
                                              2 * defpoints[npts0 - 1][axis] +
                                              defpoints[npts0 - 2][axis])
        return Vector(point), Vector(d1), Vector(d2)
Esempio n. 12
0
def test_init_two_params():
    v = Vector(1, 2)
    assert v == (1, 2)  # z is 0.

    v = Vector(5, 6, 7) - Vector(1, 1, 1)
    assert v == (4, 5, 6)

    v = Vector.from_deg_angle(0)
    assert v == (1, 0)

    length, angle = 7, 45
    v = Vector.from_deg_angle(angle, length)
    x = math.cos(math.radians(angle)) * length
    y = math.sin(math.radians(angle)) * length
    assert v == (x, y)
Esempio n. 13
0
 def __init__(self, start=(0, 0), rotation=0., length=1., paramA=1.0, mirror=''):
     self.start = Vector(start)
     self.rotation = float(rotation)
     self.length = float(length)
     self.paramA = float(paramA)
     self.mirrorx = 'x' in mirror.lower()
     self.mirrory = 'y' in mirror.lower()
Esempio n. 14
0
 def set_location(self, insert, rotation=None, attachment_point=None):
     self.dxf.insert = Vector(insert)
     if rotation is not None:
         self.set_rotation(rotation)
     if attachment_point is not None:
         self.dxf.attachment_point = attachment_point
     return self
Esempio n. 15
0
    def __init__(self,
                 pos,
                 measure_points,
                 angle=0.,
                 dimstyle='Default',
                 layer=None,
                 roundval=None):
        """
        LinearDimension Constructor.

        Args:
            pos: location as (x, y) tuple of dimension line, line goes through this point
            measure_points: list of points as (x, y) tuples to dimension (two or more)
            float angle: angle (in degree) of dimension line
            dimstyle: dimstyle name, 'Default' - style is the default value
            str layer: dimension line layer, override the default value of dimstyle
            int roundval: count of decimal places

        """
        super(LinearDimension, self).__init__(dimstyle, layer, roundval)
        self.angle = angle
        self.measure_points = measure_points
        self.text_override = [""] * self.section_count
        self.dimlinepos = Vector(pos)
        self.layout = None
Esempio n. 16
0
 def __init__(self, center=(0., 0., 0.), rx=1.0, ry=1.0, startangle=0., endangle=360., rotation=0., segments=100):
     self.center = Vector(center)
     self.rx = float(rx)
     self.ry = float(ry)
     self.startangle = float(startangle)
     self.endangle = float(endangle)
     self.rotation = float(rotation)
     self.segments = int(segments)
Esempio n. 17
0
    def append(self, point, tangent1, tangent2=None, segments=20):
        """
        Append a control point with two control tangents.

        Args:
            point: the control point
            tangent1: first control tangent as vector *left* of point
            tangent2: second control tangent as vector *right* of point, if omitted tangent2 = -tangent1
            segments: count of line segments for polyline approximation, count of line segments from previous
            control point to this point.

        """
        tangent1 = Vector(tangent1)
        if tangent2 is None:
            tangent2 = -tangent1
        else:
            tangent2 = Vector(tangent2)
        self.points.append((point, tangent1, tangent2, int(segments)))
Esempio n. 18
0
def test_vector_as_tuple():
    v = Vector(1, 2, 3)
    assert v[0] == 1
    assert v[1] == 2
    assert v[2] == 3
    assert tuple(v) == (1, 2, 3)

    assert isinstance(v[:2], tuple)
    assert v[:2] == (1, 2)
    assert v[1:] == (2, 3)
    assert isinstance(v.xyz, tuple)
    assert v.xyz == (1, 2, 3)
Esempio n. 19
0
 def _setup(self):
     """
     Calc setup values and determines the point order of the dimension line points.
     """
     self.measure_points = [Vector(point) for point in self.measure_points]
     dimlineray = Ray2D(self.dimlinepos, angle=radians(self.angle))
     self.dimline_points = [
         self._get_point_on_dimline(point, dimlineray)
         for point in self.measure_points
     ]
     self.point_order = self._indices_of_sorted_points(self.dimline_points)
     self._build_vectors()
Esempio n. 20
0
    def __init__(self,
                 center,
                 target,
                 length=1.,
                 dimstyle='Default',
                 layer=None,
                 roundval=None):
        """
        Args:
            center: center point of radius
            target: target point of radius
            length: length of radius arrow (drawing length)
            dimstyle: dimstyle name, 'Default' - style is the default value
            layer: dimension line layer, override the default value of dimstyle
            roundval: count of decimal places

        """
        super(RadialDimension, self).__init__(dimstyle, layer, roundval)
        self.center = Vector(center)
        self.target = Vector(target)
        self.length = float(length)
Esempio n. 21
0
def test_angle_between():
    v1 = Vector(0, 1)
    v2 = Vector(1, 1)
    angle = v1.angle_between(v2)
    assert is_close(angle, math.pi / 4)
    # reverse order, same result
    angle = v2.angle_between(v1)
    assert is_close(angle, math.pi / 4)
Esempio n. 22
0
def test_deep_copy():
    import copy

    v = Vector(1, 2, 3)
    l1 = [v, v, v]
    l2 = copy.copy(l1)
    assert l2[0] is l2[1]
    assert l2[1] is l2[2]
    assert l2[0] is v

    l3 = copy.deepcopy(l1)
    assert l3[0] is l3[1]
    assert l3[1] is l3[2]
    assert l3[0] is not v
Esempio n. 23
0
    def point(self, t):
        """
        Returns point at BezierCurve(t) as tuple (x, y, z)

        Args:
            t: parameter in range [0, 1]

        Returns: Vector(x, y, z)

        """
        if t < 0. or t > 1.:
            raise ValueError('parameter t in range [0, 1]')
        if (1.0 - t) < 5e-6:
            t = 1.0
        point = [0., 0., 0.]
        defpoints = self._defpoints
        len_defpoints = len(defpoints)

        for axis in (0, 1, 2):
            for i in range(len_defpoints):
                bsf = bernstein_basis(len_defpoints - 1, i, t)
                point[axis] += bsf * defpoints[i][axis]
        return Vector(point)
Esempio n. 24
0
def test_init_three_params():
    v = Vector(1, 2, 3)
    assert v == (1, 2, 3)
Esempio n. 25
0
def test_spatial_angle():
    v = Vector(3, 3, 0)
    assert is_close(v.spatial_angle_deg, 45)
    assert is_close(v.spatial_angle_rad, math.radians(45))
Esempio n. 26
0
def test_get_angle():
    v = Vector(3, 3)
    assert is_close(v.angle_deg, 45)
    assert is_close(v.angle_rad, math.radians(45))
Esempio n. 27
0
def test_init_no_params():
    v = Vector()
    assert v == (0, 0, 0)
    assert v == Vector()
Esempio n. 28
0
def test_iter():
    assert sum(Vector(1, 2, 3)) == 6
Esempio n. 29
0
def test_vector_as_tuple():
    v = Vector(1, 2, 3)
    assert v[0] == 1
    assert v[1] == 2
    assert v[2] == 3
    assert tuple(v) == (1, 2, 3)
Esempio n. 30
0
def test_from_angle():
    angle = math.radians(50)
    length = 3.
    assert Vector.from_rad_angle(angle,
                                 length) == (math.cos(angle) * length,
                                             math.sin(angle) * length, 0)