| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #include "experimental/usd/kinematic_tree.h" |
|
|
| #include <algorithm> |
| #include <deque> |
| #include <map> |
| #include <memory> |
| #include <set> |
| #include <utility> |
| #include <vector> |
|
|
| #include <mujoco/mujoco.h> |
| #include <pxr/usd/sdf/path.h> |
| #include <pxr/usd/usdPhysics/joint.h> |
|
|
| namespace mujoco { |
| namespace usd { |
|
|
| bool GetJointBodies(const pxr::UsdPhysicsJoint& joint, |
| const pxr::SdfPath& default_prim_path, pxr::SdfPath* from, |
| pxr::SdfPath* to) { |
| pxr::SdfPathVector body1_paths; |
| joint.GetBody1Rel().GetTargets(&body1_paths); |
| if (body1_paths.empty()) { |
| mju_warning("Joint %s does not have body1 rel. Skipping.", |
| joint.GetPath().GetAsString().c_str()); |
| return false; |
| } else if (body1_paths.size() > 1) { |
| mju_warning("Joint %s has multiple body1 rels. Skipping.", |
| joint.GetPath().GetAsString().c_str()); |
| return false; |
| } |
| *to = body1_paths[0]; |
|
|
| pxr::SdfPathVector body0_paths; |
| joint.GetBody0Rel().GetTargets(&body0_paths); |
| if (body0_paths.size() > 1) { |
| mju_warning("Joint %s has multiple body0 rels. Skipping.", |
| joint.GetPath().GetAsString().c_str()); |
| return false; |
| } |
| |
| |
| if (body0_paths.empty() || body0_paths[0] == default_prim_path) { |
| *from = pxr::SdfPath(); |
| } else { |
| *from = body0_paths[0]; |
| } |
| return true; |
| } |
|
|
| std::unique_ptr<KinematicNode> BuildKinematicTree( |
| const std::vector<pxr::UsdPhysicsJoint>& joints, |
| const std::vector<pxr::SdfPath>& all_body_paths, |
| const pxr::SdfPath& default_prim_path) { |
| std::map<pxr::SdfPath, std::vector<pxr::SdfPath>> children_map; |
| std::map<pxr::SdfPath, pxr::SdfPath> parent_map; |
| std::map<std::pair<pxr::SdfPath, pxr::SdfPath>, pxr::SdfPath> |
| edge_to_joint_map; |
| std::set<pxr::SdfPath> all_nodes(all_body_paths.begin(), |
| all_body_paths.end()); |
|
|
| for (const auto& joint : joints) { |
| pxr::SdfPath from, to; |
| if (!GetJointBodies(joint, default_prim_path, &from, &to)) { |
| continue; |
| } |
|
|
| auto edge_key = std::make_pair(from, to); |
| auto it = edge_to_joint_map.find(edge_key); |
| if (it == edge_to_joint_map.end()) { |
| edge_to_joint_map[edge_key] = joint.GetPath(); |
| } else { |
| mju_warning( |
| "Multiple explicit joints defined between body %s and body %s. " |
| "Joint1: %s, Joint2: %s. Keeping the first one found: %s", |
| (from.IsEmpty() ? "<worldbody>" : from.GetString()).c_str(), |
| to.GetString().c_str(), it->second.GetString().c_str(), |
| joint.GetPath().GetString().c_str(), it->second.GetString().c_str()); |
| continue; |
| } |
|
|
| if (from == to) { |
| mju_error("Self-loop detected at node %s", to.GetString().c_str()); |
| return nullptr; |
| } |
| if (parent_map.count(to)) { |
| mju_error("Node %s has multiple parents ('%s' and '%s').", |
| to.GetString().c_str(), parent_map.at(to).GetString().c_str(), |
| from.GetString().c_str()); |
| return nullptr; |
| } |
| children_map[from].push_back(to); |
| parent_map[to] = from; |
| all_nodes.insert(from); |
| all_nodes.insert(to); |
| } |
|
|
| |
| for (auto& [_, children] : children_map) { |
| std::sort( |
| children.begin(), children.end(), |
| [&v = all_body_paths](const auto& a, const auto& b) { |
| return std::distance(v.begin(), std::find(v.begin(), v.end(), a)) < |
| std::distance(v.begin(), std::find(v.begin(), v.end(), b)); |
| }); |
| } |
|
|
| |
| auto world_root = std::make_unique<KinematicNode>(); |
| std::map<pxr::SdfPath, KinematicNode*> node_map; |
| node_map[pxr::SdfPath()] = world_root.get(); |
|
|
| |
| |
| std::deque<pxr::SdfPath> q; |
|
|
| |
| |
| for (const auto& body_path : all_body_paths) { |
| if (!body_path.IsEmpty()) { |
| const auto it = parent_map.find(body_path); |
| |
| if (it == parent_map.end() || it->second.IsEmpty()) { |
| q.push_back(body_path); |
| } |
| } |
| } |
|
|
| while (!q.empty()) { |
| pxr::SdfPath current_path = q.front(); |
| q.pop_front(); |
|
|
| pxr::SdfPath parent_path = parent_map.count(current_path) |
| ? parent_map.at(current_path) |
| : pxr::SdfPath(); |
| KinematicNode* parent_node = node_map.at(parent_path); |
|
|
| auto new_node = std::make_unique<KinematicNode>(); |
| new_node->body_path = current_path; |
| if (edge_to_joint_map.count({parent_path, current_path})) { |
| new_node->joint_path = edge_to_joint_map.at({parent_path, current_path}); |
| } |
| node_map[current_path] = new_node.get(); |
| parent_node->children.push_back(std::move(new_node)); |
|
|
| if (children_map.count(current_path)) { |
| const auto& children = children_map.at(current_path); |
| |
| |
| for (auto it = children.rbegin(); it != children.rend(); ++it) { |
| q.push_front(*it); |
| } |
| } |
| } |
|
|
| |
| |
| for (const auto& node : all_nodes) { |
| if (!node.IsEmpty() && !node_map.count(node)) { |
| mju_error("Cycle detected involving node %s.", node.GetString().c_str()); |
| return nullptr; |
| } |
| } |
|
|
| return world_root; |
| } |
| } |
| } |
|
|