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

This commit is contained in:
ozantonkal 2013-08-23 18:49:21 +02:00
parent 6bc393676d
commit d83be1dccc
5 changed files with 50 additions and 20 deletions

View File

@ -95,15 +95,18 @@ namespace cv
class CV_EXPORTS VizAccessor class CV_EXPORTS VizAccessor
{ {
public: public:
~VizAccessor(); static VizAccessor & getInstance();
static VizAccessor * getInstance(); static void release();
Viz3d get(const String &window_name); Viz3d get(const String &window_name);
void add(Viz3d window); void add(Viz3d window);
void remove(const String &window_name); void remove(const String &window_name);
static void generateWindowName(const String &window_name, String &output);
private: private:
VizAccessor(); // Singleton VizAccessor(); // Singleton
~VizAccessor();
static VizAccessor * instance_; static VizAccessor * instance_;
static bool is_instantiated_; static bool is_instantiated_;

View File

@ -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::~VizAccessor() {}
{
is_instantiated_ = false;
}
cv::viz::VizAccessor * cv::viz::VizAccessor::getInstance() cv::viz::VizAccessor & cv::viz::VizAccessor::getInstance()
{ {
if (is_instantiated_) if (!is_instantiated_)
{ {
instance_ = new VizAccessor(); instance_ = new VizAccessor();
is_instantiated_ = true; is_instantiated_ = true;
} }
return instance_; return *instance_;
}
void cv::viz::VizAccessor::release()
{
if (is_instantiated_)
{
delete instance_;
instance_ = 0;
is_instantiated_ = false;
}
} }
cv::viz::Viz3d cv::viz::VizAccessor::get(const String & window_name) cv::viz::Viz3d cv::viz::VizAccessor::get(const String & window_name)
{ {
// Add the prefix Viz // Add the prefix Viz
String name("Viz"); String name;
name = window_name.empty() ? name : name + " - " + window_name; generateWindowName(window_name, name);
VizMap::iterator vm_itr = viz_map_.find(name); VizMap::iterator vm_itr = viz_map_.find(name);
bool exists = vm_itr != viz_map_.end(); bool exists = vm_itr != viz_map_.end();
if (exists) return vm_itr->second; 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) 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(); bool exists = vm_itr != viz_map_.end();
if (!exists) return ; if (!exists) return ;
viz_map_.erase(vm_itr); 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) 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);
} }

View File

@ -28,7 +28,7 @@ void cv::viz::Viz3d::create(const String &window_name)
impl_ = new VizImpl(window_name); impl_ = new VizImpl(window_name);
impl_->ref_counter = 1; impl_->ref_counter = 1;
// Register the window // Register the window
cv::viz::VizAccessor::getInstance()->add(*this); cv::viz::VizAccessor::getInstance().add(*this);
} }
void cv::viz::Viz3d::release() void cv::viz::Viz3d::release()
@ -36,7 +36,7 @@ void cv::viz::Viz3d::release()
if (impl_ && CV_XADD(&impl_->ref_counter, -1) == 1) if (impl_ && CV_XADD(&impl_->ref_counter, -1) == 1)
{ {
// Erase the window // Erase the window
cv::viz::VizAccessor::getInstance()->remove(getWindowName()); cv::viz::VizAccessor::getInstance().remove(getWindowName());
delete impl_; delete impl_;
impl_ = 0; impl_ = 0;
} }

View File

@ -74,9 +74,8 @@ cv::viz::Viz3d::VizImpl::VizImpl (const std::string &name)
////////////////////////////// //////////////////////////////
String window_name;
String window_name("Viz"); VizAccessor::generateWindowName(name, window_name);
window_name = name.empty() ? window_name : window_name + " - " + name;
window_->SetWindowName (window_name.c_str ()); window_->SetWindowName (window_name.c_str ());
} }
@ -85,6 +84,7 @@ cv::viz::Viz3d::VizImpl::~VizImpl ()
{ {
if (interactor_) if (interactor_)
interactor_->DestroyTimer(timer_id_); interactor_->DestroyTimer(timer_id_);
if (renderer_) renderer_->Clear();
} }
///////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -69,7 +69,11 @@ public:
void close () void close ()
{ {
stopped_ = true; stopped_ = true;
interactor_->TerminateApp (); // This tends to close the window... if (interactor_)
{
interactor_->GetRenderWindow()->Finalize();
interactor_->TerminateApp (); // This tends to close the window...
}
} }