Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_geometries / src / org / gvsig / fmap / geom / primitive / impl / AbstractPrimitive.java @ 29018

History | View | Annotate | Download (9.78 KB)

1
package org.gvsig.fmap.geom.primitive.impl;
2

    
3
import java.awt.Shape;
4
import java.awt.geom.AffineTransform;
5
import java.awt.geom.PathIterator;
6
import java.awt.geom.Point2D;
7
import java.io.Serializable;
8

    
9
import org.cresques.cts.ICoordTrans;
10
import org.cresques.cts.IProjection;
11
import org.gvsig.fmap.geom.Geometry;
12
import org.gvsig.fmap.geom.GeometryLocator;
13
import org.gvsig.fmap.geom.GeometryManager;
14
import org.gvsig.fmap.geom.handler.Handler;
15
import org.gvsig.fmap.geom.operation.GeometryOperationContext;
16
import org.gvsig.fmap.geom.operation.GeometryOperationException;
17
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
18
import org.gvsig.fmap.geom.primitive.FShape;
19
import org.gvsig.fmap.geom.primitive.GeneralPathX;
20
import org.gvsig.fmap.geom.primitive.Primitive;
21
import org.gvsig.fmap.geom.type.GeometryType;
22

    
23

    
24
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
25
 *
26
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
27
 *
28
 * This program is free software; you can redistribute it and/or
29
 * modify it under the terms of the GNU General Public License
30
 * as published by the Free Software Foundation; either version 2
31
 * of the License, or (at your option) any later version.
32
 *
33
 * This program is distributed in the hope that it will be useful,
34
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36
 * GNU General Public License for more details.
37
 *
38
 * You should have received a copy of the GNU General Public License
39
 * along with this program; if not, write to the Free Software
40
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
41
 *
42
 * For more information, contact:
43
 *
44
 *  Generalitat Valenciana
45
 *   Conselleria d'Infraestructures i Transport
46
 *   Av. Blasco Ib??ez, 50
47
 *   46010 VALENCIA
48
 *   SPAIN
49
 *
50
 *      +34 963862235
51
 *   gvsig@gva.es
52
 *      www.gvsig.gva.es
53
 *
54
 *    or
55
 *
56
 *   IVER T.I. S.A
57
 *   Salamanca 50
58
 *   46005 Valencia
59
 *   Spain
60
 *
61
 *   +34 963163400
62
 *   dac@iver.es
63
 */
64
/* CVS MESSAGES:
65
 *
66
 * $Id: AbstractGeometry.java,v 1.2 2008/03/25 08:47:41 cvs Exp $
67
 * $Log: AbstractGeometry.java,v $
68
 * Revision 1.2  2008/03/25 08:47:41  cvs
69
 * Visitors removed
70
 *
71
 * Revision 1.1  2008/03/12 08:46:20  cvs
72
 * *** empty log message ***
73
 *
74
 *
75
 */
76
/**
77
 * @author Jorge Piera Llodr? (jorge.piera@iver.es)
78
 */
79
public abstract class AbstractPrimitive implements Primitive, FShape, Serializable {
80
        private static final long serialVersionUID = -4334977368955260872L;
81
        protected String id = null;
82
        protected IProjection projection = null;
83
        private static GeometryManager geomManager =  GeometryLocator.getGeometryManager();
84

    
85
        /**
86
         * Constructor without arguments. It is necessary to create
87
         * geometries using the {@link GeometryType}{@link #create()}
88
         * method
89
         */
90
        public AbstractPrimitive() {
91
                super();
92
        }
93

    
94
        public AbstractPrimitive(String id, IProjection projection) {
95
                super();
96
                this.id = id;
97
                this.projection = projection;
98
        }
99

    
100
        public AbstractPrimitive(IProjection projection) {
101
                this(null, projection);
102
        }
103

    
104
        /**
105
         * (non-Javadoc)
106
         * @see com.iver.cit.gvsig.fmap.core.Geometry#getInternalShape()
107
         * @deprecated this Geometry is a Shape.
108
         */
109
        public Shape getInternalShape() {
110
                return this;
111
        }
112

    
113
        /* (non-Javadoc)
114
         * @see org.gvsig.geometries.iso.AbstractGeometry#getId()
115
         */
116
        public String getId() {
117
                return id;
118
        }
119

    
120
        /* (non-Javadoc)
121
         * @see org.gvsig.geometries.iso.AbstractGeometry#getSRS()
122
         */
123
        public IProjection getSRS() {
124
                return projection;
125
        }
126

    
127
        /* (non-Javadoc)
128
         * @see org.gvsig.geometries.iso.AbstractGeometry#transform(org.cresques.cts.IProjection)
129
         */
130
        public AbstractPrimitive transform(IProjection newProjection) {
131
                Geometry newGeom = cloneGeometry();
132
                ICoordTrans coordTrans = projection.getCT(newProjection);
133
                newGeom.reProject(coordTrans);
134
                return (AbstractPrimitive)newGeom;
135
        }
136

    
137

    
138
        /*
139
         * TODO adaptar metodo procedente de UtilFunctions
140
         */
141
        public static void rotateGeom(Geometry geometry, double radAngle, double basex, double basey) {
142
                AffineTransform at = new AffineTransform();
143
                at.rotate(radAngle,basex,basey);
144
                geometry.transform(at);
145

    
146
        }
147

    
148
        /*
149
         * TODO adaptar metodo procedente de UtilFunctions
150
         */
151
        public static void moveGeom(Geometry geometry, double dx, double dy) {
152
        AffineTransform at = new AffineTransform();
153
        at.translate(dx, dy);
154
        geometry.transform(at);
155
        }
156

    
157
        /*
158
         * TODO adaptar metodo procedente de UtilFunctions
159
         */
160
        public static void scaleGeom(Geometry geometry, Point2D basePoint, double sx, double sy) {
161
                AffineTransform at = new AffineTransform();
162
                at.setToTranslation(basePoint.getX(),basePoint.getY());
163
                at.scale(sx,sy);
164
                at.translate(-basePoint.getX(),-basePoint.getY());
165
                geometry.transform(at);
166
        }
167

    
168
        /*
169
         * (non-Javadoc)
170
         * @see org.gvsig.fmap.geom.Geometry#fastIntersects(double, double, double, double)
171
         */
172
        public boolean fastIntersects(double x, double y, double w, double h) {
173
                return intersects(x,y,w,h);
174
        }
175

    
176
        /*
177
         * (non-Javadoc)
178
         * @see org.gvsig.fmap.geom.Geometry#cloneGeometry()
179
         */
180
        public Geometry cloneGeometry() {
181
                return (Geometry)cloneFShape();
182
        }
183

    
184
        /*
185
         * (non-Javadoc)
186
         * @see org.gvsig.fmap.geom.Geometry#getHandlers(int)
187
         */
188
        public Handler[] getHandlers(int type) {
189
                if (type==STRETCHINGHANDLER){
190
                        return getStretchingHandlers();
191
                }else if (type==SELECTHANDLER){
192
                        return getSelectHandlers();
193
                }
194
                return null;
195
        }
196

    
197
        /*
198
         * (non-Javadoc)
199
         * @see org.gvsig.fmap.geom.Geometry#isSimple()
200
         */
201
        public boolean isSimple() {
202
                return true;
203
        }
204

    
205
        /*
206
         * (non-Javadoc)
207
         * @see org.gvsig.fmap.geom.Geometry#invokeOperation(int, org.gvsig.fmap.geom.operation.GeometryOperationContext)
208
         */
209
        public Object invokeOperation(int index, GeometryOperationContext ctx) throws GeometryOperationNotSupportedException, GeometryOperationException {
210
                return geomManager.invokeOperation(index, this, ctx);
211
        }
212

    
213
        /*
214
         * (non-Javadoc)
215
         * @see org.gvsig.fmap.geom.Geometry#invokeOperation(java.lang.String, org.gvsig.fmap.geom.operation.GeometryOperationContext)
216
         */
217
        public Object invokeOperation(String oppName, GeometryOperationContext ctx) throws GeometryOperationNotSupportedException, GeometryOperationException {
218
                return geomManager.invokeOperation(oppName, this, ctx);
219
        }
220

    
221
        /*
222
         * (non-Javadoc)
223
         * @see java.lang.Comparable#compareTo(T)
224
         */
225
        public int compareTo(Object arg0) {
226
                // TODO Auto-generated method stub
227
                return -1;
228
        }
229

    
230
        /*
231
         * (non-Javadoc)
232
         * @see java.lang.Object#toString()
233
         */
234
        public String toString() {
235
                String name=getGeometryType().getName();
236
                return name.substring(name.lastIndexOf(".")+1);
237
        }
238

    
239
        /*
240
         * (non-Javadoc)
241
         * @see java.lang.Object#equals(java.lang.Object)
242
         */
243
        public boolean equals(Object obj) {
244
                if (obj == null) {
245
                        return false;
246
                }
247
                if (this.getClass() != obj.getClass()) {
248
                        return false;
249
                }
250

    
251
                AbstractPrimitive other = (AbstractPrimitive) obj;
252
                if (this.getGeometryType().getType() != other.getGeometryType()
253
                                .getType()) {
254
                        return false;
255

    
256
                }
257
                if (this.getGeometryType().getSubType() != other.getGeometryType()
258
                                .getSubType()) {
259
                        return false;
260

    
261
                }
262
                if (this.projection != other.projection) {
263
                        if (this.projection == null) {
264
                                return false;
265
                        }
266
                        if (this.projection.getAbrev() != other.projection.getAbrev()) { //FIXME this must be false
267
                                return false;
268
                        }
269
                }
270
                if (!this.getBounds().equals(other.getBounds())) {
271
                        return false;
272
                }
273

    
274

    
275
                GeneralPathX myPath = this.getGeneralPath();
276
                GeneralPathX otherPath = other.getGeneralPath();
277
                if (myPath == null) {
278
                        if (otherPath != null) {
279
                                return false;
280
                        } else {
281
                                // TODO checkThis
282
                                return true;
283
                        }
284

    
285
                }
286
                if (myPath.getNumTypes() != otherPath.getNumTypes()) {
287
                        return false;
288
                }
289
                if (Math.abs(myPath.getNumCoords() - otherPath.getNumCoords()) > this
290
                                .getDimension()) {
291
                        return false;
292
                }
293
                PathIterator myIter = myPath.getPathIterator(null);
294
                PathIterator otherIter = otherPath.getPathIterator(null);
295
                int myType,otherType;
296
                // FIXME when 3D, 2DM and 3DM
297
                double[] myData = new double[6];
298
                double[] otherData = new double[6];
299
                double[] myFirst = new double[] { myPath.getPointCoords()[0],myPath.getPointCoords()[1]};
300
                double[] otherFirst = new double[] { otherPath.getPointCoords()[0],otherPath.getPointCoords()[1]};
301

    
302
                while (!myIter.isDone()) {
303
                        if (otherIter.isDone()) {
304
                                return false;
305
                        }
306
                        for (int i = 0; i < myData.length; i++) {
307
                                myData[i] = 0.0;
308
                                otherData[i] = 0.0;
309
                        }
310

    
311
                        myType = myIter.currentSegment(myData);
312
                        otherType = otherIter.currentSegment(otherData);
313

    
314
                        switch (myType) {
315
                        case PathIterator.SEG_LINETO:
316
                                if (otherType != myType) {
317
                                        if (otherType == PathIterator.SEG_CLOSE){
318
                                                if (!comparePathIteratorData(otherFirst, myData)) {
319
                                                        return false;
320
                                                }
321
                                        } else {
322
                                                return false;
323
                                        }
324
                                } else {
325
                                        if (!comparePathIteratorData(myData, otherData)) {
326
                                                return false;
327
                                        }
328
                                }
329
                                break;
330

    
331

    
332
                        case PathIterator.SEG_CLOSE:
333
                                if (otherType != myType) {
334
                                        if (otherType == PathIterator.SEG_LINETO) {
335
                                                if (!comparePathIteratorData(myFirst, otherData)) {
336
                                                        return false;
337
                                                }
338
                                        } else {
339
                                                return false;
340
                                        }
341
                                } else {
342
                                        if (!comparePathIteratorData(myData, otherData)) {
343
                                                return false;
344
                                        }
345
                                }
346
                                break;
347

    
348

    
349

    
350
                        case PathIterator.SEG_MOVETO:
351
                        case PathIterator.SEG_QUADTO:
352
                        case PathIterator.SEG_CUBICTO:
353
                                if (otherType != myType) {
354
                                        return false;
355
                                }
356
                                if (!comparePathIteratorData(myData, otherData)) {
357
                                        return false;
358
                                }
359
                                break;
360

    
361
                        } // end switch
362

    
363

    
364
                        myIter.next();
365
                        otherIter.next();
366
                }
367
                if (!otherIter.isDone()) {
368
                        return false;
369
                }
370
                return true;
371
        }
372

    
373
        private boolean comparePathIteratorData(double[] org, double[] other) {
374
                for (int i = 0; i < org.length; i++) {
375
                        if (Math.abs(org[i] - other[i]) > 0.0000001) {
376
                                return false;
377
                        }
378
                }
379
                return true;
380
        }
381

    
382
        /* (non-Javadoc)
383
         * @see org.gvsig.fmap.geom.primitive.FShape#getShapeType()
384
         */
385
        public int getShapeType() {
386
                return getType();
387
        }
388
        
389
        
390
}