Matching a Cog to a Rail (accurate tooth spacing)

So I have been trying to make a rail system, but it doesnt line up.
I measured all the components and directly flipped/re-oriented them where possible.

Obviously there is a measurement variation im missing due the curvature of the cog that is mucking things up.
Is there a formula for this, or another/simpler way of doing this? I dont mind starting from scratch.

OR a way of unrolling the cog (I can make it again without the corner fillets)

Thank you.

There are lots of calculator on the internet.

See for example these:

Thank you. I will check these out.