-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMeshFace.cpp
589 lines (509 loc) · 14.4 KB
/
MeshFace.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
// MeshFace.cpp: implementation of the CMeshFace class.
// Copyright (c) 2009, Dan Heeks
// This program is released under the BSD license. See the file COPYING for details.
#include "stdafx.h"
#include "MeshFace.h"
#include "Mesh.h"
#include "interface/Tool.h"
#include "Plane.h"
#include "Line.h"
#include "interface/GripperTypes.h"
#include "interface/GripData.h"
CMeshFace::CMeshFace()
{
}
CMeshFace::~CMeshFace()
{
}
const wxBitmap &CMeshFace::GetIcon()
{
static wxBitmap* icon = NULL;
if(icon == NULL)icon = new wxBitmap(wxImage(theApp.GetResFolder() + _T("/icons/meshface.png")));
return *icon;
}
class RecalcGoodCentre:public Tool{
public:
CMeshFace* m_f;
// Tool's virtual functions
const wxChar* GetTitle(){return _("Recalculate a Good Centre Point");}
void Run(){m_f->RecalculateAGoodCentre();}
wxString BitmapPath(){ return _T("recalccentre");}
};
static RecalcGoodCentre recalculate_good_centre;
void CMeshFace::GetTools(std::list<Tool*>* t_list, const wxPoint* p)
{
recalculate_good_centre.m_f = this;
t_list->push_back(&recalculate_good_centre);
CMeshChild::GetTools(t_list, p);
}
HeeksObj *CMeshFace::MakeACopy(void)const
{
return new CMeshFace(*this);
}
#define EVERY_OTHER
void CMeshFace::split_bezier_triangle(int level, int edge_to_split, const Point& vt0, const Point& vt1, const Point& vt2, const Point& c, const Point& vt01, const Point& vt10, const Point& vt12, const Point& vt21, const Point& vt20, const Point& vt02, void(*call_back)(const Point& vt0, const Point& vt1, const Point& vt2, const Point& n0, const Point& n1, const Point& n2))
{
if(level > 0){
Point avt0;
Point avt1;
Point avt2;
Point avt01;
Point avt10;
Point avt12;
Point avt21;
Point avt20;
Point avt02;
if(edge_to_split == 0){
avt0 = vt1;
avt1 = vt2;
avt2 = vt0;
avt01 = vt12;
avt10 = vt21;
avt12 = vt20;
avt21 = vt02;
avt20 = vt01;
avt02 = vt10;
}
else if(edge_to_split == 1){
avt0 = vt2;
avt1 = vt0;
avt2 = vt1;
avt01 = vt20;
avt10 = vt02;
avt12 = vt01;
avt21 = vt10;
avt20 = vt12;
avt02 = vt21;
}
else{
avt0 = vt0;
avt1 = vt1;
avt2 = vt2;
avt01 = vt01;
avt10 = vt10;
avt12 = vt12;
avt21 = vt21;
avt20 = vt20;
avt02 = vt02;
}
Point nvt0[2];
Point nvt1[2];
Point nvt2[2];
Point nc[2];
Point nvt01[2];
Point nvt10[2];
Point nvt12[2];
Point nvt21[2];
Point nvt20[2];
Point nvt02[2];
// split into two sub triangles
for(int i = 0; i<2; i++){
nvt0[i] = avt0;
nvt1[i] = avt1;
nvt2[i] = avt2;
nc[i] = c;
nvt01[i] = avt01;
nvt10[i] = avt10;
nvt12[i] = avt12;
nvt21[i] = avt21;
nvt20[i] = avt20;
nvt02[i] = avt02;
if(i == 1){
nvt0[i]=(nvt02[i]+nvt0[i])/2;
nvt02[i]=(nvt20[i]+nvt02[i])/2;
nvt0[i]=(nvt02[i]+nvt0[i])/2;
nvt20[i]=(nvt2[i]+nvt20[i])/2;
nvt02[i]=(nvt20[i]+nvt02[i])/2;
nvt0[i]=(nvt02[i]+nvt0[i])/2;
nvt01[i]=(nc[i]+nvt01[i])/2;
nc[i]=(nvt21[i]+nc[i])/2;
nvt01[i]=(nc[i]+nvt01[i])/2;
nvt10[i]=(nvt12[i]+nvt10[i])/2;
}
else{
nvt2[i]=(nvt20[i]+nvt2[i])/2;
nvt20[i]=(nvt02[i]+nvt20[i])/2;
nvt2[i]=(nvt20[i]+nvt2[i])/2;
nvt02[i]=(nvt0[i]+nvt02[i])/2;
nvt20[i]=(nvt02[i]+nvt20[i])/2;
nvt2[i]=(nvt20[i]+nvt2[i])/2;
nvt21[i]=(nc[i]+nvt21[i])/2;
nc[i]=(nvt01[i]+nc[i])/2;
nvt21[i]=(nc[i]+nvt21[i])/2;
nvt12[i]=(nvt10[i]+nvt12[i])/2;
}
}
int new_edge_to_split[2] = {0, 1};
for(int i = 0; i<2; i++){
split_bezier_triangle(level - 1, new_edge_to_split[i], nvt0[i], nvt1[i], nvt2[i], nc[i], nvt01[i], nvt10[i], nvt12[i], nvt21[i], nvt20[i], nvt02[i], call_back);
}
}
else{
// render triangle
// calculate normal, to do - evaluate norm from surface
Point v0(vt0, vt1);
Point v1(vt0, vt2);
Point norm = (v0 ^ v1).norm();
(*call_back)(vt0, vt1, vt2, norm, norm, norm);
}
}
static void render_triangle_vertices2(const Point& vt0, const Point& vt1, const Point& vt2, const Point& n0, const Point& n1, const Point& n2){
glNormal3dv(n0.GetPtr());
glVertex3dv(vt0.GetPtr());
glNormal3dv(n1.GetPtr());
glVertex3dv(vt1.GetPtr());
glNormal3dv(n2.GetPtr());
glVertex3dv(vt2.GetPtr());
}
void CMeshFace::GetSmoothTriangles(void(*call_back)(const Point& vt0, const Point& vt1, const Point& vt2, const Point& n0, const Point& n1, const Point& n2)){
Point vt01 = m_edge_dir[0] ? m_e[0]->m_c[1].vertex() : m_e[0]->m_c[0].vertex();
Point vt10 = m_edge_dir[0] ? m_e[0]->m_c[0].vertex() : m_e[0]->m_c[1].vertex();
Point vt12 = m_edge_dir[1] ? m_e[1]->m_c[1].vertex() : m_e[1]->m_c[0].vertex();
Point vt21 = m_edge_dir[1] ? m_e[1]->m_c[0].vertex() : m_e[1]->m_c[1].vertex();
Point vt20 = m_edge_dir[2] ? m_e[2]->m_c[1].vertex() : m_e[2]->m_c[0].vertex();
Point vt02 = m_edge_dir[2] ? m_e[2]->m_c[0].vertex() : m_e[2]->m_c[1].vertex();
int edge_to_split;
// choose longest edge to split
Point vc0(m_v[0]->vertex(), m_v[1]->vertex());
Point vc1(m_v[1]->vertex(), m_v[2]->vertex());
Point vc2(m_v[2]->vertex(), m_v[0]->vertex());
if(vc1.magn() > vc0.magn()){
if(vc2.magn() > vc1.magn()){
edge_to_split = 2;
}
else{
edge_to_split = 1;
}
}
else{
if(vc2.magn() > vc0.magn()){
edge_to_split = 2;
}
else{
edge_to_split = 0;
}
}
split_bezier_triangle(theApp.m_num_facets_on_edge, edge_to_split, m_v[0]->vertex(), m_v[1]->vertex(), m_v[2]->vertex(), m_centre.vertex(), vt01, vt10, vt12, vt21, vt20, vt02, call_back);
}
void CMeshFace::MakeSureDisplayListExists(){
if(m_gl_list)return;
m_gl_list = glGenLists(1);
glNewList(m_gl_list, GL_COMPILE);
glShadeModel(GL_SMOOTH);
glBegin(GL_TRIANGLES);
GetSmoothTriangles(render_triangle_vertices2);
glEnd();
glShadeModel(GL_FLAT);
glEndList();
}
void CMeshFace::GetGripperPositions(std::list<GripData> *list, bool just_for_endof){
{
for(int i = 0; i<3; i++){
Point vt = m_v[i]->vertex();
list->push_back(GripData(GripperTypeStretch,vt.x,vt.y,vt.z,NULL, false, 1));
}
}
{
for(int i = 0; i<3; i++){
for(int j = 0; j<2; j++){
Point vt = m_e[i]->m_c[j].vertex();
list->push_back(GripData(GripperTypeStretch,vt.x,vt.y,vt.z,NULL, true));
}
}
}
{
Point vt = m_centre.vertex();
list->push_back(GripData(GripperTypeStretch,vt.x,vt.y,vt.z,NULL, true, 3));
}
}
double CMeshFace::angle_at_vertex(const CMeshVertex* v)const{
std::list<CMeshEdge*>::const_iterator It;
Point vc[2];
int i = 0;
for(int k = 0; k<3; k++){
CMeshEdge* edge = m_e[k];
if(edge->m_v[0] == v){
vc[i] = Point(v->vertex(), edge->m_v[1]->vertex());
i++;
}
else if(edge->m_v[1] == v){
vc[i] = Point(v->vertex(), edge->m_v[0]->vertex());
i++;
}
if(i==2)break;
}
if(i<2)return 0;
double dotp = vc[0].norm() * vc[1].norm();
double angle = acos(dotp);
return angle;
}
Point CMeshFace::direction_at_vertex(const CMeshVertex* v)const{
std::list<CMeshEdge*>::const_iterator It;
Point vc[2];
int i = 0;
for(int k = 0; k<3; k++){
CMeshEdge* edge = m_e[k];
if(edge->m_v[0] == v){
vc[i] = Point(v->vertex(), edge->m_v[1]->vertex());
i++;
}
else if(edge->m_v[1] == v){
vc[i] = Point(v->vertex(), edge->m_v[0]->vertex());
i++;
}
if(i==2)break;
}
if(i<2)return Point(0, 0 ,0);
return (vc[0] + vc[1]).norm();
}
bool CMeshFace::Stretch(const double *p, const double* shift, void* data){
// stretch the vertex at "p" by a vector "shift"
Point vp(p);
{
// Find which vertex it is
for(int i = 0; i<3; i++){
Point vt = m_v[i]->vertex();
if(vp == vt){
Point new_vertex = vp + Point(shift);
CMesh* mesh = (CMesh*)Owner();
mesh->ChangeVertex(m_v[i], new_vertex);
return false;
}
}
}
// check control points
{
for(int i = 0; i<3; i++){
for(int j = 0; j<2; j++){
Point vt = m_e[i]->m_c[j].vertex();
if(vp == vt){
Point new_vertex = vp + Point(shift);
m_e[i]->m_c[j].set_vertex(new_vertex);
m_e[i]->KillGLLists();
m_e[i]->InvalidateBothFacesDisplayLists();
return false;
}
}
}
}
// check centre point
{
Point vt = m_centre.vertex();
if(vp == vt){
Point new_vertex = vp + Point(shift);
m_centre.set_vertex(new_vertex);
KillGLLists();
return false;
}
}
return false;
}
CMeshVertex* CMeshFace::SplitOnGivenEdge(CMeshEdge* edge)
{
Point vt0;
Point vt1;
Point vt2;
Point vt01;
Point vt10;
Point vt12;
Point vt21;
Point vt20;
Point vt02;
Point c = m_centre.vertex();
CMeshVertex* new_vertex = NULL;
if(edge == m_e[0]){
vt0 = m_v[1]->vertex();
vt1 = m_v[2]->vertex();
vt2 = m_v[0]->vertex();
vt01 = m_edge_dir[1] ? m_e[1]->m_c[1].vertex() : m_e[1]->m_c[0].vertex();
vt10 = m_edge_dir[1] ? m_e[1]->m_c[0].vertex() : m_e[1]->m_c[1].vertex();
vt12 = m_edge_dir[2] ? m_e[2]->m_c[1].vertex() : m_e[2]->m_c[0].vertex();
vt21 = m_edge_dir[2] ? m_e[2]->m_c[0].vertex() : m_e[2]->m_c[1].vertex();
vt20 = m_edge_dir[0] ? m_e[0]->m_c[1].vertex() : m_e[0]->m_c[0].vertex();
vt02 = m_edge_dir[0] ? m_e[0]->m_c[0].vertex() : m_e[0]->m_c[1].vertex();
}
else if(edge == m_e[1]){
vt0 = m_v[2]->vertex();
vt1 = m_v[0]->vertex();
vt2 = m_v[1]->vertex();
vt01 = m_edge_dir[2] ? m_e[2]->m_c[1].vertex() : m_e[2]->m_c[0].vertex();
vt10 = m_edge_dir[2] ? m_e[2]->m_c[0].vertex() : m_e[2]->m_c[1].vertex();
vt12 = m_edge_dir[0] ? m_e[0]->m_c[1].vertex() : m_e[0]->m_c[0].vertex();
vt21 = m_edge_dir[0] ? m_e[0]->m_c[0].vertex() : m_e[0]->m_c[1].vertex();
vt20 = m_edge_dir[1] ? m_e[1]->m_c[1].vertex() : m_e[1]->m_c[0].vertex();
vt02 = m_edge_dir[1] ? m_e[1]->m_c[0].vertex() : m_e[1]->m_c[1].vertex();
}
else{
vt0 = m_v[0]->vertex();
vt1 = m_v[1]->vertex();
vt2 = m_v[2]->vertex();
vt01 = m_edge_dir[0] ? m_e[0]->m_c[1].vertex() : m_e[0]->m_c[0].vertex();
vt10 = m_edge_dir[0] ? m_e[0]->m_c[0].vertex() : m_e[0]->m_c[1].vertex();
vt12 = m_edge_dir[1] ? m_e[1]->m_c[1].vertex() : m_e[1]->m_c[0].vertex();
vt21 = m_edge_dir[1] ? m_e[1]->m_c[0].vertex() : m_e[1]->m_c[1].vertex();
vt20 = m_edge_dir[2] ? m_e[2]->m_c[1].vertex() : m_e[2]->m_c[0].vertex();
vt02 = m_edge_dir[2] ? m_e[2]->m_c[0].vertex() : m_e[2]->m_c[1].vertex();
}
// remove the existing face
CMesh* mesh = (CMesh*)Owner();
mesh->Remove(this);
delete this;
// split into two sub triangles
for(int i = 0; i<2; i++){
Point nvt0 = vt0;
Point nvt1 = vt1;
Point nvt2 = vt2;
Point nc = c;
Point nvt01 = vt01;
Point nvt10 = vt10;
Point nvt12 = vt12;
Point nvt21 = vt21;
Point nvt20 = vt20;
Point nvt02 = vt02;
if(i == 1){
// mirror
Point t12 = nvt12;
Point t21 = nvt21;
Point t2 = nvt2;
Point t20 = nvt20;
nvt12 = nvt10;
nvt21 = nvt01;
nvt2 = nvt0;
nvt20 = nvt02;
nvt10 = t12;
nvt01 = t21;
nvt0 = t2;
nvt02 = t20;
}
nvt2=(nvt20+nvt2)/2;
nvt20=(nvt02+nvt20)/2;
nvt2=(nvt20+nvt2)/2;
nvt02=(nvt0+nvt02)/2;
nvt20=(nvt02+nvt20)/2;
nvt2=(nvt20+nvt2)/2;
nvt21=(nc+nvt21)/2;
nc=(nvt01+nc)/2;
nvt21=(nc+nvt21)/2;
nvt12=(nvt10+nvt12)/2;
// add the new triangles
if(i == 1){
mesh->AddTriangle(nvt0, nvt2, nvt1, nvt02, nvt20, nvt21, nvt12, nvt10, nvt01, nc);
}
else{
mesh->AddTriangle(nvt0, nvt1, nvt2, nvt01, nvt10, nvt12, nvt21, nvt20, nvt02, nc);
new_vertex = mesh->FindVertex(nvt2);
}
}
return new_vertex;
}
void CMeshFace::RemoveGivenEdge(CMeshEdge* edge)
{
for(int i = 0; i<3; i++){
if(m_e[i] == edge){
m_e[i] = NULL;
break;
}
}
}
Point CMeshFace::simple_3_pts_normal(){
Point v0(m_v[0]->vertex(), m_v[1]->vertex());
Point v1(m_v[1]->vertex(), m_v[2]->vertex());
Point v2(m_v[2]->vertex(), m_v[0]->vertex());
Point n0 = v2 ^ v0;
Point n1 = v0 ^ v1;
Point n2 = v1 ^ v2;
if(n0.magn() > n1.magn()){
if(n0.magn() > n2.magn()){
return n0.norm();
}
return n2.norm();
}
if(n1.magn() > n2.magn()){
return n1.norm();
}
return n2.norm();
}
Point CMeshFace::midpoints3normal(){
Point m0 = m_e[0]->GetMidPoint();
Point m1 = m_e[1]->GetMidPoint();
Point m2 = m_e[2]->GetMidPoint();
Point v0(m0, m1);
Point v1(m1, m2);
Point v2(m2, m0);
Point n0 = v2 ^ v0;
Point n1 = v0 ^ v1;
Point n2 = v1 ^ v2;
if(n0.magn() > n1.magn()){
if(n0.magn() > n2.magn()){
return n0.norm();
}
return n2.norm();
}
if(n1.magn() > n2.magn()){
return n1.norm();
}
return n2.norm();
}
double CMeshFace::longest_length(){
double max_dist = m_v[0]->vertex().dist(m_v[1]->vertex());
double dist = m_v[1]->vertex().dist(m_v[2]->vertex());
if(dist>max_dist)max_dist = dist;
dist = m_v[2]->vertex().dist(m_v[0]->vertex());
if(dist>max_dist)max_dist = dist;
return max_dist;
}
bool CMeshFace::is_connected_to_edge(const CMeshEdge* edge)const{
for(int i = 0; i<3; i++){
if(m_e[i] == edge)return true;
}
return false;
}
bool CMeshFace::RecalculateAGoodCentre()
{
Plane pl[3];
for(int i = 0; i<3; i++)
{
CMeshEdge* edge = m_e[i];
Point dir;
Point mid_point = edge->evaluate_bezier_curve(0.5, edge->m_v[0]->vertex(), edge->m_c[0].vertex(), edge->m_c[1].vertex(), edge->m_v[1]->vertex(), &dir);
dir = dir.norm();
Point n = edge->get_av_face_pair_normal();
Point cp = (n ^ dir).norm();
Point n2 = (dir ^ cp).norm();
pl[i] = Plane(n2, mid_point);
}
bool centre_found = false;
Line line;
if(pl[0].Intersect(pl[1], line))
{
Point p;
if(pl[2].Intersect(line, p))
{
m_centre.set_vertex(p);
KillGLLists();
centre_found = true;
// move it away some more
Plane flat_pl(midpoints3normal(), m_e[0]->GetMidPoint());
Point close_point = flat_pl.Near(m_centre.vertex());
Point new_centre = m_centre.vertex() + Point(close_point, m_centre.vertex()) * 0.5;
m_centre.set_vertex(new_centre);
}
}
return centre_found;
}
void CMeshFace::DrawGripperSelectItems()
{
if(heeksCAD->GetBackgroundColor() == HeeksColor(0, 0, 0))glColor3ub(255, 255, 255);
else glColor3ub(0, 0, 0);
glBegin(GL_LINES);
for(int i = 0; i<3; i++){
for(int j = 0; j<2; j++){
Point p0 = m_e[i]->m_v[j]->vertex();
CMeshPosition& p = m_e[i]->GetControlPointNearVertex(m_e[i]->m_v[j]);
Point p1 = p.vertex();
glVertex3d(p0.x, p0.y, p0.z);
glVertex3d(p1.x, p1.y, p1.z);
}
}
glEnd();
}