View Javadoc
1   package nl.tudelft.simulation.language.d3;
2   
3   import java.io.Serializable;
4   
5   /**
6    * A spherical point as defined in <a href="https://mathworld.wolfram.com/SphericalCoordinates.html">
7    * https://mathworld.wolfram.com/SphericalCoordinates.html </a>. Also according to ISO 31-11
8    * <p>
9    * Copyright (c) 2002-2024 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
10   * for project information <a href="https://simulation.tudelft.nl/" target="_blank"> https://simulation.tudelft.nl</a>. The DSOL
11   * project is distributed under a three-clause BSD-style license, which can be found at
12   * <a href="https://https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">
13   * https://https://simulation.tudelft.nl/dsol/docs/latest/license.html</a>.
14   * </p>
15   * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
16   */
17  public class SphericalPoint implements Serializable
18  {
19      /** */
20      private static final long serialVersionUID = 20191116L;
21  
22      /** radius. */
23      private double radius = 0.0;
24  
25      /** theta. */
26      private double theta = 0.0;
27  
28      /** phi. */
29      private double phi = 0.0;
30  
31      /**
32       * constructs a new SphericalPoint.
33       * @param radius double; radius
34       * @param theta double; theta
35       * @param phi double; phi
36       */
37      public SphericalPoint(final double radius, final double theta, final double phi)
38      {
39          super();
40          this.phi = phi;
41          this.radius = radius;
42          this.theta = theta;
43      }
44  
45      /**
46       * @return phi
47       */
48      public double getPhi()
49      {
50          return this.phi;
51      }
52  
53      /**
54       * @return radius
55       */
56      public double getRadius()
57      {
58          return this.radius;
59      }
60  
61      /**
62       * @return theta
63       */
64      public double getTheta()
65      {
66          return this.theta;
67      }
68  
69      /**
70       * converts a spherical point to a Cartesian point.
71       * @return the Cartesian point
72       */
73      public CartesianPoint toCartesianPoint()
74      {
75          return SphericalPoint.toCartesianPoint(this);
76      }
77  
78      /**
79       * converts a spherical point to a Cartesian point.
80       * @param point SphericalPoint; the spherical point
81       * @return the Cartesian point
82       */
83      public static CartesianPoint toCartesianPoint(final SphericalPoint point)
84      {
85          double x = point.radius * Math.sin(point.phi) * Math.cos(point.theta);
86          double y = point.radius * Math.sin(point.phi) * Math.sin(point.theta);
87          double z = point.radius * Math.cos(point.phi);
88          return new CartesianPoint(x, y, z);
89      }
90  
91      /** {@inheritDoc} */
92      @Override
93      public int hashCode()
94      {
95          final int prime = 31;
96          int result = 1;
97          long temp;
98          temp = Double.doubleToLongBits(this.phi);
99          result = prime * result + (int) (temp ^ (temp >>> 32));
100         temp = Double.doubleToLongBits(this.radius);
101         result = prime * result + (int) (temp ^ (temp >>> 32));
102         temp = Double.doubleToLongBits(this.theta);
103         result = prime * result + (int) (temp ^ (temp >>> 32));
104         return result;
105     }
106 
107     /** {@inheritDoc} */
108     @Override
109     @SuppressWarnings({"checkstyle:designforextension", "checkstyle:needbraces"})
110     public boolean equals(final Object obj)
111     {
112         if (this == obj)
113             return true;
114         if (obj == null)
115             return false;
116         if (getClass() != obj.getClass())
117             return false;
118         SphericalPoint other = (SphericalPoint) obj;
119         if (Double.doubleToLongBits(this.phi) != Double.doubleToLongBits(other.phi))
120             return false;
121         if (Double.doubleToLongBits(this.radius) != Double.doubleToLongBits(other.radius))
122             return false;
123         if (Double.doubleToLongBits(this.theta) != Double.doubleToLongBits(other.theta))
124             return false;
125         return true;
126     }
127 
128     /** {@inheritDoc} */
129     @Override
130     public String toString()
131     {
132         return "SphericalPoint [radius=" + this.radius + ", phi=" + this.phi + ", theta=" + this.theta + "]";
133     }
134 
135 }