For test cases I need a unique way to set any structure to zero. For openCV I wrote this:
// Fill every basic structure of openCV with zero.
template < typename Tp >
void zero( cv::Point_<Tp>& p ) {
p.x = p.y = 0;
}
template < typename Tp >
void zero( cv::Point3_<Tp>& p ) {
p.x = p.y = p.z = 0;
}
template < typename Tp >
void zero( cv::Size_<Tp>& s ) {
s.width = s.height = 0;
}
template < typename Tp >
void zero( cv::Rect_<Tp>& r ) {
r.x = r.y = r.width = r.height = 0;
}
inline void zero( cv::RotatedRect& r ) {
zero( r.center );
zero( r.size );
r.angle = 0;
}
inline void zero( cv::TermCriteria& t ) {
t.type = t.maxCount = 0;
t.epsilon = 0.0;
}
template < typename Tp, int M, int N>
void zero( cv::Matx<Tp, M, N>& m ) {
m = 0;
}
// Vec<> and Scalar<> are derived from Matx<>
inline void zero( cv::Range& r ) {
r.start = r.end = 0;
}
inline void zero( cv::Mat& m ) {
m = 0;
}
inline void zero( cv::SparseMat& sm ) {
sm.clear();
}
template < typename Tp >
void zero( cv::SparseMat_<Tp>& sm ) {
sm.clear();
}
Setting cv::Mat
to zero by assigning a new zero filled Mat is reallocating. But looping over would be slow (now done by *= 0). Using memset( m.data, ... )
is only possible if m.isContinuous()
. Any idea of being safe and fast? Is it possible to join some derived cases?
The template parameters _Tp
has been replaced by Tp
because starting with _
, followed by uppercase letter is reserved. That is something the openCV authors do violate too (I copied it from the openCV core code).
Mat
, Matx
and derived Vec
and Scalar
can be set to zero by multiplying with zero. Internal this is a for()
over all elements but without the realloc of the zeros()
assignment.
_
, followed by uppercase letter, are reserved for use by the compiler and Standard library. Better just name your template parametersT
. – glampert Jan 11 at 16:33