Пример #1
0
def test_transforms():
    """Test basic transforms"""
    xfm = np.random.randn(4, 4).astype(np.float32)

    for rot in [xrotate, yrotate, zrotate]:
        new_xfm = rot(rot(xfm, 90), -90)
        assert_allclose(xfm, new_xfm)

    new_xfm = rotate(rotate(xfm, 90, 1, 0, 0), 90, -1, 0, 0)
    assert_allclose(xfm, new_xfm)

    new_xfm = translate(translate(xfm, 1, -1), 1, -1, 1)
    assert_allclose(xfm, new_xfm)

    new_xfm = scale(scale(xfm, 1, 2, 3), 1, 1. / 2., 1. / 3.)
    assert_allclose(xfm, new_xfm)

    # These could be more complex...
    xfm = ortho(-1, 1, -1, 1, -1, 1)
    assert_equal(xfm.shape, (4, 4))

    xfm = frustum(-1, 1, -1, 1, -1, 1)
    assert_equal(xfm.shape, (4, 4))

    xfm = perspective(1, 1, -1, 1)
    assert_equal(xfm.shape, (4, 4))
Пример #2
0
    def update(self):
        yaw = State.yaw

        counter_yaw = 360 - (np.degrees(yaw) % 360)
        # Could use optimization by subtracting current yaw from previous yaw
        map_transform = np.eye(4)
        # map_transform = zrotate(map_transform, counter_yaw)
        scale(map_transform, 0.2)
        
        self.program['map_transform'] = map_transform

        # Convert Forrest's ecef position to lla
        new_position = State.position_ecef
        abs_changes = np.fabs(self.position_lla - new_position)

        # if max(abs_changes) > 100:  # (m)
            # self.set_map(new_position)
            # pass

        new_position_lla = self.ecef2llh(State.position_ecef)[:2]
        self.program['user_position'] = self.position_lla[:2]


        if State.hide_map:
            self.program['hide'] = 1
        else:
            self.program['hide'] = 0
Пример #3
0
    def update(self):
        yaw = State.yaw

        counter_yaw = 360 - (np.degrees(yaw) % 360)
        # Could use optimization by subtracting current yaw from previous yaw
        map_transform = np.eye(4)
        # map_transform = zrotate(map_transform, counter_yaw)
        scale(map_transform, 0.2)

        self.program['map_transform'] = map_transform

        # Convert Forrest's ecef position to lla
        new_position = State.position_ecef
        abs_changes = np.fabs(self.position_lla - new_position)

        # if max(abs_changes) > 100:  # (m)
        # self.set_map(new_position)
        # pass

        new_position_lla = self.ecef2llh(State.position_ecef)[:2]
        self.program['user_position'] = self.position_lla[:2]

        if State.hide_map:
            self.program['hide'] = 1
        else:
            self.program['hide'] = 0
Пример #4
0
 def get_projection(self, viewbox):
     w, h = self.fov
     from vispy.util import transforms
     projection = np.eye(4)
     transforms.scale(projection, 2.0/w, 2.0/h)
     transforms.scale(projection, 1, -1)  # Flip y-axis
     return projection
Пример #5
0
def create_transform_ortho(aspect=1.0, view="isometric", fake_ortho=True):
    model = np.eye(4, dtype=np.float32)

    if view == "isometric":
        if fake_ortho:
            # 0.816479 = 0.5 * sqrt(3) * x = 0.5 * sqrt(2)
            # scale of y-axis to make sides and top of same height: (1.0, 0.81649, 1.0)
            # scale to get block completely into viewport (-1;1): (1.0 / math.sqrt(2), ..., ...)
            model = np.dot(model, transforms.scale((1.0 / math.sqrt(2), 0.816479 / math.sqrt(2), 1.0 / math.sqrt(2))))
        else:
            # this scale factor is just for nicely viewing the block image manually
            model = np.dot(model, transforms.scale((0.5, 0.5, 0.5)))

        # and do that nice tilt
        model = np.dot(model, np.dot(transforms.rotate(45, (0, 1, 0)), transforms.rotate(30, (1, 0, 0))))
    elif view == "topdown":
        model = np.dot(model, transforms.rotate(90, (1, 0, 0)))
    elif view == "side":
        # same thing with scaling factor as with isometric view
        #f = 1.0 / math.sqrt(2)
        f = 0.5 / math.cos(math.radians(45))
        model = np.dot(model, transforms.scale((f / math.cos(math.radians(45)), f, f)))
        model = np.dot(model, transforms.rotate(45, (1, 0, 0)))
    elif view == "default":
        pass
    else:
        assert False, "Invalid view '%s'!" % view

    view = transforms.translate((0, 0, -5))
    projection = transforms.ortho(-aspect, aspect, -1, 1, 2.0, 50.0)
    return model, view, projection
Пример #6
0
 def get_projection(self, viewport):
     w, h = self.fov
     from vispy.util import transforms
     projection = np.eye(4)
     transforms.scale(projection, 2.0 / w, 2.0 / h)
     transforms.scale(projection, 1, -1)  # Flip y-axis
     return projection
def test_transforms():
    """Test basic transforms"""
    xfm = np.random.randn(4, 4).astype(np.float32)

    for rot in [xrotate, yrotate, zrotate]:
        new_xfm = rot(rot(xfm, 90), -90)
        assert_allclose(xfm, new_xfm)

    new_xfm = rotate(rotate(xfm, 90, 1, 0, 0), 90, -1, 0, 0)
    assert_allclose(xfm, new_xfm)

    new_xfm = translate(translate(xfm, 1, -1), 1, -1, 1)
    assert_allclose(xfm, new_xfm)

    new_xfm = scale(scale(xfm, 1, 2, 3), 1, 1. / 2., 1. / 3.)
    assert_allclose(xfm, new_xfm)

    # These could be more complex...
    xfm = ortho(-1, 1, -1, 1, -1, 1)
    assert_equal(xfm.shape, (4, 4))

    xfm = frustum(-1, 1, -1, 1, -1, 1)
    assert_equal(xfm.shape, (4, 4))

    xfm = perspective(1, 1, -1, 1)
    assert_equal(xfm.shape, (4, 4))
