|
| 1 | +package com.github.mittyrobotics.pathfollowing; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | + |
| 5 | +public class QuinticHermiteSplineGroup extends Parametric { |
| 6 | + |
| 7 | + //list of splines |
| 8 | + ArrayList<QuinticHermiteSpline> splines = new ArrayList<>(); |
| 9 | + |
| 10 | + /** |
| 11 | + * Creates a new spline group with an initial {@link QuinticHermiteSpline} |
| 12 | + * @param initialSpline {@link QuinticHermiteSpline} to initialize with |
| 13 | + */ |
| 14 | + public QuinticHermiteSplineGroup(QuinticHermiteSpline initialSpline) { |
| 15 | + length = initialSpline.getLength(); |
| 16 | + splines.add(initialSpline); |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * Creates a new empty spline group |
| 21 | + */ |
| 22 | + public QuinticHermiteSplineGroup() { |
| 23 | + length = 0; |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Adds a {@link QuinticHermiteSpline} to the list of splines |
| 28 | + * @param spline {@link QuinticHermiteSpline} to add |
| 29 | + */ |
| 30 | + public void addSpline(QuinticHermiteSpline spline) { |
| 31 | + length += spline.getLength(); |
| 32 | + splines.add(spline); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Adds a {@link QuinticHermiteSpline} to the list of splines at the given index |
| 37 | + * @param index index to add the spline at |
| 38 | + * @param spline {@link QuinticHermiteSpline} to add |
| 39 | + */ |
| 40 | + public void addSpline(int index, QuinticHermiteSpline spline) { |
| 41 | + length += spline.getLength(); |
| 42 | + splines.add(index, spline); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Get the {@link QuinticHermiteSpline} at the index of the list |
| 47 | + * @param index index to get spline at |
| 48 | + * @return {@link QuinticHermiteSpline} at the index of the list |
| 49 | + */ |
| 50 | + public QuinticHermiteSpline getSpline(int index) { |
| 51 | + return splines.get(index); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Removes a {@link QuinticHermiteSpline} from the list |
| 56 | + * @param spline {@link QuinticHermiteSpline} to remove |
| 57 | + */ |
| 58 | + public void removeSpline(QuinticHermiteSpline spline) { |
| 59 | + if(splines.contains(spline)) { |
| 60 | + length -= spline.getLength(); |
| 61 | + splines.remove(spline); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Removes the {@link QuinticHermiteSpline} at the given index |
| 67 | + * @param index index to remove the {@link QuinticHermiteSpline} |
| 68 | + */ |
| 69 | + public void removeSpline(int index) { |
| 70 | + length -= splines.get(index).getLength(); |
| 71 | + splines.remove(index); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Gets the list of spline |
| 76 | + * @return an {@link ArrayList} of {@link QuinticHermiteSpline}s |
| 77 | + */ |
| 78 | + public ArrayList<QuinticHermiteSpline> getSplines() { |
| 79 | + return splines; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Returns the index of {@link QuinticHermiteSpline} associated with the given t |
| 84 | + * @param t t value to get spline of |
| 85 | + * @return the index of {@link QuinticHermiteSpline} associated with the given t |
| 86 | + */ |
| 87 | + public int getSplineFromT(double t) { |
| 88 | + if(t < 0) return 0; |
| 89 | + if(t >= 1) return splines.size() - 1; |
| 90 | + return (int) (t * splines.size()); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Returns t for the spline given the overall t and the index of the spline |
| 95 | + * @param t overall t parameter |
| 96 | + * @param splineIndex index of the spline in array |
| 97 | + * @return t for the spline given the overall t and the index of the spline |
| 98 | + */ |
| 99 | + public double getSplineTFromT(double t, int splineIndex) { |
| 100 | + if(t < 0) return t * splines.size(); |
| 101 | + if(t >= 1) return 1 + (t-1) * splines.size(); |
| 102 | + return (t - (double) splineIndex / splines.size()) * splines.size(); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Returns the {@link Point2D} at t |
| 107 | + * @param t t to get associated {@link Point2D} |
| 108 | + * @return {@link Point2D} at t |
| 109 | + */ |
| 110 | + @Override |
| 111 | + public Point2D getPoint(double t) { |
| 112 | + int index = getSplineFromT(t); |
| 113 | + return splines.get(index).getPoint(getSplineTFromT(t, index)); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Returns the {@link Angle} at t |
| 118 | + * @param t t to get associated {@link Angle} |
| 119 | + * @return {@link Angle} at t |
| 120 | + */ |
| 121 | + @Override |
| 122 | + public Angle getAngle(double t) { |
| 123 | + int index = getSplineFromT(t); |
| 124 | + return splines.get(index).getAngle(getSplineTFromT(t, index)); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Returns the {@link Pose2D} at t |
| 129 | + * @param t t to get associated {@link Pose2D} |
| 130 | + * @return {@link Pose2D} at t |
| 131 | + */ |
| 132 | + @Override |
| 133 | + public Pose2D getPose(double t) { |
| 134 | + int index = getSplineFromT(t); |
| 135 | + return splines.get(index).getPose(getSplineTFromT(t, index)); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Returns the nth derivative as a {@link Point2D} at t |
| 140 | + * @param t t to get associated nth derivative |
| 141 | + * @param n degree of the derivative |
| 142 | + * @return the nth derivative as a {@link Point2D} at t |
| 143 | + */ |
| 144 | + @Override |
| 145 | + public Point2D getDerivative(double t, int n) { |
| 146 | + int index = getSplineFromT(t); |
| 147 | + return splines.get(index).getDerivative(getSplineTFromT(t, index), n); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public double findClosestPointOnSpline(Point2D point, int steps, int iterations) { |
| 152 | + Vector2D cur_min = new Vector2D(Double.POSITIVE_INFINITY, 0); |
| 153 | + double i = 0; |
| 154 | + for(QuinticHermiteSpline spline : splines) { |
| 155 | + double t = spline.findClosestPointOnSpline(point, steps, iterations); |
| 156 | + if(spline.getPoint(t).distance(point) < cur_min.getX()) { |
| 157 | + cur_min = new Vector2D(spline.getPoint(t).distance(point), i + t / splines.size()); |
| 158 | + } |
| 159 | + i += 1. / splines.size(); |
| 160 | + } |
| 161 | + return cur_min.getY(); |
| 162 | + |
| 163 | + } |
| 164 | + |
| 165 | +// /** |
| 166 | +// * Returns the Gaussian quadrature length of the parametric from a start to end t parameter |
| 167 | +// * @param start t parameter to start length calculation |
| 168 | +// * @param end t parameter to end length calculation |
| 169 | +// * @param steps number of steps (degree) of quadrature |
| 170 | +// * @return the Gaussian quadrature length of the parametric from a start to end t parameter |
| 171 | +// */ |
| 172 | +// public double getGaussianQuadratureLength(double start, double end, int steps) { |
| 173 | +// |
| 174 | +// } |
| 175 | + |
| 176 | + @Override |
| 177 | + public double getTFromLength(double length) { |
| 178 | + double t = 0; |
| 179 | + double l = 0; |
| 180 | + for(QuinticHermiteSpline spline : splines) { |
| 181 | + if(spline.getLength() + l <= length) { |
| 182 | + l += spline.getLength(); |
| 183 | + t += 1. / splines.size(); |
| 184 | + } else { |
| 185 | + t += spline.getTFromLength(length - l) / splines.size(); |
| 186 | + break; |
| 187 | + } |
| 188 | + } |
| 189 | + return t; |
| 190 | + } |
| 191 | + |
| 192 | + public int getIndexOfSplineFromPoint(Point2D point, int newtonsSteps) { |
| 193 | + return getSplineFromT(findClosestPointOnSpline(point, newtonsSteps, 5)); |
| 194 | + } |
| 195 | + |
| 196 | + public double getIndexOfSplineFromPointI(Point2D point, int newtonsSteps) { |
| 197 | + return findClosestPointOnSpline(point, newtonsSteps, 5); |
| 198 | + } |
| 199 | +} |
0 commit comments