/**************************************************************************** * VCGLib o o * * Visual and Computer Graphics Library o o * * _ O _ * * Copyright(C) 2004-2016 \/)\/ * * Visual Computing Lab /\/| * * ISTI - Italian National Research Council | * * \ * * All rights reserved. * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * * for more details. * * * ****************************************************************************/ #ifndef __VCG_OUTLINE2_PACKER_H__ #define __VCG_OUTLINE2_PACKER_H__ #include #include #include #include #include #include #include namespace vcg { template class PolyPacker { typedef typename vcg::Box2 Box2x; typedef typename vcg::Point2 Point2x; typedef typename vcg::Similarity2 Similarity2x; public: static Box2f getPolyBB(const std::vector &poly) { Box2f bb; for(size_t i=0;i &poly, float &rot) { const int stepNum=32; float bestAngle; float bestArea = std::numeric_limits::max(); Box2f bestBB; for(int i=0;i > &polyVec, const Point2i containerSizeX, std::vector &trVec, Point2x &/*coveredContainer*/) { int minSide = std::min(containerSizeX[0],containerSizeX[1]); const vcg::Point2i containerSize(minSide,minSide); int polyPerLine = ceil(sqrt((double)polyVec.size())); int pixelPerPoly = minSide / (polyPerLine); if(pixelPerPoly < 1) return false; trVec.clear(); trVec.resize(polyVec.size()); Box2f bbMax; std::vector bbVec; for(size_t i=0;iminSide) { baseY+=pixelPerPoly; baseX=0; } } return true; } static bool PackAsAxisAlignedRect(const std::vector< std::vector > &polyVec, const Point2i containerSizeX, std::vector &trVec, Point2x &coveredContainer) { trVec.clear(); trVec.resize(polyVec.size()); std::vector bbVec; for(size_t i=0;i0); bbVec.push_back(getPolyBB(polyVec[i])); } return RectPacker::Pack(bbVec,containerSizeX,trVec,coveredContainer); } static bool PackAsObjectOrientedRect(const std::vector< std::vector > &polyVec, const Point2i containerSizeX, std::vector &trVec, Point2x &coveredContainer) { trVec.clear(); trVec.resize(polyVec.size()); std::vector bbVec; std::vector rotVec; for(size_t i=0;i::Pack(bbVec,containerSizeX,trVec,coveredContainer); for(size_t i=0;i > &polyVec, const Point2x containerSizeX, const int containerNum, std::vector &trVec, std::vector &indVec, std::vector &coveredContainerVec) { trVec.clear(); trVec.resize(polyVec.size()); std::vector bbVec; std::vector rotVec; for(size_t i=0;i::PackMulti(bbVec,containerSizeI,containerNum,trVec,indVec,coveredContainerVec); for(size_t i=0;i > &polyVec, const char *filename) { FILE *fp=fopen(filename,"w"); if(!fp) return false; fprintf(fp,"%lu\n",polyVec.size()); for(size_t i=0;i > &polyVec, const char *filename) { FILE *fp=fopen(filename,"r"); if(!fp) return false; int sz; fscanf(fp,"%i\n",&sz); polyVec.clear(); polyVec.resize(sz); for(size_t i=0;i