cvstd.inl.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  16. // Third party copyrights are property of their respective owners.
  17. //
  18. // Redistribution and use in source and binary forms, with or without modification,
  19. // are permitted provided that the following conditions are met:
  20. //
  21. // * Redistribution's of source code must retain the above copyright notice,
  22. // this list of conditions and the following disclaimer.
  23. //
  24. // * Redistribution's in binary form must reproduce the above copyright notice,
  25. // this list of conditions and the following disclaimer in the documentation
  26. // and/or other materials provided with the distribution.
  27. //
  28. // * The name of the copyright holders may not be used to endorse or promote products
  29. // derived from this software without specific prior written permission.
  30. //
  31. // This software is provided by the copyright holders and contributors "as is" and
  32. // any express or implied warranties, including, but not limited to, the implied
  33. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  34. // In no event shall the Intel Corporation or contributors be liable for any direct,
  35. // indirect, incidental, special, exemplary, or consequential damages
  36. // (including, but not limited to, procurement of substitute goods or services;
  37. // loss of use, data, or profits; or business interruption) however caused
  38. // and on any theory of liability, whether in contract, strict liability,
  39. // or tort (including negligence or otherwise) arising in any way out of
  40. // the use of this software, even if advised of the possibility of such damage.
  41. //
  42. //M*/
  43. #ifndef OPENCV_CORE_CVSTDINL_HPP
  44. #define OPENCV_CORE_CVSTDINL_HPP
  45. #include <complex>
  46. #include <ostream>
  47. //! @cond IGNORED
  48. #ifdef _MSC_VER
  49. #pragma warning( push )
  50. #pragma warning( disable: 4127 )
  51. #endif
  52. namespace cv
  53. {
  54. template<typename _Tp> class DataType< std::complex<_Tp> >
  55. {
  56. public:
  57. typedef std::complex<_Tp> value_type;
  58. typedef value_type work_type;
  59. typedef _Tp channel_type;
  60. enum { generic_type = 0,
  61. depth = DataType<channel_type>::depth,
  62. channels = 2,
  63. fmt = DataType<channel_type>::fmt + ((channels - 1) << 8),
  64. type = CV_MAKETYPE(depth, channels) };
  65. typedef Vec<channel_type, channels> vec_type;
  66. };
  67. inline
  68. String::String(const std::string& str)
  69. : cstr_(0), len_(0)
  70. {
  71. size_t len = str.size();
  72. if (len) memcpy(allocate(len), str.c_str(), len);
  73. }
  74. inline
  75. String::String(const std::string& str, size_t pos, size_t len)
  76. : cstr_(0), len_(0)
  77. {
  78. size_t strlen = str.size();
  79. pos = min(pos, strlen);
  80. len = min(strlen - pos, len);
  81. if (!len) return;
  82. memcpy(allocate(len), str.c_str() + pos, len);
  83. }
  84. inline
  85. String& String::operator = (const std::string& str)
  86. {
  87. deallocate();
  88. size_t len = str.size();
  89. if (len) memcpy(allocate(len), str.c_str(), len);
  90. return *this;
  91. }
  92. inline
  93. String& String::operator += (const std::string& str)
  94. {
  95. *this = *this + str;
  96. return *this;
  97. }
  98. inline
  99. String::operator std::string() const
  100. {
  101. return std::string(cstr_, len_);
  102. }
  103. inline
  104. String operator + (const String& lhs, const std::string& rhs)
  105. {
  106. String s;
  107. size_t rhslen = rhs.size();
  108. s.allocate(lhs.len_ + rhslen);
  109. if (lhs.len_) memcpy(s.cstr_, lhs.cstr_, lhs.len_);
  110. if (rhslen) memcpy(s.cstr_ + lhs.len_, rhs.c_str(), rhslen);
  111. return s;
  112. }
  113. inline
  114. String operator + (const std::string& lhs, const String& rhs)
  115. {
  116. String s;
  117. size_t lhslen = lhs.size();
  118. s.allocate(lhslen + rhs.len_);
  119. if (lhslen) memcpy(s.cstr_, lhs.c_str(), lhslen);
  120. if (rhs.len_) memcpy(s.cstr_ + lhslen, rhs.cstr_, rhs.len_);
  121. return s;
  122. }
  123. inline
  124. FileNode::operator std::string() const
  125. {
  126. String value;
  127. read(*this, value, value);
  128. return value;
  129. }
  130. template<> inline
  131. void operator >> (const FileNode& n, std::string& value)
  132. {
  133. read(n, value, std::string());
  134. }
  135. template<> inline
  136. FileStorage& operator << (FileStorage& fs, const std::string& value)
  137. {
  138. return fs << cv::String(value);
  139. }
  140. static inline
  141. std::ostream& operator << (std::ostream& os, const String& str)
  142. {
  143. return os << str.c_str();
  144. }
  145. static inline
  146. std::ostream& operator << (std::ostream& out, Ptr<Formatted> fmtd)
  147. {
  148. fmtd->reset();
  149. for(const char* str = fmtd->next(); str; str = fmtd->next())
  150. out << str;
  151. return out;
  152. }
  153. static inline
  154. std::ostream& operator << (std::ostream& out, const Mat& mtx)
  155. {
  156. return out << Formatter::get()->format(mtx);
  157. }
  158. static inline
  159. std::ostream& operator << (std::ostream& out, const UMat& m)
  160. {
  161. return out << m.getMat(ACCESS_READ);
  162. }
  163. template<typename _Tp> static inline
  164. std::ostream& operator << (std::ostream& out, const Complex<_Tp>& c)
  165. {
  166. return out << "(" << c.re << "," << c.im << ")";
  167. }
  168. template<typename _Tp> static inline
  169. std::ostream& operator << (std::ostream& out, const std::vector<Point_<_Tp> >& vec)
  170. {
  171. return out << Formatter::get()->format(Mat(vec));
  172. }
  173. template<typename _Tp> static inline
  174. std::ostream& operator << (std::ostream& out, const std::vector<Point3_<_Tp> >& vec)
  175. {
  176. return out << Formatter::get()->format(Mat(vec));
  177. }
  178. template<typename _Tp, int m, int n> static inline
  179. std::ostream& operator << (std::ostream& out, const Matx<_Tp, m, n>& matx)
  180. {
  181. return out << Formatter::get()->format(Mat(matx));
  182. }
  183. template<typename _Tp> static inline
  184. std::ostream& operator << (std::ostream& out, const Point_<_Tp>& p)
  185. {
  186. out << "[" << p.x << ", " << p.y << "]";
  187. return out;
  188. }
  189. template<typename _Tp> static inline
  190. std::ostream& operator << (std::ostream& out, const Point3_<_Tp>& p)
  191. {
  192. out << "[" << p.x << ", " << p.y << ", " << p.z << "]";
  193. return out;
  194. }
  195. template<typename _Tp, int n> static inline
  196. std::ostream& operator << (std::ostream& out, const Vec<_Tp, n>& vec)
  197. {
  198. out << "[";
  199. if (cv::traits::Depth<_Tp>::value <= CV_32S)
  200. {
  201. for (int i = 0; i < n - 1; ++i) {
  202. out << (int)vec[i] << ", ";
  203. }
  204. out << (int)vec[n-1] << "]";
  205. }
  206. else
  207. {
  208. for (int i = 0; i < n - 1; ++i) {
  209. out << vec[i] << ", ";
  210. }
  211. out << vec[n-1] << "]";
  212. }
  213. return out;
  214. }
  215. template<typename _Tp> static inline
  216. std::ostream& operator << (std::ostream& out, const Size_<_Tp>& size)
  217. {
  218. return out << "[" << size.width << " x " << size.height << "]";
  219. }
  220. template<typename _Tp> static inline
  221. std::ostream& operator << (std::ostream& out, const Rect_<_Tp>& rect)
  222. {
  223. return out << "[" << rect.width << " x " << rect.height << " from (" << rect.x << ", " << rect.y << ")]";
  224. }
  225. static inline std::ostream& operator << (std::ostream& out, const MatSize& msize)
  226. {
  227. int i, dims = msize.dims();
  228. for( i = 0; i < dims; i++ )
  229. {
  230. out << msize[i];
  231. if( i < dims-1 )
  232. out << " x ";
  233. }
  234. return out;
  235. }
  236. } // cv
  237. #ifdef _MSC_VER
  238. #pragma warning( pop )
  239. #endif
  240. //! @endcond
  241. #endif // OPENCV_CORE_CVSTDINL_HPP