OGS
GeoLib::IO::TINInterface Class Reference

Detailed Description

Interface for reading and writing Triangulated Irregular Network (TIN) file formats.

Definition at line 28 of file TINInterface.h.

#include <TINInterface.h>

Static Public Member Functions

static GeoLib::SurfacereadTIN (std::string const &fname, GeoLib::PointVec &pnt_vec, std::vector< std::string > *errors=nullptr)
 
static void writeSurfaceAsTIN (GeoLib::Surface const &surface, std::string const &file_name)
 

Member Function Documentation

◆ readTIN()

GeoLib::Surface * GeoLib::IO::TINInterface::readTIN ( std::string const & fname,
GeoLib::PointVec & pnt_vec,
std::vector< std::string > * errors = nullptr )
static

Reads TIN file

Parameters
fnameTIN file name
pnt_veca point vector to which triangle vertices are added
errorsa vector of error messages
Returns
a pointer to a GeoLib::Surface object created from TIN data. nullptr is returned if it fails to read the file.

Definition at line 27 of file TINInterface.cpp.

30{
31 // open file
32 std::ifstream in(fname.c_str());
33 if (!in)
34 {
35 WARN("readTIN(): could not open stream from {:s}.", fname);
36 if (errors)
37 {
38 errors->push_back("readTINFile error opening stream from " + fname);
39 }
40 return nullptr;
41 }
42
43 auto* sfc = new GeoLib::Surface(pnt_vec.getVector());
44 std::size_t id;
48 std::string line;
49 while (std::getline(in, line).good())
50 {
51 // allow empty lines
52 if (line.empty())
53 {
54 continue;
55 }
56
57 // parse line
58 std::stringstream input(line);
59 // read id
60 if (!(input >> id))
61 {
62 in.close();
63 delete sfc;
64 return nullptr;
65 }
66 // read first point
67 if (!(input >> p0[0] >> p0[1] >> p0[2]))
68 {
69 ERR("Could not read coords of 1st point of triangle {:d}.", id);
70 if (errors)
71 {
72 errors->push_back(
73 std::string("readTIN error: ") +
74 std::string(
75 "Could not read coords of 1st point in triangle ") +
76 std::to_string(id));
77 }
78 in.close();
79 delete sfc;
80 return nullptr;
81 }
82 // read second point
83 if (!(input >> p1[0] >> p1[1] >> p1[2]))
84 {
85 ERR("Could not read coords of 2nd point of triangle {:d}.", id);
86 if (errors)
87 {
88 errors->push_back(
89 std::string("readTIN error: ") +
90 std::string(
91 "Could not read coords of 2nd point in triangle ") +
92 std::to_string(id));
93 }
94 in.close();
95 delete sfc;
96 return nullptr;
97 }
98 // read third point
99 if (!(input >> p2[0] >> p2[1] >> p2[2]))
100 {
101 ERR("Could not read coords of 3rd point of triangle {:d}.", id);
102 if (errors)
103 {
104 errors->push_back(
105 std::string("readTIN error: ") +
106 std::string(
107 "Could not read coords of 3rd point in triangle ") +
108 std::to_string(id));
109 }
110 in.close();
111 delete sfc;
112 return nullptr;
113 }
114
115 // check area of triangle
116 double const d_eps(std::numeric_limits<double>::epsilon());
117 if (MathLib::calcTriangleArea(p0, p1, p2) < d_eps)
118 {
119 ERR("readTIN: Triangle {:d} has zero area.", id);
120 if (errors)
121 {
122 errors->push_back(std::string("readTIN: Triangle ") +
123 std::to_string(id) +
124 std::string(" has zero area."));
125 }
126 delete sfc;
127 return nullptr;
128 }
129
130 // determine size pnt_vec to insert the correct ids
131 std::size_t const s(pnt_vec.getVector().size());
132
133 std::size_t const pnt_pos_0(
134 pnt_vec.push_back(new GeoLib::Point(p0, s)));
135 std::size_t const pnt_pos_1(
136 pnt_vec.push_back(new GeoLib::Point(p1, s + 1)));
137 std::size_t const pnt_pos_2(
138 pnt_vec.push_back(new GeoLib::Point(p2, s + 2)));
139 // create new Triangle
140 if (pnt_pos_0 != std::numeric_limits<std::size_t>::max() &&
141 pnt_pos_1 != std::numeric_limits<std::size_t>::max() &&
142 pnt_pos_2 != std::numeric_limits<std::size_t>::max())
143 {
144 sfc->addTriangle(pnt_pos_0, pnt_pos_1, pnt_pos_2);
145 }
146 }
147
148 if (sfc->getNumberOfTriangles() == 0)
149 {
150 WARN("readTIN(): No triangle found.", fname);
151 if (errors)
152 {
153 errors->push_back("readTIN error because of no triangle found");
154 }
155 delete sfc;
156 return nullptr;
157 }
158
159 return sfc;
160}
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
std::size_t push_back(Point *pnt)
Definition PointVec.cpp:133
A Surface is represented by Triangles. It consists of a reference to a vector of (pointers to) points...
Definition Surface.h:33
std::vector< T * > const & getVector() const
double calcTriangleArea(MathLib::Point3d const &a, MathLib::Point3d const &b, MathLib::Point3d const &c)

References MathLib::calcTriangleArea(), ERR(), GeoLib::TemplateVec< T >::getVector(), GeoLib::PointVec::push_back(), and WARN().

Referenced by main(), and FileIO::Legacy::readSurface().

◆ writeSurfaceAsTIN()

void GeoLib::IO::TINInterface::writeSurfaceAsTIN ( GeoLib::Surface const & surface,
std::string const & file_name )
static

Writes surface data into TIN file

Parameters
surfacesurface object
file_nameTIN file name

Definition at line 162 of file TINInterface.cpp.

164{
165 std::ofstream os(file_name.c_str());
166 if (!os)
167 {
168 WARN("writeSurfaceAsTIN(): could not open stream to {:s}.", file_name);
169 return;
170 }
171 os.precision(std::numeric_limits<double>::digits10);
172 const std::size_t n_tris(surface.getNumberOfTriangles());
173 for (std::size_t l(0); l < n_tris; l++)
174 {
175 GeoLib::Triangle const& tri(*(surface[l]));
176 os << l << " " << *(tri.getPoint(0)) << " " << *(tri.getPoint(1)) << " "
177 << *(tri.getPoint(2)) << "\n";
178 }
179 os.close();
180}
Class Triangle consists of a reference to a point vector and a vector that stores the indices in the ...
Definition Triangle.h:27

References GeoLib::Surface::getNumberOfTriangles(), GeoLib::Triangle::getPoint(), and WARN().

Referenced by main(), and FileIO::Legacy::writeTINSurfaces().


The documentation for this class was generated from the following files: