commit f19d533dea59627b170a348bb8e64788efb0d16d
Author: leszek <leszek@koltunski.pl>
Date:   Tue Jun 27 01:16:52 2023 +0200

    MeshBandedTriangle implemented

diff --git a/src/main/java/org/distorted/library/mesh/MeshBandedTriangle.java b/src/main/java/org/distorted/library/mesh/MeshBandedTriangle.java
index 6be6d70..e186042 100644
--- a/src/main/java/org/distorted/library/mesh/MeshBandedTriangle.java
+++ b/src/main/java/org/distorted/library/mesh/MeshBandedTriangle.java
@@ -32,10 +32,11 @@ import org.distorted.library.main.DistortedLibrary;
  */
 public class MeshBandedTriangle extends MeshBase
   {
+  public static final int MODE_NORMAL  = 0;
+  public static final int MODE_INVERTED= 1;
+  public static final int MODE_FLAT    = 2;
+
   private static final int NUM_CACHE = 20;
-  private static final int MODE_UP   = 0;
-  private static final int MODE_DOWN = 1;
-  private static final int MODE_FLAT = 2;
 
   private float mLeftX, mLeftY;
   private float mRightX, mRightY;
@@ -49,52 +50,49 @@ public class MeshBandedTriangle extends MeshBase
 
   private int remainingVert;
   private int numVertices;
-  private int extraIndex, extraVertices;
+  private int extraBands, extraVertices;
 
   private float[] mCurveCache;
   private float mVecX, mVecY;
-  private int[] mEdgeShape;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
-// mNumBands>=2
 
   private void computeNumberOfVertices()
-     {
-     /*
-     if( mNumBands==2 && extraIndex>0 )
-       {
-       numVertices = 1 + 2*mNumPolygonVertices*(1+extraIndex+2*extraVertices);
-       }
-     else
-       {
-       numVertices = (mNumPolygonVertices*mNumPolygonBands+2)*(mNumPolygonBands-1) - 1;
-       numVertices+= 2*mNumPolygonVertices*(2*extraIndex*extraVertices);
-       }
-
-     remainingVert = numVertices;
-
-      */
-     }
+    {
+    if( mMode==MODE_FLAT )
+      {
+      numVertices = 3;
+      }
+    else if( mMode==MODE_NORMAL )
+      {
+      numVertices = mNumBands*(mNumBands+2) + 4*extraVertices*extraBands;
+      }
+    else
+      {
+      numVertices = mNumBands*(mNumBands+2);
+      }
+
+    remainingVert = numVertices;
+    }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   private void computeCache()
     {
-    /*
     mCurveCache = new float[NUM_CACHE];
-    float[] tmpD = new float[mNumPolygonBands+1];
-    float[] tmpX = new float[mNumPolygonBands+1];
+    float[] tmpD = new float[mNumBands+1];
+    float[] tmpX = new float[mNumBands+1];
 
-    for(int i=1; i<mNumPolygonBands; i++)
+    for(int i=1; i<mNumBands; i++)
       {
-      tmpD[i] = (mPolygonBands[2*i-1]-mPolygonBands[2*i+1]) / (mPolygonBands[2*i]-mPolygonBands[2*i-2]);
-      tmpX[i] = 1.0f - (mPolygonBands[2*i]+mPolygonBands[2*i-2])/2;
+      tmpD[i] = (mBands[2*i-1]-mBands[2*i+1]) / (mBands[2*i]-mBands[2*i-2]);
+      tmpX[i] = 1.0f - (mBands[2*i]+mBands[2*i-2])/2;
       }
 
     tmpD[0] = tmpD[1];
-    tmpD[mNumPolygonBands] = tmpD[mNumPolygonBands-1];
+    tmpD[mNumBands] = tmpD[mNumBands-1];
     tmpX[0] = 0.0f;
-    tmpX[mNumPolygonBands] = 1.0f;
+    tmpX[mNumBands] = 1.0f;
 
     int prev = 0;
     int next = 0;
@@ -106,7 +104,7 @@ public class MeshBandedTriangle extends MeshBase
       if( x>=tmpX[next] )
         {
         prev = next;
-        while( next<=mNumPolygonBands && x>=tmpX[next] ) next++;
+        while( next<=mNumBands && x>=tmpX[next] ) next++;
         }
 
       if( next>prev )
@@ -120,82 +118,7 @@ public class MeshBandedTriangle extends MeshBase
         }
       }
 
-    mCurveCache[NUM_CACHE-1] = tmpD[mNumPolygonBands];
-
-     */
-    }
-/*
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  private float getSpecialQuot(int index)
-    {
-    int num = 1 + extraIndex + 2*extraVertices;
-    int change1 = extraVertices+1;
-    int change2 = num-change1;
-    float quot = 1.0f/(extraIndex+1);
-
-    if( index<change1 )      return index*quot/change1;
-    else if( index>change2 ) return 1-quot + (index-change2)*quot/change1;
-    else                     return (index-change1+1)*quot;
-    }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  private float getQuot(int index, int band, boolean isExtra)
-    {
-    int num = mNumPolygonBands-1-band;
-
-    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;
-          }
-        }
-
-      return (float)index/num;
-      }
-
-    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;
+    mCurveCache[NUM_CACHE-1] = tmpD[mNumBands];
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -220,145 +143,59 @@ public class MeshBandedTriangle extends MeshBase
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  private void figureOutNormalVector2D(int edgeShape, int polyVertex, int polyEndVer, float quot, float xEdge, float yEdge )
+  private void figureOutNormalVector2D(float qx)
     {
-    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;
-        }
-      }
+    mVecX = mNormL[0] + qx*(mNormR[0]-mNormL[0]);
+    mVecY = mNormL[1] + qx*(mNormR[1]-mNormL[1]);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  private float figureOutDerivative(int edgeShape, int polyBand, float quot)
+  private float figureOutDerivative(float qy)
     {
-    if( edgeShape==SHAPE_DD )
+    switch(mMode)
       {
-      return derivative(1-mPolygonBands[2*polyBand]);
+      case MODE_NORMAL  : return derivative(1-qy);
+      case MODE_INVERTED: return -derivative(qy);
+      default           : return 0;
       }
-    if( edgeShape==SHAPE_UU || (quot>=0.5f && edgeShape==SHAPE_DU) || (quot<0.5f && edgeShape==SHAPE_UD) )
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  private float computeElevation(int band)
+    {
+    switch(mMode)
       {
-      return 0;
+      case MODE_NORMAL  : return mBands[2*band+1];
+      case MODE_INVERTED: return mBands[2*(mNumBands-band)+1];
+      default           : return mBands[2*mNumBands+1];
       }
-
-    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,
-                        float[] attribs1, float[] attribs2)
+  private int addStripVertex(int vertex, float qx, float qy, int band, float[] attribs1, float[] attribs2)
     {
     remainingVert--;
 
-    float Xfirst= mPolygonVertices[2*polyVertex  ];
-    float Yfirst= mPolygonVertices[2*polyVertex+1];
-    float Xlast = mPolygonVertices[2*polyEndVer  ];
-    float Ylast = mPolygonVertices[2*polyEndVer+1];
-
-    float xEdge = Xfirst + quot*(Xlast-Xfirst);
-    float yEdge = Yfirst + quot*(Ylast-Yfirst);
-    float zEdge;
+    // android.util.Log.e("D", "new v: qx="+qx+" band="+band);
 
-    float q = mPolygonBands[2*polyBand];
-    float o = mPolygonBands[2*polyBand+1];
-    float t = mPolygonBands[2*mNumPolygonBands-1];
+    float bx = mLeftX + qx*(mRightX-mLeftX);
+    float by = mLeftY + qx*(mRightY-mLeftY);
 
-    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 q = 1-qy;
+    float x = bx + q*(mTopX-bx);
+    float y = by + q*(mTopY-by);
+    float z = computeElevation(band);
 
-    float x = q*xEdge;
-    float y = q*yEdge;
-    float z = o + (t-o)*(zEdge/t);
+    figureOutNormalVector2D(band<mNumBands ? qx : 0.5f);
+    float d = figureOutDerivative(qy);
 
     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 vx = d*mVecX;
     float vy = d*mVecY;
     float vz = mVecX*mVecX + mVecY*mVecY;
@@ -377,74 +214,76 @@ public class MeshBandedTriangle extends MeshBase
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  private int createBandStrip(int vertex, int polyBand, int polyVertex, float[] attribs1, float[] attribs2)
+  private int createBand(int vertex, int band, float[] attribs1, float[] attribs2)
     {
-    if( polyVertex==0 )
+    boolean fromLeft = (band%2 == 0);
+    int extra = ((band<extraBands && mMode==MODE_NORMAL) ? extraVertices : 0);
+    int n = mNumBands-band-1;
+
+    float b = 1.0f/(mNumBands-band);
+    float dxb = fromLeft ? b : -b;
+    float sdx = dxb/(extra+1);
+
+    float qx = fromLeft ? 0.0f : 1.0f;
+
+    int bb = mMode==MODE_NORMAL ? band   : mNumBands-band;
+    int bt = mMode==MODE_NORMAL ? band+1 : mNumBands-(band+1);
+
+    float qyb = mBands[2*bb];
+    float qyt = mBands[2*bt];
+
+    if( mMode!=MODE_NORMAL )
       {
-      vertex = addVertex(vertex,polyBand,0,1,0,attribs1,attribs2);
-      if( polyBand>0 ) vertex = addVertex(vertex,polyBand,0,1,0,attribs1,attribs2);
+      qyb = 1-qyb;
+      qyt = 1-qyt;
       }
 
-    boolean specialCase = mNumPolygonBands==2 && polyBand==0 && extraIndex>0;
-    boolean isExtra = polyBand<extraIndex;
-    int numPairs = specialCase ? extraIndex+1 : mNumPolygonBands-1-polyBand;
-    if( isExtra ) numPairs += 2*extraVertices;
+    vertex = addStripVertex(vertex, qx, qyb, band, attribs1,attribs2);
 
-    int polyEndVer = polyVertex==mNumPolygonVertices-1 ? 0 : polyVertex+1;
-    float quot1, quot2;
+    for(int v=0; v<extra; v++)
+      {
+      vertex = addStripVertex(vertex, qx          , qyt, band+1, attribs1,attribs2);
+      vertex = addStripVertex(vertex, qx+(v+1)*sdx, qyb, band  , attribs1,attribs2);
+      }
 
-    for(int index=0; index<numPairs; index++)
+    if( n>0 )
       {
-      if( specialCase )
-        {
-        quot1 = 1.0f;
-        quot2 = getSpecialQuot(index+1);
-        }
-      else
+      float t = 1.0f/n;
+      float dxt = fromLeft ? t : -t;
+
+      for(int v=0; v<n; v++)
         {
-        quot1 = getQuot(index  ,polyBand+1, isExtra);
-        quot2 = getQuot(index+1,polyBand  , isExtra);
+        vertex=addStripVertex(vertex, qx+ v   *dxt, qyt, band+1, attribs1, attribs2);
+        vertex=addStripVertex(vertex, qx+(v+1)*dxb, qyb, band  , attribs1, attribs2);
         }
+      }
 
-      vertex = addVertex(vertex,polyBand+1,polyVertex,polyEndVer,quot1,attribs1,attribs2);
-      vertex = addVertex(vertex,polyBand  ,polyVertex,polyEndVer,quot2,attribs1,attribs2);
+    qx = 1-qx;
+
+    for(int v=0; v<=extra; v++)
+      {
+      vertex = addStripVertex(vertex, qx              , qyt, band+1, attribs1,attribs2);
+      vertex = addStripVertex(vertex, qx-dxb+(v+1)*sdx, qyb, band  , attribs1,attribs2);
       }
 
     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);
-
-     */
+    if( mMode==MODE_FLAT )
+      {
+      addStripVertex(0, 0, 1, 0, attribs1,attribs2);
+      addStripVertex(1, 0, 0, 0, attribs1,attribs2);
+      addStripVertex(2, 1, 1, 0, attribs1,attribs2);
+      }
+    else
+      {
+      int vertex=0;
+      for(int b=0; b<mNumBands; b++) vertex=createBand(vertex, b, attribs1, attribs2);
+      }
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -475,13 +314,13 @@ public class MeshBandedTriangle extends MeshBase
  *                   band which defines the strip aong the 'left-right' edge.
  *                   If FLAT, everything is flat anyway, so we disregard the bands and return a mesh
  *                   with 3 vertices.
- * @param exIndex    This and the next parameter describe how to make the mesh denser at the
- *                   'left' and 'right' vertices. If e.g. exIndex=3 and exVertices=2, then 3 triangles
+ * @param exBands    This and the next parameter describe how to make the mesh denser at the
+ *                   'left' and 'right' vertices. If e.g. exBands=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.
  * @param exVertices See above.
  */
-  public MeshBandedTriangle(float[] vL, float[] vR, float[] vT, float[] bands, float[] normL, float[] normR, int mode, int exIndex, int exVertices)
+  public MeshBandedTriangle(float[] vL, float[] vR, float[] vT, float[] bands, float[] normL, float[] normR, int mode, int exBands, int exVertices)
     {
     super();
 
@@ -497,8 +336,8 @@ public class MeshBandedTriangle extends MeshBase
     mNormR= normR;
 
     mBands        = bands;
-    mNumBands     = mBands.length /2;
-    extraIndex    = exIndex;
+    mNumBands     = mBands.length/2 -1;
+    extraBands    = exBands;
     extraVertices = exVertices;
 
     computeNumberOfVertices();
