-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVisionSystem.cpp
351 lines (306 loc) · 8.3 KB
/
VisionSystem.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
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
#include "VisionSystem.h"
VisionSystem::VisionSystem(std::string installPath)
:VST3D_RESULT(VST3D_RESULT_ERROR), captureMethod(2)
{
this->installPath = installPath;
initVisionSystem();
if (!isConnected())
throw std::bad_exception();
}
VisionSystem::~VisionSystem()
{
}
void VisionSystem::initVisionSystem()
{
int result = VST3D_Init(this->installPath.data(), true); // default path, start without software's window
if (result != VST3D_RESULT_OK)
{
printf("Could not Start Scanner software.\n");
VST3D_Exit();
}
result = VST3D_Connect(); // Self-check and connect
if (result != VST3D_RESULT_OK)
{
printf("Could not connect to Scanner.\n");
VST3D_Exit();
}
VST3D_SetCameraID(0); // check camera by ID
}
bool VisionSystem::scanOnce(std::vector<VST3D_PT> & VSTPoints)
{
int result = VST3D_Scan(); // Start scanning once
if (result != VST3D_RESULT_OK)
{
retryCon("VST3D_Scan", __LINE__, __FILE__);
return false;
}
int totalNum = 0;
result = VST3D_GetNumPoints(totalNum); // obtain number of point at once
if (result != VST3D_RESULT_OK)
{
retryCon("VST3D_GetNumPoints", __LINE__, __FILE__);
return false;
}
VST3D_PT *pPointClouds = nullptr;
result = VST3D_GetPointClouds(totalNum, &pPointClouds);
// Method 1
if (captureMethod == 1)
{
VST3D_PT * myPts = new VST3D_PT[totalNum];
if (myPts)
{
memcpy((void *)myPts, (void *)(pPointClouds), totalNum * sizeof(VST3D_PT));
VST3D_PT tmp;
for (int i = 0; i < totalNum; i++)
{
VST3D_PT &pt = myPts[i];
//float x, y, z, nx, ny, nz;
//float cr, cg, cb;
tmp.x = pt.x;
tmp.y = pt.y;
tmp.z = pt.z;
tmp.nx = pt.nx;
tmp.ny = pt.ny;
tmp.nz = pt.nz;
tmp.cr = (float)pt.cr / 255;
tmp.cg = (float)pt.cg / 255;
tmp.cb = (float)pt.cb / 255;
//file << x << " " << y << " " << z << " " << cr << " " << cg << " " << cb << " " << nx << " " << ny << " " << nz << endl;
VSTPoints.push_back(tmp);
}
delete[] myPts;
}
}
else if (captureMethod == 2)
{
// Method 2
VST3D_PT tmp;
// Iterate through the copy of all the acquired point clouds in disordered order
// to get the point cloud coordinates and other information, by indexing the way [0 1 2 ....]
for (int i = 0; i < totalNum; i++)
{
VST3D_PT *pt = nullptr;
VST3D_GetEachPointByIndex(i, &pt);
//float x, y, z, nx, ny, nz;
//float cr, cg, cb;
tmp.x = pt->x;
tmp.y = pt->y;
tmp.z = pt->z;
tmp.nx = pt->nx;
tmp.ny = pt->ny;
tmp.nz = pt->nz;
tmp.cr = (float)pt->cr / 255;
tmp.cg = (float)pt->cg / 255;
tmp.cb = (float)pt->cb / 255;
VSTPoints.push_back(tmp);
}
}
return true;
}
void VisionSystem::save2File(const std::vector<VST3D_PT> &VSTPoints, std::string filename)
{
std::ofstream of;
of.open(filename);
for (auto &p : VSTPoints)
{
of << p.x << " " << p.y << " " << p.z << " "
<< p.nx << " " << p.ny << " " << p.nz << " "
<< p.cr << " " << p.cg << " " << p.cb << "\n";
}
of.close();
}
void VisionSystem::transformPointcloud(const std::vector<VST3D_PT>& VSTPoints, Eigen::Matrix4f & m, std::vector<VST3D_PT>& new_VSTPoints, const cropSize_t & cropSize, bool ifCrop = false)
{
std::vector<VST3D_PT> optVSTPoints;
if (ifCrop)
{
cropPointCloud(cropSize, VSTPoints, optVSTPoints);
}
else
{
optVSTPoints = VSTPoints;
}
std::vector<Eigen::Vector4f> pointsEigen;
pointsEigen.resize(optVSTPoints.size());
for (auto &p : optVSTPoints)
{
pointsEigen.push_back(Eigen::Vector4f{p.x, p.y, p.z, 1});
}
new_VSTPoints.resize(optVSTPoints.size());
VST3D_PT tmp;
for (auto &p : pointsEigen)
{
tmp.x = (m * p)[0];
tmp.y = (m * p)[1];
tmp.z = (m * p)[2];
new_VSTPoints.push_back(tmp);
}
}
void VisionSystem::cropPointCloud(const cropSize_t & cropSize, const std::vector<VST3D_PT>& VSTPoints, std::vector<VST3D_PT>& new_VSTPoints)
{
new_VSTPoints.clear();
for (auto &p : VSTPoints)
{
if (p.x > cropSize.min_x && p.x < cropSize.max_x &&
p.y > cropSize.min_y && p.y < cropSize.max_y &&
p.z > cropSize.min_z && p.z < cropSize.max_z)
{
new_VSTPoints.push_back(p);
}
}
}
const cropSize_t VisionSystem::calBoundingBox(const std::vector<VST3D_PT>& VSTPoints)
{
cropSize_t tmpCropSize;
for (auto &p : VSTPoints)
{
if (p.x > tmpCropSize.max_x) tmpCropSize.max_x = p.x;
if (p.y > tmpCropSize.max_y) tmpCropSize.max_y = p.y;
if (p.z > tmpCropSize.max_z) tmpCropSize.max_z = p.z;
if (p.x < tmpCropSize.min_x) tmpCropSize.min_x = p.x;
if (p.y < tmpCropSize.min_y) tmpCropSize.min_y = p.y;
if (p.z < tmpCropSize.min_z) tmpCropSize.min_z = p.z;
}
return tmpCropSize;
}
const cropSize_t VisionSystem::calBoundingBox(const std::string pointsFilename)
{
std::vector<VST3D_PT> VSTPoints;
loadPointsFromFile(pointsFilename, VSTPoints);
// bounding box size
cropSize_t tmpCropSize;
for (auto &p : VSTPoints)
{
if (p.x > tmpCropSize.max_x) tmpCropSize.max_x = p.x;
if (p.y > tmpCropSize.max_y) tmpCropSize.max_y = p.y;
if (p.z > tmpCropSize.max_z) tmpCropSize.max_z = p.z;
if (p.x < tmpCropSize.min_x) tmpCropSize.min_x = p.x;
if (p.y < tmpCropSize.min_y) tmpCropSize.min_y = p.y;
if (p.z < tmpCropSize.min_z) tmpCropSize.min_z = p.z;
}
return tmpCropSize;
}
void VisionSystem::fittingCylidner(const std::string pointsFilename, Eigen::Vector3f & point, Eigen::Vector3f & axis)
{
//std::vector<VST3D_PT> VSTPoints;
//loadPointsFromFile(pointsFilename, VSTPoints);
Py_Initialize();
if (!Py_IsInitialized())
{
throw std::runtime_error("Python Initialization Failed");
return;
}
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");
std::cout << std::filesystem::current_path();
PyObject * module = PyImport_ImportModule("CylinderFitting");
if (!module)
{
PyErr_Print();
throw std::runtime_error("Module not found");
return;
}
PyObject * func = PyObject_GetAttrString(module, "CylinderFitting");
if (!func || !PyCallable_Check(func))
{
throw std::runtime_error("Func in Module not found");
return;
}
PyObject * args = PyTuple_New(1);
PyTuple_SetItem(args, 0, Py_BuildValue("s", "CylinderPoints.txt"));
PyObject * res = PyObject_CallObject(func, args);
point = Eigen::Vector3f::Zero();
axis = Eigen::Vector3f::Zero();
for (int i = 0; i < 6; ++i)
{
PyObject *pRet = PyList_GetItem(res, i);
float tmp = 0;
PyArg_Parse(pRet, "f", &tmp);
if (i < 3) point[i] = tmp;
else if (i >= 3) axis[i - 3] = tmp;
}
Py_Finalize();
}
Eigen::Matrix4f VisionSystem::generateRMatrixAlongAxis(Eigen::Vector3f point, Eigen::Vector3f axis, float angle)
{
Eigen::Matrix4f transform = Eigen::Matrix4f::Identity();
transform <<
1, 0, 0, -point[0],
0, 1, 0, -point[1],
0, 0, 1, -point[2],
0, 0, 0, 1;
auto t = Eigen::AngleAxis<float>(ToRadians(angle), axis.normalized());
Eigen::Matrix4f R4X4 = Eigen::Matrix4f::Identity();
R4X4.block<3, 3>(0, 0) = t.matrix();
return transform.inverse() * R4X4 * transform;
}
void VisionSystem::retryCon(const std::string errorStr, const int codeLine, const std::string codeFile)
{
printf("Error(%s) at line number %d in file %s\n", errorStr.data(), __LINE__, __FILE__);
int nreset = 0;
while (true)
{
// Delay of a few seconds before each reconnection, camera, raster hardware driver loading takes time
Sleep(6000);
int result = VST3D_Reset(this->installPath.data());
if (result != VST3D_RESULT_OK)
{
printf("Check cables connected to Scanner.\n");
}
else
{
break;
}
// Make 5 reconnect attempts, if the scanner does not connect properly, exit the software system.
if (nreset++ > 5)
{
VST3D_Exit();
}
}
}
void VisionSystem::disConnect()
{
VST3D_Exit();
}
float VisionSystem::ToRadians(float deg)
{
return (float)(deg * M_PI / 180.0f);
}
void VisionSystem::loadPointsFromFile(const std::string pointsFilename, std::vector<VST3D_PT>& VSTPoints)
{
// loading points from the specific file
std::ifstream ifile;
try
{
std::ifstream ifile(pointsFilename, std::ios::in);
}
catch (const std::exception&)
{
throw std::runtime_error("Could not open file");
return;
}
std::string line;
VST3D_PT VSTpoint;
while (std::getline(ifile, line))
{
std::stringstream s(line);
float tmp = 0.0, value[3] = { 0 };
int i = 0;
while (s >> tmp)
{
value[i] = tmp;
i++;
}
VSTpoint.x = value[0];
VSTpoint.y = value[1];
VSTpoint.z = value[2];
VSTPoints.push_back(VSTpoint);
}
}
bool VisionSystem::isConnected()
{
if (VST3D_RESULT != VST3D_RESULT_OK)
return false;
else
return true;
}