Project

General

Profile

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

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

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 1fc23462 Leszek Koltunski
    public static void surfaceChanged(int w, int h)
183 2666a48c Leszek Koltunski
      {
184 1fc23462 Leszek Koltunski
      halfScreenWidth = w/2;
185
      halfScreenHeight= h/2;
186 2666a48c Leszek Koltunski
      }
187
188 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
189 427ab7bf Leszek Koltunski
190 fe3c72ce Leszek Koltunski
    public void setMode(int mode)
191 427ab7bf Leszek Koltunski
      {
192
      di1D.setMode(mode);  
193
      di2D.setMode(mode);
194
      di3D.setMode(mode);
195 d586fda6 Leszek Koltunski
      di4D.setMode(mode);
196 f20265a7 Leszek Koltunski
      diQu.setMode(mode);
197 427ab7bf Leszek Koltunski
      }
198
199 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
200 427ab7bf Leszek Koltunski
201 fe3c72ce Leszek Koltunski
    public void setDuration(int duration)
202 427ab7bf Leszek Koltunski
      {
203
      mDuration = duration;
204
      
205 2666a48c Leszek Koltunski
      di1D.setDuration(duration);
206
      di2D.setDuration(duration);
207
      di3D.setDuration(duration);
208 d586fda6 Leszek Koltunski
      di4D.setDuration(duration);
209 f20265a7 Leszek Koltunski
      diQu.setDuration(duration);
210 2666a48c Leszek Koltunski
      }
211
212 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
213 2666a48c Leszek Koltunski
214
    public void setCount(float count)
215
      {
216
      mCount = count;
217
218
      di1D.setCount(count);
219
      di2D.setCount(count);
220
      di3D.setCount(count);
221 d586fda6 Leszek Koltunski
      di4D.setCount(count);
222 f20265a7 Leszek Koltunski
      diQu.setCount(count);
223 427ab7bf Leszek Koltunski
      }
224
225 dea555b9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
226
227
    public void setConvexity(float convexity)
228
      {
229
      di1D.setConvexity(convexity);
230
      di2D.setConvexity(convexity);
231
      di3D.setConvexity(convexity);
232 d586fda6 Leszek Koltunski
      di4D.setConvexity(convexity);
233 f20265a7 Leszek Koltunski
      diQu.setConvexity(convexity);
234 dea555b9 Leszek Koltunski
      }
235
236 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
237 f20265a7 Leszek Koltunski
// DynamicQuat does not support noise
238 427ab7bf Leszek Koltunski
239 d586fda6 Leszek Koltunski
    public void setNoise(float noise0, float noise1, float noise2, float noise3)
240 427ab7bf Leszek Koltunski
      {
241 d586fda6 Leszek Koltunski
      mNoise[0] = noise0;
242
      mNoise[1] = noise1;
243
      mNoise[2] = noise2;
244
      mNoise[3] = noise3;
245 9ff0c8c3 Leszek Koltunski
246 d586fda6 Leszek Koltunski
      p1N.set(mNoise[0]);
247
      p2N.set(mNoise[0],mNoise[1]);
248
      p3N.set(mNoise[0],mNoise[1],mNoise[2]);
249
      p4N.set(mNoise[0],mNoise[1],mNoise[2], mNoise[3]);
250 9ff0c8c3 Leszek Koltunski
251
      di1D.setNoise(p1N);
252
      di2D.setNoise(p2N);
253
      di3D.setNoise(p3N);
254 d586fda6 Leszek Koltunski
      di4D.setNoise(p4N);
255 427ab7bf Leszek Koltunski
      }
256
    
257 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
258 427ab7bf Leszek Koltunski
259 fe3c72ce Leszek Koltunski
    public void setDimension(int dim)
260 427ab7bf Leszek Koltunski
      {
261
      if( currentDim != dim )
262
        {
263 d586fda6 Leszek Koltunski
        if( !(currentDim==DIM_3DXY && dim==DIM_3DXZ) && !(currentDim==DIM_3DXZ && dim==DIM_3DXY) &&
264 f20265a7 Leszek Koltunski
            !(currentDim==DIM_4DXY && dim==DIM_4DZW) && !(currentDim==DIM_4DZW && dim==DIM_4DXY) &&
265
            !(currentDim==DIM_Q_XY && dim==DIM_Q_ZW) && !(currentDim==DIM_Q_ZW && dim==DIM_Q_XY)  )
266 427ab7bf Leszek Koltunski
          {
267 2666a48c Leszek Koltunski
          resetPoints();
268 427ab7bf Leszek Koltunski
          }
269 2666a48c Leszek Koltunski
270 427ab7bf Leszek Koltunski
        currentDim = dim;
271
        }
272
      }
