xxxxxxxxxx
import std;
void main()
{
float[3] pos = [0f, 0f, 0f];
float[3][] verts = [[1f,1f,1f],[2f,2f,2f],[3f,3f,3f],[4f,4f,4f],[5f,5f,5f],[6f,6f,6f],[7f,7f,7f],[8f,8f,8f],[9f,9f,9f],[10f,10f,10f],[11f,11f,11f],[12f,12f,12f]];
writeln(vEUCLIDps(pos, verts));
}
float[] vEUCLIDps(float[3] pos, float[3][] verts)
{
//"broadcast" single pos into an array.
float[3][] broadcastedpos;
broadcastedpos.length = verts.length;
broadcastedpos.fill(pos);
debug writefln!"d1 %s"(broadcastedpos);
debug writefln!"d2 %s"(verts);
for(size_t i; i < verts.length; ++i){//bugged if done with nested foreach and ref, contents are incorrect...
broadcastedpos[i][] -= verts[i][];//VSUBPS
broadcastedpos[i][] *= broadcastedpos[i][];//VMULPS
}
debug writefln!"d3 %s"(broadcastedpos);
//broadcastedpos[][0..3] -= verts[][0..3];//doesn't work
//broadcastedpos[][] *= broadcastedpos[][];//neither does this work
float[] sums;
sums.length = verts.length;
foreach (i, ref vertice; broadcastedpos)
{
//sums[i] = sum(vertice);//sum does not support fixed size arrays
sums[i] = vertice[0] + vertice[1] + vertice[2];
sums[i] = sqrt(sums[i]);//should see SQRTPS in the assembly output, with ymm operands.
}
return sums;
//return cast(float[])map!(a => sqrt(a))(sums);//incompatible types
//no need for abs.
}