Camera_impl.java
Go to the documentation of this file.
00001 // -*- indent-tabs-mode: nil; tab-width: 4; -*-
00002 /*
00003  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
00004  * All rights reserved. This program is made available under the terms of the
00005  * Eclipse Public License v1.0 which accompanies this distribution, and is
00006  * available at http://www.eclipse.org/legal/epl-v10.html
00007  * Contributors:
00008  * General Robotix Inc.
00009  * National Institute of Advanced Industrial Science and Technology (AIST) 
00010  */
00011 package com.generalrobotix.ui.view.vsensor;
00012 
00013 import java.awt.Canvas;
00014 import java.awt.EventQueue;
00015 import java.awt.FlowLayout;
00016 import java.awt.Graphics;
00017 import java.awt.image.BufferedImage;
00018 
00019 import javax.swing.*;
00020 
00021 import javax.media.j3d.*;
00022 import javax.vecmath.*;
00023 
00024 import com.generalrobotix.ui.view.Grx3DView;
00025 
00026 import jp.go.aist.hrp.simulator.CameraPOA;
00027 import jp.go.aist.hrp.simulator.ImageData;
00028 import jp.go.aist.hrp.simulator.PixelFormat;
00029 import jp.go.aist.hrp.simulator.CameraPackage.*;
00030 
00036 public class Camera_impl extends CameraPOA {
00037 
00038         // camera parameter
00039         private CameraParameter param_;
00040 
00041         private ImageData image_;
00042         private Raster  raster_;
00043         
00044         // screen size
00045         private int     width_;
00046         private int     height_;
00047 
00048         // branch-graph of the viewpoint
00049         private BranchGroup                     bgVp_;
00050         private TransformGroup          tgVp_;
00051         private Canvas3DI                       canvas_;
00052         private MyCanvas                        canvas2;
00053         private ViewPlatform            vplatform_;
00054         private View                            view_;
00055         private PhysicalBody            pbody_;
00056         private PhysicalEnvironment     penv_;
00057         
00058         private JFrame  frm_;
00059 
00060         private int lastRenderedFrame_=0;
00061 
00062         // ---------- Constructor ----------
00063 
00069         public Camera_impl(CameraParameter param, boolean offScreen) {
00070                 param_ = param;
00071                 //
00072                 // raster
00073                 //
00074 
00075                 // save screen size
00076                 width_ = param.width;
00077                 height_ = param.height;
00078 
00079                 image_ = new ImageData();
00080 
00081                 image_.width = param_.width;
00082                 image_.height = param_.height;
00083                 
00084                 image_.octetData = new byte[1];
00085                 image_.longData = new int[1];
00086                 image_.floatData = new float[1];
00087                 
00088                 // camera type
00089                 CameraType cameraType = param.type;
00090 
00091                 if (cameraType == CameraType.MONO)
00092                     image_.format = PixelFormat.GRAY;
00093                 else
00094                     image_.format = PixelFormat.ARGB;
00095 
00096                 // create color information for reading color buffer
00097                 // type int, (Alpha:8bit,) R:8bit, G:8bit, B:8bit
00098                 BufferedImage bimageRead = null;
00099                 ImageComponent2D readImage = null;
00100                 if (cameraType == CameraType.COLOR || cameraType == CameraType.COLOR_DEPTH ||
00101                                 cameraType == CameraType.MONO || cameraType == CameraType.MONO_DEPTH) {
00102                         bimageRead = new BufferedImage(width_, height_, BufferedImage.TYPE_INT_RGB);
00103                         readImage = new ImageComponent2D(ImageComponent.FORMAT_RGB, bimageRead);
00104                 }
00105 
00106                 // create depth information for reading depth buffer
00107                 DepthComponentFloat readDepthFloat = null;
00108                 if (cameraType == CameraType.DEPTH 
00109                                 || cameraType == CameraType.COLOR_DEPTH
00110                                 || cameraType == CameraType.MONO_DEPTH) {
00111                         readDepthFloat = new DepthComponentFloat(width_, height_);
00112                 }
00113 
00114                 // create raster
00115                 int rasterType = -1;
00116                 if (cameraType != CameraType.NONE) {
00117                         if (cameraType == CameraType.COLOR 
00118                                         || cameraType == CameraType.MONO) 
00119                                 rasterType = Raster.RASTER_COLOR;
00120                         
00121                         else if (cameraType == CameraType.DEPTH)
00122                                 rasterType = Raster.RASTER_DEPTH;
00123                         
00124                         else 
00125                                 rasterType = Raster.RASTER_COLOR_DEPTH;
00126 
00127                         raster_ = new Raster(
00128                                 new Point3f(), rasterType,
00129                                 0, 0, width_, height_,
00130                                 readImage, readDepthFloat
00131                         );
00132                 }
00133 
00134                 //
00135                 // create branch-graph elements
00136                 //
00137 
00138                 bgVp_ = new BranchGroup();
00139                 bgVp_.setCapability(BranchGroup.ALLOW_DETACH);
00140                 bgVp_.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
00141                 bgVp_.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
00142                 bgVp_.setUserData(param_.defName);
00143 
00144                 tgVp_ = new TransformGroup();
00145                 tgVp_.setCapability(TransformGroup.ALLOW_CHILDREN_READ);
00146                 tgVp_.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
00147                 tgVp_.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
00148 
00149                 vplatform_ = new ViewPlatform();
00150 
00151                 view_ = new View();
00152                 view_.setFrontClipPolicy(View.VIRTUAL_EYE);
00153                 view_.setBackClipPolicy(View.VIRTUAL_EYE);
00154                 view_.setFrontClipDistance(param.frontClipDistance);
00155                 view_.setBackClipDistance(param.backClipDistance);
00156                 view_.setFieldOfView(param.fieldOfView);
00157 
00158                 pbody_ = new PhysicalBody();
00159 
00160                 penv_ = new PhysicalEnvironment();
00161 
00162         if (offScreen) {
00163             canvas_ = new OffScreenCanvas3D(
00164                         Grx3DView.graphicsConfiguration,
00165                                         raster_,
00166                                         width_,
00167                                         height_,
00168                                         rasterType
00169                                 );
00170         } else {
00171             canvas_ = new OnScreenCanvas3D(
00172                         Grx3DView.graphicsConfiguration,
00173                                         raster_,
00174                                         width_,
00175                                         height_,
00176                                         rasterType
00177                                 );
00178                 }
00179 
00180                 //
00181                 // construct branch-graph
00182                 //
00183 
00184                 bgVp_.addChild(tgVp_);
00185                 tgVp_.addChild(vplatform_);
00186                 view_.addCanvas3D(canvas_);
00187                 view_.attachViewPlatform(vplatform_);
00188                 view_.setPhysicalBody(pbody_);
00189                 view_.setPhysicalEnvironment(penv_);
00190 
00191                 //
00192                 // Swing
00193                 //
00194                 
00195                 frm_ = new JFrame(param.sensorName);
00196                 frm_.setSize(width_ + 20, height_ + 30);
00197                 frm_.getContentPane().setLayout(new FlowLayout());
00198         if (offScreen){
00199             canvas2 = new MyCanvas();
00200             canvas2.setSize(width_, height_);
00201             frm_.getContentPane().add(canvas2);
00202         }else{
00203             frm_.getContentPane().add(canvas_);
00204         }
00205                 frm_.pack();
00206                 frm_.setResizable(false);
00207                 frm_.setAlwaysOnTop(true);
00208         }
00209         
00210         @SuppressWarnings("serial")
00211         public class MyCanvas extends Canvas{
00212         BufferedImage bImage_;
00213         public void paint(Graphics g){
00214             if (bImage_ == null){
00215                 bImage_ = new BufferedImage(width_, height_, BufferedImage.TYPE_INT_ARGB);
00216             }
00217             if (getColorBuffer().length > 1){
00218                 bImage_.setRGB(0,0,width_,height_,getColorBuffer(),
00219                               0,width_); 
00220                 g.drawImage(bImage_, 0, 0, null);
00221             }
00222         }
00223         public void update(Graphics g){
00224             paint(g);
00225         }
00226     }
00227 
00228         // ------------ Camera interface implementation ------------
00229 
00233         public void destroy() {
00234                 System.out.println("Camera_impl::destroy()");
00235             //これらのif(...!=null)は
00236             //二重destroy時のエラーを防ぐため
00237                 if(canvas_!=null)
00238                 canvas_.getView().removeCanvas3D(canvas_);
00239                 
00240         if(frm_!=null) {
00241                 EventQueue.invokeLater(new Runnable () {
00242                                 public void run() {
00243                                         frm_.setVisible(false);
00244                                         frm_.dispose ();
00245                                         frm_ = null;
00246                                 }
00247                 });
00248         }
00249 
00250                 raster_ = null;
00251 
00252         if(bgVp_!=null) {
00253                 bgVp_.detach();
00254                         bgVp_ = null;
00255                 }
00256         
00257                 tgVp_ = null;
00258                 canvas_ = null;
00259                 vplatform_ = null;
00260                 view_ = null;
00261                 pbody_ = null;
00262                 penv_ = null;
00263         }
00264 
00269         public CameraParameter getCameraParameter() {
00270                 return param_;
00271         }
00272 
00273     public boolean isVisible(){
00274         return frm_.isVisible();
00275     }
00276 
00277         public void setVisible(boolean b){
00278         if (frm_.isVisible() != b){
00279             frm_.setVisible(b);
00280             if (b){
00281                 // note: setVisible returns immediately. 
00282                 // But it takes for a while until the window become really visible
00283                 try{
00284                     Thread.sleep(1000);
00285                 }catch(Exception e){}
00286             }
00287         }
00288         }
00289         
00290         public void updateView(double time) {
00291                 if (!canvas_.isOffScreen()) setVisible(true);
00292                 
00293                 int frame = (int)(time*param_.frameRate);
00294                 if (time == 0 || frame != lastRenderedFrame_){
00295                         canvas_.renderOnce();
00296                         if (canvas_.isOffScreen()) {
00297                                 canvas2.repaint();
00298                         }
00299                         lastRenderedFrame_ = frame;
00300                 }
00301         }
00302         
00307         public int[] getColorBuffer() {
00308                 return canvas_.getColorBuffer();
00309         }
00310 
00315         public float[] getDepthBuffer() {
00316                 return canvas_.getDepthBuffer();
00317         }
00318 
00319         public ImageData getImageData() {
00320         if (!canvas_.isOffScreen()) setVisible(true);
00321 
00322         if (param_.type == CameraType.COLOR ||
00323             param_.type == CameraType.COLOR_DEPTH){
00324             image_.longData = canvas_.getColorBuffer();
00325         }
00326         
00327         if (param_.type == CameraType.MONO ||
00328             param_.type == CameraType.MONO_DEPTH){
00329             image_.octetData = canvas_.getMonoBuffer();
00330         }
00331           
00332         if (param_.type == CameraType.DEPTH ||
00333             param_.type == CameraType.COLOR_DEPTH){
00334             image_.floatData = canvas_.getDepthBuffer();
00335         }
00336         return image_;
00337         }
00338 
00343         public BranchGroup getBranchGroup() {
00344                 return bgVp_;
00345         }
00346 
00347         public TransformGroup getTransformGroup() {
00348                 return tgVp_;
00349         }
00350 }


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Apr 11 2019 03:30:15