/* Create a matrix where the eye is at the xyz basis looking down the -z axis */ tmat viewMat(const vec3 &eye, const vec3 &gaze, const vec3 &up) { tmat mat = identityMatrix(); // orthonormal basis from params vec3 w = -makeUnitVector(gaze); vec3 u = makeUnitVector(cross(up, w)); // get a vector orthogonal to up and w vec3 v = cross(w, u); // normalized since the others already are // rotate the orthonormal basis to the xyz basis mat.m[0][0] = u.x; mat.m[0][1] = u.y; mat.m[0][2] = u.z; mat.m[1][0] = v.x; mat.m[1][1] = v.y; mat.m[1][2] = v.z; mat.m[2][0] = w.x; mat.m[2][1] = w.y; mat.m[2][2] = w.z; // translate the matrix to the xyz origin tmat move = identityMatrix(); move.m[0][3] = -(eye.x); move.m[1][3] = -(eye.y); move.m[2][3] = -(eye.z); mat *= move; return mat; }
/** **************************************************************************************************** \fn Matrix Identity( void ) \brief Matrix identity \param NONE \return Matrix \retval The identity of this Matrix class **************************************************************************************************** */ GameEngine::Math::Matrix GameEngine::Math::Matrix::Identity( void ) { FUNCTION_START; Matrix identityMatrix( _u32Row, _u32Column); assert( _u32Row == _u32Column ); for( UINT32 i = 0; i < _u32Row; ++i ) identityMatrix( i, i ) = 1; FUNCTION_FINISH; return identityMatrix; }
Mat16 translationMatrix(double x, double y, double z) { Mat16 ret = identityMatrix(); ret.m[12] = x; ret.m[13] = y; ret.m[14] = z; return ret; }
Mat16 scaleMatrix(double x, double y, double z) { Mat16 ret = identityMatrix(); ret.m[0] = x; ret.m[5] = y; ret.m[10] = z; return ret; }
inline Matrix viewMatrix(const Vector3& eye, const Vector3& gaze, const Vector3& up){ Matrix ret; //Create an orthogonal basis from parameters Vector3 w = -(unitVector(gaze)); Vector3 u = unitVector(cross(up, w)); Vector3 v = cross(w, u); //Rotate orthogonal basis to xyz basis ret.x[0][0] = u.x(); ret.x[0][1] = u.y(); ret.x[0][2] = u.z(); ret.x[1][0] = v.x(); ret.x[1][1] = v.y(); ret.x[1][2] = v.z(); ret.x[2][0] = w.x(); ret.x[2][1] = w.y(); ret.x[2][2] = w.z(); //Translate eye to xyz origin Matrix move = identityMatrix(); move.x[0][3] = -eye.x(); move.x[1][3] = -eye.x(); move.x[2][3] = -eye.x(); ret *= move; return ret; }
Matrix translate(double x, double y, double z) { Matrix ret = identityMatrix(); ret.x[0][3] = x; ret.x[1][3] = y; ret.x[2][3] = z; return ret; }
inline Matrix translate(precision x, precision y, precision z){ Matrix ret = identityMatrix(); ret.x[0][3] = x; ret.x[1][3] = y; ret.x[2][3] = z; return ret; }
/* Create a tmat that translates things by x in the x direction, y in the * y direction, z in the z direction */ tmat translate(float x, float y, float z) { tmat mat = identityMatrix(); mat.m[0][3] = x; mat.m[1][3] = y; mat.m[2][3] = z; return mat; }
/* Create a tmat that rotates things by angle radians in the z direction */ tmat rotateZ(float angle) { tmat mat = identityMatrix(); float cosine = cos(angle); float sine = sin(angle); mat.m[0][0] = cosine; mat.m[0][1] = -sine; mat.m[1][0] = sine; mat.m[1][1] = cosine; return mat; }
Matrix rotateZ(double angle) { Matrix ret = identityMatrix(); double cosine = cos(angle); double sine = sin(angle); ret.x[0][0] = cosine; ret.x[0][1] = -sine; ret.x[1][0] = sine; ret.x[1][1] = cosine; return ret; }
//Angle is in radians inline Matrix rotateZ(precision angle){ Matrix ret = identityMatrix(); precision cosine = cos(angle); precision sine = sin(angle); ret.x[0][0] = cosine; ret.x[0][1] = -sine; ret.x[1][0] = sine; ret.x[1][1] = cosine; return ret; }
Mat16 inverseMatrix(Mat16 &m) { Mat16 ret; float *mat = m.m; // inverse(M) = t(adj(M)) / det(M) // M:matrix([a0,a4,a8,a12],[a1,a5,a9,a13], [a2,a6,a10,a14],[a3,a7,a11,a15]); double a0 = mat[0], a1 = mat[1], a2 = mat[2], a3 = mat[3], a4 = mat[4], a5 = mat[5], a6 = mat[6], a7 = mat[7], a8 = mat[8], a9 = mat[9], a10 = mat[10], a11 = mat[11], a12 = mat[12], a13 = mat[13], a14 = mat[14], a15 = mat[15]; // stardisp: true; // transpose(adjoint(M)); ret.m[0] = - (a15*a6 - a14*a7)*a9 + a13*(a11*a6 - a10*a7) + (a10*a15 - a11*a14)*a5; ret.m[1] = (a15*a2 - a14*a3)*a9 - a13*(a11*a2 - a10*a3)- a1*(a10*a15 - a11*a14); ret.m[2] = a13*(a2*a7 - a3*a6) + a1*(a15*a6 - a14*a7)- (a15*a2 - a14*a3)*a5; ret.m[3] = - (a2*a7 - a3*a6)*a9 - a1*(a11*a6 - a10*a7) + (a11*a2 - a10*a3)*a5; ret.m[4] = (a15*a6 - a14*a7)*a8 - a12*(a11*a6 - a10*a7) - (a10*a15 - a11*a14)*a4; ret.m[5] = - (a15*a2 - a14*a3)*a8 + a12*(a11*a2 - a10*a3) + a0*(a10*a15 - a11*a14); ret.m[6] = - a12*(a2*a7 - a3*a6) - a0*(a15*a6 - a14*a7) + (a15*a2 - a14*a3)*a4; ret.m[7] = (a2*a7 - a3*a6)*a8 + a0*(a11*a6 - a10*a7) - (a11*a2 - a10*a3)*a4; ret.m[8] = a12*(a11*a5 - a7*a9) + a4*(a15*a9 - a11*a13) - (a15*a5 - a13*a7)*a8; ret.m[9] = - a12*(a1*a11 - a3*a9) - a0*(a15*a9 - a11*a13) + (a1*a15 - a13*a3)*a8; ret.m[10] = a0*(a15*a5 - a13*a7) + a12*(a1*a7 - a3*a5) - (a1*a15 - a13*a3)*a4; ret.m[11] = - a0*(a11*a5 - a7*a9) + a4*(a1*a11 - a3*a9) - (a1*a7 - a3*a5)*a8; ret.m[12] = - a12*(a10*a5 - a6*a9) - a4*(a14*a9 - a10*a13) + (a14*a5 - a13*a6)*a8; ret.m[13] = a12*(a1*a10 - a2*a9) + a0*(a14*a9 - a10*a13) - (a1*a14 - a13*a2)*a8; ret.m[14] = - a0*(a14*a5 - a13*a6) - a12*(a1*a6 - a2*a5) + (a1*a14 - a13*a2)*a4; ret.m[15] = a0*(a10*a5 - a6*a9) - a4*(a1*a10 - a2*a9) + (a1*a6 - a2*a5)*a8; double det = (a0 * ret.m[0] + a4 * ret.m[1] + a8 * ret.m[2] + a12 * ret.m[3]); if (det != 0) { double det_inv = 1 / det; for (int i = 0; i < 12; i++) ret.m[i] *= det_inv; } else { printf("Error while inverting matrix.\n"); ret = identityMatrix(); } return ret; }
// http://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle Mat16 rotationMatrix(double angle, double ax, double ay, double az) { Mat16 ret = identityMatrix(); double len = sqrt(ax * ax + ay * ay + az * az); // printf("rotationMatrix: rad=%f (%f ,%f ,%f) len=%f\n", angle, ax, ay, az, len); if (len == 0) return ret; ax /= len; ay /= len; az /= len; double c = cos(angle), s = sin(angle); ret.m[0] = ax * ax * (1 - c) + c; ret.m[1] = ax * ay * (1 - c) + az * s; ret.m[2] = ax * az * (1 - c) - ay * s; ret.m[4] = ax * ay * (1 - c) - az * s; ret.m[5] = ay * ay * (1 - c) + c; ret.m[6] = ay * az * (1 - c) + ax * s; ret.m[8] = ax * az * (1 - c) + ay * s; ret.m[9] = ay * az * (1 - c) - ax * s; ret.m[10] = az * az * (1 - c) + c; return ret; }
void loadIdentityMatrix(Matrix *ident) { (*ident) = identityMatrix(); }
void SpeedTest(FunctionCall fc) { int i, min, max, step, sec=0; int sizeTimeArray, fct; char fnc[MAXSIZE_FCT]; char foutput[256]; struct sigaction action; action.sa_handler = handler; sigemptyset(&action.sa_mask); struct timeval start, end, result; double maxtime = 0; double time; double usec; double* timeArray; Matrix a=NULL, b=NULL, tmp=NULL; E s; int n; TokenizeSpeedTest(fc, fnc, &min, &max, &step, &sec); if (min>0 && max>0 && step>0) { sizeTimeArray = ((max-min)/step)+1; timeArray = (double*)malloc(sizeTimeArray*sizeof(double)); for (i=0; i<sizeTimeArray; i++) timeArray[i] = 0; } usec = (double)sec * 1000000; sprintf(foutput, "speedtest_%s_%d_%d_%d_%d", fnc, min, max, step, sec); fct = GetFunction(fnc); switch (fct) { case NMX : { printf("\t You must specify another function\n"); return; } case ADD : { sigaction(SIGINT, &action, NULL); for (i=min; i<=max; i+=step) { a = randomMatrix(i, i, MIN_E, MAX_E); b = randomMatrix(i, i, MIN_E, MAX_E); gettimeofday(&start, NULL); tmp = addMatricis(a, b); gettimeofday(&end, NULL); timersub(&end, &start, &result); time = (result.tv_sec*1000000)+result.tv_usec; if (maxtime < time) maxtime = time; timeArray[(i-min)/step] = time; printf("\t* %s Size:%5d ;\tTime:%8.0f µs ;\n", fnc, i, time); deleteMatrix(a); deleteMatrix(b); deleteMatrix(tmp); if (usec >= 1000000) { if (time >= usec) { i++; break; } } if (CTRLC) { i++; break; } } CreateGNUPLOTFile(min, i-step, step, timeArray, maxtime, foutput); break; } case SUB : { sigaction(SIGINT, &action, NULL); for (i=min; i<=max; i+=step) { a = randomMatrix(i, i, MIN_E, MAX_E); b = randomMatrix(i, i, MIN_E, MAX_E); gettimeofday(&start, NULL); tmp = substractMatricis(a, b); gettimeofday(&end, NULL); timersub(&end, &start, &result); time = (result.tv_sec*1000000)+result.tv_usec; if (maxtime < time) maxtime = time; timeArray[(i-min)/step] = time; printf("\t* %s Size:%5d ;\tTime:%8.0f µs ;\n", fnc, i, time); deleteMatrix(a); deleteMatrix(b); deleteMatrix(tmp); if (usec >= 1000000) { if (time >= usec) { i++; break; } } if (CTRLC) { i++; break; } } CreateGNUPLOTFile(min, i-step, step, timeArray, maxtime, foutput); break; } case MUL : { sigaction(SIGINT, &action, NULL); for (i=min; i<=max; i+=step) { a = randomMatrix(i, i, MIN_E, MAX_E); b = randomMatrix(i, i, MIN_E, MAX_E); gettimeofday(&start, NULL); tmp = mulMatricis(a, b); gettimeofday(&end, NULL); timersub(&end, &start, &result); time = (result.tv_sec*1000000)+result.tv_usec; if (maxtime < time) maxtime = time; timeArray[(i-min)/step] = time; printf("\t* %s Size:%5d ;\tTime:%8.0f µs ;\n", fnc, i, time); deleteMatrix(a); deleteMatrix(b); deleteMatrix(tmp); if (usec >= 1000000) { if (time >= usec) { i++; break; } } if (CTRLC) { i++; break; } } CreateGNUPLOTFile(min, i-step, step, timeArray, maxtime, foutput); break; } case MSC : { sigaction(SIGINT, &action, NULL); for (i=min; i<=max; i+=step) { a = randomMatrix(i, i, MIN_E, MAX_E); s = mRand(MIN_SCA, MAX_SCA); gettimeofday(&start, NULL); tmp = mult_scal(a, s); gettimeofday(&end, NULL); timersub(&end, &start, &result); time = (result.tv_sec*1000000)+result.tv_usec; if (maxtime < time) maxtime = time; timeArray[(i-min)/step] = time; printf("\t* %s Size:%5d ;\tTime:%8.0f µs ;\n", fnc, i, time); deleteMatrix(a); deleteMatrix(tmp); if (usec >= 1000000) { if (time >= usec) { i++; break; } } if (CTRLC) { i++; break; } } CreateGNUPLOTFile(min, i-step, step, timeArray, maxtime, foutput); break; } case EXP : { sigaction(SIGINT, &action, NULL); for (i=min; i<=max; i+=step) { a = randomMatrix(i, i, MIN_E, MAX_E); n = (int)mRand(MIN_EXP, MAX_EXP); gettimeofday(&start, NULL); tmp = expo(a, n); gettimeofday(&end, NULL); timersub(&end, &start, &result); time = (result.tv_sec*1000000)+result.tv_usec; if (maxtime < time) maxtime = time; timeArray[(i-min)/step] = time; printf("\t* %s Size:%5d ;\tTime:%8.0f µs ;\n", fnc, i, time); deleteMatrix(a); deleteMatrix(tmp); if (usec >= 1000000) { if (time >= usec) { i++; break; } } if (CTRLC) { i++; break; } } CreateGNUPLOTFile(min, i-step, step, timeArray, maxtime, foutput); break; } case TRA : { sigaction(SIGINT, &action, NULL); for (i=min; i<=max; i+=step) { a = randomMatrix(i, i, MIN_E, MAX_E); gettimeofday(&start, NULL); tmp = transposeMatrix(a); gettimeofday(&end, NULL); timersub(&end, &start, &result); time = (result.tv_sec*1000000)+result.tv_usec; if (maxtime < time) maxtime = time; timeArray[(i-min)/step] = time; printf("\t* %s Size:%5d ;\tTime:%8.0f µs ;\n", fnc, i, time); deleteMatrix(a); deleteMatrix(tmp); if (usec >= 1000000) { if (time >= usec) { i++; break; } } if (CTRLC) { i++; break; } } CreateGNUPLOTFile(min, i-step, step, timeArray, maxtime, foutput); break; } case DET : { sigaction(SIGINT, &action, NULL); for (i=min; i<=max; i+=step) { a = randomMatrix(i, i, MIN_E, MAX_E); gettimeofday(&start, NULL); s = determinant(a); gettimeofday(&end, NULL); timersub(&end, &start, &result); time = (result.tv_sec*1000000)+result.tv_usec; if (maxtime < time) maxtime = time; timeArray[(i-min)/step] = time; printf("\t* %s Size:%5d ;\tTime:%8.0f µs ;\n", fnc, i, time); deleteMatrix(a); if (usec >= 1000000) { if (time >= usec) { i++; break; } } if (CTRLC) { i++; break; } } CreateGNUPLOTFile(min, i-step, step, timeArray, maxtime, foutput); break; } case DLU : { sigaction(SIGINT, &action, NULL); for (i=min; i<=max; i+=step) { a = randomMatrix(i, i, MIN_E, MAX_E); b = newMatrix(i, i); tmp = identityMatrix(i); gettimeofday(&start, NULL); decomposition(a, b, tmp); gettimeofday(&end, NULL); timersub(&end, &start, &result); time = (result.tv_sec*1000000)+result.tv_usec; if (maxtime < time) maxtime = time; timeArray[(i-min)/step] = time; printf("\t* %s Size:%5d ;\tTime:%8.0f µs ;\n", fnc, i, time); deleteMatrix(a); deleteMatrix(b); deleteMatrix(tmp); if (usec >= 1000000) { if (time >= usec) { i++; break; } } if (CTRLC) { i++; break; } } CreateGNUPLOTFile(min, i-step, step, timeArray, maxtime, foutput); break; } case SOL : { sigaction(SIGINT, &action, NULL); for (i=min; i<=max; i+=step) { a = randomMatrix(i, i, MIN_E, MAX_E); b = randomMatrix(i, 1, MIN_E, MAX_E); gettimeofday(&start, NULL); tmp = gauss(a, b); gettimeofday(&end, NULL); timersub(&end, &start, &result); time = (result.tv_sec*1000000)+result.tv_usec; if (maxtime < time) maxtime = time; timeArray[(i-min)/step] = time; printf("\t* %s Size:%5d ;\tTime:%8.0f µs ;\n", fnc, i, time); deleteMatrix(a); deleteMatrix(b); deleteMatrix(tmp); if (usec >= 1000000) { if (time >= usec) { i++; break; } } if (CTRLC) { i++; break; } } CreateGNUPLOTFile(min, i-step, step, timeArray, maxtime, foutput); break; } case INV : { sigaction(SIGINT, &action, NULL); for (i=min; i<=max; i+=step) { a = randomMatrix(i, i, MIN_E, MAX_E); gettimeofday(&start, NULL); tmp = invert(a); gettimeofday(&end, NULL); timersub(&end, &start, &result); time = (result.tv_sec*1000000)+result.tv_usec; if (maxtime < time) maxtime = time; timeArray[(i-min)/step] = time; printf("\t* %s Size:%5d ;\tTime:%8.0f µs ;\n", fnc, i, time); deleteMatrix(a); deleteMatrix(tmp); if (usec >= 1000000) { if (time >= usec) { i++; break; } } if (CTRLC) { i++; break; } } CreateGNUPLOTFile(min, i-step, step, timeArray, maxtime, foutput); break; } case RNK : { sigaction(SIGINT, &action, NULL); for (i=min; i<=max; i+=step) { a = randomMatrix(i, i, MIN_E, MAX_E); gettimeofday(&start, NULL); n = rank(a); gettimeofday(&end, NULL); timersub(&end, &start, &result); time = (result.tv_sec*1000000)+result.tv_usec; if (maxtime < time) maxtime = time; timeArray[(i-min)/step] = time; printf("\t* %s Size:%5d ;\tTime:%8.0f µs ;\n", fnc, i, time); deleteMatrix(a); if (usec >= 1000000) { if (time >= usec) { i++; break; } } if (CTRLC) { i++; break; } } CreateGNUPLOTFile(min, i-step, step, timeArray, maxtime, foutput); break; } case VAR : // default case NOF : // default default : { printf("\t%s : Function Not Implemented\n", fnc); fni++; break; } } if (fct!=NOF && fct !=VAR) fni = 0; free(timeArray); CTRLC = 0; sigemptyset(&action.sa_mask); }
bool CMatrix::isIdentityMatrix( ) const { return( *this == identityMatrix() ); }
void CMatrix::setIdentityMatrix( ) { *this = identityMatrix(); }
IFCBuilder::IFCBuilder() { model = 0; saveIfx = false; aggrRelatedElements = NULL; aggrRepresentations = NULL; ifcApplicationInstance = 0, ifcBuildingInstance = 0, ifcBuildingInstancePlacement = 0, ifcBuildingStoreyInstance = 0, ifcBuildingStoreyInstancePlacement = 0, ifcBuildOwnerHistoryInstance = 0, ifcConversionBasedUnitInstance = 0, ifcDimensionalExponentsInstance = 0, ifcGeometricRepresentationContextInstance = 0, ifcOrganizationInstance = 0, ifcPersonAndOrganizationInstance = 0, ifcPersonInstance = 0, ifcProjectInstance = 0, ifcSiteInstance = 0, ifcSiteInstancePlacement = 0, ifcUnitAssignmentInstance = 0; char ifcSchemaName[30] = "../IFC2X3_Final.exp"; if ( !PathFileExists(ifcSchemaName) ) { std::cerr << "IFC schema file is not accessible" << std::endl; } model = sdaiCreateModelBN(1, NULL, ifcSchemaName); if (!model) { std::cerr << ("IFC Model could not be instantiated, probably it cannot find the schema file."); getch(); exit(-1); return; } identityMatrix(&matrix); ifcApplicationInstance = 0; ifcBuildOwnerHistoryInstance = 0; ifcConversionBasedUnitInstance = 0; ifcDimensionalExponentsInstance = 0; ifcGeometricRepresentationContextInstance = 0; ifcOrganizationInstance = 0; ifcPersonAndOrganizationInstance = 0; ifcPersonInstance = 0; ifcUnitAssignmentInstance = 0; // // Build standard IFC structures // ifcProjectInstance = getProjectInstance(); ifcSiteInstance = buildSiteInstance(&matrix, NULL, &ifcSiteInstancePlacement); buildRelAggregatesInstance("ProjectContainer", "ProjectContainer for Sites", ifcProjectInstance, ifcSiteInstance); // CDP: now own function: addBuilding(CDP_Building building) --Andreas //ifcBuildingInstance = buildBuildingInstance(&matrix, ifcSiteInstancePlacement, &ifcBuildingInstancePlacement); //buildRelAggregatesInstance("SiteContainer", "SiteContainer For Buildings", ifcSiteInstance, ifcBuildingInstance); // we don't modell the individual floors --Andreas //ifcBuildingStoreyInstance = buildBuildingStoreyInstance(&matrix, ifcBuildingInstancePlacement, &ifcBuildingStoreyInstancePlacement); //buildRelAggregatesInstance("BuildingContainer", "BuildingContainer for BuildigStories", ifcBuildingInstance, ifcBuildingStoreyInstance); /* if (objectsWillBeAdded) { buildRelContainedInSpatialStructureInstance("BuildingStoreyContainer", "BuildingStoreyContainer for Building Elements", ifcBuildingStoreyInstance, &aggrRelatedElements); } */ }
void Decomposition(FunctionCall fc) { int i, j, indexL, indexU, existL = 0, existU = 0; Matrix a, l, u; char nameA[MAXSIZE_NAME] = {0}; char nameL[MAXSIZE_NAME] = {0}; char nameU[MAXSIZE_NAME] = {0}; for (i = 0; i < MAXSIZE_NAME; i++) { nameL[i] = fc->name[i]; nameU[i] = fc->name[i]; if (fc->name[i] == '\0') { if (i<MAXSIZE_NAME-2) { nameL[i] = '_'; nameU[i] = '_'; nameL[i+1] = 'L'; nameU[i+1] = 'U'; nameL[i+2] = '\0'; nameU[i+2] = '\0'; } break; } } indexL = IndexMatrix(nameL); if (indexL==-1) { indexL = cur_mat; mats[indexL] = (StrMatObject*)malloc(sizeof(StrMatObject)); if (mats[indexL] == NULL) { printf("NewMatrix(): Could not allocate the new matrix\n"); return; } for (i = 0; i < MAXSIZE_NAME; i++) { mats[indexL]->name[i] = nameL[i]; if (nameL[i]=='\0') break; } } else existL = 1; indexU = IndexMatrix(nameU); if (indexU==-1) { indexU = cur_mat+1; mats[indexU] = (StrMatObject*)malloc(sizeof(StrMatObject)); if (mats[indexU] == NULL) { printf("NewMatrix(): Could not allocate the new matrix\n"); return; } for (i = 0; i < MAXSIZE_NAME; i++) { mats[indexU]->name[i] = nameU[i]; if (nameU[i]=='\0') break; } } else existU = 1; j = 0; // searching for matrix name for (i = 0; i < MAXSIZE_NAME; i++) { nameA[j] = fc->args[i]; if (nameA[j] == ',') { nameA[j] = '\0'; break; } j++; } a = GetMatrix(nameA); if (a==NULL) { printf("\tMatrix %s Not Found\n", nameA); if (!existL) free(mats[indexL]); if (!existU) free(mats[indexU]); return; } l = identityMatrix(a->rows); u = newMatrix(a->rows, a->cols); if (l!=NULL && u!=NULL) { decomposition(a, l, u); } if (existL) deleteMatrix(mats[indexL]->mat); mats[indexL]->mat = l; if (existU) deleteMatrix(mats[indexU]->mat); mats[indexU]->mat = u; // test: display the result displayMatrix(mats[indexL]->mat); displayMatrix(mats[indexU]->mat); if (!existL) cur_mat++; if (!existU) cur_mat++; }
/** **************************************************************************************************** \fn void UnitTest( void ) \brief The unit test of Matrix class \param NONE \return NONE **************************************************************************************************** */ void GameEngine::Math::Matrix::UnitTest( void ) { FUNCTION_START; Matrix matrix2x2( 2, 2 ); // Check creation for( UINT8 i = 0; i < 2; ++i ) { for( UINT8 j = 0; j < 2; ++j ) assert( matrix2x2(i, j) == 0 ); } // Check matrix identity matrix2x2(0, 0) = 1; matrix2x2(0, 1) = 2; matrix2x2(1, 0) = 3; matrix2x2(1, 1) = 4; Matrix identityMatrix( 2, 2 ); identityMatrix(0, 0) = 1; identityMatrix(1, 1) = 1; Matrix m( 2, 2 ); m = matrix2x2.Identity(); assert( matrix2x2.Identity() == identityMatrix ); // Check matrix transpose Matrix matrix2x3( 2, 3 ); matrix2x3( 0, 0 ) = 1; matrix2x3( 0, 1 ) = 2; matrix2x3( 0, 2 ) = 3; matrix2x3( 1, 0 ) = 4; matrix2x3( 1, 1 ) = 5; matrix2x3( 1, 2 ) = 6; matrix2x3.Transpose(); Matrix matrix3x2( 3, 2 ); matrix3x2( 0, 0 ) = 1; matrix3x2( 0, 1 ) = 4; matrix3x2( 1, 0 ) = 2; matrix3x2( 1, 1 ) = 5; matrix3x2( 2, 0 ) = 3; matrix3x2( 2, 1 ) = 6; assert( matrix2x3 == matrix3x2 ); // Check matrix 3x3 inverse Matrix matrix3x3( 3, 3 ); matrix3x3( 0, 0 ) = 2; matrix3x3( 0, 1 ) = -1; matrix3x3( 0, 2 ) = 3; matrix3x3( 1, 0 ) = 1; matrix3x3( 1, 1 ) = 6; matrix3x3( 1, 2 ) = -4; matrix3x3( 2, 0 ) = 5; matrix3x3( 2, 1 ) = 0; matrix3x3( 2, 2 ) = 8; Matrix matrix3x3Inverse( 3, 3 ); Matrix3Inverse( matrix3x3, matrix3x3Inverse ); assert( (matrix3x3 * matrix3x3Inverse) == matrix3x3.Identity() ); // Check matrix 4x4 inverse Matrix matrix4x4( 4, 4 ); matrix4x4( 0, 0 ) = 2; matrix4x4( 0, 1 ) = 3; matrix4x4( 0, 2 ) = 4; matrix4x4( 0, 3 ) = 5; matrix4x4( 1, 0 ) = 5; matrix4x4( 1, 1 ) = 7; matrix4x4( 1, 2 ) = 9; matrix4x4( 1, 3 ) = 9; matrix4x4( 2, 0 ) = 5; matrix4x4( 2, 1 ) = 8; matrix4x4( 2, 2 ) = 7; matrix4x4( 2, 3 ) = 4; matrix4x4( 3, 0 ) = 4; matrix4x4( 3, 1 ) = 3; matrix4x4( 3, 2 ) = 3; matrix4x4( 3, 3 ) = 2; Matrix matrix4x4Inverse( 4, 4 ); Matrix4Inverse( matrix4x4, matrix4x4Inverse ); Matrix matrix4x4Result( 4, 4 ); matrix4x4Result = matrix4x4 * matrix4x4Inverse; assert( (matrix4x4 * matrix4x4Inverse) == matrix4x4.Identity() ); FUNCTION_FINISH; }
void Parser::parsefile(ifstream &inputfile, Camera *cam, RayTracer *tracer, int* maxD){ // this sets the default BRDF // whenever a primitive is created // this is used for its material // the defaults as in OpenGL are //ka ambient = Color(0.2,0.2,0.2); //kd diffuse = Color(0.8,0.8,0.8); //ks specular = Color(0.0,0.0,0.0); //ke emission = Color(0.0,0.0,0.0); //kr reflection = Color(0.0,0.0,0.0); //s shininess = 0.0; BRDF tempBRDF; vector<mat4> transforms; mat4 identity, currMatrix, tempMat, helper,helper2,mvt; identityMatrix(identity); identityMatrix(currMatrix); identityMatrix(tempMat); identityMatrix(helper); identityMatrix(helper2); identityMatrix(mvt); vec3 tempVec; string line; char command[MAX_CHARS]; while (!inputfile.eof()) { getline(inputfile, line); if (inputfile.eof()) break; if (line[0]=='#') continue; //comment lines int num = sscanf (line.c_str(), "%s", command); if (num != 1) continue; // Blank lines; // Now, we simply parse the file by looking at the first line for the various commands /************** CAMERA LOCATION **********/ if (!strcmp(command, "camera")) { double lookfrom[3], lookat[3], up[3], fov ; vec3 lookfrom0,lookat0,up0; int num = sscanf(line.c_str(), "%s %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf", command, lookfrom, lookfrom+1, lookfrom+2, lookat, lookat+1, lookat+2, up, up+1, up+2, &fov) ; if (num != 11) { fprintf(stderr, "camera from[3] at[3] up[3] fov\n") ; exit(1) ; } assert(!strcmp(command,"camera")) ; // Set up modelview and projection matrices per camera parameters lookfrom0.x=lookfrom[0]; lookfrom0.y=lookfrom[1]; lookfrom0.z=lookfrom[2]; lookat0.x=lookat[0]; lookat0.y=lookat[1]; lookat0.z=lookat[2]; up0.x=up[0]; up0.y=up[1]; up0.z=up[2]; cam->SetCamera(lookfrom0,lookat0,up0,fov); } /****************************************/ /*********** GEOMETRY *******************/ else if (!strcmp(command, "sphere")) { double radius ; // Syntax is sphere x y z radius double pos[3] ; int num = sscanf(line.c_str(), "%s %lf %lf %lf %lf", command, pos, pos+1, pos+2, &radius); if (num != 5) { fprintf(stderr, "sphere x y z radius\n") ; exit(1) ; } tempVec = vec3((float)pos[0],(float)pos[1],(float)pos[2]); invert(tempMat, currMatrix); transpose(mvt, tempMat); tracer->addSphere(Sphere(tempVec, radius, Material(tempBRDF), tempMat, currMatrix, mvt));// makes a sphere with material props from the tempBRDF } else if (!strcmp(command, "maxverts")) { int num = sscanf(line.c_str(), "%s %d", command, &maxverts) ; assert(num == 2) ; assert(maxverts > 0) ; assert(!strcmp(command,"maxverts")) ; assert(vert = new Vertex[maxverts]) ; } else if (!strcmp(command, "maxvertnorms")) { int num = sscanf(line.c_str(), "%s %d", command, &maxvertnorms) ; assert(num == 2) ; assert(maxvertnorms > 0) ; assert(!strcmp(command,"maxvertnorms")) ; assert(vertnorm = new VertexNormal[maxvertnorms]) ; } else if (!strcmp(command, "vertex")) { // Add a vertex to the stack assert(maxverts) ; assert(curvert < maxverts) ; Vertex v ; int num = sscanf(line.c_str(), "%s %lf %lf %lf", command, v.pos, v.pos+1, v.pos+2) ; assert(num == 4) ; assert(!strcmp(command,"vertex")) ; vert[curvert] = v ; ++curvert ; } else if (!strcmp(command, "vertexnormal")) { // Add a vertex to the stack with a normal assert(maxvertnorms) ; assert(curvertnorm < maxvertnorms) ; VertexNormal vn ; int num = sscanf(line.c_str(), "%s %lf %lf %lf %lf %lf %lf", command, vn.pos, vn.pos+1, vn.pos+2, vn.normal, vn.normal+1, vn.normal+2) ; assert(num == 7) ; assert(!strcmp(command,"vertexnormal")) ; vertnorm[curvertnorm] = vn ; ++curvertnorm ; } else if (!strcmp(command, "tri")) { // Triangle from 3 vertices int pts[3] ; int num = sscanf(line.c_str(), "%s %d %d %d", command, pts, pts+1, pts+2) ; assert(num == 4) ; assert(!strcmp(command,"tri")) ; int i,j ; for (i = 0 ; i < 3 ; i++) { assert(pts[i] >= 0 && pts[i] < maxverts) ; } double vertex[3][3] ; double normal[3] ; for (i = 0 ; i < 3 ; i++) for (j = 0 ; j < 3 ; j++) vertex[i][j] = vert[pts[i]].pos[j] ; // Calculate the face normal double vec1[3], vec2[3], vect3[3] ; for (i = 0 ; i < 3 ; i++) { vec1[i] = vertex[1][i] - vertex[0][i] ; vec2[i] = vertex[2][i] - vertex[0][i] ; } vect3[0] = vec1[1]*vec2[2] - vec1[2]*vec2[1] ; vect3[1] = vec1[2]*vec2[0] - vec1[0]*vec2[2] ; vect3[2] = vec1[0]*vec2[1] - vec1[1]*vec2[0] ; double norm = 0 ; for (i = 0 ; i < 3 ; i++) norm += vect3[i] * vect3[i] ; norm = sqrt(norm) ; if (norm == 0) {normal[0] = 0 ; normal[1] = 0 ; normal[2] = 1.0 ;} else { for (i = 0 ; i < 3 ; i++) normal[i] = vect3[i] / norm ; } //starts vec3 verts[3], temps[3], tempnormie; for(i = 0; i < 3; i++){ verts[i] = vec3((float)vertex[i][0], (float)vertex[i][1],(float)vertex[i][2]); mult(temps[i], currMatrix, verts[i]); } vec3 normie((float)normal[0], (float)normal[1], (float)normal[2]); invert(tempMat, currMatrix); transpose(tempMat); mult(tempnormie,tempMat,normie); normalize(tempnormie); Triangle t(temps, tempnormie, Material(tempBRDF));// makes a triangle with material props from the tempBRDF tracer->addTriangle(t); } else if (!strcmp(command, "trinormal")) { int pts[3] ; int num = sscanf(line.c_str(), "%s %d %d %d", command, pts, pts+1, pts+2) ; assert(num == 4) ; assert(!strcmp(command,"trinormal")) ; int i; for (i = 0 ; i < 3 ; i++) { assert(pts[i] >= 0 && pts[i] < maxvertnorms) ; } printf("trinormal\n"); vec3 verts[3]; vec3 normie[3]; for(int i = 0; i < 3; i++){ verts[i] = vec3((float)vertnorm[pts[i]].pos[0], (float)vertnorm[pts[i]].pos[1],(float)vertnorm[pts[i]].pos[2]); normie[i] = vec3((float)vertnorm[pts[i]].normal[0], (float)vertnorm[pts[i]].normal[1], (float)vertnorm[pts[i]].normal[2]); } double normal[3] ; double vec1[3], vec2[3], vect3[3] ; for (i = 0 ; i < 3 ; i++) { vec1[i] = vertnorm[1].pos[i] - vertnorm[0].pos[i] ; vec2[i] = vertnorm[2].pos[i] - vertnorm[0].pos[i] ; } vect3[0] = vec1[1]*vec2[2] - vec1[2]*vec2[1] ; vect3[1] = vec1[2]*vec2[0] - vec1[0]*vec2[2] ; vect3[2] = vec1[0]*vec2[1] - vec1[1]*vec2[0] ; double norm = 0 ; for (i = 0 ; i < 3 ; i++) norm += vect3[i] * vect3[i] ; norm = sqrt(norm) ; if (norm == 0) {normal[0] = 0 ; normal[1] = 0 ; normal[2] = 1.0 ; } else { for (i = 0 ; i < 3 ; i++) normal[i] = vect3[i] / norm ; } Triangle t(verts, normie, Material(tempBRDF));// makes a triangle with material props from the tempBRDF t.setFaceNormal(vec3 ((float)normal[0], (float)normal[1], (float)normal[2])); tracer->addTriangle(t); } /******************************************/ /**************** TRANSFORMATIONS *********/ else if (!strcmp(command, "translate")) { double x,y,z ; // Translate by x y z as in standard OpenGL int num = sscanf(line.c_str(), "%s %lf %lf %lf",command, &x, &y, &z) ; if (num != 4) { fprintf(stderr, "translate x y z\n") ; exit(1) ; } currMatrix.a03 += (float)x; currMatrix.a13 += (float)y; currMatrix.a23 += (float)z; } else if (!strcmp(command, "rotate")) { double ang, x,y,z ; // Rotate by an angle about axis x y z as in standard OpenGL int num = sscanf(line.c_str(), "%s %lf %lf %lf %lf",command, &x, &y, &z, &ang) ; if (num != 5) { fprintf(stderr, "rotate angle x y z\n") ; exit(1) ; } vec3 tempVec =vec3 ((float)x,(float)y,(float)z); normalize(tempVec); identityMatrix(helper); helper.set_rot((float)ang*(float)PI/(float)180.0,tempVec); helper2=currMatrix; mult(currMatrix, helper2, helper); } else if (!strcmp(command, "rotatex")) { double ang; // Rotate by an angle about x axis as in standard OpenGL int num = sscanf(line.c_str(), "%s %lf ",command, &ang) ; if (num != 2) { fprintf(stderr, "rotatex angle \n") ; exit(1) ; } identityMatrix(helper); helper.set_rotx((float)ang*(float)PI/(float)180.0); helper2=currMatrix; mult(currMatrix, helper2, helper); } else if (!strcmp(command, "rotatey")) { double ang; // Rotate by an angle about x axis as in standard OpenGL int num = sscanf(line.c_str(), "%s %lf ",command, &ang) ; if (num != 2) { fprintf(stderr, "rotatey angle \n") ; exit(1) ; } identityMatrix(helper); helper.set_roty((float)ang*(float)PI/(float)180.0); helper2=currMatrix; mult(currMatrix,helper2,helper); } else if (!strcmp(command, "rotatez")) { double ang; // Rotate by an angle about x axis as in standard OpenGL int num = sscanf(line.c_str(), "%s %lf ",command, &ang) ; if (num != 2) { fprintf(stderr, "rotatez angle \n") ; exit(1) ; } identityMatrix(helper); helper.set_rotz((float)ang*(float)PI/(float)180.0); helper2=currMatrix; mult(currMatrix,helper2,helper); } else if (!strcmp(command, "scale")) { double x,y,z ; // Scale by x y z as in standard OpenGL int num = sscanf(line.c_str(), "%s %lf %lf %lf",command, &x, &y, &z) ; if (num != 4) { fprintf(stderr, "scale x y z\n") ; exit(1) ; } identityMatrix(helper); helper.a00=(float)x; helper.a11=(float)y; helper.a22=(float)z; helper2=currMatrix; mult(currMatrix,helper2,helper); } else if (!strcmp(command, "pushTransform")) { transforms.push_back(currMatrix); // Push the current matrix on the stack as in OpenGL } else if (!strcmp(command, "popTransform")) { currMatrix = transforms.back(); // Pop the current matrix as in OpenGL transforms.pop_back(); } else if (!strcmp(command, "maxdepth")) { int num = sscanf(line.c_str(), "%s %d", command, &maxdepth) ; assert(num == 2) ; assert(!strcmp(command, "maxdepth")) ; *maxD=maxdepth; cout<<" maxdepth: "<<*maxD<<endl; } else if (!strcmp(command, "output")) { char out[300] ; int num = sscanf(line.c_str(), "%s %s", command, out) ; assert(num == 2) ; assert(!strcmp(command, "output")) ; //output->assign(out); //cout<<*output<<endl; } /*************************************************/ /*************** LIGHTS *******************/ else if (!strcmp(command, "directional")) { float direction[4], color[4] ; color[3] = 1.0 ; direction[3] = 0.0 ; int num = sscanf(line.c_str(), "%s %f %f %f %f %f %f", command, direction, direction+1, direction+2, color, color+1, color+2) ; assert(num == 7) ; vec3 temp(direction[0],direction[1],direction[2]); vec3 temp2=temp; normalize(temp2); Color tempC(color[0],color[1],color[2]); Light* tempLight = new DirectionalLight(temp,tempC,temp2); tempLight->m_idirl = true; tracer->AddLight(tempLight); } else if (!strcmp(command, "point")) { float direction[4], color[4] ; color[3] = 1.0 ; direction[3] = 1.0 ; int num = sscanf(line.c_str(), "%s %f %f %f %f %f %f", command, direction, direction+1, direction+2, color, color+1, color+2) ; assert(num == 7) ; vec3 temp(direction[0],direction[1],direction[2]); Color tempC((double)color[0],(double)color[1],(double)color[2]); Light* tempLight = new PointLight(temp,tempC,temp,attenuation[0],attenuation[1],attenuation[2]); tempLight->m_idirl = false; tracer->AddLight(tempLight); } else if (!strcmp(command, "attenuation")) { int num = sscanf(line.c_str(), "%s %lf %lf %lf", command, attenuation, attenuation + 1, attenuation +2) ; assert(num == 4) ; assert(!strcmp(command, "attenuation")) ; // this changes the attenuation and // whenever we set a point light, // since attenuation is a global variable, // these are the values used to set // its attenuation } else if (!strcmp(command, "ambient")) { float ambient[4] ; ambient[3] = 1.0 ; int num = sscanf(line.c_str(), "%s %f %f %f", command, ambient, ambient+1, ambient+2) ; assert(num == 4) ; assert(!strcmp(command, "ambient")) ; // changes the ka in the tempBRDF used to set primitives' material tempBRDF.SetKa(Color((double)ambient[0],(double)ambient[1],(double)ambient[2])); } /*******************************************************/ /****************** MATERIALS ************************/ else if (!strcmp(command, "diffuse")) { float diffuse[4] ; diffuse[3] = 1.0 ; int num = sscanf(line.c_str(), "%s %f %f %f", command, diffuse, diffuse+1, diffuse+2) ; assert(num == 4) ; assert (!strcmp(command, "diffuse")) ; // changes the kd in the tempBRDF used to set primitives' material tempBRDF.SetKd(Color((double)diffuse[0],(double)diffuse[1],(double)diffuse[2])); } else if (!strcmp(command, "specular")) { float specular[4] ; specular[3] = 1.0 ; int num = sscanf(line.c_str(), "%s %f %f %f", command, specular, specular+1, specular+2) ; assert(num == 4) ; assert (!strcmp(command, "specular")) ; // changes the Ks in the tempBRDF used to set primitives' material tempBRDF.SetKs(Color((double)specular[0],(double)specular[1],(double)specular[2])); } else if (!strcmp(command, "shininess")) { float shininess ; int num = sscanf(line.c_str(), "%s %f", command, &shininess) ; assert(num == 2) ; assert (!strcmp(command, "shininess")) ; // changes the s (shininess) in the tempBRDF used to set primitives' material tempBRDF.SetS((double)shininess); } else if (!strcmp(command, "emission")) { float emission[4] ; emission[3] = 1.0 ; int num = sscanf(line.c_str(), "%s %f %f %f", command, emission, emission+1, emission+2) ; assert(num == 4) ; assert (!strcmp(command, "emission")) ; // changes the Ke in the tempBRDF used to set primitives' material tempBRDF.SetKe(Color((double)emission[0],(double)emission[1],(double)emission[2])); } else if (!strcmp(command, "reflection")) { float reflection[3]; int num = sscanf(line.c_str(), "%s %f %f %f", command, reflection, reflection+1, reflection+2) ; assert(num == 4) ; assert (!strcmp(command, "reflection")) ; // changes the Kr in the tempBRDF used to set primitives' material tempBRDF.SetKr(Color((double)reflection[0],(double)reflection[1],(double)reflection[2])); } else if (!strcmp(command, "refraction")) { double refraction; int num = sscanf(line.c_str(), "%s %lf", command, &refraction) ; assert(num == 2) ; assert (!strcmp(command, "refraction")) ; // changes the Kt in the tempBRDF used to set primitives' material tempBRDF.SetKt((double)refraction); } /*****************************************************/ else { fprintf(stderr, "Unimplemented command: %s\n", command) ; exit(1) ; } } vert=0; vertnorm=0; maxverts=0; maxvertnorms=0; curvert=0; curvertnorm=0; maxdepth=3; lightnum=0; parsed = 0 ; tnumber++; cout << "parsing ended successfully " << tnumber << endl; }