1 /* Copyright 2002-2025 CS GROUP
2 * Licensed to CS GROUP (CS) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * CS licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.orekit.utils.units;
18
19 import org.orekit.errors.OrekitException;
20 import org.orekit.errors.OrekitMessages;
21
22 /** Converter between units.
23 * <p>
24 * Instances of this class are immutable.
25 * </p>
26 * @author Luc Maisonobe
27 * @since 11.0
28 */
29 public class UnitsConverter {
30
31 /** Identity converter. */
32 public static final UnitsConverter IDENTITY =
33 new UnitsConverter(Unit.ONE, Unit.ONE);
34
35 /** Percents to units converter. */
36 public static final UnitsConverter PERCENTS_TO_UNIT =
37 new UnitsConverter(Unit.PERCENT, Unit.ONE);
38
39 /** Arcseconds to radians converter. */
40 public static final UnitsConverter ARC_SECONDS_TO_RADIANS =
41 new UnitsConverter(Unit.parse("as"), Unit.RADIAN);
42
43 /** Milli arcseconds to radians converter. */
44 public static final UnitsConverter MILLI_ARC_SECONDS_TO_RADIANS =
45 new UnitsConverter(Unit.parse("mas"), Unit.RADIAN);
46
47 /** Milli seconds to seconds converter. */
48 public static final UnitsConverter MILLI_SECONDS_TO_SECONDS =
49 new UnitsConverter(Unit.parse("ms"), Unit.SECOND);
50
51 /** Nano Teslas to Tesla converter. */
52 public static final UnitsConverter NANO_TESLAS_TO_TESLAS =
53 new UnitsConverter(Unit.parse("nT"), Unit.TESLA);
54
55 /** Days to seconds converter. */
56 public static final UnitsConverter DAYS_TO_SECONDS =
57 new UnitsConverter(Unit.DAY, Unit.SECOND);
58
59 /** Kilometres to metres converter. */
60 public static final UnitsConverter KILOMETRES_TO_METRES =
61 new UnitsConverter(Unit.KILOMETRE, Unit.METRE);
62
63 /** Square kilometres to square metres converter. */
64 public static final UnitsConverter KILOMETRES_2_TO_METRES_2 =
65 new UnitsConverter(Unit.parse("km²"), Unit.parse("m²"));
66
67 /** km³/s² to m³/s² converter. */
68 public static final UnitsConverter KM3_P_S2_TO_M3_P_S2 =
69 new UnitsConverter(Unit.parse("km³/s²"), Unit.parse("m³/s²"));
70
71 /** Source unit. */
72 private final Unit from;
73
74 /** Destination unit. */
75 private final Unit to;
76
77 /** Conversion factor. */
78 private final double factor;
79
80 /** Simple constructor.
81 * @param from source unit
82 * @param to destination unit
83 */
84 public UnitsConverter(final Unit from, final Unit to) {
85 this.from = from;
86 this.to = to;
87 if (!from.sameDimension(to)) {
88 throw new OrekitException(OrekitMessages.INCOMPATIBLE_UNITS,
89 from.getName(), to.getName());
90 }
91 this.factor = from.getScale() / to.getScale();
92 }
93
94 /** Get the source unit.
95 * @return source unit
96 */
97 public Unit getFrom() {
98 return from;
99 }
100
101 /** Get the destination unit.
102 * @return destination unit
103 */
104 public Unit getTo() {
105 return to;
106 }
107
108 /** Convert a value.
109 * @param value value in the {@link #getFrom() source unit}
110 * @return value converted in the {@link #getTo() destination unit}
111 */
112 public double convert(final double value) {
113 return factor * value;
114 }
115
116 /** {@inheritDoc} */
117 @Override
118 public String toString() {
119 return from.getName() + " → " + to.getName();
120 }
121
122 }