Giter VIP home page Giter VIP logo

anathema.vectors's Introduction

Anathema Vectors

Basic linear algebra, mostly for computer graphics.

Why?

System.Numerics contains a native SIMD-enabled Vector class. It's extremely fast and is what most developers would expect. Why not just use that?

Anathema Vectors has a few benefits:

  • Rich feature set: Broad support for a wide range of features.
  • Dependency-free: Builds with nothing more than .NET Fx 2.0 - compatible with any platform newer than that (including transpilers).
  • Convenience: Swizzles and easy interoperation between types.
  • Modularity: Compile time preprocessor directives allow you to chop the library down to the features you're using, and nothing more.
  • Interoperability: Aliases for many System.Numerics members, so code can be swapped over with minimal changes.
  • Familiarity: Syntax is as close to that of GLSL as practical.
  • Full test suite: 100% test coverage.

Approach

  • The notation is terse and annotations are minimal. The assumption is that you either have a decent understanding of basic linear algebra, or that you are willing to learn it on your own.
    • I acknowledge that this is a major structural problem in maths. "It's not my job to teach you how to understand this" becomes quite toxic when the entire field takes that attitude for almost everything. However, this is not the problem I'm trying to solve right now.
    • I intend on making the annotations more helpful -- give some explanation of what the members actually do. But the actual names of properties/methods will have to remain terse.

Notation

d = double precision floating point data type

f = single precision floating point data type

t = generic (template) defined data type

mat = matrix

vec = vector

quat = quaternion

Number at end = number of components (per row/column for matrices).

So, for example, dmat2 = double precision floating point 2x2 matrix.

Or, tvec3<int> = 32-bit integer 3-component vector.

Nested Template Vectors

  • One quirk of the use of templates which are not constrained to numeric values is that a template vector can use another vector as its template type.

    • I am aware that .NET 7 allows the use of an INumeric<T> constraint on template types, obviating the need for explicit conversions. This project is not targeting .NET 7.
  • This means that you can construct a matrix out of template vectors

  • You can even take it further and create a tensor if you're feeling keen

  • I do not know that this is actually a useful feature but it's kind of a cool emergent property we get mostly for free

  • It has the interesting property that if we access vector.x.y, we get row 0 column 1 of the matrix - which is of course the mapping of x to y when you multiply a vector with that matrix. Might be a useful/interesting educational tool.

Swizzles

  • This idea is implemented almost exactly like GLSL. If you have a vec3 with components x, y and z, you can mix and match different components in different orders as needed - for example, fvec3.zxy.
  • Similar to GLSL, there are swizzle aliases for rgba -> xyzw.
  • A nice property of this is that if you ask for a 2-vector out of a larger one, you can trivially convert the output to polar notation to turn it into an angle. This means that you can go fvec3.xz.angle and get a useful number.

Gotchas/Limitations

  • Matrices and vectors (except 2) are subclasses of the rank lower than it (ie a vec4 is a subclass of vec3 which is a subclass of vec2). This cuts out a lot of redundant code, and makes porting data between them easier, but it also means that if a method asks for a vec2 you can pass it a vec4 instead. Which will work in a sense, but could cause problems later.
  • Performance is pretty lousy, both from the extensive use of object oriented language features, and the lack of SIMD vector CPU extension usage.
  • Interactions between the various entities are extensive (you can usually mix and match types), but not exhaustive (there will still be times where you have to do intermediate steps to get it done).
  • The folders inside Vector.Core do not reflect the namespaces the classes are in. This is deliberate. The proliferation of partial classes mean there is a large volume of files which need to be rationalised, but the namespace needs to be flat for usability reasons.
  • Unlike GLSL, you cannot use a swizzle value more than once (ie vec3.xyy). This is out of simple pragmatism, as there would be a large number of permutations.
  • No native support for non-square matrices (ie mat4x2), though you can construct those out of nested template vectors if you really need them.
  • No template matrices. Narrow use case and a giant can of worms implementation-wise.

Complex Numbers and Quaternions

Todo: fill out

Compile-Time Flags

FLOATS_ENABLED
  • Everything to do with single precision floating point
DOUBLES_ENABLED
  • Everything to do with double precision floating point
NESTING_ENABLED
  • Template vectors which contain other template vectors (used to create effective matrices)
TEMPLATES_ENABLED
  • Template vectors (vectors which can have any other type of components)
SWIZZLES_ENABLED
  • Combining different subsets of a vector's components, possibly out of order, ie vec3.xy
  • When combined with THREED_ENABLED, significantly increases binary size.
COLOURS_ENABLED
  • Scalar and swizzle aliases for RGBA in the place of XYZW.
  • When combined with THREED_ENABLED, significantly increases binary size.
COMPLEX_ENABLED
  • Complex numbers and quaternion support
CONVERSIONS_ENABLED
  • Conversions between single and double precision floating point types
INTERPOLATION_ENABLED
  • Linear and Bezier interpolation of vectors
THREED_ENABLED
  • Three dimensional vectors, and the matrices and quaternions used to support them.
TEMPLATE_OPERATIONS_ENABLED
  • Arithmetic operators (+-/*) of template vectors.
  • Because of the lack of INumeric support, this significantly increases binary size.
TEMPLATE_DIVISION_ENABLED
  • Division of template vectors, which might have integer components. This is to preclude the possibility of zero divides.
  • Note: this does disable division for tvec<float> and tvec<double>, but of course you can just convert them to fvecs and dvecs.
    • The logic is that it's better to simply remove the option to divide tvecs than it is to throw a different exception if you try to do so for an integral type.
CAPITALS_ENABLED
  • Capital letter versions of the components (XYZW and RGBA) as well as Length and LengthSquared for compatibility with System.Numerics.Vectors.

Special Thanks

  • Arran Ubels for reviewing the code (and suggesting partial classes)
  • Peter Berlin for reviewing the code (and suggesting joining #ifs with Boolean operators)
  • Matthew Mack for reviewing some of the maths
  • Finn Morgan for suggesting vec4.cross products

anathema.vectors's People

Contributors

blame-lewis avatar

Stargazers

Finn Morgan avatar Matthew Mack avatar  avatar Arran Ubels avatar

Forkers

matthras

anathema.vectors's Issues

Matrix-matrix multiplication

Basic use case
Of different data types (float/double/T) #if CONVERSIONS_ENABLED
Of different order (2/3/4)
Don't bother with mixed data type+order difference

Add vec4.cross

Finn Morgan: "a vec4 mostly represents a vector in 3D space, and the w value is 0 if it's a direction and 1 if it's a position, because that determines whether it's affected by the translation part of a mat4 - if you're storing a vertex in a mesh as a vec4 (with w = 1), you might want to calculate the normal of a triangle with cross(p2-p1, p3-p1) - mathematically fake as hell, but convenient"

Consolidate test data

  • Simplify InlineData(values) rather than InlineData(new object[] { values })
  • Use ClassData rather than InlineData

Quaternions Checklist

Defining Object

  • Define quaternion: 4-length vector, all real numbers (floats)

Operations on Quaternions

  • Adding & subtracting
  • Scalar multiplication
  • Hamilton product (i.e. product)
  • Inverse
  • Dot product
  • Conjugate
  • Norm

Conversions

  • Axis-angle to quaternion
  • Quaternion to axis-angle
  • 3x3 Rotation matrix to quaternion
  • Quaternion to 3x3 rotation matrix

Sources

Add/edit new details as required

Complex Numbers Checklist

Object

  • 2-length vector, two real numbers

Operations on Complex Numbers

  • Addition & subtraction
  • Multiplying
  • Dividing
  • Conjugate
  • Norm
  • Argument (angle)

Conversions

  • Complex number to rotation matrix
  • [] Rotation matrix to complex number (to go into mat2.cs and dmat2.cs)

Pull Request Checks [Added 6/11]

  • [] Implementation
  • [] Unit tests
  • [] A section in the README that has been left for complex/quats
  • [] Tick off anything in the TODO file
  • [] Update the copyright to add yourself (last contributor goes to the top)

Update [14/11/23] Removed polar form conversions due to redundancy

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.