├── .gitignore ├── README.md ├── demo ├── ControlPathPanel.java ├── CurveDemo.java ├── CurveListCellRenderer.java ├── EditCurvePanel.java ├── PointFactory.java └── UnsortedValueVectorPanel.java ├── license.txt ├── pom.xml ├── readme.txt ├── release-notes.txt ├── scshot.png └── src ├── main └── java │ └── com │ └── graphbuilder │ ├── curve │ ├── BSpline.java │ ├── BezierCurve.java │ ├── BinaryCurveApproximationAlgorithm.java │ ├── CardinalSpline.java │ ├── CatmullRomSpline.java │ ├── ControlPath.java │ ├── ControlStringParseException.java │ ├── CubicBSpline.java │ ├── Curve.java │ ├── GroupIterator.java │ ├── LagrangeCurve.java │ ├── MultiPath.java │ ├── NURBSpline.java │ ├── NaturalCubicSpline.java │ ├── ParametricCurve.java │ ├── Point.java │ ├── Polyline.java │ ├── ShapeMultiPath.java │ ├── ShapeMultiPathIterator.java │ ├── ValueVector.java │ ├── ValueVectorListener.java │ └── doc-files │ │ ├── basic_idea.gif │ │ ├── bezier1.gif │ │ ├── bspline1.gif │ │ ├── bspline2.gif │ │ ├── cardinal1.gif │ │ ├── cubicb1.gif │ │ ├── cubicb2.gif │ │ ├── cubicb3.gif │ │ ├── lagrange1.gif │ │ ├── lagrange2.gif │ │ ├── lagrange3.gif │ │ ├── lagrange4.gif │ │ ├── natcubic1.gif │ │ ├── natcubic2.gif │ │ ├── nurbs1.gif │ │ ├── nurbs2.gif │ │ └── problem_case.gif │ ├── geom │ ├── Geom.java │ ├── Point2d.java │ ├── Point3d.java │ └── PointFactory.java │ ├── math │ ├── AddNode.java │ ├── DivNode.java │ ├── Expression.java │ ├── ExpressionParseException.java │ ├── ExpressionTree.java │ ├── FuncMap.java │ ├── FuncNode.java │ ├── MultNode.java │ ├── OpNode.java │ ├── PascalsTriangle.java │ ├── PowNode.java │ ├── SubNode.java │ ├── TermNode.java │ ├── ValNode.java │ ├── VarMap.java │ ├── VarNode.java │ └── func │ │ ├── AbsFunction.java │ │ ├── AcosFunction.java │ │ ├── AcoshFunction.java │ │ ├── AsinFunction.java │ │ ├── AsinhFunction.java │ │ ├── AtanFunction.java │ │ ├── AtanhFunction.java │ │ ├── AvgFunction.java │ │ ├── CeilFunction.java │ │ ├── CombinFunction.java │ │ ├── CosFunction.java │ │ ├── CoshFunction.java │ │ ├── EFunction.java │ │ ├── ExpFunction.java │ │ ├── FactFunction.java │ │ ├── FloorFunction.java │ │ ├── Function.java │ │ ├── LgFunction.java │ │ ├── LnFunction.java │ │ ├── LogFunction.java │ │ ├── MaxFunction.java │ │ ├── MinFunction.java │ │ ├── ModFunction.java │ │ ├── PiFunction.java │ │ ├── PowFunction.java │ │ ├── RandFunction.java │ │ ├── RoundFunction.java │ │ ├── SignFunction.java │ │ ├── SinFunction.java │ │ ├── SinhFunction.java │ │ ├── SqrtFunction.java │ │ ├── SumFunction.java │ │ ├── TanFunction.java │ │ └── TanhFunction.java │ ├── org │ └── apache │ │ └── harmony │ │ └── awt │ │ └── gl │ │ └── Crossing.java │ └── struc │ ├── Bag.java │ ├── LinkedList.java │ └── Stack.java └── test └── java └── com └── graphbuilder └── curve └── TestShapeMultiPath.java /.gitignore: -------------------------------------------------------------------------------- 1 | .settings 2 | .classpath 3 | .project 4 | target -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | curvesapi 2 | ========= 3 | 4 | Implementation of various mathematical curves that define themselves over 5 | a set of control points. The API is written in Java. The curves supported 6 | are: Bezier, B-Spline, Cardinal Spline, Catmull-Rom Spline, Lagrange, 7 | Natural Cubic Spline, and NURBS. 8 | 9 | About this project 10 | ------------------ 11 | 12 | This is a mavenized version of http://sourceforge.net/projects/curves . This 13 | is not quite a fork -- as I do not intend to change/improve the project. 14 | However, bugfixes and reasonably scoped improvements will be accepted. 15 | 16 | 17 | List of changes 18 | --------------- 19 | 20 | * Version has been bumped to 1.08 21 | * The javadocs have been removed from this distribution 22 | * The build scripts have been removed and pom.xml has been added to support 23 | usage with maven 24 | * No longer using JDK internals to compute path intersection, copied 25 | Crossing.java from Apache Harmony instead 26 | * The 'appendTo' method now raises IllegalArgumentException with useful error 27 | messages on invalid arguments instead of swallowing the error 28 | * A PointFactory has been added to aid in creating points 29 | * Point2d now extends Point 30 | * Uses standard maven source layout 31 | * Added simple test for ShapeMultiPath 32 | 33 | Licenses 34 | -------- 35 | 36 | The original project used a BSD license, and remains so. 37 | 38 | com.graphbuilder.org.apache.harmony.awt.gl.Crossing is from the Apache 39 | Harmony project and is released under the Apache 2.0 license. 40 | -------------------------------------------------------------------------------- /demo/CurveDemo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | import javax.swing.*; 33 | import java.awt.*; 34 | import java.awt.event.*; 35 | 36 | public class CurveDemo implements ActionListener { 37 | 38 | private EditCurvePanel editCurvePanel = new EditCurvePanel(); 39 | private ControlPathPanel controlPathPanel = new ControlPathPanel(); 40 | private JFrame frame = new JFrame("Curve Demo"); 41 | private JDialog editDialog = new JDialog(frame, "Curve Options"); 42 | 43 | private JMenuItem editCurveMenuItem = new JMenuItem("Edit Curves"); 44 | private JMenuItem setFlatnessMenuItem = new JMenuItem("Set Flatness"); 45 | private JCheckBoxMenuItem showControlPointsCheckBox = new JCheckBoxMenuItem("Show Control Points", controlPathPanel.getShowControlPoints()); 46 | private JCheckBoxMenuItem showControlLineCheckBox = new JCheckBoxMenuItem("Show Control Line", controlPathPanel.getShowControlLine()); 47 | private JCheckBoxMenuItem showPointNumbersCheckBox = new JCheckBoxMenuItem("Show Point Numbers", controlPathPanel.getShowPointNumbers()); 48 | private JCheckBoxMenuItem showPointLocationsCheckBox = new JCheckBoxMenuItem("Show Point Locations", controlPathPanel.getShowPointLocations()); 49 | private JCheckBoxMenuItem antialiasCurvesCheckBox = new JCheckBoxMenuItem("Anti-alias Curves", controlPathPanel.getAntialiasCurves()); 50 | private JCheckBoxMenuItem showCurvesCheckBox = new JCheckBoxMenuItem("Show Curves", controlPathPanel.getShowCurves()); 51 | 52 | public CurveDemo() { 53 | Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); 54 | frame.setSize(500, 500); 55 | 56 | frame.addWindowListener(new WindowAdapter() { 57 | public void windowClosing(WindowEvent evt) { 58 | System.exit(0); 59 | } 60 | }); 61 | 62 | frame.setContentPane(controlPathPanel); 63 | 64 | JMenuBar menuBar = new JMenuBar(); 65 | frame.setJMenuBar(menuBar); 66 | 67 | JMenu controlPathMenu = new JMenu("Control-Path"); 68 | 69 | controlPathMenu.add(showControlPointsCheckBox); 70 | controlPathMenu.add(showControlLineCheckBox); 71 | controlPathMenu.add(showPointNumbersCheckBox); 72 | controlPathMenu.add(showPointLocationsCheckBox); 73 | controlPathMenu.add(antialiasCurvesCheckBox); 74 | controlPathMenu.add(showCurvesCheckBox); 75 | controlPathMenu.add(setFlatnessMenuItem); 76 | controlPathMenu.add(editCurveMenuItem); 77 | 78 | menuBar.add(controlPathMenu); 79 | 80 | editDialog.setContentPane(editCurvePanel); 81 | editDialog.setSize(500, 300); 82 | editDialog.setLocation((d.width - editDialog.getWidth()) / 2, (d.height - editDialog.getHeight()) / 2); 83 | 84 | editCurveMenuItem.addActionListener(this); 85 | setFlatnessMenuItem.addActionListener(this); 86 | showControlPointsCheckBox.addActionListener(this); 87 | showControlLineCheckBox.addActionListener(this); 88 | showPointNumbersCheckBox.addActionListener(this); 89 | showPointLocationsCheckBox.addActionListener(this); 90 | antialiasCurvesCheckBox.addActionListener(this); 91 | showCurvesCheckBox.addActionListener(this); 92 | 93 | frame.show(); 94 | } 95 | 96 | public void actionPerformed(ActionEvent evt) { 97 | Object src = evt.getSource(); 98 | 99 | if (src == editCurveMenuItem) { 100 | editCurvePanel.setControlPath(controlPathPanel.getControlPath()); 101 | editCurvePanel.setRepaintPanel(controlPathPanel); 102 | editDialog.show(); 103 | } 104 | else if (src == showControlPointsCheckBox) { 105 | controlPathPanel.setShowControlPoints(showControlPointsCheckBox.isSelected()); 106 | } 107 | else if (src == showControlLineCheckBox) { 108 | controlPathPanel.setShowControlLine(showControlLineCheckBox.isSelected()); 109 | } 110 | else if (src == showPointNumbersCheckBox) { 111 | controlPathPanel.setShowPointNumbers(showPointNumbersCheckBox.isSelected()); 112 | } 113 | else if (src == showPointLocationsCheckBox) { 114 | controlPathPanel.setShowPointLocations(showPointLocationsCheckBox.isSelected()); 115 | } 116 | else if (src == antialiasCurvesCheckBox) { 117 | controlPathPanel.setAntialiasCurves(antialiasCurvesCheckBox.isSelected()); 118 | } 119 | else if (src == showCurvesCheckBox) { 120 | controlPathPanel.setShowCurves(showCurvesCheckBox.isSelected()); 121 | } 122 | else if (src == setFlatnessMenuItem) { 123 | double f = controlPathPanel.getFlatness(); 124 | 125 | Object o = JOptionPane.showInputDialog(frame, "Enter flatness value:", "Flatness", JOptionPane.QUESTION_MESSAGE, null, null, String.valueOf(f)); 126 | 127 | if (o != null) { 128 | try { 129 | f = Double.parseDouble(o.toString()); 130 | controlPathPanel.setFlatness(f); 131 | } catch (Throwable err) { 132 | JOptionPane.showMessageDialog(frame, err, "ERROR", JOptionPane.ERROR_MESSAGE); 133 | } 134 | } 135 | } 136 | 137 | controlPathPanel.repaint(); 138 | } 139 | 140 | public static void main(String[] args) { 141 | new CurveDemo(); 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /demo/CurveListCellRenderer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | import javax.swing.*; 33 | import java.awt.*; 34 | import com.graphbuilder.curve.*; 35 | 36 | public class CurveListCellRenderer extends DefaultListCellRenderer { 37 | 38 | public CurveListCellRenderer() { 39 | super(); 40 | } 41 | 42 | public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 43 | String name = ""; 44 | 45 | if (value instanceof BezierCurve) 46 | name = "Bezier Curve"; 47 | else if (value instanceof NURBSpline) // must come before BSpline 48 | name = "NURB-Spline"; 49 | else if (value instanceof BSpline) 50 | name = "B-Spline"; 51 | else if (value instanceof CardinalSpline) 52 | name = "Cardinal Spline"; 53 | else if (value instanceof CatmullRomSpline) 54 | name = "Catmull-Rom Spline"; 55 | else if (value instanceof CubicBSpline) 56 | name = "Cubic B-Spline"; 57 | else if (value instanceof LagrangeCurve) 58 | name = "Lagrange Curve"; 59 | else if (value instanceof NaturalCubicSpline) 60 | name = "Natural Cubic Spline"; 61 | else if (value instanceof Polyline) 62 | name = "Polyline"; 63 | 64 | return super.getListCellRendererComponent(list, name, index, isSelected, cellHasFocus); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /demo/PointFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | import com.graphbuilder.curve.*; 33 | 34 | public class PointFactory { 35 | 36 | public Point createPoint(double x, double y) { 37 | final double[] arr = new double[] {x, y}; 38 | 39 | return new Point() { 40 | public double[] getLocation() { 41 | return arr; 42 | } 43 | 44 | public void setLocation(double[] loc) { 45 | arr[0] = loc[0]; 46 | arr[1] = loc[1]; 47 | } 48 | }; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2005, Graph Builder 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions 6 | are met: 7 | 8 | -Redistributions of source code must retain the above copyright notice, 9 | this list of conditions and the following disclaimer. 10 | 11 | -Redistributions in binary form must reproduce the above copyright notice, 12 | this list of conditions and the following disclaimer in the documentation 13 | and/or other materials provided with the distribution. 14 | 15 | -Neither the name of Graph Builder nor the names of its contributors may be 16 | used to endorse or promote products derived from this software without 17 | specific prior written permission. 18 | 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 22 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 23 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 26 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 27 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | com.github.virtuald 4 | curvesapi 5 | 1.08 6 | curvesapi 7 | Implementation of various mathematical curves that define themselves over a set of control points. The API is written in Java. The curves supported are: Bezier, B-Spline, Cardinal Spline, Catmull-Rom Spline, Lagrange, Natural Cubic Spline, and NURBS. 8 | https://github.com/virtuald/curvesapi 9 | 10 | 11 | 12 | 13 | org.apache.maven.plugins 14 | maven-compiler-plugin 15 | 16 | 1.7 17 | 1.7 18 | 19 | 20 | 21 | org.apache.maven.plugins 22 | maven-jar-plugin 23 | 24 | 25 | 26 | com.github.virtuald.curvesapi 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | release 37 | 38 | 39 | 40 | org.sonatype.plugins 41 | nexus-staging-maven-plugin 42 | 1.6.13 43 | true 44 | 45 | ossrh 46 | https://oss.sonatype.org/ 47 | true 48 | 49 | 50 | 51 | org.apache.maven.plugins 52 | maven-source-plugin 53 | 2.4 54 | 55 | 56 | attach-sources 57 | 58 | jar-no-fork 59 | 60 | 61 | 62 | 63 | 64 | org.apache.maven.plugins 65 | maven-javadoc-plugin 66 | 2.10.3 67 | 68 | 69 | attach-javadocs 70 | 71 | jar 72 | 73 | 74 | -Xdoclint:none 75 | 76 | 77 | 78 | 79 | 80 | org.apache.maven.plugins 81 | maven-gpg-plugin 82 | 1.6 83 | 84 | 85 | sign-artifacts 86 | verify 87 | 88 | sign 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | BSD License 101 | http://opensource.org/licenses/BSD-3-Clause 102 | repo 103 | 104 | 105 | 106 | 107 | https://github.com/virtuald/curvesapi 108 | scm:git:git://github.com/virtuald/curvesapi.git 109 | scm:git:git@github.com/virtuald/curvesapi.git 110 | 111 | 112 | 113 | 114 | stormdollar 115 | http://sourceforge.net/u/stormdollar/profile/ 116 | stormdollar 117 | 118 | 119 | Dustin Spicuzza 120 | https://github.com/virtuald 121 | virtuald 122 | 123 | 124 | 125 | 126 | 127 | junit 128 | junit 129 | 4.13.2 130 | test 131 | 132 | 133 | 134 | 135 | -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- 1 | Useful Information 2 | ------------------ 3 | 4 | The copyfiles.bat file is not required. It is used to copy only 5 | the necessary files from the com.graphbuilder package hierarchy 6 | that are required for the Curve API. 7 | 8 | In the src directory, there is a makejar.bat file. This file will 9 | compile the nested Java files and create a Jar file called Capi.jar. 10 | Compilation requires Java 1.2 or higher. After the Jar file is 11 | created, add it to the classpath. Note: CAPI comes with MESP (Math 12 | Expression String Parser). 13 | 14 | The following is a sample program: 15 | 16 | import com.graphbuilder.curve.*; 17 | 18 | public class CurveTest { 19 | 20 | public static void main(String[] args) { 21 | ControlPath cp = new ControlPath(); 22 | cp.addPoint(createPoint(0,0)); 23 | cp.addPoint(createPoint(10,10)); 24 | cp.addPoint(createPoint(20, 10)); 25 | cp.addPoint(createPoint(30,0)); 26 | 27 | cp.addCurve(new BezierCurve(cp, new GroupIterator("0:n-1", cp.numPoints()))); 28 | 29 | int dimension = 2; 30 | MultiPath mp = new MultiPath(dimension); 31 | mp.setFlatness(1.0); // default flatness is 1.0 32 | 33 | for (int i = 0; i < cp.numCurves(); i++) 34 | cp.getCurve(i).appendTo(mp); 35 | 36 | for (int i = 0; i < mp.getNumPoints(); i++) { 37 | double[] p = mp.get(i); 38 | System.out.println(p[0] + ", " + p[1]); 39 | } 40 | } 41 | 42 | private static Point createPoint(double x, double y) { 43 | final double[] arr = new double[] { x, y }; 44 | return new Point() { 45 | public double[] getLocation() { 46 | return arr; 47 | } 48 | 49 | public void setLocation(double[] loc) { 50 | arr[0] = loc[0]; 51 | arr[1] = loc[1]; 52 | } 53 | }; 54 | } 55 | } 56 | 57 | The demo directory contains a GUI to the API. To run it first compile the 58 | files and then run java CurveDemo. The demo also requires Java 1.2 or higher. -------------------------------------------------------------------------------- /release-notes.txt: -------------------------------------------------------------------------------- 1 | Curve API (CAPI) 2 | 3 | Version: 1.01 4 | Date: August 10, 2005 5 | -CubicBSpline now interpolates endpoints when there are 4, 5, or 6 points. Before, 6 | only >= 7 points were supported for interpolating the endpoints. 7 | -Updated CubicBSpline API class documentation. 8 | -Updated CubicBspline API appendTo method documentation. 9 | -Added ability to set the UnsortedValueVectorPanel to disabled input mode for demo app. 10 | -Updated Point API getLocation method documentation. 11 | -Upgraded development status from alpha to beta. 12 | 13 | Version: 1.0 14 | Date: April 1, 2005 15 | -Initial release (under the BSD License). 16 | -------------------------------------------------------------------------------- /scshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/virtuald/curvesapi/a39c6d234bd1b70a324a8cf4f5a2b0e1f2780c7f/scshot.png -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/curve/CardinalSpline.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.curve; 33 | 34 | 35 | /** 36 |

The Cardinal-spline passes through the points in the control-path specified by the group-iterator. 37 | However, the curve does not pass through the first or the last control-point, it begins at the 38 | second control-point and ends at the second last control-point. 39 | 40 |

There is a single parameter, alpha, that gives some control over the shape of the curve. When 41 | the value of alpha is 0.5 the curve becomes the CatmullRomSpline. Figure 1 shows an example of a 42 | CardinalSpline. 43 | 44 |

45 | 46 | @see com.graphbuilder.curve.CatmullRomSpline 47 | */ 48 | public class CardinalSpline extends ParametricCurve { 49 | 50 | private static final ThreadLocal SHARED_DATA = new ThreadLocal(){ 51 | protected SharedData initialValue() { 52 | return new SharedData(); 53 | } 54 | }; 55 | private final SharedData sharedData = SHARED_DATA.get(); 56 | 57 | private static class SharedData { 58 | private double[][] pt = new double[4][]; 59 | } 60 | 61 | private double alpha = 0.5; 62 | 63 | public CardinalSpline(ControlPath cp, GroupIterator gi) { 64 | super(cp, gi); 65 | } 66 | 67 | protected void eval(double[] p) { 68 | double t = p[p.length - 1]; 69 | double t2 = t * t; 70 | double t3 = t2 * t; 71 | 72 | double a = 2 * t3 - 3 * t2 + 1; 73 | double b = -2 * t3 + 3 * t2; 74 | double c = alpha * (t3 - 2 * t2 + t); 75 | double d = alpha * (t3 - t2); 76 | 77 | for (int i = 0; i < p.length - 1; i++) 78 | p[i] = a * sharedData.pt[1][i] + b * sharedData.pt[2][i] + c * (sharedData.pt[2][i] - sharedData.pt[0][i]) + d * (sharedData.pt[3][i] - sharedData.pt[1][i]); 79 | } 80 | 81 | /** 82 | Returns the value of alpha. The default value is 0.5. 83 | 84 | @see #setAlpha(double) 85 | */ 86 | public double getAlpha() { 87 | return alpha; 88 | } 89 | 90 | /** 91 | Sets the value of alpha. 92 | 93 | @see #getAlpha() 94 | */ 95 | public void setAlpha(double a) { 96 | alpha = a; 97 | } 98 | 99 | /** 100 | Returns a value of 1. 101 | */ 102 | public int getSampleLimit() { 103 | return 1; 104 | } 105 | 106 | /** 107 | The requirements for this curve are the group-iterator must be in-range and have a group size of at least 4. 108 | If these requirements are not met then this method raises IllegalArgumentException 109 | */ 110 | public void appendTo(MultiPath mp) { 111 | if (!gi.isInRange(0, cp.numPoints())) 112 | throw new IllegalArgumentException("group iterator not in range"); 113 | if (gi.getGroupSize() < 4) 114 | throw new IllegalArgumentException("more than 4 groups required"); 115 | 116 | gi.set(0, 0); 117 | 118 | for (int i = 0; i < 4; i++) 119 | sharedData.pt[i] = cp.getPoint(gi.next()).getLocation(); 120 | 121 | double[] d = new double[mp.getDimension() + 1]; 122 | eval(d); 123 | 124 | if (connect) 125 | mp.lineTo(d); 126 | else 127 | mp.moveTo(d); 128 | 129 | gi.set(0, 0); 130 | 131 | while (true) { 132 | int index_i = gi.index_i(); 133 | int count_j = gi.count_j(); 134 | 135 | for (int i = 0; i < 4; i++) { 136 | if (!gi.hasNext()) { 137 | return; 138 | } 139 | sharedData.pt[i] = cp.getPoint(gi.next()).getLocation(); 140 | } 141 | 142 | gi.set(index_i, count_j); 143 | gi.next(); 144 | 145 | BinaryCurveApproximationAlgorithm.genPts(this, 0.0, 1.0, mp); 146 | } 147 | } 148 | } 149 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/curve/CatmullRomSpline.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.curve; 33 | 34 | /** 35 | The CatmullRomSpline is equal to the CardinalSpline with the value of alpha fixed at 0.5. 36 | The implementation for the CatmullRomSpline is about 10% faster then the CardinalSpline 37 | implementation. 38 | 39 | @see com.graphbuilder.curve.CardinalSpline 40 | */ 41 | public class CatmullRomSpline extends ParametricCurve { 42 | 43 | private static final ThreadLocal SHARED_DATA = new ThreadLocal(){ 44 | protected SharedData initialValue() { 45 | return new SharedData(); 46 | } 47 | }; 48 | private final SharedData sharedData = SHARED_DATA.get(); 49 | 50 | private static class SharedData { 51 | private double[][] pt = new double[4][]; 52 | } 53 | 54 | public CatmullRomSpline(ControlPath cp, GroupIterator gi) { 55 | super(cp, gi); 56 | } 57 | 58 | protected void eval(double[] p) { 59 | double t = p[p.length - 1]; 60 | double t2 = t * t; 61 | double t3 = t2 * t; 62 | 63 | // Note: The 0.5 does NOT represent alpha. It is a result of the simplification. 64 | 65 | for (int i = 0; i < p.length - 1; i++) { 66 | p[i] = 0.5 * ((sharedData.pt[3][i] - sharedData.pt[0][i] + 3 * (sharedData.pt[1][i] - sharedData.pt[2][i])) * t3 67 | + (2 * (sharedData.pt[0][i] + 2*sharedData.pt[2][i]) - 5*sharedData.pt[1][i] - sharedData.pt[3][i]) * t2 68 | + (sharedData.pt[2][i] - sharedData.pt[0][i]) * t) + sharedData.pt[1][i]; 69 | } 70 | } 71 | 72 | /** 73 | Returns a value of 1. 74 | */ 75 | public int getSampleLimit() { 76 | return 1; 77 | } 78 | 79 | /** 80 | The requirements for this curve are the group-iterator must be in-range and have a group size of at least 4. 81 | If these requirements are not met then this method returns quietly. 82 | */ 83 | public void appendTo(MultiPath mp) { 84 | if (!gi.isInRange(0, cp.numPoints())) 85 | throw new IllegalArgumentException("Group iterator not in range");; 86 | if (gi.getGroupSize() < 4) 87 | throw new IllegalArgumentException("Group iterator size < 4");; 88 | 89 | gi.set(0, 0); 90 | 91 | for (int i = 0; i < 4; i++) 92 | sharedData.pt[i] = cp.getPoint(gi.next()).getLocation(); 93 | 94 | double[] d = new double[mp.getDimension() + 1]; 95 | eval(d); 96 | 97 | if (connect) 98 | mp.lineTo(d); 99 | else 100 | mp.moveTo(d); 101 | 102 | gi.set(0, 0); 103 | 104 | while (true) { 105 | int index_i = gi.index_i(); 106 | int count_j = gi.count_j(); 107 | 108 | for (int i = 0; i < 4; i++) { 109 | if (!gi.hasNext()) { 110 | return; 111 | } 112 | sharedData.pt[i] = cp.getPoint(gi.next()).getLocation(); 113 | } 114 | 115 | gi.set(index_i, count_j); 116 | gi.next(); 117 | 118 | BinaryCurveApproximationAlgorithm.genPts(this, 0.0, 1.0, mp); 119 | } 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/curve/ControlStringParseException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.curve; 33 | 34 | import com.graphbuilder.math.*; 35 | 36 | /** 37 |

Exception thrown if the parsing of a control-string fails. A ControlStringParseException 38 | contains a description, and may contain any of the following information: 39 | 40 |

    41 |
  • An index or index range of the substring that caused the problem.
  • 42 |
  • An ExpressionParseException that was thrown by the ExpressionTree.parse(String) method.
  • 43 |
44 | 45 |

Otherwise the index values will be -1 if they are unassigned and the ExpressionParseException 46 | will be null. 47 | 48 | @see GroupIterator 49 | */ 50 | public class ControlStringParseException extends RuntimeException { 51 | 52 | private String descrip = null; 53 | private int fromIndex = -1; 54 | private int toIndex = -1; 55 | private ExpressionParseException epe = null; 56 | 57 | /** 58 | Constructor with only a description. 59 | */ 60 | public ControlStringParseException(String descrip) { 61 | this.descrip = descrip; 62 | } 63 | 64 | /** 65 | Constructor with a description and index value. The index value is assigned to both the 66 | fromIndex and the toIndex. 67 | */ 68 | public ControlStringParseException(String descrip, int index) { 69 | this.descrip = descrip; 70 | fromIndex = index; 71 | toIndex = index; 72 | } 73 | 74 | /** 75 | Constructor with a description and index range. 76 | */ 77 | public ControlStringParseException(String descrip, int fromIndex, int toIndex) { 78 | this.descrip = descrip; 79 | this.fromIndex = fromIndex; 80 | this.toIndex = toIndex; 81 | } 82 | 83 | /** 84 | Constructor with a description, index range and ExpressionParseException. 85 | */ 86 | public ControlStringParseException(String descrip, int fromIndex, int toIndex, ExpressionParseException epe) { 87 | this.descrip = descrip; 88 | this.fromIndex = fromIndex; 89 | this.toIndex = toIndex; 90 | this.epe = epe; 91 | } 92 | 93 | /** 94 | Returns the index location in the control-string that marks the start of the problem or -1 if 95 | not available. 96 | */ 97 | public int getFromIndex() { 98 | return fromIndex; 99 | } 100 | 101 | /** 102 | Returns the index location in the control-string that marks the end of the problem or -1 if not 103 | available. 104 | */ 105 | public int getToIndex() { 106 | return toIndex; 107 | } 108 | 109 | /** 110 | Returns the description of the problem. 111 | */ 112 | public String getDescription() { 113 | return descrip; 114 | } 115 | 116 | /** 117 | Returns an ExpressionParseException if an expression in the control-string could not be 118 | parsed correctly or null if this was not the problem. 119 | */ 120 | public ExpressionParseException getExpressionParseException() { 121 | return epe; 122 | } 123 | 124 | /** 125 | Returns a nicely formatted string of this exception. 126 | */ 127 | public String toString() { 128 | String e = ""; 129 | if (epe != null) 130 | e = "\n" + epe.toString(); 131 | 132 | if (fromIndex == -1 && toIndex == -1) 133 | return descrip + e; 134 | 135 | if (fromIndex == toIndex) 136 | return descrip + " : [" + toIndex + "]" + e; 137 | 138 | return descrip + " : [" + fromIndex + ", " + toIndex + "]" + e; 139 | } 140 | } 141 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/curve/NURBSpline.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.curve; 33 | 34 | /** 35 |

General implementation of the Non-Uniform Rational B-spline or NURB-Spline. The main advantage 36 | of the NURB-Spline over the B-Spline is the ability to represent conic sections. To do this, a curve 37 | with degree of 2 is used. Figure 1 contains examples of conic arcs. 38 | 39 |

40 | 41 |

NURB-Splines can also be used to generate circles as shown in figure 2. 42 | 43 |

44 | 45 |

As seen in the figures, every control-point has an associated weight value. The weight-values control 46 | how much relative pull each control-point has on the curve. If the weight-value is 0, then the associated 47 | point will have no affect on the curve. If a point has an associated weight of 0, but the curve is expected 48 | to pass through that point, then it is likely that the result of evaluation will be the origin. All weights 49 | must be >= 0. 50 | */ 51 | public class NURBSpline extends BSpline { 52 | 53 | private static final ThreadLocal SHARED_DATA = new ThreadLocal(){ 54 | protected SharedData initialValue() { 55 | return new SharedData(); 56 | } 57 | }; 58 | private final SharedData sharedData = SHARED_DATA.get(); 59 | 60 | private static class SharedData { 61 | private double[] nw = new double[0]; // (required length >= numPts) 62 | private double[] weight = new double[0]; // (required length >= numPts) 63 | } 64 | 65 | private ValueVector weightVector = new ValueVector(new double[] { 1, 1, 1, 1 }, 4); 66 | private boolean useWeightVector = true; 67 | 68 | public NURBSpline(ControlPath cp, GroupIterator gi) { 69 | super(cp, gi); 70 | } 71 | 72 | protected void eval(double[] p) { 73 | int dim = p.length - 1; 74 | double t = p[dim]; 75 | double sum2 = 0; 76 | 77 | int numPts = gi.getGroupSize(); 78 | 79 | for (int i = 0; i < numPts; i++) { 80 | sharedData.nw[i] = N(t, i) * sharedData.weight[i]; 81 | sum2 += sharedData.nw[i]; 82 | } 83 | 84 | if (sum2 == 0) sum2 = 1; 85 | 86 | for (int i = 0; i < dim; i++) { 87 | double sum1 = 0; 88 | gi.set(0,0); 89 | 90 | for (int j = 0; j < numPts; j++) 91 | sum1 += sharedData.nw[j] * cp.getPoint(gi.next()).getLocation()[i]; 92 | 93 | p[i] = sum1 / sum2; 94 | } 95 | } 96 | 97 | /** 98 | Returns the weight-vector. 99 | 100 | @see #setWeightVector(ValueVector) 101 | */ 102 | public ValueVector getWeightVector() { 103 | return weightVector; 104 | } 105 | 106 | /** 107 | Sets the weight-vector. 108 | 109 | @see #getWeightVector() 110 | @throws IllegalArgumentException If the value-vector is null. 111 | */ 112 | public void setWeightVector(ValueVector v) { 113 | if (v == null) 114 | throw new IllegalArgumentException("Weight-vector cannot be null."); 115 | 116 | weightVector = v; 117 | } 118 | 119 | /** 120 | Returns the value of the useWeightVector flag. The default value is true. 121 | 122 | @see #setUseWeightVector(boolean) 123 | */ 124 | public boolean getUseWeightVector() { 125 | return useWeightVector; 126 | } 127 | 128 | /** 129 | Sets the value of the useWeightVector flag. If the flag is true, then the internal weightVector 130 | will be used. If the flag is false, then all weights will be assumed to be 1. 131 | 132 | @see #getUseWeightVector() 133 | */ 134 | public void setUseWeightVector(boolean b) { 135 | useWeightVector = b; 136 | } 137 | 138 | /** 139 | The requirements of the appendTo method include the requirements of the BSpline appendTo method, plus 140 | a couple more. The additional requirements only apply if the useWeightVector flag is true. If so, then 141 | the weight-vector must have size equal to the group-size of the GroupIterator and all weights must have a 142 | value >= 0. This method returns quietly if these requirements are not met. 143 | 144 | @see com.graphbuilder.curve.BSpline#appendTo(MultiPath) 145 | */ 146 | public void appendTo(MultiPath mp) { 147 | if (!gi.isInRange(0, cp.numPoints())) 148 | throw new IllegalArgumentException("Group iterator not in range"); 149 | int numPts = gi.getGroupSize(); 150 | 151 | if (sharedData.nw.length < numPts) { 152 | sharedData.nw = new double[2 * numPts]; 153 | sharedData.weight = new double[2 * numPts]; 154 | } 155 | 156 | if (useWeightVector) { 157 | if (weightVector.size() != numPts) 158 | throw new IllegalArgumentException("weightVector.size(" + weightVector.size() + ") != group iterator size(" + numPts + ")"); 159 | 160 | for (int i = 0; i < numPts; i++) { 161 | sharedData.weight[i] = weightVector.get(i); 162 | if (sharedData.weight[i] < 0) 163 | throw new IllegalArgumentException("Negative weight not allowed"); 164 | } 165 | } 166 | else { 167 | for (int i = 0; i < numPts; i++) 168 | sharedData.weight[i] = 1; 169 | } 170 | 171 | super.appendTo(mp); 172 | } 173 | 174 | public void resetMemory() { 175 | super.resetMemory(); 176 | if (sharedData.nw.length > 0) { 177 | sharedData.nw = new double[0]; 178 | sharedData.weight = new double[0]; 179 | } 180 | } 181 | } 182 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/curve/ParametricCurve.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.curve; 33 | 34 | /** 35 | Curves that extend the ParametricCurve class are continuous and can use the 36 | BinaryCurveApproximationAlgorithm class to generate a sequence of points that 37 | approximate the curve. Note: Approximate means a finite set of points that 38 | are on the curve, not close to the curve. 39 | 40 | @see com.graphbuilder.curve.BinaryCurveApproximationAlgorithm 41 | @see com.graphbuilder.curve.Curve 42 | */ 43 | public abstract class ParametricCurve extends Curve { 44 | 45 | public ParametricCurve(ControlPath cp, GroupIterator gp) { 46 | super(cp, gp); 47 | } 48 | 49 | /** 50 | The eval method evaluates a point on a curve given a parametric value "t". The parametric 51 | value "t" is stored in the last index location of the specified double array. This value 52 | should not be changed. The dimension of the point to evaluate is p.length - 1. The result 53 | of the evaluation is placed in index locations 0 .. p.length - 2 (inclusive). 54 | 55 | The eval method should remain protected except for those curves that do no need any 56 | preparation to be done in the appendTo method. 57 | */ 58 | protected abstract void eval(double[] p); 59 | 60 | /** 61 | The sample limit specifies how many additional subdivisions are done to ensure that there 62 | are no missed pieces of the curve. The sample limit must be >= 0. 63 | */ 64 | public abstract int getSampleLimit(); 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/curve/Point.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.curve; 33 | 34 | /** 35 | A representation of an n-dimensional point. The dimension of the point is the length of the 36 | array returned by the getLocation() method. 37 | 38 | @see com.graphbuilder.curve.ControlPath 39 | @see com.graphbuilder.curve.Curve 40 | */ 41 | public interface Point { 42 | 43 | /** 44 | Sets the location of the point. Some implementations may copy the values of the 45 | array or may set the internal reference to the array. Some implementations may check 46 | the length of the array or the values. If the values need to be restricted, then the 47 | getLocation() method should return a copy of the data. 48 | */ 49 | public void setLocation(double[] p); 50 | 51 | /** 52 | Returns either a new array or internal temporary array with a copy of the data or a 53 | direct reference to the array. In the case where a copy is returned and later modified, 54 | the setLocation must be called to apply the data. The values in the returned array 55 | must represent the absolute location of the point. 56 | */ 57 | public double[] getLocation(); 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/curve/Polyline.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.curve; 33 | 34 | /** 35 | A polyline is a sequence of connected line segments based on the control-path points specified 36 | by the group-iterator. 37 | */ 38 | 39 | public class Polyline extends Curve { 40 | 41 | public Polyline(ControlPath cp, GroupIterator gi) { 42 | super(cp, gi); 43 | } 44 | 45 | public void appendTo(MultiPath mp) { 46 | if (!gi.isInRange(0, cp.numPoints())) 47 | throw new IllegalArgumentException("Group iterator not in range"); 48 | 49 | gi.set(0, 0); 50 | 51 | if (connect) 52 | mp.lineTo(cp.getPoint(gi.next()).getLocation()); 53 | else 54 | mp.moveTo(cp.getPoint(gi.next()).getLocation()); 55 | 56 | while (gi.hasNext()) 57 | mp.lineTo(cp.getPoint(gi.next()).getLocation()); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/curve/ShapeMultiPathIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.curve; 33 | 34 | import java.awt.geom.*; 35 | 36 | class ShapeMultiPathIterator implements PathIterator { 37 | 38 | private final int ai0; 39 | private final int ai1; 40 | private final AffineTransform at; 41 | private final ShapeMultiPath smp; 42 | private final int windingRule; 43 | private int n = 0; 44 | 45 | ShapeMultiPathIterator(ShapeMultiPath smp, AffineTransform at) { 46 | this.smp = smp; 47 | int[] bv = smp.getBasisVectors(); 48 | ai0 = bv[0]; 49 | ai1 = bv[1]; 50 | this.at = at; 51 | windingRule = smp.getWindingRule(); 52 | } 53 | 54 | public int getWindingRule() { 55 | return windingRule; 56 | } 57 | 58 | public boolean isDone() { 59 | return n >= smp.getNumPoints(); 60 | } 61 | 62 | public void next() { 63 | n++; 64 | } 65 | 66 | public int currentSegment(float[] coords) { 67 | double[] p = smp.get(n); 68 | coords[0] = (float) p[ai0]; 69 | coords[1] = (float) p[ai1]; 70 | 71 | if (n > 0 && p == smp.get(0)) 72 | return PathIterator.SEG_CLOSE; 73 | 74 | if (at != null) 75 | at.transform(coords, 0, coords, 0, 1); 76 | 77 | if (smp.getType(n) == MultiPath.MOVE_TO) 78 | return PathIterator.SEG_MOVETO; 79 | 80 | return PathIterator.SEG_LINETO; 81 | } 82 | 83 | public int currentSegment(double[] coords) { 84 | double[] p = smp.get(n); 85 | coords[0] = p[ai0]; 86 | coords[1] = p[ai1]; 87 | 88 | if (n > 0 && p == smp.get(0)) 89 | return PathIterator.SEG_CLOSE; 90 | 91 | if (at != null) 92 | at.transform(coords, 0, coords, 0, 1); 93 | 94 | 95 | if (smp.getType(n) == MultiPath.MOVE_TO) 96 | return PathIterator.SEG_MOVETO; 97 | 98 | return PathIterator.SEG_LINETO; 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/curve/ValueVector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.curve; 33 | 34 | /** 35 | A value-vector is a sequence of values that some curves use to define themselves, 36 | sometimes called a knot-vector or a weight-vector. The values are stored using an 37 | array. 38 | */ 39 | public class ValueVector { 40 | 41 | protected int size = 0; 42 | protected double[] value = null; 43 | 44 | /** 45 | Creates a ValueVector with initial capacity of 2. 46 | */ 47 | public ValueVector() { 48 | value = new double[2]; 49 | } 50 | 51 | /** 52 | Creates a ValueVector using the specified array and initial size. 53 | 54 | @throws IllegalArgumentException If the value array is null or size < 0 or size > data.length. 55 | */ 56 | public ValueVector(double[] value, int size) { 57 | if (value == null) 58 | throw new IllegalArgumentException("value array cannot be null."); 59 | 60 | if (size < 0 || size > value.length) 61 | throw new IllegalArgumentException("size >= 0 && size <= value.length required"); 62 | 63 | this.value = value; 64 | this.size = size; 65 | } 66 | 67 | /** 68 | Creates a ValueVector with the specified initial capacity. 69 | */ 70 | public ValueVector(int initialCapacity) { 71 | value = new double[initialCapacity]; 72 | } 73 | 74 | /** 75 | Returns the number of values in the value array. 76 | */ 77 | public int size() { 78 | return size; 79 | } 80 | 81 | /** 82 | Returns the value at the specified index. 83 | 84 | @throws IllegalArgumentException If index < 0 or index >= size. 85 | */ 86 | public double get(int index) { 87 | if (index < 0 || index >= size) 88 | throw new IllegalArgumentException("required: (index >= 0 && index < size) but: (index = " + index + ", size = " + size + ")"); 89 | 90 | return value[index]; 91 | } 92 | 93 | /** 94 | Sets the value at the specified index. 95 | 96 | @throws IllegalArgumentException If index < 0 or index >= size. 97 | */ 98 | public void set(double d, int index) { 99 | if (index < 0 || index >= size) 100 | throw new IllegalArgumentException("required: (index >= 0 && index < size) but: (index = " + index + ", size = " + size + ")"); 101 | 102 | value[index] = d; 103 | } 104 | 105 | /** 106 | Removes the value at the specified index. Values at a higher index are shifted to fill the space. 107 | 108 | @throws IllegalArgumentException If index < 0 or index >= size. 109 | */ 110 | public void remove(int index) { 111 | if (index < 0 || index >= size) 112 | throw new IllegalArgumentException("required: (index >= 0 && index < size) but: (index = " + index + ", size = " + size + ")"); 113 | 114 | for (int i = index + 1; i < size; i++) 115 | value[i-1] = value[i]; 116 | 117 | size--; 118 | } 119 | 120 | /** 121 | Adds a value to the value array at index location size. 122 | */ 123 | public void add(double d) { 124 | insert(d, size); 125 | } 126 | 127 | /** 128 | Inserts the value at the specified index location. Values at an equal or higher index are shifted to make space. 129 | 130 | @throws IllegalArgumentException If index < 0 or index > size. 131 | */ 132 | public void insert(double d, int index) { 133 | if (index < 0 || index > size) 134 | throw new IllegalArgumentException("required: (index >= 0 && index <= size) but: (index = " + index + ", size = " + size + ")"); 135 | 136 | ensureCapacity(size + 1); 137 | 138 | for (int i = size; i > index; i--) 139 | value[i] = value[i-1]; 140 | 141 | value[index] = d; 142 | size++; 143 | } 144 | 145 | /** 146 | Checks that the value array has the specified capacity, otherwise the capacity of the 147 | value array is increased to be the maximum between twice the current capacity and the 148 | specified capacity. 149 | */ 150 | public void ensureCapacity(int capacity) { 151 | if (value.length < capacity) { 152 | int x = 2 * value.length; 153 | if (x < capacity) x = capacity; 154 | double[] arr = new double[x]; 155 | for (int i = 0; i < size; i++) 156 | arr[i] = value[i]; 157 | value = arr; 158 | } 159 | } 160 | 161 | /** 162 | Creates a new value array of exact size, copying the values from the old array into the 163 | new one. 164 | */ 165 | public void trimArray() { 166 | if (size < value.length) { 167 | double[] arr = new double[size]; 168 | for (int i = 0; i < size; i++) 169 | arr[i] = value[i]; 170 | value = arr; 171 | } 172 | } 173 | } 174 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/curve/ValueVectorListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.curve; 33 | 34 | /** 35 | Listener interface to listen for changes in a ValueVector. The ValueVector class 36 | does not support listeners because it is designed to be lightweight. This class is 37 | intended to be used as a listener to a GUI component that is used to manipulate 38 | a value-vector. 39 | */ 40 | public interface ValueVectorListener { 41 | 42 | public void valueChanged(ValueVector v, int index, double oldValue); 43 | public void valueInserted(ValueVector v, int index, double value); 44 | public void valueRemoved(ValueVector v, int index, double oldValue); 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/curve/doc-files/basic_idea.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/virtuald/curvesapi/a39c6d234bd1b70a324a8cf4f5a2b0e1f2780c7f/src/main/java/com/graphbuilder/curve/doc-files/basic_idea.gif -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/curve/doc-files/bezier1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/virtuald/curvesapi/a39c6d234bd1b70a324a8cf4f5a2b0e1f2780c7f/src/main/java/com/graphbuilder/curve/doc-files/bezier1.gif -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/curve/doc-files/bspline1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/virtuald/curvesapi/a39c6d234bd1b70a324a8cf4f5a2b0e1f2780c7f/src/main/java/com/graphbuilder/curve/doc-files/bspline1.gif -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/curve/doc-files/bspline2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/virtuald/curvesapi/a39c6d234bd1b70a324a8cf4f5a2b0e1f2780c7f/src/main/java/com/graphbuilder/curve/doc-files/bspline2.gif -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/curve/doc-files/cardinal1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/virtuald/curvesapi/a39c6d234bd1b70a324a8cf4f5a2b0e1f2780c7f/src/main/java/com/graphbuilder/curve/doc-files/cardinal1.gif -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/curve/doc-files/cubicb1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/virtuald/curvesapi/a39c6d234bd1b70a324a8cf4f5a2b0e1f2780c7f/src/main/java/com/graphbuilder/curve/doc-files/cubicb1.gif -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/curve/doc-files/cubicb2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/virtuald/curvesapi/a39c6d234bd1b70a324a8cf4f5a2b0e1f2780c7f/src/main/java/com/graphbuilder/curve/doc-files/cubicb2.gif -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/curve/doc-files/cubicb3.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/virtuald/curvesapi/a39c6d234bd1b70a324a8cf4f5a2b0e1f2780c7f/src/main/java/com/graphbuilder/curve/doc-files/cubicb3.gif -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/curve/doc-files/lagrange1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/virtuald/curvesapi/a39c6d234bd1b70a324a8cf4f5a2b0e1f2780c7f/src/main/java/com/graphbuilder/curve/doc-files/lagrange1.gif -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/curve/doc-files/lagrange2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/virtuald/curvesapi/a39c6d234bd1b70a324a8cf4f5a2b0e1f2780c7f/src/main/java/com/graphbuilder/curve/doc-files/lagrange2.gif -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/curve/doc-files/lagrange3.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/virtuald/curvesapi/a39c6d234bd1b70a324a8cf4f5a2b0e1f2780c7f/src/main/java/com/graphbuilder/curve/doc-files/lagrange3.gif -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/curve/doc-files/lagrange4.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/virtuald/curvesapi/a39c6d234bd1b70a324a8cf4f5a2b0e1f2780c7f/src/main/java/com/graphbuilder/curve/doc-files/lagrange4.gif -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/curve/doc-files/natcubic1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/virtuald/curvesapi/a39c6d234bd1b70a324a8cf4f5a2b0e1f2780c7f/src/main/java/com/graphbuilder/curve/doc-files/natcubic1.gif -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/curve/doc-files/natcubic2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/virtuald/curvesapi/a39c6d234bd1b70a324a8cf4f5a2b0e1f2780c7f/src/main/java/com/graphbuilder/curve/doc-files/natcubic2.gif -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/curve/doc-files/nurbs1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/virtuald/curvesapi/a39c6d234bd1b70a324a8cf4f5a2b0e1f2780c7f/src/main/java/com/graphbuilder/curve/doc-files/nurbs1.gif -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/curve/doc-files/nurbs2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/virtuald/curvesapi/a39c6d234bd1b70a324a8cf4f5a2b0e1f2780c7f/src/main/java/com/graphbuilder/curve/doc-files/nurbs2.gif -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/curve/doc-files/problem_case.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/virtuald/curvesapi/a39c6d234bd1b70a324a8cf4f5a2b0e1f2780c7f/src/main/java/com/graphbuilder/curve/doc-files/problem_case.gif -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/geom/Point2d.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.geom; 33 | 34 | import com.graphbuilder.curve.Point; 35 | 36 | /** 37 | The Point2d interface represents a two-dimensional point using double precision. 38 | This interface is useful when writing general geometry algorithms. 39 | */ 40 | public interface Point2d extends Point { 41 | 42 | /** 43 | Sets the location of the point. 44 | */ 45 | public void setLocation(double x, double y); 46 | 47 | /** 48 | Returns the x-coordinate. 49 | */ 50 | public double getX(); 51 | 52 | /** 53 | Returns the y-coordinate. 54 | */ 55 | public double getY(); 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/geom/Point3d.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.geom; 33 | 34 | /** 35 | The Point3d interface represents a three-dimensional point using double precision. 36 | This interface is useful when writing general geometry algorithms. 37 | */ 38 | public interface Point3d extends Point2d { 39 | 40 | /** 41 | Sets the location of the point. 42 | */ 43 | public void setLocation(double x, double y, double z); 44 | 45 | /** 46 | Returns the z-coordinate. 47 | */ 48 | public double getZ(); 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/geom/PointFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.geom; 33 | 34 | public class PointFactory { 35 | 36 | static class Point2D implements Point2d { 37 | 38 | double [] pts; 39 | 40 | public Point2D(double x, double y) { 41 | pts = new double[]{x, y}; 42 | } 43 | 44 | public double getX() { 45 | return pts[0]; 46 | } 47 | 48 | public double getY() { 49 | return pts[1]; 50 | } 51 | 52 | public void setLocation(double[] p) { 53 | pts[0] = p[0]; 54 | pts[1] = p[1]; 55 | } 56 | 57 | public void setLocation(double x, double y) { 58 | pts[0] = x; 59 | pts[1] = y; 60 | } 61 | 62 | public double[] getLocation() { 63 | return pts; 64 | } 65 | } 66 | 67 | public static Point2d create(double x, double y) { 68 | return new Point2D(x, y); 69 | } 70 | 71 | 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/AddNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math; 33 | 34 | /** 35 | A node of an expression tree, represented by the symbol "+". 36 | */ 37 | public class AddNode extends OpNode { 38 | 39 | public AddNode(Expression leftChild, Expression rightChild) { 40 | super(leftChild, rightChild); 41 | } 42 | 43 | /** 44 | Adds the evaluation of the left side to the evaluation of the right side and returns the result. 45 | */ 46 | public double eval(VarMap v, FuncMap f) { 47 | double a = leftChild.eval(v, f); 48 | double b = rightChild.eval(v, f); 49 | return a + b; 50 | } 51 | 52 | public String getSymbol() { 53 | return "+"; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/DivNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math; 33 | 34 | /** 35 | A node of an expression tree, represented by the symbol "/". 36 | */ 37 | public class DivNode extends OpNode { 38 | 39 | public DivNode(Expression leftChild, Expression rightChild) { 40 | super(leftChild, rightChild); 41 | } 42 | 43 | /** 44 | Divides the evaluation of the left side by the evaluation of the right side and returns the result. 45 | */ 46 | public double eval(VarMap v, FuncMap f) { 47 | double a = leftChild.eval(v, f); 48 | double b = rightChild.eval(v, f); 49 | return a / b; 50 | } 51 | 52 | public String getSymbol() { 53 | return "/"; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/Expression.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math; 33 | 34 | import com.graphbuilder.struc.Bag; 35 | 36 | /** 37 | The class from which all nodes of an expression tree are descendents. Expressions can be evaluated 38 | using the eval method. Expressions that are or have FuncNodes or VarNodes as descendents must provide 39 | a VarMap or FuncMap respectively. Expressions that consist entirely of OpNodes and ValNodes do not 40 | require a VarMap or FuncMap. For Expressions that support children (OpNodes, FuncNodes), a child can 41 | only be accepted provided it currently has no parent, a cyclic reference is not formed, and it is 42 | non-null. 43 | */ 44 | public abstract class Expression { 45 | 46 | protected Expression parent = null; 47 | 48 | /** 49 | Returns the result of evaluating the expression tree rooted at this node. 50 | */ 51 | public abstract double eval(VarMap v, FuncMap f); 52 | 53 | /** 54 | Returns true if this node is a descendent of the specified node, false otherwise. By this 55 | methods definition, a node is a descendent of itself. 56 | */ 57 | public boolean isDescendent(Expression x) { 58 | Expression y = this; 59 | 60 | while (y != null) { 61 | if (y == x) 62 | return true; 63 | y = y.parent; 64 | } 65 | 66 | return false; 67 | } 68 | 69 | /** 70 | Returns the parent of this node. Returns null if this node is the root node of an expression-tree. 71 | */ 72 | public Expression getParent() { 73 | return parent; 74 | } 75 | 76 | /** 77 | Protected method used to verify that the specified expression can be included as a child 78 | expression of this node. An expression cannot be included as a child if it is null, it 79 | currently has a parent, or a cyclic reference would be formed. 80 | 81 | @throws IllegalArgumentException If the specified expression is not accepted. 82 | */ 83 | protected void checkBeforeAccept(Expression x) { 84 | if (x == null) 85 | throw new IllegalArgumentException("expression cannot be null"); 86 | 87 | if (x.parent != null) 88 | throw new IllegalArgumentException("expression must be removed parent"); 89 | 90 | if (isDescendent(x)) 91 | throw new IllegalArgumentException("cyclic reference"); 92 | } 93 | 94 | /** 95 | Returns an array of exact length of the variable names contained in the expression tree rooted at this node. 96 | */ 97 | public String[] getVariableNames() { 98 | return getTermNames(true); 99 | } 100 | 101 | /** 102 | Returns an array of exact length of the function names contained in the expression tree rooted at this node. 103 | */ 104 | public String[] getFunctionNames() { 105 | return getTermNames(false); 106 | } 107 | 108 | private String[] getTermNames(boolean varNames) { 109 | Bag b = new Bag(); 110 | getTermNames(this, b, varNames); 111 | String[] arr = new String[b.size()]; 112 | for (int i = 0; i < arr.length; i++) 113 | arr[i] = (String) b.get(i); 114 | return arr; 115 | } 116 | 117 | private static void getTermNames(Expression x, Bag b, boolean varNames) { 118 | if (x instanceof OpNode) { 119 | OpNode o = (OpNode) x; 120 | getTermNames(o.leftChild, b, varNames); 121 | getTermNames(o.rightChild, b, varNames); 122 | } 123 | else if (x instanceof VarNode) { 124 | if (varNames) { 125 | VarNode v = (VarNode) x; 126 | if (!b.contains(v.name)) 127 | b.add(v.name); 128 | } 129 | } 130 | else if (x instanceof FuncNode) { 131 | FuncNode f = (FuncNode) x; 132 | 133 | if (!varNames) { 134 | if (!b.contains(f.name)) 135 | b.add(f.name); 136 | } 137 | 138 | for (int i = 0; i < f.numChildren(); i++) 139 | getTermNames(f.child(i), b, varNames); 140 | } 141 | } 142 | 143 | /** 144 | Returns a string that represents the expression tree rooted at this node. 145 | */ 146 | public String toString() { 147 | StringBuffer sb = new StringBuffer(); 148 | toString(this, sb); 149 | return sb.toString(); 150 | } 151 | 152 | private static void toString(Expression x, StringBuffer sb) { 153 | if (x instanceof OpNode) { 154 | OpNode o = (OpNode) x; 155 | sb.append("("); 156 | toString(o.leftChild, sb); 157 | sb.append(o.getSymbol()); 158 | toString(o.rightChild, sb); 159 | sb.append(")"); 160 | } 161 | else if (x instanceof TermNode) { 162 | TermNode t = (TermNode) x; 163 | 164 | if (t.getNegate()) { 165 | sb.append("("); 166 | sb.append("-"); 167 | } 168 | 169 | sb.append(t.getName()); 170 | 171 | if (t instanceof FuncNode) { 172 | FuncNode f = (FuncNode) t; 173 | 174 | sb.append("("); 175 | 176 | if (f.numChildren() > 0) 177 | toString(f.child(0), sb); 178 | 179 | for (int i = 1; i < f.numChildren(); i++) { 180 | sb.append(", "); 181 | toString(f.child(i), sb); 182 | } 183 | 184 | sb.append(")"); 185 | } 186 | 187 | if (t.getNegate()) 188 | sb.append(")"); 189 | } 190 | else if (x instanceof ValNode) { 191 | sb.append(((ValNode) x).val); 192 | } 193 | } 194 | } 195 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/ExpressionParseException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math; 33 | 34 | /** 35 | Exception thrown if expression cannot be parsed correctly. 36 | 37 | @see com.graphbuilder.math.ExpressionTree 38 | */ 39 | public class ExpressionParseException extends RuntimeException { 40 | 41 | private String descrip = null; 42 | private int index = 0; 43 | 44 | public ExpressionParseException(String descrip, int index) { 45 | this.descrip = descrip; 46 | this.index = index; 47 | } 48 | 49 | /** 50 | Returns the description that explains why the exception was thrown. 51 | */ 52 | public String getDescription() { 53 | return descrip; 54 | } 55 | 56 | /** 57 | Returns an index value into the expression string. Note, the index value 58 | may be less than zero or greater then the length of the expression string. 59 | */ 60 | public int getIndex() { 61 | return index; 62 | } 63 | 64 | /** 65 | Returns a string formatted such as "(index) description". 66 | */ 67 | public String toString() { 68 | return "(" + index + ") " + descrip; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/FuncNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math; 33 | 34 | import com.graphbuilder.struc.Bag; 35 | 36 | /** 37 | A node of an expression tree that represents a function. A FuncNode can have 0 or more children. 38 | */ 39 | public class FuncNode extends TermNode { 40 | 41 | private Bag bag = new Bag(1); 42 | private double[] of = new double[1]; 43 | 44 | public FuncNode(String name, boolean negate) { 45 | super(name, negate); 46 | } 47 | 48 | /** 49 | Adds the expression as a child. 50 | */ 51 | public void add(Expression x) { 52 | insert(x, bag.size()); 53 | } 54 | 55 | /** 56 | Adds the expression as a child at the specified index. 57 | */ 58 | public void insert(Expression x, int i) { 59 | checkBeforeAccept(x); 60 | int oldCap = bag.getCapacity(); 61 | bag.insert(x, i); 62 | int newCap = bag.getCapacity(); 63 | 64 | if (oldCap != newCap) 65 | of = new double[newCap]; 66 | 67 | x.parent = this; 68 | } 69 | 70 | /** 71 | Removes the specified expression as a child. Does nothing if the expression was not a child. 72 | */ 73 | public void remove(Expression x) { 74 | int size = bag.size(); 75 | bag.remove(x); 76 | if (size != bag.size()) 77 | x.parent = null; 78 | } 79 | 80 | /** 81 | Returns the number of child expressions. 82 | */ 83 | public int numChildren() { 84 | return bag.size(); 85 | } 86 | 87 | /** 88 | Returns the child expression at the specified index. 89 | */ 90 | public Expression child(int i) { 91 | return (Expression) bag.get(i); 92 | } 93 | 94 | /** 95 | Evaluates each of the children, storing the result in an internal double array. The FuncMap is 96 | used to obtain a Function object based on the name of this FuncNode. The function is passed 97 | the double array and returns a result. If negate is true, the result is negated. The result 98 | is then returned. The numParam passed to the function is the number of children of this FuncNode. 99 | */ 100 | public double eval(VarMap v, FuncMap f) { 101 | int numParam = bag.size(); 102 | 103 | for (int i = 0; i < numParam; i++) 104 | of[i] = child(i).eval(v, f); 105 | 106 | double result = f.getFunction(name, numParam).of(of, numParam); 107 | 108 | if (negate) result = -result; 109 | 110 | return result; 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/MultNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math; 33 | 34 | /** 35 | A node of an expression tree, represented by the symbol "*". 36 | */ 37 | public class MultNode extends OpNode { 38 | 39 | public MultNode(Expression leftChild, Expression rightChild) { 40 | super(leftChild, rightChild); 41 | } 42 | 43 | /** 44 | Multiples the evaluation of the left side and the evaluation of the right side and returns the result. 45 | */ 46 | public double eval(VarMap v, FuncMap f) { 47 | double a = leftChild.eval(v, f); 48 | double b = rightChild.eval(v, f); 49 | return a * b; 50 | } 51 | 52 | public String getSymbol() { 53 | return "*"; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/OpNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math; 33 | 34 | /** 35 | A node of an expression tree that has exactly 2 children, a left child and a right child. After the 36 | children are evaluated, a mathematical operation is applied and the result is returned. 37 | */ 38 | public abstract class OpNode extends Expression { 39 | 40 | protected Expression leftChild = null; 41 | protected Expression rightChild = null; 42 | 43 | public OpNode(Expression leftChild, Expression rightChild) { 44 | setLeftChild(leftChild); 45 | setRightChild(rightChild); 46 | } 47 | 48 | public void setLeftChild(Expression x) { 49 | checkBeforeAccept(x); 50 | if (leftChild != null) 51 | leftChild.parent = null; 52 | x.parent = this; 53 | leftChild = x; 54 | } 55 | 56 | public void setRightChild(Expression x) { 57 | checkBeforeAccept(x); 58 | if (rightChild != null) 59 | rightChild.parent = null; 60 | x.parent = this; 61 | rightChild = x; 62 | } 63 | 64 | public Expression getLeftChild() { 65 | return leftChild; 66 | } 67 | 68 | public Expression getRightChild() { 69 | return rightChild; 70 | } 71 | 72 | /** 73 | Returns the text symbol that represents the operation. 74 | */ 75 | public abstract String getSymbol(); 76 | } 77 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/PascalsTriangle.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math; 33 | 34 | 35 | /** 36 | PascalsTriangle can be used for O(1) lookup of the nCr function. 37 | */ 38 | public final class PascalsTriangle { 39 | 40 | private static final ThreadLocal SHARED_DATA = new ThreadLocal(){ 41 | protected SharedData initialValue() { 42 | return new SharedData(); 43 | } 44 | }; 45 | private final SharedData sharedData = SHARED_DATA.get(); 46 | 47 | private static class SharedData { 48 | private double[][] pt = new double[][] { {1} }; 49 | } 50 | 51 | public PascalsTriangle() {} 52 | 53 | /** 54 | The nCr function returns the number of ways r things can be chosen from a set of size n. 55 | Mathematically, it is defined as: n! / (r! * (n - r)!) 56 | Although the result is always a whole number, double precision is used because 57 | the maximum value a double can represent is larger than long. Thus, large returned 58 | values will only be an approximation of the actual value. If the result exceeds the 59 | capabilities of double precision then the result can be checked using Double.isInfinite(...). 60 | For example: System.out.println(PascalsTriangle.nCr(1030, 515)); // outputs: Infinity 61 | If the value of n or r is less than 0 or the value of r is greater than n then 0 is 62 | returned. 63 | */ 64 | public double nCr(int n, int r) { 65 | if (n < 0 || r < 0 || r > n) return 0; 66 | 67 | if (n >= sharedData.pt.length) { 68 | int d = 2 * sharedData.pt.length; 69 | double[][] pt2 = null; 70 | if (n > d) 71 | pt2 = new double[n + 1][]; 72 | else 73 | pt2 = new double[d + 1][]; 74 | 75 | for (int i = 0; i < sharedData.pt.length; i++) 76 | pt2[i] = sharedData.pt[i]; 77 | 78 | for (int i = sharedData.pt.length; i < pt2.length; i++) { 79 | pt2[i] = new double[(i / 2) + 1]; 80 | 81 | pt2[i][0] = 1; 82 | 83 | for (int j = 1; j < pt2[i].length; j++) { 84 | double x = pt2[i-1][j-1]; 85 | if (j < pt2[i-1].length) 86 | x = x + pt2[i-1][j]; 87 | else 88 | x = 2 * x; 89 | 90 | pt2[i][j] = x; 91 | } 92 | } 93 | sharedData.pt = pt2; 94 | } 95 | 96 | if (2 * r > n) 97 | r = n - r; 98 | 99 | return sharedData.pt[n][r]; 100 | } 101 | 102 | /** 103 | Resets the internal array to the initial state to free up memory. 104 | */ 105 | public void reset() { 106 | sharedData.pt = new double[][] { {1} }; 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/PowNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math; 33 | 34 | /** 35 | A node of an expression tree, represented by the symbol "^". 36 | */ 37 | public class PowNode extends OpNode { 38 | 39 | public PowNode(Expression leftChild, Expression rightChild) { 40 | super(leftChild, rightChild); 41 | } 42 | 43 | /** 44 | Raises the evaluation of the left side to the power of the evaluation of the right side and returns the result. 45 | */ 46 | public double eval(VarMap v, FuncMap f) { 47 | double a = leftChild.eval(v, f); 48 | double b = rightChild.eval(v, f); 49 | return Math.pow(a, b); 50 | } 51 | 52 | public String getSymbol() { 53 | return "^"; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/SubNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math; 33 | 34 | /** 35 | A node of an expression tree, represented by the symbol "-". 36 | */ 37 | public class SubNode extends OpNode { 38 | 39 | public SubNode(Expression leftChild, Expression rightChild) { 40 | super(leftChild, rightChild); 41 | } 42 | 43 | /** 44 | Subtracts the evaluation of the right side from the evaluation of the left side and returns the result. 45 | */ 46 | public double eval(VarMap v, FuncMap f) { 47 | double a = leftChild.eval(v, f); 48 | double b = rightChild.eval(v, f); 49 | return a - b; 50 | } 51 | 52 | public String getSymbol() { 53 | return "-"; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/TermNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math; 33 | 34 | /** 35 | A node of an expression tree that represents a variable or a function. 36 | */ 37 | public abstract class TermNode extends Expression { 38 | 39 | protected String name = null; 40 | protected boolean negate = false; 41 | 42 | public TermNode(String name, boolean negate) { 43 | setName(name); 44 | setNegate(negate); 45 | } 46 | 47 | /** 48 | Returns true if the term should negate the result before returning it in the eval method. 49 | */ 50 | public boolean getNegate() { 51 | return negate; 52 | } 53 | 54 | public void setNegate(boolean b) { 55 | negate = b; 56 | } 57 | 58 | /** 59 | Returns the name of the term. 60 | */ 61 | public String getName() { 62 | return name; 63 | } 64 | 65 | /** 66 | Sets the name of the term. Valid names must not begin with a digit or a decimal, and must not contain 67 | round brackets, operators, commas or whitespace. 68 | 69 | @throws IllegalArgumentException If the name is null or invalid. 70 | */ 71 | public void setName(String s) { 72 | if (s == null) 73 | throw new IllegalArgumentException("name cannot be null"); 74 | 75 | if (!isValidName(s)) 76 | throw new IllegalArgumentException("invalid name: " + s); 77 | 78 | name = s; 79 | } 80 | 81 | private static boolean isValidName(String s) { 82 | if (s.length() == 0) return false; 83 | 84 | char c = s.charAt(0); 85 | 86 | if (c >= '0' && c <= '9' || c == '.' || c == ',' || c == '(' || c == ')' || c == '^' || c == '*' || c == '/' || c == '+' || c == '-' || c == ' ' || c == '\t' || c == '\n') 87 | return false; 88 | 89 | for (int i = 1; i < s.length(); i++) { 90 | c = s.charAt(i); 91 | 92 | if (c == ',' || c == '(' || c == ')' || c == '^' || c == '*' || c == '/' || c == '+' || c == '-' || c == ' ' || c == '\t' || c == '\n') 93 | return false; 94 | } 95 | 96 | return true; 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/ValNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math; 33 | 34 | /** 35 | A node of an expression tree that represents a value. A ValNode cannot have any children. 36 | */ 37 | public class ValNode extends Expression { 38 | 39 | protected double val = 0.0; 40 | 41 | public ValNode(double d) { 42 | val = d; 43 | } 44 | 45 | /** 46 | Returns the value. 47 | */ 48 | public double eval(VarMap v, FuncMap f) { 49 | return val; 50 | } 51 | 52 | public double getValue() { 53 | return val; 54 | } 55 | 56 | public void setValue(double d) { 57 | val = d; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/VarMap.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math; 33 | 34 | /** 35 |

VarMap maps a name to a value. A VarMap is used in the eval method of an Expression object. 36 | This class can be used as the default variable-map. 37 | 38 |

During the evaluation of an expression, if a variable is not supported then a RuntimeException is thrown. 39 | Case sensitivity can only be specified in the constructor (for consistency). When case sensitivity is false, 40 | the String.equalsIgnoreCase method is used. When case sensitivity is true, the String.equals method is used. 41 | By default, case sensitivity is true. 42 | */ 43 | public class VarMap { 44 | 45 | private boolean caseSensitive = true; 46 | private String[] name = new String[2]; 47 | private double[] value = new double[2]; 48 | private int numVars = 0; 49 | 50 | public VarMap() { 51 | this(true); 52 | } 53 | 54 | public VarMap(boolean caseSensitive) { 55 | this.caseSensitive = caseSensitive; 56 | } 57 | 58 | /** 59 | Returns the value associated with the specified variable name. 60 | 61 | @throws RuntimeException If a matching variable name cannot be found. 62 | */ 63 | public double getValue(String varName) { 64 | for (int i = 0; i < numVars; i++) 65 | if (caseSensitive && name[i].equals(varName) || !caseSensitive && name[i].equalsIgnoreCase(varName)) 66 | return value[i]; 67 | 68 | throw new RuntimeException("variable value has not been set: " + varName); 69 | } 70 | 71 | /** 72 | Assigns the value to the specified variable name. 73 | 74 | @throws IllegalArgumentException If the variable name is null. 75 | */ 76 | public void setValue(String varName, double val) { 77 | if (varName == null) 78 | throw new IllegalArgumentException("varName cannot be null"); 79 | 80 | for (int i = 0; i < numVars; i++) { 81 | if (caseSensitive && name[i].equals(varName) || !caseSensitive && name[i].equalsIgnoreCase(varName)) { 82 | value[i] = val; 83 | return; 84 | } 85 | } 86 | 87 | if (numVars == name.length) { 88 | String[] tmp1 = new String[2 * numVars]; 89 | double[] tmp2 = new double[tmp1.length]; 90 | 91 | for (int i = 0; i < numVars; i++) { 92 | tmp1[i] = name[i]; 93 | tmp2[i] = value[i]; 94 | } 95 | 96 | name = tmp1; 97 | value = tmp2; 98 | } 99 | 100 | name[numVars] = varName; 101 | value[numVars] = val; 102 | numVars++; 103 | } 104 | 105 | /** 106 | Returns true if the case of the variable names is considered. 107 | */ 108 | public boolean isCaseSensitive() { 109 | return caseSensitive; 110 | } 111 | 112 | /** 113 | Returns an array of exact length of the variable names stored in this map. 114 | */ 115 | public String[] getVariableNames() { 116 | String[] arr = new String[numVars]; 117 | 118 | for (int i = 0; i < arr.length; i++) 119 | arr[i] = name[i]; 120 | 121 | return arr; 122 | } 123 | 124 | /** 125 | Returns an array of exact length of the values stored in this map. The returned 126 | array corresponds to the order of the names returned by getVariableNames. 127 | */ 128 | public double[] getValues() { 129 | double[] arr = new double[numVars]; 130 | 131 | for (int i = 0; i < arr.length; i++) 132 | arr[i] = value[i]; 133 | 134 | return arr; 135 | } 136 | 137 | /** 138 | Removes the variable-name from the map. Does nothing if the variable-name is not found. 139 | */ 140 | public void remove(String varName) { 141 | for (int i = 0; i < numVars; i++) { 142 | if (caseSensitive && name[i].equals(varName) || !caseSensitive && name[i].equalsIgnoreCase(varName)) { 143 | for (int j = i + 1; j < numVars; j++) { 144 | name[j - 1] = name[j]; 145 | value[j - 1] = value[j]; 146 | } 147 | numVars--; 148 | name[numVars] = null; 149 | value[numVars] = 0; 150 | break; 151 | } 152 | } 153 | } 154 | } 155 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/VarNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math; 33 | 34 | /** 35 | A node of an expression tree that represents a variable. A VarNode cannot have any children. 36 | */ 37 | public class VarNode extends TermNode { 38 | 39 | public VarNode(String name, boolean negate) { 40 | super(name, negate); 41 | } 42 | 43 | /** 44 | Returns the value associated with the variable name in the VarMap. 45 | */ 46 | public double eval(VarMap v, FuncMap f) { 47 | double val = v.getValue(name); 48 | 49 | if (negate) val = -val; 50 | 51 | return val; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/func/AbsFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math.func; 33 | 34 | /** 35 | The absolute function. 36 | 37 | @see java.lang.Math#abs(double) 38 | */ 39 | public class AbsFunction implements Function { 40 | 41 | public AbsFunction() {} 42 | 43 | /** 44 | Returns the positive value of the value stored at index location 0. 45 | */ 46 | public double of(double[] d, int numParam) { 47 | return Math.abs(d[0]); 48 | } 49 | 50 | /** 51 | Returns true only for 1 parameter, false otherwise. 52 | */ 53 | public boolean acceptNumParam(int numParam) { 54 | return numParam == 1; 55 | } 56 | 57 | public String toString() { 58 | return "abs(x)"; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/func/AcosFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math.func; 33 | 34 | /** 35 | The arc cosine function. 36 | 37 | @see java.lang.Math#acos(double) 38 | */ 39 | public class AcosFunction implements Function { 40 | 41 | public AcosFunction() {} 42 | 43 | /** 44 | Returns the arc cosine of the value at index location 0. 45 | */ 46 | public double of(double[] d, int numParam) { 47 | return java.lang.Math.acos(d[0]); 48 | } 49 | 50 | /** 51 | Returns true only for 1 parameter, false otherwise. 52 | */ 53 | public boolean acceptNumParam(int numParam) { 54 | return numParam == 1; 55 | } 56 | 57 | public String toString() { 58 | return "acos(x)"; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/func/AcoshFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math.func; 33 | 34 | /** 35 | The hyperbolic arc cosine function. 36 | */ 37 | public class AcoshFunction implements Function { 38 | 39 | public AcoshFunction() {} 40 | 41 | /** 42 | Returns the value of 2 * ln(sqrt((x+1)/2) + sqrt((x-1)/2)), where x is the 43 | value at index location 0. 44 | */ 45 | public double of(double[] d, int numParam) { 46 | double a = Math.sqrt((d[0] + 1) / 2); 47 | double b = Math.sqrt((d[0] - 1) / 2); 48 | return 2 * Math.log(a + b); 49 | } 50 | 51 | /** 52 | Returns true only for 1 parameter, false otherwise. 53 | */ 54 | public boolean acceptNumParam(int numParam) { 55 | return numParam == 1; 56 | } 57 | 58 | public String toString() { 59 | return "acosh(x)"; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/func/AsinFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math.func; 33 | 34 | /** 35 | The arc sine function. 36 | 37 | @see java.lang.Math#asin(double) 38 | */ 39 | public class AsinFunction implements Function { 40 | 41 | public AsinFunction() {} 42 | 43 | /** 44 | Returns the arc sine of the value at index location 0. 45 | */ 46 | public double of(double[] d, int numParam) { 47 | return java.lang.Math.asin(d[0]); 48 | } 49 | 50 | /** 51 | Returns true only for 1 parameter, false otherwise. 52 | */ 53 | public boolean acceptNumParam(int numParam) { 54 | return numParam == 1; 55 | } 56 | 57 | public String toString() { 58 | return "asin(x)"; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/func/AsinhFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math.func; 33 | 34 | /** 35 | The hyperbolic arc sine function. 36 | */ 37 | public class AsinhFunction implements Function { 38 | 39 | public AsinhFunction() {} 40 | 41 | /** 42 | Returns the value of ln(x + sqrt(1 + x2)), where x is the value at index 43 | location 0. 44 | */ 45 | public double of(double[] d, int numParam) { 46 | return Math.log(d[0] + Math.sqrt(1 + d[0] * d[0])); 47 | } 48 | 49 | /** 50 | Returns true only for 1 parameter, false otherwise. 51 | */ 52 | public boolean acceptNumParam(int numParam) { 53 | return numParam == 1; 54 | } 55 | 56 | public String toString() { 57 | return "asinh(x)"; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/func/AtanFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math.func; 33 | 34 | /** 35 | The arc tangent function. 36 | 37 | @see java.lang.Math#atan(double) 38 | */ 39 | public class AtanFunction implements Function { 40 | 41 | public AtanFunction() {} 42 | 43 | /** 44 | Returns the arc tangent of the value at index location 0. 45 | */ 46 | public double of(double[] d, int numParam) { 47 | return java.lang.Math.atan(d[0]); 48 | } 49 | 50 | /** 51 | Returns true only for 1 parameter, false otherwise. 52 | */ 53 | public boolean acceptNumParam(int numParam) { 54 | return numParam == 1; 55 | } 56 | 57 | public String toString() { 58 | return "atan(x)"; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/func/AtanhFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math.func; 33 | 34 | /** 35 | The hyperbolic tangent sine function. 36 | */ 37 | public class AtanhFunction implements Function { 38 | 39 | public AtanhFunction() {} 40 | 41 | /** 42 | Returns the value of (ln(1+x) - ln(1-x)) / 2, where x is the value at index location 0. 43 | */ 44 | public double of(double[] d, int numParam) { 45 | return (Math.log(1 + d[0]) - Math.log(1 - d[0])) / 2; 46 | } 47 | 48 | /** 49 | Returns true only for 1 parameter, false otherwise. 50 | */ 51 | public boolean acceptNumParam(int numParam) { 52 | return numParam == 1; 53 | } 54 | 55 | public String toString() { 56 | return "atanh(x)"; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/func/AvgFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math.func; 33 | 34 | /** 35 | The average function. 36 | */ 37 | public class AvgFunction implements Function { 38 | 39 | public AvgFunction() {} 40 | 41 | /** 42 | Returns the average of the values in the array from [0, numParam). 43 | */ 44 | public double of(double[] d, int numParam) { 45 | double sum = 0; 46 | 47 | for (int i = 0; i < numParam; i++) 48 | sum += d[i]; 49 | 50 | return sum / numParam; 51 | } 52 | 53 | /** 54 | Returns true for 1 or more parameters, false otherwise. 55 | */ 56 | public boolean acceptNumParam(int numParam) { 57 | return numParam > 0; 58 | } 59 | 60 | public String toString() { 61 | return "avg(x1, x2, ..., xn)"; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/func/CeilFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math.func; 33 | 34 | /** 35 | The ceiling function. 36 | 37 | @see java.lang.Math#ceil(double) 38 | */ 39 | public class CeilFunction implements Function { 40 | 41 | public CeilFunction() {} 42 | 43 | /** 44 | Returns the ceiling of the value at index location 0. 45 | */ 46 | public double of(double[] d, int numParam) { 47 | return java.lang.Math.ceil(d[0]); 48 | } 49 | 50 | /** 51 | Returns true only for 1 parameter, false otherwise. 52 | */ 53 | public boolean acceptNumParam(int numParam) { 54 | return numParam == 1; 55 | } 56 | 57 | public String toString() { 58 | return "ceil(x)"; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/func/CombinFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math.func; 33 | 34 | import com.graphbuilder.math.PascalsTriangle; 35 | 36 | /** 37 | The combination function. 38 | 39 | @see com.graphbuilder.math.PascalsTriangle 40 | */ 41 | public class CombinFunction implements Function { 42 | 43 | private final PascalsTriangle pascalsTriangle = new PascalsTriangle(); 44 | 45 | public CombinFunction() {} 46 | 47 | /** 48 | Returns the number of ways r items can be chosen from n items. The value of 49 | n is (int) d[0] and the value of r is (int) d[1]. 50 | */ 51 | public double of(double[] d, int numParam) { 52 | int n = (int) d[0]; 53 | int r = (int) d[1]; 54 | return pascalsTriangle.nCr(n, r); 55 | } 56 | 57 | /** 58 | Returns true only for 2 parameters, false otherwise. 59 | */ 60 | public boolean acceptNumParam(int numParam) { 61 | return numParam == 2; 62 | } 63 | 64 | public String toString() { 65 | return "combin(n, r)"; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/func/CosFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math.func; 33 | 34 | /** 35 | The cosine function. 36 | 37 | @see java.lang.Math#cos(double) 38 | */ 39 | public class CosFunction implements Function { 40 | 41 | public CosFunction() {} 42 | 43 | /** 44 | Returns the cosine of the angle value at index location 0. 45 | */ 46 | public double of(double[] d, int numParam) { 47 | return java.lang.Math.cos(d[0]); 48 | } 49 | 50 | /** 51 | Returns true only for 1 parameter, false otherwise. 52 | */ 53 | public boolean acceptNumParam(int numParam) { 54 | return numParam == 1; 55 | } 56 | 57 | public String toString() { 58 | return "cos(x)"; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/func/CoshFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math.func; 33 | 34 | /** 35 | The hyperbolic cosine function. 36 | */ 37 | public class CoshFunction implements Function { 38 | 39 | public CoshFunction() {} 40 | 41 | /** 42 | Returns the value of (ex + e-x)/2, where x is the value 43 | at index location 0 and e is the base of natural logarithms. 44 | */ 45 | public double of(double[] d, int numParam) { 46 | return (Math.pow(Math.E, d[0]) + Math.pow(Math.E, -d[0])) / 2; 47 | } 48 | 49 | /** 50 | Returns true only for 1 parameter, false otherwise. 51 | */ 52 | public boolean acceptNumParam(int numParam) { 53 | return numParam == 1; 54 | } 55 | 56 | public String toString() { 57 | return "cosh(x)"; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/func/EFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math.func; 33 | 34 | /** 35 | Euler's number, e, also called the base of natural logarithms. 36 | 37 | @see java.lang.Math#E 38 | */ 39 | public class EFunction implements Function { 40 | 41 | public EFunction() {} 42 | 43 | /** 44 | Returns the constant e regardless of the input. 45 | */ 46 | public double of(double[] d, int numParam) { 47 | return java.lang.Math.E; 48 | } 49 | 50 | /** 51 | Returns true only for 0 parameters, false otherwise. 52 | */ 53 | public boolean acceptNumParam(int numParam) { 54 | return numParam == 0; 55 | } 56 | 57 | public String toString() { 58 | return "e()"; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/func/ExpFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math.func; 33 | 34 | /** 35 | The exp function. 36 | 37 | @see java.lang.Math#exp(double) 38 | */ 39 | public class ExpFunction implements Function { 40 | 41 | public ExpFunction() {} 42 | 43 | /** 44 | Returns Euler's number, e, raised to the exponent of the value at index location 0. 45 | */ 46 | public double of(double[] d, int numParam) { 47 | return Math.exp(d[0]); 48 | } 49 | 50 | /** 51 | Returns true only for 1 parameter, false otherwise. 52 | */ 53 | public boolean acceptNumParam(int numParam) { 54 | return numParam == 1; 55 | } 56 | 57 | public String toString() { 58 | return "exp(x)"; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/func/FactFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math.func; 33 | 34 | /** 35 | The factorial function. 36 | */ 37 | public class FactFunction implements Function { 38 | 39 | public FactFunction() {} 40 | 41 | /** 42 | Takes the (int) of the value at index location 0 and computes the factorial 43 | of that number. 44 | */ 45 | public double of(double[] d, int numParam) { 46 | int n = (int) d[0]; 47 | 48 | double result = 1; 49 | 50 | for (int i = n; i > 1; i--) 51 | result *= i; 52 | 53 | return result; 54 | } 55 | 56 | /** 57 | Returns true only for 1 parameter, false otherwise. 58 | */ 59 | public boolean acceptNumParam(int numParam) { 60 | return numParam == 1; 61 | } 62 | 63 | public String toString() { 64 | return "fact(n)"; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/func/FloorFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math.func; 33 | 34 | /** 35 | The floor function. 36 | 37 | @see java.lang.Math#floor(double) 38 | */ 39 | public class FloorFunction implements Function { 40 | 41 | public FloorFunction() {} 42 | 43 | /** 44 | Returns the floor of the value at index location 0. 45 | */ 46 | public double of(double[] d, int numParam) { 47 | return java.lang.Math.floor(d[0]); 48 | } 49 | 50 | /** 51 | Returns true only for 1 parameter, false otherwise. 52 | */ 53 | public boolean acceptNumParam(int numParam) { 54 | return numParam == 1; 55 | } 56 | 57 | public String toString() { 58 | return "floor(x)"; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/func/Function.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math.func; 33 | 34 | /** 35 | The Function interface represents a function that takes a number of inputs 36 | and returns a value. The number of inputs expected depends on the function. 37 | For example, the PiFunction returns the value of Pi regardless of the input. 38 | The CosFunction uses the value at index location 0. The PowFunction uses the 39 | values at index locations 0 and 1. See the FuncMap class for more information. 40 | 41 | @see com.graphbuilder.math.FuncMap 42 | */ 43 | public interface Function { 44 | 45 | /** 46 | Takes the specified double array as input and returns a double value. Functions 47 | that accept a variable number of inputs can take numParam to be the number of inputs. 48 | */ 49 | public double of(double[] param, int numParam); 50 | 51 | /** 52 | Returns true if the numParam is an accurate representation of the number of inputs 53 | the function processes. 54 | */ 55 | public boolean acceptNumParam(int numParam); 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/func/LgFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math.func; 33 | 34 | /** 35 | The log base 2 function. 36 | */ 37 | public class LgFunction implements Function { 38 | 39 | public LgFunction() {} 40 | 41 | /** 42 | Returns the log base 2 of the value at index location 0. 43 | */ 44 | public double of(double[] d, int numParam) { 45 | return java.lang.Math.log(d[0]) / java.lang.Math.log(2); 46 | } 47 | 48 | /** 49 | Returns true only for 1 parameter, false otherwise. 50 | */ 51 | public boolean acceptNumParam(int numParam) { 52 | return numParam == 1; 53 | } 54 | 55 | public String toString() { 56 | return "lg(x)"; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/func/LnFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math.func; 33 | 34 | /** 35 | The natural logarithm function. 36 | 37 | @see java.lang.Math#log(double) 38 | */ 39 | public class LnFunction implements Function { 40 | 41 | public LnFunction() {} 42 | 43 | /** 44 | Returns the natural logarithm of the value at index location 0. 45 | */ 46 | public double of(double[] d, int numParam) { 47 | return java.lang.Math.log(d[0]); 48 | } 49 | 50 | /** 51 | Returns true only for 1 parameter, false otherwise. 52 | */ 53 | public boolean acceptNumParam(int numParam) { 54 | return numParam == 1; 55 | } 56 | 57 | public String toString() { 58 | return "ln(x)"; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/func/LogFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math.func; 33 | 34 | /** 35 | The log function. 36 | */ 37 | public class LogFunction implements Function { 38 | 39 | public LogFunction() {} 40 | 41 | /** 42 | If the number of parameters specified is 1, then the log base 10 is taken of the 43 | value at index location 0. If the number of parameters specified is 2, then the 44 | base value is at index location 1. 45 | */ 46 | public double of(double[] d, int numParam) { 47 | if (numParam == 1) { 48 | return java.lang.Math.log(d[0]) / java.lang.Math.log(10); 49 | } 50 | return java.lang.Math.log(d[0]) / java.lang.Math.log(d[1]); 51 | } 52 | 53 | /** 54 | Returns true only for 1 or 2 parameters, false otherwise. 55 | */ 56 | public boolean acceptNumParam(int numParam) { 57 | return numParam == 1 || numParam == 2; 58 | } 59 | 60 | public String toString() { 61 | return "log(x):log(x, y)"; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/func/MaxFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math.func; 33 | 34 | /** 35 | The max function. 36 | */ 37 | public class MaxFunction implements Function { 38 | 39 | public MaxFunction() {} 40 | 41 | /** 42 | Returns the maximum value of the specified inputs. Double.MAX_VALUE is returned for 0 parameters. 43 | */ 44 | public double of(double[] d, int numParam) { 45 | if (numParam == 0) 46 | return Double.MAX_VALUE; 47 | 48 | double max = -Double.MAX_VALUE; 49 | for (int i = 0; i < numParam; i++) 50 | if (d[i] > max) 51 | max = d[i]; 52 | return max; 53 | } 54 | 55 | /** 56 | Returns true for 0 or more parameters, false otherwise. 57 | */ 58 | public boolean acceptNumParam(int numParam) { 59 | return numParam >= 0; 60 | } 61 | 62 | public String toString() { 63 | return "max(x1, x2, ..., xn)"; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/func/MinFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math.func; 33 | 34 | /** 35 | The min function. 36 | */ 37 | public class MinFunction implements Function { 38 | 39 | public MinFunction() {} 40 | 41 | /** 42 | Returns the minimum value of the specified inputs. Double.MIN_VALUE is returned for 0 parameters. 43 | */ 44 | public double of(double[] d, int numParam) { 45 | if (numParam == 0) 46 | return Double.MIN_VALUE; 47 | 48 | double min = Double.MAX_VALUE; 49 | 50 | for (int i = 0; i < numParam; i++) 51 | if (d[i] < min) 52 | min = d[i]; 53 | return min; 54 | } 55 | 56 | /** 57 | Returns true for 0 or more parameters, false otherwise. 58 | */ 59 | public boolean acceptNumParam(int numParam) { 60 | return numParam >= 0; 61 | } 62 | 63 | public String toString() { 64 | return "min(x1, x2, ..., xn)"; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/func/ModFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math.func; 33 | 34 | /** 35 | The mod function. 36 | */ 37 | public class ModFunction implements Function { 38 | 39 | public ModFunction() {} 40 | 41 | /** 42 | Returns the value of x % y, where x = d[0] and y = d[1]. More precisely, the value returned is 43 | x minus the value of x / y, where x / y is rounded to the closest integer value towards 0. 44 | */ 45 | public double of(double[] d, int numParam) { 46 | return d[0] % d[1]; 47 | } 48 | 49 | /** 50 | Returns true only for 2 parameters, false otherwise. 51 | */ 52 | public boolean acceptNumParam(int numParam) { 53 | return numParam == 2; 54 | } 55 | 56 | public String toString() { 57 | return "mod(x, y)"; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/func/PiFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math.func; 33 | 34 | /** 35 | The constant Pi. 36 | 37 | @see java.lang.Math#PI 38 | */ 39 | public class PiFunction implements Function { 40 | 41 | public PiFunction() {} 42 | 43 | /** 44 | Returns the constant Pi regardless of the input. 45 | */ 46 | public double of(double[] d, int numParam) { 47 | return java.lang.Math.PI; 48 | } 49 | 50 | /** 51 | Returns true only for 0 parameters, false otherwise. 52 | */ 53 | public boolean acceptNumParam(int numParam) { 54 | return numParam == 0; 55 | } 56 | 57 | public String toString() { 58 | return "pi()"; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/func/PowFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math.func; 33 | 34 | /** 35 | The power function. 36 | 37 | @see java.lang.Math#pow(double,double) 38 | */ 39 | public class PowFunction implements Function { 40 | 41 | public PowFunction() {} 42 | 43 | /** 44 | Returns the value at index location 0 to the exponent of the value 45 | at index location 1. 46 | */ 47 | public double of(double[] d, int numParam) { 48 | return java.lang.Math.pow(d[0], d[1]); 49 | } 50 | 51 | /** 52 | Returns true only for 2 parameters, false otherwise. 53 | */ 54 | public boolean acceptNumParam(int numParam) { 55 | return numParam == 2; 56 | } 57 | 58 | public String toString() { 59 | return "pow(x, y)"; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/func/RandFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math.func; 33 | 34 | /** 35 | The random function. 36 | 37 | @see java.lang.Math#random() 38 | */ 39 | public class RandFunction implements Function { 40 | 41 | public RandFunction() {} 42 | 43 | /** 44 | Returns a random value in the range [0, 1) that does not depend on the input. 45 | */ 46 | public double of(double[] d, int numParam) { 47 | return java.lang.Math.random(); 48 | } 49 | 50 | /** 51 | Returns true only for 0 parameters, false otherwise. 52 | */ 53 | public boolean acceptNumParam(int numParam) { 54 | return numParam == 0; 55 | } 56 | 57 | public String toString() { 58 | return "rand()"; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/func/RoundFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math.func; 33 | 34 | /** 35 | The round function. 36 | 37 | @see java.lang.Math#round(double) 38 | */ 39 | public class RoundFunction implements Function { 40 | 41 | public RoundFunction() {} 42 | 43 | /** 44 | Returns the value at d[0] rounded to the nearest integer value. 45 | If the value exceeds the capabilities of long precision then 46 | the value itself is returned. 47 | */ 48 | public double of(double[] d, int numParam) { 49 | if (d[0] >= Long.MAX_VALUE || d[0] <= Long.MIN_VALUE) 50 | return d[0]; 51 | 52 | return Math.round(d[0]); 53 | } 54 | 55 | /** 56 | Returns true only for 1 parameter, false otherwise. 57 | */ 58 | public boolean acceptNumParam(int numParam) { 59 | return numParam == 1; 60 | } 61 | 62 | public String toString() { 63 | return "round(x)"; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/func/SignFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math.func; 33 | 34 | /** 35 | The sign function. 36 | */ 37 | public class SignFunction implements Function { 38 | 39 | public SignFunction() {} 40 | 41 | /** 42 | The sign function returns 1 if the d[0] > 0, -1 if d[0] < 0, else 0. 43 | */ 44 | public double of(double[] d, int numParam) { 45 | if (d[0] > 0) return 1; 46 | if (d[0] < 0) return -1; 47 | return 0; 48 | } 49 | 50 | /** 51 | Returns true only for 1 parameter, false otherwise. 52 | */ 53 | public boolean acceptNumParam(int numParam) { 54 | return numParam == 1; 55 | } 56 | 57 | public String toString() { 58 | return "sign(x)"; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/func/SinFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math.func; 33 | 34 | /** 35 | The sine function. 36 | 37 | @see java.lang.Math#sin(double) 38 | */ 39 | public class SinFunction implements Function { 40 | 41 | public SinFunction() {} 42 | 43 | /** 44 | Returns the sine of the angle value at index location 0. 45 | */ 46 | public double of(double[] d, int numParam) { 47 | return java.lang.Math.sin(d[0]); 48 | } 49 | 50 | /** 51 | Returns true only for 1 parameter, false otherwise. 52 | */ 53 | public boolean acceptNumParam(int numParam) { 54 | return numParam == 1; 55 | } 56 | 57 | public String toString() { 58 | return "sin(x)"; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/func/SinhFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math.func; 33 | 34 | /** 35 | The hyperbolic sine function. 36 | */ 37 | public class SinhFunction implements Function { 38 | 39 | public SinhFunction() {} 40 | 41 | /** 42 | Returns the value of (ex - e-x)/2, 43 | where x is the value at index location 0 and e is the base of natural logarithms. 44 | */ 45 | public double of(double[] d, int numParam) { 46 | return (Math.pow(Math.E, d[0]) - Math.pow(Math.E, -d[0])) / 2; 47 | } 48 | 49 | /** 50 | Returns true only for 1 parameter, false otherwise. 51 | */ 52 | public boolean acceptNumParam(int numParam) { 53 | return numParam == 1; 54 | } 55 | 56 | public String toString() { 57 | return "sinh(x)"; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/func/SqrtFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math.func; 33 | 34 | /** 35 | The square root function. 36 | 37 | @see java.lang.Math#sqrt(double) 38 | */ 39 | public class SqrtFunction implements Function { 40 | 41 | public SqrtFunction() {} 42 | 43 | /** 44 | Returns the square root of the value at index location 0. 45 | */ 46 | public double of(double[] d, int numParam) { 47 | return java.lang.Math.sqrt(d[0]); 48 | } 49 | 50 | /** 51 | Returns true only for 1 parameter, false otherwise. 52 | */ 53 | public boolean acceptNumParam(int numParam) { 54 | return numParam == 1; 55 | } 56 | 57 | public String toString() { 58 | return "sqrt(x)"; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/func/SumFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math.func; 33 | 34 | /** 35 | The sum function. 36 | */ 37 | public class SumFunction implements Function { 38 | 39 | public SumFunction() {} 40 | 41 | /** 42 | Returns the sum of the values in the array from [0, numParam). 43 | */ 44 | public double of(double[] d, int numParam) { 45 | double sum = 0; 46 | 47 | for (int i = 0; i < numParam; i++) 48 | sum += d[i]; 49 | 50 | return sum; 51 | } 52 | 53 | /** 54 | Returns true for 1 or more parameters, false otherwise. 55 | */ 56 | public boolean acceptNumParam(int numParam) { 57 | return numParam > 0; 58 | } 59 | 60 | public String toString() { 61 | return "sum(x1, x2, ..., xn)"; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/func/TanFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math.func; 33 | 34 | /** 35 | The tangent function. 36 | 37 | @see java.lang.Math#tan(double) 38 | */ 39 | public class TanFunction implements Function { 40 | 41 | public TanFunction() {} 42 | 43 | /** 44 | Returns the tangent of the angle value at index location 0. 45 | */ 46 | public double of(double[] d, int numParam) { 47 | return java.lang.Math.tan(d[0]); 48 | } 49 | 50 | /** 51 | Returns true only for 1 parameter, false otherwise. 52 | */ 53 | public boolean acceptNumParam(int numParam) { 54 | return numParam == 1; 55 | } 56 | 57 | public String toString() { 58 | return "tan(x)"; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/math/func/TanhFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.math.func; 33 | 34 | /** 35 | The hyperbolic tangent function. 36 | */ 37 | public class TanhFunction implements Function { 38 | 39 | public TanhFunction() {} 40 | 41 | /** 42 | Returns the value of (ex - e-x)/(ex + e-x), 43 | where x is the value at index location 0 and e is the base of natural logarithms. 44 | */ 45 | public double of(double[] d, int numParam) { 46 | double e = Math.pow(Math.E, 2 * d[0]); 47 | return (e - 1) / (e + 1); 48 | } 49 | 50 | /** 51 | Returns true only for 1 parameter, false otherwise. 52 | */ 53 | public boolean acceptNumParam(int numParam) { 54 | return numParam == 1; 55 | } 56 | 57 | public String toString() { 58 | return "tanh(x)"; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/struc/Bag.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.struc; 33 | 34 | /** 35 | Bag is a container of objects using an array. The Bag is designed to be as light weight as possible. 36 | It only contains a reference to an array and a size counter. In methods that accept both an Object and 37 | an int as parameters, the Object is always specified first. 38 | */ 39 | public class Bag { 40 | 41 | protected Object[] data = null; 42 | protected int size = 0; 43 | 44 | public Bag() { 45 | data = new Object[2]; 46 | } 47 | 48 | public Bag(int initialCapacity) { 49 | data = new Object[initialCapacity]; 50 | } 51 | 52 | public Bag(Object[] data, int size) { 53 | if (data == null) 54 | throw new IllegalArgumentException("data array cannot be null."); 55 | 56 | if (size < 0 || size > data.length) 57 | throw new IllegalArgumentException("required: (size >= 0 && size <= data.length) but: (size = " + size + ", data.length = " + data.length + ")"); 58 | 59 | this.data = data; 60 | this.size = size; 61 | } 62 | 63 | public void add(Object o) { 64 | insert(o, size); 65 | } 66 | 67 | public int size() { 68 | return size; 69 | } 70 | 71 | public void setSize(int s) { 72 | if (s < 0 || s > data.length) 73 | throw new IllegalArgumentException("required: (size >= 0 && size <= data.length) but: (size = " + size + ", data.length = " + data.length + ")"); 74 | 75 | size = s; 76 | } 77 | 78 | public void insert(Object o, int index) { 79 | if (index < 0 || index > size) 80 | throw new IllegalArgumentException("required: (index >= 0 && index <= size) but: (index = " + index + ", size = " + size + ")"); 81 | 82 | ensureCapacity(size + 1); 83 | 84 | for (int i = size; i > index; i--) 85 | data[i] = data[i - 1]; 86 | 87 | data[index] = o; 88 | size++; 89 | } 90 | 91 | public void ensureCapacity(int capacity) { 92 | if (capacity > data.length) { 93 | int x = 2 * data.length; 94 | 95 | if (x < capacity) 96 | x = capacity; 97 | 98 | Object[] arr = new Object[x]; 99 | 100 | for (int i = 0; i < size; i++) 101 | arr[i] = data[i]; 102 | 103 | data = arr; 104 | } 105 | } 106 | 107 | public int getCapacity() { 108 | return data.length; 109 | } 110 | 111 | private int find(Object o, int i, boolean forward) { 112 | if (i < 0 || i >= size) return -1; 113 | 114 | if (forward) { 115 | if (o == null) { 116 | for (; i < size; i++) 117 | if (data[i] == null) 118 | return i; 119 | } 120 | else { 121 | for (; i < size; i++) 122 | if (o.equals(data[i])) 123 | return i; 124 | } 125 | } 126 | else { 127 | if (o == null) { 128 | for (; i >= 0; i--) 129 | if (data[i] == null) 130 | return i; 131 | } 132 | else { 133 | for (; i >= 0; i--) 134 | if (o.equals(data[i])) 135 | return i; 136 | } 137 | } 138 | return -1; 139 | } 140 | 141 | public int remove(Object o) { 142 | int i = find(o, 0, true); 143 | if (i >= 0) 144 | remove(i); 145 | return i; 146 | } 147 | 148 | public Object remove(int index) { 149 | if (index < 0 || index >= size) 150 | throw new IllegalArgumentException("required: (index >= 0 && index < size) but: (index = " + index + ", size = " + size + ")"); 151 | 152 | Object o = data[index]; 153 | 154 | for (int i = index + 1; i < size; i++) 155 | data[i-1] = data[i]; 156 | 157 | data[--size] = null; 158 | return o; 159 | } 160 | 161 | public Object get(int index) { 162 | if (index < 0 || index >= size) 163 | throw new IllegalArgumentException("required: (index >= 0 && index < size) but: (index = " + index + ", size = " + size + ")"); 164 | 165 | return data[index]; 166 | } 167 | 168 | public Object set(Object o, int index) { 169 | if (index < 0 || index >= size) 170 | throw new IllegalArgumentException("required: (index >= 0 && index < size) but: (index = " + index + ", size = " + size + ")"); 171 | 172 | Object old = data[index]; 173 | data[index] = o; 174 | return old; 175 | } 176 | 177 | public boolean contains(Object o) { 178 | return find(o, 0, true) >= 0; 179 | } 180 | 181 | public boolean isEmpty() { 182 | return size == 0; 183 | } 184 | 185 | public void trimArray() { 186 | if (size < data.length) { 187 | Object[] arr = new Object[size]; 188 | for (int i = 0; i < size; i++) 189 | arr[i] = data[i]; 190 | data = arr; 191 | } 192 | } 193 | 194 | public int indexOf(Object o) { 195 | return find(o, 0, true); 196 | } 197 | 198 | public int indexOf(Object o, int startIndex) { 199 | return find(o, startIndex, true); 200 | } 201 | 202 | public int lastIndexOf(Object o) { 203 | return find(o, size - 1, false); 204 | } 205 | 206 | public int lastIndexOf(Object o, int startIndex) { 207 | return find(o, startIndex, false); 208 | } 209 | } 210 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/struc/LinkedList.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.struc; 33 | 34 | public class LinkedList { 35 | 36 | protected Node head = null; 37 | protected Node tail = null; 38 | protected int size = 0; 39 | 40 | public LinkedList() {} 41 | 42 | public static class Node { 43 | protected LinkedList list = null; 44 | protected Node next = null; 45 | protected Node prev = null; 46 | protected Object userObject = null; 47 | 48 | protected Node(LinkedList list, Object userObject) { 49 | this.list = list; 50 | this.userObject = userObject; 51 | } 52 | 53 | public LinkedList list() { 54 | return list; 55 | } 56 | 57 | public Node next() { 58 | return next; 59 | } 60 | 61 | public Node prev() { 62 | return prev; 63 | } 64 | 65 | public Object getUserObject() { 66 | return userObject; 67 | } 68 | 69 | public void setUserObject(Object userObject) { 70 | this.userObject = userObject; 71 | } 72 | 73 | public void insertBefore(Object o) { 74 | list.insertBefore(this, o); 75 | } 76 | 77 | public void insertAfter(Object o) { 78 | list.insertAfter(this, o); 79 | } 80 | 81 | public void remove() { 82 | list.removeNode(this); 83 | } 84 | } 85 | 86 | protected Node createNode(Object o) { 87 | return new Node(this, o); 88 | } 89 | 90 | protected void insertBefore(Node n, Object o) { 91 | Node p = createNode(o); 92 | 93 | if (size == 0) { 94 | head = p; 95 | tail = p; 96 | } 97 | else { 98 | if (n == head) { 99 | p.next = head; 100 | head.prev = p; 101 | head = p; 102 | } 103 | else { 104 | n.prev.next = p; 105 | p.prev = n.prev; 106 | n.prev = p; 107 | p.next = n; 108 | } 109 | } 110 | 111 | size++; 112 | } 113 | 114 | protected void insertAfter(Node n, Object o) { 115 | Node p = createNode(o); 116 | 117 | if (size == 0) { 118 | head = p; 119 | tail = p; 120 | } 121 | else { 122 | if (n == tail) { 123 | p.prev = tail; 124 | tail.next = p; 125 | tail = p; 126 | } 127 | else { 128 | n.next.prev = p; 129 | p.next = n.next; 130 | n.next = p; 131 | p.prev = n; 132 | } 133 | } 134 | 135 | size++; 136 | } 137 | 138 | protected Object removeNode(Node n) { 139 | if (size == 0) 140 | return null; 141 | 142 | Object o = n.userObject; 143 | 144 | if (n == head) { 145 | head = head.next; 146 | 147 | if (head == null) 148 | tail = null; 149 | else 150 | head.prev = null; 151 | } 152 | else if (n == tail) { 153 | tail = tail.prev; 154 | tail.next = null; 155 | } 156 | else { 157 | n.prev.next = n.next; 158 | n.next.prev = n.prev; 159 | } 160 | 161 | n.list = null; 162 | size--; 163 | return o; 164 | } 165 | 166 | public Node getHead() { 167 | return head; 168 | } 169 | 170 | public Node getTail() { 171 | return tail; 172 | } 173 | 174 | public void addToHead(Object o) { 175 | insertBefore(head, o); 176 | } 177 | 178 | public void addToTail(Object o) { 179 | insertAfter(tail, o); 180 | } 181 | 182 | public Object removeHead() { 183 | return removeNode(head); 184 | } 185 | 186 | public Object removeTail() { 187 | return removeNode(tail); 188 | } 189 | 190 | public int size() { 191 | return size; 192 | } 193 | 194 | public boolean isEmpty() { 195 | return size == 0; 196 | } 197 | 198 | public String toString() { 199 | StringBuffer sb = new StringBuffer(6 * size); 200 | sb.append("["); 201 | Node n = head; 202 | 203 | if (n != null) { 204 | sb.append(n.userObject); 205 | n = n.next; 206 | } 207 | 208 | while (n != null) { 209 | sb.append(", "); 210 | sb.append(n.userObject); 211 | n = n.next; 212 | } 213 | 214 | return sb.append("]").toString(); 215 | } 216 | } 217 | -------------------------------------------------------------------------------- /src/main/java/com/graphbuilder/struc/Stack.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2005, Graph Builder 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of Graph Builder nor the names of its contributors may be 17 | * used to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package com.graphbuilder.struc; 33 | 34 | public class Stack extends LinkedList { 35 | 36 | public Stack() {} 37 | 38 | public Object peek() { 39 | if (head == null) 40 | return null; 41 | 42 | return head.userObject; 43 | } 44 | 45 | public Object pop() { 46 | return removeHead(); 47 | } 48 | 49 | public void push(Object o) { 50 | addToHead(o); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/test/java/com/graphbuilder/curve/TestShapeMultiPath.java: -------------------------------------------------------------------------------- 1 | package com.graphbuilder.curve; 2 | 3 | import org.junit.*; 4 | 5 | import com.graphbuilder.geom.PointFactory; 6 | 7 | import static org.junit.Assert.*; 8 | 9 | 10 | /** 11 | * TODO: Needs more tests, these only verify that switching from sun.awt.geom to 12 | * the harmony implementation won't break things too badly... 13 | */ 14 | public class TestShapeMultiPath { 15 | 16 | @Test 17 | public void testContains() throws Exception { 18 | 19 | // TODO: test other types of shapes 20 | ControlPath cp = new ControlPath(); 21 | cp.addPoint(PointFactory.create(10, 10)); 22 | cp.addPoint(PointFactory.create(10, 200)); 23 | cp.addPoint(PointFactory.create(290, 200)); 24 | cp.addPoint(PointFactory.create(290, 10)); 25 | Curve c = new BezierCurve(cp, new GroupIterator("0:n-1", cp.numPoints())); 26 | 27 | ShapeMultiPath smp = new ShapeMultiPath(); 28 | c.appendTo(smp); 29 | 30 | // TODO: more nuanced points.. 31 | assertFalse(smp.contains(0, 0)); 32 | assertFalse(smp.contains(9, 9)); 33 | assertFalse(smp.contains(30, 180)); 34 | assertFalse(smp.contains(280, 180)); 35 | 36 | assertTrue(smp.contains(11, 11)); 37 | assertTrue(smp.contains(100, 100)); 38 | assertTrue(smp.contains(289, 11)); 39 | } 40 | } 41 | --------------------------------------------------------------------------------