Giter Club home page Giter Club logo

vortice.mathematics's Introduction

Vortice.Mathematics

License: MIT Build status NuGet

Vortice.Mathematics is a high-performance cross platform .NET 7 and .NET 8 math library.

Sponsors

Please consider becoming a SPONSOR to further help development and to allow faster issue triaging and new features to be implemented. NOTE: any feature request would require a sponsor in order to allow faster implementation and allow this project to continue.

Credits

Library development, contributions and bugfixes by:

  • Amer Koleci

Insipration, ideas and code has been based on:

vortice.mathematics's People

Contributors

amerkoleci avatar andrew-boyarshin avatar gitii avatar h1cks avatar roy-t avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

vortice.mathematics's Issues

Suggestion - "To do" readme file

Suggest if you are happy for any contributions also, creating a to do list on your API. Happy to contribute and grow it.

I know your time is limited and the community would be happy to do add to your code base.

Feel free to close once reviewed.

Unproject - Possible incorrect?

Hi,

Using the unproject to determine a raycast into the screen (0, 1) range.

I get values starting near 0 to a very large value. The implementation differs from the Sharpdx version where I call

Vector3 screenSpacePoint = new Vector3(mousex, mousey, 0);

 Matrix4x4.Invert(camera.WVPMatrix, out Matrix4x4 inverse);

Vector3 point = Unproject(camera.ViewPort, screenSpacePoint, inverse);

I tested this against the vortice implementation and getting very different result.

The underlying code for the unproject is:

      public static Vector3 Unproject(Viewport vp,   Vector3 source,  Matrix4x4 matrix)
        {
            Vector3 vector_ = new Vector3();

            vector_.X = (((source.X - vp.X) / (vp.Width)) * 2f) - 1f;
            vector_.Y = -((((source.Y - vp.Y) / (vp.Height)) * 2f) - 1f);
            vector_.Z = (source.Z - vp.MinDepth) / (vp.MaxDepth - vp.MinDepth);

            float a = (((vector_.X * matrix.M14) + (vector_.Y * matrix.M24)) + (vector_.Z * matrix.M34)) + matrix.M44;
            //Vector3.Transform(ref vector, ref matrix, out vector);

            vector_ = Vector3.Transform(vector_, matrix);

            if (!IsOne(a))
            {
                vector_ = (vector_ / a);
            }

            return vector_;
        }

        public const float ZeroTolerance = 1e-6f; // Value a 8x higher than 1.19209290E-07F

        public static bool IsOne(float a)
        {
            return IsZero(a - 1.0f);
        }

        public static bool IsZero(float a)
        {
            return Math.Abs(a) < ZeroTolerance;
        }

Happy to provide more samples, but this maybe down to my use fundamentally of the current unproject call.

Matrix5x4 constructor is missing

A Matrix5x4 instance cannot be created because the constructor does not exist.
Just like Matrix3x3 and other Matrix structs, I want the following constructors.
Matrix5x4(float value) Matrix5x4(float m11, float m12, ...)

Like this constructor:

public Matrix4x3(float value)
{
M11 = M12 = M13 =
M21 = M22 = M23 =
M31 = M32 = M33 =
M41 = M42 = M43 = value;
}
/// <summary>
/// Initializes a new instance of the <see cref="Matrix4x3"/> struct.
/// </summary>
/// <param name="m11">The value to assign at row 1 column 1 of the Matrix4x3.</param>
/// <param name="m12">The value to assign at row 1 column 2 of the Matrix4x3.</param>
/// <param name="m13">The value to assign at row 1 column 3 of the Matrix4x3.</param>
/// <param name="m21">The value to assign at row 2 column 1 of the Matrix4x3.</param>
/// <param name="m22">The value to assign at row 2 column 2 of the Matrix4x3.</param>
/// <param name="m23">The value to assign at row 2 column 3 of the Matrix4x3.</param>
/// <param name="m31">The value to assign at row 3 column 1 of the Matrix4x3.</param>
/// <param name="m32">The value to assign at row 3 column 2 of the Matrix4x3.</param>
/// <param name="m33">The value to assign at row 3 column 3 of the Matrix4x3.</param>
/// <param name="m41">The value to assign at row 4 column 1 of the Matrix4x3.</param>
/// <param name="m42">The value to assign at row 4 column 2 of the Matrix4x3.</param>
/// <param name="m43">The value to assign at row 4 column 3 of the Matrix4x3.</param>
public Matrix4x3(
float m11, float m12, float m13,
float m21, float m22, float m23,
float m31, float m32, float m33,
float m41, float m42, float m43)
{
M11 = m11; M12 = m12; M13 = m13;
M21 = m21; M22 = m22; M23 = m23;
M31 = m31; M32 = m32; M33 = m33;
M41 = m41; M42 = m42; M43 = m43;
}

Can't compile with net native

Description (optional)

MCG0017: MCG0017:NotWinRTType Cannot marshal generic Windows Runtime types with a non Windows Runtime type as a generic type argument. Check the generic arguments of type 'System.Nullable`1<Vortice.Mathematics.Color4>'

Actual Behavior

It runs in debug mode, but won't compile in release mode

Ray Cast Class

Hi, for functional parity, is there consideration of pulling the Ray class from Sharpdx across into Vortice Mathematics?

Proposal: Optional Higher Accuracy Unproject

I've been doing a fair few hours work around why unproject seems to lose accuracy as you get further from 0. The obvious answer is floating point accuracy.