Пример #8
0
 def on_mouse_wheel(self, event):
     if event.delta[1] > 0:
         self.model = np.dot(scale([1.5,1.5,1.5] ), self.model)
     else:
         self.model = np.dot(scale([0.8, 0.8, 0.8]), self.model)
     self.program['u_model'] = self.model
     self.update()
Пример #9
0
def test_transforms():
    """Test basic transforms"""
    xfm = np.random.randn(4, 4).astype(np.float32)

    # Do a series of rotations that should end up into the same orientation
    # again, to ensure the order of computation is all correct
    # i.e. if rotated would return the transposed matrix this would not work
    # out (the translation part would be incorrect)
    new_xfm = xfm.dot(rotate(180, (1, 0, 0)).dot(rotate(-90, (0, 1, 0))))
    new_xfm = new_xfm.dot(rotate(90, (0, 0, 1)).dot(rotate(90, (0, 1, 0))))
    new_xfm = new_xfm.dot(rotate(90, (1, 0, 0)))
    assert_allclose(xfm, new_xfm)

    new_xfm = translate((1, -1, 1)).dot(translate((-1, 1, -1))).dot(xfm)
    assert_allclose(xfm, new_xfm)

    new_xfm = scale((1, 2, 3)).dot(scale((1, 1. / 2., 1. / 3.))).dot(xfm)
    assert_allclose(xfm, new_xfm)

    # These could be more complex...
    xfm = ortho(-1, 1, -1, 1, -1, 1)
    assert_equal(xfm.shape, (4, 4))

    xfm = frustum(-1, 1, -1, 1, -1, 1)
    assert_equal(xfm.shape, (4, 4))

    xfm = perspective(1, 1, -1, 1)
    assert_equal(xfm.shape, (4, 4))
Пример #10
0
def test_transforms():
    """Test basic transforms"""
    xfm = np.random.randn(4, 4).astype(np.float32)

    # Do a series of rotations that should end up into the same orientation
    # again, to ensure the order of computation is all correct
    # i.e. if rotated would return the transposed matrix this would not work
    # out (the translation part would be incorrect)
    new_xfm = xfm.dot(rotate(180, (1, 0, 0)).dot(rotate(-90, (0, 1, 0))))
    new_xfm = new_xfm.dot(rotate(90, (0, 0, 1)).dot(rotate(90, (0, 1, 0))))
    new_xfm = new_xfm.dot(rotate(90, (1, 0, 0)))
    assert_allclose(xfm, new_xfm)

    new_xfm = translate((1, -1, 1)).dot(translate((-1, 1, -1))).dot(xfm)
    assert_allclose(xfm, new_xfm)

    new_xfm = scale((1, 2, 3)).dot(scale((1, 1. / 2., 1. / 3.))).dot(xfm)
    assert_allclose(xfm, new_xfm)

    # These could be more complex...
    xfm = ortho(-1, 1, -1, 1, -1, 1)
    assert_equal(xfm.shape, (4, 4))

    xfm = frustum(-1, 1, -1, 1, -1, 1)
    assert_equal(xfm.shape, (4, 4))

    xfm = perspective(1, 1, -1, 1)
    assert_equal(xfm.shape, (4, 4))
Пример #11
0
 def get_projection(self, viewport):
     w, h = viewport.resolution
     from vispy.util import transforms
     projection = np.eye(4)
     transforms.scale(projection, 2.0/w, 2.0/h)
     transforms.translate(projection, -1, -1)
     transforms.scale(projection, 1, -1)  # Flip y-axis
     return projection
Пример #12
0
 def get_projection(self, viewport):
     w, h = viewport.resolution
     from vispy.util import transforms
     projection = np.eye(4)
     transforms.scale(projection, 2.0 / w, 2.0 / h)
     transforms.translate(projection, -1, -1)
     transforms.scale(projection, 1, -1)  # Flip y-axis
     return projection
Пример #13
0
    def on_initialize(self, event):
        # Build cube data
        # --------------------------------------

        texcoord = [(0, 0), (0, 1), (1, 0), (1, 1)]
        vertices = [(-2, -1, 0), (-2, +1, 0), (+2, -1, 0), (+2, +1, 0)]

        vertices = VertexBuffer(vertices)

        camera_pitch = 0.0  # Degrees
        self.rotate = [camera_pitch, 0, 0]
        self.translate = [0, 0, -3]

        # Build program
        # --------------------------------------
        view = np.eye(4, dtype=np.float32)
        model = np.eye(4, dtype=np.float32)
        scale(model, 1, 1, 1)
        self.phi = 0

        self.cube = Program(cube_vertex, cube_fragment)
        self.cube['position'] = vertices
        # 4640 x 2256
        imtex = cv2.imread(os.path.join(img_path, 'stage.jpg'))
        self.cube['texcoord'] = texcoord
        self.cube["texture"] = np.uint8(
            np.clip(imtex + np.random.randint(-60, 20, size=imtex.shape), 0,
                    255)) + 5
        self.cube["texture"].interpolation = 'linear'
        self.cube['model'] = model
        self.cube['view'] = view

        color = Texture2D((640, 640, 3), interpolation='linear')
        self.framebuffer = FrameBuffer(color, RenderBuffer((640, 640)))

        self.quad = Program(quad_vertex, quad_fragment)
        self.quad['texcoord'] = [(0, 0), (0, 1), (1, 0), (1, 1)]

        self.quad['position'] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)]

        self.quad['texture'] = color

        self.objects = [self.cube]

        # OpenGL and Timer initalization
        # --------------------------------------
        set_state(clear_color=(.3, .3, .35, 1), depth_test=True)
        self.timer = app.Timer('auto', connect=self.on_timer, start=True)
        self.pub_timer = app.Timer(0.1, connect=self.send_ros_img, start=True)
        self._set_projection(self.size)
