Project

General

Profile

« Previous | Next » 

Revision 0729bc41

Added by Leszek Koltunski over 8 years ago

Add DistortedGridFactory to share Grids among same-shaped DistortedObjects.

View differences:

src/main/java/org/distorted/library/Distorted.java
387 387
 */
388 388
  public static void onDestroy()
389 389
    {
390
    DistortedGridFactory.release();
390 391
    DistortedObjectList.release();
391 392
    DistortedNode.release();
392 393
    EffectQueue.reset();
src/main/java/org/distorted/library/DistortedBitmap.java
81 81
     mSizeX= width;
82 82
     mSizeY= height;
83 83
     mSizeZ= 1;
84
     mGrid = new DistortedBitmapGrid(xsize,ysize);
84
     mGrid = DistortedGridFactory.getGrid(xsize,ysize);
85 85
     initializeData();
86 86
     }
87 87

  
src/main/java/org/distorted/library/DistortedCubes.java
96 96
   mSizeX= cubeSize*Cs;
97 97
   mSizeY= cubeSize*Rs;
98 98
   mSizeZ= frontOnly ? 1 : cubeSize;
99
   mGrid = new DistortedCubesGrid(cols,desc, frontOnly);
99
   mGrid = DistortedGridFactory.getGrid(cols,desc, frontOnly);
100 100
   initializeData();
101 101
   }
102 102

  
......
120 120

  
121 121
///////////////////////////////////////////////////////////////////////////////////////////////////
122 122

  
123
   protected DistortedObject deepCopy(int flags)
124
     {
125
     return new DistortedCubes(this,flags);
126
     }
123
 protected DistortedObject deepCopy(int flags)
124
   {
125
   return new DistortedCubes(this,flags);
126
   }
127 127

  
128 128
///////////////////////////////////////////////////////////////////////////////////////////////////   
129 129
 }
src/main/java/org/distorted/library/DistortedCubesGrid.java
159 159
     }   
160 160
*/ 
161 161
///////////////////////////////////////////////////////////////////////////////////////////////////
162
// desc is guaranteed to be padded with 0s in the end (DistortedCubes constructor does it)
162 163

  
163 164
   private void buildGrid(int cols, String desc, boolean frontOnly)
164 165
     {
......
166 167
     mCols     =0;
167 168
     dataLength=0;
168 169
     
169
     if( cols>0 )
170
     if( cols>0 && desc.contains("1") )
170 171
       {
171
       int reallen = desc.length();
172
       int len = reallen;
172
       mCols = cols;
173
       mRows = desc.length()/cols;
173 174

  
174
       if( (reallen/cols)*cols != reallen )
175
         {
176
         len = ((reallen/cols)+1)*cols; 
177
         for(int i=reallen; i<len; i++) desc += "0";
178
         }
179
    
180
       if( desc.contains("1") )
181
         {
182
         mCols = cols;
183
         mRows = len/cols;
184

  
185
         mCubes = new short[mRows][mCols];
175
       mCubes = new short[mRows][mCols];
186 176
       
187
         for(int j=0; j<mCols; j++) 
188
           for(int i=0; i<mRows; i++)
189
             mCubes[i][j] = (short)(desc.charAt(i*mCols+j) == '1' ? 1:0); 
177
       for(int j=0; j<mCols; j++)
178
         for(int i=0; i<mRows; i++)
179
           mCubes[i][j] = (short)(desc.charAt(i*mCols+j) == '1' ? 1:0);
190 180
       
191
         markRegions();
192
         dataLength = computeDataLength(frontOnly);
193
         }
181
       markRegions();
182
       dataLength = computeDataLength(frontOnly);
194 183
       }
195 184
     }
196 185
 
src/main/java/org/distorted/library/DistortedGridFactory.java
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Distorted is distributed in the hope that it will be useful,                                  //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

  
20
package org.distorted.library;
21

  
22
import java.util.HashMap;
23
/**
24
 * HashMap of all DistortedObjectGrids ever created. The point: we can share the Grids among
25
 * DistortedObjects that are of the same shape.
26
 */
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28

  
29
final class DistortedGridFactory
30
  {
31
  private static HashMap<String,DistortedObjectGrid> mGrids = new HashMap<>();
32

  
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34
// DistortedCubesGrid
35

  
36
  static synchronized DistortedObjectGrid getGrid(int cols, String desc, boolean frontOnly)
37
    {
38
    String d = desc+("_"+cols+"_"+(frontOnly?"1":"0"));
39

  
40
    Object o = mGrids.get(d);
41

  
42
    if( o!=null ) return (DistortedObjectGrid)o;
43

  
44
    DistortedCubesGrid grid = new DistortedCubesGrid(cols,desc,frontOnly);
45

  
46
    mGrids.put(d,grid);
47

  
48
    return grid;
49
    }
50

  
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52
// DistortedBitmapGrid
53

  
54
  static synchronized DistortedObjectGrid getGrid(int cols, int rows)
55
    {
56
    String d = cols+"+"+rows;
57

  
58
    Object o = mGrids.get(d);
59

  
60
    if( o!=null ) return (DistortedObjectGrid)o;
61

  
62
    DistortedBitmapGrid grid = new DistortedBitmapGrid(cols,rows);
63

  
64
    mGrids.put(d,grid);
65

  
66
    return grid;
67
    }
68

  
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

  
71
  static synchronized void release()
72
    {
73
    mGrids.clear();
74
    }
75

  
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77
  }

Also available in: Unified diff