-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.cpp
More file actions
136 lines (116 loc) · 3.62 KB
/
Copy pathmain.cpp
File metadata and controls
136 lines (116 loc) · 3.62 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
#include <iostream>
#include <fstream>
#include "triangle.h"
using std::cout;
using std::endl;
int main()
{
// Step0: Read in raw points
std::ifstream fin("/media/psf/Home/Documents/MATLAB_codes/3D_boundary/pts.txt");
std::string ptline;
double x, y, z;
std::vector<cv::Point3f> pts;
while (getline(fin, ptline))
{
std::stringstream ss(ptline);
ss >> x >> y >> z;
pts.push_back(cv::Point3f(x,y,z));
}
// Step1: Project all points to plane X0Z and X0Y, find the boundary points
std::vector<Point> points;
Triangle::findBoundary(pts, points);
// Step1: I only want to find a convex hull, not an alpha shape, so I will set the radius of the chop
// sphere big enough to contain all tiangles
int ptNum = points.size();
std::vector<Triangle> triangles;
triangles.reserve(ptNum*(ptNum-1)*(ptNum-2) / 6);
float maxR = 0;
for (int i = 0; i < ptNum; ++i)
{
for (int j = i+1; j < ptNum; ++j)
{
for (int k = j+1; k < ptNum; ++k)
{
Triangle triangle(points[i], points[j], points[k]);
triangles.push_back(triangle);
if (triangle.radius_ > maxR)
{
maxR = triangle.radius_;
}
}
}
}
std::cout << "maxR is " << maxR << std::endl;
// Step2: for every triangle, use a sphere whose radius is maxR to touch it, inspect if there is other
// points in this sphere, if not, it is a boundary triangle
float sphereR = 0.0;
for (Triangle &triangle : triangles)
{
bool sphere1 = false; // sphere1 has other points
bool sphere2 = false; // sphere2 has other points
Point sphereCenter1 = triangle.computeSphereCenter(maxR, 1);
Point sphereCenter2 = triangle.computeSphereCenter(maxR, -1);
sphereR = (sphereCenter1 - triangle.p1_).norm();
for (const Point &pt : points)
{
if ((pt - sphereCenter1).norm() < sphereR && !triangle.passPoint(pt))
{
sphere1 = true;
break;
}
}
for (const Point &pt : points)
{
if ((pt - sphereCenter2).norm() < sphereR && !triangle.passPoint(pt))
{
sphere2 = true;
break;
}
}
if ((!sphere1) || (!sphere2))
{
triangle.isBoundary = true;
}
}
// Step4: Output the result
std::ofstream fout1("/media/psf/Home/Documents/MATLAB_codes/3D_boundary/points.txt");
for (const Point &pt : points)
{
fout1 << pt.x() << " " << pt.y() << " " << pt.z() << endl;
}
fout1.close();
std::ofstream fout("/media/psf/Home/Documents/MATLAB_codes/3D_boundary/pt1.txt");
int id1(0), id2(0), id3(0);
for (const Triangle &tri : triangles)
{
if (tri.isBoundary)
{
for (int i = 0; i < ptNum; ++i)
{
if (points[i] == tri.p1_)
{
id1 = i;
break;
}
}
for (int i = 0; i < ptNum; ++i)
{
if (points[i] == tri.p2_)
{
id2 = i;
break;
}
}
for (int i = 0; i < ptNum; ++i)
{
if (points[i] == tri.p3_)
{
id3 = i;
break;
}
}
fout << id1+1 << " " << id2+1 << " " << id3+1 << endl;
}
}
fout.close();
}