00001
00011 #include "StvFile.h"
00012 #include <fstream.h>
00013
00014 static std::vector< double > getNumbersInLine (char *line)
00015 {
00016 std::vector< double > rtn;
00017 double curNumber;
00018 char separators[] = " ][\t";
00019 char *token = strtok (line, separators);
00020 while (token != NULL)
00021 {
00022 if (1 == sscanf (token, "%lf", &curNumber))
00023 rtn.push_back (curNumber);
00024 token = strtok (NULL, separators);
00025 }
00026
00027 return rtn;
00028 }
00029
00030 static void eraseFirstToken (std::string &s)
00031 {
00032
00033 while (s.size() && isspace(s[0]))
00034 s.erase (0,1);
00035
00036 while (s.size() && !isspace(s[0]))
00037 s.erase (0,1);
00038
00039 while (s.size() && isspace(s[0]))
00040 s.erase (0,1);
00041 }
00042
00043 static void removeSurroundingSpaces (std::string & s)
00044 {
00045
00046 while (s.size() && isspace(s[s.size()-1]))
00047 s.resize (s.size() - 1);
00048
00049
00050 while (s.size() && isspace(s[0]))
00051 s.erase (0,1);
00052 }
00053
00054 StvFile::StvFile (const char *filename, Viewpoint &photo) : img(photo)
00055 {
00056 ifstream stvFile (filename);
00057 if (stvFile.fail())
00058 throw RuntimeError ("Unable to open file: %s", filename);
00059
00060
00061
00062
00063 std::vector< double > center_of_projection_numbers;
00064 std::vector< double > projection_matrix_numbers;
00065
00066 char stvLine[512];
00067 while (stvFile.getline (stvLine, sizeof(stvLine)))
00068 {
00069 if (stvLine == strstr(stvLine, "center_of_projection"))
00070 center_of_projection_numbers = getNumbersInLine (stvLine);
00071 else if (stvLine == strstr(stvLine, "projection_matrix"))
00072 projection_matrix_numbers = getNumbersInLine (stvLine);
00073 else if (stvLine == strstr(stvLine, "silhouette"))
00074 silhouette_filename = stvLine;
00075 else if (stvLine == strstr(stvLine, "texture"))
00076 photo_filename = stvLine;
00077 }
00078
00079
00080
00081
00082 eraseFirstToken (silhouette_filename);
00083 eraseFirstToken (photo_filename);
00084 removeSurroundingSpaces (silhouette_filename);
00085 removeSurroundingSpaces (photo_filename);
00086
00087 bool bDisableSilhouettes = false;
00088 if (!silhouette_filename.size())
00089 bDisableSilhouettes = true;
00090
00091 img.loadFromFiles (photo_filename.c_str(), !bDisableSilhouettes?silhouette_filename.c_str():NULL);
00092
00093 if (center_of_projection_numbers.size() != 3)
00094 throw RuntimeError ("Bad/missing center_of_projection in %s", filename);
00095 if (projection_matrix_numbers.size() != 9)
00096 throw RuntimeError ("Bad/missing projection matrix in %s", filename);
00097 if (photo_filename.size() < 1)
00098 throw RuntimeError ("Bad/missing texture filename in %s.", filename);
00099
00100 Matrix<3, 3, double> projMatrix;
00101 memcpy (&projMatrix[0], &projection_matrix_numbers[0], sizeof(double)*9);
00102 projMatrix = projMatrix.getTranspose();
00103
00104
00105
00106
00107
00108 memcpy (&du[0], &projMatrix(0,0), sizeof(double)*3);
00109 memcpy (&dv[0], &projMatrix(1,0), sizeof(double)*3);
00110 memcpy (&cop_to_origin_vector[0], &projMatrix(2,0), sizeof(double)*3);
00111
00112
00113 memcpy (¢er_of_projection[0], ¢er_of_projection_numbers[0], sizeof(double)*3);
00114
00115 origin_of_projection = center_of_projection + cop_to_origin_vector;
00116 }
00117
00118 void StvFile::print() const
00119 {
00120 MTRACE ("******** StvData %s Printing **********", photo_filename.c_str());
00121 center_of_projection.print("center_of_projection");
00122 du.print("du");
00123 dv.print("dv");
00124 cop_to_origin_vector.print("cop_to_origin_vector");
00125 }
00126
00127 double StvFile::calculateWidthOverHeight() const
00128 {
00129
00130 double imageWidth = img.getWidth() * du.getLength();
00131 double imageHeight = img.getHeight() * dv.getLength();
00132 return (double)imageWidth / imageHeight;
00133 }
00134
00135
00144 Vertex StvFile::calculateLookatPoint() const
00145 {
00146 Vertex projection_plane_center;
00147 ColVector3 cop_to_center, cop_to_worldspace_origin;
00148 double dist_to_worldspace_origin;
00149
00150 projection_plane_center =
00151 origin_of_projection +
00152 du * (double)img.getWidth()/2 +
00153 dv * (double)img.getHeight()/2;
00154
00155 cop_to_worldspace_origin = Vertex(0,0,0) - center_of_projection;
00156 dist_to_worldspace_origin = cop_to_worldspace_origin.getLength();
00157
00158 cop_to_center = projection_plane_center - center_of_projection;
00159 cop_to_center.normalize();
00160 cop_to_center = cop_to_center * dist_to_worldspace_origin;
00161
00162 return (center_of_projection + cop_to_center);
00163 }
00164
00165 ColVector3 StvFile::calculateUpVector() const
00166 {
00167 ColVector3 upVector = dv * -1.;
00168 upVector.normalize();
00169 return upVector;
00170 }
00171
00172 double StvFile::calculateVerticalFOV() const
00173 {
00174 Vertex middle_top =
00175 origin_of_projection +
00176 (du * (double)img.getWidth() / 2.);
00177
00178 Vertex middle_bottom =
00179 middle_top +
00180 (dv * (double)img.getHeight());
00181
00182 ColVector3 cop_to_top = middle_top - center_of_projection;
00183 ColVector3 cop_to_bottom = middle_bottom - center_of_projection;
00184 cop_to_top.normalize();
00185 cop_to_bottom.normalize();
00186
00187 double cos_vertical_fov = cop_to_top.dotProduct(cop_to_bottom);
00188
00189 return (180. * acos(cos_vertical_fov) / M_PI);
00190 }
00191
00192 Vertex StvFile::getCenterOfProjection () const
00193 { return center_of_projection; }
00194