Class SweepLineEvent

 1  
 2  
 3  
 4 /*
 5  * Copyright (c) 2016 Vivid Solutions.
 6  *
 7  * All rights reserved. This program and the accompanying materials
 8  * are made available under the terms of the Eclipse Public License 2.0
 9  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
10  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
11  * and the Eclipse Distribution License is available at
12  *
13  * http://www.eclipse.org/org/documents/edl-v10.php.
14  */
15 package org.locationtech.jts.geomgraph.index;
16  
17 /**
18  * @version 1.7
19  */
20 public class SweepLineEvent
21   implements Comparable
22
23   private static final int INSERT = 1;
24   private static final int DELETE = 2;
25  
26   private Object label;    // used for red-blue intersection detection
27   private double xValue;
28   private int eventType;
29   private SweepLineEvent insertEvent = null// null if this is an INSERT event
30   private int deleteEventIndex;
31   private Object obj;
32  
33   /**
34    * Creates an INSERT event.
35    * 
36    * @param label the edge set label for this object
37    * @param x the event location
38    * @param obj the object being inserted
39    */
40   public SweepLineEvent(Object label, double x, Object obj)
41   {
42     this.eventType = INSERT;
43     this.label = label;
44     xValue = x;
45     this.obj = obj;
46   }
47  
48   /**
49    * Creates a DELETE event.
50    * 
51    * @param x the event location
52    * @param insertEvent the corresponding INSERT event
53    */
54   public SweepLineEvent(double x, SweepLineEvent insertEvent)
55   {
56     eventType = DELETE;
57     xValue = x;
58     this.insertEvent = insertEvent;
59   }
60  
61   public boolean isInsert() { return eventType == INSERT; }
62   public boolean isDelete() { return eventType == DELETE; }
63   public SweepLineEvent getInsertEvent() { return insertEvent; }
64   public int getDeleteEventIndex() { return deleteEventIndex; }
65   public void setDeleteEventIndex(int deleteEventIndex) { this.deleteEventIndex = deleteEventIndex; }
66  
67   public Object getObject() { return obj; }
68  
69   public boolean isSameLabel(SweepLineEvent ev)
70   {
71     // no label set indicates single group
72     if (label == nullreturn false;
73     return label == ev.label;
74   }
75   /**
76    * Events are ordered first by their x-value, and then by their eventType.
77    * Insert events are sorted before Delete events, so that
78    * items whose Insert and Delete events occur at the same x-value will be
79    * correctly handled.
80    */
81   public int compareTo(Object o) {
82     SweepLineEvent pe = (SweepLineEvent) o;
83     if (xValue < pe.xValue) return  -1;
84     if (xValue > pe.xValue) return   1;
85     if (eventType < pe.eventType) return  -1;
86     if (eventType > pe.eventType) return   1;
87     return 0;
88   }
89  
90  
91 }
92