コード例 #1
0
 def __init__(self,
              axis=Vector([1, 0, 0]),
              pos=Vector([0, 0, 0]),
              width=1.0,
              height=1.0,
              length=1.0,
              color=Rgb(),
              other=None):
     """
     :param color: The object's color.
     :type color: pyglet_helper.util.Rgb
     :param pos: The object's position.
     :type pos: pyglet_helper.util.Vector
     :param width: The object's width.
     :type width: float
     :param height: The object's height.
     :type height: float
     :param length: The object's length.
     :type length: float
     """
     super(Rectangular, self).__init__(color=color, pos=pos, axis=axis)
     self._height = None
     self._width = None
     if other is None:
         self.width = width
         self.height = height
     else:
         self.width = other.width
         self.height = other.height
     self.length = length
コード例 #2
0
ファイル: primitive.py プロジェクト: encukou/pyglet_helper
    def __init__(self, axis=Vector([1, 0, 0]), up_vector=Vector([0, 1, 0]),
                 pos=Vector([0, 0, 0]), obj_initialized=False, color=Rgb(),
                 material=Material()):
        """

        :param axis: The orientation to use when drawing.
        :type axis: pyglet_helper.util.Vector
        :param up_vector: A vector that points to the current up direction in
        the view.
        :type up_vector: pyglet_helper.util.Vector
        :param pos: The object's position.
        :type pos: pyglet_helper.util.Vector
        :param obj_initialized: If True, the object has been initialized
        :type obj_initialized: bool
        :param color: The object's color.
        :type color: pyglet_helper.util.Rgb
        :param material: The object's material
        :type material: pyglet_helper.util.Material
        """
        super(Primitive, self).__init__(color=color, mat=material)
        self._axis = None
        self._pos = None
        self._up = None
        self._width = None
        self._height = None

        self.startup = True
        self.obj_initialized = obj_initialized

        # position must be defined first, before the axis
        self.up_vector = Vector(up_vector)
        self.pos = Vector(pos)
        self.axis = Vector(axis)
コード例 #3
0
ファイル: test_linear.py プロジェクト: encukou/pyglet_helper
def test_vector_set_mag():
    from pyglet_helper.util import Vector
    vec1 = Vector([1.0, 0, 2.0])
    vec1.set_mag(0.1)
    print(vec1)
    assert vec1[0] == 0.044721359549995794
    assert vec1[1] == 0
コード例 #4
0
 def __init__(self,
              color=Rgb(),
              pos=Vector([0, 0, 0]),
              axis=Vector([1.0, 0.0, 0.0]),
              radius=1.0,
              material=Material(),
              other=None):
     """
     :param radius: The sphere's radius.
     :type radius: float
     :param color: The object's color.
     :type color: pyglet_helper.util.Rgb
     :param pos: The object's position.
     :type pos: pyglet_helper.util.Vector
     :param axis: The cone points from the base to the point along the axis.
     :type axis: pyglet_helper.util.Vector
     :param material: The object's material
     :type material: pyglet_helper.util.Material
     :param other: another sphere object to copy properties from (optional)
     :type other: pyglet_helper.objects.Sphere
     """
     super(Sphere, self).__init__(color=color,
                                  pos=pos,
                                  radius=radius,
                                  material=material,
                                  axis=axis)
     # Construct a unit sphere at the origin.
     if other is not None:
         self.axial = other
     self.compiled = False
コード例 #5
0
    def __init__(self,
                 height=1.0,
                 width=1.0,
                 length=1.0,
                 color=Rgb(),
                 pos=Vector([0, 0, 0]),
                 axis=Vector([1.0, 0.0, 0.0])):
        """

        :param width: The ellipsoid's width.
        :type width: float
        :param height: The ellipsoid's height.
        :type height: float
        :param length: The ellipsoid's length.
        :type length: float
        :param color: The object's color.
        :type color: pyglet_helper.util.Rgb
        :param pos: The object's position.
        :type pos: pyglet_helper.util.Vector
        """
        super(Ellipsoid, self).__init__(color=color, pos=pos, axis=axis)
        self._height = None
        self._width = None
        self.height = height
        self.width = width
        self.length = length
コード例 #6
0
def test_arrow_center():
    from pyglet_helper.objects import Arrow
    from pyglet_helper.util import Vector
    blo = Arrow(pos=Vector([-10, -10, -10]), axis=Vector([10, 10, 10]))
    assert (blo.center[0] == 0)
    assert (blo.center[1] == 0)
    assert (blo.center[2] == 0)
