Project

General

Profile

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

examples / src / main / java / org / distorted / examples / interpolator / InterpolatorSurfaceView.java @ bc0a685b

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.interpolator;
21

    
22
import android.content.Context;
23
import android.opengl.GLSurfaceView;
24
import android.os.Build;
25
import android.view.MotionEvent;
26
import android.util.AttributeSet;
27
import android.graphics.Canvas;
28
import android.graphics.Paint.Style;
29
import android.graphics.Paint;
30

    
31
import org.distorted.library.Interpolator1D;
32
import org.distorted.library.Interpolator2D;
33
import org.distorted.library.Interpolator3D;
34
import org.distorted.library.Float1D;
35
import org.distorted.library.Float2D;
36
import org.distorted.library.Float3D;
37

    
38
///////////////////////////////////////////////////////////////////
39

    
40
public class InterpolatorSurfaceView extends GLSurfaceView
41
    {
42
    public static final int DIM_1D   = 0; 
43
    public static final int DIM_2D   = 1; 
44
    public static final int DIM_3DXY = 2; 
45
    public static final int DIM_3DXZ = 3; 
46
   
47
    private static final int NUM_INTERPOLATIONS= 100;
48
    private static final int MAX_VECTORS       =   6;
49
   
50
    private InterpolatorRenderer mRenderer;
51
    private static int xDown,yDown;
52
    private static int mScrW, mScrH;
53
   
54
    private static Interpolator1D di1D;
55
    private static Interpolator2D di2D;
56
    private static Interpolator3D di3D;
57
    
58
    private static Paint mPaint;
59
    private static int moving;
60
    private static Object lock = new Object();
61
    private static long mTime = 0;
62
    private static int mDuration;
63
    private static float mPosition;
64
    private static float mNoise;
65
    
66
    private static int currentDim = DIM_2D;
67
    
68
    private static Float1D p1D;
69
    private static Float2D p2D;
70
    private static Float3D p3D;
71
    
72
    private static float[] mDrawCoord = new float[3];
73
      
74
///////////////////////////////////////////////////////////////////
75
    
76
    public InterpolatorSurfaceView(Context c, AttributeSet attrs) 
77
      {
78
      super(c, attrs);
79
      
80
      mPaint = new Paint();
81
      mPaint.setStyle(Style.FILL);
82
      mPaint.setAntiAlias(true);
83
      
84
      moving    = -1;
85
      mDuration = 10000;
86
      mPosition = 0;
87
      mNoise    = 0.0f;
88
      
89
      di1D = new Interpolator1D();
90
      di1D.setDuration(mDuration);
91
      di1D.setNoise(mNoise);
92
      
93
      di2D = new Interpolator2D();
94
      di2D.setDuration(mDuration);
95
      di2D.setNoise(mNoise);
96
      
97
      di3D = new Interpolator3D();
98
      di3D.setDuration(mDuration);
99
      di3D.setNoise(mNoise);
100
        
101
      if(!isInEditMode())
102
        {
103
        setFocusable(true);
104
        setFocusableInTouchMode(true);
105
        
106
        setEGLContextClientVersion(2);
107
        
108
        if( Build.FINGERPRINT.startsWith("generic") ) // when running on the emulator, insert a magic line that is
109
          {                                           // supposed to cure the 'no config chosen' crash on emulator startup
110
          setEGLConfigChooser(8, 8, 8, 8, 16, 0);   
111
          }
112
        
113
        mRenderer = new InterpolatorRenderer(this);
114
        setRenderer(mRenderer);
115
        }
116
      }
117

    
118
///////////////////////////////////////////////////////////////////
119

    
120
    public static void setMode(int mode)
121
      {
122
      di1D.setMode(mode);  
123
      di2D.setMode(mode);
124
      di3D.setMode(mode);
125
      }
126
      
127
///////////////////////////////////////////////////////////////////
128
    
129
    public static void setScreenSize(int width, int height)
130
      {
131
      mScrW = width;
132
      mScrH = height;
133
      }
134

    
135
///////////////////////////////////////////////////////////////////
136

    
137
    public static void setDuration(int duration)
138
      {
139
      mDuration = duration;
140
      
141
      di1D.setDuration(duration);
142
      di2D.setDuration(duration);
143
      di3D.setDuration(duration);
144
      }
145

    
146
///////////////////////////////////////////////////////////////////
147

    
148
    public static void setNoise(float noise)
149
      {
150
      mNoise = noise;
151
      
152
      di1D.setNoise(noise);
153
      di2D.setNoise(noise);
154
      di3D.setNoise(noise);
155
      }
156
    
157
///////////////////////////////////////////////////////////////////
158

    
159
    public static void setDimension(int dim)
160
      {
161
      if( currentDim != dim )
162
        {
163
        if( !(currentDim==DIM_3DXY && dim==DIM_3DXZ) && !(currentDim==DIM_3DXZ && dim==DIM_3DXY) )
164
          {
165
          synchronized(lock)
166
            {
167
            di1D.removeAll();
168
            di2D.removeAll();
169
            di3D.removeAll();
170
            }
171
          }
172
      
173
        currentDim = dim;
174
        }
175
      }
176
    
177
///////////////////////////////////////////////////////////////////
178
    
179
    public static void drawCurve(Canvas c, long time)
180
      {
181
      mPosition += (mTime>0 && mDuration>0) ? ((float)(time-mTime)*NUM_INTERPOLATIONS/mDuration) : 0; 
182
         
183
      synchronized(lock)
184
        {
185
        switch(currentDim)
186
          {
187
          case DIM_1D: drawCurve1D(c,time); break;
188
          case DIM_2D: drawCurve2D(c,time); break;
189
          default    : drawCurve3D(c,time); break;
190
          }
191
        }
192
      
193
      mTime = time;
194
      }
195

    
196
///////////////////////////////////////////////////////////////////
197
      
198
    private static void drawCurve1D(Canvas c, long time)
199
      {
200
      int len = di1D.getNumPoints();   
201
        
202
      mPaint.setColor(0xff000000);
203
      
204
      c.drawLine(0, InterpolatorRenderer.BHEI/2, InterpolatorRenderer.BWID, InterpolatorRenderer.BHEI/2, mPaint);
205
      c.drawText("x", 0.95f*InterpolatorRenderer.BWID, 0.55f*InterpolatorRenderer.BHEI , mPaint);
206
      
207
      if( len>=2 )
208
        {
209
        for(int i=0; i<NUM_INTERPOLATIONS; i++) 
210
          {
211
          int color = i<=mPosition ? 0xff - ((int)mPosition                   -i)*0xff/(NUM_INTERPOLATIONS-1)  
212
                                   : 0xff - ((int)mPosition+NUM_INTERPOLATIONS-i)*0xff/(NUM_INTERPOLATIONS-1);
213
         
214
          mPaint.setColor( 0xffffff + ((color&0xff)<<24) ); 
215
          di1D.interpolate(mDrawCoord,0, (float)i/NUM_INTERPOLATIONS);
216
          c.drawCircle(mDrawCoord[0], InterpolatorRenderer.BHEI/2 , 2.0f, mPaint );
217
          }
218
        }
219
     
220
      mPaint.setColor(0xffff0000);
221
      
222
      for(int curr=0; curr<len; curr++)
223
        {      
224
        p1D = di1D.getPoint(curr);
225
        c.drawCircle(p1D.getX(), InterpolatorRenderer.BHEI/2 , 5.0f, mPaint);
226
        }   
227
      }
228
    
229
///////////////////////////////////////////////////////////////////
230
      
231
    private static void drawCurve2D(Canvas c, long time)
232
      {
233
      int len = di2D.getNumPoints();   
234
        
235
      mPaint.setColor(0xff000000);
236
      
237
      c.drawLine(0, InterpolatorRenderer.BHEI/2, InterpolatorRenderer.BWID, InterpolatorRenderer.BHEI/2, mPaint);
238
      c.drawLine(InterpolatorRenderer.BWID/2, 0, InterpolatorRenderer.BWID/2, InterpolatorRenderer.BHEI, mPaint);
239
      
240
      c.drawText("x", 0.95f*InterpolatorRenderer.BWID, 0.55f*InterpolatorRenderer.BHEI , mPaint);
241
      c.drawText("y", 0.55f*InterpolatorRenderer.BWID, 0.05f*InterpolatorRenderer.BHEI , mPaint);
242
      
243
      if( len>=2 )
244
        {
245
        for(int i=0; i<NUM_INTERPOLATIONS; i++) 
246
          {
247
          int color = i<=mPosition ? 0xff - ((int)mPosition                   -i)*0xff/(NUM_INTERPOLATIONS-1)  
248
                                   : 0xff - ((int)mPosition+NUM_INTERPOLATIONS-i)*0xff/(NUM_INTERPOLATIONS-1);
249
         
250
          mPaint.setColor( 0xffffff + ((color&0xff)<<24) ); 
251
          di2D.interpolate(mDrawCoord,0, (float)i/NUM_INTERPOLATIONS);
252
          c.drawCircle(mDrawCoord[0], mDrawCoord[1], 2.0f, mPaint );
253
          }
254
        }
255
     
256
      mPaint.setColor(0xffff0000);
257
      
258
      for(int curr=0; curr<len; curr++)
259
        {      
260
        p2D = di2D.getPoint(curr);
261
        c.drawCircle(p2D.getX(),p2D.getY(), 5.0f, mPaint);
262
        }
263
      }
264

    
265
///////////////////////////////////////////////////////////////////
266
      
267
    private static void drawCurve3D(Canvas c, long time)
268
      {
269
      int len = di3D.getNumPoints();   
270
      
271
      mPaint.setColor(0xff000000);
272
      
273
      c.drawLine(0, InterpolatorRenderer.BHEI/2, InterpolatorRenderer.BWID, InterpolatorRenderer.BHEI/2, mPaint);
274
      c.drawLine(InterpolatorRenderer.BWID/2, 0, InterpolatorRenderer.BWID/2, InterpolatorRenderer.BHEI, mPaint);
275
      
276
      c.drawText("x", 0.95f*InterpolatorRenderer.BWID, 0.55f*InterpolatorRenderer.BHEI , mPaint);
277
      c.drawText( currentDim==DIM_3DXY ? "y" : "z", 0.55f*InterpolatorRenderer.BWID, 0.05f*InterpolatorRenderer.BHEI , mPaint);
278
      
279
      if( len>=2 )
280
        {
281
        for(int i=0; i<NUM_INTERPOLATIONS; i++) 
282
          {
283
          int color = i<=mPosition ? 0xff - ((int)mPosition                   -i)*0xff/(NUM_INTERPOLATIONS-1)  
284
                                   : 0xff - ((int)mPosition+NUM_INTERPOLATIONS-i)*0xff/(NUM_INTERPOLATIONS-1);
285
         
286
          mPaint.setColor( 0xffffff + ((color&0xff)<<24) ); 
287
          di3D.interpolate(mDrawCoord,0, (float)i/NUM_INTERPOLATIONS);
288
          c.drawCircle(mDrawCoord[0], mDrawCoord[currentDim==DIM_3DXY ? 1:2], 2.0f, mPaint );
289
          }
290
        }
291
     
292
      mPaint.setColor(0xffff0000);
293
      
294
      for(int curr=0; curr<len; curr++)
295
        {      
296
        p3D = di3D.getPoint(curr);
297
        c.drawCircle(p3D.getX(), currentDim==DIM_3DXY ? p3D.getY():p3D.getZ(), 5.0f, mPaint);
298
        }   
299
      }
300
    
301
///////////////////////////////////////////////////////////////////
302

    
303
    private void addNewPoint(int x, int y)
304
      {
305
      float gx,gy,gz;
306
      int len;
307
      
308
      switch(currentDim)
309
        {
310
        case DIM_1D: len = di1D.getNumPoints();
311
                
312
                     for(int g=0; g<len; g++)
313
                       {
314
                       p1D = di1D.getPoint(g);  
315
                       gx = p1D.getX();
316
                                    
317
                       if( (xDown-gx)*(xDown-gx) < (InterpolatorRenderer.BHEI)*(InterpolatorRenderer.BHEI)/100 )
318
                         {
319
                         moving = g;
320
                         break;
321
                         }
322
                       }
323
                     if( moving<0 )
324
                       {
325
                       synchronized(lock)
326
                         {
327
                         if( len>=MAX_VECTORS ) di1D.removeAll();
328
                         di1D.add(new Float1D(xDown)); 
329
                         }
330
                       }
331
                     break;
332
        case DIM_2D: len = di2D.getNumPoints();
333
                                 
334
                     for(int g=0; g<len; g++)
335
                       {
336
                       p2D = di2D.getPoint(g);  
337
                       gx = p2D.getX();
338
                       gy = p2D.getY();
339
                                    
340
                       if( (xDown-gx)*(xDown-gx) + (yDown-gy)*(yDown-gy) < (InterpolatorRenderer.BHEI)*(InterpolatorRenderer.BHEI)/100 )
341
                         {
342
                         moving = g;
343
                         break;
344
                         }
345
                       }
346
                     if( moving<0 )
347
                       {
348
                       synchronized(lock)
349
                         {
350
                         if( len>=MAX_VECTORS ) di2D.removeAll();
351
                         di2D.add(new Float2D(xDown,yDown)); 
352
                         }
353
                       }
354
                     break;
355
        default    : len = di3D.getNumPoints();
356
                                 
357
                     for(int g=0; g<len; g++)
358
                       {
359
                       p3D = di3D.getPoint(g);  
360
                       gx = p3D.getX();
361
                       gy = p3D.getY();
362
                       gz = p3D.getZ();
363
                               
364
                     if( currentDim==DIM_3DXY )
365
                       {
366
                       if( (xDown-gx)*(xDown-gx) + (yDown-gy)*(yDown-gy) < (InterpolatorRenderer.BHEI)*(InterpolatorRenderer.BHEI)/100 )
367
                         {
368
                         moving = g;
369
                         break;
370
                         }
371
                       }
372
                     if( currentDim==DIM_3DXZ )
373
                       {
374
                       if( (xDown-gx)*(xDown-gx) + (yDown-gz)*(yDown-gz) < (InterpolatorRenderer.BHEI)*(InterpolatorRenderer.BHEI)/100 )
375
                         {
376
                         moving = g;
377
                         break;
378
                         }
379
                       }
380
                     }
381
                   if( moving<0 )
382
                     { 
383
                     synchronized(lock)
384
                       {
385
                       if( len>=MAX_VECTORS ) di3D.removeAll();
386
                    
387
                       if( currentDim==DIM_3DXY )
388
                         {
389
                         di3D.add(new Float3D(xDown,yDown,InterpolatorRenderer.BHEI/2));
390
                         }
391
                       if( currentDim==DIM_3DXZ )
392
                         {
393
                         di3D.add(new Float3D(xDown,InterpolatorRenderer.BHEI/2,yDown));
394
                         }
395
                       }
396
                     }
397
                   break; 
398
        }
399
      }
400
    
401
///////////////////////////////////////////////////////////////////
402
    
403
    @Override public boolean onTouchEvent(MotionEvent event) 
404
      {
405
      int action = event.getAction();
406
          
407
      switch(action)
408
        {
409
        case MotionEvent.ACTION_DOWN: xDown = (int)event.getX()*InterpolatorRenderer.BWID/mScrW; 
410
                                      yDown = (int)event.getY()*InterpolatorRenderer.BHEI/mScrH;
411
                                      
412
                                      addNewPoint(xDown,yDown);
413
                                    
414
                                      break;
415
        case MotionEvent.ACTION_MOVE: if( moving>=0 )
416
                                        {
417
                                        xDown = (int)event.getX()*InterpolatorRenderer.BWID/mScrW; 
418
                                        yDown = (int)event.getY()*InterpolatorRenderer.BHEI/mScrH;
419
                                        
420
                                        switch(currentDim)
421
                                          {
422
                                          case DIM_1D  : di1D.setPoint(moving, xDown); 
423
                                                         break;
424
                                          case DIM_2D  : di2D.setPoint(moving, xDown, yDown);
425
                                                         break;
426
                                          case DIM_3DXY: di3D.setPoint(moving, xDown, yDown, (int)di3D.getPoint(moving).getZ());
427
                                                         break;
428
                                          case DIM_3DXZ: di3D.setPoint(moving, xDown, (int)di3D.getPoint(moving).getY(), yDown);
429
                                                         break;
430
                                          }
431
                                        }                           
432
                                      break;
433
        case MotionEvent.ACTION_UP  : moving = -1;
434
                                      break;
435
        }
436
            
437
      return true;
438
      }
439
 
440
///////////////////////////////////////////////////////////////////
441
  }
(3-3/3)