From 6d2fbf042170fee889424da428860e9a1ad44439 Mon Sep 17 00:00:00 2001 From: hodasemi Date: Sat, 9 Jun 2018 10:16:18 +0200 Subject: [PATCH] Create raw bone hierarchy --- .vscode/settings.json | 9 ++++- CGII/src/IKViewer.cpp | 92 +++++++++++++++++++++++++++++++++---------- CGII/src/IKViewer.h | 33 +++++++--------- CGII/src/main.cpp | 12 +++--- 4 files changed, 100 insertions(+), 46 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index c4b72bd..e13998e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -53,7 +53,14 @@ "utility": "cpp", "variant": "cpp", "algorithm": "cpp", - "new": "cpp" + "new": "cpp", + "deque": "cpp", + "list": "cpp", + "unordered_map": "cpp", + "unordered_set": "cpp", + "vector": "cpp", + "__config": "cpp", + "__nullptr": "cpp" }, "editor.formatOnSave": true } \ No newline at end of file diff --git a/CGII/src/IKViewer.cpp b/CGII/src/IKViewer.cpp index 97f9144..bef62a9 100644 --- a/CGII/src/IKViewer.cpp +++ b/CGII/src/IKViewer.cpp @@ -1,5 +1,5 @@ -// This source code is property of the Computer Graphics and Visualization -// chair of the TU Dresden. Do not distribute! +// This source code is property of the Computer Graphics and Visualization +// chair of the TU Dresden. Do not distribute! // Copyright (C) CGV TU Dresden - All Rights Reserved // #include "IKViewer.h" @@ -12,30 +12,79 @@ #include #include -IKViewer::IKViewer(DataStore* data) +IKViewer::IKViewer(DataStore *data) : node("IK Viewer"), data(data), modifying(false), target_position(0, 0, 0, 1), max_iterations(20) { connect(data->endeffector_changed, this, &IKViewer::endeffector_changed); connect(data->base_changed, this, &IKViewer::base_changed); } -void IKViewer::endeffector_changed(Bone* b) +void IKViewer::endeffector_changed(Bone *b) { + std::cout << "endeffector changed!" << std::endl; + calculate_kinematic_chain(data->get_base(), data->get_endeffector()); post_redraw(); } -void IKViewer::base_changed(Bone* b) +void IKViewer::base_changed(Bone *b) { + std::cout << "base changed!" << std::endl; + calculate_kinematic_chain(data->get_base(), data->get_endeffector()); post_redraw(); } -void IKViewer::calculate_kinematic_chain(Bone* base, Bone* endeffector) +void IKViewer::calculate_kinematic_chain(Bone *base, Bone *endeffector) { + std::cout << "calculate_kinematic_chain" << std::endl; + + if (base) + std::cout << "Base bone: " << base->get_name() << std::endl; + else + { + std::cout << "no base set" << std::endl; + std::cout << std::endl; + return; + } + + if (endeffector) + std::cout << "End bone: " << endeffector->get_name() << std::endl; + else + { + std::cout << "no endeffector set" << std::endl; + std::cout << std::endl; + return; + } + + std::cout << std::endl; + /*Task 3.1: Calculate kinematic chain*/ + + // get list of bones in order of endeffector to base + std::vector bones; + Bone *current_bone = endeffector; + + while (1) + { + bones.emplace_back(current_bone); + + // break if we reached base bone + if (current_bone == base) + break; + + // set next bone in hierarchy + current_bone = current_bone->get_parent(); + } + + for (Bone *bone : bones) + { + // TODO: calculate and add transforms to queue + std::shared_ptr transform; + kinematic_chain.emplace_front(transform); + } } void IKViewer::optimize() @@ -44,7 +93,7 @@ void IKViewer::optimize() data->dof_changed_by_ik = true; auto skeleton_size = (data->get_skeleton()->getMax() - data->get_skeleton()->getMin()); - float distance_threshold = 0.0001f * std::max({ skeleton_size.x(), skeleton_size.y(), skeleton_size.z() }); + float distance_threshold = 0.0001f * std::max({skeleton_size.x(), skeleton_size.y(), skeleton_size.z()}); //split the current matrix in: // before_dof -> dof -> after_dof @@ -81,7 +130,7 @@ void IKViewer::set_target_position_2d(int x, int y) int width = ctx->get_width(); int height = ctx->get_height(); - float proj_position[4] = { (2.0f * x) / width - 1.0f, (-2.0f * y) / height + 1.0f, z, 1.0f }; + float proj_position[4] = {(2.0f * x) / width - 1.0f, (-2.0f * y) / height + 1.0f, z, 1.0f}; auto unprojected = inv_mvp * cgv::math::vec(4, proj_position); unprojected *= 1.0f / unprojected.w(); @@ -95,19 +144,21 @@ void IKViewer::set_target_position_2d(int x, int y) post_redraw(); } - -bool IKViewer::handle(event& e) +bool IKViewer::handle(event &e) { - if (e.get_kind() == EID_MOUSE) { - cgv::gui::mouse_event me = (cgv::gui::mouse_event&) e; + if (e.get_kind() == EID_MOUSE) + { + cgv::gui::mouse_event me = (cgv::gui::mouse_event &)e; - switch (me.get_action()) { + switch (me.get_action()) + { case MA_PRESS: - if (me.get_button() == MB_LEFT_BUTTON && me.get_modifiers() == EM_CTRL) { + if (me.get_button() == MB_LEFT_BUTTON && me.get_modifiers() == EM_CTRL) + { modifying = true; set_target_position_2d(me.get_x(), me.get_y()); return true; - } + } break; case MA_RELEASE: modifying = false; @@ -118,24 +169,25 @@ bool IKViewer::handle(event& e) set_target_position_2d(me.get_x(), me.get_y()); return true; } - break; - default: break; + break; + default: + break; } } return false; } -void IKViewer::stream_help(std::ostream& os) +void IKViewer::stream_help(std::ostream &os) { } -void IKViewer::draw(cgv::render::context& ctx) +void IKViewer::draw(cgv::render::context &ctx) { if (!data->get_skeleton()) return; auto skeleton_size = (data->get_skeleton()->getMax() - data->get_skeleton()->getMin()); - float scale = 0.2f * std::max({ skeleton_size.x(), skeleton_size.y(), skeleton_size.z() }); + float scale = 0.2f * std::max({skeleton_size.x(), skeleton_size.y(), skeleton_size.z()}); glBegin(GL_LINES); diff --git a/CGII/src/IKViewer.h b/CGII/src/IKViewer.h index 40ccdfa..36409ec 100644 --- a/CGII/src/IKViewer.h +++ b/CGII/src/IKViewer.h @@ -1,5 +1,5 @@ -// This source code is property of the Computer Graphics and Visualization -// chair of the TU Dresden. Do not distribute! +// This source code is property of the Computer Graphics and Visualization +// chair of the TU Dresden. Do not distribute! // Copyright (C) CGV TU Dresden - All Rights Reserved // #pragma once @@ -25,23 +25,21 @@ using namespace cgv::utils; class IKViewer : public node, public drawable, public provider, public event_handler { -private: - DataStore* data; - + private: + DataStore *data; + void set_target_position_2d(int x, int y); - void endeffector_changed(Bone*); - void base_changed(Bone*); + void endeffector_changed(Bone *); + void base_changed(Bone *); - void calculate_kinematic_chain(Bone* base, Bone* endeffector); + void calculate_kinematic_chain(Bone *base, Bone *endeffector); - bool modifying; //Specifies if the user is currently modifying the IK target position + bool modifying; //Specifies if the user is currently modifying the IK target position Vec4 target_position; Mat4 current_endeffector_matrix; //transform from base to endeffector - Mat4 current_base_matrix; //transform from global origin to base - - + Mat4 current_base_matrix; //transform from global origin to base unsigned int max_iterations; @@ -49,16 +47,15 @@ private: void optimize(); -public: + public: // The constructor of this class - IKViewer(DataStore*); + IKViewer(DataStore *); - bool handle(cgv::gui::event& e); - void stream_help(std::ostream& os); + bool handle(cgv::gui::event &e); + void stream_help(std::ostream &os); // Create the gui elements void create_gui(); // Draw the scene - void draw(context& c); + void draw(context &c); }; - diff --git a/CGII/src/main.cpp b/CGII/src/main.cpp index fd166e0..e6e8b06 100644 --- a/CGII/src/main.cpp +++ b/CGII/src/main.cpp @@ -1,5 +1,5 @@ -// This source code is property of the Computer Graphics and Visualization -// chair of the TU Dresden. Do not distribute! +// This source code is property of the Computer Graphics and Visualization +// chair of the TU Dresden. Do not distribute! // Copyright (C) CGV TU Dresden - All Rights Reserved // // The main file of the plugin. It defines a global register variable @@ -16,17 +16,15 @@ using namespace cgv::base; struct Initializer { - DataStore* data; + DataStore *data; Initializer() - { - - + { data = new DataStore(); register_object(base_ptr(new SkeletonViewer(data)), ""); - /*register_object(base_ptr(new IKViewer(data)), "");*/ + register_object(base_ptr(new IKViewer(data)), ""); /*register_object(base_ptr(new SkinnedMeshViewer(data)), "");*/ }