273
    
274 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
275 427ab7bf Leszek Koltunski
    
276 fe3c72ce Leszek Koltunski
    public void drawCurve(Canvas c, long time)
277 427ab7bf Leszek Koltunski
      {
278 b041d424 Leszek Koltunski
      if( mLastTime<0 )
279
        {
280
        mLastTime = time;
281
        }
282
      else
283
        {
284
        mDiffTime = time - mLastTime;
285
        }
286
287 427ab7bf Leszek Koltunski
      synchronized(lock)
288
        {
289
        switch(currentDim)
290
          {
291 d586fda6 Leszek Koltunski
          case DIM_1D  : drawHorizontalAxis(c,"x");
292
                         drawPath(c,di1D,0,1,time);
293
                         drawRedPoints1D(c);
294
                         break;
295
          case DIM_2D  : drawHorizontalAxis(c,"x");
296
                         drawVerticalAxis  (c,"y");
297
                         drawPath(c,di2D,0,1,time);
298
                         drawRedPoints2D(c);
299
                         break;
300
          case DIM_3DXY: drawHorizontalAxis(c,"x");
301
                         drawVerticalAxis  (c,"y");
302
                         drawPath(c,di3D,0,1,time);
303
                         drawRedPoints3D(c);
304
                         break;
305
          case DIM_3DXZ: drawHorizontalAxis(c,"x");
306
                         drawVerticalAxis  (c,"z");
307
                         drawPath(c,di3D,0,2,time);
308
                         drawRedPoints3D(c);
309
                         break;
310
          case DIM_4DXY: drawHorizontalAxis(c,"x");
311
                         drawVerticalAxis  (c,"y");
312
                         drawPath(c,di4D,0,1,time);
313
                         drawRedPoints4D(c);
314
                         break;
315
          case DIM_4DZW: drawHorizontalAxis(c,"z");
316
                         drawVerticalAxis  (c,"w");
317
                         drawPath(c,di4D,2,3,time);
318
                         drawRedPoints4D(c);
319
                         break;
320 f20265a7 Leszek Koltunski
          case DIM_Q_XY: drawHorizontalAxis(c,"x");
321
                         drawVerticalAxis  (c,"y");
322
                         drawPath(c,diQu,0,1,time);
323
                         drawRedPointsQu(c);
324
                         break;
325
          case DIM_Q_ZW: drawHorizontalAxis(c,"z");
326
                         drawVerticalAxis  (c,"w");
327
                         drawPath(c,diQu,2,3,time);
328
                         drawRedPointsQu(c);
329
                         break;
330 427ab7bf Leszek Koltunski
          }
331
        }
332 b041d424 Leszek Koltunski
333
      mLastTime = time;
334 427ab7bf Leszek Koltunski
      }
335
336 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
337 508f22b5 Leszek Koltunski
338 fe3c72ce Leszek Koltunski
    private void clearPoints()
339 508f22b5 Leszek Koltunski
      {
340 d586fda6 Leszek Koltunski
      for(int i=0; i<MAX_DIM*NUM_POINTS; i++)
341 508f22b5 Leszek Koltunski
         {
342 e52efe17 Leszek Koltunski
         mPoints[i] = -100000.0f;
343 508f22b5 Leszek Koltunski
         }
344
      }
345
346 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
347 508f22b5 Leszek Koltunski
348 2666a48c Leszek Koltunski
    public void resetPoints()
