View Javadoc

1   /* file : Point3D.java
2    * 
3    * Project : geometry
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   * Created on 27 nov. 2005
24   *
25   */
26  
27  package math.geom3d;
28  
29  import javax.vecmath.Tuple3d;
30  
31  import math.geom3d.transform.AffineTransform3D;
32  
33  /**
34   * @author dlegland
35   */
36  public class Point3D implements Shape3D {
37  
38      private double x = 0;
39      private double y = 0;
40      private double z = 0;
41  
42      /**
43       * Initialize at coordinate (0,0,0).
44       */
45      public Point3D() {
46          this(0, 0, 0);
47      }
48  
49      public Point3D(double x, double y, double z) {
50          this.x = x;
51          this.y = y;
52          this.z = z;
53      }
54      
55      public Point3D(Tuple3d point) {
56      	this.x = point.getX();
57      	this.y = point.getY();
58      	this.z = point.getZ();
59      }
60  
61      public double getX() {
62          return x;
63      }
64  
65      public double getY() {
66          return y;
67      }
68  
69      public double getZ() {
70          return z;
71      }
72  
73      public void setX(double x) {
74          this.x = x;
75      }
76  
77      public void setY(double y) {
78          this.y = y;
79      }
80  
81      public void setZ(double z) {
82          this.z = z;
83      }
84  
85      public void setLocation(Point3D point) {
86          x = point.getX();
87          y = point.getY();
88          z = point.getZ();
89      }
90  
91      public void setLocation(double x, double y, double z) {
92          this.x = x;
93          this.y = y;
94          this.z = z;
95      }
96  
97      public double getDistance(Point3D point) {
98          double dx = point.x-x;
99          double dy = point.y-y;
100         double dz = point.z-z;
101 
102         return Math.hypot(Math.hypot(dx, dy), dz);
103     }
104 
105     /**
106      * A point 'contains' another point if their euclidean distance is less than
107      * the accuracy.
108      */
109     public boolean contains(Point3D point) {
110         if (getDistance(point)>ACCURACY)
111             return false;
112         return true;
113     }
114 
115     public boolean isEmpty() {
116         return false;
117     }
118 
119     public boolean isBounded() {
120         return true;
121     }
122 
123     public Box3D getBoundingBox() {
124         return new Box3D(x, x, y, y, z, z);
125     }
126 
127     public Shape3D clip(Box3D box) {
128         if (x<box.getMinX()||x>box.getMaxX())
129             return Shape3D.EMPTY_SET;
130         if (y<box.getMinY()||y>box.getMaxY())
131             return Shape3D.EMPTY_SET;
132         if (z<box.getMinZ()||z>box.getMaxZ())
133             return Shape3D.EMPTY_SET;
134         return this;
135     }
136 
137     public Point3D transform(AffineTransform3D trans) {
138         Point3D res = new Point3D();
139         trans.transformPoint(this, res);
140         return res;
141     }
142 
143     // ===================================================================
144     // methods overriding Object superclass
145 
146     @Override
147     public boolean equals(Object obj) {
148         if (!(obj instanceof Point3D))
149             return false;
150         Point3D point = (Point3D) obj;
151 
152         if (Math.abs(point.x-this.x)>Shape3D.ACCURACY)
153             return false;
154         if (Math.abs(point.y-this.y)>Shape3D.ACCURACY)
155             return false;
156         if (Math.abs(point.z-this.z)>Shape3D.ACCURACY)
157             return false;
158         return true;
159     }
160 }