コード例 #1
0
ファイル: fractal.cpp プロジェクト: hjw3001/povray
bool Fractal::Inside(const Vector3d& IPoint, TraceThreadData *Thread) const
{
    bool Result;
    Vector3d New_Point;

    if (Trans != NULL)
    {
        MInvTransPoint(New_Point, IPoint, Trans);

        Result = Iteration(New_Point, this, Thread->Fractal_IStack);
    }
    else
    {
        Result = Iteration(IPoint, this, Thread->Fractal_IStack);
    }

    if (Test_Flag(this, INVERTED_FLAG))
    {
        return (!Result);
    }
    else
    {
        return (Result);
    }
}
コード例 #2
0
ファイル: image.c プロジェクト: edisonqkj/c
void Iteration(pIntStack stack, int ocolor, int ncolor){
	if(stack->Num>0){
		int y=IntPop(stack),
		    x=IntPop(stack);
		//printf("(%d,%d)\n",x,y);
		if(IsValid(x,y)){
			image[y][x]=ncolor;
			if(IsValid(x,y-1) && image[y-1][x]==ocolor){
				IntPush(stack,x);
				IntPush(stack,y-1);
			}
			if(IsValid(x,y+1) && image[y+1][x]==ocolor){
				IntPush(stack,x);
				IntPush(stack,y+1);
			}
			if(IsValid(x-1,y) && image[y][x-1]==ocolor){
				IntPush(stack,x-1);
				IntPush(stack,y);
			}
			if(IsValid(x+1,y) && image[y][x+1]==ocolor){
				IntPush(stack,x+1);
				IntPush(stack,y);
			}
			Iteration(stack,ocolor,ncolor);
		}
	}
}
コード例 #3
0
ファイル: image.c プロジェクト: edisonqkj/c
/// paint
void Paint(int x, int y, int color){
	printf("Position: (%d,%d)\n",x,y);
	printf("Repaint color %d to %d\n",image[y-1][x-1],color);
	pIntStack stack=(pIntStack)malloc(sizeof(IntStack));
	IntInit(stack);
	/// push start point
	IntPush(stack,x-1);
	IntPush(stack,y-1);
	Iteration(stack,image[y-1][x-1],color);
}
コード例 #4
0
ファイル: main.cpp プロジェクト: lucifer-wsp/yoo
void display(void)
{
    //清除颜色缓存和深度缓存
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1.0,1.0,0);
    glLoadIdentity();

    gluLookAt(1, -1, 3,   0, 0, 0,   0.4, -3, 0.5);        //视点转换
    float center[] = {0.0f, 0.0f, 0.0f};
    float radius = 2;
    float color[] = {1.0, 0.1, 0.0, 0};
    Square(center, radius, color);
    Iteration(center, radius,6);
    glutSwapBuffers();      //交换双缓存
}
コード例 #5
0
int main()
{
    Unit  *unit;
    srand( time( NULL ) );
    UnitCollection *c = new UnitCollection;
    Unit  *u[SIZE];
    time_t seconds;
    for (int i = 0; i < SIZE; ++i)
        u[i] = createUnit();
    seconds = time( NULL );
    for (int i = 0; i < (SIZE/2); ++i)
        c->prepend( u[i] );
    seconds = time( NULL )-seconds;
    printf( "constructed list of size : %d  in %d seconds using prepend\n", SIZE/2, seconds );
    printf( "Randomnly inserting %d \n", SIZE/2 );
    int ii = SIZE/2;
    seconds = time( NULL );
    while (ii < SIZE)
        for (un_iter iter = c->createIterator(); !iter.isDone() && ii < SIZE; ++iter) {
            int rnd = rand();
            if (rnd < RAND_MAX/200) {
                iter.postinsert( u[ii] );
                ++ii;
            } else if (rnd < RAND_MAX/100) {
                iter.preinsert( u[ii] );
                ++ii;
            }
        }
    seconds = time( NULL )-seconds;
    printf( ".... took %d seconds \n", seconds );
    for (int i = 0; i < SIZE/32; ++i)
        if (rand() < RAND_MAX/20)
            u[i]->Kill();
    printf( "randomly killed SIZE/32 to start off with \n" );

    printf( "beginning unitCollection removal/advance operations\n" );
    seconds = time( NULL );
    int passes = 0;
    int levels = 0;
    while ( !c->empty() ) {
        ++passes;
        Iteration( c, &levels );
#if !oldtest
    }
#else
        UnitCollection::FreeUnusedNodes();
    }
