datastructures.cpp
上传用户:fengshi120
上传日期:2014-07-17
资源大小:6155k
文件大小:8k
源码类别:

3D图形编程

开发平台:

C/C++

  1. /*************************************************************************** 
  2. *
  3. * Copyright 2000 by David Demirdjian.   All rights reserved. 
  4. *  
  5. * Developed  by David Demirdjian
  6. *  
  7. * Permission to use, copy, or modify this software and  its documentation 
  8. * for  educational  and  research purposes only and without fee  is hereby 
  9. * granted, provided  that this copyright notice and the original authors's 
  10. * names appear  on all copies and supporting documentation.  If individual 
  11. * files are  separated from  this  distribution directory  structure, this 
  12. * copyright notice must be included.  For any other uses of this software, 
  13. * in original or  modified form, including but not limited to distribution 
  14. * in whole or in  part, specific  prior permission  must be  obtained from 
  15. * MIT.  These programs shall not  be  used, rewritten, or  adapted as  the 
  16. * basis  of  a  commercial  software  or  hardware product  without  first 
  17. * obtaining appropriate  licenses from David Demirdjian.  The author makes 
  18. * no representations about the suitability of this software for any purpose.  
  19. * It is provided "as is" without express or implied warranty. 
  20. *  
  21. **************************************************************************/
  22. #include "stdafx.h"
  23. #include "datastructures.h"
  24. #include "processingmmx.h"
  25. #include <iostream>
  26. #include <fstream>
  27. using namespace std;
  28. #define ALLOC_ALIGN_MEMORY(X, X_origin, type, size) (X_origin)=(type*)malloc((size)*sizeof(type)+127); (X) = (type*)((((unsigned int)(X_origin))+127) & (~127));
  29. #define DELETENULL(object) if (object) {delete object;object = NULL;}
  30. #define FREENULL(object) if (object) {free(object); object = NULL;}
  31. typedef unsigned char uchar;
  32. typedef unsigned short ushort;
  33. #ifdef _DEBUG
  34. #define new DEBUG_NEW
  35. #undef THIS_FILE
  36. static char THIS_FILE[] = __FILE__;
  37. #endif
  38. // ******************************************************************
  39. // ******************************************************************
  40. StereoImage::StereoImage()
  41. {
  42. UNDEFINED_DEPTH = 0;
  43. width = 0;
  44. height = 0;
  45. imDepth8u = imDepth8u_origin = NULL;
  46. imDepth32f = imDepth32f_origin = NULL;
  47. depth_float_list_origin = NULL; 
  48. valid_pixels_origin = NULL;
  49. x_origin = NULL;
  50. y_origin = NULL;
  51. }
  52. StereoImage::~StereoImage()
  53. {
  54. deAlloc();
  55. }
  56. void StereoImage::alloc(int w, int h)
  57. {
  58. deAlloc();
  59. width = w;
  60. height = h;
  61. int siz = 8*(w/8+1)*h;
  62. ALLOC_ALIGN_MEMORY(imDepth8u, imDepth8u_origin, unsigned char, siz);
  63. ALLOC_ALIGN_MEMORY(imDepth32f, imDepth32f_origin, float, siz);
  64. ALLOC_ALIGN_MEMORY(depth_float_list, depth_float_list_origin, float, siz);
  65. ALLOC_ALIGN_MEMORY(valid_pixels, valid_pixels_origin, int, siz);
  66. ALLOC_ALIGN_MEMORY(x, x_origin, short, siz);
  67. ALLOC_ALIGN_MEMORY(y, y_origin, short, siz);
  68. // set (x,y) images (pixel value containing the coordinate of the pixel itself)
  69. short *ptx = x, *pty = y;
  70. for (int i=0; i<width; ++i) {
  71. for (int j=0; j<height; ++j, ++ptx,++pty) {
  72. *ptx = i; *pty = j;
  73. }
  74. }
  75. }
  76. void StereoImage::Reset(void)
  77. {
  78. }
  79. // set the left and right hand pixels of the disparity image as undefined
  80. // (they have been wrongly estimated)
  81. void StereoImage::setUndefinedBorders(int leftMargin, int rightMargin)
  82. {
  83. for (int j=0; j<height; ++j) {
  84. // left border
  85. uchar* ptd = imDepth8u + j*width;
  86. for (int i=0; i<leftMargin; ++i,++ptd) *ptd = UNDEFINED_DEPTH;
  87. // right border
  88. ptd = imDepth8u + j*width + (width-rightMargin);
  89. for (int i=0; i<rightMargin; ++i,++ptd) *ptd = UNDEFINED_DEPTH;
  90. }
  91. }
  92. void StereoImage::setUndefinedDepthValue(const uchar undefined_val)
  93. {
  94. UNDEFINED_DEPTH = undefined_val ;
  95. }
  96. void StereoImage::deAlloc()
  97. {
  98. FREENULL(imDepth8u_origin); 
  99. FREENULL(imDepth32f_origin); 
  100. FREENULL(depth_float_list_origin); 
  101. FREENULL(valid_pixels_origin);
  102. FREENULL(x_origin);
  103. FREENULL(y_origin);
  104. imDepth8u = NULL;
  105. }
  106. void StereoImage::createValidPixelsList(int topmargin)
  107. {
  108. int siz = width*height;
  109. const uchar *ptIm = imDepth8u + topmargin*width;
  110. int* ptValid = valid_pixels;
  111. short* ptx = x, *pty = y;
  112. m_nGood3DPoints=0;
  113. for (int i=topmargin*width; i<siz-topmargin*width; ++i, ++ptIm) {
  114. if (*ptIm != UNDEFINED_DEPTH) {
  115. *ptValid = i;
  116. *ptx = i%width;
  117. *pty = i/width;
  118. ++ptx; ++pty;
  119. ++ptValid;
  120. ++m_nGood3DPoints;
  121. }
  122. }
  123. }
  124. void StereoImage::generateDepth32f()
  125. {
  126. // build imDepth32f from x, y, depth_float_list and m_nGood3DPoints
  127. memset(imDepth32f, 0, sizeof(float)*width*height);
  128. short* ptx = x;
  129. short* pty = y;
  130. float * d = depth_float_list;
  131. for (int i=0; i<m_nGood3DPoints; ++i,++ptx,++pty,++d) {
  132. int idx = *ptx + width*(*pty);
  133. *(imDepth32f+idx) = *d;
  134. }
  135. }
  136. void StereoImage::generateDepth8uFromDepth32f()
  137. {
  138. unsigned char* ptDepth8u_new = imDepth8u;
  139. float* ptDepth32f_new = imDepth32f;
  140. for (int i=0; i<height*width; ++i,++ptDepth8u_new,++ptDepth32f_new) {
  141. *ptDepth8u_new = (unsigned char)(*ptDepth32f_new+0.5f);
  142. }
  143. }
  144. // ******************************************************************
  145. // ******************************************************************
  146. InputImages::InputImages()
  147. {
  148. subIm_l_origin = subIm_r_origin = subIm_t_origin = NULL; 
  149. }
  150. InputImages::~InputImages()
  151. {
  152. }
  153. void InputImages::alloc(int width, int height, int extra_margin)
  154. {
  155. // allocation
  156. ALLOC_ALIGN_MEMORY(subIm_l, subIm_l_origin, uchar, (width+extra_margin)*height );
  157. ALLOC_ALIGN_MEMORY(subIm_r, subIm_r_origin, uchar, width*height);
  158. //ALLOC_ALIGN_MEMORY(subIm_r, subIm_r_origin, uchar, (width+8)*height);
  159. ALLOC_ALIGN_MEMORY(subIm_t, subIm_t_origin, uchar, width*(height+extra_margin) );
  160. setMMX((char*) subIm_l, 0, (width+extra_margin)*height );
  161. setMMX((char*) subIm_r, 0, width*height);
  162. setMMX((char*) subIm_t, 0, width*(height+extra_margin) );
  163. }
  164. void InputImages::deAlloc()
  165. {
  166. FREENULL(subIm_l_origin); 
  167. FREENULL(subIm_r_origin); 
  168. FREENULL(subIm_t_origin); 
  169. }
  170. // ******************************************************************
  171. // ******************************************************************
  172. ReconstPoints3D::ReconstPoints3D()
  173. {
  174. m_fExtentMinX = 0.0f;
  175. m_fExtentMaxX = 0.0f;
  176. m_fExtentMinY = 0.0f;
  177. m_fExtentMaxY = 0.0f;
  178. m_fExtentMinZ = 0.0f;
  179. m_fExtentMaxZ = 0.0f;
  180. m_p3DPointsX_origin = NULL;
  181. m_p3DPointsY_origin = NULL;
  182. m_p3DPointsZ_origin = NULL;
  183. m_p3DPointsC = NULL;
  184. m_p3DPointsS = NULL;
  185. m_nGood3DPoints = 0;
  186. }
  187. ReconstPoints3D::~ReconstPoints3D()
  188. {
  189. FreeMemoryBuffers();
  190. }
  191. void ReconstPoints3D::alloc(int N)
  192. {
  193. // 3D reconstructions
  194. ALLOC_ALIGN_MEMORY(m_p3DPointsX, m_p3DPointsX_origin, float, N);
  195. ALLOC_ALIGN_MEMORY(m_p3DPointsY, m_p3DPointsY_origin, float, N);
  196. ALLOC_ALIGN_MEMORY(m_p3DPointsZ, m_p3DPointsZ_origin, float, N);
  197. //m_p3DPointsC = new Color[N];
  198. m_p3DPointsC = new uchar[N*4];
  199. m_p3DPointsS = new float[N];
  200. }
  201. void ReconstPoints3D::FreeMemoryBuffers(void)
  202. {
  203. FREENULL(m_p3DPointsX_origin);
  204. FREENULL(m_p3DPointsY_origin);
  205. FREENULL(m_p3DPointsZ_origin);
  206. if(m_p3DPointsC)
  207. delete [] m_p3DPointsC;
  208. if(m_p3DPointsS)
  209. delete [] m_p3DPointsS;
  210. }
  211. int ReconstPoints3D::load(char* filename)
  212. {
  213. ifstream file(filename, ios::in);   
  214. if (!file) {
  215. return 0;
  216. }
  217. // read num. of points
  218. file >> m_nGood3DPoints;
  219. alloc(m_nGood3DPoints);
  220. // read points coo3D
  221. float* ptX=m_p3DPointsX, *ptY=m_p3DPointsY, *ptZ=m_p3DPointsZ;
  222. while (!file.eof()) {
  223. file >> *ptX;
  224. file >> *ptY;
  225. file >> *ptZ;
  226. ++ptX; ++ptY; ++ptZ;
  227. }
  228. return 1;
  229. }
  230. int ReconstPoints3D::save(char* filename)
  231. {
  232. ofstream file(filename, ios::out);   
  233. if (file.fail()) return 0;
  234. file << m_nGood3DPoints << endl;//" " << width << " " << height << endl;
  235. float* ptX=m_p3DPointsX, *ptY=m_p3DPointsY, *ptZ=m_p3DPointsZ;
  236. for (int i=0; i<(int)m_nGood3DPoints; ++i, ++ptX,++ptY,++ptZ) {
  237. file << *ptX << " " << *ptY << " " << *ptZ << " ";
  238. }
  239. return 1;
  240. }
  241. float* ReconstPoints3D::getXlist() const
  242. {
  243. return m_p3DPointsX;
  244. }
  245. float* ReconstPoints3D::getYlist() const
  246. {
  247. return m_p3DPointsY;
  248. }
  249. float* ReconstPoints3D::getZlist() const
  250. {
  251. return m_p3DPointsZ;
  252. }
  253. unsigned char* ReconstPoints3D::getARGBlist() const
  254. {
  255. return m_p3DPointsC;
  256. }
  257. float* ReconstPoints3D::getPointSizelist() const
  258. {
  259. return m_p3DPointsS;
  260. }
  261. int ReconstPoints3D::getNumPoints() const
  262. {
  263. return m_nGood3DPoints;
  264. }
  265. // return 3 images containing X,Y and Z values
  266. void ReconstPoints3D::getImages_3D(float* X_im, float* Y_im, float* Z_im, const int* valid_pixels)
  267. {
  268. }