Exemple #1
0
    public bool ShowFileMapRegion(string filename)
    {
        if (_lastFileMapRegionName != filename)
        {
            FileMapRegion newFile = new FileMapRegion();

            try
            {
                byte[] region_data = Util.ReadFile(filename);
                if (!newFile.ReadFromMemory(region_data))
                {
                    HobaDebuger.LogWarning("ShowFileMapRegion, Failed to load " + filename);
                    return(false);
                }
            }
            catch (Exception)
            {
                HobaDebuger.LogWarning("ShowFileMapRegion, Failed to load " + filename);
                return(false);
            }

            _fileMapRegion = newFile;

            _lastFileMapRegionName = filename;
        }

        for (int i = 0; i < _mapRegionGameObjList.Count; ++i)
        {
            GameObject go = _mapRegionGameObjList[i];
            if (go != null)
            {
                GameObject.Destroy(go);
            }
        }
        _mapRegionGameObjList.Clear();
        List <int> Keys = new List <int>(_fileMapRegion.RegionMap.Keys);

        for (int i = 0; i < Keys.Count; ++i)
        {
            FileRegion fileRegion = _fileMapRegion.RegionMap[Keys[i]];

            //
            HashSet <uint> regionGridSet = fileRegion.RegionGridSet;

            List <Vector3> vertices  = new List <Vector3>();
            List <int>     triangles = new List <int>();

            Vector3[] vGrid   = new Vector3[4];
            int       voffset = 0;
            foreach (uint v in regionGridSet)
            {
                if (vertices.Count + 4 > 60000)  //split
                {
                    int        index = _mapRegionGameObjList.Count;
                    GameObject go    = new GameObject(HobaText.Format("MapRegionGameObj_{0}_{1}", Keys[i], index));

                    MeshRenderer meshRenderer = go.AddComponent <MeshRenderer>();
                    meshRenderer.enabled         = true;
                    meshRenderer.name            = HobaText.Format("MapRegionGameObj_{0}_{1}", Keys[i], index);
                    meshRenderer.material.shader = Shader.Find("Legacy Shaders/Bumped Diffuse");
                    meshRenderer.material.color  = GetMeshColor(Keys[i]);
                    CUnityUtil.DisableLightAndShadow(meshRenderer);

                    MeshFilter meshFilter = go.AddComponent <MeshFilter>();
                    meshFilter.sharedMesh           = new Mesh();
                    meshFilter.sharedMesh.name      = HobaText.Format("MeshFilterMesh_{0}_{1}", Keys[i], index);
                    meshFilter.sharedMesh.vertices  = vertices.ToArray();
                    meshFilter.sharedMesh.triangles = triangles.ToArray();

                    _mapRegionGameObjList.Add(go);

                    vertices.Clear();
                    triangles.Clear();
                    voffset = 0;
                }

                uint row = (v & 0xffff0000) >> 16;
                uint col = (v & 0xffff);

                _fileMapRegion.GetGridPositions(row, col, vGrid, 0);

                float fH0 = CUnityUtil.GetMapHeight(vGrid[0]);
                float fH1 = CUnityUtil.GetMapHeight(vGrid[1]);
                float fH2 = CUnityUtil.GetMapHeight(vGrid[2]);
                float fH3 = CUnityUtil.GetMapHeight(vGrid[3]);
//                 if (fH0 == 0.0f || fH1 == 0.0f || fH2 == 0.0f || fH3 == 0.0f)
//                     continue;

                vGrid[0].y = fH0 + 0.1f;
                vGrid[1].y = fH1 + 0.1f;
                vGrid[2].y = fH2 + 0.1f;
                vGrid[3].y = fH3 + 0.1f;

                vertices.Add(vGrid[0]);
                vertices.Add(vGrid[1]);
                vertices.Add(vGrid[2]);
                vertices.Add(vGrid[3]);

                triangles.Add(voffset + 0);
                triangles.Add(voffset + 1);
                triangles.Add(voffset + 2);
                triangles.Add(voffset + 3);
                triangles.Add(voffset + 2);
                triangles.Add(voffset + 1);

                voffset += 4;
            }

            if (vertices.Count > 0)
            {
                int        index = _mapRegionGameObjList.Count;
                GameObject go    = new GameObject(HobaText.Format("MapRegionGameObj_{0}_{1}", Keys[i], index));

                MeshRenderer meshRenderer = go.AddComponent <MeshRenderer>();
                meshRenderer.enabled         = true;
                meshRenderer.name            = HobaText.Format("MapRegionGameObj_{0}_{1}", Keys[i], index);
                meshRenderer.material.shader = Shader.Find("Legacy Shaders/Bumped Diffuse");
                meshRenderer.material.color  = GetMeshColor(Keys[i]);
                CUnityUtil.DisableLightAndShadow(meshRenderer);
                MeshFilter meshFilter = go.AddComponent <MeshFilter>();
                meshFilter.sharedMesh           = new Mesh();
                meshFilter.sharedMesh.name      = HobaText.Format("MeshFilterMesh_{0}_{1}", Keys[i], index);
                meshFilter.sharedMesh.vertices  = vertices.ToArray();
                meshFilter.sharedMesh.triangles = triangles.ToArray();

                _mapRegionGameObjList.Add(go);

                vertices.Clear();
                triangles.Clear();
                voffset = 0;
            }
        }

        return(true);
    }
        public void Read()
        {
            FileRegion    currentSection  = FileRegion.Base;
            AssetInstance currentInstance = null;
            string        currentLine;
            string        trimmedLine;

            using (var fileStream = File.Open())
                using (var reader = new System.IO.StreamReader(fileStream))
                {
                    while (!reader.EndOfStream)
                    {
                        currentLine = reader.ReadLine();
                        trimmedLine = currentLine.Trim();
                        if (currentLine.StartsWith("!"))
                        {
                            continue;
                        }                                          // This line is a comment
                        if (currentLine.StartsWith("["))
                        {
                            var    match       = sectionParser.Match(currentLine);
                            string sectionName = null;
                            if (match.Success)
                            {
                                sectionName = match.Groups[1].Value;
                            }
                            if (sectionName != null)
                            {
                                sectionName = sectionName.ToLower();
                            }

                            switch (sectionName)
                            {
                            case "instances": currentSection = FileRegion.Instances; break;

                            case "visible": currentSection = FileRegion.Visible; break;

                            case "settings": currentSection = FileRegion.Settings; break;

                            default: currentSection = FileRegion.Unknown; break;
                            }
                            continue;
                        }

                        switch (currentSection)
                        {
                        case FileRegion.Instances:
                        {
                            if (trimmedLine.StartsWith("."))
                            {
                                // This is a value for the currentInstance
                                if (trimmedLine.StartsWith(".Position="))
                                {
                                    var match = instanceValueParser.Match(currentLine);
                                    if (match.Success)
                                    {
                                        currentInstance.HasPosition = true;
                                        string posStr = match.Groups[2].Value;
                                        posStr = posStr.Substring(1, posStr.Length - 2);
                                        string[] coords = posStr.Split(',');

                                        currentInstance.PositionX = float.Parse(coords[0]);
                                        currentInstance.PositionY = float.Parse(coords[1]);
                                        currentInstance.PositionZ = float.Parse(coords[2]);
                                    }
                                }
                                else if (trimmedLine.StartsWith(".ParentInstance="))
                                {
                                    var match = instanceValueParser.Match(currentLine);
                                    if (match.Success)
                                    {
                                        long parentInstanceId = long.Parse(match.Groups[2].Value);
                                        if (parentInstanceId != 0)
                                        {
                                            currentInstance.ParentInstanceId = parentInstanceId;
                                            List <AssetInstance> parentAssetInstances;
                                            if (!InstancesByParentId.TryGetValue(parentInstanceId, out parentAssetInstances))
                                            {
                                                parentAssetInstances = new List <AssetInstance>();
                                                InstancesByParentId[parentInstanceId] = parentAssetInstances;
                                            }
                                            parentAssetInstances.Add(currentInstance);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                currentInstance = new AssetInstance();

                                // Parse Instance Id and Asset Id
                                var match = instanceHeaderParser.Match(trimmedLine);
                                if (match.Success)
                                {
                                    long instanceId = long.Parse(match.Groups[1].Value);
                                    long assetId    = long.Parse(match.Groups[2].Value);
                                    currentInstance.Id      = instanceId;
                                    currentInstance.AssetId = assetId;
                                    currentInstance.Room    = this;
                                    //Instances.Add(currentInstance);
                                    InstancesById.Add(instanceId, currentInstance);
                                    List <AssetInstance> assetInstances;
                                    if (!InstancesByAssetId.TryGetValue(assetId, out assetInstances))
                                    {
                                        assetInstances = new List <AssetInstance>();
                                        InstancesByAssetId[assetId] = assetInstances;
                                    }
                                    assetInstances.Add(currentInstance);
                                }
                            }
                        }
                        break;
                        }
                    }
                }
        }
Exemple #3
0
        /// <summary>
        /// 使用相似系数分析光谱
        /// </summary>
        /// <param name="analyteModel">分析模型</param>
        /// <param name="filename">待分析的光谱</param>
        /// <returns>相似系数值,如果返回值小于0,表示错误</returns>
        public static double CorCoeffAnalyte(ModelInfo analyteModel, string filename)
        {
            try
            {
                if (analyteModel == null || analyteModel.files == null || analyteModel.files.Count == 0)
                {
                    throw new Exception("分析模型设置错误");
                }
                if (filename == null)
                {
                    throw new Exception("光谱文件错误");
                }

                //将模型文件和光谱文件添加到一个列表中
                List <string> allFiles = new List <string>();
                var           temp     = from item in analyteModel.files select item.filename;
                allFiles.AddRange(temp);
                allFiles.Add(filename);

                //光谱有效区域
                NirLib.BasicAlgorithm.FileRegion[] regions = null;
                if (analyteModel.regions.Count > 0)
                {
                    regions = new FileRegion[analyteModel.regions.Count];
                    for (int i = 0; i < regions.Length; i++)
                    {
                        regions[i] = new FileRegion(analyteModel.regions[i].firstX, analyteModel.regions[i].lastX);
                    }
                }

                double xstep = 1;
                //Ai.Hong.NirAlgorithm.NormalAlgorithm.
                List <double[]> allDatas = Ai.Hong.NirAlgorithm.NormalAlgorithm.LoadFiles(allFiles, regions, LoadingCallBack, out xstep);
                if (allDatas == null)
                {
                    throw new Exception(Ai.Hong.NirAlgorithm.NormalAlgorithm.ErrorString);
                }

                //最后一个数据为待测光谱
                double[] spectrumData = allDatas[allDatas.Count - 1];

                //计算平均光谱
                allDatas.RemoveAt(allDatas.Count - 1);
                double[] AverageData = NirLib.BasicAlgorithm.BasicClusterAlgorithm.CreateAverageSpectrum(allDatas);

                //预处理光谱
                //矢量归一化
                AverageData  = NirLib.BasicAlgorithm.BasicClusterAlgorithm.VertorNormalize(AverageData);
                spectrumData = NirLib.BasicAlgorithm.BasicClusterAlgorithm.VertorNormalize(spectrumData);

                //一阶导数,17个平滑点
                AverageData  = NirLib.BasicAlgorithm.BasicClusterAlgorithm.SG_FirstDerivative(AverageData, xstep, 17);
                spectrumData = NirLib.BasicAlgorithm.BasicClusterAlgorithm.SG_FirstDerivative(spectrumData, xstep, 17);

                //相似系数
                double cc = Ai.Hong.NirAlgorithm.CorCoeffDistance.Distance(AverageData, spectrumData);

                return(cc);
            }
            catch (Exception ex)
            {
                ErrorString = ex.Message;
                return(-1);
            }
        }
        public void Read()
        {
            FileRegion currentSection = FileRegion.Base;
            string     currentLine;

            using (var fileStream = File.Open())
                using (var reader = new System.IO.StreamReader(fileStream))
                {
                    while (!reader.EndOfStream)
                    {
                        currentLine = reader.ReadLine();
                        if (currentLine.StartsWith("!"))
                        {
                            continue;
                        }                                          // This line is a comment
                        if (currentLine.StartsWith("["))
                        {
                            var    match       = sectionParser.Match(currentLine);
                            string sectionName = null;
                            if (match.Success)
                            {
                                sectionName = match.Groups[1].Value;
                            }
                            if (sectionName != null)
                            {
                                sectionName = sectionName.ToLower();
                            }

                            switch (sectionName)
                            {
                            case "assets": currentSection = FileRegion.Assets; break;

                            case "paths": currentSection = FileRegion.Paths; break;

                            case "rooms": currentSection = FileRegion.Rooms; break;

                            case "schemes": currentSection = FileRegion.Schemes; break;

                            case "terraintextures": currentSection = FileRegion.TerrainTextures; break;

                            case "dydtextures": currentSection = FileRegion.DydTextures; break;

                            case "dydchannelparams": currentSection = FileRegion.DydChannelParams; break;

                            case "settings": currentSection = FileRegion.Settings; break;

                            default: currentSection = FileRegion.Unknown; break;
                            }
                            continue;
                        }

                        switch (currentSection)
                        {
                        case FileRegion.Assets:
                        {
                            var assetInfo = assetParser.Match(currentLine);
                            if (!assetInfo.Success)
                            {
                                continue;
                            }

                            AreaAsset asset = new AreaAsset();
                            asset.Id = long.Parse(assetInfo.Groups[1].Value);
                            if (!String.IsNullOrEmpty(assetInfo.Groups[2].Value))
                            {
                                asset.EncounterIndex = assetInfo.Groups[2].Value.ToLower();
                            }
                            asset.Path = assetInfo.Groups[3].Value;
                            string ext = assetInfo.Groups[4].Value;
                            AssetIdMap.Add(asset.Id, asset);
                            List <AreaAsset> pathList;
                            if (!AssetsByExtension.TryGetValue(ext, out pathList))
                            {
                                pathList = new List <AreaAsset>();
                                AssetsByExtension.Add(ext, pathList);
                            }
                            pathList.Add(asset);
                        }
                        break;

                        case FileRegion.Rooms:
                            var    roomFileName = currentLine.Trim();
                            string roomFilePath = String.Format("{0}/{1}.dat", File.FilePath.Remove(File.FilePath.LastIndexOf('/')), roomFileName);
                            File   roomFile     = Assets.FindFile(roomFilePath);
                            if (roomFile == null)
                            {
                                Console.WriteLine("Cannot find area room file: {0}", roomFilePath);
                                break;
                            }

                            FileReaders.RoomSpecification roomSpec = new RoomSpecification(roomFile, roomFileName, this);
                            roomSpec.Read();
                            this.Rooms.Add(roomSpec);
                            break;
                        }
                    }
                }
        }