-
Notifications
You must be signed in to change notification settings - Fork 0
/
StrokeManager.pde
211 lines (185 loc) · 7.05 KB
/
StrokeManager.pde
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
//
class StrokeManager {
//
ArrayList<Stroke> strokes = new ArrayList<Stroke>();
//
int selected_count = 0;
//
public StrokeManager() {}
//
public void DrawAll() {
pushStyle();
hint(ENABLE_STROKE_PERSPECTIVE);
for(int i=0; i < strokes.size(); i++) {
Stroke s = strokes.get(i);
float h = s.GetHeight(); // returns 0 when !isFlat
if(!s.isSelected) s.c = color(s.col_fil, (IsCloseToCurrentLayer(h) ? 255 : 50));
else s.c = color(selected_color, (IsCloseToCurrentLayer(h) ? 255 : 100));
s.stroke_weight = CameraDistanceScaleDown();
s.Draw();
if(__isDebug) for(int j=0; j<s.bb.size(); j++) s.bb.get(j).Draw();
// draw the direction indicator dot
if(s.vertices.size() > 1) {
PVector t = PVector.mult((s.vertices.get(s.vertices.size()-1)), b2w(1));
float r= CameraDistanceScaleDown();
pushMatrix(); pushStyle();
translate(t.x, t.y, t.z);
sphereDetail(4);
noStroke();
fill(s.c);
sphere(r);
popStyle(); popMatrix();
}
}
hint(DISABLE_STROKE_PERSPECTIVE);
popStyle();
}
// BRUSH STROKE FUNCTIONS
/////////////////////
//
// onpress
Stroke temp=null;
public void StartStroke() {
temp = new Stroke();
temp.isFlat = true; // its bound to the plane in this mode
strokes.add(temp);
}
// ondrag
public void CollectStroke() {
if(temp != null && strokes.size() >= 1) {
Stroke s = strokes.get(strokes.size()-1);
current_stroke_len = nfc(s.length, 2)+" mm";
PVector wc = p.MousePointInWorldCoordinates();
if (wc!=null && !s.isClosed) {
s.AddVertex(w2b(wc.x), w2b(wc.y), p.current_height);
}
}
}
// onrelease
public void EndStroke() {
// actual printing if draw mode is immediate
if(temp != null && __drawMode) (new PrintSender(temp)).start();
if (temp != null && temp.vertices.size() <= 1) // if its just a click, delete the last stroke
strokes.remove(strokes.size()-1);
temp = null; // release temp
current_stroke_len = "";
}
//////////////////////////////////////////////
//
// generates count many interpolated strokes in between two strokes, -1 fluds the middle of two curves
public void Interpolate(Stroke s0, Stroke s1, int count) {
float h1 = s0.GetHeight();
float h2 = s1.GetHeight();
// if count is -1, assign count to maximum layers in between (ideally solid)
if(count == -1) {
float[] min = new float[] {1000, 0, 0}; // min_dist, idx_i, idx_j
for(int i = 0; i<s0.vertices.size();i++) {
for(int j = 0; j<s1.vertices.size(); j++) {
float d = s0.vertices.get(i).dist(s1.vertices.get(j));
if(d < min[0]) {
min[0] = d; min[1] = i; min[2] = j;
}
}
}
count = int(min[0]/p.layer_height);
}
// check for maximum amount of layers
int maxcount = int(abs(h1-h2)/p.layer_height);
if( maxcount>1 && count>maxcount ) count = maxcount;
int l1 = s0.vertices.size();
int l2 = s1.vertices.size();
int diff = abs(l1-l2); // difference in vertex count
for(int i=1; i<=count; i++){
// rows normalized [0,1]
float __i = i/float(count+1);
Stroke new_stroke = new Stroke();
new_stroke.isFlat = false; // mark it as !flat, check after creating the vertices
new_stroke.isClosed = (s0.isClosed&&s1.isClosed ? true:false); // mark it closed if both curves are closed
new_stroke.col_fil = s0.col_fil; // use one of the stroke's colors
for(int j = 0; j < min(l1, l2)+int(diff*__i); j++) {
// cols normalized [0,1] -- to select which coordinate to use
float __j = j/float(min(l1, l2)+int(diff*__i));
PVector p1 = s0.vertices.get(int(__j*l1));
PVector p2 = s1.vertices.get(int(__j*l2));
PVector v = PVector.add(PVector.mult(p1,__i), PVector.mult(p2,(1-__i)));
// snap z coordinate to closes layer
v.z = round(v.z/p.layer_height) * p.layer_height;
new_stroke.AddVertex(v.x,v.y,v.z);
}
new_stroke.UpdateIsFlat(); // update flatness
selected_count = new_stroke.Select(true, selected_count); // mark it selected after interpolation
strokes.add(new_stroke);
}
}
// SELECT STROKE FUNCTIONS
/////////////////////
//
// Updates the selected strokes on click
private int fail_count = 0;
private int fail_count_max = 2;
public void UpdateStrokeSelection() {
for(int i=0; i<strokes.size();i++) {
Stroke s = strokes.get(i);
PVector wc = s.IsMouseHit();
// hit
if (wc!=null) {
fail_count=0;
//if(IsCloseToCurrentLayer(wc.z))
if(s.isSelected) selected_count = s.Select(false, selected_count);
else selected_count = s.Select(true, selected_count);
break;
}
}
// clear all strokes
if(fail_count < fail_count_max) fail_count++;
else {ClearSelection(); fail_count=0;}
}
public void SelectBetweenStrokes() {
if(selected_count >= 2) {
Collections.sort(strokes, new SortBySelection());
Stroke s0 = strokes.get(0);
Stroke s1 = strokes.get(1);
PVector d0 = _min(s0.s_min, s1.s_min), //in world coords
d1 = _max(s0.s_max, s1.s_max); //in world coords
for(int i=0; i<strokes.size(); i++) {
for(int j=0; j<strokes.get(i).shape.getVertexCount(); j++) {
PVector _d = strokes.get(i).shape.getVertex(j); //in world coords
PVector _d0 = PVector.sub(_d,d0);
PVector _d1 = PVector.sub(_d,d1);
// if the vertex is in +++ region relative to min point, and in --- relative to max point
if(PVector.dot(_d0, _X)>=0 && PVector.dot(_d0, _Y)>=0 && PVector.dot(_d0, _Z)>=0 &&
PVector.dot(_d1, _X)<=0 && PVector.dot(_d1, _Y)<=0 && PVector.dot(_d1, _Z)<=0) {
selected_count=strokes.get(i).Select(true, selected_count);
break;
}
}
}
}
}
public void ClearSelection() {
for(int i=0; i<strokes.size();i++) selected_count = strokes.get(i).Select(false, selected_count);
}
public void SelectAll() {
for(int i=0; i<strokes.size();i++) selected_count = strokes.get(i).Select(true, selected_count);
}
////////////////////////////////
////////////////
//
public void ClearSelectedStrokes() {
// sort by selection, selected items are in the beginnning
Collections.sort(strokes, new SortBySelection());
println(strokes.get(0).isSelected, strokes.get(strokes.size()-1).isSelected);
println(strokes.size());
//List removed = new ArrayList(sublist);
while(selected_count>0) {
strokes.remove(0);
selected_count--;
}
println(strokes.size());
}
////////////////////////////////////////////////////////////
//
private boolean IsCloseToCurrentLayer(float h) {
return (h>(p.current_height-p.layer_height/2) && h<(p.current_height+p.layer_height/2));
}
}