Project

General

Profile

« Previous | Next » 

Revision 46ef24e5

Added by Leszek Koltunski 11 months ago

Implement MeshMultigon out of BandedTriangles (doesn't work yet)

View differences:

src/main/java/org/distorted/library/mesh/MeshMultigon.java
46 46

  
47 47
///////////////////////////////////////////////////////////////////////////////////////////////////
48 48

  
49
  private boolean isUp(float[][] vertices, int polygon, int edge)
49
  private int isUp(float[][] vertices, int polygon, int edge)
50 50
    {
51 51
    //android.util.Log.e("D", "checking polygon "+polygon+" edge "+edge);
52 52

  
......
79 79
          //android.util.Log.e("D", "comparing v2 to "+x1+" "+y1);
80 80
          //android.util.Log.e("D", "comparing v1 to "+x2+" "+y2);
81 81

  
82
          if( isSame(v2x-x1,v2y-y1) && isSame(v1x-x2,v1y-y2) ) return true;
82
          if( isSame(v2x-x1,v2y-y1) && isSame(v1x-x2,v1y-y2) ) return i;
83 83
          }
84 84
        }
85 85

  
86 86
    //android.util.Log.e("D", "FALSE");
87 87

  
88
    return false;
88
    return -1;
89 89
    }
90 90

  
91 91
///////////////////////////////////////////////////////////////////////////////////////////////////
92 92

  
93
  private boolean[][] computeEdgesUp(float[][] vertices)
93
  private int[][] computeEdgesUp(float[][] vertices)
94 94
    {
95 95
    int num = vertices.length;
96
    boolean[][] up = new boolean[num][];
96
    int[][] up = new int[num][];
97 97

  
98 98
    for(int i=0; i<num; i++)
99 99
      {
100 100
      int len = vertices[i].length/2;
101
      up[i] = new boolean[len];
101
      up[i] = new int[len];
102 102
      for(int j=0; j<len; j++)
103 103
        {
104 104
        up[i][j] = isUp(vertices,i,j);
......
236 236
    return up;
237 237
    }
238 238

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

  
241
  private float[] computeCenter(float[] vertices)
242
    {
243
    int num = vertices.length/2;
244
    float[] ret = new float[2];
245

  
246
    for(int i=0; i<num; i++)
247
      {
248
      ret[0] += vertices[2*i];
249
      ret[1] += vertices[2*i+1];
250
      }
251

  
252
    ret[0] /= num;
253
    ret[1] /= num;
254

  
255
    return ret;
256
    }
257

  
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259

  
260
  private float[][] computeCenters(float[][] vertices)
261
    {
262
    int num = vertices.length;
263
    float[][] ret = new float[num][];
264
    for(int i=0; i<num; i++) ret[i] = computeCenter(vertices[i]);
265
    return ret;
266
    }
267

  
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269

  
270
  private int computeMode(float[] vL, float[] vR, float[] vT, float[] normL, float[] normR,
271
                          int[][] edgesUp, boolean[][] vertsUp, float[][] vertices, float[][] centers, int component, int curr)
272
    {
273
    float[] v = vertices[component];
274
    int[] edges = edgesUp[component];
275
    int len = v.length /2;
276
    int next= curr==len-1 ? 0 : curr+1;
277
    int prev= curr==0 ? len-1 : curr-1;
278
    int eupc = edges[curr];
279

  
280
    if( eupc<0 )
281
      {
282
      int eupp = edges[prev];
283
      int eupn = edges[next];
284

  
285
      vL[0] = v[2*curr];
286
      vL[1] = v[2*curr+1];
287
      vR[0] = v[2*next];
288
      vR[1] = v[2*next+1];
289
      vT[0] = centers[component][0];
290
      vT[1] = centers[component][1];
291

  
292
      if( eupp<0 )
293
        {
294
        normL[0]=vT[0]-vL[0];
295
        normL[1]=vT[1]-vL[1];
296
        }
297
      else
298
        {
299
        normL[0]= v[2*prev  ] - v[2*curr  ];
300
        normL[1]= v[2*prev+1] - v[2*curr+1];
301
        }
302

  
303
      if( eupn<0 )
304
        {
305
        normR[0]=vT[0]-vR[0];
306
        normR[1]=vT[1]-vR[1];
307
        }
308
      else
309
        {
310
        int nnxt= next==len-1 ? 0 : next+1;
311
        normL[0]= v[2*nnxt  ] - v[2*next  ];
312
        normL[1]= v[2*nnxt+1] - v[2*next+1];
313
        }
314

  
315
      return MeshBandedTriangle.MODE_NORMAL;
316
      }
317
    else
318
      {
319
      vL[0] = centers[eupc][0];
320
      vL[1] = centers[eupc][1];
321
      vR[0] = centers[component][0];
322
      vR[1] = centers[component][1];
323
      vT[0] = v[2*curr];
324
      vT[1] = v[2*curr+1];
325

  
326
      boolean vup = vertsUp[component][curr];
327

  
328
      if( vup )
329
        {
330
        return MeshBandedTriangle.MODE_FLAT;
331
        }
332
      else
333
        {
334
        float dx=v[2*curr]-v[2*next];
335
        float dy=v[2*curr+1]-v[2*next+1];
336

  
337
        normL[0]=dx;
338
        normL[1]=dy;
339
        normR[0]=dx;
340
        normR[1]=dy;
341

  
342
        return MeshBandedTriangle.MODE_INVERTED;
343
        }
344
      }
345
    }
346

  
239 347
///////////////////////////////////////////////////////////////////////////////////////////////////
240 348
// PUBLIC API
241 349
///////////////////////////////////////////////////////////////////////////////////////////////////
......
246 354
 *
247 355
 * @param vertices   an array of arrays, each specifying vertices of a single polygon.
248 356
 * @param band       see MeshPolygon. Shared among all polygons.
249
 * @param exIndex    see MeshPolygon. Shared among all polygons.
357
 * @param exBands    see MeshPolygon. Shared among all polygons.
250 358
 * @param exVertices see MeshPolygon. Shared among all polygons.
251
 * @param centers    for each polygon, coordinates of its center.
252 359
 */
253
  public MeshMultigon(float[][] vertices, float[] band, int exIndex, int exVertices, float[][] centers)
360
  public MeshMultigon(float[][] vertices, float[] band, int exBands, int exVertices)
254 361
    {
255 362
    super();
256 363

  
257
    int numPolygons = vertices.length;
258
    MeshPolygon[] meshes = new MeshPolygon[numPolygons];
259
    boolean[][] edgesUp = computeEdgesUp(vertices);
260
    boolean[][] vertsUp = computeVertsUp(vertices);
261

  
262
    for(int i=0; i<numPolygons; i++)
263
      meshes[i] = new MeshPolygon(vertices[i],band,edgesUp[i],vertsUp[i],exIndex,exVertices,centers[i][0],centers[i][1]);
264

  
265
    join(meshes);
266
    mergeEffComponents();
267
    mergeTexComponents();
268
    }
364
    int numTriangles=0;
365
    for(float[] vertex : vertices) numTriangles+=vertex.length/2;
366
    MeshBandedTriangle[] triangles = new MeshBandedTriangle[numTriangles];
269 367

  
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271
/**
272
 * Auto-create the centers to be the centers of gravity of each polygon.
273
 */
368
    int[][] edgesUp = computeEdgesUp(vertices);
369
    boolean[][] vertsUp = computeVertsUp(vertices);
370
    float[][] centers = computeCenters(vertices);
274 371

  
275
  public MeshMultigon(float[][] vertices, float[] band, int exIndex, int exVertices)
276
    {
277
    super();
372
    float[] vL = new float[2];
373
    float[] vR = new float[2];
374
    float[] vT = new float[2];
375
    float[] normL = new float[2];
376
    float[] normR = new float[2];
278 377

  
279
    int numPolygons = vertices.length;
280
    MeshPolygon[] meshes = new MeshPolygon[numPolygons];
281
    boolean[][] edgesUp = computeEdgesUp(vertices);
282
    boolean[][] vertsUp = computeVertsUp(vertices);
378
    int index=0,len = vertices.length;
283 379

  
284
    for(int i=0; i<numPolygons; i++)
380
    for(int i=0; i<len; i++)
285 381
      {
286
      float[] v = vertices[i];
287
      int num = v.length/2;
288
      float xsum=0, ysum = 0;
382
      int num=vertices[i].length/2;
289 383

  
290 384
      for(int j=0; j<num; j++)
291 385
        {
292
        xsum += v[2*j];
293
        ysum += v[2*j+1];
386
        int mode=computeMode(vL, vR, vT, normL, normL, edgesUp, vertsUp,vertices,centers, i,j);
387
        triangles[index++] = new MeshBandedTriangle(vL, vR, vT, band, normL, normR, mode, exBands, exVertices);
294 388
        }
295

  
296
      meshes[i] = new MeshPolygon(v,band,edgesUp[i],vertsUp[i],exIndex,exVertices,xsum/num,ysum/num);
297 389
      }
298 390

  
299
    join(meshes);
391
    join(triangles);
300 392
    mergeEffComponents();
301 393
    mergeTexComponents();
302 394
    }

Also available in: Unified diff