-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
485 lines (440 loc) · 17.4 KB
/
main.cpp
File metadata and controls
485 lines (440 loc) · 17.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#include <iostream>
#include <vector>
#include <dirent.h>
#include <errno.h>
#include <string>
#include <fstream>
#include <sstream>
#include <limits>
#include <cmath>
#include <iomanip> // For std::fixed and std::setprecision
#include <sstream> // For std::istringstream
#include "Utils.cpp"
#include "Point.h"
Utils utils;
/**
* @brief Lists all files in the point_sets directory.
*
* This function scans the point_sets directory and prints out the names of all files
* contained within, excluding the current (".") and parent ("..") directory entries.
* If the directory cannot be opened, an error message is printed to stderr.
*/
void listFiles();
/**
* @brief Retrieves a list of point files with valid headers and matching point counts from a directory.
*
* This function scans the 'point_sets' directory, checks each file for a proper extension,
* and validates the file format including the version, format, points count, and data type.
* It returns a list of filenames that meet all criteria. Each file's header is checked for
* specific criteria, and the actual point count is compared against the expected count.
* If any checks fail, the file is skipped. If the directory cannot be opened or if a file
* cannot be read, an error message is printed to stderr.
*
* @return std::vector<std::string> List of filenames with valid point file headers.
*/
std::vector<std::string> getSuitablePointFiles();
/**
* @brief Finds the closest and farthest point pairs in a collection of point files.
*
* Iterates over provided files, calculates distances between points, and tracks the minimum
* and maximum distances along with the corresponding point pairs.
*
* @param files A list of filenames with point data.
*/
void checkClosestAndFarthestPoints(const std::vector<std::string> &files);
/**
* @brief Identifies the corner points of the smallest cube that contains all points in a collection of files.
*
* Iterates over provided files, reads the point data, and finds the minimum and maximum x, y, and z values.
* The corner points of the smallest cube are then printed for each file.
*
* @param files A list of filenames with point data.
*/
void identifyCornerPoints(const std::vector<std::string>& files);
/**
* @brief Prompts the user for a sphere center and diameter, then finds points within the sphere in a collection of files.
*
* Iterates over provided files, reads the point data, and finds points within the specified sphere.
* The sphere center and diameter are prompted from the user.
*
* @param suitablePointFiles A list of filenames with point data.
*/
void specifySphereAndFindPoints(const std::vector<std::string>& suitablePointFiles);
/**
* @brief Calculates the average distance between all points in a collection of files.
*
* Iterates over provided files, reads the point data, and calculates the average distance between all points.
*
* @param suitablePointFiles A list of filenames with point data.
*/
void calculateAverageDistance(const std::vector<std::string>& suitablePointFiles);
bool _promptRepeatMenu()
{
char repeat;
std::cout << "\nWould you like to see the menu again? (y/n): ";
std::cin >> repeat;
return repeat == 'y' || repeat == 'Y';
}
int main()
{
int choice = -1;
std::vector<std::string> suitableFiles;
do
{
std::cout << "Menu:\n"
<< "0. List files present\n"
<< "1. Check if point files are suitable in format\n"
<< "2. Check the closest and farthest two points in each file\n"
<< "3. Identify corner points of the smallest cube for all points\n"
<< "4. Specify sphere and find points within sphere\n"
<< "5. Calculate average distance between points\n"
<< "9. Exit\n"
<< "Enter your choice: ";
std::cin >> choice;
switch (choice)
{
case 0:
listFiles();
break;
case 1:
suitableFiles = getSuitablePointFiles();
for (const std::string &filePath : suitableFiles)
{
std::cout << "Suitable file: " << filePath << std::endl;
}
break;
case 2:
checkClosestAndFarthestPoints(suitableFiles);
break;
case 3:
identifyCornerPoints(suitableFiles);
break;
case 4:
specifySphereAndFindPoints(suitableFiles);
break;
case 5:
calculateAverageDistance(suitableFiles);
break;
case 9:
std::cout << "Exiting the program." << std::endl;
return 0; // Exit the program immediately
default:
std::cout << "Invalid choice. Please try again." << std::endl;
}
} while (_promptRepeatMenu()); // This will repeat the menu if the user wants to
return 0;
}
void listFiles()
{
const char *directoryPath = "./point_sets";
DIR *dir = opendir(directoryPath);
if (dir == nullptr)
{
std::cerr << "Error opening directory: " << errno << std::endl;
return;
}
struct dirent *entry;
while ((entry = readdir(dir)) != nullptr)
{
// Skip the "." and ".." entries
if (entry->d_name[0] != '.')
{
std::cout << entry->d_name << std::endl;
}
}
closedir(dir);
}
std::vector<std::string> getSuitablePointFiles()
{
const char *directoryPath = "./point_sets";
std::vector<std::string> suitableFiles;
DIR *dir = opendir(directoryPath);
if (dir == nullptr)
{
std::cerr << "Error opening directory: " << errno << std::endl;
return suitableFiles;
}
dirent *entry;
while ((entry = readdir(dir)) != nullptr)
{
std::string filename(entry->d_name);
if (filename[0] != '.' && utils.checkFileExtension(filename))
{
std::ifstream file(directoryPath + std::string("/") + filename);
if (!file.is_open())
{
std::cerr << "Error opening file: " << filename << std::endl;
continue;
}
std::string line;
int lineCount = 0, pointCount = 0;
bool validHeader = true;
while (getline(file, line) && lineCount < 4)
{
switch (lineCount)
{
case 0:
if (!utils.checkVersion(line))
{
std::cerr << "Error in file " << filename << ": Invalid version format." << std::endl;
validHeader = false;
}
break;
case 1:
if (!utils.checkFormat(line))
{
std::cerr << "Error in file " << filename << ": Invalid format, should be 'x y z' or 'x y z r g b'." << std::endl;
validHeader = false;
}
break;
case 2:
if (!utils.checkPointsCount(line, pointCount))
{
std::cerr << "Error in file " << filename << ": Invalid points count." << std::endl;
validHeader = false;
}
break;
case 3:
if (!utils.checkData(line))
{
std::cerr << "Error in file " << filename << ": Data type must be 'ascii'." << std::endl;
validHeader = false;
}
break;
}
if (!validHeader)
{
break;
}
lineCount++;
}
if (!validHeader)
{
file.close();
continue;
}
// Check if the number of points matches the count specified in the header
std::string pointLine;
int actualPointsCount = 0;
while (getline(file, pointLine) || !file.eof())
{
if (file.fail() && !file.eof())
{
std::cerr << "Error reading file: " << filename << std::endl;
validHeader = false;
break;
}
if (!pointLine.empty() && !file.eof())
{
actualPointsCount++;
}
else if (file.eof())
{
// Handle the last line if it did not end with a newline
std::istringstream iss(pointLine);
std::string point;
if (iss >> point)
{ // Check if there's at least one value in the last line
actualPointsCount++;
}
break;
}
}
file.close();
if (actualPointsCount == pointCount)
{
suitableFiles.push_back(directoryPath + std::string("/") + filename);
}
}
else if (filename[0] != '.')
{
std::cerr << "Error: File " << filename << " does not have a .pt extension and will not be analyzed." << std::endl;
}
}
closedir(dir);
return suitableFiles;
}
void checkClosestAndFarthestPoints(const std::vector<std::string> &files)
{
double maxDistance = std::numeric_limits<double>::min();
double minDistance = std::numeric_limits<double>::max();
Point maxPointA, maxPointB, minPointA, minPointB;
for (const std::string &filename : files)
{
std::ifstream file(filename);
if (!file.is_open())
{
std::cerr << "Could not open file: " << filename << std::endl;
continue;
}
std::vector<Point> points;
std::string line;
while (getline(file, line))
{
std::istringstream iss(line);
Point p;
if (iss >> p.x >> p.y >> p.z)
{
points.push_back(p);
// Skip the rest of the line in case there are RGB values or other extra data
iss.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
file.close();
// Calculate distances between all pairs of points
for (size_t i = 0; i < points.size(); ++i)
{
for (size_t j = i + 1; j < points.size(); ++j)
{
double distance = points[i].distanceTo(points[j]);
if (distance < minDistance)
{
minDistance = distance;
minPointA = points[i];
minPointB = points[j];
}
if (distance > maxDistance)
{
maxDistance = distance;
maxPointA = points[i];
maxPointB = points[j];
}
}
}
}
// Output the results
std::cout << "Closest points: (" << minPointA.x << ", " << minPointA.y << ", " << minPointA.z
<< ") and (" << minPointB.x << ", " << minPointB.y << ", " << minPointB.z
<< ") with distance " << minDistance << std::endl;
std::cout << "Farthest points: (" << maxPointA.x << ", " << maxPointA.y << ", " << maxPointA.z
<< ") and (" << maxPointB.x << ", " << maxPointB.y << ", " << maxPointB.z
<< ") with distance " << maxDistance << std::endl;
}
void identifyCornerPoints(const std::vector<std::string>& files) {
for (const std::string& filename : files) {
std::ifstream file(filename);
if (!file.is_open()) {
std::cerr << "Could not open file: " << filename << std::endl;
continue;
}
Point minPoint{std::numeric_limits<double>::max(), std::numeric_limits<double>::max(), std::numeric_limits<double>::max()};
Point maxPoint{std::numeric_limits<double>::lowest(), std::numeric_limits<double>::lowest(), std::numeric_limits<double>::lowest()};
std::string line;
while (getline(file, line)) {
if (!utils.isHeaderLine(line)) { // Skip header lines
std::istringstream iss(line);
Point point;
if (iss >> point.x >> point.y >> point.z) {
minPoint.x = std::min(minPoint.x, point.x);
minPoint.y = std::min(minPoint.y, point.y);
minPoint.z = std::min(minPoint.z, point.z);
maxPoint.x = std::max(maxPoint.x, point.x);
maxPoint.y = std::max(maxPoint.y, point.y);
maxPoint.z = std::max(maxPoint.z, point.z);
}
}
}
file.close();
// Set the precision for floating-point values to three decimal places
std::cout << std::fixed << std::setprecision(3);
// Output the results for this file
std::cout << "File: " << filename << std::endl;
std::cout << "Smallest cube corner points:" << std::endl;
std::cout << "(" << minPoint.x << ", " << minPoint.y << ", " << minPoint.z << ")" << std::endl;
std::cout << "(" << maxPoint.x << ", " << minPoint.y << ", " << minPoint.z << ")" << std::endl;
std::cout << "(" << minPoint.x << ", " << maxPoint.y << ", " << minPoint.z << ")" << std::endl;
std::cout << "(" << maxPoint.x << ", " << maxPoint.y << ", " << minPoint.z << ")" << std::endl;
std::cout << "(" << minPoint.x << ", " << minPoint.y << ", " << maxPoint.z << ")" << std::endl;
std::cout << "(" << maxPoint.x << ", " << minPoint.y << ", " << maxPoint.z << ")" << std::endl;
std::cout << "(" << minPoint.x << ", " << maxPoint.y << ", " << maxPoint.z << ")" << std::endl;
std::cout << "(" << maxPoint.x << ", " << maxPoint.y << ", " << maxPoint.z << ")" << std::endl;
std::cout << std::endl;
// Reset the precision if needed elsewhere with default behavior
std::cout.unsetf(std::ios_base::fixed);
std::cout.precision(6);
}
}
void specifySphereAndFindPoints(const std::vector<std::string>& suitablePointFiles) {
Point sphereCenter;
double diameter, radius;
// Prompt user for sphere center and diameter
std::cout << "Enter the center of the sphere (x y z): ";
std::cin >> sphereCenter.x >> sphereCenter.y >> sphereCenter.z;
std::cout << "Enter the diameter of the sphere: ";
std::cin >> diameter;
radius = diameter / 2.0;
for (const std::string& filename : suitablePointFiles) {
std::ifstream file(filename);
if (!file.is_open()) {
std::cerr << "Could not open file: " << filename << std::endl;
continue;
}
std::string line;
std::vector<Point> pointsInsideSphere;
while (getline(file, line)) {
if (!utils.isHeaderLine(line)) { // Skip header lines
std::istringstream iss(line);
Point point;
if (iss >> point.x >> point.y >> point.z) {
// Check if the point is inside the sphere
if (point.distanceTo(sphereCenter) <= radius) {
pointsInsideSphere.push_back(point);
}
}
}
}
file.close();
// Print out the points inside the sphere for this file
std::cout << "File: " << filename << std::endl;
std::cout << "Points inside the sphere:" << std::endl;
for (const auto& point : pointsInsideSphere) {
std::cout << std::fixed << std::setprecision(3);
std::cout << "(" << point.x << ", " << point.y << ", " << point.z << ")" << std::endl;
}
std::cout << std::endl;
// Reset the precision if needed elsewhere with default behavior
std::cout.unsetf(std::ios_base::fixed);
std::cout.precision(6);
}
}
void calculateAverageDistance(const std::vector<std::string>& suitablePointFiles) {
for (const std::string& filename : suitablePointFiles) {
std::ifstream file(filename);
if (!file.is_open()) {
std::cerr << "Could not open file: " << filename << std::endl;
continue;
}
std::string line;
std::vector<Point> points;
while (getline(file, line)) {
if (!utils.isHeaderLine(line)) { // Skip header lines
std::istringstream iss(line);
Point point;
if (iss >> point.x >> point.y >> point.z) {
points.push_back(point);
}
}
}
file.close();
// Now calculate the average distance between all points
double totalDistance = 0;
int count = 0;
for (size_t i = 0; i < points.size(); ++i) {
for (size_t j = i + 1; j < points.size(); ++j) {
totalDistance += points[i].distanceTo(points[j]);
++count;
}
}
double averageDistance = 0;
if (count > 0) { // Avoid division by zero
averageDistance = totalDistance / count;
}
// Print out the average distance for this file
std::cout << "File: " << filename << std::endl;
std::cout << "Average distance between points: " << std::fixed << std::setprecision(3) << averageDistance << std::endl;
std::cout << std::endl;
// Reset the precision if needed elsewhere with default behavior
std::cout.unsetf(std::ios_base::fixed);
std::cout.precision(6);
}
}