Project

General

Profile

« Previous | Next » 

Revision f20265a7

Added by Leszek Koltunski over 4 years ago

Dynamics App: fix moving Quat Points
Library: remove negating the Quat Points in the DynamicQuat in order to always go the shortest route.

View differences:

src/main/java/org/distorted/examples/dynamic/DynamicSurfaceView.java
35 35
import org.distorted.library.type.Dynamic2D;
36 36
import org.distorted.library.type.Dynamic3D;
37 37
import org.distorted.library.type.Dynamic4D;
38
import org.distorted.library.type.DynamicQuat;
38 39
import org.distorted.library.type.Static1D;
39 40
import org.distorted.library.type.Static2D;
40 41
import org.distorted.library.type.Static3D;
......
52 53
    public static final int DIM_3DXZ = 3; 
53 54
    public static final int DIM_4DXY = 4;
54 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;
55 58

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

  
58 62
    static final int NUM_POINTS = 250;
59 63
    private static final Object lock = new Object();
......
63 67
    private static int halfScreenHeight=0;
64 68
    private static int halfScreenWidth =0;
65 69

  
66
    private Dynamic1D di1D;
67
    private Dynamic2D di2D;
68
    private Dynamic3D di3D;
69
    private Dynamic4D di4D;
70
    private Dynamic1D   di1D;
71
    private Dynamic2D   di2D;
72
    private Dynamic3D   di3D;
73
    private Dynamic4D   di4D;
74
    private DynamicQuat diQu;
70 75
    
71 76
    private Paint mPaint;
72 77
    private int mMoving;
......
85 90
    private Static2D p2D;
86 91
    private Static3D p3D;
87 92
    private Static4D p4D;
93
    private Static4D pQD;
88 94

  
89 95
    private Static1D p1N;
90 96
    private Static2D p2N;
91 97
    private Static3D p3N;
92 98
    private Static4D p4N;
99
    private Static4D pQN;
93 100

  
94 101
    private float[] mPoints = new float[MAX_DIM*NUM_POINTS];
95 102
    private boolean mRunning;
......
128 135
      p3N  = new Static3D(mNoise[0],mNoise[1],mNoise[2]);
129 136
      di4D = new Dynamic4D(mDuration,mCount);
130 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]);
131 140

  
132 141
      di1D.setAccessType(Dynamic.ACCESS_TYPE_SEQUENTIAL);
133 142
      di2D.setAccessType(Dynamic.ACCESS_TYPE_SEQUENTIAL);
134 143
      di3D.setAccessType(Dynamic.ACCESS_TYPE_SEQUENTIAL);
135 144
      di4D.setAccessType(Dynamic.ACCESS_TYPE_SEQUENTIAL);
145
      diQu.setAccessType(Dynamic.ACCESS_TYPE_SEQUENTIAL);
136 146

  
137 147
      if(!isInEditMode())
138 148
        {
......
189 199
      di2D.setMode(mode);
190 200
      di3D.setMode(mode);
191 201
      di4D.setMode(mode);
202
      diQu.setMode(mode);
192 203
      }
193 204

  
194 205
///////////////////////////////////////////////////////////////////////////////////////////////////
......
201 212
      di2D.setDuration(duration);
202 213
      di3D.setDuration(duration);
203 214
      di4D.setDuration(duration);
215
      diQu.setDuration(duration);
204 216
      }
205 217

  
206 218
///////////////////////////////////////////////////////////////////////////////////////////////////
......
213 225
      di2D.setCount(count);
214 226
      di3D.setCount(count);
215 227
      di4D.setCount(count);
228
      diQu.setCount(count);
216 229
      }
217 230

  
218 231
///////////////////////////////////////////////////////////////////////////////////////////////////
......
223 236
      di2D.setConvexity(convexity);
224 237
      di3D.setConvexity(convexity);
225 238
      di4D.setConvexity(convexity);
239
      diQu.setConvexity(convexity);
226 240
      }
