Exemple #1
0
Space::Space(unsigned int amount, unsigned int size, bool rare, FigureType figure) : figure_(figure)
{
    if(rare)
    {
        for(unsigned int i = 0;i<amount;++i)
        {
            /* produce points for square size 2*size*amount x 2*size*amount */
            int x = getRandomNumber(0,amount*size);
            int y = getRandomNumber(0,amount*size);
            Point randomPoint(x,y);
            points.push_back(randomPoint);
        }
    }
    else
    {
        int partChooser = getRandomNumber(0,3);
        Point offset;
        switch(partChooser)
        {
            case 0:
                offset.x = 0;
                offset.y = 0;
            break;
            case 1:
                offset.x = amount*size/2;
                offset.y = 0;
            break;
            case 2:
                offset.x = 0;
                offset.y = amount*size/2;
            break;
            case 3:
                offset.x = amount*size/2;
                offset.y = amount*size/2;
            break;
        }
        int notRareAmount = amount/4;
        int x = getRandomNumber(0,notRareAmount*size);
        for(unsigned int i = 0;i<notRareAmount;++i)
        {
            int y = getRandomNumber(0,notRareAmount*size);
            Point randomPoint(x,y);
            randomPoint = randomPoint + offset;
            points.push_back(randomPoint);
        }
        int restAmount = amount-notRareAmount;
        for(unsigned int i = 0;i<restAmount;++i)
        {
            x = getRandomNumber(notRareAmount*size,restAmount*size);
            int y = getRandomNumber(notRareAmount*size,restAmount*size);
            Point randomPoint(x,y);
            points.push_back(randomPoint);
        }
    }
}
void Bullets::runEnemyAction(){
    
    size = CCDirector::sharedDirector()->getWinSize();
    CCJumpTo* jump = CCJumpTo::create(0.8f, randomPoint(), 50, 1);
    CCFadeTo* fade = CCFadeTo::create(1.0f, 0.0f);
    CCSequence* seq = CCSequence::create(jump,fade,NULL);
    
    this->runAction(seq);
}
std::vector<double> RandomGlobalPoll::nextPoint() {

    if(hasConverged())
        return min_element ( population.begin(),population.end() )->point();

    if ( population.size() < dimPopul ) {
        ++timesInitialPopulation;
        return randomPoint();
    }

    return computeFamilyStepPoint ( std::min ( bounds.size()+1,population.size()-1 ) );

}
Exemple #4
0
    void init(const Graph<P, W, H>& graph)
    {
        _embedding.clear();

        for (const auto& n: graph._nodes)
        {
            _embedding.insert(
                std::pair<const Node<P, W, H>*, 
                          EuclidianVector<T, N>>
                    (&*n, randomPoint()));
        }

        initDilatation();
    }
void testAABB()
{

   static const math::Vector3<real_t> ZERO( real_t( 0 ), real_t( 0 ), real_t( 0 ) );
   static const math::Vector3<real_t> UNIT( real_t( 1 ), real_t( 1 ), real_t( 1 ) );
   static const real_t EPSILON = real_t(1e-4);

   boost::random::mt19937 randomEngine;

   std::vector<math::AABB> testAABBs;
   testAABBs.push_back( math::AABB( -UNIT, UNIT ) );
   testAABBs.push_back( math::AABB(  ZERO, UNIT ) );
   testAABBs.push_back( math::AABB( -UNIT, ZERO ) );

   for( auto aabbIt = testAABBs.begin(); aabbIt != testAABBs.end(); ++aabbIt )
   {
      const math::AABB outerAABB = aabbIt->getScaled( real_t( 2 ) );
      std::vector< std::pair< Vector3<real_t>, Vector3<real_t> > > testPoints;

      for( int i = 0; i < 100; ++i )
      {
         Vector3<real_t> outerPoint, innerPoint;
         do { outerPoint = outerAABB.randomPoint( randomEngine ); } while( aabbIt->contains( outerPoint ) );
         innerPoint = aabbIt->randomPoint( randomEngine );
         testPoints.push_back( std::make_pair( outerPoint, innerPoint - outerPoint ) );
      }
      
      for( auto pointIt = testPoints.begin(); pointIt != testPoints.end(); ++pointIt )
      {
         const Vector3<real_t> & fluidPoint = pointIt->first;
         const Vector3<real_t> & direction  = pointIt->second;

         real_t q = lbm::intersectionRatio( *aabbIt, fluidPoint, direction, EPSILON );
         Vector3<real_t> intersectionPoint = fluidPoint + direction * q;
         WALBERLA_CHECK_LESS( std::fabs( aabbIt->sqSignedDistance( intersectionPoint ) ), EPSILON * EPSILON );

         q = lbm::intersectionRatioBisection( *aabbIt, fluidPoint, direction, EPSILON );
         intersectionPoint = fluidPoint + direction * q;
         WALBERLA_CHECK_LESS( std::fabs( aabbIt->sqSignedDistance( intersectionPoint ) ), EPSILON * EPSILON );
      }
   }

}
void testAABBDistance( const Vector3<real_t> & translationVector = Vector3<real_t>() )
{
   auto mesh = make_shared<MeshType>();
   mesh::readAndBroadcast( "cube.obj", *mesh);

   translate( *mesh, translationVector );

   auto aabb = computeAABB( *mesh ); // works since the mesh is a cube

   auto testVolume = aabb.getScaled( real_t(2) ); // AABB containing the test points

   TriangleDistance<MeshType> triDist( mesh );

   std::mt19937 rng;

   for( int i = 0; i < 10000; ++i )
   {
      auto p = testVolume.randomPoint( rng );
      WALBERLA_CHECK_FLOAT_EQUAL( triDist.sqSignedDistance( toOpenMesh(p) ), aabb.sqSignedDistance( p ) );
   }

}
Exemple #7
0
void generateBuildingSeeds(Context &context, Stronghold &stronghold, Polygon2D const &inner, std::vector<BuildingSeed> &out)
{
	// habitations
	const int numSeeds = 5000;
	//const float attractionThreshold = 10000;
	float threshold = 0.95f;

	std::vector<BuildingSeed> &seeds = stronghold.seeds;
	
	// test LJ
	for (int i = 0; i < numSeeds; ++i) {
		// new seed
		BuildingSeed s;
		s.location = randomPoint(inner);
		s.attraction = calculateAttractionPotential(s.location, seeds);
		s.flatness = flatnessScoreWall(s.location, 4.0f, context);

		// 
		// Si le terrain n'est pas assez plat, on rejette la graine
		if (s.flatness > 0.3f) {
			continue;
		}

		//
		// La maison est trop proche d'autres habitations: rejet
		if (s.attraction < 0.f) {
			// reject
			continue;
		}

        // TODO: autres formes
        Rectangle2D house = generateBuilding(context, s, glm::vec2(7, 7));
        bool accepted = true;

        for (int j = 0; j < seeds.size(); j++) {
            Rectangle2D house2 = generateBuilding(context, seeds[j], glm::vec2(7, 7));
            if (intersect_houses(house, house2)) {
                accepted = false;
                break;
            }
        }

        if (!accepted) {
            continue;
        }


		// si la l'attraction n'est pas suffisante, il y a quand même une chance pour qu'un nouveau bâtiment apparaisse
		if (s.attraction < 0.5f) {
			if (GRandom.RandomNumber(0, 1) > threshold) {
				seeds.push_back(s);
			}
		} else {
			//
			// La graine a passé le test de 'platitude' et a une force d'attraction suffisante 
			seeds.push_back(s);				
		}


	}

	std::clog << "Created " << seeds.size() << " seeds\n";

	/* debugSeeds(context, stronghold, seeds); */

	// filtrage
}
Exemple #8
0
// TODO : random landMass from min to max
// TODO : putting next landMass level for height
static void civGeneration(GOC_HANDLER mapa, int numStartPoints, int minLM, int maxLM, int MINLEVEL, int NEWLEVEL)
{
//	int MINLEVEL=0;
//	int NEWLEVEL=1;
    int type = 0;
    stChange point;
    point.x = 0;
    point.y = 0;
    point.v = 0;
    stChange** pSet = NULL;
    int nSet = 0;
    int landMass;
    int svStartPoints = numStartPoints;
    int createdLandMass;

    GOC_BINFO("Start civ generation system [numStartPoint: %d, minLandMass: %d, maxLandMass: %d, level: %d]", numStartPoints, minLM, maxLM, NEWLEVEL);

    if ( MINLEVEL >= 10 )
        return;
    if ( numStartPoints <= 0 )
        return;
    if ( maxLM <= 0 )
        return;


    while (numStartPoints--)
    {
        GOC_BDEBUG("Number of start points %d", numStartPoints);
        // inicjowanie do kolejnej iteracji tworzenia masy ladowej
        type = 0;
        landMass = minLM + goc_random(maxLM-minLM);
        createdLandMass = 0;
        GOC_BINFO("Generated landmass: %d", landMass);
        if ( nSet )
        {
            GOC_DEBUG("Clear set");
            int i;
            for ( i=0; i<nSet; i++ )
                free(pSet[i]);
            pSet = goc_tableClear(pSet, &nSet);
        }
        while ( landMass )
        {
            GOC_BDEBUG("Landmass to generate %d", landMass);
            // random new point
            if ( randomPoint(&point, type, MINLEVEL, mapa) )
            {
                GOC_BDEBUG("Setting a point (%d,%d,%d)", point.x, point.y, NEWLEVEL);
                pSet = goc_tableAdd(pSet, &nSet, sizeof(void*));
                pSet[nSet-1] = mallocPoint(point.x, point.y, NEWLEVEL);
                goc_maparawSetPoint(mapa, point.x, point.y, NEWLEVEL);
                createdLandMass++;
            }
            landMass--;
            while ( nSet )
            {
                // random point from set
                int randp = goc_random(nSet);
                int x, y;
                type = 1; // neighbour
                // check a point has any free neighbour
                x = pSet[randp]->x;
                y = pSet[randp]->y;
                if (
                    ( (x+1 < context.configuration.maxx) && (goc_maparawGetPoint(mapa, x+1, y) <= MINLEVEL) ) ||
                    ( (y+1 < context.configuration.maxy) && (goc_maparawGetPoint(mapa, x, y+1) <= MINLEVEL )) ||
                    ( (y > 0) && (goc_maparawGetPoint(mapa, x, y-1) <= MINLEVEL) ) ||
                    ( (x > 0) && (goc_maparawGetPoint(mapa, x-1, y) <= MINLEVEL) )
                )
                {
                    point.x = x;
                    point.y = y;
                    point.v = pSet[randp]->v;
                    GOC_BDEBUG("Find point in set (%d,%d,%d)", point.x, point.y, point.v);
                    break;
                }
                else
                {
                    GOC_DEBUG("Remove point from set");
                    free(pSet[randp]);
                    pSet = goc_tableRemove(pSet, &nSet, sizeof(void*), randp);
                }
            }
            // check, if there are any points in set
            if ( !nSet )
                break;
        }
        GOC_BINFO("Created landmass: %d", createdLandMass);
    }
    civGeneration(
        mapa,
        svStartPoints, //-svStartPoints/20,
        minLM-minLM*precentageCut[MINLEVEL]/10,
        maxLM-maxLM*precentageCut[MINLEVEL]/10,
        MINLEVEL+1,
        NEWLEVEL+1);
    /*
    pStartPoint = randomStartPoints(&nStartPoint, numStartPoints);
    for (i=0; i<nStartPoint; i++)
    {
    	goc_maparawSetPoint(mapa, pStartPoint[i]->x, pStartPoint[i]->y, 1);
    }
    */
}
Exemple #9
0
// A modified k-means clustering algorithm, where output points is the reduction
// of each cluster.
vector<Point> findCentroids(int k, int init, vector<Point> points, int maxIter) {
  vector<Group> groups;

  Point center = hullCenter(points);

  // Init groups to a random point
  for(int i = 0; i < k; i++) {
   Point c = center + randomPoint(-init, init); 

   Group group(c, points);
   groups.push_back(group);
  }

  bool cont = true;
  int iter = 0;

  while(cont) {
    iter++;
    

    vector<Point> centroids;

    // Save centroids and clear points 
    for (int i = 0; i < k; i++) {
      centroids.push_back(groups[i].centroid);
      (&(groups[i].points))->clear();
    }

    // Assing points to the closest centroid
    for (int i = 0; i < points.size(); i++) {
      int closest = 0;
      double dist = 100000000.0;
      Point p = points[i];

      for (int j = 0; j < k; j++) {
       Point c = groups[j].centroid;
       double d = norm(c-p);
       if (d < dist) {
         closest = j;
         dist = d; 
       }
      }

      (&(groups[closest].points))->push_back(p);
    }

    cont = false;

    // Relocate centroids and check for change.
    for (int i = 0; i < k; i++) {
      Point c = groups[i].centroid;
      
      if (c == Point(0, 0))
        (&groups[i])->centroid = center + randomPoint(-init, init);
      else
        (&groups[i])->centroid = hullCenter(groups[i].points);
      
      // Check if centroid has not changed.
      if (norm(groups[i].centroid - centroids[i]) > 0.001) {
        cont = true; 
      }
    }

    if (iter > maxIter) {
      break;
    }
  }

  // Extract centroids.
  vector<Point> centroids;

  for (int i = 0; i < k; i++) {
    Point reduced = reduce(groups[i].points);
    centroids.push_back(reduced);
  }

  return centroids;
}
Ray taf::randomRay( double a, double b )
{
	return Ray( randomPoint(a, b), randomDirection() );
}
BBox taf::randomBox( double a, double b )
{
	Point3D point1 = randomPoint( a, b );
	Point3D point2 = randomPoint( a, b );
    return BBox( point1, point2 );
}