Exemple #1
0
Eigen::Vector3d HoleFinder::getRandomPoint()
{
  Q_D(HoleFinder);

  // Randomly select a hole
  const Hole &hole = d->getRandomHole();

  // Trim the radius of the sphere by 1.5 units. If the sphere is smaller than
  // 1.5 units, trim it to 0.5 units.
  const double rmax = (hole.radius >= d->minDist)
      ? hole.radius - d->minDist : 0.5;

  // Randomly select point from uniform distribution around sphere. The radius
  // is chosen such that
  //
  // P(r) [is prop. to] Area(r) = 4*pi*r^2, or
  // P(r) = N * 4*pi*r^2
  //
  // We want P(r) to lie between [0,1] and r to be between [0, rmax].
  //
  // Lower boundary conditions are trivially satisfied:
  //
  // P(0) = N * 4*pi*0^2 = 0
  //
  // Upper boundary conditions:
  //
  // P(rmax) = 1 = N * 4*pi*rmax^2, or
  // N = 1 / (4*pi*rmax^2)
  //
  // So P(r) simplifies to
  //
  // P(r) = (1/(4*pi*r^2)) * 4*pi*r^2 = r^2 / (rmax^2)
  //
  // So we can pick a "p" value from a uniform distribution from [0,1] and
  // transform it to a random radius that is uniformly distributed throughout
  // the sphere by
  const double p = RANDDOUBLE();
  const double r = sqrt(p) * rmax;

  // A random unit vector representing the displacement:
  Eigen::Vector3d displacement (2.0 * RANDDOUBLE() - 1.0,
                                2.0 * RANDDOUBLE() - 1.0,
                                2.0 * RANDDOUBLE() - 1.0);
  displacement.normalize();

  // Put the two together
  displacement *= r;

  // Shift by the hole's origin
  const Eigen::Vector3d ret (hole.center + displacement);

  // Ta-da!
  return ret;
}
void RANDDOUBLETest::generateImageDefault()
{
    INIT_RANDOM_GENERATOR();

    // Create matrix to store hits
    std::vector<std::vector<unsigned int> > hits;
    hits.resize(size);
    for (int i = 0; i < size; i++) {
        hits[i].resize(size);
        for (int j = 0; j < size; j++) {
            hits[i][j] = 0;
        }
    }

    // Generate numbers
    int x,y;
    QBENCHMARK {
        for (int i = 0; i < numPoints; i++) {
            x = (int)(RANDDOUBLE() * size);
            y = (int)(RANDDOUBLE() * size);
            hits[x][y]++;
        }
    }

    // Find greatest number of hits
    unsigned int max = 0;
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            if (hits[i][j] > max) {
                max = hits[i][j];
            }
        }
    }

    // Generate image
    unsigned int normHit;
    unsigned int rgb;
    QImage im (size, size, QImage::Format_RGB32);
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            normHit = (hits[i][j] * 100) / max;
            rgb = (normHit * 0xff) / 100;
            im.setPixel(i, j,
                        0xff000000 +
                        0x00010000 * rgb +
                        0x00000100 * rgb +
                        0x00000001 * rgb );
        }
    }
    im.save("RANDDOUBLETest-default.png", 0, 100);
}
Exemple #3
0
const Hole & HoleFinderPrivate::getRandomHole()
{
  Q_ASSERT(this->holeProbs.size() != 0);

  double r = RANDDOUBLE();
  int ind;
  for (ind = 0; ind < this->holeProbs.size(); ind++)
    if (r < this->holeProbs[ind]) break;

  QMap<double, Hole>::const_iterator retIt = this->holeMap.constBegin();
  for (int i = 0; i < ind; ++i) ++retIt;

  return retIt.value();
}
Exemple #4
0
void init_dna(shape_t * dna)
{
    for(int i = 0; i < NUM_SHAPES; i++)
    {
        for(int j = 0; j < NUM_POINTS; j++)
        {
            dna[i].points[j].x = RANDDOUBLE(WIDTH);
            dna[i].points[j].y = RANDDOUBLE(HEIGHT);
        }
        dna[i].r = RANDDOUBLE(1);
        dna[i].g = RANDDOUBLE(1);
        dna[i].b = RANDDOUBLE(1);
        dna[i].a = RANDDOUBLE(1);
        //dna[i].r = 0.5;
        //dna[i].g = 0.5;
        //dna[i].b = 0.5;
        //dna[i].a = 1;
    }
}
Exemple #5
0
int mutate(int * mutated_shape,  shape_t *  dna_test)
{
    *mutated_shape = RANDINT(NUM_SHAPES);
    double roulette = RANDDOUBLE(2.8);
    double drastic = RANDDOUBLE(2);

    // mutate color
    if(roulette<1)
    {
        if(dna_test[*mutated_shape].a < 0.01 // completely transparent shapes are stupid
                || roulette<0.25)
        {
            if(drastic < 1)
            {
                dna_test[*mutated_shape].a += RANDDOUBLE(0.1);
                dna_test[*mutated_shape].a = CLAMP(dna_test[*mutated_shape].a, 0.0, 1.0);
            }
            else
                dna_test[*mutated_shape].a = RANDDOUBLE(1.0);
        }
        else if(roulette<0.50)
        {
            if(drastic < 1)
            {
                dna_test[*mutated_shape].r += RANDDOUBLE(0.1);
                dna_test[*mutated_shape].r = CLAMP(dna_test[*mutated_shape].r, 0.0, 1.0);
            }
            else
                dna_test[*mutated_shape].r = RANDDOUBLE(1.0);
        }
        else if(roulette<0.75)
        {
            if(drastic < 1)
            {
                dna_test[*mutated_shape].g += RANDDOUBLE(0.1);
                dna_test[*mutated_shape].g = CLAMP(dna_test[*mutated_shape].g, 0.0, 1.0);
            }
            else
                dna_test[*mutated_shape].g = RANDDOUBLE(1.0);
        }
        else
        {
            if(drastic < 1)
            {
                dna_test[*mutated_shape].b += RANDDOUBLE(0.1);
                dna_test[*mutated_shape].b = CLAMP(dna_test[*mutated_shape].b, 0.0, 1.0);
            }
            else
                dna_test[*mutated_shape].b = RANDDOUBLE(1.0);
        }
    }
    
    // mutate shape
    else if(roulette < 2.0)
    {
        int point_i = RANDINT(NUM_POINTS);
        if(roulette<1.5)
        {
            if(drastic < 1)
            {
                dna_test[*mutated_shape].points[point_i].x += (int)RANDDOUBLE(WIDTH/10.0);
                dna_test[*mutated_shape].points[point_i].x = CLAMP(dna_test[*mutated_shape].points[point_i].x, 0, WIDTH-1);
            }
            else
                dna_test[*mutated_shape].points[point_i].x = RANDDOUBLE(WIDTH);
        }
        else
        {
            if(drastic < 1)
            {
                dna_test[*mutated_shape].points[point_i].y += (int)RANDDOUBLE(HEIGHT/10.0);
                dna_test[*mutated_shape].points[point_i].y = CLAMP(dna_test[*mutated_shape].points[point_i].y, 0, HEIGHT-1);
            }
            else
                dna_test[*mutated_shape].points[point_i].y = RANDDOUBLE(HEIGHT);
        }
    }

    // mutate stacking
    else
    {
        int destination = RANDINT(NUM_SHAPES);
        shape_t s = dna_test[*mutated_shape];
        dna_test[*mutated_shape] = dna_test[destination];
        dna_test[destination] = s;
        return destination;
    }
    return -1;

}
Exemple #6
0
Mutation getMutation(const ShapeCollection &collection)
{
    Mutation mutation{};

    double roulette = (collection.polygonCount > 1 ? RANDDOUBLE(2.2) : RANDDOUBLE(2.0));

    mutation.type = (roulette >= 2.0f ? Mutation::kOrderSwapped :
                     (roulette < 1.0f ? Mutation::kColorChanged :
                      Mutation::kPointChanged));

    mutation.shapeIdx = RANDINT(collection.polygonCount);

    switch(mutation.type) {

    case Mutation::kOrderSwapped:
    {
        mutation.data.shapeIdxB = RANDINT(collection.polygonCount);
        while (mutation.shapeIdx == mutation.data.shapeIdxB) {
            mutation.data.shapeIdxB = RANDINT(collection.polygonCount);
        }
        if (mutation.shapeIdx > mutation.data.shapeIdxB) {
            std::swap(mutation.shapeIdx, mutation.data.shapeIdxB);
        }
    }
    break;

    case Mutation::kColorChanged:
    {
        mutation.data.color = collection.shapes[mutation.shapeIdx].rgba;

        bool drastic = RANDBOOL();

        int part = roulette  * 4;
        mutation.data.color[part] = (drastic ?
                                     CLAMPINT(RANDUINT8(), part == 3 ? 0 : 1, 255) :
                                     CLAMPINT(mutation.data.color[part] +
                                              (RANDBOOL() ? + 1 : -1)
                                              * RANDINT(25), part == 3 ? 0 : 1, 255));
    }
    break;

    case Mutation::kPointChanged:
    {
        mutation.data.pt.idx = RANDINT(collection.vertexCount);
        mutation.data.pt.val = collection.shapes[mutation.shapeIdx].polygon[mutation.data.pt.idx];

        bool drastic = RANDBOOL(); ;

        bool changeX = roulette < 1.5;
        std::array<float, 2> &ptRef = mutation.data.pt.val;

        if (changeX) {
            double x = ptRef[0];
            if (drastic) {
                ptRef[0] = RANDINT(collection.width + 1);
                while (x == ptRef[0]) {
                    ptRef[0] = RANDINT(collection.width + 1);
                }
            } else {
                ptRef[0] = CLAMPINT(ptRef[0] + (RANDBOOL() ? +1 : -1) *
                                    static_cast<int>(RANDDOUBLE(collection.width / 10.0)), 0, collection.width);
            }
        } else {
            double y = ptRef[1];
            if (drastic) {
                ptRef[1] = RANDINT(collection.height + 1);
                while (y == ptRef[1]) {
                    ptRef[1] = RANDINT(collection.height + 1);
                }
            } else {
                ptRef[1] = CLAMPINT(ptRef[1] + (RANDBOOL() ? +1 : -1) *
                                    static_cast<int>(RANDDOUBLE(collection.height / 10.0)), 0,
                                    collection.height);
            }
        }
    }
    break;

    default:
        ;
    }

    return mutation;
}