Skip to content

Repository files navigation

mathLib

A small, header-only C++17 mathematics library for 2D/3D work: fixed-size vectors, fixed-size matrices, and a simple scene graph. Everything has value semantics and dimensions are encoded in the type, so size mismatches are caught at compile time.

Requirements

  • A C++17 compiler (g++, clang++, or MSVC)
  • No external dependencies (standard library only)

Components

HeaderProvides
vec.hVector<T, N> — generic fixed-size vector (base class)
vec2.hvec2<T> with .x() / .y() accessors and 2D cross
vec3.hvec3<T> with .x() / .y() / .z() accessors and 3D cross
vec4.hvec4<T> with .x() / .y() / .z() / .w() accessors
matrix.hmatrix<T, M, N> — fixed-size matrix
SceneGraph.hSceneNode — hierarchical scene graph
Mesh.hMinimal Mesh / OGLRenderer stubs used by the scene graph

Vectors

#include"vec3.h"
vec3<float> a(1, 2, 3);
vec3<float> b(4, 5, 6);
auto sum = a + b; // component-wise addauto diff = a - b; // component-wise subtractauto had = a * b; // component-wise (Hadamard) productauto scaled = a * 2.0f; // scalar multiply (also 2.0f * a)float d = a.dot(b); // dot productauto c = a.cross(b); // cross product (vec3)float len = a.length(); // magnitudeauto unit = a.normalized(); // unit-length copyfloat xs = a.x(); // named accessor
a[0] = 10; // indexed access

Also available: operator+= -= *= /=, unary -, operator== / !=, lengthSquared(), and in-place normalize(). vec2::cross returns the scalar signed area (z-component of the 3D cross product).

Matrices

#include"matrix.h"
matrix<float, 2, 3> a;
a(0, 0) = 1.0f; // (row, col) accessauto i = matrix<float, 4, 4>::identity();
auto t = a.transpose(); // 3x2auto p = a * b; // (2x3)*(3x2) -> 2x2, checked at compile timeauto s = 2.0f * a; // scalar multiplyfloat dt = c.det(); // determinant (square matrices only)

Dimension mismatches (e.g. multiplying incompatible sizes, or taking the determinant of a non-square matrix) are compile-time errors.

Scene graph

#include"SceneGraph.h"
SceneNode root;
SceneNode* child = new SceneNode(); // ownership transfers on AddChild
root.AddChild(child);
root.Update(0.0f); // propagates world transforms

A node's world transform is parent.worldTransform * localTransform. Mesh.h provides placeholder Mesh / OGLRenderer types so the scene graph compiles standalone; swap them for your real rendering backend.

Building the demo

g++ -std=c++17 -Wall -Wextra Source.cpp -o mathlib_demo
./mathlib_demo

License

See LICENSE.

About

implementation includes vectors, matrices

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages