[Patchanim-commit] SF.net SVN: patchanim: [185] trunk/patchanim/src/com/mebigfatguy/patchanim
Brought to you by:
dbrosius
From: <dbr...@us...> - 2008-02-14 07:18:40
|
Revision: 185 http://patchanim.svn.sourceforge.net/patchanim/?rev=185&view=rev Author: dbrosius Date: 2008-02-13 23:18:45 -0800 (Wed, 13 Feb 2008) Log Message: ----------- allow for documents with user specified patch order Modified Paths: -------------- trunk/patchanim/src/com/mebigfatguy/patchanim/PatchAnimDocument.java trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JColorControlPatchPanel.java trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JPatchAnimFrame.java trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JPatchListPanel.java trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JPatchSamplePanel.java trunk/patchanim/src/com/mebigfatguy/patchanim/io/PatchAnimDoc.xsd trunk/patchanim/src/com/mebigfatguy/patchanim/io/PatchAnimDoc.xsl trunk/patchanim/src/com/mebigfatguy/patchanim/io/PatchAnimExtension.java trunk/patchanim/src/com/mebigfatguy/patchanim/io/PatchAnimIO.java trunk/patchanim/src/com/mebigfatguy/patchanim/main/PatchAnimBundle.java trunk/patchanim/src/com/mebigfatguy/patchanim/resources.properties trunk/patchanim/src/com/mebigfatguy/patchanim/surface/CombinedPatch.java trunk/patchanim/src/com/mebigfatguy/patchanim/surface/PatchCoords.java trunk/patchanim/src/com/mebigfatguy/patchanim/surface/PatchGenerator.java Modified: trunk/patchanim/src/com/mebigfatguy/patchanim/PatchAnimDocument.java =================================================================== --- trunk/patchanim/src/com/mebigfatguy/patchanim/PatchAnimDocument.java 2008-02-14 05:14:11 UTC (rev 184) +++ trunk/patchanim/src/com/mebigfatguy/patchanim/PatchAnimDocument.java 2008-02-14 07:18:45 UTC (rev 185) @@ -29,9 +29,10 @@ */ public class PatchAnimDocument implements Serializable { - private static final long serialVersionUID = -587863884486252874L; + private static final long serialVersionUID = -7412254429829665944L; private boolean dirty; + private int order; private List<CombinedPatch> patches; private int width; private int height; @@ -42,9 +43,10 @@ /** * constructs a new document for the New menu item */ - public PatchAnimDocument() { + public PatchAnimDocument(int patchOrder) { + order = patchOrder; patches = new ArrayList<CombinedPatch>(); - CombinedPatch patch = new CombinedPatch(true); + CombinedPatch patch = new CombinedPatch(order, true); patches.add(patch); width = 200; height = 200; @@ -70,6 +72,14 @@ } /** + * returns the order of the bezier patch used for this document + * @return the order of the patch + */ + public int getOrder() { + return order; + } + + /** * retrieves a list of all the bezier patches * @return the list of all the bezier patches */ Modified: trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JColorControlPatchPanel.java =================================================================== --- trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JColorControlPatchPanel.java 2008-02-14 05:14:11 UTC (rev 184) +++ trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JColorControlPatchPanel.java 2008-02-14 07:18:45 UTC (rev 185) @@ -143,8 +143,9 @@ return; g.setColor(Color.yellow); - for (int u = 0; u < PatchCoords.ORDER; u++) { - for (int v = 0; v < PatchCoords.ORDER; v++) { + int order = coords.getOrder(); + for (int u = 0; u < order; u++) { + for (int v = 0; v < order; v++) { Coordinate c = coords.getCoordinate(u, v); if ((selectedXPt == u) && (selectedYPt == v)) { g.fillOval((int)(((c.getX() * (bounds.width - 5)) / 100.0) + bounds.x), @@ -210,8 +211,9 @@ int minU = 0; int minV = 0; - for (int u = 0; u < PatchCoords.ORDER; u++) { - for (int v = 0; v < PatchCoords.ORDER; v++) { + int order = coords.getOrder(); + for (int u = 0; u < order; u++) { + for (int v = 0; v < order; v++) { Coordinate c = coords.getCoordinate(u, v); double xSq = c.getX() - inputX; xSq *= xSq; Modified: trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JPatchAnimFrame.java =================================================================== --- trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JPatchAnimFrame.java 2008-02-14 05:14:11 UTC (rev 184) +++ trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JPatchAnimFrame.java 2008-02-14 07:18:45 UTC (rev 185) @@ -82,7 +82,7 @@ cp.setLayout(new BorderLayout(4, 4)); JPatchAnimPanel patchPanel = new JPatchAnimPanel(); - document = new PatchAnimDocument(); + document = new PatchAnimDocument(4); documentLocation = null; PatchPanelMediator mediator = PatchPanelMediator.getMediator(); mediator.setDocument(document); @@ -171,6 +171,7 @@ newItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { + ResourceBundle rb = PatchAnimBundle.getBundle(); try { if (document.isDirty()) { int choice = askSave(); @@ -181,13 +182,28 @@ } } - document = new PatchAnimDocument(); + Integer choice = (Integer)JOptionPane.showInputDialog(JPatchAnimFrame.this, + rb.getString(PatchAnimBundle.SETORDER), + rb.getString(PatchAnimBundle.TITLE), + JOptionPane.QUESTION_MESSAGE, + new ImageIcon(JPatchAnimFrame.this.getIconImage()), + new Object[] { Integer.valueOf(3), + Integer.valueOf(4), + Integer.valueOf(5), + Integer.valueOf(6), + Integer.valueOf(7), + Integer.valueOf(8), + Integer.valueOf(9) }, + Integer.valueOf(4)); + if (choice == null) + choice = Integer.valueOf(4); + + document = new PatchAnimDocument(choice.intValue()); documentLocation = null; saveItem.setEnabled(false); PatchPanelMediator mediator = PatchPanelMediator.getMediator(); mediator.setDocument(document); } catch (IOException ioe) { - ResourceBundle rb = PatchAnimBundle.getBundle(); JOptionPane.showMessageDialog(JPatchAnimFrame.this, rb.getString(PatchAnimBundle.SAVEFAILED)); } } @@ -338,7 +354,7 @@ ResourceBundle rb = PatchAnimBundle.getBundle(); JOptionPane.showMessageDialog(JPatchAnimFrame.this, rb.getString(PatchAnimBundle.LOADFAILED)); documentLocation = null; - document = new PatchAnimDocument(); + document = new PatchAnimDocument(4); } finally { PatchPanelMediator mediator = PatchPanelMediator.getMediator(); mediator.setDocument(document); Modified: trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JPatchListPanel.java =================================================================== --- trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JPatchListPanel.java 2008-02-14 05:14:11 UTC (rev 184) +++ trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JPatchListPanel.java 2008-02-14 07:18:45 UTC (rev 185) @@ -130,7 +130,8 @@ else selIndex++; - CombinedPatch newPatch = new CombinedPatch(true); + PatchPanelMediator mediator = PatchPanelMediator.getMediator(); + CombinedPatch newPatch = new CombinedPatch(mediator.getDocument().getOrder(), true); patchListModel.add(selIndex, newPatch); patchList.setSelectedIndex(selIndex); removeButton.setEnabled(patchListModel.getSize() > 1); Modified: trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JPatchSamplePanel.java =================================================================== --- trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JPatchSamplePanel.java 2008-02-14 05:14:11 UTC (rev 184) +++ trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JPatchSamplePanel.java 2008-02-14 07:18:45 UTC (rev 185) @@ -286,8 +286,9 @@ CombinedPatch patch = mediator.getActivePatch(); PatchCoords srcCoords = patch.getPatch(copyColor); PatchCoords dstCoords = patch.getPatch(color); - for (int i = 0; i < PatchCoords.ORDER; i++) { - for (int j = 0; j < PatchCoords.ORDER; j++) { + int order = srcCoords.getOrder(); + for (int i = 0; i < order; i++) { + for (int j = 0; j < order; j++) { Coordinate srcCoord = srcCoords.getCoordinate(i, j); double value = srcCoord.getColor(); Coordinate dstCoord = dstCoords.getCoordinate(i, j); @@ -302,8 +303,9 @@ PatchPanelMediator mediator = PatchPanelMediator.getMediator(); CombinedPatch patch = mediator.getActivePatch(); PatchCoords coords = patch.getPatch(color); - for (int i = 0; i < PatchCoords.ORDER; i++) { - for (int j = 0; j < PatchCoords.ORDER; j++) { + int order = coords.getOrder(); + for (int i = 0; i < order; i++) { + for (int j = 0; j < order; j++) { Coordinate c = coords.getCoordinate(i, j); c.setColor(c.getColor() + d); coords.setCoordinate(i, j, c); @@ -316,8 +318,9 @@ PatchPanelMediator mediator = PatchPanelMediator.getMediator(); CombinedPatch patch = mediator.getActivePatch(); PatchCoords coords = patch.getPatch(color); - for (int i = 0; i < PatchCoords.ORDER; i++) { - for (int j = 0; j < PatchCoords.ORDER; j++) { + int order = coords.getOrder(); + for (int i = 0; i < order; i++) { + for (int j = 0; j < order; j++) { Coordinate c = coords.getCoordinate(i, j); c.setColor(d); coords.setCoordinate(i, j, c); Modified: trunk/patchanim/src/com/mebigfatguy/patchanim/io/PatchAnimDoc.xsd =================================================================== --- trunk/patchanim/src/com/mebigfatguy/patchanim/io/PatchAnimDoc.xsd 2008-02-14 05:14:11 UTC (rev 184) +++ trunk/patchanim/src/com/mebigfatguy/patchanim/io/PatchAnimDoc.xsd 2008-02-14 07:18:45 UTC (rev 185) @@ -29,6 +29,7 @@ <xsd:complexType name="SettingsClass"> <xsd:attribute name="animationType" type="AnimationTypeClass" use="required"/> <xsd:attribute name="height" type="xsd:integer" use="required"/> + <xsd:attribute name="order" type="xsd:positiveInteger" default="4"/> <xsd:attribute name="outOfBoundsColor" type="OutOfBoundsColorClass" use="required"/> <xsd:attribute name="tweenCount" type="xsd:integer" use="required"/> <xsd:attribute name="width" type="xsd:integer" use="required"/> @@ -77,7 +78,7 @@ </xsd:simpleType> <xsd:complexType name="PatchesClass"> <xsd:sequence> - <xsd:element maxOccurs="4" minOccurs="4" name="CombinedPatch" type="CombinedPatchClass"/> + <xsd:element maxOccurs="unbounded" minOccurs="1" name="CombinedPatch" type="CombinedPatchClass"/> </xsd:sequence> </xsd:complexType> <xsd:element name="PatchAnimDoc" type="PatchAnimDocClass"/> Modified: trunk/patchanim/src/com/mebigfatguy/patchanim/io/PatchAnimDoc.xsl =================================================================== --- trunk/patchanim/src/com/mebigfatguy/patchanim/io/PatchAnimDoc.xsl 2008-02-14 05:14:11 UTC (rev 184) +++ trunk/patchanim/src/com/mebigfatguy/patchanim/io/PatchAnimDoc.xsl 2008-02-14 07:18:45 UTC (rev 185) @@ -37,6 +37,9 @@ <xsl:value-of select="pae:getVersion($ext)"/> </xsl:attribute> <Settings> + <xsl:attribute name="order"> + <xsl:value-of select="pae:getOrder($ext)"/> + </xsl:attribute> <xsl:attribute name="width"> <xsl:value-of select="pae:getWidth($ext)"/> </xsl:attribute> Modified: trunk/patchanim/src/com/mebigfatguy/patchanim/io/PatchAnimExtension.java =================================================================== --- trunk/patchanim/src/com/mebigfatguy/patchanim/io/PatchAnimExtension.java 2008-02-14 05:14:11 UTC (rev 184) +++ trunk/patchanim/src/com/mebigfatguy/patchanim/io/PatchAnimExtension.java 2008-02-14 07:18:45 UTC (rev 185) @@ -34,7 +34,6 @@ import com.mebigfatguy.patchanim.PatchAnimDocument; import com.mebigfatguy.patchanim.PatchColor; import com.mebigfatguy.patchanim.surface.Coordinate; -import com.mebigfatguy.patchanim.surface.PatchCoords; public class PatchAnimExtension { private static final String VERSION_URL = "/com/mebigfatguy/patchanim/io/Version.txt"; @@ -60,6 +59,10 @@ } } + public String getOrder(@SuppressWarnings("unused") ExpressionContext context) { + return String.valueOf(paDoc.getOrder()); + } + public String getWidth(@SuppressWarnings("unused") ExpressionContext context) { return String.valueOf(paDoc.getWidth()); } @@ -104,11 +107,11 @@ NodeList nl = new NodeList() { public int getLength() { - return PatchCoords.ORDER * PatchCoords.ORDER; + int order = paDoc.getOrder(); + return order * order; } public Node item(int index) { - // TODO Auto-generated method stub return d.createTextNode(String.valueOf(index)); } }; @@ -117,30 +120,24 @@ } public String getX(@SuppressWarnings("unused") ExpressionContext context, String color, Node patchIndexNode, Node coordIndexNode) { - PatchColor patchColor = PatchColor.valueOf(PatchColor.class, color); - int patchIndex = Integer.parseInt(patchIndexNode.getNodeValue()); - int coordIndex = Integer.parseInt(coordIndexNode.getNodeValue()); - - Coordinate coord = paDoc.getPatches().get(patchIndex).getPatch(patchColor).getCoordinate(coordIndex % PatchCoords.ORDER, coordIndex / PatchCoords.ORDER); - return String.valueOf(coord.getX()); + return String.valueOf(getCoordinate(color, patchIndexNode, coordIndexNode).getX()); } public String getY(@SuppressWarnings("unused") ExpressionContext context, String color, Node patchIndexNode, Node coordIndexNode) { - PatchColor patchColor = PatchColor.valueOf(PatchColor.class, color); - int patchIndex = Integer.parseInt(patchIndexNode.getNodeValue()); - int coordIndex = Integer.parseInt(coordIndexNode.getNodeValue()); - - Coordinate coord = paDoc.getPatches().get(patchIndex).getPatch(patchColor).getCoordinate(coordIndex % PatchCoords.ORDER, coordIndex / PatchCoords.ORDER); - return String.valueOf(coord.getY()); + return String.valueOf(getCoordinate(color, patchIndexNode, coordIndexNode).getY()); } public String getColor(@SuppressWarnings("unused") ExpressionContext context, String color, Node patchIndexNode, Node coordIndexNode) { + return String.valueOf(getCoordinate(color, patchIndexNode, coordIndexNode).getColor()); + } + + private Coordinate getCoordinate(String color, Node patchIndexNode, Node coordIndexNode) { PatchColor patchColor = PatchColor.valueOf(PatchColor.class, color); int patchIndex = Integer.parseInt(patchIndexNode.getNodeValue()); int coordIndex = Integer.parseInt(coordIndexNode.getNodeValue()); + int order = paDoc.getOrder(); - Coordinate coord = paDoc.getPatches().get(patchIndex).getPatch(patchColor).getCoordinate(coordIndex % PatchCoords.ORDER, coordIndex / PatchCoords.ORDER); - return String.valueOf(coord.getColor()); + return paDoc.getPatches().get(patchIndex).getPatch(patchColor).getCoordinate(coordIndex % order, coordIndex / order); } Modified: trunk/patchanim/src/com/mebigfatguy/patchanim/io/PatchAnimIO.java =================================================================== --- trunk/patchanim/src/com/mebigfatguy/patchanim/io/PatchAnimIO.java 2008-02-14 05:14:11 UTC (rev 184) +++ trunk/patchanim/src/com/mebigfatguy/patchanim/io/PatchAnimIO.java 2008-02-14 07:18:45 UTC (rev 185) @@ -86,73 +86,18 @@ public static PatchAnimDocument loadFile(File f) throws IOException { InputStream xmlIs = null; - final PatchAnimDocument doc = new PatchAnimDocument(); - doc.getPatches().clear(); try { XMLReader reader = XMLReaderFactory.createXMLReader(); - reader.setContentHandler(new DefaultHandler() { - private static final String SETTINGS = "Settings"; - private static final String WIDTH = "width"; - private static final String HEIGHT = "height"; - private static final String ANIMATIONTYPE = "animationType"; - private static final String OUTOFBOUNDSCOLOR = "outOfBoundsColor"; - private static final String TWEENCOUNT = "tweenCount"; - private static final String COMBINEDPATCH = "CombinedPatch"; - private static final String PATCH = "Patch"; - private static final String COLOR = "color"; - private static final String COORDINATE = "Coordinate"; - private static final String X = "x"; - private static final String Y = "y"; - - private CombinedPatch cPatch = null; - private PatchCoords patchCoords = null; - private PatchColor patchColor = null; - private int coordIndex = 0; - - @Override - public void startElement(String uri, String localName, String qName, Attributes atts) { - if (SETTINGS.equals(localName)) { - doc.setWidth(Integer.parseInt(atts.getValue(WIDTH))); - doc.setHeight(Integer.parseInt(atts.getValue(HEIGHT))); - doc.setAnimationType(AnimationType.valueOf(AnimationType.class, atts.getValue(ANIMATIONTYPE))); - doc.setOutOfBoundsColor(OutOfBoundsColor.valueOf(OutOfBoundsColor.class, atts.getValue(OUTOFBOUNDSCOLOR))); - doc.setTweenCount(Integer.parseInt(atts.getValue(TWEENCOUNT))); - } else if (COMBINEDPATCH.equals(localName)) { - cPatch = new CombinedPatch(false); - } else if (PATCH.equals(localName)) { - patchCoords = new PatchCoords(); - patchColor = PatchColor.valueOf(PatchColor.class, atts.getValue(COLOR)); - } else if (COORDINATE.equals(localName)) { - Coordinate c = new Coordinate(Double.parseDouble(atts.getValue(X)), - Double.parseDouble(atts.getValue(Y)), - Double.parseDouble(atts.getValue(COLOR))); - patchCoords.setCoordinate(coordIndex / PatchCoords.ORDER, coordIndex % PatchCoords.ORDER, c); - coordIndex++; - c = null; - } - } - - @Override - public void endElement(String uri, String localName, String qName) { - if (COMBINEDPATCH.equals(localName)) { - doc.getPatches().add(cPatch); - cPatch = null; - } else if (PATCH.equals(localName)) { - cPatch.setPatch(patchColor, patchCoords); - patchColor = null; - patchCoords = null; - coordIndex = 0; - } - } - }); + PatchAnimDocContentHandler handler = new PatchAnimDocContentHandler(); + reader.setContentHandler(handler); xmlIs = new BufferedInputStream(new FileInputStream(f)); reader.setFeature("http://apache.org/xml/features/validation/schema", true); reader.setFeature("http://xml.org/sax/features/validation", true); reader.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", PatchAnimDocument.class.getResource(PATCHANIMDOC_SCHEMA).toString()); reader.parse(new InputSource(xmlIs)); - return doc; + return handler.getDocument(); } catch (IOException ioe) { throw ioe; } catch (Exception e) { @@ -163,4 +108,71 @@ Closer.close(xmlIs); } } + + static class PatchAnimDocContentHandler extends DefaultHandler { + private static final String SETTINGS = "Settings"; + private static final String ORDER = "order"; + private static final String WIDTH = "width"; + private static final String HEIGHT = "height"; + private static final String ANIMATIONTYPE = "animationType"; + private static final String OUTOFBOUNDSCOLOR = "outOfBoundsColor"; + private static final String TWEENCOUNT = "tweenCount"; + private static final String COMBINEDPATCH = "CombinedPatch"; + private static final String PATCH = "Patch"; + private static final String COLOR = "color"; + private static final String COORDINATE = "Coordinate"; + private static final String X = "x"; + private static final String Y = "y"; + + PatchAnimDocument doc = null; + + private CombinedPatch cPatch = null; + private PatchCoords patchCoords = null; + private PatchColor patchColor = null; + private int coordIndex = 0; + + @Override + public void startElement(String uri, String localName, String qName, Attributes atts) { + if (SETTINGS.equals(localName)) { + int order = Integer.parseInt(atts.getValue(ORDER)); + doc = new PatchAnimDocument(order); + doc.getPatches().clear(); + doc.setWidth(Integer.parseInt(atts.getValue(WIDTH))); + doc.setHeight(Integer.parseInt(atts.getValue(HEIGHT))); + doc.setAnimationType(AnimationType.valueOf(AnimationType.class, atts.getValue(ANIMATIONTYPE))); + doc.setOutOfBoundsColor(OutOfBoundsColor.valueOf(OutOfBoundsColor.class, atts.getValue(OUTOFBOUNDSCOLOR))); + doc.setTweenCount(Integer.parseInt(atts.getValue(TWEENCOUNT))); + } else if (COMBINEDPATCH.equals(localName)) { + cPatch = new CombinedPatch(doc.getOrder(), false); + } else if (PATCH.equals(localName)) { + patchCoords = new PatchCoords(doc.getOrder()); + patchColor = PatchColor.valueOf(PatchColor.class, atts.getValue(COLOR)); + } else if (COORDINATE.equals(localName)) { + Coordinate c = new Coordinate(Double.parseDouble(atts.getValue(X)), + Double.parseDouble(atts.getValue(Y)), + Double.parseDouble(atts.getValue(COLOR))); + int order = doc.getOrder(); + patchCoords.setCoordinate(coordIndex % order, coordIndex / order, c); + coordIndex++; + c = null; + } + } + + @Override + public void endElement(String uri, String localName, String qName) { + if (COMBINEDPATCH.equals(localName)) { + doc.getPatches().add(cPatch); + cPatch = null; + } else if (PATCH.equals(localName)) { + cPatch.setPatch(patchColor, patchCoords); + patchColor = null; + patchCoords = null; + coordIndex = 0; + } + } + + public PatchAnimDocument getDocument() { + return doc; + } + } } Modified: trunk/patchanim/src/com/mebigfatguy/patchanim/main/PatchAnimBundle.java =================================================================== --- trunk/patchanim/src/com/mebigfatguy/patchanim/main/PatchAnimBundle.java 2008-02-14 05:14:11 UTC (rev 184) +++ trunk/patchanim/src/com/mebigfatguy/patchanim/main/PatchAnimBundle.java 2008-02-14 07:18:45 UTC (rev 185) @@ -45,6 +45,7 @@ public static final String EXPORTINGFILE = "patchanim.exportfile"; public static final String QUIT = "patchanim.quit"; public static final String CONTROLS = "patchanim.control"; + public static final String SETORDER = "patchanim.setorder"; public static final String PATCHES = "patchanim.patches"; public static final String WIDTH = "patchanim.width"; public static final String WIDTH_TT = "patchanim.tooltip.width"; Modified: trunk/patchanim/src/com/mebigfatguy/patchanim/resources.properties =================================================================== --- trunk/patchanim/src/com/mebigfatguy/patchanim/resources.properties 2008-02-14 05:14:11 UTC (rev 184) +++ trunk/patchanim/src/com/mebigfatguy/patchanim/resources.properties 2008-02-14 07:18:45 UTC (rev 185) @@ -38,6 +38,7 @@ patchanim.exportfile = Exporting Animation patchanim.quit = Quit patchanim.control = Controls +patchanim.setorder = Set the order of the patches to patchanim.patches = Patches patchanim.width = Width patchanim.tooltip.width = The width of the exported animation Modified: trunk/patchanim/src/com/mebigfatguy/patchanim/surface/CombinedPatch.java =================================================================== --- trunk/patchanim/src/com/mebigfatguy/patchanim/surface/CombinedPatch.java 2008-02-14 05:14:11 UTC (rev 184) +++ trunk/patchanim/src/com/mebigfatguy/patchanim/surface/CombinedPatch.java 2008-02-14 07:18:45 UTC (rev 185) @@ -32,15 +32,15 @@ private EnumMap<PatchColor, PatchCoords> patches = new EnumMap<PatchColor, PatchCoords>(PatchColor.class); private String name; - public CombinedPatch(boolean init) { + public CombinedPatch(int order, boolean init) { if (init) { - patches.put(PatchColor.Red, PatchCoords.buildRandomPatch()); - patches.put(PatchColor.Green, PatchCoords.buildRandomPatch()); - patches.put(PatchColor.Blue, PatchCoords.buildRandomPatch()); + patches.put(PatchColor.Red, PatchCoords.buildRandomPatch(order)); + patches.put(PatchColor.Green, PatchCoords.buildRandomPatch(order)); + patches.put(PatchColor.Blue, PatchCoords.buildRandomPatch(order)); } else { - patches.put(PatchColor.Red, new PatchCoords()); - patches.put(PatchColor.Green, new PatchCoords()); - patches.put(PatchColor.Blue, new PatchCoords()); + patches.put(PatchColor.Red, new PatchCoords(order)); + patches.put(PatchColor.Green, new PatchCoords(order)); + patches.put(PatchColor.Blue, new PatchCoords(order)); } ResourceBundle rb = PatchAnimBundle.getBundle(); name = rb.getString(PatchAnimBundle.DEFAULTPATCHNAME); @@ -62,11 +62,11 @@ } return clonedPatch; } catch (CloneNotSupportedException cnse) { - return new CombinedPatch(true); + return new CombinedPatch(getPatch(PatchColor.Red).getOrder(), true); } } public static CombinedPatch tween(CombinedPatch startPatch, CombinedPatch endPatch, double frac) { - CombinedPatch tweenPatch = new CombinedPatch(false); + CombinedPatch tweenPatch = new CombinedPatch(startPatch.getPatch(PatchColor.Red).getOrder(), false); { PatchCoords sRedCoords = startPatch.getPatch(PatchColor.Red); PatchCoords eRedCoords = endPatch.getPatch(PatchColor.Red); Modified: trunk/patchanim/src/com/mebigfatguy/patchanim/surface/PatchCoords.java =================================================================== --- trunk/patchanim/src/com/mebigfatguy/patchanim/surface/PatchCoords.java 2008-02-14 05:14:11 UTC (rev 184) +++ trunk/patchanim/src/com/mebigfatguy/patchanim/surface/PatchCoords.java 2008-02-14 07:18:45 UTC (rev 185) @@ -25,26 +25,27 @@ public class PatchCoords implements Serializable, Cloneable { private static final long serialVersionUID = -4052789167154764908L; - public static final int ORDER = 4; - + private int order; private Coordinate[][] coords; - public static PatchCoords buildRandomPatch() { - Coordinate[][] coords = new Coordinate[PatchCoords.ORDER][PatchCoords.ORDER]; + public static PatchCoords buildRandomPatch(int patchOrder) { + Coordinate[][] coords = new Coordinate[patchOrder][patchOrder]; Random r = new Random(); - for (int u = 0; u < PatchCoords.ORDER; u++) { - for (int v = 0; v < PatchCoords.ORDER; v++) { - coords[u][v] = new Coordinate((u * 100.0) / (PatchCoords.ORDER - 1), (v * 100.0) / (PatchCoords.ORDER - 1), r.nextInt(400) - 50); + for (int u = 0; u < patchOrder; u++) { + for (int v = 0; v < patchOrder; v++) { + coords[u][v] = new Coordinate((u * 100.0) / (patchOrder - 1), (v * 100.0) / (patchOrder - 1), r.nextInt(400) - 50); } } - return new PatchCoords(coords); + return new PatchCoords(patchOrder, coords); } - public PatchCoords() { - coords = new Coordinate[ORDER][ORDER]; + public PatchCoords(int patchOrder) { + order = patchOrder; + coords = new Coordinate[patchOrder][patchOrder]; } - public PatchCoords(Coordinate[][] coordinates) { + public PatchCoords(int patchOrder, Coordinate[][] coordinates) { + order = patchOrder; coords = coordinates; } @@ -52,22 +53,27 @@ public Object clone() { try { PatchCoords clonedCoords = (PatchCoords)super.clone(); - clonedCoords.coords = new Coordinate[ORDER][ORDER]; - for (int u = 0; u < PatchCoords.ORDER; u++) { - for (int v = 0; v < PatchCoords.ORDER; v++) { + clonedCoords.coords = new Coordinate[order][order]; + for (int u = 0; u < order; u++) { + for (int v = 0; v < order; v++) { clonedCoords.coords[u][v] = (Coordinate)coords[u][v].clone(); } } return clonedCoords; } catch (CloneNotSupportedException cnse) { - return buildRandomPatch(); + return buildRandomPatch(order); } } + + public int getOrder() { + return order; + } + public static PatchCoords tween(PatchCoords startCoords, PatchCoords endCoords, double frac) { - PatchCoords tweenCoords = new PatchCoords(); - for (int x = 0; x < ORDER; x++) { - for (int y = 0; y < ORDER; y++) { + PatchCoords tweenCoords = new PatchCoords(startCoords.getOrder()); + for (int x = 0; x < tweenCoords.order; x++) { + for (int y = 0; y < tweenCoords.order; y++) { Coordinate startC = startCoords.getCoordinate(x,y); Coordinate endC = endCoords.getCoordinate(x,y); int tweenColor = (int)(startC.getColor() + (endC.getColor() - startC.getColor()) * frac); Modified: trunk/patchanim/src/com/mebigfatguy/patchanim/surface/PatchGenerator.java =================================================================== --- trunk/patchanim/src/com/mebigfatguy/patchanim/surface/PatchGenerator.java 2008-02-14 05:14:11 UTC (rev 184) +++ trunk/patchanim/src/com/mebigfatguy/patchanim/surface/PatchGenerator.java 2008-02-14 07:18:45 UTC (rev 185) @@ -46,11 +46,12 @@ coords[0] = patch.getPatch(PatchColor.Red); coords[1] = patch.getPatch(PatchColor.Green); coords[2] = patch.getPatch(PatchColor.Blue); + int order = coords[0].getOrder(); double u; double v; - double[] uCoeffs = new double[PatchCoords.ORDER]; - double[] vCoeffs = new double[PatchCoords.ORDER]; + double[] uCoeffs = new double[order]; + double[] vCoeffs = new double[order]; double[] value = new double[3]; int[] iValue = new int[3]; @@ -67,8 +68,8 @@ value[0] = value[1] = value[2] = 0.0; - for (int i = 0; i < PatchCoords.ORDER; i++) { - for (int j = 0; j < PatchCoords.ORDER; j++) { + for (int j = 0; j < order; j++) { + for (int i = 0; i < order; i++) { double coeff = uCoeffs[i] * vCoeffs[j]; for (int k = 0; k < 3; k++) { value[k] += coords[k].getCoordinate(i, j).getColor() * coeff; @@ -108,11 +109,12 @@ int pixel = 0; PatchCoords coords = patch.getPatch(color); + int order = coords.getOrder(); double u; double v; - double[] uCoeffs = new double[PatchCoords.ORDER]; - double[] vCoeffs = new double[PatchCoords.ORDER]; + double[] uCoeffs = new double[order]; + double[] vCoeffs = new double[order]; int sampleSizeX = image.getWidth(); int sampleSizeY = image.getHeight(); @@ -126,8 +128,8 @@ buildCoefficients(u, uCoeffs); double value = 0.0; - for (int i = 0; i < PatchCoords.ORDER; i++) { - for (int j = 0; j < PatchCoords.ORDER; j++) { + for (int j = 0; j < order; j++) { + for (int i = 0; i < order; i++) { value += coords.getCoordinate(i, j).getColor() * uCoeffs[i] * vCoeffs[j]; } } @@ -178,16 +180,29 @@ } private static void buildCoefficients(double t, double[] coeffs) { - double t2 = t * t; - double t3 = t2 * t; + double tt = 1.0; + for (int i = 0; i < coeffs.length; i++) { + coeffs[i] = tt; + tt = tt * t; + } + double oneMinusT = 1.0 - t; - double oneMinusT2 = oneMinusT * oneMinusT; - double oneMinusT3 = oneMinusT2 * oneMinusT; + double oneMinusTT = 1.0; + for (int i = coeffs.length - 1; i >= 0; i--) { + coeffs[i] *= oneMinusTT; + oneMinusTT = oneMinusTT * oneMinusT; + } - coeffs[0] = oneMinusT3; - coeffs[1] = 3.0 * t * oneMinusT2; - coeffs[2] = 3.0 * t2 * oneMinusT; - coeffs[3] = t3; + for (int i = 0; i < coeffs.length; i++) { + coeffs[i] *= NChooseI.nChooseI(coeffs.length-1, i); + } } + static class NChooseI { + private static double[] factorial = { 1.0, 1.0, 2.0, 6.0, 24.0, 120.0, 720.0, 5040.0, 40320.0, 362880.0 }; + + public static double nChooseI(int n, int i) { + return factorial[n]/(factorial[i] * factorial[n-i]); + } + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |