generated from agdturner/ccg-v2d
/
V3D_V.java
293 lines (263 loc) · 8.31 KB
/
V3D_V.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* Copyright 2020 Andy Turner, University of Leeds.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package uk.ac.leeds.ccg.v3d.geometry.light;
import ch.obermuhlner.math.big.BigRational;
import java.io.Serializable;
import java.math.RoundingMode;
import java.util.Objects;
import uk.ac.leeds.ccg.math.number.Math_BigRationalSqrt;
import uk.ac.leeds.ccg.v3d.geometry.V3D_Point;
import uk.ac.leeds.ccg.v3d.geometry.V3D_Vector;
/**
* Intended for use as a vector or a coordinate. The idea is that this is
* lightweight despite using heavyweight numbers. It is not as heavyweight as
* V3D_Vector which uses even more heavyweight Math_BigRationalSqrt numbers
* for coordinates.
*
* @author Andy Turner
* @version 1.0
*/
public class V3D_V implements Serializable {
private static final long serialVersionUID = 1L;
/**
* The x.
*/
public BigRational x;
/**
* The y.
*/
public BigRational y;
/**
* The z.
*/
public BigRational z;
/**
* The origin {@code <0,0,0>}.
*/
public static final V3D_V ZERO = new V3D_V(0, 0, 0);
/**
* @param v Used to construct this.
*/
public V3D_V(V3D_V v) {
this.x = v.x;
this.y = v.y;
this.z = v.z;
}
/**
* @param x What {@link #x} is set to.
* @param y What {@link #y} is set to.
* @param z What {@link #z} is set to.
*/
public V3D_V(BigRational x, BigRational y, BigRational z) {
this.x = x;
this.y = y;
this.z = z;
}
/**
* @param dx What {@link #x} is set to after conversion.
* @param dy What {@link #y} is set to after conversion.
* @param dz What {@link #z} is set to after conversion.
*/
public V3D_V(long dx, long dy, long dz) {
this(BigRational.valueOf(dx), BigRational.valueOf(dy),
BigRational.valueOf(dz));
}
/**
* @param dx What {@link #x} is set to after conversion.
* @param dy What {@link #y} is set to after conversion.
* @param dz What {@link #z} is set to after conversion.
*/
public V3D_V(double dx, double dy, double dz) {
this(BigRational.valueOf(dx), BigRational.valueOf(dy),
BigRational.valueOf(dz));
}
/**
* @param p A point to construct from.
* @param oom The Order of Magnitude for the precision.
* @param rm The RoundingMode if rounding is needed.
*/
public V3D_V(V3D_Point p, int oom, RoundingMode rm) {
this(p.getX(oom, rm), p.getY(oom, rm), p.getZ(oom, rm));
}
/**
* @param v A vector to construct from.
* @param oom The Order of Magnitude for the precision.
* @param rm The RoundingMode if rounding is needed.
*/
public V3D_V(V3D_Vector v, int oom, RoundingMode rm) {
this(v.getDX(oom, rm), v.getDY(oom, rm), v.getDZ(oom, rm));
}
/**
* Effectively this becomes a vector of the difference. What needs to be
* applied to p to get q.
*
* @param p The start.
* @param q The end.
*/
public V3D_V(V3D_V p, V3D_V q) {
this(q.x.subtract(p.x), q.y.subtract(p.y), q.z.subtract(p.z));
}
@Override
public String toString() {
return toString("");
}
/**
* @param pad A padding of spaces.
* @return A description of this.
*/
public String toString(String pad) {
return this.getClass().getSimpleName() + "\n"
+ pad + "(\n"
+ toStringFields(pad + " ") + "\n"
+ pad + ")";
}
/**
* @param pad A padding of spaces.
* @return A description of the fields.
*/
protected String toStringFields(String pad) {
return pad + "x=" + x + ",\n"
+ pad + "y=" + y + ",\n"
+ pad + "z=" + z;
}
@Override
public boolean equals(Object o) {
if (o instanceof V3D_V v) {
return equals(v);
}
return false;
}
/**
* Indicates if {@code this} and {@code v} are equal.
*
* @param v The instance to test for equality with {@code this}.
* @return {@code true} iff {@code this} is the same as {@code v}.
*/
public boolean equals(V3D_V v) {
return x.compareTo(v.x) == 0 && y.compareTo(v.y) == 0
&& z.compareTo(v.z) == 0;
}
@Override
public int hashCode() {
int hash = 3;
hash = 23 * hash + Objects.hashCode(this.x);
hash = 23 * hash + Objects.hashCode(this.y);
hash = 23 * hash + Objects.hashCode(this.z);
return hash;
}
/**
* @return {@code true} if {@code this} equals {@link #ZERO}.
*/
public boolean isZero() {
return this.equals(ZERO);
}
/**
* Scale by multiplication.
*
* @param s The scalar value to multiply this by.
*/
public void multiply(BigRational s) {
x = x.multiply(s);
y = y.multiply(s);
z = z.multiply(s);
}
/**
* Scale by division.
*
* @param s The scalar value to divide this by.
*/
public void divide(BigRational s) {
x = x.divide(s);
y = y.divide(s);
z = z.divide(s);
}
/**
* Add v to this.
*
* @param v The coordinate to apply.
*/
public void translate(V3D_V v) {
x = x.add(v.x);
y = y.add(v.y);
z = z.add(v.z);
}
/**
* @param oom The Order of Magnitude for the precision.
* @param rm The RoundingMode if rounding is needed.
* @return The magnitude.
*/
public BigRational getMagnitude(int oom, RoundingMode rm) {
return new Math_BigRationalSqrt(x.pow(2).add(y.pow(2))
.add(z.pow(2)), oom, rm).getSqrt(oom, rm);
}
/**
* Calculate and return the
* <A href="https://en.wikipedia.org/wiki/Cross_product">cross product</A>.
* Treat this as the first vector and {@code v} as the second vector. The
* resulting vector is in the direction given by the right hand rule.
*
* @param v V3D_V
* @return The CrossProduct.
*/
public V3D_V getCrossProduct(V3D_V v) {
return new V3D_V(
y.multiply(v.z).subtract(z.multiply(v.y)),
z.multiply(v.x).subtract(x.multiply(v.z)),
x.multiply(v.y).subtract(v.x.multiply(y)));
}
/**
* Scales by the magnitude to give a unit vector. (N.B. There is no check to
* be sure that the resulting vector has a magnitude of less than 1).
*
* @param oom The Order of Magnitude for the precision.
* @param rm The RoundingMode if rounding is needed.
* @return this scaled by the magnitude.
*/
public V3D_V getUnitVector(int oom, RoundingMode rm) {
BigRational d = getMagnitude(oom, RoundingMode.UP);
V3D_V r = new V3D_V(x.divide(d), y.divide(d), z.divide(d));
return r;
}
/**
* Calculate and return the
* <A href="https://en.wikipedia.org/wiki/Dot_product">dot product</A>.
*
* @param v The other vector.
* @return dot product
*/
public BigRational getDotProduct(V3D_V v) {
return (v.x.multiply(x)).add(v.y.multiply(y)).add(v.z.multiply(z));
}
/**
* Get the distance between this and {@code p} assuming both are points.
*
* @param p A point.
* @param oom The Order of Magnitude for the precision.
* @param rm The RoundingMode if rounding is needed.
* @return The distance from {@code p} to this.
*/
public BigRational getDistance(V3D_V p, int oom, RoundingMode rm) {
return new Math_BigRationalSqrt(x.subtract(p.x).pow(2)
.add(y.subtract(p.y).pow(2))
.add(y.subtract(p.y).pow(2)), oom, rm).getSqrt(oom, rm);
}
/**
* @return A new vector which is the opposite to {@code this}.
*/
public V3D_V reverse() {
return new V3D_V(x.negate(), y.negate(), z.negate());
}
}