curvedraw

diff src/curve.cc @ 2:ce7aa9a0594c

improved curve editing
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 17 Dec 2015 05:13:25 +0200
parents 8e524989c904
children 2b7ae76c173f
line diff
     1.1 --- a/src/curve.cc	Wed Dec 16 04:49:16 2015 +0200
     1.2 +++ b/src/curve.cc	Thu Dec 17 05:13:25 2015 +0200
     1.3 @@ -46,11 +46,26 @@
     1.4  	return res;
     1.5  }
     1.6  
     1.7 -int Curve::get_point_count() const
     1.8 +bool Curve::empty() const
     1.9 +{
    1.10 +	return cp.empty();
    1.11 +}
    1.12 +
    1.13 +int Curve::size() const
    1.14  {
    1.15  	return (int)cp.size();
    1.16  }
    1.17  
    1.18 +Vector3 &Curve::operator [](int idx)
    1.19 +{
    1.20 +	return cp[idx];
    1.21 +}
    1.22 +
    1.23 +const Vector3 &Curve::operator [](int idx) const
    1.24 +{
    1.25 +	return cp[idx];
    1.26 +}
    1.27 +
    1.28  const Vector3 &Curve::get_homo_point(int idx) const
    1.29  {
    1.30  	return cp[idx];
    1.31 @@ -84,18 +99,28 @@
    1.32  	return true;
    1.33  }
    1.34  
    1.35 +bool Curve::move_point(int idx, const Vector2 &p)
    1.36 +{
    1.37 +	if(idx < 0 || idx >= (int)cp.size()) {
    1.38 +		return false;
    1.39 +	}
    1.40 +	cp[idx] = Vector3(p.x, p.y, cp[idx].z);
    1.41 +	return true;
    1.42 +}
    1.43 +
    1.44  Vector2 Curve::interpolate(float t, CurveType type) const
    1.45  {
    1.46 -	int num_cp = (int)cp.size();
    1.47 -	if(!num_cp) {
    1.48 +	if(cp.empty()) {
    1.49  		return Vector2(0, 0);
    1.50  	}
    1.51 +
    1.52 +	int num_cp = (int)cp.size();
    1.53  	if(num_cp == 1) {
    1.54  		return Vector2(cp[0].x, cp[0].y);
    1.55  	}
    1.56  
    1.57  	Vector3 res;
    1.58 -	int idx0 = (int)floor(t * (num_cp - 1));
    1.59 +	int idx0 = std::min((int)floor(t * (num_cp - 1)), num_cp - 2);
    1.60  	int idx1 = idx0 + 1;
    1.61  
    1.62  	float dt = 1.0 / (float)(num_cp - 1);
    1.63 @@ -135,10 +160,3 @@
    1.64  {
    1.65  	return interpolate(t);
    1.66  }
    1.67 -
    1.68 -float Curve::map_param(float x) const
    1.69 -{
    1.70 -	if(x < 0.0f) x = 0.0f;
    1.71 -	if(x >= 1.0f) x = 1.0f;
    1.72 -	return x * ((float)cp.size() - 1);
    1.73 -}