Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// BlurMatrix.cpp: implementation of the CBlurMatrix class.
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//////////////////////////////////////////////////////////////////////
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
#include "Windows.h"
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#ifdef __sgi
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <time.h></time.h>
Toshihiro Shimizu 890ddd
typedef struct timespec {
Shinya Kitaoka 120a6e
  time_t tv_sec; /* seconds */
Shinya Kitaoka 120a6e
  long tv_nsec;  /* and nanoseconds */
Toshihiro Shimizu 890ddd
} timespec_t;
Toshihiro Shimizu 890ddd
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka c12eac
Toshihiro Shimizu 890ddd
#include <vector></vector>
Shinya Kitaoka c12eac
#include <algorithm></algorithm>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 61dd76
#include <cmath></cmath>
Shinya Kitaoka 61dd76
#include <cstdlib></cstdlib>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//#include "SError.h"
Toshihiro Shimizu 890ddd
#include "BlurMatrix.h"
Toshihiro Shimizu 890ddd
//#include "tmsg.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//////////////////////////////////////////////////////////////////////
Toshihiro Shimizu 890ddd
// Construction/Destruction
Toshihiro Shimizu 890ddd
//////////////////////////////////////////////////////////////////////
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using namespace std;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
CBlurMatrix::CBlurMatrix(const CBlurMatrix &m)
Shinya Kitaoka 120a6e
    :  // throw(SBlurMatrixError) :
Shinya Kitaoka 120a6e
    m_isSAC(m.m_isSAC)
Shinya Kitaoka 120a6e
    , m_isRS(m.m_isRS) {
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    for (int i = 0; i < NBRS; i++) m_m[i] = m.m_m[i];
Shinya Kitaoka 120a6e
  } catch (exception) {
Shinya Kitaoka 120a6e
    throw SBlurMatrixError();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
CBlurMatrix::CBlurMatrix(const double d, const int nb, const bool isSAC,
Shinya Kitaoka 120a6e
                         const bool isRS)
Toshihiro Shimizu 890ddd
// throw(SBlurMatrixError)
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    m_isRS  = isRS;
Shinya Kitaoka 120a6e
    m_isSAC = isSAC;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    for (int i = 0; i < NBRS; i++) m_m[i].clear();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    if (m_isRS) {
Shinya Kitaoka 120a6e
      createRandom(d, nb);
Shinya Kitaoka 120a6e
    } else
Shinya Kitaoka 120a6e
      createEqual(d, nb);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    if (m_isSAC) addPath();
Shinya Kitaoka 120a6e
  } catch (SBlurMatrixError) {
Shinya Kitaoka 120a6e
    throw;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool CBlurMatrix::isIn(const vector<blursection> &m, const SXYD &xyd) const {</blursection>
Shinya Kitaoka 120a6e
  for (vector<blursection>::const_iterator p = m.begin(); p != m.end(); ++p) {</blursection>
Shinya Kitaoka 120a6e
    vector<sxyd>::const_iterator pp = p->begin();</sxyd>
Shinya Kitaoka 120a6e
    if (pp->x == xyd.x && pp->y == xyd.y) return true;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void CBlurMatrix::createRandom(const double d, const int nb)
Toshihiro Shimizu 890ddd
//   throw(SBlurMatrixError)
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 120a6e
  int iNb   = 0;
Shinya Kitaoka 120a6e
  int l     = I_ROUNDP(ceil(d));
Shinya Kitaoka 120a6e
  int ll    = 2 * l + 1;
Shinya Kitaoka 120a6e
  double d2 = d * d;
Shinya Kitaoka 120a6e
  //	int maxNb=(int)(4.0*d*d*PI/5.0);
Shinya Kitaoka 120a6e
  int maxNb = (int)((double)(d * d) * 2.8) + 1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    int i = 0;
Shinya Kitaoka 120a6e
    for (i = 0; i < NBRS; i++) {
Shinya Kitaoka 120a6e
      BLURSECTION bs;
Shinya Kitaoka 120a6e
      SXYD xyd = {0, 0, 0.0};
Shinya Kitaoka 120a6e
      bs.push_back(xyd);
Shinya Kitaoka 120a6e
      m_m[i].push_back(bs);
Shinya Kitaoka 120a6e
      iNb++;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (nb <= 0 || d <= 0.01) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    for (i = 0; i < NBRS; i++) {
Shinya Kitaoka 120a6e
      iNb = 1;
Shinya Kitaoka 120a6e
      while (iNb < nb && iNb <= maxNb) {
Shinya Kitaoka 120a6e
        int x = (rand() % (ll + 1)) - l;
Shinya Kitaoka 120a6e
        int y = (rand() % (ll + 1)) - l;
Shinya Kitaoka 120a6e
        if (((double)(x * x + y * y) <= d2) && (x != 0 || y != 0)) {
Shinya Kitaoka 120a6e
          SXYD xyd1 = {x, y, 0.0};
Shinya Kitaoka 120a6e
          if (!isIn(m_m[i], xyd1)) {
Shinya Kitaoka 120a6e
            BLURSECTION bs;
Shinya Kitaoka 120a6e
            SXYD xyd = {x, y, sqrt((double)(x * x + y * y))};
Shinya Kitaoka 120a6e
            bs.push_back(xyd);
Shinya Kitaoka 120a6e
            m_m[i].push_back(bs);
Shinya Kitaoka 120a6e
            iNb++;
Shinya Kitaoka 120a6e
          }
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } catch (exception) {
Shinya Kitaoka 120a6e
    throw SBlurMatrixError();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void CBlurMatrix::createEqual(const double d, const int nb)
Toshihiro Shimizu 890ddd
// throw(SBlurMatrixError)
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 120a6e
  int nq = 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    {
Shinya Kitaoka 120a6e
      BLURSECTION bs;
Shinya Kitaoka 120a6e
      SXYD xyd = {0, 0, 0.0};
Shinya Kitaoka 120a6e
      bs.push_back(xyd);
Shinya Kitaoka 120a6e
      m_m[0].push_back(bs);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (nb <= 0 || d < 1.0) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    double b  = (double)nb * (2.0 * d + 1.0) * (2.0 * d + 1.0) / (d * d * PI);
Shinya Kitaoka 120a6e
    b         = sqrt(b);
Shinya Kitaoka 120a6e
    b         = ceil(b);
Shinya Kitaoka 120a6e
    b         = (2.0 * d + 1.0) / b;
Shinya Kitaoka 120a6e
    int di    = (int)ceil(d);
Shinya Kitaoka 120a6e
    double d2 = d * d;
Shinya Kitaoka 120a6e
    double yd = 0.0;
Shinya Kitaoka 120a6e
    for (int y = 0; y <= di;) {
Shinya Kitaoka 120a6e
      double xd = 0.0;
Shinya Kitaoka 120a6e
      for (int x = 0; x <= di;) {
Shinya Kitaoka 120a6e
        double dist = sqrt((double)(x * x + y * y));
Shinya Kitaoka 120a6e
        if (dist <= d) {
Shinya Kitaoka 120a6e
          {
Shinya Kitaoka 120a6e
            BLURSECTION bs;
Shinya Kitaoka 120a6e
            SXYD xyd = {x, y, dist};
Shinya Kitaoka 120a6e
            bs.push_back(xyd);
Shinya Kitaoka 120a6e
            m_m[0].push_back(bs);
Shinya Kitaoka 120a6e
            nq++;
Shinya Kitaoka 120a6e
          }
Shinya Kitaoka 120a6e
          if (x > 0 && y > 0) {
Shinya Kitaoka 120a6e
            BLURSECTION bs;
Shinya Kitaoka 120a6e
            SXYD xyd = {-x, -y, dist};
Shinya Kitaoka 120a6e
            bs.push_back(xyd);
Shinya Kitaoka 120a6e
            m_m[0].push_back(bs);
Shinya Kitaoka 120a6e
            nq++;
Shinya Kitaoka 120a6e
          }
Shinya Kitaoka 120a6e
          if (x > 0 && y >= 0) {
Shinya Kitaoka 120a6e
            BLURSECTION bs;
Shinya Kitaoka 120a6e
            SXYD xyd = {-x, y, dist};
Shinya Kitaoka 120a6e
            bs.push_back(xyd);
Shinya Kitaoka 120a6e
            m_m[0].push_back(bs);
Shinya Kitaoka 120a6e
            nq++;
Shinya Kitaoka 120a6e
          }
Shinya Kitaoka 120a6e
          if (x >= 0 && y > 0) {
Shinya Kitaoka 120a6e
            BLURSECTION bs;
Shinya Kitaoka 120a6e
            SXYD xyd = {x, -y, dist};
Shinya Kitaoka 120a6e
            bs.push_back(xyd);
Shinya Kitaoka 120a6e
            m_m[0].push_back(bs);
Shinya Kitaoka 120a6e
            nq++;
Shinya Kitaoka 120a6e
          }
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
        xd += b;
Shinya Kitaoka 120a6e
        x = I_ROUND(xd);
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      yd += b;
Shinya Kitaoka 120a6e
      y = I_ROUND(yd);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } catch (exception) {
Shinya Kitaoka 120a6e
    throw SBlurMatrixError();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void CBlurMatrix::addPath(vector<blursection>::iterator pBS)</blursection>
Shinya Kitaoka 120a6e
// throw(exception)
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    if (pBS->size() > 0) {
Shinya Kitaoka 120a6e
      BLURSECTION::iterator xyd = pBS->begin();
Shinya Kitaoka 120a6e
      // SXYD* xyd= pBS->begin();
Shinya Kitaoka 120a6e
      int x     = xyd->x;
Shinya Kitaoka 120a6e
      int y     = xyd->y;
Shinya Kitaoka 120a6e
      int l     = std::max(std::abs(x), std::abs(y));
Shinya Kitaoka 120a6e
      double dx = -(double)x / (double)l;
Shinya Kitaoka 120a6e
      double dy = -(double)y / (double)l;
Shinya Kitaoka 120a6e
      double xx = (double)x + dx;
Shinya Kitaoka 120a6e
      double yy = (double)y + dy;
Shinya Kitaoka 120a6e
      for (int i = 1; i <= l; i++, xx += dx, yy += dy) {
Shinya Kitaoka 120a6e
        SXYD xyd1 = {I_ROUND(xx), I_ROUND(yy), 0.0};
Shinya Kitaoka 120a6e
        pBS->push_back(xyd1);
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } catch (exception) {
Shinya Kitaoka 120a6e
    throw;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void CBlurMatrix::addPath()  // throw(SBlurMatrixError)
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    for (int i = 0; i < (m_isRS ? NBRS : 1); i++) {
Shinya Kitaoka 120a6e
      for (vector<blursection>::iterator pBS = m_m[i].begin();</blursection>
Shinya Kitaoka 120a6e
           pBS != m_m[i].end(); ++pBS) {
Shinya Kitaoka 120a6e
        addPath(pBS);
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } catch (exception) {
Shinya Kitaoka 120a6e
    throw SBlurMatrixError();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
CBlurMatrix::~CBlurMatrix() {}
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
void CBlurMatrix::print() const {
Shinya Kitaoka 120a6e
  /*
Shinya Kitaoka 120a6e
  char s[1000];
Shinya Kitaoka 120a6e
  OutputDebugString("  ====== Blur Matrix ======\n");
Shinya Kitaoka 120a6e
  int nb=m_m[0].size();
Shinya Kitaoka 120a6e
  for( int i=0; i
Shinya Kitaoka 120a6e
                   int j=0;
Shinya Kitaoka 120a6e
                   for( vector<sxyd>::const_iterator pp=m_m[0][i].begin();</sxyd>
Shinya Kitaoka 120a6e
                            pp!=m_m[0][i].end();
Shinya Kitaoka 120a6e
                            ++pp,++j ) {
Shinya Kitaoka 120a6e
                          sprintf(s,"(%d,%d,%d,%d) ",i,j,pp->x,pp->y);
Shinya Kitaoka 120a6e
                          OutputDebugString(s);
Shinya Kitaoka 120a6e
                  }
Shinya Kitaoka 120a6e
                  OutputDebugString("\n");
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  OutputDebugString("  ========================\n");
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
}