Difference between revisions of "Pseudo Mutli-Dimensional Array (3D)"

From Second Life Wiki
Jump to navigation Jump to search
(duplicate. redirecting to article with shorter/simpler name (3D Pseudo-Array))
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
In my previous article [[Pseudo Multi-Dimensional Array]], I discussed how to acheive 2 dimensional array behavior in LSL.  In this article, we will look at doing the same for a 3 dimensional array.
#REDIRECT [[3D Pseudo-Array]]
 
A list is defined as “a heterogeneous list of the other data types”.  It is linear by nature and can be thought of as a single column.  At the physical level, arrays are stored in contiguous sections of memory. (Another linear structure)  An algorithm is used to translate between the logical rows and columns and the physical level of addresses.
 
 
Consider a [[LSL_list]] with 48 elements.  Normally we visualize this as a vertical column.  In this case, it is convenient to think of them as 2 tables of 4 rows and 6 columns.  Notice we are starting with element 1.  Element zero is reserved for metadata about the size of the array.
 
 
 
'''Layer 0'''
<table width="20%">
    <tr><td>&nbsp;</td><td>'''0'''</td><td>'''1'''</td><td>'''2'''</td><td>'''3'''</td><td>'''4'''</td><td>'''5'''</td></tr>
    <tr><td>'''0'''</td><td> 1</td><td> 2</td><td> 3</td><td> 4</td><td> 5</td><td> 6</td></tr>
    <tr><td>'''1'''</td><td> 7</td><td> 8</td><td> 9</td><td>10</td><td>11</td><td>12</td></tr>
    <tr><td>'''2'''</td><td>13</td><td>14</td><td>15</td><td>16</td><td>17</td><td>18</td></tr>
    <tr><td>'''3'''</td><td>19</td><td>20</td><td>21</td><td>22</td><td>23</td><td>24</td></tr>
</table>
 
'''Layer 1'''
<table width="20%">
    <tr><td>&nbsp;</td><td>'''0'''</td><td>'''1'''</td><td>'''2'''</td><td>'''3'''</td><td>'''4'''</td><td>'''5'''</td></tr>
    <tr><td>'''0'''</td><td>25</td><td>26</td><td>27</td><td>28</td><td>29</td><td>30</td></tr>
    <tr><td>'''1'''</td><td>31</td><td>32</td><td>33</td><td>34</td><td>35</td><td>36</td></tr>
    <tr><td>'''2'''</td><td>37</td><td>38</td><td>39</td><td>40</td><td>41</td><td>42</td></tr>
    <tr><td>'''3'''</td><td>43</td><td>44</td><td>45</td><td>46</td><td>47</td><td>48</td></tr>
</table>
 
 
'''Figure 1.'''  Indexes of a list arranged in rows and columns.
 
 
Our addressing algorithm can use the following formula:
 
index = (rows * cols * layer) + row * cols + col + 1
 
'''Where:'''
*row is the row number of the element
*cols is the number of columns in the array
*col is the column number of the element
 
 
'''Example:''' Find the index for the data stored in layer 1, row 3 and column 4.
 
8index = (rows * cols * layer) + row * cols + col + 1
8index = (4 * 6 * 1) + (3 * 6 + 4) + 1
8index = 47
 
 
We will implement this in a set of functions.  These functions raise the level of abstraction and give us the ability to manipulate arrays referencing rows and columns.
 
 
 
== LSL Implementation: ==
<lsl>
//------------------------------------------------------------------------------
// Function: initIntArray
// Descripton:
//      Given the number of rows, columns and layers, create an list of integers
//      that will be used as an array.
//------------------------------------------------------------------------------
list makeArray ( integer rows, integer cols, integer layers )
{
    integer i;
    list array = [];
    array += < rows, cols, layers >;
    for (i=0; i < rows * cols * layers; ++i)
    {
        array += 0;
    }
    return array;
}
 
//------------------------------------------------------------------------------
// Function: getElement
// Descripton:
//      Return the integer which is stored in the given array
//      at the given row and column numbers.
//------------------------------------------------------------------------------
integer getElement( list array, integer row, integer col, integer layer )
{
    vector metadata = llList2Vector( array, 0);
    integer rows = (integer)metadata.x;
    integer cols = (integer)metadata.y;
    integer index = (rows * cols * layer) + (row * cols + col + 1);
    return llList2Integer( array, index);
}
 
//------------------------------------------------------------------------------
// Function: setElement
// Descripton:
//      Set the value in the specified row and columnn and return
//      the array.
//------------------------------------------------------------------------------
list setElement( list array, integer row, integer col, integer layer, integer value )
{
    vector metadata = llList2Vector( array, 0);
    integer rows = (integer)metadata.x;
    integer cols = (integer)metadata.y;
    integer index = (rows * cols * layer) + (row * cols + col + 1);
    return llListReplaceList(array, [value], index, index);
}
 
 
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
default
{
    state_entry()
    {
      integer rows = 4;
      integer cols = 4;
      integer layers = 2;
      integer row;
      integer col;
      integer layer;
      integer index;
 
        // Build the array
        list elements= makeArray( rows, cols, layers );
        for (layer = 0; layer < layers; ++layer)
        {
            for (col=0; col < cols; ++col)
            {
                for ( row=0; row < rows; ++row)
                {
                    index = (rows * cols * layer) + (row * cols + col + 1);
                    elements = setElement(elements, row, col,layer, index) ;
                }
            }
        }
 
        for (layer = 0; layer < layers; ++layer)
        {
            for (row=0; row < rows; ++row)
            {
                for ( col=0; col < cols; ++col)
                {
                    // Report the value at (3,4)
                    integer element = getElement( elements,row,col,layer);
                    llOwnerSay(  "layer=" + (string)layer
                              + ", row=" + (string)row
                              + ", col=" + (string)col
                              + ", index=" + (string)element);         
                }
            }
        }
    }//end state entry
}//end default
</lsl>

Latest revision as of 17:13, 21 August 2009

Redirect to: