Exemplo n.º 1
0
void
ClusterSet::read_from_file(const char * filename,
                           const map <string, const Record*> & uid_tree) {

    unsigned int count = 0;
    const unsigned int base = 100000;
    const unsigned int primary_delim_size = strlen(ClusterInfo::primary_delim);
    const unsigned int secondary_delim_size = strlen(ClusterInfo::secondary_delim);
    std::ifstream infile ( filename);

    if (infile.good()) {

        string filedata;
        while ( getline(infile, filedata)) {

            register size_t pos = 0, prev_pos = 0;
            pos = filedata.find(ClusterInfo::primary_delim, prev_pos);
            string keystring = filedata.substr( prev_pos, pos - prev_pos);
            const Record * key = retrieve_record_pointer_by_unique_id( keystring, uid_tree );
            prev_pos = pos + primary_delim_size;

            pos = filedata.find(ClusterInfo::primary_delim, prev_pos);
            double val = 0;
            if ( true ) {
                string cohesionstring = filedata.substr( prev_pos, pos - prev_pos);
                val = atof(cohesionstring.c_str());
            }
            prev_pos = pos + primary_delim_size;


            RecordPList tempv;
            while ( ( pos = filedata.find(ClusterInfo::secondary_delim, prev_pos) )!= string::npos){
                string valuestring = filedata.substr( prev_pos, pos - prev_pos);
                const Record * value = retrieve_record_pointer_by_unique_id( valuestring, uid_tree);
                tempv.push_back(value);
                prev_pos = pos + secondary_delim_size;
            }

            ClusterHead th(key, val);
            Cluster tempc(th, tempv);
            tempc.self_repair();
            this->consolidated.push_back(tempc);

            ++count;
            if ( count % base == 0 ) {
                std::cout << count << " records have been loaded from the cluster file. " << std::endl;
            }
        }
        std::cout << "Totally, " << count << " records have been loaded from " << filename << std::endl;
    }
    else {
        throw cException_File_Not_Found(filename);
    }
}
Exemplo n.º 2
0
int main(int argc, char **argv){
	double temp_in, temp_out;
	char temp_in_unit, temp_out_unit;

	temp_in = -40; temp_in_unit = 'c'; temp_out_unit = 'f';
	tempc(temp_in, temp_in_unit, &temp_out, temp_out_unit);
	assert( temp_out == -40);

	temp_in = 0; temp_in_unit = 'c'; temp_out_unit = 'f';
	tempc(temp_in, temp_in_unit, &temp_out, temp_out_unit);
	assert( temp_out == 32);

	temp_in = 100; temp_in_unit = 'c'; temp_out_unit = 'f';
	tempc(temp_in, temp_in_unit, &temp_out, temp_out_unit);
	assert( temp_out == 212);

	temp_in = -40; temp_in_unit = 'f'; temp_out_unit = 'c';
	tempc(temp_in, temp_in_unit, &temp_out, temp_out_unit);
	assert( temp_out == -40);

	temp_in = 32; temp_in_unit = 'f'; temp_out_unit = 'c';
	tempc(temp_in, temp_in_unit, &temp_out, temp_out_unit);
	assert( temp_out == 0);

	temp_in = 212; temp_in_unit = 'f'; temp_out_unit = 'c';
	tempc(temp_in, temp_in_unit, &temp_out, temp_out_unit);
	assert( temp_out == 100);
	return 0;
}
  ComplexFloatArrayTestPatch(){
    success=true;
    debugMessage("start");
    passed=0;
    failed=0;
    int size=100;
    ComplexFloat data[size];
    ComplexFloat backupData[size];
    for(int n=0; n<size; n++){ //initialize array
      float value= n<size/2 ? n : size-n; 
      data[n].re=backupData[n].re=value;
      data[n].im=backupData[n].im=-value;
    }
    ComplexFloatArray cfa(data,size);
    ComplexFloatArray backupcf(backupData,size);
    ComplexFloat tempcData[size]; 
    ComplexFloatArray tempc(tempcData,size);
    ComplexFloat tempc2Data[size]; 
    ComplexFloatArray tempc2(tempc2Data,size);
    float tempfData[size]; 
    FloatArray tempf(tempfData,size);
        
    assertr(cfa.getSize()==size,"ComplexFloatArray.getSize()");
    for(int n=0; n<size; n++){ 
      //checking cast and indexing operators
      assertr(cfa[n].re==data[n].re,"ComplexFloat& operator []");
      assertr(cfa[n].im==data[n].im,"ComplexFloat& operator []");
      assertr((ComplexFloat*)cfa==data,"(ComplexFloat*)cfa==data");
      assertr((float*)cfa==(float *)data,"(ComplexFloat*)cfa==data");
      assertr(&cfa[n]==&data[n],"cfa[n]==data[n]");
      
      //check that indexing works properly overlaying struct and float
      float *f=(float*)data;
      assertr(data[n].re==f[2*n],"data[n].re==f[2*n]");
      assertr(data[n].im==f[2*n+1],"data[n].im==f[2*n+1]");
      //checking scalar complex math functions
      assertr(cfa.mag(n)==sqrtf(f[2*n]*f[2*n]+f[2*n+1]*f[2*n+1]),"mag");
      assertr(cfa.mag2(n)==f[2*n]*f[2*n]+f[2*n+1]*f[2*n+1],"mag2");
    }
    
    //test equals(ComplexFloatArray) method
    {
      tempc.copyFrom(cfa);
      assertr(cfa.equals(tempc), ".equals()");
      
    }
    cfa.getMagnitudeValues(tempf);
    for(int n=0; n<size; n++){
      assertr(tempf[n]==sqrtf(cfa[n].re*cfa[n].re+cfa[n].im*cfa[n].im),"getMagnitudeValues", n);
    }
    cfa.getMagnitudeSquaredValues(tempf);
    for(int n=0; n<size; n++){
      assertr(tempf[n]==cfa[n].re*cfa[n].re+cfa[n].im*cfa[n].im,"getMagnitudeSquaredValues");
    }
    cfa.getComplexConjugateValues(tempc);
    for(int n=0; n<size; n++){
      assertr(tempc[n].re==cfa[n].re,"getComplexConjugateValues-real");
      assertr(tempc[n].im==-cfa[n].im,"getComplexConjugateValues-imag");
    }
    cfa.complexByComplexMultiplication(tempc, tempc2);
    for(int n=0; n<size; n++){
      assertr(tempc2[n].re==cfa[n].re*tempc[n].re - cfa[n].im*tempc[n].im,"complexByComplexMultiplication-real");
      assertr(tempc2[n].im==cfa[n].re*tempc[n].im + cfa[n].im*tempc[n].re,"complexByComplexMultiplication-imag");
    }
    cfa.complexByRealMultiplication(tempf, tempc2);
    for(int n=0; n<size; n++){
      assertr(tempc2[n].re==cfa[n].re*tempf[n],"complexByRealMultiplication-real");
      assertr(tempc2[n].im==cfa[n].im*tempf[n],"complexByRealMultiplication-imag");
    }
    
  //ComplexDotProduct
    ComplexFloat tempcf;
    for(int n=0; n<size; n++){
      tempcData[n].re=backupData[n].re=n*2;
      tempcData[n].im=backupData[n].im=-(n*2+1);
    }
    cfa.complexDotProduct(tempc, tempcf);
    float realResult=0;
    float imagResult=0;
    for(int n=0; n<size; n++) {    
      realResult += cfa[n].re*tempc[n].re - cfa[n].im*tempc[n].im;
      imagResult += cfa[n].re*tempc[n].im + cfa[n].im*tempc[n].re;
    }
    assertr(realResult==tempcf.re,"duct-re");
    assertr(imagResult==tempcf.im,"duct-im");

    float maxMagVal=-1;
    int maxMagInd=-1;
    for(int n=0; n<size; n++){
      if(cfa.mag(n)>maxMagVal){
        maxMagVal=cfa.mag(n);
        maxMagInd=n;
      }
    }
    assertr(maxMagVal==cfa.getMaxMagnitudeValue(),"getMaxMagnitudeValue()");
    assertr(maxMagInd==cfa.getMaxMagnitudeIndex(),"getMaxMagnitudeIndex()");
    
    cfa.getRealValues(tempf);
    for(int n=0; n<size; n++){
      assertr(tempf[n]==cfa[n].re,"getRealValues");
    }
    cfa.getImaginaryValues(tempf);
    for(int n=0; n<size; n++){
      assertr(tempf[n]==cfa[n].im,"getImaginaryValues");
    }
    
    //test setAll
    // test setAll(float)
    {
      for(int n=0; n<size; n++){
        tempc[n].re=1;
        tempc[n].im=1;
      }
      float allValue=0.1;
      tempc.setAll(allValue);
      for(int n=0; n<size; n++){
        assertr(tempc[n].re==allValue, "setAll(float) real");
        assertr(tempc[n].im==allValue, "setAll(float) imag");
      }
    }
      // test setAll(float, float)
    {
      float allValueRe=0.3;
      float allValueIm=-0.3;
      tempc.setAll(allValueRe, allValueIm);
      for(int n=0; n<size; n++){
        assertr(tempc[n].re==allValueRe, "setAll(float, float) real");
        assertr(tempc[n].im==allValueIm, "setAll(float, float) imag");
      }
    }
      // test setAll(ComplexFloat)
    {
      ComplexFloat allValue;
      allValue.re=0.4;
      allValue.im=-0.1;
      tempc.setAll(allValue);
      for(int n=0; n<size; n++){
        assertr(tempc[n].re==allValue.re, "setAll(ComplexFloat) real");
        assertr(tempc[n].im==allValue.im, "setAll(ComplexFloat) imag");
      }
    }
    
    //test copyFrom
    //test copyFrom(FloatArray)
    tempc.setAll(0);
    tempc.copyFrom(tempf);
    for(int n=0; n<size; n++){
      assertr(tempf[n]==tempc[n].re, "copyFrom(FloatArray)");
    }
    //test copyFrom(ComplexFloatArray)
    tempc.setAll(0);
    tempc.copyFrom(cfa);
    for(int n=0; n<size; n++){
      assertr(cfa[n].re==tempc[n].re, "copyFrom(ComplexFloatArray), real");
      assertr(cfa[n].im==tempc[n].im, "copyFrom(ComplexFloatArray), imag");
    }
    //test copyFrom(ComplexFloat *, int)
    tempc.setAll(0);
    tempc.copyFrom(cfa.getData(), cfa.getSize());
    for(int n=0; n<size; n++){
      assertr(cfa[n].re==tempc[n].re, "copyFrom(ComplexFloat*, int), real");
      assertr(cfa[n].im==tempc[n].im, "copyFrom(ComplexFloat*, int), imag");
    }
    
      //test ComplexFloat methods

    {
      ASSERT(tempc.getSize()==size,"tempc");
      //test ComplexFloat.setPolar()
      ComplexFloat i;
      i.re=0;
      i.im=0;
      float magnitude=rand()/(float)RAND_MAX*10;
      float phase=rand()/(float)RAND_MAX*2*M_PI - M_PI;
      i.setPolar(magnitude, phase);
      assertr(i.re=magnitude*cosf(phase),"ComplexFloat setPolar() real");
      assertr(i.im=magnitude*sinf(phase),"ComplexFloat setPolar() imag");
      //test ComplexFloat.getPhase()
      assertt(i.getPhase(),phase, "ComplexFloat getPhase()");
      //test ComplexFloat.getMagnitude()
      // debugMessage("magnitude",i.getMagnitude(),magnitude, phase);
      assertt(i.getMagnitude(),magnitude, "ComplexFloat getMagnitude()", 0.0001);
    }
    {
      //test ComplexFloat.setMagnitude()
      ComplexFloat i;
      float magnitude=rand()/(float)RAND_MAX*10;
      float phase=rand()/(float)RAND_MAX*2*M_PI - M_PI;
      i.setPolar(magnitude,phase);
      float newMagnitude=magnitude+1;
      i.setMagnitude(newMagnitude);
      assertt(i.getMagnitude(), newMagnitude, "ComplexFloat setMagnitude() magnitude", 0.0001);
      assertt(i.getPhase(), phase, "ComplexFloat setMagnitude() phase", 0.0001); //check that the phase has not changed
    }
    {
      //test ComplexFloat.setPhase()
      ComplexFloat i;
      float magnitude=rand()/(float)RAND_MAX*10;
      float phase=rand()/(float)RAND_MAX*2*M_PI - M_PI;
      i.setPolar(magnitude,phase);
      float newPhase=phase+1;
      i.setPhase(newPhase);
      assertt(i.getPhase(), newPhase, "ComplexFloat setPhase() phase", 0.0001);
      assertt(i.getMagnitude(), magnitude, "ComplexFloat setPhase() magnitude", 0.0001); //check that the magnitude has not changed
    }
    {
      // test ComplexFloatArray.setPhase()
      tempc.copyFrom(cfa);
      FloatArray phase=FloatArray::create(size);
      phase.noise(-M_PI, M_PI);
      // setPhase(FloatArray)
      tempc.setPhase(phase);
      for(int n=0; n<size; n++){
        if(cfa[n].getMagnitude()<0.001)
          continue; //skip small values of magnitude which would make the phase noisy
        assertt(tempc[n].getPhase(), phase[n], "setPhase() phase", 0.01);
        assertt(tempc[n].getMagnitude(), cfa[n].getMagnitude(), "setPhase() magnitude", 0.01);
      }
      // setPhase(FloatArray, int, int)
      int offset=size/3;
      int count= size/2;
      tempc.copyFrom(cfa);
      tempc.setPhase(phase, offset, count);
      for(int n=0; n<size; n++){
        if(n>=offset && n<offset+count){
          if(cfa[n].getMagnitude()<0.001)
            continue; //skip small values of magnitude which would make the phase noisy
          assertt(tempc[n].getPhase(), phase[n], "setPhase() phase wrong", 0.01);
          assertt(tempc[n].getMagnitude(), cfa[n].getMagnitude(), "setPhase() magnitude wrong", 0.01);
        } else { //check that values outside the range have not changed
          assertr(tempc[n].getPhase()==cfa[n].getPhase(), "setPhase() phase changed", 0);
          assertr(tempc[n].getMagnitude()==cfa[n].getMagnitude(), "setPhase() magnitude changed", 0);
        }
      }
      
      // test ComplexFloatArray.setMagnitude()
      tempc.copyFrom(cfa);
      FloatArray magnitude=FloatArray::create(size);
      magnitude.noise(0, 10);
      // setMagnitude(FloatArray)
      tempc.setMagnitude(magnitude);
      for(int n=0; n<size; n++){
        if(cfa[n].getMagnitude()<0.001 || tempc[n].getMagnitude()<0.001)
          continue; //skip small values of magnitude which would make the phase noisy
        assertt(tempc[n].getPhase(), cfa[n].getPhase(), "setMagnitude() phase", 0.01);
        assertt(tempc[n].getMagnitude(), magnitude[n], "setMagnitude() magnitude", 0.01);
      }
      // setMagnitude(FloatArray, int, int)
      offset=size/3.0f;
      count=size/2.0f;
      tempc.copyFrom(cfa);
      tempc.setMagnitude(magnitude, offset, count);
      for(int n=0; n<size; n++){
        if(n>=offset && n<offset+count){
          if(cfa[n].getMagnitude()<0.001 || tempc[n].getMagnitude()<0.001)
            continue; //skip small values of magnitude which would make the phase noisy
          assertt(tempc[n].getPhase(), cfa[n].getPhase(), "setMagnitude() phase", 0.01);
          assertt(tempc[n].getMagnitude(), magnitude[n], "setMagnitude() magnitude", 0.01);
        } else { //check that values outside the range have not changed
          if(abs(tempc[n].getPhase()-cfa[n].getPhase())>0){
            debugMessage("error", (float)n, tempc[n].getPhase(), cfa[n].getPhase());
            return;
          }
          assertr(tempc[n].getPhase()==cfa[n].getPhase(), "setMagnitude()t phase");
          assertr(tempc[n].getMagnitude()==cfa[n].getMagnitude(), "setMagnitude() magnitude");
        }
      }
      FloatArray::destroy(phase);
      FloatArray::destroy(magnitude);
    }
/*
    ComplexFloatArray subarray(int offset, int length);
*/
    if(success==true){
      debugMessage("Tests passed",passed);
    }
  };
