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:Main.java

public static void sensorRotation2Matrix(float[] gravity, float[] geomagnetic, int rotation, float[] output) {
    switch (rotation) {
    case Surface.ROTATION_0:
    case Surface.ROTATION_180: /* Notice: not supported for ROTATION_180! */
        SensorManager.getRotationMatrix(output, null, gravity, geomagnetic);
        break;//from  www  . j a  va2  s .  c o  m
    case Surface.ROTATION_90:
        SensorManager.getRotationMatrix(mTmp, null, gravity, geomagnetic);
        SensorManager.remapCoordinateSystem(mTmp, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, output);
        break;
    case Surface.ROTATION_270:
        SensorManager.getRotationMatrix(mTmp, null, gravity, geomagnetic);
        SensorManager.remapCoordinateSystem(mTmp, SensorManager.AXIS_MINUS_Y, SensorManager.AXIS_X, output);
        break;
    }
    Matrix.rotateM(output, 0, 90.0F, 1.0F, 0.0F, 0.0F);
}

From source file:Main.java

public static void sensorRotationVector2Matrix(SensorEvent event, int rotation, float[] output) {
    float[] values = event.values;
    switch (rotation) {
    case Surface.ROTATION_0:
    case Surface.ROTATION_180: /* Notice: not supported for ROTATION_180! */
        SensorManager.getRotationMatrixFromVector(output, values);
        break;/* ww  w. ja va2 s.c om*/
    case Surface.ROTATION_90:
        SensorManager.getRotationMatrixFromVector(mTmp, values);
        SensorManager.remapCoordinateSystem(mTmp, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, output);
        break;
    case Surface.ROTATION_270:
        SensorManager.getRotationMatrixFromVector(mTmp, values);
        SensorManager.remapCoordinateSystem(mTmp, SensorManager.AXIS_MINUS_Y, SensorManager.AXIS_X, output);
        break;
    }
    Matrix.rotateM(output, 0, 90.0F, 1.0F, 0.0F, 0.0F);
}

From source file:Main.java

public static void sensorRotationVector2Matrix(SensorEvent event, int rotation, float[] output) {
    if (!sIsTruncated) {
        try {/*ww  w .  jav a 2s.  c o  m*/
            SensorManager.getRotationMatrixFromVector(sUIThreadTmp, event.values);
        } catch (Exception e) {
            // On some Samsung devices, SensorManager#getRotationMatrixFromVector throws an exception
            // if the rotation vector has more than 4 elements. Since only the four first elements are used,
            // we can truncate the vector without losing precision.
            Log.e(TAG, "maybe Samsung bug, will truncate vector");
            sIsTruncated = true;
        }
    }

    if (sIsTruncated) {
        System.arraycopy(event.values, 0, sTruncatedVector, 0, 4);
        SensorManager.getRotationMatrixFromVector(sUIThreadTmp, sTruncatedVector);
    }

    float[] values = event.values;
    switch (rotation) {
    case Surface.ROTATION_0:
    case Surface.ROTATION_180: /* Notice: not supported for ROTATION_180! */
        SensorManager.getRotationMatrixFromVector(output, values);
        break;
    case Surface.ROTATION_90:
        SensorManager.getRotationMatrixFromVector(sUIThreadTmp, values);
        SensorManager.remapCoordinateSystem(sUIThreadTmp, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X,
                output);
        break;
    case Surface.ROTATION_270:
        SensorManager.getRotationMatrixFromVector(sUIThreadTmp, values);
        SensorManager.remapCoordinateSystem(sUIThreadTmp, SensorManager.AXIS_MINUS_Y, SensorManager.AXIS_X,
                output);
        break;
    }
    Matrix.rotateM(output, 0, 90.0F, 1.0F, 0.0F, 0.0F);
}

From source file:com.adstrosoftware.notificationcompass.CompassService.java

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
        if (previousEventTimeStamp == Long.MIN_VALUE
                || (event.timestamp - previousEventTimeStamp) > DELAY_IN_NS) {
            previousEventTimeStamp = event.timestamp;

            float[] orientation = new float[3];
            float[] rotationMatrix = new float[16];
            float[] remappedRotationMatrix = new float[16];

            SensorManager.getRotationMatrixFromVector(rotationMatrix, event.values);

            if (event.values[0] <= -45) {
                SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z,
                        remappedRotationMatrix);
            } else {
                remappedRotationMatrix = rotationMatrix;
            }//from w w  w .  ja v  a  2s . c o  m

            SensorManager.getOrientation(remappedRotationMatrix, orientation);

            Notification notification;

            double azimuth = Math.toDegrees(orientation[0]);

            if (BuildConfig.DEBUG) {
                Log.d(TAG, "Azimuth = " + azimuth);
            }

            if (azimuth <= (NORTH + ANGLE) && azimuth >= (NORTH - ANGLE)) {
                if (BuildConfig.DEBUG)
                    Log.d(TAG, "NORTH");
                notification = buildNotification(R.string.north, R.drawable.ic_stat_north, azimuth);
            } else if (azimuth <= (NORTH_EAST + ANGLE) && azimuth > 0) {
                if (BuildConfig.DEBUG)
                    Log.d(TAG, "NORTH_EAST");
                notification = buildNotification(R.string.north_east, R.drawable.ic_stat_north_east, azimuth);
            } else if (azimuth >= (NORTH_WEST - ANGLE) && azimuth < 0) {
                if (BuildConfig.DEBUG)
                    Log.d(TAG, "NORTH_WEST");
                notification = buildNotification(R.string.north_west, R.drawable.ic_stat_north_west, azimuth);
            } else if (azimuth <= (EAST + ANGLE) && azimuth > 0) {
                if (BuildConfig.DEBUG)
                    Log.d(TAG, "EAST");
                notification = buildNotification(R.string.east, R.drawable.ic_stat_east, azimuth);
            } else if (azimuth >= (WEST - ANGLE) && azimuth < 0) {
                if (BuildConfig.DEBUG)
                    Log.d(TAG, "WEST");
                notification = buildNotification(R.string.west, R.drawable.ic_stat_west, azimuth);
            } else if (azimuth <= (SOUTH_EAST + ANGLE) && azimuth > 0) {
                if (BuildConfig.DEBUG)
                    Log.d(TAG, "SOUTH_EAST");
                notification = buildNotification(R.string.south_east, R.drawable.ic_stat_south_east, azimuth);
            } else if (azimuth >= (SOUTH_WEST - ANGLE) && azimuth < 0) {
                if (BuildConfig.DEBUG)
                    Log.d(TAG, "SOUTH_WEST");
                notification = buildNotification(R.string.south_west, R.drawable.ic_stat_south_west, azimuth);
            } else {
                if (BuildConfig.DEBUG)
                    Log.d(TAG, "SOUTH");
                notification = buildNotification(R.string.south, R.drawable.ic_stat_south, azimuth);
            }

            notificationManager.notify(NOTIFICATION_ID, notification);
        }
    }
}

From source file:com.example.casthelloworld.MainActivity.java

@Override
public void onSensorChanged(SensorEvent sensorEvent) {

    if (sensorEvent.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) {
        return;//from ww w  . j  a v  a  2 s .  co  m
    }

    SensorManager.getRotationMatrixFromVector(mRotationMatrix, sensorEvent.values);

    SensorManager.remapCoordinateSystem(mRotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z,
            mRotationMatrix);

    SensorManager.getOrientation(mRotationMatrix, orientationVals);

    orientationVals[2] = (float) Math.toDegrees(orientationVals[2]);

    float Roll = orientationVals[2];

    if (Roll < -85 && Roll > -95 && lastVal != 0.0f) {
        // Log.d("Middle: ", "" + Roll);
        lastVal = 0.0f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll <= -95 && Roll > -105 && lastVal != -0.2f) {
        // Log.d("Left", "" + Roll);
        lastVal = -0.2f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll >= -85 && Roll < -75 && lastVal != 0.2f) {
        // Log.d("Right", "" + Roll);
        lastVal = 0.2f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll <= -105 && Roll > -115 && lastVal != -0.4f) {
        // Log.d("Left", "" + Roll);
        lastVal = -0.4f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll >= -75 && Roll < -65 && lastVal != 0.4f) {
        // Log.d("Right", "" + Roll);
        lastVal = 0.4f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll <= -115 && Roll > -125 && lastVal != -0.6f) {
        // Log.d("Left", "" + Roll);
        lastVal = -0.6f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll >= -65 && Roll < -55 && lastVal != 0.6f) {
        // Log.d("Right", "" + Roll);
        lastVal = 0.6f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll <= -125 && Roll > -135 && lastVal != -0.8f) {
        // Log.d("Left", "" + Roll);
        lastVal = -0.8f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll >= -55 && Roll < -45 && lastVal != 0.8f) {
        // Log.d("Right", "" + Roll);
        lastVal = 0.8f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll <= -135 && lastVal != -1.0f && lastVal != 1.0f) {
        // Log.d("Left", "" + Roll);
        lastVal = -1.0f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll >= -45 && lastVal != 1.0f && lastVal != -1.0f) {
        // Log.d("Right", "" + Roll);
        lastVal = 1.0f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    }

}

From source file:jp.co.recruit_lifestyle.android.widget.BeerSwipeRefreshLayout.java

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() != Sensor.TYPE_ROTATION_VECTOR || !mBeerView.isMax()) {
        return;//from  www .  ja  v  a 2  s .  co  m
    }

    float[] rotationMatrix = new float[9];
    SensorManager.getRotationMatrixFromVector(rotationMatrix, event.values);

    float[] adjustedRotationMatrix = new float[9];
    SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z,
            adjustedRotationMatrix);

    float[] orientation = new float[3];
    SensorManager.getOrientation(adjustedRotationMatrix, orientation);

    float roll = orientation[2] * -57;

    if (roll < ROLL_LIMIT && roll > -ROLL_LIMIT) {
        mBeerView.drawGlass(-roll);
        mBeerView.drawGlassFroth(-roll, 1);
        mOldRoll = -roll;
    } else {
        mBeerView.drawGlass(mOldRoll);
        mBeerView.drawGlassFroth(mOldRoll, 1);
    }
}

From source file:net.line2soft.preambul.controllers.SlippyMapListener.java

@Override
public void onSensorChanged(SensorEvent event) {
    CompassView cp = (CompassView) activity.findViewById(R.id.compass);
    CompassView cpBig = (CompassView) activity.findViewById(R.id.compassBig);
    switch (event.sensor.getType()) {
    case Sensor.TYPE_ACCELEROMETER:
        System.arraycopy(event.values, 0, mGravity, 0, 3);
        break;/*w ww .j  a v  a2  s .c  om*/
    case Sensor.TYPE_MAGNETIC_FIELD:
        System.arraycopy(event.values, 0, mMagnetic, 0, 3);
        break;
    default:
        return;
    }
    if (SensorManager.getRotationMatrix(mRotationM, null, mGravity, mMagnetic)) {
        SensorManager.remapCoordinateSystem(mRotationM, SensorManager.AXIS_X, SensorManager.AXIS_Z,
                mRemapedRotationM);
        SensorManager.getOrientation(mRemapedRotationM, mOrientation);
        int mAzimuth = (int) Math.round((Math.toDegrees(mOrientation[0])) * 2) / 2;
        cp.updateDirection(mAzimuth);
        cpBig.updateDirection(mAzimuth);
    }
}

From source file:com.metinkale.prayerapp.compass.Main.java

@Override
public void onRotationUpdate(float[] newMatrix) {
    if (mMode == Mode.Map) {
        return;/* w w  w  . j  ava2s .  com*/
    }
    // remap matrix values according to display rotation, as in
    // SensorManager documentation.
    switch (mDisplayRotation) {
    case Surface.ROTATION_0:
    case Surface.ROTATION_180:
        break;
    case Surface.ROTATION_90:
        SensorManager.remapCoordinateSystem(newMatrix, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X,
                newMatrix);
        break;
    case Surface.ROTATION_270:
        SensorManager.remapCoordinateSystem(newMatrix, SensorManager.AXIS_MINUS_Y, SensorManager.AXIS_X,
                newMatrix);
        break;
    default:
        break;
    }
    mRotationMatrix.set(newMatrix);
    mOrientationCalculator.getOrientation(mRotationMatrix, mDisplayRotation, mDerivedDeviceOrientation);

    updateFrag((mDerivedDeviceOrientation[1] > -55f) ? Mode.ThreeDim : Mode.TwoDim);

    mList.onUpdateSensors(mDerivedDeviceOrientation);
}

From source file:com.tritop.androsense2.fragments.GpsFragment.java

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        mAccel = event.values;//from ww  w . ja v  a 2s  .co m
    }
    if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
        mMag = event.values;
    }
    if ((mAccel != null) && (mMag != null)) {
        float R[] = new float[9];
        float I[] = new float[9];
        float Rot[] = new float[9];
        if (SensorManager.getRotationMatrix(R, I, mAccel, mMag)) {
            float orientation[] = new float[3];
            int axisX = 0, axisY = 0;
            switch (display.getRotation()) {
            case Surface.ROTATION_0:
                axisX = SensorManager.AXIS_X;
                axisY = SensorManager.AXIS_Y;
                break;
            case Surface.ROTATION_90:
                axisX = SensorManager.AXIS_Y;
                axisY = SensorManager.AXIS_MINUS_X;
                break;
            case Surface.ROTATION_180:
                axisX = SensorManager.AXIS_MINUS_X;
                axisY = SensorManager.AXIS_MINUS_Y;
                break;
            case Surface.ROTATION_270:
                axisX = SensorManager.AXIS_MINUS_Y;
                axisY = SensorManager.AXIS_X;
                break;
            default:
                break;
            }

            SensorManager.remapCoordinateSystem(R, axisX, axisY, Rot);
            SensorManager.getOrientation(Rot, orientation);
            aRotation = orientation[0];
            aRotation = (int) Math.toDegrees(aRotation);
            satView.setAzimutRotation(-aRotation);
            satView.invalidate();
        }
    }
}

From source file:com.shadowmaps.example.GpsTestActivity.java

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override/* w ww .  j av a 2 s  . co 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();
    }

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