-
Notifications
You must be signed in to change notification settings - Fork 5
/
poLineExtruder.cpp
219 lines (182 loc) · 4.84 KB
/
poLineExtruder.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
/*
* Copyright 2012 Potion Design. All rights reserved.
* This file is part of pocode.
*
* pocode is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* pocode is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with pocode. If not, see <http://www.gnu.org/licenses/>.
*
*/
//
// LineExtruder.cpp
// pocode
//
// Created by Joshua Fisher on 6/20/11.
// Copyright 2011 Potion Design. All rights reserved.
//
#include "poLineExtruder.h"
#include "poMath.h"
poExtrudedLineSeg::poExtrudedLineSeg()
{}
poExtrudedLineSeg::poExtrudedLineSeg(poPoint a, poPoint b, float w, poStrokePlacementProperty place) {
poPoint diff = b - a;
float angle = atan2(diff.y, diff.x);
this->place = place;
float offset1 = 0;
float offset2 = 0;
switch(place) {
case PO_STROKE_PLACE_CENTER:
offset1 = offset2 = w / 2.f;
break;
case PO_STROKE_PLACE_INSIDE:
offset1 = w;
offset2 = 0.f;
break;
case PO_STROKE_PLACE_OUTSIDE:
offset1 = 0.f;
offset2 = w;
break;
};
float c1 = cosf(angle + M_PI_2) * offset1;
float s1 = sinf(angle + M_PI_2) * offset1;
float c2 = cosf(angle - M_PI_2) * offset2;
float s2 = sinf(angle - M_PI_2) * offset2;
p1 = a + poPoint(c1,s1);
p2 = a + poPoint(c2,s2);
p3 = b + poPoint(c1,s1);
p4 = b + poPoint(c2,s2);
}
poExtrudedLineSeg::poExtrudedLineSeg(poPoint ul, poPoint ll, poPoint ur, poPoint lr)
: p1(ul)
, p2(ll)
, p3(ur)
, p4(lr)
{}
float angleBetweenSegments(poExtrudedLineSeg seg1, poExtrudedLineSeg seg2) {
poPoint p1, p2, p3;
switch(seg1.place) {
case PO_STROKE_PLACE_CENTER:
p1 = (seg1.p2 + seg1.p1) / 2.f;
p2 = (seg1.p4 + seg1.p3) / 2.f;
p3 = (seg2.p4 + seg2.p3) / 2.f;
break;
case PO_STROKE_PLACE_INSIDE:
p1 = seg1.p1;
p2 = seg1.p3;
p3 = seg2.p3;
break;
case PO_STROKE_PLACE_OUTSIDE:
p1 = seg1.p2;
p2 = seg1.p4;
p3 = seg2.p4;
break;
}
return angleBetweenPoints(p1, p2, p3);
}
bool combineExtrudedLineSegments(poExtrudedLineSeg seg1, poExtrudedLineSeg seg2, poPoint *top, poPoint *bottom) {
poPoint i1, i2, i3, i4;
float angle = angleBetweenSegments(seg1, seg2);
if(compare(angle,0.f)) {
*top = seg1.p3;
*bottom = seg2.p4;
}
else {
rayIntersection(poRay(seg1.p2, seg1.p4-seg1.p2),
poRay(seg2.p4, seg2.p2-seg2.p4),
&i1, &i2);
rayIntersection(poRay(seg1.p1, seg1.p3-seg1.p1),
poRay(seg2.p3, seg2.p1-seg2.p3),
&i3, &i4);
*top = i3;
*bottom = i1;
}
return angle > 0.f;
}
void makeStrokeForJoint(std::vector<poPoint> &stroke, poExtrudedLineSeg &seg1, poExtrudedLineSeg &seg2, poStrokeJoinProperty join, float stroke_width) {
poPoint top, bottom;
poPoint p1, p2, p3, p4, tmp;
bool top_outside = combineExtrudedLineSegments(seg1, seg2, &top, &bottom);
poPoint joint = (seg1.p4 + seg1.p3) / 2.f;
switch(join) {
case PO_STROKE_JOIN_MITRE:
if(top_outside) {
stroke.push_back(top);
stroke.push_back(bottom);
}
else {
stroke.push_back(bottom);
stroke.push_back(top);
}
break;
case PO_STROKE_JOIN_BEVEL:
if(top_outside) {
stroke.push_back(seg1.p3);
stroke.push_back(bottom);
stroke.push_back(seg2.p1);
stroke.push_back(bottom);
}
else {
stroke.push_back(top);
stroke.push_back(seg1.p4);
stroke.push_back(top);
stroke.push_back(seg2.p2);
}
break;
case PO_STROKE_JOIN_ROUND:
{
float halfW = stroke_width / 2.f;
if(top_outside) {
p1 = bottom;
p2 = seg1.p3;
p3 = joint;
p4 = seg2.p1;
stroke.push_back(p2);
stroke.push_back(p1);
float a1 = angleBetweenPoints(p2, p3, p4);
poPoint diff = p2 - p3;
float a2 = atan2f(diff.y, diff.x);
float step = a1 / 9.f;
float a = a2;
for(int j=0; j<10; j++) {
tmp.set(cosf(a)*halfW, sinf(a)*halfW, 0.f);
stroke.push_back(tmp+p3);
stroke.push_back(p3);
a += step;
}
stroke.push_back(p4);
stroke.push_back(p1);
}
else {
p1 = top;
p2 = seg1.p4;
p3 = joint;
p4 = seg2.p2;
stroke.push_back(p1);
stroke.push_back(p2);
float a1 = angleBetweenPoints(p2, p3, p4);
poPoint diff = p2 - p3;
float a2 = atan2f(diff.y, diff.x);
float step = a1 / 9.f;
float a = a2;
for(int j=0; j<10; j++) {
tmp.set(cosf(a)*halfW, sinf(a)*halfW, 0.f);
stroke.push_back(p3);
stroke.push_back(tmp+p3);
a += step;
}
stroke.push_back(p1);
stroke.push_back(p4);
}
break;
}
}
}