227 241

  
228 242
///////////////////////////////////////////////////////////////////////////////////////////////////
243
// DynamicQuat does not support noise
229 244

  
230 245
    public void setNoise(float noise0, float noise1, float noise2, float noise3)
231 246
      {
......
252 267
      if( currentDim != dim )
253 268
        {
254 269
        if( !(currentDim==DIM_3DXY && dim==DIM_3DXZ) && !(currentDim==DIM_3DXZ && dim==DIM_3DXY) &&
255
            !(currentDim==DIM_4DXY && dim==DIM_4DZW) && !(currentDim==DIM_4DZW && dim==DIM_4DXY)  )
270
            !(currentDim==DIM_4DXY && dim==DIM_4DZW) && !(currentDim==DIM_4DZW && dim==DIM_4DXY) &&
271
            !(currentDim==DIM_Q_XY && dim==DIM_Q_ZW) && !(currentDim==DIM_Q_ZW && dim==DIM_Q_XY)  )
256 272
          {
257 273
          resetPoints();
258 274
          }
......
307 323
                         drawPath(c,di4D,2,3,time);
308 324
                         drawRedPoints4D(c);
309 325
                         break;
326
          case DIM_Q_XY: drawHorizontalAxis(c,"x");
327
                         drawVerticalAxis  (c,"y");
328
                         drawPath(c,diQu,0,1,time);
329
                         drawRedPointsQu(c);
330
                         break;
331
          case DIM_Q_ZW: drawHorizontalAxis(c,"z");
332
                         drawVerticalAxis  (c,"w");
333
                         drawPath(c,diQu,2,3,time);
334
                         drawRedPointsQu(c);
335
                         break;
310 336
          }
311 337
        }
312 338

  
......
339 365
          case DIM_3DXZ: di3D.removeAll(); break;
340 366
          case DIM_4DXY:
341 367
          case DIM_4DZW: di4D.removeAll(); break;
368
          case DIM_Q_XY:
369
          case DIM_Q_ZW: diQu.removeAll(); break;
342 370
          }
343 371

  
344 372
        DynamicActivity act = mAct.get();
......
360 388
      di2D.resetToBeginning();
361 389
      di3D.resetToBeginning();
362 390
      di4D.resetToBeginning();
391
      diQu.resetToBeginning();
363 392
      }
364 393

  
365 394
///////////////////////////////////////////////////////////////////////////////////////////////////
......
415 444
          addNewSpeedPoint(time);
416 445
          }
417 446

  
418
        for(int i=0; i<NUM_POINTS; i++)
447
        if( currentDim!=DIM_Q_XY && currentDim!=DIM_Q_ZW )
448
          {
449
          for(int i=0; i<NUM_POINTS; i++)
450
            {
451
            int color = i<=mPosition ? 0xff - (mPosition           -i)*0xff/(NUM_POINTS-1)
452
                                     : 0xff - (mPosition+NUM_POINTS-i)*0xff/(NUM_POINTS-1);
453

  
454
            mPaint.setColor( 0xffffff + ((color&0xff)<<24) );
455
            c.drawCircle(mPoints[MAX_DIM*i+indexH], mPoints[MAX_DIM*i+indexW] , mSize1, mPaint );
456
            }
457
          }
458
        else
419 459
          {
420
          int color = i<=mPosition ? 0xff - (mPosition           -i)*0xff/(NUM_POINTS-1)
421
                                   : 0xff - (mPosition+NUM_POINTS-i)*0xff/(NUM_POINTS-1);
460
          float x,y,min = QUAT_QUOT* (halfScreenWidth<halfScreenHeight ? halfScreenWidth:halfScreenHeight );
422 461

  
423
          mPaint.setColor( 0xffffff + ((color&0xff)<<24) );
424
          c.drawCircle(mPoints[MAX_DIM*i+indexH], mPoints[MAX_DIM*i+indexW] , mSize1, mPaint );
462
          for(int i=0; i<NUM_POINTS; i++)
463
            {
464
            int color = i<=mPosition ? 0xff - (mPosition           -i)*0xff/(NUM_POINTS-1)
465
                                     : 0xff - (mPosition+NUM_POINTS-i)*0xff/(NUM_POINTS-1);
466

  
467
            mPaint.setColor( 0xffffff + ((color&0xff)<<24) );
468
            x = mPoints[MAX_DIM*i+indexH]*min + halfScreenWidth;
469
            y = mPoints[MAX_DIM*i+indexW]*min + halfScreenHeight;
470
            c.drawCircle( x, y, mSize1, mPaint );
471
            }
425 472
          }
