Hi guys.
I am developing custom agent and vectorfield library in grasshopper C#.
I have created multiple classes with multiple functions. Now i am working on creating a VectorField2dGrid on Surface. The base of the class would be the VectorField2dGrid because its is very fast and easy for me to find an agent position on vectorfield without using closest point/rtree etc. just using grid coordinates. Somehow, it doesnt work on surface grid. Can you help?
Constructor of the class:
public VectorFieldGrid2dSrf(Surface iSrf, int iLayersX, int iLayersY, double iWidth, double iLength, bool nic) :base( iLayersX, iLayersY, iWidth, iLength, nic)
{
srf = iSrf;
layersX = iLayersX;
layersY = iLayersY;
gridAgents2d = new VectorPheromonePoint[layersX, layersY];
Surface s = srf;
Interval u = s.Domain(0);
Interval v = s.Domain(1);
double widthU = u.Max / layersX;
double widthV = v.Max / layersY;
resolutionX = widthU;
resolutionY = widthV;
//
for (int i = 0; i < layersX; i++)
{
for (int j = 0; j < layersY; j++)
{
int[] myIndices = new int[] { i, j };
double iU = i * resolutionX;
double iV = j * resolutionY;
Point3d pos = s.PointAt(iU, iV);
VectorPheromonePoint newPointField = new VectorPheromonePoint(pos, 0, new Vector3d(0, 0, 0));
gridAgents2d.SetValue(newPointField, myIndices);
}
}
}
Function to update nodes based on agents (to leave te tracks)
public override void UpdatePheromonesWithAgents<T>(List<T> agents, double niccccc, double life)
{
Random r = new Random();
foreach (int i in Enumerable.Range(0, agents.Count).OrderBy(x => r.Next()))
{
double u, v;
srf.ClosestPoint(agents[i].boidPosition, out u, out v);
Point3d position = srf.PointAt(u, v);
int xX = (int)Utils.UtilsFunctions.Constrain((position.X - minX) / resolutionX, 0, layersX - 1);
int yY = (int)Utils.UtilsFunctions.Constrain((position.Y - minY) / resolutionY, 0, layersY - 1);
gridAgents2d[xX, yY].pheromone += life;
}
}