00001 package edu.tum.cs.ias.knowrob.vis;
00002
00003 import java.awt.Color;
00004 import java.awt.Dimension;
00005 import java.awt.Frame;
00006 import java.awt.GraphicsDevice;
00007 import java.awt.GraphicsEnvironment;
00008 import java.awt.Insets;
00009 import java.awt.Label;
00010 import java.awt.SystemColor;
00011 import java.awt.event.MouseAdapter;
00012 import java.awt.event.MouseEvent;
00013 import java.awt.event.MouseListener;
00014 import java.awt.event.MouseMotionListener;
00015 import java.awt.event.WindowAdapter;
00016 import java.awt.event.WindowEvent;
00017 import java.util.Vector;
00018 import javax.vecmath.Matrix4f;
00019
00020 import javax.vecmath.Vector3f;
00021
00022 import processing.core.PApplet;
00023 import processing.core.PFont;
00024 import processing.core.PGraphics3D;
00025
00026
00027 public class Canvas extends PApplet implements MouseListener, MouseMotionListener {
00028 static final long serialVersionUID = 0;
00029
00033 protected int width = 800, height = 600;
00034
00038 protected boolean useCamera;
00039
00043 protected float leftMouseX = -1.0f, leftMouseY = -1.0f, rightMouseX = -1.0f, rightMouseY = -1.0f, centerMouseY = -1.0f;
00044
00048 protected float xRotDisplay, yRotDisplay, xShiftDisplay, yShiftDisplay, zShiftDisplay, zoomDisplay;
00049
00053 protected Vector3f eye, eyeTarget, eyeUp;
00054
00058 protected float sceneSize = 4000;
00059
00060 public boolean debugCamera;
00061
00065 Vector<Drawable> items = new Vector<Drawable>();
00066 Vector<Drawable> items2D = new Vector<Drawable>();
00067
00072 public void setWidth(int width) {
00073 this.width = width;
00074 }
00075
00080 public void setHeight(int height) {
00081 this.height = height;
00082 }
00083
00084 public int getHeight() {
00085 return height;
00086 }
00087
00088 public int getWidth() {
00089 return width;
00090 }
00091
00092 public void setSceneSize(float size) {
00093 this.sceneSize = size;
00094 this.zoomDisplay = 400 / sceneSize;
00095 }
00096
00097 public Canvas() {
00098 useCamera = false;
00099 debugCamera = false;
00100 eye = new Vector3f(50.0f,50f,20f);
00101 eyeUp = new Vector3f(0,0,500);
00102 eyeTarget = new Vector3f(0,0,0);
00103 setStandardViewParams();
00104 }
00105
00109 public void setStandardViewParams() {
00110 xRotDisplay = -106.25027f;
00111 yRotDisplay = 0.020062504f;
00112 xShiftDisplay = 103f;
00113 zShiftDisplay = 162f;
00114 setSceneSize(this.sceneSize);
00115 }
00116
00117 public void setEyeTarget(Vector3f v) {
00118 this.eyeTarget.set(v);
00119 }
00120
00121 public void setup() {
00122 size(width, height, PGraphics3D.P3D);
00123 lights();
00124
00125 PFont font = createFont("Verdana", 12);
00126 textFont(font);
00127
00128 ellipseMode(RADIUS);
00129 frameRate(20);
00130
00131 noLoop();
00132 draw();
00133 }
00134
00135 public void draw() {
00136
00137 background(60, 60, 60);
00138 cursor(CROSS);
00139
00140 if(!useCamera) {
00141 camera();
00142
00143 pushMatrix();
00144 translate(width / 4.0f, height / 1.5f, -400.0f);
00145
00146 lights();
00147
00148 rotateZ(PI / 2);
00149 rotateY(-PI / 2);
00150 translate(0.0f, zShiftDisplay, xShiftDisplay);
00151
00152 rotateZ(radians(xRotDisplay));
00153 rotateX(radians(yRotDisplay));
00154
00155 scale(zoomDisplay);
00156
00157
00158 }
00159 else {
00160 lights();
00161
00162 setCamera();
00163 }
00164
00165 preDrawItems();
00166
00167 drawItems();
00168
00169
00170 if(debugCamera) {
00171 float coordlength = sceneSize / 8;
00172 Vector3f up = new Vector3f(eyeUp);
00173 up.scale(coordlength);
00174 up.add(eyeTarget);
00175 this.drawLine(eyeTarget, up, 0xff0000ff);
00176 Vector3f dir = new Vector3f(eyeTarget);
00177 dir.sub(eye);
00178 Vector3f third = new Vector3f();
00179 third.cross(dir, up);
00180 third.normalize();
00181 third.scale(coordlength);
00182 third.add(eyeTarget);
00183 this.drawLine(eyeTarget, third, 0xff00ff00);
00184 this.drawLine(eye, eyeTarget, 0xffff0000);
00185 }
00186
00187 if(!useCamera)
00188 popMatrix();
00189 else {
00190
00191 }
00192
00193 for(Drawable d : items2D)
00194 d.draw(this);
00195 }
00196
00197 protected void preDrawItems() {
00198
00199 }
00200
00201 protected void setCamera() {
00202
00203 camera(eye.x, eye.y, eye.z, eyeTarget.x, eyeTarget.y, eyeTarget.z, eyeUp.x, eyeUp.y, eyeUp.z);
00204
00205
00206
00207
00208 }
00209
00210 public synchronized void drawItems() {
00211 for(Drawable d : items)
00212 d.draw(this);
00213 }
00214
00215 public void drawLine(Vector3f p1, Vector3f p2, int color) {
00216
00217 drawLine(p1.x, p1.y, p1.z, p2.x, p2.y, p2.z, color);
00218
00219 }
00220
00221 public void drawLine(float x1, float y1, float z1, float x2, float y2, float z2, int color) {
00222 stroke(color);
00223 line(x1,y1,z1,x2,y2,z2);
00224 }
00225
00226 public synchronized void add(Drawable d) {
00227 this.items.add(d);
00228 }
00229
00230 public synchronized void add2D(Drawable d) {
00231 this.items2D.add(d);
00232 }
00233
00234 public void rotateAxis(double theta, Vector3f rotAxis){
00235 Matrix4f mat = edu.tum.cs.ias.knowrob.util.datastructures.Vector3f.getRotationMatrix(theta,rotAxis);
00236 applyMatrix(mat.m00, mat.m01, mat.m02, mat.m03,
00237 mat.m10, mat.m11, mat.m12, mat.m13,
00238 mat.m20, mat.m21, mat.m22, mat.m23,
00239 mat.m30, mat.m31, mat.m32, mat.m33);
00240 }
00241
00245
00246
00247
00248
00249 @Override
00250 public void keyPressed() {
00251 switch(keyCode) {
00252 case java.awt.event.KeyEvent.VK_C:
00253 useCamera = !useCamera;
00254
00255 redraw();
00256 System.out.println("Turned camera " + (useCamera ? "on" : "off"));
00257 break;
00258 }
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269 }
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00404 public void runMain() {
00405
00406 if(platform == MACOSX) {
00407
00408
00409 System.setProperty("apple.awt.graphics.UseQuartz", "true");
00410 }
00411
00412 try {
00413 boolean external = false;
00414
00415 String name = null;
00416 boolean present = false;
00417 Color backgroundColor = Color.black;
00418 Color stopColor = Color.gray;
00419 GraphicsDevice displayDevice = null;
00420 boolean hideStop = false;
00421
00422
00423
00424
00425 String folder = null;
00426 try {
00427 folder = System.getProperty("user.dir");
00428 }
00429 catch (Exception e) {
00430 }
00431
00432
00433
00434
00435
00436
00437 if(displayDevice == null) {
00438 GraphicsEnvironment environment = GraphicsEnvironment
00439 .getLocalGraphicsEnvironment();
00440 displayDevice = environment.getDefaultScreenDevice();
00441 }
00442
00443 Frame frame = new Frame(displayDevice.getDefaultConfiguration());
00444
00445
00446
00447
00448 frame.setResizable(false);
00449
00450
00452
00453 frame.setTitle(name);
00454
00455
00456
00457
00458 PApplet applet = this;
00459
00460
00461 applet.frame = frame;
00462 applet.sketchPath = folder;
00463
00465
00466
00467
00468
00469
00470
00471
00472
00473 if(present) {
00474 frame.setUndecorated(true);
00475 frame.setBackground(backgroundColor);
00476 displayDevice.setFullScreenWindow(frame);
00477 }
00478 frame.setLayout(null);
00479 frame.add(applet);
00480 frame.pack();
00481
00482 applet.init();
00483
00484
00485
00486
00487
00488 while(applet.defaultSize && !applet.finished) {
00489
00490 try {
00491 Thread.sleep(5);
00492
00493 }
00494 catch (InterruptedException e) {
00495
00496 }
00497 }
00498
00499
00500
00501 if(present) {
00502
00503
00504
00505
00506
00507 Dimension fullscreen = frame.getSize();
00508 applet.setBounds((fullscreen.width - applet.width) / 2,
00509 (fullscreen.height - applet.height) / 2, applet.width,
00510 applet.height);
00511
00512 if(!hideStop) {
00513 Label label = new Label("stop");
00514 label.setForeground(stopColor);
00515 label.addMouseListener(new MouseAdapter() {
00516 public void mousePressed(MouseEvent e) {
00517 System.exit(0);
00518 }
00519 });
00520 frame.add(label);
00521
00522 Dimension labelSize = label.getPreferredSize();
00523
00524
00525 labelSize = new Dimension(100, labelSize.height);
00526 label.setSize(labelSize);
00527 label.setLocation(20, fullscreen.height - labelSize.height
00528 - 20);
00529 }
00530
00531
00532 if(external) {
00533 applet.setupExternalMessages();
00534 }
00535
00536 }
00537 else {
00538
00539
00540
00541 Insets insets = frame.getInsets();
00542
00543 int windowW = Math.max(applet.width, MIN_WINDOW_WIDTH)
00544 + insets.left + insets.right;
00545 int windowH = Math.max(applet.height, MIN_WINDOW_HEIGHT)
00546 + insets.top + insets.bottom;
00547
00548 frame.setSize(windowW, windowH);
00549
00550 if(external) {
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574 }
00575 else {
00576 frame.setLocation((applet.screenWidth - applet.width) / 2,
00577 (applet.screenHeight - applet.height) / 2);
00578 }
00579
00580
00581
00582
00583 if(backgroundColor == Color.black) {
00584
00585 backgroundColor = SystemColor.control;
00586 }
00587 frame.setBackground(backgroundColor);
00588
00589 int usableWindowH = windowH - insets.top - insets.bottom;
00590 applet.setBounds((windowW - applet.width) / 2, insets.top
00591 + (usableWindowH - applet.height) / 2, applet.width,
00592 applet.height);
00593
00594 if(external) {
00595 applet.setupExternalMessages();
00596
00597 }
00598 else {
00599 frame.addWindowListener(new WindowAdapter() {
00600 public void windowClosing(WindowEvent e) {
00601 System.exit(0);
00602 }
00603 });
00604 }
00605
00606
00607 applet.setupFrameResizeListener();
00608
00609
00610 if(applet.displayable()) {
00611 frame.setVisible(true);
00612 }
00613 }
00614
00615
00616
00617 applet.requestFocus();
00618
00619
00620 }
00621 catch (Exception e) {
00622 e.printStackTrace();
00623 System.exit(1);
00624 }
00625 }
00626 }