ArViewer  Android Version
Arvos - Augmented reality viewer open source
 All Classes Namespaces Files Functions Variables
ArvosTouchHandler.java
Go to the documentation of this file.
1 /*
2  Copyright (C) 2013, Peter Graf
3 
4  This file is part of Arvos - AR Viewer Open Source for Android.
5  Arvos is free software.
6 
7  This program is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program. If not, see <http://www.gnu.org/licenses/>.
19 
20  For more information on the AR Viewer Open Source or Peter Graf,
21  please see: http://www.mission-base.com/.
22  */
23 
24 package com.mission_base.arviewer_android.viewer.opengl;
25 
26 import java.util.Map.Entry;
27 
28 import android.opengl.Matrix;
29 
30 import com.mission_base.arviewer_android.Arvos;
31 import com.mission_base.arviewer_android.viewer.utilities.Ray;
32 import com.mission_base.arviewer_android.viewer.utilities.Triangle;
33 import com.mission_base.arviewer_android.viewer.utilities.Vector;
34 
43 public class ArvosTouchHandler extends Thread
44 {
45  private static String Lock = "Lock";
46 
47  private Arvos mInstance;
48  private float mX;
49  private float mY;
50 
51  public ArvosTouchHandler(float x, float y)
52  {
53  super("ArvosTouchHandler");
54 
55  mInstance = Arvos.getInstance();
56  mX = x;
57  mY = y;
58  }
59 
60  public void run()
61  {
62  if (!mInstance.mHandleTouch)
63  {
64  return;
65  }
66 
67  synchronized (Lock)
68  {
69  if (!mInstance.mHandleTouch)
70  {
71  return;
72  }
73  handleTouch(mX, mY);
74  mInstance.mHandleTouch = false;
75  mInstance.mModelViewMatrixesRequested = false;
76  }
77  }
78 
79  private void handleTouch(float x, float y)
80  {
81  if (!mInstance.mHandleTouch)
82  {
83  return;
84  }
85 
86  // let the renderer provide the model view matrixes of all objects
87  //
88  mInstance.mModelViewMatrixesRequested = true;
89  while (mInstance.mModelViewMatrixesRequested)
90  {
91  try
92  {
93  Thread.sleep(18);
94  }
95  catch (InterruptedException e)
96  {
97  return;
98  }
99  if (!mInstance.mHandleTouch)
100  {
101  return;
102  }
103  }
104  if (!mInstance.mHandleTouch)
105  {
106  return;
107  }
108 
109  Triangle triangle = new Triangle(new float[3], new float[3], new float[3]);
110 
111  float[] intersection = new float[3];
112  float minLength = Float.MAX_VALUE;
113  int id = 0;
114  boolean found = false;
115 
116  float[] convertedSquare = new float[Square.vertices.length];
117  float[] resultVector = new float[4];
118  float[] inputVector = new float[4];
119 
120  float[] projection = mInstance.mProjectionMatrix;
121  if (projection == null)
122  {
123  return;
124  }
125 
126  synchronized (mInstance.mModelViewMatrixes)
127  {
128  for (Entry<Integer, float[]> entry : mInstance.mModelViewMatrixes.entrySet())
129  {
130  if (!mInstance.mHandleTouch)
131  {
132  return;
133  }
134 
135  float[] modelView = entry.getValue();
136 
137  Ray ray = new Ray(modelView, projection, mInstance.mWidth, mInstance.mHeight, x, y);
138 
139  for (int i = 0; i < Square.vertices.length; i = i + 3)
140  {
141  inputVector[0] = Square.vertices[i];
142  inputVector[1] = Square.vertices[i + 1];
143  inputVector[2] = Square.vertices[i + 2];
144  inputVector[3] = 1;
145 
146  Matrix.multiplyMV(resultVector, 0, modelView, 0, inputVector, 0);
147  convertedSquare[i] = resultVector[0] / resultVector[3];
148  convertedSquare[i + 1] = resultVector[1] / resultVector[3];
149  convertedSquare[i + 2] = resultVector[2] / resultVector[3];
150  }
151 
152  triangle.V0[0] = convertedSquare[0];
153  triangle.V0[1] = convertedSquare[1];
154  triangle.V0[2] = convertedSquare[2];
155  triangle.V1[0] = convertedSquare[3];
156  triangle.V1[1] = convertedSquare[4];
157  triangle.V1[2] = convertedSquare[5];
158  triangle.V2[0] = convertedSquare[6];
159  triangle.V2[1] = convertedSquare[7];
160  triangle.V2[2] = convertedSquare[8];
161 
162  if (Triangle.intersectRayAndTriangle(ray, triangle, intersection) == 1)
163  {
164  found = true;
165  float length = Vector.length(intersection);
166  if (length < minLength)
167  {
168  minLength = length;
169  id = entry.getKey();
170  }
171  continue;
172  }
173 
174  triangle.V0[0] = convertedSquare[9];
175  triangle.V0[1] = convertedSquare[10];
176  triangle.V0[2] = convertedSquare[11];
177 
178  if (Triangle.intersectRayAndTriangle(ray, triangle, intersection) == 1)
179  {
180  found = true;
181  float length = Vector.length(intersection);
182  if (length < minLength)
183  {
184  minLength = length;
185  id = entry.getKey();
186  }
187  }
188  }
189  mInstance.mModelViewMatrixes.clear();
190  }
191 
192  if (found)
193  {
194  // Arvos.log("touch", " " + id);
195  mInstance.mAugment.addClick(id);
196  }
197  }
198 }