NTRT Simulator  v1.1
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
tgRigidAutoCompound.cpp
Go to the documentation of this file.
1 /*
2  * Copyright © 2012, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The NASA Tensegrity Robotics Toolkit (NTRT) v1 platform is licensed
7  * under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * http://www.apache.org/licenses/LICENSE-2.0.
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
15  * either express or implied. See the License for the specific language
16  * governing permissions and limitations under the License.
17 */
18 
27 #include "tgRigidAutoCompound.h"
28 
29 #include "BulletSoftBody/btSoftRigidDynamicsWorld.h"
30 #include "tgCompoundRigidInfo.h"
31 #include <map>
32 
33 // Debugging
34 #include <iostream>
35 #include "tgUtil.h"
36 
37 using namespace std;
38 
39 
40 // @todo: we want to start using this and get rid of the set-based constructor, but until we can refactor...
41 tgRigidAutoCompound::tgRigidAutoCompound(std::vector<tgRigidInfo*> rigids)
42 {
43  m_rigids.insert(m_rigids.end(), rigids.begin(), rigids.end());
44 }
45 
46 tgRigidAutoCompound::tgRigidAutoCompound(std::deque<tgRigidInfo*> rigids) : m_rigids(rigids)
47 {}
48 
49 std::vector< tgRigidInfo* > tgRigidAutoCompound::execute() {
50 
51  // Determine the grouping of our rigids
52  groupRigids();
53 
54  // Create the compounds as necessary
55  createCompounds();
56 
57  // Set the rigid body for the various groups
58  for(int i=0; i < m_groups.size(); i++) {
59  // Note: rigids that are not connected to anything else are placed
60  //into a group of their own, so they're represented here too
61  setRigidInfoForGroup(m_compounded[i], m_groups[i]);
62  }
63  // Need to return this so we can delete it at the appropreate time
64  return m_compounded;
65 };
66 
67 // @todo: we probably don't need this any more -- this will be taken care of in the tgRigidInfo => tgModel step
68 // @todo: NOTE: we need to have a way to check to see if a rigid has already been instantiated -- maybe just check get
69 void tgRigidAutoCompound::setRigidBodyForGroup(btCollisionObject* body, std::deque<tgRigidInfo*>& group) {
70  for(int i = 0; i < group.size(); i++) {
71  group[i]->setCollisionObject(body);
72  }
73 }
74 
75 void tgRigidAutoCompound::setRigidInfoForGroup(tgRigidInfo* rigidInfo, std::deque<tgRigidInfo*>& group) {
76  for(int i = 0; i < group.size(); i++) {
77  group[i]->setRigidInfoGroup(rigidInfo);
78  }
79 }
80 
81 void tgRigidAutoCompound::groupRigids()
82 {
83  std::deque<tgRigidInfo*> ungrouped = std::deque<tgRigidInfo*>(m_rigids); // Copy of m_rigids
84 
85  while(ungrouped.size() > 0) {
86  // go through each ungrouped element and find the groups for it
87  tgRigidInfo* elem = ungrouped[0]; // Note: the 0 element is removed by findGroup, so ungrouped[0] is different each iteration
88 
89  // Find all elements linked to the first item in ungrouped
90  // This will also remove the elements in the group from ungrouped
91  std::deque<tgRigidInfo*> group = findGroup(elem, ungrouped);
92 
93  // Add the group to the groups list
94  m_groups.push_back(group);
95  }
96 }
97 
98 // Find all rigids that should be in a group with the given rigid
99 // @todo: This may contain an off-by-one error (the last rigid may not be grouped properly...)
100 std::deque<tgRigidInfo*> tgRigidAutoCompound::findGroup(tgRigidInfo* rigid, std::deque<tgRigidInfo*>& ungrouped) {
101 
102  std::deque<tgRigidInfo*> group;
103 
104  // Add the rigid to the current group
105  group.push_back(rigid);
106 
107  // Remove the rigid from the ungrouped since it's now in a group
108  ungrouped.erase(std::remove(ungrouped.begin(), ungrouped.end(), rigid), ungrouped.end());
109 
110  // Recursively find linked elements
111  int i = 0;
112  while(i < ungrouped.size()) {
113  tgRigidInfo* other = ungrouped[i];
114  if(rigid->sharesNodesWith(*other)) {
115  std::deque<tgRigidInfo*> links = findGroup(other, ungrouped);
116  group.insert(group.end(), links.begin(), links.end());
117  i = 0;
118  } else {
119  i++;
120  }
121  }
122 
123  return group;
124 };
125 
126 void tgRigidAutoCompound::createCompounds() {
127  for(int i=0; i < m_groups.size(); i++) {
128  std::deque<tgRigidInfo*>& group = m_groups[i];
129  if(group.size() == 1) {
130  // Only one, no compounding necessary
131  m_compounded.push_back(group[0]);
132  } else {
133  tgRigidInfo* compound = createCompound(group);
134  m_compounded.push_back(compound);
135  }
136  }
137 }
138 
139 tgRigidInfo* tgRigidAutoCompound::createCompound(std::deque<tgRigidInfo*> rigids) {
141  for(int i = 0; i < rigids.size(); i++) {
142  c->addRigid(*rigids[i]);
143  }
144  return (tgRigidInfo*)c;
145 }
146 
147 bool tgRigidAutoCompound::rigidBelongsIn(tgRigidInfo* rigid, std::deque<tgRigidInfo*> group) {
148  for(int i = 0; i < group.size(); i++) {
149  tgRigidInfo* other = group[i];
150  if(rigid->sharesNodesWith(*other))
151  return true;
152  }
153  return false;
154 };
virtual bool sharesNodesWith(const tgRigidInfo &other) const
Definition of class tgCompoundRigidInfo.
void addRigid(tgRigidInfo &rigid)
Contains the definition of class tgUtil and overloaded operator<<() free functions.
Definition of class tgRigidAutoCompound.