Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show camera as mesh #262

Merged
merged 1 commit into from Nov 29, 2018
Merged

Show camera as mesh #262

merged 1 commit into from Nov 29, 2018

Conversation

wkentaro
Copy link
Contributor

@wkentaro wkentaro commented Nov 26, 2018

#!/usr/bin/env python

import numpy as np
import trimesh
import trimesh.transformations as tf


trimesh.util.attach_to_log(level='WARNING')


def get_homography_rpy(roll, pitch, yaw, distance, center):
    translation = tf.translation_matrix(center)
    translation[2][3] += distance  # z from center
    rotation = (
        tf.rotation_matrix(roll, [1, 0, 0], point=center) @
        tf.rotation_matrix(pitch, [0, 1, 0], point=center) @
        tf.rotation_matrix(yaw, [0, 0, 1], point=center)
    )
    transform = translation @ rotation
    return transform


def get_homography_lookat(eye, lookat):
    # both are positions
    assert len(eye) == 3
    assert len(lookat) == 3


if __name__ == '__main__':
    extents = (0.5, 0.5, 0.5)
    box = trimesh.creation.box(extents)
    box.visual.face_colors = [255, 0, 0, 127]

    scene = trimesh.Scene()
    scene.add_geometry(box, node_name='box')

    scene.add_geometry(trimesh.creation.axis())

    roll, pitch, yaw = 0, 0, np.deg2rad(45)
    distance = 0.9
    center = (0, 0, 0)
    transform = get_homography_rpy(roll, pitch, yaw, distance, center)
    print('Using RPY (Red)')
    print('Distance:', distance)
    print('RPY:', (roll, pitch, yaw))

    scene_camera = scene.copy()
    camera = trimesh.scene.Camera(
        resolution=(320, 240),
        fov=(60, 45),
        transform=transform
    )
    scene_camera.set_camera(camera=camera)
    scene_camera.show(start_loop=False)

    # this transform is necessary to get correct image
    transform = transform @ tf.rotation_matrix(np.deg2rad(-180), [1, 0, 0])
    camera.transform = transform
    scene.add_geometry(camera.as_meshes())
    scene.show()

screen shot 2018-11-26 at 22 54 44

@wkentaro
Copy link
Contributor Author

wkentaro commented Nov 26, 2018

# this transform is necessary to get correct image
transform = transform @ tf.rotation_matrix(np.deg2rad(-180), [1, 0, 0])

Actually this is necessary because z axis is other way around in trimesh.

@coveralls
Copy link

coveralls commented Nov 26, 2018

Coverage Status

Coverage increased (+0.05%) to 83.934% when pulling 29ae6ce on wkentaro:camera_mesh into 9c27e7d on mikedh:master.

@mikedh
Copy link
Owner

mikedh commented Nov 27, 2018

Nice! I think this probably makes most sense intrimesh.creation, as camera_marker or something:

def camera_marker(camera, marker_height):
    """
    Create a visual marker for a camera object, including an axis marker and FOV indicator. 

    Parameters
    ---------------
    camera : trimesh.scene.Camera
        Camera object with FOV and transform defined
    marker_height : float
        How far along the camera Z should FOV indicators be
 
    Returns
    ------------
    meshes : list
       Contains Trimesh and Path3D objects which can be visualized
    """
  • imports should probably be relative (i.e. from .path import load_path)
  • could you add a docstring?
  • I think focal_length is a little overloaded; I changed it to "marker_height" or something. Probably also want to make origin radius always relative to this.
  • Is the PointCloud necessary? I don't see them in your screenshot.

@wkentaro
Copy link
Contributor Author

Updated example:

#!/usr/bin/env python

import numpy as np
import trimesh
import trimesh.transformations as tf


trimesh.util.attach_to_log(level='WARNING')


def get_homography_rpy(roll, pitch, yaw, distance, center):
    translation = tf.translation_matrix(center)
    translation[2][3] += distance  # z from center
    rotation = (
        tf.rotation_matrix(roll, [1, 0, 0], point=center) @
        tf.rotation_matrix(pitch, [0, 1, 0], point=center) @
        tf.rotation_matrix(yaw, [0, 0, 1], point=center)
    )
    transform = translation @ rotation
    return transform


def get_homography_lookat(eye, lookat):
    # both are positions
    assert len(eye) == 3
    assert len(lookat) == 3


if __name__ == '__main__':
    extents = (0.5, 0.5, 0.5)
    box = trimesh.creation.box(extents)
    box.visual.face_colors = [255, 0, 0, 127]

    scene = trimesh.Scene()
    scene.add_geometry(box, node_name='box')

    scene.add_geometry(trimesh.creation.axis())

    roll, pitch, yaw = 0, 0, np.deg2rad(45)
    distance = 0.9
    center = (0, 0, 0)
    transform = get_homography_rpy(roll, pitch, yaw, distance, center)
    print('Using RPY (Red)')
    print('Distance:', distance)
    print('RPY:', (roll, pitch, yaw))

    scene_camera = scene.copy()
    camera = trimesh.scene.Camera(
        resolution=(320, 240),
        fov=(60, 45),
        transform=transform
    )
    scene_camera.set_camera(camera=camera)
    scene_camera.show(start_loop=False)

    # this transform is necessary to get correct image
    transform = transform @ tf.rotation_matrix(np.deg2rad(-180), [1, 0, 0])
    camera.transform = transform
    mesh = trimesh.creation.camera_marker(camera=camera)
    scene.add_geometry(mesh)
    scene.show()

@wkentaro
Copy link
Contributor Author

I addressed all of your comments. Could you review again?

@mikedh mikedh changed the base branch from master to marker_merge November 29, 2018 04:59
@mikedh mikedh merged commit 54101fd into mikedh:marker_merge Nov 29, 2018
mikedh added a commit that referenced this pull request Nov 29, 2018
@mikedh
Copy link
Owner

mikedh commented Nov 29, 2018

Awesome! Merged to master in ef90ebc and should release as soon as the build finishes.

I made some minor changes, the main thing was moving the import into the function and a try/except ImportError block to keep path a soft dependency. I also changed the load_path to a single call by stacking into one array of line segments.

Thanks for the PR's!! It's great to get your work into the master branch and releases 😄

@wkentaro wkentaro deleted the camera_mesh branch November 29, 2018 13:11
@wkentaro
Copy link
Contributor Author

Thanks for your help!

lejafar added a commit to oqton/trimesh that referenced this pull request Dec 18, 2018
…ctm-support

* upstream/master: (60 commits)
  add test failing any function returning NaN or inf
  fix strict DXF test on python2
  DXF export whitespace fix
  bump for mikedh#268
  update radius calculation fallback logic in nsphere
  further nsphere speedup
  test for remove_unreferenced
  Fix missing arg in call to unique_bincount
  in docker builds check certs and sha256
  add checksums to docker builds and fix align_vectors test
  freeze miniconda version
  align_vectors to more robust method and tests
  bump for mikedh#265
  use unique_bincount and unify slice_plane with slice_plane
  add unit test for unique_bincount
  creation docstring
  add unique_bincount
  slice with multiple planes and performance tweaks
  readme, comments, and bump for mikedh#262 and mikedh#264
  wrap load_path import in camera marker
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants