Project

General

Profile

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

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

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[] mEdgeQuots;
56

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

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

    
72
     remainingVert = numVertices;
73
     }
74

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

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

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

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

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

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

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

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

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

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122

    
123
  private void computeQuots()
124
    {
125
    mEdgeQuots = new float[mNumPolygonVertices];
126

    
127
    for(int i=0; i<mNumPolygonVertices; i++)
128
      {
129
      int n = (i==mNumPolygonVertices-1 ? 0:i+1);
130

    
131
      float x1 = mPolygonVertices[2*i];
132
      float y1 = mPolygonVertices[2*i+1];
133
      float x2 = mPolygonVertices[2*n];
134
      float y2 = mPolygonVertices[2*n+1];
135
      float A = x1-x2;
136
      float B = y1-y2;
137
      float C = A*A+B*B;
138

    
139
      float x = (B*B*x1-A*B*y1)/C;
140
      float y = (A*A*y1-A*B*x1)/C;
141

    
142
      float dx = x1-x;
143
      float dy = y1-y;
144

    
145
      mEdgeQuots[i] = (float)Math.sqrt((dx*dx+dy*dy)/C);
146
      }
147
    }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

    
151
  private float getSpecialQuot(int index)
152
    {
153
    int num = 1 + extraIndex + 2*extraVertices;
154
    int change1 = extraVertices+1;
155
    int change2 = num-change1;
156
    float quot = 1.0f/(extraIndex+1);
157

    
158
    if( index<change1 )      return index*quot/change1;
159
    else if( index>change2 ) return 1-quot + (index-change2)*quot/change1;
160
    else                     return (index-change1+1)*quot;
161
    }
162

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164

    
165
  private float getQuot(int index, int band, boolean isExtra)
166
    {
167
    int num = mNumPolygonBands-1-band;
168

    
169
    if( num>0 )
170
      {
171
      if( isExtra )
172
        {
173
        int extra = extraIndex-band+extraVertices;
174

    
175
        if( index < extra )
176
          {
177
          float quot = ((float)extraIndex-band)/(extra*num);
178
          return index*quot;
179
          }
180
        else if( index > num+2*extraVertices-extra )
181
          {
182
          float quot = ((float)extraIndex-band)/(extra*num);
183
          return (1.0f-((float)extraIndex-band)/num) + (index-num-2*extraVertices+extra)*quot;
184
          }
185
        else
186
          {
187
          return ((float)(index-extraVertices))/num;
188
          }
189
        }
190

    
191
      return (float)index/num;
192
      }
193

    
194
    return 1.0f;
195
    }
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198

    
199
  private float computeZEdge(float quot)
200
    {
201
    if( quot>=1.0f ) return 0.0f;
202

    
203
    for(int band=1; band<mNumPolygonBands; band++)
204
      {
205
      float curr = mPolygonBands[2*band];
206

    
207
      if( curr<=quot )
208
        {
209
        float prev = mPolygonBands[2*band-2];
210
        float prevH= mPolygonBands[2*band-1];
211
        float currH= mPolygonBands[2*band+1];
212

    
213
        float A = (prev-quot)/(prev-curr);
214

    
215
        return A*currH + (1-A)*prevH;
216
        }
217
      }
218

    
219
    return 0.0f;
220
    }
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

    
224
  private float derivative(float x)
225
    {
226
    if( x>=1.0f )
227
      {
228
      return mCurveCache[NUM_CACHE-1];
229
      }
230
    else
231
      {
232
      float tmp = x*(NUM_CACHE-1);
233
      int i1 = (int)tmp;
234
      int i2 = i1+1;
235
      return (tmp-i1)*(mCurveCache[i2]-mCurveCache[i1]) + mCurveCache[i1];
236
      }
237
    }
238

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

    
241
  private void doNormals(float[] attribs1, int index, float quot, int edgeShape,
242
                         float xEdge, float yEdge, float zEdge, int polyVertex, int polyBand)
243
    {
244
    if( ( quot<=0.5f && (edgeShape==SHAPE_UD || edgeShape==SHAPE_UU) ) ||
245
        ( quot> 0.5f && (edgeShape==SHAPE_DU || edgeShape==SHAPE_UU) )  )
246
      {
247
      attribs1[index  ] = 0;
248
      attribs1[index+1] = 0;
249
      attribs1[index+2] = 1;
250
      }
251
    else
252
      {
253
      float d,x = 1-mPolygonBands[2*polyBand];
254

    
255
      if( quot==0.0f || quot==1.0f || edgeShape==SHAPE_DD )
256
        {
257
        float t = mPolygonBands[2*mNumPolygonBands-1];
258
        d = ((t-zEdge)/t)*derivative(x);
259
        }
260
      else
261
        {
262
        float q = quot + x*(mEdgeQuots[polyVertex]-quot);
263
        d = (q<0.5f ? derivative(2*q) : -derivative(2-2*q));
264
        int nextVertex = polyVertex==mNumPolygonVertices-1 ? 0:polyVertex+1;
265
        xEdge = mPolygonVertices[2*polyVertex  ] - mPolygonVertices[2*nextVertex  ];
266
        yEdge = mPolygonVertices[2*polyVertex+1] - mPolygonVertices[2*nextVertex+1];
267

    
268
        //android.util.Log.e("D", "normals: edgeQuot="+mEdgeQuots[polyVertex]+" vertex="+polyVertex+" d="+d+" xEdge="+xEdge+" yEdge="+yEdge+" q="+q+" quot="+quot);
269
        }
270

    
271
      float vx = d*xEdge;
272
      float vy = d*yEdge;
273
      float vz = xEdge*xEdge + yEdge*yEdge;
274
      float len = (float)Math.sqrt(vx*vx + vy*vy + vz*vz);
275

    
276
      attribs1[index  ] = vx/len;
277
      attribs1[index+1] = vy/len;
278
      attribs1[index+2] = vz/len;
279
      }
280
    }
281

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283

    
284
  private int addVertex(int vertex, int polyBand, int polyVertex, int polyEndVer, float quot,
285
                        int edgeShape, float[] attribs1, float[] attribs2)
286
    {
287
    remainingVert--;
288

    
289
    float Xfirst= mPolygonVertices[2*polyVertex  ];
290
    float Yfirst= mPolygonVertices[2*polyVertex+1];
291
    float Xlast = mPolygonVertices[2*polyEndVer  ];
292
    float Ylast = mPolygonVertices[2*polyEndVer+1];
293

    
294
    float xEdge = Xfirst + quot*(Xlast-Xfirst);
295
    float yEdge = Yfirst + quot*(Ylast-Yfirst);
296
    float zEdge;
297

    
298
    float q = mPolygonBands[2*polyBand];
299
    float o = mPolygonBands[2*polyBand+1];
300
    float t = mPolygonBands[2*mNumPolygonBands-1];
301

    
302
    switch(edgeShape)
303
      {
304
      case SHAPE_DD : zEdge = 0.0f; break;
305
      case SHAPE_UU : zEdge = t;    break;
306
      case SHAPE_DU : zEdge = quot>=0.5f ? t : computeZEdge(1-2*quot); break;
307
      case SHAPE_UD : zEdge = quot<=0.5f ? t : computeZEdge(2*quot-1); break;
308
      default       : zEdge = quot<=0.5f ? computeZEdge(1-2*quot) : computeZEdge(2*quot-1); break;
309
      }
310

    
311
    float x = q*xEdge;
312
    float y = q*yEdge;
313
    float z = o + (t-o)*(zEdge/t);
314

    
315
    attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB  ] = x;
316
    attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] = y;
317
    attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = z;
318

    
319
    int index = VERT1_ATTRIBS*vertex + NOR_ATTRIB;
320
    doNormals(attribs1,index, quot, edgeShape, xEdge, yEdge, zEdge, polyVertex, polyBand);
321

    
322
    attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB  ] = x+0.5f;
323
    attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = y+0.5f;
324

    
325
    return vertex+1;
326
    }
327

    
328
///////////////////////////////////////////////////////////////////////////////////////////////////
329

    
330
  private int createBandStrip(int vertex, int polyBand, int polyVertex, int edgeShape, float[] attribs1, float[] attribs2)
331
    {
332
    if( polyVertex==0 )
333
      {
334
      vertex = addVertex(vertex,polyBand,0,1,0,edgeShape,attribs1,attribs2);
335
      if( polyBand>0 ) vertex = addVertex(vertex,polyBand,0,1,0,edgeShape,attribs1,attribs2);
336
      }
337

    
338
    boolean specialCase = mNumPolygonBands==2 && polyBand==0 && extraIndex>0;
339
    boolean isExtra = polyBand<extraIndex;
340
    int numPairs = specialCase ? extraIndex+1 : mNumPolygonBands-1-polyBand;
341
    if( isExtra ) numPairs += 2*extraVertices;
342

    
343
    int polyEndVer = polyVertex==mNumPolygonVertices-1 ? 0 : polyVertex+1;
344
    float quot1, quot2;
345

    
346
    for(int index=0; index<numPairs; index++)
347
      {
348
      if( specialCase )
349
        {
350
        quot1 = 1.0f;
351
        quot2 = getSpecialQuot(index+1);
352
        }
353
      else
354
        {
355
        quot1 = getQuot(index  ,polyBand+1, isExtra);
356
        quot2 = getQuot(index+1,polyBand  , isExtra);
357
        }
358

    
359
      vertex = addVertex(vertex,polyBand+1,polyVertex,polyEndVer,quot1,edgeShape,attribs1,attribs2);
360
      vertex = addVertex(vertex,polyBand  ,polyVertex,polyEndVer,quot2,edgeShape,attribs1,attribs2);
361
      }
362

    
363
    return vertex;
364
    }
365

    
366
///////////////////////////////////////////////////////////////////////////////////////////////////
367

    
368
  private int computeEdgeShape(int curr)
369
    {
370
    if( mEdgeUp==null || !mEdgeUp[curr] ) return SHAPE_DD;
371

    
372
    int prev = (curr==0 ? mNumPolygonVertices-1 : curr-1);
373
    int next = (curr==mNumPolygonVertices-1 ? 0 : curr+1);
374

    
375
    boolean rd = ( !mEdgeUp[next] || mVertUp==null || !mVertUp[next] );
376
    boolean ld = ( !mEdgeUp[prev] || mVertUp==null || !mVertUp[curr] );
377

    
378
    return rd ? (ld ? SHAPE_DUD : SHAPE_UD) : (ld ? SHAPE_DU : SHAPE_UU);
379
    }
380

    
381
///////////////////////////////////////////////////////////////////////////////////////////////////
382

    
383
  private void buildGrid(float[] attribs1, float[] attribs2)
384
    {
385
    int vertex=0;
386

    
387
    int[] edgeShape = new int[mNumPolygonVertices];
388

    
389
    for(int polyVertex=0; polyVertex<mNumPolygonVertices; polyVertex++)
390
      edgeShape[polyVertex] = computeEdgeShape(polyVertex);
391

    
392
    for(int polyBand=0; polyBand<mNumPolygonBands-1; polyBand++)
393
      for(int polyVertex=0; polyVertex<mNumPolygonVertices; polyVertex++)
394
        {
395
       // android.util.Log.e("D", "creating strip polyBand="+polyBand+" polyVertex="+polyVertex+" : "+vertex);
396
        vertex = createBandStrip(vertex,polyBand,polyVertex,edgeShape[polyVertex],attribs1,attribs2);
397
       // android.util.Log.e("D", "  "+vertex);
398
        }
399
    }
400

    
401
///////////////////////////////////////////////////////////////////////////////////////////////////
402
// PUBLIC API
403
///////////////////////////////////////////////////////////////////////////////////////////////////
404
/**
405
 * Create a polygon of any shape and varying elevations from the edges towards the center.
406
 * Optionally make it more dense at the vertices.
407
 *
408
 * @param verticesXY 2N floats - packed description of polygon vertices. N pairs (x,y).
409
 *                   Vertices HAVE TO be specified in a COUNTERCLOCKWISE order (starting from any).
410
 * @param bands      2K floats; K pairs of two floats each describing a single band.
411
 *                   From (1.0,Z[0]) (outer edge, its Z elevation) to (0.0,Z[K]) (the center,
412
 *                   its elevation). The polygon is split into such concentric bands.
413
 *                   Must be band[2*i] > band[2*(i+1)] !
414
 * @param edgeUp     N booleans - one for each edge; the first connects vertices (0,1) and (2,3);
415
 *                   then just like 'verticesXY', i.e. counterclockwise.
416
 *                   If this is null, all edges are by default 'down'.
417
 *                   'Down' means that edge's Z is equal to 0; 'up' means that its Z, at least in its
418
 *                   middle, is equal to the highest elevation in the middle of the mesh.
419
 *                   If the 'previous' edge is also up, then the Z is up horizontally from its middle
420
 *                   to the left vertex; else it goes down along the same function it goes along the bands.
421
 *                   Same with the right half of the edge.
422
 * @param vertUp     N booleans - one for each vertex. The first is about vertex 0, then vertex 1, etc.
423
 *                   If this is null or false, the vertex is down; otheerwise - it is 'up'.
424
 *                   This is taken into account only in one case: if both edges the vertex is the end of
425
 *                   are 'up', then the vertex can be 'up' or 'down'; otherwise - if at least one of
426
 *                   those edges is 'down' - then the vertex must be 'down' as well and this is ignored.
427
 * @param exIndex    This and the next parameter describe how to make the mesh denser at the
428
 *                   polyVertices. If e.g. exIndex=3 and exVertices=2, then 3 triangles of the
429
 *                   outermost band (and 2 triangles of the next band, and 1 triangle of the third
430
 *                   band) get denser - the 3 triangles become 3+2 = 5.
431
 * @param exVertices See above.
432
 * @param centerX    the X coordinate of the 'center' of the Polygon, i.e. point of the mesh
433
 *                   all bands go to.
434
 * @param centerY    Y coordinate of the center.
435
 */
436
  public MeshPolygon(float[] verticesXY, float[] bands, boolean[] edgeUp, boolean[] vertUp, int exIndex, int exVertices, float centerX, float centerY)
437
    {
438
    super();
439

    
440
    mPolygonVertices   = verticesXY;
441
    mPolygonBands      = bands;
442
    mNumPolygonVertices= mPolygonVertices.length /2;
443
    mNumPolygonBands   = mPolygonBands.length /2;
444
    mEdgeUp            = edgeUp;
445
    mVertUp            = vertUp;
446
    extraIndex         = exIndex;
447
    extraVertices      = exVertices;
448

    
449
    if( centerX!=0.0f || centerY!=0.0f )
450
      {
451
      for(int v=0; v<mNumPolygonVertices; v++)
452
        {
453
        mPolygonVertices[2*v  ] -= centerX;
454
        mPolygonVertices[2*v+1] -= centerY;
455
        }
456
      }
457

    
458
    computeNumberOfVertices();
459
    computeCache();
460
    computeQuots();
461

    
462
    float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
463
    float[] attribs2= new float[VERT2_ATTRIBS*numVertices];
464

    
465
    buildGrid(attribs1,attribs2);
466

    
467
    if( remainingVert!=0 )
468
      DistortedLibrary.logMessage("MeshPolygon: remainingVert " +remainingVert );
469

    
470
    if( centerX!=0.0f || centerY!=0.0f )
471
      {
472
      for(int v=0; v<numVertices; v++)
473
        {
474
        attribs1[VERT1_ATTRIBS*v + POS_ATTRIB  ] += centerX;
475
        attribs1[VERT1_ATTRIBS*v + POS_ATTRIB+1] += centerY;
476
        attribs2[VERT2_ATTRIBS*v + TEX_ATTRIB  ] += centerX;
477
        attribs2[VERT2_ATTRIBS*v + TEX_ATTRIB+1] += centerY;
478
        }
479
      }
480

    
481
    setAttribs(attribs1,attribs2);
482
    }
483

    
484
///////////////////////////////////////////////////////////////////////////////////////////////////
485

    
486
  public MeshPolygon(float[] verticesXY, float[] bands, int exIndex, int exVertices)
487
    {
488
    this(verticesXY,bands,null,null,exIndex,exVertices,0.0f,0.0f);
489
    }
490

    
491
///////////////////////////////////////////////////////////////////////////////////////////////////
492
/**
493
 * Create a polygon of any shape and varying elevations from the edges towards the center.
494
 * Equivalent of the previous with exIndex=0 or exVertices=0.
495
 */
496
  public MeshPolygon(float[] verticesXY, float[] bands)
497
    {
498
    this(verticesXY,bands,null,null,0,0,0.0f,0.0f);
499
    }
500

    
501
///////////////////////////////////////////////////////////////////////////////////////////////////
502
/**
503
 * Copy constructor.
504
 */
505
  public MeshPolygon(MeshPolygon mesh, boolean deep)
506
    {
507
    super(mesh,deep);
508
    }
509

    
510
///////////////////////////////////////////////////////////////////////////////////////////////////
511
/**
512
 * Copy the Mesh.
513
 *
514
 * @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
515
 *             normals and inflates (the rest, in particular the mVertAttribs2 containing texture
516
 *             coordinates and effect associations, is always deep copied)
517
 */
518
  public MeshPolygon copy(boolean deep)
519
    {
520
    return new MeshPolygon(this,deep);
521
    }
522
 }
(7-7/11)