Optimize this struct? (3D Integer Vector)

Hey everyone! Been a while since I posted here. I was working the other day on some projects, and I have a class I’ve been using for a while. I made it myself, it’s a simple class that’s a Vector3, but it uses integers instead of floats.

I’m just wondering if anyone has any tips on optimizing it? I use this class a LOT (For pathfinding, position storage, ect) and I just need to know if there’s ways to better it. Thanks in advance!

using UnityEngine;

[System.Serializable]
public struct IntPos {
    public int x, y, z;

    public IntPos(int ix, int iy, int iz) {
        x = ix;
        y = iy;
        z = iz;
    }

    public Vector3 toVec {
        get {
            return new Vector3(x, y, z);
        }
    }

    public static IntPos zero = new IntPos(0, 0, 0);

    public static IntPos up = new IntPos(0, 1, 0);

    public static IntPos right = new IntPos(1, 0, 0);

    public static IntPos forward = new IntPos(0, 0, 1);

    public static IntPos one = new IntPos(1, 1, 1);

    //IntPos - IntPos operators
    public static IntPos operator +(IntPos i1, IntPos i2) {
        return new IntPos(i1.x + i2.x, i1.y + i2.y, i1.z + i2.z);
    }
    public static IntPos operator -(IntPos i1, IntPos i2) {
        return new IntPos(i1.x - i2.x, i1.y - i2.y, i1.z - i2.z);
    }
    public static IntPos operator *(IntPos i1, IntPos i2) {
        return new IntPos(i1.x * i2.x, i1.y * i2.y, i1.z * i2.z);
    }
    public static IntPos operator /(IntPos i1, IntPos i2) {
        return new IntPos(i1.x / i2.x, i1.y / i2.y, i1.z / i2.z);
    }
    public static bool operator ==(IntPos i1, IntPos i2) {
        return i1.x == i2.x && i1.y == i2.y && i1.z == i2.z;
    }
    public static bool operator !=(IntPos i1, IntPos i2) {
        return !(i1==i2);
    }

    public bool isNegative {
        get {
            return x < 0 || y < 0 || z < 0;
        }
    }

    //IntPos - Int Operators
    public static IntPos operator +(IntPos i1, int i2) {
        return new IntPos(i1.x + i2, i1.y + i2, i1.z + i2);
    }
    public static IntPos operator -(IntPos i1, int i2) {
        return new IntPos(i1.x - i2, i1.y - i2, i1.z - i2);
    }
    public static IntPos operator *(IntPos i1, int i2) {
        return new IntPos(i1.x * i2, i1.y * i2, i1.z * i2);
    }
    public static IntPos operator /(IntPos i1, int i2) {
        return new IntPos(Mathf.FloorToInt((float)i1.x / i2), Mathf.FloorToInt((float)i1.y / i2), Mathf.FloorToInt((float)i1.z / i2));
    }

    public IntPos normalized {
        get {
            return IntPos.Vector3ToIntPos(toVec * 1.2f);
        }
    }

    public static float DistanceSqr(IntPos i1, IntPos i2) {

        return ( i1 - i2 ).sqrMagnitude;
    }

    public float sqrMagnitude {
        get {
            return ( x * x + y * y + z * z );
        }
    }

    public static IntPos Vector3ToIntPos(Vector3 input) {
        return new IntPos(Mathf.FloorToInt(input.x), Mathf.FloorToInt(input.y), Mathf.FloorToInt(input.z));
    }

    public override string ToString() {
        return "IntPos(" + x + "," + y + "," + z + ")";
    }
    public IntPos ToFlatIntPos() {
        return new IntPos(x, 0, y);
    }
    public IntPos ToFlatZIntpos() {
        return new IntPos(x, 0, z);
    }
}

A few things. For one, the division operator actually converts i1’s values to floats before division. This is to prevent things from truncating to 0, which happens quite a bit. If I don’t do this, I have problems with values that are close to 0 coordinates, since they truncate.

Next, the !=/== operators. I don’t know if those should be better or if they are fine.

The normalized value could be optimized to not do the vector3 conversion… but I barely use it so I don’t see much point.

I wouldn’t use float division in the “/” operator; since you’re dealing with ints, the expected result would be integer division. You could make a separate function for floating point division. I expect the != operator would be a bit faster if it didn’t call the == operator. Also you need to override Equals and I think GetHashCode if you’re going to override == and !=. Ideally the struct would extend System.IEquatable with a strongly-typed overload of Equals, to prevent boxing/unboxing.

–Eric

2 Likes

Your IntVector-Int operators are a little weird. Mathematically the result of a vector plus a scalar is undefined. I’m not sure exactly what you intend to do with the operation, but it seems to be an easy way to add in bugs.

I want to stress implementing IEquatable for value types- the boxing for object comparisons is pretty harsh if you just do the override, since it can be easily avoided. Definitely need to override GetHashCode if you’re planning on using this type as keys in hashtables/dictionaries. You can use the Unity implementation for Vector3s directly, which is:

public override int GetHashCode()
{
    return this.x.GetHashCode() ^ this.y.GetHashCode() << 2 ^ this.z.GetHashCode() >> 2;
}

… if memory serves.

EDIT: Also, don’t forget the negative operator overload (unary minus).

public static IntPos operator -(IntPos a)
{
    return new IntPos(-a.x, -a.y, -a.z);
}

I figured this might actually come in handy at some point in the future, so I wrote my own version, adapted from the internal Unity implementation of the Vector3. There were some functions that just made no sense to include for an integer version, like the Lerp/Slerp functions, RotateTowards, etc, but I kept as much as I could. I chose to use RoundToInt instead of FloorToInt for situations where floats were inevitable, and I made a ShiftAll function to replace your addition/subtraction operator overload idea- BoredMormon is right that it’s not a legal operation and you could (and almost definitely will) end up hiding some mistakes in the math if you leave it that way.

Added a few conversion methods to and from other Vector formats, including an integer version of Vector2 which I wrote alongside this. Worth noting that the normalize function is going to be less than ideal- you’re essentially comparing a spherical calculation to a box with the same dimensions, and you’d have to be VERY close to the perfect “corner” directions to snap to them, so it’ll end up with the six straight directions (forward, back, up, down, left, right) far more often than the diagonals. This really needs to be changed to something a little more fitting to the situation, like angle ranges.

1 Like

The floating point conversion was so that the integers didn’t truncate towards zero. The rest was pretty much what I expected

This is literally exactly what I was looking for, thanks for this. I figured if nobody did anything I’d write my script based on Unity’s Vector3 class, but… You did it for me!

As I said, I made this class a WHILE ago (At least half a year) and now’s the time I got around to optimizing it, is all. So the operator ideas for add/sub with integers was an old idea. If I re-wrote it from the ground up now, I’d probably do a ShiftAll like thing myself.

I WAS also wondering about hashcode optimizations, since I use this class for my position references in a lot of things. So it’s used in dictionary keys and the like a lot. So, thanks again. I’ll be using this from now on.

Just a little remark: Mark the Vector3i.up etc as readonly to prevent redefinition. These should not be changed anytime anyway.

And the constructor should idealy look like this (Unity will compile without calling the parameterless constructor, but VS will complain, due to the use of Auto-Properties):

 public Vector2i(int x, int y) : this() {
    this.x = x;
    this.y = y;
}
2 Likes