Debian Codegraph @

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

raw
1 //##########################################################################
2 //# #
3 //# CLOUDCOMPARE PLUGIN: qSRA #
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 #
15 //# #
16 //##########################################################################
17
18 #include "dxfProfilesExporter.h"
19
20 //qCC_db
21 #include <ccPolyline.h>
22
23 //qCC_plugins
24 #include <ccMainAppInterface.h>
25
26 //DXF lib
27 #ifdef CC_DXF_SUPPORT
28 #include <dl_dxf.h>
29 #endif
30
31 bool DxfProfilesExporter::IsEnabled()
32 {
33 #ifdef CC_DXF_SUPPORT
34 return true;
35 #else
36 return false;
37 #endif
38 }
39
40 //constants
41 static const char PROFILE_LAYER[] = "theoretical_profile";
42 static const char LEGEND_LAYER[] = "legend";
43 static const char VERT_PROFILE_LAYER[] = "vertical_profile_%1";
44 static const char HORIZ_PROFILE_LAYER[] = "horizontal_profile_%1";
45 static const int s_lineWidth = 5;
46
47 static const double c_textHeight_mm = 2.5;
48 static const double c_textMargin_mm = 2.0;
49 static const double c_pageHeight_mm = 297.0;
50 static const double c_pageWidth_mm = 210.0;
51 static const double c_pageMargin_mm = 10.0;
52 static const double c_profileMargin_mm = 50.0;
53
54 struct VertStepData
55 {
56 double height;
57 double radius_th;
58 double deviation;
59 };
60
61 bool DxfProfilesExporter::SaveVerticalProfiles( const QSharedPointer<DistanceMapGenerationTool::Map>& map,
62 ccPolyline* profile,
63 QString filename,
64 unsigned angularStepCount,
65 double heightStep,
66 double heightShift,
67 const Parameters& params,
68 ccMainAppInterface* app/*=nullptr*/)
69 {
70 #ifdef CC_DXF_SUPPORT
71 assert(c_pageMargin_mm < c_profileMargin_mm);
72 assert(2.0*c_profileMargin_mm < std::min(c_pageWidth_mm,c_pageHeight_mm));
73
74 if (!map || !profile || angularStepCount == 0 || heightStep <= 0)
75 {
76 //invalid parameters
77 return false;
78 }
79
80 //Theoretical profile bounding box
81 CCVector3 profileBBMin;
82 CCVector3 profileBBMax;
83 profile->getAssociatedCloud()->getBoundingBox(profileBBMin,profileBBMax);
84 //Mix with the map's boundaries along 'Y'
85 double yMin = std::max(map->yMin, static_cast<double>(profileBBMin.y) + heightShift);
86 double yMax = std::min(map->yMin + map->ySteps * map->yStep, static_cast<double>(profileBBMax.y) + heightShift);
87 const double ySpan = yMax - yMin;
88 //For the 'X' dimension, it's easier to stick with the th. profile
89 const double xMin = profileBBMin.x;
90 // const double xMax = profileBBMax.x;
91 const double xSpan = profileBBMax.x - profileBBMin.x;
92
93 if (xSpan == 0.0 && ySpan == 0.0)
94 {
95 if (app)
96 app->dispToConsole(QString("Internal error: null profile?!"), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
97 return false;
98 }
99
100 DL_Dxf dxf;
101 #ifdef _WIN32
102 DL_WriterA* dw = dxf.out(filename.toStdWString(), DL_VERSION_R12);
103 #else
104 DL_WriterA* dw = dxf.out(filename.toStdString(), DL_VERSION_R12);
105 #endif
106 if (!dw)
107 {
108 if (app)
109 app->dispToConsole(QString("Failed to open '%1' file for writing!").arg(filename), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
110 return false;
111 }
112
113 //write header
114 dxf.writeHeader(*dw);
115
116 //add dimensions
117 dw->dxfString(9, "$INSBASE");
118 dw->dxfReal(10,0.0);
119 dw->dxfReal(20,0.0);
120 dw->dxfReal(30,0.0);
121 dw->dxfString(9, "$EXTMIN");
122 dw->dxfReal(10,0.0);
123 dw->dxfReal(20,0.0);
124 dw->dxfReal(30,0.0);
125 dw->dxfString(9, "$EXTMAX");
126 dw->dxfReal(10,c_pageWidth_mm);
127 dw->dxfReal(20,c_pageHeight_mm);
128 dw->dxfReal(30,0.0);
129 dw->dxfString(9, "$LIMMIN");
130 dw->dxfReal(10,0.0);
131 dw->dxfReal(20,0.0);
132 dw->dxfString(9, "$LIMMAX");
133 dw->dxfReal(10,c_pageWidth_mm);
134 dw->dxfReal(20,c_pageHeight_mm);
135
136 //close header
137 dw->sectionEnd();
138
139 //Opening the Tables Section
140 dw->sectionTables();
141 //Writing the Viewports
142 dxf.writeVPort(*dw);
143
144 //Writing the Linetypes (all by default)
145 {
146 dw->tableLinetypes(3);
147 dxf.writeLinetype(*dw, DL_LinetypeData("BYBLOCK", "BYBLOCK", 0, 0, 0.0));
148 dxf.writeLinetype(*dw, DL_LinetypeData("BYLAYER", "BYLAYER", 0, 0, 0.0));
149 dxf.writeLinetype(*dw, DL_LinetypeData("CONTINUOUS", "Continuous", 0, 0, 0.0));
150 dw->tableEnd();
151 }
152
153 //Writing the Layers
154 dw->tableLayers(angularStepCount+3);
155 QStringList profileNames;
156 {
157 //default layer
158 dxf.writeLayer(*dw,
159 DL_LayerData("0", 0),
160 DL_Attributes(
161 std::string(""), // leave empty
162 DL_Codes::black, // default color
163 100, // default width (in 1/100 mm)
164 "CONTINUOUS", // default line style
165 1.0 // linetypeScale
166 ));
167
168 //theoretical profile layer
169 dxf.writeLayer(*dw,
170 DL_LayerData(PROFILE_LAYER, 0),
171 DL_Attributes(
172 std::string(""),
173 DL_Codes::red,
174 s_lineWidth,
175 "CONTINUOUS",
176 1.0));
177
178 //legend layer
179 dxf.writeLayer(*dw,
180 DL_LayerData(LEGEND_LAYER, 0),
181 DL_Attributes(
182 std::string(""),
183 DL_Codes::black,
184 s_lineWidth,
185 "CONTINUOUS",
186 1.0));
187
188 //vert. profile layers
189 for (unsigned i=0; i<angularStepCount; ++i)
190 {
191 //default layer name
192 QString layerName = QString(VERT_PROFILE_LAYER).arg(i+1,3,10,QChar('0'));
193 //but we use the profile title if we have one!
194 //DGM: nope, as it may be longer than 31 characters (R14 limit)
195 //if (params.profileTitles.size() >= static_cast<int>(angularStepCount))
196 //{
197 // layerName = params.profileTitles[i];
198 // layerName.replace(QChar(' '),QChar('_'));
199 //}
200
201 profileNames << layerName;
202 dxf.writeLayer(*dw,
203 DL_LayerData(qPrintable(layerName), 0), //DGM: warning, toStdString doesn't preserve "local" characters
204 DL_Attributes(
205 std::string(""),
206 i == 0 ? DL_Codes::green : -DL_Codes::green, //invisible if negative!
207 s_lineWidth,
208 "CONTINUOUS",
209 1.0));
210 }
211 }
212 dw->tableEnd();
213
214 //Writing Various Other Tables
215 //dxf.writeStyle(*dw); //DXFLIB V2.5
216 dw->tableStyle(1);
217 dxf.writeStyle(*dw, DL_StyleData("Standard", 0, 0.0, 0.75, 0.0, 0, 2.5, "txt", "")); //DXFLIB V3.3
218 dw->tableEnd();
219
220 dxf.writeView(*dw);
221 dxf.writeUcs(*dw);
222
223 dw->tableAppid(1);
224 dw->tableAppidEntry(0x12);
225 dw->dxfString(2, "ACAD");
226 dw->dxfInt(70, 0);
227 dw->tableEnd();
228
229 //Writing Dimension Styles
230 dxf.writeDimStyle( *dw,
231 /*arrowSize*/1,
232 /*extensionLineExtension*/1,
233 /*extensionLineOffset*/1,
234 /*dimensionGap*/1,
235 /*dimensionTextSize*/1);
236
237 //Writing Block Records
238 dxf.writeBlockRecord(*dw);
239 //dxf.writeBlockRecord(*dw, "myblock1");
240 //dxf.writeBlockRecord(*dw, "myblock2");
241 dw->tableEnd();
242
243 //Ending the Tables Section
244 dw->sectionEnd();
245
246 //Writing the Blocks Section
247 {
248 dw->sectionBlocks();
249
250 dxf.writeBlock(*dw, DL_BlockData("*Model_Space", 0, 0.0, 0.0, 0.0));
251 dxf.writeEndBlock(*dw, "*Model_Space");
252
253 dxf.writeBlock(*dw, DL_BlockData("*Paper_Space", 0, 0.0, 0.0, 0.0));
254 dxf.writeEndBlock(*dw, "*Paper_Space");
255
256 dxf.writeBlock(*dw, DL_BlockData("*Paper_Space0", 0, 0.0, 0.0, 0.0));
257 dxf.writeEndBlock(*dw, "*Paper_Space0");
258
259 dw->sectionEnd();
260 }
261
262 //Writing the Entities Section
263 {
264 dw->sectionEntities();
265
266 //we make the profile fit in the middle of the page (21.0 x 29.7 cm)
267 double scale = 1.0;
268 if (xSpan == 0)
269 {
270 assert(ySpan != 0);
271 scale = (c_pageHeight_mm - 2.0 * c_profileMargin_mm)/ySpan;
272 }
273 else if (ySpan == 0)
274 {
275 assert(xSpan != 0);
276 scale = (c_pageWidth_mm - 2.0 * c_profileMargin_mm)/xSpan;
277 }
278 else
279 {
280 scale = std::min( (c_pageWidth_mm - 2.0 * c_profileMargin_mm)/xSpan,
281 (c_pageHeight_mm - 2.0 * c_profileMargin_mm)/ySpan );
282 }
283
284 //min corner of profile area
285 const double x0 = (c_pageWidth_mm - xSpan*scale) / 2.0;
286 const double y0 = (c_pageHeight_mm - ySpan*scale) / 2.0;
287
288 //write theoretical profile (polyline)
289 {
290 unsigned vertexCount = profile->size();
291 dxf.writePolyline( *dw,
292 DL_PolylineData(static_cast<int>(vertexCount), 0, 0, 0),
293 DL_Attributes(PROFILE_LAYER, DL_Codes::bylayer, -1, "BYLAYER", 1.0));
294
295 for (unsigned i = 0; i < vertexCount; ++i)
296 {
297 const CCVector3* P = profile->getPoint(i);
298 dxf.writeVertex(*dw, DL_VertexData(x0 + (P->x - xMin)*scale, y0 + (P->y + heightShift - yMin)*scale, 0.0));
299 }
300
301 dxf.writePolylineEnd(*dw);
302 }
303
304 //write legend
305 {
306 DL_Attributes DefaultLegendMaterial(LEGEND_LAYER, DL_Codes::bylayer, -1, "BYLAYER", 1.0);
307
308 //write page contour
309 {
310 dxf.writePolyline( *dw,
311 DL_PolylineData(4, 0, 0, 1),
312 DefaultLegendMaterial);
313
314 dxf.writeVertex(*dw, DL_VertexData( c_pageMargin_mm, c_pageMargin_mm, 0.0));
315 dxf.writeVertex(*dw, DL_VertexData( c_pageMargin_mm, c_pageHeight_mm-c_pageMargin_mm, 0.0));
316 dxf.writeVertex(*dw, DL_VertexData( c_pageWidth_mm-c_pageMargin_mm, c_pageHeight_mm-c_pageMargin_mm, 0.0));
317 dxf.writeVertex(*dw, DL_VertexData( c_pageWidth_mm-c_pageMargin_mm, c_pageMargin_mm, 0.0));
318
319 dxf.writePolylineEnd(*dw);
320 }
321
322 double xLegend = c_pageMargin_mm + 2.0*c_textHeight_mm;
323 double yLegend = c_pageMargin_mm + 2.0*c_textHeight_mm;
324 const double legendWidth_mm = 20.0;
325
326 //deviation magnification factor
327 QString magnifyStr = QString::number(params.devMagnifyCoef);
328 dxf.writeText( *dw,
329 DL_TextData(xLegend, yLegend, 0.0, xLegend, yLegend, 0.0, c_textHeight_mm, 1.0, 0, 0, 0, qPrintable(QString("Deviation magnification factor: ") + magnifyStr), "STANDARD", 0.0), //DGM: warning, toStdString doesn't preserve "local" characters
330 DefaultLegendMaterial);
331
332 //next line
333 yLegend += c_textHeight_mm*2.0;
334
335 //units
336 dxf.writeText(*dw,
337 DL_TextData(xLegend, yLegend, 0.0, xLegend, yLegend, 0.0, c_textHeight_mm, 1.0, 0, 0, 0, qPrintable(QString("Deviation units: ") + params.scaledDevUnits), "STANDARD", 0.0),
338 DefaultLegendMaterial);
339
340 //next line
341 yLegend += c_textHeight_mm*2.0;
342
343 //true profile line (red)
344 dxf.writeLine( *dw,
345 DL_LineData(xLegend, yLegend, 0, xLegend + legendWidth_mm, yLegend, 0.0),
346 DL_Attributes(LEGEND_LAYER, DL_Codes::green, -1, "BYLAYER", 1.0));
347
348 dxf.writeText( *dw,
349 DL_TextData(xLegend + legendWidth_mm + c_textMargin_mm, yLegend, 0.0, xLegend + legendWidth_mm + c_textMargin_mm, yLegend, 0.0, c_textHeight_mm, 1.0, 0, 0, 0, qPrintable(params.legendRealProfileTitle), "STANDARD", 0.0), //DGM: warning, toStdString doesn't preserve "local" characters
350 DefaultLegendMaterial);
351
352 //next line
353 yLegend += c_textHeight_mm*2.0;
354
355 //theoretical profile line (red)
356 dxf.writeLine( *dw,
357 DL_LineData(xLegend, yLegend, 0, xLegend + legendWidth_mm, yLegend, 0.0),
358 DL_Attributes(LEGEND_LAYER, DL_Codes::red, -1, "BYLAYER", 1.0));
359
360 dxf.writeText( *dw,
361 DL_TextData(xLegend + legendWidth_mm + c_textMargin_mm, yLegend, 0.0, xLegend + legendWidth_mm + c_textMargin_mm, yLegend, 0.0, c_textHeight_mm, 1.0, 0, 0, 0, qPrintable(params.legendTheoProfileTitle), "STANDARD", 0.0), //DGM: warning, toStdString doesn't preserve "local" characters
362 DefaultLegendMaterial);
363 }
364
365 //write vertical profiles
366 for (unsigned angleStep = 0; angleStep < angularStepCount; ++angleStep)
367 {
368 std::vector<VertStepData> polySteps;
369 try
370 {
371 polySteps.reserve(map->ySteps);
372 }
373 catch (const std::bad_alloc&)
374 {
375 //not enough memory
376 dw->dxfEOF();
377 dw->close();
378 delete dw;
379 return false;
380 }
381
382 unsigned iMap = static_cast<unsigned>(static_cast<double>(angleStep * map->xSteps) / angularStepCount);
383 for (unsigned jMap = 0; jMap < map->ySteps; ++jMap)
384 {
385 const DistanceMapGenerationTool::MapCell& cell = map->at(iMap + jMap * map->xSteps);
386
387 VertStepData step;
388 step.height = map->yMin + static_cast<double>(jMap) * map->yStep;
389
390 if (step.height >= yMin && step.height <= yMax)
391 {
392 //find corresponding radius
393 bool found = false;
394 for (unsigned i = 1; i < profile->size(); ++i)
395 {
396 const CCVector3* A = profile->getPoint(i - 1);
397 const CCVector3* B = profile->getPoint(i);
398
399 double alpha = (step.height - A->y - heightShift) / (B->y - A->y);
400 if (alpha >= 0.0 && alpha <= 1.0)
401 {
402 //we deduce the right radius by linear interpolation
403 step.radius_th = A->x + alpha * (B->x - A->x);
404 found = true;
405 break;
406 }
407 }
408
409 if (found)
410 {
411 step.deviation = cell.count ? cell.value : 0.0;
412 polySteps.push_back(step);
413 }
414 }
415 }
416
417 const DL_Attributes DefaultMaterial(qPrintable(profileNames[angleStep]), DL_Codes::bylayer, -1, "BYLAYER", 1.0); //DGM: warning, toStdString doesn't preserve "local" characters
418 const DL_Attributes GrayMaterial (qPrintable(profileNames[angleStep]), DL_Codes::l_gray , -1, "", 1.0);
419
420 //write layer title
421 if (static_cast<int>(angleStep) < params.profileTitles.size())
422 {
423 const QString& title = params.profileTitles[angleStep];
424
425 CCVector3d Ptop(c_pageWidth_mm / 2.0, y0 + ySpan * scale + c_profileMargin_mm / 2.0, 0.0);
426
427 dxf.writeText( *dw,
428 DL_TextData(Ptop.x, Ptop.y, Ptop.z, Ptop.x, Ptop.y, Ptop.z, c_textHeight_mm, 1.0, 0, 1, 0, qPrintable(title), "STANDARD", 0.0), //DGM: warning, toStdString doesn't preserve "local" characters
429 GrayMaterial);
430
431 }
432
433 //write corresponding polyline
434 {
435 dxf.writePolyline( *dw,
436 DL_PolylineData(static_cast<int>(polySteps.size()), 0, 0, 0),
437 DefaultMaterial);
438
439 for (size_t i = 0; i < polySteps.size(); ++i)
440 {
441 const VertStepData& step = polySteps[i];
442 dxf.writeVertex(*dw, DL_VertexData( x0 + (step.radius_th + step.deviation * params.devMagnifyCoef - xMin) * scale,
443 y0 + (step.height - yMin) * scale, 0.0));
444 }
445
446 dxf.writePolylineEnd(*dw);
447 }
448
449 //write corresponding 'deviation bars' and labels
450 CCVector3d pageShift(x0 - xMin * scale, y0 - yMin * scale, 0.0);
451 {
452 size_t lastStep = 0;
453 for (size_t i = 0; i < polySteps.size(); ++i)
454 {
455 const VertStepData& step = polySteps[i];
456 bool displayIt = (i == 0 || i + 1 == polySteps.size());
457 if (!displayIt)
458 {
459 double dh = polySteps[i].height - polySteps[lastStep].height;
460 double next_dh = polySteps[i + 1].height - polySteps[lastStep].height;
461 if (dh >= heightStep || (next_dh > heightStep && std::abs(dh - heightStep) < std::abs(next_dh - heightStep)))
462 {
463 displayIt = true;
464 }
465 }
466
467 if (displayIt)
468 {
469 CCVector3d Pheight(step.radius_th,step.height,0.0);
470 CCVector3d Pdev(step.radius_th + step.deviation * params.devMagnifyCoef, step.height, 0.0);
471
472 //page scaling
473 Pheight = pageShift + Pheight * scale;
474 Pdev = pageShift + Pdev * scale;
475
476 //deviation bar
477 dxf.writeLine( *dw,
478 DL_LineData(Pheight.x, Pheight.y, Pheight.z, Pdev.x, Pdev.y, Pdev.z),
479 GrayMaterial);
480
481 //labels
482
483 //Horizontal justification: 0 = Left , 1 = Center, 2 = Right
484 int hJustification = 0;
485 //Vertical justification: 0 = Baseline, 1 = Bottom, 2 = Middle, 3 = Top
486 int vJustification = 2;
487
488 if (step.deviation < 0.0)
489 {
490 //deviation label on the left
491 Pdev.x -= 2.0*c_textMargin_mm;
492 //opposite for the height label
493 Pheight.x += c_textMargin_mm;
494 hJustification = 2; //Right
495 }
496 else
497 {
498 //deviation label on the right
499 Pdev.x += 2.0*c_textMargin_mm;
500 //opposite for the height label
501 Pheight.x -= c_textMargin_mm;
502 hJustification = 0; //Left
503 }
504
505 QString devText = QString::number(polySteps[i].deviation * params.devLabelMultCoef,'f',params.precision);
506 dxf.writeText( *dw,
507 DL_TextData(Pdev.x, Pdev.y, Pdev.z, Pdev.x, Pdev.y, Pdev.z, c_textHeight_mm, 1.0, 0, hJustification, vJustification, qPrintable(devText), "STANDARD", 0.0), //DGM: warning, toStdString doesn't preserve "local" characters
508 DefaultMaterial);
509
510 QString heightText = QString::number(polySteps[i].height,'f',params.precision);
511 dxf.writeText( *dw,
512 DL_TextData(Pheight.x, Pheight.y, Pheight.z, Pheight.x, Pheight.y, Pheight.z, c_textHeight_mm, 1.0, 0, 2 - hJustification, vJustification, qPrintable(heightText), "STANDARD", 0.0),
513 GrayMaterial);
514
515 lastStep = i;
516 }
517 }
518 }
519 }
520
521 dw->sectionEnd();
522
523 }
524
525 //Writing the Objects Section
526 dxf.writeObjects(*dw);
527 dxf.writeObjectsEnd(*dw);
528
529 //Ending and Closing the File
530 dw->dxfEOF();
531 dw->close();
532 delete dw;
533 dw = nullptr;
534
535 return true;
536
537 #else
538 return false;
539 #endif
540 }
541
542 struct HorizStepData
543 {
544 double angle_rad;
545 double deviation;
546 };
547
548 bool DxfProfilesExporter::SaveHorizontalProfiles( const QSharedPointer<DistanceMapGenerationTool::Map>& map,
549 ccPolyline* profile,
550 QString filename,
551 unsigned heightStepCount,
552 double heightShift,
553 double angularStep_rad,
554 double radToUnitConvFactor,
555 QString angleUnit,
556 const Parameters& params,
557 ccMainAppInterface* app/*=nullptr*/)
558 {
559 #ifdef CC_DXF_SUPPORT
560 assert(c_pageMargin_mm < c_profileMargin_mm);
561 assert(2.0*c_profileMargin_mm < std::min(c_pageWidth_mm, c_pageHeight_mm));
562
563 if (!map || !profile || heightStepCount == 0 || angularStep_rad <= 0)
564 {
565 //invalid parameters
566 return false;
567 }
568
569 //Theoretical profile bounding box
570 CCVector3 profileBBMin;
571 CCVector3 profileBBMax;
572 profile->getAssociatedCloud()->getBoundingBox(profileBBMin, profileBBMax);
573 //Mix with the map's boundaries along 'Y'
574 double yMin = std::max( map->yMin + 0.5 * map->xStep, //central height of first row
575 static_cast<double>(profileBBMin.y) + heightShift);
576 double yMax = std::min( map->yMin + (static_cast<double>(map->ySteps) - 0.5) * map->yStep, //central height of last row
577 static_cast<double>(profileBBMax.y) + heightShift);
578 const double ySpan = yMax - yMin;
579
580 //For the 'X' dimension, it's easier to stick with the th. profile
581 // const double xMin = profileBBMin.x;
582 const double xMax = profileBBMax.x;
583 //shortcut for clarity
584 const double& maxRadius = xMax;
585
586 if (ySpan == 0.0)
587 {
588 if (app)
589 app->dispToConsole(QString("Internal error: null profile?!"),ccMainAppInterface::ERR_CONSOLE_MESSAGE);
590 return false;
591 }
592
593 DL_Dxf dxf;
594 #ifdef _WIN32
595 DL_WriterA* dw = dxf.out(filename.toStdWString(), DL_VERSION_R12);
596 #else
597 DL_WriterA* dw = dxf.out(filename.toStdString(), DL_VERSION_R12);
598 #endif
599 if (!dw)
600 {
601 if (app)
602 app->dispToConsole(QString("Failed to open '%1' file for writing!").arg(filename),ccMainAppInterface::ERR_CONSOLE_MESSAGE);
603 return false;
604 }
605
606 //write header
607 dxf.writeHeader(*dw);
608
609 //add dimensions
610 dw->dxfString(9, "$INSBASE");
611 dw->dxfReal(10,0.0);
612 dw->dxfReal(20,0.0);
613 dw->dxfReal(30,0.0);
614 dw->dxfString(9, "$EXTMIN");
615 dw->dxfReal(10,0.0);
616 dw->dxfReal(20,0.0);
617 dw->dxfReal(30,0.0);
618 dw->dxfString(9, "$EXTMAX");
619 dw->dxfReal(10,c_pageWidth_mm);
620 dw->dxfReal(20,c_pageHeight_mm);
621 dw->dxfReal(30,0.0);
622 dw->dxfString(9, "$LIMMIN");
623 dw->dxfReal(10,0.0);
624 dw->dxfReal(20,0.0);
625 dw->dxfString(9, "$LIMMAX");
626 dw->dxfReal(10,c_pageWidth_mm);
627 dw->dxfReal(20,c_pageHeight_mm);
628
629 //close header
630 dw->sectionEnd();
631
632 //Opening the Tables Section
633 dw->sectionTables();
634 //Writing the Viewports
635 dxf.writeVPort(*dw);
636
637 //Writing the Linetypes (all by default)
638 {
639 dw->tableLinetypes(3);
640 dxf.writeLinetype(*dw, DL_LinetypeData("BYBLOCK", "BYBLOCK", 0, 0, 0.0));
641 dxf.writeLinetype(*dw, DL_LinetypeData("BYLAYER", "BYLAYER", 0, 0, 0.0));
642 dxf.writeLinetype(*dw, DL_LinetypeData("CONTINUOUS", "Continuous", 0, 0, 0.0));
643 dw->tableEnd();
644 }
645
646 //Writing the Layers
647 dw->tableLayers(heightStepCount+2);
648 QStringList profileNames;
649 {
650 //default layer
651 dxf.writeLayer(*dw,
652 DL_LayerData("0", 0),
653 DL_Attributes(
654 std::string(""), // leave empty
655 DL_Codes::black, // default color
656 100, // default width (in 1/100 mm)
657 "CONTINUOUS", // default line style
658 1.0 // linetypeScale
659 ));
660
661 //legend layer
662 dxf.writeLayer(*dw,
663 DL_LayerData(LEGEND_LAYER, 0),
664 DL_Attributes(
665 std::string(""),
666 DL_Codes::black,
667 s_lineWidth,
668 "CONTINUOUS",
669 1.0));
670
671 //horiz. profile layers
672 for (unsigned i=0; i<heightStepCount; ++i)
673 {
674 //default profile name
675 QString layerName = QString(HORIZ_PROFILE_LAYER).arg(i + 1, 3, 10, QChar('0'));
676 //but we use the profile title if we have one!
677 //DGM: nope, as it may be longer than 31 characters (R14 limit)
678 //if (params.profileTitles.size() == 1)
679 //{
680 // //profile height
681 // double height = yMin + (static_cast<double>(i) / (heightStepCount-1)) * ySpan;
682 // layerName = QString(params.profileTitles[0]).arg(height,0,'f',params.precision);
683 // layerName.replace(QChar(' '),QChar('_'));
684 //}
685
686 profileNames << layerName;
687 dxf.writeLayer(*dw,
688 DL_LayerData(qPrintable(layerName), 0), //DGM: warning, toStdString doesn't preserve "local" characters
689 DL_Attributes(
690 std::string(""),
691 i == 0 ? DL_Codes::green : -DL_Codes::green, //invisible if negative!
692 s_lineWidth,
693 "CONTINUOUS",
694 1.0));
695 }
696 }
697 dw->tableEnd();
698
699 //Writing Various Other Tables
700 //dxf.writeStyle(*dw); //DXFLIB V2.5
701 dw->tableStyle(1);
702 dxf.writeStyle(*dw, DL_StyleData("Standard", 0, 0.0, 0.75, 0.0, 0, 2.5, "txt", "")); //DXFLIB V3.3
703 dw->tableEnd();
704
705 dxf.writeView(*dw);
706 dxf.writeUcs(*dw);
707
708 dw->tableAppid(1);
709 dw->tableAppidEntry(0x12);
710 dw->dxfString(2, "ACAD");
711 dw->dxfInt(70, 0);
712 dw->tableEnd();
713
714 //Writing Dimension Styles
715 dxf.writeDimStyle( *dw,
716 /*arrowSize*/1,
717 /*extensionLineExtension*/1,
718 /*extensionLineOffset*/1,
719 /*dimensionGap*/1,
720 /*dimensionTextSize*/1);
721
722 //Writing Block Records
723 dxf.writeBlockRecord(*dw);
724 dw->tableEnd();
725
726 //Ending the Tables Section
727 dw->sectionEnd();
728
729 //Writing the Blocks Section
730 {
731 dw->sectionBlocks();
732
733 dxf.writeBlock(*dw, DL_BlockData("*Model_Space", 0, 0.0, 0.0, 0.0));
734 dxf.writeEndBlock(*dw, "*Model_Space");
735
736 dxf.writeBlock(*dw, DL_BlockData("*Paper_Space", 0, 0.0, 0.0, 0.0));
737 dxf.writeEndBlock(*dw, "*Paper_Space");
738
739 dxf.writeBlock(*dw, DL_BlockData("*Paper_Space0", 0, 0.0, 0.0, 0.0));
740 dxf.writeEndBlock(*dw, "*Paper_Space0");
741
742 dw->sectionEnd();
743 }
744
745 //Writing the Entities Section
746 {
747 dw->sectionEntities();
748
749 //we make the profile fit in the middle of the page (21.0 x 29.7 cm)
750 double scale = std::min((c_pageWidth_mm - 2.0 * c_profileMargin_mm) / (2.0 * maxRadius),
751 (c_pageHeight_mm - 2.0 * c_profileMargin_mm) / (2.0 * maxRadius));
752
753 //min corner of profile area
754 //const double x0 = (c_pageWidth_mm - 2.0 * maxRadius*scale) / 2.0;
755 const double y0 = (c_pageHeight_mm - 2.0 * maxRadius*scale) / 2.0;
756 //center of profile area
757 const double xc = c_pageWidth_mm / 2.0;
758 const double yc = c_pageHeight_mm / 2.0;
759
760 //write legend
761 {
762 DL_Attributes DefaultLegendMaterial(LEGEND_LAYER, DL_Codes::bylayer, -1, "BYLAYER", 1.0);
763
764 //write page contour
765 {
766 dxf.writePolyline( *dw,
767 DL_PolylineData(4, 0, 0, 1),
768 DefaultLegendMaterial);
769
770 dxf.writeVertex(*dw, DL_VertexData( c_pageMargin_mm, c_pageMargin_mm, 0.0));
771 dxf.writeVertex(*dw, DL_VertexData( c_pageMargin_mm, c_pageHeight_mm-c_pageMargin_mm, 0.0));
772 dxf.writeVertex(*dw, DL_VertexData( c_pageWidth_mm-c_pageMargin_mm, c_pageHeight_mm-c_pageMargin_mm, 0.0));
773 dxf.writeVertex(*dw, DL_VertexData( c_pageWidth_mm-c_pageMargin_mm, c_pageMargin_mm, 0.0));
774
775 dxf.writePolylineEnd(*dw);
776 }
777
778 double xLegend = c_pageMargin_mm + 2.0*c_textHeight_mm;
779 double yLegend = c_pageMargin_mm + 2.0*c_textHeight_mm;
780 const double legendWidth_mm = 20.0;
781
782 //Y axis
783 double axisTip = maxRadius*scale + 5.0;
784 dxf.writeLine( *dw,
785 DL_LineData(xc, yc, 0.0, xc, yc-axisTip, 0.0),
786 DefaultLegendMaterial);
787
788 //Y axis tip as triangle
789 {
790 double axisTipSize = 3.0;
791
792 dxf.writePolyline( *dw,
793 DL_PolylineData(3, 0, 0, 1), //closed polyline!
794 DefaultLegendMaterial);
795 dxf.writeVertex(*dw, DL_VertexData(xc, yc - (axisTip + axisTipSize), 0.0));
796 dxf.writeVertex(*dw, DL_VertexData(xc - axisTipSize / 2.0, yc - axisTip, 0.0));
797 dxf.writeVertex(*dw, DL_VertexData(xc + axisTipSize / 2.0, yc - axisTip, 0.0));
798 dxf.writePolylineEnd(*dw);
799
800 dxf.writeText( *dw,
801 DL_TextData(xc, yc - (axisTip + 2.0*axisTipSize), 0.0, xc, yc - (axisTip + 2.0*axisTipSize), 0.0, c_textHeight_mm, 1.0, 0, 1, 3, "Y", "STANDARD", 0.0),
802 DefaultLegendMaterial);
803 }
804
805 //deviation magnification factor
806 QString magnifyStr = QString::number(params.devMagnifyCoef);
807 dxf.writeText( *dw,
808 DL_TextData(xLegend, yLegend, 0.0, xLegend, yLegend, 0.0, c_textHeight_mm, 1.0, 0, 0, 0, qPrintable(QString("Deviation magnification factor: ") + magnifyStr), "STANDARD", 0.0), //DGM: warning, toStdString doesn't preserve "local" characters
809 DefaultLegendMaterial);
810
811 //next line
812 yLegend += c_textHeight_mm*2.0;
813
814 //units
815 dxf.writeText( *dw,
816 DL_TextData(xLegend, yLegend, 0.0, xLegend, yLegend, 0.0, c_textHeight_mm, 1.0, 0, 0, 0, qPrintable(QString("Deviation units: ") + params.scaledDevUnits), "STANDARD", 0.0), //DGM: warning, toStdString doesn't preserve "local" characters
817 DefaultLegendMaterial);
818
819 //next line
820 yLegend += c_textHeight_mm*2.0;
821
822 //true profile line (red)
823 dxf.writeLine( *dw,
824 DL_LineData(xLegend, yLegend, 0, xLegend + legendWidth_mm, yLegend, 0.0),
825 DL_Attributes(LEGEND_LAYER, DL_Codes::green, -1, "BYLAYER", 1.0));
826
827 dxf.writeText( *dw,
828 DL_TextData(xLegend + legendWidth_mm + c_textMargin_mm, yLegend, 0.0, xLegend + legendWidth_mm + c_textMargin_mm, yLegend, 0.0, c_textHeight_mm, 1.0, 0, 0, 0, qPrintable(params.legendRealProfileTitle), "STANDARD", 0.0),
829 DefaultLegendMaterial);
830
831 //next line
832 yLegend += c_textHeight_mm*2.0;
833
834 //theoretical profile line (red)
835 dxf.writeLine( *dw,
836 DL_LineData(xLegend, yLegend, 0, xLegend + legendWidth_mm, yLegend, 0.0),
837 DL_Attributes(LEGEND_LAYER, DL_Codes::red, -1, "BYLAYER", 1.0));
838
839 dxf.writeText( *dw,
840 DL_TextData(xLegend + legendWidth_mm + c_textMargin_mm, yLegend, 0.0, xLegend + legendWidth_mm + c_textMargin_mm, yLegend, 0.0, c_textHeight_mm, 1.0, 0, 0, 0, qPrintable(params.legendTheoProfileTitle), "STANDARD", 0.0),
841 DefaultLegendMaterial);
842 }
843
844 //profile values (fixed size: one per angular step of the input grid)
845 std::vector<HorizStepData> polySteps;
846 try
847 {
848 polySteps.resize(map->xSteps);
849 }
850 catch (const std::bad_alloc&)
851 {
852 //not engouh memory
853 dw->dxfEOF();
854 dw->close();
855 delete dw;
856 return false;
857 }
858
859 //write horizontal profiles
860 for (unsigned heightStep = 0; heightStep < heightStepCount; ++heightStep)
861 {
862 //profile height
863 double height = yMin + (static_cast<double>(heightStep) / (heightStepCount-1)) * ySpan;
864
865 //corresponding index in map
866 if (height < map->yMin || height >= map->yMin + static_cast<double>(map->ySteps) * map->yStep)
867 {
868 assert(false); //we have computed yMin and yMax so that those values are totally included inside the map's boundaries...
869 continue;
870 }
871 unsigned jMap = static_cast<unsigned>((height - map->yMin) / map->yStep);
872 assert(jMap < map->ySteps);
873
874 //find corresponding radius
875 double currentRadius = 0.0;
876 {
877 bool found = false;
878 for (unsigned i = 1; i < profile->size(); ++i)
879 {
880 const CCVector3* A = profile->getPoint(i - 1);
881 const CCVector3* B = profile->getPoint(i);
882
883 double alpha = (height - A->y - heightShift) / (B->y - A->y);
884 if (alpha >= 0.0 && alpha <= 1.0)
885 {
886 //we deduce the right radius by linear interpolation
887 currentRadius = A->x + alpha * (B->x - A->x);
888 found = true;
889 break;
890 }
891 }
892
893 if (!found)
894 {
895 assert(false); //we have computed yMin and yMax so that 'height' is totally included inside the profile's boundaries...
896 continue;
897 }
898 }
899
900 const QString& currentLayer = profileNames[heightStep];
901 const DL_Attributes DefaultMaterial(qPrintable(currentLayer), DL_Codes::bylayer, -1, "BYLAYER", 1.0); //DGM: warning, toStdString doesn't preserve "local" characters
902 const DL_Attributes GrayMaterial (qPrintable(currentLayer), DL_Codes::l_gray , -1, "", 1.0);
903
904 //write layer title
905 if (params.profileTitles.size() == 1)
906 {
907 QString title = QString(params.profileTitles[0]).arg(height,0,'f',params.precision);
908
909 CCVector3d Ptop(xc, y0 + 2.0 * maxRadius * scale + c_profileMargin_mm / 2.0, 0.0);
910
911 dxf.writeText( *dw,
912 DL_TextData(Ptop.x, Ptop.y, Ptop.z, Ptop.x, Ptop.y, Ptop.z, c_textHeight_mm, 1.0, 0, 1, 0, qPrintable(title), "STANDARD", 0.0), //DGM: warning, toStdString doesn't preserve "local" characters
913 GrayMaterial);
914 }
915
916 //write theoretical profile (polyline = circle)
917 {
918 dxf.writeCircle(*dw,
919 DL_CircleData(xc, yc, 0.0, currentRadius*scale),
920 DL_Attributes(qPrintable(currentLayer), DL_Codes::red, -1, "BYLAYER", 1.0));
921 }
922
923 assert(polySteps.size() == map->xSteps);
924 {
925 const DistanceMapGenerationTool::MapCell* cell = &map->at(static_cast<size_t>(jMap) * map->xSteps);
926 for (unsigned iMap = 0; iMap < map->xSteps; ++iMap, ++cell)
927 {
928 HorizStepData step;
929 step.angle_rad = 2.0*M_PI * static_cast<double>(iMap) / map->xSteps;
930 step.deviation = cell->count ? cell->value : 0.0;
931 polySteps[iMap] = step;
932 }
933 }
934
935 //profile "direction"
936 double cwSign = map->counterclockwise ? -1.0 : 1.0;
937
938 CCVector3d pageShift(xc, yc, 0.0);
939
940 //write profile polyline
941 {
942 dxf.writePolyline( *dw,
943 DL_PolylineData(static_cast<int>(polySteps.size()), 0, 0, 1), //closed shape!
944 DefaultMaterial);
945
946 for (size_t i = 0; i < polySteps.size(); ++i)
947 {
948 const HorizStepData& step = polySteps[i];
949 double radius = currentRadius + step.deviation * params.devMagnifyCoef;
950 dxf.writeVertex(*dw, DL_VertexData( pageShift.x - (cwSign * radius * sin(step.angle_rad)) * scale,
951 pageShift.y - (radius * cos(step.angle_rad)) * scale,
952 0.0));
953 }
954
955 dxf.writePolylineEnd(*dw);
956 }
957
958 //write corresponding 'deviation bars' and labels
959 {
960 size_t lastStep = 0;
961 for (size_t i = 0; i < polySteps.size(); ++i)
962 {
963 const HorizStepData& step = polySteps[i];
964 bool displayIt = (i == 0/*|| i+1 == polySteps.size()*/); //warning: cycle
965 if (i != 0)
966 {
967 double dAngle = polySteps[i].angle_rad - polySteps[lastStep].angle_rad;
968 double next_dAngle = (i+1 == polySteps.size() ? polySteps[0].angle_rad + 2.0*M_PI : polySteps[i+1].angle_rad) - polySteps[lastStep].angle_rad;
969 if (dAngle >= angularStep_rad || (next_dAngle > angularStep_rad && std::abs(dAngle - angularStep_rad) < std::abs(next_dAngle - angularStep_rad)))
970 {
971 displayIt = true;
972 }
973 }
974
975 if (displayIt && i != 0) //we skip 0 as we always display a vertical axis!
976 {
977 CCVector3d relativePos( -cwSign * sin(step.angle_rad),
978 -cos(step.angle_rad),
979 0.0);
980 CCVector3d Pangle = relativePos * currentRadius;
981 CCVector3d Pdev = relativePos * (currentRadius + step.deviation * params.devMagnifyCoef);
982
983 //page scaling
984 Pangle = pageShift + Pangle * scale;
985 Pdev = pageShift + Pdev * scale;
986
987 //deviation bar
988 dxf.writeLine( *dw,
989 DL_LineData(Pangle.x, Pangle.y, Pangle.z, Pdev.x, Pdev.y, Pdev.z),
990 GrayMaterial);
991
992 //labels
993
994 //justification depends on the current angle
995 const double c_angleMargin_rad = 5.0 / 180.0 * M_PI; //margin: 5 degrees
996 const double c_margin = sin(c_angleMargin_rad);
997
998 //Horizontal justification: 0 = Left , 1 = Center, 2 = Right
999 int hJustification = 1;
1000 if (relativePos.x < -c_margin) //point on the left
1001 hJustification = 2; //Right
1002 else if (relativePos.x > c_margin) //point on the right
1003 hJustification = 0; //Left
1004
1005 //Vertical justification: 0 = Baseline, 1 = Bottom, 2 = Middle, 3 = Top
1006 int vJustification = 2;
1007 if (relativePos.y < -c_margin) //point in the lower part
1008 vJustification = 3; //Top
1009 else if (relativePos.y > c_margin) //point in the upper part
1010 vJustification = 1; //Bottom
1011
1012 int hJustificationDev = hJustification;
1013 int hJustificationAng = hJustification;
1014 int vJustificationDev = vJustification;
1015 int vJustificationAng = vJustification;
1016
1017 //negative deviation: profile is inside the circle
1018 // - deviation is displayed insde
1019 // - angle is displayed outside
1020 if (step.deviation < 0.0)
1021 {
1022 Pdev -= relativePos * 2.0*c_textMargin_mm;
1023 Pangle += relativePos * c_textMargin_mm;
1024 //invert dev. text justification
1025 if (hJustificationDev != 1)
1026 hJustificationDev = 2 - hJustificationDev; // 0 <-> 2
1027 if (vJustificationDev != 2)
1028 vJustificationDev = 4 - vJustificationDev; // 1 <-> 3
1029 }
1030 else
1031 {
1032 Pdev += relativePos * 2.0*c_textMargin_mm;
1033 Pangle -= relativePos * c_textMargin_mm;
1034 //invert ang. text justification
1035 if (hJustificationAng != 1)
1036 hJustificationAng = 2 - hJustificationAng; // 0 <-> 2
1037 if (vJustificationAng != 2)
1038 vJustificationAng = 4 - vJustificationAng; // 1 <-> 3
1039 }
1040
1041 QString devText = QString::number(polySteps[i].deviation * params.devLabelMultCoef, 'f', params.precision);
1042 dxf.writeText( *dw,
1043 DL_TextData(Pdev.x, Pdev.y, Pdev.z, Pdev.x, Pdev.y, Pdev.z, c_textHeight_mm, 1.0, 0, hJustificationDev, vJustificationDev, qPrintable(devText), "STANDARD", 0.0),
1044 DefaultMaterial);
1045
1046 QString angleText = QString::number(polySteps[i].angle_rad *radToUnitConvFactor, 'f', params.precision) + angleUnit;
1047 dxf.writeText( *dw,
1048 DL_TextData(Pangle.x, Pangle.y, Pangle.z, Pangle.x, Pangle.y, Pangle.z, c_textHeight_mm, 1.0, 0, hJustificationAng, vJustificationAng, qPrintable(angleText), "STANDARD", 0.0),
1049 GrayMaterial);
1050
1051 lastStep = i;
1052 }
1053 }
1054 }
1055 }
1056
1057 dw->sectionEnd();
1058
1059 }
1060
1061 //Writing the Objects Section
1062 dxf.writeObjects(*dw);
1063 dxf.writeObjectsEnd(*dw);
1064
1065 //Ending and Closing the File
1066 dw->dxfEOF();
1067 dw->close();
1068 delete dw;
1069 dw = nullptr;
1070
1071 return true;
1072
1073 #else
1074 return false;
1075 #endif
1076 }