Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A simpler way to create a Vector2d from a Vector3d #336

Closed
xupwup opened this issue May 26, 2023 · 6 comments
Closed

A simpler way to create a Vector2d from a Vector3d #336

xupwup opened this issue May 26, 2023 · 6 comments

Comments

@xupwup
Copy link

xupwup commented May 26, 2023

I'd like to have an easier way to get a lower dimension vector from a higher dimension vector. Usually from 3 dimensions to two, but from 4 to 3 too.

Currently, the only way to do this is the following:

Vector3d a = new Vector3d(1, 2, 3);
Vector2d b = new Vector2d(a.x, a.y);

I'd like to have something like in GLSL:

Vector2d b = a.xy(); // the one you need 9 times out of 10
Vector2d c = a.xx();
Vector2d d = a.zy();

This mainly helps when the vector3d is obtained via a long function chain:

Vector3d a = getDirectionOfSomethingDifficult().cross(getOtherThing());
Vector2d b = new Vector2d(a.x, a.y);
double lengthIn2d = b.length();
@httpdigest
Copy link
Member

We could do this, but given that JOML wants to stay allocation-free, we would have to make it like this:

Vector2d b = a.xy(new Vector2d());

(of course this new Vector2d() can be another variable declared and allocated somewhere else)

@xupwup
Copy link
Author

xupwup commented May 26, 2023

That would be a great improvement. Alternatively, maybe you could return it as a view on the data? (through Vector2dc) EDIT: never mind, a view also requires you to allocate something.

Or just make it explicit in the function name that this function allocates something.

@httpdigest
Copy link
Member

httpdigest commented May 26, 2023

EDIT: never mind, a view also requires you to allocate something.

Initially, I thought that you meant that every Vector3d should also be a Vector2dc. :)
But that would invalidate Liskov Substitution Principle (e.g. changing the z component on the Vector3d would result in a different result for Vector2dc's length().

@xupwup
Copy link
Author

xupwup commented May 26, 2023

No, my idea was just bad :D
But Vector3d.xy(new Vector2d()) would be great. Could you also make Vector3d.xy(new Vector2f()) work? And Vector3f.xy(new Vector2d()) and Vector3f.xy(new Vector2f())?

@httpdigest
Copy link
Member

But would adding these "swizzle" methods really justify bloating the classes/jar up? I mean, theoretically, people could then ask for all sorts of other 4d to 3d swizzles, etc, including vector4.xxx(vector3d), etc.
I am not thaaat convinced that writing Vector2d xy = new Vector2d(xyz.x(), xyz.y()) is thaaat much of a burden.

@xupwup
Copy link
Author

xupwup commented May 26, 2023

No, I think all the swizzles don't add that much value. But truncating a 3d vector to a 2d vector is quite common in our codebase.

Another suggestion: why not add it to Vector2d's constructor? Like this:

   public Vector2d(Vector3d v) {
      x = v.x;
      y = v.y;
   }

Also, on a separate note, could you add a double to float constructor to Vector2f (and 3f and 4f)?

    public Vector2f(Vector2dc v) {
        x = (float) v.x();
        y = (float) v.y();
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants