commit d456b0755c8298c8565901fec503491f72f0714d
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Tue Sep 8 19:03:09 2020 +0100

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

diff --git a/src/main/java/org/distorted/library/mesh/MeshPolygon.java b/src/main/java/org/distorted/library/mesh/MeshPolygon.java
index 9670061..7981e87 100644
--- a/src/main/java/org/distorted/library/mesh/MeshPolygon.java
+++ b/src/main/java/org/distorted/library/mesh/MeshPolygon.java
@@ -36,15 +36,18 @@ public class MeshPolygon extends MeshBase
 
   private int remainingVert;
   private int numVertices;
+  private int extraIndex, extraVertices;
 
   private float[] mBandQuot;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // polygonVertices>=3 , polygonBands>=2
 
-  private void computeNumberOfVertices(int numPolygonVertices, int numPolygonBands)
+  private void computeNumberOfVertices()
      {
-     numVertices = (numPolygonVertices*numPolygonBands+2)*(numPolygonBands-1) - 1;
+     numVertices = (mNumPolygonVertices*mNumPolygonBands+2)*(mNumPolygonBands-1) - 1;
+     numVertices+= 2*mNumPolygonVertices*(2*extraIndex*extraVertices);
+
      remainingVert = numVertices;
      }
 
@@ -67,14 +70,45 @@ public class MeshPolygon extends MeshBase
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  private int addVertex(int vertex, int polyBand, int polyVertex, int polyEndVer, int index, float[] attribs1, float[] attribs2)
+  private float getQuot(int index, int band, boolean isExtra)
     {
-    remainingVert--;
+    int num = mNumPolygonBands-1-band;
 
-    float quot = polyBand<mNumPolygonBands-1 ? (float)index / (mNumPolygonBands-1-polyBand) : 1.0f;
+    if( num>0 )
+      {
+      if( isExtra )
+        {
+        int extra = extraIndex-band+extraVertices;
+
+        if( index < extra )
+          {
+          float quot = ((float)extraIndex-band)/(extra*num);
+          return index*quot;
+          }
+        else if( index > num+2*extraVertices-extra )
+          {
+          float quot = ((float)extraIndex-band)/(extra*num);
+          return (1.0f-((float)extraIndex-band)/num) + (index-num-2*extraVertices+extra)*quot;
+          }
+        else
+          {
+          return ((float)(index-extraVertices))/num;
+          }
+        }
 
-    float vx,vy,vz;
+      return (float)index/num;
+      }
 
+    return 1.0f;
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  private int addVertex(int vertex, int polyBand, int polyVertex, int polyEndVer, float quot, float[] attribs1, float[] attribs2)
+    {
+    remainingVert--;
+
+    float vx,vy,vz;
     float Xfirst= mPolygonVertices[2*polyVertex  ];
     float Yfirst= mPolygonVertices[2*polyVertex+1];
     float Xlast = mPolygonVertices[2*polyEndVer  ];
@@ -131,12 +165,23 @@ public class MeshPolygon extends MeshBase
       }
 
     int numPairs = mNumPolygonBands-1-polyBand;
+    boolean isExtra = polyBand<extraIndex;
+
+    if( isExtra )
+      {
+      numPairs += 2*extraVertices;
+      }
+
     int polyEndVer = polyVertex==mNumPolygonVertices-1 ? 0 : polyVertex+1;
+    float quot1, quot2;
 
     for(int index=0; index<numPairs; index++)
       {
-      vertex = addVertex(vertex,polyBand+1,polyVertex,polyEndVer,index  ,attribs1,attribs2);
-      vertex = addVertex(vertex,polyBand  ,polyVertex,polyEndVer,index+1,attribs1,attribs2);
+      quot1 = getQuot(index  ,polyBand+1, isExtra);
+      quot2 = getQuot(index+1,polyBand  , isExtra);
+
+      vertex = addVertex(vertex,polyBand+1,polyVertex,polyEndVer,quot1,attribs1,attribs2);
+      vertex = addVertex(vertex,polyBand  ,polyVertex,polyEndVer,quot2,attribs1,attribs2);
       }
 
     return vertex;
@@ -160,23 +205,31 @@ public class MeshPolygon extends MeshBase
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
  * Create a polygon of any shape and varying elevations from the edges towards the center.
+ * Optionally make it more dense at the vertices.
  *
  * @param verticesXY 2N floats - packed description of polygon vertices. N pairs (x,y).
  * @param bands      2K floats; K pairs of two floats each describing a single band.
  *                   From (1.0,Z[0]) (outer edge, its Z elevation) to (0.0,Z[K]) (the center,
  *                   its elevation). The polygon is split into such concentric bands.
  *                   Must be band[2*i] > band[2*(i+1)] !
+ * @param exIndex    This and the next parameter describe how to make the mesh denser at the
+ *                   polyVertices. If e.g. exIndex=3 and exVertices=2, then 3 triangles of the
+ *                   outermost band (and 2 traingles of the next band, and 1 triange of the third
+ *                   band) get denser - the 3 triangles become 3+2 = 5.
+ * @param exVertices See above.
  */
-  public MeshPolygon(float[] verticesXY, float[] bands)
+  public MeshPolygon(float[] verticesXY, float[] bands, int exIndex, int exVertices)
     {
     super();
 
-    mPolygonVertices      = verticesXY;
-    mPolygonBands         = bands;
-    mNumPolygonVertices   = mPolygonVertices.length /2;
-    mNumPolygonBands      = mPolygonBands.length /2;
+    mPolygonVertices   = verticesXY;
+    mPolygonBands      = bands;
+    mNumPolygonVertices= mPolygonVertices.length /2;
+    mNumPolygonBands   = mPolygonBands.length /2;
+    extraIndex         = exIndex;
+    extraVertices      = exVertices;
 
-    computeNumberOfVertices(mNumPolygonVertices,mNumPolygonBands);
+    computeNumberOfVertices();
     computeCache();
 
     float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
@@ -190,6 +243,16 @@ public class MeshPolygon extends MeshBase
     setAttribs(attribs1,attribs2);
     }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+/**
+ * Create a polygon of any shape and varying elevations from the edges towards the center.
+ * Equivalent of the previous with exIndex=0 or exVertices=0.
+ */
+  public MeshPolygon(float[] verticesXY, float[] bands)
+    {
+    this(verticesXY,bands,0,0);
+    }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
  * Copy constructor.
