Viewpoint.cpp

Go to the documentation of this file.
00001 
00012 #include "../common/standard.h"
00013 #include <windows.h>
00014 #include <GL/glu.h>
00015 #include <fstream.h>
00016 #include <string>
00017 using std::string;
00018 
00019 #include "XmlNode.h"
00020 #include "Viewpoint.h"
00021 #include "StvFile.h"
00022 #include "GLutils.h"
00023 
00024 
00025 void Viewpoint::loadFromFiles (const char *photo_filename, const char *silhouette_filename /* can be NULL */)
00026 {
00027     photo.loadFile (photo_filename);
00028     assert (photo.getWidth() && photo.getHeight());
00029     if (silhouette_filename)
00030     {
00031         silhouette.loadFile (silhouette_filename);
00032         bHasSilhouette = true;
00033     }
00034     else
00035         bHasSilhouette = false;
00036     
00037 }
00038 
00039 
00040 
00041 
00042 
00043 void Viewpoint::setExtrinsics (const ColVector3 up_vector, const Vertex center_of_projection, const Vertex lookat_point) 
00044 {
00045     upVector = up_vector;
00046     COP = center_of_projection;
00047     lookatPoint = lookat_point;
00048 
00049     ModelviewMatrix *m = (ModelviewMatrix*)&modelview;
00050     m->setToIdentity();
00051     m->lookAt (COP, lookatPoint, upVector);
00052 }
00053 
00054 void Viewpoint::setIntrinsics (double vert_fov, double width_over_height, double near_plane, double far_plane) 
00055 {
00056     verticalFOV     = vert_fov;
00057     widthOverHeight = width_over_height;
00058     nearPlane       = near_plane;
00059     farPlane        = far_plane;
00060 
00061     ProjectionMatrix *m = (ProjectionMatrix*)&projection;
00062     m->setToIdentity();
00063     m->setPerspective (
00064         verticalFOV,
00065         widthOverHeight,
00066         nearPlane,
00067         farPlane);
00068 }
00069 
00070 double* Viewpoint::getModelviewMatrix () const
00071 {
00072     ModelviewMatrix *m = (ModelviewMatrix*)&modelview;
00073     return (&m->operator[](0));
00074 }
00075 
00076 
00077 double* Viewpoint::getProjectionMatrix() const
00078 {
00079     ProjectionMatrix *m = (ProjectionMatrix*)&projection;
00080     return (&m->operator[](0));
00081 }
00082 
00083 
00084 
00085 
00086 
00091 void ViewpointList::loadSettingsFromXml(const char *filename)
00092 {
00093     XmlNode d;
00094     d.loadFile(filename);
00095     
00096 /*
00097  * Load info for each image that we have
00098  */
00099     
00100     XmlNodeList nodes = d.getNodes("scanner/img");
00101     
00102     if (nodes.getCount() < 1)
00103         throw RuntimeError ("Settings::loadFromFile(): please specify an img tag.");
00104     
00105     char *fname = strrchr (filename, '\\');
00106     if (!fname) 
00107         throw LogicError("ViewpointList::loadSettingsFromXml(): The filename must include a fully-qualified path");
00108 
00109     char path[512];
00110     memset(path, 0, sizeof(path));
00111     strncpy(path, filename, MIN(fname - filename, sizeof(path)-1));
00112 
00113     if (!SetCurrentDirectory (path))
00114         throw LogicError("ViewpointList::loadSettingsFromXml(): Can't set directory to %s", path);
00115 
00116     for (int i = 0; i < nodes.getCount(); i++)
00117     {
00118         XmlNode n = nodes.getNode(i);
00119         
00120         Viewpoint img;
00121         
00122     /* 
00123      * Load extrinsics
00124      */ 
00125         Vertex COP, lookatPoint;
00126         n.getAttrib (COP[0], "center_of_projection/@x");
00127         n.getAttrib (COP[1], "center_of_projection/@y");
00128         n.getAttrib (COP[2], "center_of_projection/@z");
00129         
00130         n.getAttrib (lookatPoint[0], "lookat_point/@x");
00131         n.getAttrib (lookatPoint[1], "lookat_point/@y");
00132         n.getAttrib (lookatPoint[2], "lookat_point/@z");
00133         
00134         ColVector3 upVector;
00135 
00136         // the up vector is optional, so if we can't get it,
00137         // we'll just set it to the y-axis.
00138         try 
00139         {
00140             n.getAttrib (upVector[0], "up_vector/@x");
00141             n.getAttrib (upVector[1], "up_vector/@y");
00142             n.getAttrib (upVector[2], "up_vector/@z");
00143         }
00144         catch (...)
00145         {
00146             upVector = ColVector3(0,1,0);
00147         }
00148         upVector.normalize();
00149         
00150         // finally, give our extrinsics to the image.
00151         img.setExtrinsics (upVector, COP, lookatPoint);
00152         
00153     /*
00154      * Load our camera intrinsic info
00155      */
00156         double nearPlane, verticalFOV, widthOverHeight, farPlane;
00157         n.getAttrib (nearPlane,       "near_plane/@value");
00158         n.getAttrib (verticalFOV,     "vertical_fov/@value");
00159         n.getAttrib (widthOverHeight, "width_over_height/@value");
00160         
00161         
00162         ColVector3 diff = lookatPoint - COP;
00163         diff = diff * 2;
00164         farPlane = diff.getLength();
00165         
00166         img.setIntrinsics (verticalFOV, widthOverHeight, nearPlane, farPlane);
00167         
00168         push_back(img);
00169         
00170         string filename = fmtString ("%s", n.getAttrib("@src").c_str());
00171         Viewpoint &lastImage = this->at(this->size()-1);
00172 
00173         try 
00174         {
00175             std::string s = n.getAttrib("silhouette/@value");
00176             char silhouette_filename[512];
00177             sprintf(silhouette_filename, "%s", s.c_str());
00178             lastImage.loadFromFiles(filename.c_str(), silhouette_filename);
00179         }
00180         catch (...)
00181         {   
00182             lastImage.loadFromFiles(filename.c_str(), NULL);
00183         }
00184     }
00185 }
00186 
00187 
00188 void ViewpointList::loadFromFile (const char *filename)
00189 {
00190     char *ext = strrchr (filename, '.');
00191 
00192     if (!ext)
00193         throw ArgError ("loadSettings(): Your file must have an .xml or a .col extension.");
00194 
00195     clear();
00196     if (0 == _stricmp(ext, ".scn"))
00197         loadSettingsFromXml (filename);
00198     else if (0 == _stricmp(ext, ".col"))
00199         loadSettingsFromCol (filename);
00200     else 
00201         throw ArgError ("loadSettings(): Your file must have an .xml or a .col extension.");
00202 }
00203 
00204 
00205 static void removeSurroundingSpaces (string & s)
00206 {
00207     // erase trailing spaces
00208     while (s.size() && isspace(s[s.size()-1]))
00209         s.resize (s.size() - 1);
00210 
00211     // erase leading spaces
00212     while (s.size() && isspace(s[0]))
00213         s.erase (0,1);
00214 }
00215 
00220 void ViewpointList::loadSettingsFromCol (const char *filename)
00221 {
00222     ifstream colFile (filename);
00223 
00224     if (colFile.fail())
00225         throw RuntimeError ("Unable to open file: %s", filename);
00226 
00227     // get number of .stv files in this .col file.
00228     int numStvFiles;
00229     colFile >> numStvFiles;
00230     char stvFilename[512];
00231     colFile.eatwhite(); // go to next line
00232 
00233     // for each stv file, read in its data. One CalibratedImage is added
00234     // to our list for each stv file.
00235     for (int i = 0; i < numStvFiles; i++)
00236     {
00237         if (!colFile.getline(stvFilename, sizeof(stvFilename)))
00238             throw RuntimeError("Didn't find entry %d/%d in %s.", i, numStvFiles, filename);
00239 
00240         std::string fname_clean = stvFilename;
00241         removeSurroundingSpaces (fname_clean);
00242 
00243         Viewpoint img;
00244         StvFile stvData (fname_clean.c_str(), img);
00245 
00246         double widthOverHeight = stvData.calculateWidthOverHeight();
00247         double verticalFOV     = stvData.calculateVerticalFOV();
00248         ColVector3 up_vector   = stvData.calculateUpVector();
00249         Vertex lookat_point    = stvData.calculateLookatPoint();
00250 
00251         ColVector3 look_vector = (lookat_point - stvData.getCenterOfProjection());
00252         double dist_to_lookat_point = look_vector.getLength();
00253 
00254         // set the intrinsics and extrinsics for this image, and add it to the list
00255         img.setIntrinsics(verticalFOV, widthOverHeight, dist_to_lookat_point/20., dist_to_lookat_point * 2);
00256         img.setExtrinsics(up_vector, stvData.getCenterOfProjection(), lookat_point);
00257 
00258         push_back (img);
00259     }
00260 }

Generated on Tue May 21 03:34:51 2002 for Archimedes by doxygen1.2.15