-
Notifications
You must be signed in to change notification settings - Fork 1
/
meanCurvatureFlow.pde
288 lines (243 loc) · 7.36 KB
/
meanCurvatureFlow.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
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
import g4p_controls.*;
// maximum number of vertices, edges and faces
int nVmax = 2000;
int nEmax = 2000;
int nFmax = 3500;
// maximum number of open windows
int maxS = 6;
// values to bound the flows
float maxFlowComp = 100;
Float pInfiniteFloat = Float.POSITIVE_INFINITY;
Float nInfiniteFloat = Float.NEGATIVE_INFINITY;
// interface buttons and fields
GButton btnStart;
GButton btnFlow;
GButton editFlow;
GButton editActiveFlow;
GButton editTau;
GButton editSurface;
GButton endEdit;
GLabel flowName;
GTextField flowT;
GTextField tauT;
// bool to start/stop the flows on all windows
boolean flowing = false;
//tableau des fenetres
MyWinData surfaces[];
int nbS = 0;
int editing = -1;
void setup() {
size(800, 300, P3D);
surfaces = new MyWinData[maxS];
btnStart = new GButton(this, 20, 30, 80, 80, "NEW");
btnFlow = new GButton(this, 20, 150, 80, 80, "Start flow");
editFlow = new GButton(this, 40, 30, 120, 90, "Flow type");
editTau = new GButton(this, 250, 30, 120, 90, "Tau factor");
editActiveFlow = new GButton(this, 40, 180,120, 90, "Active");
editSurface = new GButton(this, 250, 180, 120, 90, "Surface File");
endEdit = new GButton(this, 460, 180, 120, 90, "OK");
flowT = new GTextField(this, 50, 140, 110, 24);
tauT = new GTextField(this, 260, 140, 110, 24);
flowName = new GLabel(this, 40, 116, 500, 24);
flowT.setPromptText("new flow value");
tauT.setPromptText("new tau value");
buttonsVisibility(true);
}
void draw() {
background(130, 130, 130);
}
// change menu (by changing buttons visibility)
void buttonsVisibility(boolean menu) { // true => general // false => bouton
// Menu general
btnStart.setVisible(menu);
btnFlow.setVisible(menu);
for(int i=0;i<nbS;i++) {
surfaces[i].menuB.setVisible(menu);
}
// Menu bouton
editFlow.setVisible(!menu);
editActiveFlow.setVisible(!menu);
editTau.setVisible(!menu);
editSurface.setVisible(!menu);
endEdit.setVisible(!menu);
flowName.setVisible(!menu);
flowT.setVisible(!menu);
tauT.setVisible(!menu);
refreshFlowButtons();
}
// update button labels
void refreshFlowButtons() {
if(editing >= 0) {
if(surfaces[editing].flow > 0)
editActiveFlow.setText("Active");
else
editActiveFlow.setText("Inactive");
editFlow.setText("Flow type = " + surfaces[editing].flow);
editTau.setText("Tau factor = " + surfaces[editing].tau);
flowName.setText(nameFlow( surfaces[editing].flow));
}
}
// handle the events associated to each button
public void handleButtonEvents(GButton button, GEvent event) {
if (btnStart == button) {
println("new surface.");
surfaceWindow();
return;
}
if (btnFlow == button) {
flowing = !flowing;
println("Change Flowing to : " + flowing);
if(!flowing)
btnFlow.setText("Start flow");
else
btnFlow.setText("Stop flow");
return;
}
for(int i=0;i<nbS;i++) {
if (surfaces[i].menuB == button) {
editing = i;
buttonsVisibility(false);
}
}
if (editFlow == button) {
String r = flowT.getText();
if(r.isEmpty())
return;
for (int i=0; i< r.length();i++) {
if (r.charAt(i) <'0' || r.charAt(i) > '9') {
println("not an int");
flowT.setText("");
return;
}
}
int f = Integer.parseInt(r);
if (f < 0 || f > 8) {
println("must be between 0 and 8");
flowT.setText("");
return;
}
if(surfaces[editing].flow < 0)
f = -f;
surfaces[editing].flow = f;
flowT.setText("");
refreshFlowButtons();
return;
}
if (editActiveFlow == button) {
surfaces[editing].flow *= -1;
refreshFlowButtons();
return;
}
if (editTau == button) {
String r = tauT.getText();
if(r.isEmpty())
return;
try {
float t = Float.parseFloat(r);
if (t <= 0 || t > 1) {
println("bad value (need in ]0,1])");
tauT.setText("");
return;
}
surfaces[editing].tau = t;
tauT.setText("");
editTau.setText("Tau factor = " + surfaces[editing].tau);
return;
} catch(Exception e) {
println("not an float");
tauT.setText("");
return;
}
}
if (editSurface == button) {
String filename = G4P.selectInput("Input Dialog", "txt", "Surface file");
try{
if(filename.isEmpty())
return;
Surface temp = new Surface(filename);
surfaces[editing].S = temp;
surfaces[editing].initialVol = surfaces[editing].S.volume();
} catch(Exception e) {
println("can't use this file");
return;
}
return;
}
if (endEdit == button) {
editing = -1;
buttonsVisibility(true);
flowT.setText("");
tauT.setText("");
return;
}
}
// update mouse position if the button is active
public void windowMouse(PApplet appc, GWinData data, MouseEvent event) {
MyWinData d = (MyWinData)data;
switch(event.getAction()) {
case MouseEvent.PRESS:
d.rotaX = appc.mouseX;
d.rotaY = appc.mouseY;
break;
case MouseEvent.DRAG:
d.rotaX = appc.mouseX;
d.rotaY = appc.mouseY;
break;
}
}
// zoom in and out funcionality
public void windowKey(PApplet appc, GWinData data, KeyEvent ev) {
MyWinData d = (MyWinData)data;
if(ev.getAction() == KeyEvent.PRESS) {
switch(ev.getKey()) {
case 'z':
for(int i=0; i<d.S.positions.size(); i++) {
d.S.positions.get(i).mult(0.9);
}
d.initialVol = d.S.volume();
break;
case 'a':
for(int i=0; i<d.S.positions.size(); i++) {
d.S.positions.get(i).mult(1.1);
}
d.initialVol = d.S.volume();
break;
}
}
}
// draw a window
public void windowDraw(PApplet appc, GWinData data) { // width partout
MyWinData d = (MyWinData)data;
appc.background(50);
appc.camera(appc.width, appc.width, 1800, appc.width/2, appc.width/2, appc.width/2, 0, 1, 0);
appc.translate(appc.width/2, appc.width/2, 0);
appc.rotateX(TWO_PI * d.rotaX / appc.width);
appc.rotateY(TWO_PI * d.rotaY / appc.width);
appc.line(0,0,0,appc.width*10,0,0);
appc.line(0,0,0,0,appc.width*10,0);
appc.line(0,0,0,0,0,appc.width*10);
if(flowing && d.flow > 0) {
applyFlow(d);
}
d.S.drawSurface(appc);
}
// create a window
public void surfaceWindow() {
if(nbS >= maxS) {
println("max surfaces, no more creation");
return;
}
GWindow mywindow = GWindow.getWindow(this, "Surface", 50, 50, 600, 600, P3D); //P3D
MyWinData mydata = new MyWinData();
mydata.S = new Surface("cube.txt");
mydata.initialVol = mydata.S.volume();
mydata.flow = 0;
mydata.menuB = new GButton(this, 130 + nbS*110, 70, 80, 80, "Surface n°" + nbS);
mywindow.addData(mydata);
mywindow.addDrawHandler(this, "windowDraw");
mywindow.addMouseHandler(this, "windowMouse");
mywindow.addKeyHandler(this,"windowKey");
mywindow.setActionOnClose(GWindow.KEEP_OPEN);
surfaces[nbS] = mydata;
nbS++;
}