Merge pull request #19 from ozantonkal/acess_by_name_infrastructure

Acess by name infrastructure
This commit is contained in:
Anatoly Baksheev 2013-08-26 23:46:47 -07:00
commit 8b20ceb024
6 changed files with 173 additions and 14 deletions

View File

@ -58,13 +58,17 @@
namespace cv namespace cv
{ {
namespace viz namespace viz
{ {
typedef std::map<String, Viz3d> VizMap;
typedef std::pair<String, Viz3d> VizPair;
//! takes coordiante frame data and builds transfrom to global coordinate frame //! takes coordiante frame data and builds transfrom to global coordinate frame
CV_EXPORTS Affine3f makeTransformToGlobal(const Vec3f& axis_x, const Vec3f& axis_y, const Vec3f& axis_z, const Vec3f& origin = Vec3f::all(0)); CV_EXPORTS Affine3f makeTransformToGlobal(const Vec3f& axis_x, const Vec3f& axis_y, const Vec3f& axis_z, const Vec3f& origin = Vec3f::all(0));
//! constructs camera pose from position, focal_point and up_vector (see gluLookAt() for more infromation //! constructs camera pose from position, focal_point and up_vector (see gluLookAt() for more infromation
CV_EXPORTS Affine3f makeCameraPose(const Vec3f& position, const Vec3f& focal_point, const Vec3f& y_dir); CV_EXPORTS Affine3f makeCameraPose(const Vec3f& position, const Vec3f& focal_point, const Vec3f& y_dir);
CV_EXPORTS Viz3d get(const String &window_name);
//! checks float value for Nan //! checks float value for Nan
inline bool isNan(float x) inline bool isNan(float x)
@ -87,6 +91,27 @@ namespace cv
//! checks point for Nans //! checks point for Nans
template<typename _Tp> inline bool isNan(const Point3_<_Tp>& p) template<typename _Tp> inline bool isNan(const Point3_<_Tp>& p)
{ return isNan(p.x) || isNan(p.y) || isNan(p.z); } { return isNan(p.x) || isNan(p.y) || isNan(p.z); }
class CV_EXPORTS VizAccessor
{
public:
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_;
static VizMap viz_map_;
};
} }
} }

View File

@ -21,6 +21,8 @@ namespace cv
typedef void (*MouseCallback)(const MouseEvent&, void*); typedef void (*MouseCallback)(const MouseEvent&, void*);
Viz3d(const String& window_name = String()); Viz3d(const String& window_name = String());
Viz3d(const Viz3d&);
Viz3d& operator=(const Viz3d&);
~Viz3d(); ~Viz3d();
void setBackgroundColor(const Color& color = Color::black()); void setBackgroundColor(const Color& color = Color::black());
@ -50,6 +52,8 @@ namespace cv
Size getWindowSize() const; Size getWindowSize() const;
void setWindowSize(const Size &window_size); void setWindowSize(const Size &window_size);
String getWindowName() const;
void spin(); void spin();
void spinOnce(int time = 1, bool force_redraw = false); void spinOnce(int time = 1, bool force_redraw = false);
@ -58,11 +62,12 @@ namespace cv
void registerKeyboardCallback(KeyboardCallback callback, void* cookie = 0); void registerKeyboardCallback(KeyboardCallback callback, void* cookie = 0);
void registerMouseCallback(MouseCallback callback, void* cookie = 0); void registerMouseCallback(MouseCallback callback, void* cookie = 0);
private: private:
Viz3d(const Viz3d&);
Viz3d& operator=(const Viz3d&);
struct VizImpl; struct VizImpl;
VizImpl* impl_; VizImpl* impl_;
void create(const String &window_name);
void release();
}; };
} /* namespace viz */ } /* namespace viz */

View File

@ -85,3 +85,84 @@ namespace cv
} }
} }
} }
///////////////////////////////////////////////////////////////////////////////////////////////
/// Viz accessor implementation
cv::viz::VizAccessor * cv::viz::VizAccessor::instance_ = 0;
bool cv::viz::VizAccessor::is_instantiated_ = false;
cv::viz::VizMap cv::viz::VizAccessor::viz_map_;
cv::viz::VizAccessor::VizAccessor() {}
cv::viz::VizAccessor::~VizAccessor() {}
cv::viz::VizAccessor & cv::viz::VizAccessor::getInstance()
{
if (!is_instantiated_)
{
instance_ = new VizAccessor();
is_instantiated_ = true;
}
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)
{
// 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 vm_itr->second;
else return viz_map_.insert(VizPair(window_name, Viz3d(window_name))).first->second;
}
void cv::viz::VizAccessor::add(Viz3d window)
{
String window_name = window.getWindowName();
VizMap::iterator vm_itr = viz_map_.find(window_name);
bool exists = vm_itr != viz_map_.end();
if (exists) return ;
viz_map_.insert(std::pair<String,Viz3d>(window_name, window));
}
void cv::viz::VizAccessor::remove(const String &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);
}

View File