コード例 #7
0
def test_vector_clear():
    from pyglet_helper.util import Vector
    vec1 = Vector([1.0, 0, 2.0])
    vec1.clear()
    assert vec1[0] == 0.0
    assert vec1[1] == 0.0
    assert vec1[2] == 0.0
コード例 #8
0
ファイル: test_linear.py プロジェクト: encukou/pyglet_helper
def test_vector_clear():
    from pyglet_helper.util import Vector
    vec1 = Vector([1.0, 0, 2.0])
    vec1.clear()
    assert vec1[0] == 0.0
    assert vec1[1] == 0.0
    assert vec1[2] == 0.0
コード例 #9
0
def test_vector_set_mag():
    from pyglet_helper.util import Vector
    vec1 = Vector([1.0, 0, 2.0])
    vec1.set_mag(0.1)
    print(vec1)
    assert vec1[0] == 0.044721359549995794
    assert vec1[1] == 0
コード例 #10
0
def test_pyramid_center():
    from pyglet_helper.objects import Pyramid
    from pyglet_helper.util import Vector
    blo = Pyramid(pos=Vector([10, 0, 0]), axis=Vector([-1, 0, 0]), height=30)
    assert (blo.center[0] == 0)
    assert (blo.center[1] == 0)
    assert (blo.center[2] == 0)
コード例 #11
0
ファイル: axial.py プロジェクト: radovankavicky/pyglet_helper
    def __init__(self,
                 axis=Vector([1, 0, 0]),
                 radius=1.0,
                 color=Rgb(),
                 pos=Vector([0, 0, 0]),
                 material=Material(),
                 other=None):
        """

        :param other: another axial object to copy properties from (optional)
        :type other: pyglet_helper.objects.Axial
        :param axis: The axis for the orientation of the object.
        :type axis: pyglet_helper.util.Vector
        :param radius: The object's radius.
        :type radius: float
        :param color: The object's color.
        :type color: pyglet_helper.util.Rgb
        :param pos: The object's position.
        :type pos: pyglet_helper.util.Vector
        :param material: The object's material
        :type material: pyglet_helper.util.Material
        """
        super(Axial, self).__init__(color=color,
                                    pos=pos,
                                    axis=axis,
                                    material=material)
        self._radius = None
        if other is not None:
            self.radius = other.radius
        else:
            self.radius = radius
コード例 #12
0
def test_pyramid_material_matrix():
    from pyglet_helper.objects import Pyramid
    from pyglet_helper.util import Vector
    blo = Pyramid(pos=Vector([10, 0, 0]), axis=Vector([-1, 0, 0]), height=40)
    assert (blo.material_matrix[0, 0] == 0.025)
    assert (blo.material_matrix[1, 1] == 0.025)
    assert (blo.material_matrix[2, 2] == 0.025)
    assert (blo.material_matrix[3, 3] == 1.0)
コード例 #13
0
ファイル: test_linear.py プロジェクト: encukou/pyglet_helper
def test_vector_scale():
    from pyglet_helper.util import Vector
    vec1 = Vector([1.0, 0, 2.0])
    vec2 = Vector([0, 1.0, 1.0])
    vec3 = vec1.scale(vec2)
    print(vec3)
    assert vec3[0] == 0
    assert vec3[2] == 2.0
コード例 #14
0
def test_vector_mult():
    from pyglet_helper.util import Vector
    vec1 = Vector([1.0, 0, 2.0])
    vec2 = Vector([0, 1.0, 0.5])
    vec3 = vec1 * vec2
    assert vec3[0] == 0
    assert vec3[1] == 0
    assert vec3[2] == 1.0
コード例 #15
0
def test_vector_scale():
    from pyglet_helper.util import Vector
    vec1 = Vector([1.0, 0, 2.0])
    vec2 = Vector([0, 1.0, 1.0])
    vec3 = vec1.scale(vec2)
    print(vec3)
    assert vec3[0] == 0
    assert vec3[2] == 2.0
コード例 #16
0
def test_cylinder_center():
    from pyglet_helper.objects import Cylinder
    from pyglet_helper.util import Vector
    blo = Cylinder(radius=0.0,
                   pos=Vector([0, -0.5, 0]),
                   axis=Vector([0, 1, 0]))
    assert (abs(blo.center[0]) < 1e-7)
    assert (blo.center[1] == 0)
    assert (blo.center[2] == 0)
コード例 #17
0
def test_vector_rotate():
    from pyglet_helper.util import Vector
    vec1 = Vector([1.0, 0, 2.0])
    vec2 = Vector([0, 1.0, 1.0])
    vec3 = vec1.rotate(angle=1.0, axis=vec2)
    assert abs(vec3[0] - 2.73032198493) < 0.01
    assert vec3[3] == 1.0
    vec3 = vec1.rotate(angle=1.0)
    assert vec3[2] == 3.0
    assert vec3[3] == 1.0
コード例 #18
0
ファイル: test_linear.py プロジェクト: encukou/pyglet_helper
def test_vector_rotate():
    from pyglet_helper.util import Vector
    vec1 = Vector([1.0, 0, 2.0])
    vec2 = Vector([0, 1.0, 1.0])
    vec3 = vec1.rotate(angle=1.0, axis=vec2)
    assert abs(vec3[0] - 2.73032198493) < 0.01
    assert vec3[3] == 1.0
    vec3 = vec1.rotate(angle=1.0)
    assert vec3[2] == 3.0
    assert vec3[3] == 1.0
コード例 #19
0
 def material_matrix(self):
     """
     Creates a transformation matrix scaled to the size of the torus
     :return: the transformation matrix
     :return: pyglet_helper.util.Tmatrix
     """
     out = Tmatrix()
     out.translate(Vector([.5, .5, .5]))
     out.scale(Vector([self.radius, self.radius, self.radius]) *
               (.5 / (self.radius + self.thickness)))
     return out
コード例 #20
0
ファイル: test_linear.py プロジェクト: encukou/pyglet_helper
def test_vector_diff_angle():
    from pyglet_helper.util import Vector
    vec1 = Vector([1.0, 1.0, 1.0])
    vec2 = Vector([1.0, 1.0, 1.0])
    result = vec1.diff_angle(vec2)
    print(result)
    assert result == 0.0
    vec1 = Vector([1.0, 1.0, 1.0])
    vec2 = Vector([-1.0, -1.0, -1.0])
    result = vec1.diff_angle(vec2)
    print(result)
    assert result == 3.141592653589793
コード例 #21
0
 def material_matrix(self):
     """
     Creates a transformation matrix for pyramid objects
     :return: the transformation matrix
     :rtype: pyglet_helper.util.Tmatrix
     """
     out = Tmatrix()
     out.translate(Vector([0, .5, .5]))
     scale = Vector([self.axis.mag(), self.height, self.width])
     out.scale(Vector([self.scale,self.scale,self.scale]) *
               (1.0 /max(scale.x_component, max(scale.y_component,
                                           scale.z_component))))
     return out
コード例 #22
0
ファイル: axial.py プロジェクト: encukou/pyglet_helper
 def material_matrix(self):
     """
     Creates a transformation matrix scaled to the size of the axial object
     :return: the transformation matrix
     :return: pyglet_helper.util.Tmatrix
     """
     out = Tmatrix()
     out.translate(Vector([.0005, .5, .5]))
     out.scale(self.scale * (.999 / max(self.scale.x_component,
                                        self.scale.y_component * 2)))
     out_vector = Vector([0, 1, 0])
     out_vector = out_vector.rotate(angle=.5*pi)
     out = out * out_vector
     return out
コード例 #23
0
ファイル: axial.py プロジェクト: radovankavicky/pyglet_helper
 def material_matrix(self):
     """
     Creates a transformation matrix scaled to the size of the axial object
     :return: the transformation matrix
     :return: pyglet_helper.util.Tmatrix
     """
     out = Tmatrix()
     out.translate(Vector([.0005, .5, .5]))
     out.scale(
         self.scale *
         (.999 / max(self.scale.x_component, self.scale.y_component * 2)))
     out_vector = Vector([0, 1, 0])
     out_vector = out_vector.rotate(angle=.5 * pi)
     out = out * out_vector
     return out
コード例 #24
0
 def axis(self, n_axis):
     """
     Set the object's axis, as a vector
     :param n_axis: the new axis
     :type n_axis: pyglet_helper.util.Vector
     """
     if self.axis is None:
         self._axis = Vector([1, 0, 0])
     _axis = self.axis.cross(n_axis)
     if _axis.mag() == 0.0:
         self._axis = n_axis
     else:
         angle = n_axis.diff_angle(self._axis)
         self._axis = n_axis.mag() * self._axis.norm()
         self.rotate(angle, _axis, self.pos)
