40 BaseSpineModelLearning::BaseSpineModelLearning(
int segments) :
46 BaseSpineModelLearning::~BaseSpineModelLearning()
67 m_allSegments.clear();
81 const std::vector<tgSpringCableActuator*>&
82 BaseSpineModelLearning::getMuscles (
const std::string& key)
const
84 const MuscleMap::const_iterator it = m_muscleMap.find(key);
85 if (it == m_muscleMap.end())
87 throw std::invalid_argument(
"Key '" + key +
"' not found in muscle map");
95 const std::vector<tgSpringCableActuator*>& BaseSpineModelLearning::getAllMuscles()
const
100 const std::vector<tgBaseRigid*> BaseSpineModelLearning::getAllRigids()
const
102 if (m_allSegments.size() != m_segments)
104 throw std::runtime_error(
"Not initialized");
107 std::vector<tgBaseRigid*> p_rods;
109 for (std::size_t i = 0; i < m_allSegments.size(); i++)
111 std::vector<tgBaseRigid*> temp = tgCast::filter<tgModel, tgBaseRigid> (m_allSegments[i]->getDescendants());
112 p_rods.insert(p_rods.end(), temp.begin(), temp.end());
118 const int BaseSpineModelLearning::getSegments()
const
123 std::vector<double> BaseSpineModelLearning::getSegmentCOM(
const int n)
const
126 btVector3 segmentCenterOfMass = getSegmentCOMVector(n);
129 std::vector<double> result(3);
130 for (
size_t i = 0; i < 3; ++i) { result[i] = segmentCenterOfMass[i]; }
135 btVector3 BaseSpineModelLearning::getSegmentCOMVector(
const int n)
const
137 if (m_allSegments.size() != m_segments)
139 throw std::runtime_error(
"Not initialized");
143 throw std::range_error(
"Negative segment number");
145 else if (n >= m_segments)
147 throw std::range_error(
tgString(
"Segment number > ", m_segments));
150 std::vector<tgRod*> p_rods =
151 tgCast::filter<tgModel, tgRod> (m_allSegments[n]->getDescendants());
154 assert(!p_rods.empty());
156 btVector3 segmentCenterOfMass(0, 0, 0);
157 double segmentMass = 0.0;
158 for (std::size_t i = 0; i < p_rods.size(); i++)
160 const tgRod*
const pRod = p_rods[i];
161 assert(pRod != NULL);
162 const double rodMass = pRod->
mass();
165 segmentCenterOfMass += rodCenterOfMass * rodMass;
166 segmentMass += rodMass;
170 assert(segmentMass > 0.0);
172 segmentCenterOfMass /= segmentMass;
174 return segmentCenterOfMass;
177 double BaseSpineModelLearning::getSpineLength()
const
179 const btVector3 start = getSegmentCOMVector(0);
180 const btVector3 end = getSegmentCOMVector(m_segments - 1);
182 return (start - end).length();
virtual void setup(tgWorld &world)
Create a box shape as an obstacle or add it to your tensegrity.
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 setup(tgWorld &world)
virtual btVector3 centerOfMass() const
A template base class for a tensegrity spine.
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.
virtual void step(double dt)
void notifyStep(double dt)