Skip to content

Commit

Permalink
python cpp merge
Browse files Browse the repository at this point in the history
  • Loading branch information
walchko committed Jul 12, 2023
1 parent c31d0e2 commit d3562a6
Show file tree
Hide file tree
Showing 21 changed files with 567 additions and 23 deletions.
43 changes: 22 additions & 21 deletions .github/workflows/python.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@ name: CheckPackage
on: [push]

jobs:
build:
runs-on: ubuntu-latest
strategy:
max-parallel: 5
fail-fast: true
matrix:
python-version: ["3.8", "3.9", "3.10"]
steps:
- uses: actions/checkout@master
- name: Setup Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install packages
run: |
echo "Installing dependencies"
python3 -m venv .venv
source .venv/bin/activate
pip install -U pip setuptools wheel poetry pytest
poetry install
pytest tests/
build:
runs-on: ubuntu-latest
strategy:
max-parallel: 5
fail-fast: true
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]
steps:
- uses: actions/checkout@master
- name: Setup Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install packages
run: |
cd python
echo "Installing dependencies"
python3 -m venv .venv
source .venv/bin/activate
pip install -U pip setuptools wheel poetry pytest
poetry install
pytest tests/
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ __pycache__
.DS_Store
*.lock
.ipynb_checkpoints

build
.DS_Store
._*
.vscode
old
2 changes: 2 additions & 0 deletions cpp/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BasedOnStyle: LLVM
IndentWidth: 2
53 changes: 53 additions & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
cmake_minimum_required(VERSION 3.10)
PROJECT(squaternion VERSION "2023.02.18")

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)


if(PROJECT_IS_TOP_LEVEL)
cmake_host_system_information(RESULT HOST QUERY HOSTNAME)
cmake_host_system_information(RESULT OSN QUERY OS_NAME)
cmake_host_system_information(RESULT OS_VERSION QUERY OS_RELEASE)
cmake_host_system_information(RESULT PROC QUERY PROCESSOR_DESCRIPTION)

message(STATUS "-------------------------------------")
message(STATUS " Project: ${PROJECT_NAME}")
message(STATUS " C++ ${CMAKE_CXX_STANDARD}")
message(STATUS "-------------------------------------")
message(STATUS " ${HOST}")
message(STATUS " ${OSN}: ${OS_VERSION}")
message(STATUS " ${PROC}")
message(STATUS "-------------------------------------")

set(BUILD_EXAMPLES ON)
set(BUILD_GTESTS ON)
else()
message(STATUS "-> ${PROJECT_NAME} is submodule")
set(BUILD_EXAMPLES OFF)
set(BUILD_GTESTS OFF)
endif()

add_library(${PROJECT_NAME}
INTERFACE
# src/quaternion.cpp
# src/quaternion_filters.cpp
)
target_include_directories(${PROJECT_NAME} INTERFACE src/)
# target_include_directories(${PROJECT_NAME}
# INTERFACE
# src/
# )

# Examples -----------------------------------------------------------
message(STATUS "Building ${PROJECT_NAME} examples is ${BUILD_EXAMPLES}")
if(BUILD_EXAMPLES)
add_subdirectory(examples)
endif()

# Tests --------------------------------------------------------------
message(STATUS "Building ${PROJECT_NAME} gtests is ${BUILD_GTESTS}")
if(BUILD_GTESTS)
add_subdirectory(gtests)
endif()
10 changes: 10 additions & 0 deletions cpp/arduino/test/test.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <squaternion.hpp>

void setup() {}

void loop() {
Quaternion q;
Quaternion qq(1,2,3,4);
qq.normalize();
q = 2.0*qq/2.0;
}
13 changes: 13 additions & 0 deletions cpp/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
list(APPEND ex
simple.cpp
)

# message(STATUS "Examples ----------------------")
foreach(src ${ex})
get_filename_component(name ${src} NAME_WE)
# message(STATUS " -> ${name}")
add_executable(${name} ${src})
target_link_libraries(${name} squaternion)
target_include_directories(${name} PUBLIC ../src)
# add_test(NAME ${name} COMMAND ${name})
endforeach()
46 changes: 46 additions & 0 deletions cpp/examples/simple.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <cstdio>
#include <iostream>
#include <squaternion.hpp>

using namespace std;

// std::ostream &operator<<(std::ostream &os, const Quaternion &q) {
// return os << q.to_str();
// }

int main() {
Quaternion q(1, 2, 3, 4);
q.normalize();
cout << "q: " << q.to_str() << endl;

string s = q.to_str(); // need to save string to avoid dangling pointer
printf("%s\n", s.c_str());

Quaternion bad(0, 0, 0, 0);
cout << "Bad: " << bad << endl;

int err = bad.normalize();

if (err) {
printf("*** division by zero for Quaternion bad ***\n");
}
else {
cout << "Normalized bad: " << bad << " - how?" << endl;
}

double r, p, y;
q.to_euler(&r, &p, &y, true); // get degrees
printf("to_euler: %lf %lf %lf\n", r, p, y);

Quaternion qq = Quaternion::from_euler(10, 20, 30, true); // from degrees
cout << "from_euler: " << qq.to_str() << endl;

Quaternion qdot = 0.5 * q * Quaternion(0, 0.1, -0.1, 0); // qdot - 1/2 * q * w
cout << "qdot: " << qdot << endl;

Quaternion a(1, 2, 3, 4);
Quaternion b(5, 6, 7, 8);
cout << "a*b: " << a * b << endl;

return 0;
}
3 changes: 3 additions & 0 deletions cpp/format-code.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/zsh