Пример #14
0
    def on_initialize(self, event):
        # Build cube data
        # --------------------------------------

        texcoord = [(0, 0), (0, 1), (1, 0), (1, 1)]
        vertices = [(-2, -1, 0), (-2, +1, 0), (+2, -1, 0), (+2, +1, 0)]

        vertices = VertexBuffer(vertices)

        camera_pitch = 0.0 # Degrees
        self.rotate = [camera_pitch, 0, 0]
        self.translate = [0, 0, -3]

        # Build program
        # --------------------------------------
        view = np.eye(4, dtype=np.float32)
        model = np.eye(4, dtype=np.float32)
        scale(model, 1, 1, 1)
        self.phi = 0

        self.cube = Program(cube_vertex, cube_fragment)
        self.cube['position'] = vertices
        # 4640 x 2256
        imtex = cv2.imread(os.path.join(img_path, 'stage.jpg')) 
        self.cube['texcoord'] = texcoord
        self.cube["texture"] = np.uint8(np.clip(imtex + np.random.randint(-60, 20, size=imtex.shape), 0, 255)) + 5
        self.cube["texture"].interpolation = 'linear'
        self.cube['model'] = model
        self.cube['view'] = view


        color = Texture2D((640, 640, 3), interpolation='linear')
        self.framebuffer = FrameBuffer(color, RenderBuffer((640, 640)))

        self.quad = Program(quad_vertex, quad_fragment)
        self.quad['texcoord'] = [(0, 0), (0, 1), (1, 0), (1, 1)]

        self.quad['position'] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)]

        self.quad['texture'] = color

        self.objects = [self.cube]

        # OpenGL and Timer initalization
        # --------------------------------------
        set_state(clear_color=(.3, .3, .35, 1), depth_test=True)
        self.timer = app.Timer('auto', connect=self.on_timer, start=True)
        self.pub_timer = app.Timer(0.1, connect=self.send_ros_img, start=True)
        self._set_projection(self.size)
Пример #15
0
 def adjust_zoom(self, event):
     """Increase ('Z') or decrease ('z') rendered zoom-level."""
     if 'Shift' in event.modifiers:
         self.zoom *= 1.25
     else:
         self.zoom *= 1./1.25
     self.scale = np.eye(4, dtype=np.float32)
     self.anti_scale = np.eye(4, dtype=np.float32)
     scale(self.scale, self.zoom, self.zoom, self.zoom)
     scale(self.anti_scale, 1./self.zoom, 1./self.zoom, 1./self.zoom) 
     # allow cropper to adjust volume for mip-levels
     self.reload_data()
     self.update_view()
     self.update()
     print 'adjust_zoom', self.zoom
Пример #16
0
	def __init__(self, atoms, radius=6.):
		app.Canvas.__init__(self, title='Molecular viewer', keys='interactive', size=(1200, 800))
		self.ps = self.pixel_scale

		self.zoom = .5/numpy.amax(numpy.linalg.norm(atoms['zyx'], axis=1))

		self.program = gloo.Program(vertex, fragment)
		self.view = scale(self.zoom*numpy.ones(3))
		self.model = numpy.eye(4, dtype=numpy.float32)
		self.projection = numpy.eye(4, dtype=numpy.float32)

		data = numpy.zeros(atoms.size, [('a_position', numpy.float32, 3),
							('a_color', numpy.float32, 4),
							('a_radius', numpy.float32, 1)])

		data['a_position'] = atoms['zyx']
		data['a_color'] = 1,0,0,1
		data['a_color'][atoms['Z']==16] = 1,1,0,1
		data['a_color'][atoms['Z']==74] = 0,.5,1,1
		data['a_radius'] = atoms['Z']**(1/3)*radius/self.zoom

		self.program.bind(gloo.VertexBuffer(data))

		self.program['u_zoom'] = self.zoom
		self.program['u_model'] = self.model
		self.program['u_view'] = self.view
		self.program['u_light_position'] = 0., 0., 2.
		self.program['u_light_spec_position'] = -5., 5., -5.
		self.apply_zoom()

		self.program['u_model'] = self.model
		self.program['u_view'] = self.view

		gloo.set_state(depth_test=True, clear_color='white')
		self.show()
Пример #17
0
    def on_timer(self, event):
        # self.phi += .5
        model = np.eye(4, dtype=np.float32)
        scale(model, 1, 1, 1)
        # rotate(model, self.phi, 0, 0, 1)
        self.cube['model'] = model

        self.view = np.eye(4)
        xrotate(self.view, self.rotate[0])
        yrotate(self.view, self.rotate[1])
        zrotate(self.view, self.rotate[2])
        translate(self.view, *self.translate)

        self.cube['view'] = self.view

        self.update()
Пример #18
0
    def on_timer(self, event):
        # self.phi += .5
        model = np.eye(4, dtype=np.float32)
        scale(model, 1, 1, 1)
        # rotate(model, self.phi, 0, 0, 1)
        self.cube['model'] = model

        self.view = np.eye(4)
        xrotate(self.view, self.rotate[0])
        yrotate(self.view, self.rotate[1])
        zrotate(self.view, self.rotate[2])
        translate(self.view, *self.translate)

        self.cube['view'] = self.view

        self.update()
Пример #19
0
	def on_mouse_wheel(self, event):
		self.zoom *= .9**event.delta[1]
		self.view = scale(self.zoom*numpy.ones(3))

		self.program['u_zoom'] = self.zoom

		self.program['u_view'] = self.view
		self.update()
