Camera_impl.java
Go to the documentation of this file.
1 // -*- indent-tabs-mode: nil; tab-width: 4; -*-
2 /*
3  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
4  * All rights reserved. This program is made available under the terms of the
5  * Eclipse Public License v1.0 which accompanies this distribution, and is
6  * available at http://www.eclipse.org/legal/epl-v10.html
7  * Contributors:
8  * General Robotix Inc.
9  * National Institute of Advanced Industrial Science and Technology (AIST)
10  */
11 package com.generalrobotix.ui.view.vsensor;
12 
13 import java.awt.Canvas;
14 import java.awt.EventQueue;
15 import java.awt.FlowLayout;
16 import java.awt.Graphics;
17 import java.awt.image.BufferedImage;
18 
19 import javax.swing.*;
20 
21 import javax.media.j3d.*;
22 import javax.vecmath.*;
23 
25 
26 import jp.go.aist.hrp.simulator.CameraPOA;
27 import jp.go.aist.hrp.simulator.ImageData;
28 import jp.go.aist.hrp.simulator.PixelFormat;
29 import jp.go.aist.hrp.simulator.CameraPackage.*;
30 
36 public class Camera_impl extends CameraPOA {
37 
38  // camera parameter
39  private CameraParameter param_;
40 
41  private ImageData image_;
42  private Raster raster_;
43 
44  // screen size
45  private int width_;
46  private int height_;
47 
48  // branch-graph of the viewpoint
49  private BranchGroup bgVp_;
50  private TransformGroup tgVp_;
51  private Canvas3DI canvas_;
52  private MyCanvas canvas2;
53  private ViewPlatform vplatform_;
54  private View view_;
55  private PhysicalBody pbody_;
56  private PhysicalEnvironment penv_;
57 
58  private JFrame frm_;
59 
60  private int lastRenderedFrame_=0;
61 
62  // ---------- Constructor ----------
63 
69  public Camera_impl(CameraParameter param, boolean offScreen) {
70  param_ = param;
71  //
72  // raster
73  //
74 
75  // save screen size
76  width_ = param.width;
77  height_ = param.height;
78 
79  image_ = new ImageData();
80 
81  image_.width = param_.width;
82  image_.height = param_.height;
83 
84  image_.octetData = new byte[1];
85  image_.longData = new int[1];
86  image_.floatData = new float[1];
87 
88  // camera type
89  CameraType cameraType = param.type;
90 
91  if (cameraType == CameraType.MONO)
92  image_.format = PixelFormat.GRAY;
93  else
94  image_.format = PixelFormat.ARGB;
95 
96  // create color information for reading color buffer
97  // type int, (Alpha:8bit,) R:8bit, G:8bit, B:8bit
98  BufferedImage bimageRead = null;
99  ImageComponent2D readImage = null;
100  if (cameraType == CameraType.COLOR || cameraType == CameraType.COLOR_DEPTH ||
101  cameraType == CameraType.MONO || cameraType == CameraType.MONO_DEPTH) {
102  bimageRead = new BufferedImage(width_, height_, BufferedImage.TYPE_INT_RGB);
103  readImage = new ImageComponent2D(ImageComponent.FORMAT_RGB, bimageRead);
104  }
105 
106  // create depth information for reading depth buffer
107  DepthComponentFloat readDepthFloat = null;
108  if (cameraType == CameraType.DEPTH
109  || cameraType == CameraType.COLOR_DEPTH
110  || cameraType == CameraType.MONO_DEPTH) {
111  readDepthFloat = new DepthComponentFloat(width_, height_);
112  }
113 
114  // create raster
115  int rasterType = -1;
116  if (cameraType != CameraType.NONE) {
117  if (cameraType == CameraType.COLOR
118  || cameraType == CameraType.MONO)
119  rasterType = Raster.RASTER_COLOR;
120 
121  else if (cameraType == CameraType.DEPTH)
122  rasterType = Raster.RASTER_DEPTH;
123 
124  else
125  rasterType = Raster.RASTER_COLOR_DEPTH;
126 
127  raster_ = new Raster(
128  new Point3f(), rasterType,
129  0, 0, width_, height_,
130  readImage, readDepthFloat
131  );
132  }
133 
134  //
135  // create branch-graph elements
136  //
137 
138  bgVp_ = new BranchGroup();
139  bgVp_.setCapability(BranchGroup.ALLOW_DETACH);
140  bgVp_.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
141  bgVp_.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
142  bgVp_.setUserData(param_.defName);
143 
144  tgVp_ = new TransformGroup();
145  tgVp_.setCapability(TransformGroup.ALLOW_CHILDREN_READ);
146  tgVp_.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
147  tgVp_.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
148 
149  vplatform_ = new ViewPlatform();
150 
151  view_ = new View();
152  view_.setFrontClipPolicy(View.VIRTUAL_EYE);
153  view_.setBackClipPolicy(View.VIRTUAL_EYE);
154  view_.setFrontClipDistance(param.frontClipDistance);
155  view_.setBackClipDistance(param.backClipDistance);
156  view_.setFieldOfView(param.fieldOfView);
157 
158  pbody_ = new PhysicalBody();
159 
160  penv_ = new PhysicalEnvironment();
161 
162  if (offScreen) {
163  canvas_ = new OffScreenCanvas3D(
165  raster_,
166  width_,
167  height_,
168  rasterType
169  );
170  } else {
171  canvas_ = new OnScreenCanvas3D(
173  raster_,
174  width_,
175  height_,
176  rasterType
177  );
178  }
179 
180  //
181  // construct branch-graph
182  //
183 
184  bgVp_.addChild(tgVp_);
185  tgVp_.addChild(vplatform_);
186  view_.addCanvas3D(canvas_);
187  view_.attachViewPlatform(vplatform_);
188  view_.setPhysicalBody(pbody_);
189  view_.setPhysicalEnvironment(penv_);
190 
191  //
192  // Swing
193  //
194 
195  frm_ = new JFrame(param.sensorName);
196  frm_.setSize(width_ + 20, height_ + 30);
197  frm_.getContentPane().setLayout(new FlowLayout());
198  if (offScreen){
199  canvas2 = new MyCanvas();
200  canvas2.setSize(width_, height_);
201  frm_.getContentPane().add(canvas2);
202  }else{
203  frm_.getContentPane().add(canvas_);
204  }
205  frm_.pack();
206  frm_.setResizable(false);
207  frm_.setAlwaysOnTop(true);
208  }
209 
210  @SuppressWarnings("serial")
211  public class MyCanvas extends Canvas{
212  BufferedImage bImage_;
213  public void paint(Graphics g){
214  if (bImage_ == null){
215  bImage_ = new BufferedImage(width_, height_, BufferedImage.TYPE_INT_ARGB);
216  }
217  if (getColorBuffer().length > 1){
218  bImage_.setRGB(0,0,width_,height_,getColorBuffer(),
219  0,width_);
220  g.drawImage(bImage_, 0, 0, null);
221  }
222  }
223  public void update(Graphics g){
224  paint(g);
225  }
226  }
227 
228  // ------------ Camera interface implementation ------------
229 
233  public void destroy() {
234  System.out.println("Camera_impl::destroy()");
235  //これらのif(...!=null)は
236  //二重destroy時のエラーを防ぐため
237  if(canvas_!=null)
238  canvas_.getView().removeCanvas3D(canvas_);
239 
240  if(frm_!=null) {
241  EventQueue.invokeLater(new Runnable () {
242  public void run() {
243  frm_.setVisible(false);
244  frm_.dispose ();
245  frm_ = null;
246  }
247  });
248  }
249 
250  raster_ = null;
251 
252  if(bgVp_!=null) {
253  bgVp_.detach();
254  bgVp_ = null;
255  }
256 
257  tgVp_ = null;
258  canvas_ = null;
259  vplatform_ = null;
260  view_ = null;
261  pbody_ = null;
262  penv_ = null;
263  }
264 
269  public CameraParameter getCameraParameter() {
270  return param_;
271  }
272 
273  public boolean isVisible(){
274  return frm_.isVisible();
275  }
276 
277  public void setVisible(boolean b){
278  if (frm_.isVisible() != b){
279  frm_.setVisible(b);
280  if (b){
281  // note: setVisible returns immediately.
282  // But it takes for a while until the window become really visible
283  try{
284  Thread.sleep(1000);
285  }catch(Exception e){}
286  }
287  }
288  }
289 
290  public void updateView(double time) {
291  if (!canvas_.isOffScreen()) setVisible(true);
292 
293  int frame = (int)(time*param_.frameRate);
294  if (time == 0 || frame != lastRenderedFrame_){
296  if (canvas_.isOffScreen()) {
297  canvas2.repaint();
298  }
299  lastRenderedFrame_ = frame;
300  }
301  }
302 
307  public int[] getColorBuffer() {
308  return canvas_.getColorBuffer();
309  }
310 
315  public float[] getDepthBuffer() {
316  return canvas_.getDepthBuffer();
317  }
318 
319  public ImageData getImageData() {
320  if (!canvas_.isOffScreen()) setVisible(true);
321 
322  if (param_.type == CameraType.COLOR ||
323  param_.type == CameraType.COLOR_DEPTH){
324  image_.longData = canvas_.getColorBuffer();
325  }
326 
327  if (param_.type == CameraType.MONO ||
328  param_.type == CameraType.MONO_DEPTH){
329  image_.octetData = canvas_.getMonoBuffer();
330  }
331 
332  if (param_.type == CameraType.DEPTH ||
333  param_.type == CameraType.COLOR_DEPTH){
334  image_.floatData = canvas_.getDepthBuffer();
335  }
336  return image_;
337  }
338 
343  public BranchGroup getBranchGroup() {
344  return bgVp_;
345  }
346 
347  public TransformGroup getTransformGroup() {
348  return tgVp_;
349  }
350 }
#define null
our own NULL pointer
Definition: IceTypes.h:57
png_bytep png_bytep png_size_t length
Definition: png.h:1541
long b
Definition: jpegint.h:371
static final GraphicsConfiguration graphicsConfiguration
Definition: Grx3DView.java:98
def run(tree, args)
typedef int
Definition: png.h:1113
Camera_impl(CameraParameter param, boolean offScreen)


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Sep 8 2022 02:24:02