View Javadoc
1   package org.djunits.value.vdouble.scalar;
2   
3   import java.util.Locale;
4   
5   import org.djunits.unit.AngleUnit;
6   import org.djunits.unit.AngularVelocityUnit;
7   import org.djunits.unit.DimensionlessUnit;
8   import org.djunits.unit.DirectionUnit;
9   import org.djunits.unit.DurationUnit;
10  import org.djunits.value.vdouble.scalar.base.DoubleScalarRel;
11  import org.djunits.value.vdouble.scalar.base.DoubleScalarRelWithAbs;
12  import org.djutils.base.NumberParser;
13  import org.djutils.exceptions.Throw;
14  
15  import jakarta.annotation.Generated;
16  
17  /**
18   * Easy access methods for the Relative Angle DoubleScalar.
19   * <p>
20   * Copyright (c) 2013-2025 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
21   * All rights reserved. <br>
22   * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>.
23   * </p>
24   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
25   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
26   */
27  @Generated(value = "org.djunits.generator.GenerateDJUNIT", date = "2025-09-06T15:16:28.380798Z")
28  public class Angle extends DoubleScalarRelWithAbs<DirectionUnit, Direction, AngleUnit, Angle>
29  {
30      /** */
31      private static final long serialVersionUID = 20150901L;
32  
33      /** Constant with value zero. */
34      public static final Angle ZERO = new Angle(0.0, AngleUnit.SI);
35  
36      /** Constant with value one. */
37      public static final Angle ONE = new Angle(1.0, AngleUnit.SI);
38  
39      /** Constant with value pi. */
40      public static final Angle PI = new Angle(Math.PI, AngleUnit.RADIAN);
41  
42      /** Constant with value pi/2. */
43      public static final Angle HALF_PI = new Angle(Math.PI / 2.0, AngleUnit.RADIAN);
44  
45      /** Constant with value tau. */
46      public static final Angle TAU = new Angle(Math.PI * 2.0, AngleUnit.RADIAN);
47  
48      /** Constant with value NaN. */
49      @SuppressWarnings("checkstyle:constantname")
50      public static final Angle NaN = new Angle(Double.NaN, AngleUnit.SI);
51  
52      /** Constant with value POSITIVE_INFINITY. */
53      public static final Angle POSITIVE_INFINITY = new Angle(Double.POSITIVE_INFINITY, AngleUnit.SI);
54  
55      /** Constant with value NEGATIVE_INFINITY. */
56      public static final Angle NEGATIVE_INFINITY = new Angle(Double.NEGATIVE_INFINITY, AngleUnit.SI);
57  
58      /** Constant with value MAX_VALUE. */
59      public static final Angle POS_MAXVALUE = new Angle(Double.MAX_VALUE, AngleUnit.SI);
60  
61      /** Constant with value -MAX_VALUE. */
62      public static final Angle NEG_MAXVALUE = new Angle(-Double.MAX_VALUE, AngleUnit.SI);
63  
64      /**
65       * Construct Angle scalar with a unit.
66       * @param value the double value, expressed in the given unit
67       * @param unit unit for the double value
68       */
69      public Angle(final double value, final AngleUnit unit)
70      {
71          super(value, unit);
72      }
73  
74      /**
75       * Construct Angle scalar.
76       * @param value Scalar from which to construct this instance
77       */
78      public Angle(final Angle value)
79      {
80          super(value);
81      }
82  
83      @Override
84      public final Angle instantiateRel(final double value, final AngleUnit unit)
85      {
86          return new Angle(value, unit);
87      }
88  
89      @Override
90      public final Direction instantiateAbs(final double value, final DirectionUnit unit)
91      {
92          return new Direction(value, unit);
93      }
94  
95      /**
96       * Construct Angle scalar based on an SI value.
97       * @param value the double value in SI units
98       * @return the new scalar with the SI value
99       */
100     public static final Angle ofSI(final double value)
101     {
102         return new Angle(value, AngleUnit.SI);
103     }
104 
105     /**
106      * Interpolate between two values. Note that the first value does not have to be smaller than the second.
107      * @param zero the value at a ratio of zero
108      * @param one the value at a ratio of one
109      * @param ratio the ratio between 0 and 1, inclusive
110      * @return a Angle at the given ratio between 0 and 1
111      */
112     public static Angle interpolate(final Angle zero, final Angle one, final double ratio)
113     {
114         Throw.when(ratio < 0.0 || ratio > 1.0, IllegalArgumentException.class,
115                 "ratio for interpolation should be between 0 and 1, but is %f", ratio);
116         return new Angle(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getDisplayUnit()) * ratio, zero.getDisplayUnit());
117     }
118 
119     /**
120      * Return the maximum value of two relative scalars.
121      * @param r1 the first scalar
122      * @param r2 the second scalar
123      * @return the maximum value of two relative scalars
124      */
125     public static Angle max(final Angle r1, final Angle r2)
126     {
127         return r1.gt(r2) ? r1 : r2;
128     }
129 
130     /**
131      * Return the maximum value of more than two relative scalars.
132      * @param r1 the first scalar
133      * @param r2 the second scalar
134      * @param rn the other scalars
135      * @return the maximum value of more than two relative scalars
136      */
137     public static Angle max(final Angle r1, final Angle r2, final Angle... rn)
138     {
139         Angle maxr = r1.gt(r2) ? r1 : r2;
140         for (Angle r : rn)
141         {
142             if (r.gt(maxr))
143             {
144                 maxr = r;
145             }
146         }
147         return maxr;
148     }
149 
150     /**
151      * Return the minimum value of two relative scalars.
152      * @param r1 the first scalar
153      * @param r2 the second scalar
154      * @return the minimum value of two relative scalars
155      */
156     public static Angle min(final Angle r1, final Angle r2)
157     {
158         return r1.lt(r2) ? r1 : r2;
159     }
160 
161     /**
162      * Return the minimum value of more than two relative scalars.
163      * @param r1 the first scalar
164      * @param r2 the second scalar
165      * @param rn the other scalars
166      * @return the minimum value of more than two relative scalars
167      */
168     public static Angle min(final Angle r1, final Angle r2, final Angle... rn)
169     {
170         Angle minr = r1.lt(r2) ? r1 : r2;
171         for (Angle r : rn)
172         {
173             if (r.lt(minr))
174             {
175                 minr = r;
176             }
177         }
178         return minr;
179     }
180 
181     /**
182      * Returns a Angle representation of a textual representation of a value with a unit. The String representation that can be
183      * parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are allowed,
184      * but not required, between the value and the unit.
185      * @param text the textual representation to parse into a Angle
186      * @return the Scalar representation of the value in its unit
187      * @throws IllegalArgumentException when the text cannot be parsed
188      * @throws NullPointerException when the text argument is null
189      */
190     public static Angle valueOf(final String text)
191     {
192         Throw.whenNull(text, "Error parsing Angle: text to parse is null");
193         Throw.when(text.length() == 0, IllegalArgumentException.class, "Error parsing Angle: empty text to parse");
194         try
195         {
196             NumberParser numberParser = new NumberParser().lenient().trailing();
197             double d = numberParser.parseDouble(text);
198             String unitString = text.substring(numberParser.getTrailingPosition()).trim();
199             AngleUnit unit = AngleUnit.BASE.getUnitByAbbreviation(unitString);
200             Throw.when(unit == null, IllegalArgumentException.class, "Unit %s not found for quantity Angle", unitString);
201             return new Angle(d, unit);
202         }
203         catch (Exception exception)
204         {
205             throw new IllegalArgumentException(
206                     "Error parsing Angle from " + text + " using Locale " + Locale.getDefault(Locale.Category.FORMAT),
207                     exception);
208         }
209     }
210 
211     /**
212      * Returns a Angle based on a value and the textual representation of the unit, which can be localized.
213      * @param value the value to use
214      * @param unitString the textual representation of the unit
215      * @return the Scalar representation of the value in its unit
216      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
217      * @throws NullPointerException when the unitString argument is null
218      */
219     public static Angle of(final double value, final String unitString)
220     {
221         Throw.whenNull(unitString, "Error parsing Angle: unitString is null");
222         Throw.when(unitString.length() == 0, IllegalArgumentException.class, "Error parsing Angle: empty unitString");
223         AngleUnit unit = AngleUnit.BASE.getUnitByAbbreviation(unitString);
224         Throw.when(unit == null, IllegalArgumentException.class, "Error parsing Angle with unit %s", unitString);
225         return new Angle(value, unit);
226     }
227 
228     /**
229      * Calculate the division of Angle and Angle, which results in a Dimensionless scalar.
230      * @param v scalar
231      * @return scalar as a division of Angle and Angle
232      */
233     public final Dimensionless divide(final Angle v)
234     {
235         return new Dimensionless(this.si / v.si, DimensionlessUnit.SI);
236     }
237 
238     /**
239      * Calculate the multiplication of Angle and Frequency, which results in a AngularVelocity scalar.
240      * @param v scalar
241      * @return scalar as a multiplication of Angle and Frequency
242      */
243     public final AngularVelocity times(final Frequency v)
244     {
245         return new AngularVelocity(this.si * v.si, AngularVelocityUnit.SI);
246     }
247 
248     /**
249      * Calculate the division of Angle and Duration, which results in a AngularVelocity scalar.
250      * @param v scalar
251      * @return scalar as a division of Angle and Duration
252      */
253     public final AngularVelocity divide(final Duration v)
254     {
255         return new AngularVelocity(this.si / v.si, AngularVelocityUnit.SI);
256     }
257 
258     /**
259      * Calculate the division of Angle and AngularVelocity, which results in a Duration scalar.
260      * @param v scalar
261      * @return scalar as a division of Angle and AngularVelocity
262      */
263     public final Duration divide(final AngularVelocity v)
264     {
265         return new Duration(this.si / v.si, DurationUnit.SI);
266     }
267 
268     @Override
269     public SIScalar reciprocal()
270     {
271         return SIScalar.divide(Dimensionless.ONE, this);
272     }
273 
274     /**
275      * Multiply two scalars that result in a scalar of type Angle.
276      * @param scalar1 the first scalar
277      * @param scalar2 the second scalar
278      * @return the multiplication of both scalars as an instance of Angle
279      */
280     public static Angle multiply(final DoubleScalarRel<?, ?> scalar1, final DoubleScalarRel<?, ?> scalar2)
281     {
282         Throw.whenNull(scalar1, "scalar1 cannot be null");
283         Throw.whenNull(scalar2, "scalar2 cannot be null");
284         Throw.when(!scalar1.getDisplayUnit().getQuantity().getSiDimensions()
285                 .plus(scalar2.getDisplayUnit().getQuantity().getSiDimensions()).equals(AngleUnit.BASE.getSiDimensions()),
286                 IllegalArgumentException.class, "Multiplying %s by %s does not result in instance of type Angle",
287                 scalar1.toDisplayString(), scalar2.toDisplayString());
288         return new Angle(scalar1.si * scalar2.si, AngleUnit.SI);
289     }
290 
291     /**
292      * Divide two scalars that result in a scalar of type Angle.
293      * @param scalar1 the first scalar
294      * @param scalar2 the second scalar
295      * @return the division of scalar1 by scalar2 as an instance of Angle
296      */
297     public static Angle divide(final DoubleScalarRel<?, ?> scalar1, final DoubleScalarRel<?, ?> scalar2)
298     {
299         Throw.whenNull(scalar1, "scalar1 cannot be null");
300         Throw.whenNull(scalar2, "scalar2 cannot be null");
301         Throw.when(!scalar1.getDisplayUnit().getQuantity().getSiDimensions()
302                 .minus(scalar2.getDisplayUnit().getQuantity().getSiDimensions()).equals(AngleUnit.BASE.getSiDimensions()),
303                 IllegalArgumentException.class, "Dividing %s by %s does not result in an instance of type Angle",
304                 scalar1.toDisplayString(), scalar2.toDisplayString());
305         return new Angle(scalar1.si / scalar2.si, AngleUnit.SI);
306     }
307 
308 }