Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 package org.ros.android.view.visualization;
00018
00019 import com.google.common.base.Preconditions;
00020
00021 import org.ros.math.RosMath;
00022 import org.ros.rosjava_geometry.FrameTransform;
00023 import org.ros.rosjava_geometry.FrameTransformTree;
00024 import org.ros.rosjava_geometry.FrameName;
00025 import org.ros.rosjava_geometry.Transform;
00026 import org.ros.rosjava_geometry.Vector3;
00027
00028 import javax.microedition.khronos.opengles.GL10;
00029
00034 public class Camera {
00035
00041 private static final double DEFAULT_ZOOM = 100.0;
00042
00046 private static final float MINIMUM_ZOOM = 10;
00047
00051 private static final float MAXIMUM_ZOOM = 500;
00052
00053 private final FrameTransformTree frameTransformTree;
00054 private final Object mutex;
00055
00056 private Viewport viewport;
00057 private Transform transform;
00058
00065 private FrameName frame;
00066
00067 public Camera(FrameTransformTree frameTransformTree) {
00068 this.frameTransformTree = frameTransformTree;
00069 mutex = new Object();
00070 resetTransform();
00071 }
00072
00073 private void resetTransform() {
00074
00075 transform = Transform.zRotation(Math.PI / 2).scale(DEFAULT_ZOOM);
00076 }
00077
00078 public void apply(GL10 gl) {
00079 synchronized (mutex) {
00080 OpenGlTransform.apply(gl, transform);
00081 }
00082 }
00083
00084 public boolean applyFrameTransform(GL10 gl, FrameName frame) {
00085 Preconditions.checkNotNull(frame);
00086 if (this.frame != null) {
00087 FrameTransform frameTransform = frameTransformTree.transform(frame, this.frame);
00088 if (frameTransform != null) {
00089 OpenGlTransform.apply(gl, frameTransform.getTransform());
00090 return true;
00091 }
00092 }
00093 return false;
00094 }
00095
00104 public void translate(double deltaX, double deltaY) {
00105 synchronized (mutex) {
00106 transform = Transform.translation(deltaX, deltaY, 0).multiply(transform);
00107 }
00108 }
00109
00120 public void rotate(double focusX, double focusY, double deltaAngle) {
00121 synchronized (mutex) {
00122 Transform focus = Transform.translation(toMetricCoordinates((int) focusX, (int) focusY));
00123 transform =
00124 transform.multiply(focus).multiply(Transform.zRotation(deltaAngle))
00125 .multiply(focus.invert());
00126 }
00127 }
00128
00139 public void zoom(double focusX, double focusY, double factor) {
00140 synchronized (mutex) {
00141 Transform focus = Transform.translation(toMetricCoordinates((int) focusX, (int) focusY));
00142 double zoom = RosMath.clamp(getZoom() * factor, MINIMUM_ZOOM, MAXIMUM_ZOOM) / getZoom();
00143 transform = transform.multiply(focus).scale(zoom).multiply(focus.invert());
00144 }
00145 }
00146
00150 public double getZoom() {
00151 return transform.getScale();
00152 }
00153
00158 public Vector3 toMetricCoordinates(int x, int y) {
00159 double centeredX = x - viewport.getWidth() / 2.0d;
00160 double centeredY = viewport.getHeight() / 2.0d - y;
00161 return transform.invert().apply(new Vector3(centeredX, centeredY, 0));
00162 }
00163
00164 public FrameName getFrame() {
00165 return frame;
00166 }
00167
00176 public void setFrame(FrameName frame) {
00177 Preconditions.checkNotNull(frame);
00178 synchronized (mutex) {
00179 if (this.frame != null && this.frame != frame) {
00180 FrameTransform frameTransform = frameTransformTree.transform(frame, this.frame);
00181 if (frameTransform != null) {
00182
00183
00184 transform = transform.multiply(frameTransform.getTransform());
00185 }
00186 }
00187 this.frame = frame;
00188 }
00189 }
00190
00194 public void setFrame(String frame) {
00195 setFrame(FrameName.of(frame));
00196 }
00197
00205 public void jumpToFrame(FrameName frame) {
00206 synchronized (mutex) {
00207 this.frame = frame;
00208 double zoom = getZoom();
00209 resetTransform();
00210 transform = transform.scale(zoom / getZoom());
00211 }
00212 }
00213
00217 public void jumpToFrame(String frame) {
00218 jumpToFrame(FrameName.of(frame));
00219 }
00220
00221 public void setViewport(Viewport viewport) {
00222 Preconditions.checkNotNull(viewport);
00223 this.viewport = viewport;
00224 }
00225 }