Skip to content
This repository has been archived by the owner on Mar 2, 2018. It is now read-only.

Commit

Permalink
Merge pull request #91 from googlesamples/release-eisa
Browse files Browse the repository at this point in the history
Release eisa
  • Loading branch information
jguomoto committed Feb 10, 2017
2 parents ad5ce28 + 2549160 commit 2dc4c02
Show file tree
Hide file tree
Showing 82 changed files with 2,298 additions and 2,673 deletions.
Binary file removed TangoReleaseLibs/aar/tango-ux-support-library.aar
Binary file not shown.
Binary file modified TangoReleaseLibs/aar/tango_support_java_lib.aar
Binary file not shown.
Binary file not shown.
Binary file modified TangoReleaseLibs/jar/tango_java_lib.jar
Binary file not shown.
Expand Up @@ -34,7 +34,6 @@
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.display.DisplayManager;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;
Expand All @@ -43,7 +42,6 @@
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.Display;
import android.view.Surface;
import android.widget.Toast;

import org.rajawali3d.scene.ASceneFrameCallback;
Expand Down Expand Up @@ -79,15 +77,11 @@ public class AugmentedRealityActivity extends Activity {
private static final String TAG = AugmentedRealityActivity.class.getSimpleName();
private static final int INVALID_TEXTURE_ID = 0;

// For all current Tango devices, color camera is in the camera id 0.
private static final int COLOR_CAMERA_ID = 0;

private static final String CAMERA_PERMISSION = Manifest.permission.CAMERA;
private static final int CAMERA_PERMISSION_CODE = 0;

private SurfaceView mSurfaceView;
private AugmentedRealityRenderer mRenderer;
private TangoCameraIntrinsics mIntrinsics;
private Tango mTango;
private TangoConfig mConfig;
private boolean mIsConnected = false;
Expand All @@ -99,7 +93,7 @@ public class AugmentedRealityActivity extends Activity {
private AtomicBoolean mIsFrameAvailableTangoThread = new AtomicBoolean(false);
private double mRgbTimestampGlThread;

private int mColorCameraToDisplayAndroidRotation = 0;
private int mDisplayRotation = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -118,7 +112,7 @@ public void onDisplayAdded(int displayId) {
@Override
public void onDisplayChanged(int displayId) {
synchronized (this) {
setAndroidOrientation();
setDisplayRotation();
}
}

Expand All @@ -136,16 +130,12 @@ protected void onStart() {
super.onStart();
mSurfaceView.onResume();

setAndroidOrientation();

// Set render mode to RENDERMODE_CONTINUOUSLY to force getting onDraw callbacks until
// the Tango service is properly set-up and we start getting onFrameAvailable callbacks.
mSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
// Check and request camera permission at run time.
if (hasCameraPermission()) {
if (checkAndRequestPermissions()) {
bindTangoService();
} else {
requestCameraPermission();
}
}

Expand Down Expand Up @@ -197,12 +187,16 @@ public void run() {
mTango.connect(mConfig);
startupTango();
mIsConnected = true;
setDisplayRotation();
} catch (TangoOutOfDateException e) {
Log.e(TAG, getString(R.string.exception_out_of_date), e);
showsToastAndFinishOnUiThread(R.string.exception_out_of_date);
} catch (TangoErrorException e) {
Log.e(TAG, getString(R.string.exception_tango_error), e);
showsToastAndFinishOnUiThread(R.string.exception_tango_error);
} catch (TangoInvalidException e) {
Log.e(TAG, getString(R.string.exception_tango_invalid), e);
showsToastAndFinishOnUiThread(R.string.exception_tango_invalid);
}
}
}
Expand Down Expand Up @@ -281,9 +275,6 @@ public void onFrameAvailable(int cameraId) {
}
}
});

// Obtain the intrinsic parameters of the color camera.
mIntrinsics = mTango.getCameraIntrinsics(TangoCameraIntrinsics.TANGO_CAMERA_COLOR);
}

/**
Expand Down Expand Up @@ -311,9 +302,12 @@ public void onPreFrame(long sceneTime, double deltaTime) {

// Set-up scene camera projection to match RGB camera intrinsics.
if (!mRenderer.isSceneCameraConfigured()) {
TangoCameraIntrinsics intrinsics =
TangoSupport.getCameraIntrinsicsBasedOnDisplayRotation(
TangoCameraIntrinsics.TANGO_CAMERA_COLOR,
mDisplayRotation);
mRenderer.setProjectionMatrix(
projectionMatrixFromCameraIntrinsics(mIntrinsics,
mColorCameraToDisplayAndroidRotation));
projectionMatrixFromCameraIntrinsics(intrinsics));
}
// Connect the camera texture to the OpenGL Texture if necessary
// NOTE: When the OpenGL context is recycled, Rajawali may re-generate the
Expand Down Expand Up @@ -347,7 +341,7 @@ public void onPreFrame(long sceneTime, double deltaTime) {
TangoPoseData.COORDINATE_FRAME_AREA_DESCRIPTION,
TangoPoseData.COORDINATE_FRAME_CAMERA_COLOR,
TangoSupport.TANGO_SUPPORT_ENGINE_OPENGL,
mColorCameraToDisplayAndroidRotation);
mDisplayRotation);
if (lastFramePose.statusCode == TangoPoseData.POSE_VALID) {
// Update the camera pose from the renderer
mRenderer.updateRenderCameraPose(lastFramePose);
Expand Down Expand Up @@ -391,71 +385,19 @@ public boolean callPreFrame() {
mSurfaceView.setSurfaceRenderer(mRenderer);
}

private static int getColorCameraToDisplayAndroidRotation(int displayRotation,
int cameraRotation) {
int cameraRotationNormalized = 0;
switch (cameraRotation) {
case 90:
cameraRotationNormalized = 1;
break;
case 180:
cameraRotationNormalized = 2;
break;
case 270:
cameraRotationNormalized = 3;
break;
default:
cameraRotationNormalized = 0;
break;
}
int ret = displayRotation - cameraRotationNormalized;
if (ret < 0) {
ret += 4;
}
return ret;
}

/**
* Use Tango camera intrinsics to calculate the projection Matrix for the Rajawali scene.
*
* @param intrinsics camera instrinsics for computing the project matrix.
* @param rotation the relative rotation between the camera intrinsics and display glContext.
*/
private static float[] projectionMatrixFromCameraIntrinsics(TangoCameraIntrinsics intrinsics,
int rotation) {
// Adjust camera intrinsics according to rotation
private static float[] projectionMatrixFromCameraIntrinsics(TangoCameraIntrinsics intrinsics) {
float cx = (float) intrinsics.cx;
float cy = (float) intrinsics.cy;
float width = (float) intrinsics.width;
float height = (float) intrinsics.height;
float fx = (float) intrinsics.fx;
float fy = (float) intrinsics.fy;

switch (rotation) {
case Surface.ROTATION_90:
cx = (float) intrinsics.cy;
cy = (float) intrinsics.width - (float) intrinsics.cx;
width = (float) intrinsics.height;
height = (float) intrinsics.width;
fx = (float) intrinsics.fy;
fy = (float) intrinsics.fx;
break;
case Surface.ROTATION_180:
cx = (float) intrinsics.width - cx;
cy = (float) intrinsics.height - cy;
break;
case Surface.ROTATION_270:
cx = (float) intrinsics.height - (float) intrinsics.cy;
cy = (float) intrinsics.cx;
width = (float) intrinsics.height;
height = (float) intrinsics.width;
fx = (float) intrinsics.fy;
fy = (float) intrinsics.fx;
break;
default:
break;
}

// Uses frustumM to create a projection matrix taking into account calibrated camera
// intrinsic parameter.
// Reference: http://ksimek.github.io/2013/06/03/calibrated_cameras_in_opengl/
Expand All @@ -481,23 +423,35 @@ private static float[] projectionMatrixFromCameraIntrinsics(TangoCameraIntrinsic
/**
* Set the color camera background texture rotation and save the camera to display rotation.
*/
private void setAndroidOrientation() {
private void setDisplayRotation() {
Display display = getWindowManager().getDefaultDisplay();
Camera.CameraInfo colorCameraInfo = new Camera.CameraInfo();
Camera.getCameraInfo(COLOR_CAMERA_ID, colorCameraInfo);
mDisplayRotation = display.getRotation();

mColorCameraToDisplayAndroidRotation =
getColorCameraToDisplayAndroidRotation(display.getRotation(),
colorCameraInfo.orientation);
// Run this in OpenGL thread.
// We also need to update the camera texture UV coordinates. This must be run in the OpenGL
// thread.
mSurfaceView.queueEvent(new Runnable() {
@Override
public void run() {
mRenderer.updateColorCameraTextureUvGlThread(mColorCameraToDisplayAndroidRotation);
if (mIsConnected) {
mRenderer.updateColorCameraTextureUvGlThread(mDisplayRotation);
}
}
});
}

/**
* Check we have the necessary permissions for this app, and ask for them if we haven't.
*
* @return True if we have the necessary permissions, false if we haven't.
*/
private boolean checkAndRequestPermissions() {
if (!hasCameraPermission()) {
requestCameraPermission();
return false;
}
return true;
}

/**
* Check we have the necessary permissions for this app.
*/
Expand Down Expand Up @@ -536,6 +490,9 @@ public void onClick(DialogInterface dialogInterface, int i) {
dialog.show();
}

/**
* Result for requesting camera permission.
*/
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
Expand All @@ -546,4 +503,20 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions,
Toast.LENGTH_LONG).show();
}
}

/**
* Display toast on UI thread.
*
* @param resId The resource id of the string resource to use. Can be formatted text.
*/
private void showsToastAndFinishOnUiThread(final int resId) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(AugmentedRealityActivity.this,
getString(resId), Toast.LENGTH_LONG).show();
finish();
}
});
}
}
Expand Up @@ -44,6 +44,8 @@

