/** Geometric operators on D2 (1D->2D). * Copyright 2012 JBC Engelen * Copyright 2007 JF Barraud * Copyright 2007 N Hurst * * The functions defined in this header related to 2d geometric operations such as arc length, * unit_vector, curvature, and centroid. Most are built on top of unit_vector, which takes an * arbitrary D2 and returns a D2 with unit length with the same direction. * * Todo/think about: * arclength D2 -> sbasis (giving arclength function) * does uniform_speed return natural parameterisation? * integrate sb2d code from normal-bundle * angle(md<2>) -> sbasis (gives angle from vector - discontinuous?) * osculating circle center? * **/ #include <2geom/sbasis-geometric.h> #include <2geom/sbasis.h> #include <2geom/sbasis-math.h> #include <2geom/sbasis-geometric.h> //namespace Geom{ using namespace Geom; using namespace std; //Some utils first. //TODO: remove this!! /** * Return a list of doubles that appear in both a and b to within error tol * a, b, vector of double * tol tolerance */ static vector vect_intersect(vector const &a, vector const &b, double tol=0.){ vector inter; unsigned i=0,j=0; while ( ib[j]){ j+=1; } } return inter; } //------------------------------------------------------------------------------ static SBasis divide_by_sk(SBasis const &a, int k) { if ( k>=(int)a.size()){ //make sure a is 0? return SBasis(); } if(k < 0) return shift(a,-k); SBasis c; c.insert(c.begin(), a.begin()+k, a.end()); return c; } static SBasis divide_by_t0k(SBasis const &a, int k) { if(k < 0) { SBasis c = Linear(0,1); for (int i=2; i<=-k; i++){ c*=c; } c*=a; return(c); }else{ SBasis c = Linear(1,0); for (int i=2; i<=k; i++){ c*=c; } c*=a; return(divide_by_sk(c,k)); } } static SBasis divide_by_t1k(SBasis const &a, int k) { if(k < 0) { SBasis c = Linear(1,0); for (int i=2; i<=-k; i++){ c*=c; } c*=a; return(c); }else{ SBasis c = Linear(0,1); for (int i=2; i<=k; i++){ c*=c; } c*=a; return(divide_by_sk(c,k)); } } static D2 RescaleForNonVanishingEnds(D2 const &MM, double ZERO=1.e-4){ D2 M = MM; //TODO: divide by all the s at once!!! while ((M[0].size()>1||M[1].size()>1) && fabs(M[0].at0())1||M[1].size()>1) && fabs(M[0].at0())1||M[1].size()>1) && fabs(M[0].at1()) RescaleForNonVanishing(D2 const &MM, double ZERO=1.e-4){ std::vector levels; levels.push_back(-ZERO); levels.push_back(ZERO); //std::vector > mr = multi_roots(MM, levels); }*/ //================================================================= //TODO: what's this for?!?! Piecewise > Geom::cutAtRoots(Piecewise > const &M, double ZERO){ vector rts; for (unsigned i=0; i seg_rts = roots((M.segs[i])[0]); seg_rts = vect_intersect(seg_rts, roots((M.segs[i])[1]), ZERO); Linear mapToDom = Linear(M.cuts[i],M.cuts[i+1]); for (unsigned r=0; r Geom::atan2(Piecewise > const &vect, double tol, unsigned order){ Piecewise result; Piecewise > v = cutAtRoots(vect,tol); result.cuts.push_back(v.cuts.front()); for (unsigned i=0; i vi = RescaleForNonVanishingEnds(v.segs[i]); SBasis x=vi[0], y=vi[1]; Piecewise angle; angle = divide (x*derivative(y)-y*derivative(x), x*x+y*y, tol, order); //TODO: I don't understand this - sign. angle = integral(-angle); Point vi0 = vi.at0(); angle += -std::atan2(vi0[1],vi0[0]) - angle[0].at0(); //TODO: deal with 2*pi jumps form one seg to the other... //TODO: not exact at t=1 because of the integral. //TODO: force continuity? angle.setDomain(Interval(v.cuts[i],v.cuts[i+1])); result.concat(angle); } return result; } /** Return a function which gives the angle of vect at each point. \param vect a piecewise parameteric curve. \param tol the maximum error allowed. \param order the maximum degree to use for approximation \relates Piecewise, D2 */ Piecewise Geom::atan2(D2 const &vect, double tol, unsigned order){ return atan2(Piecewise >(vect),tol,order); } /** tan2 is the pseudo-inverse of atan2. It takes an angle and returns a unit_vector that points in the direction of angle. \param angle a piecewise function of angle wrt t. \param tol the maximum error allowed. \param order the maximum degree to use for approximation \relates D2, SBasis */ D2 > Geom::tan2(SBasis const &angle, double tol, unsigned order){ return tan2(Piecewise(angle), tol, order); } /** tan2 is the pseudo-inverse of atan2. It takes an angle and returns a unit_vector that points in the direction of angle. \param angle a piecewise function of angle wrt t. \param tol the maximum error allowed. \param order the maximum degree to use for approximation \relates Piecewise, D2 */ D2 > Geom::tan2(Piecewise const &angle, double tol, unsigned order){ return D2 >(cos(angle, tol, order), sin(angle, tol, order)); } /** Return a Piecewise > which points in the same direction as V_in, but has unit_length. \param V_in the original path. \param tol the maximum error allowed. \param order the maximum degree to use for approximation unitVector(x,y) is computed as (b,-a) where a and b are solutions of: ax+by=0 (eqn1) and a^2+b^2=1 (eqn2) \relates Piecewise, D2 */ Piecewise > Geom::unitVector(D2 const &V_in, double tol, unsigned order){ //TODO: Handle vanishing vectors... // -This approach is numerically bad. Find a stable way to rescale V_in to have non vanishing ends. // -This done, unitVector will have jumps at zeros: fill the gaps with arcs of circles. D2 V = RescaleForNonVanishingEnds(V_in); if (V[0].isZero(tol) && V[1].isZero(tol)) return Piecewise >(D2(Linear(1),SBasis())); SBasis x = V[0], y = V[1]; SBasis r_eqn1, r_eqn2; Point v0 = unit_vector(V.at0()); Point v1 = unit_vector(V.at1()); SBasis a = SBasis(order+1, Linear(0.)); a[0] = Linear(-v0[1],-v1[1]); SBasis b = SBasis(order+1, Linear(0.)); b[0] = Linear( v0[0], v1[0]); r_eqn1 = -(a*x+b*y); r_eqn2 = Linear(1.)-(a*a+b*b); for (unsigned k=1; k<=order; k++){ double r0 = (k unitV; unitV[0] = b; unitV[1] = -a; //is it good? double rel_tol = std::max(1.,std::max(V_in[0].tailError(0),V_in[1].tailError(0)))*tol; if (r_eqn1.tailError(order)>rel_tol || r_eqn2.tailError(order)>tol){ //if not: subdivide and concat results. Piecewise > unitV0, unitV1; unitV0 = unitVector(compose(V,Linear(0,.5)),tol,order); unitV1 = unitVector(compose(V,Linear(.5,1)),tol,order); unitV0.setDomain(Interval(0.,.5)); unitV1.setDomain(Interval(.5,1.)); unitV0.concat(unitV1); return(unitV0); }else{ //if yes: return it as pw. Piecewise > result; result=(Piecewise >)unitV; return result; } } /** Return a Piecewise > which points in the same direction as V_in, but has unit_length. \param V_in the original path. \param tol the maximum error allowed. \param order the maximum degree to use for approximation unitVector(x,y) is computed as (b,-a) where a and b are solutions of: ax+by=0 (eqn1) and a^2+b^2=1 (eqn2) \relates Piecewise */ Piecewise > Geom::unitVector(Piecewise > const &V, double tol, unsigned order){ Piecewise > result; Piecewise > VV = cutAtRoots(V); result.cuts.push_back(VV.cuts.front()); for (unsigned i=0; i > unit_seg; unit_seg = unitVector(VV.segs[i],tol, order); unit_seg.setDomain(Interval(VV.cuts[i],VV.cuts[i+1])); result.concat(unit_seg); } return result; } /** returns a function giving the arclength at each point in M. \param M the Element. \param tol the maximum error allowed. \relates Piecewise */ Piecewise Geom::arcLengthSb(Piecewise > const &M, double tol){ Piecewise > dM = derivative(M); Piecewise dMlength = sqrt(dot(dM,dM),tol,3); Piecewise length = integral(dMlength); length-=length.segs.front().at0(); return length; } /** returns a function giving the arclength at each point in M. \param M the Element. \param tol the maximum error allowed. \relates Piecewise, D2 */ Piecewise Geom::arcLengthSb(D2 const &M, double tol){ return arcLengthSb(Piecewise >(M), tol); } #if 0 double Geom::length(D2 const &M, double tol){ Piecewise length = arcLengthSb(M, tol); return length.segs.back().at1(); } double Geom::length(Piecewise > const &M, double tol){ Piecewise length = arcLengthSb(M, tol); return length.segs.back().at1(); } #endif /** returns a function giving the curvature at each point in M. \param M the Element. \param tol the maximum error allowed. \relates Piecewise, D2 \todo claimed incomplete. Check. */ Piecewise Geom::curvature(D2 const &M, double tol) { D2 dM=derivative(M); Piecewise > unitv = unitVector(dM,tol); Piecewise dMlength = dot(Piecewise >(dM),unitv); Piecewise k = cross(derivative(unitv),unitv); k = divide(k,dMlength,tol,3); return(k); } /** returns a function giving the curvature at each point in M. \param M the Element. \param tol the maximum error allowed. \relates Piecewise \todo claimed incomplete. Check. */ Piecewise Geom::curvature(Piecewise > const &V, double tol){ Piecewise result; Piecewise > VV = cutAtRoots(V); result.cuts.push_back(VV.cuts.front()); for (unsigned i=0; i curv_seg; curv_seg = curvature(VV.segs[i],tol); curv_seg.setDomain(Interval(VV.cuts[i],VV.cuts[i+1])); result.concat(curv_seg); } return result; } //================================================================= /** Reparameterise M to have unit speed. \param M the Element. \param tol the maximum error allowed. \param order the maximum degree to use for approximation \relates Piecewise, D2 */ Piecewise > Geom::arc_length_parametrization(D2 const &M, unsigned order, double tol){ Piecewise > u; u.push_cut(0); Piecewise s = arcLengthSb(Piecewise >(M),tol); for (unsigned i=0; i < s.size();i++){ double t0=s.cuts[i],t1=s.cuts[i+1]; if ( are_near(s(t0),s(t1)) ) { continue; } D2 sub_M = compose(M,Linear(t0,t1)); D2 sub_u; for (unsigned dim=0;dim<2;dim++){ SBasis sub_s = s.segs[i]; sub_s-=sub_s.at0(); sub_s/=sub_s.at1(); sub_u[dim]=compose_inverse(sub_M[dim],sub_s, order, tol); } u.push(sub_u,s(t1)); } return u; } /** Reparameterise M to have unit speed. \param M the Element. \param tol the maximum error allowed. \param order the maximum degree to use for approximation \relates Piecewise */ Piecewise > Geom::arc_length_parametrization(Piecewise > const &M, unsigned order, double tol){ Piecewise > result; for (unsigned i=0; i static double sb_length_integrating(double t, void* param) { SBasis* pc = (SBasis*)param; return sqrt((*pc)(t)); } /** Calculates the length of a D2 through gsl integration. \param B the Element. \param tol the maximum error allowed. \param result variable to be incremented with the length of the path \param abs_error variable to be incremented with the estimated error \relates D2 If you only want the length, this routine may be faster/more accurate. */ void Geom::length_integrating(D2 const &B, double &result, double &abs_error, double tol) { D2 dB = derivative(B); SBasis dB2 = dot(dB, dB); gsl_function F; gsl_integration_workspace * w = gsl_integration_workspace_alloc (20); F.function = &sb_length_integrating; F.params = (void*)&dB2; double quad_result, err; /* We could probably use the non adaptive code here if we removed any cusps first. */ gsl_integration_qag (&F, 0, 1, 0, tol, 20, GSL_INTEG_GAUSS21, w, &quad_result, &err); abs_error += err; result += quad_result; } /** Calculates the length of a D2 through gsl integration. \param s the Element. \param tol the maximum error allowed. \relates D2 If you only want the total length, this routine faster and more accurate than constructing an arcLengthSb. */ double Geom::length(D2 const &s, double tol){ double result = 0; double abs_error = 0; length_integrating(s, result, abs_error, tol); return result; } /** Calculates the length of a Piecewise > through gsl integration. \param s the Element. \param tol the maximum error allowed. \relates Piecewise If you only want the total length, this routine faster and more accurate than constructing an arcLengthSb. */ double Geom::length(Piecewise > const &s, double tol){ double result = 0; double abs_error = 0; for (unsigned i=0; i < s.size();i++){ length_integrating(s[i], result, abs_error, tol); } return result; } /** * Centroid using sbasis integration. \param p the Element. \param centroid on return contains the centroid of the shape \param area on return contains the signed area of the shape. \relates Piecewise This approach uses green's theorem to compute the area and centroid using integrals. For curved shapes this is much faster than converting to polyline. Note that without an uncross operation the output is not the absolute area. * Returned values: 0 for normal execution; 2 if area is zero, meaning centroid is meaningless. */ unsigned Geom::centroid(Piecewise > const &p, Point& centroid, double &area) { Point centroid_tmp(0,0); double atmp = 0; for(unsigned i = 0; i < p.size(); i++) { SBasis curl = dot(p[i], rot90(derivative(p[i]))); SBasis A = integral(curl); D2 C = integral(multiply(curl, p[i])); atmp += A.at1() - A.at0(); centroid_tmp += C.at1()- C.at0(); // first moment. } // join ends centroid_tmp *= 2; Point final = p[p.size()-1].at1(), initial = p[0].at0(); const double ai = cross(final, initial); atmp += ai; centroid_tmp += (final + initial)*ai; // first moment. area = atmp / 2; if (atmp != 0) { centroid = centroid_tmp / (3 * atmp); return 0; } return 2; } /** * Find cubics with prescribed curvatures at both ends. * * this requires to solve a system of the form * * \f[ * \lambda_1 = a_0 \lambda_0^2 + c_0 * \lambda_0 = a_1 \lambda_1^2 + c_1 * \f] * * which is a deg 4 equation in lambda 0. * Below are basic functions dedicated to solving this assuming a0 and a1 !=0. */ static OptInterval find_bounds_for_lambda0(double aa0,double aa1,double cc0,double cc1, int insist_on_speeds_signs){ double a0=aa0,a1=aa1,c0=cc0,c1=cc1; Interval result; bool flip = a1<0; if (a1<0){a1=-a1; c1=-c1;} if (a0<0){a0=-a0; c0=-c0;} double a = (a0 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(); p = compose(p,Linear(domain->min(),domain->max())); std::vectorrts = roots(p); for (unsigned i=0; imin() + rts[i] * domain->extent(); } return rts; } /** * \brief returns the cubics fitting direction and curvature of a given * input curve at two points. * * The input can be the * value, speed, and acceleration * or * value, speed, and cross(acceleration,speed) * of the original curve at the both ends. * (the second is often technically useful, as it avoids unnecessary division by |v|^2) * Recall that K=1/R=cross(acceleration,speed)/|speed|^3. * * Moreover, a 7-th argument 'insist_on_speed_signs' can be supplied to select solutions: * If insist_on_speed_signs == 1, only consider solutions where speeds at both ends are positively * proportional to the given ones. * If insist_on_speed_signs == 0, allow speeds to point in the opposite direction (both at the same time) * If insist_on_speed_signs == -1, allow speeds to point in both direction independently. * * \relates D2 */ std::vector > Geom::cubics_fitting_curvature(Point const &M0, Point const &M1, Point const &dM0, Point const &dM1, double d2M0xdM0, double d2M1xdM1, int insist_on_speed_signs, double epsilon){ std::vector > result; //speed of cubic bezier will be lambda0*dM0 and lambda1*dM1, //with lambda0 and lambda1 s.t. curvature at both ends is the same //as the curvature of the given curve. std::vector lambda0,lambda1; double dM1xdM0=cross(dM1,dM0); if (fabs(dM1xdM0) solns=solve_lambda0(a0,a1,c0,c1,insist_on_speed_signs); for (unsigned i=0;i=0. && lbda1>=0.){ lambda0.push_back( lbda0); lambda1.push_back( lbda1); } //is this solution pointing in the - direction at both ends? else if (lbda0<=0. && lbda1<=0. && insist_on_speed_signs<=0){ lambda0.push_back( lbda0); lambda1.push_back( lbda1); } //ok,this solution is pointing in the + and - directions. else if (insist_on_speed_signs<0){ lambda0.push_back( lbda0); lambda1.push_back( lbda1); } } } } for (unsigned i=0; i cubic; for(unsigned dim=0;dim<2;dim++){ SBasis c(2, Linear()); c[0] = Linear(M0[dim],M1[dim]); c[1] = Linear( M0[dim]-M1[dim]+V0[dim], -M0[dim]+M1[dim]-V1[dim]); cubic[dim] = c; } #if 0 Piecewise k = curvature(result); double dM0_l = dM0.length(); double dM1_l = dM1.length(); g_warning("Target radii: %f, %f", dM0_l*dM0_l*dM0_l/d2M0xdM0,dM1_l*dM1_l*dM1_l/d2M1xdM1); g_warning("Obtained radii: %f, %f",1/k.valueAt(0),1/k.valueAt(1)); #endif result.push_back(cubic); } return(result); } std::vector > Geom::cubics_fitting_curvature(Point const &M0, Point const &M1, Point const &dM0, Point const &dM1, Point const &d2M0, Point const &d2M1, int insist_on_speed_signs, double epsilon){ double d2M0xdM0 = cross(d2M0,dM0); double d2M1xdM1 = cross(d2M1,dM1); return cubics_fitting_curvature(M0,M1,dM0,dM1,d2M0xdM0,d2M1xdM1,insist_on_speed_signs,epsilon); } std::vector > Geom::cubics_with_prescribed_curvature(Point const &M0, Point const &M1, Point const &dM0, Point const &dM1, double k0, double k1, int insist_on_speed_signs, double epsilon){ double length; length = dM0.length(); double d2M0xdM0 = k0*length*length*length; length = dM1.length(); double d2M1xdM1 = k1*length*length*length; return cubics_fitting_curvature(M0,M1,dM0,dM1,d2M0xdM0,d2M1xdM1,insist_on_speed_signs,epsilon); } namespace Geom { /** * \brief returns all the parameter values of A whose tangent passes through P. * \relates D2 */ std::vector find_tangents(Point P, D2 const &A) { SBasis crs (cross(A - P, derivative(A))); return roots(crs); } /** * \brief returns all the parameter values of A whose normal passes through P. * \relates D2 */ std::vector find_normals(Point P, D2 const &A) { SBasis crs (dot(A - P, derivative(A))); return roots(crs); } /** * \brief returns all the parameter values of A whose normal is parallel to vector V. * \relates D2 */ std::vector find_normals_by_vector(Point V, D2 const &A) { SBasis crs = dot(derivative(A), V); return roots(crs); } /** * \brief returns all the parameter values of A whose tangent is parallel to vector V. * \relates D2 */ std::vector find_tangents_by_vector(Point V, D2 const &A) { SBasis crs = dot(derivative(A), rot90(V)); return roots(crs); } } //}; // namespace /* Local Variables: mode:c++ c-file-style:"stroustrup" c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) indent-tabs-mode:nil fill-column:99 End: */ // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :