Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshPolygon.java @ 1b5358fb

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10
//                                                                                               //
11
// This library 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 GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

    
21
package org.distorted.library.mesh;
22

    
23
///////////////////////////////////////////////////////////////////////////////////////////////////
24

    
25
import org.distorted.library.main.DistortedLibrary;
26

    
27
/**
28
 * Create a polygon of any shape and varying elevations from the edges towards the center.
29
 * <p>
30
 * Specify a list of vertices. Any two adjacent vertices + the center (0,0,0) form a triangle. The
31
 * polygon is going to be split into such triangles, and each triangle is split into adjustable number
32
 * of 'bands' form the outer edge towards the center. Edges of each band can can at any elevation.
33
 */
34
public class MeshPolygon extends MeshBase
35
  {
36
  private static final int NUM_CACHE = 20;
37
  private static final int SHAPE_DD = 0;
38
  private static final int SHAPE_DU = 1;
39
  private static final int SHAPE_UD = 2;
40
  private static final int SHAPE_UU = 3;
41
  private static final int SHAPE_DUD= 4;
42

    
43
  private float[] mPolygonVertices;
44
  private int mNumPolygonVertices;
45
  private float[] mPolygonBands;
46
  private int mNumPolygonBands;
47
  private boolean[] mEdgeUp;
48
  private boolean[] mVertUp;
49

    
50
  private int remainingVert;
51
  private int numVertices;
52
  private int extraIndex, extraVertices;
53

    
54
  private float[] mCurveCache;
55
  private float mVecX, mVecY;
56
  private int[] mEdgeShape;
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59
// polygonVertices>=3 , polygonBands>=2
60

    
61
  private void computeNumberOfVertices()
62
     {
63
     if( mNumPolygonBands==2 && extraIndex>0 )
64
       {
65
       numVertices = 1 + 2*mNumPolygonVertices*(1+extraIndex+2*extraVertices);
66
       }
67
     else
68
       {
69
       numVertices = (mNumPolygonVertices*mNumPolygonBands+2)*(mNumPolygonBands-1) - 1;
70
       numVertices+= 2*mNumPolygonVertices*(2*extraIndex*extraVertices);
71
       }
72

    
73
     remainingVert = numVertices;
74
     }
75

    
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77

    
78
  private void computeCache()
79
    {
80
    mCurveCache = new float[NUM_CACHE];
81
    float[] tmpD = new float[mNumPolygonBands+1];
82
    float[] tmpX = new float[mNumPolygonBands+1];
83

    
84
    for(int i=1; i<mNumPolygonBands; i++)
85
      {
86
      tmpD[i] = (mPolygonBands[2*i-1]-mPolygonBands[2*i+1]) / (mPolygonBands[2*i]-mPolygonBands[2*i-2]);
87
      tmpX[i] = 1.0f - (mPolygonBands[2*i]+mPolygonBands[2*i-2])/2;
88
      }
89

    
90
    tmpD[0] = tmpD[1];
91
    tmpD[mNumPolygonBands] = tmpD[mNumPolygonBands-1];
92
    tmpX[0] = 0.0f;
93
    tmpX[mNumPolygonBands] = 1.0f;
94

    
95
    int prev = 0;
96
    int next = 0;
97

    
98
    for(int i=0; i<NUM_CACHE-1; i++)
99
      {
100
      float x = i/(NUM_CACHE-1.0f);
101

    
102
      if( x>=tmpX[next] )
103
        {
104
        prev = next;
105
        while( next<=mNumPolygonBands && x>=tmpX[next] ) next++;
106
        }
107

    
108
      if( next>prev )
109
        {
110
        float t = (x-tmpX[prev]) / (tmpX[next]-tmpX[prev]);
111
        mCurveCache[i] = t*(tmpD[next]-tmpD[prev]) + tmpD[prev];
112
        }
113
      else
114
        {
115
        mCurveCache[i] = tmpD[next];
116
        }
117
      }
118

    
119
    mCurveCache[NUM_CACHE-1] = tmpD[mNumPolygonBands];
120
    }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

    
124
  private float getSpecialQuot(int index)
125
    {
126
    int num = 1 + extraIndex + 2*extraVertices;
127
    int change1 = extraVertices+1;
128
    int change2 = num-change1;
129
    float quot = 1.0f/(extraIndex+1);
130

    
131
    if( index<change1 )      return index*quot/change1;
132
    else if( index>change2 ) return 1-quot + (index-change2)*quot/change1;
133
    else                     return (index-change1+1)*quot;
134
    }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137

    
138
  private float getQuot(int index, int band, boolean isExtra)
139
    {
140
    int num = mNumPolygonBands-1-band;
141

    
142
    if( num>0 )
143
      {
144
      if( isExtra )
145
        {
146
        int extra = extraIndex-band+extraVertices;
147

    
148
        if( index < extra )
149
          {
150
          float quot = ((float)extraIndex-band)/(extra*num);
151
          return index*quot;
152
          }
153
        else if( index > num+2*extraVertices-extra )
154
          {
155
          float quot = ((float)extraIndex-band)/(extra*num);
156
          return (1.0f-((float)extraIndex-band)/num) + (index-num-2*extraVertices+extra)*quot;
157
          }
158
        else
159
          {
160
          return ((float)(index-extraVertices))/num;
161
          }
162
        }
163

    
164
      return (float)index/num;
165
      }
166

    
167
    return 1.0f;
168
    }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

    
172
  private float computeZEdge(float quot)
173
    {
174
    if( quot>=1.0f ) return 0.0f;
175

    
176
    for(int band=1; band<mNumPolygonBands; band++)
177
      {
178
      float curr = mPolygonBands[2*band];
179

    
180
      if( curr<=quot )
181
        {
182
        float prev = mPolygonBands[2*band-2];
183
        float prevH= mPolygonBands[2*band-1];
184
        float currH= mPolygonBands[2*band+1];
185

    
186
        float A = (prev-quot)/(prev-curr);
187

    
188
        return A*currH + (1-A)*prevH;
189
        }
190
      }
191

    
192
    return 0.0f;
193
    }
194

    
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196

    
197
  private float derivative(float x)
198
    {
199
    if( x>=1.0f )
200
      {
201
      return 0.0f;
202
      }
203
    else
204
      {
205
      float tmp = x*(NUM_CACHE-1);
206
      int i1 = (int)tmp;
207
      int i2 = i1+1;
208

    
209
      // why 0.5? Arbitrarily; this way the cubit faces of Twisty Puzzles
210
      // [the main and only user of this class] look better.
211
      return 0.5f*((tmp-i1)*(mCurveCache[i2]-mCurveCache[i1]) + mCurveCache[i1]);
212
      }
213
    }
214

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

    
217
  private void figureOutNormalVector2D(int edgeShape, int polyVertex, int polyEndVer, float quot, float xEdge, float yEdge )
218
    {
219
    if( edgeShape==SHAPE_DD )
220
      {
221
      if( quot<0.5f )
222
        {
223
        int p = polyVertex>0 ? polyVertex-1 : mNumPolygonVertices-1;
224
        int prvShape = mEdgeShape[p];
225

    
226
        switch(prvShape)
227
          {
228
          case SHAPE_DD : mVecX = xEdge;
229
                          mVecY = yEdge;
230
                          break;
231
          case SHAPE_UD :
232
          case SHAPE_DUD: float a = 2*quot;
233
                          float xCurr= mPolygonVertices[2*polyVertex  ];
234
                          float yCurr= mPolygonVertices[2*polyVertex+1];
235
                          float xPrev= mPolygonVertices[2*p  ];
236
                          float yPrev= mPolygonVertices[2*p+1];
237
                          float xVec = xCurr-xPrev;
238
                          float yVec = yCurr-yPrev;
239

    
240
                          mVecX = a*xEdge + (1-a)*xVec;
241
                          mVecY = a*yEdge + (1-a)*yVec;
242

    
243
                          break;
244
          default: throw new RuntimeException("figureOutNormalVector2D: impossible1: "+prvShape);
245
          }
246
        }
247
      else
248
        {
249
        int n = polyEndVer==mNumPolygonVertices-1 ? 0 : polyEndVer+1;
250
        int nxtShape = mEdgeShape[polyEndVer];
251

    
252
        switch(nxtShape)
253
          {
254
          case SHAPE_DD : mVecX = xEdge;
255
                          mVecY = yEdge;
256
                          break;
257
          case SHAPE_DU :
258
          case SHAPE_DUD: float a = 2-2*quot;
259
                          float xCurr= mPolygonVertices[2*polyEndVer  ];
260
                          float yCurr= mPolygonVertices[2*polyEndVer+1];
261
                          float xNext= mPolygonVertices[2*n  ];
262
                          float yNext= mPolygonVertices[2*n+1];
263
                          float xVec = xCurr-xNext;
264
                          float yVec = yCurr-yNext;
265

    
266
                          mVecX = a*xEdge + (1-a)*xVec;
267
                          mVecY = a*yEdge + (1-a)*yVec;
268
                          break;
269
          default: throw new RuntimeException("figureOutNormalVector2D: impossible2: "+nxtShape);
270
          }
271
        }
272
      }
273
    else if( edgeShape==SHAPE_UU || (quot>=0.5f && edgeShape==SHAPE_DU) || (quot<0.5f && edgeShape==SHAPE_UD) )
274
      {
275
      mVecX = 1;
276
      mVecY = 0;
277
      }
278
    else
279
      {
280
      float dx = mPolygonVertices[2*polyVertex  ] - mPolygonVertices[2*polyEndVer  ];
281
      float dy = mPolygonVertices[2*polyVertex+1] - mPolygonVertices[2*polyEndVer+1];
282

    
283
      if( quot<0.5 )
284
        {
285
        mVecX = dx;
286
        mVecY = dy;
287
        }
288
      else
289
        {
290
        mVecX = -dx;
291
        mVecY = -dy;
292
        }
293
      }
294
    }
295

    
296
///////////////////////////////////////////////////////////////////////////////////////////////////
297

    
298
  private float figureOutDerivative(int edgeShape, int polyBand, float quot)
299
    {
300
    if( edgeShape==SHAPE_DD )
301
      {
302
      return derivative(1-mPolygonBands[2*polyBand]);
303
      }
304
    if( edgeShape==SHAPE_UU || (quot>=0.5f && edgeShape==SHAPE_DU) || (quot<0.5f && edgeShape==SHAPE_UD) )
305
      {
306
      return 0;
307
      }
308

    
309
    float x = 1-mPolygonBands[2*polyBand];
310
    float q = quot>=0.5f ? 2-2*quot : 2*quot;
311
    return derivative((1-x)*q+x);
312
    }
313

    
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315

    
316
  private int addVertex(int vertex, int polyBand, int polyVertex, int polyEndVer, float quot,
317
                        float[] attribs1, float[] attribs2)
318
    {
319
    remainingVert--;
320

    
321
    float Xfirst= mPolygonVertices[2*polyVertex  ];
322
    float Yfirst= mPolygonVertices[2*polyVertex+1];
323
    float Xlast = mPolygonVertices[2*polyEndVer  ];
324
    float Ylast = mPolygonVertices[2*polyEndVer+1];
325

    
326
    float xEdge = Xfirst + quot*(Xlast-Xfirst);
327
    float yEdge = Yfirst + quot*(Ylast-Yfirst);
328
    float zEdge;
329

    
330
    float q = mPolygonBands[2*polyBand];
331
    float o = mPolygonBands[2*polyBand+1];
332
    float t = mPolygonBands[2*mNumPolygonBands-1];
333

    
334
    int shape = mEdgeShape[polyVertex];
335

    
336
    switch(shape)
337
      {
338
      case SHAPE_DD : zEdge = 0.0f; break;
339
      case SHAPE_UU : zEdge = t;    break;
340
      case SHAPE_DU : zEdge = quot>=0.5f ? t : computeZEdge(1-2*quot); break;
341
      case SHAPE_UD : zEdge = quot<=0.5f ? t : computeZEdge(2*quot-1); break;
342
      default       : zEdge = quot<=0.5f ? computeZEdge(1-2*quot) : computeZEdge(2*quot-1); break;
343
      }
344

    
345
    float x = q*xEdge;
346
    float y = q*yEdge;
347
    float z = o + (t-o)*(zEdge/t);
348

    
349
    attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB  ] = x;
350
    attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] = y;
351
    attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = z;
352

    
353
    figureOutNormalVector2D(shape, polyVertex, polyEndVer, quot, xEdge, yEdge);
354
    float d = figureOutDerivative(shape,polyBand,quot);
355

    
356
    float vx = d*mVecX;
357
    float vy = d*mVecY;
358
    float vz = mVecX*mVecX + mVecY*mVecY;
359
    float len = (float)Math.sqrt(vx*vx + vy*vy + vz*vz);
360

    
361
    int index = VERT1_ATTRIBS*vertex + NOR_ATTRIB;
362
    attribs1[index  ] = vx/len;
363
    attribs1[index+1] = vy/len;
364
    attribs1[index+2] = vz/len;
365

    
366
    attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB  ] = x+0.5f;
367
    attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = y+0.5f;
368

    
369
    return vertex+1;
370
    }
371

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

    
374
  private int createBandStrip(int vertex, int polyBand, int polyVertex, float[] attribs1, float[] attribs2)
375
    {
376
    if( polyVertex==0 )
377
      {
378
      vertex = addVertex(vertex,polyBand,0,1,0,attribs1,attribs2);
379
      if( polyBand>0 ) vertex = addVertex(vertex,polyBand,0,1,0,attribs1,attribs2);
380
      }
381

    
382
    boolean specialCase = mNumPolygonBands==2 && polyBand==0 && extraIndex>0;
383
    boolean isExtra = polyBand<extraIndex;
384
    int numPairs = specialCase ? extraIndex+1 : mNumPolygonBands-1-polyBand;
385
    if( isExtra ) numPairs += 2*extraVertices;
386

    
387
    int polyEndVer = polyVertex==mNumPolygonVertices-1 ? 0 : polyVertex+1;
388
    float quot1, quot2;
389

    
390
    for(int index=0; index<numPairs; index++)
391
      {
392
      if( specialCase )
393
        {
394
        quot1 = 1.0f;
395
        quot2 = getSpecialQuot(index+1);
396
        }
397
      else
398
        {
399
        quot1 = getQuot(index  ,polyBand+1, isExtra);
400
        quot2 = getQuot(index+1,polyBand  , isExtra);
401
        }
402

    
403
      vertex = addVertex(vertex,polyBand+1,polyVertex,polyEndVer,quot1,attribs1,attribs2);
404
      vertex = addVertex(vertex,polyBand  ,polyVertex,polyEndVer,quot2,attribs1,attribs2);
405
      }
406

    
407
    return vertex;
408
    }
