[committed] Accessors for DiscreteLODNode and DistanceSwitchModel

added accessors to allow for editing of discretelodnode and distancemodel after creation…



Index: src/com/jme/scene/lod/DiscreteLodNode.java
===================================================================
--- src/com/jme/scene/lod/DiscreteLodNode.java  (revision 4052)
+++ src/com/jme/scene/lod/DiscreteLodNode.java  (working copy)
@@ -68,6 +68,14 @@
+
+       /**
+        * Gets the switch model associated with this node.
+        * @return
+        */
+       public  SwitchModel             getSwitchModel() {
+               return model;
+       }
 
Index: src/com/jme/scene/DistanceSwitchModel.java
===================================================================
--- src/com/jme/scene/DistanceSwitchModel.java  (revision 4052)
+++ src/com/jme/scene/DistanceSwitchModel.java  (working copy)
@@ -90,6 +90,30 @@
        /**
+        * Returns the number of children that distances exist for.
+        * @return
+        */
+       public  int             getNumChildren() {
+               return numChildren;
+       }
+
+       /**
+        * Gets all the model min distances.
+        * @return
+        */
+       public  float[]         getModelMinDistances() {
+               return modelMin;
+       }
+
+       /**
+        * Gets all the model maximum distances.
+        * @return
+        */
+       public  float[]         getModelMaxDistances() {
+               return modelMax;
+       }
+

+        /**
+        * Creates larger arrays for the max and mins while copying existing data.
+        * @param newLength
+        */
+       public  void    reallocateArrays(int newLength) {
+               // new size is less than the existing size....ignore the reallocation
+               if(newLength < numChildren) {
+                       return;
+               }
+
+               // create the new arrays
+               float   modelMinNew[] = new float[newLength];
+               float   modelMaxNew[] = new float[newLength];
+               float   worldMinNew[] = new float[newLength];
+               float   worldMaxNew[] = new float[newLength];
+
+               // copy in existing
+               for(int i = 0; i < numChildren; i++) {
+                       modelMinNew[i] = modelMin[i];
+                       modelMaxNew[i] = modelMax[i];
+                       worldMinNew[i] = worldMin[i];
+                       worldMaxNew[i] = worldMax[i];
+               }
+
+               // reassign
+               modelMin = modelMinNew;
+               modelMax = modelMaxNew;
+               worldMin = worldMinNew;
+               worldMax = worldMaxNew;
+               numChildren = newLength;
+       }

I know it used this way in jME already, but generally a getXY method to modify something is not very intuitive/good. What about a setter instead to keep internal data encapsulated?

i don't think i follow you…

are you talking about the getSwitchModel in DiscreteLodNode or the gets in the DistanceSwitchModel?

I proposed to call


someDistanceSwitchModel.setModelMinDistance(2, 1.5f);


instead of


someDistanceSwitchModel.getModelMinDistances()[2] = 1.5f;


You can make the reallocateArrays method private then, as well.

ah ok…this makes sense…

so in the



setModelMinDistance(2, 1.5f);



method i reallocate here if the index is greater than numChildren?
the functionality i'm aiming for is that the user can create a DiscreteLodNode and then add spatials to it then afterwards modify the min and max distance parameters per spatial index....
so i would need to be able to arbitrarily resize the arrays...

so the modification is now



+++ DistanceSwitchModel.java    (working copy)
@@ -90,6 +90,32 @@
        }
 
        /**
+        * Returns the number of children that distances exist for.
+        * @return
+        */
+       public  int             getNumChildren() {
+               return numChildren;
+       }
+
+       /**
+        * Gets the minimum distance for this spatial index.
+        * @param index
+        * @return
+        */
+       public  float           getModelMinDistance(int index) {
+               return modelMin[index];
+       }
+
+       /**
+        * Gets the maximum distance for this spatial index.
+        * @param index
+        * @return
+        */
+       public  float           getModelMaxDistance(int index) {
+               return modelMax[index];
+       }
+
+       /**
         *
         * <code>setModelMinDistance</code> sets the minimum distance that a
         * particular child should be used.
@@ -100,11 +126,40 @@
         *            the minimum of this child.
         */
        public void setModelMinDistance(int index, float minDist) {
-
+               if(index >= numChildren) {
+                       reallocateArrays(index + 1);
+               }
+
                modelMin[index] = minDist;
        }
 
        /**
+        * Creates larger arrays for the max and mins while copying existing data.
+        * @param newLength
+        */
+       private void    reallocateArrays(int newLength) {
+               // create the new arrays
+               float   modelMinNew[] = new float[newLength];
+               float   modelMaxNew[] = new float[newLength];
+               float   worldMinNew[] = new float[newLength];
+               float   worldMaxNew[] = new float[newLength];
+
+               // copy in existing
+               for(int i = 0; i < numChildren; i++) {
+                       modelMinNew[i] = modelMin[i];
+                       modelMaxNew[i] = modelMax[i];
+                       worldMinNew[i] = worldMin[i];
+                       worldMaxNew[i] = worldMax[i];
+               }
+
+               // reassign
+               modelMin = modelMinNew;
+               modelMax = modelMaxNew;
+               worldMin = worldMinNew;
+               worldMax = worldMaxNew;
+               numChildren = newLength;
+       }
+       /**
         *
         * <code>setModelMaxDistance</code> sets the maximum distance that a
         * particular child should be used.
@@ -115,6 +170,10 @@
         *            the maximum of this child.
         */
        public void setModelMaxDistance(int index, float maxDist) {
+               if(index >= numChildren) {
+                       reallocateArrays(index + 1);
+               }
+
                modelMax[index] = maxDist;
        }
 
@@ -131,7 +190,10 @@
         *            the maximum of this child.
         */
        public void setModelDistance(int index, float minDist, float maxDist) {
-
+               if(index >= numChildren) {
+                       reallocateArrays(index + 1);
+               }
+
                modelMin[index] = minDist;
                modelMax[index] = maxDist;
        }