1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.geometry.euclidean.threed.line;
18
19 import org.apache.commons.geometry.core.Transform;
20 import org.apache.commons.geometry.euclidean.threed.Bounds3D;
21 import org.apache.commons.geometry.euclidean.threed.Vector3D;
22 import org.apache.commons.numbers.core.Precision;
23
24
25
26
27
28
29
30
31 public final class Segment3D extends LineConvexSubset3D {
32
33
34 private final double start;
35
36
37 private final double end;
38
39
40
41
42
43
44
45 Segment3D(final Line3D line, final Vector3D startPoint, final Vector3D endPoint) {
46 this(line, line.abscissa(startPoint), line.abscissa(endPoint));
47 }
48
49
50
51
52
53
54
55 Segment3D(final Line3D line, final double start, final double end) {
56 super(line);
57
58 this.start = start;
59 this.end = end;
60 }
61
62
63
64
65
66 @Override
67 public boolean isInfinite() {
68 return false;
69 }
70
71
72
73
74
75 @Override
76 public boolean isFinite() {
77 return true;
78 }
79
80
81 @Override
82 public Vector3D getStartPoint() {
83 return getLine().toSpace(start);
84 }
85
86
87 @Override
88 public double getSubspaceStart() {
89 return start;
90 }
91
92
93 @Override
94 public Vector3D getEndPoint() {
95 return getLine().toSpace(end);
96 }
97
98
99 @Override
100 public double getSubspaceEnd() {
101 return end;
102 }
103
104
105 @Override
106 public double getSize() {
107 return end - start;
108 }
109
110
111 @Override
112 public Vector3D getCentroid() {
113 return getLine().toSpace((0.5 * (end - start)) + start);
114 }
115
116
117 @Override
118 public Bounds3D getBounds() {
119 return Bounds3D.builder()
120 .add(getStartPoint())
121 .add(getEndPoint())
122 .build();
123 }
124
125
126 @Override
127 public Segment3D transform(final Transform<Vector3D> transform) {
128 final Vector3D t1 = transform.apply(getStartPoint());
129 final Vector3D t2 = transform.apply(getEndPoint());
130
131 final Line3D tLine = getLine().transform(transform);
132
133 return new Segment3D(tLine, t1, t2);
134 }
135
136
137 @Override
138 public String toString() {
139 final StringBuilder sb = new StringBuilder();
140 sb.append(getClass().getSimpleName())
141 .append("[startPoint= ")
142 .append(getStartPoint())
143 .append(", endPoint= ")
144 .append(getEndPoint())
145 .append(']');
146
147 return sb.toString();
148 }
149
150
151 @Override
152 boolean containsAbscissa(final double abscissa) {
153 final Precision.DoubleEquivalence precision = getLine().getPrecision();
154 return precision.gte(abscissa, start) &&
155 precision.lte(abscissa, end);
156 }
157 }