ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
HeightProfileFilter.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #include "HeightProfileFilter.h"
9 
10 // qCC_db
11 #include <ecvPolyline.h>
12 
13 // Qt
14 #include <QFile>
15 
16 // Qt5/Qt6 Compatibility
17 #include <QtCompat.h>
18 
20  : FileIOFilter({"_Height profile Filter",
21  21.0f, // priority
22  QStringList(), "", QStringList(),
23  QStringList{"Height profile (*.csv)"}, Export}) {}
24 
26  bool& multiple,
27  bool& exclusive) const {
28  if (type == CV_TYPES::POLY_LINE) {
29  multiple = false;
30  exclusive = true;
31  return true;
32  }
33  return false;
34 }
35 
37  ccHObject* entity,
38  const QString& filename,
39  const SaveParameters& parameters) {
40  if (!entity || filename.isEmpty()) {
41  return CC_FERR_BAD_ARGUMENT;
42  }
43 
44  // get the polyline
45  if (!entity->isA(CV_TYPES::POLY_LINE)) {
47  }
48  ccPolyline* poly = static_cast<ccPolyline*>(entity);
49  unsigned vertCount = poly->size();
50  if (vertCount == 0) {
51  // invalid size
52  CVLog::Warning(QString("[Height profile] Polyline '%1' is empty")
53  .arg(poly->getName()));
54  return CC_FERR_NO_SAVE;
55  }
56 
57  // open ASCII file for writing
58  QFile file(filename);
59  if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
60  return CC_FERR_WRITING;
61  }
62 
63  QTextStream outFile(&file);
64  outFile.setRealNumberNotation(QTextStream::FixedNotation);
65  outFile.setRealNumberPrecision(
66  sizeof(PointCoordinateType) == 4 && !poly->isShifted() ? 8 : 12);
67  outFile << "Curvilinear abscissa; Z" << QtCompat::endl;
68 
69  // curvilinear abscissa
70  double s = 0;
71  const CCVector3* lastP = 0;
72  for (unsigned j = 0; j < vertCount; ++j) {
73  const CCVector3* P = poly->getPoint(j);
74  // update the curvilinear abscissa
75  if (lastP) {
76  s += (*P - *lastP).normd();
77  }
78  lastP = P;
79 
80  // convert to 'local' coordinate system
81  CCVector3d Pg = poly->toGlobal3d(*P);
82  outFile << s << "; " << Pg.z << QtCompat::endl;
83  }
84 
85  file.close();
86 
87  return CC_FERR_NO_ERROR;
88 }
float PointCoordinateType
Type of the coordinates of a (N-D) point.
Definition: CVTypes.h:16
int64_t CV_CLASS_ENUM
Type of object type flags (64 bits)
Definition: CVTypes.h:97
std::string filename
char type
CC_FILE_ERROR
Typical I/O filter errors.
Definition: FileIOFilter.h:20
@ CC_FERR_WRITING
Definition: FileIOFilter.h:25
@ CC_FERR_BAD_ARGUMENT
Definition: FileIOFilter.h:22
@ CC_FERR_NO_SAVE
Definition: FileIOFilter.h:27
@ CC_FERR_NO_ERROR
Definition: FileIOFilter.h:21
@ CC_FERR_BAD_ENTITY_TYPE
Definition: FileIOFilter.h:29
static bool Warning(const char *format,...)
Prints out a formatted warning message in console.
Definition: CVLog.cpp:133
Generic file I/O filter.
Definition: FileIOFilter.h:46
virtual bool canSave(CV_CLASS_ENUM type, bool &multiple, bool &exclusive) const override
Returns whether this I/O filter can save the specified type of entity.
virtual CC_FILE_ERROR saveToFile(ccHObject *entity, const QString &filename, const SaveParameters &parameters) override
Saves an entity (or a group of) to a file.
Type z
Definition: CVGeom.h:137
Hierarchical CLOUDVIEWER Object.
Definition: ecvHObject.h:25
virtual QString getName() const
Returns object name.
Definition: ecvObject.h:72
bool isA(CV_CLASS_ENUM type) const
Definition: ecvObject.h:131
Colored polyline.
Definition: ecvPolyline.h:24
CCVector3d toGlobal3d(const Vector3Tpl< T > &Plocal) const
Returns the point back-projected into the original coordinates system.
bool isShifted() const
Returns whether the cloud is shifted or not.
unsigned size() const override
Returns the number of points.
const CCVector3 * getPoint(unsigned index) const override
Returns the ith point.
@ POLY_LINE
Definition: CVTypes.h:112
QTextStream & endl(QTextStream &stream)
Definition: QtCompat.h:718
Generic saving parameters.
Definition: FileIOFilter.h:84