Exemplo n.º 1
0
	void PointStarfield::addRandomStars (int count)
	{
		for (int i = 0; i < count; ++i) {
			// Generate a vector inside a sphere
			Ogre::Vector3 pos;
			do {
				pos.x = randReal(-1, 1);
				pos.y = randReal(-1, 1);
				pos.z = randReal(-1, 1);
			} while (pos.squaredLength () >= 1);

			// Convert to rasc/decl angles.
			LongReal rasc, decl, dist;
			Astronomy::convertRectangularToSpherical(
					pos.x, pos.y, pos.z,
					rasc, decl, dist);

			Star s;
			s.RightAscension = Ogre::Degree (rasc);
			s.Declination = Ogre::Degree (decl);
			// This distribution is wrong.
			s.Magnitude = 6 * pos.squaredLength () + 1.5;
			mStars.push_back(s);
		}
		notifyStarVectorChanged ();
	}
bool PhaseIsotropic::s(HitInfo &info) const{
    double pu, pv, pw;
    mapToHemisphere(randReal(), randReal(), pu, pv, pw);
    info.wi = Vector(pu, pv, pw);
    if (rand() % 2 == 0)
        info.wi = -info.wi;
    info.emitted = 0;
    info.scattered = color * (texture == 0 ? 1 : texture->value(info));
    return true;
}
void Film::getSample(int k, double &x, double &y){
    currentIndex = k;
    if (k < w * h)
        std::tie(cx, cy, std::ignore) = sampleList[k];
    else{
        cx = (k / h) % w;
        cy = k % h;
    }
    x = (xmin + cx + randReal()) / scale;
    y = (ymax - cy - randReal()) / scale;
    //sampler->getSample(k, tx, ty);
}
Exemplo n.º 4
0
void ArrayTest::get_test_array_types_and_sizes( int /*test_case_idx*/, vector<vector<Size> >& sizes, vector<vector<int> >& types )
{
    RNG& rng = ts->get_rng();
    Size size;
    double val;
    size_t i, j;

    val = randReal(rng) * (max_log_array_size - min_log_array_size) + min_log_array_size;
    size.width = cvRound( exp(val*CV_LOG2) );
    val = randReal(rng) * (max_log_array_size - min_log_array_size) + min_log_array_size;
    size.height = cvRound( exp(val*CV_LOG2) );

    for( i = 0; i < test_array.size(); i++ )
    {
        size_t sizei = test_array[i].size();
        for( j = 0; j < sizei; j++ )
        {
            sizes[i][j] = size;
            types[i][j] = CV_8UC1;
        }
    }
}
RGBColor MonteCarloPathTracer::trace(Ray ray, int depth, int index, Primitive *currentObj){
    /*
    if (!(ray.d.norm() < 10000)){
        printf("%d %d\n", depth, currentObj == 0 ? -1 : currentObj->id);
        printf("%lf %lf %lf\n", ray.o.x, ray.o.y, ray.o.z);
        printf("%lf %lf %lf\n", ray.d.x, ray.d.y, ray.d.z);
    }
    */
    if (!(ray.d.norm() < 10000))
        return 0;
    HitPoint hitPoint, ht;
    HitInfo info;
    RGBColor scattered, emitted, light;
    Point lightPos;
    info.ray = ++ray;
    bool f;
    if (currentObj == 0){
        if (scene->hit(ray, hitPoint, Scene::HitAll)){
            info.t = hitPoint.t;
            info.tag = hitPoint.tag;
            hitPoint.obj->getHitInfo(info);
            info.wo = -ray.d.normalized();
            info.n1 = 1;
            info.n2 = hitPoint.obj->material->ior;
            //samplers[depth]->getSample(index, info.sx, info.sy);
            info.sx = randReal();
            info.sy = randReal();
            if (depth == maxDepth - 1)
                return directLighting(hitPoint.obj, info);
            else{
                if (!hitPoint.obj->material->surfaceBSDF->s(info))
                    return info.emitted;
                if (!info.isReflected)
                    currentObj = hitPoint.obj;
                ray.o = info.hitPoint;
                ray.d = info.wi;
                ray.tMin = EPSILON;
                ray.tMax = INF;
                //
                //printf("#1 %lf\n", info.t);
                //printf("%lf %lf %lf\n", ray.o.x, ray.o.y, ray.o.z);
                scattered = info.scattered * trace(ray, depth + 1, index, currentObj) * fabs(info.normal * info.wi) / info.pdf;
                return info.emitted + scattered;
            }
        }
        return scene->background(ray.d);
    }
    else{
        //printf("%d\n", currentObj->id);
        Material *material = currentObj->material;
        double l;
        if (material->doesScatter)
            l = log(randReal()) / log(material->scatterRate);
        double l2;
        ray.tMin = EPSILON;
        ray.tMax = INF;
        bool ft;
        if (currentObj->csg == 0)
            ft = currentObj->hit(ray, hitPoint);
        else
            ft = currentObj->csg->hit(ray, hitPoint);
        /*
        if (!ft)
            printf("! %d %d %d\n", currentObj->id, ft, hitPoint.tag);
        */
        double tMax = hitPoint.t;
        ray.tMax = tMax;
        if (scene->hit(ray, ht, Scene::HitHigher, currentObj->layer)){
            l2 = ht.t * ray.d.norm();
            if ((!material->doesScatter) || (l2 < l)){
                info.t = ht.t;
                info.tag = ht.tag;
                ht.obj->getHitInfo(info);
                info.wo = -ray.d.normalized();
                info.n1 = material->ior;
                info.n2 = ht.obj->material->ior;
                //samplers[depth]->getSample(index, info.sx, info.sy);
                info.sx = randReal();
                info.sy = randReal();
                f = ht.obj->material->surfaceBSDF->s(info);
                if (depth == maxDepth - 1)
                    return directLighting(hitPoint.obj, info);
                else{
                    if (!hitPoint.obj->material->surfaceBSDF->s(info))
                        return info.emitted;
                    if (!info.isReflected)
                        currentObj = ht.obj;
                    ray.o = info.hitPoint;
                    ray.d = info.wi;
                    ray.tMin = EPSILON;
                    ray.tMax = INF;
                    //
                    //printf("#2 %lf\n", info.t);
                    //printf("%lf %lf %lf\n", ray.o.x, ray.o.y, ray.o.z);
                    l2 *= material->ior;
                    //emitted = material->emitFilter(l2);
                    emitted = 0;
                    scattered = info.scattered * trace(ray, depth + 1, index, currentObj) * fabs(info.normal * info.wi) / info.pdf;
                    return (info.emitted + scattered) * pow(material->filterColor, l2) + emitted;
                }
            }
            else{
                info.wo = -ray.d.normalized();
                //samplers[depth]->getSample(index, info.sx, info.sy);
                info.sx = randReal();
                info.sy = randReal();
                f = material->scatterBSDF->s(info);
                if ((depth == maxDepth - 1) || (!f))
                    return info.emitted;
                else{
                    ray.o = info.ray.o + l * info.ray.d / info.ray.d.norm();
                    ray.d = info.wi;
                    ray.tMin = -EPSILON;
                    ray.tMax = INF;
                    l *= material->ior;
                    //emitted = material->emitFilter(l);
                    emitted = 0;
                    scattered = material->scatterColor * info.scattered * trace(ray, depth + 1, index, currentObj);
                    return (info.emitted + scattered) * pow(material->filterColor, l2) + emitted;
                }
            }
        }
        else{
            l2 = tMax * ray.d.norm();
            if ((material->doesScatter) && (l < l2)){
                info.wo = -ray.d.normalized();
                //samplers[depth]->getSample(index, info.sx, info.sy);
                info.sx = randReal();
                info.sy = randReal();
                f = material->scatterBSDF->s(info);
                if ((depth == maxDepth - 1) || (!f))
                    return info.emitted;
                else{
                    ray.o = info.ray.o + l * info.ray.d / info.ray.d.norm();
                    ray.d = info.wi;
                    ray.tMin = -EPSILON;
                    ray.tMax = INF;
                    l *= material->ior;
                    //emitted = material->emitFilter(l);
                    emitted = 0;
                    scattered = material->scatterColor * info.scattered * trace(ray, depth + 1, index, currentObj);
                    return (info.emitted + scattered) * pow(material->filterColor, l) + emitted;
                }
            }
            else{
                info.t = tMax;
                info.tag = hitPoint.tag;
                //printf("! %d\n", info.tag);
                //if (!ft)
                //    printf("<");
                currentObj->getHitInfo(info);
                info.wo = -ray.d.normalized();
                info.n1 = material->ior;
                ray.tMin = tMax;
                ray.tMax = INF;
                bool f2 = scene->hit(ray, ht, Scene::HitLower, currentObj->layer);
                if (f2)
                    info.n2 = ht.obj->material->ior;
                else
                    info.n2 = 1;
                //samplers[depth]->getSample(index, info.sx, info.sy);
                info.sx = randReal();
                info.sy = randReal();
                if (depth == maxDepth - 1)
                    return directLighting(hitPoint.obj, info);
                else{
                    if (!hitPoint.obj->material->surfaceBSDF->s(info))
                        return info.emitted;
                    if (!info.isReflected)
                        if (f2)
                            currentObj = ht.obj;
                        else
                            currentObj = 0;
                    //printf("%d\n", f2);
                    ray.o = info.hitPoint;
                    ray.d = info.wi;
                    ray.tMin = EPSILON;
                    ray.tMax = INF;
                    //
                    //printf("#3 %lf\n", info.t);
                    //printf("%lf %lf %lf\n", ray.o.x, ray.o.y, ray.o.z);
                    l2 *= material->ior;
                    //emitted = material->emitFilter(l2);
                    emitted = 0;
                    scattered = info.scattered * trace(ray, depth + 1, index, currentObj) * fabs(info.normal * info.wi) / info.pdf;
                    return (info.emitted + scattered) * pow(material->filterColor, l2) + emitted;
                }
            }
        }
    }
}
Exemplo n.º 6
0
bool StochasticSupervisor::trainFor(NeuralNetwork &neuralNetwork, const int count)
{
    emit started(count);
    qreal bestError = 1e100;
    NeuralNetwork networkBackup;
    RandomSupervisor rnd(-5, 5);
    rnd.train(neuralNetwork);
    NeuralNetwork bestNetwork = neuralNetwork;
    emit iterationInfo(0, count);
    emit targetErrorInfo(EPS, bestError, bestError);
    for(int e = 1; e < count; ++e)
    {
        const qreal temperature = MAX_TEMPERATURE / (1 + e);
        const qreal temperature2 = temperature * temperature;
        const qreal oldError = getError(neuralNetwork, trainingSet());
        networkBackup = neuralNetwork;
        for (NeuralLayer &layer: neuralNetwork)
            for (Neuron &neuron: layer)
                for (int i = 0; i < neuron.weightNumber(); ++i)
                    neuron.setWeight(i, neuron.weight(i) + randReal(-0.25, 0.25));
        const qreal error = getError(neuralNetwork, trainingSet());
        { // info
            if (e % 100 == 0)
            {
                qDebug() << 100. * e / count << error << bestError; // FIXME DEBUG
                emit iterationInfo(e, count);
                emit targetErrorInfo(EPS, error, bestError);
                QCoreApplication::processEvents();
                if (checkAborted())
                {
                    emit finished(false);
                    qDebug() << "ABORTED";
                    return false;
                }
            }
        }
        if (error > oldError)
        {
            const qreal errorDiff = error - oldError;
            const qreal errorDiff2 = errorDiff * errorDiff;
            const qreal P = temperature2 / (temperature2 + errorDiff2);
            const qreal random = randReal(0, 1);
            if (random > P)
                neuralNetwork.swap(networkBackup);
        }
        else
        {
            if (error < bestError)
            {
                bestNetwork = neuralNetwork;
                bestError = error;
            }
        }
        if (error < EPS)
        {
            emit iterationInfo(e, count);
            emit targetErrorInfo(EPS, error, bestError);
            emit finished(true);
            qDebug() << "SUCCESS";
            return true;
        }
    }
    neuralNetwork.swap(bestNetwork);
    emit iterationInfo(count, count);
    emit targetErrorInfo(EPS, bestError, bestError);
    emit finished(false);
    qDebug() << "FAILED";
    return false;
}
Exemplo n.º 7
0
void setToRandom(Real &r) {
  r = randReal();
}
Exemplo n.º 8
0
Real randReal(const Real &lower, const Real &upper) {
  return randReal() * (upper-lower) + lower;
}
Exemplo n.º 9
0
	Real randReal (Real min, Real max) {
		Real f = randReal ();
		return min * (1 - f) + max * f;
	}
Exemplo n.º 10
0
void Phong::trace(int numThread,int totalThread)
{
	int width=view->width;
	int height=view->height;
	int done=0;
	for (int i=0;i<width;i++)
	{
		for (int j=0;j<height;j++)
		{
			if ((done++)%totalThread!=numThread) continue;
			Vector eye=view->eye,destination=view->getPoint(i,j);
			/*if ((i==0||i==width-1)&&(j==0||j==height-1)&&!i&&!j)
			{
				qDebug()<<i<<j;
				view->dw.print();
				view->dh.print();
				destination.print();
			}*/
			Ray ray;
			Color color;
			if (!useDepthOfField)
			{
				ray=Ray(eye,destination-eye,0,OUTSIDE);
				color=traceSimpleRay(ray);
			}
			else
			{
				color=COLOR_NONE;
				for (int k=0;k<DOF_SAMPLE_CNT;k++)
				{
					real rx = randReal(2), dx = rx < 1 ? sqrt(rx) - 1 : 1 - sqrt(2 - rx);
					real ry = randReal(2), dy = ry < 1 ? sqrt(ry) - 1 : 1 - sqrt(2 - ry);
					Vector focusPoint=eye+(view->getPoint(i+dx,j+dy)-eye).getNormalize()*CAMERA_FOCUS;
					destination=view->getPoint(i+dx,j+dy);
					real theta=randReal(M_PI*2);
					Vector move=view->dw.getNormalize()*cos(theta)+view->dh.getNormalize()*sin(theta);
					move.normalize();
					move*=sqrt(randReal())*APERTURE_RADIUS;
					Vector nowEye=eye+move;
					ray=Ray(nowEye,focusPoint-nowEye,0,OUTSIDE);
					color+=traceSimpleRay(ray);
				}
//				Vector nowEye=eye+(destination-eye)*DOF_SCREEN_DIST_FACTOR;
//				for (int i=0;i<DOF_SAMPLE_CNT;i++)
//				{
//					real theta=randReal(M_PI*2);
//					Vector move=view->dw.getNormalize()*cos(theta)+view->dh.getNormalize()*sin(theta);
//					move.normalize();
//					move*=randReal(DOF_SAMPLE_RADIUS);
//					Vector tmpEye=nowEye+move;
//					ray=Ray(tmpEye,destination-tmpEye,(eye-tmpEye).mod(),OUTSIDE);
//					color+=traceSimpleRay(ray);
//				}
				color/=DOF_SAMPLE_CNT;
			}
			imageColor[i][j]=color;
			isDonePoint[i][j]=true;
		}
//		if (numThread==0&&(i%(width/10)==(width/10-1))) qDebug()<<100.0*(i+1)/width<<"%    done!";
		if (numThread==0&&i%5==0)
		{
			while (queue.size())
			{
				PII point=queue.top();
				int x=point.first,y=point.second;
				if (!isDonePoint[x][y]) break;
				queue.pop();
				Color color=imageColor[x][y]*255;
				image->setPixel(x,y,qRgb(color.r,color.g,color.b));
			}
			QApplication::processEvents();
		}
	}
}
Ray PointLight::shootRay(){
    double x, y, z;
    mapToSphere(randReal(), randReal(), x, y, z);
    return Ray(pos, Vector(x, y, z));
}
Exemplo n.º 12
0
//////////////////////////////////////////////////////////////////////////
// randRealRange
BcF32 BcRandom::randRealRange( BcF32 Min, BcF32 Max )
{
	return ( ( ( randReal() + 1.0f ) * 0.5f ) * ( Max - Min ) ) + Min;

}