Пример #20
0
    def __init__(self):
        '''Map drawable - contains the goddamn map
        '''
        self.projection = np.eye(4)
        self.view = np.eye(4)

        self.model = scale(np.eye(4), 0.6)
        orientation_vector = (0, 1, 0)
        unit_orientation_angle = np.array(orientation_vector) / np.linalg.norm(orientation_vector)
        rotate(self.model, -30, *unit_orientation_angle)
        translate(self.model, -2.2, -2.4, -9)
        
        height, width = 5.0, 5.0  # Meters

        # Add texture coordinates
        # Rectangle of height height
        self.vertices = np.array([
            [-width / 2, -height / 2, 0],
            [ width / 2, -height / 2, 0],
            [ width / 2,  height / 2, 0],
            [-width / 2,  height / 2, 0],
        ], dtype=np.float32)

        self.tex_coords = np.array([
            [0, 1],
            [1, 1],
            [1, 0],
            [0, 0],
        ], dtype=np.float32)

        self.indices = IndexBuffer([
            0, 1, 2,
            2, 3, 0,
        ])

        ###### TESTING
        self.position_lla = self.ecef2llh((738575.65, -5498374.10, 3136355.42))
        ###### TESTING

        self.map, self.ranges = self.cache_map(self.position_lla[:2])
        self.map, self.ranges = self.get_map(self.position_lla[:2])
        self.program = Program(self.frame_vertex_shader, self.frame_frag_shader)

        default_map_transform = np.eye(4)

        self.program['vertex_position'] = self.vertices
        self.program['default_texcoord'] = self.tex_coords
        self.program['zoom'] = 1
        self.program['view'] = self.view
        self.program['model'] = self.model
        self.program['projection'] = self.projection

        self.program['map_transform'] = default_map_transform
        self.program['map_center'] = self.position_lla[:2]
        self.program['map_texture'] = self.map
        self.program['corners'] = self.ranges
        self.program['user_position'] = self.position_lla[:2]
        self.program['hide'] = 0
Пример #21
0
 def update_scene(self):
     scale_matrix = scale([1, 1, 1])
     rotation_matrix = rotate(self.rotation, [0, 1, 0])
     translation_matrix = translate([0.4, 0, -4])
     model_matrix = scale_matrix @ rotation_matrix @ translation_matrix
     self.program['model_matrix'] = model_matrix
     self.program['normal_matrix'] = np.linalg.inv(model_matrix)
     self.program['view_matrix'] = translate([0, 0, 0])
     self.program['projection_matrix'] = perspective(45, 1.66, 1., 100.)
def generate_image(img_def):
    c = context.FakeCanvas()

    img = _load_img(img_def['background']['path'])
    h, w, _ = img.shape

    render_fbo = gloo.FrameBuffer(gloo.Texture2D(img), gloo.RenderBuffer(
        (h, w)))

    program = gloo.Program(_vert_std, _frag_tex)
    program['a_pos'] = _unit_square()
    program['a_tex_coord'] = _unit_square()
    gloo.set_state(blend=True,
                   blend_func=('one', 'one_minus_src_alpha'),
                   depth_test=True,
                   depth_func='always')

    with render_fbo:
        gloo.set_viewport(0, 0, w, h)
        gloo.set_clear_depth(0)
        gloo.clear(depth=True, color=False)

        instances = img_def['instances']
        # The unsigned byte depth buffer extraction sets a limit of 255 instances.
        # Can be extracted as short if necessary.
        assert (len(instances) <= 255)
        for i, inst in enumerate(instances):
            img = _load_img(inst['path'])
            ih, iw, _ = img.shape
            x, y, s, r = (inst[k] for k in ['x', 'y', 's', 'r'])

            program['u_tex'] = gloo.Texture2D(img, interpolation='linear')
            program['u_mvp'] = \
                transforms.translate((-0.5, -0.5, 0)) @ \
                transforms.scale((s * iw, s * ih, 1)) @ \
                transforms.rotate(r, (0, 0, 1)) @ \
                transforms.translate((x, y, -i-1)) @ \
                transforms.ortho(0, w, 0, h, 0, 255)

            program.draw()

        rgb = render_fbo.read(alpha=False)
        depth = gl.glReadPixels(0, 0, w, h, gl.GL_DEPTH_COMPONENT,
                                gl.GL_UNSIGNED_BYTE)
        if not isinstance(depth, np.ndarray):
            depth = np.frombuffer(depth, np.uint8)
        depth = np.flip(depth.reshape(h, w), axis=0)
        masks = np.empty((h, w, len(instances)), np.bool)
        for i in range(len(instances)):
            masks[:, :, i] = depth == i + 1

    return rgb, depth, masks
Пример #23
0
 def __init__(self):
     app.Canvas.__init__(self, keys='interactive')
     self.program = gloo.Program(vertex, fragment)
     self.default_view = np.eye(4, dtype=np.float32)
     self.view = self.default_view
     self.model = np.eye(4, dtype=np.float32)
     self.projection = np.eye(4, dtype=np.float32)
     self.translate = [0, 0, 0]
     self.rotate = [0, 0, 0]
     self.program["u_light_position"] = self.lightx,self.lighty,self.lightz
     self.program["u_light_intensity"] = 1, 1, 1
     self.program['u_model'] = scale(self.model,cellsize,cellsize,1.0)
     translate(self.view, -1250, -1250, -2000)
     zrotate(self.view, 90)
     self.program['u_view'] = self.view
     self.program['u_normal'] = np.array(np.matrix(np.dot(self.view, self.model)).I.T)
     self.update()
     self.program['a_position'] = gloo.VertexBuffer(triangles)
     self.program['a_scale'] = np.array([cellsize,cellsize,1.0,1.0])
Пример #24
0
 def __init__(self):
     app.Canvas.__init__(self, keys='interactive')
     self.program = gloo.Program(vertex, fragment)
     self.default_view = np.eye(4, dtype=np.float32)
     self.view = self.default_view
     self.model = np.eye(4, dtype=np.float32)
     self.projection = np.eye(4, dtype=np.float32)
     self.translate = [0, 0, 0]
     self.rotate = [0, 0, 0]
     self.program[
         "u_light_position"] = self.lightx, self.lighty, self.lightz
     self.program["u_light_intensity"] = 1, 1, 1
     self.program['u_model'] = scale(self.model, cellsize, cellsize, 1.0)
     translate(self.view, -1250, -1250, -2000)
     zrotate(self.view, 90)
     self.program['u_view'] = self.view
     self.program['u_normal'] = np.array(
         np.matrix(np.dot(self.view, self.model)).I.T)
     self.update()
     self.program['a_position'] = gloo.VertexBuffer(triangles)
     self.program['a_scale'] = np.array([cellsize, cellsize, 1.0, 1.0])
