Topic locked

Distance to target for dummies?

March 19, 2014 - 8:37am #11

Hey

 

I'm very new to both Android development and Vuforia.
All I want to with Vuforia is to calculate the distance from camera to target and use that to fluently change the amplitude of a given sound.

 

However this: https://developer.vuforia.com/resources/dev-guide/distance-target doesn't help as I can't figure out where to do this.

I'll probably use the Image Target sample for this. As I understand, I should include the code, given in above link, somewhere in the sample file, but where? And how exactly?

 

Thank you in advance for your help :)

Distance to target for dummies?

March 19, 2014 - 8:54am #10

Well, first you should decide what exact sample you want to start with;

if you start with the ImageTargetNative sample, you can look for the code in the ImageTargets.cpp file, where the Pose of each Trackable is reported (look for the function called "renderFrame()"..)

then you can put the Tutorial code in there.

 

Distance to target for dummies?

March 21, 2014 - 3:25am #9

Thank you for your help but I can't find "renderFrame" in the ImageTargetsNative sample's cpp-file (ImageTargets.cpp)

The closets I get in the cpp file is this one line 309:

Java_com_qualcomm_QCARSamples_ImageTargets_ImageTargetsRenderer_renderFrame(JNIEnv *, jobject)

Is this the one?

 

Otherwise I found an empty method in the java file: ImageTargetsRenderer.java

Line 61:

    /** The native render function. */
    public native void renderFrame();
 
 
Should I place it here?

Distance to target for dummies?

March 21, 2014 - 3:34am #8

Java_com_qualcomm_QCARSamples_ImageTargets_ImageTargetsRenderer_renderFrame

Yes, that's the one.

 

Distance to target for dummies?

March 24, 2014 - 4:04am #7

Thank you again for your help, however I can't get the native example to compile. I suppose my setup is insufficient, I suspect that I failed with Cygwin.

However as I can understand from the documentation, I do not need the native sample to do what I want (change amplitude of sound based on distance to target).

 

Therefore I have tried to work with the ImageTargets part of the "VuforiaSamples" project.

However I suspect the sample code provided is in C++ and I'm not able to "convert" it to java, in order to use it in the VuforiaSamples project.

I'm working in ImageTargetRenderer.java >> renderFrame()

I've found the following which seems like what I need to focus on: (app. line 190 in the original sample).

// did we find any trackables this frame?
        for (int tIdx = 0; tIdx < state.getNumTrackableResults(); tIdx++)
        {
            TrackableResult result = state.getTrackableResult(tIdx);
            Trackable trackable = result.getTrackable();
            printUserData(trackable);
            Matrix44F modelViewMatrix_Vuforia = Tool
                .convertPose2GLMatrix(result.getPose());
            float[] modelViewMatrix = modelViewMatrix_Vuforia.getData();

The sample code tells me to include "math.h", in java I've imported "java.lang.Object" which should contain "sqrt()" (which I suspect is the reason for including math.h?).

In order to calculate the distance I've added the following code right below what is shown above here. But I have som problems I can't resolve and I hope I can get some help to get this working. (I've added "Aske" to my own variable to ensure it doesn't conflict with unknown existing variables).

 

//Aske
            float[] poseAske = modelViewMatrix;
            Vector3 positionAske = new Vector3((poseAske.data[3], poseAske.data[7], poseAske.data[11]);
            float distance = sqrt(	positionAske.data[0] * positionAske.data[0] +
                    				positionAske.data[1] * positionAske.data[1] +
                    				positionAske.data[2] * positionAske.data[2]);
            
            String tempDistance = Float.toString(distance);
            Log.d(LOGTAG, tempDistance);

As I see it, my major problem is to get some working equivalent of "Matrix34F" and "Vec3F". It is not something recognised in the java sample and not something I know hot get imported.
 

Distance to target for dummies?

March 24, 2014 - 4:06am #6

Sorry for double post, can't figure out how to delete the second one.

Distance to target for dummies?

March 24, 2014 - 4:28am #5

The Java equivalent of sqrt() (and other maths functions) is :

Math.sqrt( x )

and you need to import this package:

import java.lang.Math;

 

Distance to target for dummies?

March 24, 2014 - 4:49am #4

Ok, thanks :)

I'm still having trouble with implementing the rest of the code, do you have any feedback on that as well?

Distance to target for dummies?

March 24, 2014 - 4:56am #3

No, we plan to add more tutorials in the future which are Java-centric (as many of the old ones still refer to C++); however, porting from C++ to Java shoud be straigthforward, provided that you have some familiarity with Java programming.

 

Distance to target for dummies?

March 26, 2014 - 7:08am #2

Again thank you for your help.

I got it working and here is my "solution", hope that helps whoever might stumble upon this topic and need help:

 

//In the project "VuforiaSamples" find ImageTargetRenderer.java
 
//Import the following
import java.lang.Math;
import com.qualcomm.vuforia.Matrix34F;
import com.qualcomm.vuforia.Vec3F;
 
//in the method "renderFrame()" below " float[] modelViewMatrix = modelViewMatrix_Vuforia.getData();" insert:
 
            Matrix34F poseAske = result.getPose();
            float[] tempFloatArray = poseAske.getData();
            float distanceAske = (float) Math.sqrt( tempFloatArray[3] * tempFloatArray[3] + 
                                        tempFloatArray[7] * tempFloatArray[7] +
                                        tempFloatArray[11] * tempFloatArray[11] );
             
            String distanceAskeString = Float.toString(distanceAske);
            Log.d(LOGTAG, "Distance: " + distanceAskeString);

Note, this can probably be done better, but it works. I've included my name in the variables to ensure it doesn't conflict with existing variables and so I can easily locate my code in the project at any time. You can probably remove those without any issues, haven't tested it though.

Distance to target for dummies?

March 26, 2014 - 7:11am #1

Great, thanks for sharing, this will be helpful to many others.

Topic locked