Author Topic: Vuforia + jPCT-ae issue: 'Live' polyline update  (Read 678 times)

Offline jiarongkoh

  • byte
  • *
  • Posts: 18
    • View Profile
Vuforia + jPCT-ae issue: 'Live' polyline update
« on: February 15, 2015, 10:54:03 am »
I am working on the vuforia + jpct-ae app which I am attempting to simulate a 'live' feed of spatial data into jpct to draw a polyline. A polyline requires a start and end point. For this simulation, I created a .txt file containing the start points x,y and z, and used bufferedreader to feed the code into the polyline class. I fixed end point at (0,0,0) for this simulation.

The end result I am hoping to acheive is that the line is constantly being 'updated' as the AR application is running. Ultimately, I wish to feed those spatial data obtained via Kinect live (not sure if its possible just yet, hence simulating using a .txt file first).

My codes are as such, in the initApplicationAR() method in ImageTargets.java class, I added a Button to call for the NeedleUpdate() method which I define in ImageTargetsRenderer class.
Code: [Select]
private void initApplicationAR()
    {
        ...
        Button swap =(Button) findViewById(R.id.swap);
swap.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
ImageTargetsRenderer.NeedleUpdate();
}
});
    }

And at the ImageTargetsRenderer:
Code: [Select]
public ImageTargetsRenderer(ImageTargets activity) {
                SimpleVector needleStart = new SimpleVector (30.2, 30.4, -1);
SimpleVector needleEnd = new SimpleVector (0,0,0);
SimpleVector[] needlePoints= new SimpleVector[]{needleStart,needleEnd};
needle= new Polyline(needlePoints, RGBColor.BLACK);
needle.setWidth(5f);
world.addPolyline(needle);
needle.setParent(sphere);

cam = world.getCamera();

SimpleVector sv = new SimpleVector();
sv.set(cube.getTransformedCenter());
sv.y += 100;
sv.z += 100;

sun.setPosition(sv);

MemoryHelper.compact();
}

        public static void NeedleUpdate(){
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard, "point1.txt");

// Read text from file
StringBuilder text = new StringBuilder();
int numberOfRows = 0;
String line = null;

ArrayList<String> points = new ArrayList<String>();
ArrayList<Float> point1X = new ArrayList<Float>();
ArrayList<Float> point1Y = new ArrayList<Float>();
ArrayList<Float> point1Z = new ArrayList<Float>();
ArrayList<Integer> commaPos = new ArrayList<Integer>();

try {
//Read the Txt file containing the points.
//Stringbuilder reads everything as one whole 'paragraph'
BufferedReader br = new BufferedReader(new FileReader(file));

while ((line = br.readLine()) != null) {
numberOfRows++;
text.append(line);
text.append('\n');
//Replace spaces with commas
for (int c = 0; c < text.length(); c++) {
if (text.charAt(c) == ' ') {
text.replace(c, c + 1, ",");
}
}
//Store the individual lines of text into an array called points
points.add(text.toString());
text.delete(0, text.length());
}

br.close();

} catch (IOException e) {
}

//Stores the position of the commas into a separate array called commaPos
for (int i = 0; i < points.size(); i++) {
for (int j = 0; j < points.get(i).length(); j++) {
if (points.get(i).charAt(j) == ',') {
commaPos.add(j);
}
}
}

for (int pointsRow = 0; pointsRow < points.size(); pointsRow++) {

int firstpos = pointsRow * 2;
String xpoint = points.get(pointsRow).substring(0,
commaPos.get(firstpos));

int secondpos = (pointsRow * 2) + 1;

String ypoint = points.get(pointsRow)
.substring(commaPos.get(firstpos)+1,
commaPos.get(secondpos));

String zpoint=points.get(pointsRow)
.substring(commaPos.get(secondpos)+1,
points.get(pointsRow).length());

float xfloat= Float.parseFloat(xpoint);
point1X.add(xfloat);

float yfloat= Float.parseFloat(ypoint);
point1Y.add(yfloat);

float zfloat= Float.parseFloat(zpoint);
point1Z.add(zfloat);

SimpleVector point1 = new SimpleVector (point1X.get(pointsRow),point1Y.get(pointsRow)*-1,point1Z.get(pointsRow)*-1);// multiply -1 cuz flipped axis
SimpleVector point2 = new SimpleVector(0, 0, 0);

needle.update(new SimpleVector[]{point1,point2});
}

}

The result did perform the 'updating' of the needle. However, everytime it updates the camera layout freezes (see video here https://www.youtube.com/watch?v=WNN-VlQO-JY&feature=youtu.be ). This does not appear 'live'. I suspect that I might have written the NeedleUpdate() method at the wrong place such that the ImageTargetsRenderer class renders the initial 3D models, then renders again when the button is pressed. That is to say the needle is drawn only when button is pressed which obviously does not continuously 'updates' the needle.

What I wanna acheive is that the needle is always being updated continuously without the screen being frozen. Any advise?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11748
    • View Profile
    • http://www.jpct.net
Re: Vuforia + jPCT-ae issue: 'Live' polyline update
« Reply #1 on: February 15, 2015, 09:01:46 pm »
Am i getting this correct that for each update, you open some file on the sd card, create half a dozen of ArrayList instances, read the file and then update the Polyline. If that's really the case, i would say the solution to your problem is: Don't do that! Why are you reading the coordinates from a file over and over again?

Offline jiarongkoh

  • byte
  • *
  • Posts: 18
    • View Profile
Re: Vuforia + jPCT-ae issue: 'Live' polyline update
« Reply #2 on: February 17, 2015, 07:28:15 am »
For this simulation yes, this is what I'm doing.

If thats the case, how should I go about doing it? The coordinates needs to be streaming in continuously to allow the Polyline to update continuously without looking laggy.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11748
    • View Profile
    • http://www.jpct.net
Re: Vuforia + jPCT-ae issue: 'Live' polyline update
« Reply #3 on: February 17, 2015, 08:42:45 am »
Try to profile your code and watch the garbage collection. If you want to stream points at runtime, use a more optimized formats than strings and try to avoid all these SimpleVector creations at runtime. Try to reuse instances.
But first, make sure what actually causes the hick-up. It's most likely either your file access or the garbage collection but not the Polyline update itself, because that it actually pretty cheap.