1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
//##########################################################################
//# #
//# CLOUDCOMPARE #
//# #
//# This program is free software; you can redistribute it and/or modify #
//# it under the terms of the GNU General Public License as published by #
//# the Free Software Foundation; version 2 or later of the License. #
//# #
//# This program is distributed in the hope that it will be useful, #
//# but WITHOUT ANY WARRANTY; without even the implied warranty of #
//# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
//# GNU General Public License for more details. #
//# #
//# COPYRIGHT: EDF R&D / TELECOM ParisTech (ENST-TSI) #
//# #
//##########################################################################
#include "MascaretFilter.h"
//local
#include "ui_saveMascaretFileDlg.h"
//qCC_db
#include <ccLog.h>
#include <ccPolyline.h>
//Qt
#include <QFile>
#include <QTextStream>
#include <QDialog>
//System
#include <string.h>
//! Mascaret File Save dialog
class SaveMascaretFileDlg : public QDialog, public Ui::SaveMascaretFileDlg
{
Q_OBJECT
public:
//! Default constructor
SaveMascaretFileDlg(QWidget* parent = 0)
: QDialog(parent, Qt::Tool)
, Ui::SaveMascaretFileDlg()
{
setupUi(this);
}
};
bool MascaretFilter::canSave(CC_CLASS_ENUM type, bool& multiple, bool& exclusive) const
{
if (type == CC_TYPES::POLY_LINE)
{
multiple = true;
exclusive = true;
return true;
}
return false;
}
QString MakeMascaretName(QString name)
{
//max 30 characeters
name = name.left(30);
//no space characters
name.replace(' ','_');
return name;
}
inline void ToLocalAbscissa(const CCVector3& P, const CCVector3& C, const CCVector3& U, unsigned char upDir, CCVector2& localP)
{
//convert to 'local' coordinate system
localP.x = (P-C).dot(U);
localP.y = P.u[upDir];
}
CC_FILE_ERROR MascaretFilter::saveToFile(ccHObject* entity, const QString& filename, const SaveParameters& parameters)
{
if (!entity || filename.isEmpty())
return CC_FERR_BAD_ARGUMENT;
//look for valid profiles
std::vector<ccPolyline*> profiles;
try
{
//get all polylines
std::vector<ccPolyline*> candidates;
if (entity->isA(CC_TYPES::POLY_LINE))
{
candidates.push_back(static_cast<ccPolyline*>(entity));
}
else if (entity->isA(CC_TYPES::HIERARCHY_OBJECT))
{
for (unsigned i=0; i<entity->getChildrenNumber(); ++i)
if (entity->getChild(i) && entity->getChild(i)->isA(CC_TYPES::POLY_LINE))
candidates.push_back(static_cast<ccPolyline*>(entity->getChild(i)));
}
//then keep the valid profiles only
for (size_t i=0; i<candidates.size(); ++i)
{
ccPolyline* poly = candidates[i];
if ( !poly->hasMetaData(ccPolyline::MetaKeyUpDir())
|| !poly->hasMetaData(ccPolyline::MetaKeyAbscissa())
|| !poly->hasMetaData(ccPolyline::MetaKeyPrefixCenter()+".x")
|| !poly->hasMetaData(ccPolyline::MetaKeyPrefixCenter()+".y")
|| !poly->hasMetaData(ccPolyline::MetaKeyPrefixCenter()+".z")
|| !poly->hasMetaData(ccPolyline::MetaKeyPrefixDirection()+".x")
|| !poly->hasMetaData(ccPolyline::MetaKeyPrefixDirection()+".y")
|| !poly->hasMetaData(ccPolyline::MetaKeyPrefixDirection()+".z") )
{
ccLog::Warning(QString("[Mascaret] Polyline '%1' is not a valid profile (missing meta-data)").arg(poly->getName()));
break;
}
else
{
profiles.push_back(poly);
}
}
}
catch (const std::bad_alloc&)
{
return CC_FERR_NOT_ENOUGH_MEMORY;
}
if (profiles.empty())
return CC_FERR_NO_SAVE;
//open ASCII file for writing
QFile file(filename);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return CC_FERR_WRITING;
QTextStream outFile(&file);
outFile.setRealNumberNotation(QTextStream::FixedNotation);
outFile.setRealNumberPrecision(12);
//ask some parameters
SaveMascaretFileDlg smfDlg;
if (!smfDlg.exec())
return CC_FERR_CANCELED_BY_USER;
QString biefName = smfDlg.biefNameLineEdit->text();
QString type("T"); //B or T --> ask the user
switch(smfDlg.typeComboBox->currentIndex())
{
case 0:
type = "B"; //bathy
break;
case 1:
type = "T"; //topo
break;
default:
assert(false);
}
//sanitize the 'bief' (reach) name
biefName = MakeMascaretName(biefName);
//sort the sections by their abscissa
if (profiles.size() > 1)
{
for (size_t i=0; i<profiles.size()-1; ++i)
{
size_t smallestIndex = i;
double smallestAbscissa = profiles[i]->getMetaData(ccPolyline::MetaKeyAbscissa()).toDouble();
for (size_t j=i+1; j<profiles.size(); ++j)
{
double a = profiles[j]->getMetaData(ccPolyline::MetaKeyAbscissa()).toDouble();
if (a < smallestAbscissa)
{
smallestAbscissa = a;
smallestIndex = j;
}
}
if (i != smallestIndex)
{
std::swap(profiles[i],profiles[smallestIndex]);
}
}
}
CC_FILE_ERROR result = CC_FERR_NO_SAVE;
//for each profile
for (size_t i=0; i<profiles.size(); ++i)
{
ccPolyline* poly = profiles[i];
unsigned vertCount = poly ? poly->size() : 0;
if (vertCount < 2)
{
//invalid size
ccLog::Warning(QString("[Mascaret] Polyline '%1' does not have enough vertices")
.arg(poly ? poly->getName() : QStringLiteral("unnamed")));
continue;
}
//decode meta-data
bool ok = true;
int upDir = 2;
double absc = 0.0;
CCVector3d Cd(0,0,0);
CCVector3d Ud(0,0,0);
while (true) //fake loop for easy break
{
upDir = poly->getMetaData(ccPolyline::MetaKeyUpDir()).toInt(&ok);
if (!ok) break;
absc = poly->getMetaData(ccPolyline::MetaKeyAbscissa()).toDouble(&ok);
if (!ok) break;
Cd.x = poly->getMetaData(ccPolyline::MetaKeyPrefixCenter()+".x").toDouble(&ok);
if (!ok) break;
Cd.y = poly->getMetaData(ccPolyline::MetaKeyPrefixCenter()+".y").toDouble(&ok);
if (!ok) break;
Cd.z = poly->getMetaData(ccPolyline::MetaKeyPrefixCenter()+".z").toDouble(&ok);
if (!ok) break;
Ud.x = poly->getMetaData(ccPolyline::MetaKeyPrefixDirection()+".x").toDouble(&ok);
if (!ok) break;
Ud.y = poly->getMetaData(ccPolyline::MetaKeyPrefixDirection()+".y").toDouble(&ok);
if (!ok) break;
Ud.z = poly->getMetaData(ccPolyline::MetaKeyPrefixDirection()+".z").toDouble(&ok);
break;
}
if (!ok)
{
ccLog::Warning(QString("[Mascaret] At least one of the meta-data entry of polyline '%1' is invalid?!").arg(poly->getName()));
continue;
}
QString profileName = poly->getName();
profileName = MakeMascaretName(profileName);
CCVector3 C = CCVector3::fromArray(Cd.u);
CCVector3 U = CCVector3::fromArray(Ud.u);
U.normalize();
//write header
outFile << "PROFIL " << biefName << " " << profileName << " " << absc;
#define SAVE_AS_GEO_MASCARET
#ifdef SAVE_AS_GEO_MASCARET
int xDir = upDir == 2 ? 0 : upDir+1;
int yDir = xDir == 2 ? 0 : xDir+1;
//for "geo"-mascaret, we add some more information:
// - first point
{
const CCVector3* firstP = poly->getPoint(0);
CCVector3d firstPg = poly->toGlobal3d(*firstP);
outFile << " ";
outFile << firstPg.u[xDir] << " " << firstPg.u[yDir];
}
// - last point
{
const CCVector3* lastP = poly->getPoint(vertCount-1);
CCVector3d lastPg = poly->toGlobal3d(*lastP);
outFile << " ";
outFile << lastPg.u[xDir] << " " << lastPg.u[yDir];
}
// - profile/path intersection point
{
outFile << " AXE ";
CCVector3d Cdg = poly->toGlobal3d(Cd);
outFile << Cdg.u[xDir] << " " << Cdg.u[yDir];
}
#endif
outFile << endl;
//check the abscissa values order (must be increasing!)
bool inverted = false;
{
const CCVector3* P0 = poly->getPoint(0);
//convert to 'local' coordinate system
CCVector2 Q0;
ToLocalAbscissa(*P0, C, U, upDir, Q0);
const CCVector3* P1 = poly->getPoint(vertCount-1);
//convert to 'local' coordinate system
CCVector2 Q1;
ToLocalAbscissa(*P1, C, U, upDir, Q1);
inverted = (Q1.x < Q0.x);
}
for (unsigned j=0; j<vertCount; ++j)
{
const CCVector3* P = poly->getPoint(inverted ? vertCount-1-j : j);
//convert to 'local' coordinate system
CCVector2 Q;
ToLocalAbscissa(*P, C, U, upDir, Q);
outFile << Q.x << " " << Q.y << " " << type;
#ifdef SAVE_AS_GEO_MASCARET
{
//for "geo"-mascaret, we add some more information:
// - real coordinates of the point
outFile << " ";
CCVector3d Pg = poly->toGlobal3d(*P);
outFile << Pg.u[xDir] << " " << Pg.u[yDir];
}
#endif
outFile << endl;
}
result = CC_FERR_NO_ERROR;
}
file.close();
return result;
}
#include "MascaretFilter.moc"
|