コード例 #25
0
def on_draw():
    global screennum
    scene.setup()
    spin = Vector([sin(rx) * cos(ry), sin(rx) * sin(ry), cos(rx)])
    _ellipsoid.axis = _ellipsoid.length * spin
    _ellipsoid.render(scene)

    # The sphere will look the same from all angles, so rotating it doesn't make sense
    _ball.render(scene)

    _pyramid.axis = _pyramid.length * spin
    _pyramid.render(scene)

    _box.axis = _box.length * spin
    _box.render(scene)

    _cylinder.axis = _cylinder.length * spin
    _cylinder.render(scene)

    _arrow.axis = _arrow.length * spin
    _arrow.render(scene)

    _cone.axis = _cone.length * spin
    _cone.render(scene)

    _ring.axis = spin
    _ring.render(scene)
    if screennum < 99:
        path = os.path.dirname(__file__)
        filename = os.path.join(path, 'screenshot%02d.png' % (screennum, ))
        get_buffer_manager().get_color_buffer().save(filename)
        screennum += 1
コード例 #26
0
    def pixel_coverage(self, pos, radius):
        """ Compute the apparent diameter, in pixels, of a circle that is
        parallel to the screen, with a center at pos, and some radius.  If pos
        is behind the camera, it will return negative.

        :param pos: The position in the view to examine
        :type pos: pyglet_helper.util.Vector
        :param radius: The radius of coverage to examine
        :type radius: float
        :return: The diameter in pixels.
        :rtype: float
        """
        # The distance from the camera to this position, in the direction of 
        # the camera.  This is the distance to the viewing plane that the 
        # coverage circle lies in.

        pos = Vector(pos)
        dist = (pos - self.camera).dot(self.forward)
        # Half of the width of the viewing plane at this distance.
        apparent_hwidth = self.tan_hfov_x * dist
        # The fraction of the apparent width covered by the coverage circle.
        if apparent_hwidth == 0:
            coverage_fraction = 1
        else:
            coverage_fraction = radius / apparent_hwidth
        # Convert from fraction to pixels.
        return coverage_fraction * self.view_width
コード例 #27
0
    def render(self, scene):
        """Add the cone to the scene.

        :param scene: The view to render the model into
        :type scene: pyglet_helper.objects.View
        """
        if self.radius == 0:
            return

        self.init_model(scene)

        coverage_levels = [10, 30, 90, 250, 450]
        lod = self.lod_adjust(scene, coverage_levels, self.pos, self.radius)

        length = self.axis.mag()
        gl.glPushMatrix()
        self.model_world_transform(scene.gcf,
                                   Vector([length, self.radius,
                                           self.radius])).gl_mult()

        self.color.gl_set(self.opacity)
        if self.translucent:
            gl.glEnable(gl.GL_CULL_FACE)

            # Render the back half.
            gl.glCullFace(gl.GL_FRONT)
            scene.cone_model[lod].gl_render()

            # Render the front half.
            gl.glCullFace(gl.GL_BACK)
            scene.cone_model[lod].gl_render()
        else:
            scene.cone_model[lod].gl_render()
        gl.glPopMatrix()
コード例 #28
0
def test_primitive_center():
    from pyglet_helper.objects import Primitive
    from pyglet_helper.util import Vector
    _primitive = Primitive(pos=Vector([-1, 0, 1]))
    assert _primitive.center[0] == -1
    assert _primitive.center[1] == 0
    assert _primitive.center[2] == 1
コード例 #29
0
 def size(self):
     """
     Gets a vector with the object's scale along the x, y, and z axes
     :return: a vector with the scales
     :rtype: pyglet_helper.util.Vector
     """
     return Vector([self.axis.mag(), self.height, self.width])
コード例 #30
0
 def scale(self):
     """
     Gets the scaling factor of the ellipsoid (1/2 of the size)
     :return: the scaling factors
     :rtype: pyglet_helper.util.Vector
     """
     return Vector([self.axis.mag(), self.height, self.width]) * 0.5
コード例 #31
0
def test_view_pixel_coverage():
    from pyglet_helper.objects import View
    from pyglet_helper.util import Vector
    scene = View()
    pix_coverage = scene.pixel_coverage(pos=Vector([10, 0, 0]), radius=0.2)
    print(pix_coverage)
    assert (pix_coverage == 800.0)
コード例 #32
0
def test_arrow_shaft_width():
    from pyglet_helper.objects import Arrow
    from pyglet_helper.util import Vector
    blo = Arrow(shaft_width=0.75)
    assert (blo.shaft_width == 0.75)
    blo = Arrow(axis=Vector([10.0, 0, 0]))
    assert (blo.shaft_width == 1.0)
コード例 #33
0
 def pos(self, n_pos):
     """
     Set the object's position with a Vector
     :param n_pos: a vector with the object's new position
     :type n_pos: pyglet_helper.util.Vector
     :return:
     """
     self._pos = Vector(n_pos)
コード例 #34
0
 def center(self):
     """
     Gets the light's center. Since the light is not a physical object,
     this will always be an empty vector
     :return: the light's center
     :rtype: pyglet_helper.util.Vector
     """
     return Vector()
コード例 #35
0
def test_vector_set_index():
    from pyglet_helper.util import Vector
    vec1 = Vector([1.0, 0, 2.0])
    vec1[0] = -1.0
    vec1[1] = -2.0
    vec1[2] = -3.0
    assert vec1.x_component == -1.0
    assert vec1.y_component == -2.0
    assert vec1.z_component == -3.0
コード例 #36
0
    def __init__(self,
                 radius=1.0,
                 color=Rgb(),
                 pos=Vector([0, 0, 0]),
                 axis=Vector([1, 0, 0])):
        """

        :param radius: The cone's bottom radius.
        :type radius: float
        :param color: The object's color.
        :type color: pyglet_helper.util.Rgb
        :param pos: The object's position.
        :type pos: pyglet_helper.util.Vector
        :param axis: The cone points from the base to the point along the axis.
        :type axis: pyglet_helper.util.Vector
        :return:
        """
        super(Cone, self).__init__(radius=radius, color=color, pos=Vector(pos))
        self.axis = Vector(axis)
コード例 #37
0
def test_vector_diff_angle():
    from pyglet_helper.util import Vector
    vec1 = Vector([1.0, 1.0, 1.0])
    vec2 = Vector([1.0, 1.0, 1.0])
    result = vec1.diff_angle(vec2)
    print(result)
    assert result == 0.0
    vec1 = Vector([1.0, 1.0, 1.0])
    vec2 = Vector([-1.0, -1.0, -1.0])
    result = vec1.diff_angle(vec2)
    print(result)
    assert result == 3.141592653589793
コード例 #38
0
ファイル: primitive.py プロジェクト: encukou/pyglet_helper
    def model_world_transform(self, world_scale=0.0,
                              object_scale=Vector([1, 1, 1])):
        """Performs scale, rotation, translation, and world scale (gcf)
        transforms in that order.

        :param world_scale: The global scaling factor.
        :type world_scale: float
        :param object_scale: The scaling to applied to this specific object
        :type object_scale: pyglet_helper.util.Vector
        :rtype: pyglet_helper.util.Tmatrix
        :returns:  Returns a tmatrix that performs reorientation of the object
        from model orientation to world
         (and view) orientation.
        """
        ret = Tmatrix()
        # A unit vector along the z_axis.
        z_axis = Vector([0, 0, 1])
        if abs(self.axis.dot(self.up_vector) / self.up_vector.mag()**2.0) \
                > 0.98:
            # Then axis and up are in (nearly) the same direction: therefore,
            # try two other possible directions for the up vector.
            if abs(self.axis.norm().dot(Vector([-1, 0, 0]))) > 0.98:
                z_axis = self.axis.cross(Vector([0, 0, 1])).norm()
            else:
                z_axis = self.axis.cross(Vector([-1, 0, 0])).norm()
        else:
            z_axis = self.axis.cross(self.up_vector).norm()

        y_axis = z_axis.cross(self.axis).norm()
        x_axis = self.axis.norm()
        ret.x_column(x_axis)
        ret.y_column(y_axis)
        ret.z_column(z_axis)
        w_column = world_scale*self.pos
        ret.w_column(w_column)
        ret.w_row()

        ret.scale(object_scale * world_scale, 1)

        return ret
コード例 #39
0
ファイル: primitive.py プロジェクト: encukou/pyglet_helper
 def axis(self, n_axis):
     """
     Set the object's axis, as a vector
     :param n_axis: the new axis
     :type n_axis: pyglet_helper.util.Vector
     """
     if self.axis is None:
         self._axis = Vector([1, 0, 0])
     _axis = self.axis.cross(n_axis)
     if _axis.mag() == 0.0:
         self._axis = n_axis
     else:
         angle = n_axis.diff_angle(self._axis)
         self._axis = n_axis.mag() * self._axis.norm()
         self.rotate(angle, _axis, self.pos)
コード例 #40
0
ファイル: cone.py プロジェクト: encukou/pyglet_helper
    def __init__(self, radius=1.0, color=Rgb(), pos=Vector([0, 0, 0]),
                 axis=Vector([1, 0, 0])):
        """

        :param radius: The cone's bottom radius.
        :type radius: float
        :param color: The object's color.
        :type color: pyglet_helper.util.Rgb
        :param pos: The object's position.
        :type pos: pyglet_helper.util.Vector
        :param axis: The cone points from the base to the point along the axis.
        :type axis: pyglet_helper.util.Vector
        :return:
        """
        super(Cone, self).__init__(radius=radius, color=color, pos=Vector(pos))
        self.axis = Vector(axis)
コード例 #41
0
ファイル: test_linear.py プロジェクト: encukou/pyglet_helper
def test_vector_nonzero():
    from pyglet_helper.util import Vector
    vec1 = Vector([1.0, 0, 2.0])
    vec2 = Vector([0, 0, 0])
    assert vec1.nonzero()
    assert not vec2.nonzero()
コード例 #42
0
ファイル: primitive.py プロジェクト: encukou/pyglet_helper
class Primitive(Renderable):
    """
     A base class for all geometric shapes.
    """
    def __init__(self, axis=Vector([1, 0, 0]), up_vector=Vector([0, 1, 0]),
                 pos=Vector([0, 0, 0]), obj_initialized=False, color=Rgb(),
                 material=Material()):
        """

        :param axis: The orientation to use when drawing.
        :type axis: pyglet_helper.util.Vector
        :param up_vector: A vector that points to the current up direction in
        the view.
        :type up_vector: pyglet_helper.util.Vector
        :param pos: The object's position.
        :type pos: pyglet_helper.util.Vector
        :param obj_initialized: If True, the object has been initialized
        :type obj_initialized: bool
        :param color: The object's color.
        :type color: pyglet_helper.util.Rgb
        :param material: The object's material
        :type material: pyglet_helper.util.Material
        """
        super(Primitive, self).__init__(color=color, mat=material)
        self._axis = None
        self._pos = None
        self._up = None
        self._width = None
        self._height = None

        self.startup = True
        self.obj_initialized = obj_initialized

        # position must be defined first, before the axis
        self.up_vector = Vector(up_vector)
        self.pos = Vector(pos)
        self.axis = Vector(axis)

    def model_world_transform(self, world_scale=0.0,
                              object_scale=Vector([1, 1, 1])):
        """Performs scale, rotation, translation, and world scale (gcf)
        transforms in that order.

        :param world_scale: The global scaling factor.
        :type world_scale: float
        :param object_scale: The scaling to applied to this specific object
        :type object_scale: pyglet_helper.util.Vector
        :rtype: pyglet_helper.util.Tmatrix
        :returns:  Returns a tmatrix that performs reorientation of the object
        from model orientation to world
         (and view) orientation.
        """
        ret = Tmatrix()
        # A unit vector along the z_axis.
        z_axis = Vector([0, 0, 1])
        if abs(self.axis.dot(self.up_vector) / self.up_vector.mag()**2.0) \
                > 0.98:
            # Then axis and up are in (nearly) the same direction: therefore,
            # try two other possible directions for the up vector.
            if abs(self.axis.norm().dot(Vector([-1, 0, 0]))) > 0.98:
                z_axis = self.axis.cross(Vector([0, 0, 1])).norm()
            else:
                z_axis = self.axis.cross(Vector([-1, 0, 0])).norm()
        else:
            z_axis = self.axis.cross(self.up_vector).norm()

        y_axis = z_axis.cross(self.axis).norm()
        x_axis = self.axis.norm()
        ret.x_column(x_axis)
        ret.y_column(y_axis)
        ret.z_column(z_axis)
        w_column = world_scale*self.pos
        ret.w_column(w_column)
        ret.w_row()

        ret.scale(object_scale * world_scale, 1)

        return ret

    def rotate(self, angle, axis, origin):
        """Rotate the primitive's axis by angle about a specified axis at a
        specified origin.

        :param angle: the angle to rotate by, in radians
        :type angle: float
        :param axis: The axis to rotate around.
        :type axis: pyglet_helper.util.Vector
        :param origin: The center of the axis of rotation.
        :type origin: pyglet_helper.util.Vector
        """
        rotation_matrix = rotation(angle, axis, origin)
        fake_up = self.up_vector
        if not self.axis.cross(fake_up):
            fake_up = Vector([1, 0, 0])
            if not self.axis.cross(fake_up):
                fake_up = Vector([0, 1, 0])
        # is this rotation needed at present? Is it already included in the
        # transformation matrix?
        #self.pos = R * self._pos
        self.up_vector = rotation_matrix.times_v(fake_up)
        self._axis = rotation_matrix.times_v(self._axis)

    @property
    def center(self):
        """
        Gets the object's center
        :return: the object's center, as a vector
        :rtype: pyglet_helper.util.Vector
        """
        return self.pos

    @property
    def pos(self):
        """
        Get the object's current position
        :return: the object's position
        :rtype: pyglet_helper.util.Vector
        """
        return self._pos

    @pos.setter
    def pos(self, n_pos):
        """
        Set the object's position with a Vector
        :param n_pos: a vector with the object's new position
        :type n_pos: pyglet_helper.util.Vector
        :return:
        """
        self._pos = Vector(n_pos)

    @property
    def length(self):
        """
        Get the object's length (for most objects, this is the magnitude of
        its axis)
        :return: the object's length
        :rtype: float
        """
        return self.axis.mag()

    @length.setter
    def length(self, new_length):
        """
        Set the object's length
        :param new_length: the new length of the object
        :type new_length: float
        :return:
        """
        if new_length < 0:
            raise ValueError("length cannot be negative")
        self.axis = self.axis.norm() * new_length

    @property
    def height(self):
        """
        Gets the scale of the object along the y axis
        :return: the object's scale along the y axis
        :rtype: float
        """
        return self._height

    @height.setter
    def height(self, new_height):
        """
        Sets the scale of the object along the y axis
        :param new_height: the object's scale along the y axis
        :type new_height: float
        """
        if new_height < 0:
            raise ValueError("height cannot be negative")
        self._height = new_height

    @property
    def width(self):
        """
        Gets the scale of the object along the z axis
        :return: the object's scale along the z axis
        :rtype: float
        """
        return self._width

    @width.setter
    def width(self, new_width):
        """
        Sets the scale of the object along the z axis
        :param new_width: the object's scale along the y axis
        :type new_width: float
        """
        if new_width < 0:
            raise ValueError("width cannot be negative")
        self._width = new_width

    @property
    def size(self):
        """
        Gets a vector with the object's scale along the x, y, and z axes
        :return: a vector with the scales
        :rtype: pyglet_helper.util.Vector
        """
        return Vector([self.axis.mag(), self.height, self.width])

    @size.setter
    def size(self, new_size):
        """
        Sets the object's scale along the x, y, and z axes
        :param new_size: the new scales, in a vector
        :type new_size: pyglet_helper.util.Vector
        """
        if new_size.x_component < 0:
            raise ValueError("length cannot be negative")
        if new_size.y_component < 0:
            raise ValueError("height cannot be negative")
        if new_size.z_component < 0:
            raise ValueError("width cannot be negative")
        self.axis = self.axis.norm() * new_size.x_component
        self.height = new_size.y_component
        self.width = new_size.z_component

    @property
    def axis(self):
        """
        Get the object's axis, which defines the orientation and size of the
        object
        :return: the object's axis
        :rtype; pyglet_helper.util.Vector
        """
        return self._axis

    @axis.setter
    def axis(self, n_axis):
        """
        Set the object's axis, as a vector
        :param n_axis: the new axis
        :type n_axis: pyglet_helper.util.Vector
        """
        if self.axis is None:
            self._axis = Vector([1, 0, 0])
        _axis = self.axis.cross(n_axis)
        if _axis.mag() == 0.0:
            self._axis = n_axis
        else:
            angle = n_axis.diff_angle(self._axis)
            self._axis = n_axis.mag() * self._axis.norm()
            self.rotate(angle, _axis, self.pos)

    @property
    def up_vector(self):
        """
        Get the object's up axis
        :return: the object's up axis
        :rtype: pyglet_helper.util.Vector
        """
        return self._up

    @up_vector.setter
    def up_vector(self, n_up):
        """
        Set the object's up axis
        :param n_up: the object's up axis
        :type n_up: pyglet_helper.util.Vector
        """
        self._up = n_up

    @property
    def is_light(self):
        """
        Returns false if the object is not a light. By default,
        all primitives are not lights
        :return: whether the object is a light
        :rtype: bool
        """
        return False
コード例 #43
0
ファイル: cone.py プロジェクト: encukou/pyglet_helper
class Cone(Axial):
    """
    A Cone object
    """
    def __init__(self, radius=1.0, color=Rgb(), pos=Vector([0, 0, 0]),
                 axis=Vector([1, 0, 0])):
        """

        :param radius: The cone's bottom radius.
        :type radius: float
        :param color: The object's color.
        :type color: pyglet_helper.util.Rgb
        :param pos: The object's position.
        :type pos: pyglet_helper.util.Vector
        :param axis: The cone points from the base to the point along the axis.
        :type axis: pyglet_helper.util.Vector
        :return:
        """
        super(Cone, self).__init__(radius=radius, color=color, pos=Vector(pos))
        self.axis = Vector(axis)

    def init_model(self, scene):
        """Add the cone quadrics to the view.

        :param scene: The view to render the model to.
        :type scene: pyglet_helper.objects.View
        """
        # The number of faces corresponding to each level of detail.
        n_sides = [8, 16, 32, 46, 68, 90]
        n_stacks = [1, 2, 4, 7, 10, 14]
        for i in range(0, 6):
            scene.cone_model[i].gl_compile_begin()
            _quadric = Quadric()
            _quadric.render_cylinder(1.0, 1.0, n_sides[i], n_stacks[i],
                                     top_radius=0.0)
            _quadric.render_disk(1.0, n_sides[i], n_stacks[i] * 2, -1)
            scene.cone_model[i].gl_compile_end()

    def render(self, scene):
        """Add the cone to the scene.

        :param scene: The view to render the model into
        :type scene: pyglet_helper.objects.View
        """
        if self.radius == 0:
            return

        self.init_model(scene)

        coverage_levels = [10, 30, 90, 250, 450]
        lod = self.lod_adjust(scene, coverage_levels, self.pos, self.radius)

        length = self.axis.mag()
        gl.glPushMatrix()
        self.model_world_transform(scene.gcf, Vector([length, self.radius,
                                                      self.radius])).gl_mult()

        self.color.gl_set(self.opacity)
        if self.translucent:
            gl.glEnable(gl.GL_CULL_FACE)

            # Render the back half.
            gl.glCullFace(gl.GL_FRONT)
            scene.cone_model[lod].gl_render()

            # Render the front half.
            gl.glCullFace(gl.GL_BACK)
            scene.cone_model[lod].gl_render()
        else:
            scene.cone_model[lod].gl_render()
        gl.glPopMatrix()

    @property
    def center(self):
        """ Get the center of the object.

        :return: position + axis/2
        :rtype: float
        """
        return self.pos + self.axis / 2.0

    @property
    def degenerate(self):
        """

        :return: True if radius == 0 or the length is 0
        :rtype: bool
        """
        return self.radius == 0.0 or self.axis.mag() == 0.0
