Skip to content
Snippets Groups Projects
Commit 4b2912ef authored by Philip Trettner's avatar Philip Trettner
Browse files

initial commit

parent c9e0403f
No related branches found
No related tags found
No related merge requests found
# Style file for clang-format ( http://clang.llvm.org/docs/ClangFormatStyleOptions.html )
# Style is based on google's c++ coding style.
# see http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml
BasedOnStyle: Google
#Language: Cpp
Standard: Cpp11
# 80 columns guideline
ColumnLimit: 150
PenaltyExcessCharacter: 1
PenaltyBreakString: 50
# Indentation and Braces
IndentWidth: 4
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: true
#BraceBreakingStyle: ???
BreakBeforeBinaryOperators: true
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakBeforeBraces: Allman
BinPackParameters: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
Cpp11BracedListStyle: true
IndentCaseLabels: false
# Spaces
DerivePointerAlignment: true
DerivePointerBinding: true
MaxEmptyLinesToKeep: 2
SpaceAfterControlStatementKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
#SpacesInSquareBrackets: false
UseTab: Never
ConstructorInitializerIndentWidth: 2
AccessModifierOffset: -4
# Comments
AlignTrailingComments: true
CommentPragmas: '!Api.*'
cmake_minimum_required(VERSION 3.8)
project(TypedMath)
# ===============================================
# Create target
file(GLOB_RECURSE SOURCES
"src/*.cc"
"src/*.hh"
)
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${SOURCES})
add_library(typed-math INTERFACE)
target_include_directories(typed-math INTERFACE "src")
# typed-math
Strongly typed math library for graphics.
\ No newline at end of file
Header-only strongly typed math library for graphics.
## Usage / Example
```
#include <tm/typed-math.hh>
tg::vec3 v;
// TODO
```
## Namespace `tg`
Why not `tm::vec3`?
* `struct tm` in `time.h` conflicts with this namespace
* `tgm` is too long
* This library closely works with `typed-graphics` anyways
## Dependencies
None.
#pragma once
#include <limits>
#include "types.hh"
namespace tg
{
template <class T>
constexpr T pi = static_cast<T>(3.14159265358979323846);
// TODO: proper min vs. lowest
template <class T>
constexpr T min = std::numeric_limits<T>::min();
template <class T>
constexpr T max = std::numeric_limits<T>::max();
}
#pragma once
#include <cstdint>
namespace tg
{
struct half
{
// TODO
private:
uint16_t data;
};
} // namespace tg
#pragma once
#include <cstdint>
namespace tg
{
struct quarter
{
// TODO
private:
std::uint8_t data;
};
} // namespace tg
#pragma once
#include <array>
#include <cassert>
namespace tg
{
template <int Order>
struct shape
{
static inline constexpr int order = Order;
std::array<int, order> dims;
public:
shape() = default;
constexpr shape(std::array<int, order> const& dims) : dims(dims) {}
public:
constexpr int operator[](int o) const
{
assert(0 <= o && o < order);
return dims[o];
}
template <int Order2>
constexpr bool operator==(shape<Order2> const& rhs) const
{
if constexpr (Order == Order2)
{
for (auto i = 0; i < Order; ++i)
if (dims[i] != rhs[i])
return false;
return true;
}
else
return false;
}
template <int Order2>
constexpr bool operator!=(shape<Order2> const& rhs) const
{
return !operator==(rhs);
}
};
inline constexpr shape<1> make_shape(int w) { return {{w}}; }
inline constexpr shape<2> make_shape(int w, int h) { return {{w, h}}; }
inline constexpr shape<3> make_shape(int w, int h, int d) { return {{w, h, d}}; }
template <int Order>
constexpr shape<Order> make_shape(std::array<int, Order> const& dims)
{
return {dims};
}
} // namespace tg
#pragma once
#include <cstdint>
#include "half.hh"
#include "quarter.hh"
#include "vec.hh"
namespace tg
{
// Scalar types
using i8 = std::int8_t;
using i16 = std::int16_t;
using i32 = std::int32_t;
using i64 = std::int64_t;
using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
using f8 = quarter;
using f16 = half;
using f32 = float;
using f64 = double;
// Common vector types
using vec1 = vec<1, f32>;
using vec2 = vec<2, f32>;
using vec3 = vec<3, f32>;
using vec4 = vec<4, f32>;
using bvec1 = vec<1, bool>;
using bvec2 = vec<2, bool>;
using bvec3 = vec<3, bool>;
using bvec4 = vec<4, bool>;
using fvec1 = vec<1, f32>;
using fvec2 = vec<2, f32>;
using fvec3 = vec<3, f32>;
using fvec4 = vec<4, f32>;
using dvec1 = vec<1, f64>;
using dvec2 = vec<2, f64>;
using dvec3 = vec<3, f64>;
using dvec4 = vec<4, f64>;
using ivec1 = vec<1, i32>;
using ivec2 = vec<2, i32>;
using ivec3 = vec<3, i32>;
using ivec4 = vec<4, i32>;
using uvec1 = vec<1, u32>;
using uvec2 = vec<2, u32>;
using uvec3 = vec<3, u32>;
using uvec4 = vec<4, u32>;
// Sized vector types
using i8vec1 = vec<1, i8>;
using i8vec2 = vec<2, i8>;
using i8vec3 = vec<3, i8>;
using i8vec4 = vec<4, i8>;
using i16vec1 = vec<1, i16>;
using i16vec2 = vec<2, i16>;
using i16vec3 = vec<3, i16>;
using i16vec4 = vec<4, i16>;
using i32vec1 = vec<1, i32>;
using i32vec2 = vec<2, i32>;
using i32vec3 = vec<3, i32>;
using i32vec4 = vec<4, i32>;
using i64vec1 = vec<1, i64>;
using i64vec2 = vec<2, i64>;
using i64vec3 = vec<3, i64>;
using i64vec4 = vec<4, i64>;
using u8vec1 = vec<1, u8>;
using u8vec2 = vec<2, u8>;
using u8vec3 = vec<3, u8>;
using u8vec4 = vec<4, u8>;
using u16vec1 = vec<1, u16>;
using u16vec2 = vec<2, u16>;
using u16vec3 = vec<3, u16>;
using u16vec4 = vec<4, u16>;
using u32vec1 = vec<1, u32>;
using u32vec2 = vec<2, u32>;
using u32vec3 = vec<3, u32>;
using u32vec4 = vec<4, u32>;
using u64vec1 = vec<1, u64>;
using u64vec2 = vec<2, u64>;
using u64vec3 = vec<3, u64>;
using u64vec4 = vec<4, u64>;
using f8vec1 = vec<1, f8>;
using f8vec2 = vec<2, f8>;
using f8vec3 = vec<3, f8>;
using f8vec4 = vec<4, f8>;
using f16vec1 = vec<1, f16>;
using f16vec2 = vec<2, f16>;
using f16vec3 = vec<3, f16>;
using f16vec4 = vec<4, f16>;
using f32vec1 = vec<1, f32>;
using f32vec2 = vec<2, f32>;
using f32vec3 = vec<3, f32>;
using f32vec4 = vec<4, f32>;
using f64vec1 = vec<1, f64>;
using f64vec2 = vec<2, f64>;
using f64vec3 = vec<3, f64>;
using f64vec4 = vec<4, f64>;
// Quaternion types
// TODO
// Matrix types
// TODO
} // namespace tg
#pragma once
#include "shape.hh"
namespace tg
{
template <int D, class ScalarT>
struct vec
{
};
template <class ScalarT>
struct vec<1, ScalarT>
{
using scalar_t = ScalarT;
static inline constexpr shape shape = make_shape(1);
scalar_t x = static_cast<scalar_t>(0);
vec() = default;
explicit vec(scalar_t v) : x(v) {}
};
template <class ScalarT>
struct vec<2, ScalarT>
{
using scalar_t = ScalarT;
static inline constexpr shape shape = make_shape(2);
scalar_t x = static_cast<scalar_t>(0);
scalar_t y = static_cast<scalar_t>(0);
vec() = default;
explicit vec(scalar_t v) : x(v), y(v) {}
vec(scalar_t x, scalar_t y) : x(x), y(y) {}
};
template <class ScalarT>
struct vec<3, ScalarT>
{
using scalar_t = ScalarT;
static inline constexpr shape shape = make_shape(3);
scalar_t x = static_cast<scalar_t>(0);
scalar_t y = static_cast<scalar_t>(0);
scalar_t z = static_cast<scalar_t>(0);
vec() = default;
explicit vec(scalar_t v) : x(v), y(v), z(v) {}
vec(scalar_t x, scalar_t y, scalar_t z) : x(x), y(y), z(z) {}
};
template <class ScalarT>
struct vec<4, ScalarT>
{
using scalar_t = ScalarT;
static inline constexpr shape shape = make_shape(4);
scalar_t x = static_cast<scalar_t>(0);
scalar_t y = static_cast<scalar_t>(0);
scalar_t z = static_cast<scalar_t>(0);
scalar_t w = static_cast<scalar_t>(0);
vec() = default;
explicit vec(scalar_t v) : x(v), y(v), z(v), w(v) {}
vec(scalar_t x, scalar_t y, scalar_t z, scalar_t w) : x(x), y(y), z(z), w(w) {}
};
} // namespace tg
#pragma once
#include "detail/constants.hh"
#include "detail/mat.hh"
#include "detail/quat.hh"
#include "detail/shape.hh"
#include "detail/types.hh"
#include "detail/vec.hh"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment