RenderingTechniqueQuadratic.java

00001 package edu.stanford.hci.r3.render.ink;
00002 
00003 import java.awt.Graphics2D;
00004 import java.awt.geom.GeneralPath;
00005 import java.util.List;
00006 
00007 import edu.stanford.hci.r3.pen.ink.InkStroke;
00008 
00021 class RenderingTechniqueQuadratic implements RenderingTechnique {
00022 
00023         private static int ERROR_THRESHOLD = 500;
00024 
00025         public void render(Graphics2D g2d, final List<InkStroke> strokes) {
00026                 
00027                 g2d.setStroke(DEFAULT_INK_STROKE);
00028                 
00029                 // Each Stroke will be One Path (it's just more efficient this way)
00030                 for (final InkStroke s : strokes) {
00031 
00032                         final double[] xArr = s.getXSamples();
00033                         final double[] yArr = s.getYSamples();
00034 
00035                         final GeneralPath strokePath = new GeneralPath();
00036                         final int len = xArr.length;
00037                         if (len > 0) {
00038                                 strokePath.moveTo(xArr[0], yArr[0]);
00039                         }
00040 
00041                         // keeps last known "good point"
00042                         double lastGoodX = xArr[0];
00043                         double lastGoodY = yArr[0];
00044 
00045                         // connect the samples w/ quadratic curve segments
00046                         // in the future, do catmull-rom, because that's ideal...
00047                         int numPointsCollected = 0;
00048                         for (int i = 0; i < len; i++) {
00049                                 final double currX = xArr[i];
00050                                 final double currY = yArr[i];
00051 
00052                                 numPointsCollected++;
00053 
00054                                 final double diffFromLastX = currX - lastGoodX;
00055                                 final double diffFromLastY = currY - lastGoodY;
00056 
00057                                 if (Math.abs(diffFromLastX) > ERROR_THRESHOLD || Math.abs(diffFromLastY) > ERROR_THRESHOLD) {
00058                                         // too much error; eliminate totally random data...
00059                                         // this usually arises from writing outside the margin onto disjoint pattern
00060                                         // (like the anoto pidget)
00061                                         // try just discarding this point!
00062                                         // strokePath.lineTo(lastGoodX, lastGoodY);
00063                                 } else {
00064                                         // OK, not that much error
00065                                         if (numPointsCollected == 2) {
00066                                                 numPointsCollected = 0;
00067                                                 strokePath.quadTo(lastGoodX, lastGoodY, currX, currY);
00068                                         }
00069 
00070                                         // set the last known good point
00071                                         lastGoodX = currX;
00072                                         lastGoodY = currY;
00073                                 }
00074                         }
00075 
00076                         // if there's any points left, just render them
00077                         if (numPointsCollected == 1) {
00078                                 strokePath.lineTo(lastGoodX, lastGoodY);
00079                         }
00080                         g2d.draw(strokePath);
00081                 }
00082         }
00083 
00084 }

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