349 427ab7bf Leszek Koltunski
      {
350 2666a48c Leszek Koltunski
      synchronized(lock)
351 427ab7bf Leszek Koltunski
        {
352 2666a48c Leszek Koltunski
        clearPoints();
353 97dadfe5 Leszek Koltunski
354 2666a48c Leszek Koltunski
        switch(currentDim)
355 427ab7bf Leszek Koltunski
          {
356 2666a48c Leszek Koltunski
          case DIM_1D  : di1D.removeAll(); break;
357
          case DIM_2D  : di2D.removeAll(); break;
358
          case DIM_3DXY:
359
          case DIM_3DXZ: di3D.removeAll(); break;
360 d586fda6 Leszek Koltunski
          case DIM_4DXY:
361
          case DIM_4DZW: di4D.removeAll(); break;
362 f20265a7 Leszek Koltunski
          case DIM_Q_XY:
363
          case DIM_Q_ZW: diQu.removeAll(); break;
364 427ab7bf Leszek Koltunski
          }
365 e52efe17 Leszek Koltunski
366 d84f5b73 Leszek Koltunski
        DynamicActivity act = mAct.get();
367
        act.setNumRedPoints(0);
368
        act.clearPoints();
369 427ab7bf Leszek Koltunski
        }
370
      }
371 2666a48c Leszek Koltunski
372 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
373 2666a48c Leszek Koltunski
374
    public void startDynamic()
375
      {
376
      mRunning = true;
377
      mLastTime= -1;
378
      mStartTime = System.currentTimeMillis();
379
380
      clearPoints();
381
      di1D.resetToBeginning();
382
      di2D.resetToBeginning();
383
      di3D.resetToBeginning();
384 d586fda6 Leszek Koltunski
      di4D.resetToBeginning();
385 f20265a7 Leszek Koltunski
      diQu.resetToBeginning();
386 2666a48c Leszek Koltunski
      }
387
388 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
389 2666a48c Leszek Koltunski
390
    public void stopDynamic()
391
      {
392
      mRunning = false;
393
      }
394
395 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
396 2666a48c Leszek Koltunski
397
    private void drawHorizontalAxis(Canvas c, String label)
398 427ab7bf Leszek Koltunski
      {
399
      mPaint.setColor(0xff000000);
400 2666a48c Leszek Koltunski
401
      c.drawLine(0, halfScreenHeight, halfScreenWidth*2, halfScreenHeight, mPaint);
402
      c.drawText( label, 0.95f*halfScreenWidth*2, halfScreenHeight + mSizeT , mPaint);
403
      }
404
405
406 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
407 2666a48c Leszek Koltunski
408
    private void drawVerticalAxis(Canvas c, String label)
409
      {
410
      mPaint.setColor(0xff000000);
411
412
      c.drawLine(halfScreenWidth, 0, halfScreenWidth, halfScreenHeight*2, mPaint);
413
      c.drawText(label, halfScreenWidth + mSizeT,                mSizeT , mPaint);
414
      }
415
416 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
417 2666a48c Leszek Koltunski
418 d586fda6 Leszek Koltunski
    private void drawPath(Canvas c, Dynamic dyn, int indexH, int indexW, long time)
419 2666a48c Leszek Koltunski
      {
420
      int len = dyn.getNumPoints();
421
422 427ab7bf Leszek Koltunski
      if( len>=2 )
423
        {
424 2666a48c Leszek Koltunski
        if( mRunning )
425
          {
426
          if ( ++mPosition >= NUM_POINTS ) mPosition=0;
427
428
          if( dyn.getDimension()==1 )
429
            {
430 d586fda6 Leszek Koltunski
            mPoints[MAX_DIM*mPosition+indexW] = halfScreenHeight;
431 2666a48c Leszek Koltunski
            }
432
433 d5e134f2 Leszek Koltunski
          if( dyn.get(mPoints,MAX_DIM*mPosition, time, mDiffTime) )
434 2666a48c Leszek Koltunski
            {
435
            stopDynamic();
436
            }
437 e52efe17 Leszek Koltunski
438
          addNewSpeedPoint(time);
439 2666a48c Leszek Koltunski
          }
440 97dadfe5 Leszek Koltunski
441 f20265a7 Leszek Koltunski
        if( currentDim!=DIM_Q_XY && currentDim!=DIM_Q_ZW )
442
          {
443
          for(int i=0; i<NUM_POINTS; i++)
444
            {
445
            int color = i<=mPosition ? 0xff - (mPosition           -i)*0xff/(NUM_POINTS-1)
446
                                     : 0xff - (mPosition+NUM_POINTS-i)*0xff/(NUM_POINTS-1);
447
448
            mPaint.setColor( 0xffffff + ((color&0xff)<<24) );
449
            c.drawCircle(mPoints[MAX_DIM*i+indexH], mPoints[MAX_DIM*i+indexW] , mSize1, mPaint );
450
            }
451
          }
452
        else
453 427ab7bf Leszek Koltunski
          {
454 f20265a7 Leszek Koltunski
          float x,y,min = QUAT_QUOT* (halfScreenWidth<halfScreenHeight ? halfScreenWidth:halfScreenHeight );
455 2666a48c Leszek Koltunski
456 f20265a7 Leszek Koltunski
          for(int i=0; i<NUM_POINTS; i++)
457
            {
458
            int color = i<=mPosition ? 0xff - (mPosition           -i)*0xff/(NUM_POINTS-1)
459
                                     : 0xff - (mPosition+NUM_POINTS-i)*0xff/(NUM_POINTS-1);
460
461
            mPaint.setColor( 0xffffff + ((color&0xff)<<24) );
462
            x = mPoints[MAX_DIM*i+indexH]*min + halfScreenWidth;
463
            y = mPoints[MAX_DIM*i+indexW]*min + halfScreenHeight;
464
            c.drawCircle( x, y, mSize1, mPaint );
465
            }
466 427ab7bf Leszek Koltunski
          }
467
        }
468 2666a48c Leszek Koltunski
      }
