Loading [MathJax]/extensions/tex2jax.js
Radium Engine  1.7.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
HandleToSkeleton.cpp
1#include <Core/Animation/HandleArray.hpp>
2#include <Core/Animation/Skeleton.hpp>
3#include <Core/Asset/HandleData.hpp>
4#include <Core/Asset/HandleToSkeleton.hpp>
5#include <Core/Containers/AlignedStdVector.hpp>
6#include <Core/CoreMacros.hpp>
7#include <Core/Types.hpp>
8#include <Core/Utils/ContainerIntrospectionInterface.hpp>
9#include <Core/Utils/Log.hpp>
10#include <Eigen/Core>
11#include <Eigen/Geometry>
12#include <map>
13#include <ostream>
14#include <set>
15#include <string>
16#include <vector>
17
18using namespace Ra::Core::Utils;
19
20namespace Ra {
21namespace Core {
22namespace Asset {
23
24namespace {
25// Recursive function to add bones
26void addBone( const uint parent, // index of parent bone
27 const uint dataID, // index in map
28 const Ra::Core::Asset::HandleData& data, // handle data
29 const Ra::Core::AlignedStdVector<Ra::Core::Vector2ui>& edgeList, // list of edges
30 std::vector<bool>& processed, // which ids have been processed
31 Core::Animation::Skeleton& skelOut ) // correspondance between bone name and bone idx
32{
33 if ( !processed[dataID] ) {
34 processed[dataID] = true;
35 const auto& dd = data.getComponentData()[dataID];
36 uint index = skelOut.addBone(
37 parent, dd.m_frame, Ra::Core::Animation::HandleArray::SpaceType::MODEL, dd.m_name );
38 for ( const auto& edge : edgeList ) {
39 if ( edge[0] == dataID ) {
40 addBone( index, edge[1], data, edgeList, processed, skelOut );
41 }
42 }
43 }
44}
45// function to add bones from a given root
46void addRoot( const uint dataID, // index in map
47 const Ra::Core::Asset::HandleData& data, // handle data
48 const Ra::Core::AlignedStdVector<Ra::Core::Vector2ui>& edgeList, // list of edges
49 std::vector<bool>& processed, // which ids have been processed
50 Core::Animation::Skeleton& skelOut ) // correspondance between bone name and bone idx
51{
52 if ( !processed[dataID] ) {
53 processed[dataID] = true;
54 const auto& dd = data.getComponentData()[dataID];
55 uint index = skelOut.addRoot( dd.m_frame, dd.m_name );
56 for ( const auto& edge : edgeList ) {
57 if ( edge[0] == dataID ) {
58 addBone( index, edge[1], data, edgeList, processed, skelOut );
59 }
60 }
61 }
62}
63} // namespace
64
65void createSkeleton( const Ra::Core::Asset::HandleData& data, Core::Animation::Skeleton& skelOut ) {
66 const uint size = data.getComponentDataSize();
67 auto component = data.getComponentData();
68
69 std::set<uint> root;
70 for ( uint i = 0; i < size; ++i ) {
71 root.insert( i );
72 }
73 std::set<uint> leaves = root;
74
75 auto edgeList = data.getEdgeData();
76 for ( const auto& edge : edgeList ) {
77 root.erase( edge[1] );
78 leaves.erase( edge[0] );
79 }
80
81 std::vector<bool> processed( size, false );
82 for ( const auto& r : root ) {
83 addRoot( r, data, edgeList, processed, skelOut );
84 }
85
86 if ( data.needsEndNodes() ) {
88 for ( uint i = 0; i < skelOut.size(); ++i ) {
89 boneNameMap[skelOut.getLabel( i )] = i;
90 }
91 for ( const auto& l : leaves ) {
92 const auto& dd = component[l];
93 if ( dd.m_weights.size() ) {
94 LOG( logDEBUG ) << "Adding end-bone at " << dd.m_name << ".";
95 skelOut.addBone( boneNameMap[dd.m_name],
96 data.getFrame().inverse() * dd.m_frame,
97 Ra::Core::Animation::HandleArray::SpaceType::MODEL,
98 dd.m_name + "_Ra_endBone" );
99 }
100 }
101 }
102}
103} // namespace Asset
104} // namespace Core
105} // namespace Ra
Label getLabel(const uint i) const
uint addRoot(const Transform &T=Transform::Identity(), const Label label="")
Definition Skeleton.cpp:142
uint size() const override
Definition Skeleton.hpp:46
uint addBone(const uint parent, const Transform &T=Transform::Identity(), const SpaceType MODE=SpaceType::LOCAL, const Label label="")
Definition Skeleton.cpp:149
const Core::AlignedStdVector< HandleComponentData > & getComponentData() const
const Core::AlignedStdVector< Core::Vector2ui > & getEdgeData() const
Core::Transform getFrame() const
T erase(T... args)
T insert(T... args)
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:4