1 package org.djunits.quantity;
2
3 import org.djunits.quantity.def.Quantity;
4 import org.djunits.unit.AbstractUnit;
5 import org.djunits.unit.UnitInterface;
6 import org.djunits.unit.UnitRuntimeException;
7 import org.djunits.unit.Unitless;
8 import org.djunits.unit.scale.LinearScale;
9 import org.djunits.unit.scale.Scale;
10 import org.djunits.unit.si.SIPrefix;
11 import org.djunits.unit.si.SIUnit;
12 import org.djunits.unit.system.UnitSystem;
13
14 /**
15 * Solid angle is the two-dimensional angle in three-dimensional space, measured in steradians (sr).
16 * <p>
17 * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
18 * for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
19 * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
20 * @author Alexander Verbraeck
21 */
22 public class SolidAngle extends Quantity<SolidAngle>
23 {
24 /** Constant with value zero. */
25 public static final SolidAngle ZERO = ofSi(0.0);
26
27 /** Constant with value one. */
28 public static final SolidAngle ONE = ofSi(1.0);
29
30 /** Constant with value NaN. */
31 @SuppressWarnings("checkstyle:constantname")
32 public static final SolidAngle NaN = ofSi(Double.NaN);
33
34 /** Constant with value POSITIVE_INFINITY. */
35 public static final SolidAngle POSITIVE_INFINITY = ofSi(Double.POSITIVE_INFINITY);
36
37 /** Constant with value NEGATIVE_INFINITY. */
38 public static final SolidAngle NEGATIVE_INFINITY = ofSi(Double.NEGATIVE_INFINITY);
39
40 /** Constant with value MAX_VALUE. */
41 public static final SolidAngle POS_MAXVALUE = ofSi(Double.MAX_VALUE);
42
43 /** Constant with value -MAX_VALUE. */
44 public static final SolidAngle NEG_MAXVALUE = ofSi(-Double.MAX_VALUE);
45
46 /** */
47 private static final long serialVersionUID = 600L;
48
49 /**
50 * Instantiate a SolidAngle quantity with an SI or base value and a display unit.
51 * @param value the quantity value expressed in the SI or base unit
52 * @param displayUnit the display unit to use
53 * @param useSi use SI value when true, use value in unit when false
54 */
55 public SolidAngle(final double value, final SolidAngle.Unit displayUnit, final boolean useSi)
56 {
57 super(value, displayUnit, useSi);
58 }
59
60 /**
61 * Instantiate a SolidAngle quantity expressed in the given unit.
62 * @param valueInUnit the quantity value expressed in the given unit
63 * @param unit the unit of the value, also acts as the display unit
64 */
65 public SolidAngle(final double valueInUnit, final SolidAngle.Unit unit)
66 {
67 this(valueInUnit, unit, false);
68 }
69
70 /**
71 * Return a SolidAngle instance based on an SI value.
72 * @param si the si value
73 * @return the SolidAngle instance based on an SI value
74 */
75 public static SolidAngle ofSi(final double si)
76 {
77 return new SolidAngle(si, SolidAngle.Unit.SI, true);
78 }
79
80 /**
81 * Instantiate a SolidAngle quantity with an SI or base value and a display unit.
82 * @param siValue the quantity value expressed in the SI or base unit
83 * @param displayUnit the display unit to use
84 * @return the SolidAngle instance based on an SI value with the given display unit
85 */
86 public static SolidAngle ofSi(final double siValue, final SolidAngle.Unit displayUnit)
87 {
88 return new SolidAngle(siValue, displayUnit, true);
89 }
90
91 @Override
92 public SolidAngle instantiateSi(final double siValue, final UnitInterface<SolidAngle> displayUnit)
93 {
94 return new SolidAngle(siValue, (SolidAngle.Unit) displayUnit, true);
95 }
96
97 /**
98 * Returns a SolidAngle representation of a textual representation of a value with a unit. The String representation that
99 * can be parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are
100 * allowed, but not required, between the value and the unit.
101 * @param text the textual representation to parse into a SolidAngle
102 * @return the Scalar representation of the value in its unit
103 * @throws IllegalArgumentException when the text cannot be parsed
104 * @throws NullPointerException when the text argument is null
105 */
106 public static SolidAngle valueOf(final String text)
107 {
108 return Quantity.valueOf(text, ZERO);
109 }
110
111 /**
112 * Returns a SolidAngle based on a value expressed in the unit.
113 * @param valueInUnit the value, expressed in the given unit
114 * @param unit the unit of the value, also acts as the display unit
115 * @return ab SolidAngle representation of the value in its unit
116 */
117 public static SolidAngle of(final double valueInUnit, final SolidAngle.Unit unit)
118 {
119 return new SolidAngle(valueInUnit, unit);
120 }
121
122 /**
123 * Returns a SolidAngle based on a value and the textual representation of the unit, which can be localized.
124 * @param valueInUnit the value, expressed in the unit as given by unitString
125 * @param unitString the textual representation of the unit
126 * @return the Scalar representation of the value in its unit
127 * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
128 * @throws NullPointerException when the unitString argument is null
129 */
130 public static SolidAngle of(final double valueInUnit, final String unitString)
131 {
132 return Quantity.of(valueInUnit, unitString, ZERO);
133 }
134
135 @Override
136 public SolidAngle.Unit getDisplayUnit()
137 {
138 return (SolidAngle.Unit) super.getDisplayUnit();
139 }
140
141 /**
142 * Calculate the division of SolidAngle and SolidAngle, which results in a Dimensionless quantity.
143 * @param v quantity
144 * @return quantity as a division of SolidAngle and SolidAngle
145 */
146 public Dimensionless divide(final SolidAngle v)
147 {
148 return new Dimensionless(this.si() / v.si(), Unitless.BASE);
149 }
150
151 /**
152 * Calculate the multiplication of SolidAngle and LuminousIntensity, which results in a LuminousFlux scalar.
153 * @param v scalar
154 * @return scalar as a multiplication of SolidAngle and LuminousIntensity
155 */
156 public LuminousFlux multiply(final LuminousIntensity v)
157 {
158 return new LuminousFlux(this.si() * v.si(), LuminousFlux.Unit.SI);
159 }
160
161 /******************************************************************************************************/
162 /********************************************** UNIT CLASS ********************************************/
163 /******************************************************************************************************/
164
165 /**
166 * SolidAngle.Unit encodes the units of solid angles.
167 * <p>
168 * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved.
169 * See for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
170 * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
171 * @author Alexander Verbraeck
172 */
173 @SuppressWarnings("checkstyle:constantname")
174 public static class Unit extends AbstractUnit<SolidAngle>
175 {
176 /** The dimensions of the solid angle: sr. */
177 public static final SIUnit SI_UNIT = SIUnit.of("sr");
178
179 /** Steradian. */
180 public static final SolidAngle.Unit sr = new SolidAngle.Unit("sr", "steradian", 1.0, UnitSystem.SI_DERIVED);
181
182 /** The SI or BASE unit. */
183 public static final SolidAngle.Unit SI = sr;
184
185 /** square degree. */
186 public static final SolidAngle.Unit sq_deg = sr.deriveUnit("sq.deg", "sq.deg", "square degree",
187 (Math.PI / 180.0) * (Math.PI / 180.0), UnitSystem.SI_DERIVED, null);
188
189 /**
190 * Create a new SolidAngle unit.
191 * @param id the id or main abbreviation of the unit
192 * @param name the full name of the unit
193 * @param scaleFactorToBaseUnit the scale factor of the unit to convert it TO the base (SI) unit
194 * @param unitSystem the unit system such as SI or IMPERIAL
195 */
196 public Unit(final String id, final String name, final double scaleFactorToBaseUnit, final UnitSystem unitSystem)
197 {
198 super(id, name, scaleFactorToBaseUnit, unitSystem);
199 }
200
201 /**
202 * Return a derived unit for this unit, with textual abbreviation(s) and a display abbreviation.
203 * @param textualAbbreviation the textual abbreviation of the unit, which doubles as the id
204 * @param displayAbbreviation the display abbreviation of the unit
205 * @param name the full name of the unit
206 * @param scale the scale to use to convert from this unit to the standard (e.g., SI, BASE) unit
207 * @param unitSystem unit system, e.g. SI or Imperial
208 * @param siPrefix the SI Prefix of this unit
209 */
210 public Unit(final String textualAbbreviation, final String displayAbbreviation, final String name, final Scale scale,
211 final UnitSystem unitSystem, final SIPrefix siPrefix)
212 {
213 super(textualAbbreviation, displayAbbreviation, name, scale, unitSystem, siPrefix);
214 }
215
216 @Override
217 public SIUnit siUnit()
218 {
219 return SI_UNIT;
220 }
221
222 @Override
223 public Unit getBaseUnit()
224 {
225 return SI;
226 }
227
228 @Override
229 public SolidAngle ofSi(final double si, final UnitInterface<SolidAngle> displayUnit)
230 {
231 return new SolidAngle(si, (Unit) displayUnit, true);
232 }
233
234 @Override
235 public SolidAngle.Unit deriveUnit(final String textualAbbreviation, final String displayAbbreviation, final String name,
236 final double scaleFactor, final UnitSystem unitSystem, final SIPrefix siPrefix)
237 {
238 if (getScale() instanceof LinearScale ls)
239 {
240 return new SolidAngle.Unit(textualAbbreviation, displayAbbreviation, name,
241 new LinearScale(ls.getScaleFactorToBaseUnit() * scaleFactor), unitSystem, siPrefix);
242 }
243 throw new UnitRuntimeException("Only possible to derive a unit from a unit with a linear scale");
244 }
245
246 }
247
248 }