469
470 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
471 2666a48c Leszek Koltunski
472
    private void drawRedPoints1D(Canvas c)
473
      {
474
      int len = di1D.getNumPoints();
475
476 427ab7bf Leszek Koltunski
      for(int curr=0; curr<len; curr++)
477 2666a48c Leszek Koltunski
        {
478
        p1D = di1D.getPoint(curr);
479 bcbd5b45 Leszek Koltunski
        drawRedPoint(c,curr+"", p1D.get0(), halfScreenHeight);
480 427ab7bf Leszek Koltunski
        }
481
      }
482
483 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
484 2666a48c Leszek Koltunski
485
    private void drawRedPoints2D(Canvas c)
486 427ab7bf Leszek Koltunski
      {
487 2666a48c Leszek Koltunski
      int len = di2D.getNumPoints();
488 97dadfe5 Leszek Koltunski
489 2666a48c Leszek Koltunski
      for(int curr=0; curr<len; curr++)
490
        {
491
        p2D = di2D.getPoint(curr);
492 bcbd5b45 Leszek Koltunski
        drawRedPoint(c,curr+"", p2D.get0(), p2D.get1());
493 427ab7bf Leszek Koltunski
        }
494 2666a48c Leszek Koltunski
      }
495
496 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
497 2666a48c Leszek Koltunski
498
    private void drawRedPoints3D(Canvas c)
499
      {
500
      int len = di3D.getNumPoints();
501
502 427ab7bf Leszek Koltunski
      for(int curr=0; curr<len; curr++)
503 2666a48c Leszek Koltunski
        {
504 427ab7bf Leszek Koltunski
        p3D = di3D.getPoint(curr);
505 bcbd5b45 Leszek Koltunski
        drawRedPoint(c,curr+"", p3D.get0(), currentDim==DIM_3DXY ? p3D.get1():p3D.get2());
506 2666a48c Leszek Koltunski
        }
507 427ab7bf Leszek Koltunski
      }
508 2666a48c Leszek Koltunski
509 d586fda6 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
510
511
    private void drawRedPoints4D(Canvas c)
512
      {
513
      int len = di4D.getNumPoints();
514
515
      for(int curr=0; curr<len; curr++)
516
        {
517
        p4D = di4D.getPoint(curr);
518
519 bcbd5b45 Leszek Koltunski
        if( currentDim==DIM_4DXY ) drawRedPoint(c,curr+"", p4D.get0(), p4D.get1());
520
        else                       drawRedPoint(c,curr+"", p4D.get2(), p4D.get3());
521 d586fda6 Leszek Koltunski
        }
522
      }
523
524 f20265a7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
525
526
    private void drawRedPointsQu(Canvas c)
