Array unique 3 for overlapping coordinates - ANSWERED

Wondering, is there a way to have array unique work for coordinates? or an array3x

I don’t suppose anyone has a workaround for this?

I often find myself working with rounded coordinate arrays and end up having quite a few points that when rounded are on top of each other. I’m trying to filter these duplicates out but not sure how.

I’d say that the approach is the problem.
You can generate an array of linear values and then shuffle them. This will ensure that no numbers are a duplicate. This patch version is only for a single array, I think with some tweaks and the array3 versions you should be able to get something working.

Just updated the patch, there’s now two examples, one with a single array, and one with an array xyz

Thanks for this @andro, it makes a lot of sense but where I’m finding it tricky is situations like this;

any advise how to reduce this array to its 12 unique xyz values for example?

Hi @keepitvisual,

I wrote an op to solve your problem. I don’t know how I can share it with you so here is the code.

// ArrayUnique3 by dino
const inArray = op.inArray("array");
const outArray = op.outArray("arrayOut");

let showingError = false;

inArray.onChange = function ()
{
    const inValue = inArray.get();

    // Error handling
    if (!inValue)
    {
        outArray.set(null);
        return;
    }

    if (inValue.length % 3 !== 0)
    {
        if (!showingError)
        {
            op.uiAttr({ "error": "Arrays length not divisible by 3 !" });
            showingError = true;
        }
        return;
    }

    if (showingError)
    {
        showingError = false;
        op.uiAttr({ "error": null });
    }

    // Unique check
    const checkValue = inValue;

    let len = checkValue.length;
    for (let l = 0; l < len; l += 3)
    {
        for (let m = 3; m < len - l; m += 3)
        {
            if (checkValue[l] === checkValue[l + m] && checkValue[l + 1] === checkValue[l + 1 + m] && checkValue[l + 2] === checkValue[l + 2 + m])
            {
                checkValue.splice(l + m, 3);
                m -= 3;
                len -= 3;
            }
        }
    }

    outArray.set(null);
    outArray.set(checkValue);
};

Can you test and confirm that it works?

Cheers, dino

edit: small code cleanup and pushing the format button

@dino I can confirm that worked.
So many times I’ve wanted this op, don’t know how to thank you enough :hugs:
Thanks for taking the time, I’m very grateful!

That’s great to hear!
I cleaned up the code a bit and updated the above post.

I’m not sure how good the performance of the op is when using larger dynamic arrays.
It worked fine with up to 1000 triplets and a sine animation with more triplets you see that the CPU time rises in the performance op. This is dependent on the CPU of the system. Haven’t tested on mobile so far.

Thanks for the update @dino
This kept me up late yesterday :slight_smile:
Tried with 3000 triplets and works great for me on desktop and mobile (iOS)