Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleTriangleMeshQuad.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 ExampleTriangleMeshQuad.cpp
26  * @relates ThreeDOut,TriangleMesh
27  @ingroup g_examples
28  @brief Example for TriangleMesh object, writes a wrl with an image using textured quad
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 #include <Base/Math/Random.hh>
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> <texture> [projection] \n";
52  cout<<endl<<endl;
53 }
54 
55 int main(int argc, char* argv[])
56 {
57 
58  if(argc<3) {
59  usage(argv[0]);
60  return -1;
61  }
62 
63  Camera<unsigned char> texture;
64  if(ImageIO::Load(argv[2], texture)!=0) {
65  cout<<"texture could not be loaded!\n";
66  usage(argv[0]);
67  return -1;
68  }
69  texture.ParseMetaData();
70  Camera<float> depthMap;
71  if(ImageIO::Load(argv[1], depthMap)!=0) {
72  cout<<"depth map could not be loaded!\n";
73  usage(argv[0]);
74  if (string(argv[1])==string("UNITSPHERE")) {
75  depthMap.Init(texture.GetWidth(), texture.GetHeight(), 1);
76  depthMap.FillImageWithConstValue(1.0f);
77  }
78  else return -1;
79  }
80  depthMap.ParseMetaData();
81 
82  Projection P;
83  bool loaded = false;
84  if (argc>3) if (P.Load(argv[2])==0) loaded = true;
85  if (!loaded) {
86  if (texture.IsProjValid()) {
87  P = texture.GetProj();
88  } else if (depthMap.IsProjValid()) {
89  P = depthMap.GetProj();
90  } else {
91  cout<<"Projection could not be loaded from file or meta data!\n";
92  usage(argv[0]);
93  return -1;
94  }
95  }
96 
97 
98  cout<<"meshing..."; cout.flush();
99  double minCornerAngle = 3.0*M_PI/180.0;
100  double maxViewingAngle = 91.0 * M_PI / 180.0;
101  if(argc>4) {
102  minCornerAngle = atof(argv[4])*M_PI/180.0;
103  }
104  cout<<"minCornerAngle = "<<minCornerAngle<<endl;
105  TriangleMesh mesh(minCornerAngle, maxViewingAngle);
106 
107 
108 
110  dynamic_cast<ProjectionParametersPerspective *>(P.GetParameters());
111  ThreeDOut vrmlOut;
112 
113  Random R;
114 
115  double hws = 20;
116  double hws2 = hws + 2;
117  for (unsigned int q=0; q<30; q++) {
119  texture.GetWidth()-hws2),
120  R.GetUniformDistributed(hws2,
121  texture.GetHeight()-hws2));
122 
123  HomgPoint2D p[4];
124  Vector3<double> X[4];
125  for (unsigned int i=0; i<4; i++) {
126  p[i] = p1;
127  switch (i) {
128  case 0: p[i][0] -= hws; p[i][1] -= hws; break;
129  case 1: p[i][0] += hws; p[i][1] -= hws; break;
130  case 2: p[i][0] -= hws; p[i][1] += hws; break;
131  case 3: p[i][0] += hws; p[i][1] += hws; break;
132  }
133  X[i] =
134  P.UnProjectToPoint(p[i],
135  depthMap.BilinearInterpolation(p[i][0],p[i][1]));
136  }
137 
138  PMatrix Pmat = ppp->GetP();
139 
140  mesh.GenerateTexturedQuad(Pmat, texture, X[0], X[1], X[2], X[3]);
141  vrmlOut.AddTriangleMesh(mesh,string("myquad"), argv[2], false);
142  }
143  string vrmlfilename = "Quad.wrl";
144  cout<<"Writing VRML to "<<vrmlfilename<<flush<<endl;
145 
146  vrmlOut.VRMLOut(vrmlfilename);
147  cout<<"finished\n";
148 
149  return 0;
150 }
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
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
double BilinearInterpolation(const double x, const double y, const unsigned short int channel=0) const
Generic bilinear interpolation.
Definition: Image.cpp:1341
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
camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
Unified output of 3D entities via OpenGL or VRML.
Definition: ThreeDOut.hh:349
double GetUniformDistributed(const double min, const double max)
on succesive calls return uniform distributed random variable between min and max ...
Definition: Random.hh:84
unsigned int GetWidth() const
Definition: ImageBase.hh:312
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:319
void FillImageWithConstValue(StorageType Value)
fill grey images
Definition: Image.cpp:456
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
Vector3< double > UnProjectToPoint(const HomgPoint2D &pos, double depth, unsigned int cam=0, bool IgnoreDistortion=false) const
calculates a 3D point in the global (not the rig) coordinate system, which belongs to the image posit...
Definition: Projection.cpp:349
bool IsProjValid() const
Definition: Camera.hh:221
describes a projective 3D -&gt; 2D mapping in homogenous coordinates
Definition: PMatrix.hh:88
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
class for producing random numbers from different distributions
Definition: Random.hh:51