527
      {
528
      int len = diQu.getNumPoints();
529
      float x,y, min = QUAT_QUOT* (halfScreenWidth<halfScreenHeight ? halfScreenWidth:halfScreenHeight );
530
531
      for(int curr=0; curr<len; curr++)
532
        {
533
        pQD = diQu.getPoint(curr);
534
535
        if( currentDim==DIM_Q_XY )
536
          {
537 bcbd5b45 Leszek Koltunski
          x = pQD.get0()*min + halfScreenWidth;
538
          y = pQD.get1()*min + halfScreenHeight;
539 f20265a7 Leszek Koltunski
          }
540
        else
541
          {
542 bcbd5b45 Leszek Koltunski
          x = pQD.get2()*min + halfScreenWidth;
543
          y = pQD.get3()*min + halfScreenHeight;
544 f20265a7 Leszek Koltunski
          }
545
546
        drawRedPoint(c,curr+"", x,y);
547
        }
548
      }
549
550 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
551 2666a48c Leszek Koltunski
552
    private void drawRedPoint(Canvas c, String label, float width, float height)
553
      {
554
      mPaint.setColor(0xffff0000);
555
      c.drawCircle( width, height, mSize2, mPaint);
556
      mPaint.setColor(0xffffffff);
557
      c.drawText(label, width,height-mFontHeight, mPaint);
558
      }
559
560 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
561 427ab7bf Leszek Koltunski
562 f20265a7 Leszek Koltunski
    private void addNewPoint(float x, float y)
563 427ab7bf Leszek Koltunski
      {
564 d586fda6 Leszek Koltunski
      float gx,gy,gz,gw;
565 427ab7bf Leszek Koltunski
      int len;
566 d586fda6 Leszek Koltunski
      int minDist = (mAvg*mAvg)/100;
567 dea555b9 Leszek Koltunski
568 427ab7bf Leszek Koltunski
      switch(currentDim)
569
        {
570 d586fda6 Leszek Koltunski
        case DIM_1D : len = di1D.getNumPoints();
571 dea555b9 Leszek Koltunski
572 d586fda6 Leszek Koltunski
                      for(int g=0; g<len; g++)
573
                        {
574
                        p1D = di1D.getPoint(g);
575 bcbd5b45 Leszek Koltunski
                        gx = p1D.get0();
576 427ab7bf Leszek Koltunski
                                    
577 d586fda6 Leszek Koltunski
                        if( (x-gx)*(x-gx) < minDist )
578
                          {
579
                          mMoving = g;
580
                          break;
581
                          }
582
                        }
583
                      if( mMoving <0 )
584
                        {
585
                        synchronized(lock)
586
                          {
587
                          di1D.add(new Static1D(x));
588
                          mAct.get().setNumRedPoints(len+1);
589
                          }
590
                        }
591
                      break;
592
        case DIM_2D : len = di2D.getNumPoints();
593 427ab7bf Leszek Koltunski
                                 
594 d586fda6 Leszek Koltunski
                      for(int g=0; g<len; g++)
595
                        {
596
                        p2D = di2D.getPoint(g);
597 bcbd5b45 Leszek Koltunski
                        gx = p2D.get0();
598
                        gy = p2D.get1();
599 427ab7bf Leszek Koltunski
                                    
600 d586fda6 Leszek Koltunski
                        if( (x-gx)*(x-gx) + (y-gy)*(y-gy) < minDist )
601
                          {
602
                          mMoving = g;
603
                          break;
604
                          }
605
                        }
606
                      if( mMoving <0 )
607
                        {
608
                        synchronized(lock)
609
                          {
610
                          di2D.add(new Static2D(x,y));
611
                          mAct.get().setNumRedPoints(len+1);
612
                          }
613
                        }
614
                      break;
615
        case DIM_3DXY:
616
        case DIM_3DXZ:len = di3D.getNumPoints();
617 427ab7bf Leszek Koltunski
                                 
618 d586fda6 Leszek Koltunski
                      for(int g=0; g<len; g++)
619
                        {
620
                        p3D = di3D.getPoint(g);
621 bcbd5b45 Leszek Koltunski
                        gx = p3D.get0();
622
                        gy = p3D.get1();
623
                        gz = p3D.get2();
624 427ab7bf Leszek Koltunski
                               
625 d586fda6 Leszek Koltunski
                        if( currentDim==DIM_3DXY )
626
                          {
627
                          if( (x-gx)*(x-gx) + (y-gy)*(y-gy) < minDist )
628
                            {
629
                            mMoving = g;
630
                            break;
631
                            }
632
                          }
633
                        if( currentDim==DIM_3DXZ )
634
                          {
635
                          if( (x-gx)*(x-gx) + (y-gz)*(y-gz) < minDist )
636
                            {
637
                            mMoving = g;
638
                            break;
639
                            }
640
                          }
641
                        }
642
643
                      if( mMoving <0 )
644
                        {
645
                        synchronized(lock)
646
                          {
647
                          if( currentDim==DIM_3DXY ) di3D.add(new Static3D(x,y, halfScreenHeight));
648
                          if( currentDim==DIM_3DXZ ) di3D.add(new Static3D(x, halfScreenHeight,y));
649
                          mAct.get().setNumRedPoints(len+1);
650
                          }
651
                        }
652
                      break;
653
        case DIM_4DXY:
654
        case DIM_4DZW:len = di4D.getNumPoints();
655
656
                      for(int g=0; g<len; g++)
657
                        {
658
                        p4D = di4D.getPoint(g);
659 bcbd5b45 Leszek Koltunski
                        gx = p4D.get0();
660
                        gy = p4D.get1();
661
                        gz = p4D.get2();
662
                        gw = p4D.get3();
663 d586fda6 Leszek Koltunski
664
                        if( currentDim==DIM_4DXY )
665
                          {
666
                          if( (x-gx)*(x-gx) + (y-gy)*(y-gy) < minDist )
667
                            {
668
                            mMoving = g;
669
                            break;
670
                            }
671
                          }
672
                        if( currentDim==DIM_4DZW )
673
                          {
674
                          if( (x-gz)*(x-gz) + (y-gw)*(y-gw) < minDist )
675
                            {
676
                            mMoving = g;
677
                            break;
678
                            }
679
                          }
680
                        }
681
682
                      if( mMoving <0 )
683
                        {
684
                        synchronized(lock)
685
                          {
686
                          if( currentDim==DIM_4DXY ) di4D.add(new Static4D(x,y, halfScreenWidth, halfScreenHeight));
687
                          if( currentDim==DIM_4DZW ) di4D.add(new Static4D( halfScreenWidth, halfScreenHeight,x,y));
688
                          mAct.get().setNumRedPoints(len+1);
689
                          }
690
                        }
691
                      break;
692 f20265a7 Leszek Koltunski
        case DIM_Q_XY:
693
        case DIM_Q_ZW:len = diQu.getNumPoints();
694
                      float min = QUAT_QUOT* (halfScreenWidth<halfScreenHeight ? halfScreenWidth:halfScreenHeight );
695
696
                      for(int g=0; g<len; g++)
697
                        {
698
                        pQD = diQu.getPoint(g);
699
700
                        if( currentDim==DIM_Q_XY )
701
                          {
702 bcbd5b45 Leszek Koltunski
                          gx = pQD.get0()*min + halfScreenWidth;
703
                          gy = pQD.get1()*min + halfScreenHeight;
704 f20265a7 Leszek Koltunski
705
                          if( (x-gx)*(x-gx) + (y-gy)*(y-gy) < minDist )
706
                            {
707
                            mMoving = g;
708
                            break;
709
                            }
710
                          }
711
                        if( currentDim==DIM_Q_ZW )
712
                          {
713 bcbd5b45 Leszek Koltunski
                          gz = pQD.get2()*min + halfScreenWidth;
714
                          gw = pQD.get3()*min + halfScreenHeight;
715 f20265a7 Leszek Koltunski
716
                          if( (x-gz)*(x-gz) + (y-gw)*(y-gw) < minDist )
717
                            {
718
                            mMoving = g;
719
                            break;
720
                            }
721
                          }
722
                        }
723
724
                      if( mMoving <0 )
725
                        {
726
                        float z,w;
727
                        x = (x - halfScreenWidth ) / min;
728
                        y = (y - halfScreenHeight) / min;
729
                        float len1 = x*x + y*y;
730
731
                        if( len1>= 1.0f )
732
                          {
733
                          float A = (float)Math.sqrt(len1);
734
                          x = x/A;
735
                          y = y/A;
736
                          z = 0.0f;
737
                          w = 0.0f;
738
                          }
739
                        else
740
                          {
741
                          z = (float)Math.sqrt(1-len1);
742
                          w = 0.0f;
743
                          }
744
745
                        synchronized(lock)
746
                          {
747
                          if( currentDim==DIM_Q_XY ) diQu.add(new Static4D(x,y,z,w));
748
                          if( currentDim==DIM_Q_ZW ) diQu.add(new Static4D(z,w,x,y));
749
                          mAct.get().setNumRedPoints(len+1);
750
                          }
751
                        }
752
                      break;
753 427ab7bf Leszek Koltunski
        }
754
      }
755 f20265a7 Leszek Koltunski
756 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
757
758
    private void addNewSpeedPoint(long time)
759
      {
760
      int prev = mPosition-1;
761
      if( prev<0 ) prev = NUM_POINTS-1;
762
763 d586fda6 Leszek Koltunski
      float xdiff = mPoints[MAX_DIM*prev  ]-mPoints[MAX_DIM*mPosition  ];
764
      float ydiff = mPoints[MAX_DIM*prev+1]-mPoints[MAX_DIM*mPosition+1];
765
      float zdiff = mPoints[MAX_DIM*prev+2]-mPoints[MAX_DIM*mPosition+2];
766
      float wdiff = mPoints[MAX_DIM*prev+3]-mPoints[MAX_DIM*mPosition+3];
767 e52efe17 Leszek Koltunski
768 d586fda6 Leszek Koltunski
      float dist = (float)Math.sqrt( xdiff*xdiff + ydiff*ydiff + zdiff*zdiff + wdiff*wdiff);
769 e52efe17 Leszek Koltunski
      float speed= mDiffTime<=0 ? 0: dist / mDiffTime;
770
      float timepoint = ((float)(time-mStartTime))/mDuration;
771
772
      if( dist<1000.0f )   // otherwise this is a very first call; do not send it!
773
        {
774
        mAct.get().addPoint( timepoint - (int)timepoint, speed );
775
        }
776
      }
777
778
///////////////////////////////////////////////////////////////////////////////////////////////////
779
780 2666a48c Leszek Koltunski
    @Override
781
    public boolean onTouchEvent(MotionEvent event)
