0

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

  • Why didn't you post the exception? Is it a secret? – isnot2bad Dec 12 '13 at 23:00
  • Btw. I think it is not allowed to use the same array for both, result and rhs input vector. – isnot2bad Dec 12 '13 at 23:03
  • Okay, I got it. There was an exception which I was expecting in the code but at a different place. I didn't see the source of the exception correctly. It was the assignment operation before the multiplyMV method call. I made some indexing mistake there and though that the error was because of the MultiplyMV method. – YashaswiniPrathivadi Dec 12 '13 at 23:23
  • And I checked, we are allowed to use the same array for both result and rhs vector. Thanks @isnot2bad for your pointers! – YashaswiniPrathivadi Dec 12 '13 at 23:24
  • May be you should edit the post and add the exception. It will be helpful to others ! – darshan Dec 12 '13 at 23:48
  • @YashaswiniPrathivadi Yes, it is allowed to use the same array for both, but the documentation also says: (...) However, the resultVec element values are undefined if the resultVec elements overlap either the lhsMat or rhsVec elements.(...). – isnot2bad Dec 13 '13 at 07:25
  • @darshan the exception was the array out of bounds for the assignment "inVector[4] = 2" statement. Since it was right before the multiplyMV method call, when I was stepping through the code, thought it must be this method that caused my execution to go into thread.UncaughtException Handler. But when I looked closely, I found out my mistake. multiplyMV method was working like an angel.. – YashaswiniPrathivadi Dec 21 '13 at 09:07
  • @isnot2bad You are right. But I passed the same vector with the same offset to both rhsvector and the resultVector and it doesn't seem to having any problem. it translates the data just fine. – YashaswiniPrathivadi Dec 21 '13 at 09:15
  • @YashaswiniPrathivadi OK. But it's dangerous! Implementation might change in future and then suddenly it might not work any more. Better don't do it that way. – isnot2bad Dec 29 '13 at 10:21

2 Answers2

0

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

Phani
  • 61
  • 1
  • 1
  • Hi Phani. I think the order is R first as the lhsMat and inVector as the rhsVec .I confirmed it with the code execution too. – YashaswiniPrathivadi Dec 13 '13 at 06:42
  • ok another try :) `float[] inVector = new float[] {1,0,0,0};' `inVector[4] = 2; // edited here // Line 09` array size is 4 = max index is 3. – Phani Dec 17 '13 at 18:14
  • you are right. That was the error I had and I then edited my original post and highlighted it to show that my problem was with the array usage and not multiplyMV anymore. – YashaswiniPrathivadi Dec 21 '13 at 09:17
0

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.

Gregory Stein
  • 325
  • 4
  • 14