Advertisement
Guest User

java.touch

a guest
Jan 31st, 2017
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.80 KB | None | 0 0
  1. boolean isTapOnSetColor(float x, float y) {
  2.  
  3.     Vec3F intersection, lineStart, lineEnd;
  4.  
  5.     DisplayMetrics metrics = new DisplayMetrics();
  6.     mActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
  7.  
  8.     for ( Map.Entry<String, Matrix44F> entry : modelViewMatrix_colors.entrySet()) {
  9.         String key = entry.getKey();
  10.         Matrix44F matrix44F = entry.getValue();
  11.  
  12.         intersection = SampleMath.getPointToPlaneIntersection(
  13.                 SampleMath.Matrix44FInverse(vuforiaAppSession.getProjectionMatrix()),
  14.                 matrix44F, metrics.widthPixels, metrics.heightPixels,
  15.                 new Vec2F(x, y), new Vec3F(0, 0, 0), new Vec3F(0, 0, 1));
  16.         lineStart = SampleMath.getPointToPlaneLineStart(
  17.                 SampleMath.Matrix44FInverse(vuforiaAppSession.getProjectionMatrix()),
  18.                 matrix44F, metrics.widthPixels, metrics.heightPixels,
  19.                 new Vec2F(x, y), new Vec3F(0, 0, 0), new Vec3F(0, 0, 1));
  20.         lineEnd = SampleMath.getPointToPlaneLineEnd(
  21.                 SampleMath.Matrix44FInverse(vuforiaAppSession.getProjectionMatrix()),
  22.                 matrix44F, metrics.widthPixels, metrics.heightPixels,
  23.                 new Vec2F(x, y), new Vec3F(0, 0, 0), new Vec3F(0, 0, 1));
  24.  
  25.         boolean bool = checkIntersectionLine(matrix44F, lineStart, lineEnd);
  26.         if (bool) {
  27.             String[] ids = key.split("_");
  28.             Realm realm = Realm.getDefaultInstance();
  29.             Colors color = realm.where(Colors.class).equalTo("id", Integer.parseInt(ids[1])).findFirst();
  30.             Log.d(Constants.TAG, "CLICKED!!!!!!!!!!!!!!!!!! " + key + " / " + color.getName_ru());
  31.             return true;
  32.         }
  33.  
  34.     }
  35.     return false;
  36. }
  37.  
  38. boolean checkIntersectionLine(Matrix44F transformA2, Vec3F pointA, Vec3F pointB) {
  39.     Matrix44F transformA = SampleMath.Matrix44FTranspose(transformA2);
  40.     Vec3F lineDir = SampleMath.Vec3FSub(pointB, pointA);
  41.  
  42.     List<Vec3F> dominoTransformedVertices = new ArrayList<>();
  43.     for (int i = 0; i < Verts_colors.size()/3; i++) {
  44.         dominoTransformedVertices.add(SampleMath.Vec3FTransform(new Vec3F(Verts_colors.get(i*3).floatValue(), Verts_colors.get(i*3+1).floatValue(), Verts_colors.get(i*3+2).floatValue()), transformA));
  45.     }
  46.  
  47.     for(int i=0; i<Normals_colors.size()/3; i++) {
  48.         Vec3F normal = SampleMath.Vec3FTransformNormal(new Vec3F(Normals_colors.get(i*3).floatValue(), Normals_colors.get(i*3+1).floatValue(), Normals_colors.get(i*3+2).floatValue()), transformA);
  49.         Vec3F cross = SampleMath.Vec3FCross(normal, lineDir);
  50.  
  51.         if (isSeparatingAxisLine(dominoTransformedVertices, normal, pointA, pointB))
  52.             return false;
  53.  
  54.         if (isSeparatingAxisLine(dominoTransformedVertices, cross, pointA, pointB))
  55.             return false;
  56.     }
  57.  
  58.     return true;
  59. }
  60.  
  61. boolean isSeparatingAxisLine(List<Vec3F> dominoTransformedVertices, Vec3F axis, Vec3F pointA, Vec3F pointB) {
  62.  
  63.     float magnitude = axis.getData()[0] * axis.getData()[0] + axis.getData()[1] * axis.getData()[1] + axis.getData()[2] * axis.getData()[2];
  64.     if (magnitude < 0.00001) return false;
  65.  
  66.     float minA, maxA, minB, maxB;
  67.  
  68.     minA = maxA = SampleMath.Vec3FDot(dominoTransformedVertices.toArray(new Vec3F[dominoTransformedVertices.size()])[0], axis);
  69.  
  70.     float p;
  71.  
  72.     for (int i = 1; i < dominoTransformedVertices.size(); i++) {
  73.         p = SampleMath.Vec3FDot(dominoTransformedVertices.toArray(new Vec3F[dominoTransformedVertices.size()])[i], axis);
  74.         if (p < minA) minA = p;
  75.         if (p > maxA) maxA = p;
  76.     }
  77.  
  78.     minB = maxB = SampleMath.Vec3FDot(pointA, axis);
  79.     p = SampleMath.Vec3FDot(pointB, axis);
  80.     if (p < minB) minB = p;
  81.     if (p > maxB) maxB = p;
  82.  
  83.     if (maxA < minB) return true;
  84.     if (minA > maxB) return true;
  85.  
  86.     return false;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement