NTRT Simulator
 All Classes 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 
26 #ifndef TG_PAIRS_H
27 #define TG_PAIRS_H
28 
29 #include <string>
30 #include <vector>
31 #include <algorithm> // std::find, std::remove
32 #include "core/tgTaggables.h"
33 #include "tgPair.h"
34 #include "tgUtil.h"
35 
36 class tgPairs : public tgTaggables<tgPair>
37 {
38 public:
39 
40  // @todo: do we need to initialize the pairs here?
41  tgPairs() : tgTaggables() {}
42 
43  // tgPairs(std::vector<tgPair>& pairs) : tgTaggables(pairs) { // @todo: Fix this -- casting is a problem...
44  tgPairs(std::vector<tgPair>& pairs) : tgTaggables() {
45  // @todo: make sure each pair is unique
46  for(int i = 0; i < pairs.size(); i++) {
47  addElement(pairs[i]);
48  }
49  }
50 
51  ~tgPairs() {}
52 
53  std::vector<tgPair>& getPairs()
54  {
55  return getElements();
56  }
57 
58  const std::vector<tgPair>& getPairs() const
59  {
60  return getElements();
61  }
62 
63  int addPair(const tgPair& pair) {
64  // @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) )
65  return addElement(pair);
66  }
67 
68  int addPair(tgPair pair, const tgTags& tags)
69  {
70  pair.addTags(tags);
71  return addPair(pair);
72  }
73 
74  int addPair(tgPair pair, const std::string& space_separated_tags)
75  {
76  pair.addTags(space_separated_tags);
77  return addPair(pair);
78  }
79 
80  void setPair(int key, tgPair pair) {
81  setElement(key, pair);
82  }
83 
84  void move(const btVector3& offset)
85  {
86  std::vector<tgPair>& pairs = getPairs();
87  for(int i = 0; i < pairs.size(); i++) {
88  pairs[i].move(offset);
89  }
90  }
91 
92  void addRotation(const btVector3& fixedPoint,
93  const btVector3& axis,
94  double angle)
95  {
96  btQuaternion rotation(axis, angle);
97  addRotation(fixedPoint, rotation);
98  }
99 
100  void addRotation(const btVector3& fixedPoint,
101  const btVector3& fromOrientation,
102  const btVector3& toOrientation)
103  {
104  btQuaternion rotation = tgUtil::getQuaternionBetween(fromOrientation,
105  toOrientation);
106  addRotation(fixedPoint, rotation);
107  }
108 
109  void addRotation(const btVector3& fixedPoint,
110  const btQuaternion& rotation)
111  {
112  std::vector<tgPair>& pairs = getPairs();
113  for(int i = 0; i < pairs.size(); i++) {
114  pairs[i].addRotation(fixedPoint, rotation);
115  }
116  }
117 
118 
122  tgPairs& operator-=(const tgPairs& other) {
123  this->removeElements(other.getElements());
124  return *this;
125  }
126 
127  tgPairs& operator-=(const std::vector<tgPair*> other) {
128  this->removeElements(other);
129  return *this;
130  }
131 
132 };
133 
134 
135 inline tgPairs operator-(tgPairs lhs, const tgPairs& rhs)
136 {
137  lhs -= rhs;
138  return lhs;
139 }
140 
141 inline tgPairs operator-(tgPairs lhs, const std::vector<tgPair*>& rhs)
142 {
143  lhs -= rhs;
144  return lhs;
145 }
146 
147 
155 inline std::ostream&
156 operator<<(std::ostream& os, const tgPairs& p)
157 {
158 
159  os << "tgPairs(" << std::endl;
160  const std::vector<tgPair>& pairs = p.getPairs();
161  for(int i = 0; i < pairs.size(); i++) {
162  os << " " << pairs[i] << std::endl;
163  }
164  os << ")";
165 
166  return os;
167 }
168 
169 #endif
tgPairs & operator-=(const tgPairs &other)
Definition: tgPairs.h:122
Definition of class tgPair.
static btQuaternion getQuaternionBetween(btVector3 a, btVector3 b)
Definition: tgUtil.h:182
Contains the definition of class tgTaggables $Id$.
Definition: tgPair.h:47
std::ostream & operator<<(std::ostream &os, const tgPairs &p)
Definition: tgPairs.h:156
Contains the definition of class tgUtil and overloaded operator<<() free functions.
Definition: tgTags.h:43