NTRT Simulator
 All Classes Files Functions Variables Typedefs Friends Pages
tgStructure.cpp
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 // This module
27 #include "tgStructure.h"
28 // This library
29 #include "tgNode.h"
30 #include "tgPair.h"
31 // The Bullet Physics library
32 #include <LinearMath/btQuaternion.h>
33 #include <LinearMath/btVector3.h>
34 
35 tgStructure::tgStructure() : tgTaggable()
36 {
37 }
38 
39 tgStructure::tgStructure(const tgTags& tags) : tgTaggable(tags)
40 {
41 }
42 
43 tgStructure::tgStructure(const std::string& space_separated_tags) : tgTaggable(space_separated_tags)
44 {
45 }
46 
47 tgStructure::~tgStructure()
48 {
49  for (std::size_t i = 0; i < m_children.size(); ++i)
50  {
51  delete m_children[i];
52  }
53 }
54 
55 void tgStructure::addNode(double x, double y, double z, std::string tags)
56 {
57  m_nodes.addNode(x, y, z, tags);
58 }
59 
61 {
62  m_nodes.addNode(newNode);;
63 }
64 
65 void tgStructure::addPair(int fromNodeIdx, int toNodeIdx, std::string tags)
66 {
67  addPair(m_nodes[fromNodeIdx], m_nodes[toNodeIdx], tags);
68 }
69 
70 void tgStructure::addPair(const btVector3& from, const btVector3& to, std::string tags)
71 {
72  // @todo: do we need to pass in tags here? might be able to save some proc time if not...
73  tgPair p = tgPair(from, to);
74  if (!m_pairs.contains(p))
75  {
76  m_pairs.addPair(tgPair(from, to, tags));
77  }
78  else
79  {
80  std::ostringstream os;
81  os << "A pair matching " << p << " already exists in this structure.";
82  throw tgException(os.str());
83  }
84 }
85 
86 void tgStructure::move(const btVector3& offset)
87 {
88  m_nodes.move(offset);
89  m_pairs.move(offset);
90  for (size_t i = 0; i < m_children.size(); ++i)
91  {
92  tgStructure * const pStructure = m_children[i];
93  assert(pStructure != NULL);
94  pStructure->move(offset);
95  }
96 }
97 
98 void tgStructure::addRotation(const btVector3& fixedPoint,
99  const btVector3& axis,
100  double angle)
101 {
102  const btQuaternion rotation(axis, angle);
103  addRotation(fixedPoint, rotation);
104 }
105 
106 void tgStructure::addRotation(const btVector3& fixedPoint,
107  const btVector3& fromOrientation,
108  const btVector3& toOrientation)
109 {
110  addRotation(fixedPoint,
111  tgUtil::getQuaternionBetween(fromOrientation,
112  toOrientation));
113 }
114 
115 void tgStructure::addRotation(const btVector3& fixedPoint,
116  const btQuaternion& rotation)
117 {
118  m_nodes.addRotation(fixedPoint, rotation);
119  m_pairs.addRotation(fixedPoint, rotation);
120  for (std::size_t i = 0; i < m_children.size(); ++i)
121  {
122  tgStructure * const pStructure = m_children[i];
123  assert(pStructure != NULL);
124  pStructure->addRotation(fixedPoint, rotation);
125  }
126 }
127 
129 {
134  if (pChild != NULL)
135  {
136  m_children.push_back(pChild);
137  }
138 }
void addChild(tgStructure *child)
Definition of class tgPair.
static btQuaternion getQuaternionBetween(btVector3 a, btVector3 b)
Definition: tgUtil.h:182
void addPair(int fromNodeIdx, int toNodeIdx, std::string tags="")
Definition: tgStructure.cpp:65
void addRotation(const btVector3 &fixedPoint, const btVector3 &axis, double angle)
Definition: tgStructure.cpp:98
Definition of class tgNode.
Definition: tgPair.h:47
Definition: tgNode.h:43
Definition of class tgStructure.
void move(const btVector3 &offset)
Definition: tgNodes.h:174
int addNode(const btVector3 &node)
Definition: tgNodes.h:128
Definition: tgTags.h:43
void addNode(double x, double y, double z, std::string tags="")
Definition: tgStructure.cpp:55