426 473
        }
427 474
      }
......
480 527
        }
481 528
      }
482 529

  
530
///////////////////////////////////////////////////////////////////////////////////////////////////
531

  
532
    private void drawRedPointsQu(Canvas c)
533
      {
534
      int len = diQu.getNumPoints();
535
      float x,y, min = QUAT_QUOT* (halfScreenWidth<halfScreenHeight ? halfScreenWidth:halfScreenHeight );
536

  
537
      for(int curr=0; curr<len; curr++)
538
        {
539
        pQD = diQu.getPoint(curr);
540

  
541
        if( currentDim==DIM_Q_XY )
542
          {
543
          x = pQD.get1()*min + halfScreenWidth;
544
          y = pQD.get2()*min + halfScreenHeight;
545
          }
546
        else
547
          {
548
          x = pQD.get3()*min + halfScreenWidth;
549
          y = pQD.get4()*min + halfScreenHeight;
550
          }
551

  
552
        drawRedPoint(c,curr+"", x,y);
553
        }
554
      }
555

  
483 556
///////////////////////////////////////////////////////////////////////////////////////////////////
484 557

  
485 558
    private void drawRedPoint(Canvas c, String label, float width, float height)
......
492 565

  
493 566
///////////////////////////////////////////////////////////////////////////////////////////////////
494 567

  
495
    private void addNewPoint(int x, int y)
568
    private void addNewPoint(float x, float y)
496 569
      {
497 570
      float gx,gy,gz,gw;
498 571
      int len;
......
622 695
                          }
623 696
                        }
624 697
                      break;
698
        case DIM_Q_XY:
699
        case DIM_Q_ZW:len = diQu.getNumPoints();
700
                      float min = QUAT_QUOT* (halfScreenWidth<halfScreenHeight ? halfScreenWidth:halfScreenHeight );
701

  
702
                      for(int g=0; g<len; g++)
703
                        {
704
                        pQD = diQu.getPoint(g);
705

  
706
                        if( currentDim==DIM_Q_XY )
707
                          {
708
                          gx = pQD.get1()*min + halfScreenWidth;
709
                          gy = pQD.get2()*min + halfScreenHeight;
710

  
711
                          if( (x-gx)*(x-gx) + (y-gy)*(y-gy) < minDist )
712
                            {
713
                            mMoving = g;
714
                            break;
715
                            }
716
                          }
717
                        if( currentDim==DIM_Q_ZW )
718
                          {
719
                          gz = pQD.get3()*min + halfScreenWidth;
720
                          gw = pQD.get4()*min + halfScreenHeight;
721

  
722
                          if( (x-gz)*(x-gz) + (y-gw)*(y-gw) < minDist )
723
                            {
724
                            mMoving = g;
725
                            break;
726
                            }
727
                          }
728
                        }
729

  
730
                      if( mMoving <0 )
731
                        {
732
                        float z,w;
733
                        x = (x - halfScreenWidth ) / min;
734
                        y = (y - halfScreenHeight) / min;
735
                        float len1 = x*x + y*y;
736

  
737
                        if( len1>= 1.0f )
738
                          {
739
                          float A = (float)Math.sqrt(len1);
740
                          x = x/A;
741
                          y = y/A;
742
                          z = 0.0f;
743
                          w = 0.0f;
744
                          }
745
                        else
746
                          {
747
                          z = (float)Math.sqrt(1-len1);
748
                          w = 0.0f;
749
                          }
750

  
751
                        synchronized(lock)
752
                          {
753
                          if( currentDim==DIM_Q_XY ) diQu.add(new Static4D(x,y,z,w));
754
                          if( currentDim==DIM_Q_ZW ) diQu.add(new Static4D(z,w,x,y));
755
                          mAct.get().setNumRedPoints(len+1);
756
                          }
757
                        }
