Hugintrunk  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pano_trafo.cpp
Go to the documentation of this file.
1 // -*- c-basic-offset: 4 -*-
2 
13 /* This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public
15  * License as published by the Free Software Foundation; either
16  * version 2 of the License, or (at your option) any later version.
17  *
18  * This software is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  * General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public
24  * License along with this software. If not, see
25  * <http://www.gnu.org/licenses/>.
26  *
27  * KFJ 2010-12-28 I've thrown out C fprintf style IO, now
28  * it's all done with streams, removing the problem I had with
29  * unflushed output.
30  * I've also added a new mode where no image number is passed on
31  * the command line and instead the image number is passed with
32  * each coordinate pair.
33  *
34  */
35 
36 #include <fstream>
37 #include <sstream>
38 #include <getopt.h>
39 
40 #include <panodata/Panorama.h>
42 
43 static void usage(const char* name)
44 {
45  std::cout << name << ": transform pixel coordinates" << std::endl
46  << "pano_trafo version " << hugin_utils::GetHuginVersion() << std::endl
47  << std::endl
48  << "Usage: " << name << " input.pto [ image_nr ]" << std::endl
49  << std::endl
50  << "pano_trafo reads pixel coordinates from standard input and" << std::endl
51  << "prints the transformed coordinates to standard output." << std::endl
52  << "If you pass an image number on the command line," << std::endl
53  << "it reads pairs of coordinates and transforms them." << std::endl
54  << "If you don't pass an image number, it will read triplets" << std::endl
55  << "of the form <image number> <x coordinate> <y coordinate>" << std::endl
56  << "and output the transformed coordinates." << std::endl
57  << std::endl
58  << " -r|--recursive Transform from panorama to image coordinates" << std::endl
59  << " -h|--help Shows help" << std::endl
60  << std::endl;
61 }
62 
63 // alternative behaviour if no image number is passed.
64 // main() with the original behaviour follows below.
65 
66 void work_on_triplets(const HuginBase::Panorama& pano, bool reverse)
67 {
68  // pano tools interface
69  int images = pano.getNrOfImages() ;
70  int image = 0 ;
71 
72  // instead of just one transform, create one for each image
73 
74  HuginBase::PTools::Transform* trafo_set =
75  new HuginBase::PTools::Transform [ images ] ;
76 
77  if ( ! trafo_set )
78  {
79  std::cerr << "not enough memory" ; // very unlikely...
80  exit ( -1 ) ;
81  }
82 
83  for ( image = 0 ; image < images ; image++ )
84  {
85  if (reverse)
86  {
87  trafo_set[image].createTransform(pano.getSrcImage(image), pano.getOptions());
88  }
89  else
90  {
91  trafo_set[image].createInvTransform(pano.getSrcImage(image), pano.getOptions());
92  }
93  }
94 
95  // now we can process data triplets from cin
96 
97  double xin , yin , xout , yout ;
98 
99  while ( std::cin >> image >> xin >> yin )
100  {
101  if ( image < 0 || image >= images )
102  {
103  std::cerr << "no image " << image << " in pano" << std::endl ;
104  exit ( 1 ) ; // we don't want an index out of range
105  }
106  trafo_set[image].transformImgCoord(xout, yout, xin, yin);
107  std::cout << xout << " " << yout << std::endl ;
108  }
109  delete [] trafo_set;
110 }
111 
112 int main(int argc, char* argv[])
113 {
114  // parse arguments
115  const char* optstring = "hr";
116  static struct option longOptions[] =
117  {
118  { "recursive", no_argument, NULL, 'r' },
119  { "help", no_argument, NULL, 'h' },
120  0
121  };
122  int c;
123 
124  bool reverse = false;
125  while ((c = getopt_long(argc, argv, optstring, longOptions, nullptr)) != -1)
126  {
127  switch (c)
128  {
129  case 'h':
130  usage(hugin_utils::stripPath(argv[0]).c_str());
131  return 0;
132  case 'r':
133  reverse = true;
134  break;
135  case ':':
136  case '?':
137  // missing argument or invalid switch
138  return 1;
139  break;
140  default:
141  // this should not happen
142  abort();
143  }
144  }
145 
146  if (argc - optind < 1 || argc - optind > 2)
147  {
148  std::cerr << hugin_utils::stripPath(argv[0]) << ": Expected one project file and optional one image number" << std::endl;
149  return 1;
150  }
151 
152  std::string input=argv[optind];
153 
154  HuginBase::Panorama pano;
155  if (!pano.ReadPTOFile(input, hugin_utils::getPathPrefix(input)))
156  {
157  return 1;
158  };
159 
160  // set up output format
161  std::cout.setf ( std::ios::fixed ) ;
162  std::cout.precision ( 6 ) ; // should be ample
163 
164  if ( argc - optind == 1 )
165  {
166  // no image number was passed. This triggers the new
167  // behaviour to accept triplets on cin
168  work_on_triplets ( pano , reverse ) ;
169  return 0;
170  }
171 
172  // an image number was passed, so proceed
173  // as in the original version
174 
175  int imageNumber = atoi(argv[optind+1]);
176  if (imageNumber >= pano.getNrOfImages())
177  {
178  std::cerr << "Not enough images in panorama" << std::endl;
179  return 1;
180  }
181 
182  // pano tools interface
184  if (reverse)
185  {
186  trafo.createTransform(pano.getSrcImage(imageNumber), pano.getOptions());
187  }
188  else
189  {
190  trafo.createInvTransform(pano.getSrcImage(imageNumber), pano.getOptions());
191  }
192 
193  double xin , yin , xout , yout ;
194 
195  // here's where the old-style IO was, now it's all streams.
196  // It's also format-free input, so newlines don't matter
197  while ( std::cin >> xin >> yin )
198  {
199  trafo.transformImgCoord(xout, yout, xin, yin);
200  std::cout << xout << " " << yout << std::endl ;
201  }
202 }
SrcPanoImage getSrcImage(unsigned imgNr) const
get a description of a source image
Definition: Panorama.cpp:1620
void work_on_triplets(const HuginBase::Panorama &pano, bool reverse)
Definition: pano_trafo.cpp:66
Model for a panorama.
Definition: Panorama.h:152
std::string getPathPrefix(const std::string &filename)
Get the path to a filename.
Definition: utils.cpp:184
std::size_t getNrOfImages() const
number of images.
Definition: Panorama.h:205
void createInvTransform(const vigra::Diff2D &srcSize, VariableMap srcVars, Lens::LensProjectionFormat srcProj, const vigra::Diff2D &destSize, PanoramaOptions::ProjectionFormat destProj, const std::vector< double > &destProjParam, double destHFOV, const vigra::Diff2D &origSrcSize)
create image-&gt;pano transformation
bool ReadPTOFile(const std::string &filename, const std::string &prefix="")
read pto file from the given filename into Panorama object it does some checks on the file and issues...
Definition: Panorama.cpp:2023
bool transformImgCoord(double &x_dest, double &y_dest, double x_src, double y_src) const
like transform, but return image coordinates, not cartesian coordinates
const PanoramaOptions & getOptions() const
returns the options for this panorama
Definition: Panorama.h:481
static void usage()
Definition: Main.cpp:32
Holds transformations for Image -&gt; Pano and the other way.
std::string GetHuginVersion()
return a string with version numbers
Definition: utils.cpp:907
void createTransform(const vigra::Diff2D &srcSize, VariableMap srcVars, Lens::LensProjectionFormat srcProj, const vigra::Diff2D &destSize, PanoramaOptions::ProjectionFormat destProj, const std::vector< double > &destProjParam, double destHFOV, const vigra::Diff2D &origSrcSize)
initialize pano-&gt;image transformation
std::string stripPath(const std::string &filename)
remove the path of a filename (mainly useful for gui display of filenames)
Definition: utils.cpp:160
int main(int argc, char *argv[])
Definition: Main.cpp:167