Implement read pinocchio file
This commit is contained in:
parent
da0b6fcfb4
commit
cac81a40d9
2 changed files with 51 additions and 5 deletions
|
@ -275,8 +275,13 @@ Bone *find_root(Bone *bone)
|
|||
|
||||
for (int i = 0; i < bone->childCount(); i++)
|
||||
{
|
||||
return find_root(bone->child_at(i));
|
||||
Bone *new_root = find_root(bone->child_at(i));
|
||||
|
||||
if (new_root != nullptr)
|
||||
return new_root;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void Skeleton::get_bones(Bone *bone, std::vector<Bone *> &bone_list)
|
||||
|
@ -292,7 +297,7 @@ void Skeleton::get_bones(Bone *bone, std::vector<Bone *> &bone_list)
|
|||
}
|
||||
}
|
||||
|
||||
int Skeleton::find_bone(Bone *bone, std::vector<Bone *> &bone_list)
|
||||
int Skeleton::find_index(Bone *bone, std::vector<Bone *> &bone_list)
|
||||
{
|
||||
for (int i = 0; i < bone_list.size(); i++)
|
||||
{
|
||||
|
@ -505,13 +510,13 @@ void Skeleton::write_pinocchio_file(const std::string &filename)
|
|||
{
|
||||
//float len = length(bone->get_bone_local_tip_position());
|
||||
|
||||
int parent_index = find_bone(bone->get_parent(), bone_list);
|
||||
int parent_index = find_index(bone->get_parent(), bone_list);
|
||||
|
||||
if (parent_index == -1)
|
||||
{
|
||||
if (bone != new_root)
|
||||
{
|
||||
parent_index = find_bone(new_root, bone_list);
|
||||
parent_index = find_index(new_root, bone_list);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -528,6 +533,17 @@ void Skeleton::write_pinocchio_file(const std::string &filename)
|
|||
o.close();
|
||||
}
|
||||
|
||||
Bone *get_bone(int i, std::vector<Bone *> &bone_list)
|
||||
{
|
||||
if (i < 0)
|
||||
return nullptr;
|
||||
|
||||
if (i >= bone_list.size())
|
||||
return nullptr;
|
||||
|
||||
return bone_list[i];
|
||||
}
|
||||
|
||||
void Skeleton::read_pinocchio_file(std::string filename)
|
||||
{
|
||||
std::ifstream o;
|
||||
|
@ -539,6 +555,36 @@ void Skeleton::read_pinocchio_file(std::string filename)
|
|||
#endif
|
||||
if (o)
|
||||
{
|
||||
std::vector<Bone *> bone_list;
|
||||
get_bones(root, bone_list);
|
||||
|
||||
std::string line;
|
||||
|
||||
while (std::getline(o, line))
|
||||
{
|
||||
std::istringstream iss(line);
|
||||
|
||||
int bone_id, parent_id;
|
||||
float x, y, z;
|
||||
|
||||
iss >> bone_id >> x >> y >> z >> parent_id;
|
||||
|
||||
//std::cout << bone_id << " " << x << " " << y << " " << z << " " << parent_id << std::endl;
|
||||
|
||||
Vec3 position(x, y, z);
|
||||
Bone *current_bone = get_bone(bone_id, bone_list);
|
||||
|
||||
if (current_bone == nullptr)
|
||||
abort();
|
||||
|
||||
current_bone->set_length(position.normalize());
|
||||
current_bone->set_direction_in_world_space(position);
|
||||
|
||||
// TODO: add new orientation
|
||||
|
||||
current_bone->calculate_matrices();
|
||||
}
|
||||
|
||||
/*Task 4.1: Read Pinocchio file */
|
||||
}
|
||||
|
||||
|
|
|
@ -46,7 +46,7 @@ class Skeleton : public IHasBoundingBox
|
|||
void postprocess(Bone *node, const Vec3 &global_position);
|
||||
|
||||
void get_bones(Bone *bone, std::vector<Bone *> &bone_list);
|
||||
int find_bone(Bone *bone, std::vector<Bone *> &bone_list);
|
||||
int find_index(Bone *bone, std::vector<Bone *> &bone_list);
|
||||
|
||||
std::map<const std::string, Bone *> bones;
|
||||
};
|
Loading…
Reference in a new issue