409

    
410
///////////////////////////////////////////////////////////////////////////////////////////////////
411

    
412
  private int computeEdgeShape(int curr)
413
    {
414
    if( mEdgeUp==null || !mEdgeUp[curr] ) return SHAPE_DD;
415

    
416
    int prev = (curr==0 ? mNumPolygonVertices-1 : curr-1);
417
    int next = (curr==mNumPolygonVertices-1 ? 0 : curr+1);
418

    
419
    boolean rd = ( !mEdgeUp[next] || mVertUp==null || !mVertUp[next] );
420
    boolean ld = ( !mEdgeUp[prev] || mVertUp==null || !mVertUp[curr] );
421

    
422
    return rd ? (ld ? SHAPE_DUD : SHAPE_UD) : (ld ? SHAPE_DU : SHAPE_UU);
423
    }
424

    
425
///////////////////////////////////////////////////////////////////////////////////////////////////
426

    
427
  private void buildGrid(float[] attribs1, float[] attribs2)
428
    {
429
    int vertex=0;
430

    
431
    mEdgeShape = new int[mNumPolygonVertices];
432

    
433
    for(int polyVertex=0; polyVertex<mNumPolygonVertices; polyVertex++)
434
      mEdgeShape[polyVertex] = computeEdgeShape(polyVertex);
435

    
436
    for(int polyBand=0; polyBand<mNumPolygonBands-1; polyBand++)
437
      for(int polyVertex=0; polyVertex<mNumPolygonVertices; polyVertex++)
438
        vertex = createBandStrip(vertex,polyBand,polyVertex,attribs1,attribs2);
439
    }
