Skip to content
Snippets Groups Projects
src_window.cpp 5.59 KiB
Newer Older
#include "src_window.hpp"

Nicolas Pope's avatar
Nicolas Pope committed
#include "screen.hpp"
#include "camera.hpp"
#include <nanogui/imageview.h>
#include <nanogui/textbox.h>
#include <nanogui/slider.h>
#include <nanogui/combobox.h>
#include <nanogui/label.h>
#include <nanogui/opengl.h>
#include <nanogui/glutil.h>
#include <nanogui/screen.h>
#include <nanogui/layout.h>
#include <nanogui/vscrollpanel.h>
#ifdef HAVE_LIBARCHIVE
#include "ftl/rgbd/snapshot.hpp"
#include "thumbview.hpp"

using ftl::gui::SourceWindow;
Nicolas Pope's avatar
Nicolas Pope committed
using ftl::gui::Screen;
using ftl::rgbd::Source;
using std::string;
using std::vector;
Nicolas Pope's avatar
Nicolas Pope committed
using ftl::config::json_t;
Nicolas Pope's avatar
Nicolas Pope committed
SourceWindow::SourceWindow(ftl::gui::Screen *screen)
		: nanogui::Window(screen, ""), screen_(screen) {
	setLayout(new nanogui::BoxLayout(nanogui::Orientation::Vertical, nanogui::Alignment::Fill, 20, 5));

	using namespace nanogui;
Nicolas Pope's avatar
Nicolas Pope committed
	//if (!screen->root()->get<json_t>("sources")) {
	//	screen->root()->getConfig()["sources"] = json_t::array();
	//}

	//src_ = ftl::create<Source>(ctrl->getRoot(), "source", ctrl->getNet());
Nicolas Pope's avatar
Nicolas Pope committed
	//Widget *tools = new Widget(this);
	//    tools->setLayout(new BoxLayout(Orientation::Horizontal,
	//                                   Alignment::Middle, 0, 6));
	auto label = new Label(this, "Select Camera","sans-bold",20);

	//auto select = new ComboBox(this, available_);
	//select->setCallback([this,select](int ix) {
Nicolas Pope's avatar
Nicolas Pope committed
		//src_->set("uri", available_[ix]);
		// TODO(Nick) Check camera exists first
	//	screen_->setActiveCamera(cameras_[available_[ix]]);
	//	LOG(INFO) << "Change source: " << ix;
	//});
	auto vscroll = new VScrollPanel(this);
	ipanel_ = new Widget(vscroll);
	ipanel_->setLayout(new GridLayout(nanogui::Orientation::Horizontal, 2,
		nanogui::Alignment::Middle, 0, 5));
	//ipanel_ = new ImageView(vscroll, 0);
	screen->net()->onConnect([this](ftl::net::Peer *p) {
		UNIQUE_LOCK(mutex_, lk);
		//select->setItems(available_);
		_updateCameras(screen_->net()->findAll<string>("list_streams"));
	UNIQUE_LOCK(mutex_, lk);

	std::vector<ftl::rgbd::Source*> srcs = ftl::createArray<ftl::rgbd::Source>(screen_->root(), "sources", screen_->net());
	for (auto *src : srcs) {
		available_.push_back(src->getURI());
	}

	_updateCameras(screen_->control()->getNet()->findAll<string>("list_streams"));
Nicolas Pope's avatar
Nicolas Pope committed
	/*new Label(this, "Source Options","sans-bold");
Nicolas Pope's avatar
Nicolas Pope committed

	auto tools = new Widget(this);
    tools->setLayout(new BoxLayout(Orientation::Horizontal,
                                       Alignment::Middle, 0, 6));

	auto button_rgb = new Button(tools, "RGB");
	button_rgb->setTooltip("RGB left image");
	button_rgb->setFlags(Button::RadioButton);
	button_rgb->setPushed(true);
	button_rgb->setChangeCallback([this](bool state) { mode_ = Mode::rgb; });

Nicolas Pope's avatar
Nicolas Pope committed
	auto button_depth = new Button(tools, "Depth");
	button_depth->setFlags(Button::RadioButton);
	button_depth->setChangeCallback([this](bool state) { mode_ = Mode::depth; });

Nicolas Pope's avatar
Nicolas Pope committed
	auto button_stddev = new Button(tools, "SD. 25");
	button_stddev->setTooltip("Standard Deviation over 25 frames");
	button_stddev->setFlags(Button::RadioButton);
	button_stddev->setChangeCallback([this](bool state) { mode_ = Mode::stddev; });
Nicolas Pope's avatar
Nicolas Pope committed
	//auto button_pose = new Button(this, "Adjust Pose", ENTYPO_ICON_COMPASS);
	//button_pose->setCallback([this]() {
	//	auto posewin = new PoseWindow(screen_, screen_->control(), src_->getURI());
	//	posewin->setTheme(theme());
	//});
#ifdef HAVE_LIBARCHIVE
Nicolas Pope's avatar
Nicolas Pope committed
	auto button_snapshot = new Button(this, "Snapshot", ENTYPO_ICON_IMAGES);
	button_snapshot->setCallback([this] {
Nicolas Pope's avatar
Nicolas Pope committed
			/*char timestamp[18];
			std::time_t t=std::time(NULL);
			std::strftime(timestamp, sizeof(timestamp), "%F-%H%M%S", std::localtime(&t));
			auto writer = ftl::rgbd::SnapshotWriter(std::string(timestamp) + ".tar.gz");
			cv::Mat rgb, depth;
			this->src_->getFrames(rgb, depth);
			if (!writer.addCameraRGBD(
					"0", // TODO
					rgb,
					depth,
					this->src_->getPose(),
					this->src_->parameters()
				)) {
				LOG(ERROR) << "Snapshot failed";
			}
		}
		catch(std::runtime_error) {
			LOG(ERROR) << "Snapshot failed (file error)";
		}
	});
#endif
Nicolas Pope's avatar
Nicolas Pope committed
	//auto imageView = new VirtualCameraView(this);
	//cam.view = imageView;
Nicolas Pope's avatar
Nicolas Pope committed
	//imageView->setGridThreshold(20);
	//imageView->setSource(src_);
Nicolas Pope's avatar
Nicolas Pope committed
	//image_ = imageView;*/
}

void SourceWindow::_updateCameras(const vector<string> &netcams) {
	for (auto s : netcams) {
Nicolas Pope's avatar
Nicolas Pope committed
		if (cameras_.find(s) == cameras_.end()) {
			available_.push_back(s);
Nicolas Pope's avatar
Nicolas Pope committed
			json_t srcjson;
			srcjson["uri"] = s;
			screen_->root()->getConfig()["sources"].push_back(srcjson);
	std::vector<ftl::rgbd::Source*> srcs = ftl::createArray<ftl::rgbd::Source>(screen_->root(), "sources", screen_->net());
	for (auto *src : srcs) {
		if (cameras_.find(src->getURI()) == cameras_.end()) {
			LOG(INFO) << "Making camera: " << src->getURI();

Nicolas Pope's avatar
Nicolas Pope committed
			auto *cam = new ftl::gui::Camera(screen_, src);
			cameras_[src->getURI()] = cam;
Nicolas Pope's avatar
Nicolas Pope committed
		} else {
			//LOG(INFO) << "Camera already exists: " << s;

	refresh_thumbs_ = true;
	if (thumbs_.size() != available_.size()) {
		thumbs_.resize(available_.size());
	}
}

SourceWindow::~SourceWindow() {

}

void SourceWindow::draw(NVGcontext *ctx) {
	if (refresh_thumbs_) {
		UNIQUE_LOCK(mutex_, lk);
		//refresh_thumbs_ = false;

		for (size_t i=0; i<thumbs_.size(); ++i) {
			cv::Mat t;
			auto *cam = cameras_[available_[i]];
			if (cam) {
				if (cam->source()->thumbnail(t)) {
					thumbs_[i].update(t);
				} else {
					refresh_thumbs_ = true;
				}
			}

			if (ipanel_->childCount() < i+1) {
				new ftl::gui::ThumbView(ipanel_, screen_, cam);
			}
			if (thumbs_[i].isValid()) dynamic_cast<nanogui::ImageView*>(ipanel_->childAt(i))->bindImage(thumbs_[i].texture());
		}

		// TODO(Nick) remove excess image views

		center();
	}

	nanogui::Window::draw(ctx);
}