View Javadoc

1   /* File Domain2D.java 
2    *
3    * Project : Java Geometry Library
4    *
5    * ===========================================
6    * 
7    * This library is free software; you can redistribute it and/or modify it 
8    * under the terms of the GNU Lesser General Public License as published by
9    * the Free Software Foundation, either version 2.1 of the License, or (at
10   * your option) any later version.
11   *
12   * This library is distributed in the hope that it will be useful, but 
13   * WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
14   * or FITNESS FOR A PARTICULAR PURPOSE.
15   *
16   * See the GNU Lesser General Public License for more details.
17   *
18   * You should have received a copy of the GNU Lesser General Public License
19   * along with this library. if not, write to :
20   * The Free Software Foundation, Inc., 59 Temple Place, Suite 330,
21   * Boston, MA 02111-1307, USA.
22   * 
23   * author : Legland
24   * Created on 18 sept. 2004
25   */
26  
27  package math.geom2d.domain;
28  
29  import java.awt.Graphics2D;
30  
31  import math.geom2d.AffineTransform2D;
32  import math.geom2d.Box2D;
33  import math.geom2d.Point2D;
34  
35  /**
36   * A domain defined from its boundary. The boundary curve must be correctly
37   * oriented, non self intersecting, and clearly separating interior and
38   * exterior.
39   * <p>
40   * All contains and intersect tests are computed from the signed distance of the
41   * boundary curve.
42   * 
43   * @author Legland
44   */
45  public class GenericDomain2D implements Domain2D {
46  
47      protected Boundary2D boundary = null;
48  
49      public GenericDomain2D(Boundary2D boundary) {
50          this.boundary = boundary;
51      }
52  
53      // ===================================================================
54      // methods implementing the Domain2D interface
55  
56      public Boundary2D getBoundary() {
57          return boundary;
58      }
59  
60      public Domain2D complement() {
61          return new GenericDomain2D((Boundary2D)boundary.getReverseCurve());
62      }
63  
64      // ===================================================================
65      // methods implementing the Shape2D interface
66  
67      public double getDistance(java.awt.geom.Point2D p) {
68          return Math.max(boundary.getSignedDistance(p.getX(), p.getY()), 0);
69      }
70  
71      public double getDistance(double x, double y) {
72          return Math.max(boundary.getSignedDistance(x, y), 0);
73      }
74  
75      /**
76       * Returns true if the domain is bounded. The domain is unbounded if either
77       * its boundary is unbounded, or a point located outside of the boundary
78       * bounding box is located inside of the domain.
79       */
80      public boolean isBounded() {
81          // If boundary is not bounded, the domain is not bounded.
82          if (!boundary.isBounded())
83              return false;
84  
85          // If boundary is bounded, get the bounding box, choose a point
86          // outside of the box, and check if its belongs to the domain.
87          Box2D box = boundary.getBoundingBox();
88          Point2D point = new Point2D(box.getMinX(), box.getMinY());
89  
90          return !boundary.isInside(point);
91      }
92  
93      public boolean isEmpty() {
94          return boundary.isEmpty()&&!this.contains(0, 0);
95      }
96  
97      public Domain2D clip(Box2D box) {
98          return new GenericDomain2D(
99          		Boundary2DUtils.clipBoundary(this.getBoundary(), box));
100     }
101 
102     /**
103      * If the domain is bounded, returns the bounding box of its boundary,
104      * otherwise returns an infinite bounding box.
105      */
106     public Box2D getBoundingBox() {
107         if (this.isBounded())
108             return boundary.getBoundingBox();
109         return new Box2D(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY,
110                 Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
111     }
112 
113     /**
114      * Returns a new domain which is created from the transformed domain of this
115      * boundary.
116      */
117     public GenericDomain2D transform(AffineTransform2D trans) {
118         Boundary2D transformed = (Boundary2D)boundary.transform(trans);
119         if (!trans.isDirect())
120             transformed = (Boundary2D)transformed.getReverseCurve();
121         return new GenericDomain2D(transformed);
122     }
123 
124     public boolean contains(double x, double y) {
125         return boundary.getSignedDistance(x, y)<=0;
126     }
127 
128     // ===================================================================
129     // methods implementing the Shape interface
130 
131     public boolean contains(java.awt.geom.Point2D p) {
132         return contains(p.getX(), p.getY());
133     }
134 
135     public void draw(Graphics2D g2) {
136         boundary.draw(g2);
137     }
138 
139     public void fill(Graphics2D g2) {
140         boundary.fill(g2);
141     }
142 }