public ConfigurationSpaces(
            ILineIntersection <OrthogonalLineGrid2D> lineIntersection, int roomTemplateInstancesCount, int nodesCount, Func <TConfiguration, TConfiguration, int> configurationSpaceSelector) : base(lineIntersection)
        {
            this.configurationSpaceSelector = configurationSpaceSelector;
            // Init configuration spaces array
            ConfigurationSpaces_ = new ConfigurationSpace[roomTemplateInstancesCount][][];
            for (var i = 0; i < roomTemplateInstancesCount; i++)
            {
                ConfigurationSpaces_[i] = new ConfigurationSpace[roomTemplateInstancesCount][];
            }

            // Init shapes for node lists
            ShapesForNodes = new List <List <WeightedShape> >(nodesCount);
            for (var i = 0; i < nodesCount; i++)
            {
                ShapesForNodes.Add(new List <WeightedShape>());
            }
        }
Exemplo n.º 2
0
        public ConfigurationSpaces <TConfiguration> GetConfigurationSpaces <TConfiguration>(IMapDescription <int> mapDescription)
            where TConfiguration : IConfiguration <IntAlias <PolygonGrid2D>, int>
        {
            var graph = mapDescription.GetGraph();

            var roomDescriptions = graph
                                   .Vertices
                                   .ToDictionary(x => x, mapDescription.GetRoomDescription);

            var roomTemplates = roomDescriptions
                                .Values
                                .SelectMany(x => x.RoomTemplates)
                                .Distinct()
                                .ToList();

            var roomTemplateInstances = roomTemplates
                                        .ToDictionary(x => x, GetRoomTemplateInstances);

            var roomTemplateInstancesMapping = roomTemplateInstances
                                               .SelectMany(x => x.Value)
                                               .CreateIntMapping();

            var roomTemplateInstancesCount = roomTemplateInstancesMapping.Count;

            var corridorRoomDescriptionsMapping = roomDescriptions
                                                  .Values
                                                  .Where(x => x.GetType() == typeof(CorridorRoomDescription))
                                                  .Cast <CorridorRoomDescription>()
                                                  .Distinct()
                                                  .CreateIntMapping();

            var corridorRoomTemplateInstances = corridorRoomDescriptionsMapping
                                                .Keys
                                                .ToDictionary(
                x => x,
                x => x.RoomTemplates.SelectMany(y => roomTemplateInstances[y]).ToList());

            var nodesToCorridorMapping = GetNodesToCorridorMapping(mapDescription)
                                         .ToDictionary(
                x => x.Key,
                x => corridorRoomDescriptionsMapping[x.Value] + 1
                );

            var configurationSpaces = new ConfigurationSpaces <TConfiguration>(lineIntersection,
                                                                               roomTemplateInstancesCount, graph.VerticesCount, (configuration1, configuration2) =>
            {
                if (nodesToCorridorMapping.TryGetValue(new Tuple <int, int>(configuration1.Node, configuration2.Node), out var corridor))
                {
                    return(corridor);
                }

                return(0);
            });

            // Generate configuration spaces
            foreach (var shape1 in roomTemplateInstancesMapping.Keys)
            {
                foreach (var shape2 in roomTemplateInstancesMapping.Keys)
                {
                    var configurationSpacesList = new ConfigurationSpace[corridorRoomDescriptionsMapping.Count + 1];

                    configurationSpacesList[0] = GetConfigurationSpace(shape1, shape2);

                    foreach (var pair in corridorRoomDescriptionsMapping)
                    {
                        var roomDescription = pair.Key;
                        var intAlias        = pair.Value;

                        configurationSpacesList[intAlias + 1] = GetConfigurationSpaceOverCorridors(shape1, shape2,
                                                                                                   corridorRoomTemplateInstances[roomDescription]);
                    }

                    configurationSpaces.AddConfigurationSpace(shape1, shape2, configurationSpacesList.ToArray());
                }
            }

            foreach (var vertex in graph.Vertices)
            {
                var roomDescription = mapDescription.GetRoomDescription(vertex);

                foreach (var roomTemplate in roomDescription.RoomTemplates)
                {
                    var instances = roomTemplateInstances[roomTemplate];

                    foreach (var roomTemplateInstance in instances)
                    {
                        configurationSpaces.AddShapeForNode(vertex, roomTemplateInstance, 1d / instances.Count);
                    }
                }
            }

            return(configurationSpaces);
        }