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

Commit 3827954

Browse files
committed
Merge pull request #25 from googlesamples/release-leibniz
release-leibniz
2 parents 24dde1d + ed8730f commit 3827954

13 files changed

Lines changed: 443 additions & 319 deletions

File tree

8.02 KB
Binary file not shown.

AreaLearningJava/src/com/projecttango/experiments/javaarealearning/ALRenderer.java

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,13 @@
3030
import com.projecttango.tangoutils.renderables.Trajectory;
3131

3232
/**
33-
* OpenGL rendering class for the AreaLearningActivity API sample. This class manages
34-
* the objects visible in the OpenGL view which are the {@link CameraFrustum},
35-
* {@link CameraFrustumAndAxis}, {@link Trajectory}, and the {@link Grid}. These
36-
* objects are implemented in the TangoUtils library in the package
37-
* {@link com.projecttango.tangoutils.renderables}.
33+
* OpenGL rendering class for the AreaLearningActivity API sample. This class manages the objects
34+
* visible in the OpenGL view which are the {@link CameraFrustum}, {@link CameraFrustumAndAxis},
35+
* {@link Trajectory}, and the {@link Grid}. These objects are implemented in the TangoUtils library
36+
* in the package {@link com.projecttango.tangoutils.renderables}.
3837
*
39-
* This class receives also handles the user-selected camera view, which can be
40-
* 1st person, 3rd person, or top-down.
38+
* This class receives also handles the user-selected camera view, which can be 1st person, 3rd
39+
* person, or top-down.
4140
*/
4241
public class ALRenderer extends Renderer implements GLSurfaceView.Renderer {
4342

@@ -57,33 +56,32 @@ public void onSurfaceCreated(GL10 gl, EGLConfig config) {
5756
mFloorGrid = new Grid();
5857
mCameraFrustumAndAxis = new CameraFrustumAndAxis();
5958
mGreenTrajectory = new Trajectory(3);
60-
mGreenTrajectory.setColor(
61-
new float[] { 0.39f, 0.56f, 0.03f, 1.0f});
59+
mGreenTrajectory.setColor(new float[] { 0.39f, 0.56f, 0.03f, 1.0f });
6260
mBlueTrajectory = new Trajectory(3);
63-
mBlueTrajectory.setColor(
64-
new float[] { 0.22f, 0.28f, 0.67f, 1.0f});
61+
mBlueTrajectory.setColor(new float[] { 0.22f, 0.28f, 0.67f, 1.0f });
6562
// Construct the initial view matrix
6663
Matrix.setIdentityM(mViewMatrix, 0);
6764
Matrix.setLookAtM(mViewMatrix, 0, 5f, 5f, 5f, 0f, 0f, 0f, 0f, 1f, 0f);
68-
mCameraFrustumAndAxis.setModelMatrix(getModelMatCalculator()
69-
.getModelMatrix());
65+
mCameraFrustumAndAxis.setModelMatrix(getModelMatCalculator().getModelMatrix());
7066
}
7167

7268
@Override
7369
public void onSurfaceChanged(GL10 gl, int width, int height) {
7470
GLES20.glViewport(0, 0, width, height);
7571
mCameraAspect = (float) width / height;
76-
Matrix.perspectiveM(mProjectionMatrix, 0, THIRD_PERSON_FOV,
77-
mCameraAspect, CAMERA_NEAR, CAMERA_FAR);
72+
Matrix.perspectiveM(mProjectionMatrix, 0, THIRD_PERSON_FOV, mCameraAspect, CAMERA_NEAR,
73+
CAMERA_FAR);
7874
}
7975

8076
@Override
8177
public void onDrawFrame(GL10 gl) {
82-
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
83-
mGreenTrajectory.draw(getViewMatrix(), mProjectionMatrix);
84-
mBlueTrajectory.draw(getViewMatrix(), mProjectionMatrix);
85-
mFloorGrid.draw(getViewMatrix(), mProjectionMatrix);
86-
mCameraFrustumAndAxis.draw(getViewMatrix(), mProjectionMatrix);
78+
synchronized (AreaLearningActivity.sharedLock) {
79+
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
80+
mGreenTrajectory.draw(getViewMatrix(), mProjectionMatrix);
81+
mBlueTrajectory.draw(getViewMatrix(), mProjectionMatrix);
82+
mFloorGrid.draw(getViewMatrix(), mProjectionMatrix);
83+
mCameraFrustumAndAxis.draw(getViewMatrix(), mProjectionMatrix);
84+
}
8785
}
8886

8987
public CameraFrustum getCameraFrustum() {
@@ -97,7 +95,7 @@ public CameraFrustumAndAxis getCameraFrustumAndAxis() {
9795
public Trajectory getBlueTrajectory() {
9896
return mBlueTrajectory;
9997
}
100-
98+
10199
public Trajectory getGreenTrajectory() {
102100
return mGreenTrajectory;
103101
}

AreaLearningJava/src/com/projecttango/experiments/javaarealearning/AreaLearningActivity.java

Lines changed: 162 additions & 96 deletions
Large diffs are not rendered by default.
8.02 KB
Binary file not shown.

MotionTrackingJava/src/com/projecttango/experiments/javamotiontracking/MTGLRenderer.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,12 @@ public void onSurfaceChanged(GL10 gl, int width, int height) {
7575

7676
@Override
7777
public void onDrawFrame(GL10 gl) {
78-
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
79-
mTrajectory.draw(getViewMatrix(), mProjectionMatrix);
80-
mFloorGrid.draw(getViewMatrix(), mProjectionMatrix);
81-
mCameraFrustumAndAxis.draw(getViewMatrix(), mProjectionMatrix);
78+
synchronized (MotionTrackingActivity.sharedLock) {
79+
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
80+
mTrajectory.draw(getViewMatrix(), mProjectionMatrix);
81+
mFloorGrid.draw(getViewMatrix(), mProjectionMatrix);
82+
mCameraFrustumAndAxis.draw(getViewMatrix(), mProjectionMatrix);
83+
}
8284
}
8385

8486
public CameraFrustum getCameraFrustum() {

MotionTrackingJava/src/com/projecttango/experiments/javamotiontracking/MotionTrackingActivity.java

Lines changed: 93 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ public class MotionTrackingActivity extends Activity implements View.OnClickList
6969
private boolean mIsAutoRecovery;
7070
private MTGLRenderer mRenderer;
7171
private GLSurfaceView mGLView;
72+
private boolean mIsProcessing = false;
73+
private TangoPoseData mPose;
74+
private static final int UPDATE_INTERVAL_MS = 100;
75+
public static Object sharedLock = new Object();
7276

7377
@Override
7478
protected void onCreate(Bundle savedInstanceState) {
@@ -106,7 +110,6 @@ protected void onCreate(Bundle savedInstanceState) {
106110
mRenderer = new MTGLRenderer();
107111
mGLView.setEGLContextClientVersion(2);
108112
mGLView.setRenderer(mRenderer);
109-
mGLView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
110113

111114
// Instantiate the Tango service
112115
mTango = new Tango(this);
@@ -137,7 +140,7 @@ protected void onCreate(Bundle savedInstanceState) {
137140

138141
// Display the library version for debug purposes
139142
mTangoServiceVersionTextView.setText(mConfig.getString("tango_service_library_version"));
140-
143+
startUIThread();
141144
}
142145

143146
/**
@@ -147,8 +150,7 @@ protected void onCreate(Bundle savedInstanceState) {
147150
private void setTangoListeners() {
148151
// Lock configuration and connect to Tango
149152
// Select coordinate frame pair
150-
final ArrayList<TangoCoordinateFramePair> framePairs =
151-
new ArrayList<TangoCoordinateFramePair>();
153+
final ArrayList<TangoCoordinateFramePair> framePairs = new ArrayList<TangoCoordinateFramePair>();
152154
framePairs.add(new TangoCoordinateFramePair(
153155
TangoPoseData.COORDINATE_FRAME_START_OF_SERVICE,
154156
TangoPoseData.COORDINATE_FRAME_DEVICE));
@@ -157,55 +159,30 @@ private void setTangoListeners() {
157159

158160
@Override
159161
public void onPoseAvailable(final TangoPoseData pose) {
160-
// Log whenever Motion Tracking enters a n invalid state
161-
if (!mIsAutoRecovery && (pose.statusCode == TangoPoseData.POSE_INVALID)) {
162-
Log.w(TAG, "Invalid State");
163-
}
164-
if (mPreviousPoseStatus != pose.statusCode) {
165-
count = 0;
166-
}
167-
count++;
168-
mPreviousPoseStatus = pose.statusCode;
169-
mDeltaTime = (float) (pose.timestamp - mPreviousTimeStamp) * SECS_TO_MILLISECS;
170-
mPreviousTimeStamp = (float) pose.timestamp;
171-
// Update the OpenGL renderable objects with the new Tango Pose
172-
// data
173-
float[] translation = pose.getTranslationAsFloats();
174-
mRenderer.getTrajectory().updateTrajectory(translation);
175-
mRenderer.getModelMatCalculator().updateModelMatrix(translation,
176-
pose.getRotationAsFloats());
177-
mRenderer.updateViewMatrix();
178-
mGLView.requestRender();
179-
180-
// Update the UI with TangoPose information
181-
runOnUiThread(new Runnable() {
182-
@Override
183-
public void run() {
184-
DecimalFormat threeDec = new DecimalFormat("0.000");
185-
String translationString = "[" + threeDec.format(pose.translation[0])
186-
+ ", " + threeDec.format(pose.translation[1]) + ", "
187-
+ threeDec.format(pose.translation[2]) + "] ";
188-
String quaternionString = "[" + threeDec.format(pose.rotation[0]) + ", "
189-
+ threeDec.format(pose.rotation[1]) + ", "
190-
+ threeDec.format(pose.rotation[2]) + ", "
191-
+ threeDec.format(pose.rotation[3]) + "] ";
192-
193-
// Display pose data on screen in TextViews
194-
mPoseTextView.setText(translationString);
195-
mQuatTextView.setText(quaternionString);
196-
mPoseCountTextView.setText(Integer.toString(count));
197-
mDeltaTextView.setText(threeDec.format(mDeltaTime));
198-
if (pose.statusCode == TangoPoseData.POSE_VALID) {
199-
mPoseStatusTextView.setText(R.string.pose_valid);
200-
} else if (pose.statusCode == TangoPoseData.POSE_INVALID) {
201-
mPoseStatusTextView.setText(R.string.pose_invalid);
202-
} else if (pose.statusCode == TangoPoseData.POSE_INITIALIZING) {
203-
mPoseStatusTextView.setText(R.string.pose_initializing);
204-
} else if (pose.statusCode == TangoPoseData.POSE_UNKNOWN) {
205-
mPoseStatusTextView.setText(R.string.pose_unknown);
206-
}
162+
//Make sure to have atomic access to Tango Pose Data so that
163+
//render loop doesn't interfere while Pose call back is updating
164+
// the data.
165+
synchronized (sharedLock) {
166+
mPose = pose;
167+
mDeltaTime = (float) (pose.timestamp - mPreviousTimeStamp) * SECS_TO_MILLISECS;
168+
mPreviousTimeStamp = (float) pose.timestamp;
169+
// Log whenever Motion Tracking enters an invalid state
170+
if (!mIsAutoRecovery && (pose.statusCode == TangoPoseData.POSE_INVALID)) {
171+
Log.w(TAG, "Invalid State");
207172
}
208-
});
173+
if (mPreviousPoseStatus != pose.statusCode) {
174+
count = 0;
175+
}
176+
count++;
177+
mPreviousPoseStatus = pose.statusCode;
178+
// Update the OpenGL renderable objects with the new Tango Pose
179+
// data
180+
float[] translation = pose.getTranslationAsFloats();
181+
mRenderer.getTrajectory().updateTrajectory(translation);
182+
mRenderer.getModelMatCalculator().updateModelMatrix(translation,
183+
pose.getRotationAsFloats());
184+
mRenderer.updateViewMatrix();
185+
}
209186
}
210187

211188
@Override
@@ -302,7 +279,10 @@ public void onClick(View v) {
302279
public boolean onTouchEvent(MotionEvent event) {
303280
return mRenderer.onTouchEvent(event);
304281
}
305-
282+
283+
/**
284+
* Setup the extrinsics of the device.
285+
*/
306286
private void setUpExtrinsics() {
307287
// Get device to imu matrix.
308288
TangoPoseData device2IMUPose = new TangoPoseData();
@@ -322,4 +302,64 @@ private void setUpExtrinsics() {
322302
mRenderer.getModelMatCalculator().SetColorCamera2IMUMatrix(
323303
color2IMUPose.getTranslationAsFloats(), color2IMUPose.getRotationAsFloats());
324304
}
305+
/**
306+
* Create a separate thread to update Log information on UI at the specified
307+
* interval of UPDATE_INTERVAL_MS. This function also makes sure to have access
308+
* to the mPose atomically.
309+
*/
310+
private void startUIThread() {
311+
new Thread(new Runnable() {
312+
DecimalFormat threeDec = new DecimalFormat("00.000");
313+
@Override
314+
public void run() {
315+
while (true) {
316+
try {
317+
Thread.sleep(UPDATE_INTERVAL_MS);
318+
runOnUiThread(new Runnable() {
319+
@Override
320+
public void run() {
321+
try {
322+
synchronized (sharedLock) {
323+
if (mPose == null) {
324+
return;
325+
}
326+
327+
String translationString = "["
328+
+ threeDec.format(mPose.translation[0]) + ", "
329+
+ threeDec.format(mPose.translation[1]) + ", "
330+
+ threeDec.format(mPose.translation[2]) + "] ";
331+
String quaternionString = "["
332+
+ threeDec.format(mPose.rotation[0]) + ", "
333+
+ threeDec.format(mPose.rotation[1]) + ", "
334+
+ threeDec.format(mPose.rotation[2]) + ", "
335+
+ threeDec.format(mPose.rotation[3]) + "] ";
336+
337+
// Display pose data on screen in TextViews
338+
mPoseTextView.setText(translationString);
339+
mQuatTextView.setText(quaternionString);
340+
mPoseCountTextView.setText(Integer.toString(count));
341+
mDeltaTextView.setText(threeDec.format(mDeltaTime));
342+
if (mPose.statusCode == TangoPoseData.POSE_VALID) {
343+
mPoseStatusTextView.setText(R.string.pose_valid);
344+
} else if (mPose.statusCode == TangoPoseData.POSE_INVALID) {
345+
mPoseStatusTextView.setText(R.string.pose_invalid);
346+
} else if (mPose.statusCode == TangoPoseData.POSE_INITIALIZING) {
347+
mPoseStatusTextView.setText(R.string.pose_initializing);
348+
} else if (mPose.statusCode == TangoPoseData.POSE_UNKNOWN) {
349+
mPoseStatusTextView.setText(R.string.pose_unknown);
350+
}
351+
}
352+
} catch (NullPointerException e) {
353+
e.printStackTrace();
354+
}
355+
}
356+
});
357+
358+
} catch (InterruptedException e) {
359+
e.printStackTrace();
360+
}
361+
}
362+
}
363+
}).start();
364+
}
325365
}
8.02 KB
Binary file not shown.

PointCloudJava/src/com/projecttango/experiments/javapointcloud/PCRenderer.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,14 @@
3030
import com.projecttango.tangoutils.renderables.PointCloud;
3131

3232
/**
33-
* OpenGL rendering class for the Motion Tracking API sample. This class
34-
* managers the objects visible in the OpenGL view which are the
35-
* {@link CameraFrustum}, {@link PointCloud} and the {@link Grid}. These objects
36-
* are implemented in the TangoUtils library in the package
33+
* OpenGL rendering class for the Motion Tracking API sample. This class managers the objects
34+
* visible in the OpenGL view which are the {@link CameraFrustum}, {@link PointCloud} and the
35+
* {@link Grid}. These objects are implemented in the TangoUtils library in the package
3736
* {@link com.projecttango.tangoutils.renderables}.
3837
*
39-
* This class receives {@link TangoPose} data from the {@link MotionTracking}
40-
* class and updates the model and view matrices of the {@link Renderable}
41-
* objects appropriately. It also handles the user-selected camera view, which
42-
* can be 1st person, 3rd person, or top-down.
38+
* This class receives {@link TangoPose} data from the {@link MotionTracking} class and updates the
39+
* model and view matrices of the {@link Renderable} objects appropriately. It also handles the
40+
* user-selected camera view, which can be 1st person, 3rd person, or top-down.
4341
*
4442
*/
4543
public class PCRenderer extends Renderer implements GLSurfaceView.Renderer {
@@ -62,24 +60,27 @@ public void onSurfaceCreated(GL10 gl, EGLConfig config) {
6260
mCameraFrustumAndAxis = new CameraFrustumAndAxis();
6361
Matrix.setIdentityM(mViewMatrix, 0);
6462
Matrix.setLookAtM(mViewMatrix, 0, 5f, 5f, 5f, 0f, 0f, 0f, 0f, 1f, 0f);
65-
mCameraFrustumAndAxis.setModelMatrix(getModelMatCalculator()
66-
.getModelMatrix());
63+
mCameraFrustumAndAxis.setModelMatrix(getModelMatCalculator().getModelMatrix());
6764
}
6865

6966
@Override
7067
public void onSurfaceChanged(GL10 gl, int width, int height) {
7168
GLES20.glViewport(0, 0, width, height);
7269
mCameraAspect = (float) width / height;
73-
Matrix.perspectiveM(mProjectionMatrix, 0, CAMERA_FOV, mCameraAspect,
74-
CAMERA_NEAR, CAMERA_FAR);
70+
Matrix.perspectiveM(mProjectionMatrix, 0, CAMERA_FOV, mCameraAspect, CAMERA_NEAR,
71+
CAMERA_FAR);
7572
}
7673

7774
@Override
7875
public void onDrawFrame(GL10 gl) {
7976
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
8077
mGrid.draw(mViewMatrix, mProjectionMatrix);
81-
mPointCloud.draw(mViewMatrix, mProjectionMatrix);
82-
mCameraFrustumAndAxis.draw(mViewMatrix, mProjectionMatrix);
78+
synchronized (PointCloudActivity.depthLock) {
79+
mPointCloud.draw(mViewMatrix, mProjectionMatrix);
80+
}
81+
synchronized (PointCloudActivity.poseLock) {
82+
mCameraFrustumAndAxis.draw(mViewMatrix, mProjectionMatrix);
83+
}
8384
}
8485

8586
public PointCloud getPointCloud() {

0 commit comments

Comments
 (0)