Skip to content

Commit

Permalink
Port HaversineDistance to C++ for use on other platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
tmn committed Oct 20, 2023
1 parent 350438a commit 0382567
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 71 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
.DS_Store
/.build
/build
/Packages
/*.xcodeproj
xcuserdata/
DerivedData/
.swiftpm/configuration/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
16 changes: 16 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.26)
project(haversine LANGUAGES CXX)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_STANDARD 11)

include_directories("${CMAKE_CURRENT_SOURCE_DIR}/src/include")

set(SOURCES
src/haversineFormula.cpp

src/include/haversineFormula.h
)

add_library(haversine ${SOURCES})
target_include_directories(haversine PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/src")
22 changes: 18 additions & 4 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
Copyright 2018 tmn
MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
Copyright (c) 2023 Tri Nguyen

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
18 changes: 11 additions & 7 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.2
// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand All @@ -7,15 +7,19 @@ let package = Package(
name: "HaversineDistance",
products: [
.library(
name: "HaversineDistance",
targets: ["HaversineDistance"]),
name: "haversineFormula",
targets: ["haversineFormula"]),
],
targets: [
.target(
name: "HaversineDistance",
dependencies: []),
name: "haversineFormula",
path: "src",
swiftSettings: [.interoperabilityMode(.Cxx)]
),
.testTarget(
name: "HaversineDistanceTests",
dependencies: ["HaversineDistance"]),
name: "haversineFormulaTests",
dependencies: ["haversineFormula"],
swiftSettings: [.interoperabilityMode(.Cxx)]
),
]
)
26 changes: 0 additions & 26 deletions Sources/HaversineDistance/HaversineDistance.swift

This file was deleted.

17 changes: 0 additions & 17 deletions Tests/HaversineDistanceTests/HaversineDistanceTests.swift

This file was deleted.

9 changes: 0 additions & 9 deletions Tests/HaversineDistanceTests/XCTestManifests.swift

This file was deleted.

17 changes: 17 additions & 0 deletions Tests/HaversineDistanceTests/haversineFormulaTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import XCTest
@testable import haversineFormula

final class HaversineDistanceTests: XCTestCase {
func testDistance() {
let distance = Haversine.distance(
Location(lat: 59.890465, lon: 10.523493),
Location(lat: 59.904499, lon: 10.786372)
)

XCTAssertEqual(distance, 1474.35)
}

static var allTests = [
("testDistance", testDistance)
]
}
7 changes: 0 additions & 7 deletions Tests/LinuxMain.swift

This file was deleted.

31 changes: 31 additions & 0 deletions src/haversineFormula.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "haversineFormula.h"


Haversine::Haversine()
{

}

Haversine::~Haversine()
{

}

double Haversine::to_radians(double degree)
{
return degree * M_PI / 180;
}

double Haversine::distance(Location start, Location end)
{
double φ1 = to_radians(start.lat);
double φ2 = to_radians(end.lat);
double δφ = to_radians(end.lat - start.lat);
double δλ = to_radians(end.lon - start.lon);

double a = sin(δφ / 2.0) * sin(δφ / 2.0)
+ cos1) * cos2) * sin(δλ / 2.0) * sin(δλ / 2.0);
double c = 2.0 * asin(sqrt(a));

return round(637100.0 * c * 100)/100;
}
21 changes: 21 additions & 0 deletions src/include/haversineFormula.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include <math.h>

struct Location
{
double lat;
double lon;
};

class Haversine
{
public:
Haversine();
~Haversine();

static double distance(Location start, Location end);

private:
static double to_radians(double degree);
};

0 comments on commit 0382567

Please sign in to comment.