1 /****************************************************************************
2 * MeshLab                                                           o o     *
3 * An extendible mesh processor                                    o     o   *
4 *                                                                _   O  _   *
5 * Copyright(C) 2005, 2009                                          \/)\/    *
6 * Visual Computing Lab                                            /\/|      *
7 * ISTI - Italian National Research Council                           |      *
8 *                                                                    \      *
9 * All rights reserved.                                                      *
10 *                                                                           *
11 * This program is free software; you can redistribute it and/or modify      *
12 * it under the terms of the GNU General Public License as published by      *
13 * the Free Software Foundation; either version 2 of the License, or         *
14 * (at your option) any later version.                                       *
15 *                                                                           *
16 * This program is distributed in the hope that it will be useful,           *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
20 * for more details.                                                         *
21 *                                                                           *
22 ****************************************************************************/
23 
24 #ifndef __OM_ALNPARSER_
25 #define __OM_ALNPARSER_
26 
27 #include <vector>
28 #include <string>
29 #include <vcg/math/matrix44.h>
30 
31 struct RangeMap
32 {
RangeMapRangeMap33     RangeMap()
34     {
35         quality = 1.0f;
36     }
37 
38     std::string			filename;
39     Matrix44m	trasformation;
40     float						quality;
41 };
42 
43 class ALNParser
44 {
45 public:
46     enum ALNParserCodes {NoError, CantOpen, UnexpectedEOF, ExpectingComment};
47 
ErrorMsg(int message_code)48     static const char* ErrorMsg(int message_code)
49     {
50         static const char* error_msg[4] = {"No errors", "Can't open file", "Premature End of file", "I was expecting a comment"};
51 
52         if(message_code>3 || message_code<0)
53             return "Unknown error";
54         else
55             return error_msg[message_code];
56     };
57 
BuildALN(std::vector<RangeMap> & rangemaps,std::vector<std::string> & files)58     static int BuildALN(std::vector<RangeMap> &rangemaps, std::vector< std::string > &files)
59     {
60         rangemaps.clear();
61         rangemaps.resize( files.size() );
62         std::vector< RangeMap >::iterator		 rm  = rangemaps.begin();
63         std::vector< std::string >::iterator it  = files.begin();
64         std::vector< std::string >::iterator end = files.end();
65         for ( ; it!=end; it++, rm++)
66         {
67             (*rm).filename = (*it);
68             (*rm).quality  = 1.0f;
69             (*rm).trasformation.SetIdentity();
70         }
71         files.clear();
72         return NoError;
73     };
74 
ParseALN(std::vector<RangeMap> & rangemaps,const char * ALNname)75     static int ParseALN(std::vector<RangeMap> &rangemaps, const char *ALNname)
76     {
77         rangemaps.clear();
78 
79         FILE *stream=fopen(ALNname, "rt");
80         if(stream==NULL)
81             return CantOpen;
82 
83         int mesh_number;
84         fscanf(stream, "%i\n", &mesh_number);
85 
86         char buffer[1024];
87         for (int m=0; m<mesh_number; m++)
88         {
89             RangeMap rm;
90 
91             fgets(buffer, 1024, stream);
92             *strchr(buffer, '\n')=0;
93             if(strchr(buffer,'\r')) 	*strchr(buffer,'\r')=0;
94             rm.filename = buffer;
95 
96             fgets(buffer, 1024, stream);
97             if(buffer[0]!='#')
98                 return ExpectingComment;
99 
100             *strchr(buffer,'\n')=0;
101             if(strchr(buffer,'\r')) 	*strchr(buffer,'\r')=0;
102 
103 
104             char *occurrence = strchr(buffer, 'W');
105             if(occurrence!=NULL && occurrence[1]==':')
106                 rm.quality = (float) atof(occurrence+2);
107             assert(rm.quality>0);
108 
109             fscanf(stream,"%f %f %f %f \n",&(rm.trasformation[0][0]),&(rm.trasformation[0][1]),&(rm.trasformation[0][2]),&(rm.trasformation[0][3]));
110             fscanf(stream,"%f %f %f %f \n",&(rm.trasformation[1][0]),&(rm.trasformation[1][1]),&(rm.trasformation[1][2]),&(rm.trasformation[1][3]));
111             fscanf(stream,"%f %f %f %f \n",&(rm.trasformation[2][0]),&(rm.trasformation[2][1]),&(rm.trasformation[2][2]),&(rm.trasformation[2][3]));
112             fscanf(stream,"%f %f %f %f \n",&(rm.trasformation[3][0]),&(rm.trasformation[3][1]),&(rm.trasformation[3][2]),&(rm.trasformation[3][3]));
113 
114             rangemaps.push_back(rm);
115         }
116     fclose(stream);
117         return NoError;
118     } // end of ParseALN
119 
SaveALN(const char * alnfile,std::vector<std::string> & names)120 static bool SaveALN(const char *alnfile, std::vector<std::string> &names)
121 {
122     std::vector<vcg::Matrix44f> Tr(names.size());
123     for(int i=0; i < static_cast<int>(Tr.size()); ++i) Tr[i].SetIdentity();
124     return SaveALN(alnfile,names, Tr);
125 }
126 template <class matrixfloat>
SaveALN(const char * alnfile,std::vector<std::string> & names,std::vector<vcg::Matrix44<matrixfloat>> & Tr)127 static bool SaveALN(const char *alnfile, std::vector<std::string> &names, std::vector<vcg::Matrix44<matrixfloat> > &Tr)
128 {
129  // printf("Saving aln file %s\n",alnfile);
130   FILE *fp=fopen(alnfile,"w");
131   if(!fp)
132   {
133     printf("unable to open file %s\n",alnfile);
134     return false;
135   }
136 
137   fprintf(fp,"%i\n",(int)names.size());
138   for(int i=0;i < static_cast<int>(names.size());++i)
139   {
140     fprintf(fp,"%s\n",names[i].c_str());
141 
142     fprintf(fp,"#\n");
143     fprintf(fp,"%lf %lf %lf %lf \n",(Tr[i][0][0]),(Tr[i][0][1]),(Tr[i][0][2]),(Tr[i][0][3]));
144     fprintf(fp,"%lf %lf %lf %lf \n",(Tr[i][1][0]),(Tr[i][1][1]),(Tr[i][1][2]),(Tr[i][1][3]));
145     fprintf(fp,"%lf %lf %lf %lf \n",(Tr[i][2][0]),(Tr[i][2][1]),(Tr[i][2][2]),(Tr[i][2][3]));
146     fprintf(fp,"%lf %lf %lf %lf \n",(Tr[i][3][0]),(Tr[i][3][1]),(Tr[i][3][2]),(Tr[i][3][3]));
147   }
148   fprintf(fp,"0\n");
149 
150   fclose(fp);
151   return true;
152 }
153 
154 };
155 
156 #endif //__OM_ALNPARSER_
157