commit 80cb15abc2b4b7b97c1bf9590114da99fe3386fb
Author: leszek <leszek@koltunski.pl>
Date:   Tue May 16 22:58:42 2017 +0100

    Change of API in anticipation for MeshCubes of any depth (currently only 0 and 1 work)

diff --git a/src/main/java/org/distorted/library/MeshCubes.java b/src/main/java/org/distorted/library/MeshCubes.java
index c1da802..932a88f 100644
--- a/src/main/java/org/distorted/library/MeshCubes.java
+++ b/src/main/java/org/distorted/library/MeshCubes.java
@@ -63,7 +63,7 @@ public class MeshCubes extends MeshObject
        }
      }
    
-   private int mCols, mRows;
+   private int mCols, mRows, mSlices;
    private int[][] mCubes;
    private ArrayList<Edge> mEdges = new ArrayList<>();
 
@@ -72,22 +72,6 @@ public class MeshCubes extends MeshObject
    private int mEdgeNum;
    private int mSideWalls;
 
-   private float[] mBoundingVert;
-
-   private static float[] mBoundingVert1 = new float[] { -0.5f,+0.5f, FRONTZ,
-                                                         -0.5f,-0.5f, FRONTZ,
-                                                         +0.5f,+0.5f, FRONTZ,
-                                                         +0.5f,-0.5f, FRONTZ };
-
-   private static float[] mBoundingVert2 = new float[] { -0.5f,+0.5f, FRONTZ,
-                                                         -0.5f,-0.5f, FRONTZ,
-                                                         +0.5f,+0.5f, FRONTZ,
-                                                         +0.5f,-0.5f, FRONTZ,
-                                                         -0.5f,+0.5f, BACKZ ,
-                                                         -0.5f,-0.5f, BACKZ ,
-                                                         +0.5f,+0.5f, BACKZ ,
-                                                         +0.5f,-0.5f, BACKZ  };
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // a Block is split into two triangles along the NE-SW line iff it is in the top-right
 // or bottom-left quadrant of the grid.
@@ -100,7 +84,7 @@ public class MeshCubes extends MeshObject
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // return the number of vertices our grid will contain
 
-   private int computeDataLength(boolean frontOnly)
+   private int computeDataLength(int slices)
       {
       int frontWalls=0, frontSegments=0, triangleShifts=0, windingShifts=0;
       int shiftCol = (mCols-1)/2;
@@ -127,8 +111,8 @@ public class MeshCubes extends MeshObject
 
       int frontVert = 2*( frontWalls + 2*frontSegments - 1) +2*triangleShifts + windingShifts;
       int sideVert  = 2*( mSideWalls + mSideBends + mEdgeNum -1);
-      int firstWinding= (!frontOnly && (frontVert+1)%2==1 ) ? 1:0;
-      int dataL = frontOnly ? frontVert : (frontVert+1) +firstWinding+ (1+sideVert+1) + (1+frontVert);
+      int firstWinding= (slices>0 && (frontVert+1)%2==1 ) ? 1:0;
+      int dataL = slices==0 ? frontVert : (frontVert+1) +firstWinding+ (1+sideVert+1) + (1+frontVert);
 /*
       android.util.Log.e("CUBES","triangleShifts="+triangleShifts+" windingShifts="+windingShifts+" winding1="+firstWinding+" frontVert="+frontVert+" sideVert="+sideVert);
       android.util.Log.e("CUBES", "frontW="+frontWalls+" fSegments="+frontSegments+" sWalls="+mSideWalls+" sSegments="+mEdgeNum+" sideBends="+mSideBends+" dataLen="+dataL );
@@ -183,10 +167,11 @@ public class MeshCubes extends MeshObject
 */
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-   private void prepareDataStructures(int cols, String desc, boolean frontOnly)
+   private void prepareDataStructures(int cols, String desc, int slices)
      {
      mRows      =0;
      mCols      =0;
+     mSlices    =slices;
      numVertices=0;
      
      if( cols>0 && desc.contains("1") )
@@ -200,12 +185,10 @@ public class MeshCubes extends MeshObject
          for(int i=0; i<mRows; i++)
            mCubes[i][j] = (desc.charAt(i*mCols+j) == '1' ? 1:0);
 
-       buildBoundingVert(frontOnly);
-
-//android.util.Log.d("cubes", "VERT STRING:"+debug(mBoundingVert,3));
+       //android.util.Log.d("cubes", "VERT STRING:"+debug(mBoundingVert,3));
 
        markRegions();
-       numVertices = computeDataLength(frontOnly);
+       numVertices = computeDataLength(slices);
 
        remainingVert = numVertices;
        }
@@ -214,10 +197,11 @@ public class MeshCubes extends MeshObject
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // full grid
 
-   private void prepareDataStructures(int cols, int rows, boolean frontOnly)
+   private void prepareDataStructures(int cols, int rows, int slices)
      {
      mRows       =rows;
      mCols       =cols;
+     mSlices     =slices;
      numVertices =   0;
 
      if( cols>0 && rows>0 )
@@ -228,205 +212,13 @@ public class MeshCubes extends MeshObject
          for(int i=0; i<mRows; i++)
            mCubes[i][j] = 1;
 
-       mBoundingVert = frontOnly ? mBoundingVert1 : mBoundingVert2;
-
-//android.util.Log.d("cubes", "VERT FULL:"+debug(mBoundingVert,3));
-
        markRegions();
-       numVertices = computeDataLength(frontOnly);
+       numVertices = computeDataLength(slices);
 
        remainingVert = numVertices;
        }
      }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  private float retRightmost(int row)