Exemplo n.º 4
0
QImage RayTracer::render (const Vec3Df & camPos,
                          const Vec3Df & direction,
                          const Vec3Df & upVector,
                          const Vec3Df & rightVector,
                          float fieldOfView,
                          float aspectRatio,
                          unsigned int screenWidth,
                          unsigned int screenHeight) {
    QImage image (QSize (screenWidth, screenHeight), QImage::Format_RGB888);
    Scene * scene = Scene::getInstance ();
    std::vector<Light> lights = scene->getLights();
    Light light = lights[0];

    QProgressDialog progressDialog ("Raytracing...", "Cancel", 0, 100);
    progressDialog.show ();

    //#pragma omp parallel for
    for (unsigned int i = 0; i < screenWidth; i++) {
        progressDialog.setValue ((100*i)/screenWidth);
        for (unsigned int j = 0; j < screenHeight; j++) {


            float tanX = tan (fieldOfView)*aspectRatio;
            float tanY = tan (fieldOfView);

            //Nombre de découpe par dimension du pixel (Antialiasing)
            int aliaNb = 2;
            if(!activeAA) aliaNb=1;
            aliaNb++;

            Vec3Df c (backgroundColor);
            Vec3Df tempc(0.,0.,0.);
            for(int pixi=1; pixi<aliaNb; pixi++){
                for(int pixj=1; pixj<aliaNb; pixj++){
                    Vec3Df stepX = (float (i)-0.5+float(pixi)/float(aliaNb) - screenWidth/2.f)/screenWidth * tanX * rightVector;
                    Vec3Df stepY = (float (j)-0.5+float(pixj)/float(aliaNb) - screenHeight/2.f)/screenHeight * tanY * upVector;
                    Vec3Df step = stepX + stepY;

                    Vec3Df dir = direction + step;
                    dir.normalize ();
                    Vec3Df intersectionPoint;
                    Vec3Df IntersPointNormal;
                    float occlusion;

                    int idObj = getIntersectionPoint(camPos,dir,intersectionPoint,IntersPointNormal,occlusion);

                    if(idObj>=0)
                    {

                        tempc += Brdf(camPos, IntersPointNormal, idObj,intersectionPoint,occlusion,0)/std::pow(aliaNb-1,2);
                        c=tempc;

                    }
                }
            }

            image.setPixel (i, j, qRgb (clamp (c[0], 0, 255), clamp (c[1], 0, 255), clamp (c[2], 0, 255)));
        }
    }
    progressDialog.setValue (100);
    return image;
}