/Users/lyon/j4p/src/sound/filterDesign/Hs3DCanvas.java
|
1 package sound.filterDesign;
2
3 // Decompiled by Jad v1.5.8c. Copyright 2001 Pavel Kouznetsov.
4 // Jad home page: http://www.geocities.com/kpdus/jad.html
5 // Decompiler options: packimports(3)
6 // Source File Name: Hs3DCanvas.java
7
8 import java.awt.*;
9 import java.awt.event.MouseEvent;
10 import java.awt.event.MouseListener;
11 import java.awt.event.MouseMotionListener;
12 import java.io.Serializable;
13 import java.util.Vector;
14
15 public class Hs3DCanvas extends Canvas
16 implements Runnable,
17 MouseListener,
18 MouseMotionListener,
19 Serializable {
20
21 public Hs3DCanvas() {
22 width = 600;
23 height = 600;
24 setSize(width, height);
25 setCursor(new Cursor(1));
26 setBackground(Color.black);
27 setForeground(Color.white);
28 addMouseListener(this);
29 addMouseMotionListener(this);
30 NPOINTS = 20;
31 X = new double[NPOINTS * NPOINTS];
32 Y = new double[NPOINTS * NPOINTS];
33 Z = new double[NPOINTS * NPOINTS];
34 mag = new double[NPOINTS * NPOINTS];
35 sx = new int[4];
36 sy = new int[4];
37 Zbuffer = new double[(NPOINTS - 1) * (NPOINTS - 1)];
38 Fbuffer = new int[(NPOINTS - 1) * (NPOINTS - 1)];
39 AXIS = true;
40 WIRE = false;
41 VALUE = false;
42 SURF = true;
43 PALETTE = 0;
44 xan = 0.10000000000000001D;
45 yan = 0.10000000000000001D;
46 zan = 0.050000000000000003D;
47 radius = 100D;
48 zoom = 50D;
49 zoom2 = 250D;
50 cnst = 4D;
51 cnst2 = 4D;
52 Axis = new double[9];
53 Axis[0] = Axis[4] = Axis[8] = 1.0D;
54 Axis[1] = Axis[2] = Axis[3] = Axis[5] = Axis[6] = Axis[7] = 0.0D;
55 thread = new Thread(this);
56 }
57
58 public void run() {
59 do
60 try {
61 rotatePoints();
62 rotateAxis();
63 repaint();
64 Thread.sleep(0L);
65 } catch (InterruptedException _ex) {
66 }
67 while (true);
68 }
69
70 public void paint(Graphics g) {
71 update(g);
72 }
73
74 public void update(Graphics g) {
75 Dimension dimension = getSize();
76 if (offGraphics == null ||
77 dimension.width != offDimension.width ||
78 dimension.height != offDimension.height) {
79 offDimension = dimension;
80 offImage = createImage(dimension.width, dimension.height);
81 offGraphics = offImage.getGraphics();
82 }
83 offGraphics.setColor(getBackground());
84 offGraphics.fillRect(0, 0, width, height);
85 if (SURF)
86 drawSurface(offGraphics);
87 else if (WIRE)
88 drawMesh(offGraphics);
89 else
90 drawWireframe(offGraphics);
91 g.drawImage(offImage, 0, 0, this);
92 }
93
94 public void drawSurface(Graphics g) {
95 for (int i = 0; i < NPOINTS - 1; i++) {
96 for (int j = 0; j < NPOINTS - 1; j++) {
97 Zbuffer[j + (NPOINTS - 1) * i] = Z[j + NPOINTS * i] +
98 Z[j + 1 + NPOINTS * (i + 1)] +
99 Z[j + NPOINTS * (i + 1)] +
100 Z[j + NPOINTS * i + 1];
101 Fbuffer[j + (NPOINTS - 1) * i] = j + NPOINTS * i;
102 }
103
104 }
105
106 QuickSort(Zbuffer, 0, Zbuffer.length - 1);
107 for (int k = Zbuffer.length - 1; k > -1; k--) {
108 int l = Fbuffer[k] % NPOINTS;
109 int i1 = Fbuffer[k] / NPOINTS;
110 sx[0] = (int) Math.rint(zoom * X[l + NPOINTS * i1]) +
111 width / 2;
112 sy[0] = (int) Math.rint(zoom * Y[l + NPOINTS * i1]) +
113 height / 2;
114 sx[1] = (int) Math.rint(zoom * X[l + NPOINTS * (i1 + 1)]) +
115 width / 2;
116 sy[1] = (int) Math.rint(zoom * Y[l + NPOINTS * (i1 + 1)]) +
117 height / 2;
118 sx[2] = (int) Math.rint(zoom * X[l + 1 + NPOINTS * (i1 + 1)]) +
119 width / 2;
120 sy[2] = (int) Math.rint(zoom * Y[l + 1 + NPOINTS * (i1 + 1)]) +
121 height / 2;
122 sx[3] = (int) Math.rint(zoom * X[l + NPOINTS * i1 + 1]) +
123 width / 2;
124 sy[3] = (int) Math.rint(zoom * Y[l + NPOINTS * i1 + 1]) +
125 height / 2;
126 switch (PALETTE) {
127 case 0: // '\0'
128 g.setColor(
129 findColor(
130 (mag[l + NPOINTS * i1] +
131 mag[l + NPOINTS * i1 + 1] +
132 mag[l + NPOINTS * (i1 + 1)] +
133 mag[l + 1 + NPOINTS * (i1 + 1)]) /
134 4D));
135 break;
136
137 case 1: // '\001'
138 g.setColor(
139 findColor2(
140 (mag[l + NPOINTS * i1] +
141 mag[l + NPOINTS * i1 + 1] +
142 mag[l + NPOINTS * (i1 + 1)] +
143 mag[l + 1 + NPOINTS * (i1 + 1)]) /
144 4D));
145 break;
146
147 case 2: // '\002'
148 g.setColor(
149 findColor3(
150 (mag[l + NPOINTS * i1] +
151 mag[l + NPOINTS * i1 + 1] +
152 mag[l + NPOINTS * (i1 + 1)] +
153 mag[l + 1 + NPOINTS * (i1 + 1)]) /
154 4D));
155 break;
156 }
157 g.fillPolygon(sx, sy, 4);
158 if (WIRE) {
159 g.setColor(Color.black);
160 g.drawPolygon(sx, sy, 4);
161 }
162 if (k == Zbuffer.length / 2 + 1 && AXIS)
163 drawAxis(g);
164 }
165
166 }
167
168 public void drawMesh(Graphics g) {
169 for (int i = 0; i < NPOINTS - 1; i++) {
170 for (int j = 0; j < NPOINTS - 1; j++) {
171 Zbuffer[j + (NPOINTS - 1) * i] = Z[j + NPOINTS * i] +
172 Z[j + 1 + NPOINTS * (i + 1)] +
173 Z[j + NPOINTS * (i + 1)] +
174 Z[j + NPOINTS * i + 1];
175 Fbuffer[j + (NPOINTS - 1) * i] = j + NPOINTS * i;
176 }
177
178 }
179
180 QuickSort(Zbuffer, 0, Zbuffer.length - 1);
181 for (int k = Zbuffer.length - 1; k > -1; k--) {
182 int l = Fbuffer[k] % NPOINTS;
183 int i1 = Fbuffer[k] / NPOINTS;
184 sx[0] = (int) Math.rint(zoom * X[l + NPOINTS * i1]) +
185 width / 2;
186 sy[0] = (int) Math.rint(zoom * Y[l + NPOINTS * i1]) +
187 height / 2;
188 sx[1] = (int) Math.rint(zoom * X[l + NPOINTS * (i1 + 1)]) +
189 width / 2;
190 sy[1] = (int) Math.rint(zoom * Y[l + NPOINTS * (i1 + 1)]) +
191 height / 2;
192 sx[2] = (int) Math.rint(zoom * X[l + 1 + NPOINTS * (i1 + 1)]) +
193 width / 2;
194 sy[2] = (int) Math.rint(zoom * Y[l + 1 + NPOINTS * (i1 + 1)]) +
195 height / 2;
196 sx[3] = (int) Math.rint(zoom * X[l + NPOINTS * i1 + 1]) +
197 width / 2;
198 sy[3] = (int) Math.rint(zoom * Y[l + NPOINTS * i1 + 1]) +
199 height / 2;
200 g.setColor(
201 findColor(
202 (mag[l + NPOINTS * i1] +
203 mag[l + NPOINTS * i1 + 1] +
204 mag[l + NPOINTS * (i1 + 1)] +
205 mag[l + 1 + NPOINTS * (i1 + 1)]) /
206 4D));
207 g.drawPolygon(sx, sy, 4);
208 if (k == Zbuffer.length / 2 + 1 && AXIS)
209 drawAxis(g);
210 }
211
212 }
213
214 public void drawWireframe(Graphics g) {
215 g.setColor(new Color(-1 - getBackground().getRGB()));
216 for (int i = 0; i < NPOINTS - 1; i++) {
217 for (int j = 0; j < NPOINTS - 1; j++) {
218 g.drawLine(
219 (int) Math.rint(zoom * X[j + NPOINTS * i]) +
220 width / 2,
221 (int) Math.rint(zoom * Y[j + NPOINTS * i]) +
222 height / 2,
223 (int) Math.rint(zoom * X[j + NPOINTS * (i + 1)]) +
224 width / 2,
225 (int) Math.rint(zoom * Y[j + NPOINTS * (i + 1)]) +
226 height / 2);
227 g.drawLine(
228 (int) Math.rint(zoom * X[j + NPOINTS * i]) +
229 width / 2,
230 (int) Math.rint(zoom * Y[j + NPOINTS * i]) +
231 height / 2,
232 (int) Math.rint(zoom * X[j + NPOINTS * i + 1]) +
233 width / 2,
234 (int) Math.rint(zoom * Y[j + NPOINTS * i + 1]) +
235 height / 2);
236 }
237
238 }
239
240 int k = NPOINTS * (NPOINTS - 1);
241 for (int l = 0; l < NPOINTS - 1; l++) {
242 g.drawLine((int) Math.rint(zoom * X[k + l]) + width / 2,
243 (int) Math.rint(zoom * Y[k + l]) + height / 2,
244 (int) Math.rint(zoom * X[k + l + 1]) + width / 2,
245 (int) Math.rint(zoom * Y[k + l + 1]) + height / 2);
246 g.drawLine(
247 (int) Math.rint(zoom * X[(l + 1) * NPOINTS - 1]) +
248 width / 2,
249 (int) Math.rint(zoom * Y[(l + 1) * NPOINTS - 1]) +
250 height / 2,
251 (int) Math.rint(zoom * X[(l + 2) * NPOINTS - 1]) +
252 width / 2,
253 (int) Math.rint(zoom * Y[(l + 2) * NPOINTS - 1]) +
254 height / 2);
255 }
256
257 if (AXIS)
258 drawAxis(g);
259 }
260
261 public void drawAxis(Graphics g) {
262 g.setColor(new Color(-1 - getBackground().getRGB()));
263 g.drawLine(width / 2,
264 height / 2,
265 (int) Math.rint(zoom2 * Axis[0]) + width / 2,
266 (int) Math.rint(zoom2 * Axis[1]) + height / 2);
267 g.drawString(" X ",
268 (int) Math.rint(zoom2 * Axis[0]) + width / 2,
269 (int) Math.rint(zoom2 * Axis[1]) + height / 2);
270 g.drawLine(width / 2,
271 height / 2,
272 (int) Math.rint(zoom2 * Axis[3]) + width / 2,
273 (int) Math.rint(zoom2 * Axis[4]) + height / 2);
274 g.drawString(" Y ",
275 (int) Math.rint(zoom2 * Axis[3]) + width / 2,
276 (int) Math.rint(zoom2 * Axis[4]) + height / 2);
277 g.drawLine(width / 2,
278 height / 2,
279 (int) Math.rint(zoom2 * Axis[6]) + width / 2,
280 (int) Math.rint(zoom2 * Axis[7]) + height / 2);
281 g.drawString(" Z ",
282 (int) Math.rint(zoom2 * Axis[6]) + width / 2,
283 (int) Math.rint(zoom2 * Axis[7]) + height / 2);
284 }
285
286 public void QuickSort(double ad[], int i, int j) {
287 int k = i;
288 int l = j;
289 if (j > i) {
290 double d = ad[(i + j) / 2];
291 while (k <= l) {
292 while (k < j && ad[k] < d)
293 k++;
294 for (; l > i && ad[l] > d; l--) ;
295 if (k <= l) {
296 swap(Zbuffer, k, l);
297 k++;
298 l--;
299 }
300 }
301 if (i < l)
302 QuickSort(ad, i, l);
303 if (k < j)
304 QuickSort(ad, k, j);
305 }
306 }
307
308 private void swap(double ad[], int i, int j) {
309 double d = ad[i];
310 ad[i] = ad[j];
311 ad[j] = d;
312 int k = Fbuffer[i];
313 Fbuffer[i] = Fbuffer[j];
314 Fbuffer[j] = k;
315 }
316
317 public void mouseDragged(MouseEvent mouseevent) {
318 xNew = mouseevent.getX() - width / 2;
319 yNew = mouseevent.getY() - height / 2;
320 Rotation(xNew - xOld, yNew - yOld);
321 xOld = xNew;
322 yOld = yNew;
323 repaint();
324 }
325
326 public void mouseMoved(MouseEvent mouseevent) {
327 }
328
329 public void mouseClicked(MouseEvent mouseevent) {
330 }
331
332 public void mouseEntered(MouseEvent mouseevent) {
333 }
334
335 public void mouseExited(MouseEvent mouseevent) {
336 }
337
338 public void mousePressed(MouseEvent mouseevent) {
339 xOld = mouseevent.getX() - width / 2;
340 yOld = mouseevent.getY() - height / 2;
341 }
342
343 public void mouseReleased(MouseEvent mouseevent) {
344 }
345
346 public Color findColor2(double d) {
347 return new Color(Color.HSBtoRGB((float) (d / cnst), 1.0F, 1.0F));
348 }
349
350 public Color findColor3(double d) {
351 return new Color(
352 Color.HSBtoRGB(1.0F - (float) (d / cnst), 1.0F, 1.0F));
353 }
354
355 public Color findColor(double d) {
356 int i;
357 if (d < 1.0D / cnst)
358 i = (int) (d * 200D * cnst) + 56;
359 else if (d < 5D / cnst) {
360 i = (int) (d * 40D * cnst) + 56;
361 i <<= 8;
362 } else if (d > 20D / cnst) {
363 i = 0xffffff;
364 } else {
365 i = (int) (d * 10D * cnst) + 56;
366 i <<= 16;
367 }
368 return new Color(i);
369 }
370
371 public void calculate() {
372 for (int i = 0; i < NPOINTS; i++) {
373 double d = (2D * xmax * (double) i) / (double) (NPOINTS - 1) -
374 xmax;
375 for (int j = 0; j < NPOINTS; j++) {
376 double d1 = (2D * ymax * (double) j) /
377 (double) (NPOINTS - 1) -
378 ymax;
379 double d2 = 1.0D;
380 for (int k = 0; k < zeros.size(); k++) {
381 double d3 = ((Zero) zeros.elementAt(k)).getReal();
382 double d5 = ((Zero) zeros.elementAt(k)).getImag();
383 d2 *= (d3 - d) * (d3 - d) + (d5 - d1) * (d5 - d1);
384 if (d5 != 0.0D) {
385 d5 = -d5;
386 d2 *= (d3 - d) * (d3 - d) + (d5 - d1) * (d5 - d1);
387 }
388 }
389
390 for (int l = 0; l < poles.size(); l++) {
391 double d4 = ((Pole) poles.elementAt(l)).getReal();
392 double d6 = ((Pole) poles.elementAt(l)).getImag();
393 d2 /= (d4 - d) * (d4 - d) + (d6 - d1) * (d6 - d1);
394 if (d6 != 0.0D) {
395 d6 = -d6;
396 d2 /= (d4 - d) * (d4 - d) + (d6 - d1) * (d6 - d1);
397 }
398 }
399
400 d2 = Math.sqrt(d2);
401 X[j + NPOINTS * i] = d;
402 Y[j + NPOINTS * i] = d1;
403 Z[j + NPOINTS * i] = d2 <= cnst ? d2 : cnst;
404 mag[j + NPOINTS * i] = d2 <= 4D * cnst ? d2 / cnst : cnst;
405 }
406
407 }
408
409 }
410
411 public void rotatePoints() {
412 for (int i = 0; i < NPOINTS; i++) {
413 for (int j = 0; j < NPOINTS; j++) {
414 double d1 = Y[j + NPOINTS * i] * Math.cos(xan) -
415 Z[j + NPOINTS * i] * Math.sin(xan);
416 double d2 = Y[j + NPOINTS * i] * Math.sin(xan) +
417 Z[j + NPOINTS * i] * Math.cos(xan);
418 Y[j + NPOINTS * i] = d1;
419 Z[j + NPOINTS * i] = d2;
420 double d = X[j + NPOINTS * i] * Math.cos(yan) -
421 Z[j + NPOINTS * i] * Math.sin(yan);
422 d2 = X[j + NPOINTS * i] * Math.sin(yan) +
423 Z[j + NPOINTS * i] * Math.cos(yan);
424 X[j + NPOINTS * i] = d;
425 Z[j + NPOINTS * i] = d2;
426 d = X[j + NPOINTS * i] * Math.cos(zan) -
427 Y[j + NPOINTS * i] * Math.sin(zan);
428 d1 = X[j + NPOINTS * i] * Math.sin(zan) +
429 Y[j + NPOINTS * i] * Math.cos(zan);
430 X[j + NPOINTS * i] = d;
431 Y[j + NPOINTS * i] = d1;
432 }
433
434 }
435
436 }
437
438 public void rotateAxis() {
439 for (int i = 0; i < 3; i++) {
440 double d1 = Axis[1 + i * 3] * Math.cos(xan) -
441 Axis[2 + i * 3] * Math.sin(xan);
442 double d2 = Axis[1 + i * 3] * Math.sin(xan) +
443 Axis[2 + i * 3] * Math.cos(xan);
444 Axis[1 + i * 3] = d1;
445 Axis[2 + i * 3] = d2;
446 double d = Axis[i * 3] * Math.cos(yan) -
447 Axis[2 + i * 3] * Math.sin(yan);
448 d2 = Axis[i * 3] * Math.sin(yan) +
449 Axis[2 + i * 3] * Math.cos(yan);
450 Axis[i * 3] = d;
451 Axis[2 + i * 3] = d2;
452 d = Axis[i * 3] * Math.cos(zan) -
453 Axis[1 + i * 3] * Math.sin(zan);
454 d1 = Axis[i * 3] * Math.sin(zan) +
455 Axis[1 + i * 3] * Math.cos(zan);
456 Axis[i * 3] = d;
457 Axis[1 + i * 3] = d1;
458 }
459
460 }
461
462 public void Rotation(int i, int j) {
463 if (i != 0 && j != 0) {
464 double d8 = Math.sqrt(i * i + j * j);
465 double d9 = Math.sqrt(radius * radius + d8 * d8);
466 double d6 = radius / d9;
467 double d7 = d8 / d9;
468 double d10 = (double) j / d8;
469 double d11 = -(double) i / d8;
470 double d12 = 1.0D - d6;
471 for (int k = 0; k < NPOINTS; k++) {
472 for (int l = 0; l < NPOINTS; l++) {
473 double d = X[l + NPOINTS * k] *
474 (d6 + d10 * d10 * d12);
475 d += Y[l + NPOINTS * k] * d10 * d11 * d12;
476 d += Z[l + NPOINTS * k] * d11 * d7;
477 double d2 = X[l + NPOINTS * k] * d10 * d11 * d12;
478 d2 += Y[l + NPOINTS * k] * (d6 + d11 * d11 * d12);
479 d2 += -Z[l + NPOINTS * k] * d10 * d7;
480 double d4 = -X[l + NPOINTS * k] * d11 * d7;
481 d4 += Y[l + NPOINTS * k] * d10 * d7;
482 d4 += Z[l + NPOINTS * k] * d6;
483 X[l + NPOINTS * k] = d;
484 Y[l + NPOINTS * k] = d2;
485 Z[l + NPOINTS * k] = d4;
486 }
487
488 }
489
490 for (int i1 = 0; i1 < 3; i1++) {
491 double d1 = Axis[3 * i1] * (d6 + d10 * d10 * d12);
492 d1 += Axis[1 + 3 * i1] * d10 * d11 * d12;
493 d1 += Axis[2 + 3 * i1] * d11 * d7;
494 double d3 = Axis[3 * i1] * d10 * d11 * d12;
495 d3 += Axis[1 + 3 * i1] * (d6 + d11 * d11 * d12);
496 d3 += -Axis[2 + 3 * i1] * d10 * d7;
497 double d5 = -Axis[3 * i1] * d11 * d7;
498 d5 += Axis[1 + 3 * i1] * d10 * d7;
499 d5 += Axis[2 + 3 * i1] * d6;
500 Axis[3 * i1] = d1;
501 Axis[1 + 3 * i1] = d3;
502 Axis[2 + 3 * i1] = d5;
503 }
504
505 }
506 }
507
508 public void newInfo(Vector vector,
509 Vector vector1,
510 double d,
511 double d1) {
512 xmax = Math.max(d, d1);
513 ymax = xmax;
514 cnst = 2D * d;
515 zoom = 200D / cnst;
516 zeros = new Vector();
517 poles = new Vector();
518 synchronized (this) {
519 if (vector1 != null && vector1.size() > 0) {
520 for (int i = 0; i < vector1.size(); i++)
521 poles.addElement(new Pole((Pole) vector1.elementAt(i)));
522
523 }
524 if (vector != null && vector.size() > 0) {
525 for (int j = 0; j < vector.size(); j++)
526 zeros.addElement(new Zero((Zero) vector.elementAt(j)));
527
528 }
529 }
530 Axis[0] = Axis[4] = Axis[8] = 1.0D;
531 Axis[1] = Axis[2] = Axis[3] = Axis[5] = Axis[6] = Axis[7] = 0.0D;
532 calculate();
533 repaint();
534 }
535
536 public void newBgcolor(Color color) {
537 setBackground(color);
538 repaint();
539 }
540
541 public void newNpoints(int i) {
542 NPOINTS = i;
543 X = new double[NPOINTS * NPOINTS];
544 Y = new double[NPOINTS * NPOINTS];
545 Z = new double[NPOINTS * NPOINTS];
546 mag = new double[NPOINTS * NPOINTS];
547 Zbuffer = new double[(NPOINTS - 1) * (NPOINTS - 1)];
548 Fbuffer = new int[(NPOINTS - 1) * (NPOINTS - 1)];
549 Axis[0] = Axis[4] = Axis[8] = 1.0D;
550 Axis[1] = Axis[2] = Axis[3] = Axis[5] = Axis[6] = Axis[7] = 0.0D;
551 calculate();
552 repaint();
553 }
554
555 public void stopThread() {
556 if (thread != null) {
557 thread = null;
558 }
559 }
560
561 public void startThread() {
562 if (thread == null)
563 thread = new Thread(this);
564 thread.start();
565 }
566
567 public void valueOn(boolean flag) {
568 VALUE = flag;
569 repaint();
570 }
571
572 public void wireOn(boolean flag) {
573 WIRE = flag;
574 repaint();
575 }
576
577 public void axisOn(boolean flag) {
578 AXIS = flag;
579 repaint();
580 }
581
582 public void surfOn(boolean flag) {
583 SURF = flag;
584 repaint();
585 }
586
587 public void palette(int i) {
588 PALETTE = i;
589 repaint();
590 }
591
592 public void rotOn(boolean flag) {
593 if (flag) {
594 startThread();
595 return;
596 } else {
597 stopThread();
598 return;
599 }
600 }
601
602 Vector zeros;
603 Vector poles;
604 double X[];
605 double Y[];
606 double Z[];
607 double mag[];
608 int sx[];
609 int sy[];
610 double Zbuffer[];
611 int Fbuffer[];
612 double Axis[];
613 int width;
614 int height;
615 int xNew;
616 int yNew;
617 int xOld;
618 int yOld;
619 double xmax;
620 double ymax;
621 double K;
622 double xan;
623 double yan;
624 double zan;
625 double zoom;
626 double zoom2;
627 double cnst;
628 double cnst2;
629 double radius;
630 int NPOINTS;
631 int PALETTE;
632 boolean AXIS;
633 boolean WIRE;
634 boolean VALUE;
635 boolean SURF;
636 Dimension offDimension;
637 Image offImage;
638 Graphics offGraphics;
639 transient Thread thread;
640 }
641