Skip to content
Snippets Groups Projects
camera.hpp 3.81 KiB
Newer Older
Nicolas Pope's avatar
Nicolas Pope committed
#ifndef _FTL_GUI_CAMERA_HPP_
#define _FTL_GUI_CAMERA_HPP_

#include <ftl/rgbd/frameset.hpp>
#include <ftl/render/CUDARender.hpp>
#include <ftl/codecs/writer.hpp>
Nicolas Pope's avatar
Nicolas Pope committed
#include "gltexture.hpp"

#include <ftl/streams/filestream.hpp>
#include <ftl/streams/sender.hpp>

Nicolas Pope's avatar
Nicolas Pope committed
#include <string>
#include <array>
Nicolas Pope's avatar
Nicolas Pope committed
#ifdef HAVE_OPENVR
#include <openvr/openvr.h>
#endif

class StatisticsImage;
Nicolas Pope's avatar
Nicolas Pope committed

namespace ftl {
namespace gui {

class Screen;
class PoseWindow;

class Camera {
	public:
	Camera(ftl::gui::Screen *screen, int fsmask, int fid, ftl::codecs::Channel chan=ftl::codecs::Channel::Colour);
Nicolas Pope's avatar
Nicolas Pope committed
	~Camera();

	Camera(const Camera &)=delete;

	int width() const { return width_; }
	int height() const { return height_; }
	int getFramesetMask() const { return fsmask_; }

	bool usesFrameset(int id) const { return fsmask_ & (1 << id); }
Nicolas Pope's avatar
Nicolas Pope committed
	void setPose(const Eigen::Matrix4d &p);

	void mouseMovement(int rx, int ry, int button);
	void keyMovement(int key, int modifiers);

	void showPoseWindow();
	void showSettings();

	void setChannel(ftl::codecs::Channel c);
Sebastian Hahta's avatar
Sebastian Hahta committed
	const ftl::codecs::Channel getChannel() { return channel_; }
	
Nicolas Pope's avatar
Nicolas Pope committed
	void togglePause();
	void isPaused();
	inline bool isVirtual() const { return fid_ == 255; }
	const ftl::codecs::Channels<0> &availableChannels() { return channels_; }
	inline bool isStereo() const { return stereo_; }

	void setStereo(bool v);

	/**
	 * Main function to obtain latest frames.
	 */
	void update(std::vector<ftl::rgbd::FrameSet *> &fss);

	/**
	 * Update the available channels.
	 */
	void update(int fsid, const ftl::codecs::Channels<0> &c);
	void draw(std::vector<ftl::rgbd::FrameSet*> &fss);
	/**
	 * @internal. Used to inform the camera if it is the active camera or not.
	 */
	void active(bool);

	const void captureFrame();
Sebastian Hahta's avatar
Sebastian Hahta committed
	const GLTexture &getLeft() const { return texture1_; }
	const GLTexture &getRight() const { return texture2_; }
	bool thumbnail(cv::Mat &thumb);

Iiro Rastas's avatar
Iiro Rastas committed
	void snapshot(const std::string &filename);
Iiro Rastas's avatar
Iiro Rastas committed
	void startVideoRecording(const std::string &filename);

	void stopVideoRecording();
	//nlohmann::json getMetaData();
	const std::string &name() const { return name_; }

	StatisticsImage *stats_ = nullptr;
Sebastian Hahta's avatar
Sebastian Hahta committed

#ifdef HAVE_OPENVR
	bool isVR() { return vr_mode_; }
	bool setVR(bool on);
#else
	bool isVR() { return false; }
#endif

Nicolas Pope's avatar
Nicolas Pope committed
	private:
Nicolas Pope's avatar
Nicolas Pope committed
	Screen *screen_;
	unsigned int fsmask_;  // Frameset Mask
	int fid_;

	int width_;
	int height_;

Nicolas Pope's avatar
Nicolas Pope committed
	GLTexture thumb_;
Sebastian Hahta's avatar
Sebastian Hahta committed
	GLTexture texture1_; // first channel (always left at the moment)
	GLTexture texture2_; // second channel ("right")

Nicolas Pope's avatar
Nicolas Pope committed
	ftl::gui::PoseWindow *posewin_;
	//nlohmann::json meta_;
Nicolas Pope's avatar
Nicolas Pope committed
	Eigen::Vector4d neye_;
	Eigen::Vector3d eye_;
	//Eigen::Vector3f orientation_;
	Eigen::Matrix4d rotmat_;
	float ftime_;
	float delta_;
	float lerpSpeed_;
	bool sdepth_;
Nicolas Pope's avatar
Nicolas Pope committed
	bool pause_;
	ftl::codecs::Channel channel_;
	ftl::codecs::Channels<0> channels_;
Sebastian Hahta's avatar
Sebastian Hahta committed
	cv::Mat im1_; // first channel (left)
	cv::Mat im2_; // second channel ("right")
	bool stereo_;
	std::atomic_flag new_frame_;
	int rx_;
	int ry_;
	std::vector<ftl::rgbd::FrameSet*> *framesets_;
	ftl::render::CUDARender *renderer_;
	ftl::render::CUDARender *renderer2_;
	ftl::render::Colouriser *colouriser_;

	ftl::Configurable *intrinsics_;
	ftl::operators::Graph *post_pipe_;
	ftl::rgbd::Frame frame_;
	ftl::rgbd::FrameState state_;
	ftl::stream::File *record_stream_;
	ftl::stream::Sender *record_sender_;

	std::string name_;
Sebastian Hahta's avatar
Sebastian Hahta committed

	int transform_ix_;
	std::array<Eigen::Matrix4d,ftl::stream::kMaxStreams> transforms_;  // Frameset transforms for virtual cam

	MUTEX mutex_;
Nicolas Pope's avatar
Nicolas Pope committed

	#ifdef HAVE_OPENVR
	vr::TrackedDevicePose_t rTrackedDevicePose_[ vr::k_unMaxTrackedDeviceCount ];
Sebastian Hahta's avatar
Sebastian Hahta committed
	bool vr_mode_;
	float baseline_;
Nicolas Pope's avatar
Nicolas Pope committed
	#endif
	void _downloadFrames(ftl::cuda::TextureObject<uchar4> &, ftl::cuda::TextureObject<uchar4> &);
	void _downloadFrame(ftl::cuda::TextureObject<uchar4> &);
	void _draw(std::vector<ftl::rgbd::FrameSet*> &fss);
Nicolas Pope's avatar
Nicolas Pope committed
};

}
}

#endif  // _FTL_GUI_CAMERA_HPP_