NTRT Simulator
 All Classes Files Functions Variables Typedefs Friends Pages
tgUtil.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_UTIL_H
20 #define TG_UTIL_H
21 
30 #include "btBulletDynamicsCommon.h"
31 #include "LinearMath/btQuaternion.h"
32 #include "LinearMath/btTransform.h"
33 #include "LinearMath/btVector3.h"
34 #include <cmath>
35 #include <iostream>
36 #include <sstream>
37 #include <string>
38 
39 #include "tgRigidInfo.h"
40 
45 class tgUtil
46 {
47 public:
48 
53  inline static btVector3 upVector()
54  {
55  return btVector3(0.0, 1.0, 0.0);
56  };
57 
62  inline static std::string degSymbol()
63  {
64  return "\u00B0";
65  }
66 
73  inline static btVector3 center(const btVector3& start,
74  const btVector3& end)
75  {
76  return (start + end) / 2.0;
77  }
78 
89  inline static btTransform getTransform(const btVector3& startOrientation,
90  const btVector3& start,
91  const btVector3& end)
92  {
93 
94  const btVector3 origin = center(start, end);
95  btTransform t = btTransform();
96  t.setIdentity();
97  t.setOrigin(origin);
98  t.setRotation(getQuaternionBetween(startOrientation,
99  getVector(start, end)));
100  return t;
101  }
102 
108  inline static btTransform getTransform(const btVector3& start,
109  const btVector3& end)
110  {
111  return getTransform(upVector(), start, end);
112  }
113 
120  static btVector3 getVector(const btVector3& from,
121  const btVector3& to)
122  {
123  return to - from;
124  }
125 
144  static btVector3 getRadiusVector(btVector3 axis,
145  double radius,
146  btVector3 target)
147  {
148  // NOTE: axis and target are passed by value,
149  // so we can alter them.
150  axis.normalize();
151  target.normalize();
152  // Get a vector normal to both
153  const btVector3 norm = axis.cross(target);
154  // Rotate the axis by 90 degrees around the normal vector
155  axis.rotate(norm, (M_PI / 2.0));
156  return axis;
157  }
158 
167  static bool almostEqual(const btVector3& from,
168  const btVector3& to,
169  int precision = 5)
170  {
171  return from.distance(to) < pow(10.0, (precision < 0 ? 5 : -precision));
172  }
173 
182  static btQuaternion getQuaternionBetween(btVector3 a, btVector3 b)
183  {
184  a.normalize();
185  b.normalize();
186 
187  // The return value
188  btQuaternion result;
189 
190  // Account for equal vectors (can't calculate c in this case)
191  if (almostEqual(a, b)) {
192  result = btQuaternion::getIdentity();
193  } else if (almostEqual(a, -b)) {
194  // Account for opposing vectors (can't calculate c in
195  // this case either)
196  // What to do here?
197  const btVector3 arb =
199  const btVector3 c = (a.cross(arb)).normalize();
200  result = btQuaternion(c, M_PI).normalize();;
201  } else {
202  // Create a vector normal to both a and b
203  const btVector3 c = (a.cross(b)).normalize();
204 
205  // Create a quaternion that represents a rotation about
206  // c by the angle between a and b
207  result = btQuaternion(c, acos(a.dot(b))).normalize();
208  }
209  return result;
210  }
211 
217  inline static btVector3 getArbitraryNonParallelVector(btVector3 v)
218  {
219  btVector3 arb;
220  v.normalize();
221  do {
222  arb = btVector3(rand()%10, rand()%10, rand()%10).normalize();
223  } while (arb == v || arb == -v);
224  return arb;
225  }
226 
230  inline static void addRotation(btVector3& v,
231  const btVector3& fixedPoint,
232  const btVector3& axis,
233  double angle)
234  {
235  // Get a vector from fixedPoint to this
236  btVector3 relative = v - fixedPoint;
237 
238  // Rotate the relative vector
239  btVector3 rotated = relative.rotate(axis, angle);
240 
241  // Set our new values
242  v.setX(fixedPoint.x() + rotated.x());
243  v.setY(fixedPoint.y() + rotated.y());
244  v.setZ(fixedPoint.z() + rotated.z());
245  }
246 
250  inline static void addRotation(btVector3& v,
251  const btVector3& fixedPoint,
252  const btVector3& fromOrientation,
253  const btVector3& toOrientation)
254  {
255  btQuaternion rotation = getQuaternionBetween(fromOrientation, toOrientation);
256  addRotation(v, fixedPoint, rotation);
257  }
258 
262  inline static void addRotation(btVector3& v,
263  const btVector3& fixedPoint,
264  const btQuaternion& rotation)
265  {
266  addRotation(v, fixedPoint, rotation.getAxis(), rotation.getAngle());
267  }
268 
269 
276  inline static double rad2deg(double radians)
277  {
278  return radians * 180.0 / M_PI;
279  }
280 
287  inline static double deg2rad(double degrees)
288  {
289  return degrees * (M_PI / 180.0);
290  }
291 
299  inline static std::string strDeg(double degrees) {
300  std::ostringstream s;
301  s << degrees << degSymbol();
302  return s.str();
303  }
304  //static std::string strDeg(double degrees);
305 
306  inline static double round(double d, int precision = 5)
307  {
308  const double base = 10.0;
309  const int m = static_cast<int>(pow(base, precision));
310  return floor(d * m + 0.5)/m;
311  }
312 
313 };
314 
322 inline std::ostream&
323 operator<<(std::ostream& os, const btQuaternion& q)
324 {
325  os << "btQuaternion( "
326  << q.x() << ", " << q.y() << ", " << q.z() << ", " << q.w()
327  << " )";
328  return os;
329 }
330 
338 inline std::ostream&
339 operator<<(std::ostream& os, const btVector3& v)
340 {
341  os << "btVector3( " << v.x() << ", " << v.y() << ", " << v.z() << " )";
342  return os;
343 }
344 
352 inline std::ostream&
353 operator<<(std::ostream& os, const btTransform& xf)
354 {
355  os << "btTransform: origin = " << xf.getOrigin()
356  << "; rotation = " << xf.getRotation();
357  return os;
358 }
359 
367 inline std::ostream&
368 operator<<(std::ostream& os, const btRigidBody& rb)
369 {
370  os << "btRigidBody: " << rb.getCollisionShape();
371  return os;
372 }
373 
381 inline std::ostream&
382 operator<<(std::ostream& os, const btCollisionShape& cs)
383 {
384  os << "btCollisionShape: " << cs.getShapeType();
385  return os;
386 }
387 
395 inline std::ostream&
396 operator<<(std::ostream& os, const btCompoundShape& cs)
397 {
398  os << "btCompoundShape: " << cs.getShapeType() << std::endl
399  << " # Children: " << cs.getNumChildShapes() << std::endl;
400  return os;
401 }
402 
403 
404 
405 #endif // TG_UTIL_H
Definition of abstract class tgRigidInfo.
std::ostream & operator<<(std::ostream &os, const btQuaternion &q)
Definition: tgUtil.h:323
static btVector3 getVector(const btVector3 &from, const btVector3 &to)
Definition: tgUtil.h:120
static btVector3 getArbitraryNonParallelVector(btVector3 v)
Definition: tgUtil.h:217
Definition: tgUtil.h:45
static std::string degSymbol()
Definition: tgUtil.h:62
static bool almostEqual(const btVector3 &from, const btVector3 &to, int precision=5)
Definition: tgUtil.h:167
static btQuaternion getQuaternionBetween(btVector3 a, btVector3 b)
Definition: tgUtil.h:182
static btVector3 upVector()
Definition: tgUtil.h:53
static void addRotation(btVector3 &v, const btVector3 &fixedPoint, const btVector3 &fromOrientation, const btVector3 &toOrientation)
Definition: tgUtil.h:250
static double deg2rad(double degrees)
Definition: tgUtil.h:287
static btVector3 center(const btVector3 &start, const btVector3 &end)
Definition: tgUtil.h:73
static void addRotation(btVector3 &v, const btVector3 &fixedPoint, const btQuaternion &rotation)
Definition: tgUtil.h:262
static btVector3 getRadiusVector(btVector3 axis, double radius, btVector3 target)
Definition: tgUtil.h:144
static btTransform getTransform(const btVector3 &startOrientation, const btVector3 &start, const btVector3 &end)
Definition: tgUtil.h:89
static btTransform getTransform(const btVector3 &start, const btVector3 &end)
Definition: tgUtil.h:108
static std::string strDeg(double degrees)
Definition: tgUtil.h:299
static void addRotation(btVector3 &v, const btVector3 &fixedPoint, const btVector3 &axis, double angle)
Definition: tgUtil.h:230
static double rad2deg(double radians)
Definition: tgUtil.h:276