コード例 #44
0
ファイル: test_linear.py プロジェクト: encukou/pyglet_helper
def test_vector_render():
    from pyglet_helper.util import Vector
    vec1 = Vector([1.0, 0, 2.0])
    vec1.gl_normal()
    vec1.gl_render()
コード例 #45
0
ファイル: test_linear.py プロジェクト: encukou/pyglet_helper
def test_vector_proj():
    from pyglet_helper.util import Vector
    vec1 = Vector([1.0, 0, 2.0])
    vec2 = Vector([0, 1.0, 0])
    result = vec1.proj(vec2)
    assert result == Vector([0.0, 0.0, 0.0])
コード例 #46
0
ファイル: test_linear.py プロジェクト: encukou/pyglet_helper
def test_vector_comp():
    from pyglet_helper.util import Vector
    vec1 = Vector([1.0, 0, 2.0])
    vec2 = Vector([0, 1.0, 0])
    vec3 = vec1.comp(vec2)
    assert vec3 == 0
コード例 #47
0
ファイル: test_linear.py プロジェクト: encukou/pyglet_helper
def test_vector_fabs():
    from pyglet_helper.util import Vector
    vec1 = Vector([-1.0, 0, -2.0])
    vec1 = vec1.fabs()
    assert vec1.x_component == 1.0
    assert vec1.z_component == 2.0
コード例 #48
0
ファイル: test_linear.py プロジェクト: encukou/pyglet_helper
def test_vector_sum():
    from pyglet_helper.util import Vector
    vec1 = Vector([-1.0, 0, -2.0])
    assert vec1.sum() == -3.0