/* * hw02.c: * * For a cube specified in three-space by the coordinates of its vertices, * perform two rotations and one tanslation. Then project the cube into * the image plane. * * 2/15/2002 jph * */ #include #include #include #include #include /* * Type for a point in upright pinhole camera model (NOTES p. 1.17) */ struct point { float X; /* X-coordinate in the real world */ float Y; /* Y-coordinate in the real world */ float Z; /* Z-coordinate in the real world */ float x; /* x-coordinate in the image plane */ float y; /* y-coordinate in the image plane */ }; typedef struct point point; typedef point *Ppoint; /*----------------------------------------------------------------------*/ /* MAIN */ /*----------------------------------------------------------------------*/ main(argc,argv) int argc; char *argv[]; { int i; /* loop counter */ point P[8]; /* array of cube corners */ float f; /* camera focal length in meters */ double Theta; /* rotation angle */ double Xold; /* old X coordinate of a point */ double Yold; /* old Y coordinate of a point */ double Zold; /* old Z coordinate of a point */ /* * Initialize the world coordinates of the eight cube corners */ P[0].X = 0.00; P[0].Y = 0.00; P[0].Z = 0.00; P[1].X = 0.00; P[1].Y = 0.00; P[1].Z = 0.05; P[2].X = 0.05; P[2].Y = 0.00; P[2].Z = 0.05; P[3].X = 0.05; P[3].Y = 0.00; P[3].Z = 0.00; P[4].X = 0.00; P[4].Y = 0.05; P[4].Z = 0.00; P[5].X = 0.00; P[5].Y = 0.05; P[5].Z = 0.05; P[6].X = 0.05; P[6].Y = 0.05; P[6].Z = 0.05; P[7].X = 0.05; P[7].Y = 0.05; P[7].Z = 0.00; /* * First rotation - update X & Z coordinates of all eight points */ Theta = (double)30.0 * (double)2.0 * M_PI / (double)360.0; for (i=0; i < 8; i++) { Zold = (double)P[i].Z; Xold = (double)P[i].X; P[i].Z = (float)(cos(Theta)*Zold - sin(Theta)*Xold); P[i].X = (float)(sin(Theta)*Zold + cos(Theta)*Xold); } /* * Second rotation - update Y & Z coordinates of all eight points */ Theta = (double)-20.0 * (double)2.0 * M_PI / (double)360.0; for (i=0; i < 8; i++) { Yold = (double)P[i].Y; Zold = (double)P[i].Z; P[i].Y = (float)(cos(Theta)*Yold - sin(Theta)*Zold); P[i].Z = (float)(sin(Theta)*Yold + cos(Theta)*Zold); } /* * Translation: update Z coordinates of all eight points */ for (i=0; i < 8; i++) { P[i].Z += (float)1.0; } /* * Project all eight points into the image plane */ f = (float)0.05; for (i=0; i < 8; i++) { if (P[i].Z == (float)0.0) { printf("\nError: point %d has Z=0.0\n\n",i); exit(-1); } else { P[i].x = f/P[i].Z * P[i].X; P[i].y = f/P[i].Z * P[i].Y; } } /* * Report */ printf("\n\n"); for (i=0; i < 8; i++) { printf("P%d: x=%9.5f y=%9.5f\n",i+1,P[i].x,P[i].y); } printf("\n\n"); return; } /*---------------- Main ----------------------------------------------*/