758
                      break;
625 759
        }
626 760
      }
627
    
761

  
628 762
///////////////////////////////////////////////////////////////////////////////////////////////////
629 763

  
630 764
    private void addNewSpeedPoint(long time)
......
653 787
    public boolean onTouchEvent(MotionEvent event)
654 788
      {
655 789
      int action = event.getAction();
656
      int xDown, yDown;
790
      float xDown, yDown;
657 791

  
658 792
      switch(action)
659 793
        {
660
        case MotionEvent.ACTION_DOWN: xDown = (int)event.getX();
661
                                      yDown = (int)event.getY();
662
                                      
794
        case MotionEvent.ACTION_DOWN: xDown = event.getX();
795
                                      yDown = event.getY();
796

  
663 797
                                      addNewPoint(xDown,yDown);
664
                                    
798

  
665 799
                                      break;
666 800
        case MotionEvent.ACTION_MOVE: if( mMoving >=0 )
667 801
                                        {
668
                                        xDown = (int)event.getX();
669
                                        yDown = (int)event.getY();
802
                                        xDown = event.getX();
803
                                        yDown = event.getY();
670 804
                                        
671 805
                                        switch(currentDim)
672 806
                                          {
......
682 816
                                                         break;
683 817
                                          case DIM_4DZW: di4D.setPoint(mMoving, (int)di4D.getPoint(mMoving).get1(), (int)di4D.getPoint(mMoving).get2(), xDown, yDown);
684 818
                                                         break;
819
                                          case DIM_Q_XY: float min1 = QUAT_QUOT* (halfScreenWidth<halfScreenHeight ? halfScreenWidth:halfScreenHeight );
820
                                                         float x1 = (xDown - halfScreenWidth ) / min1;
821
                                                         float y1 = (yDown - halfScreenHeight) / min1;
822
                                                         float z1 = diQu.getPoint(mMoving).get3();
823
                                                         float w1 = diQu.getPoint(mMoving).get4();
824
                                                         float len1 = x1*x1 + y1*y1;
825

  
826
                                                         if( len1 <= 1.0f )
827
                                                           {
828
                                                           float len2 = z1*z1 + w1*w1;
829

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

  
867
                                                         if( len3 <= 1.0f )
868
                                                           {
869
                                                           float len4 = x2*x2 + y2*y2;
870

  
871
                                                           if( len4 == 0 )
872
                                                             {
873
                                                             if( len3 == 0 )
874
                                                               {
875
                                                               w2 = 1.0f;
876
                                                               }
877
                                                             else
878
                                                               {
879
                                                               float B = (float)Math.sqrt(len3);
880
                                                               z2 = z2/B;
881
                                                               w2 = w2/B;
882
                                                               }
883
                                                             }
884
                                                           else
885
                                                             {
886
                                                             float A = (float)Math.sqrt((1.0f - len3)/len4);
887
                                                             x2 = A*x2;
888
                                                             y2 = A*y2;
889
                                                             }
890
                                                           }
891
                                                         else
892
                                                           {
893
                                                           float B = (float)Math.sqrt(len3);
894
                                                           x2 = 0.0f;
895
                                                           y2 = 0.0f;
896
                                                           z2 = z2/B;
897
                                                           w2 = w2/B;
898
                                                           }
899
                                                         diQu.setPoint(mMoving, x2,y2,z2,w2);
900
                                                         break;
685 901
                                          }
686 902
                                        }                           
687 903
                                      break;

Also available in: Unified diff