Project

General

Profile

Download (34.6 KB) Statistics
| Branch: | Revision:

examples / src / main / java / org / distorted / examples / dynamic / DynamicSurfaceView.java @ f20265a7

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 bc0a685b Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 bc0a685b Leszek Koltunski
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 bc0a685b Leszek Koltunski
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20 f988589e Leszek Koltunski
package org.distorted.examples.dynamic;
21 427ab7bf Leszek Koltunski
22 e4330c89 Leszek Koltunski
import android.app.ActivityManager;
23 427ab7bf Leszek Koltunski
import android.content.Context;
24 e4330c89 Leszek Koltunski
import android.content.pm.ConfigurationInfo;
25 2666a48c Leszek Koltunski
import android.graphics.Rect;
26 427ab7bf Leszek Koltunski
import android.opengl.GLSurfaceView;
27
import android.view.MotionEvent;
28
import android.util.AttributeSet;
29
import android.graphics.Canvas;
30
import android.graphics.Paint.Style;
31
import android.graphics.Paint;
32
33 27e12007 Leszek Koltunski
import org.distorted.library.type.Dynamic;
34 7589635e Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
35
import org.distorted.library.type.Dynamic2D;
36
import org.distorted.library.type.Dynamic3D;
37 d586fda6 Leszek Koltunski
import org.distorted.library.type.Dynamic4D;
38 f20265a7 Leszek Koltunski
import org.distorted.library.type.DynamicQuat;
39 7589635e Leszek Koltunski
import org.distorted.library.type.Static1D;
40
import org.distorted.library.type.Static2D;
41
import org.distorted.library.type.Static3D;
42 d586fda6 Leszek Koltunski
import org.distorted.library.type.Static4D;
43 427ab7bf Leszek Koltunski
44 e52efe17 Leszek Koltunski
import java.lang.ref.WeakReference;
45
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47 427ab7bf Leszek Koltunski
48 f988589e Leszek Koltunski
public class DynamicSurfaceView extends GLSurfaceView
49 427ab7bf Leszek Koltunski
    {
50
    public static final int DIM_1D   = 0; 
51
    public static final int DIM_2D   = 1; 
52
    public static final int DIM_3DXY = 2; 
53
    public static final int DIM_3DXZ = 3; 
54 d586fda6 Leszek Koltunski
    public static final int DIM_4DXY = 4;
55
    public static final int DIM_4DZW = 5;
56 f20265a7 Leszek Koltunski
    public static final int DIM_Q_XY = 6;
57
    public static final int DIM_Q_ZW = 7;
58 d586fda6 Leszek Koltunski
59
    private static final int MAX_DIM = 4;
60 f20265a7 Leszek Koltunski
    private static final float QUAT_QUOT = 0.9f;
61 8b7c0ab3 Leszek Koltunski
62 e52efe17 Leszek Koltunski
    static final int NUM_POINTS = 250;
63 8b7c0ab3 Leszek Koltunski
    private static final Object lock = new Object();
64
65 e52efe17 Leszek Koltunski
    private WeakReference<DynamicActivity> mAct;
66
67 2666a48c Leszek Koltunski
    private static int halfScreenHeight=0;
68
    private static int halfScreenWidth =0;
69
70 f20265a7 Leszek Koltunski
    private Dynamic1D   di1D;
71
    private Dynamic2D   di2D;
72
    private Dynamic3D   di3D;
73
    private Dynamic4D   di4D;
74
    private DynamicQuat diQu;
75 427ab7bf Leszek Koltunski
    
76 fe3c72ce Leszek Koltunski
    private Paint mPaint;
77 2666a48c Leszek Koltunski
    private int mMoving;
78 fe3c72ce Leszek Koltunski
    private int mDuration;
79
    private int mPosition;
80 2666a48c Leszek Koltunski
    private long mDiffTime, mLastTime, mStartTime;
81 d586fda6 Leszek Koltunski
    private float[] mNoise = new float[MAX_DIM];
82 2666a48c Leszek Koltunski
    private float mCount;
83 508f22b5 Leszek Koltunski
84 fe3c72ce Leszek Koltunski
    private int mSize1, mSize2, mSizeT, mAvg;
85 2666a48c Leszek Koltunski
    private float mFontHeight;
86 508f22b5 Leszek Koltunski
87 fe3c72ce Leszek Koltunski
    private int currentDim= DIM_2D;
88 427ab7bf Leszek Koltunski
    
89 fe3c72ce Leszek Koltunski
    private Static1D p1D;
90
    private Static2D p2D;
91
    private Static3D p3D;
92 d586fda6 Leszek Koltunski
    private Static4D p4D;
93 f20265a7 Leszek Koltunski
    private Static4D pQD;
94 9ff0c8c3 Leszek Koltunski
95 fe3c72ce Leszek Koltunski
    private Static1D p1N;
96
    private Static2D p2N;
97
    private Static3D p3N;
98 d586fda6 Leszek Koltunski
    private Static4D p4N;
99 f20265a7 Leszek Koltunski
    private Static4D pQN;
100 9ff0c8c3 Leszek Koltunski
101 d586fda6 Leszek Koltunski
    private float[] mPoints = new float[MAX_DIM*NUM_POINTS];
102 2666a48c Leszek Koltunski
    private boolean mRunning;
103
104 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
105 427ab7bf Leszek Koltunski
    
106 41a81a14 Leszek Koltunski
    public DynamicSurfaceView(Context context, AttributeSet attrs)
107 427ab7bf Leszek Koltunski
      {
108 41a81a14 Leszek Koltunski
      super(context, attrs);
109 e52efe17 Leszek Koltunski
110
      DynamicActivity act = (DynamicActivity)context;
111
      mAct = new WeakReference<>(act);
112
113 427ab7bf Leszek Koltunski
      mPaint = new Paint();
114
      mPaint.setStyle(Style.FILL);
115
      mPaint.setAntiAlias(true);
116 97dadfe5 Leszek Koltunski
117 427ab7bf Leszek Koltunski
      mDuration = 10000;
118 2666a48c Leszek Koltunski
      mCount    = 0.0f;
119 427ab7bf Leszek Koltunski
      mPosition = 0;
120 b041d424 Leszek Koltunski
      mDiffTime = -1;
121
      mLastTime = -1;
122 2666a48c Leszek Koltunski
      mStartTime= -1;
123 d586fda6 Leszek Koltunski
      mMoving   = -1;
124
      mRunning  = false;
125 2666a48c Leszek Koltunski
126 d586fda6 Leszek Koltunski
      for(int i=0; i<MAX_DIM; i++) mNoise[i] = 0.0f;
127 8b7c0ab3 Leszek Koltunski
128 e52efe17 Leszek Koltunski
      clearPoints();
129
130 2666a48c Leszek Koltunski
      di1D = new Dynamic1D(mDuration,mCount);
131 d586fda6 Leszek Koltunski
      p1N  = new Static1D(mNoise[0]);
132 2666a48c Leszek Koltunski
      di2D = new Dynamic2D(mDuration,mCount);
133 d586fda6 Leszek Koltunski
      p2N  = new Static2D(mNoise[0],mNoise[1]);
134 2666a48c Leszek Koltunski
      di3D = new Dynamic3D(mDuration,mCount);
135 d586fda6 Leszek Koltunski
      p3N  = new Static3D(mNoise[0],mNoise[1],mNoise[2]);
136
      di4D = new Dynamic4D(mDuration,mCount);
137
      p4N  = new Static4D(mNoise[0],mNoise[1],mNoise[2],mNoise[3]);
138 f20265a7 Leszek Koltunski
      diQu = new DynamicQuat(mDuration,mCount);
139
      pQN  = new Static4D(mNoise[0],mNoise[1],mNoise[2],mNoise[3]);
140 bf36cb6e Leszek Koltunski
141 8db5b725 Leszek Koltunski
      di1D.setAccessType(Dynamic.ACCESS_TYPE_SEQUENTIAL);
142
      di2D.setAccessType(Dynamic.ACCESS_TYPE_SEQUENTIAL);
143
      di3D.setAccessType(Dynamic.ACCESS_TYPE_SEQUENTIAL);
144 d586fda6 Leszek Koltunski
      di4D.setAccessType(Dynamic.ACCESS_TYPE_SEQUENTIAL);
145 f20265a7 Leszek Koltunski
      diQu.setAccessType(Dynamic.ACCESS_TYPE_SEQUENTIAL);
146 27e12007 Leszek Koltunski
147 427ab7bf Leszek Koltunski
      if(!isInEditMode())
148
        {
149
        setFocusable(true);
150
        setFocusableInTouchMode(true);
151 8b7c0ab3 Leszek Koltunski
        DynamicRenderer mRenderer = new DynamicRenderer(this);
152 e4330c89 Leszek Koltunski
        final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
153
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
154
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
155 427ab7bf Leszek Koltunski
        setRenderer(mRenderer);
156
        }
157
      }
158
159 508f22b5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
160
161 fe3c72ce Leszek Koltunski
    public void onSurfaceChanged(int width,int height)
162 508f22b5 Leszek Koltunski
      {
163
      mAvg = (width+height)/2;
164
165
      mSize1 = mAvg/150;
166 2666a48c Leszek Koltunski
      mSize2 = mAvg/50;
167 508f22b5 Leszek Koltunski
      mSizeT = mAvg/30;
168
169
      mPaint.setTextSize(mSizeT);
170 2666a48c Leszek Koltunski
      mPaint.setTextAlign(Paint.Align.CENTER);
171
172
      final Rect textBounds = new Rect();
173
      String text = "1";
174
      mPaint.getTextBounds(text, 0, text.length(), textBounds);
175
      mFontHeight = textBounds.exactCenterY();
176 508f22b5 Leszek Koltunski
177
      clearPoints();
178
      }
179
180 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
181 2666a48c Leszek Koltunski
182
    public static void setHalfWidth(int hw)
183
      {
184
      halfScreenWidth = hw;
185
      }
186
187 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
188 2666a48c Leszek Koltunski
189
    public static void setHalfHeight(int hh)
190
      {
191
      halfScreenHeight = hh;
192
      }
193
194 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
195 427ab7bf Leszek Koltunski
196 fe3c72ce Leszek Koltunski
    public void setMode(int mode)
197 427ab7bf Leszek Koltunski
      {
198
      di1D.setMode(mode);  
199
      di2D.setMode(mode);
200
      di3D.setMode(mode);
201 d586fda6 Leszek Koltunski
      di4D.setMode(mode);
202 f20265a7 Leszek Koltunski
      diQu.setMode(mode);
203 427ab7bf Leszek Koltunski
      }
204
205 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
206 427ab7bf Leszek Koltunski
207 fe3c72ce Leszek Koltunski
    public void setDuration(int duration)
208 427ab7bf Leszek Koltunski
      {
209
      mDuration = duration;
210
      
211 2666a48c Leszek Koltunski
      di1D.setDuration(duration);
212
      di2D.setDuration(duration);
213
      di3D.setDuration(duration);
214 d586fda6 Leszek Koltunski
      di4D.setDuration(duration);
215 f20265a7 Leszek Koltunski
      diQu.setDuration(duration);
216 2666a48c Leszek Koltunski
      }
217
218 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
219 2666a48c Leszek Koltunski
220
    public void setCount(float count)
221
      {
222
      mCount = count;
223
224
      di1D.setCount(count);
225
      di2D.setCount(count);
226
      di3D.setCount(count);
227 d586fda6 Leszek Koltunski
      di4D.setCount(count);
228 f20265a7 Leszek Koltunski
      diQu.setCount(count);
229 427ab7bf Leszek Koltunski
      }
230
231 dea555b9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
232
233
    public void setConvexity(float convexity)
234
      {
235
      di1D.setConvexity(convexity);
236
      di2D.setConvexity(convexity);
237
      di3D.setConvexity(convexity);
238 d586fda6 Leszek Koltunski
      di4D.setConvexity(convexity);
239 f20265a7 Leszek Koltunski
      diQu.setConvexity(convexity);
240 dea555b9 Leszek Koltunski
      }
241
242 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
243 f20265a7 Leszek Koltunski
// DynamicQuat does not support noise
244 427ab7bf Leszek Koltunski
245 d586fda6 Leszek Koltunski
    public void setNoise(float noise0, float noise1, float noise2, float noise3)
246 427ab7bf Leszek Koltunski
      {
247 d586fda6 Leszek Koltunski
      mNoise[0] = noise0;
248
      mNoise[1] = noise1;
249
      mNoise[2] = noise2;
250
      mNoise[3] = noise3;
251 9ff0c8c3 Leszek Koltunski
252 d586fda6 Leszek Koltunski
      p1N.set(mNoise[0]);
253
      p2N.set(mNoise[0],mNoise[1]);
254
      p3N.set(mNoise[0],mNoise[1],mNoise[2]);
255
      p4N.set(mNoise[0],mNoise[1],mNoise[2], mNoise[3]);
256 9ff0c8c3 Leszek Koltunski
257
      di1D.setNoise(p1N);
258
      di2D.setNoise(p2N);
259
      di3D.setNoise(p3N);
260 d586fda6 Leszek Koltunski
      di4D.setNoise(p4N);
261 427ab7bf Leszek Koltunski
      }
262
    
263 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
264 427ab7bf Leszek Koltunski
265 fe3c72ce Leszek Koltunski
    public void setDimension(int dim)
266 427ab7bf Leszek Koltunski
      {
267
      if( currentDim != dim )
268
        {
269 d586fda6 Leszek Koltunski
        if( !(currentDim==DIM_3DXY && dim==DIM_3DXZ) && !(currentDim==DIM_3DXZ && dim==DIM_3DXY) &&
270 f20265a7 Leszek Koltunski
            !(currentDim==DIM_4DXY && dim==DIM_4DZW) && !(currentDim==DIM_4DZW && dim==DIM_4DXY) &&
271
            !(currentDim==DIM_Q_XY && dim==DIM_Q_ZW) && !(currentDim==DIM_Q_ZW && dim==DIM_Q_XY)  )
272 427ab7bf Leszek Koltunski
          {
273 2666a48c Leszek Koltunski
          resetPoints();
274 427ab7bf Leszek Koltunski
          }
275 2666a48c Leszek Koltunski
276 427ab7bf Leszek Koltunski
        currentDim = dim;
277
        }
278
      }
279
    
280 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
281 427ab7bf Leszek Koltunski
    
282 fe3c72ce Leszek Koltunski
    public void drawCurve(Canvas c, long time)
283 427ab7bf Leszek Koltunski
      {
284 b041d424 Leszek Koltunski
      if( mLastTime<0 )
285
        {
286
        mLastTime = time;
287
        }
288
      else
289
        {
290
        mDiffTime = time - mLastTime;
291
        }
292
293 427ab7bf Leszek Koltunski
      synchronized(lock)
294
        {
295
        switch(currentDim)
296
          {
297 d586fda6 Leszek Koltunski
          case DIM_1D  : drawHorizontalAxis(c,"x");
298
                         drawPath(c,di1D,0,1,time);
299
                         drawRedPoints1D(c);
300
                         break;
301
          case DIM_2D  : drawHorizontalAxis(c,"x");
302
                         drawVerticalAxis  (c,"y");
303
                         drawPath(c,di2D,0,1,time);
304
                         drawRedPoints2D(c);
305
                         break;
306
          case DIM_3DXY: drawHorizontalAxis(c,"x");
307
                         drawVerticalAxis  (c,"y");
308
                         drawPath(c,di3D,0,1,time);
309
                         drawRedPoints3D(c);
310
                         break;
311
          case DIM_3DXZ: drawHorizontalAxis(c,"x");
312
                         drawVerticalAxis  (c,"z");
313
                         drawPath(c,di3D,0,2,time);
314
                         drawRedPoints3D(c);
315
                         break;
316
          case DIM_4DXY: drawHorizontalAxis(c,"x");
317
                         drawVerticalAxis  (c,"y");
318
                         drawPath(c,di4D,0,1,time);
319
                         drawRedPoints4D(c);
320
                         break;
321
          case DIM_4DZW: drawHorizontalAxis(c,"z");
322
                         drawVerticalAxis  (c,"w");
323
                         drawPath(c,di4D,2,3,time);
324
                         drawRedPoints4D(c);
325
                         break;
326 f20265a7 Leszek Koltunski
          case DIM_Q_XY: drawHorizontalAxis(c,"x");
327
                         drawVerticalAxis  (c,"y");
328
                         drawPath(c,diQu,0,1,time);
329
                         drawRedPointsQu(c);
330
                         break;
331
          case DIM_Q_ZW: drawHorizontalAxis(c,"z");
332
                         drawVerticalAxis  (c,"w");
333
                         drawPath(c,diQu,2,3,time);
334
                         drawRedPointsQu(c);
335
                         break;
336 427ab7bf Leszek Koltunski
          }
337
        }
338 b041d424 Leszek Koltunski
339
      mLastTime = time;
340 427ab7bf Leszek Koltunski
      }
341
342 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
343 508f22b5 Leszek Koltunski
344 fe3c72ce Leszek Koltunski
    private void clearPoints()
345 508f22b5 Leszek Koltunski
      {
346 d586fda6 Leszek Koltunski
      for(int i=0; i<MAX_DIM*NUM_POINTS; i++)
347 508f22b5 Leszek Koltunski
         {
348 e52efe17 Leszek Koltunski
         mPoints[i] = -100000.0f;
349 508f22b5 Leszek Koltunski
         }
350
      }
351
352 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
353 508f22b5 Leszek Koltunski
354 2666a48c Leszek Koltunski
    public void resetPoints()
355 427ab7bf Leszek Koltunski
      {
356 2666a48c Leszek Koltunski
      synchronized(lock)
357 427ab7bf Leszek Koltunski
        {
358 2666a48c Leszek Koltunski
        clearPoints();
359 97dadfe5 Leszek Koltunski
360 2666a48c Leszek Koltunski
        switch(currentDim)
361 427ab7bf Leszek Koltunski
          {
362 2666a48c Leszek Koltunski
          case DIM_1D  : di1D.removeAll(); break;
363
          case DIM_2D  : di2D.removeAll(); break;
364
          case DIM_3DXY:
365
          case DIM_3DXZ: di3D.removeAll(); break;
366 d586fda6 Leszek Koltunski
          case DIM_4DXY:
367
          case DIM_4DZW: di4D.removeAll(); break;
368 f20265a7 Leszek Koltunski
          case DIM_Q_XY:
369
          case DIM_Q_ZW: diQu.removeAll(); break;
370 427ab7bf Leszek Koltunski
          }
371 e52efe17 Leszek Koltunski
372 d84f5b73 Leszek Koltunski
        DynamicActivity act = mAct.get();
373
        act.setNumRedPoints(0);
374
        act.clearPoints();
375 427ab7bf Leszek Koltunski
        }
376
      }
377 2666a48c Leszek Koltunski
378 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
379 2666a48c Leszek Koltunski
380
    public void startDynamic()
381
      {
382
      mRunning = true;
383
      mLastTime= -1;
384
      mStartTime = System.currentTimeMillis();
385
386
      clearPoints();
387
      di1D.resetToBeginning();
388
      di2D.resetToBeginning();
389
      di3D.resetToBeginning();
390 d586fda6 Leszek Koltunski
      di4D.resetToBeginning();
391 f20265a7 Leszek Koltunski
      diQu.resetToBeginning();
392 2666a48c Leszek Koltunski
      }
393
394 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
395 2666a48c Leszek Koltunski
396
    public void stopDynamic()
397
      {
398
      mRunning = false;
399
      }
400
401 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
402 2666a48c Leszek Koltunski
403
    private void drawHorizontalAxis(Canvas c, String label)
404 427ab7bf Leszek Koltunski
      {
405
      mPaint.setColor(0xff000000);
406 2666a48c Leszek Koltunski
407
      c.drawLine(0, halfScreenHeight, halfScreenWidth*2, halfScreenHeight, mPaint);
408
      c.drawText( label, 0.95f*halfScreenWidth*2, halfScreenHeight + mSizeT , mPaint);
409
      }
410
411
412 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
413 2666a48c Leszek Koltunski
414
    private void drawVerticalAxis(Canvas c, String label)
415
      {
416
      mPaint.setColor(0xff000000);
417
418
      c.drawLine(halfScreenWidth, 0, halfScreenWidth, halfScreenHeight*2, mPaint);
419
      c.drawText(label, halfScreenWidth + mSizeT,                mSizeT , mPaint);
420
      }
421
422 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
423 2666a48c Leszek Koltunski
424 d586fda6 Leszek Koltunski
    private void drawPath(Canvas c, Dynamic dyn, int indexH, int indexW, long time)
425 2666a48c Leszek Koltunski
      {
426
      int len = dyn.getNumPoints();
427
428 427ab7bf Leszek Koltunski
      if( len>=2 )
429
        {
430 2666a48c Leszek Koltunski
        if( mRunning )
431
          {
432
          if ( ++mPosition >= NUM_POINTS ) mPosition=0;
433
434
          if( dyn.getDimension()==1 )
435
            {
436 d586fda6 Leszek Koltunski
            mPoints[MAX_DIM*mPosition+indexW] = halfScreenHeight;
437 2666a48c Leszek Koltunski
            }
438
439 d586fda6 Leszek Koltunski
          if( dyn.get(mPoints,MAX_DIM*mPosition, time-mStartTime, mDiffTime) )
440 2666a48c Leszek Koltunski
            {
441
            stopDynamic();
442
            }
443 e52efe17 Leszek Koltunski
444
          addNewSpeedPoint(time);
445 2666a48c Leszek Koltunski
          }
446 97dadfe5 Leszek Koltunski
447 f20265a7 Leszek Koltunski
        if( currentDim!=DIM_Q_XY && currentDim!=DIM_Q_ZW )
448
          {
449
          for(int i=0; i<NUM_POINTS; i++)
450
            {
451
            int color = i<=mPosition ? 0xff - (mPosition           -i)*0xff/(NUM_POINTS-1)
452
                                     : 0xff - (mPosition+NUM_POINTS-i)*0xff/(NUM_POINTS-1);
453
454
            mPaint.setColor( 0xffffff + ((color&0xff)<<24) );
455
            c.drawCircle(mPoints[MAX_DIM*i+indexH], mPoints[MAX_DIM*i+indexW] , mSize1, mPaint );
456
            }
457
          }
458
        else
459 427ab7bf Leszek Koltunski
          {
460 f20265a7 Leszek Koltunski
          float x,y,min = QUAT_QUOT* (halfScreenWidth<halfScreenHeight ? halfScreenWidth:halfScreenHeight );
461 2666a48c Leszek Koltunski
462 f20265a7 Leszek Koltunski
          for(int i=0; i<NUM_POINTS; i++)
463
            {
464
            int color = i<=mPosition ? 0xff - (mPosition           -i)*0xff/(NUM_POINTS-1)
465
                                     : 0xff - (mPosition+NUM_POINTS-i)*0xff/(NUM_POINTS-1);
466
467
            mPaint.setColor( 0xffffff + ((color&0xff)<<24) );
468
            x = mPoints[MAX_DIM*i+indexH]*min + halfScreenWidth;
469
            y = mPoints[MAX_DIM*i+indexW]*min + halfScreenHeight;
470
            c.drawCircle( x, y, mSize1, mPaint );
471
            }
472 427ab7bf Leszek Koltunski
          }
473
        }
474 2666a48c Leszek Koltunski
      }
475
476 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
477 2666a48c Leszek Koltunski
478
    private void drawRedPoints1D(Canvas c)
479
      {
480
      int len = di1D.getNumPoints();
481
482 427ab7bf Leszek Koltunski
      for(int curr=0; curr<len; curr++)
483 2666a48c Leszek Koltunski
        {
484
        p1D = di1D.getPoint(curr);
485
        drawRedPoint(c,curr+"", p1D.get1(), halfScreenHeight);
486 427ab7bf Leszek Koltunski
        }
487
      }
488
489 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
490 2666a48c Leszek Koltunski
491
    private void drawRedPoints2D(Canvas c)
492 427ab7bf Leszek Koltunski
      {
493 2666a48c Leszek Koltunski
      int len = di2D.getNumPoints();
494 97dadfe5 Leszek Koltunski
495 2666a48c Leszek Koltunski
      for(int curr=0; curr<len; curr++)
496
        {
497
        p2D = di2D.getPoint(curr);
498
        drawRedPoint(c,curr+"", p2D.get1(), p2D.get2());
499 427ab7bf Leszek Koltunski
        }
500 2666a48c Leszek Koltunski
      }
501
502 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
503 2666a48c Leszek Koltunski
504
    private void drawRedPoints3D(Canvas c)
505
      {
506
      int len = di3D.getNumPoints();
507
508 427ab7bf Leszek Koltunski
      for(int curr=0; curr<len; curr++)
509 2666a48c Leszek Koltunski
        {
510 427ab7bf Leszek Koltunski
        p3D = di3D.getPoint(curr);
511 2666a48c Leszek Koltunski
        drawRedPoint(c,curr+"", p3D.get1(), currentDim==DIM_3DXY ? p3D.get2():p3D.get3());
512
        }
513 427ab7bf Leszek Koltunski
      }
514 2666a48c Leszek Koltunski
515 d586fda6 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
516
517
    private void drawRedPoints4D(Canvas c)
518
      {
519
      int len = di4D.getNumPoints();
520
521
      for(int curr=0; curr<len; curr++)
522
        {
523
        p4D = di4D.getPoint(curr);
524
525
        if( currentDim==DIM_4DXY ) drawRedPoint(c,curr+"", p4D.get1(), p4D.get2());
526
        else                       drawRedPoint(c,curr+"", p4D.get3(), p4D.get4());
527
        }
528
      }
529
530 f20265a7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
531
532
    private void drawRedPointsQu(Canvas c)
533
      {
534
      int len = diQu.getNumPoints();
535
      float x,y, min = QUAT_QUOT* (halfScreenWidth<halfScreenHeight ? halfScreenWidth:halfScreenHeight );
536
537
      for(int curr=0; curr<len; curr++)
538
        {
539
        pQD = diQu.getPoint(curr);
540
541
        if( currentDim==DIM_Q_XY )
542
          {
543
          x = pQD.get1()*min + halfScreenWidth;
544
          y = pQD.get2()*min + halfScreenHeight;
545
          }
546
        else
547
          {
548
          x = pQD.get3()*min + halfScreenWidth;
549
          y = pQD.get4()*min + halfScreenHeight;
550
          }
551
552
        drawRedPoint(c,curr+"", x,y);
553
        }
554
      }
555
556 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
557 2666a48c Leszek Koltunski
558
    private void drawRedPoint(Canvas c, String label, float width, float height)
559
      {
560
      mPaint.setColor(0xffff0000);
561
      c.drawCircle( width, height, mSize2, mPaint);
562
      mPaint.setColor(0xffffffff);
563
      c.drawText(label, width,height-mFontHeight, mPaint);
564
      }
565
566 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
567 427ab7bf Leszek Koltunski
568 f20265a7 Leszek Koltunski
    private void addNewPoint(float x, float y)
569 427ab7bf Leszek Koltunski
      {
570 d586fda6 Leszek Koltunski
      float gx,gy,gz,gw;
571 427ab7bf Leszek Koltunski
      int len;
572 d586fda6 Leszek Koltunski
      int minDist = (mAvg*mAvg)/100;
573 dea555b9 Leszek Koltunski
574 427ab7bf Leszek Koltunski
      switch(currentDim)
575
        {
576 d586fda6 Leszek Koltunski
        case DIM_1D : len = di1D.getNumPoints();
577 dea555b9 Leszek Koltunski
578 d586fda6 Leszek Koltunski
                      for(int g=0; g<len; g++)
579
                        {
580
                        p1D = di1D.getPoint(g);
581
                        gx = p1D.get1();
582 427ab7bf Leszek Koltunski
                                    
583 d586fda6 Leszek Koltunski
                        if( (x-gx)*(x-gx) < minDist )
584
                          {
585
                          mMoving = g;
586
                          break;
587
                          }
588
                        }
589
                      if( mMoving <0 )
590
                        {
591
                        synchronized(lock)
592
                          {
593
                          di1D.add(new Static1D(x));
594
                          mAct.get().setNumRedPoints(len+1);
595
                          }
596
                        }
597
                      break;
598
        case DIM_2D : len = di2D.getNumPoints();
599 427ab7bf Leszek Koltunski
                                 
600 d586fda6 Leszek Koltunski
                      for(int g=0; g<len; g++)
601
                        {
602
                        p2D = di2D.getPoint(g);
603
                        gx = p2D.get1();
604
                        gy = p2D.get2();
605 427ab7bf Leszek Koltunski
                                    
606 d586fda6 Leszek Koltunski
                        if( (x-gx)*(x-gx) + (y-gy)*(y-gy) < minDist )
607
                          {
608
                          mMoving = g;
609
                          break;
610
                          }
611
                        }
612
                      if( mMoving <0 )
613
                        {
614
                        synchronized(lock)
615
                          {
616
                          di2D.add(new Static2D(x,y));
617
                          mAct.get().setNumRedPoints(len+1);
618
                          }
619
                        }
620
                      break;
621
        case DIM_3DXY:
622
        case DIM_3DXZ:len = di3D.getNumPoints();
623 427ab7bf Leszek Koltunski
                                 
624 d586fda6 Leszek Koltunski
                      for(int g=0; g<len; g++)
625
                        {
626
                        p3D = di3D.getPoint(g);
627
                        gx = p3D.get1();
628
                        gy = p3D.get2();
629
                        gz = p3D.get3();
630 427ab7bf Leszek Koltunski
                               
631 d586fda6 Leszek Koltunski
                        if( currentDim==DIM_3DXY )
632
                          {
633
                          if( (x-gx)*(x-gx) + (y-gy)*(y-gy) < minDist )
634
                            {
635
                            mMoving = g;
636
                            break;
637
                            }
638
                          }
639
                        if( currentDim==DIM_3DXZ )
640
                          {
641
                          if( (x-gx)*(x-gx) + (y-gz)*(y-gz) < minDist )
642
                            {
643
                            mMoving = g;
644
                            break;
645
                            }
646
                          }
647
                        }
648
649
                      if( mMoving <0 )
650
                        {
651
                        synchronized(lock)
652
                          {
653
                          if( currentDim==DIM_3DXY ) di3D.add(new Static3D(x,y, halfScreenHeight));
654
                          if( currentDim==DIM_3DXZ ) di3D.add(new Static3D(x, halfScreenHeight,y));
655
                          mAct.get().setNumRedPoints(len+1);
656
                          }
657
                        }
658
                      break;
659
        case DIM_4DXY:
660
        case DIM_4DZW:len = di4D.getNumPoints();
661
662
                      for(int g=0; g<len; g++)
663
                        {
664
                        p4D = di4D.getPoint(g);
665
                        gx = p4D.get1();
666
                        gy = p4D.get2();
667
                        gz = p4D.get3();
668
                        gw = p4D.get4();
669
670
                        if( currentDim==DIM_4DXY )
671
                          {
672
                          if( (x-gx)*(x-gx) + (y-gy)*(y-gy) < minDist )
673
                            {
674
                            mMoving = g;
675
                            break;
676
                            }
677
                          }
678
                        if( currentDim==DIM_4DZW )
679
                          {
680
                          if( (x-gz)*(x-gz) + (y-gw)*(y-gw) < minDist )
681
                            {
682
                            mMoving = g;
683
                            break;
684
                            }
685
                          }
686
                        }
687
688
                      if( mMoving <0 )
689
                        {
690
                        synchronized(lock)
691
                          {
692
                          if( currentDim==DIM_4DXY ) di4D.add(new Static4D(x,y, halfScreenWidth, halfScreenHeight));
693
                          if( currentDim==DIM_4DZW ) di4D.add(new Static4D( halfScreenWidth, halfScreenHeight,x,y));
694
                          mAct.get().setNumRedPoints(len+1);
695
                          }
696
                        }
697
                      break;
698 f20265a7 Leszek Koltunski
        case DIM_Q_XY:
699
        case DIM_Q_ZW:len = diQu.getNumPoints();
700
                      float min = QUAT_QUOT* (halfScreenWidth<halfScreenHeight ? halfScreenWidth:halfScreenHeight );
701
702
                      for(int g=0; g<len; g++)
703
                        {
704
                        pQD = diQu.getPoint(g);
705
706
                        if( currentDim==DIM_Q_XY )
707
                          {
708
                          gx = pQD.get1()*min + halfScreenWidth;
709
                          gy = pQD.get2()*min + halfScreenHeight;
710
711
                          if( (x-gx)*(x-gx) + (y-gy)*(y-gy) < minDist )
712
                            {
713
                            mMoving = g;
714
                            break;
715
                            }
716
                          }
717
                        if( currentDim==DIM_Q_ZW )
718
                          {
719
                          gz = pQD.get3()*min + halfScreenWidth;
720
                          gw = pQD.get4()*min + halfScreenHeight;
721
722
                          if( (x-gz)*(x-gz) + (y-gw)*(y-gw) < minDist )
723
                            {
724
                            mMoving = g;
725
                            break;
726
                            }
727
                          }
728
                        }
729
730
                      if( mMoving <0 )
731
                        {
732
                        float z,w;
733
                        x = (x - halfScreenWidth ) / min;
734
                        y = (y - halfScreenHeight) / min;
735
                        float len1 = x*x + y*y;
736
737
                        if( len1>= 1.0f )
738
                          {
739
                          float A = (float)Math.sqrt(len1);
740
                          x = x/A;
741
                          y = y/A;
742
                          z = 0.0f;
743
                          w = 0.0f;
744
                          }
745
                        else
746
                          {
747
                          z = (float)Math.sqrt(1-len1);
748
                          w = 0.0f;
749
                          }
750
751
                        synchronized(lock)
752
                          {
753
                          if( currentDim==DIM_Q_XY ) diQu.add(new Static4D(x,y,z,w));
754
                          if( currentDim==DIM_Q_ZW ) diQu.add(new Static4D(z,w,x,y));
755
                          mAct.get().setNumRedPoints(len+1);
756
                          }
757
                        }
758
                      break;
759 427ab7bf Leszek Koltunski
        }
760
      }
761 f20265a7 Leszek Koltunski
762 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
763
764
    private void addNewSpeedPoint(long time)
765
      {
766
      int prev = mPosition-1;
767
      if( prev<0 ) prev = NUM_POINTS-1;
768
769 d586fda6 Leszek Koltunski
      float xdiff = mPoints[MAX_DIM*prev  ]-mPoints[MAX_DIM*mPosition  ];
770
      float ydiff = mPoints[MAX_DIM*prev+1]-mPoints[MAX_DIM*mPosition+1];
771
      float zdiff = mPoints[MAX_DIM*prev+2]-mPoints[MAX_DIM*mPosition+2];
772
      float wdiff = mPoints[MAX_DIM*prev+3]-mPoints[MAX_DIM*mPosition+3];
773 e52efe17 Leszek Koltunski
774 d586fda6 Leszek Koltunski
      float dist = (float)Math.sqrt( xdiff*xdiff + ydiff*ydiff + zdiff*zdiff + wdiff*wdiff);
775 e52efe17 Leszek Koltunski
      float speed= mDiffTime<=0 ? 0: dist / mDiffTime;
776
      float timepoint = ((float)(time-mStartTime))/mDuration;
777
778
      if( dist<1000.0f )   // otherwise this is a very first call; do not send it!
779
        {
780
        mAct.get().addPoint( timepoint - (int)timepoint, speed );
781
        }
782
      }
783
784
///////////////////////////////////////////////////////////////////////////////////////////////////
785
786 2666a48c Leszek Koltunski
    @Override
787
    public boolean onTouchEvent(MotionEvent event)
788 427ab7bf Leszek Koltunski
      {
789
      int action = event.getAction();
790 f20265a7 Leszek Koltunski
      float xDown, yDown;
791 97dadfe5 Leszek Koltunski
792 427ab7bf Leszek Koltunski
      switch(action)
793
        {
794 f20265a7 Leszek Koltunski
        case MotionEvent.ACTION_DOWN: xDown = event.getX();
795
                                      yDown = event.getY();
796
797 427ab7bf Leszek Koltunski
                                      addNewPoint(xDown,yDown);
798 f20265a7 Leszek Koltunski
799 427ab7bf Leszek Koltunski
                                      break;
800 2666a48c Leszek Koltunski
        case MotionEvent.ACTION_MOVE: if( mMoving >=0 )
801 427ab7bf Leszek Koltunski
                                        {
802 f20265a7 Leszek Koltunski
                                        xDown = event.getX();
803
                                        yDown = event.getY();
804 427ab7bf Leszek Koltunski
                                        
805
                                        switch(currentDim)
806
                                          {
807 2666a48c Leszek Koltunski
                                          case DIM_1D  : di1D.setPoint(mMoving, xDown);
808 427ab7bf Leszek Koltunski
                                                         break;
809 2666a48c Leszek Koltunski
                                          case DIM_2D  : di2D.setPoint(mMoving, xDown, yDown);
810 427ab7bf Leszek Koltunski
                                                         break;
811 2666a48c Leszek Koltunski
                                          case DIM_3DXY: di3D.setPoint(mMoving, xDown, yDown, (int)di3D.getPoint(mMoving).get3());
812 427ab7bf Leszek Koltunski
                                                         break;
813 2666a48c Leszek Koltunski
                                          case DIM_3DXZ: di3D.setPoint(mMoving, xDown, (int)di3D.getPoint(mMoving).get2(), yDown);
814 427ab7bf Leszek Koltunski
                                                         break;
815 d586fda6 Leszek Koltunski
                                          case DIM_4DXY: di4D.setPoint(mMoving, xDown, yDown, (int)di4D.getPoint(mMoving).get3(), (int)di4D.getPoint(mMoving).get4());
816
                                                         break;
817
                                          case DIM_4DZW: di4D.setPoint(mMoving, (int)di4D.getPoint(mMoving).get1(), (int)di4D.getPoint(mMoving).get2(), xDown, yDown);
818
                                                         break;
819 f20265a7 Leszek Koltunski
                                          case DIM_Q_XY: float min1 = QUAT_QUOT* (halfScreenWidth<halfScreenHeight ? halfScreenWidth:halfScreenHeight );
820
                                                         float x1 = (xDown - halfScreenWidth ) / min1;
821
                                                         float y1 = (yDown - halfScreenHeight) / min1;
822
                                                         float z1 = diQu.getPoint(mMoving).get3();
823
                                                         float w1 = diQu.getPoint(mMoving).get4();
824
                                                         float len1 = x1*x1 + y1*y1;
825
826
                                                         if( len1 <= 1.0f )
827
                                                           {
828
                                                           float len2 = z1*z1 + w1*w1;
829
830
                                                           if( len2 == 0 )
831
                                                             {
832
                                                             if( len1 == 0 )
833
                                                               {
834
                                                               w1 = 1.0f;
835
                                                               }
836
                                                             else
837
                                                               {
838
                                                               float B = (float)Math.sqrt(len1);
839
                                                               x1 = x1/B;
840
                                                               y1 = y1/B;
841
                                                               }
842
                                                             }
843
                                                           else
844
                                                             {
845
                                                             float A = (float)Math.sqrt((1.0f - len1)/len2);
846
                                                             z1 = A*z1;
847
                                                             w1 = A*w1;
848
                                                             }
849
                                                           }
850
                                                         else
851
                                                           {
852
                                                           float B = (float)Math.sqrt(len1);
853
                                                           x1 = x1/B;
854
                                                           y1 = y1/B;
855
                                                           z1 = 0.0f;
856
                                                           w1 = 0.0f;
857
                                                           }
858
                                                         diQu.setPoint(mMoving, x1,y1,z1,w1);
859
                                                         break;
860
                                          case DIM_Q_ZW: float min2 = QUAT_QUOT* (halfScreenWidth<halfScreenHeight ? halfScreenWidth:halfScreenHeight );
861
                                                         float x2 = diQu.getPoint(mMoving).get1();
862
                                                         float y2 = diQu.getPoint(mMoving).get2();
863
                                                         float z2 = (xDown - halfScreenWidth ) / min2;
864
                                                         float w2 = (yDown - halfScreenHeight) / min2;
865
                                                         float len3 = z2*z2 + w2*w2;
866
867
                                                         if( len3 <= 1.0f )
868
                                                           {
869
                                                           float len4 = x2*x2 + y2*y2;
870
871
                                                           if( len4 == 0 )
872
                                                             {
873
                                                             if( len3 == 0 )
874
                                                               {
875
                                                               w2 = 1.0f;
876
                                                               }
877
                                                             else
878
                                                               {
879
                                                               float B = (float)Math.sqrt(len3);
880
                                                               z2 = z2/B;
881
                                                               w2 = w2/B;
882
                                                               }
883
                                                             }
884
                                                           else
885
                                                             {
886
                                                             float A = (float)Math.sqrt((1.0f - len3)/len4);
887
                                                             x2 = A*x2;
888
                                                             y2 = A*y2;
889
                                                             }
890
                                                           }
891
                                                         else
892
                                                           {
893
                                                           float B = (float)Math.sqrt(len3);
894
                                                           x2 = 0.0f;
895
                                                           y2 = 0.0f;
896
                                                           z2 = z2/B;
897
                                                           w2 = w2/B;
898
                                                           }
899
                                                         diQu.setPoint(mMoving, x2,y2,z2,w2);
900
                                                         break;
901 427ab7bf Leszek Koltunski
                                          }
902
                                        }                           
903
                                      break;
904 2666a48c Leszek Koltunski
        case MotionEvent.ACTION_UP  : mMoving = -1;
905 427ab7bf Leszek Koltunski
                                      break;
906
        }
907
            
908
      return true;
909
      }
910
  }