LiangliangNan / Easy3D

A lightweight, easy-to-use, and efficient C++ library for processing and rendering 3D data
GNU General Public License v3.0
1.37k stars 245 forks source link

How can I translate a model? #154

Closed KenKenhehe closed 1 year ago

KenKenhehe commented 1 year ago

How can I change the position of a loaded model (Surfacemesh, pointcloud), for example, mesh.x += ..., mesh.y += ..., mesh.z += ...

LiangliangNan commented 1 year ago

That is correct. For any type of model, you can transform it similarly. Below is an example:

    template<typename MODEL>
    void translate(MODEL* model, const vec3& p) {
        auto points = model->template get_vertex_property<vec3>("v:point");
        for (auto v : model->vertices())
            points[v] -= p;
    }
KenKenhehe commented 1 year ago

Thanks for getting back, what about rotation?

LiangliangNan commented 1 year ago

It is similar, e.g.,

    template<typename MODEL>
    void translate(MODEL* model, const mat4& R) {   // R is the rotation matrix
        auto points = model->template get_vertex_property<vec3>("v:point");
        for (auto v : model->vertices())
            points[v] = R * points[v];                                  // apply R to every vertex
    }

If you are visualizing the model using the Easy3D viewer, don't forget to call the following functions to update the viewer:

    model->renderer()->update();
    viewer->update();
KenKenhehe commented 1 year ago

Hi, thanks for the answers.

Is there a easy way to convert euler angles(x, y, z) to a rotation matrix?

LiangliangNan commented 1 year ago

See here: https://github.com/LiangliangNan/Easy3D/blob/1d1460cd179dbbb2293aa6c5828859697f08f36f/easy3d/core/mat.h#L2043