-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
118 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,106 +1,42 @@ | ||
#pragma once | ||
#include "map.hpp" | ||
#include <array> | ||
#include <condition_variable> | ||
#include <memory> | ||
#include <mutex> | ||
#include <opencv2/opencv.hpp> | ||
#include <thread> | ||
|
||
//namespace | ||
//{ | ||
//const int PL = 0; | ||
//const int PR = 1; | ||
//const int CL = 2; | ||
//const int CR = 3; | ||
//} // namespace | ||
|
||
class Viewer | ||
{ | ||
public: | ||
Viewer() | ||
: m_window_name("VIEW"), | ||
m_thread(std::make_shared<std::thread>(&Viewer::drawLoop, this)), | ||
m_reset_requested(false), | ||
m_stop_requested(false), | ||
m_updated(false) {} | ||
Viewer(); | ||
|
||
~Viewer() { stop(); } | ||
~Viewer(); | ||
|
||
void update( | ||
const std::array<cv::Mat, 4>& images, | ||
const std::vector<MapPointPtr>& mappoints) | ||
{ | ||
std::lock_guard lock(m_mutex); | ||
m_mappoints = mappoints; | ||
m_images = images; | ||
m_updated = true; | ||
} | ||
const std::vector<MapPointPtr>& mappoints); | ||
|
||
void reset() { m_reset_requested = true; } | ||
void stop() { m_stop_requested = true; } | ||
void reset(); | ||
void stop(); | ||
int waitKeyEver(); | ||
|
||
private: | ||
void drawLoop() | ||
{ | ||
cv::namedWindow(m_window_name, cv::WINDOW_NORMAL); | ||
cv::resizeWindow(m_window_name, 640, 480); | ||
|
||
while (1) { | ||
// 描画 | ||
if (m_updated) { | ||
m_updated = false; | ||
|
||
cv::Mat show, merge1, merge2; | ||
cv::hconcat(m_images.at(PL), m_images.at(PR), merge1); | ||
cv::hconcat(m_images.at(CL), m_images.at(CR), merge2); | ||
cv::vconcat(merge1, merge2, show); | ||
|
||
const cv::Size size = m_images.at(CL).size(); | ||
const cv::Point2f OFFSET_PL(0, 0); | ||
const cv::Point2f OFFSET_PR(size.width, 0); | ||
const cv::Point2f OFFSET_CL(0, size.height); | ||
const cv::Point2f OFFSET_CR(size.width, size.height); | ||
|
||
for (const MapPointPtr p : m_mappoints) { | ||
if (p->enable(PL)) | ||
cv::circle(show, p->preLeft() + OFFSET_CL, 1, CV_RGB(255, 0, 0), 0, cv::LineTypes::LINE_AA); | ||
if (p->enable(PR)) | ||
cv::circle(show, p->preRight() + OFFSET_PR, 1, CV_RGB(255, 0, 0), 0, cv::LineTypes::LINE_AA); | ||
if (p->enable(CL)) | ||
cv::circle(show, p->curLeft() + OFFSET_CL, 1, CV_RGB(255, 0, 0), 0, cv::LineTypes::LINE_AA); | ||
if (p->enable(CR)) | ||
cv::circle(show, p->curRight() + OFFSET_CL, 1, CV_RGB(255, 0, 0), 0, cv::LineTypes::LINE_AA); | ||
|
||
if (p->triangulatable()) | ||
cv::line(show, p->curRight() + OFFSET_CL, p->curLeft() + OFFSET_CL, CV_RGB(255, 255, 0), 1, cv::LineTypes::LINE_AA); | ||
if (p->motionEstimatable()) | ||
cv::line(show, p->preLeft() + OFFSET_CL, p->curLeft() + OFFSET_CL, CV_RGB(0, 255, 255), 1, cv::LineTypes::LINE_AA); | ||
} | ||
cv::imshow(m_window_name, show); | ||
} | ||
|
||
if (m_reset_requested) { | ||
// TODO: | ||
m_reset_requested = false; | ||
} | ||
|
||
if (m_stop_requested) { | ||
break; | ||
} | ||
|
||
cv::waitKey(10); | ||
} | ||
|
||
std::cout << "viewer shut down " << std::endl; | ||
} | ||
void drawLoop(); | ||
|
||
std::array<cv::Mat, 4> m_images; | ||
std::vector<MapPointPtr> m_mappoints; | ||
|
||
const std::string m_window_name; | ||
|
||
std::shared_ptr<std::thread> m_thread; | ||
std::mutex m_mutex; | ||
std::condition_variable m_cv; | ||
|
||
bool m_reset_requested; | ||
bool m_stop_requested; | ||
bool m_updated; | ||
bool m_update_called; | ||
|
||
int m_last_key; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,99 @@ | ||
#include "viewer.hpp" | ||
#include "params.hpp" | ||
#include <chrono> | ||
|
||
Viewer::Viewer() | ||
: m_window_name("VIEW"), | ||
m_thread(std::make_shared<std::thread>(&Viewer::drawLoop, this)), | ||
m_reset_requested(false), | ||
m_stop_requested(false), | ||
m_update_called(false), | ||
m_last_key(-1) {} | ||
|
||
Viewer::~Viewer() { stop(); } | ||
|
||
void Viewer::update( | ||
const std::array<cv::Mat, 4>& images, | ||
const std::vector<MapPointPtr>& mappoints) | ||
{ | ||
std::lock_guard lock(m_mutex); | ||
m_mappoints = mappoints; | ||
m_images = images; | ||
m_update_called = true; | ||
} | ||
|
||
void Viewer::reset() { m_reset_requested = true; } | ||
void Viewer::stop() { m_stop_requested = true; } | ||
|
||
int Viewer::waitKeyEver() | ||
{ | ||
std::unique_lock lock(m_mutex); | ||
m_cv.wait(lock, [this] { return (m_last_key != -1); }); | ||
return m_last_key; | ||
} | ||
void Viewer::drawLoop() | ||
{ | ||
cv::namedWindow(m_window_name, cv::WINDOW_NORMAL); | ||
cv::resizeWindow(m_window_name, 640, 480); | ||
|
||
while (1) { | ||
|
||
bool update = false; | ||
{ | ||
std::lock_guard lock(m_mutex); | ||
update = m_update_called; | ||
m_update_called = false; | ||
} | ||
|
||
if (update) { | ||
cv::Mat show, merge1, merge2; | ||
cv::hconcat(m_images.at(PL), m_images.at(PR), merge1); | ||
cv::hconcat(m_images.at(CL), m_images.at(CR), merge2); | ||
cv::vconcat(merge1, merge2, show); | ||
|
||
const cv::Size2f size = m_images.at(CL).size(); | ||
const cv::Point2f OFFSET_PL(0, 0); | ||
const cv::Point2f OFFSET_PR(size.width, 0); | ||
const cv::Point2f OFFSET_CL(0, size.height); | ||
const cv::Point2f OFFSET_CR(size.width, size.height); | ||
|
||
for (const MapPointPtr p : m_mappoints) { | ||
if (p->enable(PL)) | ||
cv::circle(show, p->preLeft() + OFFSET_CL, 1, CV_RGB(255, 0, 0), 0, cv::LineTypes::LINE_AA); | ||
if (p->enable(PR)) | ||
cv::circle(show, p->preRight() + OFFSET_PR, 1, CV_RGB(255, 0, 0), 0, cv::LineTypes::LINE_AA); | ||
if (p->enable(CL)) | ||
cv::circle(show, p->curLeft() + OFFSET_CL, 1, CV_RGB(255, 0, 0), 0, cv::LineTypes::LINE_AA); | ||
if (p->enable(CR)) | ||
cv::circle(show, p->curRight() + OFFSET_CL, 1, CV_RGB(255, 0, 0), 0, cv::LineTypes::LINE_AA); | ||
|
||
if (p->triangulatable()) | ||
cv::line(show, p->curRight() + OFFSET_CL, p->curLeft() + OFFSET_CL, CV_RGB(255, 255, 0), 1, cv::LineTypes::LINE_AA); | ||
if (p->motionEstimatable()) | ||
cv::line(show, p->preLeft() + OFFSET_CL, p->curLeft() + OFFSET_CL, CV_RGB(0, 255, 255), 1, cv::LineTypes::LINE_AA); | ||
} | ||
cv::imshow(m_window_name, show); | ||
} | ||
|
||
if (m_reset_requested) { | ||
// TODO: | ||
m_reset_requested = false; | ||
} | ||
|
||
if (m_stop_requested) { | ||
break; | ||
} | ||
|
||
// key event | ||
{ | ||
std::lock_guard lock(m_mutex); | ||
m_last_key = cv::waitKey(5); | ||
} | ||
|
||
// wait for other thread action | ||
m_cv.notify_all(); | ||
std::this_thread::sleep_for(std::chrono::milliseconds(10)); | ||
} | ||
|
||
std::cout << "viewer shut down " << std::endl; | ||
} |