NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
tgStructure.h
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 
19 #ifndef TG_STRUCTURE_H
20 #define TG_STRUCTURE_H
21 
30 // This library
31 #include "tgNodes.h"
32 #include "tgPairs.h"
33 // The NTRT Core Library
34 #include "core/tgTaggable.h"
35 // The C++ Standard Library
36 #include <string>
37 #include <vector>
38 #include <queue>
39 
40 // Forward declarations
41 class btQuaternion;
42 class btVector3;
43 class tgNode;
44 class tgTags;
45 
54 class tgStructure : public tgTaggable
55 {
56 public:
57 
58  tgStructure();
59 
60  tgStructure(const tgStructure& orig);
61 
62  tgStructure(const tgTags& tags);
63 
64  tgStructure(const std::string& space_separated_tags);
65 
66  virtual ~tgStructure();
67 
71  void addNode(double x, double y, double z, std::string tags = "");
72 
77  void addNode(tgNode& newNode);
78 
82  void addPair(int fromNodeIdx, int toNodeIdx, std::string tags = "");
83 
87  void addPair(const btVector3& from, const btVector3& to, std::string tags = "");
88 
89  /*
90  * Removes the pair that's passed in as a parameter from the structure
91  * (added to accommodate structures encoded in YAML)
92  * @param[in] pair a reference to the pair to remove
93  */
94  void removePair(const tgPair& pair);
95 
96  void move(const btVector3& offset);
97 
101  void addRotation(const btVector3& fixedPoint,
102  const btVector3& axis,
103  double angle);
104 
105  void addRotation(const btVector3& fixedPoint,
106  const btVector3& fromOrientation,
107  const btVector3& toOrientation);
108 
109  void addRotation(const btVector3& fixedPoint,
110  const btQuaternion& rotation);
111 
112  /*
113  * Scales structure by a scale factor
114  * @param[in] scaleFactor the scale factor by which to scale the structure
115  */
116  void scale(double scaleFactor);
117 
118  /*
119  * Scales structure relative to a reference point
120  * @param[in] referencePoint a btVector3 reference point to scale the structure from/to
121  * @param[in] scaleFactor the scale factor by which to scale the structure
122  */
123  void scale(const btVector3& referencePoint, double scaleFactor);
124 
129  void addChild(tgStructure* child);
130 
131  void addChild(const tgStructure& child);
132 
138  const tgNodes& getNodes() const
139  {
140  return m_nodes;
141  }
142 
151  tgNode& findNode(const std::string& name);
152 
158  btVector3 getCentroid() const;
159 
165  const tgPairs& getPairs() const
166  {
167  return m_pairs;
168  }
169 
179  tgPair& findPair(const btVector3& from, const btVector3& to);
180 
184  const std::vector<tgStructure*>& getChildren() const
185  {
186  return m_children;
187  }
188 
197  tgStructure& findChild(const std::string& name);
198 
199 private:
200 
201  tgNodes m_nodes;
202 
203  tgPairs m_pairs;
204 
205  // we own these
206  std::vector<tgStructure*> m_children;
207 
208 };
209 
217  /*
218 inline std::ostream&
219 operator<<(std::ostream& os, const tgStructure& structure)
220 {
221  os << "tgStructure with the following tgNodes and tgPairs: " << std::endl;
222  os << structure.getNodes() << std::endl;
223  os << structure.getPairs() << std::endl;
224  os << "This tgStructure has the following children (also tgStructures): "
225  << std::endl;
226 
227  // add each child structure's string output to the output stream
228  std::vector<tgStructure*> children = structure.getChildren();
229  for(std::size_t i= 0; i < children.size(); i++)
230  {
231  os << "Child " << i << ": " << std::endl;
232  os << *(children[i]) << std::endl;
233  }
234 
235  // If this tgStructure has no children, say so.
236  if( children.size() == 0)
237  {
238  os << "(no children)" << std::endl;
239  }
240 
241  return os;
242 }
243 */
244 
245 
246 
251 std::string asYamlElement(const tgStructure& structure, int indentLevel=0);
252 
257 std::string asYamlItem(const tgStructure& structure, int indentLevel=0);
258 
263 std::string asYamlItems(const std::vector<tgStructure*> structures, int indentLevel=0);
264 
272 inline std::ostream&
273 operator<<(std::ostream& os, const tgStructure& structure)
274 {
275  //os << asYamlElement(os, structure) << std::endl;
276  os << asYamlElement(structure) << std::endl;
277  return os;
278 };
279 
280 /*
281 std::string asYamlElement(const tgStructure& structure, int indentLevel=0)
282 {
283  std::stringstream os;
284  std::string indent = std::string(2 * (indentLevel), ' ');
285  os << indent << "structure:" << std::endl;
286  os << indent << " tags: " << asYamlList(structure.getTags()) << std::endl;
287  os << asYamlItems(structure.getNodes(), indentLevel + 1);
288  os << asYamlItems(structure.getPairs(), indentLevel + 1);
289  os << asYamlItems(structure.getChildren(), indentLevel + 1);
290  return os.str();
291 };
292 
293 inline std::string asYamlItem(const tgStructure& structure, int indentLevel=0)
294 {
295  std::stringstream os;
296  std::string indent = std::string(2 * (indentLevel), ' ');
297  os << indent << "- tags: " << asYamlList(structure.getTags()) << std::endl;
298  os << asYamlItems(structure.getNodes(), indentLevel + 1);
299  os << asYamlItems(structure.getPairs(), indentLevel + 1);
300  os << asYamlItems(structure.getChildren(), indentLevel + 1);
301  return os.str();
302 };
303 
304 inline std::string asYamlItems(const std::vector<tgStructure*> structures, int indentLevel)
305 {
306  std::stringstream os;
307  std::string indent = std::string(2 * (indentLevel), ' ');
308  if (structures.size() == 0) {
309  os << indent << "structures: []" << std::endl;
310  return os.str();
311  }
312 
313  os << indent << "structures:" << std::endl;
314  for(size_t i = 0; i < structures.size(); i++)
315  {
316  os << asYamlItem(*structures[i], indentLevel+1);
317  }
318  return os.str();
319 };
320 
321 */
322 #endif
tgStructure & findChild(const std::string &name)
tgNode & findNode(const std::string &name)
const std::vector< tgStructure * > & getChildren() const
Definition: tgStructure.h:184
void addChild(tgStructure *child)
std::ostream & operator<<(std::ostream &os, const tgStructure &structure)
Definition: tgStructure.h:273
std::string asYamlItem(const tgStructure &structure, int indentLevel=0)
btVector3 getCentroid() const
const tgNodes & getNodes() const
Definition: tgStructure.h:138
Definition of class tgPairs.
void addPair(int fromNodeIdx, int toNodeIdx, std::string tags="")
Definition: tgStructure.cpp:80
const tgPairs & getPairs() const
Definition: tgStructure.h:165
Definition of class tgNodes.
void addRotation(const btVector3 &fixedPoint, const btVector3 &axis, double angle)
std::string asYamlItems(const std::vector< tgStructure * > structures, int indentLevel=0)
Definition: tgPair.h:48
std::string asYamlElement(const tgStructure &structure, int indentLevel=0)
Definition: tgNode.h:45
Contains the definition of class tgTaggable.
tgPair & findPair(const btVector3 &from, const btVector3 &to)
Definition: tgTags.h:44
void addNode(double x, double y, double z, std::string tags="")
Definition: tgStructure.cpp:70