コード例 #6
0
ファイル: main-02.c プロジェクト: yas4891/MUTEX
void Mandelbrot(tComplex k)
{
    int x,y,wdh;
    tComplex neu = k;
    for (y=0; y<SIZE_Y; y++)
    {
        k.Im += 0.004;
        for (x=0; x<SIZE_X; x++)
        {
            k.Re += 0.004;
            wdh = Iteration(k);
            PutPixel(x,y,colorList[wdh]);

        }
        k.Re = neu.Re;
    }
}
コード例 #7
0
void Iteration( UnitCollection *c, int *levels2 )
{
    Unit *unit = NULL;
    ++(*levels2);
    for (un_iter iter = c->createIterator(); unit = *iter;) {
        int temp = rand();
        if (temp < RAND_MAX/400) {
            unit->Kill();
        } else if (temp < RAND_MAX/102) {
            iter.remove();
            continue;
        } else if (temp < RAND_MAX/90) {
            Iteration( c, levels2 );
        }
        ++iter;
    }
}
コード例 #8
0
ファイル: main.cpp プロジェクト: lucifer-wsp/yoo
void Iteration(float center[3], float size, int nIter)
{
    if(nIter<0)return;

    SurroundSquare(center, size, colortab[nIter]);

    float halfcenter[6][3] =
    {
        center[0] + size * 3/4.0, center[1], center[2],
        center[0] - size * 3/4.0, center[1], center[2],
        center[0] , center[1] + size * 3/4.0, center[2],
        center[0] , center[1] - size * 3/4.0, center[2],
        center[0] , center[1], center[2] + size * 3/4.0,
        center[0] , center[1], center[2] - size * 3/4.0,
    };

    for (int i=0; i<6; i++)
    {
        Iteration(halfcenter[i], size/2, nIter-1);
    }
}
コード例 #9
0
ファイル: fractal.cpp プロジェクト: hjw3001/povray
bool Fractal::All_Intersections(const Ray& ray, IStack& Depth_Stack, TraceThreadData *Thread)
{
    bool Intersection_Found;
    bool LastIsInside = false;
    bool CurrentIsInside, NextIsInside;
    DBL Depth, Depth_Max;
    DBL Dist, Dist_Next, LenSqr, LenInv;

    Vector3d IPoint, Mid_Point, Next_Point, Real_Pt;
    Vector3d Real_Normal, F_Normal;
    Vector3d Direction;
    BasicRay New_Ray;

    Thread->Stats()[Ray_Fractal_Tests]++;

    Intersection_Found = false;

    /* Get into Fractal's world. */

    if (Trans != NULL)
    {
        MInvTransDirection(Direction, ray.Direction, Trans);
        LenSqr = Direction.lengthSqr();

        if (LenSqr == 0.0)
        {
            return (false);
        }

        if (LenSqr != 1.0)
        {
            LenInv = 1.0 / sqrt(LenSqr);
            Direction *= LenInv;
        }
        else
            LenInv = 1.0;

        New_Ray.Direction = Direction;
        MInvTransPoint(New_Ray.Origin, ray.Origin, Trans);
    }
    else
    {
        Direction = ray.Direction;
        New_Ray = ray;
        LenInv = 1.0;
    }

    /* Bound fractal. */

    if (!F_Bound(New_Ray, this, &Depth, &Depth_Max))
    {
        return (false);
    }

    if (Depth_Max < Fractal_Tolerance)
    {
        return (false);
    }

    if (Depth < Fractal_Tolerance)
    {
        Depth = Fractal_Tolerance;
    }

    /* Jump to starting point */

    Next_Point = New_Ray.Origin + Direction * Depth;

    CurrentIsInside = D_Iteration(Next_Point, this, Direction, &Dist, Thread->Fractal_IStack);

    /* Light ray starting inside ? */

    if (CurrentIsInside)
    {
        Next_Point += (2.0 * Fractal_Tolerance) * Direction;

        Depth += 2.0 * Fractal_Tolerance;

        if (Depth > Depth_Max)
        {
            return (false);
        }

        CurrentIsInside = D_Iteration(Next_Point, this, Direction, &Dist, Thread->Fractal_IStack);
    }

    /* Ok. Trace it */

    while (Depth < Depth_Max)
    {
        /*
         * Get close to the root: Advance with Next_Point, keeping track of last
         * position in IPoint...
         */

        while (1)
        {
            if (Dist < Precision)
                Dist = Precision;

            Depth += Dist;

            if (Depth > Depth_Max)
            {
                if (Intersection_Found)
                    Thread->Stats()[Ray_Fractal_Tests_Succeeded]++;
                return (Intersection_Found);
            }

            IPoint = Next_Point;
            Next_Point += Dist * Direction;

            NextIsInside = D_Iteration(Next_Point, this, Direction, &Dist_Next, Thread->Fractal_IStack);

            if (NextIsInside != CurrentIsInside)
            {
                /* Set surface was crossed... */

                Depth -= Dist;
                break;
            }
            else
            {
                Dist = Dist_Next; /* not reached */
            }
        }

        /* then, polish the root via bisection method... */

        while (Dist > Fractal_Tolerance)
        {
            Dist *= 0.5;
            Mid_Point = IPoint + Dist * Direction;

            LastIsInside = Iteration(Mid_Point, this, Thread->Fractal_IStack);

            if (LastIsInside == CurrentIsInside)
            {
                IPoint = Mid_Point;

                Depth += Dist;

                if (Depth > Depth_Max)
                {
                    if (Intersection_Found)
                        Thread->Stats()[Ray_Fractal_Tests_Succeeded]++;
                    return (Intersection_Found);
                }
            }
        }

        if (!CurrentIsInside) /* Mid_Point isn't inside the set */
        {
            IPoint += Dist * Direction;

            Depth += Dist;

            Iteration(IPoint, this, Thread->Fractal_IStack);
        }
        else
        {
            if (LastIsInside != CurrentIsInside)
            {
                Iteration(IPoint, this, Thread->Fractal_IStack);
            }
        }

        if (Trans != NULL)
        {
            MTransPoint(Real_Pt, IPoint, Trans);
            Normal_Calc(this, F_Normal, Thread->Fractal_IStack);
            MTransNormal(Real_Normal, F_Normal, Trans);
        }
        else
        {
            Real_Pt = IPoint;
            Normal_Calc(this, Real_Normal, Thread->Fractal_IStack);
        }

        if (Clip.empty() || Point_In_Clip(Real_Pt, Clip, Thread))
        {
            Real_Normal.normalize();
            Depth_Stack->push(Intersection(Depth * LenInv, Real_Pt, Real_Normal, this));
            Intersection_Found = true;

            /* If fractal isn't used with CSG we can exit now. */

            if (!(Type & IS_CHILD_OBJECT))
            {
                break;
            }
        }

        /* Start over where work was left */

        IPoint = Next_Point;
        Dist = Dist_Next;
        CurrentIsInside = NextIsInside;

    }

    if (Intersection_Found)
        Thread->Stats()[Ray_Fractal_Tests_Succeeded]++;
    return (Intersection_Found);
}
コード例 #10
0
TPicResult
TPXPictureValidator::Scan(LPTSTR input, uint termCh, uint& i, uint& j)
{
  tchar ch;
  TPicResult rslt = prEmpty;

  uint len = ::_tcslen(input);
  while (i != termCh && Pic[i] != _T(',')) {
    if (j >= len)
      return CheckComplete(termCh, i, rslt);

    ch = input[j];
    switch (Pic[i]) {
      case _T('#'):
        if (!_istdigit((tchar)ch))
          return prError;
        else {
          input[j++] = ch;
          i++;
        }
        break;
      case _T('?'):
        if (!_istalpha((tchar)ch))
          return prError;
        else {
          input[j++] = ch;
          i++;
        }
        break;
      case _T('&'):
        if (!_istalpha((tchar)ch))
          return prError;
        else {
          input[j++] = (tchar)_totupper(ch);
          i++;
        }
        break;
      case _T('!'): {
#if defined(BI_DBCS_SUPPORT) 
        uint n = CharSize(&input[j]) / sizeof(tchar);
        if (j + n >= len)
          j = len;
        else{
          if (n == 1)
            input[j++] = (tchar)_totupper((tchar)ch);
          else
            j += n;
        }
#else
        input[j++] = (tchar)_totupper(ch);
#endif
        i++;
        break;
      }
      case _T('@'): {
#if defined(BI_DBCS_SUPPORT)
        uint n = CharSize(&input[j]) / sizeof(tchar);
        if (j + n >= len)
          j = len;
        else
          j += n;
#else
        input[j++] = ch;
#endif
        i++;
        break;
      }
      case _T('*'):
        rslt = Iteration(input, termCh, i, j);
        if (!IsComplete(rslt))
          return rslt;
        if (rslt == prError)
          rslt = prAmbiguous;
        break;
      case _T('{'):
        rslt = Group(input, termCh, i, j);
        if (!IsComplete(rslt))
          return rslt;
        break;
      case _T('['):
        rslt = Group(input, termCh, i, j);
        if (IsIncomplete(rslt))
          return rslt;
        if (rslt == prError)
          rslt = prAmbiguous;
        break;
      default: {
#if defined(BI_DBCS_SUPPORT)
#if defined(BI_PDOXWINJ_SUPPORT)
        // Paradox for Windows/J database program has two special picture to
        // support Japanese characters in CodePage 932
        //
        // '��' 0x81+0x93 - (2 byte '%' symbol)
        //      1 byte KATAKANA and KATAKANA symbols (0xA1 - 0xDF)
        // '��' 0x81+0x97 - (2 byte '@' symbol)
        //      any 2 byte characters except 2 byte space (0x81+0x40)
        //
        // This is hard coded, because we don't know how to get current
        // code page in Windows 3.1
        //
        uint n  = CharSize(&input[j]) / sizeof(tchar);
        uint n2 = CharSize(((const char *))Pic.c_str() + i) / sizeof(tchar);
        if (n2 == 2) {
          utchar uc1, uc2;
          uc1 = (utchar)Pic[i];
          uc2 = (utchar)Pic[i+1];
          if (uc1 == 0x81 && uc2 == 0x93) {
            if ((utchar)ch >= 0xA1 && (utchar)ch <= 0xDF){
              i += n2;
              j += n;
              break;
            }
            else
              return prError;
          } 
          else if (uc1 == 0x81 && uc2 == 0x97){
            if (n == 2 && j + n < len &&
                ((utchar)ch != 0x81 || (utchar)input[j+1] != 0x40)) {
              i += n2;
              j += n;
              break;
            } else
              return prError;
          }
        }
        if (n2 == 1 && Pic[i] == ';'){
          i++;
          n2 = CharSize((const char *)Pic.c_str() + i) / sizeof(tchar);
        }
#else
        if (Pic[i] == _T(';'))
          i++;
        uint n  = CharSize(&input[j]) / sizeof(tchar);
        uint n2 = CharSize((LPCTSTR)Pic.c_str() + i) / sizeof(tchar);
#endif
        if (j + n >= len)
          n = len - j;
        if (n == 1) {
          if (ch == _T(' ')) {
#if defined(BI_AUTO_COMPLETION_DBCS_BY_SPACE)
// But, couldn't expand input buffer TValidator classes.
//
            if (n < n2) {
              memmove(input+n2, input+n, len-n+1);
              len += n2 - n;
              n = n2;
            }
            while (n-- > 0)
              input[j++] = Pic[i++];
#else
            if (n != n2)
              return prError;
            input[j++] = Pic[i++];
#endif
          }
          else {
            if (n != n2)
              return prError;
            if (_totupper((tchar)Pic[i]) != _totupper((tchar)ch))
              return prError;
            input[j++] = Pic[i++];
          }
        }
        else {
          if (n > n2)
            return prError;
          for (uint i1 = 0; i1 < n; i1++)
            if (input[j+i1] != Pic[i+i1])
              return prError;
          while (n-- > 0)
            input[j++] = Pic[i++];
        }
#else
        if (Pic[i] == _T(';'))
          i++;
        if (_totupper(Pic[i]) != _totupper(ch))
          if (ch == _T(' '))
            ch = Pic[i];
          else
            return prError;
        input[j++] = Pic[i];
        i++;
#endif
      }
    }
    if (rslt == prAmbiguous)
      rslt = prIncompNoFill;
    else
      rslt = prIncomplete;
  }

  return (rslt == prIncompNoFill) ? prAmbiguous : prComplete;
}
コード例 #11
0
/**迭代控制次数*/
void Garment::numIteration(int number,NetGrid * netGrid){
	//int num = 0;
	while(!Iteration(netGrid)){
		//num++;
	}
	//num++;

	int i = numdiedadi;
	while(i--){
		CollisonIteration(netGrid);
	}


	/*//////////////////////////////////////////////////////////////////////////////////////////////////////

										读衣服的下半部分

	///////////////////////////////////////////////////////////////////////////////////////////////////////*/
	
	//FILE *dataFile1d;				//读入的文件showMass_body_down
	//dataFile1d = fopen("bodydown_out.txt", "w");
	//
	//fprintf(dataFile1d,"#  ----------------------------------------------------------\n");

	//fprintf(dataFile1d,"#  Num verts:  %d\n",showMass_body_down.size()*showMass_body_down.at(0)->size()+showMass_body_down.size());
	//fprintf(dataFile1d,"#  Num rc: %d %d\n",showMass_body_down.size(),showMass_body_down.at(0)->size()+1);
	//fprintf(dataFile1d,"#  Num tElems: 8\n");

	//for(int i = 0; i < showMass_body_down.size(); i++)			//长
	//{
	//	for(int j = 0; j < showMass_body_down.at(i)->size(); j++)		//宽
	//	{
	//		fprintf(dataFile1d,"v %f %f %f\n",showMass_body_down.at(i)->at(j)->x,showMass_body_down.at(i)->at(j)->y,showMass_body_down.at(i)->at(j)->z);
	//	}
	//	fprintf(dataFile1d,"v %f %f %f\n",showMass_body_down.at(i)->at(0)->x,showMass_body_down.at(i)->at(0)->y,showMass_body_down.at(i)->at(0)->z);
	//}


	//fclose(dataFile1d);


	/*//////////////////////////////////////////////////////////////////////////////////////////////////////

										读衣服的中间前半部分

	///////////////////////////////////////////////////////////////////////////////////////////////////////*/
	
	
	//FILE *dataFile1centerf;				//读入的文件showMass_body_centerf
	//dataFile1centerf = fopen("bodycenterf_out.txt", "w");
	//
	//fprintf(dataFile1centerf,"#  ----------------------------------------------------------\n");

	//fprintf(dataFile1centerf,"#  Num verts:  %d\n",showMass_body_centerf.size()*showMass_body_centerf.at(0).size());
	//fprintf(dataFile1centerf,"#  Num rc: %d %d\n",showMass_body_centerf.size(),showMass_body_centerf.at(0).size());
	//fprintf(dataFile1centerf,"#  Num tElems: 8\n");

	//for(int i = 0; i < showMass_body_centerf.size(); i++)			//长
	//{
	//	for(int j = 0; j < showMass_body_centerf.at(i).size(); j++)		//宽
	//	{
	//		fprintf(dataFile1centerf,"v %f %f %f\n",showMass_body_centerf.at(i).at(j)->x,showMass_body_centerf.at(i).at(j)->y,showMass_body_centerf.at(i).at(j)->z);
	//	}
	//}


	//fclose(dataFile1centerf);

	/*//////////////////////////////////////////////////////////////////////////////////////////////////////

										读衣服的中间前半部分

	///////////////////////////////////////////////////////////////////////////////////////////////////////*/
	
	//FILE *dataFile1centerb;				//读入的文件showMass_body_centerf
	//dataFile1centerb = fopen("bodycenterb_out.txt", "w");
	//
	//fprintf(dataFile1centerb,"#  ----------------------------------------------------------\n");

	//fprintf(dataFile1centerb,"#  Num verts:  %d\n",showMass_body_centerb.size()*showMass_body_centerb.at(0).size());
	//fprintf(dataFile1centerb,"#  Num rc: %d %d\n",showMass_body_centerb.size(),showMass_body_centerb.at(0).size());
	//fprintf(dataFile1centerb,"#  Num tElems: 8\n");

	//for(int i = 0; i < showMass_body_centerb.size(); i++)			//长
	//{
	//	for(int j = 0; j < showMass_body_centerb.at(i).size(); j++)		//宽
	//	{
	//		fprintf(dataFile1centerb,"v %f %f %f\n",showMass_body_centerb.at(i).at(j)->x,showMass_body_centerb.at(i).at(j)->y,showMass_body_centerb.at(i).at(j)->z);
	//	}
	//}


	//fclose(dataFile1centerb);


	

	/*//////////////////////////////////////////////////////////////////////////////////////////////////////

										读左手臂

	///////////////////////////////////////////////////////////////////////////////////////////////////////*/
	
	//FILE *dataFile2;				//读入的文件
	//dataFile2 = fopen("arml_out.txt", "w");
	//
	//fprintf(dataFile2,"#  ----------------------------------------------------------\n");

	//fprintf(dataFile2,"#  Num verts:  %d\n",showMass_larm.size()*showMass_larm.at(0).size()+showMass_rarm.size());
	//fprintf(dataFile2,"#  Num rc: %d %d\n",showMass_larm.size(),showMass_larm.at(0).size()+1);
	//fprintf(dataFile2,"#  Num tElems: 8\n");

	//for(int i = 0; i < showMass_larm.size(); i++)			//长
	//{
	//	for(int j = 0; j < showMass_larm.at(i).size(); j++)		//宽
	//	{
	//		fprintf(dataFile2,"v %f %f %f\n",showMass_larm.at(i).at(j)->x,showMass_larm.at(i).at(j)->y,showMass_larm.at(i).at(j)->z);
	//	}
	//	fprintf(dataFile2,"v %f %f %f\n",showMass_larm.at(i).at(0)->x,showMass_larm.at(i).at(0)->y,showMass_larm.at(i).at(0)->z);
	//}


	//fclose(dataFile2);



	/*//////////////////////////////////////////////////////////////////////////////////////////////////////

										读右手臂

	///////////////////////////////////////////////////////////////////////////////////////////////////////*/
	
	//FILE *dataFile3;				//读入的文件
	//dataFile3 = fopen("armr_out.txt", "w");
	//
	//fprintf(dataFile3,"#  ----------------------------------------------------------\n");

	//fprintf(dataFile3,"#  Num verts:  %d\n",showMass_rarm.size()*showMass_rarm.at(0).size()+showMass_rarm.size());
	//fprintf(dataFile3,"#  Num rc: %d %d\n",showMass_rarm.size(),showMass_rarm.at(0).size()+1);
	//fprintf(dataFile3,"#  Num tElems: 8\n");

	//for(int i = 0; i < showMass_rarm.size(); i++)			//长
	//{
	//	for(int j = 0; j < showMass_rarm.at(i).size(); j++)		//宽
	//	{
	//		fprintf(dataFile3,"v %f %f %f\n",showMass_rarm.at(i).at(j)->x,showMass_rarm.at(i).at(j)->y,showMass_rarm.at(i).at(j)->z);
	//	}
	//	fprintf(dataFile3,"v %f %f %f\n",showMass_rarm.at(i).at(0)->x,showMass_rarm.at(i).at(0)->y,showMass_rarm.at(i).at(0)->z);
	//}


	//fclose(dataFile3);

}
コード例 #12
0
Simulation::Simulation(double perchvalue, double speedvalue,double maxchangeanglevalue) {

    std::string savevalue = make_directory("/Users/student/Documents/Bats/Simulations/Run23Oct2013", perchvalue, speedvalue, maxchangeanglevalue);
    std::string returnvalue = make_directory("/Users/student/Dropbox/SimulationReached", perchvalue, speedvalue, maxchangeanglevalue);


    //-------------------------------------------------------
    // Creates files for saving - Including headings
    //--------------------------------------------------------
    //Creates file for Sensors (CSV file) and writes in the header
    std::ofstream Sensors;
    Sensors.open(make_filename(savevalue, ",Sensors.csv" ).c_str());
    Sensors << "ID" <<
            "," << "X location" <<
            "," << "Y location" <<
            "," << "CentreAngle" <<
            "," << "HalfWidthAngle" <<
            "," << "Radius" <<
            "\n";

    //Creates file for Captures (CSV file) and writes in the header
    std::ofstream CapturesNotRef;
    std::ofstream &Captures = CapturesNotRef;
    Captures.open(make_filename(savevalue, ",Captures.csv" ).c_str());
    Captures << "AnimalNumber" <<
             "," << "Time_step" <<
             "," << "SensorID" <<
             "," << "Iteration number" <<
             "," << "location_x_animal" <<
             "," << "location_y_animal" <<
             "," << "time" <<
             "," << "angle from Camera To Animal" <<
             "," << "angle from Animal to Camera" <<
             "," << "DistToCam" <<
             "\n";

    //Creates file for Movement (CSV file) and writes in the header
    std::ofstream MovementNotRef;
    std::ofstream &Movement = MovementNotRef;
    Movement.open(make_filename(savevalue, ",Movement.csv" ).c_str());
    Movement << "AnimalNumber" <<
             "," << "StepNumber" <<
             "," << "Xlocation" <<
             "," << "Ylocation" <<
             "," << "Angle" <<
             "," << "TotalDistance" <<
             "," << "Speed" <<
             "," << "Re-enterWorld" <<
             "," << "Dist" <<
             "," << "Iternation number" <<
             "\n";

    // Saves the settings used
    std::ofstream Settings;
    Settings.open(make_filename(savevalue,",Settings.csv").c_str());
    //Simulation values - #Animals, #Steps,  #CT
    Settings << "DensityAnimals" << ","<< DensityAnimals << "\n"
             << "NoOfAnimals" << "," << NoAnimal << "\n"
             << "Area"<< "," << area << "\n"
             << "LengthMonitoring" << "," << LengthMonitoring << "\n"
             << "NoOfIterations" << "," << NoOfIterations << "\n"
             << "NoSteps"   << "," << NoSteps << "\n"
             << "StepLength"   << "," << StepLength << "\n"
             << "Seed" << "," << Seed << "\n"
             << "Sq_MinX" << ","<<  Sq_MinX << "\n"
             << "Sq_MaxX" << ","<<  Sq_MaxX << "\n"
             << "Sq_MinY" << ","<<  Sq_MinY << "\n"
             << "Sq_MaxY" << ","<<  Sq_MaxY << "\n"
             << "CorrWalkMaxAngleChange" << ","<<   maxchangeanglevalue<< "\n"
             << "AnimalSpeed" << ","<< speedvalue<< "\n"
             << "Perch"<<","<< perchvalue<<"\n"
             ;
    //Closes file
    Settings.close();

    //--------------------------------------------------------------------------------------------------
    //
    //                                 !!! WARNINGS && TESTS !!!
    //
    // Will automatically stop the simulation if certain conditions they aren't satisfied.
    // Runs automated unit tests on:
    //     * Animal
    //     * Sensor
    //
    //---------------------------------------------------------------------------------------------------


    //Number of animals needs to be greater than zero
    if(NoAnimal<=0) {
        std::cout<<"No of Animals = "<<NoAnimal<< ", Increase density"<< std::endl;
        exit (EXIT_FAILURE);
    };
    // No steps
    if(NoSteps==0) {
        std::cout<<"No steps, Increase Length of monitoring"<< std::endl;
        exit (EXIT_FAILURE);
    };


    //--------------------------------------------------------------------------------------------------
    //                                          Creating sensors
    //
    // Creates Sensors as they do not change location when the movement changes
    //  - only after the set up parameters change (change in length of study, etc)
    // Saves the locations in a CSV file
    //---------------------------------------------------------------------------------------------------
    //Creates a list of pointers to the Sensors

    std::vector<Sensor*> AllSensors(NoSensors);

    int sensorcount(0);
    for(int sensor1 =0; sensor1<LengthSW; sensor1 ++) {
        for(int sensor=0; sensor<LengthSR; sensor++) {

            AllSensors[sensorcount] =new Sensor(sensorcount,SensorWidth[sensor1],SensorRadius[sensor]);


            if(SaveSensor==1) {
                //Saves the locations and the angle of the Sensor
                Sensors << AllSensors[sensorcount] -> getID() << //1st column
                        "," << AllSensors[sensorcount] -> getXloc() << //2nd column
                        "," << AllSensors[sensorcount] -> getYloc() << //...
                        "," << AllSensors[sensorcount] -> getAngle() << //...
                        "," << AllSensors[sensorcount] -> getHalfAngle() << //4th column
                        "," << AllSensors[sensorcount] -> getRadius() << //4th column
                        "\n";
                sensorcount +=1;
            };
        };
    };
    //Closes the csv Sensor file
    Sensors.close();

    //------------------------------------------------------------------------------------------------
    //                                      Iteration Loop
    //
    // Starts a loop for the rest of the code, this loop "NoOfIterations" number of times
    // Each set of movement traces is saved where the file name ends with the number
    // The seed that starts all each simulation is the number of the simulation plus the start seed
    // This means that can run simulations in blocks 1-10 then 11-20 etc
    //------------------------------------------------------------------------------------------------
    for(int iterationnumber=Seed; iterationnumber<Seed + NoOfIterations; iterationnumber++) {

        Iteration(AllSensors, Captures, Movement, iterationnumber, speedvalue, perchvalue, maxchangeanglevalue);

    };//End of iteration

    // Closes files that are open in all iterations
    Captures.close();
    Movement.close();
    CapturesNotRef.close();
    MovementNotRef.close();

    std::ofstream Return;
    Return.open(make_filename(returnvalue, ",finished.csv" ).c_str());
    Return.close();

    for(int i=0; i<NoSensors; i++) {
        delete AllSensors[i];
    };
};
コード例 #13
0
ファイル: Murphy.hpp プロジェクト: Adrien81/XCSoar
  void Wideline(int x1, int y1, int x2, int y2, uint8_t width, uint8_t miter) {
    assert(x1 != x2 || y1 != y2);

    float offset = (float)width / 2.f;

    /* Initialisation */
    u = x2 - x1; /* delta x */
    v = y2 - y1; /* delta y */

    if (u < 0) {
      /* swap to make sure we are in quadrants 1 or 4 */
      std::swap(x1, x2);
      std::swap(y1, y2);
      u *= -1;
      v *= -1;
    }

    if (v < 0) {
      /* swap to 1st quadrant and flag */
      v *= -1;
      quad4 = true;
    } else {
      quad4 = false;
    }

    if (v > u) {
      /* swap things if in 2 octant */
      std::swap(u, v);
      oct2 = 1;
    } else {
      oct2 = 0;
    }

    ku = u + u; /* change in l for square shift */
    kv = v + v; /* change in d for square shift */
    kd = kv - ku; /* change in d for diagonal shift */
    kt = u - kv; /* diag/square decision threshold */

    /* difference terms d0=perpendicular to line, d1=along line */
    int d0 = 0, d1 = 0;

    /* distance along line */
    int dd = 0;

    /* angle for initial point calculation */
    const double ang = atan((double) v / (double) u);
    const double sang = sin(ang);
    const double cang = cos(ang);

    int ptx, pty;
    if (oct2 == 0) {
      ptx = x1 + (int)lrint(offset * sang);
      if (!quad4) {
        pty = y1 - (int)lrint(offset * cang);
      } else {
        pty = y1 + (int)lrint(offset * cang);
      }
    } else {
      ptx = x1 - (int)lrint(offset * cang);
      if (!quad4) {
        pty = y1 + (int)lrint(offset * sang);
      } else {
        pty = y1 - (int)lrint(offset * sang);
      }
    }

    /* thickness threshold: used here for constant thickness line */
    const int tk = int(4. * hypot(ptx - x1, pty - y1) * hypot(u, v));

    if (miter == 0) {
      first1x = -32768;
      first1y = -32768;
      first2x = -32768;
      first2y = -32768;
      last1x = -32768;
      last1y = -32768;
      last2x = -32768;
      last2y = -32768;
    }

    /* outer loop, stepping perpendicular to line */
    int ml1x, ml1y, ml2x, ml2y, ml1bx, ml1by, ml2bx, ml2by;
    for (unsigned q = 0; dd <= tk; q++) {

      /* call to inner loop - right edge */
      Paraline(ptx, pty, d1);
      if (q == 0) {
        ml1x = ptx;
        ml1y = pty;
        ml1bx = tempx;
        ml1by = tempy;
      } else {
        ml2x = ptx;
        ml2y = pty;
        ml2bx = tempx;
        ml2by = tempy;
      }

      if (d0 < kt) {
        /* square move */
        if (oct2 == 0) {
          if (!quad4) {
            pty++;
          } else {
            pty--;
          }
        } else {
          ptx++;
        }
      } else {
        /* diagonal move */
        dd += kv;
        d0 -= ku;
        if (d1 < kt) {
          /* normal diagonal */
          if (oct2 == 0) {
            ptx--;
            if (!quad4) {
              pty++;
            } else {
              pty--;
            }
          } else {
            ptx++;
            if (!quad4) {
              pty--;
            } else {
              pty++;
            }
          }
          d1 += kv;
        } else {
          /* double square move, extra parallel line */
          if (oct2 == 0) {
            ptx--;
          } else {
            if (!quad4) {
              pty--;
            } else {
              pty++;
            }
          }
          d1 += kd;
          if (dd > tk) {
            Iteration(miter,
                      ml1bx, ml1by, ml2bx, ml2by,
                      ml1x, ml1y, ml2x, ml2y);
            /* breakout on the extra line */
            return;
          }
          Paraline(ptx, pty, d1);
          if (oct2 == 0) {
            if (!quad4) {
              pty++;
            } else {

              pty--;
            }
          } else {
            ptx++;
          }
        }
      }
      dd += ku;
      d0 += kv;
    }

    Iteration(miter, ml1bx, ml1by, ml2bx, ml2by, ml1x, ml1y, ml2x, ml2y);
  }