Compare commits
12 Commits
79997dbce2
...
0.2.2
| Author | SHA1 | Date | |
|---|---|---|---|
| da2539692a | |||
| acbbbc4436 | |||
| 6cb9b4584c | |||
| 3ef3035cb5 | |||
| 80a9457876 | |||
| 9c33df5256 | |||
| 0279742c0e | |||
| 4d9fd00784 | |||
| e9df33520a | |||
| 11c891c2f1 | |||
| fdb5dfd75e | |||
| 5e70354ec2 |
@@ -1,6 +1,6 @@
|
||||
name: CI
|
||||
|
||||
on: [push, pull_request]
|
||||
on: [ push, pull_request ]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
||||
@@ -1,27 +1,60 @@
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
project(curtle VERSION 0.1.1 LANGUAGES C)
|
||||
project(curtle VERSION 0.2.2 LANGUAGES C)
|
||||
|
||||
add_library(curtle
|
||||
src/vector.c
|
||||
)
|
||||
include(GNUInstallDirs)
|
||||
include(CMakePackageConfigHelpers)
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_C_STANDARD_REQUIRED ON)
|
||||
|
||||
add_library(curtle STATIC src/vector.c)
|
||||
add_library(curtle::curtle ALIAS curtle)
|
||||
|
||||
target_include_directories(curtle
|
||||
PUBLIC
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
||||
$<INSTALL_INTERFACE:include>
|
||||
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
|
||||
)
|
||||
|
||||
target_link_libraries(curtle PUBLIC m)
|
||||
|
||||
install(TARGETS curtle
|
||||
EXPORT curtleTargets
|
||||
LIBRARY DESTINATION lib
|
||||
ARCHIVE DESTINATION lib
|
||||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
||||
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
|
||||
)
|
||||
|
||||
install(DIRECTORY include/ DESTINATION include)
|
||||
install(
|
||||
DIRECTORY include/
|
||||
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
|
||||
)
|
||||
|
||||
enable_testing()
|
||||
install(EXPORT curtleTargets
|
||||
FILE curtleTargets.cmake
|
||||
NAMESPACE curtle::
|
||||
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/curtle
|
||||
)
|
||||
|
||||
add_executable(test_vector tests/test_vector.c)
|
||||
target_link_libraries(test_vector curtle)
|
||||
write_basic_package_version_file(
|
||||
${CMAKE_CURRENT_BINARY_DIR}/curtleConfigVersion.cmake
|
||||
VERSION ${PROJECT_VERSION}
|
||||
COMPATIBILITY SameMajorVersion
|
||||
)
|
||||
|
||||
add_test(NAME vector_test COMMAND test_vector)
|
||||
configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/curtleConfig.cmake"
|
||||
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/curtle
|
||||
)
|
||||
|
||||
install(FILES
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/curtleConfig.cmake"
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/curtleConfigVersion.cmake"
|
||||
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/curtle
|
||||
)
|
||||
|
||||
if(${PROJECT_IS_TOP_LEVEL})
|
||||
enable_testing()
|
||||
add_subdirectory(tests)
|
||||
endif()
|
||||
5
Config.cmake.in
Normal file
5
Config.cmake.in
Normal file
@@ -0,0 +1,5 @@
|
||||
@PACKAGE_INIT@
|
||||
|
||||
include("${CMAKE_CURRENT_LIST_DIR}/curtleTargets.cmake")
|
||||
|
||||
check_required_components(curtle)
|
||||
56
README.md
56
README.md
@@ -1,3 +1,57 @@
|
||||
|
||||

