Esempio n. 1
0
void ir_bool_flow(nodeType* n)
{
	nodeType* B1 = get_operand(n,0);
	nodeType* B2 = get_operand(n,1);
	debugger("n true label:%s\n",get_T(n));
	debugger("n false label:%s\n",get_F(n));
	switch(n->opr.oper)
	{
	case BOOL_OR:
		debugger("MATCHED BOOL_OR in ir_bool_flow\n");
		set_T(B1,n->opr.T);
		set_F(B1,newlabel());
		set_T(B2,n->opr.T);
		set_F(B2,n->opr.F);
		generate(B1);
		debugger("%s:",get_F(B1)); 
		fprintf(output,"%s:",get_F(B1)); 
		debugger("seen_bool_flow : %d\n",seen_bool_flow);
		generate(B2);
		break;
	case BOOL_EQ:
		//the rule is to load value of B1 and B2 on stack then use beq to jump accordingly so we have to switch of seen_bool_flow flag and restart later.
		seen_bool_flow = 0;
		generate(B1);
		generate(B2);
		seen_bool_flow = 1;
		debugger("MATCHED BOOL_EQ in ir_relop_flow\n");
		debugger("beq %s\n",get_T(n));
		fprintf(output,"beq %s\n",get_T(n));
		debugger("br %s\n",get_F(n));
		fprintf(output,"br %s\n",get_F(n));
		break;
	case NEQ:
		debugger("NOT EQUAL TO\n");
		//the rule is to load value of B1 and B2 on stack then use bne.un to jump accordingly so we have to switch of seen_bool_flow flag and restart later.
		seen_bool_flow = 0;
		generate(B1);
		generate(B2);
		seen_bool_flow = 1;
		debugger("MATCHED NEQ in ir_relop_flow\n");
		debugger("bne.un %s\n",get_T(n));
		fprintf(output,"bne.un %s\n",get_T(n));
		debugger("br %s\n",get_F(n));
		fprintf(output,"br %s\n",get_F(n));
		break;
	case BOOL_AND:
		set_T(B1,newlabel());
		set_F(B1, get_F(n));
		set_T(B2, get_T(n));
		set_F(B2, get_F(n));
		generate(B1);
		debugger("%s:",get_T(B1)); 
		fprintf(output,"%s:",get_T(B1)); 
		generate(B2);
		break;
	default: debugger("Bool DEFAULT\n");
	}
	
}
Esempio n. 2
0
int main()
{
    // Optimal fold
    const char* sequence = "CGCAGGGAUACCCGCGCC";
    char* structure;
    float mfe, gfe;
    structure = seq_fold(sequence, &mfe);
    printf("%s %s %f\n", sequence, structure, mfe);
    free(structure);

    // Ensemble fold
    structure = seq_pf_fold(sequence, &gfe);
    printf("%s %s %f\n", sequence, structure, gfe);
    free(structure);

    printf("\n");

    // Find suboptimal structures
    SOLUTION* sol = seq_subopt(sequence, 4.0);
    for(SOLUTION* s = sol; s->structure != NULL; s++)
    {
        printf("%s %s %f\n", sequence, s->structure, s->energy);
        free(s->structure);
    }
    free(sol);

    printf("\n");

    // Evaluate fe of a structure (given a sequence)...
    printf("%f\n", get_T());
    const char* test_str = "(((.((.....)))))..";
    printf("%s %s %f\n", sequence, test_str, seq_eval(sequence, test_str));
    // ... and how it changes with temperature
    set_T(15.0);
    printf("%f\n", get_T());
    printf("%s %s %f\n", sequence, test_str, seq_eval(sequence, test_str));
    set_T(37.0);

    printf("\n");

    // Take a not so different sequence with a different optimal structure
    const char* seed_seq = "AAUAGGGAUACCCGCGCC";
    structure = seq_fold(seed_seq, &mfe);
    printf("%s %s %f\n", seed_seq, structure, mfe);

    // See that is not even stable on the test fold
    printf("%s %s %f\n", seed_seq, test_str, seq_eval(seed_seq, test_str));

    // Mutate it until you get the test fold...
    char* seq = malloc(strlen(seed_seq) + 1);
    strcpy(seq, seed_seq);
    float dist = str_inverse(seq, test_str, 12345, 0);
    // ... and confirm it's its ground state
    structure = seq_fold(seq, &mfe);
    printf("%s %s %f\n", seq, structure, mfe);

    free(seq);
}
Esempio n. 3
0
void ir_for(nodeType* n)
{
	nodeType* initialize = get_operand(n,0);
	nodeType* expr = get_operand(n,1);
	nodeType* increament = get_operand(n,2);
	nodeType* stmt = get_operand(n,3);
	char initializenext[16], begin[16];
	memset(initializenext,0,16);
	memset(begin,0,16);
	strcat(initializenext,newlabel());
	strcat(begin,newlabel());
	set_T(expr,begin);
	set_F(expr,n->opr.next);
	generate(initialize);				//generate code for initialization expression
	
	//##for break statement##
	char initial_break_label[16];
	memset(initial_break_label,0,16);
	strcpy(initial_break_label,break_label);
	memset(break_label,0,16);
	strcpy(break_label,newlabel());
	//#####for continue statement############
	char initial_continue_label[16];
	memset(initial_continue_label,0,16);
	strcpy(initial_continue_label,continue_label);
	memset(continue_label,0,16);
	strcpy(continue_label,newlabel());
	//#######################################
	loop_flag = loop_flag + 1;

	debugger("br %s\n",initializenext);
	fprintf(output,"br %s\n",initializenext);
	debugger("%s: \n",begin);
	fprintf(output,"%s: ",begin);
	
	generate(stmt);
	
	prepost_put = 0;
	debugger("%s:\n",continue_label);
	fprintf(output,"%s:\n",continue_label);
	
	generate(increament);
	
	debugger("%s: ",initializenext);
	fprintf(output,"%s: ",initializenext);
	seen_bool_flow = 1;
	
	generate(expr);
	
	seen_bool_flow = 0;
	debugger("%s: \n",break_label);
	fprintf(output,"%s: \n",break_label);

	//for break statement
	loop_flag = loop_flag - 1;
	memset(break_label,0,16);
	strcpy(break_label,initial_break_label);
	memset(continue_label,0,16);
	strcpy(continue_label,initial_continue_label);
}
Esempio n. 4
0
void ir_ternary(nodeType* n)
{
	nodeType* expr = get_operand(n,0);
	nodeType* stmt1 = get_operand(n,1);
	nodeType* stmt2 = get_operand(n,2);
	set_T(expr,newlabel());
	set_F(expr,newlabel());
	memset(stmt1->opr.next,0,16);
	memset(stmt2->opr.next,0,16);
	memset(n->opr.next,0,16);
	strcpy(stmt1->opr.next,n->opr.next);
	strcpy(stmt2->opr.next,n->opr.next);
	strcpy(n->opr.next,newlabel());
	
	seen_bool_flow = 1;
	generate(expr);
	seen_bool_flow = 0;
	
	debugger("%s:\n",get_T(expr));
	fprintf(output,"%s:\n",get_T(expr));
	prepost_put = 1;
	generate(stmt1);
	prepost_put = 0;
	debugger("br.s %s\n ",n->opr.next);
	fprintf(output,"br.s %s\n",n->opr.next);
	debugger("%s:\n",get_F(expr));
	fprintf(output,"%s:\n",get_F(expr));
	prepost_put = 1;
	generate(stmt2);
	prepost_put = 0;
	debugger("%s:\n",n->opr.next);
	fprintf(output,"%s:\n",n->opr.next);
}	
Esempio n. 5
0
void ir_if(nodeType* n)
{
	nodeType* expr = get_operand(n,0);
	nodeType* stmt = get_operand(n,1);
	set_T(expr,newlabel());
	set_F(expr,n->opr.next);
	
	memset(stmt->opr.next,0,16);
	strcat(stmt->opr.next,n->opr.next);
	
	debugger("expr true label:%s\n",get_T(expr));
	debugger("expr false label:%s\n",get_F(expr));
	
	seen_bool_flow = 1;prepost_put = 1;
	
	generate(expr);
	
	seen_bool_flow = 0;prepost_put = 0;
	
	debugger("%s:\n",get_T(expr));
	fprintf(output,"%s:\n",get_T(expr));
	
	generate(stmt);
	
	return;
}	
Esempio n. 6
0
void ir_while(nodeType* n)
{
	nodeType* expr = get_operand(n,0);
	nodeType* stmt = get_operand(n,1);
	char begin[16];
	memset(begin,0,16);
	strcat(begin,newlabel());
	set_T(expr,newlabel());
	set_F(expr,n->opr.next);
	memset(stmt->opr.next,0,16);
	strcpy(stmt->opr.next,begin);
	debugger("%s:\n",begin);
	fprintf(output,"%s:\n",begin);
	
	seen_bool_flow = 1;prepost_put = 1;
	generate(expr);
	seen_bool_flow = 0;prepost_put = 0;
	
	debugger("%s:\n",get_T(expr));
	fprintf(output,"%s:\n",get_T(expr));

	//##for break statement##
	char initial_break_label[16];
	memset(initial_break_label,0,16);
	strcat(initial_break_label,break_label);
	memset(break_label,0,16);
	loop_flag = loop_flag + 1;
	strcat(break_label,n->opr.next);
	//#####for continue statement############
	char initial_continue_label[16];
	memset(initial_continue_label,0,16);
	strcpy(initial_continue_label,continue_label);
	memset(continue_label,0,16);
	strcpy(continue_label,begin);
	debugger("CONTINUE LABEL: %s\n",continue_label);
	//#######################################

	
	generate(stmt);
	
	debugger("br.s %s\n ",begin);
	fprintf(output,"br.s %s\n",begin);

	//for break statement
	loop_flag = loop_flag - 1;
	memset(break_label,0,16);
	strcat(break_label,initial_break_label);
	memset(continue_label,0,16);
	strcpy(continue_label,initial_continue_label);	
}	
Esempio n. 7
0
void my_raytrace(int mousex, int mousey)
{
	double modelViewMatrix[16];
	double projMatrix[16];
	float heldSta[3];
	float heldDir[3];
	int viewport[4];
	int foundIntersection = 0;
	int hit = 0;
	int i;
	double clickPoint[3];
	GLfloat clickedPoint[4];
	GLfloat intersectionPoint[3];
	float closestPoint[3];
	float rayStart[3];
	float rayDirection[3];
	OBJECT *cur;
	OBJECT *temp;
	double norm;
	
	
	// first we need to get the modelview matrix, the projection matrix, and the viewport
	glGetDoublev(GL_MODELVIEW_MATRIX, modelViewMatrix);
	glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
	glGetIntegerv(GL_VIEWPORT, viewport);
	mousey = viewport[3]-mousey;

	// gluUnProject with a Z value of 1 will find the point on the far clipping plane
	// corresponding the the mouse click. This is not the same as the vector
	// representing the click.
	gluUnProject(mousex, mousey, 1.0, modelViewMatrix, projMatrix, viewport, &clickPoint[0], &clickPoint[1], &clickPoint[2]);

	// Now we need a vector representing the click. It should start at the camera
	// position. We can subtract the click point, we will get the vector
	

	/* code for finding direction vector, set rayStart and rayDirection */
	rayStart[0] = my_cam.pos[0];
	rayStart[1] = my_cam.pos[1];
	rayStart[2] = my_cam.pos[2];
	
	
	rayDirection[0] = clickPoint[0]-rayStart[0];
	rayDirection[1] = clickPoint[1]-rayStart[1];
	rayDirection[2] = clickPoint[2]-rayStart[2];
			
	norm = sqrt(rayDirection[0]*rayDirection[0]+rayDirection[1]*rayDirection[1]+rayDirection[2]*rayDirection[2]);
		
	rayDirection[0] = rayDirection[0]/norm;
	rayDirection[1] = rayDirection[1]/norm;
	rayDirection[2] = rayDirection[2]/norm;
	
	heldDir[0] = rayDirection[0];
	heldDir[1] = rayDirection[1];
	heldDir[2] = rayDirection[2];

	heldSta[0] = rayStart[0];
	heldSta[1] = rayStart[1];
	heldSta[2] = rayStart[2];
	
	
	lineStart[0]=rayStart[0];
	lineStart[1]=rayStart[1];
	lineStart[2]=rayStart[2];
	
	lineEnd[0]=clickPoint[0];
	lineEnd[1]=clickPoint[1];
	lineEnd[2]=clickPoint[2];


	// now go through the shapes and see if there is a hit
	for (i=0; i<num_objects; i++)
	{
		cur = my_objects + i;
		hit = 0;
		rayDirection[0] = heldDir[0];
		rayDirection[1] = heldDir[1];
		rayDirection[2] = heldDir[2];

		rayStart[0] = heldSta[0];
		rayStart[1] = heldSta[1];
		rayStart[2] = heldSta[2];
		

		switch (cur->sid)
		{
		
		case 3:
			//translation
			clickedPoint[0] = rayStart[0];
			clickedPoint[1] = rayStart[1];
			clickedPoint[2] =rayStart[2];
			clickedPoint[3] =1;
			set_T(-cur->translate[0],-cur->translate[1],-cur->translate[2]);
			matrix_mult(T,clickedPoint);
			rayStart[0] = result[0];
			rayStart[1] = result[1];
			rayStart[2] = result[2];
			//rotation point
			
			//RzT
			clickedPoint[0] = rayStart[0];
			clickedPoint[1] = rayStart[1];
			clickedPoint[2] =rayStart[2];
			clickedPoint[3] =1;
			set_RzT(cur->rotate[2]);
			matrix_mult(RzT,clickedPoint);
			rayStart[0] = result[0];
			rayStart[1] = result[1];
			rayStart[2] = result[2];
			
			//RyT
			clickedPoint[0] = rayStart[0];
			clickedPoint[1] = rayStart[1];
			clickedPoint[2] =rayStart[2];
			clickedPoint[3] =1;
			set_RyT(cur->rotate[1]);
			matrix_mult(RyT,clickedPoint);
			rayStart[0] = result[0];
			rayStart[1] = result[1];
			rayStart[2] = result[2];
			
			//RxT
			
			clickedPoint[0] = rayStart[0];
			clickedPoint[1] = rayStart[1];
			clickedPoint[2] =rayStart[2];
			clickedPoint[3] =1;
			set_RxT(cur->rotate[0]);
			matrix_mult(RxT,clickedPoint);
			rayStart[0] = result[0];
			rayStart[1] = result[1];
			rayStart[2] = result[2];
			
				
			//scaling point
			clickedPoint[0] = rayStart[0];
			clickedPoint[1] = rayStart[1];
			clickedPoint[2] =rayStart[2];
			clickedPoint[3] =1;
			
			set_S(1/cur->scale[0],1/cur->scale[1],1/cur->scale[2]);
			matrix_mult(S,clickedPoint);
			rayStart[0] = result[0];
			rayStart[1] = result[1];
			rayStart[2] = result[2];
			
			
			//rotation vector
			
			//RzT
			clickedPoint[0] = rayDirection[0];
			clickedPoint[1] = rayDirection[1];
			clickedPoint[2] =rayDirection[2];
			clickedPoint[3] =1;
			set_RzT(cur->rotate[2]);
			matrix_mult(RzT,clickedPoint);
			rayDirection[0] = result[0];
			rayDirection[1] = result[1];
			rayDirection[2] = result[2];
			
			//RyT
			clickedPoint[0] = rayDirection[0];
			clickedPoint[1] = rayDirection[1];
			clickedPoint[2] =rayDirection[2];
			clickedPoint[3] =1;
			set_RyT(cur->rotate[1]);
			matrix_mult(RyT,clickedPoint);
			rayDirection[0] = result[0];
			rayDirection[1] = result[1];
			rayDirection[2] = result[2];
			
			//RxT
			
			clickedPoint[0] = rayDirection[0];
			clickedPoint[1] = rayDirection[1];
			clickedPoint[2] =rayDirection[2];
			clickedPoint[3] =1;
			set_RxT(cur->rotate[0]);
			matrix_mult(RxT,clickedPoint);
			rayDirection[0] = result[0];
			rayDirection[1] = result[1];
			rayDirection[2] = result[2];
			
			
			//scaling vector
			
			clickedPoint[0] = rayDirection[0];
			clickedPoint[1] = rayDirection[1];
			clickedPoint[2] =rayDirection[2];
			clickedPoint[3] =1;
			
			set_S(1/cur->scale[0],1/cur->scale[1],1/cur->scale[2]);
			matrix_mult(S,clickedPoint);
			rayDirection[0] = result[0];
			rayDirection[1] = result[1];
			rayDirection[2] = result[2];
			
				
			hit = my_raytrace_sph(cur, rayStart, rayDirection, intersectionPoint);
			
			//scale intersect
			clickedPoint[0] = intersectionPoint[0];
			clickedPoint[1] = intersectionPoint[1];
			clickedPoint[2] = intersectionPoint[2];
			clickedPoint[3] = 1;
		
			set_S(cur->scale[0],cur->scale[1],cur->scale[2]);
			matrix_mult(S,clickedPoint);
			intersectionPoint[0]= result[0];
			intersectionPoint[1]=result[1];
			intersectionPoint[2]=result[2];
			
			
			//rotate intersect
			
			//Rx
			clickedPoint[0] = intersectionPoint[0];
			clickedPoint[1] = intersectionPoint[1];
			clickedPoint[2] =intersectionPoint[2];
			clickedPoint[3] =1;
			set_Rx(cur->rotate[0]);
			matrix_mult(Rx,clickedPoint);
			intersectionPoint[0] = result[0];
			intersectionPoint[1] = result[1];
			intersectionPoint[2] = result[2];
			
			//Ry
			clickedPoint[0] = intersectionPoint[0];
			clickedPoint[1] = intersectionPoint[1];
			clickedPoint[2] =intersectionPoint[2];
			clickedPoint[3] =1;
			set_Ry(cur->rotate[1]);
			matrix_mult(Ry,clickedPoint);
			intersectionPoint[0] = result[0];
			intersectionPoint[1] = result[1];
			intersectionPoint[2] = result[2];
			
			//Rz
			
			clickedPoint[0] = intersectionPoint[0];
			clickedPoint[1] = intersectionPoint[1];
			clickedPoint[2] =intersectionPoint[2];
			clickedPoint[3] =1;
			set_Rz(cur->rotate[2]);
			matrix_mult(Rz,clickedPoint);
			intersectionPoint[0] = result[0];
			intersectionPoint[1] = result[1];
			intersectionPoint[2] = result[2];
			
			
			//translate intersect
			
			clickedPoint[0] = intersectionPoint[0];
			clickedPoint[1] = intersectionPoint[1];
			clickedPoint[2] = intersectionPoint[2];
			clickedPoint[3] = 1;
		
			set_T(cur->translate[0],cur->translate[1],cur->translate[2]);
			matrix_mult(T,clickedPoint);
			intersectionPoint[0]= result[0];
			intersectionPoint[1]=result[1];
			intersectionPoint[2]=result[2];
			break;
	
		
			
		default:
			break;
		}

		// found intersection
		if (hit)
		{
			if (foundIntersection)
			{
				if(intersectionPoint[2]>closestPoint[2]){
					closestPoint[0] = intersectionPoint[0];
					closestPoint[1] = intersectionPoint[1];
					closestPoint[2] = intersectionPoint[2];
					printf("found closer collision\n");
				}
				else{
					printf("found another collision, but it was not closer\n");
				}
				temp=cur;
			}
			else
			{
				closestPoint[0] = intersectionPoint[0];
				closestPoint[1] = intersectionPoint[1];
				closestPoint[2] = intersectionPoint[2];
				temp= cur;
				
			}

			foundIntersection = 1;
		}
	}

	if (foundIntersection)
	{
		LITE *pl;
		printf("Intersected with object %s at (%f, %f, %f)\n", "object_name", closestPoint[0], closestPoint[1], closestPoint[2]);

  		illum_light_b=0;
  		illum_light_g=0;
  		illum_light_r=0;
  		float L_vec[4];
  		float temp_center[4];
  		float N_vec[4];
  		L_vec[0]=0;
  		L_vec[1]=0;
  		L_vec[2]=0;
  		L_vec[3]=0;
  		
  		N_vec[0]=0;
  		N_vec[1]=0;
  		N_vec[2]=0;
  		N_vec[3]=0;
  		
  		
  		
  		illu_r=0;
  		illu_g=0;
  		illu_b=0;
  		
  		//go through lights
  		for(i=1;i<num_lights+1;i++){
  		
  			pl = my_lights+i;
  			
  			
  			
  			//calculate normal
  			N_vec[0] = closestPoint[0]-temp->translate[0];
  			N_vec[1] = closestPoint[1]-temp->translate[1];
  			N_vec[2] = closestPoint[2]-temp->translate[2];
  			N_vec[3] = 0;
  			
  			
  			//normalize normal
  			normalize(N_vec);
  			
  			
  			//calc light dir
  			L_vec[0]=pl->pos[0] - closestPoint[0];
  			L_vec[1]=pl->pos[1] - closestPoint[1];
  			L_vec[2]=pl->pos[2] - closestPoint[2];
  			L_vec[3]=0;
  			
  				
  			
  			
  			
  			//normalize light
  			normalize(L_vec);
  			
  			//calculate light illumination
  			illum_light_r+=pl->diff[0]*(k_d*temp->diff[0]*dotprod(N_vec,L_vec));
  			illum_light_g+=pl->diff[1]*(k_d*temp->diff[1]*dotprod(N_vec,L_vec));
  			illum_light_b+=pl->diff[2]*(k_d*temp->diff[2]*dotprod(N_vec,L_vec));
  		

  		
  		}
  		
  		//calculate entire rgb illumination
  		illu_r=k_a*temp->amb[0]+illum_light_r;
  		illu_g=k_a*temp->amb[1]+illum_light_g;
  		illu_b =k_a*temp->amb[2]+illum_light_b;
  		
  		
  		
  		
  		
	}
}
Esempio n. 8
0
void parse_obj(char *buffer){
  OBJECT *po;
  char *pshape, *pshine, *pemi, *pamb, *pdiff, *pspec, *ptranslate, *pscale, *protate;



  my_assert ((num_objects < NUM_OBJECTS), "too many objects");
  po = &my_objects[num_objects++];

  pshape  = strtok(buffer, " ");
  //printf("pshape is %s\n",pshape);

  ptranslate    = strtok(NULL, "()");  strtok(NULL, "()");
  pscale        = strtok(NULL, "()");  strtok(NULL, "()"); 
  protate       = strtok(NULL, "()");  strtok(NULL, "()");  

  pshine  = strtok(NULL, "()");strtok(NULL, "()");
  //printf("pshine is %s\n",pshine);
 
  pemi    = strtok(NULL, "()");  strtok(NULL, "()"); 
  pamb    = strtok(NULL, "()");  strtok(NULL, "()"); 
  pdiff   = strtok(NULL, "()");  strtok(NULL, "()"); 
  pspec   = strtok(NULL, "()");  strtok(NULL, "()"); 


  po->sid  = atoi(pshape);
  po->shine = atof(pshine);

  parse_floats(ptranslate, po->translate);
  parse_floats(pscale, po->scale);
  parse_floats(protate, po->rotate);

  parse_floats(pemi, po->emi);
  parse_floats(pamb, po->amb);
  parse_floats(pdiff, po->diff);
  parse_floats(pspec, po->spec);
	
  po->center[0] = 0;
  po->center[1] = 0;
  po->center[2] = 0;
  po->center[3] = 1;
	
  
  //translation
	held_val[0] = po->center[0];
	held_val[1] = po->center[1];
	held_val[2] =po->center[2];
	held_val[3] =po->center[3];
	set_T(po->translate[0],po->translate[1],po->translate[2]);
	matrix_mult(T,held_val);
	po->center[0] = result[0];
	po->center[1] = result[1];
	po->center[2] = result[2];
	po->center[3] = result[3];
	//rotation point
	
	
	
	
	

		
	
  
  
  
  printf("objects center:\n x: %f\ny: %f\nz: %f\n",po->center[0],po->center[1],po->center[2]);
  // scale, rotate, translate using your real tranformations from assignment 3 depending on input from spec file
 
  
  printf("read object\n");
}