-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
213 lines (188 loc) · 7.91 KB
/
main.cpp
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
#include <iostream>
#include "stl_header.h"
#include <string>
#include "include/tinynurbs/tinynurbs.h"
#include "simple_example.h"
#include "intersect/MeshIntersect.h"
#include "curvefitting/CurveFitting.h"
#include "nurbs/tests/catch.hpp"
#include "gcode/Printer.h"
#include "slice/Slice.h"
#include "simple_example.h"
#include "operations/Analysis.h"
#include "datastruct/HalfEdge.h"
//
// Created by simar on 6/12/2023.
//
void printTriangles(const std::string filePath) {
try{
stl_reader::StlMesh<float, unsigned int> mesh (filePath);
std::cout<< "The number of triangles : " << mesh.num_tris() <<std::endl;
for(size_t itri = 0; itri < mesh.num_tris(); ++itri) {
std::cout << "coordinates of triangle " << itri << ": ";
for(size_t icorner = 0; icorner < 3; ++icorner) {
const float* c = mesh.tri_corner_coords (itri, icorner);
// or alternatively:
// float* c = mesh.vrt_coords (mesh.tri_corner_ind (itri, icorner));
std::cout << "(" << c[0] << ", " << c[1] << ", " << c[2] << ") ";
}
std::cout << std::endl;
float* n = const_cast<float *>(mesh.tri_normal(itri));
std::cout << "normal of triangle " << itri << ": "
<< "(" << n[0] << ", " << n[1] << ", " << n[2] << ")\n";
}
}
catch (std::exception& e) {
std::cout << e.what() << std::endl;
}
}
void testNurbs(){
tinynurbs::Surface3f srf;
srf.degree_u = 1;
srf.degree_v = 1;
srf.knots_u = {0, 0, 1, 1};
srf.knots_v = {0, 0, 1, 1};
// 2x2 grid (tinynurbs::array2) of control points
srf.control_points = {2, 2,
{glm::vec3(-1, 0, 1), glm::vec3(-1, 0, -1),
glm::vec3(1, 0, 1), glm::vec3(1, 0, -1)
}
};
std::cout<< "Output" << std::to_string(surfaceNormal(srf,0.1f,0.1f)[0])<<std::endl;
//tinynurbs::surfaceSaveOBJ(R"(C:\Code\SculptPlane\surface.obj)", srf);
}
void generateGCode(std::vector<MeshIntersect::intersection> intersections) {
std::ofstream file(R"(C:\Code\SculptPlane\test_mp_2.obj)");
if (!file.is_open()) {
std::cerr << "Failed to open output.gcode for writing." << std::endl;
return;
}
// G-code header and initial setup
file << "G28 ; Home all axes" << std::endl;
file << "G21 ; Set units to millimeters" << std::endl;
file << "G90 ; Use absolute coordinates" << std::endl;
// Traverse the intersections
for (int i = 0; i < intersections.size(); i++) {
std::cout << "Iteration" << std::endl;
std::cout << intersections[i];
file << "G0 X" << intersections[i].intersectionPoint.values[0] + 150.0f
<< " Y" << intersections[i].intersectionPoint.values[1] + 150.0f
<< " Z" << intersections[i].intersectionPoint.values[2] << std::endl;
}
// G-code footer and ending commands
file << "G0 Z10 ; Move the tool head up" << std::endl;
file << "M84 ; Disable motors" << std::endl;
file.close();
}
int main(){
// Printer printer;
// printer.setXSize(350);
// printer.setYSize(350);
// printer.setZSize(350);
// printer.setNozzleSize(0.4);
// printer.setMaxLayerHeight(0.2);
// printer.setMinLayerHeight(0.1);
// printer.setFilamentTemp(200);
// printer.setBedTemp(60);
// printer.setMinLayerWidth(0.4);
// printer.setMaxLayerWidth(0.45);
//
// Slice slice;
// slice.beginSlice(R"(C:\code\SP_NURBS_RTX\test_mp_2.obj)", printer);
//
// std::cout << "Welcome to Sculpt Path" << std::endl;
std::string filePath = R"(C:\Code\SculptPlane\test_mp_2.obj)";
// std::cout<<"Loading file : "<<filePath <<std::endl;
// testNurbs();
// //printTriangles(filePath);
// //testMesh(filePath);
MeshIntersect meshIntersect;
meshIntersect.loadMesh(filePath);
// //NURBSObj();
// //meshIntersect.print_triangles();
// meshIntersect.getMinMax(true);
// std::vector<Vec3> intPoints = meshIntersect.planeIntersect(1.5f, true);
// CurveFitting curveFitting;
// tinynurbs::Curve<float> curv = curveFitting.fitCurve(intPoints);
// for (int i = 0; i < curv.control_points.size(); ++i) {
// std::cout << "Control point number : " << std::to_string(i)<< std::endl;
// std::cout << std::to_string(curv.control_points[i][0]) << std::endl;
// std::cout << std::to_string(curv.control_points[i][1]) << std::endl;
// std::cout << std::to_string(curv.control_points[i][2]) << std::endl;
// }
// std::cout << "Constructed curve knots : " << std::to_string(curv.knots.size()) << std::endl;
// for (int i = 0; i < curv.knots.size(); ++i) {
// std::cout << "Knot number : " << std::to_string(i)<< std::endl;
// std::cout << "Knot value : " <<std::to_string(curv.knots[i]) << std::endl;
// }
auto ray = Ray {
Vec3(0., 0.,0.3), // Ray origin
Vec3(0., 0., 1.), // Ray direction
0., // Minimum intersection distance
50 // Maytxfximum intersection distance
};
Analysis analysis;
analysis.performAnalysis(meshIntersect);
HalfEdge he;
he.loadMesh(meshIntersect.filePath);
he.generateHalfEdge();
he.walkMesh();
//meshIntersect.perform_intersect(ray);
//meshIntersect.getMinMax(true);
//std::vector<std::vector<Vec3>> grid = meshIntersect.generateLinOvercastRayField(0.5f);
//std::vector<MeshIntersect::intersection> intersectList = meshIntersect.gridPlaneIntersectMollerTrombore(grid);
//std::cout << intersectList.size() << " Size of intersect list" << std::endl;
//generateGCode(intersectList);
// meshIntersect.getMinMax(true);
// std::cout << "Welcome to Sculpt Path" << std::endl;
// std::string filePath = R"(C:\Code\SculptPlane\test_mp_2.obj)";
// std::cout<<"Loading file : "<<filePath <<std::endl;
// testNurbs();
// //printTriangles(filePath);
// //testMesh(filePath);
// MeshIntersect meshIntersect;
// meshIntersect.loadMesh(filePath);
// //NURBSObj();
// //meshIntersect.print_triangles();
// std::vector<Vec3> intPoints = meshIntersect.planeIntersect(1.5f, true);
// CurveFitting curveFitting;
// tinynurbs::Curve<float> curv = curveFitting.fitCurve(intPoints);
// for (int i = 0; i < curv.control_points.size(); ++i) {
// std::cout << "Control point number : " << std::to_string(i)<< std::endl;
// std::cout << std::to_string(curv.control_points[i][0]) << std::endl;
// std::cout << std::to_string(curv.control_points[i][1]) << std::endl;
// std::cout << std::to_string(curv.control_points[i][2]) << std::endl;
// }
// std::cout << "Constructed curve knots : " << std::to_string(curv.knots.size()) << std::endl;
// for (int i = 0; i < curv.knots.size(); ++i) {
// std::cout << "Knot number : " << std::to_string(i)<< std::endl;
// std::cout << "Knot value : " <<std::to_string(curv.knots[i]) << std::endl;
// }
//
//
// testMesh(filePath, 5.0f, 5.0f);
//testMesh(filePath, -5.0f, 1.0f);
// testMesh(filePath, -5.0f, -1.0f);
// testMesh(filePath, -5.0f, 0.5f);
ray = Ray {
Vec3(0., 5.,-3.), // Ray origin
Vec3(0., 0., 1.), // Ray direction
0., // Minimum intersection distance
50. // Maytxfximum intersection distance
};
//meshIntersect.perform_intersect(ray);
ray = Ray {
Vec3(0., 0.,-3.), // Ray origin
Vec3(0., 0., 1.), // Ray direction
0., // Minimum intersection distance
50. // Maximum intersection distance
};
//meshIntersect.perform_intersect(ray);
ray = Ray {
Vec3(0., 2,0.3), // Ray origin
Vec3(0., 1., 0.), // Ray direction
0., // Minimum intersection distance
50. // Maximum intersection distance
};
//meshIntersect.perform_intersect(ray);
}