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