Agents on 2dGrid on Surface in C#

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;
        }
    }

Why doesn’t it work? What’s wrong? You must specify the error if you do not offer how to reproduce it.

I don’t think this is the cause of the error, but this could get you into trouble. You have no guarantee that the domain will start at 0, you should use the length of the domain to calculate the resolution and calculate the surface coordinates by interpolating between the domain.

Error is like on a screenshot - there is some mismatch between agents actual positions and correlating node position. similiar code work in 2d and 3d grid, but not here.

“I don’t think this is the cause of the error, but this could get you into trouble. You have no guarantee that the domain will start at 0, you should use the length of the domain to calculate the resolution and calculate the surface coordinates by interpolating between the domain.”

can you elaborate?

maybe i will ask my queastion in other words.

how to create a 2d array of points3d distributed evenly on surface, and find a closest point (from that grid) to an Agent, using only numerical coordinates of the grid?