Project

General

Profile

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

examples / src / main / java / org / distorted / examples / dynamic / DynamicSurfaceView.java @ 77e66c58

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 final WeakReference<DynamicActivity> mAct;
66

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

    
70
    private final Dynamic1D   di1D;
71
    private final Dynamic2D   di2D;
72
    private final Dynamic3D   di3D;
73
    private final Dynamic4D   di4D;
74
    private final DynamicQuat diQu;
75
    
76
    private final Paint mPaint;
77
    private int mMoving;
78
    private int mDuration;
79
    private int mPosition;
80
    private long mDiffTime, mLastTime, mStartTime;
81
    private final 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 final Static1D p1N;
96
    private final Static2D p2N;
97
    private final Static3D p3N;
98
    private final Static4D p4N;
99
    private final Static4D pQN;
100

    
101
    private final 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 setSpeedMode(int mode)
202
      {
203
      di1D.setSpeedMode(mode);
204
      di2D.setSpeedMode(mode);
205
      di3D.setSpeedMode(mode);
206
      di4D.setSpeedMode(mode);
207
      diQu.setSpeedMode(mode); // not implemented yet!
208
      }
209

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

    
212
    public void setDuration(int duration)
213
      {
214
      mDuration = duration;
215
      
216
      di1D.setDuration(duration);
217
      di2D.setDuration(duration);
218
      di3D.setDuration(duration);
219
      di4D.setDuration(duration);
220
      diQu.setDuration(duration);
221
      }
222

    
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224

    
225
    public void setCount(float count)
226
      {
227
      mCount = count;
228

    
229
      di1D.setCount(count);
230
      di2D.setCount(count);
231
      di3D.setCount(count);
232
      di4D.setCount(count);
233
      diQu.setCount(count);
234
      }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
    public void setConvexity(float convexity)
239
      {
240
      di1D.setConvexity(convexity);
241
      di2D.setConvexity(convexity);
242
      di3D.setConvexity(convexity);
243
      di4D.setConvexity(convexity);
244
      diQu.setConvexity(convexity);
245
      }
246

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248
// DynamicQuat does not support noise
249

    
250
    public void setNoise(float noise0, float noise1, float noise2, float noise3)
251
      {
252
      mNoise[0] = noise0;
253
      mNoise[1] = noise1;
254
      mNoise[2] = noise2;
255
      mNoise[3] = noise3;
256

    
257
      p1N.set(mNoise[0]);
258
      p2N.set(mNoise[0],mNoise[1]);
259
      p3N.set(mNoise[0],mNoise[1],mNoise[2]);
260
      p4N.set(mNoise[0],mNoise[1],mNoise[2], mNoise[3]);
261

    
262
      di1D.setNoise(p1N);
263
      di2D.setNoise(p2N);
264
      di3D.setNoise(p3N);
265
      di4D.setNoise(p4N);
266
      }
267
    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269

    
270
    public void setDimension(int dim)
271
      {
272
      if( currentDim != dim )
273
        {
274
        if( !(currentDim==DIM_3DXY && dim==DIM_3DXZ) && !(currentDim==DIM_3DXZ && dim==DIM_3DXY) &&
275
            !(currentDim==DIM_4DXY && dim==DIM_4DZW) && !(currentDim==DIM_4DZW && dim==DIM_4DXY) &&
276
            !(currentDim==DIM_Q_XY && dim==DIM_Q_ZW) && !(currentDim==DIM_Q_ZW && dim==DIM_Q_XY)  )
277
          {
278
          resetPoints();
279
          }
280

    
281
        currentDim = dim;
282
        }
283
      }
284
    
285
///////////////////////////////////////////////////////////////////////////////////////////////////
286
    
287
    public void drawCurve(Canvas c, long time)
