BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
Unit.cpp File Reference

Description

Implements class Unit.

Homepage:\n http://www.bornagainproject.org
License:\n GNU General Public License v3 or higher (see COPYING)
Authors
Scientific Computing Group at MLZ (see CITATION, AUTHORS)

Definition in file Unit.cpp.

Include dependency graph for Unit.cpp:

Go to the source code of this file.

Functions

double convert (double d, Unit from, Unit to)
 Convert the given value d from unit "from" to unit "to" Throws an exception if no conversion possible. More...
 
QString unitAsString (const Unit &unit)
 Returns the string for the given unit. More...
 

Function Documentation

◆ convert()

double convert ( double  d,
Unit  from,
Unit  to 
)

Convert the given value d from unit "from" to unit "to" Throws an exception if no conversion possible.

Definition at line 20 of file Unit.cpp.

21 {
22  if (from == to)
23  return d;
24 
25  if (from == Unit::angstrom && to == Unit::nanometer)
26  return d / 10.0;
27 
28  if (from == Unit::nanometer && to == Unit::angstrom)
29  return d * 10.0;
30 
31  if (from == Unit::angstrom2 && to == Unit::nanometer2)
32  return d / 100.0;
33 
34  if (from == Unit::nanometer2 && to == Unit::angstrom2)
35  return d * 100.0;
36 
37  if (from == Unit::angstromMinus2 && to == Unit::nanometerMinus2)
38  return d * 100.0;
39 
40  if (from == Unit::nanometerMinus2 && to == Unit::angstromMinus2)
41  return d / 100.0;
42 
43  if (from == Unit::radiant && to == Unit::degree)
44  return Units::rad2deg(d);
45 
46  if (from == Unit::degree && to == Unit::radiant)
47  return Units::deg2rad(d);
48 
49  if (from == Unit::other || to == Unit::other) {
50  ASSERT(false); // no conversion possible
51  return d;
52  }
53 
54  ASSERT(false); // no conversion implemented
55  return d;
56 }
@ nanometerMinus2
@ angstrom
@ other
The unit has no enum value defined in here (e.g. when defined as an explicit string)
@ nanometer2
@ angstrom2
@ radiant
@ angstromMinus2
@ nanometer
@ degree

References angstrom, angstrom2, angstromMinus2, degree, nanometer, nanometer2, nanometerMinus2, other, and radiant.

Referenced by DoubleSpinBox::toBaseValue(), and DoubleSpinBox::toDisplayValue().

◆ unitAsString()

QString unitAsString ( const Unit unit)

Returns the string for the given unit.

Definition at line 58 of file Unit.cpp.

59 {
60  switch (unit) {
61  case Unit::unitless:
62  return "";
63  case Unit::nanometer:
64  return "nm";
65  case Unit::nanometer2:
66  return u8"nm\u00B2";
68  return u8"1/nm\u00B2";
69  case Unit::angstrom:
70  return u8"\u00c5";
71  case Unit::angstrom2:
72  return u8"\u00c5\u00B2";
74  return u8"1/\u00c5\u00B2";
75  case Unit::degree:
76  return "°";
77  case Unit::radiant:
78  return "rad";
79  case Unit::other:
80  ASSERT(false); // this function should not be called for Unit::other
81  return "";
82  default:
83  ASSERT(false); // no string implemented
84  return "";
85  }
86 }
@ unitless

References angstrom, angstrom2, angstromMinus2, degree, nanometer, nanometer2, nanometerMinus2, other, radiant, and unitless.

Referenced by ParameterTreeBuilder::addDetector(), and DoubleSpinBox::displayUnitAsString().