import javax.microedition.khronos.opengles.GL10;

import com.projecttango.tangosupport.TangoSupport;

/**
* Renderer that implements a basic augmented reality scene using Rajawali.
* It creates a scene with a background quad taking the whole screen, where the color camera is
Expand All @@ -54,9 +56,6 @@ public class AugmentedRealityRenderer extends Renderer {
private static final String TAG = AugmentedRealityRenderer.class.getSimpleName();

private float[] textureCoords0 = new float[]{0.0F, 1.0F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F, 0.0F};
private float[] textureCoords270 = new float[]{1.0F, 1.0F, 0.0F, 1.0F, 1.0F, 0.0F, 0.0F, 0.0F};
private float[] textureCoords180 = new float[]{1.0F, 0.0F, 1.0F, 1.0F, 0.0F, 0.0F, 0.0F, 1.0F};
private float[] textureCoords90 = new float[]{0.0F, 0.0F, 1.0F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F};

// Rajawali texture used to render the Tango color camera.
private ATexture mTangoCameraTexture;
Expand Down Expand Up @@ -172,20 +171,9 @@ public void updateColorCameraTextureUvGlThread(int rotation) {
mBackgroundQuad = new ScreenQuad();
}

switch (rotation) {
case Surface.ROTATION_90:
mBackgroundQuad.getGeometry().setTextureCoords(textureCoords90, true);
break;
case Surface.ROTATION_180:
mBackgroundQuad.getGeometry().setTextureCoords(textureCoords180, true);
break;
case Surface.ROTATION_270:
mBackgroundQuad.getGeometry().setTextureCoords(textureCoords270, true);
break;
default:
mBackgroundQuad.getGeometry().setTextureCoords(textureCoords0, true);
break;
}
float[] textureCoords =
TangoSupport.getVideoOverlayUVBasedOnDisplayRotation(textureCoords0, rotation);
mBackgroundQuad.getGeometry().setTextureCoords(textureCoords, true);
mBackgroundQuad.getGeometry().reload();
}

Expand Down
Expand Up @@ -103,14 +103,18 @@ public void run() {
startupTango();
} catch (TangoOutOfDateException e) {
Log.e(TAG, getString(R.string.tango_out_of_date_exception), e);
showsToastAndFinishOnUiThread(R.string.tango_out_of_date_exception);
} catch (TangoErrorException e) {
Log.e(TAG, getString(R.string.tango_error), e);
showsToastAndFinishOnUiThread(R.string.tango_error);
} catch (TangoInvalidException e) {
Log.e(TAG, getString(R.string.tango_invalid), e);
showsToastAndFinishOnUiThread(R.string.tango_invalid);
} catch (SecurityException e) {
// Area Learning permissions are required. If they are not available,
// SecurityException is thrown.
Log.e(TAG, getString(R.string.no_permissions), e);
showsToastAndFinishOnUiThread(R.string.no_permissions);
}
}

Expand Down Expand Up @@ -358,4 +362,20 @@ private void showSetAdfNameDialog() {
setAdfNameDialog.setArguments(bundle);
setAdfNameDialog.show(manager, "ADFNameDialog");
}

/**
* Display toast on UI thread.
*
* @param resId The resource id of the string resource to use. Can be formatted text.
*/
private void showsToastAndFinishOnUiThread(final int resId) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(HelloAreaDescriptionActivity.this,
getString(resId), Toast.LENGTH_LONG).show();
finish();
}
});
}
}
Expand Up @@ -31,6 +31,7 @@
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import java.nio.FloatBuffer;
import java.util.ArrayList;
Expand Down Expand Up @@ -73,10 +74,13 @@ public void run() {
startupTango();
} catch (TangoOutOfDateException e) {
Log.e(TAG, getString(R.string.exception_out_of_date), e);
showsToastAndFinishOnUiThread(R.string.exception_out_of_date);
} catch (TangoErrorException e) {
Log.e(TAG, getString(R.string.exception_tango_error), e);
showsToastAndFinishOnUiThread(R.string.exception_tango_error);
} catch (TangoInvalidException e) {
Log.e(TAG, getString(R.string.exception_tango_invalid), e);
showsToastAndFinishOnUiThread(R.string.exception_tango_invalid);
}
}
}
Expand Down Expand Up @@ -177,4 +181,20 @@ private float calculateAveragedDepth(FloatBuffer pointCloudBuffer, int numPoints
}
return averageZ;
}

/**
* Display toast on UI thread.
*
* @param resId The resource id of the string resource to use. Can be formatted text.
*/
private void showsToastAndFinishOnUiThread(final int resId) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(HelloDepthPerceptionActivity.this,
getString(resId), Toast.LENGTH_LONG).show();
finish();
}
});
}
}

0 comments on commit 2dc4c02

Please sign in to comment.