288
      {
289
      if( mLastTime<0 )
290
        {
291
        mLastTime = time;
292
        }
293
      else
294
        {
295
        mDiffTime = time - mLastTime;
296
        }
297

    
298
      synchronized(lock)
299
        {
300
        switch(currentDim)
301
          {
302
          case DIM_1D  : drawHorizontalAxis(c,"x");
303
                         drawPath(c,di1D,0,1,time);
304
                         drawRedPoints1D(c);
305
                         break;
306
          case DIM_2D  : drawHorizontalAxis(c,"x");
307
                         drawVerticalAxis  (c,"y");
308
                         drawPath(c,di2D,0,1,time);
309
                         drawRedPoints2D(c);
310
                         break;
311
          case DIM_3DXY: drawHorizontalAxis(c,"x");
312
                         drawVerticalAxis  (c,"y");
313
                         drawPath(c,di3D,0,1,time);
314
                         drawRedPoints3D(c);
315
                         break;
316
          case DIM_3DXZ: drawHorizontalAxis(c,"x");
317
                         drawVerticalAxis  (c,"z");
318
                         drawPath(c,di3D,0,2,time);
319
                         drawRedPoints3D(c);
320
                         break;
321
          case DIM_4DXY: drawHorizontalAxis(c,"x");
322
                         drawVerticalAxis  (c,"y");
323
                         drawPath(c,di4D,0,1,time);
324
                         drawRedPoints4D(c);
325
                         break;
326
          case DIM_4DZW: drawHorizontalAxis(c,"z");
327
                         drawVerticalAxis  (c,"w");
328
                         drawPath(c,di4D,2,3,time);
329
                         drawRedPoints4D(c);
330
                         break;
331
          case DIM_Q_XY: drawHorizontalAxis(c,"x");
332
                         drawVerticalAxis  (c,"y");
333
                         drawPath(c,diQu,0,1,time);
334
                         drawRedPointsQu(c);
335
                         break;
336
          case DIM_Q_ZW: drawHorizontalAxis(c,"z");
337
                         drawVerticalAxis  (c,"w");
338
                         drawPath(c,diQu,2,3,time);
339
                         drawRedPointsQu(c);
340
                         break;
341
          }
342
        }
343

    
344
      mLastTime = time;
345
      }
346

    
347
///////////////////////////////////////////////////////////////////////////////////////////////////
348

    
349
    private void clearPoints()
350
      {
351
      for(int i=0; i<MAX_DIM*NUM_POINTS; i++)
352
         {
353
         mPoints[i] = -100000.0f;
354
         }
355
      }
356

    
357
///////////////////////////////////////////////////////////////////////////////////////////////////
358

    
359
    public void resetPoints()
360
      {
361
      synchronized(lock)
362
        {
363
        clearPoints();
364

    
365
        switch(currentDim)
366
          {
367
          case DIM_1D  : di1D.removeAll(); break;
368
          case DIM_2D  : di2D.removeAll(); break;
369
          case DIM_3DXY:
370
          case DIM_3DXZ: di3D.removeAll(); break;
371
          case DIM_4DXY:
372
          case DIM_4DZW: di4D.removeAll(); break;
373
          case DIM_Q_XY:
374
          case DIM_Q_ZW: diQu.removeAll(); break;
375
          }
376

    
377
        DynamicActivity act = mAct.get();
378
        act.setNumRedPoints(0);
379
        act.clearPoints();
380
        }
381
      }
382

    
383
///////////////////////////////////////////////////////////////////////////////////////////////////
384

    
385
    public void startDynamic()
386
      {
387
      mRunning = true;
388
      mLastTime= -1;
389
      mStartTime = System.currentTimeMillis();
390

    
391
      clearPoints();
392
      di1D.resetToBeginning();
393
      di2D.resetToBeginning();
394
      di3D.resetToBeginning();
395
      di4D.resetToBeginning();
396
      diQu.resetToBeginning();
397
      }
398

    
399
///////////////////////////////////////////////////////////////////////////////////////////////////
400

    
401
    public void stopDynamic()
402
      {
403
      mRunning = false;
404
      }
405

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

    
408
    private void drawHorizontalAxis(Canvas c, String label)
409
      {
410
      mPaint.setColor(0xff000000);
411

    
412
      c.drawLine(0, halfScreenHeight, halfScreenWidth*2, halfScreenHeight, mPaint);
413
      c.drawText( label, 0.95f*halfScreenWidth*2, halfScreenHeight + mSizeT , mPaint);
414
      }
415

    
416

    
417
///////////////////////////////////////////////////////////////////////////////////////////////////
418

    
419
    private void drawVerticalAxis(Canvas c, String label)
420
      {
421
      mPaint.setColor(0xff000000);
422

    
423
      c.drawLine(halfScreenWidth, 0, halfScreenWidth, halfScreenHeight*2, mPaint);
424
      c.drawText(label, halfScreenWidth + mSizeT,                mSizeT , mPaint);
425
      }
426

    
427
///////////////////////////////////////////////////////////////////////////////////////////////////
428

    
429
    private void drawPath(Canvas c, Dynamic dyn, int indexH, int indexW, long time)
430
      {
431
      int len = dyn.getNumPoints();
432

    
433
      if( len>=2 )
434
        {
435
        if( mRunning )
436
          {
437
          if ( ++mPosition >= NUM_POINTS ) mPosition=0;
438

    
439
          if( dyn.getDimension()==1 )
440
            {
441
            mPoints[MAX_DIM*mPosition+indexW] = halfScreenHeight;
442
            }
443

    
444
          if( dyn.get(mPoints,MAX_DIM*mPosition, time, mDiffTime) )
445
            {
446
            stopDynamic();
447
            }
448

    
449
          addNewSpeedPoint(time);
450
          }
451

    
452
        if( currentDim!=DIM_Q_XY && currentDim!=DIM_Q_ZW )
453
          {
454
          for(int i=0; i<NUM_POINTS; i++)
455
            {
456
            int color = i<=mPosition ? 0xff - (mPosition           -i)*0xff/(NUM_POINTS-1)
457
                                     : 0xff - (mPosition+NUM_POINTS-i)*0xff/(NUM_POINTS-1);
458

    
459
            mPaint.setColor( 0xffffff + ((color&0xff)<<24) );
460
            c.drawCircle(mPoints[MAX_DIM*i+indexH], mPoints[MAX_DIM*i+indexW] , mSize1, mPaint );
461
            }
462
          }
463
        else
464
          {
465
          float x,y,min = QUAT_QUOT* (Math.min(halfScreenWidth, halfScreenHeight));
466

    
467
          for(int i=0; i<NUM_POINTS; i++)
468
            {
469
            int color = i<=mPosition ? 0xff - (mPosition           -i)*0xff/(NUM_POINTS-1)
470
                                     : 0xff - (mPosition+NUM_POINTS-i)*0xff/(NUM_POINTS-1);
471

    
472
            mPaint.setColor( 0xffffff + ((color&0xff)<<24) );
473
            x = mPoints[MAX_DIM*i+indexH]*min + halfScreenWidth;
474
            y = mPoints[MAX_DIM*i+indexW]*min + halfScreenHeight;
475
            c.drawCircle( x, y, mSize1, mPaint );
476
            }
477
          }
478
        }
479
      }
480

    
481
///////////////////////////////////////////////////////////////////////////////////////////////////
482

    
483
    private void drawRedPoints1D(Canvas c)
484
      {
485
      int len = di1D.getNumPoints();
486

    
487
      for(int curr=0; curr<len; curr++)
488
        {
489
        p1D = di1D.getPoint(curr);
490
        drawRedPoint(c,curr+"", p1D.get0(), halfScreenHeight);
491
        }
492
      }
493

    
494
///////////////////////////////////////////////////////////////////////////////////////////////////
495

    
496
    private void drawRedPoints2D(Canvas c)
497
      {
498
      int len = di2D.getNumPoints();
499

    
500
      for(int curr=0; curr<len; curr++)
501
        {
502
        p2D = di2D.getPoint(curr);
503
        drawRedPoint(c,curr+"", p2D.get0(), p2D.get1());
504
        }
505
      }
506

    
507
///////////////////////////////////////////////////////////////////////////////////////////////////
508

    
509
    private void drawRedPoints3D(Canvas c)
510
      {
511
      int len = di3D.getNumPoints();
512

    
513
      for(int curr=0; curr<len; curr++)
514
        {
515
        p3D = di3D.getPoint(curr);
516
        drawRedPoint(c,curr+"", p3D.get0(), currentDim==DIM_3DXY ? p3D.get1():p3D.get2());
517
        }
518
      }
519

    
520
///////////////////////////////////////////////////////////////////////////////////////////////////
521

    
522
    private void drawRedPoints4D(Canvas c)
523
      {
524
      int len = di4D.getNumPoints();
525

    
526
      for(int curr=0; curr<len; curr++)
527
        {
528
        p4D = di4D.getPoint(curr);
529

    
530
        if( currentDim==DIM_4DXY ) drawRedPoint(c,curr+"", p4D.get0(), p4D.get1());
531
        else                       drawRedPoint(c,curr+"", p4D.get2(), p4D.get3());
532
        }
533
      }
534

    
535
///////////////////////////////////////////////////////////////////////////////////////////////////
536

    
537
    private void drawRedPointsQu(Canvas c)
538
      {
539
      int len = diQu.getNumPoints();
540
      float x,y, min = QUAT_QUOT* (halfScreenWidth<halfScreenHeight ? halfScreenWidth:halfScreenHeight );
541

    
542
      for(int curr=0; curr<len; curr++)
543
        {
544
        pQD = diQu.getPoint(curr);
545

    
546
        if( currentDim==DIM_Q_XY )
547
          {
548
          x = pQD.get0()*min + halfScreenWidth;
549
          y = pQD.get1()*min + halfScreenHeight;
550
          }
551
        else
552
          {
553
          x = pQD.get2()*min + halfScreenWidth;
554
          y = pQD.get3()*min + halfScreenHeight;
555
          }
556

    
557
        drawRedPoint(c,curr+"", x,y);
558
        }
559
      }
560

    
561
///////////////////////////////////////////////////////////////////////////////////////////////////
562

    
563
    private void drawRedPoint(Canvas c, String label, float width, float height)
564
      {
565
      mPaint.setColor(0xffff0000);
566
      c.drawCircle( width, height, mSize2, mPaint);
567
      mPaint.setColor(0xffffffff);
568
      c.drawText(label, width,height-mFontHeight, mPaint);
569
      }
570

    
571
///////////////////////////////////////////////////////////////////////////////////////////////////
572

    
573
    private void addNewPoint(float x, float y)
