Your IP : 216.73.216.40


Current Path : /var/www/html/venkat/check3/file/cg2013/pawan/
Upload File :
Current File : /var/www/html/venkat/check3/file/cg2013/pawan/RIT2013002_1.cpp

//Lab Assignment 
//RIT2013002 Saniya Najeeb

/*Square (x,y) : (-100,700) (-100, 500) (100,700) (100,500) (Z=0)
Lines ---
(-50, 0;-50,500)(-150, 0;-500,400)(-500,400;-100,700)(-50, 0;-400,400)(-400, 400 ;-100,600)
+ Symmetric lines w.r.t. x + (5 lines)
Look at vertex (0,0,0)
Eye coordinate (0,-100,10)
UP vector (0,0,10)
Distance to screen d = 10
Draw the diagram change to camera frame and do perspective projection.
Print the coordinates of vertex as INT (P+0.5) in the order of input.*/

#include <iostream>
#include <math.h>
#include <string.h>

using namespace std;

double a[24][3];
double obs[3];
double lp[3];
double upV[3];
double lc[3];
double rc[3];
double uc[3];
double camera[4][4];
double cam[19][6];
double mul[4][1];
double presp_proj[19][6];
double lv[3];
double rv[3];
double upper[3];
double d;

void MatrixMultiplication(int n1,int n2,int n3,int i1,int i2){
	double ans[n1][n3];
	int i, j, k;
	
	memset(ans,0,sizeof(ans));
	
	for(i = 0; i < n1; i++){
		for(j = 0; j < n3; j++){
			for(k = 0; k < n2; k++){
				ans[i][j] += camera[i][k]*mul[k][j];
			}
		}
	}
	for(int i = 0;i<3;i++)
		cam[i1][i+i2] = ans[i][0];
}

void Simulate(){ //Computing the LeftCap, RightCap, UpperVector
	int i;
	double lmod, rmod;

	
	for(i = 0; i < 3; i++){
		lv[i] = lp[i]-obs[i];
	}
	
	lmod = sqrt(lv[0]*lv[0]+lv[1]*lv[1]+lv[2]*lv[2]);
	
	for(i = 0; i < 3; i++){
		lc[i] = lmod > 0 ? lv[i]/lmod : lv[i];
	}
		
	rv[0] = lv[1]*upV[2]-lv[2]*upV[1];
	rv[1] = lv[2]*upV[0]-lv[0]*upV[2];
	rv[2] = lv[0]*upV[1]-lv[1]*upV[0];
	
	rmod = sqrt(rv[0]*rv[0]+rv[1]*rv[1]+rv[2]*rv[2]);
	
	for(i = 0; i < 3; i++){
		rc[i] = rmod > 0 ? rv[i]/rmod : rv[i];
	}
	
	upper[0] = rv[1]*lv[2]-rv[2]*lv[1];
	upper[1] = rv[2]*lv[0]-rv[0]*lv[2];
	upper[2] = rv[0]*lv[1]-rv[1]*lv[0];
	
	double umod = sqrt(upper[0]*upper[0]+upper[1]*upper[1]+upper[2]*upper[2]);
	
	for(i = 0; i < 3; i++){
		uc[i] = umod > 0 ? upper[i]/umod : upper[i];
	}
}

void ComputeCameraMatrix(){ //Computing the Camera Matrix
	
	memset(camera,0,sizeof(camera));
	int i, j;
	
	camera[0][0] = rc[0];
	camera[0][1] = rc[1];
	camera[0][2] = rc[2];	
		
	camera[1][0] = uc[0];
	camera[1][1] = uc[1];
	camera[1][2] = uc[2];		
		
	camera[2][0] = (-1)*lc[0];
	camera[2][1] = (-1)*lc[1];
	camera[2][2] = (-1)*lc[2];		
		
	camera[0][3] += (-1)*(rc[0]*obs[0]);
	camera[0][3] += (-1)*(rc[1]*obs[1]);
	camera[0][3] += (-1)*(rc[2]*obs[2]);
		
	camera[1][3] += (-1)*(uc[0]*obs[0]);
	camera[1][3] += (-1)*(uc[1]*obs[1]);
	camera[1][3] += (-1)*(uc[2]*obs[2]);
	
	camera[2][3] += lc[0]*obs[0];
	camera[2][3] += lc[1]*obs[1];
	camera[2][3] += lc[2]*obs[2];
	camera[3][3] = 1;
	
}

void PrespectiveProjection(){
	double dm;
	int i, j;
	
	for(i = 1; i <= 14; i++){
		dm = cam[i][2]/d;
		for(j = 0; j < 3; j++){
			presp_proj[i][j] = cam[i][j]/dm;
		}
		dm = cam[i][5]/d;
		for(j = 3; j < 6; j++){
			presp_proj[i][j] = cam[i][j]/dm;
		}
	}
	
}

void Display(){
	int i, j;
	
	for (i = 1; i <= 4; i++) {
		for (j = 0; j < 2; j++) {
			printf("%d,", (int)(presp_proj[i][j]+0.5));
		}
		printf("%d\n",(int)(presp_proj[i][2]+0.5));
	}
	
	for(i = 5; i <= 14; i++){
		for(j = 0; j < 2; j++){
			printf("%d,", (int)(presp_proj[i][j]+0.5));
		}
		printf("%d\n",(int)(presp_proj[i][2]+0.5));
		for(j = 3; j < 5; j++){
			printf("%d,", (int)(presp_proj[i][j]+0.5));
		}
		printf("%d\n",(int)(presp_proj[i][5]+0.5));	
	}
}
int main()
{
	int i, j;
	obs[0] = 0; obs[1] = -100; obs[2] = 10; //Cordinates of Observer
	
	lp[0] = 0; lp[1] = 0; lp[2] = 0; //Coordinates of Lookat Point
	
	upV[0] = 0; upV[1] = 0; upV[2] = 10; //Cordinates of Up Vector
	
	d = 10; //Distance to screen d=10

	//Initializing the coordinates
	double a[][6] = { 0,0,0,0,0,0,-100,700,0,-100,500,0,-100,500,0,100,500,0,100,700,0,-100,700,0,
	100,500,0,100,700,0,-50,0,0,-50,500,0,-150,0,0,-500,400,0,-500,400,0,-100,700,0,-50,0,0,-400,400,0,
	-400,400,0,-100,600,0,50,0, 0,50,500,0,150,0,0,500,400,0,500,400,0,100,700,0,50,0,0,400,400,0,400,400,0,100,600,0};	

	Simulate(); //Computation of LeftVector, Right Vector and Upper Vector
	
	ComputeCameraMatrix(); // Comuting the Camera Matrix using UpVector, RightVector and Left Vector
	
	for(int i = 1;i <= 14;i++){
		mul[0][0] = a[i][0];
		mul[1][0] = a[i][1];
		mul[2][0] = a[i][2];
		mul[3][0] = 1;
		
		MatrixMultiplication(4,4,1,i,0);
		
		mul[0][0] = a[i][3];
		mul[1][0] = a[i][4];
		mul[2][0] = a[i][5];
		mul[3][0] = 1;
		
		MatrixMultiplication(4,4,1,i,3);
	}
	
	/*for (i = 1; i <= 14; i++) {
		for (j = 0; j < 6; j++) {
			printf("%lf ", cam[i][j]);		
		}
		printf("\n");
	}*/
	
	PrespectiveProjection(); //Change to Camera Frmae and Prespective Projection at d = 10;

	
	Display(); //Display the Cordinates
	
	return 0;
}