Repository navigation
Expand file tree
/
Copy patharstl.cpp
More file actions
372 lines (339 loc) · 11.6 KB
/
Copy patharstl.cpp
File metadata and controls
372 lines (339 loc) · 11.6 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
/* arstl - Augmented Reality for 3D Printing with STL Files
* Copyright (c) 2011, Nirav Patel <nrp@eclecti.cc>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Hacked together from the aruco_test_board_gl example available under a
* BSD license from the ArUco library by Rafael Munoz-Salinas: rmsalinas@uco.es
*
* TODO: Clean up this file to move away from aruco_test_board_gl towards my
* coding style.
* Make the shader look more convincing.
* Add controls to change transparency and freeze board location.
*/
#include <iostream>
#include <getopt.h>
#include <fstream>
#include <sstream>
#include <GL/glew.h>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <aruco/aruco.h>
#include <aruco/boarddetector.h>
#include "common.h"
#include "stlparser.h"
using namespace cv;
using namespace aruco;
string TheInputVideo, TheIntrinsicFile, TheBoardConfigFile;
bool isIntrinsicFileYAML=false;
bool The3DInfoAvailable=false;
float TheMarkerSize=-1;
MarkerDetector MDetector;
BoardDetector BDetector;
VideoCapture TheVideoCapturer;
vector<Marker> TheMarkers;
BoardDetector TheBoardDetector;
pair<Board,float> TheBoardDetected; //the board and its probability
BoardConfiguration TheBoardConfig;
Mat TheInputImage, TheUndInputImage, TheResizedImage;
CameraParameters TheCameraParams;
Size TheGlWindowSize;
char *STLFile;
GLuint STLDisplayList;
GLuint program;
GLuint vertex;
GLuint fragment;
bool TheCaptureFlag=true;
void readArguments(int argc,char **argv);
void usage();
void vDrawScene();
void vIdle();
void vResize(GLsizei iWidth, GLsizei iHeight);
void vMouse(int b, int s, int x, int y);
static int load_shaders();
static GLuint loadSTL(const char *filename);
int main(int argc, char **argv)
{
try {
if (argc == 1) usage();
readArguments(argc, argv);
if (TheIntrinsicFile == "") {
cerr<<"-f or -y option required"<<endl;
return -1;
}
if (TheMarkerSize == -1) {
cerr<<"-s option required"<<endl;
return -1;
}
if (TheBoardConfigFile == "") {
cerr<<"The board configuration info must be provided (-b option)"<<endl;
return -1;
}
//read board configuration
TheBoardConfig.readFromFile(TheBoardConfigFile);
//Open video input source
if (TheInputVideo == "") //read from camera
TheVideoCapturer.open(0);
else
TheVideoCapturer.open(TheInputVideo);
if (!TheVideoCapturer.isOpened()) {
cerr<<"Could not open video"<<endl;
return -1;
}
//read first image
TheVideoCapturer>>TheInputImage;
//read camera paramters if passed
if (isIntrinsicFileYAML)
TheCameraParams.readFromXMLFile(TheIntrinsicFile);
else
TheCameraParams.readFromFile(TheIntrinsicFile);
TheCameraParams.resize(TheInputImage.size());
glutInit(&argc, argv);
glutInitWindowPosition(0, 0);
glutInitWindowSize(TheInputImage.size().width, TheInputImage.size().height);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutCreateWindow("arstl");
glutDisplayFunc(vDrawScene);
glutIdleFunc(vIdle);
glutReshapeFunc(vResize);
glutMouseFunc(vMouse);
glClearColor(0.0, 0.0, 0.0, 1.0);
glClearDepth(1.0);
GLfloat LightAmbient[] = { 0.9f, 0.9f, 0.9f, 1.0f };
GLfloat LightDiffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat LightPosition[] = { -10.0f, 10.0f, 10.0f, 1.0f };
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);
glEnable(GL_LIGHT1);
GLfloat material_diffuse[] = { 0.8, 0.8, 1.0, 1.0 };
GLfloat material_specular[] = { 0.5, 0.5, 0.8, 1.0 };
GLfloat material_shininess[] = { 75.0 };
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material_diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material_specular);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, material_shininess);
TheGlWindowSize=TheInputImage.size();
glewInit();
if (load_shaders()) {
fprintf(stderr, "Failed to load shaders\n");
return -1;
}
STLDisplayList = loadSTL(STLFile);
vResize(TheGlWindowSize.width,TheGlWindowSize.height);
glutMainLoop();
} catch (std::exception &ex) {
cout<<"Exception :"<<ex.what()<<endl;
}
}
static int load_shaders()
{
char *vbuf, *fbuf;
int len = 0;
vbuf = load_file("arstl.vert", &len);
if (vbuf) {
fbuf = load_file("arstl.frag", &len);
if (!fbuf) {
free(vbuf);
return 1;
}
} else {
return 1;
}
vertex = glCreateShader(GL_VERTEX_SHADER);
fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(vertex, 1, (const GLchar **)&vbuf, NULL);
glShaderSource(fragment, 1, (const GLchar **)&fbuf, NULL);
free(vbuf);
free(fbuf);
glCompileShader(vertex);
glCompileShader(fragment);
program = glCreateProgram();
glAttachShader(program, vertex);
glAttachShader(program, fragment);
glLinkProgram(program);
glUseProgram(program);
return 0;
}
static void drawSTL(float *faces, int numFaces)
{
glBegin(GL_TRIANGLES);
glEnable(GL_NORMALIZE);
for (int i = 0; i < numFaces; i++) {
// Switching y and z since OpenGL uses a Y-up coordinate system.
// Also convert millimeters to meters
glNormal3f(faces[i*12],faces[i*12+2],-faces[i*12+1]);
glVertex3f(faces[i*12+3]/1000.0,faces[i*12+5]/1000.0,-faces[i*12+4]/1000.0);
glVertex3f(faces[i*12+6]/1000.0,faces[i*12+8]/1000.0,-faces[i*12+7]/1000.0);
glVertex3f(faces[i*12+9]/1000.0,faces[i*12+11]/1000.0,-faces[i*12+10]/1000.0);
}
glEnd();
}
static GLuint loadSTL(const char *filename)
{
GLuint displayList = glGenLists(1);
int numFaces = 0;
float *faces = load_stl(filename, &numFaces);
if (faces) {
glNewList(displayList,GL_COMPILE);
drawSTL(faces, numFaces);
glEndList();
free(faces);
}
return displayList;
}
void vMouse(int b, int s, int x, int y)
{
if (b == GLUT_LEFT_BUTTON && s == GLUT_DOWN) {
TheCaptureFlag = !TheCaptureFlag;
}
}
void vDrawScene()
{
//prevent from going on until the image is initialized
if (TheResizedImage.rows == 0)
return;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
// draw image in the buffer
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, TheGlWindowSize.width, 0, TheGlWindowSize.height, -1.0, 1.0);
glViewport(0, 0, TheGlWindowSize.width , TheGlWindowSize.height);
glPixelZoom(1, -1);
glRasterPos3f(0, TheGlWindowSize.height - 0.5, -1.0);
glDrawPixels(TheGlWindowSize.width, TheGlWindowSize.height, GL_BGR, GL_UNSIGNED_BYTE, TheResizedImage.ptr(0));
glEnable(GL_DEPTH_TEST);
// Set the appropriate projection matrix so that rendering is done in an
// environment like the real camera (without distortion)
glMatrixMode(GL_PROJECTION);
double proj_matrix[16];
MarkerDetector::glGetProjectionMatrix(TheCameraParams, TheInputImage.size(), TheGlWindowSize, proj_matrix, 0.05, 10);
glLoadIdentity();
glLoadMatrixd(proj_matrix);
double modelview_matrix[16];
// If the board is detected with enough probability
if (TheBoardDetected.second > 0.3){
TheBoardDetected.first.glGetModelViewMatrix(modelview_matrix);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glLoadMatrixd(modelview_matrix);
glPushMatrix();
glCallList(STLDisplayList);
glPopMatrix();
}
glutSwapBuffers();
}
void vIdle()
{
if (TheCaptureFlag) {
// Capture image
TheVideoCapturer.grab();
TheVideoCapturer.retrieve(TheInputImage);
TheUndInputImage.create(TheInputImage.size(), CV_8UC3);
// Remove distortion in image
cv::undistort(TheInputImage,TheUndInputImage, TheCameraParams.CameraMatrix, TheCameraParams.Distorsion);
// Detect markers
MDetector.detect(TheUndInputImage, TheMarkers, TheCameraParams.CameraMatrix,Mat(), TheMarkerSize);
// Detect board
TheBoardDetected.second = TheBoardDetector.detect(TheMarkers, TheBoardConfig, TheBoardDetected.first, TheCameraParams,TheMarkerSize);
// Resize the image to the size of the GL window
cv::resize(TheUndInputImage, TheResizedImage, TheGlWindowSize);
}
glutPostRedisplay();
}
void vResize(GLsizei iWidth, GLsizei iHeight)
{
TheGlWindowSize = Size(iWidth, iHeight);
// Not all sizes are allowed. OpenCV images have padding at the end of
// each line in these that are not aligned to 4 bytes
if (iWidth*3%4 != 0) {
iWidth += iWidth*3%4;
vResize(iWidth, TheGlWindowSize.height);
} else {
//resize the image to the size of the GL window
if (TheUndInputImage.rows != 0)
cv::resize(TheUndInputImage, TheResizedImage, TheGlWindowSize);
}
}
void usage()
{
cout<<"arstl is an Augmented Reality STL viewer\nUsage:\n";
cout<<" arstl -f camera.yml -b board.abc -s 0.035 file.stl"<<endl;
cout<<" -i <video.avi>: specifies a input video file. If not, images from camera are captured"<<endl;
cout<<" -f <file.int>: if you have calibrated your camera, pass calibration information here so as to be able to get 3D marker info"<<endl;
cout<<" -y <file.yml>: if you have calibrated your camera in yml format as provided by the calibration.cpp aplication in OpenCv >= 2.2"<<endl;
cout<<" -b <boardConfiguration.abc>: file with the board configuration"<<endl;
cout<<" -s <size>: size of the marker's sides (expressed in meters!)"<<endl;
}
void readArguments (int argc, char **argv)
{
static const char short_options[] = "hi:f:s:b:y:";
static const struct option long_options[] =
{
{ "help", no_argument, NULL, 'h' },
{ "input", required_argument, NULL, 'i' },
{ "intFile", required_argument, NULL, 'f' },
{ "YAMLFile", required_argument, NULL, 'y' },
{ "size", required_argument, NULL, 's' },
{ "boardFile", required_argument, NULL, 'b' },
{ 0, 0, 0, 0 }
};
while (1) {
int index;
int c;
c = getopt_long(argc, argv, short_options, long_options, &index);
if (-1 == c)
break;
switch (c) {
case 0:
break;
case 'h':
usage();
exit(EXIT_SUCCESS);
break;
case 'i':
TheInputVideo = optarg;
break;
case 'f':
TheIntrinsicFile = optarg;
isIntrinsicFileYAML = false;
break;
case 'y':
TheIntrinsicFile = optarg;
isIntrinsicFileYAML = true;
break;
case 's':
TheMarkerSize = atof(optarg);
break;
case 'b':
TheBoardConfigFile = optarg;
break;
default:
usage();
exit(EXIT_FAILURE);
};
}
if (optind >= argc) {
fprintf(stderr, "Error: STL filename required\n");
usage();
exit(EXIT_FAILURE);
} else {
STLFile = argv[optind];
}
}