574
      {
575
      float gx,gy,gz,gw;
576
      int len;
577
      int minDist = (mAvg*mAvg)/100;
578

    
579
      switch(currentDim)
580
        {
581
        case DIM_1D : len = di1D.getNumPoints();
582

    
583
                      for(int g=0; g<len; g++)
584
                        {
585
                        p1D = di1D.getPoint(g);
586
                        gx = p1D.get0();
587
                                    
588
                        if( (x-gx)*(x-gx) < minDist )
589
                          {
590
                          mMoving = g;
591
                          break;
592
                          }
593
                        }
594
                      if( mMoving <0 )
595
                        {
596
                        synchronized(lock)
597
                          {
598
                          di1D.add(new Static1D(x));
599
                          mAct.get().setNumRedPoints(len+1);
600
                          }
601
                        }
602
                      break;
603
        case DIM_2D : len = di2D.getNumPoints();
604
                                 
605
                      for(int g=0; g<len; g++)
606
                        {
607
                        p2D = di2D.getPoint(g);
608
                        gx = p2D.get0();
609
                        gy = p2D.get1();
610
                                    
611
                        if( (x-gx)*(x-gx) + (y-gy)*(y-gy) < minDist )
612
                          {
613
                          mMoving = g;
614
                          break;
615
                          }
616
                        }
617
                      if( mMoving <0 )
618
                        {
619
                        synchronized(lock)
620
                          {
621
                          di2D.add(new Static2D(x,y));
622
                          mAct.get().setNumRedPoints(len+1);
623
                          }
624
                        }
625
                      break;
626
        case DIM_3DXY:
627
        case DIM_3DXZ:len = di3D.getNumPoints();
628
                                 
629
                      for(int g=0; g<len; g++)
630
                        {
631
                        p3D = di3D.getPoint(g);
632
                        gx = p3D.get0();
633
                        gy = p3D.get1();
634
                        gz = p3D.get2();
635
                               
636
                        if( currentDim==DIM_3DXY )
637
                          {
638
                          if( (x-gx)*(x-gx) + (y-gy)*(y-gy) < minDist )
639
                            {
640
                            mMoving = g;
641
                            break;
642
                            }
643
                          }
644
                        if( currentDim==DIM_3DXZ )
645
                          {
646
                          if( (x-gx)*(x-gx) + (y-gz)*(y-gz) < minDist )
647
                            {
648
                            mMoving = g;
649
                            break;
650
                            }
651
                          }
652
                        }
653

    
654
                      if( mMoving <0 )
655
                        {
656
                        synchronized(lock)
657
                          {
658
                          if( currentDim==DIM_3DXY ) di3D.add(new Static3D(x,y, halfScreenHeight));
659
                          if( currentDim==DIM_3DXZ ) di3D.add(new Static3D(x, halfScreenHeight,y));
660
                          mAct.get().setNumRedPoints(len+1);
661
                          }
662
                        }
663
                      break;
664
        case DIM_4DXY:
665
        case DIM_4DZW:len = di4D.getNumPoints();
666

    
667
                      for(int g=0; g<len; g++)
668
                        {
669
                        p4D = di4D.getPoint(g);
670
                        gx = p4D.get0();
671
                        gy = p4D.get1();
672
                        gz = p4D.get2();
673
                        gw = p4D.get3();
674

    
675
                        if( currentDim==DIM_4DXY )
676
                          {
677
                          if( (x-gx)*(x-gx) + (y-gy)*(y-gy) < minDist )
678
                            {
679
                            mMoving = g;
680
                            break;
681
                            }
682
                          }
683
                        if( currentDim==DIM_4DZW )
684
                          {
685
                          if( (x-gz)*(x-gz) + (y-gw)*(y-gw) < minDist )
686
                            {
687
                            mMoving = g;
688
                            break;
689
                            }
690
                          }
691
                        }
692

    
693
                      if( mMoving <0 )
694
                        {
695
                        synchronized(lock)
696
                          {
697
                          if( currentDim==DIM_4DXY ) di4D.add(new Static4D(x,y, halfScreenWidth, halfScreenHeight));
698
                          if( currentDim==DIM_4DZW ) di4D.add(new Static4D( halfScreenWidth, halfScreenHeight,x,y));
699
                          mAct.get().setNumRedPoints(len+1);
700
                          }
701
                        }
702
                      break;
703
        case DIM_Q_XY:
704
        case DIM_Q_ZW:len = diQu.getNumPoints();
705
                      float min = QUAT_QUOT* (halfScreenWidth<halfScreenHeight ? halfScreenWidth:halfScreenHeight );
706

    
707
                      for(int g=0; g<len; g++)
708
                        {
709
                        pQD = diQu.getPoint(g);
710

    
711
                        if( currentDim==DIM_Q_XY )
712
                          {
713
                          gx = pQD.get0()*min + halfScreenWidth;
714
                          gy = pQD.get1()*min + halfScreenHeight;
715

    
716
                          if( (x-gx)*(x-gx) + (y-gy)*(y-gy) < minDist )
717
                            {
718
                            mMoving = g;
719
                            break;
720
                            }
721
                          }
722
                        if( currentDim==DIM_Q_ZW )
723
                          {
724
                          gz = pQD.get2()*min + halfScreenWidth;
725
                          gw = pQD.get3()*min + halfScreenHeight;
726

    
727
                          if( (x-gz)*(x-gz) + (y-gw)*(y-gw) < minDist )
728
                            {
729
                            mMoving = g;
730
                            break;
731
                            }
732
                          }
733
                        }
734

    
735
                      if( mMoving <0 )
736
                        {
737
                        float z,w;
738
                        x = (x - halfScreenWidth ) / min;
739
                        y = (y - halfScreenHeight) / min;
740
                        float len1 = x*x + y*y;
741

    
742
                        if( len1>= 1.0f )
743
                          {
744
                          float A = (float)Math.sqrt(len1);
745
                          x = x/A;
746
                          y = y/A;
747
                          z = 0.0f;
748
                          w = 0.0f;
749
                          }
750
                        else
751
                          {
752
                          z = (float)Math.sqrt(1-len1);
753
                          w = 0.0f;
754
                          }
755

    
756
                        synchronized(lock)
757
                          {
758
                          if( currentDim==DIM_Q_XY ) diQu.add(new Static4D(x,y,z,w));
759
                          if( currentDim==DIM_Q_ZW ) diQu.add(new Static4D(z,w,x,y));
760
                          mAct.get().setNumRedPoints(len+1);
761
                          }
762
                        }
763
                      break;
764
        }
765
      }
766

    
767
///////////////////////////////////////////////////////////////////////////////////////////////////
768

    
769
    private void addNewSpeedPoint(long time)
770
      {
771
      int prev = mPosition-1;
772
      if( prev<0 ) prev = NUM_POINTS-1;
773

    
774
      float xdiff = mPoints[MAX_DIM*prev  ]-mPoints[MAX_DIM*mPosition  ];
775
      float ydiff = mPoints[MAX_DIM*prev+1]-mPoints[MAX_DIM*mPosition+1];
776
      float zdiff = mPoints[MAX_DIM*prev+2]-mPoints[MAX_DIM*mPosition+2];
777
      float wdiff = mPoints[MAX_DIM*prev+3]-mPoints[MAX_DIM*mPosition+3];
778

    
779
      float dist = (float)Math.sqrt( xdiff*xdiff + ydiff*ydiff + zdiff*zdiff + wdiff*wdiff);
780
      float speed= mDiffTime<=0 ? 0: dist / mDiffTime;
781
      float timepoint = ((float)(time-mStartTime))/mDuration;
782

    
783
      if( dist<1000.0f )   // otherwise this is a very first call; do not send it!
784
        {
785
        mAct.get().addPoint( timepoint - (int)timepoint, speed );
786
        }
787
      }
788

    
789
///////////////////////////////////////////////////////////////////////////////////////////////////
790

    
791
    @Override
792
    public boolean onTouchEvent(MotionEvent event)
