00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef __VCGLIB_EXPORTERU3D
00025 #define __VCGLIB_EXPORTERU3D
00026
00027 #include <cstdlib>
00028 #include <string>
00029 #include <QDir>
00030 #include <QString>
00031 #include <QProcess>
00032 #include <vector>
00033 #include <QMessageBox>
00034
00035 #include "export_idtf.h"
00036 #include<vcg/space/point3.h>
00037
00038 namespace vcg {
00039 namespace tri {
00040 namespace io {
00041 namespace u3dparametersclasses
00042 {
00043 struct IDTFConverterParameters
00044 {
00045 const QString _converter_loc;
00046 const QString _input_file;
00047 const QString _output_file;
00048 int positionQuality;
00049
00050 IDTFConverterParameters(const QString& converter_loc,const QString& input_file,const QString& output_file)
00051 :_converter_loc(converter_loc),_input_file(input_file),_output_file(output_file)
00052 {
00053 }
00054 };
00055
00056 struct Movie15Parameters
00057 {
00058 Movie15Parameters()
00059 {
00060 _campar = NULL;
00061 }
00062
00063
00064 class CameraParameters
00065 {
00066 public:
00067 CameraParameters()
00068 :_cam_fov_angle(0.0f),_cam_roll_angle(0.0f),_obj_to_cam_dir(vcg::Point3f(0.0f,0.0f,0.0f)),_obj_to_cam_dist(0.0f),_obj_bbox_diag(0.0f),_obj_pos(vcg::Point3f(0.0f,0.0f,0.0f))
00069 {
00070
00071 }
00072
00073 CameraParameters(const vcg::Point3f& mesh_center,const float mesh_bbox_diag)
00074 :_cam_fov_angle(0.0f),_cam_roll_angle(0.0f),_obj_to_cam_dir(vcg::Point3f(0.0f,0.0f,mesh_bbox_diag)),_obj_to_cam_dist(0.0),_obj_pos(mesh_center),_obj_bbox_diag(mesh_bbox_diag)
00075 {
00076
00077 }
00078
00079 CameraParameters(const float cam_fov_angle,const float cam_roll_angle,
00080 const vcg::Point3f& obj_to_cam_dir,const float obj_to_cam_dist,
00081 const float obj_bbox_diag,
00082 const vcg::Point3f& obj_pos = vcg::Point3f(0.0f,0.0f,0.0f))
00083 :_cam_fov_angle(cam_fov_angle),_cam_roll_angle(cam_roll_angle),_obj_to_cam_dir(obj_to_cam_dir),_obj_to_cam_dist(obj_to_cam_dist),_obj_pos(obj_pos),_obj_bbox_diag(obj_bbox_diag)
00084 {
00085
00086 }
00087
00088 float _cam_fov_angle;
00089 float _cam_roll_angle;
00090 vcg::Point3f _obj_to_cam_dir;
00091 float _obj_to_cam_dist;
00092 vcg::Point3f _obj_pos;
00093 float _obj_bbox_diag;
00094
00095 };
00096 CameraParameters* _campar;
00097 int positionQuality;
00098 };
00099 }
00100
00101 template<typename SaveMeshType>
00102 class ExporterU3D
00103 {
00104
00105 public:
00106 enum U3DError
00107 {
00108 E_NOERROR,
00109 E_ABORTED_CONVERSION
00110 };
00111
00112 static const char *ErrorMsg(int error)
00113 {
00114 static const char * dae_error_msg[] =
00115 {
00116 "No errors",
00117 "Conversion Process From Idtf intermediate file to U3D format aborted"
00118 };
00119
00120 if(error>1 || error<0) return "Unknown error";
00121 else return dae_error_msg[error];
00122 };
00123
00124 static void substituteChar(QString& st,const QChar& ch_remove,const QString& sub)
00125 {
00126 int ii = 0;
00127 while ((ii = st.indexOf(ch_remove,ii)) != -1)
00128 st = st.replace(ii,1,sub);
00129 }
00130
00131 private:
00132 static int InvokeConverter(const u3dparametersclasses::IDTFConverterParameters& par)
00133 {
00134 QProcess p;
00135 QString convstring = par._converter_loc;
00136 #if defined(Q_OS_WIN)
00137 convstring = "\""+convstring + "\" -en 1 -rzf 0 -pq "+QString::number(par.positionQuality)+" -input \"" + par._input_file + "\" -output \"" + par._output_file +"\"";
00138 #else
00139 QString mac_input=par._input_file;
00140 QString mac_output=par._output_file;
00141
00142
00143 convstring = convstring + " -en 1 -rzf 0 -pq "+ QString::number(par.positionQuality)+" -input \"" + mac_input + "\" -output \"" + mac_output +"\"";
00144 #endif
00145
00146 qDebug("Starting converter %s", qPrintable(convstring));
00147 p.setProcessChannelMode(QProcess::MergedChannels);
00148 p.start(convstring);
00149
00150 bool t = p.waitForFinished(-1);
00151 if(!t) QMessageBox::warning(0, QString("Saving Error"), QString("Failed conversion executable '%1'").arg(convstring));
00152 p.close();
00153 return (int) t;
00154 }
00155
00156 static void SaveLatex(SaveMeshType& ,const QString& file,const u3dparametersclasses::Movie15Parameters& mov_par)
00157 {
00158 Output_File latex(file.toStdString() + ".tex");
00159 QString u3df = file + ".u3d";
00160 QStringList file_trim;
00161 QtUtilityFunctions::splitFilePath(u3df,file_trim);
00162 std::string u3d_final = QtUtilityFunctions::fileNameFromTrimmedPath(file_trim).toStdString();
00163 latex.write(0,"\\documentclass[a4paper]{article}");
00164 latex.write(0,"\\usepackage[3D]{movie15}");
00165 latex.write(0,"\\usepackage{hyperref}");
00166 latex.write(0,"\\usepackage[UKenglish]{babel}");
00167 latex.write(0,"\\begin{document}");
00168 latex.write(0,"\\includemovie[");
00169 latex.write(1,"poster,");
00170 latex.write(1,"toolbar, %same as `controls\'");
00171
00172
00173 QString u3d_text = QString::fromStdString(u3d_final);
00174 substituteChar(u3d_text,QChar('_'),QString(""));
00175 latex.write(1,"label=" + u3d_text.toStdString() + ",");
00176 latex.write(1,"text=(" + u3d_text.toStdString() + "),");
00177 std::string cam_string;
00178 u3dparametersclasses::Movie15Parameters::CameraParameters* cam = mov_par._campar;
00179 if (cam != NULL)
00180 {
00181 cam_string = cam_string + "3Daac=" + TextUtility::nmbToStr(cam->_cam_fov_angle) +
00182 ", 3Droll=" + TextUtility::nmbToStr(cam->_cam_roll_angle) +
00183 ", 3Dc2c=" + TextUtility::nmbToStr(cam->_obj_to_cam_dir.X()) + " " + TextUtility::nmbToStr(cam->_obj_to_cam_dir.Z()) + " " + TextUtility::nmbToStr(cam->_obj_to_cam_dir.Y()) +
00184 ", 3Droo=" + TextUtility::nmbToStr(cam->_obj_to_cam_dist) +
00185 ", 3Dcoo=" + TextUtility::nmbToStr(-cam->_obj_pos.X()) + " " + TextUtility::nmbToStr(cam->_obj_pos.Z()) + " " + TextUtility::nmbToStr(cam->_obj_pos.Y()) + ",";
00186 latex.write(1,cam_string);
00187 }
00188 latex.write(1,"3Dlights=CAD,");
00189 latex.write(0,"]{\\linewidth}{\\linewidth}{" + u3d_final + "}");
00190 latex.write(0,"\\end{document}");
00191 }
00192
00193 public:
00194
00195 static int Save(SaveMeshType& m,const char* output_file,const char* conv_loc,const u3dparametersclasses::Movie15Parameters& mov_par,const int mask)
00196 {
00197 QString curr = QDir::currentPath();
00198 QString out(output_file);
00199 QStringList out_trim;
00200 QtUtilityFunctions::splitFilePath(out,out_trim);
00201 QString tmp(QDir::tempPath());
00202
00203
00204 tmp = tmp + "/" + QtUtilityFunctions::fileNameFromTrimmedPath(out_trim) + ".idtf";
00205
00206 QString conv_loc_st(conv_loc);
00207 QString output_file_st(output_file);
00208
00212
00213
00214
00215
00216
00218
00219
00220
00221 vcg::tri::io::ExporterIDTF<SaveMeshType>::Save(m,qPrintable(tmp),mask);
00222 u3dparametersclasses::IDTFConverterParameters idtfpar(conv_loc_st,tmp,output_file_st);
00223 idtfpar.positionQuality = mov_par.positionQuality;
00224 qDebug("conv_loc_st '%s'", qPrintable(conv_loc_st));
00225 qDebug("conv_loc '%s'", conv_loc);
00226 qDebug("idtfpar._converter_loc '%s'", qPrintable(idtfpar._converter_loc));
00227 int res = InvokeConverter(idtfpar);
00228
00229
00230
00231
00232
00234
00235
00236 QDir::setCurrent(curr);
00237 QString lat (output_file);
00238 QStringList l = lat.split(".");
00239 SaveLatex(m,l[0],mov_par);
00240 QDir dir(QDir::tempPath());
00241 dir.remove(tmp);
00242
00243 if (res)
00244 return 0;
00245 else
00246 return 1;
00247 }
00248
00249 static int GetExportMaskCapability()
00250 {
00251 int capability = 0;
00252
00253
00254
00255 capability |= vcg::tri::io::Mask::IOM_VERTCOLOR;
00256
00257
00258
00259 capability |= vcg::tri::io::Mask::IOM_FACECOLOR;
00260
00262 capability |= Mask::IOM_WEDGTEXCOORD;
00263
00264
00265 return capability;
00266 }
00267
00268 };
00269
00270 }
00271 }
00272 }
00273
00274 #endif