Java Code Examples for com.google.ar.sceneform.math.Vector3#length()

The following examples show how to use com.google.ar.sceneform.math.Vector3#length() . 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: Stroke.java    From sceneform-samples with Apache License 2.0 6 votes vote down vote up
public void add(Vector3 pointInWorld) {
  Vector3 pointInLocal = anchorNode.worldToLocalPoint(pointInWorld);
  List<Vector3> points = lineSimplifier.getPoints();
  if (getNumOfPoints() < 1) {
    lineSimplifier.add(pointInLocal);
    return;
  }

  Vector3 prev = points.get(points.size() - 1);
  Vector3 diff = Vector3.subtract(prev, pointInLocal);
  if (diff.length() < MINIMUM_DISTANCE_BETWEEN_POINTS) {
    return;
  }

  lineSimplifier.add(pointInLocal);

  RenderableDefinition renderableDefinition =
      ExtrudedCylinder.makeExtrudedCylinder(CYLINDER_RADIUS, points, material);
  if (shape == null) {
    shape = ModelRenderable.builder().setSource(renderableDefinition).build().join();
    node.setRenderable(shape);
  } else {
    shape.updateFromDefinition(renderableDefinition);
  }
}
 
Example 2
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 3
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 4
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 5
Source File: ExtrudedCylinder.java    From sceneform-samples with Apache License 2.0 4 votes vote down vote up
private static void generateVerticesFromPoints(
    Vector3 desiredUp,
    List<Vertex> vertices,
    List<Quaternion> rotations,
    Vector3 firstPoint,
    Vector3 secondPoint,
    float radius) {

  final Vector3 difference = Vector3.subtract(firstPoint, secondPoint);
  Vector3 directionFromTopToBottom = difference.normalized();
  Quaternion rotationFromAToB = Quaternion.lookRotation(directionFromTopToBottom, desiredUp);

  // cosTheta0 provides the angle between the rotations
  if (!rotations.isEmpty()) {
    double cosTheta0 = dot(rotations.get(rotations.size() - 1), rotationFromAToB);
    // Flip end rotation to get shortest path if needed
    if (cosTheta0 < 0.0) {
      rotationFromAToB = negated(rotationFromAToB);
    }
  }
  rotations.add(rotationFromAToB);

  directionFromTopToBottom =
      Quaternion.rotateVector(rotationFromAToB, Vector3.forward()).normalized();
  Vector3 rightDirection =
      Quaternion.rotateVector(rotationFromAToB, Vector3.right()).normalized();
  Vector3 upDirection = Quaternion.rotateVector(rotationFromAToB, Vector3.up()).normalized();
  desiredUp.set(upDirection);

  List<Vertex> bottomVertices = new ArrayList<>();

  final float halfHeight = difference.length() / 2;
  final Vector3 center = Vector3.add(firstPoint, secondPoint).scaled(.5f);

  final float thetaIncrement = (float) (2 * Math.PI) / NUMBER_OF_SIDES;
  float theta = 0;
  float cosTheta = (float) Math.cos(theta);
  float sinTheta = (float) Math.sin(theta);
  float uStep = (float) 1.0 / NUMBER_OF_SIDES;

  // Generate edge vertices along the sides of the cylinder.
  for (int edgeIndex = 0; edgeIndex <= NUMBER_OF_SIDES; edgeIndex++) {
    // Create top edge vertex
    Vector3 topPosition =
        Vector3.add(
            directionFromTopToBottom.scaled(-halfHeight),
            Vector3.add(
                rightDirection.scaled(radius * cosTheta), upDirection.scaled(radius * sinTheta)));
    Vector3 normal =
        Vector3.subtract(topPosition, directionFromTopToBottom.scaled(-halfHeight)).normalized();
    topPosition = Vector3.add(topPosition, center);
    UvCoordinate uvCoordinate = new UvCoordinate(uStep * edgeIndex, 0);

    Vertex vertex =
        Vertex.builder()
            .setPosition(topPosition)
            .setNormal(normal)
            .setUvCoordinate(uvCoordinate)
            .build();
    vertices.add(vertex);

    // Create bottom edge vertex
    Vector3 bottomPosition =
        Vector3.add(
            directionFromTopToBottom.scaled(halfHeight),
            Vector3.add(
                rightDirection.scaled(radius * cosTheta), upDirection.scaled(radius * sinTheta)));
    normal =
        Vector3.subtract(bottomPosition, directionFromTopToBottom.scaled(halfHeight))
            .normalized();
    bottomPosition = Vector3.add(bottomPosition, center);
    float vHeight = halfHeight * 2;
    uvCoordinate = new UvCoordinate(uStep * edgeIndex, vHeight);

    vertex =
        Vertex.builder()
            .setPosition(bottomPosition)
            .setNormal(normal)
            .setUvCoordinate(uvCoordinate)
            .build();
    bottomVertices.add(vertex);

    theta += thetaIncrement;
    cosTheta = (float) Math.cos(theta);
    sinTheta = (float) Math.sin(theta);
  }
  vertices.addAll(bottomVertices);
}
 
Example 6
Source File: LineSimplifier.java    From sceneform-samples with Apache License 2.0 4 votes vote down vote up
private float getPerpendicularDistance(Vector3 start, Vector3 end, Vector3 point) {
  Vector3 crossProduct =
      Vector3.cross(Vector3.subtract(point, start), Vector3.subtract(point, end));
  float result = crossProduct.length() / Vector3.subtract(end, start).length();
  return result;
}