793
      {
794
      int action = event.getAction();
795
      float xDown, yDown;
796

    
797
      switch(action)
798
        {
799
        case MotionEvent.ACTION_DOWN: xDown = event.getX();
800
                                      yDown = event.getY();
801

    
802
                                      addNewPoint(xDown,yDown);
803

    
804
                                      break;
805
        case MotionEvent.ACTION_MOVE: if( mMoving >=0 )
806
                                        {
807
                                        xDown = event.getX();
808
                                        yDown = event.getY();
809
                                        
810
                                        switch(currentDim)
811
                                          {
812
                                          case DIM_1D  : di1D.setPoint(mMoving, xDown);
813
                                                         break;
814
                                          case DIM_2D  : di2D.setPoint(mMoving, xDown, yDown);
815
                                                         break;
816
                                          case DIM_3DXY: di3D.setPoint(mMoving, xDown, yDown, (int)di3D.getPoint(mMoving).get2());
817
                                                         break;
818
                                          case DIM_3DXZ: di3D.setPoint(mMoving, xDown, (int)di3D.getPoint(mMoving).get1(), yDown);
819
                                                         break;
820
                                          case DIM_4DXY: di4D.setPoint(mMoving, xDown, yDown, (int)di4D.getPoint(mMoving).get2(), (int)di4D.getPoint(mMoving).get3());
821
                                                         break;
822
                                          case DIM_4DZW: di4D.setPoint(mMoving, (int)di4D.getPoint(mMoving).get0(), (int)di4D.getPoint(mMoving).get1(), xDown, yDown);
823
                                                         break;
824
                                          case DIM_Q_XY: float min1 = QUAT_QUOT* (halfScreenWidth<halfScreenHeight ? halfScreenWidth:halfScreenHeight );
825
                                                         float x1 = (xDown - halfScreenWidth ) / min1;
826
                                                         float y1 = (yDown - halfScreenHeight) / min1;
827
                                                         float z1 = diQu.getPoint(mMoving).get2();
828
                                                         float w1 = diQu.getPoint(mMoving).get3();
829
                                                         float len1 = x1*x1 + y1*y1;
830

    
831
                                                         if( len1 <= 1.0f )
832
                                                           {
833
                                                           float len2 = z1*z1 + w1*w1;
834

    
835
                                                           if( len2 == 0 )
836
                                                             {
837
                                                             if( len1 == 0 )
838
                                                               {
839
                                                               w1 = 1.0f;
840
                                                               }
841
                                                             else
842
                                                               {
843
                                                               float B = (float)Math.sqrt(len1);
844
                                                               x1 = x1/B;
845
                                                               y1 = y1/B;
846
                                                               }
847
                                                             }
848
                                                           else
849
                                                             {
850
                                                             float A = (float)Math.sqrt((1.0f - len1)/len2);
851
                                                             z1 = A*z1;
852
                                                             w1 = A*w1;
853
                                                             }
854
                                                           }
855
                                                         else
856
                                                           {
857
                                                           float B = (float)Math.sqrt(len1);
858
                                                           x1 = x1/B;
859
                                                           y1 = y1/B;
860
                                                           z1 = 0.0f;
861
                                                           w1 = 0.0f;
862
                                                           }
863
                                                         diQu.setPoint(mMoving, x1,y1,z1,w1);
864
                                                         break;
865
                                          case DIM_Q_ZW: float min2 = QUAT_QUOT* (halfScreenWidth<halfScreenHeight ? halfScreenWidth:halfScreenHeight );
866
                                                         float x2 = diQu.getPoint(mMoving).get0();
867
                                                         float y2 = diQu.getPoint(mMoving).get1();
868
                                                         float z2 = (xDown - halfScreenWidth ) / min2;
869
                                                         float w2 = (yDown - halfScreenHeight) / min2;
870
                                                         float len3 = z2*z2 + w2*w2;
871

    
872
                                                         if( len3 <= 1.0f )
873
                                                           {
874
                                                           float len4 = x2*x2 + y2*y2;
875

    
876
                                                           if( len4 == 0 )
877
                                                             {
878
                                                             if( len3 == 0 )
879
                                                               {
880
                                                               w2 = 1.0f;
881
                                                               }
882
                                                             else
883
                                                               {
884
                                                               float B = (float)Math.sqrt(len3);
885
                                                               z2 = z2/B;
886
                                                               w2 = w2/B;
887
                                                               }
888
                                                             }
889
                                                           else
890
                                                             {
891
                                                             float A = (float)Math.sqrt((1.0f - len3)/len4);
892
                                                             x2 = A*x2;
893
                                                             y2 = A*y2;
894
                                                             }
895
                                                           }
896
                                                         else
897
                                                           {
898
                                                           float B = (float)Math.sqrt(len3);
899
                                                           x2 = 0.0f;
900
                                                           y2 = 0.0f;
901
                                                           z2 = z2/B;
902
                                                           w2 = w2/B;
903
                                                           }
904
                                                         diQu.setPoint(mMoving, x2,y2,z2,w2);
905
                                                         break;
906
                                          }
907
                                        }                           
908
                                      break;
909
        case MotionEvent.ACTION_UP  : mMoving = -1;
910
                                      break;
911
        }
912
            
913
      return true;
914
      }
915
  }
(4-4/4)