Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

Diff of /src/tools/pano_trafo.cpp [da8710] .. [280785] Maximize Restore

  Switch to side-by-side view

--- a/src/tools/pano_trafo.cpp
+++ b/src/tools/pano_trafo.cpp
@@ -4,7 +4,7 @@
  *
  *  @brief Transform between image <-> panorama coordinates
  *
- *  @author Pablo d'Angelo
+ *  @author Pablo d'Angelo, Kay F. Jahnke
  *
  *  $Id: cpclean.cpp 4822 2009-12-19 23:17:06Z brunopostle $
  *
@@ -24,6 +24,13 @@
  *  License along with this software; if not, write to the Free Software
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
+ *  KFJ 2010-12-28 I've thrown out C fprintf style IO, now
+ *  it's all done with streams, removing the problem I had with
+ *  unflushed output.
+ *  I've also added a new mode where no image number is passed on
+ *  the command line and instead the image number is passed with
+ *  each coordinate pair.
+ * 
  */
 
 #include <hugin_version.h>
@@ -48,13 +55,69 @@
     cout << name << ": transform pixel coordinates" << endl
          << "pano_trafo version " << DISPLAY_VERSION << endl
          << endl
-         << "Usage:  " << name << " input.pto image_nr" << endl
+         << "Usage:  " << name << " input.pto [ image_nr ]" << endl
          << endl
-         << "pano_trafo reads pixel coordinates from standard input and prints" << endl
-	 << "the transformed coordinates to standard out." << endl
+         << "pano_trafo reads pixel coordinates from standard input and" << endl
+	 << "prints the transformed coordinates to standard output." << endl
+	 << "If you pass an image number on the command line," << endl
+	 << "it reads pairs of coordinates and transforms them." << endl
+	 << "If you don't pass an image number, it will read triplets" << endl
+	 << "of the form <image number> <x coordinate> <y coordinate>" << endl
+	 << "and output the transformed coordinates." << endl
+	 << endl
          << "     -r       Transform from panorama to image coordinates" << endl
          << "     -h       shows help" << endl
          << endl;
+}
+
+// alternative behaviour if no image number is passed.
+// main() with the original behaviour follows below.
+
+int work_on_triplets ( Panorama pano , bool reverse )
+{
+    // pano tools interface
+    int images = pano.getNrOfImages() ;
+    int image = 0 ;
+
+    // instead of just one transform, create one for each image
+    
+    HuginBase::PTools::Transform * trafo_set =
+        new HuginBase::PTools::Transform [ images ] ;
+
+    if ( ! trafo_set )
+      {
+        cerr << "not enough memory" ; // very unlikely...
+        exit ( -1 ) ;
+      }
+    
+    for ( image = 0 ; image < images ; image++ )
+      {
+        if (reverse)
+          {
+	    trafo_set[image].createTransform (
+	      pano.getSrcImage(image), pano.getOptions() );
+          } 
+        else
+          {
+	    trafo_set[image].createInvTransform (
+	      pano.getSrcImage(image), pano.getOptions() );
+	  }
+      }
+
+    // now we can process data triplets from cin
+    
+    double xin , yin , xout , yout ;
+
+while ( cin >> image >> xin >> yin )
+      {
+        if ( image < 0 || image >= images )
+	  {
+	    cerr << "no image " << image << " in pano" << endl ;
+	    exit ( 1 ) ; // we don't want an index out of range
+	  }
+	trafo_set[image].transformImgCoord(xout, yout, xin, yin);
+	cout << xout << " " << yout << endl ;
+      }
 }
 
 int main(int argc, char *argv[])
@@ -80,14 +143,13 @@
         }
     }
 
-    if (argc - optind != 2) 
-    {
-        usage(argv[0]);
-        return 1;
-    };
-    
+    if (argc - optind < 1 || argc - optind > 2) 
+      {
+         usage(argv[0]);
+         return 1;
+      }
+
     string input=argv[optind];
-    int imageNumber = atoi(argv[optind+1]);
 
     Panorama pano;
     ifstream prjfile(input.c_str());
@@ -103,6 +165,21 @@
         return 1;
     }
 
+    // set up output format
+    cout.setf ( ios::fixed ) ;
+    cout.precision ( 6 ) ; // should be ample
+
+    if ( argc - optind == 1 )
+      {
+	// no image number was passed. This triggers the new
+	// behaviour to accept triplets on cin
+	return work_on_triplets ( pano , reverse ) ;
+      }
+
+    // an image number was passed, so proceed
+    // as in the original version
+    
+    int imageNumber = atoi(argv[optind+1]);
     if (imageNumber >= pano.getNrOfImages()) {
 	cerr << "Not enough images in panorama" << endl;
 	return 1;
@@ -116,9 +193,13 @@
 	trafo.createInvTransform(pano.getSrcImage(imageNumber), pano.getOptions());
     }
 
-    double xin,yin,xout,yout;
-    while( fscanf(stdin, "%lf %lf", &xin, &yin) == 2 ) {
+    double xin , yin , xout , yout ;
+
+    // here's where the old-style IO was, now it's all streams.
+    // It's also format-free input, so newlines don't matter
+    while ( cin >> xin >> yin )
+      {
 	trafo.transformImgCoord(xout, yout, xin, yin);
-	fprintf(stdout,"%f %f\n",xout, yout);
-    }
+	cout << xout << " " << yout << endl ;
+      }
 }