Usage of Matrix.multiplyMV method leading to uncaught exception

867 views Asked by At

I am trying to do a basic Matrix multiplication with a vector using the static method multiplyMV present in the "Matrix" class in android. Following is a small snippet of the code:

// packaged included
import java.lang.Object;
import android.opengl.Matrix;

public class Example extends Thread
{
  float[] R = new float[]  {1,0,0,0,   0,1,0,0,  0,0,1,0,  0,0,0,0};
  float[] inVector = new float[] {1,0,0,0};
  public void run()
  {
    inVector[4] = 2; // edited here // Line 09
    Matrix.multiplyMV(inVector , 0, R, 0, inVector , 0); // Line 10

  }
}

I am not sure what mistake I am doing, but this code leads to an uncaught exception. I tried to see at what point in the code I am getting this exception. If I put a break point at line10, the execution stops at the line before that, and if I then give a step-into command I again end up with the exception. So the usage of the method is causing this exception but I don't know why. Am I missing some packages? I can't even catch this exception and print stack-trace to see what is happening. Any help is much appreciated!

The context for the code is that it has to perform translations of vectors from one coordinate system to another.

Edit: Exception found: IndexOutOfBounds at line 09

2

There are 2 answers

3
Phani On

Swap R and inVector in multiplyMV call. Use inVector as lhsMat.

0
Gregory Stein On

Although you can use same array, you cannot use same elements as stated here: https://developer.android.com/reference/android/opengl/Matrix.html

multiplyMV

Added in API level 1

void multiplyMV (float[] resultVec, 
                int resultVecOffset, 
                float[] lhsMat, 
                int lhsMatOffset, 
                float[] rhsVec, 
                int rhsVecOffset)

Multiplies a 4 element vector by a 4x4 matrix and stores the result in a 4-element column vector. In matrix notation: result = lhs x rhs

The same float array may be passed for resultVec, lhsMat, and/or rhsVec. However, the resultVec element values are undefined if the resultVec elements overlap either the lhsMat or rhsVec elements.

Your code is wrong since you set same offset for resultVec and rhsVec.