const Object *ObjReader::ReadIndirectObj(int nObjNum, int nGeneration) { unsigned int nOffset; char str[4]; nOffset = m_pXref->GetOffset(nObjNum, nGeneration); if (nOffset == 0) return NULL; m_pSource->Seek(nOffset, SEEK_SET); m_pSource->ReadInt(); //object number m_pSource->ReadInt(); //generation m_pSource->ReadStr(str, sizeof(str)); //obj return ReadObj(); }
// ********************************************************************* // File System, a process to answer queries and return file contents // via message passing // ********************************************************************* void FileSystem() { int SHELL, // the shell that's asking FileSystem for service result, // the result to be included to return to shell i, my_pid; msg_t msg; // after all the directory entries have been assigned, then fill in the size // of this "directory". _dir[1] is ".." which must point to parent dir root_dir[0].size = sizeof( root_dir ); bin_dir[0].size = sizeof( bin_dir ); bin_dir[1].size = root_dir[0].size; www_dir[0].size = sizeof( www_dir ); www_dir[1].size = root_dir[0].size; // mark all file descriptors as available, make sure UNUSED in FileSystem.h for(i=0; i<MAX_FD; i++) fd_array[i].owner = UNUSED; my_pid = GetPid(); while(1) { // start serving requests for shells MsgRcv(my_pid, &msg); SHELL = msg.sender; // shell's pid to return query results switch( msg.code ) { // depending on what's requested // CHK_OBJ: Shell wants to "check" obj (name in msg.data), // and attr of it will be returned via msg in msg.data as well case CHK_OBJ: result = ChkObj( msg.data, (attr_t *)msg.data ); break; // OPEN_OBJ: Shell wants to open obj, an FD will be returned // in msg.number[0], the ownership (SHELL) will be recorded case OPEN_OBJ: result = OpenObj( msg.data, SHELL, &msg.number[0] ); break; // READ_OBJ: Shell wants content of obj FD (msg.number[0]) // return (msg.data) and # of bytes read (msg.number[1]) case READ_OBJ: result = ReadObj( msg.number[0], msg.data, SHELL, &msg.number[1] ); break; // CLOSE_OBJ: Shell wants to close FD (msg.number[0]) // check if the owner of FD is Shell (SHELL) case CLOSE_OBJ: result = CloseObj( msg.number[0], SHELL ); break; default: // unknown code received result = UNKNOWN; cons_printf( "FileSystem: Bad code %d in message from PID #%d\n", msg.code, SHELL ); } msg.code = result; MsgSnd( SHELL, &msg ); } }
double RecoverAffine(double **matrix, double cost[ANGLE][ANGLE][CAMNUM_2][CAMNUM_2], int *MinSrcCam) { double err, MinErr; int align[60][20], i, j, k, angle, index, srcCam; FILE *fpt; char filename[100]; pVer VerRot; pTri TriRot; int NumVerRot, NumTriRot; vector e1[2], e2[2]; // coordinate of edge // read align sequence fpt = fopen("align20.txt", "r"); for(i=0; i<60; i++) for(j=0; j<CAMNUM_2; j++) fscanf(fpt, "%d", &align[i][j]); fclose(fpt); // get the minimum error among those alignment MinErr = DBL_MAX; for(srcCam=0; srcCam<ANGLE; srcCam++) // each src angle for(i=0; i<ANGLE; i++) // each dest angle for(j=0; j<60; j++) // each align { err = 0; for(k=0; k<CAMNUM_2; k++) // each vertex err += cost[srcCam][i][k][align[j][k]]; if( err < MinErr ) { MinErr = err; *MinSrcCam = srcCam; angle = i; index = j; } } sprintf(filename, "12_%1d", *MinSrcCam); ReadObj(filename, &VerRot, &TriRot, &NumVerRot, &NumTriRot); e1[0].x = VerRot[0].coor[0]; e1[0].y = VerRot[0].coor[1]; e1[0].z = VerRot[0].coor[2]; e1[1].x = VerRot[1].coor[0]; e1[1].y = VerRot[1].coor[1]; e1[1].z = VerRot[1].coor[2]; free(VerRot); free(TriRot); sprintf(filename, "12_%1d", angle); ReadObj(filename, &VerRot, &TriRot, &NumVerRot, &NumTriRot); e2[0].x = VerRot[align[index][0]].coor[0]; e2[0].y = VerRot[align[index][0]].coor[1]; e2[0].z = VerRot[align[index][0]].coor[2]; e2[1].x = VerRot[align[index][1]].coor[0]; e2[1].y = VerRot[align[index][1]].coor[1]; e2[1].z = VerRot[align[index][1]].coor[2]; free(VerRot); free(TriRot); RotateMatrix(matrix, e1, e2); // write the matrix to disk fpt=fopen("result.txt", "a"); fprintf(fpt, "\n%s to %s\n", destfn, srcfn); fprintf(fpt, "SrcAngle = %d; DestAngle = %d; index = %d; err= %f\n", *MinSrcCam, angle, index, MinErr); // for(i=0; i<4; i++) // { // for(j=0; j<4; j++) // fprintf(fpt, "%lf ", matrix[i][j]); // fprintf(fpt, "\n"); // } fclose(fpt); // return use which camera return MinErr; }
const Object *ObjReader::ReadObj(void) { unsigned int nOffset, nOffTmp; int c, n; Object *pObj; Dictionary *pDict; const Object *pcObj; char str[32 * 1024], *ptr; int nParentheses; nOffset = m_pSource->Position(); c = m_pSource->Read(); switch (c) { case '/': //name pObj = new Name(); m_pSource->ReadStr(str, sizeof(str)); ((Name *)pObj)->SetValue(str); break; case '(': //string pObj = new String(); ptr = str; nParentheses = 0; while (true) { c = m_pSource->Read(); if (c == '\\') { *ptr++ = c; c = m_pSource->Read(); } else if (c == '(') ++nParentheses; else if (c == ')') { if (--nParentheses <= 0) break; } *ptr++ = c; } *ptr = '\0'; ((String *)pObj)->SetValue(str, ptr - str, String::LITERAL); break; case '<': c = m_pSource->Read(); if (c == '<') //dictionary { pDict = new Dictionary(); while (true) { m_pSource->Skip(); c = m_pSource->Read(); if (c == '>') { m_pSource->Read(); //> m_pSource->Skip(); m_pSource->Read(str, 6); if (strncmp(str, "stream", 6) == 0) //stream { pcObj = pDict->GetValue("Length"); if (pcObj->GetType() == Object::OBJ_REFERENCE) { nOffTmp = m_pSource->Position(); pcObj = ReadIndirectObj(((const Reference *)pcObj)->GetObjNum(), ((const Reference *)pcObj)->GetGeneration()); m_pSource->Seek(nOffTmp, SEEK_SET); n = ((const Numeric *)pcObj)->GetValue(); delete pcObj; } else n = ((const Numeric *)pcObj)->GetValue(); pObj = new Stream(pDict); m_pSource->Skip(); ptr = new char[n]; m_pSource->Read(ptr, n); ((Stream *)pObj)->SetValue((unsigned char *)ptr, n); delete[] ptr; } else { pObj = pDict; m_pSource->Seek(-6, SEEK_CUR); } break; } else { m_pSource->Seek(-1, SEEK_CUR); pcObj = ReadObj(); pDict->Add(pcObj, ReadObj()); } } } else //hexadecimal string { m_pSource->Seek(-1, SEEK_CUR); pObj = new String(); n = m_pSource->ReadStr(str, sizeof(str)); ((String *)pObj)->SetValue(str, n, String::HEXADECIMAL); m_pSource->Read(); //> } break; case '[': //array pObj = new Array(); while (true) { m_pSource->Skip(); c = m_pSource->Read(); if (c == ']') break; m_pSource->Seek(-1, SEEK_CUR); ((Array *)pObj)->Add(ReadObj()); } break; case EOF: pObj = NULL; break; default: *str = c; m_pSource->ReadStr(str + 1, sizeof(str) - 1); if ((c >= '0' && c <= '9') || c == '-' || c == '+' || c == '.') { pObj = new Numeric(); ((Numeric *)pObj)->SetValue(atof(str)); } else { if (strcmp(str, "R") == 0) pObj = new Reference(); else if (strcmp(str, "true") == 0) { pObj = new Boolean(); ((Boolean *)pObj)->SetValue(true); } else if (strcmp(str, "false") == 0) { pObj = new Boolean(); ((Boolean *)pObj)->SetValue(false); } else if (strcmp(str, "null") == 0) pObj = new Null(); else if (*str == ' ') // is this normal? pObj = new Object(); // invalid object else { pObj = new Operator(); ((Operator *)pObj)->SetValue(str); } } break; } m_pSource->Skip(); if (pObj != NULL) pObj->SetOffset(nOffset); return pObj; }
double Refine(double src_Coeff[CAMNUM][ART_ANGULAR][ART_RADIAL], pVer vertex2, pTri triangle2, int NumVer2, int NumTri2, int UseCam, pVer CamVertex, pTri CamTriangle, int CamNumVer, int CamNumTri) { unsigned char *destBuff[CAMNUM]; pVer TmpVertex; pTri TmpTriangle; int TmpNumVer, TmpNumTri; // total number of vertex and triangle. int i, index=0, direct, flag, iter; double angle; double dist[3]; char filename[100]; FILE *fpt; char word[]="XYZ"; double **matrix; vector e1[2], e2[2]; // coordinate of edge // for region shape descriptor double dest_Coeff[CAMNUM][ART_ANGULAR][ART_RADIAL]; void (*pRotate[3])(pVer, double , pVer , int ); pRotate[0] = RotateX; pRotate[1] = RotateY; pRotate[2] = RotateZ; fpt = fopen("refine.txt", "a"); fprintf(fpt, "\nDest: %s ; Src: %s\n", destfn, srcfn); // ******************************************************************************** // capture CAMNUM silhouette of srcfn to memory sprintf(filename, "12_%d", UseCam); ReadObj(filename, &TmpVertex, &TmpTriangle, &TmpNumVer, &TmpNumTri); // ******************************************************************************** // capture CAMNUM silhouette of destfn to memory, // and get the cost between srcfn silhouette and destfn silhouette // read REB only, so size is winw*winh for(i=0; i<CAMNUM; i++) destBuff[i] = (unsigned char *) malloc (winw * winh * sizeof(unsigned char)); // initialize matrix and camera of model 1 matrix = (double **) malloc (4 * sizeof(double *)); for(i=0; i<4; i++) { matrix[i] = (double *) malloc(4 * sizeof(double)); memset(matrix[i], 0, 4 * sizeof(double)); } matrix[0][0] = matrix[1][1] = matrix[2][2] = matrix[3][3] = 1; e1[0].x = CamVertex[0].coor[0]; e1[0].y = CamVertex[0].coor[1]; e1[0].z = CamVertex[0].coor[2]; e1[1].x = CamVertex[1].coor[0]; e1[1].y = CamVertex[1].coor[1]; e1[1].z = CamVertex[1].coor[2]; // initialize dist[1] dist[1] = Distance(CamVertex, destBuff, dest_Coeff, src_Coeff, vertex2, triangle2, NumVer2, NumTri2); // iterative several times until no change iter = 0; do { flag = 0; iter ++; // iterative times printf("Iterative %d\n", iter); fprintf(fpt, "Iterative %d\n", iter); // for each angle for(angle = 3.1415926 * 10.0 / 180.0; angle > 3.1415926 * 1.0 / 180.0; angle /= 2.0) { // Rotate x, y, z to each polyhedron for(direct=0; direct<3; direct++) { pRotate[direct](CamVertex, -angle, TmpVertex, CamNumVer); dist[0] = Distance(TmpVertex, destBuff, dest_Coeff, src_Coeff, vertex2, triangle2, NumVer2, NumTri2); pRotate[direct](CamVertex, angle, TmpVertex, CamNumVer); dist[2] = Distance(TmpVertex, destBuff, dest_Coeff, src_Coeff, vertex2, triangle2, NumVer2, NumTri2); if( dist[0] < dist[1] && dist[0] < dist[2]) { flag = 1; do { if( dist[1] < dist[0] ) dist[0] = dist[1]; // pRotate[direct](vertex2, angle, vertex2, NumVer2); printf("Rotate Camera %c: %f\tError: %f\n", word[direct], -angle, dist[0]); fprintf(fpt, "Rotate Camera %c: %f\tError: %f\n", word[direct], -angle, dist[0]); // save two model //sprintf(filename, "%s_to_%s_refine_%d.obj", destfn, srcfn, index++); //SaveMergeObj(filename, vertex1, triangle1, NumVer1, NumTri1, vertex2, triangle2, NumVer2, NumTri2); pRotate[direct](CamVertex, -angle, CamVertex, CamNumVer); pRotate[direct](CamVertex, -angle, TmpVertex, CamNumVer); dist[1] = Distance(TmpVertex, destBuff, dest_Coeff, src_Coeff, vertex2, triangle2, NumVer2, NumTri2); }while( dist[1] < dist[0] ); dist[1] = dist[0]; } else if( dist[2] < dist[1] && dist[2] < dist[0]) { flag = 1; do { if( dist[1] < dist[2] ) dist[2] = dist[1]; // pRotate[direct](vertex2, -angle, vertex2, NumVer2); printf("Rotate Camera %c: %f\tError: %f\n", word[direct], angle, dist[2]); fprintf(fpt, "Rotate Camera %c: %f\tError: %f\n", word[direct], angle, dist[2]); // save two model //sprintf(filename, "%s_to_%s_refine_%d.obj", destfn, srcfn, index++); //SaveMergeObj(filename, vertex1, triangle1, NumVer1, NumTri1, vertex2, triangle2, NumVer2, NumTri2); pRotate[direct](CamVertex, angle, CamVertex, CamNumVer); pRotate[direct](CamVertex, angle, TmpVertex, CamNumVer); dist[1] = Distance(TmpVertex, destBuff, dest_Coeff, src_Coeff, vertex2, triangle2, NumVer2, NumTri2); }while( dist[1] < dist[2] ); dist[1] = dist[2]; } } } }while( flag && iter < MAX_ITER ); e2[0].x = CamVertex[0].coor[0]; e2[0].y = CamVertex[0].coor[1]; e2[0].z = CamVertex[0].coor[2]; e2[1].x = CamVertex[1].coor[0]; e2[1].y = CamVertex[1].coor[1]; e2[1].z = CamVertex[1].coor[2]; RotateMatrix(matrix, e1, e2); Rotate(vertex2, NumVer2, matrix); for(i=0; i<4; i++) free(matrix[i]); free(matrix); free(TmpVertex); free(TmpTriangle); for(i=0; i<CAMNUM; i++) free(destBuff[i]); fclose(fpt); return dist[1]; // return the minimum error }