📄 cvaux.hpp
字号:
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// Intel License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of Intel Corporation may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#ifdef __cplusplus
#if _MSC_VER >= 1200 || defined __BORLANDC__
#if _MSC_VER >= 1200
#pragma warning (disable: 4710)
#endif
#ifndef CVH_STORAGE_DECLARED
#define CVH_STORAGE_DECLARED
template<class Val, class Idx> struct _CvTreeNode
{
typedef Val value_type;
typedef Idx idx_type;
typedef _CvTreeNode<value_type, idx_type> node_type;
value_type val;
idx_type idx;
int balance;
node_type* link[3]; /* links to nodes (left, right, up) */
enum{ left = 0,
right = 1,
up = 2,
center = 2 };
_CvTreeNode()
{ link[0] = link[1] = link[2] = 0; idx = 0; balance = 0; val = 0; }
node_type*& next() { return link[0]; }
};
template<class Node> struct _CvNodeBlock
{
typedef Node node_type;
typedef _CvNodeBlock<node_type> block_type;
enum { block_size = 4096,
block_nodes = sizeof(node_type) < block_size ?
block_size/sizeof(node_type) : 1 };
_CvNodeBlock();
node_type& operator[](int idx){ return data[idx]; }
node_type data[block_nodes];
block_type* next;
};
template<typename Node> class CvNodeIterator
{
public:
typedef Node node_type;
typedef node_type::value_type value_type;
typedef node_type::idx_type idx_type;
typedef CvNodeIterator<node_type> iterator;
typedef _CvNodeBlock<node_type> block_type;
value_type& operator *() const { return current_block->data[idx].val; }
idx_type get_idx() const { return current_block->data[idx].idx; }
node_type* get_node() const { return ¤t_block->data[idx]; }
iterator& operator ++(); // prefix ++
const iterator operator ++(int); // postfix ++
CvNodeIterator() { current_block = 0; idx = 0; }
CvNodeIterator( block_type* _current_block )
{ current_block = _current_block; idx = 0; }
CvNodeIterator( block_type* _current_block, idx_type _idx )
{ current_block = _current_block; idx = _idx; }
bool operator == ( const iterator& another ) const
{ return (current_block == another.current_block) && (idx == another.idx); }
bool operator != ( const iterator& another ) const
{ return (current_block != another.current_block) || (idx != another.idx); }
protected:
block_type* current_block;
idx_type idx;
};
#endif
/****************************************************************************************\
* Template classes for dynamic data structures *
\****************************************************************************************/
template<class Val> class CvArrayIterator
{
public:
typedef Val value_type;
typedef int idx_type;
typedef CvArrayIterator<value_type> iterator;
value_type& operator *() const {return *current;}
idx_type get_idx() const {return current - begin;}
iterator& operator ++() { current++; return *this; }
const iterator operator ++(int) { iterator temp = *this; current++; return temp; }
operator value_type*() {return *current;}
CvArrayIterator() {current = begin = 0;}
CvArrayIterator( value_type* _begin ) {begin = current = _begin;}
CvArrayIterator( value_type* _begin, value_type* _current )
{begin = _begin; current = _current;}
CvArrayIterator( const iterator& another ) {*this = another;}
void init( value_type* _begin, value_type* _current )
{begin = _begin; current = _current;}
bool operator == ( const iterator& another ) const
{return (current == another.current) && (begin == another.begin);}
bool operator != ( const iterator& another ) const
{return (current != another.current) || (begin != another.begin);}
value_type* get_begin(){ return begin; }
value_type* get_current(){ return current; }
protected:
value_type* current;
value_type* begin;
};
template<class Node> class CvTreeIterator
{
public:
typedef Node node_type;
typedef typename node_type::value_type value_type;
typedef typename node_type::idx_type idx_type;
typedef CvTreeIterator<node_type> iterator;
value_type& operator *() { assert( node != 0 ); return node->val; }
idx_type get_idx() { assert( node != 0 ); return node->idx; }
node_type* get_node() { assert( node != 0 ); return node; }
iterator operator ++();
iterator operator ++(int);
iterator& operator =( const iterator& _iterator )
{ node = _iterator.node; return *this; }
CvTreeIterator() { node = 0; }
CvTreeIterator( const iterator& another ) { node = another.node; }
CvTreeIterator( node_type* root_node ) { node = root_node; }
void init( node_type* _node ) { node = _node; }
bool operator==( const iterator& another ) { return node == another.node; }
bool operator!=( const iterator& another ) { return node != another.node; }
protected:
node_type* node;
node_type* next();
};
template<class Node> class _CvNodeManager
{
public:
typedef Node node_type;
typedef node_type::value_type value_type;
typedef node_type::idx_type idx_type;
typedef _CvNodeBlock<node_type> block_type;
typedef CvNodeIterator<node_type> iterator;
typedef _CvNodeManager<node_type> manager_type;
_CvNodeManager();
~_CvNodeManager() { destroy(); }
void destroy();
void clear();
node_type* new_node();
void release_node( node_type* _node );
iterator begin() const { return iterator( first_block ); }
iterator end() const { return iterator( last_block, block_type::block_nodes - 1 ); }
protected:
node_type* first_free;
block_type* first_block;
block_type* last_block;
node_type* allocate_new_block();
};
/************************* tree class *******************************/
template<class Val, class Idx = int> class CvTree
{
public:
typedef Val value_type;
typedef Idx idx_type;
typedef _CvTreeNode<value_type, idx_type> node_type;
typedef _CvNodeManager<node_type> node_manager;
typedef CvNodeIterator<node_type> raw_iterator;
typedef CvTreeIterator<node_type> iterator;
typedef CvTree<value_type,idx_type> storage;
value_type query( idx_type idx ) const;
value_type& operator []( idx_type idx )
{ assert( size == 0 || idx < size ); return create_node( idx )->val; }
void remove( idx_type idx );
void clear() { root = 0; size = 0; manager.clear(); }
void destroy() { root = 0; size = 0; manager.destroy(); }
idx_type get_size() const { return size; }
iterator begin() const;
iterator end() const;
raw_iterator raw_begin() const { return manager.begin(); }
raw_iterator raw_end() const { return manager.end(); }
void set_size( idx_type _size ) { size = _size; }
CvTree() {root = 0; size = 0;}
CvTree( idx_type _size ) { root = 0; size = _size; }
CvTree( const storage& another );
storage& operator = ( const storage& another );
template<class Op> double operate_with( const storage& another, Op operation ) const
{
iterator iter1 = begin();
iterator iter2 = another.begin();
iterator end1 = end();
iterator end2 = another.end();
Op::result_type s = 0.0;
value_type val = 0;
do{
if( iter1.get_idx() > iter2.get_idx() )
s = operation( s, val, *iter2++ );
else if( iter1.get_idx() < iter2.get_idx() )
s = operation( s, *iter1++, (value_type)0 );
else if( iter1.get_idx() == iter2.get_idx() )
s = operation( s, *iter1++, *iter2++ );
}while( iter1 != end1 && iter2 != end2 );
if( iter1.get_idx() == iter2.get_idx() ) s = operation( s, *iter1, *iter2 );
return s;
}
node_type* get_root() const { return root; }
protected:
node_type* root;
node_manager manager;
idx_type size;
node_type* create_node( idx_type idx );
};
/************************* array class *******************************/
template<class Val> class CvArray
{
public:
typedef Val value_type;
typedef int idx_type;
typedef CvArrayIterator<value_type> iterator;
typedef iterator raw_iterator;
typedef CvArray<value_type> storage;
value_type query( idx_type idx ) const
{ assert( size == 0 || idx < size );
return array[idx]; }
value_type& operator []( idx_type idx )
{ assert( size == 0 || idx < size ); return array[idx]; }
void remove( idx_type idx );
void clear();
void destroy();
idx_type get_size() const { return size; };
idx_type get_capacity(){ return capacity; };
value_type* get_array() const { return array; }
iterator begin() const;
iterator end() const;
raw_iterator raw_begin() const;
raw_iterator raw_end() const;
void set_size( idx_type _size );
void set_capacity( idx_type _capacity );
CvArray() { array = 0; size = capacity = 0; }
CvArray( idx_type _size );
CvArray( idx_type _size, idx_type _capacity );
CvArray( const storage& another );
~CvArray() { if( array != 0 ) delete array; }
storage& operator = ( const storage& another );
template<class Op> double operate_with( const storage& another, Op operation ) const
{
Op::result_type s = 0.0;
for( idx_type i = 0; i < size; i++ )
s = operation( s, array[i], another.array[i] );
return s;
}
protected:
value_type* array;
idx_type size;
idx_type capacity;
};
/****************************************************************************************\
* Multi-dimensional histogram *
\****************************************************************************************/
template <class Storage = CvArray<class Val> > class CVHistogram
{
public:
typedef Storage storage_type;
typedef storage_type::value_type value_type; // type of histogram bins
typedef storage_type::idx_type idx_type; // type of bin indices
typedef typename storage_type::iterator iterator;
typedef typename storage_type::raw_iterator raw_iterator;
typedef CVHistogram<storage_type> histogram;
void create( int bins0, int bins1 = 0, int bins2 = 0,
int bins3 = 0, int bins4 = 0, int bins5 = 0 );
void clear() { storage.clear(); } // clear histogram
void destroy() { storage.destroy(); dims = 0; } // clear and free memory
// lookup operations
value_type query( int i0 ) const { return storage.query( i0 ); }
value_type query( int i0, int i1 ) const
{ return storage.query( i0 + i1 * mbins[1] ); }
value_type query( int i0, int i1, int i2 ) const
{ return storage.query( i0 + i1 * mbins[1] + i2 * mbins[2] ); }
value_type query( int i0, int i1, int i2, int i3, int i4 = 0, int i5 = 0 ) const
{ return storage.query( get_idx( i0, i1, i2, i3, i4, i5 ) ); }
value_type query( const int* idxs ) const;
// retrieving references to bins
value_type& operator []( int i0 ) { return storage[i0]; }
value_type& operator ()( int i0 ) { return storage[i0]; }
value_type& operator ()( int i0, int i1 ) { return storage[i0 + i1 * mbins[1]]; }
value_type& operator ()( int i0, int i1, int i2 )
{ return storage[i0 + i1 * mbins[1] + i2 * mbins[2]]; }
value_type& operator ()( int i0, int i1, int i2, int i3, int i4 = 0, int i5 = 0 )
{ return storage[get_idx( i0, i1, i2, i3, i4, i5 )]; }
value_type& operator ()( const int* idxs );
// hi-level iterators
iterator begin() const { return storage.begin(); }
iterator end() const { return storage.end(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -