Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// 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
// Distorted is distributed in the hope that it will be useful,                                  //
12
// 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
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.examples.dynamic;
21

    
22
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.graphics.Rect;
26
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
import org.distorted.library.type.Dynamic;
34
import org.distorted.library.type.Dynamic1D;
35
import org.distorted.library.type.Dynamic2D;
36
import org.distorted.library.type.Dynamic3D;
37
import org.distorted.library.type.Dynamic4D;
38
import org.distorted.library.type.DynamicQuat;
39
import org.distorted.library.type.Static1D;
40
import org.distorted.library.type.Static2D;
41
import org.distorted.library.type.Static3D;
42
import org.distorted.library.type.Static4D;
43

    
44
import java.lang.ref.WeakReference;
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

    
48
public class DynamicSurfaceView extends GLSurfaceView
49
    {
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
    public static final int DIM_4DXY = 4;
55
    public static final int DIM_4DZW = 5;
56
    public static final int DIM_Q_XY = 6;
57
    public static final int DIM_Q_ZW = 7;
58

    
59
    private static final int MAX_DIM = 4;
60
    private static final float QUAT_QUOT = 0.9f;
61

    
62
    static final int NUM_POINTS = 250;
63
    private static final Object lock = new Object();
64

    
65
    private WeakReference<DynamicActivity> mAct;
66

    
67
    private static int halfScreenHeight=0;
68
    private static int halfScreenWidth =0;
69

    
70
    private Dynamic1D   di1D;
71
    private Dynamic2D   di2D;
72
    private Dynamic3D   di3D;
73
    private Dynamic4D   di4D;
74
    private DynamicQuat diQu;
75
    
76
    private Paint mPaint;
77
    private int mMoving;
78
    private int mDuration;
79
    private int mPosition;
80
    private long mDiffTime, mLastTime, mStartTime;
81
    private float[] mNoise = new float[MAX_DIM];
82
    private float mCount;
83

    
84
    private int mSize1, mSize2, mSizeT, mAvg;
85
    private float mFontHeight;
86

    
87
    private int currentDim= DIM_2D;
88
    
89
    private Static1D p1D;
90
    private Static2D p2D;
91
    private Static3D p3D;
92
    private Static4D p4D;
93
    private Static4D pQD;
94

    
95
    private Static1D p1N;
96
    private Static2D p2N;
97
    private Static3D p3N;
98
    private Static4D p4N;
99
    private Static4D pQN;
100

    
101
    private float[] mPoints = new float[MAX_DIM*NUM_POINTS];
102
    private boolean mRunning;
103

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105
    
106
    public DynamicSurfaceView(Context context, AttributeSet attrs)
107
      {
108
      super(context, attrs);
109

    
110
      DynamicActivity act = (DynamicActivity)context;
111
      mAct = new WeakReference<>(act);
112

    
113
      mPaint = new Paint();
114
      mPaint.setStyle(Style.FILL);
115
      mPaint.setAntiAlias(true);
116

    
117
      mDuration = 10000;
118
      mCount    = 0.0f;
119
      mPosition = 0;
120
      mDiffTime = -1;
121
      mLastTime = -1;
122
      mStartTime= -1;
123
      mMoving   = -1;
124
      mRunning  = false;
125

    
126
      for(int i=0; i<MAX_DIM; i++) mNoise[i] = 0.0f;
127

    
128
      clearPoints();
129

    
130
      di1D = new Dynamic1D(mDuration,mCount);
131
      p1N  = new Static1D(mNoise[0]);
132
      di2D = new Dynamic2D(mDuration,mCount);
133
      p2N  = new Static2D(mNoise[0],mNoise[1]);
134
      di3D = new Dynamic3D(mDuration,mCount);
135
      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
      diQu = new DynamicQuat(mDuration,mCount);
139
      pQN  = new Static4D(mNoise[0],mNoise[1],mNoise[2],mNoise[3]);
140

    
141
      di1D.setAccessType(Dynamic.ACCESS_TYPE_SEQUENTIAL);
142
      di2D.setAccessType(Dynamic.ACCESS_TYPE_SEQUENTIAL);
143
      di3D.setAccessType(Dynamic.ACCESS_TYPE_SEQUENTIAL);
144
      di4D.setAccessType(Dynamic.ACCESS_TYPE_SEQUENTIAL);
145
      diQu.setAccessType(Dynamic.ACCESS_TYPE_SEQUENTIAL);
146

    
147
      if(!isInEditMode())
148
        {
149
        setFocusable(true);
150
        setFocusableInTouchMode(true);
151
        DynamicRenderer mRenderer = new DynamicRenderer(this);
152
        final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
153
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
154
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
155
        setRenderer(mRenderer);
156
        }
157
      }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

    
161
    public void onSurfaceChanged(int width,int height)
162
      {
163
      mAvg = (width+height)/2;
164

    
165
      mSize1 = mAvg/150;
166
      mSize2 = mAvg/50;
167
      mSizeT = mAvg/30;
168

    
169
      mPaint.setTextSize(mSizeT);
170
      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

    
177
      clearPoints();
178
      }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

    
182
    public static void surfaceChanged(int w, int h)
183
      {
184
      halfScreenWidth = w/2;
185
      halfScreenHeight= h/2;
186
      }
187

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189

    
190
    public void setMode(int mode)
191
      {
192
      di1D.setMode(mode);  
193
      di2D.setMode(mode);
194
      di3D.setMode(mode);
195
      di4D.setMode(mode);
196
      diQu.setMode(mode);
197
      }
198

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200

    
201
    public void setDuration(int duration)
202
      {
203
      mDuration = duration;
204
      
205
      di1D.setDuration(duration);
206
      di2D.setDuration(duration);
207
      di3D.setDuration(duration);
208
      di4D.setDuration(duration);
209
      diQu.setDuration(duration);
210
      }
211

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

    
214
    public void setCount(float count)
215
      {
216
      mCount = count;
217

    
218
      di1D.setCount(count);
219
      di2D.setCount(count);
220
      di3D.setCount(count);
221
      di4D.setCount(count);
222
      diQu.setCount(count);
223
      }
224

    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226

    
227
    public void setConvexity(float convexity)
228
      {
229
      di1D.setConvexity(convexity);
230
      di2D.setConvexity(convexity);
231
      di3D.setConvexity(convexity);
232
      di4D.setConvexity(convexity);
233
      diQu.setConvexity(convexity);
234
      }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237
// DynamicQuat does not support noise
238

    
239
    public void setNoise(float noise0, float noise1, float noise2, float noise3)
240
      {
241
      mNoise[0] = noise0;
242
      mNoise[1] = noise1;
243
      mNoise[2] = noise2;
244
      mNoise[3] = noise3;
245

    
246
      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

    
251
      di1D.setNoise(p1N);
252
      di2D.setNoise(p2N);
253
      di3D.setNoise(p3N);
254
      di4D.setNoise(p4N);
255
      }
256
    
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258

    
259
    public void setDimension(int dim)
260
      {
261
      if( currentDim != dim )
262
        {
263
        if( !(currentDim==DIM_3DXY && dim==DIM_3DXZ) && !(currentDim==DIM_3DXZ && dim==DIM_3DXY) &&
264
            !(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
          {
267
          resetPoints();
268
          }
269

    
270
        currentDim = dim;
271
        }
272
      }
273
    
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275
    
276
    public void drawCurve(Canvas c, long time)
277
      {
278
      if( mLastTime<0 )
279
        {
280
        mLastTime = time;
281
        }
282
      else
283
        {
284
        mDiffTime = time - mLastTime;
285
        }
286

    
287
      synchronized(lock)
288
        {
289
        switch(currentDim)
290
          {
291
          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
          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
          }
331
        }
332

    
333
      mLastTime = time;
334
      }
335

    
336
///////////////////////////////////////////////////////////////////////////////////////////////////
337

    
338
    private void clearPoints()
339
      {
340
      for(int i=0; i<MAX_DIM*NUM_POINTS; i++)
341
         {
342
         mPoints[i] = -100000.0f;
343
         }
344
      }
345

    
346
///////////////////////////////////////////////////////////////////////////////////////////////////
347

    
348
    public void resetPoints()
349
      {
350
      synchronized(lock)
351
        {
352
        clearPoints();
353

    
354
        switch(currentDim)
355
          {
356
          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
          case DIM_4DXY:
361
          case DIM_4DZW: di4D.removeAll(); break;
362
          case DIM_Q_XY:
363
          case DIM_Q_ZW: diQu.removeAll(); break;
364
          }
365

    
366
        DynamicActivity act = mAct.get();
367
        act.setNumRedPoints(0);
368
        act.clearPoints();
369
        }
370
      }
371

    
372
///////////////////////////////////////////////////////////////////////////////////////////////////
373

    
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
      di4D.resetToBeginning();
385
      diQu.resetToBeginning();
386
      }
387

    
388
///////////////////////////////////////////////////////////////////////////////////////////////////
389

    
390
    public void stopDynamic()
391
      {
392
      mRunning = false;
393
      }
394

    
395
///////////////////////////////////////////////////////////////////////////////////////////////////
396

    
397
    private void drawHorizontalAxis(Canvas c, String label)
398
      {
399
      mPaint.setColor(0xff000000);
400

    
401
      c.drawLine(0, halfScreenHeight, halfScreenWidth*2, halfScreenHeight, mPaint);
402
      c.drawText( label, 0.95f*halfScreenWidth*2, halfScreenHeight + mSizeT , mPaint);
403
      }
404

    
405

    
406
///////////////////////////////////////////////////////////////////////////////////////////////////
407

    
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
///////////////////////////////////////////////////////////////////////////////////////////////////
417

    
418
    private void drawPath(Canvas c, Dynamic dyn, int indexH, int indexW, long time)
419
      {
420
      int len = dyn.getNumPoints();
421

    
422
      if( len>=2 )
423
        {
424
        if( mRunning )
425
          {
426
          if ( ++mPosition >= NUM_POINTS ) mPosition=0;
427

    
428
          if( dyn.getDimension()==1 )
429
            {
430
            mPoints[MAX_DIM*mPosition+indexW] = halfScreenHeight;
431
            }
432

    
433
          if( dyn.get(mPoints,MAX_DIM*mPosition, time, mDiffTime) )
434
            {
435
            stopDynamic();
436
            }
437

    
438
          addNewSpeedPoint(time);
439
          }
440

    
441
        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
          {
454
          float x,y,min = QUAT_QUOT* (halfScreenWidth<halfScreenHeight ? halfScreenWidth:halfScreenHeight );
455

    
456
          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
          }
467
        }
468
      }
469

    
470
///////////////////////////////////////////////////////////////////////////////////////////////////
471

    
472
    private void drawRedPoints1D(Canvas c)
473
      {
474
      int len = di1D.getNumPoints();
475

    
476
      for(int curr=0; curr<len; curr++)
477
        {
478
        p1D = di1D.getPoint(curr);
479
        drawRedPoint(c,curr+"", p1D.get0(), halfScreenHeight);
480
        }
481
      }
482

    
483
///////////////////////////////////////////////////////////////////////////////////////////////////
484

    
485
    private void drawRedPoints2D(Canvas c)
486
      {
487
      int len = di2D.getNumPoints();
488

    
489
      for(int curr=0; curr<len; curr++)
490
        {
491
        p2D = di2D.getPoint(curr);
492
        drawRedPoint(c,curr+"", p2D.get0(), p2D.get1());
493
        }
494
      }
495

    
496
///////////////////////////////////////////////////////////////////////////////////////////////////
497

    
498
    private void drawRedPoints3D(Canvas c)
499
      {
500
      int len = di3D.getNumPoints();
501

    
502
      for(int curr=0; curr<len; curr++)
503
        {
504
        p3D = di3D.getPoint(curr);
505
        drawRedPoint(c,curr+"", p3D.get0(), currentDim==DIM_3DXY ? p3D.get1():p3D.get2());
506
        }
507
      }
508

    
509
///////////////////////////////////////////////////////////////////////////////////////////////////
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
        if( currentDim==DIM_4DXY ) drawRedPoint(c,curr+"", p4D.get0(), p4D.get1());
520
        else                       drawRedPoint(c,curr+"", p4D.get2(), p4D.get3());
521
        }
522
      }
523

    
524
///////////////////////////////////////////////////////////////////////////////////////////////////
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
          x = pQD.get0()*min + halfScreenWidth;
538
          y = pQD.get1()*min + halfScreenHeight;
539
          }
540
        else
541
          {
542
          x = pQD.get2()*min + halfScreenWidth;
543
          y = pQD.get3()*min + halfScreenHeight;
544
          }
545

    
546
        drawRedPoint(c,curr+"", x,y);
547
        }
548
      }
549

    
550
///////////////////////////////////////////////////////////////////////////////////////////////////
551

    
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
///////////////////////////////////////////////////////////////////////////////////////////////////
561

    
562
    private void addNewPoint(float x, float y)
563
      {
564
      float gx,gy,gz,gw;
565
      int len;
566
      int minDist = (mAvg*mAvg)/100;
567

    
568
      switch(currentDim)
569
        {
570
        case DIM_1D : len = di1D.getNumPoints();
571

    
572
                      for(int g=0; g<len; g++)
573
                        {
574
                        p1D = di1D.getPoint(g);
575
                        gx = p1D.get0();
576
                                    
577
                        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
                                 
594
                      for(int g=0; g<len; g++)
595
                        {
596
                        p2D = di2D.getPoint(g);
597
                        gx = p2D.get0();
598
                        gy = p2D.get1();
599
                                    
600
                        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
                                 
618
                      for(int g=0; g<len; g++)
619
                        {
620
                        p3D = di3D.getPoint(g);
621
                        gx = p3D.get0();
622
                        gy = p3D.get1();
623
                        gz = p3D.get2();
624
                               
625
                        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
                        gx = p4D.get0();
660
                        gy = p4D.get1();
661
                        gz = p4D.get2();
662
                        gw = p4D.get3();
663

    
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
        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
                          gx = pQD.get0()*min + halfScreenWidth;
703
                          gy = pQD.get1()*min + halfScreenHeight;
704

    
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
                          gz = pQD.get2()*min + halfScreenWidth;
714
                          gw = pQD.get3()*min + halfScreenHeight;
715

    
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
        }
754
      }
755

    
756
///////////////////////////////////////////////////////////////////////////////////////////////////
757

    
758
    private void addNewSpeedPoint(long time)
759
      {
760
      int prev = mPosition-1;
761
      if( prev<0 ) prev = NUM_POINTS-1;
762

    
763
      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

    
768
      float dist = (float)Math.sqrt( xdiff*xdiff + ydiff*ydiff + zdiff*zdiff + wdiff*wdiff);
769
      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
    @Override
781
    public boolean onTouchEvent(MotionEvent event)
782
      {
783
      int action = event.getAction();
784
      float xDown, yDown;
785

    
786
      switch(action)
787
        {
788
        case MotionEvent.ACTION_DOWN: xDown = event.getX();
789
                                      yDown = event.getY();
790

    
791
                                      addNewPoint(xDown,yDown);
792

    
793
                                      break;
794
        case MotionEvent.ACTION_MOVE: if( mMoving >=0 )
795
                                        {
796
                                        xDown = event.getX();
797
                                        yDown = event.getY();
798
                                        
799
                                        switch(currentDim)
800
                                          {
801
                                          case DIM_1D  : di1D.setPoint(mMoving, xDown);
802
                                                         break;
803
                                          case DIM_2D  : di2D.setPoint(mMoving, xDown, yDown);
804
                                                         break;
805
                                          case DIM_3DXY: di3D.setPoint(mMoving, xDown, yDown, (int)di3D.getPoint(mMoving).get2());
806
                                                         break;
807
                                          case DIM_3DXZ: di3D.setPoint(mMoving, xDown, (int)di3D.getPoint(mMoving).get1(), yDown);
808
                                                         break;
809
                                          case DIM_4DXY: di4D.setPoint(mMoving, xDown, yDown, (int)di4D.getPoint(mMoving).get2(), (int)di4D.getPoint(mMoving).get3());
810
                                                         break;
811
                                          case DIM_4DZW: di4D.setPoint(mMoving, (int)di4D.getPoint(mMoving).get0(), (int)di4D.getPoint(mMoving).get1(), xDown, yDown);
812
                                                         break;
813
                                          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
                                                         float z1 = diQu.getPoint(mMoving).get2();
817
                                                         float w1 = diQu.getPoint(mMoving).get3();
818
                                                         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
                                                         float x2 = diQu.getPoint(mMoving).get0();
856
                                                         float y2 = diQu.getPoint(mMoving).get1();
857
                                                         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
                                          }
896
                                        }                           
897
                                      break;
898
        case MotionEvent.ACTION_UP  : mMoving = -1;
899
                                      break;
900
        }
901
            
902
      return true;
903
      }
904
  }
(4-4/4)