MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    // Setup UI
    ui->setupUi(this);
    // Set start tab as blank
    QLabel *newTab = new QLabel(ui->tabWidget);
    newTab->setText(tr("Neither camera connected nor file loaded."));
    newTab->setAlignment(Qt::AlignCenter);
    ui->tabWidget->addTab(newTab, "");
    ui->tabWidget->setTabsClosable(false);
    // Add "Connect to Camera" button to tab
    connectToCameraButton = new QPushButton();
    connectToCameraButton->setText(tr("Connect/Open"));
    ui->tabWidget->setCornerWidget(connectToCameraButton, Qt::TopLeftCorner);
    connect(connectToCameraButton,SIGNAL(released()),this, SLOT(connectToCamera()));
    connect(ui->tabWidget,SIGNAL(tabCloseRequested(int)),this, SLOT(disconnectCamera(int)));
    // Set focus on button
    connectToCameraButton->setFocus();
    connectToCameraButton->setShortcut(QKeySequence(Qt::CTRL+Qt::Key_O));
    // Connect other signals/slots
    connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(showAboutDialog()));
    connect(ui->actionHelp, SIGNAL(triggered()), this, SLOT(showHelpDialog()));
    connect(ui->actionAboutQt, SIGNAL(triggered()), this, SLOT(showAboutQtDialog()));
    connect(ui->actionQuit, SIGNAL(triggered()), this, SLOT(close()));
    connect(ui->actionFullScreen, SIGNAL(toggled(bool)), this, SLOT(setFullScreen(bool)));
    connect(ui->actionConnect_Open, SIGNAL(triggered()), this, SLOT(connectToCamera()));
    // Create SharedImageBuffer object
    sharedImageBuffer = new SharedImageBuffer();

    addCodecs();
}
Ejemplo n.º 2
0
void JpegReceiver::mainProc()
{
	Capture_Log::getInstance()->enable();
	int connectionAttempts = 0;
	while (true)
	{
		DWORD res = ::WaitForSingleObject( event_abort, 0);
//		DWORD tm = GetTickCount();
		if (res == WAIT_OBJECT_0)
		{
			break;
		}
		if (!started)
		{
			connectToCamera();
			++connectionAttempts;
			if (connectionAttempts > maxConnectionAttempts) break;
		}
		if (started)
		{
			nextStep();
//			Sleep(20);
		}
//		Capture_Log::getInstance()->log_write("TCP time is %d\n", GetTickCount() - tm);
	}
	sock.reset();
}
void MainWindow::connectToCamera()
{
    // Get next tab index
    int nextTabIndex = (deviceNumberMap.size()+fileNumberMap.size()==0) ? 0 : ui->tabWidget->count();
    // Show dialog
    CameraConnectDialog *cameraConnectDialog = new CameraConnectDialog(this);
    if(cameraConnectDialog->exec()==QDialog::Accepted)
    {
        if(cameraConnectDialog->isCamera()) {
            // Save user-defined device number
            int deviceNumber = cameraConnectDialog->getDeviceNumber();
            // Check if this camera is already connected
            if(!deviceNumberMap.contains(deviceNumber))
            {
                // Create ImageBuffer with user-defined size
                Buffer<Mat> *imageBuffer = new Buffer<Mat>(cameraConnectDialog->getImageBufferSize());
                // Add created ImageBuffer to SharedImageBuffer object
                sharedImageBuffer->add(deviceNumber, imageBuffer);
                // Create CameraView
                cameraViewMap[deviceNumber] = new CameraView(ui->tabWidget, deviceNumber, sharedImageBuffer);
                // Attempt to connect to camera
                if(cameraViewMap[deviceNumber]->connectToCamera(cameraConnectDialog->getDropFrameCheckBoxState(),
                                               cameraConnectDialog->getCaptureThreadPrio(),
                                               cameraConnectDialog->getProcessingThreadPrio(),
                                               cameraConnectDialog->getResolutionWidth(),
                                               cameraConnectDialog->getResolutionHeight(),
                                               cameraConnectDialog->getFpsNumber()))
                {
                    // Add to map
                    deviceNumberMap[deviceNumber] = nextTabIndex;
                    // Save tab label
                    QString tabLabel = cameraConnectDialog->getTabLabel();
                    // Allow tabs to be closed
                    ui->tabWidget->setTabsClosable(true);
                    // If start tab, remove
                    if(nextTabIndex==0)
                        ui->tabWidget->removeTab(0);
                    // Add tab
                    ui->tabWidget->addTab(cameraViewMap[deviceNumber], tabLabel + " [" + QString::number(deviceNumber) + "]");
                    ui->tabWidget->setCurrentWidget(cameraViewMap[deviceNumber]);
                    // Set tooltips
                    setTabCloseToolTips(ui->tabWidget, tr("Disconnect Camera"));
                    //Set Codec
                    cameraViewMap[deviceNumber]->setCodec(saveCodec);
                }
                // Could not connect to camera
                else
                {
                    // Display error message
                    QMessageBox::warning(this,tr("ERROR:"),tr("Could not connect to camera. Please check device number."));
                    // Explicitly delete widget
                    delete cameraViewMap[deviceNumber];
                    cameraViewMap.remove(deviceNumber);
                    // Remove from shared buffer
                    sharedImageBuffer->removeByDeviceNumber(deviceNumber);
                    // Explicitly delete ImageBuffer object
                    delete imageBuffer;
                }
            }
            // Display error message
            else
                QMessageBox::warning(this,tr("ERROR:"),tr("Could not connect to camera. Already connected."));
        }
        // Attempt to load a file
        else if(cameraConnectDialog->isFile()) {
            // Get the filepath from connectDialog
            QString filepath = cameraConnectDialog->getFilepath();
            // String can't be empty
            if(filepath.isEmpty()) {
                QMessageBox::warning(this,tr("ERROR:"),tr("Please enter path to video file."));
                connectToCamera();
                return;
            }
            // Load the File
            QFileInfo file(filepath);
            QString filename = file.fileName();
            if(file.exists()){
                // Create new Videofile entry in QMap
                videoViewMap[filename] = new VideoView(ui->tabWidget,filepath);
                // Attemp to load the video
                if(videoViewMap[filename]->loadVideo(cameraConnectDialog->getPlayerThreadPrio(),
                                                     cameraConnectDialog->getResolutionWidth(),
                                                     cameraConnectDialog->getResolutionHeight(),
                                                     cameraConnectDialog->getFpsNumber()))
                {
                    // Add to map
                    fileNumberMap[filename] = nextTabIndex;
                    // Save tab label
                    QString tabLabel = cameraConnectDialog->getTabLabel();
                    // Allow tabs to be closed
                    ui->tabWidget->setTabsClosable(true);
                    // If start tab remove
                    if(nextTabIndex==0)
                        ui->tabWidget->removeTab(0);
                    // Add tab
                    ui->tabWidget->addTab(videoViewMap[filename], tabLabel+"["+filename+"]");
                    ui->tabWidget->setCurrentWidget(videoViewMap[filename]);
                    // Set tooltips
                    setTabCloseToolTips(ui->tabWidget, tr("Remove Video"));
                    // Set Codec
                    videoViewMap[filename]->setCodec(saveCodec);
                    videoViewMap[filename]->set_useVideoCodec(useVideoCodec);
                }
                // Could not load video
                else {
                    // Display error message
                    QMessageBox::warning(this,tr("ERROR:"),tr("Could not convert Video. Please check file format."));
                    // Explicitly delete widget
                    delete videoViewMap[filename];
                    videoViewMap.remove(filename);
                }
            }
            // Error if file does not exist
            else {
                QMessageBox::warning(this,tr("ERROR:"),tr("File does not exist."));
                connectToCamera();
                return;
            }
        }
    }
    // Delete dialog
    delete cameraConnectDialog;
}
void BidirPathTracing::runIteration(int iter)
{
	lightPathNum = height * width;
	cameraPathNum = lightPathNum;

	lightStateIndex.resize(lightPathNum);
	memset(&lightStateIndex[0] , 0 , lightStateIndex.size() * sizeof(int));

	lightStates.reserve(lightPathNum);
	lightStates.clear();

	cameraStates.reserve(cameraPathNum);
	cameraStates.clear();

	// generating light paths
	for (int pathIndex = 0; pathIndex < lightPathNum; pathIndex++)
	{
		BidirPathState lightState;

		bool deltaLight;
		generateLightSample(lightState);

		int s = 1;

		for (;; lightState.pathLength++ , s++)
		{
			Ray ray(lightState.origin + lightState.dir * EPS ,
				lightState.dir);
			Intersection inter;
			if (scene.intersect(ray , inter) == NULL)
				break;

			Vector3 hitPos = inter.p;

			BSDF bsdf(-ray.dir , inter , scene);
			if (!bsdf.isValid())
				break;

			lightState.pos = hitPos;
			lightState.bsdf = bsdf;

			if (lightState.pathLength > 1 || lightState.isFiniteLight)
			{
				lightState.dVCM *= mis(SQR(inter.t));
			}
			lightState.dVCM /= mis(std::abs(bsdf.cosWi()));
			lightState.dVC /= mis(std::abs(bsdf.cosWi()));
			
			if (!bsdf.isDelta)
				lightStates.push_back(lightState);

			// connect to camera
			if (!bsdf.isDelta)
			{
				if (lightState.pathLength + 1 >= minPathLength 
					&& lightState.pathLength + 1 == controlLength)
				{
					Vector3 imagePos = scene.camera.worldToRaster.tPoint(hitPos);
					if (scene.camera.checkRaster(imagePos.x , imagePos.y))
					{
						Color3 res = connectToCamera(lightState , hitPos , bsdf);

						// weight
						//Real weight = 1.f / (lightState.pathLength + 1 - lightState.specularVertexNum);

						film->addColor((int)imagePos.x , (int)imagePos.y , res);
					}
				}
			}

			if (lightState.pathLength + 2 > maxPathLength)
				break;

			if (!sampleScattering(bsdf , hitPos , lightState))
				break;
		}

		lightStateIndex[pathIndex] = (int)lightStates.size();
	}

	// generating camera paths
	for (int index = 0; index < cameraPathNum; index++)
	{
		int pathIndex = index % (height * width);

		if (pathIndex == 111 * 512 + 364)
		{
			int flag = 1;
		}

		BidirPathState cameraState;
		Vector3 screenSample = generateCameraSample(pathIndex , cameraState);

		Color3 color(0);

		for (;; cameraState.pathLength++)
		{
			Ray ray(cameraState.origin + cameraState.dir * EPS ,
				cameraState.dir);

			Intersection inter;

			if (scene.intersect(ray , inter) == NULL)
			{
				/*
				if (scene.background != NULL)
				{
					if (cameraState.pathLength >= minPathLength)
					{
						// weight
						Real weight = 1.f / (cameraState.pathLength - cameraState.specularVertexNum);

						color = color + (cameraState.throughput |
							getLightRadiance(scene.background ,
							cameraState , Vector3(0) , ray.dir)) * weight;
					}
				}
				*/
				break;
			}

			Vector3 hitPos = inter.p;

			BSDF bsdf(-ray.dir , inter , scene);
			if (!bsdf.isValid())
				break;

			cameraState.dVCM *= mis(SQR(inter.t));
			cameraState.dVCM /= mis(std::abs(bsdf.cosWi()));
			cameraState.dVC /= mis(std::abs(bsdf.cosWi()));

			if (inter.matId < 0)
			{
				AbstractLight *light = scene.lights[-inter.matId - 1];

				if (cameraState.pathLength >= minPathLength
					&& cameraState.pathLength == controlLength)
				{
					// weight
					//Real weight = 1.f / (cameraState.pathLength - cameraState.specularVertexNum);

					color = color + (cameraState.throughput |
						getLightRadiance(light , cameraState , 
						hitPos , ray.dir));
				}
				break;
			}

			if (cameraState.pathLength >= maxPathLength)
				break;
            
			// vertex connection: connect to light source
			if (!bsdf.isDelta)
			{
				if (cameraState.pathLength + 1 >= minPathLength
					&& cameraState.pathLength + 1 == controlLength)
				{
					// weight
					Real weight = 1.f / (cameraState.pathLength + 1.f - cameraState.specularVertexNum);

					color = color + (cameraState.throughput |
						getDirectIllumination(cameraState , hitPos , bsdf)) *
						weight;
				}
			}

			// vertex connection: connect to light vertices
			if (!bsdf.isDelta)
			{
				int st , ed;
				if (pathIndex == 0)
					st = 0;
				else
					st = lightStateIndex[pathIndex - 1];
				ed = lightStateIndex[pathIndex];

				for (int i = st; i < ed; i++)
				{
					BidirPathState& lightState = lightStates[i];

					if (lightState.pathLength + 1 + 
						cameraState.pathLength < minPathLength)
						continue;

					if (lightState.pathLength + 1 +
						cameraState.pathLength > maxPathLength)
						break;
					
					if (lightState.bsdf.isDelta)
						continue;

					Color3 tmp = connectVertices(lightState ,
						bsdf , hitPos , cameraState);

					// weight
					Real weight = 1.f / (lightState.pathLength + 1.f +
						cameraState.pathLength - lightState.specularVertexNum - 
						cameraState.specularVertexNum);

					if (lightState.pathLength + 1 + cameraState.pathLength ==
						controlLength)
					color = color + (cameraState.throughput |
						lightState.throughput | tmp) * weight;
				}
			}

			if (!sampleScattering(bsdf , hitPos , cameraState))
				break;
		}

		film->addColor((int)screenSample.x , (int)screenSample.y , color);
	}
}