//
//  FitCurve.h
//  PenTool
//
//  Created by Georg Seifert on 01/12/2015.
//  Copyright (c) 2015 Georg Seifert. All rights reserved.
//

#ifndef PenTool_FitCurve_h
#define PenTool_FitCurve_h

#import <GlyphsCore/GSPenProtocol.h>

void FitCurve(const NSPoint *d, int nPts, float error, NSObject<GSPenProtocol> *pen);

void FitCubic(const NSPoint *d, int first, int last, NSPoint tHat1, NSPoint tHat2, double error, NSObject<GSPenProtocol> *pen, BOOL *hasMove);

BOOL FitCubicSingle(const NSPoint *d, int count, NSPoint tHat1, NSPoint tHat2, double error, NSPoint cubic[4]);

BOOL GSCubicFitsPoints(const NSPoint* points, int num_points, double tolerance, NSPoint p0, NSPoint p1, NSPoint p2, NSPoint p3);

typedef struct {
	CGFloat dedupeEps;

	CGFloat cornerAngleDeg;
	NSUInteger minRunBetweenBreaks;

	BOOL enableLocalExtremaInsert;
	NSUInteger windowCount;				// 5..10 (recommended 7 or 9)
	CGFloat extremaMinTurnDeg;			// candidate detection on polyline
	CGFloat insertEpsDistance;			// reject near-duplicates
	CGFloat acceptDistanceToCenter;		// reject far extrema
	CGFloat acceptDistanceToEdge;		// reject extrema far from edge(i,i+1)

	double finalError;
} GSDetectAndFitParams;

void GSDetectBreaksAndFit(const NSPoint *points,
						  NSUInteger count,
						  double error,
						  NSObject<GSPenProtocol> *pen,
						  GSDetectAndFitParams params);

#endif
