31#include <easy3d/renderer/drawable_triangles.h>
32#include <easy3d/renderer/manipulator.h>
33#include <easy3d/renderer/manipulated_frame.h>
34#include <easy3d/renderer/renderer.h>
35#include <easy3d/util/resource.h>
36#include <easy3d/algo/collider.h>
41TutorialCollisionDetection::TutorialCollisionDetection(
const std::string &title)
43 , model0_color_(0.8f, 1.0f, 0.8f)
44 , model1_color_(0.8f, 0.8f, 1.0f)
46 auto m0 =
add_model(resource::directory() +
"/data/bunny.ply");
47 auto m1 =
add_model(resource::directory() +
"/data/mannequin.ply");
55 mesh0->add_face_property<
vec3>(
"face_color", model0_color_);
56 mesh0->renderer()->get_triangles_drawable(
"faces")->set_coloring(easy3d::State::COLOR_PROPERTY, easy3d::State::FACE,
"face_color");
59 mesh1->add_face_property<
vec3>(
"face_color", model1_color_);
60 mesh1->renderer()->get_triangles_drawable(
"faces")->set_coloring(easy3d::State::COLOR_PROPERTY, easy3d::State::FACE,
"face_color");
62 collider_ =
new Collider(mesh0, mesh1);
66 timer_.set_interval(50, [
this]()->
void {
67 static std::mutex mutex;
68 std::lock_guard<std::mutex> guard(mutex);
69 static float angle = 0.0f;
71 auto T = mat4::rotation(
vec3(0, 0, 1),
angle) * models_[0]->manipulator()->frame()->matrix();
72 models_[0]->manipulator()->frame()->setFromMatrix(T);
77 LOG(WARNING) <<
"not all the two meshes have been loaded";
80 "--------------------- Collision Viewer usage ----------------------\n"
81 "On start, you will see an animation with highlighted intersecting \n"
82 "faces. Press 'space' to pause/resume the animation. \n"
83 "When the animation is paused, you can manipulate the bunny model \n"
84 "(using your mouse) to perform real-time collection detection: \n"
85 " - ALT + left button: rotate bunny model \n"
86 " - ALT + right button: translate bunny model \n"
87 "------------------------------------------------------------------ \n";
91TutorialCollisionDetection::~TutorialCollisionDetection() {
97bool TutorialCollisionDetection::mouse_drag_event(
int x,
int y,
int dx,
int dy,
int button,
int modifiers) {
98 if (collider_ && models().size() == 2 && modifiers == MODIF_ALT && timer_.is_paused()) {
99 auto manipulator = models_[0]->manipulator();
100 if (button == BUTTON_LEFT)
101 manipulator->frame()->action_rotate(x, y, dx, dy, camera_, ManipulatedFrame::NONE);
102 else if (button == BUTTON_RIGHT)
103 manipulator->frame()->action_translate(x, y, dx, dy, camera_, ManipulatedFrame::NONE);
111 return Viewer::mouse_drag_event(x, y, dx, dy, button, modifiers);
115bool TutorialCollisionDetection::key_press_event(
int key,
int modifiers) {
116 if (key == KEY_SPACE) {
117 if (timer_.is_paused())
124 return Viewer::key_press_event(key, modifiers);
128void TutorialCollisionDetection::detect() {
129 const auto pairs = collider_->detect(models_[0]->manipulator()->matrix(), mat4::identity());
137 auto mesh0_colors =
dynamic_cast<SurfaceMesh *
>(models_[0])->get_face_property<vec3>(
"face_color");
138 auto mesh1_colors =
dynamic_cast<SurfaceMesh *
>(models_[1])->get_face_property<vec3>(
"face_color");
141 for (
const auto &pair : pairs) {
142 mesh0_colors[pair.first] =
vec3(1.0f, 0.0f, 0.0f);
143 mesh1_colors[pair.second] =
vec3(1.0f, 0.0f, 0.0f);
145 models_[0]->renderer()->update();
146 models_[1]->renderer()->update();
A manipulator is for manipulation of an object.
Definition: manipulator.h:62
void set_manipulator(Manipulator *manip)
Attaches a manipulator to this model.
Definition: model.h:102
A halfedge data structure for polygonal meshes of 2-manifold.
Definition: surface_mesh.h:52
unsigned int n_faces() const
returns number of faces in the mesh
Definition: surface_mesh.h:1076
SurfaceMesh & assign(const SurfaceMesh &rhs)
assign rhs to *this. does not copy custom properties.
Definition: surface_mesh.cpp:149
virtual Model * add_model(const std::string &file_name, bool create_default_drawables=true)
Add a model from a file to the viewer to be visualized. On success, the viewer will be in charge of t...
Definition: viewer.cpp:1204
double angle(const Vec &a, const Vec &b)
Computes angle between two (un-normalized) vectors.
Definition: types.h:261