Example #1
0
void doCollision(double *collideField, int *flagField, const double * const tau, int * length){
    /* 
     * For each inner grid cell in collideField, compute the post-collide
     * distribution
     */
 
    double density;
    double velocity[D];
    double feq[Q];

    double * currentCell;

    int x,y,z;
    int node[3];
    int n[3] = { length[0] + 2, length[1] + 2, length[2] + 2 };

    // Loop over inner cells: compare to streaming.c
    for (z = 1; z <= length[2]; z++) {
        node[2] = z;
        for (y = 1; y <= length[1]; y++) {
            node[1] = y;
            for (x = 1; x <= length[0]; x++) {
                node[0] = x;

                currentCell = getEl(collideField, node, 0, n);

                computeDensity(currentCell, &density);
                computeVelocity(currentCell, &density, velocity);
                computeFeq(&density, velocity, feq);
                computePostCollisionDistributions(currentCell, tau, feq);
            }
        }
    }
}
Example #2
0
void doCollision(double *collideField, int *flagField, const double * const tau,int xlength){
  double density;
  double velocity[3];
  double feq[Q];
  double* currentCell;
  int currentIndex;

  /* Set pointer to first cell that is not a boundary */
  currentIndex = (xlength+3);

  /* Last row is a border */
  while(currentIndex < (xlength+2)*(xlength+2)*(xlength+2) - (xlength+2))
  {
	  /* No left and right border, only inner cells */
	  if(flagField[currentIndex] == FLUID)
	  {
			currentCell = &collideField[currentIndex*Q];
			computeDensity(currentCell, &density);
			computeVelocity(currentCell, &density, velocity);
			computeFeq(&density, velocity, feq);
			computePostCollisionDistributions(currentCell, tau, feq);
	  }
	  currentIndex++;
  }
}
Example #3
0
int main(void) {
	float retrDuration = getDuration();
	float velocity = computeVelocity(retrDuration, dist);
	float estimatedDuration = duration(dist, velocity, wind);
	displayResults(estimatedDuration);
 	return 0;
 }
