VCG Library
space_packer.cpp
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004-2009 \/)\/ *
6 * Visual Computing Lab /\/| *
7 * ISTI - Italian National Research Council | *
8 * \ *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20 * for more details. *
21 * *
22 ****************************************************************************/
23 #include <QtOpenGL/QtOpenGL>
24 #include<vcg/space/box2.h>
25 #include<vcg/space/box3.h>
26 #include<vcg/math/random_generator.h>
27 #include<wrap/qt/col_qt_convert.h>
28 #include <vcg/space/rect_packer.h>
29 #include <vcg/space/outline2_packer.h>
30 #include <vcg/space/rasterized_outline2_packer.h>
31 #include <vcg/complex/algorithms/outline_support.h>
32 #include <wrap/qt/Outline2ToQImage.h>
33 #include <wrap/qt/outline2_rasterizer.h>
34 
35 #include <time.h>
36 
37 using namespace vcg;
38 using namespace std;
39 
40 static void buildRandRectSet(int rectNum, vector<Box2f> &rectVec)
41 {
42  math::MarsenneTwisterRNG rnd;
43  float exp=3.0f;
44  float ratioMin=0.2;
45  float ratioMax=0.9;
46  float sizeMin=0.1;
47  float sizeMax=1.0f;
48  rnd.initialize(time(0));
49  for(int i=0;i<rectNum;++i)
50  {
51  Box2f bb;
52  float ratio=ratioMin+(ratioMax-ratioMin)*rnd.generate01();
53  float size= sizeMin+(sizeMax-sizeMin)*pow((float)rnd.generate01(),exp);
54  bb.min=Point2f(-size*ratio,-size);
55  bb.max=Point2f( size*ratio, size);
56  rectVec.push_back(bb);
57  }
58 }
59 
60 int main( int /*argc*/, char **/*argv*/ )
61 {
62  vector<Similarity2f> trVec;
63  vector<Similarity2f> trPolyVec;
64  vector< vector<Point2f> > outline2Vec;
65  vector< vector<Point2f> > multiPolySet;
66  Point2f finalSize;
67  std::vector<Point2f> finalSizeVec;
68  const Point2i containerSize(1024,1024);
69  Outline2Dumper::Param pp;
70  std::vector<int> contInd;
71 
72  vector<Box2f> rectVec;
73  buildRandRectSet(10,rectVec);
74 // RectPacker<float>::Pack(rectVec,containerSize,trVec,finalSize);
75  RectPacker<float>::PackMulti(rectVec,containerSize,3,trVec,contInd,finalSizeVec);
76  RectPacker<float>::Stat s = RectPacker<float>::stat();
77  printf("RectPacker attempt %i time %5.3f %5.3f\n",s.pack_attempt_num,s.pack_total_time,s.pack_attempt_time);
78 
79 // Outline2Dumper::rectSetToPolySet(rectVec,polySet);
80 
81 // Outline2Dumper::multiRectSetToSinglePolySet(rectVec,trVec,contInd,0,polySet,trPolyVec);
82 // Outline2Dumper::dumpPolySetPNG("testpolyEq0.png",polySet,trPolyVec,pp);
83 // Outline2Dumper::multiRectSetToSinglePolySet(rectVec,trVec,contInd,1,polySet,trPolyVec);
84 // Outline2Dumper::dumpPolySetPNG("testpolyEq1.png",polySet,trPolyVec,pp);
85 // Outline2Dumper::multiRectSetToSinglePolySet(rectVec,trVec,contInd,2,polySet,trPolyVec);
86 // Outline2Dumper::dumpPolySetPNG("testpolyEq2.png",polySet,trPolyVec,pp);
87 
88 
89 // buildRandPolySet(100,polySet);
90 // PolyPacker<float>::PackMultiAsObjectOrientedRect(polySet,containerSize,3,trVec,contInd,finalSizeVec);
91 
92 // Outline2Dumper::multiPolySetToSinglePolySet(polySet,trVec,contInd,0,multiPolySet,trPolyVec);
93 // Outline2Dumper::dumpPolySetPNG("testpolyEq0.png",multiPolySet,trPolyVec,pp);
94 
95 // Outline2Dumper::multiPolySetToSinglePolySet(polySet,trVec,contInd,1,multiPolySet,trPolyVec);
96 // Outline2Dumper::dumpPolySetPNG("testpolyEq1.png",multiPolySet,trPolyVec,pp);
97 
98 // Outline2Dumper::multiPolySetToSinglePolySet(polySet,trVec,contInd,2,multiPolySet,trPolyVec);
99 // Outline2Dumper::dumpPolySetPNG("testpolyEq2.png",multiPolySet,trPolyVec,pp);
100 
101  // Outline2Dumper::dumpPolySetPNG("testpolyOO.png",polySet,trVec,pp);
102 
103  vcg::tri::OutlineUtil<float>::BuildRandomOutlineVec(25,outline2Vec);
104 
105  PolyPacker<float>::PackAsEqualSquares(outline2Vec,containerSize,trVec,finalSize);
106  Outline2Dumper::dumpOutline2VecPNG("testpolyEq.png",outline2Vec,trVec,pp);
107 
108  PolyPacker<float>::PackAsAxisAlignedRect(outline2Vec,containerSize,trVec,finalSize);
109  Outline2Dumper::dumpOutline2VecPNG("testpolyAA.png",outline2Vec,trVec,pp);
110 
111  PolyPacker<float>::PackAsObjectOrientedRect(outline2Vec,containerSize,trVec,finalSize);
112  Outline2Dumper::dumpOutline2VecPNG("testpolyOO.png",outline2Vec,trVec,pp);
113 
114  RasterizedOutline2Packer<float, QtOutline2Rasterizer>::Parameters packingParam;
115 
116  packingParam.costFunction = RasterizedOutline2Packer<float, QtOutline2Rasterizer>::Parameters::LowestHorizon;
117  packingParam.doubleHorizon = true;
118  packingParam.cellSize = 4;
119  packingParam.rotationNum = 16; //number of rasterizations in 90°
120 
121  RasterizedOutline2Packer<float, QtOutline2Rasterizer>::Pack(outline2Vec,containerSize,trVec,packingParam);
122  Outline2Dumper::dumpOutline2VecPNG("testpolyRR.png",outline2Vec,trVec,pp);
123 
124 
125  return 0;
126 }