forked from zddhub/opensse
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrimeshview.cpp
368 lines (304 loc) · 8.16 KB
/
trimeshview.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*************************************************************************
* Copyright (c) 2014 Zhang Dongdong
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**************************************************************************/
/*
#-------------------------------------------------
# @Author: zdd
# @Email: [email protected]
#
# rstc.cc in Qt
#
# Thanks:
# Original by Tilke Judd
# Tweaks by Szymon Rusinkiewicz
#
# apparentridge.h
# Compute apparent ridges.
#
# Implements method of
# Judd, T., Durand, F, and Adelson, E.
# Apparent Ridges for Line Drawing,
# ACM Trans. Graphics (Proc. SIGGRAPH), vol. 26, no. 3, 2007.
#-------------------------------------------------
*/
#include "trimeshview.h"
#include <QFile>
#include <QTextStream>
//输出数据_测试用
static void write_to_file(const vector<float> &v, QString filename)
{
QFile file(filename);
file.open(QFile::WriteOnly);
QTextStream out(&file);
for(unsigned int i = 0; i < v.size(); i++)
{
out << v[i] - 1.0f << " ";
}
out <<"\r\n";
file.close();
}
TriMeshView::TriMeshView(QWidget *parent) :
QGLWidget(parent)
{
QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
this->setSizePolicy(sizePolicy);
this->setFocusPolicy(Qt::StrongFocus);//QWidget只有设置焦点才能进行按键响应
this->timer = new QTimer(this);
timer->start(1);
connect(this->timer, SIGNAL(timeout()), this, SLOT(autospin()));
resize(840, 720);
isCtrlPressed = false;
triMesh = NULL;
feature_size = 0.0f;
currcolor = vec(0.0, 0.0, 0.0);
sug_thresh = 0.1f;
rv_thresh = 0.1f;
ar_thresh = 0.1f;
closeAllDrawings();
}
TriMeshView::~TriMeshView()
{
if(triMesh)
{
delete triMesh;
triMesh = NULL;
}
}
void TriMeshView::autospin()
{
if(camera.autospin(xf))
{
updateGL();
}
}
void TriMeshView::closeAllDrawings()
{
//draw_base
isDrawEdges = false;
isDrawNormals = false;
isDrawPreview = false;
isDrawCurv1 = false;
isDrawCurv2 = false;
isDrawCurvColors = false;
isDrawNormalColors = false;
//draw_lines
isDrawBoundaries = false;
isDrawSilhouette = false;
isDrawOccludingContours = true;
isDrawSuggestiveContours = true;
isDrawIsophotes = false;
isDrawTopolines = false;
isDrawRidges = false;
isDrawValleys = false;
isDrawApparentRidges = false;
}
bool TriMeshView::readMesh(const char *filename, const char *xffilename)
{
if(triMesh)
{
delete triMesh;
triMesh = NULL;
curv_colors.clear();
normal_colors.clear();
}
triMesh = TriangleMesh::read(filename);
if(!triMesh)
{
cout<<"read file "<<filename<<" error."<<endl;
exit(-1);
}
triMesh->need_tstrips();
triMesh->need_bsphere();
triMesh->need_normals();
triMesh->need_curvatures();
triMesh->need_dcurv();
// write_to_file(triMesh->curv1, "normal_curv1.txt");
// write_to_file(triMesh->curv2, "normal_curv2.txt");
feature_size = triMesh->feature_size();
if(!xf.read(xffilename))
xf = xform::trans(0, 0, -3.5f / 0.7 * triMesh->bsphere.r) *
xform::trans(-triMesh->bsphere.center);
else {
xf = xform::trans(0, 0, -3.5f / 0.7 * triMesh->bsphere.r) *
xform::trans(-triMesh->bsphere.center) *xf;
}
camera.stopspin();
updateGL();
return true;
}
void TriMeshView::clearMesh()
{
if(triMesh) {
delete triMesh;
triMesh = NULL;
}
updateGL();
}
//------------------------protected function------------------------
void TriMeshView::resizeGL(int width, int height)
{
//设置opengl视口与QWidget窗口大小相同
glViewport( 0, 0, (GLint)width, (GLint)height );
}
// Clear the screen and reset OpenGL modes to something sane
void TriMeshView::cls()
{
glDisable(GL_DITHER);
glDisable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
glDisable(GL_NORMALIZE);
glDisable(GL_LIGHTING);
glDisable(GL_COLOR_MATERIAL);
glClearColor(1,1,1,0);
// if (color_style == COLOR_GRAY)
// glClearColor(0.8, 0.8, 0.8, 0);
glClearDepth(1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void TriMeshView::paintGL()
{
if(!triMesh)
{
cls();
return;
}
viewpos = inv(xf) * point(0,0,0);
camera.setupGL(xf * triMesh->bsphere.center, triMesh->bsphere.r);
cls();
// Transform and draw
glPushMatrix();
glMultMatrixd((double *)xf);
draw_mesh();
glPopMatrix();
}
//鼠标交互处理
Mouse::button btn = Mouse::NONE;
void TriMeshView::mousePressEvent(QMouseEvent *e)
{
if(!triMesh)
return;
int x = e->pos().x();
int y = e->pos().y();
if(e->button() == Qt::LeftButton)
{
if(isCtrlPressed)
btn = Mouse::LIGHT;
else
btn = Mouse::ROTATE;
}
else if(e->button() == Qt::RightButton)
{
if(isCtrlPressed)
btn = Mouse::LIGHT;
else
btn = Mouse::MOVEZ;
}
else if(e->button() == Qt::MidButton)
{
btn = Mouse::MOVEXY;
}
else
btn = Mouse::NONE;
//根据鼠标交互位置(x,y)重新放置摄像机
// camera.setupGL(xf*triMesh->bsphere.center, triMesh->bsphere.r);
camera.mouse(x, y, btn, xf*triMesh->bsphere.center, triMesh->bsphere.r, xf);
mouseMoveEvent(e);
}
void TriMeshView::mouseReleaseEvent(QMouseEvent * e)
{
if(!triMesh)
return;
int x = e->pos().x();
int y = e->pos().y();
btn = Mouse::NONE;
camera.mouse(x, y, btn, xf*triMesh->bsphere.center, triMesh->bsphere.r, xf);
}
void TriMeshView::mouseMoveEvent(QMouseEvent *e)
{
if(!triMesh)
return;
int x = e->pos().x();
int y = e->pos().y();
if(e->buttons() & Qt::LeftButton) //该函数中,e->button()总是返回Qt::NoButton.
{
btn = Mouse::ROTATE;
}
else if(e->buttons() & Qt::RightButton)
{
btn = Mouse::MOVEZ;
}
else if(e->buttons() & Qt::MidButton)
{
btn = Mouse::MOVEXY;
}
else
{
btn = Mouse::NONE;
}
camera.mouse(x, y, btn, xf*triMesh->bsphere.center, triMesh->bsphere.r, xf);
if(btn != Mouse::NONE)
updateGL();
}
void TriMeshView::wheelEvent(QWheelEvent *e)
{
if(!triMesh)
return;
int x = e->pos().x();
int y = e->pos().y();
if(e->orientation() == Qt::Vertical)
{
if (e->delta() > 0)
{
btn = Mouse::WHEELUP;
}
else
{
btn = Mouse::WHEELDOWN;
}
}
e->accept();
camera.mouse(x, y, btn, xf*triMesh->bsphere.center, triMesh->bsphere.r, xf);
//btn = Mouse::NONE;
updateGL();
}
void TriMeshView::keyPressEvent(QKeyEvent *e)
{
if(!triMesh)
return;
switch(e->key())
{
case Qt::Key_Control:
isCtrlPressed = true;
break;
case Qt::Key_N:
isDrawNormalColors = !isDrawNormalColors;
break;
case Qt::Key_E:
isDrawEdges = !isDrawEdges;
break;
case Qt::Key_L:
isDrawOccludingContours = !isDrawOccludingContours;
isDrawSuggestiveContours = !isDrawSuggestiveContours;
break;
default:
QGLWidget::keyPressEvent(e);
}
updateGL();
}
void TriMeshView::keyReleaseEvent(QKeyEvent * /*e*/)
{
isCtrlPressed = false;
}