Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshPolygon.java @ fd527acd

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
      return (tmp-i1)*(mCurveCache[i2]-mCurveCache[i1]) + mCurveCache[i1];
209
      }
210
    }
211

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

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

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

    
237
                          mVecX = a*xEdge + (1-a)*xVec;
238
                          mVecY = a*yEdge + (1-a)*yVec;
239

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

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

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

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

    
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294

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

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

    
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312

    
313
  private int addVertex(int vertex, int polyBand, int polyVertex, int polyEndVer, float quot,
314
                        float[] attribs1, float[] attribs2)
315
    {
316
    remainingVert--;
317

    
318
    float Xfirst= mPolygonVertices[2*polyVertex  ];
319
    float Yfirst= mPolygonVertices[2*polyVertex+1];
320
    float Xlast = mPolygonVertices[2*polyEndVer  ];
321
    float Ylast = mPolygonVertices[2*polyEndVer+1];
322

    
323
    float xEdge = Xfirst + quot*(Xlast-Xfirst);
324
    float yEdge = Yfirst + quot*(Ylast-Yfirst);
325
    float zEdge;
326

    
327
    float q = mPolygonBands[2*polyBand];
328
    float o = mPolygonBands[2*polyBand+1];
329
    float t = mPolygonBands[2*mNumPolygonBands-1];
330

    
331
    int shape = mEdgeShape[polyVertex];
332

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

    
342
    float x = q*xEdge;
343
    float y = q*yEdge;
344
    float z = o + (t-o)*(zEdge/t);
345

    
346
    attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB  ] = x;
347
    attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] = y;
348
    attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = z;
349

    
350
    figureOutNormalVector2D(shape, polyVertex, polyEndVer, quot, xEdge, yEdge);
351
    float d = figureOutDerivative(shape,polyBand,quot);
352

    
353
    float vx = d*mVecX;
354
    float vy = d*mVecY;
355
    float vz = mVecX*mVecX + mVecY*mVecY;
356
    float len = (float)Math.sqrt(vx*vx + vy*vy + vz*vz);
357

    
358
    int index = VERT1_ATTRIBS*vertex + NOR_ATTRIB;
359
    attribs1[index  ] = vx/len;
360
    attribs1[index+1] = vy/len;
361
    attribs1[index+2] = vz/len;
362

    
363
    attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB  ] = x+0.5f;
364
    attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = y+0.5f;
365

    
366
    return vertex+1;
367
    }
368

    
369
///////////////////////////////////////////////////////////////////////////////////////////////////
370

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

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

    
384
    int polyEndVer = polyVertex==mNumPolygonVertices-1 ? 0 : polyVertex+1;
385
    float quot1, quot2;
386

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

    
400
      vertex = addVertex(vertex,polyBand+1,polyVertex,polyEndVer,quot1,attribs1,attribs2);
401
      vertex = addVertex(vertex,polyBand  ,polyVertex,polyEndVer,quot2,attribs1,attribs2);
402
      }
403

    
404
    return vertex;
405
    }
406

    
407
///////////////////////////////////////////////////////////////////////////////////////////////////
408

    
409
  private int computeEdgeShape(int curr)
410
    {
411
    if( mEdgeUp==null || !mEdgeUp[curr] ) return SHAPE_DD;
412

    
413
    int prev = (curr==0 ? mNumPolygonVertices-1 : curr-1);
414
    int next = (curr==mNumPolygonVertices-1 ? 0 : curr+1);
415

    
416
    boolean rd = ( !mEdgeUp[next] || mVertUp==null || !mVertUp[next] );
417
    boolean ld = ( !mEdgeUp[prev] || mVertUp==null || !mVertUp[curr] );
418

    
419
    return rd ? (ld ? SHAPE_DUD : SHAPE_UD) : (ld ? SHAPE_DU : SHAPE_UU);
420
    }
421

    
422
///////////////////////////////////////////////////////////////////////////////////////////////////
423

    
424
  private void buildGrid(float[] attribs1, float[] attribs2)
425
    {
426
    int vertex=0;
427

    
428
    mEdgeShape = new int[mNumPolygonVertices];
429

    
430
    for(int polyVertex=0; polyVertex<mNumPolygonVertices; polyVertex++)
431
      mEdgeShape[polyVertex] = computeEdgeShape(polyVertex);
432

    
433
    for(int polyBand=0; polyBand<mNumPolygonBands-1; polyBand++)
434
      for(int polyVertex=0; polyVertex<mNumPolygonVertices; polyVertex++)
435
        vertex = createBandStrip(vertex,polyBand,polyVertex,attribs1,attribs2);
436
    }
437

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

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

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

    
495
    computeNumberOfVertices();
496
    computeCache();
497

    
498
    float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
499
    float[] attribs2= new float[VERT2_ATTRIBS*numVertices];
500

    
501
    buildGrid(attribs1,attribs2);
502

    
503
    if( remainingVert!=0 )
504
      DistortedLibrary.logMessage("MeshPolygon: remainingVert " +remainingVert );
505

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

    
517
    setAttribs(attribs1,attribs2);
518
    }
519

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

    
522
  public MeshPolygon(float[] verticesXY, float[] bands, int exIndex, int exVertices)
523
    {
524
    this(verticesXY,bands,null,null,exIndex,exVertices,0.0f,0.0f);
525
    }
526

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

    
537
///////////////////////////////////////////////////////////////////////////////////////////////////
538
/**
539
 * Copy constructor.
540
 */
541
  public MeshPolygon(MeshPolygon mesh, boolean deep)
542
    {
543
    super(mesh,deep);
544
    }
545

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