Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

c# port of the java version #34

Merged
merged 9 commits into from
Mar 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
.classpath
java/target
benchmark/data/*.data
bin
obj
.vscode
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,28 @@ On MacOS, do the following (not yet made on other ).
3. Run `pytest` (if you do not have it, install it through `pip install pytest`) to see that it works and that all tests pass.
4. Does it work? Great! You are now ready to use the python function.

# Getting started (C#)

On Linux or MacOS, the following steps will get you going:

```bash
# Get the source code
git clone https://github.com/hbf/miniball.git
cd miniball/csharp

# Compile an example, which generates random points and computes their miniball
dotnet build

# Run it on one million points in 3D
./example/bin/Debug/net7.0/example 1000000 3
```

To run unit tests:

```bash
dotnet test
```

## Float or double?

The C++ code is written using [templates](http://en.wikipedia.org/wiki/C%2B%2B#Templates) and allows the number type to be specified as a template argument.
Expand All @@ -97,6 +119,7 @@ Many thanks go to the following people who have – sometimes substantially –

* Thomas Otto (University of Hamburg) for [submitting several compiler fixes](https://github.com/hbf/miniball/issues/3) (g++ 4.7 and 4.5 on SUSE Linux 12.2 and 11.3) and for [introducing generic point and point coordinate accessors](https://github.com/hbf/miniball/pull/5) in the code.
* [Adam Heins](https://github.com/adamheins) (University of Toronto) for [updating the Python bindings](https://github.com/hbf/miniball/pull/32) to be installable with `pip`.
* [Lorenzo Delana](https://github.com/devel0) for [porting Java to C#](https://github.com/hbf/miniball/pull/34).

# Links
* For small dimensions like 2D or 3D, [Bernd Gärtner's code](http://www.inf.ethz.ch/personal/gaertner/miniball.html), which is based on Welzl's algorithm, may be faster.
Expand Down
59 changes: 59 additions & 0 deletions csharp/example/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System.Diagnostics;
using SEB;

using static SEB.PointSetUtils;

namespace example;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("====================================================");
Console.WriteLine("Seb example");

// Check for right number of arguments ...
if (args.Length < 2)
{
Console.WriteLine($"Usage {AppDomain.CurrentDomain.FriendlyName} number-of-points dimension [boundary]");
Console.WriteLine("If 'boundary' is given, all points will be on the boundary of a sphere.");
Console.WriteLine("====================================================");
Environment.Exit(1);
}
Console.WriteLine("====================================================");

// ... and parse command line arguments
var n = int.Parse(args[0]);
var d = int.Parse(args[1]);
var on_boundary = args.Length > 2 && args[2] == "boundary";

// Construct n random points in dimension d
var rnd = new Random();
var S = RandomPointSet(d, n, rnd, on_boundary);

var sw = new Stopwatch();

Console.WriteLine("Starting computation...");
Console.WriteLine("====================================================");

sw.Start();

var mb = new Miniball(S);

var rad = mb.Radius;
var rad_squared = mb.SquaredRadius;
var center = mb.Center;

sw.Stop();

// Output
Console.WriteLine($"Running time: {sw.Elapsed.TotalSeconds}s");
Console.WriteLine($"Radius = {rad} (squared: {rad_squared})");
Console.WriteLine("Center:");
for (int j = 0; j < center.Length; ++j)
Console.WriteLine($" {center[j]}");

Console.WriteLine("====================================================");
Console.Write(mb.Verify().ConsoleFmt());
Console.WriteLine("====================================================");
}
}
14 changes: 14 additions & 0 deletions csharp/example/example.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<ProjectReference Include="..\miniball\miniball.csproj" />
</ItemGroup>

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
26 changes: 26 additions & 0 deletions csharp/miniball/highdim/Logging.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace SEB;

/// <summary>
/// A very simple logging class used for debugging.
/// </summary>
static class Logging
{

public static bool log = false;

public static void Warn(string msg)
{
if (log) System.Console.WriteLine($"[warn] {msg}");
}

public static void Info(string msg)
{
if (log) System.Console.WriteLine($"[info] {msg}");
}

public static void Debug(string msg)
{
if (log) System.Console.WriteLine($"[debug] {msg}");
}

}
Loading