예제 #1
0
void transform(int i, float rot_offset, float rot_speed, float size,
    float height, float radius, int wobble) {
    float angle = (glutGet(GLUT_ELAPSED_TIME) / 1000.0) * (180/M_PI);
    float RotationMatrixAnim[16];
    float ScaleMatrix[16];
    float InitialTransform[16];
    float scale = 0.001;
    float maxHeight = height + 0.5;
    float minHeight = height - 0.5;
    static int up[OBJECTS];
    static int time[OBJECTS];

    if(wobble == 1) {
        if(up[i] == 0)  height = maxHeight - (time[i]++) * scale;
        else            height = minHeight + (time[i]++) * scale;
        if(time[i] >= 1000) {
            up[i] = (up[i] == 0) ? 1 : 0;
            time[i] = 0;
        }
    }

    SetScaling(size, size, size, ScaleMatrix);

    SetRotationY(rot_speed*angle + rot_offset, RotationMatrixAnim);
    SetTranslation(radius, height, 0, InitialTransform);
    
    MultiplyMatrix(InitialTransform, ScaleMatrix, ModelMatrix[i]);
    MultiplyMatrix(RotationMatrixAnim, ModelMatrix[i], ModelMatrix[i]);
}
예제 #2
0
void keyboard2(unsigned char key, int x, int y) {
	float rotation[16];
	float translation[16];
    float camera_disp = -10.0;
    
    if ( key == 'w' || key == 'a' || key == 's' || key == 'd' || key == ' ') {
		SetTranslation(0.0, 0.0, camera_disp, translation);
		MultiplyMatrix(translation, ViewMatrix, ViewMatrix);
	} else {
		return;
	}
	
	switch(key) {
		case 'w' : SetRotationX(5, rotation); 	
			break;
		case 'a' : SetRotationY(5, rotation);
			break;
		case 's' : SetRotationX(-5, rotation);
			break;
		case 'd' : SetRotationY(-5, rotation);
			break;	
		case ' ' : SetTranslation(0.0, 0.0, camera_disp, ViewMatrix);
			return;	
		default : return;			
	}
	MultiplyMatrix(ViewMatrix, rotation, ViewMatrix);
	SetTranslation(0.0, 0.0,camera_disp * -1, translation);
	MultiplyMatrix(translation, ViewMatrix, ViewMatrix); 
}
예제 #3
0
void OnIdle()
{
    float angle = (glutGet(GLUT_ELAPSED_TIME) / 1000.0) * (180.0/M_PI); 
    float RotationMatrixAnim[16];

    /* Time dependent rotation */
    SetRotationY(angle, RotationMatrixAnim);

    /* Apply model rotation; finally move cube down */
    int i = 0;
    for(; i < OBJECTS; i++) {
	MultiplyMatrix(RotationMatrixAnim, InitialTransform, ModelMatrix[i]);
	MultiplyMatrix(TranslateDown, ModelMatrix[i], ModelMatrix[i]);
    }

    // transform karusell
    transform(0, 0.0, 0.3, 1.0, -2.5, 0.0, 0);
    transform(1, 0.0, 0.3, 1.0, -2.5, 0.0, 0);
    transform(2, 0.0, 0.3, 1.0, -2.5, 0.0, 0);

    // transform cube
    transform(3,   0.0, 0.3, 0.25, -1.0, 2.0, 1);
    transform(4,  90.0, 0.3, 0.5 , -1.0, 2.0, 1);
    transform(5, 180.0, 0.3, 0.25, -1.0, 2.0, 1);
    transform(6, 270.0, 0.3, 0.5 , -1.0, 2.0, 1);

    /* Request redrawing forof window content */  
    glutPostRedisplay();
}
예제 #4
0
void ColorRenderMatrixFunctions(void)
{

    MultiplyMatrix(modelViewMatrix, viewMatrix, modelWorldMatrix);
    //------------------------------------------------------------    
    LoadIdentity(tempMatrix_A);    
    MultiplyMatrix(tempMatrix_A, viewMatrix, viewRotateMatrix);    
    //---------------------------------------------------------------------    
    MultiplyMatrix(moveSet_x_modelWorldMatrix, moveSetMatrix, modelWorldMatrix);    
    //---------------------------------------------------------------------    
    LoadIdentity(tempMatrix_B);     
    MultiplyMatrix(tempMatrix_B,  shadowTextureMatrix, moveSet_x_modelWorldMatrix);     
    //---------------------------------------------------------------------    
    LoadIdentity(tempMatrix_C);     
    MultiplyMatrix(tempMatrix_C,  projectionMatrix, tempMatrix_A);       
    //---------------------------------------------------------------------    
    LoadIdentity(tempMatrix_D);     
    MultiplyMatrix(tempMatrix_D,  tempMatrix_C, moveSet_x_modelWorldMatrix);     
    
    //----------------------------------------------------------------------------------------------------------    
    LoadIdentity(shadowTextureMatrix);
    MultiplyMatrix(shadowTextureMatrix, shadowBiasMatrix, shadowProjectionMatrix);    
    MultiplyMatrix(shadowTextureMatrix, shadowTextureMatrix, shadowViewMatrix);    //_MULTIPLY THESE TO A TEMP MATRIX TO AVOID ROUND OFF ERRORS   //USES MORE CODE WITH INCREASED PRECISION 
    //----------------------------------------------------------------------------------------------------------
   
    LoadIdentity(tempMatrix_E);
    MultiplyMatrix(tempMatrix_E, viewRotateMatrix, modelRotationMatrix);
    //----------------------------------------------------------------------------------------------------------
    LoadIdentity(modelRotationINVmatrix);
    InvertMatrix(modelRotationINVmatrix, tempMatrix_E);
     //----------------------------------------------------------------------------------------------------------     
}
예제 #5
0
/** Rotates the current rendering position 
 *
 *	@param	angle	The angle by which to rotate
 *	@param	x			The x value (1 or 0)
 *	@param	y			The y value (1 or 0)
 *	@param	z			The z value (1 or 0)
 *
 *	This method specifies a type of Angle/Axis rotation, 
 *	you specify an angle to rotate, then give an axis 
 *	to rotate around, therefore x/y/z must be 1 or 0.
 *	Any other value will result in error
 */
void OGLGraphics::Rotate(float angle, float x, float y, float z)
{
	m_quat.LoadIdentity();
	m_quat.Rotate(angle,x,y,z);

	MultiplyMatrix(m_quat.GetMatrix());
}
예제 #6
0
/** Rotates the current rendering position 
 *
 *	@param	r	The Roll component
 *	@param	p	The pitch component
 *	@param	y	The Yaw component
 */
void OGLGraphics::Rotate(float r, float p, float y)
{
	m_quat.LoadIdentity();
	m_quat.Rotate(r,p,y);

	MultiplyMatrix(m_quat.GetMatrix());
}
예제 #7
0
	string TestDoubleMatrix()
	{
		double* a, * b, * x;
		a = GenerateMatrix(MatrixSize);
		b = GenerateMatrix(MatrixSize);
		x = MultiplyMatrix(a, b, MatrixSize, MatrixSize, MatrixSize);
		double result = x[MatrixSize / 2 * MatrixSize + MatrixSize / 2];
		delete[] a;
		delete[] b;
		delete[] x;
		return printstring("%f", result);
	}
예제 #8
0
void mouse(int button, int state, int x, int y){
	
	float rotation[16];
	float translation[16];
    float camera_disp = -10.0;
	if(button != 0) {return;} 
	
	/* mouse click*/
	if (state == 0) {
		mouse_x = x;
		mouse_y = y;
		return;
	/* mouse release*/	
	} else if ( state == 1) { 
		SetTranslation(0.0, 0.0, camera_disp, translation);
		MultiplyMatrix(translation, ViewMatrix, ViewMatrix);
		
		int diffX = mouse_x - x;
		int diffY = mouse_y - y;
		
		/* right to left or reverse*/
		if (fabs(diffX) > fabs(diffY)) {
			if(diffX < 0) {
				SetRotationY(-5, rotation);
			} else {
				SetRotationY(5, rotation);
			}
		} else {
			if (diffY < 0) {
				SetRotationX(-5, rotation);
			} else {
				 SetRotationX(5, rotation);
			 }
		}
	}
	MultiplyMatrix(ViewMatrix, rotation, ViewMatrix);
	SetTranslation(0.0, 0.0,camera_disp * -1, translation);
	MultiplyMatrix(translation, ViewMatrix, ViewMatrix); 
}
예제 #9
0
int calcDiffusionTensorAtPoint(floatMatrixType *Tensor, int x, int y, densityDataType *densityData, int scanningRange)
{
/*
	//Diagonal
	//Debug 
	(*Tensor).a11=0.0; 		(*Tensor).a12=1.0; 
	(*Tensor).a21=1.0;		(*Tensor).a22=0.0;
	return 0;
*/

	intVectorType point;
	point.x=x;
	point.y=y; 
	momentOfInertiaType Inertia;
	floatMatrixType BT, B, Diag, Help;//, Diffusion;
	float a, structure;
	evaluateDirectionAndStructure(&a, &structure ,&Inertia, *densityData, point, scanningRange);
	
	//BTransposed
	float magV1=FLOATVECNORM((Inertia).v1)+EPS;
	float magV2=FLOATVECNORM((Inertia).v2)+EPS;
	
	BT.a11=(Inertia).v1.x/magV1; BT.a12=(Inertia).v1.y/magV1; 
	BT.a21=(Inertia).v2.x/magV2; BT.a22=(Inertia).v2.y/magV2; 
	
	//Diagonal 
	Diag.a11=1.0; 		Diag.a12=0.0; 
	Diag.a21=0.0;		Diag.a22=EPS; 
	
	//B
	B.a11=BT.a11; B.a12=BT.a21; 
	B.a21=BT.a12; B.a22=BT.a22; 
		
	//Create Diffusion Matrix
	MultiplyMatrix(&Help, &Diag, &BT); 
	MultiplyMatrix(Tensor, &B, &Help); 
	return 0;
}
예제 #10
0
void ComputeInvModelMatrix(float* matrix, float* dest)
{
	
	float* invTranslation;
	float* invRotation;
	
	
	invTranslation[0] = 1 ;	invTranslation[4] = 0 ;	invTranslation[8] = 0 ;		invTranslation[12] = -matrix[12] ;
	invTranslation[1] = 0 ;	invTranslation[5] = 1 ;	invTranslation[9] = 0 ;		invTranslation[13] = -matrix[13] ;
	invTranslation[2] = 0 ;	invTranslation[6] = 0 ;	invTranslation[10] = 1 ;	invTranslation[14] = -matrix[14] ;
	invTranslation[3] = 0 ;	invTranslation[7] = 0 ;	invTranslation[11] = 0 ;	invTranslation[15] = 1 ;
	
	invRotation[0] = matrix[0] ;		invRotation[4] = matrix[1] ;		invRotation[8] = matrix[2] ;		invRotation[12] = 0 ;
	invRotation[1] = matrix[4] ;		invRotation[5] = matrix[5] ;		invRotation[9] = matrix[6] ;		invRotation[13] = 0 ;
	invRotation[2] = matrix[8] ;		invRotation[6] = matrix[9] ;		invRotation[10] = matrix[10] ;		invRotation[14] = 0 ;
	invRotation[3] = 0 ;				invRotation[7] = 0 ;				invRotation[11] = 0 ;				invRotation[15] = 1 ;
	
	
	MultiplyMatrix(invRotation, invTranslation, dest);
}
예제 #11
0
int main(int argc,char **argv){

	int i,j,k = 0;
	double MSE = 0,TrainingAccuracy;
	float **input,**weight,*biase,**tranpI,**tempH,**H;
	float **tranpw;
	float **train_set;
	float **T,**Y;
	float **out;
	
	train_set = (float **)calloc(DATASET,sizeof(float *));
	tranpI = (float **)calloc(INPUT_NEURONS,sizeof(float *));
	input = (float **)calloc(DATASET,sizeof(float *));        		/*datasize * INPUT_NEURONS*/
	weight = (float **)calloc(HIDDEN_NEURONS,sizeof(float *));		/*HIDDEN_NEURONS * INPUT_NEURONS*/
	biase = (float *)calloc(HIDDEN_NEURONS,sizeof(float)); 			/*HIDDEN_NEURONS*/
	tempH = (float **)calloc(HIDDEN_NEURONS,sizeof(float *)); 		/*HIDDEN_NEURONS * datasize*/
	tranpw = (float **)calloc(INPUT_NEURONS,sizeof(float *));
	H = (float **)calloc(DATASET,sizeof(float *));
	T = (float **)calloc(DATASET,sizeof(float *));
	Y = (float **)calloc(DATASET,sizeof(float *));
	out = (float **)calloc(HIDDEN_NEURONS,sizeof(float *));
	
	for(i=0;i<DATASET;i++){
		train_set[i] = (float *)calloc(NUMROWS,sizeof(float));
		input[i] = (float *)calloc(INPUT_NEURONS,sizeof(float));
		H[i] = (float *)calloc(HIDDEN_NEURONS,sizeof(float));
	}
	for(i=0;i<DATASET;i++)
	{
		T[i] = (float *)calloc(OUTPUT_NEURONS,sizeof(float));
		Y[i] = (float *)calloc(OUTPUT_NEURONS,sizeof(float));
	}
	for(i=0;i<INPUT_NEURONS;i++){
		tranpI[i] = (float *)calloc(DATASET,sizeof(float));
		tranpw[i] = (float *)calloc(HIDDEN_NEURONS,sizeof(float));
	}
	
	for(i=0;i<HIDDEN_NEURONS;i++)
	{
		weight[i] = (float *)calloc(INPUT_NEURONS,sizeof(float));
		tempH[i] = (float *)calloc(DATASET,sizeof(float));
		out[i] = (float *)calloc(OUTPUT_NEURONS,sizeof(float));
	}
	printf("begin to regression test...\n");
	printf("begin to random weight and biase...\n");
	/*得到随机的偏置和权重*/
	//RandomWeight(weight,HIDDEN_NEURONS,INPUT_NEURONS);
	//RandomBiase(biase,HIDDEN_NEURONS);
	if(LoadMatrix(tranpw,"../result/weight",INPUT_NEURONS,HIDDEN_NEURONS) == 0){
		printf("load input file error!!!\n");	
		return 0;
	}
	TranspositionMatrix(tranpw,weight,INPUT_NEURONS,HIDDEN_NEURONS);
	if(LoadMatrix_s(biase,"../result/bias",1,HIDDEN_NEURONS) == 0){
		printf("load input file error!!!\n");	
		return 0;
	}
	/*加载数据集到内存*/
	printf("begin to load input from the file...\n");
	if(LoadMatrix(train_set,"../sample/ppp",DATASET,NUMROWS) == 0){
		printf("load input file error!!!\n");	
		return 0;
	}
	
	/*将数据集划分成输入和输出*/
	for(i = 0;i < DATASET ;i++)
	{
		T[k++][0] = train_set[i][0];
		for(j = 1;j <= INPUT_NEURONS;j++)
		{
			input[i][j-1] = train_set[i][j];
		}
	}
	/*ELM*/
	printf("begin to compute...\n");
	
	TranspositionMatrix(input,tranpI,DATASET,INPUT_NEURONS);
	printf("begin to compute step 1...\n");
	MultiplyMatrix(weight,HIDDEN_NEURONS,INPUT_NEURONS, tranpI,INPUT_NEURONS,DATASET,tempH);
	printf("begin to compute setp 2...\n");
	AddMatrix_bais(tempH,biase,HIDDEN_NEURONS,DATASET);
	printf("begin to compute step 3...\n");
	SigmoidHandle(tempH,HIDDEN_NEURONS,DATASET);
	printf("begin to compute step 4...\n");
	TranspositionMatrix(tempH,H,HIDDEN_NEURONS,DATASET);
	printf("begin to load input from the file...\n");
	if(LoadMatrix(out,"../result/result",HIDDEN_NEURONS,OUTPUT_NEURONS) == 0){
		printf("load input file error!!!\n");	
		return 0;
	}
	//检测准确率
	MultiplyMatrix(H,DATASET,HIDDEN_NEURONS,out,HIDDEN_NEURONS,OUTPUT_NEURONS,Y);
	for(i = 0;i< DATASET;i++)
	{
		MSE += (Y[i][0] - T[i][0])*(Y[i][0] - T[i][0]);
	}
	TrainingAccuracy = sqrt(MSE/DATASET);
	
	printf("trainning accuracy :%f\n",TrainingAccuracy);
	
	printf("test complete...\n");
	//print(PIMatrix,DATASET,HIDDEN_NEURONS);
		
	return 0;
}
예제 #12
0
파일: elm.c 프로젝트: linuxfl/ELM_C_S
/**
     * Construct an ELM
     * @param
     * elm_type              - 0 for regression; 1 for (both binary and multi-classes) classification
     */
void ELMTrain(int elm_type)
{
	double starttime,endtime;
	double TrainingAccuracy;
	int i,j,k = 0;
	float **input,**weight,*biase,**tranpI,**tempH,**H;
	float **PIMatrix;
	float **train_set;
	float **T,**Y;
	float **out;
	
	train_set = (float **)calloc(DATASET,sizeof(float *));
	tranpI = (float **)calloc(INPUT_NEURONS,sizeof(float *));
	input = (float **)calloc(DATASET,sizeof(float *));        		/*datasize * INPUT_NEURONS*/
	weight = (float **)calloc(HIDDEN_NEURONS,sizeof(float *));		/*HIDDEN_NEURONS * INPUT_NEURONS*/
	biase = (float *)calloc(HIDDEN_NEURONS,sizeof(float)); 			/*HIDDEN_NEURONS*/
	tempH = (float **)calloc(HIDDEN_NEURONS,sizeof(float *)); 		/*HIDDEN_NEURONS * datasize*/
	PIMatrix = (float **)calloc(HIDDEN_NEURONS,sizeof(float *));
	H = (float **)calloc(DATASET,sizeof(float *));
	T = (float **)calloc(DATASET,sizeof(float *));
	Y = (float **)calloc(DATASET,sizeof(float *));
	out = (float **)calloc(HIDDEN_NEURONS,sizeof(float *));
	
	for(i=0;i<DATASET;i++){
		train_set[i] = (float *)calloc(NUMROWS,sizeof(float));
		input[i] = (float *)calloc(INPUT_NEURONS,sizeof(float));
		H[i] = (float *)calloc(HIDDEN_NEURONS,sizeof(float));
	}
	for(i=0;i<DATASET;i++)
	{
		T[i] = (float *)calloc(OUTPUT_NEURONS,sizeof(float));
		Y[i] = (float *)calloc(OUTPUT_NEURONS,sizeof(float));
	}
	for(i=0;i<INPUT_NEURONS;i++)
		tranpI[i] = (float *)calloc(DATASET,sizeof(float));
	
	for(i=0;i<HIDDEN_NEURONS;i++)
	{
		weight[i] = (float *)calloc(INPUT_NEURONS,sizeof(float));
		tempH[i] = (float *)calloc(DATASET,sizeof(float));
		out[i] = (float *)calloc(OUTPUT_NEURONS,sizeof(float));
		PIMatrix[i] = (float *)calloc(DATASET,sizeof(float));
	}
	
	printf("begin to random weight and biase...\n");
	/*得到随机的偏置和权重*/
	RandomWeight(weight,HIDDEN_NEURONS,INPUT_NEURONS);
	RandomBiase(biase,HIDDEN_NEURONS);
	SaveMatrix(weight,"./result/weight",HIDDEN_NEURONS,INPUT_NEURONS);
	SaveMatrix_s(biase,"./result/biase",1,HIDDEN_NEURONS);
	/*加载数据集到内存*/
	printf("begin to load input from the file...\n");
	if(LoadMatrix(train_set,"./sample/frieman",DATASET,NUMROWS) == 0){
		printf("load input file error!!!\n");	
		return;
	}
	
	/*将数据集划分成输入和输出*/
	if(elm_type == 0){ 	//regression
		/*
			the first column is the output of the dataset
		*/
		for(i = 0;i < DATASET ;i++)
		{
			T[k++][0] = train_set[i][0];
			for(j = 1;j <= INPUT_NEURONS;j++)
			{
				input[i][j-1] = train_set[i][j];
			}
		}
	}else{				//classification
		/*
			the last column is the lable of class of the dataset
		*/
		InitMatrix(T,DATASET,OUTPUT_NEURONS,-1);
		for(i = 0;i < DATASET ;i++)
		{
			//get the last column
			T[k++][0] = train_set[i][0] - 1;//class label starts from 0,so minus one
			for(j = 1;j <= INPUT_NEURONS;j++)
			{
				input[i][j-1] = train_set[i][j];
			}
		}
		for(i = 0;i < DATASET ;i++)
		{
			for(j = 0;j < OUTPUT_NEURONS;j++)
			{
				k =  T[i][0];
				if(k < OUTPUT_NEURONS && k >= 0){
					T[i][k] = 1;
				}
				if(k != 0){
					T[i][0] = -1;
				}
			}
		}
	}
	/*ELM*/
	printf("begin to compute...\n");
	starttime = omp_get_wtime();	
	TranspositionMatrix(input,tranpI,DATASET,INPUT_NEURONS);
	printf("begin to compute step 1...\n");
	MultiplyMatrix(weight,HIDDEN_NEURONS,INPUT_NEURONS, tranpI,INPUT_NEURONS,DATASET,tempH);
	printf("begin to compute setp 2...\n");
	AddMatrix_bais(tempH,biase,HIDDEN_NEURONS,DATASET);
	printf("begin to compute step 3...\n");
	SigmoidHandle(tempH,HIDDEN_NEURONS,DATASET);
	printf("begin to compute step 4...\n");
	TranspositionMatrix(tempH,H,HIDDEN_NEURONS,DATASET);
	PseudoInverseMatrix(H,DATASET,HIDDEN_NEURONS,PIMatrix);
	MultiplyMatrix(PIMatrix,HIDDEN_NEURONS,DATASET,T,DATASET,OUTPUT_NEURONS,out);

	//SaveMatrix(H,"./result/H",DATASET,HIDDEN_NEURONS);
	//SaveMatrix(PIMatrix,"./result/PIMatrix",HIDDEN_NEURONS,DATASET);
	printf("begin to compute step 5...\n");
	endtime = omp_get_wtime();
	//保存输出权值
	SaveMatrix(out,"./result/result",HIDDEN_NEURONS,OUTPUT_NEURONS);
	MultiplyMatrix(H,DATASET,HIDDEN_NEURONS,out,HIDDEN_NEURONS,OUTPUT_NEURONS,Y);
	printf("use time :%f\n",endtime - starttime);
	printf("train complete...\n");

	if(elm_type == 0){
	//检测准确率
		double MSE = 0;
		for(i = 0;i< DATASET;i++)
		{
			MSE += (Y[i][0] - T[i][0])*(Y[i][0] - T[i][0]);
		}
		TrainingAccuracy = sqrt(MSE/DATASET);
		printf("Regression/trainning accuracy :%f\n",TrainingAccuracy);
	}else{
		float MissClassificationRate_Training=0;
		double maxtag1,maxtag2;
		int tag1 = 0,tag2 = 0;
		for (i = 0; i < DATASET; i++) {
				maxtag1 = Y[i][0];
				tag1 = 0;
				maxtag2 = T[i][0];
				tag2 = 0;
		    	for (j = 1; j < OUTPUT_NEURONS; j++) {
					if(Y[i][j] > maxtag1){
						maxtag1 = Y[i][j];
						tag1 = j;
					}
					if(T[i][j] > maxtag2){
						maxtag2 = T[i][j];
						tag2 = j;
					}
				}
		    	if(tag1 != tag2)
		    		MissClassificationRate_Training ++;
			}
		    TrainingAccuracy = 1 - MissClassificationRate_Training*1.0f/DATASET;
			printf("Classification/training accuracy :%f\n",TrainingAccuracy);
	}
	FreeMatrix(train_set,DATASET);
	FreeMatrix(tranpI,INPUT_NEURONS);
	FreeMatrix(input,DATASET);
	FreeMatrix(weight,HIDDEN_NEURONS);
	free(biase);
	FreeMatrix(tempH,HIDDEN_NEURONS);
	FreeMatrix(PIMatrix,HIDDEN_NEURONS);
	FreeMatrix(H,DATASET);
	FreeMatrix(T,DATASET);
	FreeMatrix(Y,DATASET);
	FreeMatrix(out,HIDDEN_NEURONS);
}
예제 #13
0
void Colours_YUV2RGB(double y, double u, double v, double *r, double *g, double *b)
{
	MultiplyMatrix(y, u, v, r, g, b, YUV2RGB_matrix);
}
예제 #14
0
void Colours_RGB2YUV(double r, double g, double b, double *y, double *u, double *v)
{
	MultiplyMatrix(r, g, b, y, u, v, RGB2YUV_matrix);
}
예제 #15
0
float divDGradU(intVectorType point ,densityDataType *densityData, int scanningRange)
{
	momentOfInertiaType Inertia;
	floatMatrixType BT, B, Diag, Help, Diffusion;
	float a, structure;
	//evaluateDirectionAndStructure(&a, &structure ,&Inertia, *densityData, point, scanningRange);
	
	/*
	(Inertia).v1.x=0.0;
	(Inertia).v1.y=1.0;
	(Inertia).v2.x=1.0;
	(Inertia).v2.y=0.0;
	*/
	
	//BTransposed
	float magV1=FLOATVECNORM((Inertia).v1)+EPS;
	float magV2=FLOATVECNORM((Inertia).v2)+EPS;
	
	/*
	printf("magV1=%f\n",magV1);
	printf("magV2=%f\n",magV2);
*/
	
	BT.a11=(Inertia).v1.x/magV1; BT.a12=(Inertia).v1.y/magV1; 
	BT.a21=(Inertia).v2.x/magV2; BT.a22=(Inertia).v2.y/magV2; 
	
	//Diagonal 
	Diag.a11=1.0; 		Diag.a12=0.0; 
	Diag.a21=0.0;		Diag.a22=1.0; 
	
	//B
	B.a11=(Inertia).v1.x/magV1; B.a12=(Inertia).v2.x/magV2; 
	B.a21=(Inertia).v1.y/magV1; B.a22=(Inertia).v2.y/magV2; 
	
	/*
	printf("BT:\n");
	SHOWMATRIX(BT);
	printf("Diag:\n");
	SHOWMATRIX(Diag); 
	*/
	
	//Create Diffusion Matrix
	MultiplyMatrix(&Help, &Diag, &BT); 
	
	/*
	printf("DiagBT:\n");
	SHOWMATRIX(Help); 
	*/
	/*
	printf("B:\n");
	SHOWMATRIX(B);
	 */
	 
	MultiplyMatrix(&Diffusion, &B, &Help); 
	
	
	//printf("Diffusion\n");
	//SHOWMATRIX(Diffusion); 
	

	return Divergence(point.x , point.y, &Diag , densityData);
	//return Divergence(point.x , point.y, &Diffusion , densityData);
};
예제 #16
0
//回归
void ELMTrain()
{
	double starttime,endtime;

	int i,j,k = 0;
	double MSE = 0,TrainingAccuracy;
	float **input,**weight,*biase,**tranpI,**tempH,**H;
	float **PIMatrix;
	float **train_set;
	float **T,**Y;
	float **out;
	
	train_set = (float **)calloc(DATASET,sizeof(float *));
	tranpI = (float **)calloc(INPUT_NEURONS,sizeof(float *));
	input = (float **)calloc(DATASET,sizeof(float *));        		/*datasize * INPUT_NEURONS*/
	weight = (float **)calloc(HIDDEN_NEURONS,sizeof(float *));		/*HIDDEN_NEURONS * INPUT_NEURONS*/
	biase = (float *)calloc(HIDDEN_NEURONS,sizeof(float)); 			/*HIDDEN_NEURONS*/
	tempH = (float **)calloc(HIDDEN_NEURONS,sizeof(float *)); 		/*HIDDEN_NEURONS * datasize*/
	PIMatrix = (float **)calloc(HIDDEN_NEURONS,sizeof(float *));
	H = (float **)calloc(DATASET,sizeof(float *));
	T = (float **)calloc(DATASET,sizeof(float *));
	Y = (float **)calloc(DATASET,sizeof(float *));
	out = (float **)calloc(HIDDEN_NEURONS,sizeof(float *));
	
	for(i=0;i<DATASET;i++){
		train_set[i] = (float *)calloc(NUMROWS,sizeof(float));
		input[i] = (float *)calloc(INPUT_NEURONS,sizeof(float));
		H[i] = (float *)calloc(HIDDEN_NEURONS,sizeof(float));
	}
	for(i=0;i<DATASET;i++)
	{
		T[i] = (float *)calloc(OUTPUT_NEURONS,sizeof(float));
		Y[i] = (float *)calloc(OUTPUT_NEURONS,sizeof(float));
	}
	for(i=0;i<INPUT_NEURONS;i++)
		tranpI[i] = (float *)calloc(DATASET,sizeof(float));
	
	for(i=0;i<HIDDEN_NEURONS;i++)
	{
		weight[i] = (float *)calloc(INPUT_NEURONS,sizeof(float));
		tempH[i] = (float *)calloc(DATASET,sizeof(float));
		out[i] = (float *)calloc(OUTPUT_NEURONS,sizeof(float));
		PIMatrix[i] = (float *)calloc(DATASET,sizeof(float));
	}
	
	printf("begin to random weight and biase...\n");
	/*得到随机的偏置和权重*/
	RandomWeight(weight,HIDDEN_NEURONS,INPUT_NEURONS);
	RandomBiase(biase,HIDDEN_NEURONS);
	
	/*加载数据集到内存*/
	printf("begin to load input from the file...\n");
	if(LoadMatrix(train_set,"../TrainingDataSet/covtype",DATASET,NUMROWS,1) == 0){
		printf("load input file error!!!\n");	
		return;
	}
	
	/*将数据集划分成输入和输出*/
	for(i = 0;i < DATASET ;i++)
	{
		T[k++][0] = train_set[i][0];
		for(j = 1;j <= INPUT_NEURONS;j++)
		{
			input[i][j-1] = train_set[i][j];
		}
	}
	SaveMatrix(input,"./result/input",DATASET,INPUT_NEURONS);
	/*ELM*/
	printf("begin to compute...\n");
	starttime = omp_get_wtime();	
	TranspositionMatrix(input,tranpI,DATASET,INPUT_NEURONS);
	printf("begin to compute step 1...\n");
	MultiplyMatrix(weight,HIDDEN_NEURONS,INPUT_NEURONS, tranpI,INPUT_NEURONS,DATASET,tempH);
	printf("begin to compute setp 2...\n");
	AddMatrix_bais(tempH,biase,HIDDEN_NEURONS,DATASET);
	printf("begin to compute step 3...\n");
	SigmoidHandle(tempH,HIDDEN_NEURONS,DATASET);
	printf("begin to compute step 4...\n");
	TranspositionMatrix(tempH,H,HIDDEN_NEURONS,DATASET);
	PseudoInverseMatrix(H,DATASET,HIDDEN_NEURONS,PIMatrix);
	MultiplyMatrix(PIMatrix,HIDDEN_NEURONS,DATASET,T,DATASET,OUTPUT_NEURONS,out);

	//SaveMatrix(H,"./result/H",DATASET,HIDDEN_NEURONS);
	//SaveMatrix(PIMatrix,"./result/PIMatrix",HIDDEN_NEURONS,DATASET);

	
	printf("begin to compute step 5...\n");
	endtime = omp_get_wtime();
	//保存输出权值
	SaveMatrix(out,"./result/result",HIDDEN_NEURONS,OUTPUT_NEURONS);
	
	//检测准确率
	MultiplyMatrix(H,DATASET,HIDDEN_NEURONS,out,HIDDEN_NEURONS,OUTPUT_NEURONS,Y);
	for(i = 0;i< DATASET;i++)
	{
		MSE += (Y[i][0] - T[i][0])*(Y[i][0] - T[i][0]);
	}
	SaveMatrix(T,"./result/t",DATASET,OUTPUT_NEURONS);
	SaveMatrix(Y,"./result/y",DATASET,OUTPUT_NEURONS);
	TrainingAccuracy = sqrt(MSE/DATASET);
	
	printf("use time :%f\n",endtime - starttime);
	printf("trainning accuracy :%f\n",TrainingAccuracy);
	
	printf("train complete...\n");
	//print(PIMatrix,DATASET,HIDDEN_NEURONS);
}
예제 #17
0
파일: main.cpp 프로젝트: jg-maon/kadai
	bool Run()
	{
		int result = ::DxLib::SetGraphMode(
			static_cast<int>(m_width),
			static_cast<int>(m_height),
			32);

		::DxLib::ChangeWindowMode(TRUE);	// ウインドウモードに変更
		if (::DxLib::DxLib_Init() == -1)
		{
			MessageBox(nullptr, "Initializing error", "Error!", MB_ICONERROR | MB_TASKMODAL | MB_TOPMOST);
			return false;
		}


		// 描画先画面を裏画面にセット
		SetDrawScreen(DX_SCREEN_BACK);


		float nearZ = 1.f, farZ = 100.f;

		SetCameraNearFar(nearZ, farZ);

		VECTOR3 cameraPos(0.f, 0.5f, -10.f);
		VECTOR3 targetPos(0.f, 0.f, 0.f);

		DxLib::SetCameraPositionAndTarget_UpVecY(cameraPos, targetPos);

		Cube cube(1.f);
		int keys[] = { KEY_INPUT_UP, KEY_INPUT_DOWN, KEY_INPUT_LEFT, KEY_INPUT_RIGHT };
		float val[] = { +0.1f, -0.1f, -0.1f, +0.1f };
		float* pos[][2] = { 
			{ &cameraPos.y, &targetPos.y },
			{ &cameraPos.y, &targetPos.y },
			{ &cameraPos.x, &targetPos.x },
			{ &cameraPos.x, &targetPos.x },
		};
		while (_LoopProc() && CheckHitKey(KEY_INPUT_ESCAPE) == 0)
		{
			const std::vector<Face>& faces = cube.GetFaces();
			for (const Face& face : faces)
			{
				int vertexNum = static_cast<int>(face.GetVertexNum());
				
				for (int i = 0; i < vertexNum - 1; ++i)
				{
					int j = (i + 1) % vertexNum;

					// 頂点の取得
					VECTOR4 from(face.GetVertexAt(i));
					VECTOR4 to(face.GetVertexAt(j));

					VECTOR3 from3d(from);
					VECTOR3 to3d(to);


					//------------------------------
					// 座標変換
					//------------------------------
					float dist = targetPos.z - cameraPos.z;
										
					MATRIX matT;
					CreateTranslationMatrix(matT, from.x, from.y, from.z);

					MATRIX matP;
					CreatePerspectiveLH(matP, m_width, m_height, nearZ, farZ);

					MATRIX mat;
					MultiplyMatrix(mat, matT, matP);
					
					Vector4Transform(from, from, mat);
					
					CreateTranslationMatrix(matT, to.x, to.y, to.z);
					MultiplyMatrix(mat, matT, matP);

					Vector4Transform(to, to, mat);

					/*
					D3DXMATRIX * D3DXMatrixPerspectiveLH(
						D3DXMATRIX * pOut,
						FLOAT w,	近くのビュー プレーンでのビュー ボリュームの幅
						FLOAT h,	近くのビュー プレーンでのビュー ボリュームの高さ
						FLOAT zn,	近くのビュー プレーンの z 値
						FLOAT zf	遠くのビュー プレーンの z 値
					);

					 2*zn/w  0       0              0 
					 0       2*zn/h  0              0 
					 0       0       zf/(zf-zn)     1 
					 0       0       zn*zf/(zn-zf)  0


					 D3DXMATRIX * D3DXMatrixPerspectiveRH(
						 D3DXMATRIX * pOut,
						 FLOAT w,	 FLOAT h,
						 FLOAT zn,	 FLOAT zf);
					 2*zn/w  0       0              0
					 0       2*zn/h  0              0
					 0       0       zf/(zn-zf)    -1
					 0       0       zn*zf/(zn-zf)  0
					*/
					
					
					/*
					D3DXMATRIX* D3DXMatrixPerspectiveFovLH(
						D3DXMATRIX * pOut,
						FLOAT fovy,		y 方向の視野角 (ラジアン単位)
						FLOAT Aspect,	アスペクト比 (ビュー空間の幅を高さで除算して定義(width/height))
						FLOAT zn,		近くのビュー プレーンの z 値
						FLOAT zf		遠くのビュー プレーンの z 値
					);
					where: 
					yScale = cot(fovY/2)
					xScale = yScale / aspect ratio

					xScale     0          0               0
					0        yScale       0               0
					0          0       zf/(zf-zn)         1
					0          0       -zn*zf/(zf-zn)     0
					
					D3DXMATRIX * D3DXMatrixPerspectiveFovRH(
						  D3DXMATRIX * pOut,
						  FLOAT fovy, FLOAT Aspect,
						  FLOAT zn,	  FLOAT zf);
					xScale     0          0              0
					0        yScale       0              0
					0        0        zf/(zn-zf)        -1
					0        0        zn*zf/(zn-zf)      0
					
					*/

					/*
					D3DXMATRIX * D3DXMatrixLookAtLH(
						D3DXMATRIX * pOut,			処理の結果を表す D3DXMATRIX 構造体へのポインター
						CONST D3DXVECTOR3 * pEye,	視点を定義する D3DXVECTOR3 構造体へのポインターです。この値は変換に使用されます。
						CONST D3DXVECTOR3 * pAt,	カメラの注視対象を定義する D3DXVECTOR3 構造体へのポインターです。
						CONST D3DXVECTOR3 * pUp		現在のワールド座標における上方向を定義する D3DXVECTOR3 構造体へのポインター。この構造体の値は通常 [0, 1, 0] です。
					);

					zaxis = normal(At - Eye)
					xaxis = normal(cross(Up, zaxis))
					yaxis = cross(zaxis, xaxis)
					xaxis.x           yaxis.x           zaxis.x          0
					xaxis.y           yaxis.y           zaxis.y          0
					xaxis.z           yaxis.z           zaxis.z          0
					-dot(xaxis, eye)  -dot(yaxis, eye)  -dot(zaxis, eye)  l

					D3DXMATRIX * D3DXMatrixLookAtRH(
						D3DXMATRIX * pOut,
						CONST D3DXVECTOR3 * pEye,
						CONST D3DXVECTOR3 * pAt,
						CONST D3DXVECTOR3 * pUp
					);
					zaxis = normal(Eye - At)
					xaxis = normal(cross(Up, zaxis))
					yaxis = cross(zaxis, xaxis)
					xaxis.x           yaxis.x           zaxis.x          0
					xaxis.y           yaxis.y           zaxis.y          0
					xaxis.z           yaxis.z           zaxis.z          0
					-dot(xaxis, eye)  -dot(yaxis, eye)  -dot(zaxis, eye)  l

					*/


					
					
					//------------------------------
					// 描画
					DxLib::DrawLine(from.x, from.y,
									to.x, to.y,
									GetColor(250, 0, 0));

					printf("from(%.1f, %.1f, %.1f) ", from.x, from.y, from.z);
					printf("to(%.1f, %.1f, %.1f)\n", to.x, to.y, to.z);

					DxLib::DrawLine3D(from3d, to3d, -1);
				}

			}

			{
				int i = 0;
				for (int key : keys)
				{
					if (CheckHitKey(key) != 0)
					{
						*pos[i][0] += val[i];
						*pos[i][1] += val[i];
					}
					++i;
				}
				DxLib::SetCameraPositionAndTarget_UpVecY(cameraPos, targetPos);
				DrawExtendFormatString(0, 0, 0.75, 0.75, -1, "camera(%.1f, %.1f, %.1f) target(%.1f, %.1f, %.1f)", cameraPos.x, cameraPos.y, cameraPos.z, targetPos.x, targetPos.y, targetPos.z);
			}
		}


		return true;
	}
예제 #18
0
파일: Interaction.c 프로젝트: matGs/opengl
void Keyboard(unsigned char key, int x, int y){
  switch( key ) {				                      // NEW: Manual camera mode: allways on -> use WASDRC to translate cam position and IJKL for cam rotation
    case 'w' :      
	camera_disp += 0.1f;
	break;
    case 's' :
	camera_disp -= 0.1f;
	break;
    case 'd' :
	xx -= 0.1;
	break;
    case 'a' :
	xx += 0.1;
	break;
    case 'i' :      
	angle2 -= 0.3;
	break;
    case 'k' :
        angle2 += 0.3;
	break;
    case 'l' :
	angle3 += 0.3;
	break;
    case 'j' :
        angle3 -= 0.3;
	break;      
    case 'r' :      
	yy -= 0.1f;
	break;
    case 'c' :
	yy += 0.1f;
        break;
    case 'm' :	// set automatic camera mode and reset camera position an rotation (angle)
	anim_cam = GL_TRUE;
        camera_disp = -10.0;
        angle=0;
        angle1=0;
        angle2=0;
        angle3=0;
        xx=0;
        yy=0;
        SetTranslation(xx, yy, camera_disp, ViewMatrix);  // camera_disp == z coordinate of camera
	return;
	break;
    case 'n' :	// unset automatic camera mode and reset camera position an rotation (angle)
	anim_cam = GL_FALSE;
        camera_disp = -10.0;
        angle=0;
        angle1=0;
        angle2=0;
        angle3=0;
        xx=0;
        yy=0;
        SetTranslation(xx, yy, camera_disp, ViewMatrix);  // camera_disp == z coordinate of camera
	return;
	break;
  }
  SetIdentityMatrix(RotationMatrixCam);
  SetIdentityMatrix(RotationMatrixCam2);
  SetIdentityMatrix(RotationMatrixCam3);
  SetIdentityMatrix(TranslationMatrixCam);
  
  // set camera movement
  SetTranslation(xx, yy, camera_disp, TranslationMatrixCam);
  SetRotationX(angle2, RotationMatrixCam);                            
  SetRotationY(angle3, RotationMatrixCam2);                        
  
  // apply movement to viewMatrix (camera matrix)
  MultiplyMatrix(RotationMatrixCam, RotationMatrixCam2, RotationMatrixCam3);
  MultiplyMatrix(RotationMatrixCam3, TranslationMatrixCam, ViewMatrix);
  glutPostRedisplay();
}
예제 #19
0
파일: Interaction.c 프로젝트: matGs/opengl
void OnIdle(){
    /* Determine delta time between two frames to ensure constant animation */
    int newTime = glutGet(GLUT_ELAPSED_TIME);
    int delta = newTime - oldTime;
    oldTime = newTime;
    SetIdentityMatrix(IdentityMatrixAnim);
    SetIdentityMatrix(TranslationMatrixAnim);

    /* automatic camera mode: press 'm' to start, and 'n' to reset */			
    if(anim_cam){
	if(camera_disp > -50){             // zoom out of world (until camera position on z == -50) and rotate on x axis
	    camera_disp -= 0.1;
        }
        
        if(camMov2 < 500){        
            angle1 += 0.1;
        }
        else if(camMov2 < 800){
            angle1 -= 0.1;
        }
        else{
            camMov2 = 200;
        }
        ++camMov2;
            
        
        if(camMov1 < 10 && camMov1 > -1){               // move camera to left and right with some pause of movement
          // Pause movement
        }
        else if(camMov1 < 80){
           angle -= 0.1;
        }
        else if(camMov1 < 100){
           // Pause movement
        }
        else if(camMov1 < 240){
           angle += 0.1;
        }
        else if(camMov1 < 250){
           // Pause movement
        }
        else if(camMov1 < 320){
           angle -= 0.1;
        }
        else{
         //  Pause for some time
            if(camMov1 > 1000){
                camMov1 = 0;
            }
        }
        ++camMov1;
            
        SetIdentityMatrix(RotationMatrixCam);
        SetIdentityMatrix(RotationMatrixCam2);
        
	SetTranslation(0.0, 0.0, camera_disp, TranslationMatrixAnim);      // translate cameras z position
              
	SetRotationY(angle, RotationMatrixCam);                            // set rotation of camera
	SetRotationX(angle1, RotationMatrixCam2);
        MultiplyMatrix(RotationMatrixCam, TranslationMatrixAnim, RotationMatrixCam3);   
        MultiplyMatrix(RotationMatrixCam3, RotationMatrixCam2, ViewMatrix);
    }

    
    /* If animation is set to true, set new rotation angles */
    if(anim){
        /* Increment rotation angles and update matrix */
        if(axis == YaxisStop){
	     // angleY = 0;
	      SetRotationY(angleY, RotationMatrixAnimY);  
	      
	      
	}
	else if(axis == Yaxis){
	    angleY = fmod(angleY + delta/20.0, 360.0); 
	    SetRotationY(angleY, RotationMatrixAnimY);  
	}  
    }
    else {	/* else: do not apply rotation below */
      return;
    }

    
    // Set Translation for Center-Imported Objects (ball_01, ball_02 and ring)                  // NEW
    SetIdentityMatrix(IdentityMatrixAnim);
    SetIdentityMatrix(TranslationMatrixAnim);
    SetIdentityMatrix(TranslationMatrixAnim2);
    SetIdentityMatrix(TranslationMatrixAnim3);
    SetIdentityMatrix(TranslationMatrixAnim4);
    SetIdentityMatrix(TranslationMatrixAnim5);
    
    SetTranslation(-1.8, 1, -1.8, TranslationMatrixAnim);               // ball_01          -1,8/1,8/1
    SetTranslation(1.8, 1, 1.8, TranslationMatrixAnim3);                // ball_02           1,8/-1,8/1
    SetTranslation(-0.96, -1.73403, -0.66, TranslationMatrixAnim5);     // elliptic_ring    -0,96/0,66/-1,73403
    
    /* Rotation of models */
    int k;		
    for(k=1; k<13; ++k){         // do not rotate top ring (index 0) and fixed background structures (index 13 and 14)
       
      // Set main rotaiton
      /* Update of transformation matrices 
       * Note order of transformations and rotation of reference axes */
      SetIdentityMatrix(IdentityMatrixAnim);
      MultiplyMatrix(IdentityMatrixAnim, RotationMatrixAnimY, RotationMatrixAnim);
      
      
      // rotate top bar in different direction and double the rotation speed
      if(k==1 || k==6 || k == 7){	
	angleY2 = -fmod(angleY + delta/20.0, 360.0);	
	SetRotationY(angleY2, RotationMatrixAnimY2);
        
        // in daddition: rotate models ball_01 and ball_02 around their own center
        if(k==6){
            MultiplyMatrix(RotationMatrixAnimY2, RotationMatrixAnimY2, RotationMatrixAnimY3);
            MultiplyMatrix(RotationMatrixAnimY3, TranslationMatrixAnim, TranslationMatrixAnim2);
            MultiplyMatrix(TranslationMatrixAnim2, RotationMatrixAnimY2, ModelMatrix[k]);
            continue;
        }
        else if(k==7){
            MultiplyMatrix(RotationMatrixAnimY2, RotationMatrixAnimY2, RotationMatrixAnimY3);
            MultiplyMatrix(RotationMatrixAnimY3, TranslationMatrixAnim3, TranslationMatrixAnim2);
            MultiplyMatrix(TranslationMatrixAnim2, RotationMatrixAnimY2, ModelMatrix[k]);
            continue;
        } 

	MultiplyMatrix(RotationMatrixAnimY2, IdentityMatrixAnim, ModelMatrix[k]);	
	MultiplyMatrix(RotationMatrixAnimY2,RotationMatrixAnimY2,ModelMatrix[k]);
	
      }
     
      
      else {
        // additional rotation of ring with double speed oround its center
        if(k==11){
            MultiplyMatrix(RotationMatrixAnimY, TranslationMatrixAnim5, RotationMatrixAnimY3);
            MultiplyMatrix(RotationMatrixAnimY3, RotationMatrixAnimY, TranslationMatrixAnim4);
            MultiplyMatrix(TranslationMatrixAnim4, RotationMatrixAnimY, ModelMatrix[k]);
            continue;
        }
        MultiplyMatrix(RotationMatrixAnim, IdentityMatrixAnim, ModelMatrix[k]);			
      }
      
    }
    
    /* Issue display refresh */
    glutPostRedisplay();
}
예제 #20
0
void Initialize(void)
{   
    /* Set background (clear) color to dark blue */ 
    glClearColor(0.0, 0.0, 0.4, 0.0);

    /* Enable depth testing */
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);    
    
    /* Transform matrix for the bar in the middle*/
    
    /*copy objects into buffer*/
    memcpy(vertex_buffer_data1, vertex_octagon, sizeof(vertex_octagon));
    memcpy(vertex_buffer_data2, vertex_pyramid, sizeof(vertex_pyramid));
    memcpy(vertex_buffer_data3, vertex_mainBar, sizeof(vertex_mainBar));
    memcpy(vertex_buffer_data4, vertex_cube, sizeof(vertex_cube));    
    memcpy(vertex_buffer_data5, vertex_cube, sizeof(vertex_cube));
    memcpy(vertex_buffer_data6, vertex_cube, sizeof(vertex_cube));
    memcpy(vertex_buffer_data7, vertex_cube, sizeof(vertex_cube));
/*
    memcpy(color_buffer_data1, color_octagon, sizeof(color_octagon));
    memcpy(color_buffer_data2, color_pyramid, sizeof(color_pyramid));
    memcpy(color_buffer_data3, color_mainBar, sizeof(color_mainBar));
    memcpy(color_buffer_data4, color_cube, sizeof(color_cube));    
    memcpy(color_buffer_data5, color_cube, sizeof(color_cube));
    memcpy(color_buffer_data6, color_cube, sizeof(color_cube));
    memcpy(color_buffer_data7, color_cube, sizeof(color_cube));
*/
    memcpy(index_buffer_data1, index_octagon, sizeof(index_octagon));
    memcpy(index_buffer_data2, index_pyramid, sizeof(index_pyramid));
    memcpy(index_buffer_data3, index_mainBar, sizeof(index_mainBar));
    memcpy(index_buffer_data4, index_cube, sizeof(index_cube));
    memcpy(index_buffer_data5, index_cube, sizeof(index_cube));
    memcpy(index_buffer_data6, index_cube, sizeof(index_cube));
    memcpy(index_buffer_data7, index_cube, sizeof(index_cube));

    /* Setup vertex, color, and index buffer objects */
    SetupDataBuffers();

    /* Setup shaders and shader program */
    CreateShaderProgram();  

    /* Initialize matrices */
    SetIdentityMatrix(ProjectionMatrix);
    SetIdentityMatrix(ViewMatrix);
    int i = 0;
    for(;i<OBJECTS;i++) {
		SetIdentityMatrix(ModelMatrix[i]);
	}
	
	printf("DEBUG: adding light-source ...\n");

	/* Add lighting source to the code */
	
	glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
	glEnable(GL_LIGHTING);
	glEnable(GL_COLOR_MATERIAL);
	glEnable(GL_LIGHT0); // adds first light-source to the scene

	GLfloat ambientLight[] = {0.2, 0.2, 0.2, 1.0};
	GLfloat diffuseLight[] = {0.8, 0.8, 0.8, 1.0};
	GLfloat specularLight[]= {1.0, 1.0, 1.0, 1.0};
	GLfloat positionLight[]= {5.0, 0.0, 0.0, 0.0};
	
	//glShadeModel(GL_SMOOTH);
	
	glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
	glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
	glLightfv(GL_LIGHT0, GL_POSITION, positionLight);
	
	GLfloat black[] = {0.0, 0.0, 0.0, 1.0};
	GLfloat green[] = {0.0, 1.0, 0.0, 1.0};
	GLfloat white[] = {1.0, 1.0, 1.0, 1.0};
	
	glMaterialfv(GL_FRONT, GL_AMBIENT, green);
	glMaterialfv(GL_FRONT, GL_DIFFUSE, green);
	glMaterialfv(GL_FRONT, GL_SPECULAR, white);
	glMaterialf(GL_FRONT, GL_SHININESS, 60.0);
	
	printf("DEBUG: finished adding light-source\n");

    /* Set projection transform */
    float fovy = 45.0;
    float aspect = 1.0; 
    float nearPlane = 1.0; 
    float farPlane = 50.0;
    SetPerspectiveMatrix(fovy, aspect, nearPlane, farPlane, ProjectionMatrix);

    /* Set viewing transform */
    float camera_disp = -10.0;
    SetTranslation(0.0, 0.0, camera_disp, ViewMatrix);

    /* Translate and rotate cube */
    SetTranslation(0, 0, 0, TranslateOrigin);
    SetRotationX(0.0, RotateX);
    SetRotationZ(0.0, RotateZ);	

    /* Translate down */	
    SetTranslation(0, -sqrtf(sqrtf(2.0) * 1.0), 0, TranslateDown);

    /* Initial transformation matrix */
    MultiplyMatrix(RotateX, TranslateOrigin, InitialTransform);
    MultiplyMatrix(RotateZ, InitialTransform, InitialTransform);
}
예제 #21
0
int main(int argc,char **argv){

	int i,j,k = 0;
	double TrainingAccuracy;
	float **input,**weight,*biase,**tranpI,**tempH,**H;
	float **train_set;
	float **T,**Y;
	float **out;
	
	train_set = (float **)calloc(TESTSET,sizeof(float *));
	tranpI = (float **)calloc(INPUT_NEURONS,sizeof(float *));
	input = (float **)calloc(TESTSET,sizeof(float *));        		/*datasize * INPUT_NEURONS*/
	weight = (float **)calloc(HIDDEN_NEURONS,sizeof(float *));		/*HIDDEN_NEURONS * INPUT_NEURONS*/
	biase = (float *)calloc(HIDDEN_NEURONS,sizeof(float)); 			/*HIDDEN_NEURONS*/
	tempH = (float **)calloc(HIDDEN_NEURONS,sizeof(float *)); 		/*HIDDEN_NEURONS * datasize*/
	H = (float **)calloc(TESTSET,sizeof(float *));
	T = (float **)calloc(TESTSET,sizeof(float *));
	Y = (float **)calloc(TESTSET,sizeof(float *));
	out = (float **)calloc(HIDDEN_NEURONS,sizeof(float *));
	
	for(i=0;i<TESTSET;i++){
		train_set[i] = (float *)calloc(NUMROWS,sizeof(float));
		input[i] = (float *)calloc(INPUT_NEURONS,sizeof(float));
		H[i] = (float *)calloc(HIDDEN_NEURONS,sizeof(float));
	}
	for(i=0;i<TESTSET;i++)
	{
		T[i] = (float *)calloc(OUTPUT_NEURONS,sizeof(float));
		Y[i] = (float *)calloc(OUTPUT_NEURONS,sizeof(float));
	}
	for(i=0;i<INPUT_NEURONS;i++){
		tranpI[i] = (float *)calloc(TESTSET,sizeof(float));
	}
	
	for(i=0;i<HIDDEN_NEURONS;i++)
	{
		weight[i] = (float *)calloc(INPUT_NEURONS,sizeof(float));
		tempH[i] = (float *)calloc(TESTSET,sizeof(float));
		out[i] = (float *)calloc(OUTPUT_NEURONS,sizeof(float));
	}
	printf("begin to classification test...\n");	
	/*得到随机的偏置和权重*/
	//RandomWeight(weight,HIDDEN_NEURONS,INPUT_NEURONS);
	//RandomBiase(biase,HIDDEN_NEURONS);
	printf("begin to load weight...\n");
	if(LoadMatrix(weight,"../result/weight",HIDDEN_NEURONS,INPUT_NEURONS) == 0){
		printf("load weight file error!!!\n");	
		return 0;
	}
	printf("begin to load bias...\n");
	if(LoadMatrix_s(biase,"../result/biase",1,HIDDEN_NEURONS) == 0){
		printf("load bias file error!!!\n");	
		return 0;
	}
	/*加载数据集到内存*/
	printf("begin to load test file...\n");
	if(LoadMatrix(train_set,"../sample/covtype_test",TESTSET,NUMROWS) == 0){
		printf("load input file error!!!\n");	
		return 0;
	}
	InitMatrix(T,TESTSET,OUTPUT_NEURONS,-1);
	/*将数据集划分成输入和输出*/
	for(i = 0;i < TESTSET ;i++)
	{
			//get the last column
		T[k++][0] = train_set[i][0] - 1;//class label starts from 0,so minus one
		for(j = 1;j <= INPUT_NEURONS;j++)
		{
			input[i][j-1] = train_set[i][j];
		}
	}
	for(i = 0;i < TESTSET ;i++)
	{
		for(j = 0;j < OUTPUT_NEURONS;j++)
		{
			k =  T[i][0];
			if(k < OUTPUT_NEURONS && k >= 0){
				T[i][k] = 1;
			}
			if(k != 0){
				T[i][0] = -1;
			}
		}
	}
	/*ELM*/
	printf("begin to compute...\n");
	
	TranspositionMatrix(input,tranpI,TESTSET,INPUT_NEURONS);
	printf("begin to compute step 1...\n");
	MultiplyMatrix(weight,HIDDEN_NEURONS,INPUT_NEURONS, tranpI,INPUT_NEURONS,TESTSET,tempH);
	printf("begin to compute setp 2...\n");
	AddMatrix_bais(tempH,biase,HIDDEN_NEURONS,TESTSET);
	printf("begin to compute step 3...\n");
	SigmoidHandle(tempH,HIDDEN_NEURONS,TESTSET);
	printf("begin to compute step 4...\n");
	TranspositionMatrix(tempH,H,HIDDEN_NEURONS,TESTSET);
	printf("begin to load hidden output matrix from the file...\n");
	if(LoadMatrix(out,"../result/result",HIDDEN_NEURONS,OUTPUT_NEURONS) == 0){
		printf("load input file error!!!\n");	
		return 0;
	}
	MultiplyMatrix(H,TESTSET,HIDDEN_NEURONS,out,HIDDEN_NEURONS,OUTPUT_NEURONS,Y);
	float MissClassificationRate_Training=0;
	double maxtag1,maxtag2;
	int tag1 = 0,tag2 = 0;
	for (i = 0; i < TESTSET; i++) {
		maxtag1 = Y[i][0];
		tag1 = 0;
		maxtag2 = T[i][0];
		tag2 = 0;
		for (j = 1; j < OUTPUT_NEURONS; j++) {
			if(Y[i][j] > maxtag1){
				maxtag1 = Y[i][j];
				tag1 = j;
			}
			if(T[i][j] > maxtag2){
				maxtag2 = T[i][j];
				tag2 = j;
			}
		}
		if(tag1 != tag2)
		    	MissClassificationRate_Training ++;
	}
	TrainingAccuracy = 1 - MissClassificationRate_Training*1.0f/TESTSET;
	printf("trainning accuracy :%f\n",TrainingAccuracy);
	printf("test complete...\n");
	//print(PIMatrix,TESTSET,HIDDEN_NEURONS);
		
	return 0;
}