Ejemplo n.º 1
0
void kmeans()
{
    char ch;
	int i, k;
	double c;
	double deviation = 100;
	FILE* fp = fopen(file_name7, "r");
	FILE* fp1 = fopen(file_name8, "r");
	if(!fp)
		exit(0);
	if(!fp1)
		exit(0);
	fprintf(fp_log, "Kmeans starts\n");
	/*
	while(!feof(fp)){
        fscanf(fp, "%c", &ch);
        if(ch == '\n')
              ++N;
    }
    ++N; 
     
    while(!feof(fp1)){
        fscanf(fp1, "%c", &ch);
        if(ch == '\n')
              ++K;
    }
    */
    fprintf(fp_log, "Size of codebook = %d, No. of training vectors = %d\n", K, N);
    
    //fseek(fp, 0, SEEK_SET);
    //fseek(fp1, 0, SEEK_SET);
 
    min_distance = (double* ) malloc(sizeof(double) * N);
	data_set = (double** ) malloc(sizeof(double*) * N);
	data_set_index = (int*) malloc(sizeof(int) * N);
	for(i=0;i < N; ++i){
        data_set[i]=(double *)malloc(sizeof(double) * NO_OF_CEP);
		for(k = 0; (k < NO_OF_CEP) && (!feof(fp)); ++k){
			fscanf(fp, "%lf", &c);
			data_set[i][k] = c;
		}
    }

	cluster_length = (int*) malloc(sizeof(int) * K );
    
    centroid = (double**) malloc(sizeof(double*) * K);
    for(i=0;i < K; ++i){
        centroid[i] = (double*) malloc(sizeof(double) * NO_OF_CEP );
        			
		for(k = 0; (k < NO_OF_CEP) && (!feof(fp1)); ++k){
			fscanf(fp1, "%lf", &c);
			//printf("%35.30lf\t", c);
			centroid[i][k] = c;
			//printf("%35.30lf\t", centroid[i][k]);
		}
    }
    for(i = 0; i < K; ++i)
        cluster_length[i] = 0;
        
    prev_distortion = 1000;
    i = 0;
    while(deviation > EPSILON){
        update_centroid();
        find_avg_distortion();
        deviation = prev_distortion - new_distortion;
		fprintf(fp_dev, "%lf\n", deviation); 
		//printf("deviation: %lf\n", deviation);
        prev_distortion = new_distortion;
        ++i;
    }
    fprintf(fp_log, "no of iterations needed:%d\n", i);
    fclose(fp1);
    fp1 = fopen(file_name8, "w");
    for(i = 0; i < K; ++i){
          for(k = 0; k < NO_OF_CEP; ++k)
                fprintf(fp1, "%0.30lf\t", centroid[i][k]);
          fprintf(fp1,"\n");
    }
    //fprintf(fp_log, "Kmeans ends\n");
	fclose(fp);
	fclose(fp1);
}
Ejemplo n.º 2
0
  //!GLFW keyboard callback
  void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  {	
  	//std::cout << key << std::endl;
    //!Close the window if the ESC key was pressed
    if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
      glfwSetWindowShouldClose(window, GL_TRUE);
    else if (key == NUMERICAL1 && action == GLFW_PRESS && view_mode == 1){
    	std:: cout << "WCS to VCS" << std::endl;
    	inspection_matrix = wcsTOvcs();
    	view_mode = 2;
    }
    else if (key == NUMERICAL2 && action == GLFW_PRESS && view_mode == 2){
    	std:: cout << "VCS to CCS" << std::endl;
    	inspection_matrix = vcsTOccs() * wcsTOvcs();
    	view_mode = 3;
    }
    else if (key == NUMERICAL3 && action == GLFW_PRESS && view_mode == 3){
    	std:: cout << "CCS to NDCS" << std::endl;
    	inspection_matrix = vcsTOccs() * wcsTOvcs();
    	view_mode = 4;
    }
    else if (key == NUMERICAL4 && action == GLFW_PRESS && view_mode == 4){
    	std:: cout << "NDCS to DCS" << std::endl;
    	inspection_matrix = vcsTOccs() * wcsTOvcs();
    	view_mode = 5;
    }
    else if (key == GLFW_KEY_LEFT && action == GLFW_PRESS) // Left arrow key -> clockwise rotation about y axis
      {
      	yrot = -3.14/18;
      	update_centroid();
      	inspection_matrix = translate(-centroid.x,-centroid.y,-centroid.z) * inspection_matrix;
				inspection_matrix = rotate(0,yrot,0) * inspection_matrix;
				inspection_matrix = translate(centroid.x,centroid.y,centroid.z) * inspection_matrix;
      }
    else if (key == GLFW_KEY_RIGHT && action == GLFW_PRESS) // Right arrow key -> anticlockwise rotation about y axis
      {
      	yrot = 3.14/18;
      	update_centroid();
      	inspection_matrix = translate(-centroid.x,-centroid.y,-centroid.z)*inspection_matrix;
				inspection_matrix = rotate(0,yrot,0) * inspection_matrix;
				inspection_matrix = translate(centroid.x,centroid.y,centroid.z)*inspection_matrix;
      }

    else if (key == GLFW_KEY_UP && action == GLFW_PRESS) // Up arrow key -> anticlockwise rotation about x axis
      {
		  	xrot = 3.14/18;
		  	update_centroid();
      	inspection_matrix = translate(-centroid.x,-centroid.y,-centroid.z)*inspection_matrix;
				inspection_matrix = rotate(xrot,0,0) * inspection_matrix;
				inspection_matrix = translate(centroid.x,centroid.y,centroid.z)*inspection_matrix;
      }
    else if (key == GLFW_KEY_DOWN && action == GLFW_PRESS) // Down arrow key -> clockwise rotation about x axis
      {
		  	xrot = -3.14/18;
		  	update_centroid();
      	inspection_matrix = translate(-centroid.x,-centroid.y,-centroid.z)*inspection_matrix;
				inspection_matrix = rotate(xrot,0,0) * inspection_matrix;
				inspection_matrix = translate(centroid.x,centroid.y,centroid.z)*inspection_matrix;
      }
    else if (key == GLFW_KEY_PAGE_UP && action == GLFW_PRESS) // Page Up key -> anticlockwise rotation about z axis
      {
		  	zrot = 3.14/18;
		  	update_centroid();
      	inspection_matrix = translate(-centroid.x,-centroid.y,-centroid.z)*inspection_matrix;
				inspection_matrix = rotate(0,0,zrot) * inspection_matrix;
				inspection_matrix = translate(centroid.x,centroid.y,centroid.z)*inspection_matrix;
      }
    else if (key == GLFW_KEY_PAGE_DOWN && action == GLFW_PRESS) // Page Down key -> clockwise rotation about z axis
      {
		  	zrot = -3.14/18;
		  	update_centroid();
      	inspection_matrix = translate(-centroid.x,-centroid.y,-centroid.z)*inspection_matrix;
				inspection_matrix = rotate(0,0,zrot) * inspection_matrix;
				inspection_matrix = translate(centroid.x,centroid.y,centroid.z)*inspection_matrix;
      }
    else if (key == R_KEY_CODE && action == GLFW_PRESS) // move the origin to centroid of the model
      {
		  	update_centroid();
		  	inspection_matrix = translate(-centroid.x,-centroid.y,-centroid.z)*inspection_matrix;
      }
    else if (key == W_KEY_CODE && action == GLFW_PRESS) { // translate along y axis by 0.2 units
		  inspection_matrix = translate(0.0,0.2,0.0)*inspection_matrix;
    }
    else if (key == A_KEY_CODE && action == GLFW_PRESS) { // translate along y axis by -0.2 units
		  inspection_matrix = translate(0.0,-0.2,0.0)*inspection_matrix;
    }
    else if (key == S_KEY_CODE && action == GLFW_PRESS) { // translate along x axis by 0.2 units
		  inspection_matrix = translate(0.2,0.0,0.0)*inspection_matrix;
    }
    else if (key == D_KEY_CODE && action == GLFW_PRESS) { // translate along x axis by -0.2 units
		  inspection_matrix = translate(-0.2,0.0,0.0)*inspection_matrix;
    }
    else if (key == Z_KEY_CODE && action == GLFW_PRESS) { // translate along z axis by 0.2 units
		  inspection_matrix = translate(0.0,0.0,0.2)*inspection_matrix;
    }
    else if (key == X_KEY_CODE && action == GLFW_PRESS) { // translate along z axis by -0.2 units
		  inspection_matrix = translate(0,0,-0.2)*inspection_matrix;
    }
  }