-    {
-    if( row==0 )
-      {
-      for(int i=mCols-1; i>=0; i--)
-        if( mCubes[0][i]==1 )
-          return (float)(i+1);
-      }
-    else if(row==mRows)
-      {
-      for(int i=mCols-1; i>=0; i--)
-        if( mCubes[mRows-1][i]==1 )
-          return (float)(i+1);
-      }
-    else
-      {
-      for(int i=mCols-1; i>=0; i--)
-        if( mCubes[row][i]==1 || mCubes[row-1][i]==1 )
-          return (float)(i+1);
-      }
-
-    return 0.0f;
-    }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  private float retLeftmost(int row)
-    {
-    if( row==0 )
-      {
-      for(int i=0; i<mCols; i++)
-        if( mCubes[0][i]==1 )
-          return (float)i;
-      }
-    else if(row==mRows)
-      {
-      for(int i=0; i<mCols; i++)
-        if( mCubes[mRows-1][i]==1 )
-          return (float)i;
-      }
-    else
-      {
-      for(int i=0; i<mCols; i++)
-        if( mCubes[row][i]==1 || mCubes[row-1][i]==1 )
-          return (float)i;
-      }
-
-    return (float)mCols;
-    }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  private int addLeftmost(float[] temp, int row, int index)
-    {
-    float x2 = retLeftmost(row)/mCols - 0.5f;
-    float y2 = 0.5f - (float)row/mRows;
-
-    if( index>1 )
-      {
-      float x0 = temp[2*index-4];
-      float y0 = temp[2*index-3];
-      float x1 = temp[2*index-2];
-      float y1 = temp[2*index-1];
-
-      while( (x0-x2)*(y0-y1) >= (x0-x1)*(y0-y2) )
-        {
-        if( --index>1 )
-          {
-          x1 = x0;
-          y1 = y0;
-          x0 = temp[2*index-4];
-          y0 = temp[2*index-3];
-          }
-        else break;
-        }
-      }
-
-    temp[2*index  ] = x2;
-    temp[2*index+1] = y2;
-    return index+1;
-    }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  private int addRightmost(float[] temp, int row, int index)
-    {
-    float x2 = retRightmost(row)/mCols - 0.5f;
-    float y2 = 0.5f - (float)row/mRows;
-
-    if( index>1 )
-      {
-      float x0 = temp[2*index-4];
-      float y0 = temp[2*index-3];
-      float x1 = temp[2*index-2];
-      float y1 = temp[2*index-1];
-
-      while( (x0-x2)*(y0-y1) <= (x0-x1)*(y0-y2) )
-        {
-        if( --index>1 )
-          {
-          x1 = x0;
-          y1 = y0;
-          x0 = temp[2*index-4];
-          y0 = temp[2*index-3];
-          }
-        else break;
-        }
-      }
-
-    temp[2*index  ] = x2;
-    temp[2*index+1] = y2;
-    return index+1;
-    }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-// fill up the mBoundingVert array with the smallest set of vertices which form the same convex hull
-// like the whole thing.
-
-  private void buildBoundingVert(boolean frontOnly)
-    {
-    int lVert=0, rVert=0;
-    int firstRow=0, lastRow=mRows-1;
-
-    for(int i=0; i<mRows; i++)
-      for(int j=0; j<mCols; j++)
-        if(mCubes[i][j] !=0 )
-          {
-          firstRow = i;
-          i=mRows;
-          j=mCols;
-          }
-
-    for(int i=mRows-1; i>=0; i--)
-      for(int j=0; j<mCols; j++)
-        if(mCubes[i][j] !=0 )
-          {
-          lastRow = i;
-          i=-1;
-          j=mCols;
-          }
-
-//android.util.Log.d("cubes", "first :"+firstRow+" last: "+lastRow);
-
-    int numLines = lastRow-firstRow+2;
-
-    float[] tempL = new float[2*numLines];
-    float[] tempR = new float[2*numLines];
-
-    for(int i=firstRow; i<=lastRow+1; i++)
-      {
-      lVert = addLeftmost (tempL,i,lVert);
-      rVert = addRightmost(tempR,i,rVert);
-      }
-
-//android.util.Log.d("cubes", "LEFT :"+debug(tempL,2));
-//android.util.Log.d("cubes", "RIGHT:"+debug(tempR,2));
-
-    int numVert = lVert+rVert;
-
-    mBoundingVert = new float[ numVert*(frontOnly ? 3:6) ];
-
-    for(int i=0; i<lVert; i++)
-      {
-      mBoundingVert[3*i  ] = tempL[2*i  ];
-      mBoundingVert[3*i+1] = tempL[2*i+1];
-      mBoundingVert[3*i+2] = FRONTZ;
-      }
-
-    for(int i=0; i<rVert; i++)
-      {
-      mBoundingVert[3*(i+lVert)  ] = tempR[2*i  ];
-      mBoundingVert[3*(i+lVert)+1] = tempR[2*i+1];
-      mBoundingVert[3*(i+lVert)+2] = FRONTZ;
-      }
-
-    if( !frontOnly )
-      {
-      for(int i=0; i<numVert; i++)
-        {
-        mBoundingVert[3*(i+numVert)  ] = mBoundingVert[3*i  ];
-        mBoundingVert[3*(i+numVert)+1] = mBoundingVert[3*i+1];
-        mBoundingVert[3*(i+numVert)+2] = BACKZ;
-        }
-      }
-    }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // Mark all the 'regions' of our grid  - i.e. separate pieces of 'land' (connected blocks that will 
 // be rendered) and 'water' (connected holes in between) with integers. Each connected block of land
