android.hardware.SensorManager.getRotationMatrixFromVector()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(11.6k)|赞(0)|评价(0)|浏览(149)

本文整理了Java中android.hardware.SensorManager.getRotationMatrixFromVector()方法的一些代码示例,展示了SensorManager.getRotationMatrixFromVector()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。SensorManager.getRotationMatrixFromVector()方法的具体详情如下:
包路径:android.hardware.SensorManager
类名称:SensorManager
方法名:getRotationMatrixFromVector

SensorManager.getRotationMatrixFromVector介绍

暂无

代码示例

代码示例来源:origin: libgdx/libgdx

/** Returns the rotation matrix describing the devices rotation as per <a href=
 * "http://developer.android.com/reference/android/hardware/SensorManager.html#getRotationMatrix(float[], float[], float[], float[])"
 * >SensorManager#getRotationMatrix(float[], float[], float[], float[])</a>. Does not manipulate the matrix if the platform
 * does not have an accelerometer and compass, or a rotation vector sensor.
 * @param matrix */
public void getRotationMatrix (float[] matrix) {
  if (rotationVectorAvailable)
    SensorManager.getRotationMatrixFromVector(matrix, rotationVectorValues);
  else // compass + accelerometer
    SensorManager.getRotationMatrix(matrix, null, accelerometerValues, magneticFieldValues);
}

代码示例来源:origin: libgdx/libgdx

/** Returns the rotation matrix describing the devices rotation as per <a href=
 * "http://developer.android.com/reference/android/hardware/SensorManager.html#getRotationMatrix(float[], float[], float[], float[])"
 * >SensorManager#getRotationMatrix(float[], float[], float[], float[])</a>. Does not manipulate the matrix if the platform
 * does not have an accelerometer and compass, or a rotation vector sensor.
 * @param matrix */
public void getRotationMatrix (float[] matrix) {
  if (rotationVectorAvailable)
    SensorManager.getRotationMatrixFromVector(matrix, rotationVectorValues);
  else // compass + accelerometer
    SensorManager.getRotationMatrix(matrix, null, accelerometerValues, magneticFieldValues);
}

代码示例来源:origin: libgdx/libgdx

private void updateOrientation () {
  if (rotationVectorAvailable){
    SensorManager.getRotationMatrixFromVector(R, rotationVectorValues);
  } else if (!SensorManager.getRotationMatrix(R, null, accelerometerValues, magneticFieldValues)) {
      return; // compass + accelerometer in free fall
  }
  SensorManager.getOrientation(R, orientation);
  azimuth = (float)Math.toDegrees(orientation[0]);
  pitch = (float)Math.toDegrees(orientation[1]);
  roll = (float)Math.toDegrees(orientation[2]);
}

代码示例来源:origin: libgdx/libgdx

private void updateOrientation () {
  if (rotationVectorAvailable){
    SensorManager.getRotationMatrixFromVector(R, rotationVectorValues);
  } else if (!SensorManager.getRotationMatrix(R, null, accelerometerValues, magneticFieldValues)) {
      return; // compass + accelerometer in free fall
  }
  SensorManager.getOrientation(R, orientation);
  azimuth = (float)Math.toDegrees(orientation[0]);
  pitch = (float)Math.toDegrees(orientation[1]);
  roll = (float)Math.toDegrees(orientation[2]);
}

代码示例来源:origin: nisrulz/sensey

@Override
  protected void onSensorEvent(SensorEvent sensorEvent) {
    // Get rotation matrix
    float[] rotationMatrix = new float[16];
    SensorManager.getRotationMatrixFromVector(rotationMatrix, sensorEvent.values);

    // Remap coordinate system
    float[] remappedRotationMatrix = new float[16];
    SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z,
        remappedRotationMatrix);

    // Convert to orientations
    float[] orientations = new float[3];
    SensorManager.getOrientation(remappedRotationMatrix, orientations);

    // Convert values in radian to degrees
    for (int i = 0; i < 3; i++) {
      orientations[i] = (float) (Math.toDegrees(orientations[i]));
    }

    rotationAngleListener.onRotation(orientations[0], orientations[1], orientations[2]);
  }
}

代码示例来源:origin: google/ExoPlayer

@Override
@BinderThread
public void onSensorChanged(SensorEvent event) {
 SensorManager.getRotationMatrixFromVector(remappedPhoneMatrix, event.values);

代码示例来源:origin: doggycoder/AndroidOpenGLDemo

@Override
public void onSensorChanged(SensorEvent event) {
  SensorManager.getRotationMatrixFromVector(matrix,event.values);
  mSkySphere.setMatrix(matrix);
}

代码示例来源:origin: SchibstedSpain/Parallax-Layer-Layout

/**
 * Sets the target direction used for angle deltas to determine tilt.
 *
 * @param values a rotation vector (presumably from a ROTATION_VECTOR sensor)
 */
private void setTargetVector(float[] values) {
 SensorManager.getRotationMatrixFromVector(mTargetMatrix, values);
 mTargeted = true;
}

代码示例来源:origin: nvanbenschoten/motion

/**
 * Sets the target direction used for angle deltas to determine tilt.
 *
 * @param values a rotation vector (presumably from a ROTATION_VECTOR sensor)
 */
protected void setTargetVector(float[] values) {
  SensorManager.getRotationMatrixFromVector(mTargetMatrix, values);
  mTargeted = true;
}

代码示例来源:origin: retomeier/Wrox-ProfessionalAndroid-4E

public void onSensorChanged(SensorEvent sensorEvent) {
 float[] rotationMatrix = new float[9];
 float[] orientation = new float[3];
 // Convert the result Vector to a Rotation Matrix.
 SensorManager.getRotationMatrixFromVector(rotationMatrix,
  sensorEvent.values);
 // Extract the orientation from the Rotation Matrix.
 SensorManager.getOrientation(rotationMatrix, orientation);
 Log.d(TAG, "Yaw: " + orientation[0]); // Yaw
 Log.d(TAG, "Pitch: " + orientation[1]); // Pitch
 Log.d(TAG, "Roll: " + orientation[2]); // Roll
}

代码示例来源:origin: qiubiteme/android_api_demos

public void onSensorChanged(SensorEvent event) {
  // we received a sensor event. it is a good practice to check
  // that we received the proper event
  if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
    // convert the rotation-vector to a 4x4 matrix. the matrix
    // is interpreted by Open GL as the inverse of the
    // rotation-vector, which is what we want.
    SensorManager.getRotationMatrixFromVector(
        mRotationMatrix , event.values);
  }
}

代码示例来源:origin: THEONE10211024/ApiDemos

public void onSensorChanged(SensorEvent event) {
  // we received a sensor event. it is a good practice to check
  // that we received the proper event
  if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
    // convert the rotation-vector to a 4x4 matrix. the matrix
    // is interpreted by Open GL as the inverse of the
    // rotation-vector, which is what we want.
    SensorManager.getRotationMatrixFromVector(
        mRotationMatrix , event.values);
  }
}

代码示例来源:origin: lycha/augmented-reality-example

@Override
public void onSensorChanged(SensorEvent event) {
  azimuthFrom = azimuthTo;
  float[] orientation = new float[3];
  float[] rMat = new float[9];
  SensorManager.getRotationMatrixFromVector(rMat, event.values);
  azimuthTo = (int) ( Math.toDegrees( SensorManager.getOrientation( rMat, orientation )[0] ) + 360 ) % 360;
  mAzimuthListener.onAzimuthChanged(azimuthFrom, azimuthTo);
}

代码示例来源:origin: apacha/sensor-fusion-demo

@Override
  public void onSensorChanged(SensorEvent event) {
    // we received a sensor event. it is a good practice to check
    // that we received the proper event
    if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
      // convert the rotation-vector to a 4x4 matrix. the matrix
      // is interpreted by Open GL as the inverse of the
      // rotation-vector, which is what we want.
      SensorManager.getRotationMatrixFromVector(currentOrientationRotationMatrix.matrix, event.values);

      // Get Quaternion
      // Calculate angle. Starting with API_18, Android will provide this value as event.values[3], but if not, we have to calculate it manually.
      SensorManager.getQuaternionFromVector(temporaryQuaternion, event.values);
      currentOrientationQuaternion.setXYZW(temporaryQuaternion[1], temporaryQuaternion[2], temporaryQuaternion[3], -temporaryQuaternion[0]);
    }
  }
}

代码示例来源:origin: tvbarthel/ChaseWhisplyProject

private void updateCoordinate(float[] rotationVector) {
  SensorManager.getRotationMatrixFromVector(mRotationMatrix, rotationVector);
  SensorManager.remapCoordinateSystem(mRotationMatrix, mRemappedXAxis, mRemappedYAxis, mRemappedRotationMatrix);
  SensorManager.getOrientation(mRemappedRotationMatrix, mOrientationVals);
  if (mCurrentRotation == Surface.ROTATION_0) {
    //For some reasons, there is a difference of 100° on the Y coordinate
    //between landscape and portrait orientation. This is a poor
    //attempt at fixing this issue.
    mOrientationVals[2] -= 1.7453292519;
  }
  onSmoothCoordinateChanged(mOrientationVals.clone());
}

代码示例来源:origin: StevenRudenko/BleSensorTag

