Debian Codegraph @

plugins/core/Standard/qSRA/src/profileLoader.cpp

raw
1 //##########################################################################
2 //# #
3 //# CLOUDCOMPARE #
4 //# #
5 //# This program is free software; you can redistribute it and/or modify #
6 //# it under the terms of the GNU General Public License as published by #
7 //# the Free Software Foundation; version 2 or later of the License. #
8 //# #
9 //# This program is distributed in the hope that it will be useful, #
10 //# but WITHOUT ANY WARRANTY; without even the implied warranty of #
11 //# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
12 //# GNU General Public License for more details. #
13 //# #
14 //# COPYRIGHT: EDF R&D / TELECOM ParisTech (ENST-TSI) #
15 //# #
16 //##########################################################################
17
18 #include "profileLoader.h"
19
20 //qCC_db
21 #include <ccPointCloud.h>
22 #include <ccPolyline.h>
23
24 //qCC_plugins
25 #include <ccMainAppInterface.h>
26
27 //Qt
28 #include <QFile>
29 #include <QTextStream>
30 #include <QFileInfo>
31
32 ccPolyline* ProfileLoader::Load(QString filename, CCVector3& origin, ccMainAppInterface* app/*=nullptr*/)
33 {
34 //load profile as a polyline
35 QFile file(filename);
36 assert(file.exists());
37 if (!file.open(QFile::ReadOnly | QFile::Text))
38 {
39 if (app)
40 app->dispToConsole(QString("Failed to open file for reading! Check access rights"),ccMainAppInterface::ERR_CONSOLE_MESSAGE);
41 return nullptr;
42 }
43
44 QTextStream stream(&file);
45
46 ccPolyline* polyline = nullptr;
47
48 bool error = false;
49 for (unsigned n=0; n<1; ++n) //fake loop for easy break ;)
50 {
51 //read origin
52 {
53 QString headerLine = stream.readLine();
54 if (headerLine.isEmpty() || !headerLine.startsWith("X"))
55 {
56 if (app)
57 app->dispToConsole(QString("Malformed file (origin header expected on first line)"),ccMainAppInterface::ERR_CONSOLE_MESSAGE);
58 error = true;
59 break;
60 }
61 QString centerLine = stream.readLine();
62 {
63 QStringList tokens = centerLine.simplified().split(QChar(' '), QString::SkipEmptyParts);
64 bool validLine = false;
65 if (tokens.size() == 3)
66 {
67 bool ok[3] = {false, false, false};
68 origin.x = static_cast<PointCoordinateType>(tokens[0].toDouble(ok+0));
69 origin.y = static_cast<PointCoordinateType>(tokens[1].toDouble(ok+1));
70 origin.z = static_cast<PointCoordinateType>(tokens[2].toDouble(ok+2));
71 validLine = ok[0] && ok[1] && ok[2];
72 }
73 if (!validLine)
74 {
75 if (app)
76 app->dispToConsole(QString("Malformed file (origin coordinates expected on second line)"),ccMainAppInterface::ERR_CONSOLE_MESSAGE);
77 error = true;
78 break;
79 }
80 }
81 }
82
83 //read elevations
84 {
85 QString headerLine = stream.readLine();
86 if (headerLine.isEmpty() || !headerLine.startsWith("R"))
87 {
88 if (app)
89 app->dispToConsole(QString("Malformed file (radii/heights header expected on third line)"),ccMainAppInterface::ERR_CONSOLE_MESSAGE);
90 error = true;
91 break;
92 }
93
94 QString line = stream.readLine();
95 std::vector< CCVector2d > points;
96 while (!line.isEmpty())
97 {
98 QStringList tokens = line.simplified().split(QChar(' '), QString::SkipEmptyParts);
99 if (tokens.size() < 2)
100 {
101 if (app)
102 app->dispToConsole(QString("Malformed file (radius/height couple expected from the 4th line and afterwards)"),ccMainAppInterface::ERR_CONSOLE_MESSAGE);
103 error = true;
104 break;
105 }
106
107 CCVector2d P;
108 P.x = tokens[0].toDouble(); //radius
109 P.y = tokens[1].toDouble(); //height
110
111 try
112 {
113 points.push_back(P);
114 }
115 catch (const std::bad_alloc&)
116 {
117 //not enough memory
118 if (app)
119 app->dispToConsole(QString("Not enough memory!"),ccMainAppInterface::ERR_CONSOLE_MESSAGE);
120 error = true;
121 break;
122 }
123
124 line = stream.readLine();
125 }
126
127 //convert 2D points to polyline
128 {
129 unsigned count = static_cast<unsigned>(points.size());
130 if (count > 1)
131 {
132 ccPointCloud* vertices = new ccPointCloud("vertices");
133 polyline = new ccPolyline(vertices);
134 polyline->addChild(vertices);
135 if (!vertices->reserve(count) || !polyline->reserve(count))
136 {
137 //not enough memory
138 if (app)
139 app->dispToConsole(QString("Not enough memory!"),ccMainAppInterface::ERR_CONSOLE_MESSAGE);
140 error = true;
141 break;
142 }
143
144 //add vertices
145 {
146 for (unsigned i=0; i<count; ++i)
147 {
148 vertices->addPoint(CCVector3( static_cast<PointCoordinateType>(points[i].x),
149 static_cast<PointCoordinateType>(points[i].y),
150 0));
151 }
152 }
153
154 //add segments
155 polyline->addPointIndex(0,count);
156 polyline->setClosed(false); //just to be sure
157 polyline->set2DMode(true);
158
159 //add to DB
160 polyline->setName(QFileInfo(filename).baseName());
161 polyline->setColor(ccColor::green);
162 polyline->showColors(true);
163 polyline->setEnabled(true);
164 polyline->setLocked(true); //as we have applied a purely visual transformation, we can't let the user rotate it!!!
165 vertices->setEnabled(false);
166 }
167 else
168 {
169 if (app)
170 app->dispToConsole(QString("Not enough points in profile?!"), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
171
172 error = true;
173 break;
174 }
175 }
176 }
177
178 }
179
180 file.close();
181
182 if (error)
183 {
184 delete polyline;
185 polyline = nullptr;
186 }
187
188 return polyline;
189 }
190