NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
tgPairs.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 
27 #ifndef TG_PAIRS_H
28 #define TG_PAIRS_H
29 
30 #include <string>
31 #include <vector>
32 #include <algorithm> // std::find, std::remove
33 #include "core/tgTaggables.h"
34 #include "tgPair.h"
35 #include "tgUtil.h"
36 
37 class tgPairs : public tgTaggables<tgPair>
38 {
39 public:
40 
41  // @todo: do we need to initialize the pairs here?
42  tgPairs() : tgTaggables() {}
43 
44  // tgPairs(std::vector<tgPair>& pairs) : tgTaggables(pairs) { // @todo: Fix this -- casting is a problem...
45  tgPairs(std::vector<tgPair>& pairs) : tgTaggables() {
46  // @todo: make sure each pair is unique
47  for(std::size_t i = 0; i < pairs.size(); i++) {
48  addElement(pairs[i]);
49  }
50  }
51 
52  ~tgPairs() {}
53 
54  std::vector<tgPair>& getPairs()
55  {
56  return getElements();
57  }
58 
59  const std::vector<tgPair>& getPairs() const
60  {
61  return getElements();
62  }
63 
64  int addPair(const tgPair& pair) {
65  // @todo: make sure the pair is unique (both ways -- (a.from != b.from && a.to != b.to) && (a.from != b.to && a.to != b.from) )
66  return addElement(pair);
67  }
68 
69  int addPair(tgPair pair, const tgTags& tags)
70  {
71  pair.addTags(tags);
72  return addPair(pair);
73  }
74 
75  int addPair(tgPair pair, const std::string& space_separated_tags)
76  {
77  pair.addTags(space_separated_tags);
78  return addPair(pair);
79  }
80 
81  /*
82  * Removes the pair that's passed in as a parameter
83  * (added to accommodate structures encoded in YAML)
84  * @param[in] pair a reference to the pair to remove
85  */
86  void removePair(const tgPair& pair) {
87  removeElement(pair);
88  }
89 
90  void setPair(int key, tgPair pair) {
91  setElement(key, pair);
92  }
93 
94  void move(const btVector3& offset)
95  {
96  std::vector<tgPair>& pairs = getPairs();
97  for(std::size_t i = 0; i < pairs.size(); i++) {
98  pairs[i].move(offset);
99  }
100  }
101 
102  void addRotation(const btVector3& fixedPoint,
103  const btVector3& axis,
104  double angle)
105  {
106  btQuaternion rotation(axis, angle);
107  addRotation(fixedPoint, rotation);
108  }
109 
110  void addRotation(const btVector3& fixedPoint,
111  const btVector3& fromOrientation,
112  const btVector3& toOrientation)
113  {
114  btQuaternion rotation = tgUtil::getQuaternionBetween(fromOrientation,
115  toOrientation);
116  addRotation(fixedPoint, rotation);
117  }
118 
119  void addRotation(const btVector3& fixedPoint,
120  const btQuaternion& rotation)
121  {
122  std::vector<tgPair>& pairs = getPairs();
123  for(std::size_t i = 0; i < pairs.size(); i++) {
124  pairs[i].addRotation(fixedPoint, rotation);
125  }
126  }
127 
128  /*
129  * Scales pairs relative to a reference point
130  * @param[in] referencePoint a btVector3 reference point to scale the pairs from/to
131  * @param[in] scaleFactor the scale factor by which to scale the pairs
132  */
133  void scale(const btVector3& referencePoint, double scaleFactor) {
134  std::vector<tgPair>& pairs = getPairs();
135  for(int i = 0; i < pairs.size(); i++) {
136  pairs[i].scale(referencePoint, scaleFactor);
137  }
138  }
139 
140 
144  tgPairs& operator-=(const tgPairs& other) {
145  this->removeElements(other.getElements());
146  return *this;
147  }
148 
149  tgPairs& operator-=(const std::vector<tgPair*> other) {
150  this->removeElements(other);
151  return *this;
152  }
153 
154 };
155 
156 
157 inline tgPairs operator-(tgPairs lhs, const tgPairs& rhs)
158 {
159  lhs -= rhs;
160  return lhs;
161 }
162 
163 inline tgPairs operator-(tgPairs lhs, const std::vector<tgPair*>& rhs)
164 {
165  lhs -= rhs;
166  return lhs;
167 }
168 
169 
177 inline std::ostream&
178 operator<<(std::ostream& os, const tgPairs& p)
179 {
180 
181  os << "tgPairs(" << std::endl;
182  const std::vector<tgPair>& pairs = p.getPairs();
183  for(std::size_t i = 0; i < pairs.size(); i++) {
184  os << " " << pairs[i] << std::endl;
185  }
186  os << ")";
187 
188  return os;
189 };
190 
191 
196 inline std::string asYamlItems(const tgPairs& pairs, int indentLevel=0)
197 {
198  std::stringstream os;
199  std::string indent = std::string(2 * (indentLevel), ' ');
200 
201  if (pairs.size() == 0) {
202  os << indent << "pairs: []" << std::endl;
203  return os.str();
204  }
205 
206  os << indent << "pairs:" << std::endl;
207  for(size_t i = 0; i < pairs.size(); i++)
208  {
209  os << asYamlItem(pairs[i], indentLevel+1);
210  }
211  return os.str();
212 };
213 
214 
215 #endif
static btQuaternion getQuaternionBetween(btVector3 a, btVector3 b, const btVector3 &fallbackAxis=btVector3(0, 0, 0))
Definition: tgUtil.h:199
std::string asYamlItems(const tgPairs &pairs, int indentLevel=0)
Definition: tgPairs.h:196
tgPairs & operator-=(const tgPairs &other)
Definition: tgPairs.h:144
Definition of class tgPair.
Contains the definition of class tgTaggables.
Definition: tgPair.h:48
std::ostream & operator<<(std::ostream &os, const tgPairs &p)
Definition: tgPairs.h:178
std::string asYamlItem(const tgNode &node, int indentLevel=0)
Definition: tgNode.h:125
Rand seeding simular to the evolution and terrain classes.
Definition: tgTags.h:44