root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / tin / smoothTinBezier / SmoothTinBezierAlgorithm.java @ 59
History | View | Annotate | Download (7 KB)
1 | 59 | nbrodin | /**
|
---|---|---|---|
2 | * @author Josef Bezdek, ZCU Plzen
|
||
3 | * @version 1.0
|
||
4 | * @since JDK1.5
|
||
5 | */
|
||
6 | |||
7 | |||
8 | package es.unex.sextante.tin.smoothTinBezier; |
||
9 | |||
10 | import java.util.TreeMap; |
||
11 | |||
12 | import com.vividsolutions.jts.geom.Coordinate; |
||
13 | import com.vividsolutions.jts.geom.GeometryFactory; |
||
14 | import com.vividsolutions.jts.geom.LinearRing; |
||
15 | import com.vividsolutions.jts.index.strtree.STRtree; |
||
16 | |||
17 | import es.unex.sextante.additionalInfo.AdditionalInfoNumericalValue; |
||
18 | import es.unex.sextante.additionalInfo.AdditionalInfoVectorLayer; |
||
19 | import es.unex.sextante.core.GeoAlgorithm; |
||
20 | import es.unex.sextante.core.Sextante; |
||
21 | import es.unex.sextante.dataObjects.IFeature; |
||
22 | import es.unex.sextante.dataObjects.IFeatureIterator; |
||
23 | import es.unex.sextante.dataObjects.IRecord; |
||
24 | import es.unex.sextante.dataObjects.IVectorLayer; |
||
25 | import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
||
26 | import es.unex.sextante.outputs.OutputVectorLayer; |
||
27 | |||
28 | public class SmoothTinBezierAlgorithm |
||
29 | extends
|
||
30 | GeoAlgorithm { |
||
31 | |||
32 | public static final String TIN = "TIN"; |
||
33 | public static final String TINB = "TINB"; |
||
34 | public static final String LoD = "LoD"; |
||
35 | public static final String Smooth = "Smooth"; |
||
36 | |||
37 | private IVectorLayer m_Triangles;
|
||
38 | private IVectorLayer m_TrianglesOut;
|
||
39 | private int m_LoD; |
||
40 | private double m_Smooth; |
||
41 | |||
42 | private STRtree trianglesIndex;
|
||
43 | Coordinate[][] triangles; |
||
44 | TreeMap breakLines = new TreeMap(); |
||
45 | Bezier miniBezierTriangles[];
|
||
46 | |||
47 | |||
48 | @Override
|
||
49 | public void defineCharacteristics() { |
||
50 | |||
51 | setName(Sextante.getText("Bezier_surface"));
|
||
52 | setGroup(Sextante.getText("TIN"));
|
||
53 | setUserCanDefineAnalysisExtent(false);
|
||
54 | final String[] sDistance = { "1", "2", "3", "4", "5", "6", "7", "8", "9" }; |
||
55 | |||
56 | try {
|
||
57 | m_Parameters.addInputVectorLayer(TIN, Sextante.getText("TIN"), AdditionalInfoVectorLayer.SHAPE_TYPE_POLYGON, true); |
||
58 | |||
59 | m_Parameters.addSelection(LoD, Sextante.getText("Level_of_detail"), sDistance);
|
||
60 | m_Parameters.addNumericalValue(Smooth, Sextante.getText("Smoothing_coef"),
|
||
61 | AdditionalInfoNumericalValue.NUMERICAL_VALUE_DOUBLE, 1, 0.1, 1); |
||
62 | |||
63 | addOutputVectorLayer(TINB, Sextante.getText("Result"), OutputVectorLayer.SHAPE_TYPE_POLYGON);
|
||
64 | } |
||
65 | catch (final Exception e) { |
||
66 | Sextante.addErrorToLog(e); |
||
67 | } |
||
68 | |||
69 | } |
||
70 | |||
71 | |||
72 | @Override
|
||
73 | public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
||
74 | |||
75 | int i;
|
||
76 | int iShapeCount;
|
||
77 | double scaleZ = Double.NEGATIVE_INFINITY; |
||
78 | |||
79 | |||
80 | m_Triangles = m_Parameters.getParameterValueAsVectorLayer(TIN); |
||
81 | m_LoD = m_Parameters.getParameterValueAsInt(LoD); |
||
82 | m_Smooth = m_Parameters.getParameterValueAsDouble(Smooth); |
||
83 | |||
84 | final Class types[] = { Integer.class, String.class, Integer.class }; |
||
85 | final String sNames[] = { "ID", "HardLines", "type" }; |
||
86 | m_TrianglesOut = getNewVectorLayer(TINB, m_Triangles.getName() + "_bezier", IVectorLayer.SHAPE_TYPE_POLYGON, types, sNames);
|
||
87 | |||
88 | |||
89 | i = 0;
|
||
90 | iShapeCount = m_Triangles.getShapesCount(); |
||
91 | triangles = new Coordinate[iShapeCount][3]; |
||
92 | IFeatureIterator iter = m_Triangles.iterator(); |
||
93 | try {
|
||
94 | // dd.addField(Integer.class);
|
||
95 | // PageStore ps = new MemoryPageStore(dd);
|
||
96 | trianglesIndex = new STRtree();
|
||
97 | while (iter.hasNext()) {
|
||
98 | final IFeature feature = iter.next();
|
||
99 | //TODO: this is supposed to be TIN input, so we should check an throw an error if
|
||
100 | //TODO: we get a polygon which is not a triangle!
|
||
101 | |||
102 | final IRecord record = feature.getRecord();
|
||
103 | if (((String) record.getValue(1)) == "Y") { |
||
104 | breakLines.put(i, record.getValue(2));
|
||
105 | } |
||
106 | |||
107 | triangles[i][0] = (Coordinate) feature.getGeometry().getCoordinates()[0].clone(); |
||
108 | triangles[i][1] = (Coordinate) feature.getGeometry().getCoordinates()[1].clone(); |
||
109 | triangles[i][2] = (Coordinate) feature.getGeometry().getCoordinates()[2].clone(); |
||
110 | //TODO: add fake Z-coordinates (0.0) if this is a 2D input TIN
|
||
111 | |||
112 | // data = new Data(dd);
|
||
113 | // data.addValue(i);
|
||
114 | |||
115 | //trianglesIndex.insert(trianglePolygon.getEnvelopeInternal(), new Integer(i));
|
||
116 | trianglesIndex.insert(feature.getGeometry().getEnvelopeInternal(), new Integer(i)); |
||
117 | |||
118 | /* DEBUG */
|
||
119 | System.err.println("TRIANGLE " + i + " = " + triangles[i][0] + ", " + triangles[i][1] + ", " + triangles[i][2]); |
||
120 | |||
121 | for (int k = 0; k < 2; k++) { |
||
122 | final double diffZ = triangles[i][k].z - triangles[i][k + 1].z; |
||
123 | final double diffXY = Math.sqrt(Math.pow((triangles[i][k].x - triangles[i][k + 1].x), 2) |
||
124 | + Math.pow((triangles[i][k].y - triangles[i][k + 1].y), 2)); |
||
125 | |||
126 | //TODO: if
|
||
127 | if (scaleZ < Math.abs(diffZ / diffXY)) { |
||
128 | scaleZ = Math.abs(diffZ / diffXY);
|
||
129 | } |
||
130 | } |
||
131 | setProgress(i, 2 * iShapeCount);
|
||
132 | i++; |
||
133 | } |
||
134 | iter.close(); |
||
135 | } |
||
136 | catch (final Exception e) { |
||
137 | e.printStackTrace(); |
||
138 | } |
||
139 | |||
140 | // TODO: what happens if we have only fake Z coordinates (=0.0) ???
|
||
141 | // TODO: in that case, scaleZ*m_smooth (see below) will be = 0
|
||
142 | // TODO: would that be a problem?
|
||
143 | System.err.println("SCALEZ = " + scaleZ); |
||
144 | |||
145 | m_Triangles = null;
|
||
146 | iter = null;
|
||
147 | final BezierSurface bezierSurface = new BezierSurface(triangles, trianglesIndex, breakLines, scaleZ * m_Smooth, m_LoD + 1); |
||
148 | |||
149 | |||
150 | int indexOfInterpolatedTriangles = 0; |
||
151 | while (bezierSurface.hasNext()) {
|
||
152 | setProgress(i++, 2 * iShapeCount);
|
||
153 | final Coordinate newTin[][] = bezierSurface.nextTrinagle(); |
||
154 | System.err.println("GOT " + newTin.length + " TRIANGLES"); |
||
155 | for (int l = 0; l < newTin.length; l++) { |
||
156 | final Object[] record = { new Integer(indexOfInterpolatedTriangles), "", -1 }; |
||
157 | final GeometryFactory gf = new GeometryFactory(); |
||
158 | final Coordinate[] coords = new Coordinate[4]; |
||
159 | for (int m = 0; m < 3; m++) { |
||
160 | coords[m] = newTin[l][m]; |
||
161 | if (coords[m] != null) { |
||
162 | /* DEBUG */
|
||
163 | System.err.println(coords[m]);
|
||
164 | } |
||
165 | |||
166 | } |
||
167 | coords[3] = newTin[l][0]; |
||
168 | |||
169 | /* DEBUG */
|
||
170 | System.err.println(coords[3]); |
||
171 | |||
172 | /* BUG */
|
||
173 | //TODO: for some reason, bezierSurface.nextTriangle() only writes invalid triangles into newTin[][]
|
||
174 | //TODO: this leads to gf.createLinearRing() falling over
|
||
175 | final LinearRing ring = gf.createLinearRing(coords);
|
||
176 | m_TrianglesOut.addFeature(gf.createPolygon(ring, null), record);
|
||
177 | indexOfInterpolatedTriangles++; |
||
178 | } |
||
179 | } |
||
180 | return !m_Task.isCanceled();
|
||
181 | } |
||
182 | |||
183 | } |