Project

General

Profile

« Previous | Next » 

Revision d456b075

Added by Leszek Koltunski over 4 years ago

Add an extra option to MeshPolygon: making triangles located around the vertices of the polygon smaller.

View differences:

src/main/java/org/distorted/library/mesh/MeshPolygon.java
36 36

  
37 37
  private int remainingVert;
38 38
  private int numVertices;
39
  private int extraIndex, extraVertices;
39 40

  
40 41
  private float[] mBandQuot;
41 42

  
42 43
///////////////////////////////////////////////////////////////////////////////////////////////////
43 44
// polygonVertices>=3 , polygonBands>=2
44 45

  
45
  private void computeNumberOfVertices(int numPolygonVertices, int numPolygonBands)
46
  private void computeNumberOfVertices()
46 47
     {
47
     numVertices = (numPolygonVertices*numPolygonBands+2)*(numPolygonBands-1) - 1;
48
     numVertices = (mNumPolygonVertices*mNumPolygonBands+2)*(mNumPolygonBands-1) - 1;
49
     numVertices+= 2*mNumPolygonVertices*(2*extraIndex*extraVertices);
50

  
48 51
     remainingVert = numVertices;
49 52
     }
50 53

  
......
67 70

  
68 71
///////////////////////////////////////////////////////////////////////////////////////////////////
69 72

  
70
  private int addVertex(int vertex, int polyBand, int polyVertex, int polyEndVer, int index, float[] attribs1, float[] attribs2)
73
  private float getQuot(int index, int band, boolean isExtra)
71 74
    {
72
    remainingVert--;
75
    int num = mNumPolygonBands-1-band;
73 76

  
74
    float quot = polyBand<mNumPolygonBands-1 ? (float)index / (mNumPolygonBands-1-polyBand) : 1.0f;
77
    if( num>0 )
78
      {
79
      if( isExtra )
80
        {
81
        int extra = extraIndex-band+extraVertices;
82

  
83
        if( index < extra )
84
          {
85
          float quot = ((float)extraIndex-band)/(extra*num);
86
          return index*quot;
87
          }
88
        else if( index > num+2*extraVertices-extra )
89
          {
90
          float quot = ((float)extraIndex-band)/(extra*num);
91
          return (1.0f-((float)extraIndex-band)/num) + (index-num-2*extraVertices+extra)*quot;
92
          }
93
        else
94
          {
95
          return ((float)(index-extraVertices))/num;
96
          }
97
        }
75 98

  
76
    float vx,vy,vz;
99
      return (float)index/num;
100
      }
77 101

  
102
    return 1.0f;
103
    }
104

  
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

  
107
  private int addVertex(int vertex, int polyBand, int polyVertex, int polyEndVer, float quot, float[] attribs1, float[] attribs2)
108
    {
109
    remainingVert--;
110

  
111
    float vx,vy,vz;
78 112
    float Xfirst= mPolygonVertices[2*polyVertex  ];
79 113
    float Yfirst= mPolygonVertices[2*polyVertex+1];
80 114
    float Xlast = mPolygonVertices[2*polyEndVer  ];
......
131 165
      }
132 166

  
133 167
    int numPairs = mNumPolygonBands-1-polyBand;
168
    boolean isExtra = polyBand<extraIndex;
169

  
170
    if( isExtra )
171
      {
172
      numPairs += 2*extraVertices;
173
      }
174

  
134 175
    int polyEndVer = polyVertex==mNumPolygonVertices-1 ? 0 : polyVertex+1;
176
    float quot1, quot2;
135 177

  
136 178
    for(int index=0; index<numPairs; index++)
137 179
      {
138
      vertex = addVertex(vertex,polyBand+1,polyVertex,polyEndVer,index  ,attribs1,attribs2);
139
      vertex = addVertex(vertex,polyBand  ,polyVertex,polyEndVer,index+1,attribs1,attribs2);
180
      quot1 = getQuot(index  ,polyBand+1, isExtra);
181
      quot2 = getQuot(index+1,polyBand  , isExtra);
182

  
183
      vertex = addVertex(vertex,polyBand+1,polyVertex,polyEndVer,quot1,attribs1,attribs2);
184
      vertex = addVertex(vertex,polyBand  ,polyVertex,polyEndVer,quot2,attribs1,attribs2);
140 185
      }
141 186

  
142 187
    return vertex;
......
160 205
///////////////////////////////////////////////////////////////////////////////////////////////////
161 206
/**
162 207
 * Create a polygon of any shape and varying elevations from the edges towards the center.
208
 * Optionally make it more dense at the vertices.
163 209
 *
164 210
 * @param verticesXY 2N floats - packed description of polygon vertices. N pairs (x,y).
165 211
 * @param bands      2K floats; K pairs of two floats each describing a single band.
166 212
 *                   From (1.0,Z[0]) (outer edge, its Z elevation) to (0.0,Z[K]) (the center,
167 213
 *                   its elevation). The polygon is split into such concentric bands.
168 214
 *                   Must be band[2*i] > band[2*(i+1)] !
215
 * @param exIndex    This and the next parameter describe how to make the mesh denser at the
216
 *                   polyVertices. If e.g. exIndex=3 and exVertices=2, then 3 triangles of the
217
 *                   outermost band (and 2 traingles of the next band, and 1 triange of the third
218
 *                   band) get denser - the 3 triangles become 3+2 = 5.
219
 * @param exVertices See above.
169 220
 */
170
  public MeshPolygon(float[] verticesXY, float[] bands)
221
  public MeshPolygon(float[] verticesXY, float[] bands, int exIndex, int exVertices)
171 222
    {
172 223
    super();
173 224

  
174
    mPolygonVertices      = verticesXY;
175
    mPolygonBands         = bands;
176
    mNumPolygonVertices   = mPolygonVertices.length /2;
177
    mNumPolygonBands      = mPolygonBands.length /2;
225
    mPolygonVertices   = verticesXY;
226
    mPolygonBands      = bands;
227
    mNumPolygonVertices= mPolygonVertices.length /2;
228
    mNumPolygonBands   = mPolygonBands.length /2;
229
    extraIndex         = exIndex;
230
    extraVertices      = exVertices;
178 231

  
179
    computeNumberOfVertices(mNumPolygonVertices,mNumPolygonBands);
232
    computeNumberOfVertices();
180 233
    computeCache();
181 234

  
182 235
    float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
......
190 243
    setAttribs(attribs1,attribs2);
191 244
    }
192 245

  
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247
/**
248
 * Create a polygon of any shape and varying elevations from the edges towards the center.
249
 * Equivalent of the previous with exIndex=0 or exVertices=0.
250
 */
251
  public MeshPolygon(float[] verticesXY, float[] bands)
252
    {
253
    this(verticesXY,bands,0,0);
254
    }
255

  
193 256
///////////////////////////////////////////////////////////////////////////////////////////////////
194 257
/**
195 258
 * Copy constructor.

Also available in: Unified diff