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