int main() { FullTransform ft; IMatrix i(0); cout << "Generated Hadamard matrix of size " << FullTransform::hms << ':' << endl; PrintMatrix(ft.GetHad()); cout << "Generated negative Hadamard matrix of size " << FullTransform::hms << ':' << endl; PrintMatrix(ft.GetNegHad()); MatrixProduct(ft.GetHad(), ft.GetHad(), i); cout << "Hadamard matrix product on its transpose:" << endl; PrintMatrix(i); CodeWord cw; for(int c = -128; c < 128; ++c) { ft.Code(c, cw); char cc = ft.Decode(cw); if(c == cc) { cout << "Test passed for char: " << (int)c << ", coded word:" << endl; ft.PrintCodeWord(cw); } else { cout << "Test failed for char: " << (int)c << ", decoded char: " << (int)cc << ", coded word:" << endl; ft.PrintCodeWord(cw); break; } } return 0; }
void DrawCube(GLuint program) { float w = (float)glutGet(GLUT_WINDOW_WIDTH); float h = (float)glutGet(GLUT_WINDOW_HEIGHT); // passage des attributs de sommet au shader #ifdef VAO glBindBuffer(GL_ARRAY_BUFFER, cubeVBO); GLint positionLocation = glGetAttribLocation(program, "a_position"); glEnableVertexAttribArray(positionLocation); glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, sizeof(float)*3, 0); #else glBindVertexArray(cubeVAO); #endif // variables uniformes (constantes) durant le rendu de la primitive float projection[16]; //Orthographic(projection, 0, w, h, 0, -1.f, 1.f); Perspective(projection, 45.f, w, h, 0.1f, 1000.f); //Identity(projection); GLint projLocation = glGetUniformLocation(program, "u_projectionMatrix"); glUniformMatrix4fv(projLocation, 1, GL_FALSE, projection); int currentTime = glutGet(GLUT_ELAPSED_TIME); int delta = currentTime - previousTime; previousTime = currentTime; static float time = 1.f; time += delta/1000.f; GLint timeLocation = glGetUniformLocation(program, "u_time"); glUniform1f(timeLocation, time); float viewTransform[16]; Identity(viewTransform); viewTransform[14] = -7.0f; GLint viewLocation = glGetUniformLocation(program, "u_viewMatrix"); glUniformMatrix4fv(viewLocation, 1, GL_FALSE, viewTransform); float worldTransform[16]; //Identity(worldTransform); //Translate(worldTransform, 5.f * sin(time), 0.0f, 0.0F); float A[16], B[16]; //RotateX(A, time); Translate(A, 5.f * sin(time), 0.0f, 0.0F); RotateZ(B, time); MatrixProduct(worldTransform, A, B); GLint worldLocation = glGetUniformLocation(program, "u_worldMatrix"); glUniformMatrix4fv(worldLocation, 1, GL_FALSE, worldTransform); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cubeIBO); glDrawElements(GL_TRIANGLES, 6 * 2 * 3, GL_UNSIGNED_SHORT, 0); }
inline char DecodeHalf(short s) const { IMatrix r; #ifdef DEBUG cout << "Half word to decode: "; Print(s, 8); #endif MatrixProduct(ScanChar(s), hm, r); #ifdef DEBUG cout << "Product Vector: "; PrintArray(r[0]); #endif return FindChar(r[0]); }
int GzScaleMat(GzCoord scale, GzMatrix mat) { // Create scaling matrix // Pass back the matrix using mat value float scal[4][4] = { { scale[0], 0, 0, 0 }, { 0, scale[1], 0, 0 }, { 0, 0, scale[2], 0 }, { 0, 0, 0, 1 } }; float prod[4][4]; MatrixProduct(scal, mat, prod); for (int nCntRow = 0; nCntRow < 4; nCntRow++) { for (int nCntLine = 0; nCntLine < 4; nCntLine++) { mat[nCntRow][nCntLine] = prod[nCntRow][nCntLine]; } } return GZ_SUCCESS; }
int GzTrxMat(GzCoord translate, GzMatrix mat) { // Create translation matrix // Pass back the matrix using mat value float trans[4][4] = { { 1, 0, 0, translate[0] }, { 0, 1, 0, translate[1] }, { 0, 0, 1, translate[2] }, { 0, 0, 0, 1 } }; float prod[4][4]; MatrixProduct(trans, mat, prod); for (int nCntRow = 0; nCntRow < 4; nCntRow++) { for (int nCntLine = 0; nCntLine < 4; nCntLine++) { mat[nCntRow][nCntLine] = prod[nCntRow][nCntLine]; } } return GZ_SUCCESS; }
int GzRotXMat(float degree, GzMatrix mat) { // Create rotate matrix : rotate along x axis // Pass back the matrix using mat value float radius = degree*3.14 / 180; float Rx[4][4] = {{ 1, 0, 0, 0 }, { 0, cos(radius), -sin(radius), 0 }, { 0, sin(radius), cos(radius), 0 }, {0, 0, 0, 1}}; float prod[4][4]; MatrixProduct(Rx, mat, prod); for (int nCntRow = 0; nCntRow < 4; nCntRow++) { for (int nCntLine = 0; nCntLine < 4; nCntLine++) { mat[nCntRow][nCntLine] = prod[nCntRow][nCntLine]; } } return GZ_SUCCESS; }
void GetAMatrix (double *V, double *transferMatrix, double *A, int *eigenModesNumber, int *matDim) { int i, j, k, K, N; double *E; K=*eigenModesNumber; N=*matDim; E = malloc(sizeof(*E)*N*N); /*--- Vector rotation */ MatrixProduct(&N, &N, (double*)&V[0], &N, &N, &transferMatrix[0], E); /*--- A is 3d array. First index is eigenmode, other 2 are rows and columns of A matrix */ for (k=0; k<K; k++) { for (i=0; i<N; i++) { for (j=0; j<N; j++) { A[k*N*N+i*N+j]=E[i+2*k*N]*E[j+2*k*N]+E[i+(2*k+1)*N]*E[j+(2*k+1)*N]; } } } free(E); }
void Matrix4::Uviewpoint(const Coord3D& v1, const Coord3D& v2, const Coord3D& up) { // find the vector that points in the v21 direction Coord3D v_hat_21 = v2 - v1; // compute rotation matrix that takes -z axis to the v21 axis, // and y to the up direction Matrix4 rotMat; rotMat.UviewDirection(v_hat_21, up); // build matrix that translates the origin to v1 Matrix4 transMat; transMat.Identity(); transMat.m[3][0] = v1.cX; transMat.m[3][1] = v1.cY; transMat.m[3][2] = v1.cZ; // concatenate the matrices together MatrixProduct(rotMat, transMat); }
int GzPushMatrix(GzRender *render, GzMatrix matrix) { /* - push a matrix onto the Ximage stack - check for stack overflow */ render->matlevel++; if (render->matlevel == 0) { for (int nCntRow = 0; nCntRow < 4; nCntRow++) { for (int nCntLine = 0; nCntLine < 4; nCntLine++) { render->Ximage[render->matlevel][nCntRow][nCntLine] = matrix[nCntRow][nCntLine]; } } } else if (render->matlevel < MATLEVELS) { float prodTemp[4][4]; MatrixProduct(render->Ximage[render->matlevel - 1], matrix, prodTemp); for (int nCntRow = 0; nCntRow < 4; nCntRow++) { for (int nCntLine = 0; nCntLine < 4; nCntLine++) { render->Ximage[render->matlevel][nCntRow][nCntLine] = prodTemp[nCntRow][nCntLine]; } } } else { return GZ_FAILURE; } return GZ_SUCCESS; }
static void run(int vectorsSize, int matrixRows, int matrixCols) { int randDispersion = 10; vector<int> vectorA_ = Generator::generateVector(vectorsSize, randDispersion); vector<int> vectorB_ = Generator::generateVector(vectorsSize, randDispersion); cout << "Test vectors: \na: "; Console::printVector(vectorA_); cout << "b: "; Console::printVector(vectorB_); cout << "\n\n"; vector<vector<int> > matrixA_ = Generator::generateMatrixVector(matrixRows, matrixCols, randDispersion); vector<vector<int> > matrixB_ = Generator::generateMatrixVector(matrixRows, matrixCols, randDispersion); cout << "Test matrix: \nA:\n"; Console::printMatrix(matrixA_); cout << "\nB:\n"; Console::printMatrix(matrixB_); cout << "\n\n"; clock_t start = clock(); cout << "Task #2: \nSimple vectors product: a x b = "; Console::printVector(SimpleVectorsProduct(vectorA_, vectorB_)); cout << "\n\n"; cout << "Task #3: \nMatrix sum: A + B = \n"; Console::printMatrix(MatrixSum(matrixA_, matrixB_)); cout << "\n\n"; cout << "Task #4: \nMatrix product: A x B = \n"; Console::printMatrix(MatrixProduct(matrixA_, matrixB_)); cout << "\n\n"; cout << "Task #5: \nVectors products sum: Sum(a x b) = " << VectorsProductsSum(vectorA_, vectorB_) << "\n\n"; cout << "\n\nExec time: " << (clock() - start) / (double) (CLOCKS_PER_SEC / 1000) << " ms" << endl; }
void Matrix4::UviewDirection(const Coord3D& v21, const Coord3D& up) { double sine, cosine; // find the unit vector that points in the v21 direction Coord3D v_hat_21(v21); double len = v_hat_21.Magnitude(); Matrix4 amat; if (fabs(len) > stdEps) { len = 1.0 / len; v_hat_21 *= len; // rotate z in the xz-plane until same latitude sine = sqrt (1.0 - v_hat_21.cZ * v_hat_21.cZ); amat.RotateY(-v_hat_21.cZ, -sine); } else // error condition: zero length vecotr passed in -- do nothing */ amat.Identity(); // project v21 onto the xy plane Coord3D v_xy(v21); v_xy.cZ = 0.0; len = v_xy.Magnitude(); // rotate in the x-y plane until v21 lies on z axis --- // but of course, if its already there, do nothing Matrix4 bmat, cmat; if (fabs(len) > stdEps) { // want xy projection to be unit vector, so that sines/cosines pop out len = 1.0 / len; v_xy *= len; // rotate the projection of v21 in the xy-plane over to the x axis bmat.RotateZ(v_xy.cX, v_xy.cY); // concatenate these together cmat.MatrixProduct(amat, bmat); } else cmat = amat; /* up vector really should be perpendicular to the x-form direction -- * Use up a couple of cycles, and make sure it is, * just in case the user blew it. */ Coord3D up_proj; up_proj.Perpendicular(up, v_hat_21); len = up_proj.Magnitude(); if (fabs(len) > stdEps) { // normalize the vector len = 1.0/len; up_proj *= len; // compare the up-vector to the y-axis to get the cosine of the angle Coord3D tmp; tmp.cX = cmat.m[1][0]; tmp.cY = cmat.m[1][1]; tmp.cZ = cmat.m[1][2]; cosine = tmp.Dot(up_proj); // compare the up-vector to the x-axis to get the sine of the angle tmp.cX = cmat.m[0][0]; tmp.cY = cmat.m[0][1]; tmp.cZ = cmat.m[0][2]; sine = tmp.Dot(up_proj); // rotate to align the up vector with the y-axis amat.RotateZ(cosine, -sine); // This xform, although computed last, acts first MatrixProduct(amat, cmat); } else { // error condition: up vector is indeterminate (zero length) // -- do nothing *this = cmat; } }
int GzPutTriangle(GzRender *render, int numParts, GzToken *nameList, GzPointer *valueList) /* numParts : how many names and values */ { /* - pass in a triangle description with tokens and values corresponding to GZ_POSITION:3 vert positions in model space - Xform positions of verts using matrix on top of stack - Clip - just discard any triangle with any vert(s) behind view plane - optional: test for triangles with all three verts off-screen (trivial frustum cull) - invoke triangle rasterizer */ if ((NULL == render) || (NULL == nameList) || (NULL == valueList)) { return GZ_FAILURE; } for (int nCnt = 0; nCnt < numParts; nCnt++) { if (nameList[nCnt] == GZ_POSITION) { GzCoord* acGzCoord; acGzCoord = (GzCoord*)(valueList[nCnt]); GzMatrix topMat; int top = render->matlevel; memcpy(topMat, render->Ximage[top], sizeof(GzMatrix)); float aCoordMat[4][4] = { { acGzCoord[0][0], acGzCoord[1][0], acGzCoord[2][0], 0 }, { acGzCoord[0][1], acGzCoord[1][1], acGzCoord[2][1], 0 }, { acGzCoord[0][2], acGzCoord[1][2], acGzCoord[2][2], 0 }, { 1, 1, 1, 0 } }; float aCoordMatProduct[4][4]; MatrixProduct(topMat, aCoordMat, aCoordMatProduct); if ((aCoordMatProduct[3][0] == 0) || (aCoordMatProduct[3][1] == 0) || (aCoordMatProduct[3][2] == 0)) { continue; } #if 1 GzCoord asGzCoordX = { aCoordMatProduct[0][0] / aCoordMatProduct[3][0], aCoordMatProduct[0][1] / aCoordMatProduct[3][1], aCoordMatProduct[0][2] / aCoordMatProduct[3][2] }; GzCoord asGzCoordY = { aCoordMatProduct[1][0] / aCoordMatProduct[3][0], aCoordMatProduct[1][1] / aCoordMatProduct[3][1], aCoordMatProduct[1][2] / aCoordMatProduct[3][2] }; GzCoord asGzCoordZ = { aCoordMatProduct[2][0] / aCoordMatProduct[3][0], aCoordMatProduct[2][1] / aCoordMatProduct[3][1], aCoordMatProduct[2][2] / aCoordMatProduct[3][2] }; // clipping part if ((asGzCoordX[X] < 0 || asGzCoordX[X] > (render->display->xres)) && (asGzCoordX[Y] < 0 || asGzCoordX[Y] > (render->display->xres)) && (asGzCoordX[Z] < 0 || asGzCoordX[Z] > (render->display->xres))) { return GZ_FAILURE; } if ((asGzCoordY[X] < 0 || asGzCoordY[X] > (render->display->yres)) && (asGzCoordY[Y] < 0 || asGzCoordY[Y] > (render->display->yres)) && (asGzCoordY[Z] < 0 || asGzCoordY[Z] > (render->display->yres))) { return GZ_FAILURE; } if (asGzCoordZ[X] < 0 && asGzCoordZ[Y] < 0 && asGzCoordZ[Z] < 0) { return GZ_FAILURE; } #else GzCoord asGzCoordX = { acGzCoord[0][0], acGzCoord[1][0], acGzCoord[2][0] }; GzCoord asGzCoordY = { acGzCoord[0][1], acGzCoord[1][1], acGzCoord[2][1] }; GzCoord asGzCoordZ = { acGzCoord[0][2], acGzCoord[1][2], acGzCoord[2][2] }; #endif //Sort the matrix according to Y for (int anSortYCnt = 1; anSortYCnt < 3; anSortYCnt++) { if (asGzCoordY[anSortYCnt - 1] > asGzCoordY[anSortYCnt]) { float afSortTemmp; afSortTemmp = asGzCoordX[anSortYCnt - 1]; asGzCoordX[anSortYCnt - 1] = asGzCoordX[anSortYCnt]; asGzCoordX[anSortYCnt] = afSortTemmp; afSortTemmp = asGzCoordY[anSortYCnt - 1]; asGzCoordY[anSortYCnt - 1] = asGzCoordY[anSortYCnt]; asGzCoordY[anSortYCnt] = afSortTemmp; afSortTemmp = asGzCoordZ[anSortYCnt - 1]; asGzCoordZ[anSortYCnt - 1] = asGzCoordZ[anSortYCnt]; asGzCoordZ[anSortYCnt] = afSortTemmp; } } float afGzCoordY_Min = asGzCoordY[0]; // find min y float afGzCoordY_Max = asGzCoordY[2]; // find max y float tmp; for (int anSortYCnt = 1; anSortYCnt < 3; anSortYCnt++) { if ((asGzCoordY[anSortYCnt - 1] == asGzCoordY[anSortYCnt]) && (asGzCoordX[anSortYCnt - 1] > asGzCoordX[anSortYCnt])) { float afSortTemmp; afSortTemmp = asGzCoordX[anSortYCnt - 1]; asGzCoordX[anSortYCnt - 1] = asGzCoordX[anSortYCnt]; asGzCoordX[anSortYCnt] = afSortTemmp; afSortTemmp = asGzCoordY[anSortYCnt - 1]; asGzCoordY[anSortYCnt - 1] = asGzCoordY[anSortYCnt]; asGzCoordY[anSortYCnt] = afSortTemmp; afSortTemmp = asGzCoordZ[anSortYCnt - 1]; asGzCoordZ[anSortYCnt - 1] = asGzCoordZ[anSortYCnt]; asGzCoordZ[anSortYCnt] = afSortTemmp; } } float afCoefA = asGzCoordY[2] - asGzCoordY[0]; float afCoefB = asGzCoordX[0] - asGzCoordX[2]; float afCoefC = -afCoefB*asGzCoordY[0] - afCoefA * asGzCoordX[0]; float acTestX = -(afCoefB*asGzCoordY[1] + afCoefC) / afCoefA; if (acTestX < asGzCoordX[1]) { tmp = asGzCoordY[2]; asGzCoordY[2] = asGzCoordY[1]; asGzCoordY[1] = tmp; tmp = asGzCoordX[2]; asGzCoordX[2] = asGzCoordX[1]; asGzCoordX[1] = tmp; tmp = asGzCoordZ[2]; asGzCoordZ[2] = asGzCoordZ[1]; asGzCoordZ[1] = tmp; } //find the max and min for X and Y float afMaxX, afMinX, afMaxY, afMinY; afMaxX = asGzCoordX[0]; afMinX = asGzCoordX[0]; afMaxY = asGzCoordY[0]; afMinY = asGzCoordY[0]; for (int anCntMinMaxCnt = 1; anCntMinMaxCnt < 3; anCntMinMaxCnt++) { if (afMaxX <= asGzCoordX[anCntMinMaxCnt]) { afMaxX = asGzCoordX[anCntMinMaxCnt]; } if (afMinX >= asGzCoordX[anCntMinMaxCnt]) { afMinX = asGzCoordX[anCntMinMaxCnt]; } if (afMaxY <= asGzCoordY[anCntMinMaxCnt]) { afMaxY = asGzCoordY[anCntMinMaxCnt]; } if (afMinY >= asGzCoordY[anCntMinMaxCnt]) { afMinY = asGzCoordY[anCntMinMaxCnt]; } } //set the boundary of x and y if (afMinX < 0) { afMinX = 0; } else if (afMinX > 255) { afMinX = 255; } if (afMaxX < 0){ afMaxX = 0; } else if (afMaxX > 255) { afMaxX = 255; } if (afMinY < 0){ afMinY = 0; } else if (afMinY > 255) { afMinY = 255; } if (afMaxY < 0){ afMaxY = 0; } else if (afMaxY > 255) { afMaxY = 255; } //set the variants of vertex Ax + By + C = 0 float afCoefA1, afCoefA2, afCoefA3, afCoefB1, afCoefB2, afCoefB3, afCoefC1, afCoefC2, afCoefC3; float AfDiff1, AfDiff2, AfDiff3; float afVar1, afVar2, afVar3, afVar4; GzIntensity asR, asG, asB, asAlpha; GzDepth anZ; int anInterZ; afCoefA1 = asGzCoordY[1] - asGzCoordY[0]; afCoefA2 = asGzCoordY[2] - asGzCoordY[1]; afCoefA3 = asGzCoordY[0] - asGzCoordY[2]; afCoefB1 = -(asGzCoordX[1] - asGzCoordX[0]); afCoefB2 = -(asGzCoordX[2] - asGzCoordX[1]); afCoefB3 = -(asGzCoordX[0] - asGzCoordX[2]); afCoefC1 = ((-(afCoefB1)*asGzCoordY[1]) - (afCoefA1 * asGzCoordX[1])); afCoefC2 = ((-(afCoefB2)*asGzCoordY[2]) - (afCoefA2 * asGzCoordX[2])); afCoefC3 = ((-(afCoefB3)*asGzCoordY[0]) - (afCoefA3 * asGzCoordX[0])); afVar1 = (((asGzCoordY[1] - asGzCoordY[0])*(asGzCoordZ[2] - asGzCoordZ[0])) - ((asGzCoordY[2] - asGzCoordY[0])*(asGzCoordZ[1] - asGzCoordZ[0]))); afVar2 = (((asGzCoordX[2] - asGzCoordX[0])*(asGzCoordZ[1] - asGzCoordZ[0])) - ((asGzCoordX[1] - asGzCoordX[0])*(asGzCoordZ[2] - asGzCoordZ[0]))); afVar3 = (((asGzCoordX[1] - asGzCoordX[0])*(asGzCoordY[2] - asGzCoordY[0])) - ((asGzCoordX[2] - asGzCoordX[0])*(asGzCoordY[1] - asGzCoordY[0]))); afVar4 = -((asGzCoordY[0] * (afVar2)) + (asGzCoordX[0] * (afVar1)) + (asGzCoordZ[0] * (afVar3))); for (int anCntRangeX = int(afMinX); (float)anCntRangeX < (afMaxX); anCntRangeX++){ for (int anCntRangeY = int(afMinY); (float)anCntRangeY < (afMaxY); anCntRangeY++){ AfDiff1 = (afCoefA1*anCntRangeX) + (afCoefB1*anCntRangeY) + afCoefC1; AfDiff2 = (afCoefA2*anCntRangeX) + (afCoefB2*anCntRangeY) + afCoefC2; AfDiff3 = (afCoefA3*anCntRangeX) + (afCoefB3*anCntRangeY) + afCoefC3; anInterZ = -(afVar1*anCntRangeX + afVar2*anCntRangeY + afVar4) / afVar3; if (!(AfDiff1 < 0 || AfDiff2 < 0 || AfDiff3 < 0)){ asR = 0; asG = 0; asB = 0; asAlpha = 0; anZ = 0; if (GZ_FAILURE == GzGetDisplay(render->display, anCntRangeX, anCntRangeY, &asR, &asG, &asB, &asAlpha, &anZ)) { return GZ_FAILURE; } if (anZ == 0 || anInterZ < anZ) { if (GZ_FAILURE == GzPutDisplay(render->display, anCntRangeX, anCntRangeY, ctoi(render->flatcolor[RED]), ctoi(render->flatcolor[GREEN]), ctoi(render->flatcolor[BLUE]), asAlpha, anInterZ)) { return GZ_FAILURE; } } } } } } else if (nameList[nCnt] == GZ_NULL_TOKEN) { continue; } } return GZ_SUCCESS; }
int main(int argc, char **argv) { MatrixProduct(); return 0; }