/
Location.java
488 lines (437 loc) · 16.5 KB
/
Location.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
* Copyright (C) 2011 Jason von Nieda <jason@vonnieda.org>
*
* This file is part of OpenPnP.
*
* OpenPnP is free software: you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* OpenPnP is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with OpenPnP. If not, see
* <http://www.gnu.org/licenses/>.
*
* For more information about OpenPnP visit http://openpnp.org
*/
package org.openpnp.model;
import java.util.Locale;
import org.simpleframework.xml.Attribute;
/**
* A Location is a an immutable 3D point in X, Y, Z space with a rotation component. The rotation is
* applied about the Z axis.
*/
public class Location {
/*
* The fields on this class would be final in a perfect world, but that doesn't work correctly
* with the XML serialization.
*/
@Attribute
private LengthUnit units;
@Attribute(required = false)
private double x;
@Attribute(required = false)
private double y;
@Attribute(required = false)
private double z;
@Attribute(required = false)
private double rotation;
/**
* Only used by XML serialization.
*/
@SuppressWarnings("unused")
private Location() {
this(null);
}
public Location(LengthUnit units) {
this(units, 0, 0, 0, 0);
}
public Location(LengthUnit units, double x, double y, double z, double rotation) {
this.units = units;
this.x = x;
this.y = y;
this.z = z;
this.rotation = rotation;
}
static final public Location origin = new Location(LengthUnit.Millimeters);
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getZ() {
return z;
}
public double getRotation() {
return rotation;
}
public LengthUnit getUnits() {
return units;
}
public Location convertToUnits(LengthUnit units) {
Location location =
new Location(units, new Length(x, this.units).convertToUnits(units).getValue(),
new Length(y, this.units).convertToUnits(units).getValue(),
new Length(z, this.units).convertToUnits(units).getValue(), rotation);
return location;
}
public Length getLinearLengthTo(Location location) {
double distance = getLinearDistanceTo(location);
return new Length(distance, getUnits());
}
public Length getXyzLengthTo(Location location) {
double distance = getXyzDistanceTo(location);
return new Length(distance, getUnits());
}
/**
* Returns the distance between this Location and the specified Location in the units of this
* Location.
*
* @param location
* @return
*/
public double getLinearDistanceTo(Location location) {
location = location.convertToUnits(getUnits());
return getLinearDistanceTo(location.getX(), location.getY());
}
public double getLinearDistanceTo(double x, double y) {
return (Math.sqrt(Math.pow(this.x - x, 2) + Math.pow(this.y - y, 2)));
}
public double getXyzDistanceTo(Location location) {
location = location.convertToUnits(getUnits());
return (Math.sqrt(Math.pow(this.x - location.getX(), 2)
+ Math.pow(this.y - location.getY(), 2) + Math.pow(this.z - location.getZ(), 2)));
}
public double getXyzcDistanceTo(Location location) {
location = location.convertToUnits(getUnits());
return (Math.sqrt(
Math.pow(this.x - location.getX(), 2)
+ Math.pow(this.y - location.getY(), 2)
+ Math.pow(this.z - location.getZ(), 2)
+ Math.pow((this.rotation - location.getRotation()) % 360, 2)));
}
/**
* Returns the distance between this Location and the infinite line defined by the Locations a and b,
* in the units of this Location.
* From http://www.ahristov.com/tutorial/geometry-games/point-line-distance.html
* @return
*/
public double getLinearDistanceToLine(Location A, Location B) {
A = A.convertToUnits(getUnits());
B = B.convertToUnits(getUnits());
double normalLength = Math.sqrt((B.x - A.x) * (B.x - A.x) + (B.y - A.y) * (B.y - A.y));
return Math.abs((this.x - A.x) * (B.y - A.y) - (this.y - A.y) * (B.x - A.x)) / normalLength;
}
/**
* Returns the distance between this Location and the line segment defined by the Locations a and b,
* in the units of this Location.
* From Real Time Collision Detection p/130
* @return
*/
public double getLinearDistanceToLineSegment(Location A, Location B) {
A = A.convertToUnits(getUnits());
B = B.convertToUnits(getUnits());
org.opencv.core.Point ab = new org.opencv.core.Point(B.x - A.x, B.y - A.y);
org.opencv.core.Point ap = new org.opencv.core.Point(this.x - A.x, this.y - A.y);
org.opencv.core.Point bp = new org.opencv.core.Point(this.x - B.x, this.y - B.y);
// Handle cases where P projects outside of AB
double e = ap.dot(ab);
if (e <= 0.0) {
return Math.sqrt(ap.dot(ap));
}
double f = ab.dot(ab);
if (e >= f) {
return Math.sqrt(bp.dot(bp));
}
// P projects onto AB
return Math.sqrt(ap.dot(ap) - e * e / f);
}
public Length getLengthX() {
return new Length(x, units);
}
public Length getLengthY() {
return new Length(y, units);
}
public Length getLengthZ() {
return new Length(z, units);
}
/**
* Returns a new Location with the given Location's X, Y, and Z components subtracted from this
* Location's X, Y, and Z components. Rotation is left unchanged.
*
* @param l
* @return
*/
public Location subtract(Location l) {
l = l.convertToUnits(getUnits());
return new Location(l.getUnits(), x - l.getX(), y - l.getY(), z - l.getZ(), getRotation());
}
/**
* Same as {@link Location#subtract(Location)} but also subtracts rotation.
*
* @param l
* @return
*/
public Location subtractWithRotation(Location l) {
l = l.convertToUnits(getUnits());
return new Location(l.getUnits(), x - l.getX(), y - l.getY(), z - l.getZ(),
rotation - l.getRotation());
}
/**
* Returns a new Location with the given Location's X, Y, and Z components added to this
* Location's X, Y, and Z components. Rotation is left unchanged.
*
* @param l
* @return
*/
public Location add(Location l) {
l = l.convertToUnits(getUnits());
return new Location(l.getUnits(), x + l.getX(), y + l.getY(), z + l.getZ(), rotation);
}
/**
* Returns a new Location with the given Location's X, Y, and Z components added to this
* Location's X, Y, and Z components. Rotation is included.
*
* @param l
* @return
*/
public Location addWithRotation(Location l) {
l = l.convertToUnits(getUnits());
return new Location(l.getUnits(), x + l.getX(), y + l.getY(), z + l.getZ(),
rotation + l.getRotation());
}
/**
* Returns a new Location with the given Location's X, Y and Z components multiplied by this
* Location's X, Y and Z components. Rotation is left unchanged.
*
* @param l
* @return
*/
public Location multiply(Location l) {
l = l.convertToUnits(getUnits());
return new Location(l.getUnits(), x * l.getX(), y * l.getY(), z * l.getZ(), getRotation());
}
/**
* Returns a new Location based on this Location with values multiplied by the specified values.
* Units are the same as this Location.
*
* @param x
* @param y
* @param z
* @param rotation
* @return
*/
public Location multiply(double x, double y, double z, double rotation) {
return new Location(getUnits(), x * getX(), y * getY(), z * getZ(),
rotation * getRotation());
}
/**
* Returns a new Location based on this Location with values multiplied by the specified factor.
* Units are the same as this Location.
*
* @param factor
* @return
*/
public Location multiply(double factor) {
return new Location(getUnits(), factor * getX(), factor * getY(), factor * getZ(),
factor * getRotation());
}
/**
* Returns a new Location with the same units as this one and with any of fields specified as
* true inverted from the values of this one. Specifically, if one of the x, y, z or rotation
* fields are specified true in the method call, that field will be multipled by -1 in the
* returned Location.
*
* @param x
* @param y
* @param z
* @param rotation
* @return
*/
public Location invert(boolean x, boolean y, boolean z, boolean rotation) {
return new Location(getUnits(), getX() * (x ? -1 : 1), getY() * (y ? -1 : 1),
getZ() * (z ? -1 : 1), getRotation() * (rotation ? -1 : 1));
}
/**
* Returns the unit vector of the vector between this and location l in units of this.
*
* @param l
* @return
*/
public Location unitVectorTo(Location l) {
Location vector = l.convertToUnits(units).subtract(this);
double norm = 1/this.getXyzDistanceTo(l);
return vector.multiply(norm, norm, norm, 0.0);
}
/**
* Return the dot product of this and l. Can be used to calculate the cosinus between two unit vectors.
*
* @param l
* @return
*/
public Length dotProduct(Location l) {
l = l.convertToUnits(units);
return new Length(x * l.getX() + y * l.getY() + z * l.getZ(), getUnits());
}
/**
* Returns a new Location with the same units as this one but with values updated to the passed
* in values. A caveat is that if a specified value is null, the new Location will contain the
* value from this object instead of the new value.
*
* This is intended as a utility method, useful for creating new Locations based on existing
* ones with one or more values changed.
*
* @param x
* @param y
* @param z
* @param rotation
* @return
*/
public Location derive(Double x, Double y, Double z, Double rotation) {
return new Location(units, x == null ? this.x : x, y == null ? this.y : y,
z == null ? this.z : z, rotation == null ? this.rotation : rotation);
}
/**
* Returns a new Location with the same units as this one but with values updated to the passed
* in Lengths. A caveat is that if a specified value is null, the new Location will contain the
* value from this object instead of the new value. The Lengths will automatically be converted
* to this locations's units.
*
* This is intended as a utility method, useful for creating new Locations based on existing
* ones with one or more values changed.
*
* @param x
* @param y
* @param z
* @param rotation
* @return
*/
public Location deriveLengths(Length x, Length y, Length z, Double rotation) {
return new Location(units,
(x == null ? this.x : x.convertToUnits(units).getValue()),
(y == null ? this.y : y.convertToUnits(units).getValue()),
(z == null ? this.z : z.convertToUnits(units).getValue()),
(rotation == null ? this.rotation : rotation));
}
/**
* Returns a new Location with the same units as this one but with values updated from
* a second Location. If a specified boolean is false, the new Location will contain the
* value from this object instead of the second location.
*
* This is intended as a utility method, useful for creating new Locations based on two existing
* ones with one or more values substituted.
*
* @param location
* @param x
* @param y
* @param z
* @param rotation
* @return
*/
public Location derive(Location location, boolean x, boolean y, boolean z, boolean rotation) {
location = location.convertToUnits(this.getUnits());
return new Location(units,
x ? location.x : this.x,
y ? location.y : this.y,
z ? location.z : this.z,
rotation ? location.rotation : this.rotation);
}
/**
* Returns a new Location with this Location's X and Y rotated by angle. Z and Rotation are
* unchanged.
*
* @param angle
* @return
*/
public Location rotateXy(double angle) {
if (angle == 0.0) {
return this;
}
while (angle < 180.) {
angle += 360;
}
while (angle > 180.) {
angle -= 360;
}
angle = Math.toRadians(angle);
return new Location(getUnits(), getX() * Math.cos(angle) - getY() * Math.sin(angle),
getX() * Math.sin(angle) + getY() * Math.cos(angle), getZ(), getRotation());
}
public Location rotateXyCenterPoint(Location center, double angle) {
Location location = this.subtract(center);
location = location.rotateXy(angle);
location = location.add(center);
return location;
}
@Override
public String toString() {
return String.format(Locale.US, "(%f, %f, %f, %f %s)", x, y, z, rotation,
units.getShortName());
}
public Point getXyPoint() {
return new Point(getX(), getY());
}
/**
* Checks if targetLocation is contained in current location given the current location represents a rectangular item with the origin in originLocation.
*/
public boolean containsLocation(Location originLocation, Location targetLocation) {
Location target = targetLocation.convertToUnits(this.units);
double x = target.getX();
double y = target.getY();
Location origin = originLocation.convertToUnits(this.units);
double x1 = origin.getX();
double y1 = origin.getY();
double x2 = x1 + getX();
double y2 = y1 + getY();
return (x >= x1) && (x <= x2) && (y > y1) && (y < y2);
}
public Location offsetWithRotationFrom(Location baseLocation) {
// Start with the base location.
// Rotate the feeder's offsets by the base's rotation making the offsets
// normal to the base's orientation.
Location offsets = rotateXy(baseLocation.getRotation());
// Add the rotated offsets to the base location to get the final location. We add
// with rotation since we need to use the combined rotation of the base and the
// offsets.
return baseLocation.addWithRotation(offsets);
}
/**
* Performs a unit agnostic equality check. If the Object being tested is a Location in a
* different unit, it is first converted to the units of this Location and then each value field
* is compared.
*/
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Location)) {
return false;
}
Location that = (Location) obj;
that = that.convertToUnits(this.units);
return this.units == that.units && this.x == that.x && this.y == that.y && this.z == that.z
&& this.rotation == that.rotation;
}
@Override
public int hashCode() {
int result;
long temp;
result = this.units != null ? this.units.hashCode() : 0;
temp = Double.doubleToLongBits(this.x);
result = 31 * result + (int) (temp ^ temp >>> 32);
temp = Double.doubleToLongBits(this.y);
result = 31 * result + (int) (temp ^ temp >>> 32);
temp = Double.doubleToLongBits(this.z);
result = 31 * result + (int) (temp ^ temp >>> 32);
temp = Double.doubleToLongBits(this.rotation);
result = 31 * result + (int) (temp ^ temp >>> 32);
return result;
}
/**
* @return true if at least one of the coordinates are non-zero, assuming the location is therefore initialized.
*/
public boolean isInitialized() {
return !this.equals(origin);
}
}