Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleTriangleMesh.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 /** @example ExampleTriangleMesh.cpp
26  * @relates ThreeDOut,TriangleMesh
27  @ingroup g_examples
28  @brief Example for TriangleMesh object, writes a wrl with an image using level of detail
29  @author MIP */
30 
31 #include <Utils/TriangleMesh.hh>
32 #include <Utils/ThreeDOut.hh>
33 #include <Base/Image/ImageIO.hh>
34 #include <Filter/Rescale.hh>
35 #include <Base/Common/FileHandling.hh>
36 #include <Image/Camera.hh>
37 #include <sstream>
38 
39 #include <iostream>
40 
41 using namespace std;
42 using namespace BIAS;
43 
44 //#define TILE_MODE
45 #define TILE_WIDTH 129
46 #define TILE_HEIGHT 129
47 
48 void usage(char* name)
49 {
50  cout<<"usage:\n";
51  cout<<name<<" <depthmap> <projection> <texture> [minCornerAngleDEG] [outputFile] [LevelOfDetail]\n";
52  cout<<endl<<endl;
53  cout<<"minCornerAngleDEG: discard triangle which contain a smaller angle"
54  <<endl;
55  cout<<"LevelOfDetail: 1:vertex for each pixel of full resolution, 2:"
56  <<" of half resolution"<<endl;
57 }
58 
59 int main(int argc, char* argv[])
60 {
61  bool TileMode = true;
62  if(argc<3) {
63  usage(argv[0]);
64  return -1;
65  }
66 
67  Camera<unsigned char> texture;
68  if(ImageIO::Load(argv[3], texture)!=0) {
69  cout<<"texture could not be loaded!\n";
70  usage(argv[0]);
71  return -1;
72  }
73  texture.ParseMetaData();
74  Camera<float> depthMap;
75  if(ImageIO::Load(argv[1], depthMap)!=0) {
76  cout<<"depth map could not be loaded!\n";
77  usage(argv[0]);
78  if (string(argv[1])==string("UNITSPHERE")) {
79  depthMap.Init(texture.GetWidth(), texture.GetHeight(), 1);
80  depthMap.FillImageWithConstValue(1.0f);
81  }
82  else return -1;
83  }
84  depthMap.ParseMetaData();
85 
86  Projection P;
87  if (P.Load(argv[2])!=0) {
88  if (texture.IsProjValid()) {
89  P = texture.GetProj();
90  } else if (depthMap.IsProjValid()) {
91  P = depthMap.GetProj();
92  } else {
93  cout<<"Projection could not be loaded from file or meta data!\n";
94  usage(argv[0]);
95  return -1;
96  }
97  }
98 
99  cout<<"meshing..."; cout.flush();
100  double minCornerAngle = 3.0*M_PI/180.0;
101  double maxViewingAngle = 91.0 * M_PI / 180.0;
102  if(argc>4) {
103  minCornerAngle = atof(argv[4])*M_PI/180.0;
104  }
105  cout<<"minCornerAngle = "<<minCornerAngle<<endl;
106  TriangleMesh mesh(minCornerAngle, maxViewingAngle);
107 
108  double LevelOfDetail = -1.0;
109  if(argc>6) {
110  LevelOfDetail = atof(argv[6]);
111  }
112  if (LevelOfDetail>=0.0) TileMode = false;
113  else TileMode = true;
114 
115  cout<<"Level of detail is "<< LevelOfDetail<<endl;
116  if (fabs(LevelOfDetail)!=1.0) {
117  //double maxlevel = log(double(depthMap.GetWidth()))/log(2.0)-6;
118  //cout<<"maximum level is "<<maxlevel<<endl;
119 
120  for (double curLevel = 1.0;
121  curLevel<fabs(LevelOfDetail); curLevel += 1.0) {
122  Gauss<float, float> DepthSmooth;
123  DepthSmooth.SetSigma(sqrt(1.0*1.0 - 0.5*0.5));
124  Image<float> copyOfDepth = depthMap;
125  DepthSmooth.Filter7x7GreyIgnoreBelowThreshold(depthMap, copyOfDepth,
126  0.0f);
127  depthMap.Release();
128  depthMap.Init(copyOfDepth.GetWidth()/2, copyOfDepth.GetHeight()/2);
129  for (unsigned int y=0; y<depthMap.GetHeight(); y++) {
130  for (unsigned int x=0; x<depthMap.GetWidth(); x++) {
131  depthMap.GetImageDataArray()[y][x] =
132  copyOfDepth.GetImageDataArray()[2*y][2*x];
133  }
134  }
135  P.Rescale(2.0);
136  //ImageIO::Save("depth"+FileHandling::toString(curLevel), depthMap);
137  ImageIO::Save("depth"+FileHandling::toString(curLevel), depthMap);
138  }
139 
140  }
141 
142  string vrmlfilename = "DenseTriangleMesh.wrl";
143  if(argc>5) vrmlfilename = argv[5];
144  ThreeDOut vrmlOut;
145  if (!TileMode) {
146 
147  mesh.GenerateSimplifiedMesh(depthMap, P, texture, 0, 0,
148  depthMap.GetWidth(),
149  depthMap.GetHeight(), 0.7, 3.0);
150  cout<<"finished\n"<<flush;
151 
152  vrmlOut.AddTriangleMesh(mesh, "mesh_from_"+string(argv[1]),
153  argv[3], true);
154  cout<<"Writing VRML to "<<vrmlfilename<<flush<<endl;
155  vrmlOut.VRMLOut(vrmlfilename);
156  cout<<"finished\n";
157  } else {
158 
159  // tile here into small image blocks, allows for writing of huge files
160  ofstream vrmlfile(vrmlfilename.c_str());
161  vrmlOut.VRMLOutWriteHeader(vrmlfile);
162 
163  unsigned int width, height;
164  P.GetParameters()->GetImageSize(width, height);
165  unsigned int tileWidth, tileHeight;
166  bool xFinished = false;
167  bool yFinished = false;
168  // bool finished = false;
169  unsigned int xStart, yStart;
170  unsigned int tileNum = 0;
171  yStart = 0;
172  int numTiles = (height/(TILE_HEIGHT-1)+1)*(width/(TILE_WIDTH-1)+1);
173  cout<<"Writing "<< numTiles
174  <<" tiles: "<<flush;
175  while(!yFinished) {
176  if(yStart+TILE_HEIGHT >= height) {
177  yFinished = true;
178  tileHeight = height-yStart;
179  }
180  else tileHeight = TILE_HEIGHT;
181 
182  cout<< double(tileNum)/double(numTiles)*100.0<<"% "<<flush;
183  xFinished = false;
184  xStart = 0;
185  while(!xFinished) {
186  stringstream ss;
187  ss<<"mesh_from_"<<string(argv[1])<<"_t"<<tileNum;
188  tileNum++;
189  if(xStart+TILE_WIDTH >= width) {
190  xFinished = true;
191  tileWidth = width -xStart;
192  }
193  else
194  tileWidth = TILE_WIDTH;
195 
196  if(mesh.GenerateDenseMesh(depthMap, P, texture,
197  xStart, yStart, tileWidth, tileHeight)==0) {
198  vrmlOut.AddTriangleMesh(mesh, ss.str(),
199  argv[3], false);
200  vrmlOut.VRMLOutIndexedFaceSets(vrmlfile);
201  vrmlOut.RemoveAll();
202  }
203  xStart+=(tileWidth-1);
204  }
205  yStart+=(tileHeight-1);
206  }
207  cout<< " finished."<<endl<<flush;
208  }
209 
210  return 0;
211 }
void Release()
reimplemented from ImageBase
Definition: Image.cpp:1579
unsigned int AddTriangleMesh(const TriangleMesh &mesh, const std::string &name="", const std::string &textureOutputName="", bool writeOutTexture=true, bool calcNormals=false)
Adds triangle mesh as IndexedFaceSet to ThreeDOut mem.
Definition: ThreeDOut.cpp:1104
int VRMLOut(const std::string &sFilename)
flush all 3d objects to a vrml file with name sFilename, this is the function most users would call ...
Definition: ThreeDOut.cpp:3670
virtual int Load(const std::string &filename)
convenience wrapper which tries to read different formats
Definition: Projection.cpp:62
int Filter7x7GreyIgnoreBelowThreshold(const Image< InputStorageType > &src, Image< OutputStorageType > &dst, const InputStorageType &thresh)
7x7 gauss filtering, values below threshold are ignored useful for depth map filtering ...
Definition: Gauss.cpp:1135
Unified output of 3D entities via OpenGL or VRML.
Definition: ThreeDOut.hh:349
void Rescale(float ratio, unsigned int cam=0)
adapt internal params to resampled image
Definition: Projection.hh:471
unsigned int GetWidth() const
Definition: ImageBase.hh:292
const ProjectionParametersBase * GetParameters(unsigned int cam=0) const
const parameter access function
Definition: Projection.hh:194
This class hides the underlying projection model, like projection matrix, spherical camera...
Definition: Projection.hh:70
Create and represent a 3D triangle mesh.
Definition: TriangleMesh.hh:84
unsigned int GetHeight() const
Definition: ImageBase.hh:299
void SetSigma(const double si)
Definition: Gauss.hh:162
int VRMLOutIndexedFaceSets(std::ostream &VRMLFile)
only write (previously stored) face sets into an open vrml file Will include texture into VRML as Pix...
Definition: ThreeDOut.cpp:3203
void FillImageWithConstValue(StorageType Value)
fill grey images
Definition: Image.cpp:456
virtual int GetImageSize(unsigned int &Width, unsigned int &Height) const
Obtain image dimensions.
void Init(unsigned int Width, unsigned int Height, unsigned int channels=1, enum EStorageType storageType=ST_unsignedchar, const bool interleaved=true)
calls Init from ImageBase storageType is ignored, just dummy argument
Definition: Image.cpp:421
void RemoveAll()
removes all entites Essentially a clear on each entityt container.
Definition: ThreeDOut.cpp:3855
bool IsProjValid() const
Definition: Camera.hh:221
const BIAS::Projection & GetProj() const
Definition: Camera.hh:109
int ParseMetaData(bool bUse2x64bitTS=true)
After ImageIO::Load() operated on AppData_, this method fills P_, Timestamp, DC_*, ...
Definition: Camera.cpp:154
int VRMLOutWriteHeader(std::ostream &vrml)
write the VRML 2.0 header into an open file
Definition: ThreeDOut.cpp:3696
const StorageType ** GetImageDataArray() const
overloaded GetImageDataArray() from ImageBase
Definition: Image.hh:152