@ -2,8 +2,45 @@
#include "viz3d_impl.hpp" #include "viz3d_impl.hpp"
cv::viz::Viz3d::Viz3d(const String& window_name) : impl_(new VizImpl(window_name)) {} cv::viz::Viz3d::Viz3d(const String& window_name) : impl_(0) { create(window_name); }
cv::viz::Viz3d::~Viz3d() { delete impl_; }
cv::viz::Viz3d::Viz3d(const Viz3d& other) : impl_(other.impl_)
{
if (impl_) CV_XADD(&impl_->ref_counter, 1);
}
cv::viz::Viz3d& cv::viz::Viz3d::operator=(const Viz3d& other)
{
if (this != &other)
{
release();
impl_ = other.impl_;
if (impl_) CV_XADD(&impl_->ref_counter, 1);
}
return *this;
}
cv::viz::Viz3d::~Viz3d() { release(); }
void cv::viz::Viz3d::create(const String &window_name)
{
if (impl_) release();
impl_ = new VizImpl(window_name);
impl_->ref_counter = 1;
// Register the window
cv::viz::VizAccessor::getInstance().add(*this);
}
void cv::viz::Viz3d::release()
{
if (impl_ && CV_XADD(&impl_->ref_counter, -1) == 1)
{
// Erase the window
cv::viz::VizAccessor::getInstance().remove(getWindowName());
delete impl_;
impl_ = 0;
}
}
void cv::viz::Viz3d::setBackgroundColor(const Color& color) { impl_->setBackgroundColor(color); } void cv::viz::Viz3d::setBackgroundColor(const Color& color) { impl_->setBackgroundColor(color); }
@ -55,4 +92,5 @@ void cv::viz::Viz3d::convertToWindowCoordinates(const Point3d &pt, Point3d &wind
void cv::viz::Viz3d::converTo3DRay(const Point3d &window_coord, Point3d &origin, Vec3d &direction) { impl_->converTo3DRay(window_coord, origin, direction); } void cv::viz::Viz3d::converTo3DRay(const Point3d &window_coord, Point3d &origin, Vec3d &direction) { impl_->converTo3DRay(window_coord, origin, direction); }
cv::Size cv::viz::Viz3d::getWindowSize() const { return impl_->getWindowSize(); } cv::Size cv::viz::Viz3d::getWindowSize() const { return impl_->getWindowSize(); }
void cv::viz::Viz3d::setWindowSize(const Size &window_size) { impl_->setWindowSize(window_size.width, window_size.height); } void cv::viz::Viz3d::setWindowSize(const Size &window_size) { impl_->setWindowSize(window_size.width, window_size.height); }
cv::String cv::viz::Viz3d::getWindowName() const { return impl_->getWindowName(); }

View File

@ -74,19 +74,17 @@ 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 ());
} }
///////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////
cv::viz::Viz3d::VizImpl::~VizImpl () cv::viz::Viz3d::VizImpl::~VizImpl ()
{ {
if (interactor_ != NULL) if (interactor_)
interactor_->DestroyTimer (timer_id_); interactor_->DestroyTimer(timer_id_);
if (renderer_) renderer_->Clear();
renderer_->Clear();
} }
///////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////
@ -968,6 +966,11 @@ void cv::viz::Viz3d::VizImpl::setWindowName (const std::string &name)
window_->SetWindowName (name.c_str ()); window_->SetWindowName (name.c_str ());
} }
cv::String cv::viz::Viz3d::VizImpl::getWindowName() const
{
return (window_ ? window_->GetWindowName() : "");
}
void cv::viz::Viz3d::VizImpl::setWindowPosition (int x, int y) { window_->SetPosition (x, y); } void cv::viz::Viz3d::VizImpl::setWindowPosition (int x, int y) { window_->SetPosition (x, y); }
void cv::viz::Viz3d::VizImpl::setWindowSize (int xw, int yw) { window_->SetSize (xw, yw); } void cv::viz::Viz3d::VizImpl::setWindowSize (int xw, int yw) { window_->SetSize (xw, yw); }
cv::Size cv::viz::Viz3d::VizImpl::getWindowSize() const { return Size(window_->GetSize()[0], window_->GetSize()[1]); } cv::Size cv::viz::Viz3d::VizImpl::getWindowSize() const { return Size(window_->GetSize()[0], window_->GetSize()[1]); }

View File

@ -11,6 +11,8 @@ public:
typedef cv::Ptr<VizImpl> Ptr; typedef cv::Ptr<VizImpl> Ptr;
typedef Viz3d::KeyboardCallback KeyboardCallback; typedef Viz3d::KeyboardCallback KeyboardCallback;
typedef Viz3d::MouseCallback MouseCallback; typedef Viz3d::MouseCallback MouseCallback;
int ref_counter;
VizImpl (const String &name); VizImpl (const String &name);
virtual ~VizImpl (); virtual ~VizImpl ();
@ -67,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...
}
} }
@ -142,6 +148,7 @@ public:
void setWindowSize (int xw, int yw); void setWindowSize (int xw, int yw);
void setFullScreen (bool mode); void setFullScreen (bool mode);
void setWindowName (const String &name); void setWindowName (const String &name);
String getWindowName() const;
void setBackgroundColor (const Color& color); void setBackgroundColor (const Color& color);
void spin (); void spin ();