Example #1
0
void rayCast (int x, int y)
{
    // Ray tracing R3Vector
    R3Vector u;
    u.x = x - eye.x;
    u.y = y - eye.y;
    u.z = FILM_WALL_Z - eye.z;
    normalize (&u);
    
    Intersection * inter = intersectElement (u, eye, true);
    if (inter == NULL) return;

    Object * obj = inter->object;
    R3Vector inter_point = inter->point;
    Color c = pointColor (obj, inter_point, u, 0);
    delete inter;

    setFramebuffer (y, x, c.r, c.g, c.b);
}
Example #2
0
void MainWindow::playScene() {
    const unsigned nSubj = g_pointList.size();
    const unsigned nAnimationFrames = g_pointHistory.size();
	if (nAnimationFrames > 0 && nSubj > 0 && g_pointHistoryIdx < nAnimationFrames) {
        const unsigned nVars = g_pointList[0].size();
		const unsigned nPoints = nVars / POINT_DIMENSIONS;
        const unsigned currentImage = ui.listWidget->currentRow();
        if (currentImage >= nSubj) {
            cout << "Can't play due to current image change" << endl;
            return;
        }
        if (g_pointHistory[g_pointHistoryIdx].size() != nSubj * nVars) {
            cout << "Can't play frames due to the mismatch of number of particles ..." << endl;
            return;
        }
        const unsigned nSubjectOffset = currentImage * nVars;

        gs.clear();

        BitmapType::Pointer bitmap = g_bitmapList[currentImage];
        gs.addPixmap(getPixmap(bitmap));


        // hope to change particle colors to represent kappa values
		itk::Function::JetColormapFunction<double, itk::RGBAPixel<unsigned char> >::Pointer colorFunc = itk::Function::JetColormapFunction<double, itk::RGBAPixel<unsigned char> >::New();
		colorFunc->SetMinimumInputValue(0);
		colorFunc->SetMaximumInputValue(255);
		for (unsigned j = 0; j < nPoints; j++) {
			double x = g_pointHistory[g_pointHistoryIdx][nSubjectOffset + POINT_DIMENSIONS*j];
			double y = g_pointHistory[g_pointHistoryIdx][nSubjectOffset + POINT_DIMENSIONS*j + 1];

            ImageType::IndexType xyIdx;
            xyIdx[0] = x;
            xyIdx[1] = y;
            ImageType::PixelType xyVal = g_imageList[currentImage]->GetPixel(xyIdx);
            itk::RGBAPixel<unsigned char> rgb = colorFunc->operator()(xyVal);
			QColor pointColor(rgb[0], rgb[1], rgb[2]);
			gs.addEllipse(x, y, 1, 1, QPen(pointColor),
                          QBrush(pointColor, Qt::SolidPattern));
		}
	}
}
Example #3
0
void rotationWidget::paintEvent(QPaintEvent *){
  int side = qMin(width(), height());

  double_point3 vec(0,0,90);//centro sfera
  double_point3 front(0,0,80);
  double_point3 back(0,0,-80);
  double_point3 left(-80,0,0);
  double_point3 right(80,0,0);
  double_point3 top(0,80,0);
  double_point3 bottom(0,-80,0);

  rot.applyTransform(vec);
  rot.applyTransform(front);
  rot.applyTransform(back);
  rot.applyTransform(left);
  rot.applyTransform(right);
  rot.applyTransform(top);
  rot.applyTransform(bottom);

  QPainter painter(this);
  painter.setRenderHint(QPainter::Antialiasing);

  //coordinate tra -100 e 100
  painter.translate(width() / 2, height() / 2);
  painter.scale(side / 200.0, side / 200.0);
  
  QColor ellipseColor(0, 0, 127, 128);
  QColor pointColor(255, 0, 0);
  QColor xColor(255, 0, 0);
  QColor yColor(0, 255, 0);
  QColor zColor(0, 0, 255);
  
  if ( !isEnabled() ){
    ellipseColor=ellipseColor.darker(300);
    pointColor=pointColor.darker(300);
    xColor=xColor.darker(300);
    yColor=yColor.darker(300);
    zColor=zColor.darker(300);
  }
  
  if (vec.z()<0){ //sul retro
    painter.setBrush(pointColor);
    painter.setPen(Qt::SolidLine);
    painter.drawEllipse(QRectF(vec.x()-10,vec.y()-10,20,20));
  }
  
  painter.setPen(Qt::NoPen);
  painter.setBrush(ellipseColor);
  painter.drawEllipse(QRectF(-80,-80,160,160));

  painter.setPen(Qt::SolidLine);
  painter.setPen(zColor);
  painter.drawLine(QLine((int)front.x(),(int)front.y(),(int)back.x(),(int)back.y()));
  painter.setPen(xColor);
  painter.drawLine(QLine((int)left.x(),(int)left.y(),(int)right.x(),(int)right.y()));
  painter.setPen(yColor);
  painter.drawLine(QLine((int)top.x(),(int)top.y(),(int)bottom.x(),(int)bottom.y()));

  if (vec.z()>=0){ //sul fronte
    painter.setBrush(pointColor);
    painter.setPen(Qt::SolidLine); 
    painter.drawEllipse(QRectF(vec.x()-10,vec.y()-10,20,20));
  }  
}
Example #4
0
Color pointColor (Object * obj, R3Vector inter_point, R3Vector V, unsigned int level)
{
    Color c = obj->getColor ();
    // Ambient light
    Material mt = obj->getMaterial ();
    double k_a = mt.k_a;
    c.r *= ambient_light->getIntensity ().r  * k_a;
    c.g *= ambient_light->getIntensity ().g  * k_a;
    c.b *= ambient_light->getIntensity ().b  * k_a;
    R3Vector N = obj->getNormal (inter_point);

    // Diffuse and Specular Light
    for (unsigned int j = 0; j < lights.size (); j++)
    {
        Light * light = lights[j];

        // Verifies if its in shadow
        R3Vector itToLight = light->getCenter ();
        itToLight.x = light->getCenter ().x - inter_point.x;
        itToLight.y = light->getCenter ().y - inter_point.y;
        itToLight.z = light->getCenter ().z - inter_point.z;
        normalize (&itToLight);
        Intersection * inter = intersectElement (itToLight, inter_point, true);
        if (inter == NULL || inter->object != light)
            continue;
        delete inter;

        
        // Diffuse light
        double k_d = mt.k_d;
        Color cd = light->getDiffuseLight (N, inter_point, k_d);
        c.r += cd.r;
        c.g += cd.g;
        c.b += cd.b;
        
        // Specular light
        double k_s = mt.k_s;
        double n = mt.n;
        R3Vector E;
        E.x = -V.x;
        E.y = -V.y;
        E.z = -V.z;
        cd = light->getSpecularLight (N, inter_point, E, k_s, n);
        c.r += cd.r;
        c.g += cd.g;
        c.b += cd.b;
    }

    double k_r = mt.k_r;
    if (k_r > 0)
    {
        // Get reflection intersection
        V.x = -V.x;
        V.y = -V.y;
        V.z = -V.z;
        double VN = prod (V, N);
        R3Vector R = N;
        R.x = 2 * VN * R.x - V.x;
        R.y = 2 * VN * R.y - V.y;
        R.z = 2 * VN * R.z - V.z;
        normalize (&R);
        Intersection * inter = intersectElement (R, inter_point, true);


        Color refColor;
        if (inter != NULL)
        {
            Object * reflectingObject = inter->object;
            R3Vector reflectingPoint = inter->point;
            if (reflectingObject->isLight () || level == MAX_REFLECTION)
            {
                refColor = reflectingObject->getColor ();
            }
            else
            {
                refColor = pointColor (reflectingObject, reflectingPoint, R, level + 1);
            }
        }

        c.r += k_r * refColor.r;
        c.g += k_r * refColor.g;
        c.b += k_r * refColor.b;
    }

    return c;
}
Example #5
0
bool CaptureManager::SaveTrackImage(wxBitmap &bmp, int start, int end)
{
    if (end == -1) end = frameCount;
	if (!Access(0,0,false, true)->contourArray.size())
	{
		wxLogError(_T("No objects in the first frame. Detect/draw boundaries in the first frame and apply tracking first."));
		return false;
	}
	wxRealPoint scale(6, 6);
	bmp.Create(scale.x*size.width, scale.y*size.height);
	wxMemoryDC dc(bmp);
	dc.SetBackground(*wxWHITE_BRUSH);
	dc.Clear();

	for (int c=0; c<Access(0, 0, false, true)->contourArray.size(); c++)
	{
        std::vector<CvPoint> traj = GetTrajectory(c);
        std::vector< std::vector<double> > ratios;
        CvPoint lastTraj = traj[start];
        float maxRatio = 0;
        float minRatio = 0;
		for (int i=start+1; i<end && Access(i, 0, false, true)->contourArray.size() > c; i++)
        {
            CvSeq* oseq = Access(i-1, 0, false, true)->contourArray[c];
            CvSeq* seq = Access(i, 0, false, true)->contourArray[c];
            ratios.push_back(std::vector<double>());
            for (int j=0; j<oseq->total; j++)
            {
                CvPoint *lastLoc = (CvPoint*) cvGetSeqElem(oseq, j);
                if(seq->total <= j)
					continue;
                CvPoint *p = (CvPoint*) cvGetSeqElem(seq, j);
                float ptx = p->x - lastLoc->x;
                float pty = p->y - lastLoc->y;
                float trajx = traj[i].x - lastTraj.x;
                float trajy = traj[i].y - lastTraj.y;
                float ratio = (ptx*trajx + pty*trajy)/(trajx*trajx + trajy*trajy);
                if (maxRatio < ratio)
                    maxRatio = ratio;
                if (minRatio > ratio)
                    minRatio = ratio;
                ratios[i-1-start].push_back(ratio);
            }
            lastTraj = traj[i];
        }

		CvSeq* oseq = Access(start, 0, false, true)->contourArray[c];
		MyCanvas::DrawContour_static(&dc, oseq, wxPoint(0,0), scale);
		for (int i=0; i<oseq->total; i++)
		{
			CvPoint *lastLoc = (CvPoint*) cvGetSeqElem(oseq, i);
			dc.SetPen(wxPen(wxColour(Preferences::GetColorContourPointColor())));
			dc.SetBrush(*wxTRANSPARENT_BRUSH);
			for (int j=start+1; j<end; j++)
			{
				if(Access(j,0,false, true)->contourArray.size() <= c || Access(j,0,false, true)->contourArray[c]->total <= i)
					continue;
				CvPoint *p = (CvPoint*) cvGetSeqElem(Access(j, 0, false, true)->contourArray[c], i);
				dc.DrawLine(scale.x*lastLoc->x, scale.y*lastLoc->y, scale.x*p->x, scale.y*p->y);
				lastLoc = p;
			}
			//dc.DrawCircle(MyPoint((CvPoint*)cvGetSeqElem(Access(start, 0, false, true)->contourArray[c],i))*scale, Preferences::GetColorContourBorderWidth());
			for (int j=start+1; j<end; j++)
			{
			    wxColor pointColor(*wxLIGHT_GREY);
                if (ratios[j-1-start][i] > 0)
                    pointColor = wxColor(128 + 127*ratios[j-1-start][i]/maxRatio, 128 - 128*ratios[j-1-start][i]/maxRatio, 128 - 128*ratios[j-1-start][i]/maxRatio);
                else
                    pointColor = wxColor(128 - 128*ratios[j-1-start][i]/minRatio, 128 - 128*ratios[j-1-start][i]/minRatio, 128 + 127*ratios[j-1-start][i]/minRatio);
                dc.SetPen(wxPen(pointColor));
                dc.SetBrush(pointColor);
				if(Access(j, 0, false, true)->contourArray.size() <= c || Access(j, 0, false, true)->contourArray[c]->total <= i)
					continue;
				CvPoint *p = (CvPoint*) cvGetSeqElem(Access(j, 0, false, true)->contourArray[c], i);
				dc.DrawCircle(MyPoint(*p)*scale, Preferences::GetColorContourBorderWidth()*2);
			}
		}
		lastTraj = traj[start];
		dc.SetBrush(*wxTRANSPARENT_BRUSH);
		for (int i=start+1; i<end/*traj.size()*/; i++)
		{
			if(traj[i].x<0)
				continue;
			if (i>0)
			{
				dc.SetPen(wxPen(wxColour(Preferences::GetColorContourBorderColor()), Preferences::GetColorContourBorderWidth()));
				dc.DrawLine(scale.x*lastTraj.x,scale.y*lastTraj.y, scale.x*traj[i].x,scale.y*traj[i].y);
			}
			lastTraj = traj[i];
		}
		if(c < Access(end-1,0,false, true)->contourArray.size())
			MyCanvas::DrawContour_static(&dc, Access(end-1,0,false, true)->contourArray[c], wxPoint(0,0), scale, true, wxRED);
	}
	return true;
}