Documentation ¶
Overview ¶
Package util contains functions for linear and non-linear interpolations and their inversions.
Index ¶
- Constants
- Variables
- func AngleBetweenLines(p1, p2, p3, p4 []float64) float64
- func AngleInRange(a, r, b float64) bool
- func BBContains(p []float64, bb [][]float64) bool
- func BBFilter(pts [][]float64, bb [][]float64) [][]float64
- func BBIntersection(bb1, bb2 [][]float64) [][]float64
- func BBOutline(bb [][]float64) [][]float64
- func BBOverlap(bb1, bb2 [][]float64) bool
- func Barycentric(p, tp1, tp2, tp3 []float64) (float64, float64, float64)
- func Bezier1(pts [][]float64, t float64) []float64
- func Bezier2(pts [][]float64, t float64) []float64
- func Bezier3(pts [][]float64, t float64) []float64
- func Bezier3ToCatmull(p1, p2, p3, p4 []float64) []float64
- func BoundingBox(pts ...[]float64) [][]float64
- func CalcDerivativeWeights(w [][]float64) [][]float64
- func CalcExtremities(points [][]float64) []float64
- func CalcNextOrderWeights(w [][]float64) [][]float64
- func CalcPointsForArc(theta float64) [][]float64
- func CatmullToBezier3(tau float64, p1, p2, p3, p4 []float64) []float64
- func Centroid(pts ...[]float64) []float64
- func Circumcircle(p1, p2, p3 []float64) []float64
- func Coincident(lp1, lp2, lp3, lp4 []float64) bool
- func Collinear(lp1, lp2, p []float64) bool
- func CrossProduct(p1, p2, p3 []float64) float64
- func Cubic(t float64, p []float64) float64
- func DeCasteljau(pts [][]float64, t float64) []float64
- func Dejitter(closed bool, pts ...[]float64) [][]float64
- func DistanceE(p1, p2 []float64) float64
- func DistanceESquared(p1, p2 []float64) float64
- func DistanceESquaredN(p1, p2 []float64) float64
- func DistanceToLineSquared(lp1, lp2, p []float64) (float64, []float64, float64)
- func DotProduct(p1, p2, p3, p4 []float64) float64
- func DotProductAngle(p1, p2, p3, p4 []float64) float64
- func Equals(d1, d2 float64) bool
- func Equals32(d1, d2 float32) bool
- func EqualsP(v1, v2 []float64) bool
- func IntersectionTVals(x1, y1, x2, y2, x3, y3, x4, y4 float64) ([]float64, error)
- func IntersectionTValsP(p1, p2, p3, p4 []float64) ([]float64, error)
- func InvLerp(v, start, end float64) float64
- func InvLerp32(v, start, end float32) float32
- func InvLerpClamp(v, start, end float64) float64
- func InvLerpClamp32(v, start, end float32) float32
- func InvNLerp(v, start, end float64, f NonLinear) float64
- func InvNLerp32(v, start, end float32, f NonLinear) float32
- func InverseBarycentric(w1, w2, w3 float64, tp1, tp2, tp3 []float64) []float64
- func Kappa(dpt, d2pt []float64) float64
- func Kappa1(dw, d2w [][]float64, t float64) float64
- func KappaC(p1, p2, p3 []float64) float64
- func KappaM(p1, p2, p3 []float64) float64
- func Left(lp1, lp2, p []float64) bool
- func LeftOn(lp1, lp2, p []float64) bool
- func Lerp(t, start, end float64) float64
- func Lerp32(t, start, end float32) float32
- func LerpClamp(t, start, end float64) float64
- func LerpClamp32(t, start, end float32) float32
- func LineAngle(p1, p2 []float64) float64
- func LineNormalToPoint(lp1, lp2, p []float64) ([]float64, float64, float64)
- func MinD(pts ...[]float64) int
- func NLerp(t, start, end float64, f NonLinear) float64
- func NLerp32(t, start, end float32, f NonLinear) float32
- func NRM(start float64, f, df func(float64) float64) (float64, error)
- func Parallel(lp1, lp2, lp3, lp4 []float64) bool
- func PointInPoly(pt []float64, poly ...[]float64) bool
- func PointInTriangle(p, tp1, tp2, tp3 []float64) bool
- func PointOnLine(lp1, lp2, p []float64) (bool, float64)
- func RectToBB(rect image.Rectangle) [][]float64
- func Remap(v, istart, iend, ostart, oend float64) float64
- func Remap32(v, istart, iend, ostart, oend float32) float32
- func RemapClamp(v, istart, iend, ostart, oend float64) float64
- func RemapClamp32(v, istart, iend, ostart, oend float32) float32
- func RemapNL(v, istart, iend, ostart, oend float64, fi, fo NonLinear) float64
- func RemapNL32(v, istart, iend, ostart, oend float32, fi, fo NonLinear) float32
- func Right(lp1, lp2, p []float64) bool
- func RightOn(lp1, lp2, p []float64) bool
- func SplitCurve(pts [][]float64, t float64) [][][]float64
- func ToF32(pts ...float64) []float32
- func ToF64(pts ...float32) []float64
- func TriArea(p1, p2, p3 []float64) float64
- func Vec(p1, p2 []float64) []float64
- func VecMag(v []float64) float64
- func VecNormalize(v []float64) []float64
- func Within(d1, d2, e float64) bool
- type BezierCurve
- func (bc *BezierCurve) CurveDt2X(t float64) float64
- func (bc *BezierCurve) CurveDt2Y(t float64) float64
- func (bc *BezierCurve) CurveDt3X(t float64) float64
- func (bc *BezierCurve) CurveDt3Y(t float64) float64
- func (bc *BezierCurve) CurveDtX(t float64) float64
- func (bc *BezierCurve) CurveDtY(t float64) float64
- func (bc *BezierCurve) CurveX(t float64) float64
- func (bc *BezierCurve) CurveY(t float64) float64
- func (bc *BezierCurve) Kappa(t float64) float64
- type NLCatenary
- type NLCircle1
- type NLCircle2
- type NLCompound
- type NLCube
- type NLExponential
- type NLGauss
- type NLLame
- type NLLinear
- type NLLogarithmic
- type NLLogistic
- type NLOmt
- type NLP3
- type NLP5
- type NLRand
- type NLSin
- type NLSin1
- type NLSin2
- type NLSquare
- type NLStopped
- type NonLinear
Constants ¶
const ( OddEven wrule = iota NonZero )
const (
Epsilon float64 = 0.000001 // 1:1,000,000
)
%f formats to 6dp by default
const (
TwoPi = math.Pi * 2
)
Variables ¶
var (
SideOfLine = TriArea
)
var WindingRule = OddEven
WindingRule defines how edges in an edge crossing polygon are treated.
Functions ¶
func AngleBetweenLines ¶
AngleBetweenLines using Atan2 vs calculating the dot product (2xSqrt+Acos). Retains the directionality of the rotation from l1 to l2, unlike dot product. The result is in the range [-Pi,Pi].
func AngleInRange ¶
AngleInRange returns true if angle b is within [a,a+r].
func BBContains ¶
BBContains returns true if p is in bb at the smallest dimensionality.
func BBFilter ¶
BBFilter returns only points from pts that are in bb at the smallest dimensionality.
func BBIntersection ¶
BBIntersection returns the bb formed by the overlap or nil.
func Barycentric ¶
Barycentric converts a point on the plane into Barycentric weights given three non-coincident points, tp1, tp2 and tp3.
func Bezier3ToCatmull ¶
Bezier3ToCatmull converts a cubic bezier to a catmul curve. p1, c1, c2, p2 => t1, p1, p2, t2
func BoundingBox ¶
BoundingBox returns the minimum and maximum dimensional values in a set of points. Bounds are inclusive. The dimensionality of the smallest dimension point is used for all points.
func CalcDerivativeWeights ¶
CalcDerivativeWeights calculates the derivative of the supplied curve. Bezier gradient (differentiation): Order of curve drops by one and new weights are the difference of the original weights scaled by the original order
func CalcExtremities ¶
CalcExtremities finds the extremes of a curve in terms of t.
func CalcNextOrderWeights ¶
CalcNextOrderWeights calculates the weights necessary to represent the supplied curve at next highest order, i.e. curve promotion b2 to b3. Note, there's no inverse.
func CalcPointsForArc ¶
CalcPointsForArc takes an arc angle (less than or equal to Pi), and calculates the points for a Bezier cubic to describe it on a circle centered on (0,0) with radius 1. Mid-point of the curve is (1,0) Error increases for values > Pi/2 Returns nil if the angle is zero Ref: https://www.tinaja.com/glib/bezcirc2.pdf
func CatmullToBezier3 ¶
CatmullToBezier3 converts a catmul curve to a cubic bezier. t1, p1, p2, t2 => p1, c1, c2, p2
func Circumcircle ¶
Circumcircle returns the circle (center and radius) that passes through the three points.
func Coincident ¶
True if lines are on the same infinite line
func Collinear ¶
Collinear returns true if three points are on a line (i.e. if the area of the resultant triangle is 0)
func CrossProduct ¶
CrossProduct returns the cross product of the three points. Since the inputs are all in the x-y plane (i.e. z = 0), only the magnitude of the resultant z vector is returned (the x and y vectors are both 0).
func Cubic ¶
Cubic calculates the value of f(t) for t in range [0,1] given the values of t at -1, 0, 1, 2 in p[] fitted to a cubic polynomial: f(t) = at^3 + bt^2 + ct + d. Clamped because it over/undershoots.
func DeCasteljau ¶
DeCasteljau uses de Casteljau's algorithm for degree n curves and returns the point and the tangent of the line it's traversing. {p1, c1, c2, c3, ..., p2}
func Dejitter ¶
Dejitter takes a list of points, greater than three long, and removes the one that forms the smallest area triangle with its adjacent points. If closed is true then the end points are candidates for elimination too.
func DistanceESquared ¶
DistanceESquared returns the squared Euclidean distance between two points.
func DistanceESquaredN ¶
DistanceESquaredN returns the squared Euclidean distance between two points.
func DistanceToLineSquared ¶
DistanceToLineSquared calculates the squared Euclidean length of the normal from a point to the line. Returns the distance squared, the line intercept and the t value.
func DotProduct ¶
DotProduct returns the dot product of the two lines, p1-p2 and p3-p4.
func DotProductAngle ¶
DotProductAngle returns the angle between two lines using the dot product method. The result is in the range [0,Pi].
func IntersectionTVals ¶
IntersectionTVals obtains values of t for each line for where they intersect. Actual intersection => both are in [0,1]
func IntersectionTValsP ¶
IntersectionTValsP obtains values of t for each line for where they intersect. Actual intersection => both are in [0,1]
func InvLerpClamp ¶
InvLerpClamp is a clamped [start, end] version of InvLerp.
func InvLerpClamp32 ¶
InvLerpClamp32 is a float32 version of InvLerpClamp.
func InvNLerp ¶
InvNLerp performs the inverse of NLerp and returns the value of t for a value v (clamped to [start, end]).
func InvNLerp32 ¶
InvNLerp32 is a float32 version of InvNLerp for Path and x/image/vector
func InverseBarycentric ¶
InverseBarycentric converts Barycentric weights plus the three non-coincident points back into a point on the plane.
func Kappa1 ¶
Kappa1 calculates curvature - note curve must have 2nd order derivative. Radius of curvature at t is 1/kappa(t)
func KappaC ¶
KappaC estimates kappa from three points by calculating the center of the circumcircle.
func KappaM ¶
KappaM calculates Menger curvature: 4*area / (d(p1, p2).d(p2s, p3).d(p3, p1)) Same result as KappaC but with more square roots...
func LerpClamp32 ¶
LerpClamp32 is a float32 version of LerpClamp.
func LineNormalToPoint ¶
LineNormalToPoint returns the point on the line that's normal to the specified point, in absolute terms and as a t value. The distance from the point to the line is returned too.
func NLerp ¶
NLerp returns the value of the supplied non-linear function at t. Note t is clamped to [0,1]
func NRM ¶
NRM is a modified Newton-Raphson root search that bails if t falls outside of the range [0,1] since the curve isn't defined there.
func PointInPoly ¶
PointInPoly returns true is a point is within the polygon defined by the list of vertices according to the setting of WindingRule (OddEven or NonZero).
func PointInTriangle ¶
PointInTriangle returns true if p is in the triangle formed by tp1, tp2 and tp3.
func PointOnLine ¶
PointOnLine returns if p is on the line and t for p if it is.
func Remap ¶
Remap converts v from one space to another by applying InvLerp to find t in the initial range, and then using t to find v' in the new range.
func RemapClamp ¶
RemapClamp is a clamped version of Remap.
func RemapClamp32 ¶
RemapClamp32 is a float32 version of RemapClamp.
func RemapNL ¶
RemapNL converts v from one space to another by applying InvNLerp to find t in the initial range, and then using t to find v' in the new range.
func SplitCurve ¶
SplitCurve splits curve at t into two new curves such that the end of the lhs is the start of the rhs {p1, c1, c2, c3, ..., p2}
func TriArea ¶
TriArea returns the signed area of a triangle by finding the determinant of M = {{p1[0] p1[1] 1}
{p2[0] p2[1] 1} {p3[0] p3[1] 1}}
func VecNormalize ¶
VecNormalize scales a vector to unit length.
Types ¶
type BezierCurve ¶
type BezierCurve struct { Weights [][]float64 WeightsDt [][]float64 WeightsDt2 [][]float64 WeightsDt3 [][]float64 }
BezierCurve stores the derivative curve weights.
func NewBezierCurve ¶
func NewBezierCurve(weights [][]float64) *BezierCurve
NewBezierCurve creates a new BezierCurve with the weights of the first, second and third order derivatives of the supplied curve.
func (*BezierCurve) CurveDt2X ¶
func (bc *BezierCurve) CurveDt2X(t float64) float64
CurveDt2X returns the X value for the second order derivative of the curve at t.
func (*BezierCurve) CurveDt2Y ¶
func (bc *BezierCurve) CurveDt2Y(t float64) float64
CurveDt2Y returns the Y value for the second order derivative of the curve at t.
func (*BezierCurve) CurveDt3X ¶
func (bc *BezierCurve) CurveDt3X(t float64) float64
CurveDt3X returns the X value for the third order derivative of the curve at t.
func (*BezierCurve) CurveDt3Y ¶
func (bc *BezierCurve) CurveDt3Y(t float64) float64
CurveDt3Y returns the Y value for the third order derivative of the curve at t.
func (*BezierCurve) CurveDtX ¶
func (bc *BezierCurve) CurveDtX(t float64) float64
CurveDtX returns the X value for the derivative of the curve at t.
func (*BezierCurve) CurveDtY ¶
func (bc *BezierCurve) CurveDtY(t float64) float64
CurveDtY returns the Y value for the derivative of the curve at t.
func (*BezierCurve) CurveX ¶
func (bc *BezierCurve) CurveX(t float64) float64
CurveX returns the X value for the curve at t.
func (*BezierCurve) CurveY ¶
func (bc *BezierCurve) CurveY(t float64) float64
CurveY returns the Y value for the curve at t.
func (*BezierCurve) Kappa ¶
func (bc *BezierCurve) Kappa(t float64) float64
Kappa calculates the curvature at t. Radius of curvature at t is 1/kappa(t)
type NLCatenary ¶
type NLCatenary struct{}
NLCatenary v = cosh(t)
func (*NLCatenary) InvTransform ¶
func (nl *NLCatenary) InvTransform(v float64) float64
func (*NLCatenary) Transform ¶
func (nl *NLCatenary) Transform(t float64) float64
type NLCircle1 ¶
type NLCircle1 struct{}
NLCircle1 v = 1 - sqrt(1-t^2)
func (*NLCircle1) InvTransform ¶
type NLCircle2 ¶
type NLCircle2 struct{}
NLCircle2 v = sqrt(2t-t^2)
func (*NLCircle2) InvTransform ¶
type NLCompound ¶
type NLCompound struct {
Fs []NonLinear
}
NLCompound v = nl[0](nl[1](nl[2](...nl[n-1](t))))
func NewNLCompound ¶
func NewNLCompound(fs []NonLinear) *NLCompound
func (*NLCompound) InvTransform ¶
func (nl *NLCompound) InvTransform(v float64) float64
func (*NLCompound) Transform ¶
func (nl *NLCompound) Transform(t float64) float64
type NLExponential ¶
NLExponential v = (exp(t*k) - 1) * scale
func NewNLExponential ¶
func NewNLExponential(k float64) *NLExponential
func (*NLExponential) InvTransform ¶
func (nl *NLExponential) InvTransform(v float64) float64
func (*NLExponential) Transform ¶
func (nl *NLExponential) Transform(t float64) float64
type NLGauss ¶
type NLGauss struct {
K, Offs, Scale float64
}
NLGauss v = gauss(t, k)
func NewNLGauss ¶
func (*NLGauss) InvTransform ¶
type NLLogarithmic ¶
NLLogarithmic v = log(1+t*k) * scale
func NewNLLogarithmic ¶
func NewNLLogarithmic(k float64) *NLLogarithmic
func (*NLLogarithmic) InvTransform ¶
func (nl *NLLogarithmic) InvTransform(v float64) float64
func (*NLLogarithmic) Transform ¶
func (nl *NLLogarithmic) Transform(t float64) float64
type NLLogistic ¶
type NLLogistic struct {
K, Mp, Offs, Scale float64
}
NLLogistic v = logistic(t, k, mp)
func (*NLLogistic) InvTransform ¶
func (nl *NLLogistic) InvTransform(v float64) float64
func (*NLLogistic) Transform ¶
func (nl *NLLogistic) Transform(t float64) float64
type NLP3 ¶
type NLP3 struct{} // first derivative 0 at t=0,1
NLP3 v = t^2 * (3-2t)
func (*NLP3) InvTransform ¶
type NLP5 ¶
type NLP5 struct{} // first and second derivatives 0 at t=0,1
NLP5 v = t^3 * (t*(6t-15) + 10)
func (*NLP5) InvTransform ¶
type NLRand ¶
NLRand uses random incremental steps from a normal distribution, smoothed with a cubic.
func NewNLRand ¶
NewRandNL calculates a collection of ascending values [0,1] with an average increment of mean and standard deviation of std.
func (*NLRand) InvTransform ¶
type NLSin ¶
type NLSin struct{} // first derivative 0 at t=0,1
NLSin v = sin(t) with t mapped to [-Pi/2,Pi/2]
func (*NLSin) InvTransform ¶
type NLSin1 ¶
type NLSin1 struct{} // first derivative 0 at t=1
NLSin1 v = sin(t) with t mapped to [0,Pi/2]
func (*NLSin1) InvTransform ¶
type NLSin2 ¶
type NLSin2 struct{} // first derivative 0 at t=0,1
NLSin2 v = sin(t) with t mapped to [-Pi/2,0]
func (*NLSin2) InvTransform ¶
type NLStopped ¶
type NLStopped struct {
Stops [][]float64 // Pairs of t, v - both strictly ascending in [0,1]
}
NewStoppedNL uses linear interpolation between the supplied stops