예제 #1
0
파일: track.c 프로젝트: one-k/rmov
/*
  call-seq: rotate(degrees)
  
  Rotate the track by the given number of degrees.
*/
static VALUE track_rotate(VALUE obj, VALUE degrees)
{
  MatrixRecord matrix;
  GetTrackMatrix(TRACK(obj), &matrix);
  RotateMatrix(&matrix, FloatToFixed(NUM2DBL(degrees)), 0, 0);
  SetTrackMatrix(TRACK(obj), &matrix);
  return obj;
}
//The function recursively builds up the transformation of the pThing 
//until it's in room-space coordinates
HRESULT C2DThingCoordTransformer::BuildTransformation(IThing * pThing, IThing * pParentThing)
{
	IVector * pVector = NULL;
	HRESULT hr = S_OK;
	IThing* pTmpParentThing = NULL;
	D3DRMMATRIX4D tmpMatrix;
	float flScaleX, flScaleY, flScaleZ;
	float flThingX, flThingY, flThingZ, // Position of current Thing having cells edited
	      flThingDirX, flThingDirY, flThingDirZ; // Orientation of current Thing having cells edited

	if (!pThing || !pParentThing)
		goto EXIT_FAIL;

	// Store the position Vector of the Thing
	hr = pThing->get_ObjectProperty(bstrPosition, (IObjectProperty **) &pVector);
	if( FAILED(hr) || !pVector) goto EXIT_FAIL;
	hr = pVector->get(&flThingX, &flThingY, &flThingZ);
	if( FAILED(hr) ) goto EXIT_FAIL;
	SAFERELEASE(pVector);

	// Get the Scale Vector of the Thing
	hr = pThing->get_ObjectProperty(bstrScale, (IObjectProperty **) &pVector);
	if( FAILED(hr) || !pVector) goto EXIT_FAIL;
	hr = pVector->get(&flScaleX, &flScaleY, &flScaleZ);
	if( FAILED(hr) ) goto EXIT_FAIL;
	SAFERELEASE(pVector);

	// Store the Direction Vector of the Thing
	hr = pThing->get_ObjectProperty(bstrDirection, (IObjectProperty **) &pVector);
	if( FAILED(hr) ) goto EXIT_FAIL;
	hr = pVector->get(&flThingDirX, &flThingDirY, &flThingDirZ);
	if( FAILED(hr) ) goto EXIT_FAIL;
	SAFERELEASE(pVector);

	IdentityMatrix(&tmpMatrix);
	ScaleMatrix(&tmpMatrix, flScaleX, flScaleY, flScaleZ);
	RotateMatrix(&tmpMatrix, flThingDirX, flThingDirY, flThingDirZ);
	TranslateMatrix(&tmpMatrix, flThingX, flThingY, flThingZ);
	PostMultiplyMatrix(&m_d3dMatrix, &tmpMatrix);

	IdentityMatrix(&tmpMatrix);
	TranslateMatrix(&tmpMatrix, -flThingX, -flThingY, -flThingZ);
	InverseRotateMatrix(&tmpMatrix, flThingDirX, flThingDirY, flThingDirZ);
	ScaleMatrix(&tmpMatrix, 1.0f / flScaleX, 1.0f / flScaleY, 1.0f / flScaleZ);
	PostMultiplyMatrix(&m_d3dInverseMatrix, &tmpMatrix);

	hr = pParentThing->get_Container(&pTmpParentThing);
	if ( SUCCEEDED(hr) && pTmpParentThing)
	{
		BuildTransformation(pParentThing, pTmpParentThing);
	}


EXIT_FAIL:
	SAFERELEASE(pTmpParentThing);
	SAFERELEASE(pVector);
	return hr;
}
예제 #3
0
void main()
{
	int arr[][4] = {
		{15, 20, 40, 85},
		{20, 35, 80, 95},
		{30, 55, 95, 105},
		{40, 80, 100, 120}};

	vector< vector< int > > vArr;
	vArr.push_back(vector< int >(&arr[0][0], &arr[0][4]));
	vArr.push_back(vector< int >(&arr[1][0], &arr[1][4]));
	vArr.push_back(vector< int >(&arr[2][0], &arr[2][4]));
	vArr.push_back(vector< int >(&arr[3][0], &arr[3][4]));

	PrintVector(vArr);

	RotateMatrix(vArr);

	PrintVector(vArr);
}
예제 #4
0
	Ray HeterogeneousVolume::scatter(Ray &inRay) const{
		Ray outRay;
		outRay.isDeltaDirection = false;

		bool go_in_vol = inRay.intersectObj == this && inRay.insideObj != this;
		bool be_in_vol = inRay.insideObj == this;

		// CASE1: Go in volume.
		if(go_in_vol){
			vec3f position = inRay.origin + inRay.direction*inRay.intersectDist;
			vec3f normal = inRay.intersectObj->getWorldNormal(inRay.intersectTriangleID, position);

			outRay.origin = position;
			outRay.direction = inRay.direction;

			vec3f reflDir = -normal.dot(inRay.direction)*normal*2 + inRay.direction;
			reflDir.normalize();
			float theta = acos(inRay.direction.dot(normal));

			AbstractObject* currentInsideObject = inRay.insideObj;
			AbstractObject* outSideObject = (AbstractObject*)this;

			float current_n = currentInsideObject ? currentInsideObject->getIOR() : 1;
			float next_n = outSideObject ? outSideObject->getIOR() : 1;
			float sin_phi = current_n / next_n * sin(theta);

			outRay.intersectObj = NULL;
			outRay.radiance = vec3f(1, 1, 1);
			outRay.directionProb = 1;
			outRay.contactObj = (AbstractObject*)this;
			outRay.contactTriangleID = inRay.intersectTriangleID;

			if(sin_phi > 1){
				outRay.direction = reflDir;
				outRay.insideObj = inRay.insideObj;
				outRay.directionProb = 1;
				outRay.isDeltaDirection = true;
				outRay.photonType = Ray::NOUSE;
			}
			else{
				float phi = asin(sin_phi);
				if(theta > PI/2)	phi = PI - phi;
				vec3f axis = normal.cross(inRay.direction);
				axis.normalize();
				outRay.direction = vec3f(RotateMatrix(axis, phi) * vec4f(normal, 0));
				outRay.direction.normalize();

				float cos_theta = abs(cos(theta));
				float cos_phi = abs(cos(phi));
				float esr = powf(abs(current_n*cos_theta-next_n*cos_phi)/(current_n*cos_theta+next_n*cos_phi),2);
				float epr = powf(abs(next_n*cos_theta-current_n*cos_phi)/(next_n*cos_theta+current_n*cos_phi),2);
				float er = (esr+epr)/2;
				float p = er;

				if(rng->genFloat() < p)
				{
					outRay.direction = reflDir;
					outRay.radiance *= er / outRay.cosineTerm();
					outRay.directionProb = p;
					outRay.insideObj = inRay.insideObj;
					outRay.isDeltaDirection = true;
					outRay.photonType = Ray::NOUSE;
				}
				else
				{
					outRay.radiance *= (1-er) / outRay.cosineTerm();
					outRay.directionProb = 1-p;
					outRay.contactObj = outRay.insideObj = (AbstractObject*)this;
					outRay.isDeltaDirection = true;
					outRay.photonType = Ray::HITVOL;
				}
				outRay.direction.normalize();
			}
			return outRay;
		}

		float p_medium, P_surface, sampleDist;

		bool samplingState = sampleDistance(inRay, sampleDist, p_medium, P_surface);

		bool out_of_vol = samplingState == false;//sampleDist >= inRay.intersectDist;

		// CASE2: Be in volume.
		if(be_in_vol && !out_of_vol){
			outRay.origin = inRay.origin + inRay.direction * sampleDist;
			outRay.radiance = bsdf->sampleBSDF(inRay.direction, outRay.direction, vec3f(), *rng, &outRay.directionProb);
			outRay.insideObj = (AbstractObject*)this;
			outRay.contactTriangleID = inRay.intersectTriangleID;
			float albedo = isSubsurface ? getAlbedo(outRay.origin) : getAlbedo();
			float rander = rng->genFloat();	
			if(rander < albedo){
				outRay.contactObj = NULL;
				outRay.directionProb *= albedo;
				outRay.originProb = p_medium;//  pMedium(inRay, sampleDist);// 
				outRay.isDeltaDirection = false;
				outRay.radiance *= isSubsurface ? lookUpSubSurfaceVolumeData(outRay.origin, SCATTERING) : scatteringCoeff * lookUpDensity(outRay.origin);
				outRay.photonType = Ray::INVOL;
			}
			else{
				// terminate
				outRay.direction = vec3f(0, 0, 0); 
				outRay.radiance = vec3f(0, 0, 0);  
				outRay.directionProb = 1; 
				outRay.originProb = p_medium;//pMedium(inRay, sampleDist);//
				outRay.insideObj = NULL;
				outRay.contactObj = NULL;
				outRay.isDeltaDirection = false;
				outRay.photonType = Ray::INVOL; //Ray::NOUSE;//
			}
			return outRay;
		}

		// CASE3: Go out of volume.
		if(be_in_vol && out_of_vol){
			outRay = inRay;
			outRay.direction = inRay.direction;
			outRay.origin = inRay.origin + inRay.intersectDist * inRay.direction;
			outRay.contactObj = inRay.intersectObj;
			outRay.contactTriangleID = inRay.intersectTriangleID;
			outRay.insideObj = (AbstractObject*)this;
			outRay.directionProb = 1; 
			outRay.radiance = vec3f(1,1,1);
			bool going_out = (inRay.intersectObj == this);
			if(going_out){
				vec3f normal = inRay.intersectObj->getWorldNormal(inRay.intersectTriangleID, outRay.origin);
				vec3f reflDir = -normal.dot(inRay.direction)*normal*2 + inRay.direction;
				reflDir.normalize();
				float theta = acos(inRay.direction.dot(normal));

				AbstractObject* currentInsideObject = (AbstractObject*)this;
				AbstractObject* outSideObject = scene->findInsideObject(outRay, (AbstractObject*)this);

				float current_n = currentInsideObject ? currentInsideObject->getIOR() : 1;
				float next_n = outSideObject ? outSideObject->getIOR() : 1;
				float sin_phi = current_n / next_n * sin(theta);

				outRay.intersectObj = NULL;
				if(sin_phi > 1){
					outRay.direction = reflDir;
					outRay.insideObj = inRay.insideObj;
					outRay.contactObj = (AbstractObject*)this;
					outRay.originProb = P_surface;// PSurface(inRay, inRay.intersectDist);//
					outRay.photonType = Ray::NOUSE;
					outRay.isDeltaDirection = true;
				}
				else{
					float phi = asin(sin_phi);
					if(theta > PI/2)	phi = PI - phi;
					vec3f axis = normal.cross(inRay.direction);
					axis.normalize();
					outRay.direction = vec3f(RotateMatrix(axis, phi) * vec4f(normal, 0));
					outRay.direction.normalize();

					float cos_theta = abs(cos(theta));
					float cos_phi = abs(cos(phi));
					float esr = powf(abs(current_n*cos_theta-next_n*cos_phi)/(current_n*cos_theta+next_n*cos_phi),2);
					float epr = powf(abs(next_n*cos_theta-current_n*cos_phi)/(next_n*cos_theta+current_n*cos_phi),2);
					float er = (esr+epr)/2;
					float p = er;

					if(rng->genFloat() < p)
					{
						outRay.direction = reflDir;
						outRay.radiance *= er / outRay.cosineTerm();
						outRay.directionProb = p;
						outRay.originProb = P_surface;// PSurface(inRay, inRay.intersectDist);//
						outRay.insideObj = inRay.insideObj;
						outRay.isDeltaDirection = true;
						outRay.photonType = Ray::NOUSE;
					}
					else
					{
						outRay.radiance *= (1-er) / outRay.cosineTerm();
						outRay.directionProb = (1-p);
						outRay.originProb = P_surface;//PSurface(inRay, inRay.intersectDist);// 
						outRay.insideObj = outSideObject;
						outRay.isDeltaDirection = true;
						outRay.photonType = Ray::NOUSE;
					}
					outRay.direction.normalize();
				}	
			}
			else{
				outRay.contactObj = NULL;
				outRay.intersectDist = 0;
				outRay = inRay.intersectObj->scatter(outRay);
				outRay.originProb *= P_surface;//PSurface(inRay, inRay.intersectDist);// 
				outRay.photonType = inRay.intersectObj->isVolume() ? Ray::NOUSE : Ray::OUTVOL;
			}
			return outRay;
		}
		return outRay;
	}
