Example usage for android.hardware SensorManager remapCoordinateSystem

List of usage examples for android.hardware SensorManager remapCoordinateSystem

Introduction

In this page you can find the example usage for android.hardware SensorManager remapCoordinateSystem.

Prototype


public static boolean remapCoordinateSystem(float[] inR, int X, int Y, float[] outR) 

Source Link

Document

Rotates the supplied rotation matrix so it is expressed in a different coordinate system.

Usage

From source file:com.android.gpstest.GpsTestActivity.java

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override/*from  w ww  .ja  v a 2  s. c o  m*/
public void onSensorChanged(SensorEvent event) {

    double orientation = Double.NaN;
    double tilt = Double.NaN;

    switch (event.sensor.getType()) {
    case Sensor.TYPE_ROTATION_VECTOR:
        // Modern rotation vector sensors
        if (!mTruncateVector) {
            try {
                SensorManager.getRotationMatrixFromVector(mRotationMatrix, event.values);
            } catch (IllegalArgumentException e) {
                // On some Samsung devices, an exception is thrown if this vector > 4 (see #39)
                // Truncate the array, since we can deal with only the first four values
                Log.e(TAG, "Samsung device error? Will truncate vectors - " + e);
                mTruncateVector = true;
                // Do the truncation here the first time the exception occurs
                getRotationMatrixFromTruncatedVector(event.values);
            }
        } else {
            // Truncate the array to avoid the exception on some devices (see #39)
            getRotationMatrixFromTruncatedVector(event.values);
        }

        int rot = getWindowManager().getDefaultDisplay().getRotation();
        switch (rot) {
        case Surface.ROTATION_0:
            // No orientation change, use default coordinate system
            SensorManager.getOrientation(mRotationMatrix, mValues);
            // Log.d(TAG, "Rotation-0");
            break;
        case Surface.ROTATION_90:
            // Log.d(TAG, "Rotation-90");
            SensorManager.remapCoordinateSystem(mRotationMatrix, SensorManager.AXIS_Y,
                    SensorManager.AXIS_MINUS_X, mRemappedMatrix);
            SensorManager.getOrientation(mRemappedMatrix, mValues);
            break;
        case Surface.ROTATION_180:
            // Log.d(TAG, "Rotation-180");
            SensorManager.remapCoordinateSystem(mRotationMatrix, SensorManager.AXIS_MINUS_X,
                    SensorManager.AXIS_MINUS_Y, mRemappedMatrix);
            SensorManager.getOrientation(mRemappedMatrix, mValues);
            break;
        case Surface.ROTATION_270:
            // Log.d(TAG, "Rotation-270");
            SensorManager.remapCoordinateSystem(mRotationMatrix, SensorManager.AXIS_MINUS_Y,
                    SensorManager.AXIS_X, mRemappedMatrix);
            SensorManager.getOrientation(mRemappedMatrix, mValues);
            break;
        default:
            // This shouldn't happen - assume default orientation
            SensorManager.getOrientation(mRotationMatrix, mValues);
            // Log.d(TAG, "Rotation-Unknown");
            break;
        }
        orientation = Math.toDegrees(mValues[0]); // azimuth
        tilt = Math.toDegrees(mValues[1]);
        break;
    case Sensor.TYPE_ORIENTATION:
        // Legacy orientation sensors
        orientation = event.values[0];
        break;
    default:
        // A sensor we're not using, so return
        return;
    }

    // Correct for true north, if preference is set
    if (mFaceTrueNorth && mGeomagneticField != null) {
        orientation += mGeomagneticField.getDeclination();
        // Make sure value is between 0-360
        orientation = MathUtils.mod((float) orientation, 360.0f);
    }

    for (GpsTestListener listener : mGpsTestListeners) {
        listener.onOrientationChanged(orientation, tilt);
    }
}

From source file:com.example.zoetablet.BasicFragmentActivity.java

public float[] calculateOrientation() {
    float[] values = new float[3];
    float[] R = new float[9];
    float[] outR = new float[9];

    SensorManager.getRotationMatrix(R, null, aValues, mValues);
    SensorManager.remapCoordinateSystem(R, SensorManager.AXIS_X, SensorManager.AXIS_Y, outR);

    SensorManager.getOrientation(outR, values);

    // Convert from Radians to Degrees.
    values[0] = (float) Math.toDegrees(values[0]);
    values[1] = (float) Math.toDegrees(values[1]);
    values[2] = (float) Math.toDegrees(values[2]);

    // System.out.println("Compass " + values[0] + " " + values[1] + " " + values[2]);
    if (values[0] != 0.0 && values[1] != 0.0) {
        compassV0 = values[0];//from   ww  w  . ja  va2  s .  c o  m
        compassV1 = values[1];
        compassV2 = values[2];
    }

    if (values[0] == 0.0 && values[1] == 0.0) {
        //If data is spitting out zeros, keep last good value
        values[0] = compassV0;
        values[1] = compassV1;
        values[2] = compassV2;
    }
    return values;
}

