//TODO: this would work only for C1 pw<d2<sb>> input. Split the input at corners to work with pwd2sb... //TODO: for more general purpose, return a path... void toPoly(D2<SBasis> const &f, std::list<Point> &p, double tol, bool include_first=true){ D2<SBasis> df = derivative(f); D2<SBasis> d2f = derivative(df); double t=0; if ( include_first ){ p.push_back( f.at0() );} while (t<1){ Point v = unit_vector(df.valueAt(t)); //OptInterval bounds = bounds_local(df[X]*v[Y]-df[Y]*v[X], Interval(t,1)); OptInterval bounds = bounds_local(d2f[X]*v[Y]-d2f[Y]*v[X], Interval(t,1)); if (bounds) { double bds_max = (-bounds->min()>bounds->max() ? -bounds->min() : bounds->max()); double dt; //if (bds_max<tol) dt = 1; //else dt = tol/bds_max; if (bds_max<tol/4) dt = 1; else dt = 2*std::sqrt( tol / bds_max ); t+=dt*5; if (t>1) t = 1; }else{ t = 1; } p.push_back( f.valueAt(t) ); } return; }
OptInterval bounds_local(const SBasisOf<double> &sb, const OptInterval &i, int order) { #else OptInterval bounds_local(const SBasis &sb, const OptInterval &i, int order) { #endif double t0=i->min(), t1=i->max(), lo=0., hi=0.; for(int j = sb.size()-1; j>=order; j--) { double a=sb[j][0]; double b=sb[j][1]; double t = 0; if (lo<0) t = ((b-a)/lo+1)*0.5; if (lo>=0 || t<t0 || t>t1) { lo = std::min(a*(1-t0)+b*t0+lo*t0*(1-t0),a*(1-t1)+b*t1+lo*t1*(1-t1)); }else{ lo = lerp(t, a+lo*t, b); } if (hi>0) t = ((b-a)/hi+1)*0.5; if (hi<=0 || t<t0 || t>t1) { hi = std::max(a*(1-t0)+b*t0+hi*t0*(1-t0),a*(1-t1)+b*t1+hi*t1*(1-t1)); }else{ hi = lerp(t, a+hi*t, b); } } Interval res = Interval(lo,hi); if (order>0) res*=pow(.25,order); return res; }
void subdiv_sbasis(SBasis const & s, std::vector<double> & roots, double left, double right) { OptInterval bs = bounds_fast(s); if(!bs || bs->min() > 0 || bs->max() < 0) return; // no roots here if(s.tailError(1) < 1e-7) { double t = s[0][0] / (s[0][0] - s[0][1]); roots.push_back(left*(1-t) + t*right); return; } double middle = (left + right)/2; subdiv_sbasis(compose(s, Linear(0, 0.5)), roots, left, middle); subdiv_sbasis(compose(s, Linear(0.5, 1.)), roots, middle, right); }
static std::vector<double> solve_lambda0(double a0,double a1,double c0,double c1, int insist_on_speeds_signs){ SBasis p(3, Linear()); p[0] = Linear( a1*c0*c0+c1, a1*a0*(a0+ 2*c0) +a1*c0*c0 +c1 -1 ); p[1] = Linear( -a1*a0*(a0+2*c0), -a1*a0*(3*a0+2*c0) ); p[2] = Linear( a1*a0*a0 ); OptInterval domain = find_bounds_for_lambda0(a0,a1,c0,c1,insist_on_speeds_signs); if ( !domain ) return std::vector<double>(); p = compose(p,Linear(domain->min(),domain->max())); std::vector<double>rts = roots(p); for (unsigned i=0; i<rts.size(); i++){ rts[i] = domain->min() + rts[i] * domain->extent(); } return rts; }