1 /*
2  *_________________________________________________________________________*
3  *      POEMS: PARALLELIZABLE OPEN SOURCE EFFICIENT MULTIBODY SOFTWARE     *
4  *      DESCRIPTION: SEE READ-ME                                           *
5  *      FILE NAME: body.cpp                                                *
6  *      AUTHORS: See Author List                                           *
7  *      GRANTS: See Grants List                                            *
8  *      COPYRIGHT: (C) 2005 by Authors as listed in Author's List          *
9  *      LICENSE: Please see License Agreement                              *
10  *      DOWNLOAD: Free at www.rpi.edu/~anderk5                             *
11  *      ADMINISTRATOR: Prof. Kurt Anderson                                 *
12  *                     Computational Dynamics Lab                          *
13  *                     Rensselaer Polytechnic Institute                    *
14  *                     110 8th St. Troy NY 12180                           *
15  *      CONTACT:        anderk5@rpi.edu                                    *
16  *_________________________________________________________________________*/
17 
18 #include "bodies.h"
19 #include "point.h"
20 
21 using namespace std;
22 
Body()23 Body::Body()
24 {
25   inertia.Zeros();
26   mass = 0;
27   a_t.Zeros();
28 }
29 
~Body()30 Body::~Body()
31 {
32   points.DeleteValues();
33 }
34 
ReadIn(istream & in)35 bool Body::ReadIn(istream& in){
36   return (ReadInBodyData(in) && ReadInPoints(in));
37 }
38 
WriteOut(ostream & out)39 void Body::WriteOut(ostream& out){
40   // write out header <type> <name>
41   out << GetType() << ' ' << GetName() << endl;
42 
43   // write out body specific data
44   WriteOutBodyData(out);
45 
46   // write out points
47   WriteOutPoints(out);
48 
49 }
50 
ReadInPoints(istream & in)51 bool Body::ReadInPoints(istream& in){
52   // get numfixed points
53   int numpoints;
54   int index;
55   Point* point;
56   int pointtype;
57   char pointname[256];
58 
59   in >> numpoints;
60   for(int i=points.GetNumElements();i<numpoints;i++){
61     // error check
62     in >> index;
63     if(index != i){
64       cerr << "Invalid file format" << endl;
65       return false;
66     }
67 
68     in >> pointtype >> pointname;
69     point = NewPoint(pointtype);
70     if(!point){
71       cerr << "Unrecognized point type '" << pointtype << endl;
72       return false;
73     }
74 
75     // add the point
76     AddPoint(point);
77 
78     // set generic point info
79     point->ChangeName(pointname);
80 
81     // read in the rest of its data
82     if(!point->ReadIn(in)) return false;
83   }
84   return true;
85 }
86 
WriteOutPoints(std::ostream & out)87 void Body::WriteOutPoints(std::ostream& out){
88   int numpoints = points.GetNumElements();
89 
90   out << numpoints << endl;
91   // list element pointer
92   ListElement<Point>* ele = points.GetHeadElement();
93 
94   // set the ID of each point
95   for(int i=0;i<numpoints;i++){
96     ele->value->SetID(i);
97     out << i << ' ';
98     ele->value->WriteOut(out);
99 
100     // increment list element pointer
101     ele = ele->next;
102   }
103   out << endl;
104 }
105 
GetPoint(int p)106 Point* Body::GetPoint(int p) {
107   return points(p);
108 }
109 
AddJoint(Joint * joint)110 void Body::AddJoint(Joint* joint){
111   joints.Append(joint);
112 }
113 
AddPoint(Point * point)114 void Body::AddPoint(Point* point){
115   points.Append(point);
116 }
117 
118 //
119 // global body functions
120 //
121 
NewBody(int type)122 Body* NewBody(int type){
123   switch( BodyType(type) )
124     {
125       case INERTIALFRAME :  // The inertial reference frame
126         return new InertialFrame;
127       case RIGIDBODY :  // A Rigid Body
128         return new RigidBody;
129       case PARTICLE :  // A Particle
130         return new Particle;
131       default  :  // error
132         return 0;
133     }
134 }
135