|
||||
|
||||
|
||||
# curtle
|
||||
|
||||
Curtle is a smart turtle that helps you with math tasks.
|
||||
curtle is a simple open source cross platform math library for C.
|
||||
## Roadmap
|
||||
The goal of curtle is to provide an easy to use and easy to integrate math library. If you are interested in specific features please reach out to me!
|
||||
|
||||
- add much math tooling
|
||||
- provide easy install via package managers etc.
|
||||
|
||||
|
||||
## Features
|
||||
|
||||
- Basic operations on 2D Vectors
|
||||
- Basic operations on 3D Vectors
|
||||
|
||||
|
||||
## Installation
|
||||
|
||||
Clone the repository:
|
||||
```
|
||||
git clone https://git.lowearthorbit.de/lowearthorbit/curtle.git
|
||||
cd curtle
|
||||
```
|
||||
You can build and install the library using cmake:
|
||||
```
|
||||
mkdir build && cd build
|
||||
cmake ..
|
||||
sudo cmake --install .
|
||||
```
|
||||
## Usage/Examples
|
||||
|
||||
Once installed, you can use curtle with CMake:
|
||||
|
||||
```CMake
|
||||
find_package(curtle REQUIRED)
|
||||
|
||||
add_executable(myapp main.c)
|
||||
target_link_libraries(myapp PRIVATE curtle::curtle)
|
||||
```
|
||||
|
||||
Example in C
|
||||
```C
|
||||
#include "curtle/curtle.h"
|
||||
|
||||
int main(void) {
|
||||
struct vec2 a = { 1.0, 1.0 };
|
||||
printf("%f", vec2_len(a)); // Prints sqrt(2)
|
||||
}
|
||||
```
|
||||
## Authors
|
||||
|
||||
- [@lowearthorbit](https://git.lowearthorbit.de/lowearthorbit)
|
||||
|
||||
|
||||
@@ -1,12 +1,43 @@
|
||||
#ifndef CURTLE_VECTOR_H
|
||||
#define CURTLE_VECTOR_H
|
||||
|
||||
struct vec2
|
||||
{
|
||||
double x;
|
||||
double y;
|
||||
struct vec2 {
|
||||
float x;
|
||||
float y;
|
||||
};
|
||||
|
||||
struct vec3 {
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
};
|
||||
|
||||
struct vec2 vec2_add(struct vec2 a, struct vec2 b);
|
||||
|
||||
struct vec2 vec2_sub(struct vec2 a, struct vec2 b);
|
||||
|
||||
float vec2_dot(struct vec2 a, struct vec2 b);
|
||||
|
||||
float vec2_det(struct vec2 a, struct vec2 b);
|
||||
|
||||
struct vec2 vec2_scale(struct vec2 v, float s);
|
||||
|
||||
float vec2_len(struct vec2 v);
|
||||
|
||||
struct vec2 vec2_normalize(struct vec2 v);
|
||||
|
||||
struct vec3 vec3_add(struct vec3 a, struct vec3 b);
|
||||
|
||||
struct vec3 vec3_sub(struct vec3 a, struct vec3 b);
|
||||
|
||||
float vec3_dot(struct vec3 a, struct vec3 b);
|
||||
|
||||
struct vec3 vec3_cross(struct vec3 a, struct vec3 b);
|
||||
|
||||
struct vec3 vec3_scale(struct vec3 v, float s);
|
||||
|
||||
float vec3_len(struct vec3 v);
|
||||
|
||||
struct vec3 vec3_normalize(struct vec3 v);
|
||||
|
||||
#endif
|
||||
71
src/vector.c
71
src/vector.c
@@ -1,6 +1,73 @@
|
||||
#include "curtle/vector.h"
|
||||
#include <math.h>
|
||||
|
||||
struct vec2 vec2_add(struct vec2 a, struct vec2 b)
|
||||
struct vec2 vec2_add(const struct vec2 a, const struct vec2 b)
|
||||
{
|
||||
return (struct vec2){a.x + b.x, a.y + b.y};
|
||||
return (struct vec2){ a.x + b.x, a.y + b.y };
|
||||
}
|
||||
|
||||
struct vec2 vec2_sub(const struct vec2 a, const struct vec2 b)
|
||||
{
|
||||
return (struct vec2){ a.x - b.x, a.y - b.y };
|
||||
}
|
||||
|
||||
float vec2_dot(const struct vec2 a, const struct vec2 b)
|
||||
{
|
||||
return a.x * b.x + a.y * b.y;
|
||||
}
|
||||
|
||||
float vec2_det(const struct vec2 a, const struct vec2 b)
|
||||
{
|
||||
return a.x * b.y - a.y * b.x;
|
||||
}
|
||||
|
||||
struct vec2 vec2_scale(const struct vec2 v, const float s)
|
||||
{
|
||||
return (struct vec2){ v.x * s, v.y * s };
|
||||
}
|
||||
|
||||
float vec2_len(const struct vec2 v)
|
||||
{
|
||||
return sqrtf(v.x * v.x + v.y * v.y);
|
||||
}
|
||||
|
||||
struct vec2 vec2_normalize(const struct vec2 v)
|
||||
{
|
||||
return vec2_scale(v, 1.0f / vec2_len(v));
|
||||
}
|
||||
|
||||
struct vec3 vec3_add(const struct vec3 a, const struct vec3 b)
|
||||
{
|
||||
return (struct vec3){ a.x + b.x, a.y + b.y, a.z + b.z };
|
||||
}
|
||||
|
||||
struct vec3 vec3_sub(const struct vec3 a, const struct vec3 b)
|
||||
{
|
||||
return (struct vec3){ a.x - b.x, a.y - b.y, a.z - b.z };
|
||||
}
|
||||
|
||||
float vec3_dot(const struct vec3 a, const struct vec3 b)
|
||||
{
|
||||
return a.x * b.x + a.y * b.y + a.z * b.z;
|
||||
}
|
||||
|
||||
struct vec3 vec3_cross(const struct vec3 a, const struct vec3 b)
|
||||
{
|
||||
return (struct vec3){ a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z,
|
||||
a.x * b.y - a.y * b.x };
|
||||
}
|
||||
|
||||
struct vec3 vec3_scale(const struct vec3 v, const float s)
|
||||
{
|
||||
return (struct vec3){ v.x * s, v.y * s, v.z * s };
|
||||
}
|
||||
|
||||
float vec3_len(const struct vec3 v)
|
||||
{
|
||||
return sqrtf(v.x * v.x + v.y * v.y + v.z * v.z);
|
||||
}
|
||||
|
||||
struct vec3 vec3_normalize(const struct vec3 v)
|
||||
{
|
||||
return vec3_scale(v, 1.0f / vec3_len(v));
|
||||
}
|
||||
15
tests/CMakeLists.txt
Normal file
15
tests/CMakeLists.txt
Normal file
@@ -0,0 +1,15 @@
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
|
||||
include(FetchContent)
|
||||
set(UNITY_VERSION v2.6.1)
|
||||
|
||||
FetchContent_Declare(unity
|
||||
GIT_REPOSITORY https://github.com/ThrowTheSwitch/Unity.git
|
||||
GIT_TAG ${UNITY_VERSION}
|
||||
)
|
||||
FetchContent_MakeAvailable(unity)
|
||||
|
||||
add_executable(test_vector test_vector.c)
|
||||
target_link_libraries(test_vector PRIVATE curtle unity)
|
||||
|
||||
add_test(NAME vector_test COMMAND test_vector)
|
||||
@@ -1,13 +1,176 @@
|
||||
#include <assert.h>
|
||||
#include "unity.h"
|
||||
#include "curtle/vector.h"
|
||||
|
||||
int main()
|
||||
void setUp(void)
|
||||
{
|
||||
struct vec2 a = {1, 2};
|
||||
struct vec2 b = {3, 4};
|
||||
}
|
||||
|
||||
struct vec2 r = vec2_add(a, b);
|
||||
void tearDown(void)
|
||||
{
|
||||
}
|
||||
|
||||
assert(r.x == 4);
|
||||
assert(r.y == 6);
|
||||
void test_vec2_add()
|
||||
{
|
||||
struct vec2 a = { 1, 2 };
|
||||
struct vec2 b = { 3, 4 };
|
||||
|
||||
struct vec2 r = vec2_add(a, b);
|
||||
|
||||
struct vec2 e = { 4, 6 };
|
||||
TEST_ASSERT_EQUAL_MEMORY(&e, &r, 8);
|
||||
}
|
||||
|
||||
void test_vec2_sub()
|
||||
{
|
||||
struct vec2 a = { 1, 2 };
|
||||
struct vec2 b = { 3, 4 };
|
||||
|
||||
struct vec2 r = vec2_sub(a, b);
|
||||
|
||||
struct vec2 e = { -2, -2 };
|
||||
TEST_ASSERT_EQUAL_MEMORY(&e, &r, 8);
|
||||
}
|
||||
|
||||
void test_vec2_dot()
|
||||
{
|
||||
struct vec2 a = { 1, 2 };
|
||||
struct vec2 b = { 3, 4 };
|
||||
|
||||
float r = vec2_dot(a, b);
|
||||
|
||||
TEST_ASSERT_EQUAL_FLOAT(11, r);
|
||||
}
|
||||
|
||||
void test_vec2_det()
|
||||
{
|
||||
struct vec2 a = { 1, 2 };
|
||||
struct vec2 b = { 3, 4 };
|
||||
|
||||
float r = vec2_det(a, b);
|
||||
|
||||
TEST_ASSERT_EQUAL_FLOAT(-2, r);
|
||||
}
|
||||
|
||||
void test_vec2_scale()
|
||||
{
|
||||
struct vec2 a = { 1, 2 };
|
||||
float s = 2;
|
||||
|
||||
struct vec2 r = vec2_scale(a, 2);
|
||||
|
||||
struct vec2 e = { 2, 4 };
|
||||
TEST_ASSERT_EQUAL_MEMORY(&e, &r, 8);
|
||||
}
|
||||
|
||||
void test_vec2_len()
|
||||
{
|
||||
struct vec2 a = { 4, 3 };
|
||||
|
||||
float r = vec2_len(a);
|
||||
|
||||
TEST_ASSERT_EQUAL_FLOAT(5, r);
|
||||
}
|
||||
|
||||
void test_vec2_normalize()
|
||||
{
|
||||
struct vec2 a = { 4, 0 };
|
||||
|
||||
struct vec2 r = vec2_normalize(a);
|
||||
|
||||
struct vec2 e = { 1, 0 };
|
||||
TEST_ASSERT_EQUAL_MEMORY(&e, &r, 8);
|
||||
}
|
||||
|
||||
void test_vec3_add()
|
||||
{
|
||||
struct vec3 a = { 1, 2, 5 };
|
||||
struct vec3 b = { 3, 4, 2 };
|
||||
|
||||
struct vec3 r = vec3_add(a, b);
|
||||
|
||||
struct vec3 e = { 4, 6, 7 };
|
||||
TEST_ASSERT_EQUAL_MEMORY(&e, &r, 12);
|
||||
}
|
||||
|
||||
void test_vec3_sub()
|
||||
{
|
||||
struct vec3 a = { 1, 2, 5 };
|
||||
struct vec3 b = { 3, 4, 2 };
|
||||
|
||||
struct vec3 r = vec3_sub(a, b);
|
||||
|
||||
struct vec3 e = { -2, -2, 3 };
|
||||
TEST_ASSERT_EQUAL_MEMORY(&e, &r, 12);
|
||||
}
|
||||
|
||||
void test_vec3_dot()
|
||||
{
|
||||
struct vec3 a = { 1, 2, 5 };
|
||||
struct vec3 b = { 3, 4, 2 };
|
||||
|
||||
float r = vec3_dot(a, b);
|
||||
|
||||
TEST_ASSERT_EQUAL_FLOAT(21, r);
|
||||
}
|
||||
|
||||
void test_vec3_cross()
|
||||
{
|
||||
struct vec3 a = { 1, 2, 5 };
|
||||
struct vec3 b = { 3, 4, 2 };
|
||||
|
||||
struct vec3 r = vec3_cross(a, b);
|
||||
|
||||
struct vec3 e = { -16, 13, -2 };
|
||||
TEST_ASSERT_EQUAL_MEMORY(&e, &r, 12);
|
||||
}
|
||||
|
||||
|
||||
void test_vec3_scale()
|
||||
{
|
||||
struct vec3 a = { 1, 2, 5 };
|
||||
float s = 2;
|
||||
|
||||
struct vec3 r = vec3_scale(a, 2);
|
||||
|
||||
struct vec3 e = { 2, 4, 10 };
|
||||
TEST_ASSERT_EQUAL_MEMORY(&e, &r, 12);
|
||||
}
|
||||
|
||||
void test_vec3_len()
|
||||
{
|
||||
struct vec3 a = { 4, 3, 12 };
|
||||
|
||||
float r = vec3_len(a);
|
||||
|
||||
TEST_ASSERT_EQUAL_FLOAT(13, r);
|
||||
}
|
||||
|
||||
void test_vec3_normalize()
|
||||
{
|
||||
struct vec3 a = { 1, 2, 2 };
|
||||
|
||||
struct vec3 r = vec3_normalize(a);
|
||||
|
||||
struct vec3 e = { 1.f / 3.f, 2.f / 3.f, 2.f / 3.f };
|
||||
TEST_ASSERT_EQUAL_MEMORY(&e, &r, 12);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
UNITY_BEGIN();
|
||||
RUN_TEST(test_vec2_add);
|
||||
RUN_TEST(test_vec2_sub);
|
||||
RUN_TEST(test_vec2_dot);
|
||||
RUN_TEST(test_vec2_det);
|
||||
RUN_TEST(test_vec2_scale);
|
||||
RUN_TEST(test_vec2_len);
|
||||
RUN_TEST(test_vec2_normalize);
|
||||
RUN_TEST(test_vec3_add);
|
||||
RUN_TEST(test_vec3_sub);
|
||||
RUN_TEST(test_vec3_dot);
|
||||
RUN_TEST(test_vec3_cross);
|
||||
RUN_TEST(test_vec3_scale);
|
||||
RUN_TEST(test_vec3_len);
|
||||
RUN_TEST(test_vec3_normalize);
|
||||
return UNITY_END();
|
||||
}
|
||||
Reference in New Issue
Block a user