Skip to content

Commit

Permalink
- add readme to nuget, add solid angle units
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolaygekht committed Jul 29, 2022
1 parent ef14c70 commit b945cbd
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 21 deletions.
12 changes: 11 additions & 1 deletion Gehtsoft.Measurements.Test/AngularTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,20 @@ public class AngularTest
[InlineData(100, AngularUnit.Percent, 45, AngularUnit.Degree, 1e-5)]
[InlineData(50, AngularUnit.Percent, 26.56505, AngularUnit.Degree, 1e-5)]
[InlineData(26.56505, AngularUnit.Degree, 50, AngularUnit.Percent, 1e-5)]
public void Conversion(double value, AngularUnit unit, double expected, AngularUnit targetUnit, double accurracy = 1e-10)
public void ConversionAngular(double value, AngularUnit unit, double expected, AngularUnit targetUnit, double accurracy = 1e-10)
{
var v = new Measurement<AngularUnit>(value, unit);
v.In(targetUnit).Should().BeApproximately(expected, accurracy);
}

[Theory]
[InlineData(1, SolidAngularUnit.SquareDegree, 3.0461742e-4, SolidAngularUnit.Steradian)]
[InlineData(1, SolidAngularUnit.SquareDegree, 3600, SolidAngularUnit.SquareMinute)]
[InlineData(1, SolidAngularUnit.Steradian, 11818102.860042277, SolidAngularUnit.SquareMinute)]
public void ConversionSolid(double value, SolidAngularUnit unit, double expected, SolidAngularUnit targetUnit, double accurracy = 1e-10)
{
var v = new Measurement<SolidAngularUnit>(value, unit);
v.In(targetUnit).Should().BeApproximately(expected, accurracy);
}
}
}
4 changes: 1 addition & 3 deletions Gehtsoft.Measurements.Test/MeasurementMathTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,7 @@ public void CompareVerySmall()
1e-200.As(DistanceUnit.Centimeter).CompareTo(1e-201.As(DistanceUnit.Centimeter)).Should().Be(1);
1e-201.As(DistanceUnit.Centimeter).CompareTo(1e-200.As(DistanceUnit.Centimeter)).Should().Be(-1);
1e-200.As(DistanceUnit.Centimeter).CompareTo(1e-202.As(DistanceUnit.Meter)).Should().Be(0);
}


}

[Fact]
public void MathStatements()
Expand Down
1 change: 1 addition & 0 deletions Gehtsoft.Measurements/ConversionAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Gehtsoft.Measurements
/// <para>The attribute to specify the conversion rule for the measurement unit.</para>
/// <para>The attribute is applied on enumeration fields which describe one unit of the measurement (e.g. "meter")</para>
/// <para>One of the enumeration fields must always be attributed as a base value.</para>
/// <para>The operation defined is the conversion from the unit to the base unit.</para>
/// </summary>
[AttributeUsage(validOn: AttributeTargets.Field)]
public class ConversionAttribute : Attribute
Expand Down
29 changes: 29 additions & 0 deletions Gehtsoft.Measurements/SolidAngularUnit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace Gehtsoft.Measurements
{
/// <summary>
/// Solid angle units
/// </summary>
public enum SolidAngularUnit
{
/// <summary>
/// Steradian/square radian (4π per full sphere)
/// </summary>
[Unit("sr", 6)]
[Conversion(ConversionOperation.Base)]
Steradian = 0,

/// <summary>
/// Square degree
/// </summary>
[Unit("deg2", "sqdeg", 6)]
[Conversion(ConversionOperation.Multiply, 3.0461741978670859934674354937889e-4)]
SquareDegree = 1,

/// <summary>
/// Square degree
/// </summary>
[Unit("moa2", "sqmoa", 6)]
[Conversion(ConversionOperation.Multiply, 8.4615949940752388707428763716359e-8)]
SquareMinute = 2,
}
}
38 changes: 31 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,65 @@
# Status

