File: User2InternalIndex.cpp

package info (click to toggle)
bullet 3.06%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 15,012 kB
  • sloc: cpp: 243,705; lisp: 12,017; ansic: 11,175; python: 626; makefile: 133; sh: 75
file content (121 lines) | stat: -rw-r--r-- 2,762 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "User2InternalIndex.hpp"

namespace btInverseDynamics
{
User2InternalIndex::User2InternalIndex() : m_map_built(false) {}

void User2InternalIndex::addBody(const int body, const int parent)
{
	m_user_parent_index_map[body] = parent;
}

int User2InternalIndex::findRoot(int index)
{
	if (0 == m_user_parent_index_map.count(index))
	{
		return index;
	}
	return findRoot(m_user_parent_index_map[index]);
}

// modelled after URDF2Bullet.cpp:void ComputeParentIndices(const
// URDFImporterInterface& u2b, URDF2BulletCachedData& cache, int urdfLinkIndex,
// int urdfParentIndex)
void User2InternalIndex::recurseIndexSets(const int user_body_index)
{
	m_user_to_internal[user_body_index] = m_current_index;
	m_current_index++;
	for (size_t i = 0; i < m_user_child_indices[user_body_index].size(); i++)
	{
		recurseIndexSets(m_user_child_indices[user_body_index][i]);
	}
}

int User2InternalIndex::buildMapping()
{
	// find root index
	int user_root_index = -1;
	for (std::map<int, int>::iterator it = m_user_parent_index_map.begin();
		 it != m_user_parent_index_map.end(); it++)
	{
		int current_root_index = findRoot(it->second);
		if (it == m_user_parent_index_map.begin())
		{
			user_root_index = current_root_index;
		}
		else
		{
			if (user_root_index != current_root_index)
			{
				bt_id_error_message("multiple roots (at least) %d and %d\n", user_root_index,
									current_root_index);
				return -1;
			}
		}
	}

	// build child index map
	for (std::map<int, int>::iterator it = m_user_parent_index_map.begin();
		 it != m_user_parent_index_map.end(); it++)
	{
		m_user_child_indices[it->second].push_back(it->first);
	}

	m_current_index = -1;
	// build internal index set
	m_user_to_internal[user_root_index] = -1;  // add map for root link
	recurseIndexSets(user_root_index);

	// reverse mapping
	for (std::map<int, int>::iterator it = m_user_to_internal.begin();
		 it != m_user_to_internal.end(); it++)
	{
		m_internal_to_user[it->second] = it->first;
	}

	m_map_built = true;
	return 0;
}

int User2InternalIndex::user2internal(const int user, int *internal) const
{
	if (!m_map_built)
	{
		return -1;
	}

	std::map<int, int>::const_iterator it;
	it = m_user_to_internal.find(user);
	if (it != m_user_to_internal.end())
	{
		*internal = it->second;
		return 0;
	}
	else
	{
		bt_id_error_message("no user index %d\n", user);
		return -1;
	}
}

int User2InternalIndex::internal2user(const int internal, int *user) const
{
	if (!m_map_built)
	{
		return -1;
	}

	std::map<int, int>::const_iterator it;
	it = m_internal_to_user.find(internal);
	if (it != m_internal_to_user.end())
	{
		*user = it->second;
		return 0;
	}
	else
	{
		bt_id_error_message("no internal index %d\n", internal);
		return -1;
	}
}
}  // namespace btInverseDynamics