001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032:
033: package com.vividsolutions.jump.geom;
034:
035: import com.vividsolutions.jts.algorithm.LineIntersector;
036: import com.vividsolutions.jts.algorithm.RobustLineIntersector;
037: import com.vividsolutions.jts.geom.*;
038:
039: /**
040: * Optimized code for spatial predicates
041: * between
042: * {@link LineSegment}s and {@link Envelope}s.
043: */
044: public class LineSegmentEnvelopeIntersector {
045: private static final LineIntersector lineInt = new RobustLineIntersector();
046:
047: public LineSegmentEnvelopeIntersector() {
048: }
049:
050: public boolean touches(LineSegment seg, Envelope env) {
051: return touches(seg.p0, seg.p1, env);
052: }
053:
054: public boolean touches(Coordinate p0, Coordinate p1, Envelope env) {
055: Envelope lineEnv = new Envelope(p0, p1);
056:
057: if (!lineEnv.intersects(env)) {
058: return false;
059: }
060:
061: // now test either endpoint is inside
062: if (env.contains(p0)) {
063: return true;
064: }
065:
066: if (env.contains(p1)) {
067: return true;
068: }
069:
070: // test whether the segment intersects any of the envelope sides
071: // the coordinates of the envelope, in CW order
072: Coordinate env0 = new Coordinate(env.getMinX(), env.getMinY());
073: Coordinate env1 = new Coordinate(env.getMinX(), env.getMaxY());
074: Coordinate env2 = new Coordinate(env.getMaxX(), env.getMaxY());
075: Coordinate env3 = new Coordinate(env.getMaxX(), env.getMinY());
076:
077: lineInt.computeIntersection(p0, p1, env0, env1);
078:
079: if (lineInt.hasIntersection()) {
080: return true;
081: }
082:
083: lineInt.computeIntersection(p0, p1, env1, env2);
084:
085: if (lineInt.hasIntersection()) {
086: return true;
087: }
088:
089: lineInt.computeIntersection(p0, p1, env2, env3);
090:
091: if (lineInt.hasIntersection()) {
092: return true;
093: }
094:
095: lineInt.computeIntersection(p0, p1, env3, env0);
096:
097: if (lineInt.hasIntersection()) {
098: return true;
099: }
100:
101: return false;
102: }
103: }
|