I did play around with double, but essentially the issue is that due to the Matrices also being float, you simply can't just create data out of thin air. (float is substantially more accurate closer to 0 anyway obviously)

I did though figure out a novel approach that will work in most circumstances that drastically improved the accuracy.

What the approach does below is translate the point to the origin, maintaining the orientation (scale other than 1 will not work atm).

Once the unproject is complete, I add the translation back on, which improves the accuracy substantially.

The view matrix needs to be generated without translations, and you can't eliminate from the WVP obviously.

Proposal, that a new unproject in the view class is created (I can do the pull request) that takes in Projection, View and ScreenPosition and returns the world position.

The accuracy is improved by about 90% once your world coordinates get > 1000. I've tested it multiple times and found that the ray projection resolution is now closer to the screen resolution, before, its sub 10% at high world coordinates.

///camera function

        public Matrix4x4 GenerateNonTranslatedWVP()
        {
            Matrix4x4 viewMatrix  = Matrix4x4.CreateLookAt(_position, _targetPosition, Constants.s_upVector);

            viewMatrix.Translation = Vector3.Zero;

            Matrix4x4 projectionMatrix = Matrix4x4.CreatePerspectiveFieldOfView(c_gameFov, _viewport.AspectRatio, 0.01f, _unitDepth);

            return viewMatrix * projectionMatrix;
        }

/// test code

     public static Vector3 Determine3dPointFrom2d(Camera3d camera, in Vector3 screenSpacePoint)
        {
            Matrix4x4 nonTranslated = camera.GenerateNonTranslatedWVP();

            Matrix4x4.Invert(nonTranslated, out Matrix4x4 inverse);

            Vector3 nonTrans_ = camera.ViewPort.Unproject(screenSpacePoint, inverse);

            nonTrans_ += camera.Position;

            return nonTrans_;// Unproject(camera.ViewPort, screenSpacePoint, inverse);
        }

Thoughts?

Title: System.Half conflict in .Net 5

Error CS0433: The type 'Half' exists in both 'Vortice.Mathematics, Version=1.4.0.0, Culture=neutral, PublicKeyToken=5431ec61a7e925da' and 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

vortice.mathematics is missing NuGet package README file

We've noticed that your package on NuGet.org is missing a README file.

Why READMEs are Important

Our customer research indicates that one of the top problems that package consumers face is insufficient package documentation such as README files. Adding a README file to your package will help users quickly understand what the package is and what it does. Since your README will be the first impression for users when they view your package on NuGet.org, it is crucial for authors to write and include high-quality READMEs for their packages.

Get Started with READMEs

How to Add a README

If you're new to NuGet READMEs, follow guidance on how to add one to your package.

How to write high quality of READMEs

Follow a blog post with README best practices and a template file to help you get started.

We Value Your Feedback

We value your feedback. If you encounter any issues or have suggestions, please reply to this issue.

Thank you for your contribution to the NuGet community.

Downgrade System.Runtime.CompilerServices.Unsafe

Vortice.Mathematics uses System.Runtime.CompilerServices.Unsafe in version 6.0.0 as a dependency. Would it be possible to downgrade the version to 4.7.0?

This will increase compatibility with other products that also use System.Runtime.CompilerServices.Unsafe as a dependency.

In the versions <= 1.9.143 the library System.Runtime.CompilerServices.Unsafe was not used at all. There the compatibility had been higher.

Rect.ToString format seems incorrect

Description (optional)

=> $"{nameof(Rect)} {{ {nameof(X)} = {X.ToString(format, formatProvider)}, {{ {nameof(Y)} = {Y.ToString(format, formatProvider)}, {{ {nameof(Width)} = {Width.ToString(format, formatProvider)}, {nameof(Height)} = {Height.ToString(format, formatProvider)} }}";

Reproduction Steps

Rect rect = new ();
Console.WriteLine(rect);

Expected Behavior

Rect { X = 0, Y = 0 }, { Width = 0, Height = 0 }

Actual Behavior

Rect { X = 0, { Y = 0, { Width = 0, Height = 0 }

Clarify Ray.Intersects works on clock-wise triangles if you are using Z+ is forward.

Hi, long time since we spoke ๐Ÿ˜„!

Wanted to share this small nitpick with you.

The method public readonly bool Intersects( in Vector3 v0, in Vector3 v1, in Vector3 v2, out Vector3 pointInTriangle) on the Ray struct has the following disclaimer:

This ONLY works on CW wound triangles.

But what is clock-wise depends on the unit system the user is using. As a DirectX user, I assumed Z- is forward. But it looks like this code uses the OpenGL convention in which Z+ is forward. Which means that every triangle that I believe to be clockwise, is actually counter-clockwise.

A small proof:

    [Fact]
    public void RayTriangleIntersectionTest()
    {
        var position = new Vector3(2.75f, 1000.0f, 1.5f);
        var direction = new Vector3(0.0f, -1.0f, 0.0f);
        var ray = new Ray(position, direction);

        // nw---ne
        //  \    |
        //   \   |
        //    \ se

        var nw = new Vector3(2, 18, 1);
        var ne = new Vector3(3, 18, 1);
        var se = new Vector3(3, 18, 2);

        var hitCW = ray.Intersects(nw, ne, se, out var pitCW);
        var hitCCW = ray.Intersects(se, ne, nw, out var pitCCW);
        True(hitCW);
        False(hitCCW);
    }

It would be nice to add a small clarification, so that users double check what convention they are using.

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.