Пример #25
0
    def __init__(self, program, indices, kind):
        '''

        Hier werden alle self.(...) Variablen initiiert und die Parameter ihren Variablen zugeordnet.

        Parameter:  program und indices sind die Punkte die die Grafik darstellen sollen
                    durch kind kann man die Objekte leichter auseinanderhalten, wenn man
                    z.B. nur die Wasserstoffatomgrafiken ansprechen will.
        Rueckgabewerte: -

        '''

        self._program = program
        self._indices = indices

        self.kind = kind

        self._translation = (0, 0, 0)
        self._rotation = rotate(0, (1, 1, 1))
        self._scaling = scale((1, 1, 1))

        self._drawType = 'points'
Пример #26
0
    def get_visible_pixel(self,intrinsic,center):

        print('---------------center',center)
        #center = 10.0*center
        view_mat = translate((center[0],center[1],center[2]))
        scale_factor = 0.0725#25#0.01*6.5#.32#.375
        scale_mat = scale((scale_factor,scale_factor,scale_factor))

        model_view = np.matrix(self.model)
        model_view = np.dot(model_view,scale_mat)
        model_view = np.dot(model_view,view_mat)

        modified_normal = np.dot(self.face_normals_wrapper, (model_view.I).T)
        modified_vertices = np.dot(self.vertice_wrapper, model_view)
        modified_vertices = np.array(modified_vertices)

        idx = np.where(modified_normal[:, 2] > 1e-7)
        visible_faces = np.array(self.faces[idx[0],:]).reshape(-1)
        points = modified_vertices[visible_faces]
        out_pixel = np.zeros([points.shape[0], 2]).astype(np.int32)
        DataOperator.proj_point2pixel_func(points, intrinsic, out_pixel)

        return out_pixel
Пример #27
0
    def on_initialize(self, event):
        # Build cube data
        # --------------------------------------

        texcoord = [(0, 0), (0, 1), (1, 0), (1, 1)]
        vertices = [(-1, -1, 0), (-1, +1, 0), (+1, -1, 0), (+1, +1, 0)]

        vertices = VertexBuffer(vertices)

        #self.listener.waitForTransform("/robot", "/wrist_joint", rospy.Time(), rospy.Duration(4))

        """while not rospy.is_shutdown():
            try:
                now = rospy.Time.now()
                self.listener.waitForTransform("/wrist_joint", "/robot", rospy.Time(), rospy.Duration(4))
                (trans,rot) = listener.lookupTransform("/wrist_joint", "/robot", rospy.Time(), rospy.Duration(4))
        """

        #pos, rot = self.listener.lookupTransform("/wrist_joint", "/robot", rospy.Time(0))

        #print list(pos)
        
        camera_pitch = 0.0 # Degrees
        self.rotate = [camera_pitch, 0, 0]
        #self.translate = list(pos)
        self.translate = [0, 0, -5]

        # Build program
        # --------------------------------------
        view = np.eye(4, dtype=np.float32)
        model = np.eye(4, dtype=np.float32)
        scale(model, 10, 10, 10)
        self.phi = 0

        self.cube = Program(cube_vertex, cube_fragment)
        self.cube['position'] = vertices
        # 4640 x 2256
        imtex = cv2.imread(os.path.join(img_path, 'rubixFront.jpg')) 
        self.cube['texcoord'] = texcoord
        self.cube["texture"] = np.uint8(np.clip(imtex + np.random.randint(-60, 20, size=imtex.shape), 0, 255)) + 5
        self.cube["texture"].interpolation = 'linear'
        self.cube['model'] = model
        self.cube['view'] = view


        color = Texture2D((640, 640, 3), interpolation='linear')
        self.framebuffer = FrameBuffer(color, RenderBuffer((640, 640)))

        self.quad = Program(quad_vertex, quad_fragment)
        self.quad['texcoord'] = [(0, 0), (0, 1), (1, 0), (1, 1)]

        self.quad['position'] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)]

        self.quad['texture'] = color

        self.objects = [self.cube]

        # OpenGL and Timer initalization
        # --------------------------------------
        set_state(clear_color=(.3, .3, .35, 1), depth_test=True)
        self.timer = app.Timer('auto', connect=self.on_timer, start=True)
        self.pub_timer = app.Timer(0.1, connect=self.send_ros_img, start=True)
        self._set_projection(self.size)
Пример #28
0
    def __init__(self, canvas, background_color=(0,0,0,0), text_color=(1,1,1)):
        # State.register_button(position, text)
        
        self.canvas = canvas
        self.text = None
        self.tcolor = (text_color[0],text_color[1],text_color[2], 1)
        self.bcolor = background_color
        self.timer = 0
        self.fade_timer = 0
        self.projection = np.eye(4)
        self.view = np.eye(4)
        self.model = np.eye(4)
        height, width = 5.0, 15.0  # Meters

        orientation_vector = (1, 1, 0)
        unit_orientation_angle = np.array(orientation_vector) / np.linalg.norm(orientation_vector)

        scale_factor = 0.4
        lowest_button = -5.2 
        midset = 0.2
        position = -2
        
        scale(self.model, scale_factor)
        #yrotate(self.model, -90)
        # rotate(self.model, 30, *unit_orientation_angle)
        offset = (position * ((height + midset) * scale_factor))
        translate(self.model, -4.4, lowest_button + offset, -10)

        self.size = (int(height*100), int(width*100))

        # Add texture coordinates
        # Rectangle of height height
        self.vertices = np.array([
            [-width / 2, -height / 2, 0],
            [ width / 2, -height / 2, 0],
            [ width / 2,  height / 2, 0],
            [-width / 2,  height / 2, 0],
        ], dtype=np.float32)
        
        self.tex_coords = np.array([
            [0, 0],
            [1, 0],
            [1, 1],
            [0, 1],

        ], dtype=np.float32)

        self.indices = IndexBuffer([
            0, 1, 2,
            2, 3, 0,
        ])

        self.program = Program(self.toast_vertex_shader, self.toast_fragment_shader)

        self.program['vertex_position'] = self.vertices
        self.program['default_texcoord'] = self.tex_coords
        self.program['view'] = self.view
        self.program['model'] = self.model
        self.program['projection'] = self.projection
        self.program['background_color'] = (0,0,0,0) # no background yet
        self.program['text_color'] = (0,0,0,0) # no text yet

        # self.texture = Texture2D(shape=(1000, 1000) + (3,))        
        # self.text_buffer = FrameBuffer(self.texture, RenderBuffer((1000, 1000)))
        self.texture = Texture2D(shape=self.size + (3,))        
        self.text_buffer = FrameBuffer(self.texture, RenderBuffer(self.size))

        self.program['texture'] = self.texture
        #self.program['text_color'] = self.tcolor # set the tcolor
        #self.program['background_color'] = self.bcolor # set the tcolor
        self.first = False # do not draw text until needed
        self.make_text('Default Text') # Create Empty text object