@@ -690,13 +482,13 @@ public class MeshCubes extends MeshObject
    
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  private int buildSideGrid(int vertex, float[] attribs)
+  private int buildSideGrid(int vertex, int slices, float[] attribs)
      {
      //android.util.Log.d("CUBES", "buildSide");
 
      for(int i=0; i<mEdgeNum; i++)
        {
-       vertex = buildIthSide(mEdges.get(i), vertex, attribs);
+       vertex = buildIthSide(mEdges.get(i), vertex, slices, attribs);
        } 
       
      return vertex;
@@ -704,7 +496,7 @@ public class MeshCubes extends MeshObject
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  private int buildIthSide(Edge curr, int vertex, float[] attribs)
+  private int buildIthSide(Edge curr, int vertex, int slices, float[] attribs)
      {
      Edge prev; 
      
@@ -888,7 +680,7 @@ public class MeshCubes extends MeshObject
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  private void build(boolean frontOnly)
+  private void build(int slices)
      {
      int vertSoFar=0;
      float[] attribs= new float[(POS_DATA_SIZE+NOR_DATA_SIZE+TEX_DATA_SIZE)*numVertices];
@@ -897,7 +689,7 @@ public class MeshCubes extends MeshObject
 
      vertSoFar = buildFrontBackGrid(true, vertSoFar,attribs);
 
-     if( !frontOnly )
+     if( slices>0 )
        {
        vertSoFar = repeatLast(vertSoFar,attribs);
        if( vertSoFar%2==1 )
@@ -909,7 +701,7 @@ public class MeshCubes extends MeshObject
 
        //android.util.Log.d("MeshCubes","building side grid...");
 
-       vertSoFar = buildSideGrid (vertSoFar,attribs);
+       vertSoFar = buildSideGrid (vertSoFar,slices,attribs);
 
        //android.util.Log.d("MeshCubes","building back grid...");
 
@@ -930,59 +722,52 @@ public class MeshCubes extends MeshObject
      mVertAttribs.put(attribs).position(0);
      }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  float[] getBoundingVertices()
-     {
-     return mBoundingVert;
-     }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // PUBLIC API
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
  * Creates the underlying mesh of vertices, normals, texture coords.
  *    
- * @param cols      Integer helping to parse the next parameter.
- * @param desc      String describing the subset of a MxNx1 cuboid that we want to create.
- *                  Its MxN characters - all 0 or 1 - decide of appropriate field is taken or not.
- *                  <p></p>
- *                  <p>
- *                  <pre>
- *                  For example, (cols=2, desc="111010") describes the following shape:
+ * @param cols   Integer helping to parse the next parameter.
+ * @param desc   String describing the subset of a MxNx1 cuboid that we want to create.
+ *               Its MxN characters - all 0 or 1 - decide of appropriate field is taken or not.
+ *               <p></p>
+ *               <p>
+ *               <pre>
+ *               For example, (cols=2, desc="111010") describes the following shape:
  *
- *                  XX
- *                  X
- *                  X
+ *               XX
+ *               X
+ *               X
  *
- *                  whereas (cols=2,desc="110001") describes
+ *               whereas (cols=2,desc="110001") describes
  *
- *                  XX
+ *               XX
  *
- *                   X
- *                  </pre>
- *                  </p>
- * @param frontOnly Only create the front wall or side and back as well?
+ *                X
+ *               </pre>
+ *               </p>
+ * @param slices Number of slices, i.e. 'depth' of the Mesh.
  */
- public MeshCubes(int cols, String desc, boolean frontOnly)
+ public MeshCubes(int cols, String desc, int slices)
    {
-   super(frontOnly ? 0.0f:1.0f/cols);
-   prepareDataStructures(cols,desc,frontOnly);
-   build(frontOnly);
+   super( (float)slices/cols);
+   prepareDataStructures(cols,desc,slices);
+   build(slices);
    }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
  * Creates a full, hole-less underlying mesh of vertices, normals, texture coords and colors.
  *
- * @param cols      Number of columns.
- * @param rows      Number of rows.
- * @param frontOnly Only create the front wall or side and back as well?
+ * @param cols   Number of columns, i.e. 'width' of the Mesh.
+ * @param rows   Number of rows, i.e. 'height' of the Mesh.
+ * @param slices Number of slices, i.e. 'depth' of the Mesh.
  */
- public MeshCubes(int cols, int rows, boolean frontOnly)
+ public MeshCubes(int cols, int rows, int slices)
    {
-   super(frontOnly ? 0.0f:1.0f/cols);
-   prepareDataStructures(cols,rows,frontOnly);
-   build(frontOnly);
+   super( (float)slices/cols);
+   prepareDataStructures(cols,rows,slices);
+   build(slices);
    }
  }
diff --git a/src/main/java/org/distorted/library/MeshFlat.java b/src/main/java/org/distorted/library/MeshFlat.java
index 548e50a..1693302 100644
--- a/src/main/java/org/distorted/library/MeshFlat.java
+++ b/src/main/java/org/distorted/library/MeshFlat.java
@@ -34,11 +34,6 @@ public class MeshFlat extends MeshObject
   private int mCols, mRows;
   private int remainingVert;
 
-  private static float[] mBoundingVert = new float[] { -0.5f,+0.5f,0.0f,
-                                                       -0.5f,-0.5f,0.0f,
-                                                       +0.5f,+0.5f,0.0f,
-                                                       +0.5f,-0.5f,0.0f };
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // Create a flat, full grid.
 
@@ -167,12 +162,6 @@ public class MeshFlat extends MeshObject
      return ret;
      }
 */
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  float[] getBoundingVertices()
-     {
-     return mBoundingVert;
-     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // PUBLIC API
diff --git a/src/main/java/org/distorted/library/MeshObject.java b/src/main/java/org/distorted/library/MeshObject.java
index 5a5e2da..aa90332 100644
--- a/src/main/java/org/distorted/library/MeshObject.java
+++ b/src/main/java/org/distorted/library/MeshObject.java
@@ -124,17 +124,6 @@ public abstract class MeshObject extends DistortedObject
      return getClass().getSimpleName()+" vertices:"+ numVertices;
      }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-/**
- * Get the minimal set of Vertices which have the same convex hull as the whole set.
- * <p>
- * In case of Flat Meshes, the set is obviously just the 4 corners. In case of the Cubes Mesh,
- * it is a subset of the set of each rightmost- and leftmost- corners in each row.
- * <p>
- * This is used to be able to quickly compute, in window coordinates, the Mesh'es bounding rectangle.
- */
-   abstract float[] getBoundingVertices();
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
  * When rendering this Mesh, do we want to render the Normal vectors as well?
