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

Enhancement: Initialize SE3 matrix with look_at() function #37

Open
olaals opened this issue Dec 1, 2021 · 1 comment
Open

Enhancement: Initialize SE3 matrix with look_at() function #37

olaals opened this issue Dec 1, 2021 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@olaals
Copy link

olaals commented Dec 1, 2021

Feature description

I was wondering if it would make sense to add a look_at() function for this library for initializing an SE3 transformation matrix. I personally use this when positioning a camera etc. where I want the camera at an exact position, look at a specific point, and define which axis is up. This makes defining the pose of the camera very easy. I use an implementation of this function in mitsuba2, where the parameters are

  • origin: defines the translation part of the matrix
  • target: the target point the z-axis should point towards
  • up: the direction which is perpendicular with the x-axis of the transform and the direction of the vector from the origin to the target

Examples

Below are a couple of examples of how the function works in Mitsuba2

Example 1

T = look_at(origin=[1,2,3], target=[0,0,0], up=[0,0,1])
print(T)
[[0.894427, -0.358569, -0.267261, 1],
 [-0.447214, -0.717137, -0.534522, 2],
 [0, 0.597614, -0.801784, 3],
 [0, 0, 0, 1]]

Example 2

T = look_at(origin=[0,0,0], target=[3,0,0], up=[0,1,0])
print(T)
[[0, -0, 1, 0],
 [0, 1, 0, 0],
 [-1, 0, 0, 0],
 [0, 0, 0, 1]]

Mitsuba C++ implementation

Source: https://github.com/mitsuba-renderer/mitsuba2/blob/ab5a49d4199a1b08d4d6557dfe6b0336fff4cfdf/include/mitsuba/core/transform.h

    static Transform look_at(const Point<Float, 3> &origin,
                             const Point<Float, 3> &target,
                             const Vector<Float, 3> &up) {
        using Vector3 = Vector<Float, 3>;

        Vector3 dir = normalize(target - origin);
        dir = normalize(dir);
        Vector3 left = normalize(cross(up, dir));

        Vector3 new_up = cross(dir, left);

        Matrix result = Matrix::from_cols(
            concat(left, Scalar(0)),
            concat(new_up, Scalar(0)),
            concat(dir, Scalar(0)),
            concat(origin, Scalar(1))
        );

        Matrix inverse = Matrix::from_rows(
            concat(left, Scalar(0)),
            concat(new_up, Scalar(0)),
            concat(dir, Scalar(0)),
            Vector<Float, 4>(0.f, 0.f, 0.f, 1.f)
        );

        inverse[3] = inverse * concat(-origin, Scalar(1));

        return Transform(result, transpose(inverse));
    }

Generalization

The Mitsuba implementation only works for pointing the z-axis at a specific direction defined by the direction of the vector between the origin and the target. The up-axis is also predefined to specify a specific axis in the resulting transformation matrix. For generalization of the function, it could make sense to specify which axes should point toward to target, and which axis is up. This will however complicate the function further. One could split the functionality into three functions for example:

SE3.x_look_at(origin, target, up)
SE3.y_look_at(origin, target, up)
SE3.z_look_at(origin, target, up)

Does this makes sense for anyone else, or would it bloat the code and documentation?

@olaals olaals changed the title Initialize SE3 matrix with look_at() function Enhancement: Initialize SE3 matrix with look_at() function Dec 1, 2021
@petercorke
Copy link
Collaborator

That's a nice idea. Since it works like a constructor it should start with a capital letter for consistency. We could have an additional string argument, axis, which perhaps defaults to 'z' to handle your general case. So, how about SE3.LookAt(origin, target, up, axis)?

@petercorke petercorke added the enhancement New feature or request label Jan 31, 2022
@petercorke petercorke self-assigned this Jan 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants