From d83be1dcccf2b9eb7d8f4cb5562a70460207d0db Mon Sep 17 00:00:00 2001 From: ozantonkal Date: Fri, 23 Aug 2013 18:49:21 +0200 Subject: [PATCH] fixes based on the feedback: window name prefix is automatically added when it is not there, singleton class returns reference instead of pointer, destructor is private, release function implemented --- modules/viz/include/opencv2/viz.hpp | 7 +++-- modules/viz/src/viz.cpp | 45 ++++++++++++++++++++++------- modules/viz/src/viz3d.cpp | 4 +-- modules/viz/src/viz3d_impl.cpp | 6 ++-- modules/viz/src/viz3d_impl.hpp | 6 +++- 5 files changed, 49 insertions(+), 19 deletions(-) diff --git a/modules/viz/include/opencv2/viz.hpp b/modules/viz/include/opencv2/viz.hpp index f3745767af..0a82a9ebb7 100644 --- a/modules/viz/include/opencv2/viz.hpp +++ b/modules/viz/include/opencv2/viz.hpp @@ -95,15 +95,18 @@ namespace cv class CV_EXPORTS VizAccessor { public: - ~VizAccessor(); - static VizAccessor * getInstance(); + static VizAccessor & getInstance(); + static void release(); Viz3d get(const String &window_name); void add(Viz3d window); void remove(const String &window_name); + static void generateWindowName(const String &window_name, String &output); + private: VizAccessor(); // Singleton + ~VizAccessor(); static VizAccessor * instance_; static bool is_instantiated_; diff --git a/modules/viz/src/viz.cpp b/modules/viz/src/viz.cpp index f3059a606c..c8e09ba9ee 100644 --- a/modules/viz/src/viz.cpp +++ b/modules/viz/src/viz.cpp @@ -95,27 +95,34 @@ cv::viz::VizMap cv::viz::VizAccessor::viz_map_; cv::viz::VizAccessor::VizAccessor() {} -cv::viz::VizAccessor::~VizAccessor() +cv::viz::VizAccessor::~VizAccessor() {} + +cv::viz::VizAccessor & cv::viz::VizAccessor::getInstance() { - is_instantiated_ = false; + if (!is_instantiated_) + { + instance_ = new VizAccessor(); + is_instantiated_ = true; + } + return *instance_; } -cv::viz::VizAccessor * cv::viz::VizAccessor::getInstance() +void cv::viz::VizAccessor::release() { if (is_instantiated_) { - instance_ = new VizAccessor(); - is_instantiated_ = true; + delete instance_; + instance_ = 0; + is_instantiated_ = false; } - return instance_; } cv::viz::Viz3d cv::viz::VizAccessor::get(const String & window_name) { // Add the prefix Viz - String name("Viz"); - name = window_name.empty() ? name : name + " - " + window_name; - + String name; + generateWindowName(window_name, name); + VizMap::iterator vm_itr = viz_map_.find(name); bool exists = vm_itr != viz_map_.end(); if (exists) return vm_itr->second; @@ -133,13 +140,29 @@ void cv::viz::VizAccessor::add(Viz3d window) void cv::viz::VizAccessor::remove(const String &window_name) { - VizMap::iterator vm_itr = viz_map_.find(window_name); + // Add the prefix Viz + String name; + generateWindowName(window_name, name); + + VizMap::iterator vm_itr = viz_map_.find(name); bool exists = vm_itr != viz_map_.end(); if (!exists) return ; viz_map_.erase(vm_itr); } +void cv::viz::VizAccessor::generateWindowName(const String &window_name, String &output) +{ + output = "Viz"; + // Already is Viz + if (window_name == output) return; + + String prefixed = output + " - "; + if (window_name.substr(0, prefixed.length()) == prefixed) output = window_name; // Already has "Viz - " + else if (window_name.substr(0, output.length()) == output) output = prefixed + window_name; // Doesn't have prefix + else output = (window_name == "" ? output : prefixed + window_name); +} + cv::viz::Viz3d cv::viz::get(const String &window_name) { - return cv::viz::VizAccessor::getInstance()->get(window_name); + return cv::viz::VizAccessor::getInstance().get(window_name); } \ No newline at end of file diff --git a/modules/viz/src/viz3d.cpp b/modules/viz/src/viz3d.cpp index c31d5880a7..583ece096b 100644 --- a/modules/viz/src/viz3d.cpp +++ b/modules/viz/src/viz3d.cpp @@ -28,7 +28,7 @@ void cv::viz::Viz3d::create(const String &window_name) impl_ = new VizImpl(window_name); impl_->ref_counter = 1; // Register the window - cv::viz::VizAccessor::getInstance()->add(*this); + cv::viz::VizAccessor::getInstance().add(*this); } void cv::viz::Viz3d::release() @@ -36,7 +36,7 @@ void cv::viz::Viz3d::release() if (impl_ && CV_XADD(&impl_->ref_counter, -1) == 1) { // Erase the window - cv::viz::VizAccessor::getInstance()->remove(getWindowName()); + cv::viz::VizAccessor::getInstance().remove(getWindowName()); delete impl_; impl_ = 0; } diff --git a/modules/viz/src/viz3d_impl.cpp b/modules/viz/src/viz3d_impl.cpp index c1807a0155..95f93b1154 100644 --- a/modules/viz/src/viz3d_impl.cpp +++ b/modules/viz/src/viz3d_impl.cpp @@ -74,9 +74,8 @@ cv::viz::Viz3d::VizImpl::VizImpl (const std::string &name) ////////////////////////////// - - String window_name("Viz"); - window_name = name.empty() ? window_name : window_name + " - " + name; + String window_name; + VizAccessor::generateWindowName(name, window_name); window_->SetWindowName (window_name.c_str ()); } @@ -85,6 +84,7 @@ cv::viz::Viz3d::VizImpl::~VizImpl () { if (interactor_) interactor_->DestroyTimer(timer_id_); + if (renderer_) renderer_->Clear(); } ///////////////////////////////////////////////////////////////////////////////////////////// diff --git a/modules/viz/src/viz3d_impl.hpp b/modules/viz/src/viz3d_impl.hpp index 5f8ad367b5..ef23cc1b90 100644 --- a/modules/viz/src/viz3d_impl.hpp +++ b/modules/viz/src/viz3d_impl.hpp @@ -69,7 +69,11 @@ public: void close () { stopped_ = true; - interactor_->TerminateApp (); // This tends to close the window... + if (interactor_) + { + interactor_->GetRenderWindow()->Finalize(); + interactor_->TerminateApp (); // This tends to close the window... + } }