private async Task <List <SetVersionLevelOfDetail> > ExtractDetailLevels(SetMetadataContract setMetadata, Uri baseUrl)
        {
            List <SetVersionLevelOfDetail> detailLevels = new List <SetVersionLevelOfDetail>();

            foreach (int detailLevel in Enumerable.Range(setMetadata.MinimumLod, setMetadata.MaximumLod - setMetadata.MinimumLod + 1))
            {
                string detailLevelName = "L" + detailLevel;

                Uri lodMetadataUri = new Uri(baseUrl, "L" + detailLevel + "/metadata.json");
                CubeMetadataContract cubeMetadata = await this.Deserialize <CubeMetadataContract>(lodMetadataUri);

                OcTree <CubeBounds> octree = MetadataLoader.Load(cubeMetadata, detailLevelName, new Vector3(1, 1, 1));
                octree.UpdateTree();

                Vector3 cubeBounds = cubeMetadata.SetSize;

                ExtentsContract worldBounds        = cubeMetadata.WorldBounds;
                ExtentsContract virtualWorldBounds = cubeMetadata.VirtualWorldBounds;

                SetVersionLevelOfDetail currentSetLevelOfDetail = new SetVersionLevelOfDetail();
                currentSetLevelOfDetail.Metadata    = lodMetadataUri;
                currentSetLevelOfDetail.Number      = detailLevel;
                currentSetLevelOfDetail.Cubes       = octree;
                currentSetLevelOfDetail.ModelBounds = new BoundingBox(
                    new Vector3(worldBounds.XMin, worldBounds.YMin, worldBounds.ZMin),
                    new Vector3(worldBounds.XMax, worldBounds.YMax, worldBounds.ZMax));

                if (virtualWorldBounds != null)
                {
                    currentSetLevelOfDetail.WorldBounds =
                        new BoundingBox(
                            new Vector3(virtualWorldBounds.XMin, virtualWorldBounds.YMin, virtualWorldBounds.ZMin),
                            new Vector3(virtualWorldBounds.XMax, virtualWorldBounds.YMax, virtualWorldBounds.ZMax));
                }
                else
                {
                    currentSetLevelOfDetail.WorldBounds = new BoundingBox(
                        new Vector3(worldBounds.XMin, worldBounds.YMin, worldBounds.ZMin),
                        new Vector3(worldBounds.XMax, worldBounds.YMax, worldBounds.ZMax));
                }

                currentSetLevelOfDetail.SetSize     = new Vector3(cubeBounds.X, cubeBounds.Y, cubeBounds.Z);
                currentSetLevelOfDetail.Name        = "L" + detailLevel.ToString(CultureInfo.InvariantCulture);
                currentSetLevelOfDetail.VertexCount = cubeMetadata.VertexCount;

                currentSetLevelOfDetail.TextureTemplate = new Uri(lodMetadataUri, "texture/{x}_{y}.jpg");
                currentSetLevelOfDetail.ModelTemplate   = new Uri(lodMetadataUri, "{x}_{y}_{z}.{format}");

                currentSetLevelOfDetail.TextureSetSize = cubeMetadata.TextureSetSize;

                detailLevels.Add(currentSetLevelOfDetail);
            }
            return(detailLevels);
        }
        private async Task <List <SetVersionLevelOfDetail> > ExtractDetailLevels2(SetMetadataContract setMetadata, Uri baseUrl)
        {
            OcTree <CubeBounds> ocTree = new OcTree <CubeBounds>();

            List <LoaderSet> lods = new List <LoaderSet>();

            foreach (int detailLevel in Enumerable.Range(setMetadata.MinimumLod, setMetadata.MaximumLod - setMetadata.MinimumLod + 1))
            {
                // Allow exceptions to propagate here for logging in ELMAH
                string detailLevelName            = "L" + detailLevel;
                Uri    lodMetadataUri             = new Uri(baseUrl, detailLevelName + "/metadata.json");
                CubeMetadataContract cubeMetadata = await this.Deserialize <CubeMetadataContract>(lodMetadataUri);

                lods.Add(new LoaderSet {
                    CubeMetadataContract = cubeMetadata, Name = detailLevelName, DetailLevel = detailLevel, MetadataUrl = lodMetadataUri
                });
            }

            Vector3 maxSetSize = new Vector3(0, 0, 0);

            foreach (var loaderSet in lods)
            {
                if (loaderSet.CubeMetadataContract.SetSize.X > maxSetSize.X)
                {
                    maxSetSize.X = loaderSet.CubeMetadataContract.SetSize.X;
                }

                if (loaderSet.CubeMetadataContract.SetSize.Y > maxSetSize.Y)
                {
                    maxSetSize.Y = loaderSet.CubeMetadataContract.SetSize.Y;
                }

                if (loaderSet.CubeMetadataContract.SetSize.Z > maxSetSize.Z)
                {
                    maxSetSize.Z = loaderSet.CubeMetadataContract.SetSize.Z;
                }
            }

            List <SetVersionLevelOfDetail> detailLevels = new List <SetVersionLevelOfDetail>();

            foreach (LoaderSet loaderSet in lods)
            {
                var cubeMetadata = loaderSet.CubeMetadataContract;

                Vector3 cubeSize = maxSetSize / loaderSet.CubeMetadataContract.SetSize;

                ocTree.Add(MetadataLoader.LoadCubeBounds(loaderSet.CubeMetadataContract, loaderSet.Name, cubeSize));

                Vector3 cubeBounds = cubeMetadata.SetSize;

                ExtentsContract worldBounds        = cubeMetadata.WorldBounds;
                ExtentsContract virtualWorldBounds = cubeMetadata.VirtualWorldBounds;

                SetVersionLevelOfDetail currentSetLevelOfDetail = new SetVersionLevelOfDetail();
                currentSetLevelOfDetail.Metadata    = loaderSet.MetadataUrl;
                currentSetLevelOfDetail.Number      = loaderSet.DetailLevel;
                currentSetLevelOfDetail.Cubes       = ocTree;
                currentSetLevelOfDetail.ModelBounds = new BoundingBox(
                    new Vector3(worldBounds.XMin, worldBounds.YMin, worldBounds.ZMin),
                    new Vector3(worldBounds.XMax, worldBounds.YMax, worldBounds.ZMax));

                if (virtualWorldBounds != null)
                {
                    currentSetLevelOfDetail.WorldBounds =
                        new BoundingBox(
                            new Vector3(virtualWorldBounds.XMin, virtualWorldBounds.YMin, virtualWorldBounds.ZMin),
                            new Vector3(virtualWorldBounds.XMax, virtualWorldBounds.YMax, virtualWorldBounds.ZMax));
                }
                else
                {
                    currentSetLevelOfDetail.WorldBounds = new BoundingBox(
                        new Vector3(worldBounds.XMin, worldBounds.YMin, worldBounds.ZMin),
                        new Vector3(worldBounds.XMax, worldBounds.YMax, worldBounds.ZMax));
                }

                currentSetLevelOfDetail.SetSize     = new Vector3(cubeBounds.X, cubeBounds.Y, cubeBounds.Z);
                currentSetLevelOfDetail.Name        = "L" + loaderSet.DetailLevel.ToString(CultureInfo.InvariantCulture);
                currentSetLevelOfDetail.VertexCount = cubeMetadata.VertexCount;

                currentSetLevelOfDetail.TextureTemplate = new Uri(loaderSet.MetadataUrl, "texture/{x}_{y}.jpg");
                currentSetLevelOfDetail.ModelTemplate   = new Uri(loaderSet.MetadataUrl, "{x}_{y}_{z}.{format}");

                currentSetLevelOfDetail.TextureSetSize = cubeMetadata.TextureSetSize;

                detailLevels.Add(currentSetLevelOfDetail);
            }

            ocTree.UpdateTree();

            return(detailLevels);
        }
        public async Task <LoaderResults> LoadMetadata()
        {
            LoaderResults results = new LoaderResults();

            List <LoaderException> exceptions = new List <LoaderException>();
            Dictionary <string, Dictionary <string, SetVersion> > sets =
                new Dictionary <string, Dictionary <string, SetVersion> >(StringComparer.InvariantCultureIgnoreCase);

            SetContract[] setsMetadata   = null;
            Uri           storageRootUri = null;

            try
            {
                storageRootUri = new Uri(this.storageRoot, UriKind.Relative);

                setsMetadata = await this.Deserialize <SetContract[]>(storageRootUri);

                if (setsMetadata == null)
                {
                    throw new SerializationException("Deserialization Failed");
                }
            }
            catch (Exception ex)
            {
                exceptions.Add(new LoaderException("Sets", this.storageRoot, ex));
                results.Errors = exceptions.ToArray();
                results.Sets   = sets;
                return(results);
            }

            List <SetVersion> setVersions = new List <SetVersion>();

            foreach (SetContract set in setsMetadata)
            {
                try
                {
                    foreach (SetVersionContract version in set.Versions)
                    {
                        Uri setMetadataUri = new Uri(storageRootUri, version.Url);

                        Trace.WriteLine(String.Format("Set: {0}, Url {1}", set.Name, setMetadataUri));
                        SetMetadataContract setMetadata = await this.Deserialize <SetMetadataContract>(setMetadataUri);

                        if (setMetadata == null)
                        {
                            throw new SerializationException("Set metadata deserialization Failed");
                        }

                        Trace.WriteLine(String.Format("Discovered set {0}/{1} at {2}", set.Name, version.Name, version.Url));

                        Uri material = new Uri(setMetadataUri, setMetadata.Mtl);

                        SetVersion currentSet = new SetVersion {
                            SourceUri = setMetadataUri, Name = set.Name, Version = version.Name, Material = material
                        };

                        List <SetVersionLevelOfDetail> detailLevels = await this.ExtractDetailLevels(setMetadata, setMetadataUri);

                        currentSet.DetailLevels = new SortedDictionary <string, SetVersionLevelOfDetail>(detailLevels.ToDictionary(d => d.Name, d => d, StringComparer.OrdinalIgnoreCase));
                        setVersions.Add(currentSet);
                    }
                }
                catch (Exception ex)
                {
                    exceptions.Add(new LoaderException("Set", storageRootUri.ToString(), ex));
                }
            }

            sets = setVersions.GroupBy(s => s.Name).ToDictionary(s => s.Key, this.GenerateVersionMap, StringComparer.OrdinalIgnoreCase);

            results.Errors = exceptions.ToArray();
            results.Sets   = sets;

            return(results);
        }