782 427ab7bf Leszek Koltunski
      {
783
      int action = event.getAction();
784 f20265a7 Leszek Koltunski
      float xDown, yDown;
785 97dadfe5 Leszek Koltunski
786 427ab7bf Leszek Koltunski
      switch(action)
787
        {
788 f20265a7 Leszek Koltunski
        case MotionEvent.ACTION_DOWN: xDown = event.getX();
789
                                      yDown = event.getY();
790
791 427ab7bf Leszek Koltunski
                                      addNewPoint(xDown,yDown);
792 f20265a7 Leszek Koltunski
793 427ab7bf Leszek Koltunski
                                      break;
794 2666a48c Leszek Koltunski
        case MotionEvent.ACTION_MOVE: if( mMoving >=0 )
795 427ab7bf Leszek Koltunski
                                        {
796 f20265a7 Leszek Koltunski
                                        xDown = event.getX();
797
                                        yDown = event.getY();
798 427ab7bf Leszek Koltunski
                                        
799
                                        switch(currentDim)
800
                                          {
801 2666a48c Leszek Koltunski
                                          case DIM_1D  : di1D.setPoint(mMoving, xDown);
802 427ab7bf Leszek Koltunski
                                                         break;
803 2666a48c Leszek Koltunski
                                          case DIM_2D  : di2D.setPoint(mMoving, xDown, yDown);
804 427ab7bf Leszek Koltunski
                                                         break;
805 bcbd5b45 Leszek Koltunski
                                          case DIM_3DXY: di3D.setPoint(mMoving, xDown, yDown, (int)di3D.getPoint(mMoving).get2());
806 427ab7bf Leszek Koltunski
                                                         break;
807 bcbd5b45 Leszek Koltunski
                                          case DIM_3DXZ: di3D.setPoint(mMoving, xDown, (int)di3D.getPoint(mMoving).get1(), yDown);
808 427ab7bf Leszek Koltunski
                                                         break;
809 bcbd5b45 Leszek Koltunski
                                          case DIM_4DXY: di4D.setPoint(mMoving, xDown, yDown, (int)di4D.getPoint(mMoving).get2(), (int)di4D.getPoint(mMoving).get3());
810 d586fda6 Leszek Koltunski
                                                         break;
811 bcbd5b45 Leszek Koltunski
                                          case DIM_4DZW: di4D.setPoint(mMoving, (int)di4D.getPoint(mMoving).get0(), (int)di4D.getPoint(mMoving).get1(), xDown, yDown);
812 d586fda6 Leszek Koltunski
                                                         break;
813 f20265a7 Leszek Koltunski
                                          case DIM_Q_XY: float min1 = QUAT_QUOT* (halfScreenWidth<halfScreenHeight ? halfScreenWidth:halfScreenHeight );
814
                                                         float x1 = (xDown - halfScreenWidth ) / min1;
815
                                                         float y1 = (yDown - halfScreenHeight) / min1;
816 bcbd5b45 Leszek Koltunski
                                                         float z1 = diQu.getPoint(mMoving).get2();
817
                                                         float w1 = diQu.getPoint(mMoving).get3();
818 f20265a7 Leszek Koltunski
                                                         float len1 = x1*x1 + y1*y1;
819
820
                                                         if( len1 <= 1.0f )
821
                                                           {
822
                                                           float len2 = z1*z1 + w1*w1;
823
824
                                                           if( len2 == 0 )
825
                                                             {
826
                                                             if( len1 == 0 )
827
                                                               {
828
                                                               w1 = 1.0f;
829
                                                               }
830
                                                             else
831
                                                               {
832
                                                               float B = (float)Math.sqrt(len1);
833
                                                               x1 = x1/B;
834
                                                               y1 = y1/B;
835
                                                               }
836
                                                             }
837
                                                           else
838
                                                             {
839
                                                             float A = (float)Math.sqrt((1.0f - len1)/len2);
840
                                                             z1 = A*z1;
841
                                                             w1 = A*w1;
842
                                                             }
843
                                                           }
844
                                                         else
845
                                                           {
846
                                                           float B = (float)Math.sqrt(len1);
847
                                                           x1 = x1/B;
848
                                                           y1 = y1/B;
849
                                                           z1 = 0.0f;
850
                                                           w1 = 0.0f;
851
                                                           }
852
                                                         diQu.setPoint(mMoving, x1,y1,z1,w1);
853
                                                         break;
854
                                          case DIM_Q_ZW: float min2 = QUAT_QUOT* (halfScreenWidth<halfScreenHeight ? halfScreenWidth:halfScreenHeight );
855 bcbd5b45 Leszek Koltunski
                                                         float x2 = diQu.getPoint(mMoving).get0();
856
                                                         float y2 = diQu.getPoint(mMoving).get1();
857 f20265a7 Leszek Koltunski
                                                         float z2 = (xDown - halfScreenWidth ) / min2;
858
                                                         float w2 = (yDown - halfScreenHeight) / min2;
859
                                                         float len3 = z2*z2 + w2*w2;
860
861
                                                         if( len3 <= 1.0f )
862
                                                           {
863
                                                           float len4 = x2*x2 + y2*y2;
864
865
                                                           if( len4 == 0 )
866
                                                             {
867
                                                             if( len3 == 0 )
868
                                                               {
869
                                                               w2 = 1.0f;
870
                                                               }
871
                                                             else
872
                                                               {
873
                                                               float B = (float)Math.sqrt(len3);
874
                                                               z2 = z2/B;
875
                                                               w2 = w2/B;
876
                                                               }
877
                                                             }
878
                                                           else
879
                                                             {
880
                                                             float A = (float)Math.sqrt((1.0f - len3)/len4);
881
                                                             x2 = A*x2;
882
                                                             y2 = A*y2;
883
                                                             }
884
                                                           }
885
                                                         else
886
                                                           {
887
                                                           float B = (float)Math.sqrt(len3);
888
                                                           x2 = 0.0f;
889
                                                           y2 = 0.0f;
890
                                                           z2 = z2/B;
891
                                                           w2 = w2/B;
892
                                                           }
893
                                                         diQu.setPoint(mMoving, x2,y2,z2,w2);
894
                                                         break;
895 427ab7bf Leszek Koltunski
                                          }
896
                                        }                           
897
                                      break;
898 2666a48c Leszek Koltunski
        case MotionEvent.ACTION_UP  : mMoving = -1;
899 427ab7bf Leszek Koltunski
                                      break;
900
        }
901
            
902
      return true;
903
      }
904
  }