Пример #1
0
Vec3f RayTracer::traceRay(Ray &ray, float tmin, int bounces, float weight,
	float indexOfRefraction, Hit &hit) const
{
	//printf("当前已有光线:\n");
	//RayTree::Print();

	Vec3f canswer;
	if (bounces > max_bounces)
		return Vec3f(0.0f, 0.0f, 0.0f); 
	Camera *camera = sceneParser->getCamera();
	Group *group = sceneParser->getGroup();
	int num_lights = sceneParser->getNumLights();
	Vec3f cambient = sceneParser->getAmbientLight();
	//原来最后是这里出了问题,一旦碰到有转换的物体,那么hit带出来的值是
	//转换后的视线看到的值,而非本来视线看到的值
	//所以解决方案是:距离不变,根据距离重新计算焦点
	if (group->intersect(ray, hit, tmin))//撞到了
	{
		if (is_view_ray)
		{
			RayTree::SetMainSegment(ray, 0, hit.getT());
			is_view_ray = false;
		}
		Vec3f cobject = hit.getMaterial()->getDiffuseColor();
		Vec3f hitPoint = hit.getIntersectionPoint();
		//环境光部分
		canswer = cambient * cobject;
		Vec3f clight;//光的颜色
		Vec3f light_dir;//指向光的方向
		Vec3f normal_dir = hit.getNormal();//交点法线向量
		float distolight;//距离光源的距离
		for (int i = 0; i < num_lights; i++)
		{
			Light *light = sceneParser->getLight(i);
			//light_dir : the direction to the light
			// 该方法用于获得指向光的方向,光的颜色,和到达光的距离
			// 第一个参数传递的是焦点信息
			light->getIllumination(hitPoint, light_dir, clight, distolight);

			Ray ray2(hitPoint, light_dir);
			Vec3f init_normal(0, 0, 0);
			Hit hit2(distolight, NULL, init_normal);
			//阴影检测
			if (shadow)
			{
				if (group->intersect(ray2, hit2, tmin)){
					RayTree::AddShadowSegment(ray2, 0, hit2.getT());
					continue;
				}
				RayTree::AddShadowSegment(ray2, 0, hit2.getT());
			}
			//cpixel  =  cambient * cobject + SUMi [ clamped(Li . N) * clighti * cobject ]
			//返回局部光
			canswer = canswer + hit.getMaterial()->Shade(ray, hit, light_dir, clight);
		}

		//printf("当前已有光线:\n");
		//RayTree::Print();

		
		//反射光
		Material *material = hit.getMaterial();
		Vec3f rc = material->getReflectiveColor();
		if (rc.r() > 0 && rc.g() > 0 && rc.b() > 0)
		{
			Vec3f mirrorDir;
			Vec3f incoming = ray.getDirection();
			mirrorDir = mirrorDirection(normal_dir, incoming);
			// The ray weight is simply multiplied by the magnitude of the reflected color
			Ray ray3(hitPoint, mirrorDir);
			Vec3f init_normal(0, 0, 0);
			Hit hit3(distolight, NULL, init_normal);
			//忘记乘以本身的反射光系数%…………
			canswer += traceRay(ray3, tmin, bounces + 1, weight*rc.Length(), indexOfRefraction, hit3)*rc;
			if (bounces + 1 < max_bounces)
				RayTree::AddReflectedSegment(ray3, 0, hit3.getT());
		}

		//printf("当前已有光线:\n");
		//RayTree::Print();


		//从这里开始还都存在问题!!!!!
		//折射光
		Vec3f transmitted;
		Vec3f tc = material->getTransparentColor();
		float index = material->getIndexOfRefraction();
		if (tc.r() > 0 && tc.g() > 0 && tc.b() > 0)
		{
			Vec3f init_normal(0, 0, 0);
			Hit hit4(distolight, NULL, init_normal);
			//在判断折射光的存在之后,要考虑光线的位置:物体内还是物体外
			//这里根据normal和incoming的点积来判断
			Vec3f incoming = ray.getDirection();
			float judge = normal_dir.Dot3(incoming);
			if (judge < 0)//光线在外
			{
				if (transmittedDirection(normal_dir, incoming, 1, index, transmitted))
				{
					Ray ray4(hitPoint, transmitted);
					canswer += traceRay(ray4, tmin, bounces+1, weight*rc.Length(), index, hit4)*tc;
					RayTree::AddTransmittedSegment(ray4, 0, hit4.getT());
				}
			}
			else//光线在内
			{
				normal_dir.Negate();
				if (transmittedDirection(normal_dir, incoming, index, 1, transmitted))
				{
					Ray ray4(hitPoint, transmitted);
					canswer += traceRay(ray4, tmin, bounces+1, weight*rc.Length(), 1, hit4)*tc;
					RayTree::AddTransmittedSegment(ray4, 0, hit4.getT());
				}
			}
		}

		//printf("当前已有光线:\n");
		//RayTree::Print();

	}
	else
		canswer = sceneParser->getBackgroundColor();

	canswer.Clamp();
	return canswer;
}
Пример #2
0
void compare_wls1(TString filename="../p15m_nwls/wcsim.root",TString histoname="p15m_nwls", Int_t *flag, TString rootfile = "temp.root") {

    TFile *file1;
    if (*flag!=0) {
        //file1 = new TFile(rootfile,"Update");
        file1 = new TFile(rootfile,"RECREATE");
    } else {
        file1 = new TFile(rootfile,"RECREATE");
    }
    TString filename1;
    filename1 = histoname + "_digi";
    TTree *T = new TTree(filename1,filename1);
    T->SetDirectory(file1);
    Double_t diginpe,digitime,cor_digitime,digitheta,dis_digihit;
    Int_t neve;

    Double_t mom;
    Double_t pos_x,pos_y,pos_z;
    Double_t dir_x,dir_y,dir_z;
    Double_t tube_x,tube_y,tube_z;
    Double_t totankdis;
    Double_t vertex[3],dir[3];

    Double_t tube_pos[3];

    T->Branch("digi_eve",&neve,"data/I");
    T->Branch("diginpe",&diginpe,"data/D");
    T->Branch("digitime",&digitime,"data/D");
    T->Branch("cor_digitime",&cor_digitime,"data/D");
    T->Branch("digitheta",&digitheta,"data/D");
    T->Branch("dis_dighit",&dis_digihit,"data/D");

    T->Branch("mom",&mom,"data/D");
    T->Branch("totankdis",&totankdis,"data/D");
    T->Branch("pos_x",&vertex[0],"data/D");
    T->Branch("pos_y",&vertex[1],"data/D");
    T->Branch("pos_z",&vertex[2],"data/D");

    T->Branch("dir_x",&dir[0],"data/D");
    T->Branch("dir_y",&dir[1],"data/D");
    T->Branch("dir_z",&dir[2],"data/D");

    T->Branch("tube_x",&tube_pos[0],"data/D");
    T->Branch("tube_y",&tube_pos[1],"data/D");
    T->Branch("tube_z",&tube_pos[2],"data/D");

    filename1 = histoname + "_hit";
    TTree *t1 = new TTree(filename1,filename1);
    t1->SetDirectory(file1);

    Double_t wavelength, truetime, corr_time,theta,distance,index;
    Int_t qe_flag,parentid,tubeid,totalpe;

    Int_t ntracks;

    t1->Branch("ntracks",&ntracks,"data/I");
    t1->Branch("neve",&neve,"data/I");
    t1->Branch("wavelength",&wavelength,"data/D");
    t1->Branch("truetime",&truetime,"data/D");
    t1->Branch("corr_time",&corr_time,"data/D");
    t1->Branch("theta",&theta,"data/D");
    t1->Branch("distance",&distance,"data/D");
    t1->Branch("index",&index,"data/D");

    t1->Branch("mom",&mom,"data/D");
    t1->Branch("totankdis",&totankdis,"data/D");
    t1->Branch("pos_x",&vertex[0],"data/D");
    t1->Branch("pos_y",&vertex[1],"data/D");
    t1->Branch("pos_z",&vertex[2],"data/D");

    t1->Branch("dir_x",&dir[0],"data/D");
    t1->Branch("dir_y",&dir[1],"data/D");
    t1->Branch("dir_z",&dir[2],"data/D");

    t1->Branch("tube_x",&tube_pos[0],"data/D");
    t1->Branch("tube_y",&tube_pos[1],"data/D");
    t1->Branch("tube_z",&tube_pos[2],"data/D");

    // t1->Branch("pos_x",&pos_x,"data/D");
//   t1->Branch("pos_y",&pos_y,"data/D");
//   t1->Branch("pos_z",&pos_z,"data/D");

//   t1->Branch("dir_x",&dir_x,"data/D");
//   t1->Branch("dir_y",&dir_y,"data/D");
//   t1->Branch("dir_z",&dir_z,"data/D");

//   t1->Branch("tube_x",&tube_x,"data/D");
//   t1->Branch("tube_y",&tube_y,"data/D");
//   t1->Branch("tube_z",&tube_z,"data/D");

    t1->Branch("qe_flag",&qe_flag,"data/I");
    t1->Branch("parentid",&parentid,"data/I");
    t1->Branch("tubeid",&tubeid,"data/I");
    t1->Branch("totalpe",&totalpe,"data/I");


    TFile *file = new TFile(filename);
    TTree  *wcsimT = file->Get("wcsimT");
    WCSimRootEvent *wcsimrootsuperevent = new WCSimRootEvent();
    wcsimT->SetBranchAddress("wcsimrootevent",&wcsimrootsuperevent);
    wcsimT->GetBranch("wcsimrootevent")->SetAutoDelete(kTRUE);



    TTree *gtree = file->Get("wcsimGeoT");
    WCSimRootGeom *wcsimrootgeom = new WCSimRootGeom();
    gbranch = gtree->GetBranch("wcsimrootgeom");
    gbranch->SetAddress(&wcsimrootgeom);
    gtree->GetEntry(0);

    WCSimRootPMT *pmt;

    Double_t pmt_pos[500000][3];

    for (Int_t i=0; i!=wcsimrootgeom->GetWCNumPMT(); i++) {
        pmt_pos[i][0] = (wcsimrootgeom->GetPMT(i)).GetPosition(0);
        pmt_pos[i][1] = (wcsimrootgeom->GetPMT(i)).GetPosition(1);
        pmt_pos[i][2] = (wcsimrootgeom->GetPMT(i)).GetPosition(2);
    }

    //in terms of wavelength (total NPE) real hit
    filename1 = histoname + "_total_wl";
    TH1F *hqx = new TH1F(filename1,filename1,600,200,800);

    //NPE in each event sum over digi hit
    filename1 = histoname + "_total_npe";
    TH1F *hqx2 = new TH1F(filename1,filename1,1000,0.,10000);

    //digitized hit time
    filename1 = histoname + "_digitime";
    TH1F *hqx1 = new TH1F(filename1,filename1,500,900,1400);

    //corrected digitized hit time
    filename1 = histoname + "_cor_digitime";
    TH1F *hqx4 = new TH1F(filename1,filename1,1000,400,1400);

    //digitized hit angle
    filename1 = histoname + "_digitheta";
    TH1F *hqx5 = new TH1F(filename1,filename1,180,0,180);



    //TH2F *h1 = new TH2F("h1","h1",100,1000,20000,100,90000,140000);

    Double_t index = 1.333;



    neve = *flag;

    cout << histoname << "\t" << wcsimT->GetEntries() << endl;
    for (Int_t j=0; j!=wcsimT->GetEntries(); j++) {
        //for (Int_t j=0;j!=90;j++){
        // cout << j << endl;
        wcsimT->GetEvent(j);
        neve ++;

        WCSimRootTrigger *wcsimrootevent = wcsimrootsuperevent->GetTrigger(0);
        temp = (TClonesArray*)wcsimrootevent->GetTracks();

        Int_t ntrack =  wcsimrootevent->GetNtrack();
        //cout << ntrack << endl;
        ntracks = ntrack;

        mom = ((WCSimRootTrack*)temp->At(ntrack-1))->GetP();
        //get the vertex information
        vertex[0] = ((WCSimRootTrack*)temp->At(ntrack-1))->GetStart(0);
        vertex[1] = ((WCSimRootTrack*)temp->At(ntrack-1))->GetStart(1);
        vertex[2] = ((WCSimRootTrack*)temp->At(ntrack-1))->GetStart(2);

        //get position information
        dir[0] = ((WCSimRootTrack*)temp->At(ntrack-1))->GetDir(0);
        dir[1] = ((WCSimRootTrack*)temp->At(ntrack-1))->GetDir(1);
        dir[2] = ((WCSimRootTrack*)temp->At(ntrack-1))->GetDir(2);

        totankdis=ToTankDistance(vertex,dir);

        TVector3 vertex3(vertex[0],vertex[1],vertex[2]);
        TVector3 dir3(dir[0],dir[1],dir[2]);


        //loop through digi hit
        int max = wcsimrootevent->GetNcherenkovdigihits();

        double sum = 0;

        for (int i=0; i<max; i++) {
            // cout << max << "\t" << i << endl;
            WCSimRootCherenkovDigiHit *cDigiHit = ((WCSimRootCherenkovDigiHit*)wcsimrootevent->GetCherenkovDigiHits()->At(i));
            hqx1->Fill(cDigiHit->GetT());
            tube_pos[0] =  pmt_pos[(cDigiHit->GetTubeId()-1)][0];
            tube_pos[1] =  pmt_pos[(cDigiHit->GetTubeId()-1)][1];
            tube_pos[2] =  pmt_pos[(cDigiHit->GetTubeId()-1)][2];

            TVector3 hit3(tube_pos[0],tube_pos[1],tube_pos[2]);
            TVector3 dis = hit3-vertex3;

            diginpe = cDigiHit->GetQ();
            digitime = cDigiHit->GetT();
            cor_digitime = digitime-dis.Mag()/299792458.*1.333*1.e7;
            digitheta = dis.Angle(dir3)/3.1415926*180.;
            dis_digihit = dis.Mag();


            hqx4->Fill(cor_digitime,diginpe);
            hqx5->Fill(digitheta,diginpe);
            sum += diginpe;

            T->Fill();
        }
        hqx2->Fill(sum);


        //loop through real hit



        //loop through PMT hit first
        max = wcsimrootevent-> GetNcherenkovhits();
        //cout << max << endl;
        if (max ==0) {
            t1->Fill();
        }
        for (int i=0; i<max; i++) {
            WCSimRootCherenkovHit* wcsimrootcherenkovhit =
                dynamic_cast<WCSimRootCherenkovHit*>((wcsimrootevent->GetCherenkovHits())->At(i));

            totalpe = wcsimrootcherenkovhit->GetTotalPe(1);
            tubeid = wcsimrootcherenkovhit->GetTubeID() ;

            //loop through hit time etc
            for (int k=0; k<totalpe; k++) {
                TObject *element2 = (wcsimrootevent->GetCherenkovHitTimes())->
                                    At(wcsimrootcherenkovhit->GetTotalPe(0)+k);
                WCSimRootCherenkovHitTime *wcsimrootcherenkovhittime
                    = dynamic_cast<WCSimRootCherenkovHitTime*>(element2);

                wavelength =wcsimrootcherenkovhittime->GetWavelength();
                qe_flag = wcsimrootcherenkovhittime->GetQe_flag();
                truetime = wcsimrootcherenkovhittime->GetTruetime();
                parentid = wcsimrootcherenkovhittime->GetParentID();

                pos_x = wcsimrootcherenkovhittime->GetPosX() ;
                pos_y = wcsimrootcherenkovhittime->GetPosY() ;
                pos_z = wcsimrootcherenkovhittime->GetPosZ() ;
                dir_x = wcsimrootcherenkovhittime->GetDirX() ;
                dir_y = wcsimrootcherenkovhittime->GetDirY() ;
                dir_z = wcsimrootcherenkovhittime->GetDirZ() ;

                tube_pos[0] =  pmt_pos[tubeid-1][0];
                tube_pos[1] =  pmt_pos[tubeid-1][1];
                tube_pos[2] =  pmt_pos[tubeid-1][2];

                tube_x = tube_pos[0];
                tube_y = tube_pos[1];
                tube_z = tube_pos[2];



                TVector3 hit3(tube_pos[0],tube_pos[1],tube_pos[2]);
                TVector3 dis = hit3-vertex3;

                distance = dis.Mag();
                theta = dis.Angle(dir3)/3.1415926*180.;
                //index = index(wavelength);
                index = 1.34;
                corr_time = truetime - distance/299792458.*1e7*index;

                if (qe_flag==1) {
                    hqx->Fill(wavelength);
                }

                t1->Fill();
            }
        }



    }

    if (flag==1) {
        hqx->SetDirectory(file1);
        hqx2->SetDirectory(file1);
        hqx1->SetDirectory(file1);
        hqx4->SetDirectory(file1);
        hqx5->SetDirectory(file1);
        file1->Write();
        file1->Close();
    } else {
        hqx->SetDirectory(file1);
        hqx2->SetDirectory(file1);
        hqx1->SetDirectory(file1);
        hqx4->SetDirectory(file1);
        hqx5->SetDirectory(file1);
        file1->Write();
        file1->Close();
    }

    *flag = neve;

}
Пример #3
0
//没用 weight
Vec3f RayTracer::traceRay(Ray &ray, float tmin, int bounces, float indexOfRefraction, Hit &hit, Grid *grid) const
{
	if(bounces >= max_bounces)
		return Vec3f(0,0,0);
	RayTracingStats::IncrementNumNonShadowRays();
	//Group *group = s->getGroup();
	//group->intersect(ray,hit,tmin);  这里是没用grid的代码
	grid->intersect(ray,hit,tmin);
	
	if(hit.getMaterial()==NULL)
		return s->getBackgroundColor();
	else
	{
		RayTracingStats::IncrementNumShadowRays();
		Vec3f col(0,0,0);
		Vec3f hitPoint = hit.getIntersectionPoint();
		Vec3f tempAmb;
		Vec3f::MultRow(tempAmb,s->getAmbientLight(),hit.getMaterial()->getDiffuse(hitPoint));  //Kd La
		col += tempAmb;
		int lightNumber = s->getNumLights();
		Light *light;
		for(int i=0; i<lightNumber; i++)
		{
			light = s->getLight(i);
			Vec3f lightColor;
			Vec3f dirToLight;
			//Vec3f interPoint = hit.getIntersectionPoint();
			float distanceToLight;
			light->getIllumination(hitPoint,dirToLight,lightColor,distanceToLight);
			if(!castShadowRayGrid(hitPoint-ray.getDirection()*EPSILON,dirToLight,distanceToLight,grid))
			{
				Vec3f tempShade = hit.getMaterial()->Shade(ray,hit,dirToLight,lightColor);  //diffuse specular
				col += tempShade;
			}
		}
		if(hit.getMaterial()->isReflect(hitPoint))  
		{
			Ray rayReflect(mirrorDirection(hit.getNormal(),ray.getDirection()),hitPoint);
			Vec3f tempRefl;
			Hit hit2(1000,NULL,Vec3f(1,1,1));
			Vec3f::MultRow(tempRefl,hit.getMaterial()->getReflect(hitPoint),traceRay(rayReflect,tmin,bounces+1,indexOfRefraction,hit2,grid)); //weight,indexOfRefrection
			col += tempRefl;
		}
		if(hit.getMaterial()->isTransparent(hitPoint))
		{
			bool notTotalReflective;
			Vec3f transmittedDir;
			if(ray.getDirection().Dot3(hit.getNormal())>0) //ray is inside the object
			{
				notTotalReflective = transmittedDirection(hit.getNormal()*(-1.0f),ray.getDirection(),hit.getMaterial()->getIndexOfRefrac(hitPoint),indexOfRefraction,transmittedDir);
			}
			else  //outside
			{
				notTotalReflective = transmittedDirection(hit.getNormal(),ray.getDirection(),indexOfRefraction,hit.getMaterial()->getIndexOfRefrac(hitPoint),transmittedDir);
			}
			
			if(notTotalReflective)
			{
				Ray rayTransparent(transmittedDir,hitPoint);
				Vec3f tempTrans;
				Hit hit3(10000,NULL,Vec3f(1,1,1));
				Vec3f::MultRow(tempTrans,hit.getMaterial()->getTrans(hitPoint),traceRay(rayTransparent,tmin,bounces+1,indexOfRefraction,hit3,grid));
				col += tempTrans;
			}
			else
			{
				Vec3f tempTotalTrans;
				Vec3f::MultRow(tempTotalTrans,hit.getMaterial()->getTrans(hitPoint),hit.getMaterial()->getDiffuse(hitPoint));
				col += tempTotalTrans;
			}
		}
		return col;
	}
}