datastructures.cpp
资源名称:estereo2.zip [点击查看]
上传用户:fengshi120
上传日期:2014-07-17
资源大小:6155k
文件大小:8k
源码类别:
3D图形编程
开发平台:
C/C++
- /***************************************************************************
- *
- * Copyright 2000 by David Demirdjian. All rights reserved.
- *
- * Developed by David Demirdjian
- *
- * Permission to use, copy, or modify this software and its documentation
- * for educational and research purposes only and without fee is hereby
- * granted, provided that this copyright notice and the original authors's
- * names appear on all copies and supporting documentation. If individual
- * files are separated from this distribution directory structure, this
- * copyright notice must be included. For any other uses of this software,
- * in original or modified form, including but not limited to distribution
- * in whole or in part, specific prior permission must be obtained from
- * MIT. These programs shall not be used, rewritten, or adapted as the
- * basis of a commercial software or hardware product without first
- * obtaining appropriate licenses from David Demirdjian. The author makes
- * no representations about the suitability of this software for any purpose.
- * It is provided "as is" without express or implied warranty.
- *
- **************************************************************************/
- #include "stdafx.h"
- #include "datastructures.h"
- #include "processingmmx.h"
- #include <iostream>
- #include <fstream>
- using namespace std;
- #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));
- #define DELETENULL(object) if (object) {delete object;object = NULL;}
- #define FREENULL(object) if (object) {free(object); object = NULL;}
- typedef unsigned char uchar;
- typedef unsigned short ushort;
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- // ******************************************************************
- // ******************************************************************
- StereoImage::StereoImage()
- {
- UNDEFINED_DEPTH = 0;
- width = 0;
- height = 0;
- imDepth8u_origin = NULL;
- imDepth32f_origin = NULL;
- depth_float_list_origin = NULL;
- valid_pixels_origin = NULL;
- x_origin = NULL;
- y_origin = NULL;
- }
- StereoImage::~StereoImage()
- {
- deAlloc();
- }
- void StereoImage::alloc(int w, int h)
- {
- deAlloc();
- width = w;
- height = h;
- ALLOC_ALIGN_MEMORY(imDepth8u, imDepth8u_origin, uchar, w*h);
- ALLOC_ALIGN_MEMORY(imDepth32f, imDepth32f_origin, float, w*h);
- ALLOC_ALIGN_MEMORY(depth_float_list, depth_float_list_origin, float, w*h);
- ALLOC_ALIGN_MEMORY(valid_pixels, valid_pixels_origin, int, w*h);
- ALLOC_ALIGN_MEMORY(x, x_origin, short, w*h);
- ALLOC_ALIGN_MEMORY(y, y_origin, short, w*h);
- // set (x,y) images (pixel value containing the coordinate of the pixel itself)
- short *ptx = x, *pty = y;
- for (int i=0; i<width; ++i) {
- for (int j=0; j<height; ++j, ++ptx,++pty) {
- *ptx = i; *pty = j;
- }
- }
- }
- void StereoImage::Reset(void)
- {
- }
- // set the left and right hand pixels of the disparity image as undefined
- // (they have been wrongly estimated)
- void StereoImage::setUndefinedBorders(int leftMargin, int rightMargin)
- {
- for (int j=0; j<height; ++j) {
- // left border
- uchar* ptd = imDepth8u + j*width;
- for (int i=0; i<leftMargin; ++i,++ptd) *ptd = UNDEFINED_DEPTH;
- // right border
- ptd = imDepth8u + j*width + (width-rightMargin);
- for (int i=0; i<rightMargin; ++i,++ptd) *ptd = UNDEFINED_DEPTH;
- }
- }
- void StereoImage::setUndefinedDepthValue(const uchar undefined_val)
- {
- UNDEFINED_DEPTH = undefined_val ;
- }
- void StereoImage::deAlloc()
- {
- FREENULL(imDepth8u_origin);
- FREENULL(imDepth32f_origin);
- FREENULL(depth_float_list_origin);
- FREENULL(valid_pixels_origin);
- FREENULL(x_origin);
- FREENULL(y_origin);
- }
- void StereoImage::createValidPixelsList(int topmargin)
- {
- int siz = width*height;
- const uchar *ptIm = imDepth8u + topmargin*width;
- int* ptValid = valid_pixels;
- short* ptx = x, *pty = y;
- m_nGood3DPoints=0;
- for (int i=topmargin*width; i<siz-topmargin*width; ++i, ++ptIm) {
- if (*ptIm != UNDEFINED_DEPTH) {
- *ptValid = i;
- *ptx = i%width;
- *pty = i/width;
- ++ptx; ++pty;
- ++ptValid;
- ++m_nGood3DPoints;
- }
- }
- }
- void StereoImage::generateDepth32f()
- {
- // build imDepth32f from x, y, depth_float_list and m_nGood3DPoints
- memset(imDepth32f, 0, sizeof(float)*width*height);
- short* ptx = x;
- short* pty = y;
- float * d = depth_float_list;
- for (int i=0; i<m_nGood3DPoints; ++i,++ptx,++pty,++d) {
- int idx = *ptx + width*(*pty);
- *(imDepth32f+idx) = *d;
- }
- }
- void StereoImage::generateDepth8uFromDepth32f()
- {
- unsigned char* ptDepth8u_new = imDepth8u;
- float* ptDepth32f_new = imDepth32f;
- for (int i=0; i<height*width; ++i,++ptDepth8u_new,++ptDepth32f_new) {
- *ptDepth8u_new = (unsigned char)(*ptDepth32f_new+0.5f);
- }
- }
- // ******************************************************************
- // ******************************************************************
- InputImages::InputImages()
- {
- subIm_l_origin = subIm_r_origin = subIm_t_origin = NULL;
- }
- InputImages::~InputImages()
- {
- }
- void InputImages::alloc(int width, int height, int extra_margin)
- {
- // allocation
- ALLOC_ALIGN_MEMORY(subIm_l, subIm_l_origin, uchar, (width+extra_margin)*height );
- ALLOC_ALIGN_MEMORY(subIm_r, subIm_r_origin, uchar, width*height);
- ALLOC_ALIGN_MEMORY(subIm_t, subIm_t_origin, uchar, width*(height+extra_margin) );
- setMMX((char*) subIm_l, 0, (width+extra_margin)*height );
- setMMX((char*) subIm_r, 0, width*height);
- setMMX((char*) subIm_t, 0, width*(height+extra_margin) );
- }
- void InputImages::deAlloc()
- {
- FREENULL(subIm_l_origin);
- FREENULL(subIm_r_origin);
- FREENULL(subIm_t_origin);
- }
- // ******************************************************************
- // ******************************************************************
- ReconstPoints3D::ReconstPoints3D()
- {
- m_fExtentMinX = 0.0f;
- m_fExtentMaxX = 0.0f;
- m_fExtentMinY = 0.0f;
- m_fExtentMaxY = 0.0f;
- m_fExtentMinZ = 0.0f;
- m_fExtentMaxZ = 0.0f;
- m_p3DPointsX_origin = NULL;
- m_p3DPointsY_origin = NULL;
- m_p3DPointsZ_origin = NULL;
- m_p3DPointsC = NULL;
- m_p3DPointsS = NULL;
- m_nGood3DPoints = 0;
- }
- ReconstPoints3D::~ReconstPoints3D()
- {
- FreeMemoryBuffers();
- }
- void ReconstPoints3D::alloc(int N)
- {
- // 3D reconstructions
- ALLOC_ALIGN_MEMORY(m_p3DPointsX, m_p3DPointsX_origin, float, N);
- ALLOC_ALIGN_MEMORY(m_p3DPointsY, m_p3DPointsY_origin, float, N);
- ALLOC_ALIGN_MEMORY(m_p3DPointsZ, m_p3DPointsZ_origin, float, N);
- //m_p3DPointsC = new Color[N];
- m_p3DPointsC = new uchar[N*4];
- m_p3DPointsS = new float[N];
- }
- void ReconstPoints3D::FreeMemoryBuffers(void)
- {
- FREENULL(m_p3DPointsX_origin);
- FREENULL(m_p3DPointsY_origin);
- FREENULL(m_p3DPointsZ_origin);
- if(m_p3DPointsC)
- delete [] m_p3DPointsC;
- if(m_p3DPointsS)
- delete [] m_p3DPointsS;
- }
- int ReconstPoints3D::load(char* filename)
- {
- ifstream file(filename, ios::in);
- if (!file) {
- return 0;
- }
- // read num. of points
- file >> m_nGood3DPoints;
- alloc(m_nGood3DPoints);
- // read points coo3D
- float* ptX=m_p3DPointsX, *ptY=m_p3DPointsY, *ptZ=m_p3DPointsZ;
- while (!file.eof()) {
- file >> *ptX;
- file >> *ptY;
- file >> *ptZ;
- ++ptX; ++ptY; ++ptZ;
- }
- return 1;
- }
- int ReconstPoints3D::save(char* filename)
- {
- ofstream file(filename, ios::out);
- if (file.fail()) return 0;
- file << m_nGood3DPoints << endl;//" " << width << " " << height << endl;
- float* ptX=m_p3DPointsX, *ptY=m_p3DPointsY, *ptZ=m_p3DPointsZ;
- for (int i=0; i<(int)m_nGood3DPoints; ++i, ++ptX,++ptY,++ptZ) {
- file << *ptX << " " << *ptY << " " << *ptZ << " ";
- }
- return 1;
- }
- float* ReconstPoints3D::getXlist() const
- {
- return m_p3DPointsX;
- }
- float* ReconstPoints3D::getYlist() const
- {
- return m_p3DPointsY;
- }
- float* ReconstPoints3D::getZlist() const
- {
- return m_p3DPointsZ;
- }
- unsigned char* ReconstPoints3D::getARGBlist() const
- {
- return m_p3DPointsC;
- }
- float* ReconstPoints3D::getPointSizelist() const
- {
- return m_p3DPointsS;
- }
- int ReconstPoints3D::getNumPoints() const
- {
- return m_nGood3DPoints;
- }
- // return 3 images containing X,Y and Z values
- void ReconstPoints3D::getImages_3D(float* X_im, float* Y_im, float* Z_im, const int* valid_pixels)
- {
- }