TiledPattern.java

00001 package edu.stanford.hci.r3.pattern;
00002 
00003 import edu.stanford.hci.r3.units.PatternDots;
00004 import edu.stanford.hci.r3.units.coordinates.StreamedPatternCoordinates;
00005 import edu.stanford.hci.r3.util.DebugUtils;
00006 
00024 public class TiledPattern {
00025 
00030         private int initialDotXOffset;
00031 
00035         private int initialDotYOffset;
00036 
00041         private int initialPatternFileNum = 0;
00042 
00046         private int lastPatternFileUsed;
00047 
00051         private int numDotsXPerFullTile;
00052 
00056         private int numDotsXRightMost;
00057 
00061         private int numDotsYBottomMost;
00062 
00066         private int numDotsYPerFullTile;
00067 
00071         private int numTilesX;
00072 
00076         private int numTilesY;
00077 
00081         private int numTotalColumns;
00082 
00086         private int numTotalRows;
00087 
00092         private StringBuilder[] pattern;
00093 
00097         private StreamedPatternCoordinates patternCoordinateOfOrigin;
00098 
00102         private PatternPackage patternPackage;
00103 
00117         public TiledPattern(PatternPackage thePatternPackage, // where to get pattern from
00118                         int initialPatternFileN, int initialDotHorizOffset, int initialDotVertOffset, //
00119                         int numTilesNeededX, int numTilesNeededY, // how many we'll need
00120                         int numDotsXFromRightMostTiles, int numDotsYFromBottomMostTiles) {
00121 
00122                 // this information is used for the indexing into the first pattern file...
00123                 initialPatternFileNum = initialPatternFileN;
00124                 initialDotXOffset = initialDotHorizOffset;
00125                 initialDotYOffset = initialDotVertOffset;
00126 
00127                 patternPackage = thePatternPackage;
00128 
00129                 // every full tile will have this many dots
00130                 numDotsXPerFullTile = patternPackage.getNumPatternColsPerFile();
00131                 numDotsYPerFullTile = patternPackage.getNumPatternRowsPerFile();
00132 
00133                 // how many tiles were requested in each direction
00134                 numTilesX = numTilesNeededX;
00135                 numTilesY = numTilesNeededY;
00136 
00137                 // number of dots spilled over into the rightmost and bottom most columns...
00138                 numDotsXRightMost = numDotsXFromRightMostTiles;
00139                 numDotsYBottomMost = numDotsYFromBottomMostTiles;
00140 
00141                 // error check some values
00142                 boolean advanceToNextPatternPage = false;
00143 
00144                 if (initialDotXOffset < 0) {
00145                         initialDotXOffset = 0;
00146                 } else if (initialDotXOffset > numDotsXPerFullTile) {
00147                         advanceToNextPatternPage = true;
00148                 }
00149                 if (initialDotYOffset < 0) {
00150                         initialDotYOffset = 0;
00151                 } else if (initialDotYOffset > numDotsYPerFullTile) {
00152                         advanceToNextPatternPage = true;
00153                 }
00154 
00155                 if (advanceToNextPatternPage) {
00156                         // move to the next file! because we were out of bounds
00157                         initialDotXOffset = 0;
00158                         initialDotYOffset = 0;
00159                         initialPatternFileNum++;
00160                 }
00161 
00162                 // how many columns and rows of pattern we will need, given all the above information
00163                 numTotalColumns = (numTilesX - 1) * numDotsXPerFullTile // all columns before the rightmost
00164                                 + numDotsXFromRightMostTiles; // the rightmost tiles
00165                 numTotalRows = (numTilesY - 1) * numDotsYPerFullTile // all rows before the bottom
00166                                 + numDotsYFromBottomMostTiles; // the bottom row
00167 
00168                 // get the origin of the first pattern file
00169                 patternCoordinateOfOrigin = patternPackage
00170                                 .getPatternCoordinateOfOriginOfFile(initialPatternFileNum);
00171 
00172                 // adjust the x coordinate for the offset
00173                 patternCoordinateOfOrigin.setX(new PatternDots(patternCoordinateOfOrigin.getXVal()
00174                                 + initialDotXOffset));
00175                 // adjust the y coordinate to account for the offset
00176                 patternCoordinateOfOrigin.setY(new PatternDots(patternCoordinateOfOrigin.getYVal()
00177                                 + initialDotYOffset));
00178 
00179                 DebugUtils.println("Origin for this patch of pattern: " + patternCoordinateOfOrigin);
00180 
00181                 // read in the pattern information
00182                 loadPattern();
00183         }
00184 
00188         public int getInitialPatternFileNumber() {
00189                 return initialPatternFileNum;
00190         }
00191 
00195         public int getLastPatternFileUsed() {
00196                 return lastPatternFileUsed;
00197         }
00198 
00199         public int getNumDotsXFromRightMostTiles() {
00200                 return numDotsXRightMost;
00201         }
00202 
00206         public int getNumDotsXPerFullTile() {
00207                 return numDotsXPerFullTile;
00208         }
00209 
00210         public int getNumDotsYFromBottomMostTiles() {
00211                 return numDotsYBottomMost;
00212         }
00213 
00217         public int getNumDotsYPerFullTile() {
00218                 return numDotsYPerFullTile;
00219         }
00220 
00224         public double getNumHorizDotsBetweenTiles() {
00225                 return patternPackage.getNumDotsHorizontalBetweenPages();
00226         }
00227 
00231         public int getNumTilesX() {
00232                 return numTilesX;
00233         }
00234 
00238         public int getNumTilesY() {
00239                 return numTilesY;
00240         }
00241 
00245         public int getNumTotalColumns() {
00246                 return numTotalColumns;
00247         }
00248 
00252         public int getNumTotalRows() {
00253                 return numTotalRows;
00254         }
00255 
00259         public double getNumVertDotsBetweenTiles() {
00260                 return patternPackage.getNumDotsVerticalBetweenPages();
00261         }
00262 
00266         public double getOriginXInDots() {
00267                 return patternCoordinateOfOrigin.getXVal();
00268         }
00269 
00273         public double getOriginYInDots() {
00274                 return patternCoordinateOfOrigin.getYVal();
00275         }
00276 
00281         public String getPatternOnRow(int row) {
00282                 return pattern[row].toString();
00283         }
00284 
00289         private void loadPattern() {
00290                 // System.out.println(this);
00291 
00292                 // each string will be numTotalColumns long
00293                 pattern = new StringBuilder[numTotalRows];
00294                 for (int row = 0; row < numTotalRows; row++) {
00295                         pattern[row] = new StringBuilder();
00296                 }
00297 
00298                 // the rectangle of pattern to read from each file goes from
00299                 // 0,0 to numDotsXPerFullTile,numDotsYPerFullTile in dots
00300                 final PatternDots originX = new PatternDots(0);
00301                 final PatternDots originY = new PatternDots(0);
00302                 final PatternDots originXLeftTopMost = new PatternDots(initialDotXOffset);
00303                 final PatternDots originYLeftTopMost = new PatternDots(initialDotYOffset);
00304                 final PatternDots dotsW = new PatternDots(numDotsXPerFullTile);
00305                 final PatternDots dotsH = new PatternDots(numDotsYPerFullTile);
00306                 final PatternDots dotsWRightMost = new PatternDots(numDotsXRightMost);
00307                 final PatternDots dotsHBottomMost = new PatternDots(numDotsYBottomMost);
00308 
00309                 // go through each tile and read in the strings...
00310                 // start looking at the correct pattern file number
00311                 // this enables us to get tiled pattern from different parts of our pattern space.
00312                 int patternFileNumber = initialPatternFileNum;
00313 
00314                 for (int tileRow = 0; tileRow < numTilesY; tileRow++) {
00315                         for (int tileCol = 0; tileCol < numTilesX; tileCol++) {
00316 
00317                                 // by default, we start from the leftmost and topmost dot
00318                                 PatternDots origX = originX;
00319                                 PatternDots origY = originY;
00320 
00321                                 // by default, we want every dot from a pattern file
00322                                 PatternDots width = dotsW;
00323                                 PatternDots height = dotsH;
00324 
00325                                 // if leftmost and topmost tile, we want to start from where we were asked to
00326                                 if (tileCol == 0 && tileRow == 0) {
00327                                         origX = originXLeftTopMost;
00328                                         origY = originYLeftTopMost;
00329                                 }
00330 
00331                                 // if rightmost, we only want the dots that spill over into the rightmost column
00332                                 if (tileCol == numTilesX - 1) {
00333                                         width = dotsWRightMost;
00334                                 }
00335 
00336                                 // if bottommost, we only want the dots that spill into the bottommost row
00337                                 if (tileRow == numTilesY - 1) {
00338                                         height = dotsHBottomMost;
00339                                 }
00340 
00341                                 // if we are on the rightmost or bottommost tile, we need to use alternate bounds
00342                                 DebugUtils.println("PatternFileNumber: " + patternFileNumber);
00343                                 final String[] patternTile = patternPackage.readPatternFromFile(patternFileNumber, //
00344                                                 origX, origY, width, height);
00345 
00346                                 // copy the patternTile into our pattern variable
00347                                 // figure out which actual pattern row we start from...
00348                                 int actualPatternRow = tileRow * numDotsYPerFullTile;
00349                                 for (String patternTileRow : patternTile) {
00350                                         pattern[actualPatternRow].append(patternTileRow);
00351                                         // next row
00352                                         actualPatternRow++;
00353                                 }
00354 
00355                                 // go to the next file, but keep track of which file we last used.
00356                                 lastPatternFileUsed = patternFileNumber;
00357                                 patternFileNumber++;
00358                         }
00359                 }
00360         }
00361 
00365         public String toString() {
00366                 return "Tiling Information {\n" //
00367                                 + "\t" + patternCoordinateOfOrigin + "\n" + "\tDotsX: " + numTotalColumns
00368                                 + " DotsY: "
00369                                 + numTotalRows + "\n" + "\t" + numTilesX
00370                                 + " Tile(s) in X, with "
00371                                 + numDotsXRightMost
00372                                 + " horizontal dots from the rightmost tiles.\n" //
00373                                 + "\t" + numTilesY + " Tile(s) in Y, with "
00374                                 + numDotsYBottomMost
00375                                 + " vertical dots from the bottommost tiles.\n" //
00376                                 + "}";
00377         }
00378 }

Generated on Sat Apr 14 18:21:39 2007 for R3 Paper Toolkit by  doxygen 1.4.7