Ejemplo n.º 1
0
//-----------------------------------------------------------------------------
// Sample distances to the mesh in the entire bounding box
void ImplicitMesh::Initialize()
{
  // First, delete old data grid
  delete mData;
  mData = NULL;

  // Create new grid
  Vector3<float> dim = (mBox.pMax - mBox.pMin) / mMeshSampling;
  mData = new Volume<float>(ceil(dim[0]), ceil(dim[1]), ceil(dim[2]));

  // Setup progress bar
  unsigned int totalSamples = mData->GetDimX()*mData->GetDimY()*mData->GetDimZ();
  unsigned int currentSample = 0;
  unsigned int reportFreq = totalSamples / 30;

  // Start sampling...
  std::cerr << "Computing distances to mesh [";
  int i, j, k;
  i = 0;
  for(float x = mBox.pMin[0]; x < mBox.pMax[0]-0.5*mMeshSampling; x += mMeshSampling, i++) {
    j = 0;
    for(float y = mBox.pMin[1]; y < mBox.pMax[1]-0.5*mMeshSampling; y += mMeshSampling, j++) {
      k = 0;
      for(float z = mBox.pMin[2]; z < mBox.pMax[2]-0.5*mMeshSampling; z += mMeshSampling, k++) {
        mData->SetValue(i,j,k, DistanceToPoint(x,y,z, *mSourceMesh));

        currentSample++;
        if (currentSample % reportFreq == 0)
          std::cerr << "=";
      }
    }
  }
  std::cerr << "] done" << std::endl;

  SimpleMesh dilatedMesh = *mSourceMesh;
  dilatedMesh.Initialize();
  dilatedMesh.Dilate(0.0001);

  std::cerr << "Determining inside/outside [";
  i = 0;
  currentSample = 0;
  for(float x = mBox.pMin[0]; x < mBox.pMax[0]-0.5*mMeshSampling; x += mMeshSampling, i++) {
    j = 0;
    for(float y = mBox.pMin[1]; y < mBox.pMax[1]-0.5*mMeshSampling; y += mMeshSampling, j++) {
      k = 0;
      for(float z = mBox.pMin[2]; z < mBox.pMax[2]-0.5*mMeshSampling; z += mMeshSampling, k++) {
        float distance = DistanceToPoint(x,y,z, dilatedMesh);
        if (mData->GetValue(i,j,k) - distance < 0)
          mData->SetValue(i,j,k, -mData->GetValue(i,j,k));

        currentSample++;
        if (currentSample % reportFreq == 0)
          std::cerr << "=";
      }
    }
  }

  std::cerr << "] done" << std::endl;
  Implicit::Update();
}
Ejemplo n.º 2
0
double
sObstructionMap::DistanceToSegment(int x, int y, int i)
{
  Segment& s = m_Segments[i];

  double n = (x - s.x1) * (s.x2 - s.x1) + (y - s.y1) * (s.y2 - s.y1);
  double d = square(s.x2 - s.x1) + square(s.y2 - s.y1);
  if (d == 0) {  // line segment with 0 length
    return DistanceToPoint(x, y, s.x1, s.y1);
  }
  double u = n / d;

  if (u < 0) {
    return DistanceToPoint(x, y, s.x1, s.y1);
  } else if (u > 1) {
    return DistanceToPoint(x, y, s.x2, s.y2);
  } else {
    int x3 = s.x1 + u * (s.x2 - s.x1);
    int y3 = s.y1 + u * (s.y2 - s.y1);
    return DistanceToPoint(x, y, x3, y3);
  }
}