00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00030 #include <Q3MimeSourceFactory>
00031 #include <qmime.h>
00032 #include <q3dragobject.h>
00033 #include <QPixmap>
00034
00035 #include "debug.h"
00036 #include "mytools.h"
00037 #include "tinyxml.h"
00038 #include "matvec3D.h"
00039 #include "matvecIO.h"
00040
00045 QPixmap load_pixmap(const QString &name)
00046 {
00047 const QMimeSource *m = Q3MimeSourceFactory::defaultFactory()->data( name );
00048 if ( !m )
00049 return QPixmap();
00050 QPixmap pix;
00051 Q3ImageDrag::decode( m, pix );
00052 return pix;
00053 }
00054
00055 int nextValidLine(QTextStream *stream, QString *line)
00056 {
00057 while (1) {
00058 *line = stream->readLine();
00059 if ( line->isNull() ) break;
00060 if ( !line->isEmpty() && (*line)[0]!='#' ) break;
00061 }
00062 if ( line->isNull() ) return 0;
00063 else return 1;
00064 }
00065
00066 int nextCommentLine(QTextStream *stream, QString *line)
00067 {
00068 while (1) {
00069 *line = stream->readLine();
00070 if ( line->isNull() ) break;
00071 if ( !line->isEmpty() && (*line)[0]=='#' ) break;
00072 }
00073 if ( line->isNull() ) return 0;
00074 else return 1;
00075 }
00076
00077 int findString(QTextStream *stream, QString target)
00078 {
00079 QString line;
00080 while ( nextValidLine(stream,&line) )
00081 {
00082 if ( !(QString::compare(line,target)) )
00083 return 1;
00084 }
00085 return 0;
00086 }
00087
00093 QString
00094 relativePath(QString absolutePath, QString relativeToDir )
00095 {
00096 absolutePath.replace("\\","/");
00097 relativeToDir.replace("\\","/");
00098 QStringList absoluteDirectories = absolutePath.split( '/', QString::SkipEmptyParts );
00099 QStringList relativeDirectories = relativeToDir.split( '/', QString::SkipEmptyParts );
00100
00101
00102 int length = std::min(absoluteDirectories.count(), relativeDirectories.count());
00103
00104
00105 int lastCommonRoot = -1;
00106 int index;
00107
00108 DBGP("Absolute path: " << absolutePath.latin1());
00109 DBGP("Relative to : " << relativeToDir.latin1());
00110
00111
00112 for (index = 0; index < length; index++) {
00113 if (absoluteDirectories[index] == relativeDirectories[index]) {
00114 lastCommonRoot = index;
00115 } else {
00116 break;
00117 }
00118 }
00119 DBGP("Last common root: " << lastCommonRoot);
00120
00121
00122 if (lastCommonRoot == -1) {
00123 return absolutePath;
00124 }
00125
00126
00127 QString relativePath;
00128
00129
00130 for (index = lastCommonRoot + 1; index < relativeDirectories.count(); index++) {
00131 if (relativeDirectories[index].length() > 0) {
00132 relativePath.append("../");
00133 }
00134 }
00135
00136
00137 for (index = lastCommonRoot + 1; index < absoluteDirectories.count() - 1; index++) {
00138 relativePath.append(absoluteDirectories[index] ).append( "/" );
00139 }
00140 relativePath.append(absoluteDirectories[absoluteDirectories.count() - 1]);
00141
00142 DBGP("Relative path: " << relativePath.latin1());
00143 return relativePath;
00144 }
00145
00151 const TiXmlElement*
00152 findXmlElement(const TiXmlElement *root, QString defStr)
00153 {
00154 defStr = defStr.stripWhiteSpace();
00155 const TiXmlElement* child = root->FirstChildElement();
00156 while(child!=NULL){
00157 if(child->Value()==defStr) break;
00158 child = child->NextSiblingElement();
00159 }
00160 return child;
00161 }
00162
00168 std::list<const TiXmlElement*>
00169 findAllXmlElements(const TiXmlElement *root, QString defStr)
00170 {
00171 defStr = defStr.stripWhiteSpace();
00172 std::list<const TiXmlElement*> children;
00173 const TiXmlElement* child = root->FirstChildElement();
00174 while(child!=NULL){
00175 if(child->Value()==defStr) {
00176 children.push_back(child);
00177 }
00178 child = child->NextSiblingElement();
00179 }
00180 return children;
00181 }
00182
00188 int countXmlElements(const TiXmlElement *root, QString defStr)
00189 {
00190 int count = 0;
00191 const TiXmlElement* child = root->FirstChildElement();
00192 while(child!=NULL){
00193 if(child->Value()==defStr) count++;
00194 child = child->NextSiblingElement();
00195 }
00196 return count;
00197 }
00198
00206 bool getDouble(const TiXmlElement *root, QString defStr, double& val){
00207 const TiXmlElement* child = root->FirstChildElement();
00208 while(child!=NULL){
00209 if(child->Value()==defStr){
00210 QString valueStr = child->GetText();
00211 val = valueStr.toDouble();
00212 return true;
00213 }
00214 child = child->NextSiblingElement();
00215 }
00216 return false;
00217 }
00218
00219
00227 bool getInt(const TiXmlElement *root, QString defStr,int &val){
00228 const TiXmlElement* child = root->FirstChildElement();
00229 while(child!=NULL){
00230 if(child->Value()==defStr){
00231 QString valueStr = child->GetText();
00232 val = valueStr.toInt();
00233 return true;
00234 }
00235 child = child->NextSiblingElement();
00236 }
00237 return false;
00238 }
00239 bool getPosition(const TiXmlElement *root, vec3 &pos){
00240 bool ok1, ok2, ok3;
00241 if(root == NULL){
00242 QTWARNING("The given root is not a Position Element");
00243 return false;
00244 }
00245 QString rootValue = root->Value();
00246 rootValue = rootValue.stripWhiteSpace();
00247 if(!(rootValue=="position"||rootValue=="orientation")){
00248 QTWARNING("The given root is not a Position Element");
00249 return false;
00250 }
00251 QString valueStr = root->GetText();
00252 valueStr = valueStr.simplifyWhiteSpace().stripWhiteSpace();
00253 QStringList l;
00254 l = QStringList::split(' ', valueStr);
00255 if(l.count()!=3){
00256 QTWARNING("Invalid position input");
00257 return false;
00258 }
00259 double x,y,z;
00260 x = l[0].toDouble(&ok1);
00261 y = l[1].toDouble(&ok2);
00262 z = l[2].toDouble(&ok3);
00263 if(!ok1 || !ok2 || !ok3){
00264 QTWARNING("Invalid position input");
00265 return false;
00266 }
00267 pos.set(x,y,z);
00268 return true;
00269 }
00270
00271 bool getTransform(const TiXmlElement *root, transf &totalTran)
00272 {
00273 bool ok1, ok2, ok3;
00274 bool ok[9];
00275 if(root == NULL){
00276 QTWARNING("The given root is not a Transform Element");
00277 return false;
00278 }
00279 QString rootValue = root->Value();
00280 rootValue = rootValue.stripWhiteSpace();
00281 if(rootValue!="transform"){
00282 QTWARNING("The given root is not a Transform Element");
00283 return false;
00284 }
00285
00286 totalTran = transf::IDENTITY;
00287
00288 const TiXmlElement* child = root->FirstChildElement();
00289 while(child!=NULL){
00290 transf newTran;
00291 QString valueStr = child->GetText();
00292 valueStr = valueStr.simplifyWhiteSpace().stripWhiteSpace();
00293 QStringList l;
00294 l = QStringList::split(' ', valueStr);
00295 QString defString = child->Value();
00296 defString = defString.stripWhiteSpace();
00297 if(defString=="translation"){
00298 if(l.count()!=3){
00299 QTWARNING("Invalid translation transformation input");
00300 return false;
00301 }
00302 double x,y,z;
00303 x = l[0].toDouble(&ok1);
00304 y = l[1].toDouble(&ok2);
00305 z = l[2].toDouble(&ok3);
00306 if(!ok1 || !ok2 || !ok3){
00307 QTWARNING("Invalid translation transformation input");
00308 return false;
00309 }
00310 newTran.set(Quaternion::IDENTITY, vec3(x,y,z));
00311 }
00312 else if(defString=="rotation"){
00313 if(l.count()!=2){
00314 QTWARNING("Invalid rotation transformation input");
00315 return false;
00316 }
00317 double rotationAngle = l[0].toDouble(&ok1);
00318 if(!ok1){
00319 QTWARNING("Invalid rotation transformation input");
00320 return false;
00321 }
00322 rotationAngle *= M_PI / 180.0;
00323 QString rotationAxis = l[1];
00324 if (rotationAxis=="x") {
00325 newTran.set(Quaternion(rotationAngle, vec3::X), vec3(0,0,0));
00326 } else if (rotationAxis=="y") {
00327 newTran.set(Quaternion(rotationAngle, vec3::Y), vec3(0,0,0));
00328 } else if (rotationAxis=="z") {
00329 newTran.set(Quaternion(rotationAngle, vec3::Z), vec3(0,0,0));
00330 } else {
00331 QTWARNING("Invalid rotation transformation input");
00332 return false;
00333 }
00334 }
00335 else if(defString=="rotationMatrix"){
00336 if(l.count()!=9){
00337 QTWARNING("Invalid rotation transformation input");
00338 return false;
00339 }
00340 double R[9];
00341 for(int i=0;i<9;i++){
00342 R[i]=l[i].toDouble(&ok[i]);
00343 if(!ok[i]){
00344 QTWARNING("Invalid rotation transformation input");
00345 return false;
00346 }
00347 }
00348 newTran.set(Quaternion(mat3(R).transpose()), vec3(0,0,0));
00349 }
00350 else if(defString=="fullTransform"){
00351 QTextStream lineStream(&valueStr,QIODevice::ReadOnly);
00352 lineStream>>newTran;
00353 }
00354 totalTran = newTran*totalTran;
00355 child = child->NextSiblingElement();
00356 }
00357 return true;
00358 }