#include "object.h"

object::object()
{
}

object::~object()
{
}

int object::clear()
{
  pointset.clear();
  return 0;
}

int object::addpixel( int x, int y, int z )
{
  point3d p3;
  p3.x = x;
  p3.y = y;
  p3.z = z;
  pointset.insert( p3 );
  return 0;
}

int object::count( int x, int y, int z )
{
  point3d p3;
  p3.x = x;
  p3.y = y;
  p3.z = z;
  return (int)pointset.count( p3 );
}

int object::xmlprint( std::string file )
{
  std::ofstream of;
  of.open( file.c_str() );
  of << "<object>\n";
  for( std::set< point3d, point3d >::iterator it = pointset.begin();
       it != pointset.end(); it++ )
    {
      of << "  <point coor=\"" << it->x << " "
         << it->y << " " << it->z << "\"></point>\n";
    }
  of << "</object>\n";
  of.close();
  return 0;
}

int object::expand()
{
  std::set< point3d, point3d > newset;
  for( std::set< point3d, point3d >::iterator it = pointset.begin();
       it != pointset.end(); it++ )
    {
      point3d p3;

      for( p3.x = it->x-1; p3.x < it->x+2; p3.x++ )
        for( p3.y = it->y-1; p3.y < it->y+2; p3.y++ )
          for( p3.z = it->z-1; p3.z < it->z+2; p3.z++ )
            newset.insert(p3);
    }
  pointset = newset;
  return 0;
}

int object::errode()
{
  std::set< point3d, point3d > newset;
  for( std::set< point3d, point3d >::iterator it = pointset.begin();
       it != pointset.end(); it++ )
    {
      int flag;
      point3d p3;
      flag = 0;
      // check if edge pixel
      for( p3.x = it->x-1; p3.x < it->x+2; p3.x++ )
        {
          for( p3.y = it->y-1; p3.y < it->y+2; p3.y++ )
            {
              for( p3.z = it->z-1; p3.z < it->z+2; p3.z++ )
                {
                  if( pointset.count( p3 ) == 0 )
                    flag = 1;
                }
            }
        }
      // if not edge pixel insert
      if( flag == 0 )
        newset.insert( *it );
    }
  pointset = newset;
  return 0;
}