Пример #29
0
 def move_button(self, diff_x, diff_y, s):
     a = np.eye(4)
     scale(a, s)  ## scale by some 's'
     translate(a, diff_x, diff_y)  ## translate button by some 'x' and 'y'
     self.render.setPosition(a)
Пример #30
0
#         cam2.on_mouse_move(event)

fig = MyFigure()#visuals.Figure()
fig.size = 800, 400
fig.show()

#camera = visuals.NDCCamera(fig.world)
camera = visuals.PixelCamera(fig.world)

# Create two viewports, use the same world
vp1 = visuals.Viewport(fig.world)
vp2 = visuals.Viewport(fig.world)
vp1.world = vp2.world

# Put them next to each-other
transforms.scale(vp1.transform, 400, 400)
transforms.scale(vp2.transform, 400, 400)
transforms.translate(vp1.transform, 0)
transforms.translate(vp2.transform, 400, 0, 0)

# Create two cameras
cam0 = visuals.TwoDCamera(vp1.world)  # Placeholder camera
cam1 = visuals.TwoDCamera(cam0)
cam2 = visuals.TwoDCamera(cam0)

# Set limits of cam0, this is only to set position right, its fov is not used
cam0.xlim = -100, 500
cam0.ylim = -100, 500

# Set fov of cam1 and cam2, and translate both cameras a bit
cam1.fov = cam2.fov = 600, 600
Пример #31
0
    def __init__(self):
        '''Map drawable - contains the goddamn map
        '''
        self.projection = np.eye(4)
        self.view = np.eye(4)

        self.model = scale(np.eye(4), 0.6)
        orientation_vector = (0, 1, 0)
        unit_orientation_angle = np.array(orientation_vector) / np.linalg.norm(
            orientation_vector)
        rotate(self.model, -30, *unit_orientation_angle)
        translate(self.model, -2.2, -2.4, -9)

        height, width = 5.0, 5.0  # Meters

        # Add texture coordinates
        # Rectangle of height height
        self.vertices = np.array([
            [-width / 2, -height / 2, 0],
            [width / 2, -height / 2, 0],
            [width / 2, height / 2, 0],
            [-width / 2, height / 2, 0],
        ],
                                 dtype=np.float32)

        self.tex_coords = np.array([
            [0, 1],
            [1, 1],
            [1, 0],
            [0, 0],
        ],
                                   dtype=np.float32)

        self.indices = IndexBuffer([
            0,
            1,
            2,
            2,
            3,
            0,
        ])

        ###### TESTING
        self.position_lla = self.ecef2llh((738575.65, -5498374.10, 3136355.42))
        ###### TESTING

        self.map, self.ranges = self.cache_map(self.position_lla[:2])
        self.map, self.ranges = self.get_map(self.position_lla[:2])
        self.program = Program(self.frame_vertex_shader,
                               self.frame_frag_shader)

        default_map_transform = np.eye(4)

        self.program['vertex_position'] = self.vertices
        self.program['default_texcoord'] = self.tex_coords
        self.program['zoom'] = 1
        self.program['view'] = self.view
        self.program['model'] = self.model
        self.program['projection'] = self.projection

        self.program['map_transform'] = default_map_transform
        self.program['map_center'] = self.position_lla[:2]
        self.program['map_texture'] = self.map
        self.program['corners'] = self.ranges
        self.program['user_position'] = self.position_lla[:2]
        self.program['hide'] = 0
Пример #32
0
    def __init__(self, text, canvas, position=1, color=(0.1, 0.0, 0.7)):
        '''
        Give this the 
        - text to be written
        - main app.canvas
        - position (1-9, or which button position this should occupy)
        '''

        # State Controller
        State.register_button(position, text)

        self.position = position
        self.canvas = canvas
        self.projection = np.eye(4)
        self.view = np.eye(4)
        self.model = np.eye(4)
        height, width = 5.0, 15.0  # Meters

        orientation_vector = (1, 1, 0)
        unit_orientation_angle = np.array(orientation_vector) / np.linalg.norm(
            orientation_vector)

        scale_factor = 0.2
        lowest_button = -5.2
        midset = 0.2

        scale(self.model, scale_factor)
        yrotate(self.model, -60)
        # rotate(self.model, 30, *unit_orientation_angle)
        offset = (position * ((height + midset) * scale_factor))
        translate(self.model, -7.4, lowest_button + offset, -10)

        pixel_to_length = 10
        self.size = map(lambda o: pixel_to_length * o, [width, height])

        # Add texture coordinates
        # Rectangle of height height
        self.vertices = np.array([
            [-width / 2, -height / 2, 0],
            [width / 2, -height / 2, 0],
            [width / 2, height / 2, 0],
            [-width / 2, height / 2, 0],
        ],
                                 dtype=np.float32)

        self.tex_coords = np.array([
            [0, 0],
            [1, 0],
            [1, 1],
            [0, 1],
        ],
                                   dtype=np.float32)

        self.indices = IndexBuffer([
            0,
            1,
            2,
            2,
            3,
            0,
        ])

        self.program = Program(self.button_vertex_shader,
                               self.button_fragment_shader)

        self.program['vertex_position'] = self.vertices
        self.program['default_texcoord'] = self.tex_coords
        self.program['view'] = self.view
        self.program['model'] = self.model
        self.program['projection'] = self.projection
        self.program['background_color'] = color

        self.program['highlighted'] = 0

        # self.texture = Texture2D(shape=(1000, 1000) + (3,))
        # self.text_buffer = FrameBuffer(self.texture, RenderBuffer((1000, 1000)))
        self.texture = Texture2D(shape=(500, 1500) + (3, ))
        self.text_buffer = FrameBuffer(self.texture, RenderBuffer((500, 1500)))

        self.program['texture'] = self.texture
        self.text = text
        self.make_text(self.text)

        self.first = True