440

    
441
///////////////////////////////////////////////////////////////////////////////////////////////////
442
// PUBLIC API
443
///////////////////////////////////////////////////////////////////////////////////////////////////
444
/**
445
 * Create a polygon of any shape and varying elevations from the edges towards the center.
446
 * Optionally make it more dense at the vertices.
447
 *
448
 * @param verticesXY 2N floats - packed description of polygon vertices. N pairs (x,y).
449
 *                   Vertices HAVE TO be specified in a COUNTERCLOCKWISE order (starting from any).
450
 * @param bands      2K floats; K pairs of two floats each describing a single band.
451
 *                   From (1.0,Z[0]) (outer edge, its Z elevation) to (0.0,Z[K]) (the center,
452
 *                   its elevation). The polygon is split into such concentric bands.
453
 *                   Must be band[2*i] > band[2*(i+1)] !
454
 * @param edgeUp     N booleans - one for each edge; the first connects vertices (0,1) and (2,3);
455
 *                   then just like 'verticesXY', i.e. counterclockwise.
456
 *                   If this is null, all edges are by default 'down'.
457
 *                   'Down' means that edge's Z is equal to 0; 'up' means that its Z, at least in its
458
 *                   middle, is equal to the highest elevation in the middle of the mesh.
459
 *                   If the 'previous' edge is also up, then the Z is up horizontally from its middle
460
 *                   to the left vertex; else it goes down along the same function it goes along the bands.
461
 *                   Same with the right half of the edge.
462
 * @param vertUp     N booleans - one for each vertex. The first is about vertex 0, then vertex 1, etc.
463
 *                   If this is null or false, the vertex is down; otheerwise - it is 'up'.
464
 *                   This is taken into account only in one case: if both edges the vertex is the end of
465
 *                   are 'up', then the vertex can be 'up' or 'down'; otherwise - if at least one of
466
 *                   those edges is 'down' - then the vertex must be 'down' as well and this is ignored.
467
 * @param exIndex    This and the next parameter describe how to make the mesh denser at the
468
 *                   polyVertices. If e.g. exIndex=3 and exVertices=2, then 3 triangles of the
469
 *                   outermost band (and 2 triangles of the next band, and 1 triangle of the third
470
 *                   band) get denser - the 3 triangles become 3+2 = 5.
471
 * @param exVertices See above.
472
 * @param centerX    the X coordinate of the 'center' of the Polygon, i.e. point of the mesh
473
 *                   all bands go to.
474
 * @param centerY    Y coordinate of the center.
475
 */
