29 #include "BaseQuadModelLearning.h"
41 BaseQuadModelLearning::BaseQuadModelLearning(
int segments,
int hips) :
46 m_subStructures = m_hips + m_segments;
49 BaseQuadModelLearning::~BaseQuadModelLearning()
70 m_allSegments.clear();
84 const std::vector<tgSpringCableActuator*>&
85 BaseQuadModelLearning::getMuscles (
const std::string& key)
const
87 const MuscleMap::const_iterator it = m_muscleMap.find(key);
88 if (it == m_muscleMap.end())
90 throw std::invalid_argument(
"Key '" + key +
"' not found in muscle map");
98 const std::vector<tgSpringCableActuator*>& BaseQuadModelLearning::getAllMuscles()
const
103 const std::vector<tgBaseRigid*> BaseQuadModelLearning::getAllRigids()
const
105 if (m_allSegments.size() != m_subStructures)
107 throw std::runtime_error(
"Not initialized");
110 std::vector<tgBaseRigid*> p_rods;
112 for (std::size_t i = 0; i < m_allSegments.size(); i++)
114 std::vector<tgBaseRigid*> temp = tgCast::filter<tgModel, tgBaseRigid> (m_allSegments[i]->getDescendants());
115 p_rods.insert(p_rods.end(), temp.begin(), temp.end());
121 const int BaseQuadModelLearning::getSegments()
const
126 std::vector<double> BaseQuadModelLearning::getCOM(
const int n)
128 if (m_allSegments.size() != m_subStructures)
130 throw std::runtime_error(
"Not initialized");
134 throw std::range_error(
"Negative segment number");
136 else if (n >= m_subStructures)
138 throw std::range_error(
tgString(
"Segment number > ", m_segments));
141 std::vector<double> centerOfMass(3);
142 for (
size_t i = 0; i < 3; ++i) { centerOfMass[i] = 0; }
144 for (std::size_t i = n; i < m_subStructures; i++)
146 std::vector<double> segmentCOM = getSegmentCOM(i);
147 for (std::size_t j = 0; j < 3; j++)
149 segmentCOM[j] *= segmentMasses[i];
151 for (std::size_t j = 0; j < 3; j++)
153 centerOfMass[j] += segmentCOM[j];
155 mass += segmentMasses[i];
160 for (std::size_t j = 0; j < 3; j++)
162 centerOfMass[j] /= mass;
168 std::vector<double> BaseQuadModelLearning::getSegmentCOM(
const int n)
171 btVector3 segmentCenterOfMass = getSegmentCOMVector(n);
174 std::vector<double> result(3);
175 for (
size_t i = 0; i < 3; ++i) { result[i] = segmentCenterOfMass[i]; }
180 btVector3 BaseQuadModelLearning::getSegmentCOMVector(
const int n)
182 if (m_allSegments.size() != m_subStructures)
184 throw std::runtime_error(
"Not initialized");
188 throw std::range_error(
"Negative segment number");
190 else if (n >= m_subStructures)
192 throw std::range_error(
tgString(
"Segment number > ", m_segments));
195 std::vector<tgRod*> p_rods =
196 tgCast::filter<tgModel, tgRod> (m_allSegments[n]->getDescendants());
199 assert(!p_rods.empty());
201 btVector3 segmentCenterOfMass(0, 0, 0);
202 double segmentMass = 0.0;
203 for (std::size_t i = 0; i < p_rods.size(); i++)
205 const tgRod*
const pRod = p_rods[i];
206 assert(pRod != NULL);
207 const double rodMass = pRod->
mass();
210 segmentCenterOfMass += rodCenterOfMass * rodMass;
211 segmentMass += rodMass;
215 assert(segmentMass > 0.0);
217 segmentMasses.push_back(segmentMass);
219 segmentCenterOfMass /= segmentMass;
221 return segmentCenterOfMass;
224 double BaseQuadModelLearning::getSpineLength()
226 const btVector3 start = getSegmentCOMVector(0);
227 const btVector3 end = getSegmentCOMVector(m_segments - 1);
229 return (start - end).length();
virtual void setup(tgWorld &world)
Create a box shape as an obstacle or add it to your tensegrity.
virtual void setup(tgWorld &world)
Convenience function for combining strings with ints, mostly for naming structures.
virtual void step(double dt)
Utility class for class casting and filtering collections by type.
virtual void step(double dt)
virtual btVector3 centerOfMass() const
Contains the definition of abstract base class tgSpringCableActuator. Assumes that the string is line...
std::string tgString(std::string s, int i)
virtual double mass() const
Contains the definition of class tgRod.
void notifyStep(double dt)