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.
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:
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?