-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBauteile.pde
447 lines (382 loc) · 13.6 KB
/
Bauteile.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
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
/******************************************************************************************************************************
In this file all the building parts are defined as classes, along with corresponding methods and variables.
Those contain: - cables with cableVertices
- lamps
- collectors
******************************************************************************************************************************/
import java.util.Scanner;
import java.util.Arrays;
import java.util.Formatter;
class cable {
class cableVertex {
float x, y, z;
int lat, lon;
String state;
lamp l;
collector c;
int[] pointer = null;
cableVertex(int latitude, int longitude) {
this.lat = latitude;
this.lon = longitude;
this.x = -earthRadius * cos(radians(latitude)) * cos(radians(longitude));
this.y = -earthRadius * sin(radians(latitude));
this.z = earthRadius * cos(radians(latitude)) * sin(radians(longitude));
}
cableVertex(int latitude, int longitude, String state) {
this.lat = latitude;
this.lon = longitude;
this.x = -earthRadius * cos(radians(latitude)) * cos(radians(longitude));
this.y = -earthRadius * sin(radians(latitude));
this.z = earthRadius * cos(radians(latitude)) * sin(radians(longitude));
this.setState(state);
}
void setCoordinates(int latitude, int longitude) {
this.lat = latitude;
this.lon = longitude;
this.x = -earthRadius * cos(radians(latitude)) * cos(radians(longitude));
this.y = -earthRadius * sin(radians(latitude));
this.z = earthRadius * cos(radians(latitude)) * sin(radians(longitude));
}
void setState(String state) {
this.state = state;
if (state.equals("l")) {
this.l = new lamp(round(-degrees(asin(y/earthRadius))), round(degrees(atan2(z, -x))));
} else if (state.equals("c"))this.c = new collector(round(-degrees(asin(this.y/earthRadius))), round(degrees(atan2(this.z, -this.x))));
else state = "n";
}
/* In earlier versions, this function handled the displaying of the cableVertex' attached lamp or collector.
In the current version, this process is more complex and has to be handled via the display functions of the corresponding attached building part.
void display() {
if (this.state != null) {
if (this.state.equals("l"))this.l.display();
else if (this.state.equals("c"))this.c.display();
}
}
*/
void displayNob(int radius) {
pushMatrix();
translate(this.x, this.y, this.z);
noStroke();
fill(255);
sphere(radius);
popMatrix();
}
}
ArrayList<cableVertex> cvarr;
int weight;
int luminousFlux;
int numOfLamps;
// For construction
int lat, lon;
cableVertex cvTemp = new cableVertex(this.lat, this.lon);
/* These constructors are currently not of any use...
cable(cableVertex[] cvarr, int weight) {
this.cvarr = new ArrayList<cableVertex>(Arrays.asList(cvarr));
this.weight = weight;
}
cable(int weight) {
this.cvarr = new ArrayList<cableVertex>();
this.weight = weight;
}
*/
cable() {
this.cvarr = new ArrayList<cableVertex>();
this.weight = 5;
}
cable(int latitude, int longitude) {
this.cvarr = new ArrayList<cableVertex>();
this.weight = 5;
this.lat = latitude;
this.lon = longitude;
this.cvTemp = new cableVertex(this.lat, this.lon);
}
boolean fromFile(String fileStr) {
try {
File file = new File(fileStr);
Scanner scanner = new Scanner(file);
this.cvarr = new ArrayList<cableVertex>();
this.weight = int(scanner.next());
while (scanner.hasNextLine()) {
this.addCableVertex(int(scanner.next()), int(scanner.next()), scanner.next());
}
scanner.close();
return true;
}
catch(Exception e) {
println("Error while trying to read a cable coordinates file: " + fileStr + ": " + e);
return false;
}
}
/* REMOVED 27.05.18
boolean xyzFromFile(String fileStr) {
try {
File file = new File(fileStr);
Scanner scanner = new Scanner(file);
this.cvarr = new ArrayList<cableVertex>();
this.weight = int(scanner.next());
while (scanner.hasNextLine()) {
this.addCableVertex(float(scanner.next()), float(scanner.next()), float(scanner.next()), scanner.next());
}
scanner.close();
return true;
}
catch(Exception e) {
println("Error while trying to read a cable xyz file " + fileStr + ": " + e);
return false;
}
}*/
void addCableVertex(int lat, int lon, String state) {
this.cvarr.add(new cableVertex(lat, lon, state));
if (state.equals("l"))this.numOfLamps++;
}
void addCableVertex(cableVertex cv) {
this.cvarr.add(cv);
if (cv.state.equals("l"))this.numOfLamps++;
}
/* CHANGED 27.05.18
void addCableVertex(float x, float y, float z, String state){
this.cvarr.add(new cableVertex(x, y, z, state));
if(state.equals("l"))this.numOfLamps++;
}*/
//construction {
void right() {
this.lon++;
this.lon = ((this.lon + 540) % 360) - 180;
this.cvTemp.setCoordinates(lat, lon);
}
void left() {
this.lon--;
this.lon = ((this.lon + 540) % 360) - 180;
this.cvTemp.setCoordinates(lat, lon);
}
void up() {
if (this.lat < 90)this.lat++;
//if(this.lat == 91)this.lat = 89;
this.cvTemp.setCoordinates(lat, lon);
}
void down() {
if (this.lat > -90)this.lat--;
//if(this.lat == -91)this.lat = -89;
this.cvTemp.setCoordinates(lat, lon);
}
void deleteLastCableVertex() {
this.cvarr.remove(this.cvarr.size() - 1);
println("cableVertex removed");
}
void setCableVertex(String state) {
cvTemp.setState(state);
this.addCableVertex(this.cvTemp);
cvTemp = new cableVertex(lat, lon);
println("cableVertex set");
}
boolean crossingLon180() {
for (int i = 1; i < this.cvarr.size(); i++) {
if ((this.cvarr.get(i-1).lon < -90 && this.cvarr.get(i).lon > 90) || (this.cvarr.get(i).lon < -90 && this.cvarr.get(i-1).lon > 90))return true;
}
return false;
}
void sort() {
if ((this.cvarr.get(this.cvarr.size()-1).lon > this.cvarr.get(0).lon) ^ crossingLon180()) {
//reverse cvarr
println("sort");
ArrayList<cableVertex> temp = new ArrayList<cableVertex>(this.cvarr.size());
for (int i = 0; i < this.cvarr.size(); i++) {
temp.add(this.cvarr.get(this.cvarr.size()-i-1));
}
this.cvarr = temp;
}
}
void save() {
try {
//File file = new File(filePath + "cableNmb.txt");
File file = new File(dataFolderPath + "cableNmb.txt");
Scanner scanner = new Scanner(file);
int cableNmb = scanner.nextInt();
scanner.close();
Formatter formatterForNmb = new Formatter(file);
formatterForNmb.format("%s", cableNmb+1);
formatterForNmb.close();
//Formatter formatter = new Formatter(filePath + "cable[" + cableNmb + "].txt");
Formatter formatter = new Formatter(dataFolderPath + "cable[" + cableNmb + "].txt");
formatter.format("%s", this.weight);
for (cableVertex cvCur : this.cvarr) {
formatter.format("%s %s %s", "\r\n" + cvCur.lat, cvCur.lon, cvCur.state);
}
formatter.close();
}
catch(Exception e) {
try {
File f = new File(dataFolderPath + "cableNmb.txt");
f.getParentFile().mkdirs();
f.createNewFile();
Formatter formatterForNmb = new Formatter(f);
formatterForNmb.format("%s", 0);
formatterForNmb.close();
this.save();
}
catch(Exception e_) {
println("Error while trying to save the current cable: " + e + " and " + e_);
}
}
}
void connect(ArrayList<cable> cableArray) { //connection always happens after appension...
for (int i = 0; i < this.cvarr.size(); i++) {
int[] conCVIndex = searchCables(this.cvarr.get(i).lat, this.cvarr.get(i).lon, cableArray);
if (conCVIndex != null) {
cableVertex conCV = cableArray.get(conCVIndex[0]).cvarr.get(conCVIndex[1]);
if (i == 0 || i == (this.cvarr.size()-1))conCV.pointer = new int[]{cableArray.size()-1, i};
else if (conCVIndex[1] == 0 || conCVIndex[1] == (cableArray.get(conCVIndex[0]).cvarr.size() - 1))this.cvarr.get(i).pointer = conCVIndex;
}
}
}
//}
void displayCableVertex(cableVertex cv, color colour) {
pushMatrix();
translate(cv.x, cv.y, cv.z);
noStroke();
fill(colour);
sphere(5);
popMatrix();
}
int numOfLampsOff, numOfLampsOn, numOfConnectedLampsOn;
float maxFluxPerLamp = 0;
void setMaxFluxPerLamp() {
try {
this.maxFluxPerLamp = this.luminousFlux/(this.numOfLampsOn + this.numOfConnectedLampsOn);
}
catch (ArithmeticException e) {
}
}
void display() {
this.numOfLampsOn = this.numOfLamps - this.numOfLampsOff;
this.numOfLampsOff = 0;
this.numOfConnectedLampsOn = 0;
pushMatrix();
noFill();
stroke(170);
strokeWeight(this.weight);
beginShape();
for (cableVertex cv : this.cvarr) {
//curveVertex(cv.x, cv.y, cv.z);
vertex(cv.x, cv.y, cv.z);
}
endShape();
strokeWeight(1);
popMatrix();
for (cableVertex cv : this.cvarr) {
if (cv.pointer != null) {
cable pointTo = cables.get(cv.pointer[0]);
if (cv.pointer[1] == 0) {
this.numOfConnectedLampsOn += pointTo.numOfLampsOn;
}
}
}
for (cableVertex cv : this.cvarr) {
if (cv.state.equals("c")) {
this.luminousFlux += cv.c.calcIlluminance() * cv.c.area;
cv.c.display();
this.setMaxFluxPerLamp();
} else if (cv.state.equals("l")) {
if (cv.l.calcIlluminance() < 300) {
//turn on the lamp with brightness = maxFluxPerLamp / cv.l.numOfLanterns
try {
float brightness = constrain(this.maxFluxPerLamp / cv.l.numOfLanterns, 0, 255);
cv.l.display(brightness);
this.luminousFlux -= (brightness*cv.l.numOfLanterns);
}
catch(ArithmeticException e) {
}
} else numOfLampsOff++;
}
if (cv.pointer != null) {
cable pointTo = cables.get(cv.pointer[0]);
if (cv.pointer[1] == 0) {
try {
int ptFlux = (int) (this.maxFluxPerLamp * pointTo.numOfLampsOn);
pointTo.luminousFlux = ptFlux;
pointTo.setMaxFluxPerLamp();
this.luminousFlux -= ptFlux;
}
catch(ArithmeticException e) {
}
} else {
this.luminousFlux += pointTo.luminousFlux;
this.setMaxFluxPerLamp();
}
}
}
}
}
// CHANGED 30.05.18
ArrayList<cable> loadCables(int start, int end) {
ArrayList<cable> res = new ArrayList<cable>();
for (int i = 0; i < end-start; i++) {
res.add(new cable());
res.get(i).fromFile(dataFolderPath + "cable[" + (start + i) + "].txt");
res.get(i).connect(res);
}
return res;
}
class lamp {
float x, y, z;
int lat, lon;
int numOfLanterns = 20000;
lamp(int latitude, int longitude) {
this.x = -(earthRadius + 5) * cos(radians(latitude)) * cos(radians(longitude));
this.y = -(earthRadius + 5) * sin(radians(latitude));
this.z = (earthRadius + 5) * cos(radians(latitude)) * sin(radians(longitude));
this.lat = latitude;
this.lon = longitude;
}
public void display(float brightness) {
pushMatrix();
translate(this.x, this.y, this.z);
noStroke();
fill(brightness/10);
sphere(brightness/10);
popMatrix();
}
int calcIlluminance() {
float imX = -earthRadius * cos(radians(this.lat)) * cos(radians(this.lon)+rot+HALF_PI);
float imY = -earthRadius * sin(radians(this.lat));
float imZ = earthRadius * cos(radians(this.lat)) * sin(radians(this.lon)+rot+HALF_PI);
float sunHeight = acos((pow(earthRadius, 2) + imX*imX + imY*imY + imZ*imZ + 2*imZ*sunDist) / (2*earthRadius*sqrt(imX*imX + imY*imY + pow(imZ+sunDist, 2)))) - HALF_PI;
int illuminance;
if (sunHeight > 0)illuminance = int(300 + 21000 * sin(sunHeight));
else illuminance = 0;
return illuminance;
}
}
class collector {
float x, y, z;
int lat, lon;
int area = 500;//m²
collector(int latitude, int longitude) {
this.lat = latitude;
this.lon = longitude;
this.x = -(earthRadius + 3) * cos(radians(latitude)) * cos(radians(longitude));
this.y = -(earthRadius + 3) * sin(radians(latitude));
this.z = (earthRadius + 3) * cos(radians(latitude)) * sin(radians(longitude));
}
void display() {
pushMatrix();
rectMode(CENTER);
translate(this.x, this.y, this.z);
rotateY(HALF_PI+radians(this.lon));
rotateX(radians(-this.lat));
noStroke();
fill(255);
rect(0, 0, 10, 10);
popMatrix();
}
int calcIlluminance() {
float imX = -earthRadius * cos(radians(this.lat)) * cos(radians(this.lon)+rot+HALF_PI);
float imY = -earthRadius * sin(radians(this.lat));
float imZ = earthRadius * cos(radians(this.lat)) * sin(radians(this.lon)+rot+HALF_PI);
float sunHeight = acos((pow(earthRadius, 2) + imX*imX + imY*imY + imZ*imZ + 2*imZ*sunDist) / (2*earthRadius*sqrt(imX*imX + imY*imY + pow(imZ+sunDist, 2)))) - HALF_PI;
int illuminance;
if (sunHeight > 0)illuminance = int(300 + 21000 * sin(sunHeight));//lx
else illuminance = 0;
return illuminance;
}
}