Example #4
0
InterpMotion::InterpMotion(const Transform3f& tf1_, const Transform3f& tf2_) : MotionBase(),
                                                                               tf1(tf1_),
                                                                               tf2(tf2_),
                                                                               tf(tf1)
{
  // Compute the velocities for the motion
  computeVelocity();
}
Example #5
0
InterpMotion::InterpMotion(const Matrix3f& R1, const Vec3f& T1,
                           const Matrix3f& R2, const Vec3f& T2) : MotionBase(),
                                                                  tf1(R1, T1),
                                                                  tf2(R2, T2),
                                                                  tf(tf1)
{
  // Compute the velocities for the motion
  computeVelocity();
}
Example #6
0
InterpMotion::InterpMotion(const Matrix3f& R1, const Vec3f& T1,
                           const Matrix3f& R2, const Vec3f& T2,
                           const Vec3f& O) :
  tf1(R1, T1),
  tf2(R2, T2),
  tf(tf1),
  reference_p(O)
{
  // Compute the velocities for the motion
  computeVelocity();
}
Example #7
0
/* NOTE: We output only the fluid cells */
void writeVtkOutput(const double * const collideField, const int * const flagField, 
        const char * filename, unsigned int t, int xlength, int rank, int x_proc) {
  int i, j, k;
  double velocity[3], density;
  /* len is used to set values in lexicographic order "[ Q * ( z*len*len + y*len + x) + i ]" */
  int len = xlength + 2;

  char szFileName[80];
  FILE *fp=NULL;
  sprintf( szFileName, "%s-rank%i.%i.vtk", filename, rank, t );
  fp = fopen( szFileName, "w");
  if( fp == NULL ){
    char szBuff[80];
    sprintf( szBuff, "Failed to open %s", szFileName );
    ERROR( szBuff );
    return;
  }

  write_vtkHeader(fp, xlength);
  write_vtkPointCoordinates(fp,xlength,rank,x_proc);

  fprintf(fp,"POINT_DATA %i \n", xlength*xlength*xlength );

  fprintf(fp,"\n");
  fprintf(fp, "VECTORS velocity float\n");
  for(k = 1; k < xlength+1; k++) {
    for(j = 1; j < xlength+1; j++) {
      for(i = 1; i < xlength+1; i++) {
        computeDensity (&collideField[ 19 * ( k*len*len + j*len + i) ], &density);
        computeVelocity(&collideField[ 19 * ( k*len*len + j*len + i) ], &density, velocity);
        fprintf(fp, "%f %f %f\n",  velocity[0], velocity[1], velocity[2]);
      }
    }
  }

  fprintf(fp,"\n");
  fprintf(fp, "SCALARS density float 1 \n");
  fprintf(fp, "LOOKUP_TABLE default \n");
  for(k = 1; k < xlength+1; k++) {
    for(j = 1; j < xlength+1; j++) {
      for(i = 1; i < xlength+1; i++) {
        computeDensity (&collideField[ 19 * ( k*len*len + j*len + i) ], &density);
        fprintf(fp, "%f\n",  density);
      }
    }
  }

  if(fclose(fp)){
    char szBuff[80];
    sprintf( szBuff, "Failed to close %s", szFileName );
    ERROR( szBuff );
  }
}
void acarFluid::step() {
	//smooth level set?
	smoothLevelSet(levelset, levelsetSmooth);
	//solve for stream function using the level set
	fftw_execute( levelsetPlan);

	solvePoisson(levelsetF, streamF);
	fftw_execute(streamPlanI);
	//use stream function to compute velocity
	computeVelocity(stream, velocityU, velocityV);
	//use velocity to advect (level set?, smoothed level set?, vorticity?)
	advectLinear(levelset, levelsetTemp);
	for(int i=0;i<width*height;++i) levelset[i] = levelsetTemp[i];
	//repeat
}
Example #9
0
  void WriteVtk::perform(MultiBlockLattice2D<T,DESCRIPTOR> &lattice, 
			 plint nStep)
  {
    T dx = controller->getParams().getDeltaX();
    T physU = controller->getUnits().physVelocity();
    std::string fname = createFileName(prefix, nStep, fileNameLength);
    VtkImageOutput2D<T> vtkOut(fname, dx);
    
    std::auto_ptr<MultiScalarField2D<T> > density = computeDensity(lattice);
    PressureFromRho p(controller);

    apply(p,*density);
    
    vtkOut.writeData<float>(*density, "pressure", 1);
    vtkOut.writeData<2,float>(*computeVelocity(lattice), "velocity", physU);
    pcout << "vtk file " << fname << " written" << std::endl;
  };
Example #10
0
    const Calc::Output& Calc::operator()(const Input& frames)
    {
        cv::Mat old, current;
        cv::cvtColor(frames.old, old, CV_BGR2GRAY);
        cv::cvtColor(frames.current, current, CV_BGR2GRAY);

        frames.current.copyTo(_out.original);

        old = 255 + (old - _background);
        current = 255 + (current - _background);

        computeDensity(current);
        computeDensityMask(current);
        computeAlignment(current);
        computeVelocity(old, current);

        return _out;
    }
Example #11
0
void doCollision(double *collideField, int *flagField, const double * const tau,
        const int * const sublength)
{
    for (int z = 1; z < sublength[0] + 1; ++z) {
        for (int y = 1; y < sublength[1] + 1; ++y) {
            for (int x = 1; x < sublength[2] + 1; ++x) {
                double *currentCell = &collideField[idx(sublength, x, y, z, 0)];
                /*Updating values for velocity, density and Feq for Current cell*/
                double density;
                computeDensity(currentCell, &density);
                double velocity[D];
                computeVelocity(currentCell, &density, velocity);
                double feq[Q];
                computeFeq(&density, velocity, feq);
                computePostCollisionDistributions(currentCell, tau, feq);
            }
        }
    }
}
Example #12
0
void doCollision(double *collideField, int *flagField,const double * const tau, int *subdomain){


	double density;
	double velocity[D];
	double feq[Q];
	double *currentCell = NULL; // currentCell points to the first distribution function within the respective cell
        for (int iz=1; iz<=subdomain[2]; iz++){
		for (int iy=1; iy<=subdomain[1]; iy++){
			for (int ix=1; ix<=subdomain[0]; ix++){
				// set pointer to current cell
				currentCell = collideField + Q * compute_index(ix, iy, iz, subdomain);

				// compute density, velocity and equilibrium prob. distrib. for this cell
				computeDensity ( currentCell, &density );
				computeVelocity ( currentCell, &density, velocity );
				computeFeq ( &density, velocity, feq );

				computePostCollisionDistributions ( currentCell, tau, feq );
			}
		}
	}
}
Example #13
0
/** carries out the whole local collision process. Computes density and velocity and
 *  equilibrium distributions. Carries out BGK update.
 */
void doCollision(double *collideField, int *flagField,const double * const tau,int xlength){

	int x,y,z ;
	int counter;
	double density;
	double velocity[3] ;
	double feq[Q] ;

    /* we loop over all the inner cells i.e. fluid cells */
	for (z = 1; z < xlength+1; z++) {
		for (y = 1; y < xlength+1; y++) {
			for (x = 1; x < xlength+1; x++) {
                /* get the current index */
				counter  = Q*(z*(xlength+2)*(xlength+2) + y * (xlength+2) + x );
				/* Compute density, velocity, f_eq to finally get the postcollision distribution */
				computeDensity (&collideField[counter], &density) ;
                computeVelocity(&collideField[counter], &density,velocity) ;
				computeFeq(&density,velocity,feq) ;
				computePostCollisionDistributions(&collideField[counter],tau,feq);
			}
		}
	}
}
void writeVtkOutput(const double* const collideField, const int* const flagField, const char* filename, unsigned int timestep, int xlength)
{
  //final filename
  char szFileName[128];

  //file handle
  FILE* fp = NULL;

  //create final filename
  snprintf(szFileName, sizeof(szFileName), "%s.%i.vtk", filename, timestep);

  //try to open the file
  fp = fopen(szFileName, "w");

  //error check
  if(!fp)
  {
    ERROR("writeVtkOutput: Failed to open");
    return;
  }

  //print vtk header
  fprintf(fp, "# vtk DataFile Version 2.0\n");
  fprintf(fp, "generated by CFD LBM \n");
  fprintf(fp, "ASCII\n");
  fprintf(fp, "\n");
  fprintf(fp, "DATASET STRUCTURED_GRID\n");
  fprintf(fp, "DIMENSIONS  %i %i %i \n", xlength+3, xlength+3, xlength+3);
  fprintf(fp, "POINTS %i float\n", (xlength+3)*(xlength+3)*(xlength+3));

  //assume size=1 domain
  double dx = 1.0 / (xlength+2);

  //print list of points
  double originX = 0.0;
  double originY = 0.0;
  double originZ = 0.0;

  for(int i=0;i<=xlength+2;i++)
  {
    for(int j=0;j<=xlength+2;j++)
    {
      for(int k=0;k<=xlength+2;k++)
      {
        fprintf(fp, "%f %f %f\n", originX+(i*dx), originY+(j*dx), originZ+(k*dx));
      }
    }
  }

  //print velocity vectors
  fprintf(fp, "\n");
  fprintf(fp, "CELL_DATA %i \n", (xlength+2)*(xlength+2)*(xlength+2));
  fprintf(fp, "VECTORS velocity float\n");

  for(int x=0;x<=xlength+1;x++)
  {
    for(int y=0;y<=xlength+1;y++)
    {
      for(int z=0;z<=xlength+1;z++)
      {
        //variables for temporary calc
        double vel[3];
        double dens;

        //base of fluid cell in collideField
        int index = NUM_LATTICE*((z *(xlength + 2)*(xlength + 2)) + y * (xlength + 2) + x);

        //calculate quantities for current cell
        computeDensity(&collideField[index], &dens);
        computeVelocity(&collideField[index], &dens, vel);

        //output vector
        fprintf(fp, "%f %f %f\n", vel[0], vel[1], vel[2]);
      }
    }
  }

  //print densities
  fprintf(fp, "\n");
  fprintf(fp, "SCALARS density float 1 \n");
  fprintf(fp, "LOOKUP_TABLE default \n");

  for(int x=0;x<=xlength+1;x++)
  {
    for(int y=0;y<=xlength+1;y++)
    {
      for(int z=0;z<=xlength+1;z++)
      {
        //variables for temporary calc
        double dens;

        //base of fluid cell in collideField
        int index = NUM_LATTICE*((z *(xlength + 2)*(xlength + 2)) + y * (xlength + 2) + x);

        //calculate quantities for current cell
        computeDensity(&collideField[index], &dens);

        //output scalar
        fprintf(fp, "%f\n", dens);
      }
    }
  }

  //close file
  if(fclose(fp)!=0)
  {
    ERROR("writeVtkOutput: Failed to close");
  }
}
///--------------------------------------------------------------
void PMMotionExtractor::update()
{
    if(hasKinect){
        kinect.update();
		// Count number of tracked bodies
		int numBodiesTracked = 0;
		auto& bodies = kinect.getBodySource()->getBodies();
		for (auto& body : bodies) {
			if (body.tracked) {
				numBodiesTracked++;
			}
		}
		if (numBodiesTracked != 0) {
			if (!hasUser) {
				hasUser = true;
				ofNotifyEvent(eventUserDetection, hasUser, this);
			}
			//--
			//Getting joint positions (skeleton tracking)
			//--
			//
			{
				auto body = findClosestBody();
				auto referenceBodyZ = body.joints[JointType_SpineBase].getPosition().z;


				/*for (auto body : bodies) {
					if (body.trackingId != closestBody.trackingId) {
						cout << body.trackingId << "  " << closestBody.trackingId << endl;
						break;
					}*/

					for (auto joint : body.joints) {
						if (joint.first == JointType_HandLeft) {
							handsInfo.leftHand.pos = joint.second.getProjected(kinect.getBodySource()->getCoordinateMapper(), ofxKFW2::ProjectionCoordinates::DepthCamera);
							handsInfo.leftHand.pos.x /= 512;
							handsInfo.leftHand.pos.y /= 424;
							handsInfo.leftHand.pos.z = referenceBodyZ - joint.second.getPosition().z;
						}
						else if (joint.first == JointType_HandRight) {
							handsInfo.rightHand.pos = joint.second.getProjected(kinect.getBodySource()->getCoordinateMapper(), ofxKFW2::ProjectionCoordinates::DepthCamera);
							handsInfo.rightHand.pos.x /= 512;
							handsInfo.rightHand.pos.y /= 424;
							handsInfo.rightHand.pos.z = referenceBodyZ - joint.second.getPosition().z;
						}
						else if (joint.first == JointType_Head) {
							auto headPos = joint.second.getProjected(kinect.getBodySource()->getCoordinateMapper(), ofxKFW2::ProjectionCoordinates::DepthCamera);
							headPos.y /= 424;
							if (abs(headPos.y - handsInfo.leftHand.pos.y) < 0.05 && abs(headPos.y - handsInfo.rightHand.pos.y) < positionThreshold) {
								positionDetectedCounter++;
							}
							else {
								positionDetectedCounter = 0;
							}

							if (positionDetectedCounter >= 60) {
								auto userPositioned = true;
								ofNotifyEvent(eventUserPositioned, userPositioned, this);
								positionDetectedCounter = 0;
							}
						}
					}
				//}
				computeVelocity(handsMeanSize);
			}
		}
		else {
			if (hasUser) {
				hasUser = false;
				ofNotifyEvent(eventUserDetection, hasUser, this);
				rHandPosHist.clear();
				lHandPosHist.clear();
			}
		}

    }else if(ofGetMousePressed()){
        hasUser = true;
        ofNotifyEvent(eventUserDetection, hasUser, this);
        handsInfo.leftHand.pos.x = (float)ofGetMouseX() / (float)ofGetWidth();
        handsInfo.leftHand.pos.y = (float)ofGetMouseY() / (float)ofGetHeight();
        handsInfo.leftHand.pos.z = 1;
        handsInfo.rightHand.pos.x = ofMap(handsInfo.leftHand.pos.x, 0, 1, 1, 0);
        handsInfo.rightHand.pos.y = handsInfo.leftHand.pos.y;
        handsInfo.leftHand.pos.z = 1;
        computeVelocity(handsMeanSize);
		//cout << ofGetMouseX() << endl;
    }else{
        hasUser = false;
        ofNotifyEvent(eventUserDetection, hasUser, this);
        rHandPosHist.clear();
        lHandPosHist.clear();
    }
    
}
Example #16
0
void write_vtkFile(const char *szProblem,
                   int    t,
                   int * length,
                   double *collideField,
                   int * flagField) {
  
    int x, y, z;
    char szFileName[80];
    FILE *fp=NULL;
    int n[3] = {length[0] + 2,length[1] + 2,length[2] + 2};
    double velocity[3];
    double density;
    double * el = NULL;

    sprintf( szFileName, "%s.%i.vtk", szProblem, t );
    fp = fopen( szFileName, "w");
    if( fp == NULL )
    {
        char szBuff[80];
        sprintf( szBuff, "Failed to open %s", szFileName );
        ERROR( szBuff );
        return;
    }

    write_vtkHeader(fp, length);
    write_vtkPointCoordinates(fp, length);

    fprintf(fp,"POINT_DATA %i \n", length[0]*length[1]*length[2]);
    fprintf(fp,"\n");
    fprintf(fp, "VECTORS velocity float\n");
    for(z = 1; z  <= length[0]; z++) {
        for(y = 1; y  <= length[1]; y++) {
            for(x = 1; x  <= length[2]; x++) {
                if (*getFlag(flagField, x, y, z, n) != OBSTACLE) {
                    el = getEl(collideField, x, y, z, 0, n);
                    computeDensity(el, &density);
                    computeVelocity(el, &density, velocity);
                    fprintf(fp, "%f %f %f\n", velocity[0], velocity[1], velocity[2]);
                } else {
                    fprintf(fp, "%f %f %f\n", 0.0, 0.0, 0.0); 
                }
            }
        }
    }

    fprintf(fp,"\n");

    fprintf(fp, "SCALARS density float 1 \n"); 
    fprintf(fp, "LOOKUP_TABLE default \n");

    for(z = 1; z  <= length[0]; z++) {
        for(y = 1; y  <= length[1]; y++) {
            for(x = 1; x  <= length[2]; x++) {
                if (*getFlag(flagField, x, y, z, n) != OBSTACLE) {
                    computeDensity(getEl(collideField, x, y, z, 0, n), &density);
                    fprintf(fp, "%f\n", density);
                } else {
                    fprintf(fp, "%f\n", 1.0);
                }
            }
        }
    }

    if( fclose(fp) )
    {
        char szBuff[80];
        sprintf( szBuff, "Failed to close %s", szFileName );
        ERROR( szBuff );
    }
}
Example #17
0
void write_vtkFile(const char *szProblem, int t, int * length, double * collideField, int * my_pos, int my_rank, int * my_origin) {
    char szFileName[80];
    FILE *fp=NULL;
 
    int x, y, z;
    int node[3];
    int n[3] = { length[0] + 2, length[1] + 2, length[2] + 2 };
    double velocity[3];
    double density;
    double * el = NULL;

    sprintf( szFileName, "%s_%i.%i.vtk", szProblem, my_rank, t);
    fp = fopen( szFileName, "w");
    if( fp == NULL )		       
    {
        char szBuff[80];
        sprintf( szBuff, "Failed to open %s", szFileName );
        ERROR( szBuff );
        return;
    }

    write_vtkHeader(fp, length);
    write_vtkPointCoordinates(fp, length, my_pos, my_origin);

    fprintf(fp,"POINT_DATA %i \n", length[0] * length[1] * length[2]);
  
    fprintf(fp,"\n");
    fprintf(fp, "VECTORS velocity float\n");
    
    for(z = 1; z  <= length[2]; z++) {
        node[2] = z;
        for(y = 1; y  <= length[1]; y++) {
            node[1] = y;
            for(x = 1; x  <= length[0]; x++) {
                node[0] = x;
                el = getEl(collideField, node, 0, n);
                computeDensity(el, &density);
                computeVelocity(el, &density, velocity);
                fprintf(fp, "%f %f %f\n", velocity[0], velocity[1], velocity[2]);
            }
        }
    }

    fprintf(fp,"\n");

    fprintf(fp, "SCALARS density float 1 \n"); 
    fprintf(fp, "LOOKUP_TABLE default \n");

    for(z = 1; z <= length[2]; z++) {
        node[2] = z;
        for(y = 1; y <= length[1]; y++) {
            node[1] = y;
            for(x = 1; x <= length[0]; x++) {
                node[0] = x;
                computeDensity(getEl(collideField, node, 0, n), &density);
                fprintf(fp, "%f\n", density);
            }
        }
    }

    if( fclose(fp) )
    {
        char szBuff[80];
        sprintf( szBuff, "Failed to close %s", szFileName );
        ERROR( szBuff );
    }
}
Example #18
0
        void Player::draw(Canvas &canvas) {
            canvas.fillRect(x, y, x + WIDTH, y + HEIGHT);

            // update velocity
            computeVelocity();
        }
