NTRT Simulator
 All Classes Files Functions Variables Typedefs Friends Pages
tgStructureInfo.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 
26 // This module
27 #include "tgStructureInfo.h"
28 // This library
29 #include "tgBuildSpec.h"
30 #include "tgConnectorInfo.h"
31 #include "tgRigidAutoCompound.h"
32 #include "tgStructure.h"
33 // The C++ Standard Library
34 #include <stdexcept>
35 
36 tgStructureInfo::tgStructureInfo(tgStructure& structure, tgBuildSpec& buildSpec) :
37  tgTaggable(),
38  m_structure(structure),
39  m_buildSpec(buildSpec)
40 {
41  createTree(*this, structure);
42 }
43 
44 tgStructureInfo::tgStructureInfo(tgStructure& structure, tgBuildSpec& buildSpec,
45  const tgTags& tags) :
46  tgTaggable(tags),
47  m_structure(structure),
48  m_buildSpec(buildSpec)
49 {
50  createTree(*this, structure);
51 }
52 
53 tgStructureInfo::~tgStructureInfo()
54 {
55  // Have to do this first, if all the rigids are deleted it segfaults
56  for (std::size_t i = 0; i < m_compounded.size(); i++)
57  {
58  const tgRigidInfo * const pRigidInfo = m_compounded[i];
59  assert(pRigidInfo != NULL);
60  // If this is true, rigid is in a group with itself, and
61  // will be deleted with m_rigids
62  if (pRigidInfo->getRigidInfoGroup() != pRigidInfo)
63  {
64  delete pRigidInfo;
65  }
66  }
67 
68  for (std::size_t i = 0; i < m_rigids.size(); i++)
69  {
70  delete m_rigids[i];
71  }
72 
73  for (std::size_t i = 0; i < m_connectors.size(); i++)
74  {
75  delete m_connectors[i];
76  }
77 
78  for (std::size_t i = 0; i < m_children.size(); i++)
79  {
80  delete m_children[i];
81  }
82 }
83 
84 void tgStructureInfo::createTree(tgStructureInfo& structureInfo,
85  const tgStructure& structure)
86 {
87  const std::vector<tgStructure*> children = structure.getChildren();
88  for (int i = 0; i < children.size(); i++)
89  {
90  tgStructure * const pStructure = children[i];
91  assert(pStructure != NULL);
92  tgStructureInfo* const pStructureInfo =
93  new tgStructureInfo(*pStructure, m_buildSpec, pStructure->getTags());
94  structureInfo.addChild(pStructureInfo);
95  createTree(*pStructureInfo, *pStructure);
96  }
97 }
98 
99 std::vector<tgRigidInfo*> tgStructureInfo::getAllRigids() const
100 {
101  std::vector<tgRigidInfo*> result;
102  result.insert(result.end(), m_rigids.begin(), m_rigids.end());
103 
104  // Collect child rigids
105  for (int i = 0; i < m_children.size(); i++)
106  {
107  tgStructureInfo * const pStructureInfo = m_children[i];
108  assert(pStructureInfo != NULL);
109  std::vector<tgRigidInfo*> childRigids = pStructureInfo->getAllRigids();
110  result.insert(result.end(), childRigids.begin(), childRigids.end());
111  }
112 
113  return result;
114 }
115 
117 // Build methods
119 
120 void tgStructureInfo::initRigidInfo()
121 {
122  // Step through each of the nodes and pairs, passing them through the rigidAgents to get a list of tgRigidInfos to be added to this.
123 
124  const std::vector<tgBuildSpec::RigidAgent*> rigidAgents = m_buildSpec.getRigidAgents();
125 
126  for (int i = 0; i < rigidAgents.size(); i++)
127  {
128  tgBuildSpec::RigidAgent * const pRigidAgent = rigidAgents[i];
129  assert(pRigidAgent != NULL);
130  tgTagSearch& tagSearch = pRigidAgent->tagSearch;
131  tgRigidInfo* pRigidInfo = pRigidAgent->infoFactory;
132  assert(pRigidInfo != NULL);
133 
134  // Nodes
135  std::vector<tgRigidInfo*> nodeRigids =
136  pRigidInfo->createRigidInfos(m_structure.getNodes(), tagSearch);
137  m_rigids.insert(m_rigids.end(), nodeRigids.begin(), nodeRigids.end());
138 
139  // Pairs
140  std::vector<tgRigidInfo*> pairRigids =
141  pRigidInfo->createRigidInfos(m_structure.getPairs(), tagSearch);
142  m_rigids.insert(m_rigids.end(), pairRigids.begin(), pairRigids.end());
143 
144  }
145 
146  // Children
147  for (int i = 0; i < m_children.size(); i++)
148  {
149  tgStructureInfo * const pStructureInfo = m_children[i];
150  assert(pStructureInfo != NULL);
151  pStructureInfo->initRigidInfo();
152  }
153 }
154 
155 void tgStructureInfo::autoCompoundRigids()
156 {
157  tgRigidAutoCompound c(getAllRigids());
158  m_compounded = c.execute();
159 }
160 
161 void tgStructureInfo::initConnectorInfo()
162 {
163  // Step through each of the nodes and pairs, passing them through the
164  // rigidAgents to get a list of tgRigidInfos to be added to this.
165 
166  const std::vector<tgBuildSpec::ConnectorAgent*> connectorAgents =
167  m_buildSpec.getConnectorAgents();
168 
169  for (int i = 0; i < connectorAgents.size(); i++)
170  {
171  tgBuildSpec::ConnectorAgent* const pConnectorAgent = connectorAgents[i];
172  assert(pConnectorAgent != NULL);
173  tgTagSearch& tagSearch = pConnectorAgent->tagSearch;
174  tgConnectorInfo* const pConnectorInfo = pConnectorAgent->infoFactory;
175  assert(pConnectorInfo != NULL);
176 
177  // Note: we don't have to do nodes here since connectors are always based on pairs.
178 
179  // Pairs
180  std::vector<tgConnectorInfo*> pairConnectors =
181  pConnectorInfo->createConnectorInfos(m_structure.getPairs(), tagSearch);
182  m_connectors.insert(m_connectors.end(), pairConnectors.begin(),
183  pairConnectors.end());
184 
185  }
186 
187  // Children
188  for (int i = 0; i < m_children.size(); i++)
189  {
190  tgStructureInfo * const pStructureInfo = m_children[i];
191  assert(pStructureInfo != NULL);
192  pStructureInfo->initConnectorInfo();
193  }
194 }
195 
196 void tgStructureInfo::chooseConnectorRigids()
197 {
198  chooseConnectorRigids(getAllRigids());
199 }
200 
201 void tgStructureInfo::chooseConnectorRigids(std::vector<tgRigidInfo*> allRigids)
202 {
203  for (int i = 0; i < m_connectors.size(); i++)
204  {
205  tgConnectorInfo * const pConnectorInfo = m_connectors[i];
206  assert(pConnectorInfo != NULL);
207  pConnectorInfo->chooseRigids(allRigids);
208  }
209 
210  // Children
211  for (int i = 0; i < m_children.size(); i++)
212  {
213  tgStructureInfo * const pStructureInfo = m_children[i];
214  assert(pStructureInfo != NULL);
215  pStructureInfo->chooseConnectorRigids(allRigids);
216  }
217 }
218 
219 void tgStructureInfo::initRigidBodies(tgWorld& world)
220 {
221  // Rigids
222  for (int i = 0; i < m_rigids.size(); i++)
223  {
224  tgRigidInfo * const pRigidInfo = m_rigids[i];
225  assert(pRigidInfo != NULL);
226  pRigidInfo->initRigidBody(world);
227  }
228 
229  // Children
230  for (int i = 0; i < m_children.size(); i++)
231  {
232  tgStructureInfo * const pStructureInfo = m_children[i];
233  assert(pStructureInfo != NULL);
234  pStructureInfo->initRigidBodies(world);
235  }
236 }
237 
238 void tgStructureInfo::initConnectors(tgWorld& world)
239 {
240  // Connectors
241  for (int i = 0; i < m_connectors.size(); i++)
242  {
243  tgConnectorInfo * const pConnectorInfo = m_connectors[i];
244  assert(pConnectorInfo != NULL);
245  pConnectorInfo->initConnector(world);
246  }
247 
248  // Children
249  for (int i = 0; i < m_children.size(); i++)
250  {
251  tgStructureInfo * const pStructureInfo = m_children[i];
252  assert(pStructureInfo != NULL);
253  pStructureInfo->initConnectors(world);
254  }
255 }
256 
257 void tgStructureInfo::buildInto(tgModel& model, tgWorld& world)
258 {
259  // These take care of things on a global level
260  initRigidInfo();
261  autoCompoundRigids();
262  initConnectorInfo();
263  chooseConnectorRigids();
264  initRigidBodies(world);
265  // Note: Muscle2Ps won't show up yet --
266  // they need to be part of a model to have rendering...
267  initConnectors(world);
268  // Now build into the model
269  buildIntoHelper(model, world, *this);
270 }
271 
272 void tgStructureInfo::buildIntoHelper(tgModel& model, tgWorld& world,
273  tgStructureInfo& structureInfo)
274 {
275 
276  const std::vector<tgRigidInfo*> rigids = structureInfo.getRigids();
277  for (int i = 0; i < rigids.size(); i++)
278  {
279  tgRigidInfo * const pRigidInfo = rigids[i];
280  assert(pRigidInfo != NULL);
281  tgModel* const pModel = pRigidInfo->createModel(world);
282  if (pModel != NULL)
283  {
284  pModel->setTags(pRigidInfo->getTags());
285  model.addChild(pModel);
286  }
287  }
288 
289  const std::vector<tgConnectorInfo*> connectors = structureInfo.getConnectors();
290  for (int i = 0; i < connectors.size(); i++)
291  {
292  tgConnectorInfo * const pConnectorInfo = connectors[i];
293  assert(pConnectorInfo != NULL);
294  tgModel* const pModel = pConnectorInfo->createModel(world);
295  if (pModel != NULL)
296  {
297  pModel->setTags(pConnectorInfo->getTags());
298  model.addChild(pModel);
299  }
300  }
301 
302  const std::vector<tgStructureInfo*> children = structureInfo.getChildren();
303  for (int i = 0; i < children.size(); i++)
304  {
305  tgStructureInfo * const pStructureInfo = children[i];
306  assert(pStructureInfo != NULL);
307  tgModel* const pModel = new tgModel();
308  assert(pModel != NULL);
309  buildIntoHelper(*pModel, world, *pStructureInfo);
310  model.addChild(pModel);
311  }
312 
313  model.setTags(structureInfo.getTags());
314 }
315 
316 void tgStructureInfo::addChild(tgStructureInfo* pChild)
317 {
318  if (pChild == NULL)
319  {
320  throw std::invalid_argument("Child is NULL");
321  }
322  else
323  {
324  m_children.push_back(pChild);
325  }
326 }
327 
328 std::string tgStructureInfo::toString(const std::string& prefix) const
329 {
330  std::string p = " ";
331  std::ostringstream os;
332  os << prefix << "tgStructureInfo(" << std::endl;
333 
334  os << prefix << p << "Rigids:" << std::endl;
335  for (int i = 0; i < m_rigids.size(); i++) {
336  os << prefix << p << p << *(m_rigids[i]) << std::endl;
337  }
338 
339  os << prefix << p << "Connectors:" << std::endl;
340  for (int i = 0; i < m_connectors.size(); i++) {
341  os << prefix << p << p << *(m_connectors[i]) << std::endl;
342  }
343 
344  os << prefix << p << "Children:" << std::endl;
345  for (int i = 0; i < m_children.size(); i++) {
346  os << m_children[i]->toString(prefix + p + p) << std::endl;
347  }
348 
349  os << prefix << p << "Tags: [" << getTags() << "]" << std::endl;
350 
351  os << prefix << ")" << std::endl;
352  return os.str();
353 }
354 
355 std::ostream&
356 operator<<(std::ostream& os, const tgStructureInfo& obj)
357 {
358  os << obj.toString() << std::endl;
359  return os;
360 }
361 
362 
363 
364 
const std::vector< tgStructure * > & getChildren() const
Definition: tgStructure.h:130
void addChild(tgModel *pChild)
Definition: tgModel.cpp:126
virtual tgRigidInfo * getRigidInfoGroup()
Definition: tgRigidInfo.h:151
const tgNodes & getNodes() const
Definition: tgStructure.h:112
const tgPairs & getPairs() const
Definition: tgStructure.h:122
Definition of class tgConnectorInfo.
Definition of class tgStructure.
Definition of class tgStructureInfo.
std::ostream & operator<<(std::ostream &os, const tgStructureInfo &obj)
Definition of class tgBuildSpec.
Definition of class tgRigidAutoCompound.
Definition: tgTags.h:43