예제 #5
0
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;
}
예제 #6
0
void DXImage::Draw(int x,int y,int gx,int gy,int gwid,int ghei,float zoom,float rot,Color4f color){

	if(lpd3ddev==NULL)return;
	if(myTexture==NULL)return;

	float alpha=255;
	float ux,uy,uw,uh;

	int wid=desc.Width;
	int hei=desc.Height;

	if(gwid==0)gwid=wid-gx;
	if(ghei==0)ghei=hei-gy;
		
/*	ux=(float)(gx+0.5f)/wid;
	uy=(float)(gy+0.5f)/hei;
	uw=(float)(gx+gwid+0.5f)/wid;
	uh=(float)(gy+ghei+0.5f)/hei;*/

	ux=((float)gx)/wid;
	uy=((float)gy)/hei;
	uw=((float)gx+gwid)/wid;
	uh=((float)gy+ghei)/hei;

	if(alpha>255)alpha=255;
	if(alpha<0)alpha=0;
	if(ux<0.0f)ux=0.0f;
	if(uy<0.0f)uy=0.0f;
	if(ux>1.0f)ux=1.0f;
	if(uy>1.0f)uy=1.0f;

	float xf,yf,wf,hf;

	xf=(float)x-wid/2;
	yf=(float)y-hei/2;
	wf=(float)x+wid/2;
	hf=(float)y+hei/2;

	DWORD c = D3DCOLOR_RGBA(
		(int)(color.red),
		(int)(color.green),
		(int)(color.blue),
		(int)(color.alpha));
	CUSTOMVERTEX vertex[4]={
		{xf	,yf	,0.0f	,1.0f	,c	,ux	,uy	},
		{wf	,yf	,0.0f	,1.0f	,c	,uw	,uy	},
		{wf	,hf	,0.0f	,1.0f	,c	,uw	,uh	},
		{xf	,hf	,0.0f	,1.0f	,c	,ux	,uh	}
	};

	if(rot!=0)RotateMatrix(vertex,rot);
	if(zoom!=1.0f)ZoomMatrix(vertex,zoom,zoom,zoom);

//	void *pData;
//	if(FAILED(pVertex->Lock(0, sizeof(CUSTOMVERTEX)*4, (void**)&pData, 0))){
//		return;
//	}
//	memcpy(pData,vertex, sizeof(CUSTOMVERTEX)*4);
//	pVertex->Unlock();

	lpd3ddev->SetTexture(0,myTexture);
	lpd3ddev->SetFVF(FVF_CUSTOM);
//	lpd3ddev->SetStreamSource(0, pVertex, 0, sizeof(CUSTOMVERTEX));
//	lpd3ddev->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);
	lpd3ddev->DrawPrimitiveUP(D3DPT_TRIANGLEFAN,2,(void*)vertex,sizeof(CUSTOMVERTEX));
}
예제 #7
0
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
}