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