//----------------------------------------------------------------------------- // File: HelloVector.cpp // Class: None // Parent: None // Children: None // Purpose: Performs several operations on two specified vectors //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // 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 WriteVec3ToFile( const Vec3 &v, int precision, FILE *fptr ); bool WriteMat33RowToFile( const Mat33 &m, int precision, FILE *fptr, int row ); bool WriteMat33ToFile( const Mat33 &m, int precision, FILE *fptr ); bool WriteDoubleToFileWithSpaceAndSpecifiedPrecision( double x, int precision, FILE *fptr); FILE* FileOpenWithMessageIfCannotOpen( const char *filename, const char *attribute); //----------------------------------------------------------------------------- int main( void ) { // Write " Hello vector!" to the screen and then put a newline printf( " Hello math!\n" ); Vec3 v( 2, 3, 4 ); Vec3 w( 5, -6, 7 ); Vec3 vtimes10 = 10*v; Vec3 vover10 = v/10; Vec3 sumVW = v + w; Vec3 difVW = v - w; Real dotVW = dot( v, w ); Vec3 crossVW = cross( v, w ); Vec3 crossWV = cross( w, v ); Real squareV = dot( v, v ); Vec3 cubeV = squareV*v; Real magV = sqrt( squareV ); Real magW = sqrt( dot( w, w ) ); Vec3 unitV = v / magV ; Real radVW = acos( dotVW/(magV*magW) ); Real degVW = radVW * 180 / 3.141592 ; Mat33 dyadVW = outer(v,w) ; Mat33 dyadWV = outer(w,v); // Write results to file HelloVectorResults.txt FILE *fptr1 = FileOpenWithMessageIfCannotOpen( "HelloVectorResults.txt", "w+" ); fprintf( fptr1, "v = " ); WriteVec3ToFile( v, 2, fptr1); fprintf( fptr1, "w = " ); WriteVec3ToFile( w, 2, fptr1); fprintf( fptr1, "10v = " ); WriteVec3ToFile( vtimes10, 2, fptr1); fprintf( fptr1, "v/10 = " ); WriteVec3ToFile( vover10, 2, fptr1); fprintf( fptr1, "v + w = " ); WriteVec3ToFile( sumVW, 2, fptr1); fprintf( fptr1, "v - w = " ); WriteVec3ToFile( difVW, 2, fptr1); fprintf( fptr1, "dot(v,w) = " ); WriteDoubleToFileWithSpaceAndSpecifiedPrecision( dotVW, 2, fptr1); fprintf( fptr1, "cross(v,w) = " ); WriteVec3ToFile( crossVW, 5, fptr1); fprintf( fptr1, "cross(w,v) = " ); WriteVec3ToFile( crossWV, 5, fptr1); fprintf( fptr1, "v^2 = " ); WriteDoubleToFileWithSpaceAndSpecifiedPrecision( squareV, 5, fptr1); fprintf( fptr1, "v^3 = " ); WriteVec3ToFile( cubeV, 5, fptr1); fprintf( fptr1, "magnitude(v) = " ); WriteDoubleToFileWithSpaceAndSpecifiedPrecision( magV, 5, fptr1); fprintf( fptr1, "magnitude(w) = " ); WriteDoubleToFileWithSpaceAndSpecifiedPrecision( magW, 5, fptr1); fprintf( fptr1, "unit(v) = " ); WriteVec3ToFile( unitV, 5, fptr1); fprintf( fptr1, "angle between v and w (rad) = " ); WriteDoubleToFileWithSpaceAndSpecifiedPrecision( radVW, 5, fptr1); fprintf( fptr1, "angle between v and w (deg) = " ); WriteDoubleToFileWithSpaceAndSpecifiedPrecision( degVW, 5, fptr1); fprintf( fptr1, "v * w = \n" ); WriteMat33ToFile( dyadVW, 5, fptr1); fprintf( fptr1, "w * v = \n" ); WriteMat33ToFile( dyadWV, 5, fptr1); // Keep the screen displayed until the user presses the Enter key printf( "\n\n Press Enter to terminate the program: " ); int key = getchar(); return 0; } //----------------------------------------------------------------------------- bool WriteVec3ToFile( const Vec3 &v, int precision, FILE *fptr ) { if( precision < 0 || precision > 17 ) precision = 2; return fprintf( fptr, " %g n_x + %g n_y + %g n_z \n\n", v[0], v[1], v[2] ) > 0; } //----------------------------------------------------------------------------- bool WriteMat33RowToFile( const Mat33 &m, int precision, FILE *fptr, int row ) { if( precision < 0 || precision > 17 ) precision = 5; int fieldWidth = precision + 2; // Create the format specifier and print the number char format[40]; sprintf( format, "%%- %d.%dg", fieldWidth, precision); fprintf( fptr, "[ "); fprintf( fptr, format, m[row][0]); fprintf( fptr, format, m[row][1]); fprintf( fptr, format, m[row][2]); fprintf( fptr, " ] \n"); return true; } bool WriteMat33ToFile( const Mat33 &m, int precision, FILE *fptr ) { WriteMat33RowToFile( m, precision, fptr, 0 ); WriteMat33RowToFile( m, precision, fptr, 1 ); WriteMat33RowToFile( m, precision, fptr, 2 ); fprintf( fptr, "\n"); return true; } //----------------------------------------------------------------------------- bool WriteDoubleToFileWithSpaceAndSpecifiedPrecision( 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 + 4; // Create the format specifier and print the number char format[20]; sprintf( format, "%%- %d.%dg\n\n", fieldWidth, precision ); return fprintf( fptr, format, x ) > 0; } //----------------------------------------------------------------------------- FILE* FileOpenWithMessageIfCannotOpen( const char *filename, const char *attribute) { FILE *stream; if( (stream = fopen( filename, attribute )) == NULL) { printf("cannot open file, press enter to exit program.\n"); } else { printf("results file opened, press enter to continue.\n"); } int key2 = getchar(); return stream; } //-----------------------------------------------------------------------------