commit e529379e34c6b29fceecfb5d3a89224c52e337bf
Author: leszek <leszek@koltunski.pl>
Date:   Tue Jun 27 23:56:13 2023 +0200

    We no longer need to support 'edgesUp' in MeshPolygons.

diff --git a/src/main/java/org/distorted/library/mesh/MeshPolygon.java b/src/main/java/org/distorted/library/mesh/MeshPolygon.java
index 0664709..8bb7744 100644
--- a/src/main/java/org/distorted/library/mesh/MeshPolygon.java
+++ b/src/main/java/org/distorted/library/mesh/MeshPolygon.java
@@ -20,10 +20,9 @@
 
 package org.distorted.library.mesh;
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
 import org.distorted.library.main.DistortedLibrary;
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
  * Create a polygon of any shape and varying elevations from the edges towards the center.
  * <p>
@@ -34,40 +33,31 @@ import org.distorted.library.main.DistortedLibrary;
 public class MeshPolygon extends MeshBase
   {
   private static final int NUM_CACHE = 20;
-  private static final int SHAPE_DD = 0;
-  private static final int SHAPE_DU = 1;
-  private static final int SHAPE_UD = 2;
-  private static final int SHAPE_UU = 3;
-  private static final int SHAPE_DUD= 4;
 
   private float[] mPolygonVertices;
   private int mNumPolygonVertices;
   private float[] mPolygonBands;
   private int mNumPolygonBands;
-  private boolean[] mEdgeUp;
-  private boolean[] mVertUp;
 
   private int remainingVert;
   private int numVertices;
-  private int extraIndex, extraVertices;
+  private int extraBand, extraVertices;
 
   private float[] mCurveCache;
-  private float mVecX, mVecY;
-  private int[] mEdgeShape;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // polygonVertices>=3 , polygonBands>=2
 
   private void computeNumberOfVertices()
      {
-     if( mNumPolygonBands==2 && extraIndex>0 )
+     if( mNumPolygonBands==2 && extraBand>0 )
        {
-       numVertices = 1 + 2*mNumPolygonVertices*(1+extraIndex+2*extraVertices);
+       numVertices = 1 + 2*mNumPolygonVertices*(1+extraBand+2*extraVertices);
        }
      else
        {
        numVertices = (mNumPolygonVertices*mNumPolygonBands+2)*(mNumPolygonBands-1) - 1;
-       numVertices+= 2*mNumPolygonVertices*(2*extraIndex*extraVertices);
+       numVertices+= 2*mNumPolygonVertices*(2*extraBand*extraVertices);
        }
 
      remainingVert = numVertices;
@@ -123,10 +113,10 @@ public class MeshPolygon extends MeshBase
 
   private float getSpecialQuot(int index)
     {
-    int num = 1 + extraIndex + 2*extraVertices;
+    int num = 1 + extraBand + 2*extraVertices;
     int change1 = extraVertices+1;
     int change2 = num-change1;
-    float quot = 1.0f/(extraIndex+1);
+    float quot = 1.0f/(extraBand+1);
 
     if( index<change1 )      return index*quot/change1;
     else if( index>change2 ) return 1-quot + (index-change2)*quot/change1;
@@ -143,17 +133,17 @@ public class MeshPolygon extends MeshBase
       {
       if( isExtra )
         {
-        int extra = extraIndex-band+extraVertices;
+        int extra = extraBand-band+extraVertices;
 
         if( index < extra )
           {
-          float quot = ((float)extraIndex-band)/(extra*num);
+          float quot = ((float)extraBand-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;
+          float quot = ((float)extraBand-band)/(extra*num);
+          return (1.0f-((float)extraBand-band)/num) + (index-num-2*extraVertices+extra)*quot;
           }
         else
           {
@@ -167,31 +157,6 @@ public class MeshPolygon extends MeshBase
     return 1.0f;
     }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  private float computeZEdge(float quot)
-    {
-    if( quot>=1.0f ) return 0.0f;
-
-    for(int band=1; band<mNumPolygonBands; band++)
-      {
-      float curr = mPolygonBands[2*band];
-
-      if( curr<=quot )
-        {
-        float prev = mPolygonBands[2*band-2];
-        float prevH= mPolygonBands[2*band-1];
-        float currH= mPolygonBands[2*band+1];
-
-        float A = (prev-quot)/(prev-curr);
-
-        return A*currH + (1-A)*prevH;
-        }
-      }
-
-    return 0.0f;
-    }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   private float derivative(float x)
@@ -212,105 +177,6 @@ public class MeshPolygon extends MeshBase
       }
     }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  private void figureOutNormalVector2D(int edgeShape, int polyVertex, int polyEndVer, float quot, float xEdge, float yEdge )
-    {
-    if( edgeShape==SHAPE_DD )
-      {
-      if( quot<0.5f )
-        {
-        int p = polyVertex>0 ? polyVertex-1 : mNumPolygonVertices-1;
-        int prvShape = mEdgeShape[p];
-
-        switch(prvShape)
-          {
-          case SHAPE_DD : mVecX = xEdge;
-                          mVecY = yEdge;
-                          break;
-          case SHAPE_UD :
-          case SHAPE_DUD: float a = 2*quot;
-                          float xCurr= mPolygonVertices[2*polyVertex  ];
-                          float yCurr= mPolygonVertices[2*polyVertex+1];
-                          float xPrev= mPolygonVertices[2*p  ];
-                          float yPrev= mPolygonVertices[2*p+1];
-                          float xVec = xCurr-xPrev;
-                          float yVec = yCurr-yPrev;
-
-                          mVecX = a*xEdge + (1-a)*xVec;
-                          mVecY = a*yEdge + (1-a)*yVec;
-
-                          break;
-          default: throw new RuntimeException("figureOutNormalVector2D: impossible1: "+prvShape);
-          }
-        }
-      else
-        {
-        int n = polyEndVer==mNumPolygonVertices-1 ? 0 : polyEndVer+1;
-        int nxtShape = mEdgeShape[polyEndVer];
-
-        switch(nxtShape)
-          {
-          case SHAPE_DD : mVecX = xEdge;
-                          mVecY = yEdge;
-                          break;
-          case SHAPE_DU :
-          case SHAPE_DUD: float a = 2-2*quot;
-                          float xCurr= mPolygonVertices[2*polyEndVer  ];
-                          float yCurr= mPolygonVertices[2*polyEndVer+1];
-                          float xNext= mPolygonVertices[2*n  ];
-                          float yNext= mPolygonVertices[2*n+1];
-                          float xVec = xCurr-xNext;
-                          float yVec = yCurr-yNext;
-
-                          mVecX = a*xEdge + (1-a)*xVec;
-                          mVecY = a*yEdge + (1-a)*yVec;
-                          break;
-          default: throw new RuntimeException("figureOutNormalVector2D: impossible2: "+nxtShape);
-          }
-        }
-      }
-    else if( edgeShape==SHAPE_UU || (quot>=0.5f && edgeShape==SHAPE_DU) || (quot<0.5f && edgeShape==SHAPE_UD) )
-      {
-      mVecX = 1;
-      mVecY = 0;
-      }
-    else
-      {
-      float dx = mPolygonVertices[2*polyVertex  ] - mPolygonVertices[2*polyEndVer  ];
-      float dy = mPolygonVertices[2*polyVertex+1] - mPolygonVertices[2*polyEndVer+1];
-
-      if( quot<0.5 )
-        {
-        mVecX = dx;
-        mVecY = dy;
-        }
-      else
-        {
-        mVecX = -dx;
-        mVecY = -dy;
-        }
-      }
-    }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  private float figureOutDerivative(int edgeShape, int polyBand, float quot)
-    {
-    if( edgeShape==SHAPE_DD )
-      {
-      return derivative(1-mPolygonBands[2*polyBand]);
-      }
-    if( edgeShape==SHAPE_UU || (quot>=0.5f && edgeShape==SHAPE_DU) || (quot<0.5f && edgeShape==SHAPE_UD) )
-      {
-      return 0;
-      }
-
-    float x = 1-mPolygonBands[2*polyBand];
-    float q = quot>=0.5f ? 2-2*quot : 2*quot;
-    return derivative((1-x)*q+x);
-    }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   private int addVertex(int vertex, int polyBand, int polyVertex, int polyEndVer, float quot,
@@ -325,37 +191,21 @@ public class MeshPolygon extends MeshBase
 
     float xEdge = Xfirst + quot*(Xlast-Xfirst);
     float yEdge = Yfirst + quot*(Ylast-Yfirst);
-    float zEdge;
 
     float q = mPolygonBands[2*polyBand];
-    float o = mPolygonBands[2*polyBand+1];
-    float t = mPolygonBands[2*mNumPolygonBands-1];
-
-    int shape = mEdgeShape[polyVertex];
-
-    switch(shape)
-      {
-      case SHAPE_DD : zEdge = 0.0f; break;
-      case SHAPE_UU : zEdge = t;    break;
-      case SHAPE_DU : zEdge = quot>=0.5f ? t : computeZEdge(1-2*quot); break;
-      case SHAPE_UD : zEdge = quot<=0.5f ? t : computeZEdge(2*quot-1); break;
-      default       : zEdge = quot<=0.5f ? computeZEdge(1-2*quot) : computeZEdge(2*quot-1); break;
-      }
-
     float x = q*xEdge;
     float y = q*yEdge;
-    float z = o + (t-o)*(zEdge/t);
+    float z = mPolygonBands[2*polyBand+1];
 
     attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB  ] = x;
     attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] = y;
     attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = z;
 
-    figureOutNormalVector2D(shape, polyVertex, polyEndVer, quot, xEdge, yEdge);
-    float d = figureOutDerivative(shape,polyBand,quot);
+    float d = derivative(1-mPolygonBands[2*polyBand]);
 
-    float vx = d*mVecX;
-    float vy = d*mVecY;
-    float vz = mVecX*mVecX + mVecY*mVecY;
+    float vx = d*xEdge;
+    float vy = d*yEdge;
+    float vz = xEdge*xEdge + yEdge*yEdge;
     float len = (float)Math.sqrt(vx*vx + vy*vy + vz*vz);
 
     int index = VERT1_ATTRIBS*vertex + NOR_ATTRIB;
@@ -379,9 +229,9 @@ public class MeshPolygon extends MeshBase
       if( polyBand>0 ) vertex = addVertex(vertex,polyBand,0,1,0,attribs1,attribs2);
       }
 
-    boolean specialCase = mNumPolygonBands==2 && polyBand==0 && extraIndex>0;
-    boolean isExtra = polyBand<extraIndex;
-    int numPairs = specialCase ? extraIndex+1 : mNumPolygonBands-1-polyBand;
+    boolean specialCase = mNumPolygonBands==2 && polyBand==0 && extraBand>0;
+    boolean isExtra = polyBand<extraBand;
+    int numPairs = specialCase ? extraBand+1 : mNumPolygonBands-1-polyBand;
     if( isExtra ) numPairs += 2*extraVertices;
 
     int polyEndVer = polyVertex==mNumPolygonVertices-1 ? 0 : polyVertex+1;
@@ -407,32 +257,12 @@ public class MeshPolygon extends MeshBase
     return vertex;
     }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  private int computeEdgeShape(int curr)
-    {
-    if( mEdgeUp==null || !mEdgeUp[curr] ) return SHAPE_DD;
-
-    int prev = (curr==0 ? mNumPolygonVertices-1 : curr-1);
-    int next = (curr==mNumPolygonVertices-1 ? 0 : curr+1);
-
-    boolean rd = ( !mEdgeUp[next] || mVertUp==null || !mVertUp[next] );
-    boolean ld = ( !mEdgeUp[prev] || mVertUp==null || !mVertUp[curr] );
-
-    return rd ? (ld ? SHAPE_DUD : SHAPE_UD) : (ld ? SHAPE_DU : SHAPE_UU);
-    }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   private void buildGrid(float[] attribs1, float[] attribs2)
     {
     int vertex=0;
 
-    mEdgeShape = new int[mNumPolygonVertices];
-
-    for(int polyVertex=0; polyVertex<mNumPolygonVertices; polyVertex++)
-      mEdgeShape[polyVertex] = computeEdgeShape(polyVertex);
-
     for(int polyBand=0; polyBand<mNumPolygonBands-1; polyBand++)
       for(int polyVertex=0; polyVertex<mNumPolygonVertices; polyVertex++)
         vertex = createBandStrip(vertex,polyBand,polyVertex,attribs1,attribs2);
@@ -451,20 +281,7 @@ public class MeshPolygon extends MeshBase
  *                   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 edgeUp     N booleans - one for each edge; the first connects vertices (0,1) and (2,3);
- *                   then just like 'verticesXY', i.e. counterclockwise.
- *                   If this is null, all edges are by default 'down'.
- *                   'Down' means that edge's Z is equal to 0; 'up' means that its Z, at least in its
- *                   middle, is equal to the highest elevation in the middle of the mesh.
- *                   If the 'previous' edge is also up, then the Z is up horizontally from its middle
- *                   to the left vertex; else it goes down along the same function it goes along the bands.
- *                   Same with the right half of the edge.
- * @param vertUp     N booleans - one for each vertex. The first is about vertex 0, then vertex 1, etc.
- *                   If this is null or false, the vertex is down; otheerwise - it is 'up'.
- *                   This is taken into account only in one case: if both edges the vertex is the end of
- *                   are 'up', then the vertex can be 'up' or 'down'; otherwise - if at least one of
- *                   those edges is 'down' - then the vertex must be 'down' as well and this is ignored.
- * @param exIndex    This and the next parameter describe how to make the mesh denser at the
+ * @param exBand     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 triangles of the next band, and 1 triangle of the third
  *                   band) get denser - the 3 triangles become 3+2 = 5.
@@ -473,7 +290,7 @@ public class MeshPolygon extends MeshBase
  *                   all bands go to.
  * @param centerY    Y coordinate of the center.
  */
-  public MeshPolygon(float[] verticesXY, float[] bands, boolean[] edgeUp, boolean[] vertUp, int exIndex, int exVertices, float centerX, float centerY)
+  public MeshPolygon(float[] verticesXY, float[] bands, int exBand, int exVertices, float centerX, float centerY)
     {
     super();
 
@@ -481,9 +298,7 @@ public class MeshPolygon extends MeshBase
     mPolygonBands      = bands;
     mNumPolygonVertices= mPolygonVertices.length /2;
     mNumPolygonBands   = mPolygonBands.length /2;
-    mEdgeUp            = edgeUp;
-    mVertUp            = vertUp;
-    extraIndex         = exIndex;
+    extraBand          = exBand;
     extraVertices      = exVertices;
 
     if( centerX!=0.0f || centerY!=0.0f )
@@ -524,7 +339,7 @@ public class MeshPolygon extends MeshBase
 
   public MeshPolygon(float[] verticesXY, float[] bands, int exIndex, int exVertices)
     {
-    this(verticesXY,bands,null,null,exIndex,exVertices,0.0f,0.0f);
+    this(verticesXY,bands,exIndex,exVertices,0.0f,0.0f);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -534,7 +349,7 @@ public class MeshPolygon extends MeshBase
  */
   public MeshPolygon(float[] verticesXY, float[] bands)
     {
-    this(verticesXY,bands,null,null,0,0,0.0f,0.0f);
+    this(verticesXY,bands,0,0,0.0f,0.0f);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
