예제 #1
0
        public SoftMap(float3[] coarseNodes, float[] coarseIntensities, float lengthCutoff, float3[] actuators, float coarseSigma, int3 dimsVolume)
        {
            DimsVolume = dimsVolume;

            NCoarseNodes = coarseNodes.Length;
            CoarseNodes  = coarseNodes.ToList().ToArray();

            CoarseIntensities = coarseIntensities.ToList().ToArray();
            CoarseSigma       = coarseSigma;

            lengthCutoff *= lengthCutoff;
            List <int>[] NeighborLists = new List <int> [NCoarseNodes];
            for (int n1 = 0; n1 < NCoarseNodes; n1++)
            {
                float3     Pos1      = CoarseNodes[n1];
                List <int> Neighbors = new List <int>();

                for (int n2 = 0; n2 < NCoarseNodes; n2++)
                {
                    if (n1 == n2)
                    {
                        continue;
                    }

                    float3 Diff = (Pos1 - CoarseNodes[n2]);
                    if (Diff.LengthSq() <= lengthCutoff)
                    {
                        Neighbors.Add(n2);
                    }
                }

                NeighborLists[n1] = Neighbors;
            }

            NCoarseEdges            = NeighborLists.Select(l => l.Count).Sum();
            CoarseNeighborIDs       = new int[NCoarseEdges];
            CoarseNeighborIDOffsets = new int[NCoarseNodes + 1];
            CoarseEdgeLengths       = new float[NCoarseEdges];

            for (int n1 = 0, offset = 0; n1 < NCoarseNodes; n1++)
            {
                float3 Pos1 = CoarseNodes[n1];

                List <int> Neighbors = NeighborLists[n1];
                for (int i = 0; i < Neighbors.Count; i++)
                {
                    CoarseNeighborIDs[offset + i] = Neighbors[i];
                    CoarseEdgeLengths[offset + i] = (Pos1 - CoarseNodes[Neighbors[i]]).Length();
                }

                offset += Neighbors.Count;
                CoarseNeighborIDOffsets[n1 + 1] = offset;
            }

            NActuators          = actuators.Length;
            ActuatorNodes       = new int[NActuators];
            ActuatorOffsets     = new float3[NActuators];
            ConnectedToActuator = new int[NCoarseNodes].Select(v => - 1).ToArray();

            for (int i = 0; i < NActuators; i++)
            {
                float3 Pos         = actuators[i];
                float  ClosestDist = float.MaxValue;
                int    ClosestNode = 0;

                for (int n = 0; n < NCoarseNodes; n++)
                {
                    float Dist = (Pos - CoarseNodes[n]).LengthSq();
                    if (Dist < ClosestDist)
                    {
                        ClosestDist = Dist;
                        ClosestNode = n;
                    }
                }

                ActuatorNodes[i] = ClosestNode;
                ConnectedToActuator[ClosestNode] = i;
            }
        }