Пример #33
0
    def __init__(self):
        self.projection = np.eye(4)
        self.view       = np.eye(4)
        self.model      = np.eye(4)

        height, width   = 3.0, 6.0
        scale_factor    = 0.2
        x_offset        = -8
        y_offset        = 2
        pixel_to_length = 10
        color           = (1.0, 1.0, 1.0)

        scale(self.model, scale_factor)
        yrotate(self.model, -60)
        translate(self.model, x_offset, y_offset, -10)
        size = (int(height*100), int(width*100))

        self.vertices = np.array([
            [-width / 2, -height / 2, 0],
            [ width / 2, -height / 2, 0],
            [ width / 2,  height / 2, 0],
            [-width / 2,  height / 2, 0],
        ], dtype=np.float32)

        self.tex_coords = np.array([
            [0, 0],
            [1, 0],
            [1, 1],
            [0, 1],

        ], dtype=np.float32)

        self.indices = IndexBuffer([
            0, 1, 2,
            2, 3, 0,
        ])

        self.program = Program(self.battery_vertex_shader, self.battery_fragment_shader)

        self.texture = Texture2D(shape=size + (3,))
        self.text_buffer = FrameBuffer(self.texture, RenderBuffer(size))

        images = []
        images.append(Image.open(os.path.join(Paths.get_path_to_visar(), 'visar', 'images', 'battery', 'battery_low_color.png')))
        images.append(Image.open(os.path.join(Paths.get_path_to_visar(), 'visar', 'images', 'battery', 'battery_used_color.png')))
        images.append(Image.open(os.path.join(Paths.get_path_to_visar(), 'visar', 'images', 'battery', 'battery_full_color.png')))

        self.level_texture = {} # texture for each level

        for x in range(0, len(images)):
            default_image = images[x]
            default_image = default_image.rotate(-90)
            default_image = default_image.resize(size)
            default_image = default_image.transpose(Image.FLIP_TOP_BOTTOM)
            default_image_array = np.asarray(default_image)
            self.level_texture[x + 1] = default_image_array

        #default_image_array = imageio.imread(os.path.join(Paths.get_path_to_visar(), 'visar', 'images', 'battery', 'battery_full_color.png'))


        # self.default_tex = Texture2D(data=default_image_array)
        # self.default_tex = Texture2D(shape=size + (3,))
        # self.default_tex.set_data(self.level_texture[3])

        self.program['vertex_position']  = self.vertices
        self.program['default_texcoord'] = self.tex_coords
        self.program['view']             = self.view
        self.program['model']            = self.model
        self.program['projection']       = self.projection
        self.program['hide']             = 0

        # self.tex_program = Program(self.tex_vert_shader, self.battery_fragment_shader)

        # self.tex_program['vertex_position']  = self.vertices
        # self.tex_program['default_texcoord'] = self.tex_coords
        # self.tex_program['hide']             = 0
        # self.tex_program['texture']          = self.default_tex

        self.flag = True # flag to update the texture

        self.level = 3 # level of the battery 1 - 3
        full_middle_split = 75 # split between levels 2 and 3
        middle_low_split = 25 # split between levels 1 and 2
        fault_tolerance = 5
        self.full_lower = full_middle_split - fault_tolerance # lower limit for going from 3 to 2
        self.middle_upper = full_middle_split + fault_tolerance # upper limit for going from 2 to 3
        self.middle_lower = middle_low_split - fault_tolerance # lower limit for going from 2 to 1
        self.low_upper = middle_low_split + fault_tolerance # upper limit for going from 1 to 2
Пример #34
0
    def __init__(self, text, canvas, position=1, color=(0.1, 0.0, 0.7)):
        '''
        Give this the 
        - text to be written
        - main app.canvas
        - position (1-9, or which button position this should occupy)
        '''

        # State Controller
        State.register_button(position, text)

        self.position = position
        self.canvas = canvas
        self.projection = np.eye(4)
        self.view = np.eye(4)
        self.model = np.eye(4)
        height, width = 5.0, 15.0  # Meters

        orientation_vector = (1, 1, 0)
        unit_orientation_angle = np.array(orientation_vector) / np.linalg.norm(orientation_vector)

        scale_factor = 0.2
        lowest_button = -5.2 
        midset = 0.2

        scale(self.model, scale_factor)
        yrotate(self.model, -60)
        # rotate(self.model, 30, *unit_orientation_angle)
        offset = (position * ((height + midset) * scale_factor))
        translate(self.model, -7.4, lowest_button + offset, -10)

        pixel_to_length = 10
        self.size = map(lambda o: pixel_to_length * o, [width, height])

        # Add texture coordinates
        # Rectangle of height height
        self.vertices = np.array([
            [-width / 2, -height / 2, 0],
            [ width / 2, -height / 2, 0],
            [ width / 2,  height / 2, 0],
            [-width / 2,  height / 2, 0],
        ], dtype=np.float32)

        self.tex_coords = np.array([
            [0, 0],
            [1, 0],
            [1, 1],
            [0, 1],

        ], dtype=np.float32)

        self.indices = IndexBuffer([
            0, 1, 2,
            2, 3, 0,
        ])

        self.program = Program(self.button_vertex_shader, self.button_fragment_shader)

        self.program['vertex_position'] = self.vertices
        self.program['default_texcoord'] = self.tex_coords
        self.program['view'] = self.view
        self.program['model'] = self.model
        self.program['projection'] = self.projection
        self.program['background_color'] = color

        self.program['highlighted'] = 0

        # self.texture = Texture2D(shape=(1000, 1000) + (3,))        
        # self.text_buffer = FrameBuffer(self.texture, RenderBuffer((1000, 1000)))
        self.texture = Texture2D(shape=(500, 1500) + (3,))        
        self.text_buffer = FrameBuffer(self.texture, RenderBuffer((500, 1500)))

        self.program['texture'] = self.texture
        self.text = text
        self.make_text(self.text)

        self.first = True