![unit tests](https://github.com/gehtsoft-usa/Gehtsoft.Measurements/actions/workflows/test.yml/badge.svg)
![documentation](https://github.com/gehtsoft-usa/Gehtsoft.Measurements/actions/workflows/doc.yml/badge.svg)

# Gehtsoft.Measurements

The C# library to manipulate with various measurements (e.g. distances, weight, angles, temperatures, and so on) expressed in various units (e.g. distances in inches, yards, meters)
The C# library to manipulate and convert with various measurements/units (e.g. distances, weight, angles, temperatures, and so on)
expressed in various units (e.g. distances in inches, yards, meters).

The library may be useful for calculations, for example, in math, physic or GIS that does not depend from the system of units used (e.g. SI/Metric or Imperial) or to create a unit convertor application.

Currently, distance/length, velocity, weight, angular measurements, and energy units are supported.

The library is shared under LGPL license.

To use the last stable version of the library in your project please use the package on the nuget
https://www.nuget.org/packages/Gehtsoft.Measurements

## Using Library

The core class of the library is the generic structure `Measurement`. The structure accepts an enumeration as a parameter and this enumeration defines the measurement unit to be used:
The core class of the library is the generic structure `Measurement`.
The structure accepts an enumeration as a parameter and this enumeration
defines the measurement unit to be used:

```csharp
Measurement<DistanceUnit> v = new Measurement<DistanceUnit>(10, DistanceUnit.Feet);
var v = new Measurement<DistanceUnit>(10, DistanceUnit.Feet);
```

You can then manipulate this value using C# operator, format them or convert it into another unit:
You can then manipulate this value using C# operator, format them or
convert it into another unit:

```csharp
var v1 = v * 2;
string v = v.ToString("N3");
var v2 = v1.To(DistanceUnit.Meter);
```

The class fully supports serialization using `System.Text.Json` and `Binaron.Serializer` (see https://github.com/zachsaw/Binaron.Serializer).
`XmlSerializer` cannot be implemented for a readonly structures without introducing of non-safe code. Please refer to tests for an example
or

```csharp
var x = (10.As(DistanceUnit.Yard) + 36.As(DistanceUnit.Inch)).To(DistanceUnit.Meter);
```

The class fully supports serialization using `System.Text.Json`
and `Binaron.Serializer` (see https://github.com/zachsaw/Binaron.Serializer).
`XmlSerializer` cannot be implemented for a readonly structures without
introducing of non-safe code. Please refer to tests for an example
how to implement an XML serialization
(https://github.com/gehtsoft-usa/Gehtsoft.Measurements/blob/76fc639a657186dc91615839ca9ded4c14af7bc2/Gehtsoft.Measurements.Test/CoreClassesTest.cs#L181).

Read more on http://docs.gehtsoftusa.com/Gehtsoft.Measurements/web-content.html#index.html

## Defining your own units

You can define your own measurment units by creating a enumeration and mark it using `Unit` and `Conversion` attributes. The first attribute defines the unit name and the default accuracy of the values. The second attribute defines the rules of the conversion. One unit must always be a "base" unit, and conversion rules for other units defines how to convert the specified unit into a base unit.
You can define your own measurment units by
creating a enumeration and mark it using `Unit` and `Conversion`
attributes. The first attribute defines the unit name and the
default accuracy of the values. The second attribute defines the
rules of the conversion. One unit must always be a "base" unit, and
conversion rules for other units defines how to convert the
specified unit into a base unit.

```csharp
enum MyWeightUnit
Expand Down
3 changes: 2 additions & 1 deletion nuget/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<nuget xmlns="http://www.gehtsoft.com/build/nuget">
<!-- versions. the FIRST version is the current -->
<versions>
<version id="1.1.13">Add solid angle units.</version>
<version id="1.1.12">Fix comparison of very small values. Update dependencies, minor changes.</version>
<version id="1.1.11">Add troy oz, metric, uk and us tonne, typographical points and picas. Make some math methods extensions. Add new method to construct value (unit.New(value)). Add conversion from and to tuples.</version>
</versions>
Expand All @@ -13,7 +14,7 @@
</properties>
<projects>
<project id="Gehtsoft.Measurements">
<description>Measurements library</description>
<description>Measurements math, manipulations and unit conversion library</description>
<additional-file file="../doc/Gehtsoft.Measurements.xml" target="*" />
<additional-file file="../nuget/readme.md" target="docs/" />
</project>
Expand Down
4 changes: 4 additions & 0 deletions nuget/prepare.bat
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
cd ..
nuget restore Gehtsoft.Measurements.sln
msbuild Gehtsoft.Measurements.sln -p:Configuration="Release"
cd nuget
msbuild nuget.proj -t:Prepare
msbuild nuget.proj -t:NuSpec,NuPack
66 changes: 57 additions & 9 deletions nuget/readme.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,72 @@
# About

This package is a C# library to manipulate with various measurements (e.g. distances, weight, angles, temperatures, and so on) expressed in various units (e.g. distances in inches, yards, meters)
Currently, distance/length, velocity, weight, angular measurements, and energy units are supported.
The C# library to manipulate and convert with various measurements/units (e.g. distances, weight, angles, temperatures, and so on)
expressed in various units (e.g. distances in inches, yards, meters).

The library may be useful for calculations, for example, in math,
physic or GIS that does not depend from the system of units used (e.g. SI/Metric or Imperial)
or to create a unit convertor application.

Currently, distance/length, velocity, weight, angular measurements, and
energy units are supported.

The library is shared under LGPL license.

# Using Library
## Using Library

The core class of the library is the generic structure `Measurement`. The structure accepts an enumeration as a parameter and this enumeration defines the measurement unit to be used:
The core class of the library is the generic structure `Measurement`.
The structure accepts an enumeration as a parameter and this enumeration
defines the measurement unit to be used:

```cs
Measurement<DistanceUnit> v = new Measurement<DistanceUnit>(10, DistanceUnit.Feet);
```csharp
var v = new Measurement<DistanceUnit>(10, DistanceUnit.Feet);
```

You can then manipulate this value using C# operator, format them or convert it into another unit:
You can then manipulate this value using C# operator, format them or
convert it into another unit:

```cs
```csharp
var v1 = v * 2;
string v = v.ToString("N3");
var v2 = v1.To(DistanceUnit.Meter);
```

Read more on https://docs.gehtsoftusa.com/Gehtsoft.Measurements/index.html#main.html
or

```csharp
var x = (10.As(DistanceUnit.Yard) + 36.As(DistanceUnit.Inch)).To(DistanceUnit.Meter);
```

The class fully supports serialization using `System.Text.Json`
and `Binaron.Serializer` (see https://github.com/zachsaw/Binaron.Serializer).
`XmlSerializer` cannot be implemented for a readonly structures without
introducing of non-safe code. Please refer to tests for an example
how to implement an XML serialization
(https://github.com/gehtsoft-usa/Gehtsoft.Measurements/blob/76fc639a657186dc91615839ca9ded4c14af7bc2/Gehtsoft.Measurements.Test/CoreClassesTest.cs#L181).

Read more on http://docs.gehtsoftusa.com/Gehtsoft.Measurements/web-content.html#index.html

## Defining your own units

You can define your own measurment units by
creating a enumeration and mark it using `Unit` and `Conversion`
attributes. The first attribute defines the unit name and the
default accuracy of the values. The second attribute defines the
rules of the conversion. One unit must always be a "base" unit, and
conversion rules for other units defines how to convert the
specified unit into a base unit.

```csharp
enum MyWeightUnit
{
//1 gram
[Unit("g", 3)]
[Conversion(ConversionOperation.Base)]
Gram,

//1 kilogram (1 kilogram = 1000 gram)
[Unit("kg", 3)]
[Conversion(ConversionOperation.Multiply, 1000)]
Kilogram,
}
```

0 comments on commit b945cbd

Please sign in to comment.