public void onGyroDataUpdate(float[] gyro) {
  // initialisation of the gyroscope based rotation matrix
  if (!isGyroInitialized) {
    float[] initMatrix = getRotationMatrixFromOrientation(accMagOrientation);
    gyroMatrix = matrixMultiplication(gyroMatrix, initMatrix);
    isGyroInitialized = true;
  }
  // copy the new gyro values into the gyro array
  // convert the raw gyro data into a rotation vector
  float[] deltaVector = new float[4];
  final long currentTimestamp = System.nanoTime();
  if(timestamp != 0) {
    final float dT = (currentTimestamp - timestamp) * NS2S;
    System.arraycopy(gyro, 0, this.gyro, 0, 3);
    getRotationVectorFromGyro(this.gyro, deltaVector, dT / 2.0f);
  }
  // measurement done, save current time for next interval
  timestamp = currentTimestamp;
  // convert rotation vector into rotation matrix
  float[] deltaMatrix = new float[9];
  SensorManager.getRotationMatrixFromVector(deltaMatrix, deltaVector);
  // apply the new rotation interval on the gyroscope based rotation matrix
  gyroMatrix = matrixMultiplication(gyroMatrix, deltaMatrix);
  // get the gyroscope based orientation from the rotation matrix
  SensorManager.getOrientation(gyroMatrix, gyroOrientation);
}

代码示例来源:origin: bitcraze/crazyflie-android-client

private void update(float[] vectors) {
  int AMP_MAX = 50;
  int AMPLIFICATION = AMP_MAX / mControls.getGyroAmplification();
  float[] rotationMatrix = new float[9];
  SensorManager.getRotationMatrixFromVector(rotationMatrix, vectors);
  int worldAxisX = SensorManager.AXIS_X;
  int worldAxisY = SensorManager.AXIS_Y;
  float[] adjustedRotationMatrix = new float[9];
  SensorManager.remapCoordinateSystem(rotationMatrix, worldAxisX, worldAxisY, adjustedRotationMatrix);
  float[] orientation = new float[3];
  SensorManager.getOrientation(adjustedRotationMatrix, orientation);
  float pitch = (orientation[2] * FROM_RADS_TO_DEGS * -1) / AMPLIFICATION;
  float roll = (orientation[1] * FROM_RADS_TO_DEGS) / AMPLIFICATION;
  mSensorRoll = roll;
  mSensorPitch = pitch;
  updateFlightData();
}

代码示例来源:origin: Phantast/smartnavi

/**
 * Sets the output quaternion and matrix with the provided quaternion and synchronises the setting
 *
 * @param quaternion The Quaternion to set (the result of the sensor fusion)
 */
private void setOrientationQuaternionAndMatrix(Quaternion quaternion) {
  Quaternion correctedQuat = quaternion.clone();
  // We inverted w in the deltaQuaternion, because currentOrientationQuaternion required it.
  // Before converting it back to matrix representation, we need to revert this process
  correctedQuat.w(-correctedQuat.w());
  synchronized (syncToken) {
    // Use gyro only
    currentOrientationQuaternion.copyVec4(quaternion);
    // Set the rotation matrix as well to have both representations
    SensorManager.getRotationMatrixFromVector(currentOrientationRotationMatrix.matrix, correctedQuat.ToArray());
  }
}

代码示例来源:origin: apacha/sensor-fusion-demo

/**
   * Sets the output quaternion and matrix with the provided quaternion and synchronises the setting
   * 
   * @param quaternion The Quaternion to set (the result of the sensor fusion)
   */
  private void setOrientationQuaternionAndMatrix(Quaternion quaternion) {
    correctedQuaternion.set(quaternion);
    // We inverted w in the deltaQuaternion, because currentOrientationQuaternion required it.
    // Before converting it back to matrix representation, we need to revert this process
    correctedQuaternion.w(-correctedQuaternion.w());

    synchronized (synchronizationToken) {
      // Use gyro only
      currentOrientationQuaternion.copyVec4(quaternion);

      // Set the rotation matrix as well to have both representations
      SensorManager.getRotationMatrixFromVector(currentOrientationRotationMatrix.matrix, correctedQuaternion.array());
    }
  }
}

代码示例来源:origin: apacha/sensor-fusion-demo

/**
   * Sets the output quaternion and matrix with the provided quaternion and synchronises the setting
   * 
   * @param quaternion The Quaternion to set (the result of the sensor fusion)
   */
  private void setOrientationQuaternionAndMatrix(Quaternion quaternion) {
    correctedQuaternion.set(quaternion);
    // We inverted w in the deltaQuaternion, because currentOrientationQuaternion required it.
    // Before converting it back to matrix representation, we need to revert this process
    correctedQuaternion.w(-correctedQuaternion.w());

    synchronized (synchronizationToken) {
      // Use gyro only
      currentOrientationQuaternion.copyVec4(quaternion);

      // Set the rotation matrix as well to have both representations
      SensorManager.getRotationMatrixFromVector(currentOrientationRotationMatrix.matrix, correctedQuaternion.array());
    }
  }
}

相关文章