clang-format -style=file:../dotfiles/clang-format.yml -i src/**/*.hpp examples/**/*.cpp gtests/**/*.cpp
35 changes: 35 additions & 0 deletions cpp/gtests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
include(FetchContent)

# GTest -----------------
FetchContent_Declare(gtest
GIT_REPOSITORY "https://github.com/google/googletest"
GIT_TAG "origin/main"
SOURCE_DIR "${CMAKE_BINARY_DIR}/_deps/gtest"
)
# set(EXAMPLES OFF CACHE INTERNAL "Dont build examples")
FetchContent_MakeAvailable(gtest)
if(gtest_POPULATED)
message(STATUS "=> Found gtest")
else()
message(STATUS "*** Didn't find gtest")
endif()

#----------------------------
enable_testing()

list(APPEND gci-gtests
main-gtest
)

message(STATUS "gci::gTests ----------------------")
foreach(name ${gci-gtests})
# message(STATUS " -> ${name}")
add_executable(${name} ${name}.cpp)
target_link_libraries(${name}
GTest::gtest_main
${PROJECT_NAME})
add_test(NAME ${name} COMMAND ${name})
endforeach()

include(GoogleTest)
gtest_discover_tests(${gci-gtests})
40 changes: 40 additions & 0 deletions cpp/gtests/main-gtest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <gtest/gtest.h>
#include <squaternion.hpp>

// // Demonstrate some basic assertions.
// TEST(squaternion, dummy) {
// // Expect two strings not to be equal.
// EXPECT_STRNE("hello", "world");
// // Expect equality.
// EXPECT_EQ(7 * 6, 42);
// }

TEST(squaternion, create) {
Quaternion q;
EXPECT_TRUE(q.w == 1.0);
EXPECT_TRUE(q.x + q.y + q.z == 0.0);

// this will never (?) be arduino?
// #ifdef ARDUINO
// EXPECT_TRUE(sizeof(q.w) == sizeof(float));
// #else
EXPECT_TRUE(sizeof(q.w) == sizeof(double));
// #endif

Quaternion qq(1, 2, 3, 4);
EXPECT_EQ(qq.w, 1.0);
EXPECT_EQ(qq.x, 2.0);
EXPECT_EQ(qq.y, 3.0);
EXPECT_EQ(qq.z, 4.0);
}

TEST(squaternion, math) {
Quaternion q;
q = q * 2.0;
EXPECT_EQ(q.w, 2.0);

Quaternion qq(1, 2, 3, 4);
qq.normalize();
q = 2.0 * qq / 2.0;
ASSERT_DOUBLE_EQ(q.magnitude(), 1.0);
}
22 changes: 22 additions & 0 deletions cpp/keywords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#######################################
# Datatypes (KEYWORD1)
#######################################

Quaternion KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
#######################################

normalize KEYWORD2
conjugate KEYWORD2
to_euler KEYWORD2
from_euler KEYWORD2
to_str KEYWORD2
rotate KEYWORD2

#######################################
# Constants (LITERAL1)
#######################################
deg2rad LITERAL1
rad2deg LITERAL1
9 changes: 9 additions & 0 deletions cpp/library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=squaternion
version=2022.08.20
author=Kevin Walchko
maintainer=Kevin Walchko
sentence=A simple Quaternions library modelled after a python library called squaternion
paragraph=Quaternions
category=Data Processing
url=https://github.com/walchko
architectures=*
54 changes: 54 additions & 0 deletions cpp/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
![](https://upload.wikimedia.org/wikipedia/commons/thumb/d/d5/Inscription_on_Broom_Bridge_%28Dublin%29_regarding_the_discovery_of_Quaternions_multiplication_by_Sir_William_Rowan_Hamilton.jpg/800px-Inscription_on_Broom_Bridge_%28Dublin%29_regarding_the_discovery_of_Quaternions_multiplication_by_Sir_William_Rowan_Hamilton.jpg)

# squaternion.cpp

This is a c++ version of my python `squaternion` library.

## Install Arduino

You can either put it in your `Documents/Arduino/libraries` folder or make a symbolic link from where you keep the folder to where the Arduino IDE expects it to be. I store all of my github repo's in a `github` folder, so I know where they are.

```bash
ln -s <where you keep it> ~/Documents/Arduino/libraries
```

## Install Computer

You can use standard `cmake` commands to use this.

```bash
cd squaternion.cpp
mkdir build
cd build
cmake ..
build
```

## Todo

- [ ] Make a CPM
- [x] Should I make this header only? It is pretty simple
- [ ] Add `gtest` tests like `python` version
- [x] Add a simple Arduino compile test

# MIT License

**Copyright (c) 2022 Mom's Friendly Robot Company**

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 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.
Loading

0 comments on commit d3562a6

Please sign in to comment.