コード例 #1
0
        //public override void Execute(AgentBase agent)
        //{
        //    CellGroup cellGroup = agent as CellGroup;
        //    if (!cellGroup.IsActive) return;
        //    CellAgentSystem cellSystem = cellGroup.AgentSystem as CellAgentSystem;

        //    for (int i = 0; i < cellSystem.Agents.Count; i++)
        //    {
        //        CellGroup anotherGroup = cellSystem.Agents[i] as CellGroup;
        //        if (cellGroup == anotherGroup) continue;
        //        // check if the agent can merge with another agnent
        //        foreach (Point3d place in cellGroup.PlaceToConnect())
        //        {
        //            foreach (Plane pose in cellSystem.CellPoses.Branch(i))
        //            {
        //                if (place.DistanceTo(pose.Origin) < 0.75 * cellSystem.GridSize)
        //                {
        //                    cellGroup.Merge(anotherGroup, place, pose);
        //                    cellSystem.Agents.RemoveAt(i);
        //                    goto NextStep;
        //                }
        //            }
        //        }
        //    NextStep: continue;
        //    }
        //}

        public void ExecuteWithSystem(CellGroup cellGroup, CellAgentSystem cellSystem)
        {
            if (!cellGroup.IsActive)
            {
                return;
            }

            //List<int> RemovedIndices = new List<int>(); //for the bug during merging
            for (int i = 0; i < cellSystem.Agents.Count; i++)
            {
                //if (RemovedIndices.Contains(i)) continue; //for the bug during merging
                CellGroup anotherGroup = cellSystem.Agents[i] as CellGroup;
                if (cellGroup == anotherGroup)
                {
                    continue;
                }
                // check if the agent can merge with another agnent
                foreach (Point3d place in cellGroup.PlaceToConnect())
                {
                    foreach (Plane pose in cellSystem.CellPoses.Branch(i))
                    {
                        if (place.DistanceTo(pose.Origin) < 0.75 * cellSystem.GridSize)
                        {
                            cellGroup.Merge(anotherGroup, place, pose);
                            cellSystem.Agents.RemoveAt(i);
                            //RemovedIndices.Add(i); //for the bug during merging
                            goto NextStep;
                        }
                    }
                }
                NextStep : continue;
            }
        }
コード例 #2
0
        //public override void Execute(AgentBase agent)
        //{
        //    CellGroup cellGroup = agent as CellGroup;
        //    Vector3d t = new Vector3d(rd.NextDouble()-0.5, rd.NextDouble()-0.5, 0.0);
        //    double r = rd.NextDouble() * Math.PI * 0.05;
        //    cellGroup.Move(t, r);
        //}

        public virtual void ExecutWithSystem(CellGroup cellGroup, CellAgentSystem cellSystem)
        {
            Vector3d t = new Vector3d(rd.NextDouble() - 0.5, rd.NextDouble() - 0.5, 0.0);
            double   r = (rd.NextDouble() - 0.5) * Math.PI * 0.05;

            t += FindNeighor(cellGroup, cellSystem);
            cellGroup.Move(t, r);
        }
コード例 #3
0
 public void ExecuteSingleStep()
 {
     if (System.Agents.Count == 1)
     {
         return;
     }
     foreach (AgentBase agent in System.Agents)
     {
         CellGroup cellGroup = agent as CellGroup;
         cellGroup.ExecuteWithSystem(System);
         System.IterationCount += 1;
     }
 }
コード例 #4
0
        internal void Merge(CellGroup anotherGroup, Point3d place, Plane anotherGroupPose)
        {
            Plane       poseToConnect = GetPose(GetCellFromPoint(place));
            List <Cell> cellsToAdd    = new List <Cell>();
            int         times         = 0;

            // try 3 different angles in which they can connect without collision
            while (true)
            {
                cellsToAdd.Clear();
                foreach (Cell cell in anotherGroup.Cells)
                {
                    Point3d cellCenter         = anotherGroup.GetPose(cell).Origin;
                    Point3d cellCenterOriented = cellCenter;
                    cellCenterOriented.Transform(Transform.PlaneToPlane(anotherGroupPose, poseToConnect));
                    Cell cellToAdd = GetCellFromPoint(cellCenterOriented);
                    if (times < 3)
                    {
                        foreach (Cell c in Cells)
                        {
                            if (c == cellToAdd)
                            {
                                goto NextStep;
                            }
                        }
                    }
                    cellToAdd.IsActive = cell.IsActive;
                    cellsToAdd.Add(cellToAdd);
                }
                Cells.AddRange(cellsToAdd);
                break;
NextStep:
                poseToConnect.Rotate(2 * Math.PI / 3, poseToConnect.ZAxis);
                times += 1;
            }
        }
コード例 #5
0
        private Vector3d FindNeighor(CellGroup cellGroup, CellAgentSystem cellSystem)
        {
            int    minIndex = -1;
            double minDis   = 999999999999999999;

            for (int i = 0; i < cellSystem.Agents.Count; i++)
            {
                CellGroup g = cellSystem.Agents[i] as CellGroup;
                if (cellGroup.Pose.Origin.DistanceTo(g.Pose.Origin) < 0.01)
                {
                    continue;
                }
                if (minDis > cellGroup.Pose.Origin.DistanceTo(g.Pose.Origin))
                {
                    minIndex = i;
                    minDis   = cellGroup.Pose.Origin.DistanceTo(g.Pose.Origin);
                }
            }
            CellGroup gMin = cellSystem.Agents[minIndex] as CellGroup;
            Vector3d  v    = new Vector3d(gMin.Pose.Origin - cellGroup.Pose.Origin);

            v.Unitize();
            return(v * 0.8);
        }