From source file:uk.org.rivernile.edinburghbustracker.android.fragments.general.BusStopDetailsFragment.java

/**
 * Update the direction needle so that it is pointing towards the bus stop,
 * based on the device location and the direction it is facing.
 *//*w ww. j  a  v  a 2 s . c  o m*/
private void updateDirectionNeedle() {
    // We need values for location, the accelerometer and magnetometer to
    // continue.
    if (lastLocation == null || accelerometerValues == null || magnetometerValues == null) {
        // Make sure the needle isn't showing.
        txtDistance.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
        recycleNeedleBitmapIfNotNull(null);

        return;
    }

    // Calculating the rotation matrix may fail, for example, if the device
    // is in freefall. In that case we cannot continue as the values will
    // be unreliable.
    if (!SensorManager.getRotationMatrix(rotationMatrix, null, accelerometerValues, magnetometerValues)) {
        return;
    }

    // The screen rotation was obtained earlier.
    switch (screenRotation) {
    // There's lots of information about this elsewhere, but briefly;
    // The values from the sensors are in the device's coordinate system
    // which may be correct if the device is in its natural orientation,
    // but it needs to be remapped if the device is rotated.
    case Surface.ROTATION_0:
        SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z,
                rotationMatrix);
        break;
    case Surface.ROTATION_90:
        SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_Z, SensorManager.AXIS_MINUS_X,
                rotationMatrix);
        break;
    case Surface.ROTATION_180:
        SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_MINUS_X,
                SensorManager.AXIS_MINUS_Z, rotationMatrix);
        break;
    case Surface.ROTATION_270:
        SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_MINUS_Z, SensorManager.AXIS_X,
                rotationMatrix);
        break;
    }

    // Get the X, Y and Z orientations, which are in radians. Covert this
    // in to degrees East of North.
    SensorManager.getOrientation(rotationMatrix, headings);
    double heading = Math.toDegrees(headings[0]);

    // If there's a GeomagneticField value, then adjust the heading to take
    // this in to account.
    if (geoField != null) {
        heading -= geoField.getDeclination();
    }

    // The orientation is in the range of -180 to +180. Convert this in to
    // a range of 0 to 360.
    final float bearingTo = distance[1] < 0 ? distance[1] + 360 : distance[1];

    // This is the heading to the bus stop.
    heading = bearingTo - heading;

    // The above calculation may come out as a negative number again. Put
    // this back in to the range of 0 to 360.
    if (heading < 0) {
        heading += 360;
    }

    // This 'if' statement is required to prevent a crash during device
    // rotation. It ensured that the Fragment is still part of the Activity.
    if (isAdded()) {
        // Get the arrow bitmap from the resources.
        final Bitmap needleIn = BitmapFactory.decodeResource(getResources(), R.drawable.heading_arrow);
        // Get an identity matrix and rotate it by the required amount.
        final Matrix m = new Matrix();
        m.setRotate((float) heading % 360, (float) needleIn.getWidth() / 2, (float) needleIn.getHeight() / 2);
        // Apply the rotation matrix to the Bitmap, to create a new Bitmap.
        final Bitmap needleOut = Bitmap.createBitmap(needleIn, 0, 0, needleIn.getWidth(), needleIn.getHeight(),
                m, true);

        // Recycle the needle read in if it's not the same as the rotated
        // needle.
        if (needleIn != needleOut) {
            needleIn.recycle();
        }

        // This Bitmap needs to be converted to a Drawable type.
        final BitmapDrawable drawable = new BitmapDrawable(getResources(), needleOut);
        // Set the new needle to be on the right hand side of the TextView.
        txtDistance.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
        recycleNeedleBitmapIfNotNull(needleOut);
    } else {
        // If the Fragment is not added to the Activity, then make sure
        // there's no needle.
        txtDistance.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
        recycleNeedleBitmapIfNotNull(null);
    }
}

From source file:com.dragon4.owo.ar_trace.ARCore.MixView.java

