示例#1
0
bool LoadFromFile(Paths &ppg, char * filename, double scale= 1,
  int xOffset = 0, int yOffset = 0)
{
  ppg.clear();

  FILE *f = fopen(filename, "r");
  if (!f) return false;
  int polyCnt, vertCnt;
  char junk [80];
  double X, Y;
  if (fscanf(f, "%d", &polyCnt) == 1 && polyCnt > 0)
  {
    ppg.resize(polyCnt);
    for (int i = 0; i < polyCnt; i++) {
      if (fscanf(f, "%d", &vertCnt) != 1 || vertCnt <= 0) break;
      ppg[i].resize(vertCnt);
      for (int j = 0; j < vertCnt; j++) {
        if (fscanf(f, "%lf%*[, ]%lf", &X, &Y) != 2) break;
        ppg[i][j].X = Round((X + xOffset) * scale);
        ppg[i][j].Y = Round((Y + yOffset) * scale);
        fgets(junk, 80, f);
      }
    }
  }
  fclose(f);
  return true;
}
示例#2
0
void MakeRandomPoly(int edgeCount, int width, int height, Paths & poly)
{
  poly.resize(1);
  poly[0].resize(edgeCount);
  for (int i = 0; i < edgeCount; i++){
    poly[0][i].X = rand() % width;
    poly[0][i].Y = rand() % height;
  }
}
示例#3
0
void 
mexFunction(int nlhs, mxArray *plhs[],
	    int nrhs, const mxArray *prhs[])
{
   mxArray *par;         // ptr to mxArray structure 
   double *pda;          // ptr to polynomial data
   double *pud;          // pointer to unit conversion factor
   mxLogical *ph;        // pointer to hole flags
   double ud, iud;
   unsigned int Na, Nb, vnu;
   unsigned int k, m;
   ClipType pop;
   char ostr[STR_LEN];   //string with polygon operation


   //////////////////
   // check arguments
   //

   // argument number
   if (nrhs != 4) {
      mexErrMsgTxt("polyboolmex :  expected 4 input arguments.");
   }
   
   // argument pa
   if ( !mxIsCell(prhs[0]) ) {
      mexErrMsgTxt("polyboolmex :  argument pa must be a cell array.");
   }
   Na = mxGetM(prhs[0])*mxGetN(prhs[0]);
   if (!Na) {
      mexErrMsgTxt("polyboolmex :  no input polygons pa.");
   }
   
   // argument pb
   if ( !mxIsCell(prhs[1]) ) {
      mexErrMsgTxt("polyboolmex :  argument pb must be a cell array.");
   }
   Nb = mxGetM(prhs[1])*mxGetN(prhs[1]);
   if (!Nb) {
      mexErrMsgTxt("polyboolmex :  no input polygons pb.");
   }

   // get operation argument
   mxGetString(prhs[2], ostr, STR_LEN);
   if ( !strncmp(ostr, "or", 2) )
      pop = ctUnion; 
   else if ( !strncmp(ostr, "and", 3) )
      pop = ctIntersection; 
   else if ( !strncmp(ostr, "notb", 4) )
      pop = ctDifference; 
   else if ( !strncmp(ostr, "diff", 4) )
      pop = ctDifference; 
   else if ( !strncmp(ostr, "xor", 3) )
      pop = ctXor; 
   else {
      mexErrMsgTxt("polyboolmex :  unknown boolean set algebra operation.");
   }

   // conversion factor argument
   pud = (double*)mxGetData(prhs[3]);  
   ud = *pud;
   iud = 1.0/ud;


   ////////////////////////
   // copy and prepare data
   //

   // pa
   pa.resize(Na);
   for (k=0; k<Na; k++) {

      // get the next polygon from the cell array 
      par = mxGetCell(prhs[0], k);   // ptr to mxArray
      if ( mxIsEmpty(par) ) {
	 mexErrMsgTxt("poly_boolmex :  empty polygon in pa.");
      }
      pda = (double*)mxGetData(par); // ptr to a data     
      vnu = mxGetM(par);             // rows = vertex number

      // copy polygon and transpose, scale
      pa[k].resize(vnu);
      for (m=0; m<vnu; m++) {
	 pa[k][m].X = (cInt)(ud * pda[m]     + 0.5);
	 pa[k][m].Y = (cInt)(ud * pda[m+vnu] + 0.5);
      }

      // make sure polygons have positive orientation
      if ( !Orientation(pa[k]) )
	  ReversePath(pa[k]);
   }

   // pb
   pb.resize(Nb);
   for (k=0; k<Nb; k++) {

      // get the next polygon 
      par = mxGetCell(prhs[1], k);   // ptr to mxArray
      if ( mxIsEmpty(par) ) {
	 mexErrMsgTxt("poly_boolmex :  empty polygon in pb.");
      }
      pda = (double*)mxGetData(par); // ptr to a data     
      vnu = mxGetM(par);             // rows = vertex number

      // copy polygon and transpose, scale
      pb[k].resize(vnu);
      for (m=0; m<vnu; m++) {
	 pb[k][m].X = (cInt)(ud * pda[m]     + 0.5);
	 pb[k][m].Y = (cInt)(ud * pda[m+vnu] + 0.5);
      }

      // make sure polygons have positive orientation
      if ( !Orientation(pb[k]) )
	  ReversePath(pb[k]);
   }


   ////////////////////
   // clip the polygons
   //
   C.AddPaths(pa, ptSubject, true);
   C.AddPaths(pb, ptClip, true);

   if ( !C.Execute(pop, pc, pftEvenOdd, pftEvenOdd) )
       mexErrMsgTxt("polyboolmex :  Clipper library error.");


   //////////////////////////////////////////
   // create a cell array for output argument
   //
   plhs[0] = mxCreateCellMatrix(1, pc.size());

   //////////////////////////
   // return clipping results
   //
   for (k=0; k<pc.size(); k++) {
      
      // allocate matrix for boundary
      vnu = pc[k].size();
      par = mxCreateDoubleMatrix(vnu, 2, mxREAL);
      pda = (double*)mxGetData(par);
    
      // copy vertex array, transpose, and scale back to user units
      for (m=0; m<vnu; m++) {
         pda[m]     = iud * pc[k][m].X;
         pda[vnu+m] = iud * pc[k][m].Y;
      }

      // store in cell array
      mxSetCell(plhs[0], k, par);
   }

   ///////////////////
   // return hole flags
   //
   plhs[1] = mxCreateLogicalMatrix(1, pc.size());
   ph = (mxLogical*)mxGetData(plhs[1]);
   for (k=0; k<pc.size(); k++) 
      ph[k] = !Orientation(pc[k]); // same as input == no hole

   ///////////////////
   // clean up
   //
   C.Clear();
   pa.resize(0);
   pb.resize(0);
   pc.resize(0);
}
示例#4
0
int main(int argc, char *argv[])
{
	// Set the current locale.
	setlocale(LC_ALL, "");
	// Set the text message domain.
	bindtextdomain("tilesetter", LOCALE_DIR);
	textdomain("tilesetter");

	static const char* version = "0.1";
	std::cout << boost::format(_("Tilesetter version %1%")) % version << std::endl;
	std::cout << _(
	"Copyright (C) 2009 by Tamino Dauth\n"
	"[email protected]\n"
	"\n"
	"This program is free software; you can redistribute it and/or modify\n"
	"it under the terms of the GNU General Public License as published by\n"
	"the Free Software Foundation; either version 2 of the License, or\n"
	"(at your option) any later version.\n"
	"\n"
	"This program is distributed in the hope that it will be useful,\n"
	"but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
	"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
	"GNU General Public License for more details.\n"
	"\n"
	"You should have received a copy of the GNU General Public License\n"
	"along with this program; if not, write to the\n"
	"Free Software Foundation, Inc.,\n"
	"59 Temple Place - Suite 330, Boston, MA  02111-1307, USA."
	) << std::endl;

	typedef std::vector<boost::filesystem::path> Paths;
	typedef std::vector<std::string> Strings;
	// TODO throws exception when using escaped white spaces! - string work around
	Strings environmentStrings;
	Paths environments;

	const boost::program_options::command_line_style::style_t pstyle = boost::program_options::command_line_style::style_t(
	boost::program_options::command_line_style::unix_style
	);

	boost::program_options::options_description desc("Allowed options");
	desc.add_options()
	("version,V", _("Shows current version of tilesetter."))
	("help,h",_("Shows this text."))
	// options

	// operations
	("reassign,r", _("Reads ground and cliff types from the first environment file and assigns them to all following."))

	// input
	("files,f", boost::program_options::value<Strings>(&environmentStrings), _("Expected environment files."))
	;

	boost::program_options::positional_options_description p;
	p.add("files", -1);

	boost::program_options::variables_map vm;

	try
	{
		boost::program_options::store(boost::program_options::command_line_parser(argc, argv).style(pstyle).options(desc).positional(p).run(), vm);
	}
	catch (std::exception &exception)
	{
		std::cerr << boost::format(_("Error while parsing program options: \"%1%\"")) % exception.what() << std::endl;

		return EXIT_FAILURE;
	}

	boost::program_options::notify(vm);

	// WORKAROUND
	environments.resize(environmentStrings.size());
	environments.assign(environmentStrings.begin(), environmentStrings.end());

	if (vm.count("version"))
	{
		std::cout << boost::format(_(
		"tilesetter %1%.\n"
		"Copyright © 2009 Tamino Dauth\n"
		"License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>\n"
		"This is free software: you are free to change and redistribute it.\n"
		"There is NO WARRANTY, to the extent permitted by law."
		)) % version << std::endl;

		return EXIT_SUCCESS;
	}

	if (vm.count("help"))
	{
		std::cout << desc << std::endl;
		std::cout << _("\nReport bugs to [email protected] or on https://wc3lib.org") << std::endl;

		return EXIT_SUCCESS;
	}

	if (environments.empty())
	{
		std::cerr << _("Missing environments arguments.") << std::endl;

		return EXIT_FAILURE;
	}

	if (vm.count("reassign"))
	{
		boost::ptr_vector<map::Environment> envs;
		envs.reserve(environments.size());

		BOOST_FOREACH(Paths::const_reference path, environments)
		{
			if (!boost::filesystem::is_regular_file(path))
			{
				std::cerr << boost::format(_("File %1% does not seem to be a regular file.")) % path << std::endl;

				return EXIT_FAILURE;
			}

			boost::filesystem::ifstream ifstream;
			ifstream.exceptions(boost::filesystem::ifstream::badbit | boost::filesystem::ifstream::failbit | boost::filesystem::ifstream::eofbit);

			try
			{
				ifstream.open(path, std::ios::in | std::ios::binary);
				std::auto_ptr<map::Environment> env(new map::Environment());
				env->read(ifstream);
				envs.push_back(env);
			}
			catch (std::exception &exception)
			{
				std::cerr << boost::format(_("Error occured while opening file %1%:\n\"%2%\"")) % path % exception.what() << std::endl;

				return EXIT_FAILURE;
			}
		}

		if (envs.size() < 2)
		{
			std::cerr << _("We need at least 2 environments.") << std::endl;

			return EXIT_FAILURE;
		}

		for (std::size_t i = 1; i < envs.size(); ++i)
		{
			envs[i].cliffTilesetsIds().assign(envs[0].cliffTilesetsIds().begin(), envs[0].cliffTilesetsIds().end());
			envs[i].groundTilesetsIds().assign(envs[0].groundTilesetsIds().begin(), envs[0].groundTilesetsIds().end());

			boost::filesystem::ofstream ofstream;
			ofstream.exceptions(boost::filesystem::ofstream::badbit | boost::filesystem::ofstream::failbit | boost::filesystem::ofstream::eofbit);

			try
			{
				ofstream.open(environments[i], std::ios::out | std::ios::binary);
				envs[i].write(ofstream);
			}
			catch (std::exception &exception)
			{
				std::cerr << boost::format(_("Error occured while saving file %1%:\n\"%2%\"")) % environments[i] % exception.what() << std::endl;

				return EXIT_FAILURE;
			}
		}
	}