void fKinematicController::updateVelocities(float dt){
	_velocity.x = computeVelocity(_velocity.x, _acceleration.x, _drag.x, _maxVelocity.x, dt);
	_velocity.y = computeVelocity(_velocity.y, _acceleration.y, _drag.y, _maxVelocity.y, dt);
	_angularVelocity = computeVelocity(_angularVelocity, _angularAcceleration, _angularDrag, _maxAngularVelocity, dt);
}
Example #20
0
void writeVtkOutput(const double * const collideField, const int * const flagField, const char * filename, unsigned int t, int *xlength)
{
    int x, y, z, currentCellIndex;
    double cellVelocity[3], cellDensity;

    char szFileName[80];
    FILE *fp=NULL;
    sprintf( szFileName, "%s.%i.vtk", filename, t );
    fp = fopen( szFileName, "w");
    if( fp == NULL )
    {
        char szBuff[80];
        sprintf( szBuff, "Failed to open %s", szFileName );
        ERROR( szBuff );
        return;
    }

    write_vtkHeader( fp, xlength);
    write_vtkPointCoordinates(fp, xlength);

    fprintf(fp,"POINT_DATA %i \n", (xlength[0]) * (xlength[1]) * (xlength[2]));

    fprintf(fp,"\n");
    fprintf(fp, "VECTORS velocity float\n");
    for(z = 1; z <= xlength[2]; z++)
        for (y = 1; y <= xlength[1]; y++)
            for (x = 1; x <= xlength[0]; x++)
            {
                currentCellIndex = (z * (xlength[0] + 2) * (xlength[1] + 2) + y * (xlength[0] + 2) + x);
                computeDensity(collideField + currentCellIndex, &cellDensity, (xlength[0] + 2) * (xlength[1] + 2) * (xlength[2] + 2));
                computeVelocity(collideField + currentCellIndex, &cellDensity, cellVelocity, (xlength[0] + 2) * (xlength[1] + 2) * (xlength[2] + 2));
                fprintf(fp, "%f %f %f\n", cellVelocity[0], cellVelocity[1], cellVelocity[2]);
            }


    fprintf(fp,"\n");

    fprintf(fp, "SCALARS density float 1 \n");
    fprintf(fp, "LOOKUP_TABLE default \n");
    for(z = 1; z <= xlength[2]; z++)
        for(y = 1; y <= xlength[1]; y++)
            for (x = 1; x <= xlength[0]; x++)
            {
                currentCellIndex = (z * (xlength[0] + 2) * (xlength[1] + 2) + y * (xlength[0] + 2) + x);
                computeDensity(collideField + currentCellIndex, &cellDensity, (xlength[0] + 2) * (xlength[1] + 2) * (xlength[2] + 2));
                fprintf(fp, "%f\n", cellDensity);
            }
 fprintf(fp, "SCALARS boundaryType integer 1 \n");
    fprintf(fp, "LOOKUP_TABLE default \n");
    for(z = 1; z <= xlength[2] ; z++)
        for(y = 1; y <= xlength[1] ; y++)
            for (x = 1; x <= xlength[0] ; x++)
            {
                currentCellIndex = (z * (xlength[0] + 2) * (xlength[1] + 2) + y * (xlength[0] + 2) + x);
                fprintf(fp, "%d\n", flagField[currentCellIndex]);
            }



    if( fclose(fp) )
    {
        char szBuff[80];
        sprintf( szBuff, "Failed to close %s", szFileName );
        ERROR( szBuff );
    }
}
Example #21
0
void AI(double *X, double *Y, double *velX,double *velY)
{
C_SHOT tWork;
if(singlePlayerMode == 1 || bot == 1)
{
reset_1(&tWork);
Vector2D t1;
Vector2D strPos;
int y = 0;
int temporary = 0;

for(y = 0;y < 5 && temporary == 0; y++){
if(coinsLeft == 2 && f_queenPocketed == 0 && isQueenOnBoard == 1)
{
y=2;
temporary = 1;
}
int iP= 0;
int z=0;
for (z = 0; z < totalCoins ; z++){
double sepn = magnitude(coin[y].X - coin[z].X, coin[y].Y - coin[z].Y);
if((sepn <= coin[y].radius + coin[z].radius + 0.01f) && coin[z].visible == 1 && z != y)
{
iP = 1;
}
}
if(coin[y].visible == 1){

t1.X = coin[y].X;
t1.Y = coin[y].Y;
int loc = 0;
for(loc = 0;loc < 4; loc++){
Vector2D dir = subtract(&t1,&holes[loc]);
normalize(&dir);

Vector2D aimPos = backTrack(dir,&coin[y],&striker);
Vector2D range = getRange(y,loc,0);

double temp = range.Y;
strPos.Y = -2.1;
strPos.X = temp;
if(sweepTest1(t1,holes[loc],y) == 0){

Vector2D vel1 = computeVelocity(t1,holes[loc],NULL,&coin[y],t1);
if((coin[y].Y < -2.1 && loc >= 2) || (coin[y].Y > -2.1 && loc >= 0 && loc < 2)){
if((loc % 2 == 0 && loc < 2) || ((loc % 2 == 1 && loc >= 2)) ){
for( temp = range.Y; temp >= range.X ; temp -= 0.001f)
{
int ii =0;
double sepi = 0;
int invalidPosition= 0;
for (ii = 0; ii < totalCoins ; ii++){
sepi = magnitude(strPos.X - coin[ii].X, strPos.Y - coin[ii].Y);
if((sepi < striker.radius + coin[ii].radius) && coin[ii].visible == 1)
{
invalidPosition = 1;
}
}
if((sweepTest2(strPos,aimPos,y) == 0) && (invalidPosition == 0))
{
break;
}
strPos.X = temp;
}
}else
{
for( temp = range.Y; temp >= range.X ; temp += 0.001f)
{
int ii =0;
double sepi = 0;
int invalidPosition= 0;
for (ii = 0; ii < totalCoins ; ii++){
sepi = magnitude(strPos.X - coin[ii].X, strPos.Y - coin[ii].Y);
if((sepi < striker.radius + coin[ii].radius) && coin[ii].visible == 1)
{
invalidPosition = 1;
}
}
if((sweepTest2(strPos,aimPos,y) == 0) && (invalidPosition == 0))
{
break;
}
strPos.X = temp;
}
}
Vector2D dir2 = subtract(&strPos,&aimPos);
normalize(&dir2);
double acc = dotProduct(&dir,&dir2);
if(acc < 0) acc=-acc;
Vector2D fVel = computeVelocity(strPos,aimPos,&coin[y],&striker,vel1);
if((acc >= tWork.accuracy && acc > 0) || (coinsLeft == 2 && f_queenPocketed == 0 && isQueenOnBoard == 1))
{
tWork.accuracy = -acc;
tWork.X= strPos.X;
tWork.Y= strPos.Y;
tWork.vX = fVel.X;
tWork.vY = fVel.Y;
if(coinsLeft == 2 && f_queenPocketed == 0 && isQueenOnBoard == 1){

break;

}

}

}
}
}
}
}

}
double err = 0;
if(AI_level == 1){
err = (rand() % 100) / 1000.0f + 0.01f;
}else if(AI_level == 2)
{
err = (rand() % 1000) / 10000.0f + 0.001f;
}
*X = tWork.X + err;
*Y = tWork.Y;
*velX = tWork.vX;
*velY = tWork.vY;

}
Example #22
0
void writeVtkOutput(const double * const collideField, const int * const flagField, const char * filename, unsigned int t, int xlength) {
    char fn[80];
    int len = xlength+2;
    // save filename as a combination of passed filename and timestep
    sprintf(fn, "%s.%i.vtk", filename, t);
    
    FILE *fp = NULL;
    fp = fopen(fn, "w");
    if (fp == NULL) {
        ERROR("Failed to open file!");
        return;
    }

    // write header
    fprintf(fp, "# vtk DataFile Version 2.0\n");
    fprintf(fp, "generated by CFD-lab course output \n");
    fprintf(fp, "ASCII\n\n");
    fprintf(fp, "DATASET STRUCTURED_GRID\n");
    fprintf(fp, "DIMENSIONS %d %d %d \n", xlength, xlength, xlength);
    fprintf(fp, "POINTS %d float\n\n", (xlength) * (xlength) * (xlength));
   
    // print lattice points
    double step = 1.0 / (xlength - 1);
    for (double x = 0; x <= xlength-1; x+=1) {
        for (double y = 0; y <= xlength-1; y+=1) {
            for (double z = 0; z <= xlength-1; z+=1) {
                fprintf(fp, "%f %f %f\n", x*step, y*step, z*step);
            }
        }
    }

    double density;
    double vel[D];
    const double *currentCell;

    // write density data
    fprintf(fp, "\nPOINT_DATA %d \n", (xlength) * (xlength) * (xlength));
    fprintf(fp, "SCALARS density float 1 \n");
    fprintf(fp, "LOOKUP_TABLE default \n");
    
    for (int x = 1; x < xlength+1; ++x) {
        for (int y = 1; y < xlength+1; ++y) {
            for (int z = 1; z < xlength+1; ++z) {
                currentCell = collideField + Q*( z*len*len + y*len + x);
                computeDensity(currentCell, &density);
                fprintf(fp, "%f\n", density);
            }
        }
    }

    // compute velocities for all cells
    fprintf(fp, "\nVECTORS velocity float\n");
    
    for (int x = 1; x < xlength+1; ++x) {
        for (int y = 1; y < xlength+1; ++y) {
            for (int z = 1; z < xlength+1; ++z) {
                currentCell = collideField + Q*( z*len*len + y*len + x );
                computeDensity(currentCell, &density);
                computeVelocity(currentCell, &density, vel);
                fprintf(fp, "%f %f %f\n", vel[0], vel[1], vel[2]);
            }
        }
    }

    // close the file
    if (fclose(fp)) {
        ERROR("Failed to close file!");
        return;
    }
}