GLMotionFrame.java
Go to the documentation of this file.
1 package com.intel.realsense.librealsense;
2 
3 import android.graphics.Rect;
4 import android.opengl.GLES10;
5 import android.renderscript.Float3;
6 
7 import java.nio.ByteBuffer;
8 import java.nio.ByteOrder;
9 import java.nio.IntBuffer;
10 
11 public class GLMotionFrame extends GLFrame {
12  private IntBuffer mGlTexture;
13 
14  final private Float3 X = new Float3(1,0,0);
15  final private Float3 Y = new Float3(0,1,0);
16  final private Float3 Z = new Float3(0,0,1);
17 
18  public int getTexture() { return mGlTexture.array()[0]; }
19 
20  class Color{
21  public Color(float r, float g, float b){
22  red = r;
23  green = g;
24  blue = b;
25  }
26  public float red, green, blue;
27  }
28 
29  private Rect adjustRatio(Rect in){
30  float ratio = 4f/3f;
31  float newHeight = in.height();
32  float newWidth = in.height() * ratio;
33  if(newWidth > in.width()){
34  ratio = in.width() / newWidth;
35  newWidth *= ratio;
36  newHeight *= ratio;
37  }
38 
39  float newLeft = in.left + (in.width() - newWidth) / 2f;
40  float newTop = in.top + (in.height() - newHeight) / 2f;
41  float newRight = newLeft + newWidth;
42  float newBottom = newTop + newHeight;
43  return new Rect((int)newLeft, (int)newTop, (int)newRight, (int)newBottom);
44  }
45 
46  private void drawCircle(Float3 x, Float3 y, float axisWidth, Color color)
47  {
48  float radius = 2f;
49  Float3 center = new Float3(0,0,0);
50  final int N = 50;
51  float[] verArray = new float[N * 3];
52  for (int i = 0; i < N; i++)
53  {
54  final double theta = (2* Math.PI / N) * i;
55  final double cost = Math.cos(theta);
56  final double sint = Math.sin(theta);
57  verArray[i*3] = ((float) (center.x + radius * (x.x * cost + y.x * sint)));
58  verArray[i*3 + 1] = ((float) (center.y + radius * (x.y * cost + y.y * sint)));
59  verArray[i*3 + 2] = ((float) (center.z + radius * (x.z * cost + y.z * sint)));
60  }
61 
62  drawLine(verArray, axisWidth, color);
63  }
64 
65  private void drawLine(float[] verArray, float axisWidth, Color color)
66  {
67  ByteBuffer ver = ByteBuffer.allocateDirect(verArray.length * 4);
68  ver.order(ByteOrder.nativeOrder());
69  ver.asFloatBuffer().put(verArray);
70 
71  GLES10.glEnableClientState(GLES10.GL_VERTEX_ARRAY);
72 
73  GLES10.glColor4f(color.red, color.green, color.blue, 1f);
74  GLES10.glLineWidth(axisWidth);
75 
76  GLES10.glVertexPointer(3, GLES10.GL_FLOAT, 0, ver);
77 
78  GLES10.glDrawArrays(GLES10.GL_LINES,0,verArray.length / 3);
79 
80  GLES10.glDisableClientState(GLES10.GL_VERTEX_ARRAY);
81  GLES10.glColor4f(1f, 1f, 1f, 1f);
82  }
83 
84  private void drawTriangle(float[] verArray, Color color)
85  {
86  ByteBuffer ver = ByteBuffer.allocateDirect(verArray.length * 4);
87  ver.order(ByteOrder.nativeOrder());
88  ver.asFloatBuffer().put(verArray);
89 
90  GLES10.glEnableClientState(GLES10.GL_VERTEX_ARRAY);
91 
92  GLES10.glColor4f(color.red, color.green, color.blue, 1f);
93 
94  GLES10.glVertexPointer(3, GLES10.GL_FLOAT, 0, ver);
95 
96  GLES10.glDrawArrays(GLES10.GL_TRIANGLES,0,verArray.length / 3);
97 
98  GLES10.glDisableClientState(GLES10.GL_VERTEX_ARRAY);
99  GLES10.glColor4f(1f, 1f, 1f, 1f);
100  }
101 
102  private void drawAxes(Rect rect, float axisSize, float axisWidth)
103  {
104  float baseSize = 0.05f * axisSize;
105  float basePos = 0.9f * axisSize;
106 
107  float[] verAxisX = { 0, 0, 0, axisSize, 0, 0 };
108  float[] verTriangleX = {
109  axisSize, 0, 0,
110  basePos, baseSize, 0,
111  basePos, -baseSize, 0
112  };
113  float[] verAxisY = { 0, 0, 0, 0, axisSize, 0 };
114  float[] verTriangleY = {
115  0, axisSize, 0,
116  baseSize, basePos, 0,
117  -baseSize, basePos, 0
118  };
119  float[] verAxisZ = { 0, 0, 0, 0, 0, axisSize };
120  float[] verTriangleZ = {
121  0, 0, axisSize,
122  0, baseSize, basePos,
123  0, -baseSize, basePos
124  };
125  Color red = new Color(0.5f, 0, 0);
126  Color green = new Color(0, 0.5f, 0);
127  Color blue = new Color(0, 0, 0.5f);
128 
129  drawLine(verAxisX, axisWidth, red);
130  drawTriangle(verTriangleX, red);
131  drawLine(verAxisY, axisWidth, green);
132  drawTriangle(verTriangleY, green);
133  drawLine(verAxisZ, axisWidth, blue);
134  drawTriangle(verTriangleZ, blue);
135  }
136 
137  private Float3 calcMotionData(Float3 md, float norm){
138  float size = (float) Math.sqrt(md.x * md.x + md.y * md.y + md.z * md.z);
139  return new Float3(
140  (md.x / size) * norm,
141  (md.y / size) * norm,
142  (md.z / size) * norm);
143  }
144 
145  @Override
146  public synchronized void draw(Rect rect)
147  {
148  if (mFrame == null || !(mFrame.is(Extension.MOTION_FRAME)))
149  return;
150 
151  Rect r = adjustRatio(rect);
152  setViewport(r);
153 
154  GLES10.glMatrixMode(GLES10.GL_PROJECTION);
155  GLES10.glPushMatrix();
156  GLES10.glLoadIdentity();
157 
158  GLES10.glOrthof(-2.8f, 2.8f, -2.4f, 2.4f, -7f, 7f);
159 
160  GLES10.glRotatef(25, 1.0f, 0.0f, 0.0f);
161 
162  GLES10.glTranslatef(0, -0.33f, -1.f);
163 
164  GLES10.glRotatef(-135, 0.0f, 1.0f, 0.0f);
165  GLES10.glRotatef(180, 0.0f, 0.0f, 1.0f);
166  GLES10.glRotatef(-90, 0.0f, 1.0f, 0.0f);
167 
168  float axisSize = 2;
169  float axisWidth = 5;
170  Color white = new Color(1, 1, 1);
171 
172  drawCircle(X, Y, 1, white);
173  drawCircle(Y, Z, 1, white);
174  drawCircle(X, Z, 1, white);
175  drawAxes(r, axisSize, axisWidth);
176 
178 
179  float norm = axisSize / 2.f;
180  Float3 md = calcMotionData(mf.getMotionData(), norm);
181 
182  float[] verArray = { 0, 0, 0, md.x, md.y, md.z};
183  Color dir = new Color(Math.abs(md.x/norm), Math.abs(md.y/norm), Math.abs(md.z/norm));
184 
185  drawLine(verArray, axisWidth, dir);
186 
187  GLES10.glMatrixMode(GLES10.GL_PROJECTION);
188  GLES10.glPopMatrix();
189  }
190 
191  @Override
192  public synchronized String getLabel() {
194  Float3 d = mf.getMotionData();
195  try(StreamProfile sp = mFrame.getProfile()){
196  return sp.getType().name() +
197  " [ X: " + String.format("%+.2f", d.x) +
198  ", Y: " + String.format("%+.2f", d.y) +
199  ", Z: " + String.format("%+.2f", d.z) + " ]";
200  }
201  }
202 
203  @Override
204  public synchronized void close() {
205  if(mFrame != null)
206  mFrame.close();
207  if(mGlTexture != null)
208  GLES10.glDeleteTextures(1, mGlTexture);
209  mGlTexture = null;
210  }
211 }
static const ImVec4 white
Definition: model-views.h:45
GLboolean GLboolean GLboolean b
GLboolean GLboolean g
GLint y
void drawAxes(Rect rect, float axisSize, float axisWidth)
GLfloat green
void drawTriangle(float[] verArray, Color color)
GLuint color
d
Definition: rmse.py:171
::std_msgs::String_< std::allocator< void > > String
Definition: String.h:47
Float3 calcMotionData(Float3 md, float norm)
GLdouble f
GLsizeiptr size
GLdouble GLdouble r
GLdouble x
void drawCircle(Float3 x, Float3 y, float axisWidth, Color color)
void drawLine(float[] verArray, float axisWidth, Color color)
GLfloat GLfloat blue
Definition: example.hpp:70
GLuint in
Definition: glext.h:8859
static const ImVec4 red
Definition: model-views.h:64
boolean is(Extension extension)
Definition: Frame.java:9
int i


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:16