-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathContourChain.cpp
More file actions
160 lines (120 loc) · 3.28 KB
/
ContourChain.cpp
File metadata and controls
160 lines (120 loc) · 3.28 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
#include "ContourChain.h"
ContourChain::ContourChain() {
forceClosed=false;
}
void ContourChain::forceChainClosed() {
forceClosed=true;
}
bool ContourChain::connectsToChain(const QVector3D & v1, const QVector3D & v2) {
if (contourChain.size()==0) { //if its empty, make this the first segment
return true;
}
else {
const QVector3D start=contourChain.front();
const QVector3D end=contourChain.back();
const float scale=(v1-v2).length();
if (isCloseEnough(v1,start,scale)||
isCloseEnough(v2,start,scale)||
isCloseEnough(v1,end,scale)||
isCloseEnough(v2,end,scale)) {
return true;
}
}
return false;
}
bool ContourChain::isCloseEnough(const QVector3D & v1, const QVector3D & v2, float scale)
{
return ((v1-v2).lengthSquared()<scale*CHAIN_SEGMENT_CLOSENESS_THRESHOLD);
}
void ContourChain::addSegmentToChain(const QVector3D & v1, const QVector3D & v2)
{
if (contourChain.isEmpty()) {
contourChain.push_back(v1);
contourChain.push_back(v2);
}
else {
const QVector3D start=contourChain.front();
const QVector3D end=contourChain.back();
const float scale=(v1-v2).length();
if (isCloseEnough(v1,start,scale)) {
contourChain.push_front(v2);
}
else if (isCloseEnough(v2,start,scale)) {
contourChain.push_front(v1);
}
else if (isCloseEnough(v1,end,scale)) {
contourChain.push_back(v2);
}
else if (isCloseEnough(v2,end,scale)) {
contourChain.push_back(v1);
}
}
}
int ContourChain::getLength()
{
return contourChain.size();
}
bool ContourChain::isChainClosed()
{
if (forceClosed) {
return true;
}
if (contourChain.size()>0&&isCloseEnough(contourChain.front(),contourChain.back(),1.0))
return true;
else
return false;
}
void ContourChain::draw() {
if (isChainClosed()&&!forceClosed) {
glColor3f(0,1,0);
glBegin(GL_LINE_LOOP);
}
else {
glColor3f(1,0,0);
glBegin(GL_LINE_STRIP);
}
float curIndex=0.0;
for (QVector3D & v : contourChain) {
if (forceClosed) {
glColor3f(curIndex/((float)contourChain.size()),0,0);
curIndex+=1.0;
}
glVertex3f(v.x(), v.y(), v.z());
}
glEnd();
}
void ContourChain::reset() {
contourChain.clear();
}
void ContourChain::drawAsSpline(int numReverseChaikinPasses) {
QPointer <Line> line=new Line();
QPointer <Line> filteredLine;
for (QVector3D & v : contourChain) {
line->addComponentAtEnd(v);
}
for (int i=0;i<numReverseChaikinPasses;i++) {
filteredLine=line->reverseChaikin();
delete line;
line=filteredLine;
}
QPointer <CatmullRomSpline> spline = new CatmullRomSpline(line);
spline->draw(QVector3D());
delete spline;
delete line;
}
void ContourChain::drawAsSplineOBJ(OBJObject *obj, float maxstrokewidth, int numReverseChaikinPasses) {
Line *line=new Line();
for (QVector3D & v : contourChain) {
line->addComponentAtEnd(v);
}
Line *filteredLine;
for (int i=0;i<numReverseChaikinPasses;i++) {
filteredLine=line->reverseChaikin();
delete line;
line=filteredLine;
}
CatmullRomSpline *spline=new CatmullRomSpline(line);
spline->drawWithObject(obj, maxstrokewidth);
delete spline;
delete line;
}