root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / vectorTools / smoothLines / Bezier.java @ 59
History | View | Annotate | Download (1.96 KB)
1 |
/*********************************************************
|
---|---|
2 |
* Code adapted from Tim Lambert's Java Classes
|
3 |
* http://www.cse.unsw.edu.au/~lambert/
|
4 |
*********************************************************/
|
5 |
package es.unex.sextante.vectorTools.smoothLines; |
6 |
|
7 |
import java.awt.Point; |
8 |
import java.util.ArrayList; |
9 |
|
10 |
import com.vividsolutions.jts.geom.Coordinate; |
11 |
import com.vividsolutions.jts.geom.Geometry; |
12 |
import com.vividsolutions.jts.geom.GeometryFactory; |
13 |
import com.vividsolutions.jts.geom.LineString; |
14 |
|
15 |
public class Bezier |
16 |
extends
|
17 |
ControlCurve { |
18 |
|
19 |
public Bezier(final Geometry geom) { |
20 |
|
21 |
super(geom);
|
22 |
|
23 |
} |
24 |
|
25 |
|
26 |
// the basis function for a Bezier spline
|
27 |
static float b(final int i, |
28 |
final float t) { |
29 |
switch (i) {
|
30 |
case 0: |
31 |
return (1 - t) * (1 - t) * (1 - t); |
32 |
case 1: |
33 |
return 3 * t * (1 - t) * (1 - t); |
34 |
case 2: |
35 |
return 3 * t * t * (1 - t); |
36 |
case 3: |
37 |
return t * t * t;
|
38 |
} |
39 |
return 0; //we only get here if an invalid i is specified |
40 |
} |
41 |
|
42 |
|
43 |
//evaluate a point on the B spline
|
44 |
Point p(final int i, |
45 |
final float t) { |
46 |
float px = 0; |
47 |
float py = 0; |
48 |
for (int j = 0; j <= 3; j++) { |
49 |
px += b(j, t) * m_X[i + j]; |
50 |
py += b(j, t) * m_Y[i + j]; |
51 |
} |
52 |
return new Point((int) Math.round(px), (int) Math.round(py)); |
53 |
} |
54 |
|
55 |
|
56 |
@Override
|
57 |
public LineString getSmoothedLine(final int iSteps) { |
58 |
|
59 |
final ArrayList<Coordinate> coords = new ArrayList<Coordinate>(); |
60 |
Point q = p(0, 0); |
61 |
coords.add(new Coordinate(q.x, q.y));
|
62 |
for (int i = 0; i < m_X.length - 3; i += 3) { |
63 |
for (int j = 1; j <= iSteps; j++) { |
64 |
q = p(i, j / (float) iSteps);
|
65 |
coords.add(new Coordinate(q.x, q.y));
|
66 |
} |
67 |
} |
68 |
|
69 |
return new GeometryFactory().createLineString((Coordinate[]) coords.toArray(new Coordinate[0])); |
70 |
|
71 |
} |
72 |
|
73 |
} |