root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / vectorTools / smoothLines / BSpline.java @ 59
History | View | Annotate | Download (1.98 KB)
1 | 59 | nbrodin | /*********************************************************
|
---|---|---|---|
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 BSpline |
||
16 | extends
|
||
17 | ControlCurve { |
||
18 | |||
19 | public BSpline(final Geometry geom) { |
||
20 | |||
21 | super(geom);
|
||
22 | |||
23 | } |
||
24 | |||
25 | |||
26 | // the basis function for a cubic B spline
|
||
27 | float b(final int i, |
||
28 | float t) {
|
||
29 | switch (i) {
|
||
30 | case -2: |
||
31 | return (((-t + 3) * t - 3) * t + 1) / 6; |
||
32 | case -1: |
||
33 | return (((3 * t - 6) * t) * t + 4) / 6; |
||
34 | case 0: |
||
35 | return (((-3 * t + 3) * t + 3) * t + 1) / 6; |
||
36 | case 1: |
||
37 | return (t * t * t) / 6; |
||
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 = -2; j <= 1; 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(2, 0); |
||
61 | coords.add(new Coordinate(q.x, q.y));
|
||
62 | for (int i = 2; i < m_X.length - 1; i++) { |
||
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 | } |