Statistics
| Revision:

root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / tin / smoothTinBezier / TriangleDT.java @ 59

History | View | Annotate | Download (9.9 KB)

1
/**
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.awt.geom.GeneralPath;
11
import java.io.Serializable;
12

    
13
import com.vividsolutions.jts.geom.Coordinate;
14
import com.vividsolutions.jts.geom.Envelope;
15
import com.vividsolutions.jts.geom.GeometryFactory;
16
import com.vividsolutions.jts.geom.LineString;
17
import com.vividsolutions.jts.geom.LinearRing;
18
import com.vividsolutions.jts.geom.Polygon;
19
import com.vividsolutions.jts.geom.impl.CoordinateArraySequence;
20

    
21
public class TriangleDT
22
         implements
23
            Serializable {
24
   public Coordinate A;
25
   public Coordinate B;
26
   public Coordinate C;
27
   public boolean    haveBreakLine = false;
28
   public int        typeBreakLine = -1;
29

    
30

    
31
   /******************************************************************************************************************************
32
    * Constructor
33
    * 
34
    * @param T -
35
    *                triangle will be cloned
36
    * 
37
    */
38
   public TriangleDT(final TriangleDT T) {
39
      A = T.A;
40
      B = T.B;
41
      C = T.C;
42
   }
43

    
44

    
45
   /******************************************************************************************************************************
46
    * Constructor
47
    * 
48
    * @param A -
49
    *                first vertex
50
    * @param B -
51
    *                second vertex
52
    * @param C -
53
    *                third vertex
54
    * 
55
    */
56
   public TriangleDT(final Coordinate A,
57
                     final Coordinate B,
58
                     final Coordinate C) {
59
      this.A = A;
60
      this.B = B;
61
      this.C = C;
62
   }
63

    
64

    
65
   public TriangleDT(final Coordinate[] coords) {
66
      this.A = new Coordinate(coords[0].x, coords[0].y, coords[0].z);
67
      this.B = new Coordinate(coords[1].x, coords[1].y, coords[1].z);
68
      this.C = new Coordinate(coords[2].x, coords[2].y, coords[2].z);
69
   }
70

    
71

    
72
   /******************************************************************************************************************************
73
    * implicit Constructor
74
    */
75
   public TriangleDT() {}
76

    
77

    
78
   /******************************************************************************************************************************
79
    * The method which testing, if the line intersect the triangle
80
    * 
81
    * @param newL -
82
    *                Geometry of line
83
    * 
84
    * @return boolean true - line intersect triangle false - line doesn't intersect triangle
85
    */
86
   public boolean containsLine(final LineString newL) {
87
      final Coordinate[] newPoints = { A, B, C, A };
88
      final CoordinateArraySequence newPointsTriangle = new CoordinateArraySequence(newPoints);
89
      final LinearRing trianglesPoints = new LinearRing(newPointsTriangle, new GeometryFactory());
90
      return newL.crosses(trianglesPoints.convexHull());
91
   }
92

    
93

    
94
   protected Coordinate getCentroid() {
95
      return new Coordinate((A.x + B.x + C.x) / 3, (A.y + B.y + C.y) / 3);
96
   }
97

    
98

    
99
   /******************************************************************************************************************************
100
    * The method which testing, if the triangle contains the point
101
    * 
102
    * @param P -
103
    *                point which will be tested
104
    * 
105
    * @return boolean true - the triangle contains the point false - the triangle doesn't contains point
106
    * 
107
    */
108
   public boolean contains(final Coordinate P) {
109
      final GeneralPath triangle = new GeneralPath();
110
      triangle.moveTo((float) A.x, (float) A.y);
111
      triangle.lineTo((float) B.x, (float) B.y);
112
      triangle.lineTo((float) C.x, (float) C.y);
113
      triangle.lineTo((float) A.x, (float) A.y);
114
      triangle.closePath();
115
      return triangle.contains(P.x, P.y);
116
   }
117

    
118

    
119
   /******************************************************************************************************************************
120
    * The method which testing, if the triangle contains the point
121
    * 
122
    * @param P -
123
    *                point which will be tested
124
    * 
125
    * @return boolean true - the triangle contains the point false - the triangle doesn't contains point
126
    * 
127
    */
128
   public boolean containsPointAsVertex(final Coordinate P) {
129
      if (A.equals2D(P) || B.equals2D(P) || C.equals2D(P)) {
130
         return true;
131
      }
132
      else {
133
         return false;
134
      }
135
   }
136

    
137

    
138
   /******************************************************************************************************************************
139
    * The method which testing two triangles, if the triangles have one same point
140
    * 
141
    * @param T -
142
    *                triangle to test
143
    * 
144
    * @return boolean true - the triangles have one same point false - the triangles haven't one same point
145
    * 
146
    */
147
   protected boolean containsOneSamePointWith(final TriangleDT T) {
148
      if (T.A.equals2D(A) || T.A.equals2D(B) || T.A.equals2D(C)) {
149
         return true;
150
      }
151
      if (T.B.equals2D(A) || T.B.equals2D(B) || T.B.equals2D(C)) {
152
         return true;
153
      }
154
      if (T.C.equals2D(A) || T.C.equals2D(B) || T.C.equals2D(C)) {
155
         return true;
156
      }
157
      else {
158
         return false;
159
      }
160
   }
161

    
162

    
163
   /******************************************************************************************************************************
164
    * The method which testing two triangles, if the triangles have two same points
165
    * 
166
    * @param P1 -
167
    *                point for testing
168
    * @param P2 -
169
    *                point for testing
170
    * @return boolean true - the triangles have two same point false - the triangles haven't two same point
171
    * 
172
    */
173
   public boolean containsTwoPoints(final Coordinate P1,
174
                                    final Coordinate P2) {
175
      if ((A.equals2D(P1) || B.equals2D(P1) || C.equals2D(P1)) && (A.equals2D(P2) || B.equals2D(P2) || C.equals2D(P2))) {
176
         return true;
177
      }
178
      return false;
179
   }
180

    
181

    
182
   /******************************************************************************************************************************
183
    * The method for converting to String
184
    * 
185
    * 
186
    */
187
   /*        public void toStringa() {
188
                   //return ("TriangleDT: " + A.toString() + B.toString() + C.toString()+" KEY "+key[0]+" "+key[1]+"  neighbour>"+neighbour_idx[0][0]+ " ");
189
                   System.out.println("--------------------------------------------------------------------");
190
                     System.out.println("TDT: " + A.toString() + B.toString() + C.toString());
191
                     System.out.println(" k:"+typeBreakLine);
192
                   System.out.println("--------------------------------------------------------------------");
193
           }*/
194

    
195
   /******************************************************************************************************************************
196
    * The method which testing triangle, if the triangles is'nt line
197
    * 
198
    * @return boolean true - the triangle is triangle false - the triangle is line
199
    * 
200
    */
201
   protected boolean isTriangle() {
202
      final Coordinate[] newPoint = new Coordinate[4];
203
      newPoint[0] = A;
204
      newPoint[1] = B;
205
      newPoint[2] = C;
206
      newPoint[3] = A;
207
      final CoordinateArraySequence newPointsTriangle = new CoordinateArraySequence(newPoint);
208
      final LinearRing trianglesPoints = new LinearRing(newPointsTriangle, new GeometryFactory());
209
      if (trianglesPoints.convexHull().getGeometryType() == "Polygon") {
210
         return true;
211
      }
212
      else {
213
         return false;
214
      }
215
   }
216

    
217

    
218
   /******************************************************************************************************************************
219
    * The method which comparing two triangles, if the triangles have same coordinates of vertexes
220
    * 
221
    * @param T -
222
    *                triangle to test
223
    * 
224
    * @return boolean true - the triangles are same false - the triangles aren't same
225
    * 
226
    */
227
   public boolean compare(final TriangleDT T) {
228
      if ((T.A.equals2D(A) || T.A.equals2D(B) || T.A.equals2D(C)) && (T.B.equals2D(A) || T.B.equals2D(B) || T.B.equals2D(C))
229
          && (T.C.equals2D(A) || T.C.equals2D(B) || T.C.equals2D(C))) {
230

    
231
         return true;
232
      }
233

    
234
      return false;
235
   }
236

    
237

    
238
   /******************************************************************************************************************************
239
    * The method which compare points
240
    * 
241
    * @param P -
242
    *                points for comparing
243
    * @return index A,B,C which point is same or N if point P not exist in triangle
244
    */
245
   public char compareReturnIndex(final Coordinate P) {
246
      if (P.equals2D(A)) {
247
         return 'A';
248
      }
249
      if (P.equals2D(B)) {
250
         return 'B';
251
      }
252
      if (P.equals2D(C)) {
253
         return 'C';
254
      }
255
      return 'N';
256

    
257
   }
258

    
259

    
260
   /******************************************************************************************************************************
261
    * Protected method for getting envelope of triangle
262
    * 
263
    * @return envelope of triangle
264
    */
265
   public Envelope getEnvelope() {
266
      final Coordinate[] newPoint = new Coordinate[4];
267
      newPoint[0] = A;
268
      newPoint[1] = B;
269
      newPoint[2] = C;
270
      newPoint[3] = A;
271
      final CoordinateArraySequence newPointsTriangle = new CoordinateArraySequence(newPoint);
272

    
273
      final LinearRing trianglesPoints = new LinearRing(newPointsTriangle, new GeometryFactory());
274

    
275
      return trianglesPoints.getEnvelopeInternal();
276
   }
277

    
278

    
279
   public void normalizePolygon() {
280
      Coordinate[] coords = new Coordinate[4];
281
      final GeometryFactory gf = new GeometryFactory();
282
      coords[0] = A;
283
      coords[1] = B;
284
      coords[2] = C;
285
      coords[3] = A;
286
      final LinearRing ring = gf.createLinearRing(coords);
287
      final Polygon poly = gf.createPolygon(ring, null);
288
      poly.normalize();
289
      coords = poly.getCoordinates();
290
      A = coords[0];
291
      B = coords[1];
292
      C = coords[2];
293
   }
294

    
295
}