NTRT Simulator
 All Classes Files Functions Variables Typedefs Friends Pages
tgNodes.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_NODES_H
20 #define TG_NODES_H
21 
29 #include "LinearMath/btVector3.h"
30 #include <iostream>
31 #include <vector>
32 #include <map>
33 #include <set>
34 #include <stdexcept>
35 #include <sstream>
36 
37 #include "tgNode.h"
38 #include "core/tgTaggables.h"
39 
40 class tgPair;
41 
48 class tgNodes : public tgTaggables<tgNode> {
49 public:
50 
55  {
56  }
57 
64  tgNodes(std::vector<btVector3>& nodes) : tgTaggables()
65  {
66  // All elements must be unique
67  assertUniqueElements("All nodes must be unique.");
68 
69  // @todo: There has to be a better way to do this (maybe initializer lists with upcasting btVector3 => tgNode?)
70  for(int i = 0; i < nodes.size(); i++) {
71  addElement(tgNode(nodes[i]));
72  }
73 
74  }
75 
76  tgNodes(std::vector<tgNode>& nodes) : tgTaggables(nodes) {
77  // All elements must be unique
78  assertUniqueElements("All nodes must be unique.");
79 
80  }
81 
83  ~tgNodes() { }
84 
90  void setNode(int key, const btVector3& node)
91  {
92  setNode(key, tgNode(node));
93  };
94 
95  void setNode(int key, const tgNode& node)
96  {
97  setElement(key, node);
98  }
99 
100  std::vector<tgNode>& getNodes()
101  {
102  return getElements();
103  };
104 
105  const std::vector<tgNode>& getNodes() const
106  {
107  return getElements();
108  };
109 
110 
117  bool nodeExists(int key) const
118  {
119  return keyExists(key);
120  }
121 
128  int addNode(const btVector3& node) {
129  return addNode(tgNode(node));
130  };
131 
132  int addNode(const btVector3& node, std::string tags) {
133  return addNode(tgNode(node, tags));
134  };
135 
136  int addNode(const tgNode& node) {
137  return addElement(node);
138  }
139 
149  // @todo: Maybe replace this with something like:
150  // myNodes += tgNode(1,2,3, "some tags");
151  // or just use myNodes.addNode(tgNode(1,2,3, "tags here"));
152  int addNode(double x, double y, double z)
153  {
154  const btVector3 node(x, y, z);
155  return addNode(node);
156  }
157 
158  int addNode(double x, double y, double z, std::string tags)
159  {
160  const tgNode node(x, y, z, tags);
161  return addNode(node);
162  }
163 
167  tgPair pair(int from, int to, std::string tags = "");
168 
174  void move(const btVector3& offset)
175  {
177  std::vector<tgNode>& nodes = getElements();
178  for(int i = 0; i < nodes.size(); i++) {
179  nodes[i] += offset;
180  }
181  }
182 
183  void moveNode(int idx, const btVector3 offset)
184  {
185  (*this)[idx] += offset;
186  }
187 
188  void addRotation(const btVector3& fixedPoint,
189  const btVector3& axis,
190  double angle)
191  {
192  btQuaternion rotation(axis, angle);
193  addRotation(fixedPoint, rotation);
194  }
195 
196  void addRotation(const btVector3& fixedPoint,
197  const btVector3& fromOrientation,
198  const btVector3& toOrientation)
199  {
200  btQuaternion rotation = tgUtil::getQuaternionBetween(fromOrientation,
201  toOrientation);
202  addRotation(fixedPoint, rotation);
203  }
204 
205  void addRotation(const btVector3& fixedPoint,
206  const btQuaternion& rotation)
207  {
208  std::vector<tgNode>& nodes = getNodes();
209  for(int i = 0; i < nodes.size(); i++) {
210  nodes[i].addRotation(fixedPoint, rotation);
211  }
212  }
213 
214 protected:
215 
216  // A map of m_nodes keys to names. Note that not all m_nodes will have names.
217  std::map<int, std::string> m_names; // @todo: remove this...
218 
219  void assertNodeExists(int key) const
220  {
221  if(!keyExists(key)) {
222  std::stringstream ss;
223  ss << key;
224  throw std::out_of_range("Node at index " + ss.str() + " does not exist");
225  }
226  }
227 
228  void assertUniqueNodes() const
229  {
230  assertUniqueElements("Nodes muse be unique.");
231  }
232 
233 };
234 
235 
243 inline std::ostream&
244 operator<<(std::ostream& os, const tgNodes& n)
245 {
246 
247  os << "tgNodes(" << std::endl;
248  const std::vector<tgNode>& nodes = n.getNodes();
249  for(int i = 0; i < nodes.size(); i++) {
250  os << " " << nodes[i] << std::endl;
251  }
252  os << ")";
253 
254  return os;
255 }
256 
257 
258 #endif
void setNode(int key, const btVector3 &node)
Definition: tgNodes.h:90
~tgNodes()
Definition: tgNodes.h:83
bool nodeExists(int key) const
Definition: tgNodes.h:117
tgPair pair(int from, int to, std::string tags="")
Definition: tgNodes.cpp:29
static btQuaternion getQuaternionBetween(btVector3 a, btVector3 b)
Definition: tgUtil.h:182
Contains the definition of class tgTaggables $Id$.
Definition of class tgNode.
Definition: tgPair.h:47
Definition: tgNode.h:43
tgNodes()
Definition: tgNodes.h:54
int addNode(double x, double y, double z)
Definition: tgNodes.h:152
tgNodes(std::vector< btVector3 > &nodes)
Definition: tgNodes.h:64
std::ostream & operator<<(std::ostream &os, const tgNodes &n)
Definition: tgNodes.h:244
void move(const btVector3 &offset)
Definition: tgNodes.h:174
int addNode(const btVector3 &node)
Definition: tgNodes.h:128
bool keyExists(int key) const
Definition: tgTaggables.h:228