1 package org.djutils.draw;
2
3 import java.util.Objects;
4
5 import org.djutils.exceptions.Throw;
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 public class Direction3d
21 {
22
23 @SuppressWarnings("checkstyle:visibilitymodifier")
24 public final double dirY;
25
26
27 @SuppressWarnings("checkstyle:visibilitymodifier")
28 public final double dirZ;
29
30
31
32
33
34
35
36
37 public Direction3d(final double dirY, final double dirZ)
38 {
39 Throw.whenNaN(dirY, "dirY");
40 Throw.whenNaN(dirZ, "dirZ");
41 Throw.when((!Double.isFinite(dirY)) || (!Double.isFinite(dirZ)), IllegalArgumentException.class,
42 "dirY and dirZ must be finite");
43 this.dirY = dirY;
44 this.dirZ = dirZ;
45 }
46
47
48
49
50
51 public double getDirY()
52 {
53 return this.dirY;
54 }
55
56
57
58
59
60 public double getDirZ()
61 {
62 return this.dirZ;
63 }
64
65
66
67
68
69
70
71
72 public double directionDifference(final Direction3d otherDirection)
73 {
74 double sinDirY = Math.sin(this.dirY);
75 double uX = Math.cos(this.dirZ) * sinDirY;
76 double uY = Math.sin(this.dirZ) * sinDirY;
77 double uZ = Math.cos(this.dirY);
78 double otherSinDirY = Math.sin(otherDirection.dirY);
79 double oX = Math.cos(otherDirection.dirZ) * otherSinDirY;
80 double oY = Math.sin(otherDirection.dirZ) * otherSinDirY;
81 double oZ = Math.cos(otherDirection.dirY);
82 double cosine = uX * oX + uY * oY + uZ * oZ;
83 if (Math.abs(cosine) > 1.0 && Math.abs(cosine) < 1.0 + 10 * Math.ulp(1.0))
84 {
85 cosine = Math.signum(cosine);
86 }
87 return (Math.acos(cosine));
88 }
89
90 @Override
91 public int hashCode()
92 {
93 return Objects.hash(this.dirY, this.dirZ);
94 }
95
96 @Override
97 @SuppressWarnings("checkstyle:needbraces")
98 public boolean equals(final Object obj)
99 {
100 if (this == obj)
101 return true;
102 if (obj == null)
103 return false;
104 if (getClass() != obj.getClass())
105 return false;
106 Direction3d other = (Direction3d) obj;
107 return Double.doubleToLongBits(this.dirY) == Double.doubleToLongBits(other.dirY)
108 && Double.doubleToLongBits(this.dirZ) == Double.doubleToLongBits(other.dirZ);
109 }
110
111 @Override
112 public String toString()
113 {
114 return "Direction3d [dirY=" + this.dirY + ", dirZ=" + this.dirZ + "]";
115 }
116
117 }