Welcome to mirror list, hosted at ThFree Co, Russian Federation.

tree.cpp « kdl « itasc « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f117e54959bbc0cd8ab82ce25c65cd31b031e685 (plain)
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
// Copyright  (C)  2007  Ruben Smits <ruben dot smits at mech dot kuleuven dot be>

// Version: 1.0
// Author: Ruben Smits <ruben dot smits at mech dot kuleuven dot be>
// Maintainer: Ruben Smits <ruben dot smits at mech dot kuleuven dot be>
// URL: http://www.orocos.org/kdl

// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.

// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

#include "tree.hpp"
#include <sstream>
namespace KDL {
using namespace std;

Tree::Tree() :
    nrOfJoints(0), nrOfSegments(0) {
    segments.insert(make_pair("root", TreeElement::Root()));
}

Tree::Tree(const Tree& in) {
    segments.clear();
    nrOfSegments = 0;
    nrOfJoints = 0;

    segments.insert(make_pair("root", TreeElement::Root()));
    this->addTree(in, "", "root");

}

Tree& Tree::operator=(const Tree& in) {
    segments.clear();
    nrOfSegments = 0;
    nrOfJoints = 0;

    segments.insert(make_pair("root", TreeElement::Root()));
    this->addTree(in, "", "root");
    return *this;
}

bool Tree::addSegment(const Segment& segment, const std::string& segment_name,
        const std::string& hook_name) {
    SegmentMap::iterator parent = segments.find(hook_name);
    //check if parent exists
    if (parent == segments.end())
        return false;
    pair<SegmentMap::iterator, bool> retval;
    //insert new element
    retval = segments.insert(make_pair(segment_name, TreeElement(segment,
            parent, nrOfJoints)));
    //check if insertion succeeded
    if (!retval.second)
        return false;
    //add iterator to new element in parents children list
    parent->second.children.push_back(retval.first);
    //increase number of segments
    nrOfSegments++;
    //increase number of joints
	nrOfJoints += segment.getJoint().getNDof();
    return true;
}

bool Tree::addChain(const Chain& chain, const std::string& chain_name,
        const std::string& hook_name) {
    string parent_name = hook_name;
    for (unsigned int i = 0; i < chain.getNrOfSegments(); i++) {
        ostringstream segment_name;
        segment_name << chain_name << "Segment" << i;
        if (this->addSegment(chain.getSegment(i), segment_name.str(),
                parent_name))
            parent_name = segment_name.str();
        else
            return false;
    }
    return true;
}

bool Tree::addTree(const Tree& tree, const std::string& tree_name,
        const std::string& hook_name) {
    return this->addTreeRecursive(tree.getSegment("root"), tree_name, hook_name);
}

bool Tree::addTreeRecursive(SegmentMap::const_iterator root,
        const std::string& tree_name, const std::string& hook_name) {
    //get iterator for root-segment
    SegmentMap::const_iterator child;
    //try to add all of root's children
    for (unsigned int i = 0; i < root->second.children.size(); i++) {
        child = root->second.children[i];
        //Try to add the child
        if (this->addSegment(child->second.segment, tree_name + child->first,
                hook_name)) {
            //if child is added, add all the child's children
            if (!(this->addTreeRecursive(child, tree_name, tree_name
                    + child->first)))
                //if it didn't work, return false
                return false;
        } else
            //If the child could not be added, return false
            return false;
    }
    return true;
}

}