Module gwy :: Class Spline
[hide private]
[frames] | no frames]

Class Spline

source code

Instance Methods [hide private]
 
__init__()
Creates a new empty spline curve.
source code
 
free()
Frees a spline curve and all associated resources.
source code
 
copy()
Creates a copy of a spline curve.
source code
 
get_npoints()
Gets the number of points of a spline curve.
source code
 
get_slackness()
Gets the slackness parameter of a spline curve.
source code
 
get_closed()
Reports whether a spline curve is closed or not.
source code
 
set_points(xy, n)
Sets the coordinates of XY points a spline curve should pass through.
source code
 
set_slackness(slackness)
Sets the slackness parameter of a spline curve.
source code
 
set_closed(closed)
Sets whether a spline curve is closed or open.
source code
 
length()
Calculates the length of a spline curve.
source code
 
get_points()
Gets the coordinates of spline curve points.
source code
 
get_tangents()
Gets tangents to the curve in its defining points.
source code
 
sample_naturally()
Samples efficiently a spline curve.
source code
 
sample_uniformly(n)
Samples uniformly a spline curve.
source code
Method Details [hide private]

__init__()
(Constructor)

source code 

Creates a new empty spline curve.

You need to set the curve points using Spline.set_points() before any sampling along the curve. Alternatively, use Spline.new_from_points() to construct the spline already with some points.

Returns:
A newly created spline curve. (Spline)

Since: 2.45

free()

source code 

Frees a spline curve and all associated resources.

Since: 2.45

copy()

source code 

Creates a copy of a spline curve.

Returns:
A newly created spline curve. (Spline)

Since: 2.49

get_npoints()

source code 

Gets the number of points of a spline curve.

Returns:
The number of XY points defining the curve. (int)

Since: 2.45

get_slackness()

source code 

Gets the slackness parameter of a spline curve.

See Spline.set_slackness() for discussion.

Returns:
The slackness parameter value. (float)

Since: 2.45

get_closed()

source code 

Reports whether a spline curve is closed or not.

See Spline.set_closed() for discussion.

Returns:
True if spline is closed, False if it is open-ended. (bool)

Since: 2.45

set_points(xy, n)

source code 

Sets the coordinates of XY points a spline curve should pass through.

It is possible to pass n=0 to make the spline empty (xy can be None then) but such spline may not be sampled using Spline.sample_uniformly().

The coordinates should be device-scaled, i.e. they should data field rows and columns, or screen or image pixels. Generally, the unit length should be about the smallest distinguishable distance.

This is important namely for Spline.sample_naturally() that stops refining the curve when the details become too tiny, even though there may be sharp changes of direction. It is also important if the physical X and Y scales differ.

Using unscaled physical coordinates may produce odd results.

Parameters:
  • xy - Array of points in plane the curve will pass through. (const-XY*)
  • n - Number of points in xy. (int)

Since: 2.45

set_slackness(slackness)

source code 

Sets the slackness parameter of a spline curve.

The slackness parameter determines how taut or slack the curve is.

The curve always passes through the given XY points. For zero slackness the curve is maximally taut, i.e. the shortest possible passing through the points. Such curve is formed by straight segments. For slackness of 1 the curve is a ‘free’ spline. Values smaller than 1 mean tensile stress while values larger than 1 compressive stres. The default value is 1/sqrt(2).

Parameters:
  • slackness - New slackness parameter value from the range [0, glib.SQRT2]. (float)

Since: 2.45

set_closed(closed)

source code 

Sets whether a spline curve is closed or open.

In closed curve the last point is connected smoothly with the first point, forming a cycle. Note you should not repeat the point in the xy array. When a closed curve is sampled, the sampling starts from the first point and continues beyond the last point until it gets close to the first point again.

An open curve begins with the first point and ends with the last point. It has zero curvature at these two points.

Parameters:
  • closed - True to make spline closed, False to make it open-ended. (bool)

Since: 2.45

length()

source code 

Calculates the length of a spline curve.

This is useful when you want to sample the curve with a specific step (at least approximately).

Note Spline.sample_uniformly() also returns the length.

Returns:
The curve length. (float)

Since: 2.45

get_points()

source code 

Gets the coordinates of spline curve points.

If the spline is empty (there are no points) the function returns None.

Returns:
Coordinates of the XY points defining the curve. The returned array is owned by spline, must not be modified and is only guaranteed to exist so long as the spline is not modified nor destroyed. (list)

Since: 2.45

get_tangents()

source code 

Gets tangents to the curve in its defining points.

See Spline.sample_uniformly() for discussion.

If the spline is empty (there are no points) the function returns None.

Returns:
Tangents to the spline in the XY points defining the curve. The returned array is owned by spline, must not be modified and is only guaranteed to exist so long as the spline is not modified nor destroyed. (list)

Since: 2.45

sample_naturally()

source code 

Samples efficiently a spline curve.

This function calculates coordinates of points that lie on the spline curve and are sufficient for a good approximation by straight lines. This is particularly useful for drawing the curve.

See Spline.sample_uniformly() for some discussion of closed versus open curves and corner case handling.

Returns:
Coordinates of the XY points defining the sampled curve. The returned array is owned by spline, must not be modified and is only guaranteed to exist so long as the spline is not modified nor destroyed. (list)

Since: 2.45

sample_uniformly(n)

source code 

Samples uniformly a spline curve.

This function calculates coordinates of points that lie on the spline curve and are equidistant along it. For open curves the first sampled point coincides with the first given XY point and, similar, the last with the last. For closed curves the first point again coincides with the first given XY point but the last lies one sampling distance before the curve gets back again to the first point.

If you want to specify the sampling step instead of the number of samples use Spline.length() first to obtain the curve length and calculate n accordingly.

A single-point curve always consists of a single point. Hence all samples lie in this point. A two-point curve is always formed by straight segments, in the case of a closed curve one going forward and the other back. A meaningful sampling requires n at least 2, nevertheless, the function permits also n of one or zero.

The tangents vectors stored in t are normalised and oriented from the beginning of the curve towards the end. If two or more consecutive given XY points coincide or the curve has only a single point the vectors may be (0,0).

Parameters:
  • n - The number of samples to take. (int)
Returns:
Tuple consisting of 3 values (value, xy, t). ((float), (list), (list))

Since: 2.45