1
2
3
4
5 package math.geom3d.line;
6
7 import java.util.ArrayList;
8 import java.util.Collection;
9
10 import math.geom2d.line.StraightLine2D;
11 import math.geom3d.Box3D;
12 import math.geom3d.Point3D;
13 import math.geom3d.Shape3D;
14 import math.geom3d.Vector3D;
15 import math.geom3d.curve.ContinuousCurve3D;
16 import math.geom3d.curve.Curve3D;
17 import math.geom3d.plane.Plane3D;
18 import math.geom3d.transform.AffineTransform3D;
19
20
21
22
23 public class StraightLine3D implements ContinuousCurve3D {
24
25
26
27
28 protected double x0 = 0;
29 protected double y0 = 0;
30 protected double z0 = 0;
31 protected double dx = 1;
32 protected double dy = 0;
33 protected double dz = 0;
34
35
36
37
38 public StraightLine3D() {
39 }
40
41 public StraightLine3D(Point3D origin, Vector3D direction) {
42 this.x0 = origin.getX();
43 this.y0 = origin.getY();
44 this.z0 = origin.getZ();
45 this.dx = direction.getX();
46 this.dy = direction.getY();
47 this.dz = direction.getZ();
48 }
49
50
51
52
53
54
55
56 public StraightLine3D(Point3D p1, Point3D p2) {
57 this(p1, new Vector3D(p1, p2));
58 }
59
60 public StraightLine3D(double x0, double y0, double z0, double dx,
61 double dy, double dz) {
62 this.x0 = x0;
63 this.y0 = y0;
64 this.z0 = z0;
65 this.dx = dx;
66 this.dy = dy;
67 this.dz = dz;
68 }
69
70
71
72
73 public Point3D getOrigin() {
74 return new Point3D(x0, y0, z0);
75 }
76
77 public Point3D getExamplePoint1() {
78 return getOrigin();
79 }
80
81 public Point3D getExamplePoint2() {
82 return new Point3D(x0+dx, y0+dy, z0+dz);
83 }
84
85 public Vector3D getDirection() {
86 return new Vector3D(dx, dy, dz);
87 }
88
89 public StraightLine3D project(Plane3D plane) {
90 return new StraightLine3D(plane.projectPoint(getExamplePoint1()), plane.projectPoint(getExamplePoint2()));
91 }
92
93 public Point3D projectPoint(Point3D point) {
94 return getPoint(project(point));
95 }
96
97
98
99
100
101
102
103
104
105 public Shape3D clip(Box3D box) {
106
107 return null;
108 }
109
110
111
112
113
114
115 public boolean contains(Point3D point) {
116 return this.getDistance(point)<Shape3D.ACCURACY;
117 }
118
119 public boolean isEmpty() {
120 return false;
121 }
122
123 public boolean isBounded() {
124 return false;
125 }
126
127
128
129
130
131
132 public Box3D getBoundingBox() {
133 Vector3D v = this.getDirection();
134
135
136 if (Math.hypot(v.getY(), v.getZ())<Shape3D.ACCURACY)
137 return new Box3D(x0, x0, Double.NEGATIVE_INFINITY,
138 Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY,
139 Double.POSITIVE_INFINITY);
140
141
142 if (Math.hypot(v.getX(), v.getZ())<Shape3D.ACCURACY)
143 return new Box3D(Double.NEGATIVE_INFINITY,
144 Double.POSITIVE_INFINITY, y0, y0, Double.NEGATIVE_INFINITY,
145 Double.POSITIVE_INFINITY);
146
147
148 if (Math.hypot(v.getX(), v.getY())<Shape3D.ACCURACY)
149 return new Box3D(Double.NEGATIVE_INFINITY,
150 Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY,
151 Double.POSITIVE_INFINITY, z0, z0);
152
153 return new Box3D(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY,
154 Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY,
155 Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
156 }
157
158
159
160
161
162
163 public double getDistance(Point3D p) {
164 Vector3D vl = this.getDirection();
165 Vector3D vp = new Vector3D(this.getOrigin(), p);
166 return Vector3D.crossProduct(vl, vp).getNorm()/vl.getNorm();
167 }
168
169
170
171
172
173
174 public StraightLine3D transform(AffineTransform3D trans) {
175 return new StraightLine3D(getOrigin().transform(trans), getDirection().transform(trans));
176 }
177
178 public Point3D getFirstPoint() {
179 return new Point3D(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
180 }
181
182 public Point3D getLastPoint() {
183 return new Point3D(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
184 }
185
186 public Point3D getPoint(double t) {
187 return getPoint(t, new Point3D());
188 }
189
190 public Point3D getPoint(double t, Point3D point) {
191 if (point==null)
192 point = new Point3D();
193 point.setX(x0+t*dx);
194 point.setY(y0+t*dy);
195 point.setZ(z0+t*dz);
196 return point;
197 }
198
199 public double getPosition(Point3D point) {
200 return project(point);
201 }
202
203 public StraightLine3D getReverseCurve() {
204 return new StraightLine3D(getOrigin(), getDirection().getOpposite());
205 }
206
207
208
209
210 public Collection<Point3D> getSingularPoints() {
211 return new ArrayList<Point3D>(0);
212 }
213
214 public Curve3D getSubCurve(double t0, double t1) {
215
216 return null;
217 }
218
219
220
221
222 public double getT0() {
223 return Double.NEGATIVE_INFINITY;
224 }
225
226
227
228
229 public double getT1() {
230 return Double.POSITIVE_INFINITY;
231 }
232
233
234
235
236
237 public double project(Point3D point) {
238 Vector3D vl = this.getDirection();
239 Vector3D vp = new Vector3D(this.getOrigin(), point);
240 return Vector3D.dotProduct(vl, vp)/vl.getNormSq();
241 }
242
243 public Collection<StraightLine3D> getContinuousCurves() {
244 ArrayList<StraightLine3D> array = new ArrayList<StraightLine3D>(1);
245 array.add(this);
246 return array;
247 }
248 }