001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/model/spatialschema/CurveBoundaryImpl.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004    This file is part of deegree.
005    Copyright (C) 2001-2006 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.framework.log.ILogger;
049    import org.deegree.framework.log.LoggerFactory;
050    import org.deegree.model.crs.CoordinateSystem;
051    
052    /**
053     * default implementation of the CurveBoundary interface from
054     * package jago.model. 
055     * 
056     * <p>------------------------------------------------------------</p>
057     * @version 10.6.2001
058     * @author Andreas Poth
059     */
060    
061    class CurveBoundaryImpl extends PrimitiveBoundaryImpl implements CurveBoundary,
062                                                                             Serializable {
063        
064        private ILogger LOG = LoggerFactory.getLogger( CurveBoundaryImpl.class );
065        
066        /** Use serialVersionUID for interoperability. */
067        private final static long serialVersionUID = 4226497939552424434L;
068        private Position ep = null;
069        private Position sp = null;
070    
071        /**
072         * constructor of curve_boundary with CS_CoordinateSystem and startpoint and endpoint
073         */
074        public CurveBoundaryImpl( CoordinateSystem crs, Position sp, Position ep ) {
075            super( crs );
076    
077            this.sp = sp;
078            this.ep = ep;
079    
080            setValid( false );
081        }
082    
083        /**
084        * The operation "dimension" shall return the inherent dimension of this
085        * Geometry, which shall be less than or equal to the coordinate dimension.
086        * The dimension of a collection of geometric objects shall be the largest
087        * dimension of any of its pieces. Points are 0-dimensional, curves are
088        * 1-dimensional, surfaces are 2-dimensional, and solids are 3-dimensional.
089        */
090        public int getDimension() {
091            return 1;
092        }
093    
094        /**
095         * The operation "coordinateDimension" shall return the dimension of the
096         * coordinates that define this Geometry, which must be the same as the
097         * coordinate dimension of the coordinate reference system for this Geometry.
098         */
099        public int getCoordinateDimension() {
100            return getStartPoint().getCoordinateDimension();
101        }
102    
103        /**
104        * returns a shallow copy of the geometry
105        */
106        public Object clone() {
107            CurveBoundary cb = null;
108    
109            try {
110                cb = new CurveBoundaryImpl( getCoordinateSystem(), sp, ep );
111            } catch ( Exception ex ) {
112                LOG.logError( ex.getMessage(), ex );
113            }
114    
115            return cb;
116        }
117    
118        /**
119         * returns the StartPoint of the boundary
120         */
121        public Position getStartPoint() {
122            return sp;
123        }
124    
125        /**
126         * returns the EndPoint of the boundary
127         */
128        public Position getEndPoint() {
129            return ep;
130        }
131    
132        /**
133        * checks if this curve is completly equal to the submitted geometry
134        * @param other object to compare to
135        */
136        public boolean equals( Object other ) {
137            if ( !super.equals( other ) || !( other instanceof CurveBoundaryImpl ) ) {
138                return false;
139            }
140    
141            if ( !ep.equals( ( (CurveBoundary)other ).getEndPoint() ) || 
142                     !sp.equals( ( (CurveBoundary)other ).getStartPoint() ) ) {
143                return false;
144            }
145    
146            return true;
147        }
148    
149        /**
150         * The Boolean valued operation "intersects" shall return TRUE if this Geometry
151         * intersects another Geometry. Within a Complex, the Primitives do not
152         * intersect one another. In general, topologically structured data uses shared
153         * geometric objects to capture intersection information.
154         */
155        public boolean intersects( Geometry gmo ) {
156            boolean inter = false;
157            Point p1 = new PointImpl( sp, crs );
158            Point p2 = new PointImpl( ep, crs );
159    
160            try {
161                if ( gmo instanceof Point ) {
162                    inter = LinearIntersects.intersects( p1, (Point)gmo );
163    
164                    if ( !inter ) {
165                        inter = LinearIntersects.intersects( p2, (Point)gmo );
166                    }
167                } else if ( gmo instanceof Curve ) {
168                    inter = LinearIntersects.intersects( p1, (Curve)gmo );
169    
170                    if ( !inter ) {
171                        inter = LinearIntersects.intersects( p2, (Curve)gmo );
172                    }
173                } else if ( gmo instanceof Surface ) {
174                    inter = LinearIntersects.intersects( p1, (Surface)gmo );
175    
176                    if ( !inter ) {
177                        inter = LinearIntersects.intersects( p2, (Surface)gmo );
178                    }
179                } else if ( gmo instanceof MultiPrimitive ) {
180                    inter = intersectsMultiPrimitive( (MultiPrimitive)gmo );
181                }
182            } catch ( Exception e ) {
183            }
184    
185            return inter;
186        }
187    
188        /**
189         * the operations returns true if the submitted multi primitive intersects
190         * with the curve segment
191         */
192        private boolean intersectsMultiPrimitive( MultiPrimitive mprim ) throws Exception {
193            boolean inter = false;
194    
195            int cnt = mprim.getSize();
196    
197            for ( int i = 0; i < cnt; i++ ) {
198                if ( intersects( mprim.getPrimitiveAt( i ) ) ) {
199                    inter = true;
200                    break;
201                }
202            }
203    
204            return inter;
205        }
206    
207        /**
208         * calculates the envelope of the curve boundary
209         */
210        private void calculateEnvelope() {
211            double[] min = sp.getAsArray().clone();
212            double[] max = ep.getAsArray().clone();
213    
214            for ( int i = 0; i < min.length; i++ ) {
215                if ( min[i] > max[i] ) {
216                    double d = min[i];
217                    min[i] = max[i];
218                    max[i] = d;
219                }
220            }
221    
222            envelope = new EnvelopeImpl( new PositionImpl( min ), new PositionImpl( max ), this.crs );
223        }
224    
225        /**
226         * calculates the envelope of the curve boundary
227         */
228        protected void calculateParam() {
229            calculateEnvelope();
230            setValid( true );
231        }
232    
233        /**
234         *
235         *
236         * @return 
237         */
238        public String toString() {
239            return "point1: [" + sp + "] - point2: [" + ep + "]";
240        }
241    }/* ********************************************************************
242    Changes to this class. What the people have been up to:
243    $Log$
244    Revision 1.13  2006/11/02 10:20:51  mschneider
245    Fixed null CRS in Envelope creation.
246    
247    Revision 1.12  2006/08/07 09:49:50  poth
248    never thrown exception removed
249    
250    Revision 1.11  2006/08/07 09:49:12  poth
251    unneccessary type cast removed
252    
253    Revision 1.10  2006/07/12 14:46:15  poth
254    comment footer added
255    
256    ********************************************************************** */