19 #include "FlemonsSpineModel.h"
23 #include "btBulletDynamicsCommon.h"
39 FlemonsSpineModel::FlemonsSpineModel(
int segments) :
49 const double density = 4.2/300.0;
50 const double radius = 0.5;
62 tetra.
addNode(v_size, v_size, v_size);
63 tetra.
addNode(v_size, -v_size, -v_size);
64 tetra.
addNode(-v_size, v_size,-v_size);
65 tetra.
addNode(-v_size, -v_size, v_size);
67 tetra.
addPair(0,1,
"front rod");
68 tetra.
addPair(0,2,
"right rod");
73 tetra.move(btVector3(0.0,15.0,100.0));
78 btVector3 offset(0,0,-v_size * 1.15);
79 for(
int i = 0; i < m_segments; i++) {
82 t->addTags(
tgString(
"segment num", i + 1));
83 t->move((i + 1)*offset);
91 std::vector<tgStructure*> children = snake.
getChildren();
92 for(
int i = 1; i < children.size(); i++) {
93 tgNodes n0 = children[i-1]->getNodes();
94 tgNodes n1 = children[i]->getNodes();
95 #if (0) // Conditional compile for "regular" vs "saddle joints"
116 spec.addBuilder(
"rod",
new tgRodInfo(rodConfig));
126 structureInfo.buildInto(*
this, world);
130 allMuscles = tgCast::filter<tgModel, tgLinearString> (
getDescendants());
148 std::cout <<
"Model: " << std::endl;
149 std::cout << *
this << std::endl;
152 std::vector<tgRod*> rodSegments = tgCast::filter<tgModel, tgRod> (
getDescendants());
153 for(
int i = 0; i < rodSegments.size(); i++) {
154 std::cout <<
"Structure: " << *(rodSegments[i]) << std::endl;
157 allSegments = this->find<tgModel> (
"segment");
158 for(
int i = 0; i < allSegments.size(); i++) {
159 std::cout <<
"Segs: " << *(allSegments[i]) << std::endl;
174 std::vector<double> COM = getSegmentCOM(0);
176 std::cout <<
"X: " << COM[0] <<
" Y: " << COM[1] <<
" Z: " << COM[2]
184 const std::vector<double> FlemonsSpineModel::getSegmentCOM(
const int n)
const
186 if(n < 0) {
throw std::range_error(
"Negative segment number"); }
187 if(n >= m_segments) {
throw std::range_error(
tgString(
"Segment number > ", m_segments)); }
189 std::vector<tgRod*> p_rods = tgCast::filter<tgModel, tgRod> (allSegments[n]->getDescendants());
192 assert( p_rods.size() > 0);
194 btVector3 segmentCenterOfMass(0, 0, 0);
195 double segmentMass = 0.0;
196 for (std::size_t i = 0; i < p_rods.size(); i++)
198 const tgRod*
const pRod = p_rods[i];
199 assert(pRod != NULL);
200 const double rodMass = pRod->
mass();
202 segmentCenterOfMass += rodCenterOfMass * rodMass;
203 segmentMass += rodMass;
207 assert(segmentMass > 0.0);
209 segmentCenterOfMass /= segmentMass;
212 std::vector<double> result(3);
213 for (
size_t i = 0; i < 3; ++i) { result[i] = segmentCenterOfMass[i]; }
218 void FlemonsSpineModel::changeMuscle (
double length,
double dt)
220 for(
int i = 0; i < allMuscles.size(); i++){
221 allMuscles[i]->setRestLength(length, dt);
virtual void setup(tgWorld &world)
const std::vector< tgStructure * > & getChildren() const
virtual void setup(tgWorld &world)
void addChild(tgStructure *child)
Definition of class tgRodInfo.
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.
void addPair(int fromNodeIdx, int toNodeIdx, std::string tags="")
btVector3 centerOfMass() const
Definition of class tgConnectorInfo.
virtual void step(double dt)
Contains the definition of class tgLinearString.
std::string tgString(std::string s, int i)
Definition of class tgStructure.
Definition of class tgStructureInfo.
Definition of class tgLinearStringInfo.
Contains the definition of class tgUtil and overloaded operator<<() free functions.
Definition of class tgBuildSpec.
Definition of class tgRigidAutoCompound.
void notifyStep(double dt)
std::vector< tgModel * > getDescendants() const
void addNode(double x, double y, double z, std::string tags="")