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