Пример #35
0
 def scale_view(self, factor):
     scale(self.view, factor)
     self.set_view(self.view)
Пример #36
0
 def move_button(self, diff_x, diff_y, s):
   a = np.eye(4)
   scale(a, s) ## scale by some 's'
   translate(a, diff_x, diff_y) ## translate button by some 'x' and 'y'
   self.render.setPosition(a)
Пример #37
0
    def __init__(self):
        self.projection = np.eye(4)
        self.view = np.eye(4)
        self.model = np.eye(4)

        height, width = 3.0, 6.0
        scale_factor = 0.2
        x_offset = -8
        y_offset = 2
        pixel_to_length = 10
        color = (1.0, 1.0, 1.0)

        scale(self.model, scale_factor)
        yrotate(self.model, -60)
        translate(self.model, x_offset, y_offset, -10)
        size = (int(height * 100), int(width * 100))

        self.vertices = np.array([
            [-width / 2, -height / 2, 0],
            [width / 2, -height / 2, 0],
            [width / 2, height / 2, 0],
            [-width / 2, height / 2, 0],
        ],
                                 dtype=np.float32)

        self.tex_coords = np.array([
            [0, 0],
            [1, 0],
            [1, 1],
            [0, 1],
        ],
                                   dtype=np.float32)

        self.indices = IndexBuffer([
            0,
            1,
            2,
            2,
            3,
            0,
        ])

        self.program = Program(self.battery_vertex_shader,
                               self.battery_fragment_shader)

        self.texture = Texture2D(shape=size + (3, ))
        self.text_buffer = FrameBuffer(self.texture, RenderBuffer(size))

        images = []
        images.append(
            Image.open(
                os.path.join(Paths.get_path_to_visar(), 'visar', 'images',
                             'battery', 'battery_low_color.png')))
        images.append(
            Image.open(
                os.path.join(Paths.get_path_to_visar(), 'visar', 'images',
                             'battery', 'battery_used_color.png')))
        images.append(
            Image.open(
                os.path.join(Paths.get_path_to_visar(), 'visar', 'images',
                             'battery', 'battery_full_color.png')))

        self.level_texture = {}  # texture for each level

        for x in range(0, len(images)):
            default_image = images[x]
            default_image = default_image.rotate(-90)
            default_image = default_image.resize(size)
            default_image = default_image.transpose(Image.FLIP_TOP_BOTTOM)
            default_image_array = np.asarray(default_image)
            self.level_texture[x + 1] = default_image_array

        #default_image_array = imageio.imread(os.path.join(Paths.get_path_to_visar(), 'visar', 'images', 'battery', 'battery_full_color.png'))

        # self.default_tex = Texture2D(data=default_image_array)
        # self.default_tex = Texture2D(shape=size + (3,))
        # self.default_tex.set_data(self.level_texture[3])

        self.program['vertex_position'] = self.vertices
        self.program['default_texcoord'] = self.tex_coords
        self.program['view'] = self.view
        self.program['model'] = self.model
        self.program['projection'] = self.projection
        self.program['hide'] = 0

        # self.tex_program = Program(self.tex_vert_shader, self.battery_fragment_shader)

        # self.tex_program['vertex_position']  = self.vertices
        # self.tex_program['default_texcoord'] = self.tex_coords
        # self.tex_program['hide']             = 0
        # self.tex_program['texture']          = self.default_tex

        self.flag = True  # flag to update the texture

        self.level = 3  # level of the battery 1 - 3
        full_middle_split = 75  # split between levels 2 and 3
        middle_low_split = 25  # split between levels 1 and 2
        fault_tolerance = 5
        self.full_lower = full_middle_split - fault_tolerance  # lower limit for going from 3 to 2
        self.middle_upper = full_middle_split + fault_tolerance  # upper limit for going from 2 to 3
        self.middle_lower = middle_low_split - fault_tolerance  # lower limit for going from 2 to 1
        self.low_upper = middle_low_split + fault_tolerance  # upper limit for going from 1 to 2
Пример #38
0
 def scale_view(self, factor):
     scale(self.view, factor)
     self.set_view(self.view)
Пример #39
0
#         cam2.on_mouse_move(event)

# Create figure with one pixel camera
fig = MyFigure()  # scene.Figure()
fig.size = 800, 400
fig.show()
#camera = scene.NDCCamera(fig.viewvbox)
camera = scene.PixelCamera(fig.viewbox)

# Create two viewbox, use the same scene
vp1 = scene.ViewBox(fig.viewbox)
vp2 = scene.ViewBox(fig.viewbox)

# Put them next to each-other
transforms.scale(vp1.transform, 400, 400)
transforms.scale(vp2.transform, 400, 400)
transforms.translate(vp1.transform, 0)
transforms.translate(vp2.transform, 400, 0, 0)

# Create a world object to act as a container
# It is a child of both viewports
world = scene.Entity()
world.parents = vp1, vp2

# Create two cameras
cam0 = scene.TwoDCamera(world)  # Placeholder camera
cam1 = scene.TwoDCamera(cam0)
cam2 = scene.TwoDCamera(cam0)

# Set limits of cam0, this is only to set position right, its fov is not used
Пример #40
0
 def affine_transform(self) -> np.ndarray:
     w, h, d = self.volume.shape
     return (translate((-w / 2, -h / 2, -d / 2)) @ scale(
         (self.resolution_um, ) * 3)).T