Java Code Examples for com.google.ar.core.Pose#tz()

The following examples show how to use com.google.ar.core.Pose#tz() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ARVelocityActivity.java    From science-journal with Apache License 2.0 6 votes vote down vote up
private void calculateVelocity(Pose centerPose, float deltaSeconds) {
  Pose anchorPose = arFragment.getArSceneView().getSession().getAllAnchors().iterator().next()
      .getPose();
  Vector3 currPos =
      new Vector3(
          centerPose.tx() - anchorPose.tx(),
          centerPose.ty() - anchorPose.ty(),
          centerPose.tz() - anchorPose.tz());
  delTime += deltaSeconds;

  if (lastPos == null) {
    lastPos = currPos;
  } else if (delTime >= INTERVAL_TIME_SECONDS) {
    // Calculate velocity in meters per second.
    Vector3 displacement = Vector3.subtract(currPos, lastPos);
    float velocityValue = displacement.length() / delTime;
    velocitySensor.setNextVelocity(velocityValue);
    // TODO(b/135678092): Add a string resource for the following
    velocityText.setText(String.format(Locale.getDefault(), "%.2f m/s", velocityValue));
    delTime = 0;
    lastPos = currPos;
  }
}
 
Example 2
Source File: ARVelocityActivity.java    From science-journal with Apache License 2.0 6 votes vote down vote up
private void calculateVelocityEveryFrame(Pose centerPose, float deltaSeconds) {
  Pose anchorPose =
      arFragment.getArSceneView().getSession().getAllAnchors().iterator().next().getPose();
  Vector3 currPos =
      new Vector3(
          centerPose.tx() - anchorPose.tx(),
          centerPose.ty() - anchorPose.ty(),
          centerPose.tz() - anchorPose.tz());
  textUpdateTime += deltaSeconds;

  if (lastPos != null) {
    // Calculate velocity in meters per second.
    Vector3 displacement = Vector3.subtract(currPos, lastPos);
    float velocityValue = displacement.length() / deltaSeconds;
    velocitySensor.setNextVelocity(velocityValue);

    if (textUpdateTime >= TEXT_UPDATE_TIME_SECONDS) {
      // TODO(b/135678092): Add a string resource for the following
      velocityText.setText(String.format(Locale.getDefault(), "%.2f m/s", velocityValue));
      textUpdateTime = 0;
    }
  }
  lastPos = currPos;
}
 
Example 3
Source File: ARVelocityActivity.java    From science-journal with Apache License 2.0 6 votes vote down vote up
private void averageVelocityEveryFrame(Pose centerPose, float deltaSeconds) {
  Pose anchorPose =
      arFragment.getArSceneView().getSession().getAllAnchors().iterator().next().getPose();
  Vector3 currPos =
      new Vector3(
          centerPose.tx() - anchorPose.tx(),
          centerPose.ty() - anchorPose.ty(),
          centerPose.tz() - anchorPose.tz());
  positions.add(currPos);
  currIndex++;
  textUpdateTime += deltaSeconds;

  if (currIndex >= INTERVAL_FRAMES) {
    // Calculate velocity over the past second.
    Vector3 displacement = Vector3.subtract(currPos, positions.get(currIndex - INTERVAL_FRAMES));
    float velocityValue = displacement.length() / INTERVAL_TIME_SECONDS;
    velocitySensor.setNextVelocity(velocityValue);

    if (textUpdateTime >= TEXT_UPDATE_TIME_SECONDS) {
      // TODO(b/135678092): Add a string resource for the following
      velocityText.setText(String.format(Locale.getDefault(), "%.2f m/s", velocityValue));
      textUpdateTime = 0;
    }
  }
}
 
Example 4
Source File: ARVelocityActivity.java    From science-journal with Apache License 2.0 6 votes vote down vote up
private void calculateSpeed(Pose centerPose, float deltaSeconds) {
  Pose anchorPose =
      arFragment.getArSceneView().getSession().getAllAnchors().iterator().next().getPose();
  Vector3 currPos =
      new Vector3(
          centerPose.tx() - anchorPose.tx(),
          centerPose.ty() - anchorPose.ty(),
          centerPose.tz() - anchorPose.tz());
  delTime += deltaSeconds;

  if (lastPos != null) {
    float distance = Vector3.subtract(currPos, lastPos).length();
    totalDistance += distance;
  }
  lastPos = currPos;

  if (delTime >= INTERVAL_TIME_SECONDS) {
    // Calculate velocity in meters per second.
    float speedValue = totalDistance / delTime;
    velocitySensor.setNextVelocity(speedValue);
    // TODO(b/135678092): Add a string resource for the following
    velocityText.setText(String.format(Locale.getDefault(), "%.2f m/s", speedValue));
    delTime = 0;
    totalDistance = 0;
  }
}
 
Example 5
Source File: PlaneRenderer.java    From poly-sample-android with Apache License 2.0 5 votes vote down vote up
public static float calculateDistanceToPlane(Pose planePose, Pose cameraPose) {
  float[] normal = new float[3];
  float cameraX = cameraPose.tx();
  float cameraY = cameraPose.ty();
  float cameraZ = cameraPose.tz();
  // Get transformed Y axis of plane's coordinate system.
  planePose.getTransformedAxis(1, 1.0f, normal, 0);
  // Compute dot product of plane's normal with vector from camera to plane center.
  return (cameraX - planePose.tx()) * normal[0]
      + (cameraY - planePose.ty()) * normal[1]
      + (cameraZ - planePose.tz()) * normal[2];
}
 
Example 6
Source File: MainActivity.java    From journaldev with MIT License 5 votes vote down vote up
@Override
public void onUpdate(FrameTime frameTime) {
    Frame frame = arFragment.getArSceneView().getArFrame();

    Log.d("API123", "onUpdateframe... current anchor node " + (currentAnchorNode == null));


    if (currentAnchorNode != null) {
        Pose objectPose = currentAnchor.getPose();
        Pose cameraPose = frame.getCamera().getPose();

        float dx = objectPose.tx() - cameraPose.tx();
        float dy = objectPose.ty() - cameraPose.ty();
        float dz = objectPose.tz() - cameraPose.tz();

        ///Compute the straight-line distance.
        float distanceMeters = (float) Math.sqrt(dx * dx + dy * dy + dz * dz);
        tvDistance.setText("Distance from camera: " + distanceMeters + " metres");


        /*float[] distance_vector = currentAnchor.getPose().inverse()
                .compose(cameraPose).getTranslation();
        float totalDistanceSquared = 0;
        for (int i = 0; i < 3; ++i)
            totalDistanceSquared += distance_vector[i] * distance_vector[i];*/
    }
}