//----------------------------------------------------------------------------- // File: HelloVector.cpp // Class: None // Parent: None // Children: None // Purpose: Vector Calculations // Author: Sanjay Dastoor - May 2, 2007 //----------------------------------------------------------------------------- // The following are standard C/C++ header files. // If a filename is enclosed inside < > it means the header file is in the Include directory. // If a filename is enclosed inside " " it means the header file is in the current directory. #include // Character Types #include // Mathematical Constants #include // Variable Argument Lists #include // Standard Input/Output Functions #include // Utility Functions #include // String Operations #include // Signals (Contol-C + Unix System Calls) #include // Nonlocal Goto (For Control-C) #include // Time and Date information #include // Verify Program Assertion #include // Error Codes (Used in Unix system()) #include // Floating Point Constants #include // Implementation Constants #include // Standard Definitions #include // Exception handling (e.g., try, catch throw) //----------------------------------------------------------------------------- #include "SimTKsimbody.h" using namespace SimTK; using namespace std; //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // Prototypes for local functions (functions not called by code in other files) //----------------------------------------------------------------------------- bool WriteDoubleToFile( double x, int precision, FILE *fptr ); bool WriteVec3ToFile( const Vec3 &v, int precision, FILE *fptr ); bool WriteMat33ToFile( const Mat33 &m, int precision, FILE *fptr ); FILE* FileOpenWithMessageIfCannotOpen( const char *filename, const char *attribute ); bool WriteStringToFile( const char outputString[], FILE *fptr ) { return fputs( outputString, fptr ) != EOF; } int main( int numberOfCommandLineArguments, char *arrayOfCommandLineArguments[] ) { FILE *outputFile = FileOpenWithMessageIfCannotOpen("HelloCenterOfMass.txt","w"); //Part 3.2 double m = 12/6.022e23; Vec3 r_Q_O(1e-10,2e-10,0); Inertia I_Q_O(r_Q_O,m); Mat33 I_Q_O_matrix = I_Q_O.toMat33(); WriteStringToFile("\n\nI_Q_O = \n",outputFile); WriteMat33ToFile(I_Q_O_matrix,3,outputFile); //Part 3.4 m = 1; Vec3 r_Q1_O(-1,0,0); Vec3 r_Q2_O(0,1,0); Vec3 r_Q3_O(1,0,0); Vec3 r_Q4_O(0,-1,0); Inertia I_Q1_O(r_Q1_O,m); Inertia I_Q2_O(r_Q2_O,2*m); Inertia I_Q3_O(r_Q3_O,2*m); Inertia I_Q4_O(r_Q4_O,2*m); Vec3 r_Scm_O = (r_Q1_O + 2*r_Q2_O + 2*r_Q3_O + 2*r_Q4_O)/7; Inertia I_S_O = I_Q1_O + I_Q2_O + I_Q3_O + I_Q4_O; Inertia I_S_Scm = I_S_O.shiftToMassCenter(r_Scm_O,7); Mat33 I_S_O_matrix = I_S_O.toMat33(); WriteStringToFile("\n\nI_S_O = \n",outputFile); WriteMat33ToFile(I_S_O_matrix,4,outputFile); Mat33 I_S_Scm_matrix = I_S_Scm.toMat33(); WriteStringToFile("\n\nI_S_Scm = \n",outputFile); WriteMat33ToFile(I_S_Scm_matrix,4,outputFile); fclose(outputFile); } //----------------------------------------------------------------------------- FILE* FileOpenWithMessageIfCannotOpen( const char *filename, const char *attribute ) { // Try to open the file FILE *fptr = fopen( filename, attribute ); // If unable to open the file, issue a message if( !fptr ) { printf( "\n\n Unable to open the file: " ); printf( filename ); printf( "\n\n" ); } return fptr; } //----------------------------------------------------------------------------- bool WriteDoubleToFile( double x, int precision, FILE *fptr ) { // Ensure the precision (number of digits in the mantissa after the decimal point) makes sense. // Next, calculate the field width so it includes one extra space to the right of the number. if( precision < 0 || precision > 17 ) precision = 5; int fieldWidth = precision + 1; // Create the format specifier and print the number char format[20]; sprintf( format, " %%- %d.%dg", fieldWidth, precision ); return fprintf( fptr, format, x ) >= 0; } //----------------------------------------------------------------------------- bool WriteVec3ToFile( const Vec3 &v, int precision, FILE *fptr ) { //write vector in format (v[0],v[1],v[2]) if( !WriteStringToFile("(",fptr)) return false; if( !WriteDoubleToFile( v[0], precision, fptr ) ) return false; if( !WriteStringToFile(",",fptr)) return false; if( !WriteDoubleToFile( v[1], precision, fptr ) ) return false; if( !WriteStringToFile(",",fptr)) return false; if( !WriteDoubleToFile( v[2], precision, fptr ) ) return false; if( !WriteStringToFile(")",fptr)) return false; return true; } //----------------------------------------------------------------------------- bool WriteMat33ToFile( const Mat33 &m, int precision, FILE *fptr ) { for( unsigned int i=0; i<3; i++ ) { for( unsigned int j=0; j<3; j++ ) { const Real mij = m[i][j]; if( !WriteDoubleToFile( mij, precision, fptr ) ) return false; } if( i<=1 && !WriteStringToFile( "\n", fptr ) ) return false; } return true; }