476
  public MeshPolygon(float[] verticesXY, float[] bands, boolean[] edgeUp, boolean[] vertUp, int exIndex, int exVertices, float centerX, float centerY)
477
    {
478
    super();
479

    
480
    mPolygonVertices   = verticesXY;
481
    mPolygonBands      = bands;
482
    mNumPolygonVertices= mPolygonVertices.length /2;
483
    mNumPolygonBands   = mPolygonBands.length /2;
484
    mEdgeUp            = edgeUp;
485
    mVertUp            = vertUp;
486
    extraIndex         = exIndex;
487
    extraVertices      = exVertices;
488

    
489
    if( centerX!=0.0f || centerY!=0.0f )
490
      {
491
      for(int v=0; v<mNumPolygonVertices; v++)
492
        {
493
        mPolygonVertices[2*v  ] -= centerX;
494
        mPolygonVertices[2*v+1] -= centerY;
495
        }
496
      }
497

    
498
    computeNumberOfVertices();
499
    computeCache();
500

    
501
    float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
502
    float[] attribs2= new float[VERT2_ATTRIBS*numVertices];
503

    
504
    buildGrid(attribs1,attribs2);
505

    
506
    if( remainingVert!=0 )
507
      DistortedLibrary.logMessage("MeshPolygon: remainingVert " +remainingVert );
508

    
509
    if( centerX!=0.0f || centerY!=0.0f )
510
      {
511
      for(int v=0; v<numVertices; v++)
512
        {
513
        attribs1[VERT1_ATTRIBS*v + POS_ATTRIB  ] += centerX;
514
        attribs1[VERT1_ATTRIBS*v + POS_ATTRIB+1] += centerY;
515
        attribs2[VERT2_ATTRIBS*v + TEX_ATTRIB  ] += centerX;
516
        attribs2[VERT2_ATTRIBS*v + TEX_ATTRIB+1] += centerY;
517
        }
518
      }
519

    
520
    setAttribs(attribs1,attribs2);
521
    }
522

    
523
///////////////////////////////////////////////////////////////////////////////////////////////////
524

    
525
  public MeshPolygon(float[] verticesXY, float[] bands, int exIndex, int exVertices)
526
    {
527
    this(verticesXY,bands,null,null,exIndex,exVertices,0.0f,0.0f);
528
    }
529

    
530
///////////////////////////////////////////////////////////////////////////////////////////////////
531
/**
532
 * Create a polygon of any shape and varying elevations from the edges towards the center.
533
 * Equivalent of the previous with exIndex=0 or exVertices=0.
534
 */
535
  public MeshPolygon(float[] verticesXY, float[] bands)
536
    {
537
    this(verticesXY,bands,null,null,0,0,0.0f,0.0f);
538
    }
539

    
540
///////////////////////////////////////////////////////////////////////////////////////////////////
541
/**
542
 * Copy constructor.
543
 */
544
  public MeshPolygon(MeshPolygon mesh, boolean deep)
545
    {
546
    super(mesh,deep);
547
    }
548

    
549
///////////////////////////////////////////////////////////////////////////////////////////////////
550
/**
551
 * Copy the Mesh.
552
 *
553
 * @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
554
 *             normals and inflates (the rest, in particular the mVertAttribs2 containing texture
555
 *             coordinates and effect associations, is always deep copied)
556
 */
557
  public MeshPolygon copy(boolean deep)
558
    {
559
    return new MeshPolygon(this,deep);
560
    }
561
 }
(7-7/11)