io_utils 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef OSG_IO_UTILS
  14. #define OSG_IO_UTILS 1
  15. #include <ostream>
  16. #include <istream>
  17. #include <sstream>
  18. #include <osg/Vec4d>
  19. #include <osg/Vec4ub>
  20. #include <osg/Vec2b>
  21. #include <osg/Vec3b>
  22. #include <osg/Vec4b>
  23. #include <osg/Vec2s>
  24. #include <osg/Vec3s>
  25. #include <osg/Vec4s>
  26. #include <osg/Vec2i>
  27. #include <osg/Vec3i>
  28. #include <osg/Vec4i>
  29. #include <osg/Matrixf>
  30. #include <osg/Matrixd>
  31. #include <osg/Plane>
  32. namespace osg {
  33. /** Convinience class for building std::string using stringstream.
  34. * Usage:
  35. * MakeString str;
  36. * std::string s = str<<"Mix strings with numbers "<<0" ;
  37. * std::string s2 = str.clear()<<"and other classes such as ("<<osg::Vec3(0.0,1.0,3.0)<<)" ; */
  38. class MakeString
  39. {
  40. public:
  41. MakeString() {}
  42. std::stringstream sstream;
  43. template<typename T>
  44. MakeString& operator << (const T& t)
  45. {
  46. sstream << t;
  47. return *this;
  48. }
  49. MakeString& operator << (std::ostream& (*fun)(std::ostream&))
  50. {
  51. sstream << fun;
  52. return *this;
  53. }
  54. inline MakeString& clear() { sstream.str("") ; return *this; }
  55. inline operator std::string () const { return sstream.str(); }
  56. inline std::string str() const { return sstream.str(); }
  57. };
  58. inline std::ostream& operator << (std::ostream& output, const MakeString& str) { output << str.str(); return output; }
  59. //////////////////////////////////////////////////////////////////////////
  60. // Vec2f streaming operators
  61. inline std::ostream& operator << (std::ostream& output, const Vec2f& vec)
  62. {
  63. output << vec._v[0] << " " << vec._v[1];
  64. return output; // to enable cascading
  65. }
  66. inline std::istream& operator >> (std::istream& input, Vec2f& vec)
  67. {
  68. input >> vec._v[0] >> std::ws >> vec._v[1];
  69. return input;
  70. }
  71. //////////////////////////////////////////////////////////////////////////
  72. // Vec2d steaming operators.
  73. inline std::ostream& operator << (std::ostream& output, const Vec2d& vec)
  74. {
  75. output << vec._v[0] << " " << vec._v[1];
  76. return output; // to enable cascading
  77. }
  78. inline std::istream& operator >> (std::istream& input, Vec2d& vec)
  79. {
  80. input >> vec._v[0] >> std::ws >> vec._v[1];
  81. return input;
  82. }
  83. //////////////////////////////////////////////////////////////////////////
  84. // Vec3f steaming operators.
  85. inline std::ostream& operator << (std::ostream& output, const Vec3f& vec)
  86. {
  87. output << vec._v[0] << " "
  88. << vec._v[1] << " "
  89. << vec._v[2];
  90. return output; // to enable cascading
  91. }
  92. inline std::istream& operator >> (std::istream& input, Vec3f& vec)
  93. {
  94. input >> vec._v[0] >> std::ws >> vec._v[1] >> std::ws >> vec._v[2];
  95. return input;
  96. }
  97. //////////////////////////////////////////////////////////////////////////
  98. // Vec3d steaming operators.
  99. inline std::ostream& operator << (std::ostream& output, const Vec3d& vec)
  100. {
  101. output << vec._v[0] << " "
  102. << vec._v[1] << " "
  103. << vec._v[2];
  104. return output; // to enable cascading
  105. }
  106. inline std::istream& operator >> (std::istream& input, Vec3d& vec)
  107. {
  108. input >> vec._v[0] >> std::ws >> vec._v[1] >> std::ws >> vec._v[2];
  109. return input;
  110. }
  111. //////////////////////////////////////////////////////////////////////////
  112. // Vec3f steaming operators.
  113. inline std::ostream& operator << (std::ostream& output, const Vec4f& vec)
  114. {
  115. output << vec._v[0] << " "
  116. << vec._v[1] << " "
  117. << vec._v[2] << " "
  118. << vec._v[3];
  119. return output; // to enable cascading
  120. }
  121. inline std::istream& operator >> (std::istream& input, Vec4f& vec)
  122. {
  123. input >> vec._v[0] >> std::ws
  124. >> vec._v[1] >> std::ws
  125. >> vec._v[2] >> std::ws
  126. >> vec._v[3];
  127. return input;
  128. }
  129. //////////////////////////////////////////////////////////////////////////
  130. // Vec4d steaming operators.
  131. inline std::ostream& operator << (std::ostream& output, const Vec4d& vec)
  132. {
  133. output << vec._v[0] << " "
  134. << vec._v[1] << " "
  135. << vec._v[2] << " "
  136. << vec._v[3];
  137. return output; // to enable cascading
  138. }
  139. inline std::istream& operator >> (std::istream& input, Vec4d& vec)
  140. {
  141. input >> vec._v[0] >> std::ws
  142. >> vec._v[1] >> std::ws
  143. >> vec._v[2] >> std::ws
  144. >> vec._v[3];
  145. return input;
  146. }
  147. //////////////////////////////////////////////////////////////////////////
  148. // Vec2b steaming operators.
  149. inline std::ostream& operator << (std::ostream& output, const Vec2b& vec)
  150. {
  151. output << (int)vec._v[0] << " "
  152. << (int)vec._v[1];
  153. return output; // to enable cascading
  154. }
  155. inline std::istream& operator >> (std::istream& input, Vec2b& vec)
  156. {
  157. input >> vec._v[0] >> std::ws >> vec._v[1];
  158. return input;
  159. }
  160. //////////////////////////////////////////////////////////////////////////
  161. // Vec3b steaming operators.
  162. inline std::ostream& operator << (std::ostream& output, const Vec3b& vec)
  163. {
  164. output << (int)vec._v[0] << " "
  165. << (int)vec._v[1] << " "
  166. << (int)vec._v[2];
  167. return output; // to enable cascading
  168. }
  169. inline std::istream& operator >> (std::istream& input, Vec3b& vec)
  170. {
  171. input >> vec._v[0] >> std::ws >> vec._v[1] >> std::ws >> vec._v[2];
  172. return input;
  173. }
  174. //////////////////////////////////////////////////////////////////////////
  175. // Vec4b steaming operators.
  176. inline std::ostream& operator << (std::ostream& output, const Vec4b& vec)
  177. {
  178. output << (int)vec._v[0] << " "
  179. << (int)vec._v[1] << " "
  180. << (int)vec._v[2] << " "
  181. << (int)vec._v[3];
  182. return output; // to enable cascading
  183. }
  184. inline std::istream& operator >> (std::istream& input, Vec4b& vec)
  185. {
  186. input >> vec._v[0] >> std::ws
  187. >> vec._v[1] >> std::ws
  188. >> vec._v[2] >> std::ws
  189. >> vec._v[3];
  190. return input;
  191. }
  192. //////////////////////////////////////////////////////////////////////////
  193. // Vec2s steaming operators.
  194. inline std::ostream& operator << (std::ostream& output, const Vec2s& vec)
  195. {
  196. output << (int)vec._v[0] << " "
  197. << (int)vec._v[1];
  198. return output; // to enable cascading
  199. }
  200. inline std::istream& operator >> (std::istream& input, Vec2s& vec)
  201. {
  202. input >> vec._v[0] >> std::ws >> vec._v[1];
  203. return input;
  204. }
  205. //////////////////////////////////////////////////////////////////////////
  206. // Vec3s steaming operators.
  207. inline std::ostream& operator << (std::ostream& output, const Vec3s& vec)
  208. {
  209. output << (int)vec._v[0] << " "
  210. << (int)vec._v[1] << " "
  211. << (int)vec._v[2];
  212. return output; // to enable cascading
  213. }
  214. inline std::istream& operator >> (std::istream& input, Vec3s& vec)
  215. {
  216. input >> vec._v[0] >> std::ws >> vec._v[1] >> std::ws >> vec._v[2];
  217. return input;
  218. }
  219. //////////////////////////////////////////////////////////////////////////
  220. // Vec4s steaming operators.
  221. inline std::ostream& operator << (std::ostream& output, const Vec4s& vec)
  222. {
  223. output << (int)vec._v[0] << " "
  224. << (int)vec._v[1] << " "
  225. << (int)vec._v[2] << " "
  226. << (int)vec._v[3];
  227. return output; // to enable cascading
  228. }
  229. inline std::istream& operator >> (std::istream& input, Vec4s& vec)
  230. {
  231. input >> vec._v[0] >> std::ws
  232. >> vec._v[1] >> std::ws
  233. >> vec._v[2] >> std::ws
  234. >> vec._v[3];
  235. return input;
  236. }
  237. //////////////////////////////////////////////////////////////////////////
  238. // Vec2i steaming operators.
  239. inline std::ostream& operator << (std::ostream& output, const Vec2i& vec)
  240. {
  241. output << vec._v[0] << " "
  242. << vec._v[1];
  243. return output; // to enable cascading
  244. }
  245. inline std::istream& operator >> (std::istream& input, Vec2i& vec)
  246. {
  247. input >> vec._v[0] >> std::ws >> vec._v[1];
  248. return input;
  249. }
  250. //////////////////////////////////////////////////////////////////////////
  251. // Vec3i steaming operators.
  252. inline std::ostream& operator << (std::ostream& output, const Vec3i& vec)
  253. {
  254. output << vec._v[0] << " "
  255. << vec._v[1] << " "
  256. << vec._v[2];
  257. return output; // to enable cascading
  258. }
  259. inline std::istream& operator >> (std::istream& input, Vec3i& vec)
  260. {
  261. input >> vec._v[0] >> std::ws >> vec._v[1] >> std::ws >> vec._v[2];
  262. return input;
  263. }
  264. //////////////////////////////////////////////////////////////////////////
  265. // Vec4i steaming operators.
  266. inline std::ostream& operator << (std::ostream& output, const Vec4i& vec)
  267. {
  268. output << vec._v[0] << " "
  269. << vec._v[1] << " "
  270. << vec._v[2] << " "
  271. << vec._v[3];
  272. return output; // to enable cascading
  273. }
  274. inline std::istream& operator >> (std::istream& input, Vec4i& vec)
  275. {
  276. input >> vec._v[0] >> std::ws
  277. >> vec._v[1] >> std::ws
  278. >> vec._v[2] >> std::ws
  279. >> vec._v[3];
  280. return input;
  281. }
  282. //////////////////////////////////////////////////////////////////////////
  283. // Matrixf steaming operators.
  284. inline std::ostream& operator<< (std::ostream& os, const Matrixf& m )
  285. {
  286. os << "{"<<std::endl;
  287. for(int row=0; row<4; ++row) {
  288. os << "\t";
  289. for(int col=0; col<4; ++col)
  290. os << m(row,col) << " ";
  291. os << std::endl;
  292. }
  293. os << "}" << std::endl;
  294. return os;
  295. }
  296. //////////////////////////////////////////////////////////////////////////
  297. // Matrixd steaming operators.
  298. inline std::ostream& operator<< (std::ostream& os, const Matrixd& m )
  299. {
  300. os << "{"<<std::endl;
  301. for(int row=0; row<4; ++row) {
  302. os << "\t";
  303. for(int col=0; col<4; ++col)
  304. os << m(row,col) << " ";
  305. os << std::endl;
  306. }
  307. os << "}" << std::endl;
  308. return os;
  309. }
  310. //////////////////////////////////////////////////////////////////////////
  311. // Vec4ub steaming operators.
  312. inline std::ostream& operator << (std::ostream& output, const Vec4ub& vec)
  313. {
  314. output << (int)vec._v[0] << " "
  315. << (int)vec._v[1] << " "
  316. << (int)vec._v[2] << " "
  317. << (int)vec._v[3];
  318. return output; // to enable cascading
  319. }
  320. inline std::istream& operator >> (std::istream& input, Vec4ub& vec)
  321. {
  322. input >> vec._v[0] >> std::ws
  323. >> vec._v[1] >> std::ws
  324. >> vec._v[2] >> std::ws
  325. >> vec._v[3];
  326. return input;
  327. }
  328. //////////////////////////////////////////////////////////////////////////
  329. // Quat steaming operators.
  330. inline std::ostream& operator << (std::ostream& output, const Quat& vec)
  331. {
  332. output << vec._v[0] << " "
  333. << vec._v[1] << " "
  334. << vec._v[2] << " "
  335. << vec._v[3];
  336. return output; // to enable cascading
  337. }
  338. inline std::istream& operator >> (std::istream& input, Quat& vec)
  339. {
  340. input >> vec._v[0] >> std::ws
  341. >> vec._v[1] >> std::ws
  342. >> vec._v[2] >> std::ws
  343. >> vec._v[3];
  344. return input;
  345. }
  346. //////////////////////////////////////////////////////////////////////////
  347. // Plane steaming operators.
  348. inline std::ostream& operator << (std::ostream& output, const Plane& pl)
  349. {
  350. output << pl[0] << " "
  351. << pl[1] << " "
  352. << pl[2] << " "
  353. << pl[3];
  354. return output; // to enable cascading
  355. }
  356. inline std::istream& operator >> (std::istream& input, Plane& vec)
  357. {
  358. input >> vec[0] >> std::ws
  359. >> vec[1] >> std::ws
  360. >> vec[2] >> std::ws
  361. >> vec[3];
  362. return input;
  363. }
  364. } // end of namespace osg
  365. #endif