001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/model/spatialschema/PointImpl.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001-2008 by:
006     EXSE, Department of Geography, University of Bonn
007     http://www.giub.uni-bonn.de/deegree/
008     lat/lon GmbH
009     http://www.lat-lon.de
010    
011     This library is free software; you can redistribute it and/or
012     modify it under the terms of the GNU Lesser General Public
013     License as published by the Free Software Foundation; either
014     version 2.1 of the License, or (at your option) any later version.
015    
016     This library is distributed in the hope that it will be useful,
017     but WITHOUT ANY WARRANTY; without even the implied warranty of
018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
019     Lesser General Public License for more details.
020    
021     You should have received a copy of the GNU Lesser General Public
022     License along with this library; if not, write to the Free Software
023     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
024    
025     Contact:
026    
027     Andreas Poth 
028     lat/lon GmbH
029     Aennchenstr. 19
030     53115 Bonn
031     Germany
032     E-Mail: poth@lat-lon.de
033    
034     Prof. Dr. Klaus Greve
035     Department of Geography
036     University of Bonn
037     Meckenheimer Allee 166
038     53115 Bonn
039     Germany
040     E-Mail: greve@giub.uni-bonn.de
041    
042     
043     ---------------------------------------------------------------------------*/
044    package org.deegree.model.spatialschema;
045    
046    import java.io.Serializable;
047    
048    import org.deegree.model.crs.CoordinateSystem;
049    
050    /**
051     * default implementation of the Point interface.
052     * 
053     * <p>
054     * ------------------------------------------------------------
055     * </p>
056     * 
057     * @version 5.6.2001
058     * @author Andreas Poth
059     *         <p>
060     */
061    
062    final class PointImpl extends PrimitiveImpl implements Point, Serializable {
063        /** Use serialVersionUID for interoperability. */
064        private final static long serialVersionUID = 6106017748940535740L;
065    
066        private Position position = null;
067    
068        /**
069         * constructor. initializes a point to the coordinate 0/0
070         * 
071         * @param crs
072         *            spatial reference system of the point
073         */
074        public PointImpl( CoordinateSystem crs ) {
075            super( crs );
076            position = new PositionImpl();
077            empty = true;
078            centroid = this;
079        }
080    
081        /**
082         * constructor for initializing a point within a two-dimensional coordinate system
083         * 
084         * @param x
085         *            x-value of the point
086         * @param y
087         *            y-value of the point
088         * @param crs
089         *            spatial reference system of the point
090         */
091        public PointImpl( double x, double y, CoordinateSystem crs ) {
092            super( crs );
093            position = new PositionImpl( x, y );
094            empty = false;
095            centroid = this;
096        }
097    
098        /**
099         * constructor for initializing a point within a three-dimensional coordinate system
100         * 
101         * @param x
102         *            x-value of the point
103         * @param y
104         *            y-value of the point
105         * @param z
106         *            z-value of the point
107         * @param crs
108         *            spatial reference system of the point
109         */
110        public PointImpl( double x, double y, double z, CoordinateSystem crs ) {
111            super( crs );
112            position = new PositionImpl( x, y, z );
113            empty = false;
114            centroid = this;
115        }
116    
117        /**
118         * constructor
119         * 
120         * @param gmo
121         *            existing GM_Point
122         */
123        public PointImpl( Point gmo ) {
124            super( gmo.getCoordinateSystem() );
125            position = new PositionImpl( gmo.getAsArray() );
126            empty = false;
127            centroid = this;
128        }
129    
130        /**
131         * constructor
132         * 
133         * @param gmo
134         *            existing GM_Point
135         * @param crs
136         *            spatial reference system of the point
137         */
138        public PointImpl( Position gmo, CoordinateSystem crs ) {
139            super( crs );
140            position = gmo;
141            empty = false;
142            centroid = this;
143        }
144    
145        /**
146         * checks if this point is completly equal to the submitted geometry
147         */
148        @Override
149        public boolean equals( Object other ) {
150            if ( super.equals( other ) && ( other instanceof Point ) ) {
151                Point p = (Point) other;
152                boolean flagEq = Math.abs( getX() - p.getX() ) < mute && Math.abs( getY() - p.getY() ) < mute;
153                if ( getCoordinateDimension() == 3 ) {
154                    flagEq = flagEq && Math.abs( getZ() - p.getZ() ) < mute;
155                }
156                return flagEq;
157            }
158    
159            return false;
160        }
161    
162        /**
163         * The operation "dimension" shall return the inherent dimension of this Geometry, which shall
164         * be less than or equal to the coordinate dimension. The dimension of a collection of geometric
165         * objects shall be the largest dimension of any of its pieces. Points are 0-dimensional, curves
166         * are 1-dimensional, surfaces are 2-dimensional, and solids are 3-dimensional.
167         */
168        public int getDimension() {
169            return 0;
170        }
171    
172        /**
173         * The operation "coordinateDimension" shall return the dimension of the coordinates that define
174         * this Geometry, which must be the same as the coordinate dimension of the coordinate reference
175         * system for this Geometry.
176         */
177        public int getCoordinateDimension() {
178            return getPosition().getCoordinateDimension();
179        }
180    
181        /**
182         * returns a shallow copy of the geometry.
183         */
184        @Override
185        public Object clone() {
186            return new PointImpl( this );
187        }
188    
189        /**
190         * returns the x-value of this point
191         */
192        public double getX() {
193            return position.getX();
194        }
195    
196        /**
197         * returns the y-value of this point
198         */
199        public double getY() {
200            return position.getY();
201        }
202    
203        /**
204         * returns the y-value of this point
205         */
206        public double getZ() {
207            return position.getZ();
208        }
209    
210        /**
211         * returns the x- and y-value of the point as a two dimensional array the first field contains
212         * the x- the second field the y-value.
213         */
214        public double[] getAsArray() {
215            return position.getAsArray();
216        }
217    
218        /**
219         * translate the point by the submitted values. the <code>dz</code>- value will be ignored.
220         */
221        @Override
222        public void translate( double[] d ) {
223            setValid( false );
224            position.translate( d );
225        }
226    
227        /**
228         * @return position
229         * 
230         */
231        public Position getPosition() {
232            return position;
233        }
234    
235        /**
236         * The Boolean valued operation "intersects" shall return TRUE if this Geometry intersects
237         * another Geometry. Within a Complex, the Primitives do not intersect one another. In general,
238         * topologically structured data uses shared geometric objects to capture intersection
239         * information.
240         * <p>
241         * </p>
242         * dummy implementation
243         */
244        @Override
245        public boolean intersects( Geometry gmo ) {
246            boolean inter = false;
247    
248            try {
249                if ( gmo instanceof Point ) {
250                    inter = LinearIntersects.intersects( (Point) gmo, this );
251                } else if ( gmo instanceof Curve ) {
252                    inter = LinearIntersects.intersects( this, (Curve) gmo );
253                } else if ( gmo instanceof Surface ) {
254                    inter = LinearIntersects.intersects( this, (Surface) gmo );
255                } else if ( gmo instanceof Aggregate ) {
256                    inter = intersectsAggregate( (Aggregate) gmo );
257                }
258            } catch ( Exception e ) {
259                // ignore
260            }
261    
262            return inter;
263        }
264    
265        /**
266         * the operations returns true if the submitted multi primitive intersects with the curve
267         * segment
268         */
269        private boolean intersectsAggregate( Aggregate mprim )
270                                throws Exception {
271            boolean inter = false;
272    
273            int cnt = mprim.getSize();
274    
275            for ( int i = 0; i < cnt; i++ ) {
276                if ( intersects( mprim.getObjectAt( i ) ) ) {
277                    inter = true;
278                    break;
279                }
280            }
281    
282            return inter;
283        }
284    
285        /**
286         * The Boolean valued operation "contains" shall return TRUE if this Geometry contains another
287         * Geometry.
288         * <p>
289         * </p>
290         */
291        @Override
292        public boolean contains( Geometry gmo ) {
293            throw new NoSuchMethodError( "the contains operation for points " + "isn't supported at the moment." );
294        }
295    
296        /**
297         * recalculates internal parameters
298         */
299        @Override
300        protected void calculateParam() {
301            setValid( true );
302        }
303    
304        @Override
305        public String toString() {
306            StringBuffer ret = new StringBuffer( 30 );
307            ret.append( this.getClass().getName() ).append( ": " );
308    
309            for ( int i = 0; i < getAsArray().length; i++ ) {
310                ret.append( getAsArray()[i] ).append( ' ' );
311            }
312    
313            return ret.toString();
314        }
315    
316    }