public void onSensorChanged(SensorEvent evt) {
    try {//from w  w  w.j  a va 2 s .  c  o  m
        //         killOnError();

        // ?? , ?  ???  ? ? ? 
        if (evt.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            grav[0] = evt.values[0];
            grav[1] = evt.values[1];
            grav[2] = evt.values[2];

            augScreen.postInvalidate(); // ? 
        } else if (evt.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
            mag[0] = evt.values[0];
            mag[1] = evt.values[1];
            mag[2] = evt.values[2];

            augScreen.postInvalidate(); // ? 
        }

        //  ? 
        SensorManager.getRotationMatrix(RTmp, I, grav, mag);
        //  (?)
        SensorManager.remapCoordinateSystem(RTmp, SensorManager.AXIS_X, SensorManager.AXIS_MINUS_Z, Rot);

        //  ? ? 
        tempR.set(Rot[0], Rot[1], Rot[2], Rot[3], Rot[4], Rot[5], Rot[6], Rot[7], Rot[8]);

        //       ,  
        finalR.toIdentity();
        finalR.prod(m4);
        finalR.prod(m1);
        finalR.prod(tempR);
        finalR.prod(m3);
        finalR.prod(m2);
        finalR.invert();

        // ? ?  ? ??   ...
        histR[rHistIdx].set(finalR);
        rHistIdx++;
        if (rHistIdx >= histR.length)
            rHistIdx = 0;

        smoothR.set(0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f);
        for (int i = 0; i < histR.length; i++) {
            smoothR.add(histR[i]);
        }
        smoothR.mult(1 / (float) histR.length);

        synchronized (mixContext.rotationM) {
            mixContext.rotationM.set(smoothR);
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:org.mixare.MixViewActivity.java

public void onSensorChanged(SensorEvent evt) {
    try {/*www .  j a  v  a2s  .  c  om*/
        if (getMixViewData().getSensorGyro() != null) {

            if (evt.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
                getMixViewData().setGyro(evt.values);
            }

            if (evt.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
                getMixViewData().setGrav(
                        getMixViewData().getGravFilter().lowPassFilter(evt.values, getMixViewData().getGrav()));
            } else if (evt.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
                getMixViewData().setMag(
                        getMixViewData().getMagFilter().lowPassFilter(evt.values, getMixViewData().getMag()));
            }
            getMixViewData().setAngle(getMixViewData().getMagFilter().complementaryFilter(
                    getMixViewData().getGrav(), getMixViewData().getGyro(), 30, getMixViewData().getAngle()));

            SensorManager.getRotationMatrix(getMixViewData().getRTmp(), getMixViewData().getI(),
                    getMixViewData().getGrav(), getMixViewData().getMag());
        } else {
            if (evt.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
                getMixViewData().setGrav(evt.values);
            } else if (evt.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
                getMixViewData().setMag(evt.values);
            }
            SensorManager.getRotationMatrix(getMixViewData().getRTmp(), getMixViewData().getI(),
                    getMixViewData().getGrav(), getMixViewData().getMag());
        }

        //         augmentedView.postInvalidate();
        hudView.postInvalidate();
        projectedMapView.postInvalidate();

        int rotation = Compatibility.getRotation(this);

        if (rotation == 1) {
            SensorManager.remapCoordinateSystem(getMixViewData().getRTmp(), SensorManager.AXIS_X,
                    SensorManager.AXIS_MINUS_Z, getMixViewData().getRot());
        } else {
            SensorManager.remapCoordinateSystem(getMixViewData().getRTmp(), SensorManager.AXIS_Y,
                    SensorManager.AXIS_MINUS_Z, getMixViewData().getRot());
        }
        getMixViewData().getTempR().set(getMixViewData().getRot()[0], getMixViewData().getRot()[1],
                getMixViewData().getRot()[2], getMixViewData().getRot()[3], getMixViewData().getRot()[4],
                getMixViewData().getRot()[5], getMixViewData().getRot()[6], getMixViewData().getRot()[7],
                getMixViewData().getRot()[8]);

        getMixViewData().getFinalR().toIdentity();
        getMixViewData().getFinalR().prod(getMixViewData().getM4());
        getMixViewData().getFinalR().prod(getMixViewData().getM1());
        getMixViewData().getFinalR().prod(getMixViewData().getTempR());
        getMixViewData().getFinalR().prod(getMixViewData().getM3());
        getMixViewData().getFinalR().prod(getMixViewData().getM2());
        getMixViewData().getFinalR().invert();

        getMixViewData().getHistR()[getMixViewData().getrHistIdx()].set(getMixViewData().getFinalR());

        int histRLenght = getMixViewData().getHistR().length;

        getMixViewData().setrHistIdx(getMixViewData().getrHistIdx() + 1);
        if (getMixViewData().getrHistIdx() >= histRLenght)
            getMixViewData().setrHistIdx(0);

        getMixViewData().getSmoothR().set(0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f);
        for (int i = 0; i < histRLenght; i++) {
            getMixViewData().getSmoothR().add(getMixViewData().getHistR()[i]);
        }
        getMixViewData().getSmoothR().mult(1 / (float) histRLenght);

        MixContext.getInstance().updateSmoothRotation(getMixViewData().getSmoothR());

        //         float[] orientation = new float[3];
        //            float[] rTemp = getMixViewData().getRTmp();
        //         SensorManager.getOrientation(rTemp, orientation);
        //            Log.d("PROJECT_MAP", "Sensor Angles");
        //         Log.d("PROJECT_MAP", orientation[0] + ", " + orientation[1] + ", " + orientation[2]);
        //            Log.d("PROJECT_MAP", "Rotation Matrix Before Smoothing");
        //            Log.d("PROJECT_MAP", rTemp[0] + ", " + rTemp[1] + ", " + rTemp[2]);
        //            Log.d("PROJECT_MAP", rTemp[3] + ", " + rTemp[4] + ", " + rTemp[5]);
        //            Log.d("PROJECT_MAP", rTemp[6] + ", " + rTemp[7] + ", " + rTemp[8]);

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}