コード例 #1
0
ファイル: CTreeManager.cs プロジェクト: ja003/ForestReco2
        public static void TryMergeAllTrees(bool pOnlyInvalid)
        {
            IsMerging = true;
            DateTime mergeStartTime = DateTime.Now;

            CDebug.WriteLine("TryMergeAllTrees");

            Trees.Sort((a, b) => b.peak.Center.Z.CompareTo(a.peak.Center.Z));

            int treeCountBeforeMerge = Trees.Count;

            if (detectMethod == EDetectionMethod.AddFactor)
            {
                MergeGoodAddFactorTrees(pOnlyInvalid);
            }
            if (detectMethod == EDetectionMethod.Detection2D)
            {
                Merge2DTrees();
            }

            if (pOnlyInvalid)
            {
                CAnalytics.secondMergeDuration = CAnalytics.GetDuration(mergeStartTime);
            }
            else
            {
                CAnalytics.firstMergeDuration = CAnalytics.GetDuration(mergeStartTime);
            }
            IsMerging = false;

            CDebug.Duration("Trees merge", mergeStartTime);
            CDebug.Count("Number of trees merged", treeCountBeforeMerge - Trees.Count);
        }
コード例 #2
0
        /// <summary>
        /// Assigns vege poins to trees. Handled in TreeManager
        /// </summary>
        private void ProcessVegePoints()
        {
            vege.Sort((b, a) => a.Z.CompareTo(b.Z));             //sort descending by height

            const int debugFrequency = 10000;

            DateTime processVegePointsStart = DateTime.Now;

            CDebug.WriteLine("ProcessVegePoints", true);

            DateTime previousDebugStart = DateTime.Now;


            int pointsToAddCount = vege.Count;

            //pointsToAddCount = 12000;

            for (int i = 0; i < pointsToAddCount; i++)
            {
                if (CProjectData.backgroundWorker.CancellationPending)
                {
                    return;
                }

                Vector3 point = vege[i];
                CTreeManager.AddPoint(point, i);

                CDebug.Progress(i, vege.Count, debugFrequency, ref previousDebugStart, processVegePointsStart, "added point");
            }
            CDebug.WriteLine("maxPossibleTreesAssignment = " + CTreeManager.maxPossibleTreesAssignment + " todo: investigate if too high");


            CAnalytics.processVegePointsDuration = CAnalytics.GetDuration(processVegePointsStart);
            CDebug.Duration("ProcessVegePoints", processVegePointsStart);
        }
コード例 #3
0
ファイル: CArray.cs プロジェクト: ja003/ForestReco2
        /*public void AddPointInField(Vector3 pPoint, EPointType pType, bool pLogErrorInAnalytics)
         * {
         *      Tuple<int, int> index = GetPositionInArray(pPoint);
         *      if (!IsWithinBounds(index))
         *      {
         *              CDebug.Error($"point {pPoint} is OOB {index}", pLogErrorInAnalytics);
         *              return;
         *      }
         *      switch (pType)
         *      {
         *              case EPointType.Ground:
         *                      array[index.Item1, index.Item2].AddGroundPoint(pPoint);
         *                      break;
         *              case EPointType.Vege:
         *                      array[index.Item1, index.Item2].AddVegePoint(pPoint);
         *                      break;
         *              case EPointType.Preprocess:
         *                      array[index.Item1, index.Item2].AddPreProcessVegePoint(pPoint);
         *                      break;
         *      }
         * }*/

        //public void SortPoints()
        //{
        //	foreach (CField field in fields)
        //	{
        //		field.SortPoints();
        //	}
        //}


        /// <summary>
        /// Approximates the height in undefined fields of ground array.
        /// </summary>
        public void FillArray()
        {
            CDebug.WriteLine("FillArray", true);

            DateTime fillAllHeightsStart = DateTime.Now;

            int counter = 1;

            while (!IsAllDefined())
            {
                if (CProjectData.backgroundWorker.CancellationPending)
                {
                    return;
                }

                DateTime fillHeightsStart = DateTime.Now;

                CDebug.Count("FillMissingHeights", counter);
                FillMissingHeights(counter);
                counter++;
                const int maxFillArrayIterations = 5;
                if (counter > maxFillArrayIterations + 1)
                {
                    CDebug.Error("FillMissingHeights");
                    CDebug.Count("too many iterations", counter);
                    break;
                }
                CDebug.Duration("FillMissingHeights", fillHeightsStart);
            }
            CAnalytics.fillAllHeightsDuration = CAnalytics.GetDuration(fillAllHeightsStart);
            CDebug.Duration("fillAllHeights", fillAllHeightsStart);
        }
コード例 #4
0
ファイル: CDebug.cs プロジェクト: ja003/ForestReco2
 internal static void Error(string pText, bool pWriteInAnalytics = true)
 {
     WriteLine("ERROR: " + pText, true);
     if (pWriteInAnalytics)
     {
         CAnalytics.AddError(pText);
     }
 }
コード例 #5
0
        public static void ExportTile()
        {
            if (!export)
            {
                return;
            }
            DateTime exportStart = DateTime.Now;

            StringBuilder output = new StringBuilder();
            string        res;

            DateTime start     = DateTime.Now;
            DateTime lastDebug = DateTime.Now;

            AddPointsTo(ref output, EClass.Unassigned, ref start);
            AddPointsTo(ref output, EClass.Ground, ref start);
            AddPointsTo(ref output, EClass.Building, ref start);

            if (CTreeManager.GetDetectMethod() == EDetectionMethod.Balls)
            {
                AddPointsTo(ref output, EClass.BallsSurface, ref start);
                AddPointsTo(ref output, EClass.Balls, ref start);
                AddPointsTo(ref output, EClass.BallsMainPoints, ref start);
                AddPointsTo(ref output, EClass.BallsCenters, ref start);
            }

            //tree points
            AddTreePointsTo(ref output, true, ref start);
            //invalid tree points
            AddTreePointsTo(ref output, false, ref start);

            if (output.Length == 0)
            {
                CDebug.Warning($"CLasExporter: no output created on tile {CProgramStarter.currentTileIndex}");
                return;
            }

            //1. Create the text file
            string txtOutputPath = tileFilePath + ".txt";

            WriteToFile(output, txtOutputPath);
            //2. save output path for the main file export
            exportedTiles.Add(txtOutputPath);

            //3. Convert the txt to las using LasTools
            //format: x, y, z, class, user comment (id), R, G, B
            string cmd = $"{txt2lasCmd} {txtOutputPath}";

            CCmdController.RunLasToolsCmd(cmd, tileFilePath + ".las");

            CAnalytics.lasExportDuration = CAnalytics.GetDuration(exportStart);
        }
コード例 #6
0
ファイル: CDebug.cs プロジェクト: ja003/ForestReco2
        public static void Step(EProgramStep pStep)
        {
            string tileProgress = GetTileProgress(pStep);

            lastTextProgress = tileProgress + GetStepText(pStep);

            string[] message = new[] { lastTextProgress };
            if (pStep == EProgramStep.Exception)
            {
                CAnalytics.WriteErrors();
                return;
            }

            CProjectData.backgroundWorker.ReportProgress(0, message);
        }
コード例 #7
0
        private static void LoadTrees(List <string> pFileNames)
        {
            DateTime loadTreesStartTime = DateTime.Now;
            DateTime lastDebugTime      = DateTime.Now;

            CDebug.WriteLine("Load reftrees: ");
            foreach (string fileName in pFileNames)
            {
                CDebug.WriteLine(" - " + fileName);
            }

            for (int i = 0; i < pFileNames.Count; i++)
            {
                if (CProjectData.backgroundWorker.CancellationPending)
                {
                    return;
                }

                string fileName = pFileNames[i];
                CDebug.Progress(i, pFileNames.Count, 1, ref lastDebugTime, loadTreesStartTime, "load reftree");

                CRefTree deserializedRefTree     = CRefTree.Deserialize(fileName);
                bool     isDeserializedTreeValid = deserializedRefTree != null &&
                                                   deserializedRefTree.IsCurrentVersion();
                CRefTree refTree = isDeserializedTreeValid ?
                                   deserializedRefTree : new CRefTree(fileName, i, TREE_POINT_EXTENT, true);

                refTree.RefTreeTypeName = fileName;                 //GetTypeName(fileName);

                if (!refTree.isValid)
                {
                    //this reftree is not valid. case for reftrees in 'ignore' folder
                    CDebug.Warning($"Skipping reftree {fileName}");
                    continue;
                }
                //material set durring assigning to tree
                //refTree.Obj.UseMtl = CMaterialManager.GetRefTreeMaterial(counter);

                Trees.Add(refTree);
                CDebug.WriteLine($"Loaded tree: {fileName}. height = {refTree.GetTreeHeight()}");
            }
            CAnalytics.loadReftreesDuration = CAnalytics.GetDuration(loadTreesStartTime);
            CDebug.Duration("Load reftrees", loadTreesStartTime);

            CAnalytics.loadedReftrees = Trees.Count;

            DebugReftrees();
        }
コード例 #8
0
ファイル: CDebug.cs プロジェクト: ja003/ForestReco2
        internal static void Duration(string pText, DateTime pStartTime)
        {
            double totalSeconds = CAnalytics.GetDuration(pStartTime);

            WriteLine(pText + " | duration = " + totalSeconds);
        }
コード例 #9
0
        public static void Export(int pTileIndex)
        {
            if (!exportBitmap)
            {
                return;
            }

            //init for each tile
            treeMarkerSize = GetTreeBrushSize(false);

            DateTime bitmapStart = DateTime.Now;

            CVegeArray array  = CProjectData.Points.vegeDetailArray;
            Bitmap     bitmap = new Bitmap(array.arrayXRange, array.arrayYRange);

            int maxValue = 0;

            for (int x = 0; x < array.arrayXRange; x++)
            {
                for (int y = 0; y < array.arrayYRange; y++)
                {
                    CVegeField element  = array.GetField(x, y);
                    int?       colorVal = element.GetColorValue();               //from detailed array

                    if (colorVal == null)
                    {
                        continue;
                    }

                    int colorVaInt = (int)colorVal;
                    if (colorVaInt > maxValue)
                    {
                        maxValue = colorVaInt;
                    }

                    int rVal = colorVaInt;
                    //highlight buffer zone
                    bool isAtBufferZone = CTreeMath.IsAtBufferZone(element.Center);
                    if (isAtBufferZone)
                    {
                        rVal = Math.Min(rVal + 30, 255);
                    }

                    Color color = Color.FromArgb(rVal, colorVaInt, colorVaInt);
                    //CDebug.WriteLine($"{x},{y} = {color}");

                    bitmap.SetPixel(x, y, color);


                    if (exportMain && !isAtBufferZone)
                    {
                        Tuple <int, int> posInMain = GetIndexInMainBitmap(element.Center);

                        if (posInMain == null)
                        {
                            continue;
                        }

                        if (color.R > 255)
                        {
                            CDebug.Error("color.R = " + color.R);
                        }
                        mainMap.SetPixel(posInMain.Item1, posInMain.Item2, color);
                    }
                }
            }

            //StretchColorRange(ref bitmap, maxValue);

            //FilterBitmap(ref bitmap, GetKernelSize(array.stepSize, .2f), EFilter.Max);

            if (exportHeightmap)
            {
                ExportBitmap(bitmap, "heightmap", pTileIndex);
            }

            int  bitmapsCount = 3;
            bool useCheckTree = CParameterSetter.GetBoolSettings(ESettings.useCheckTreeFile);

            if (useCheckTree)
            {
                bitmapsCount++;
            }

            CDebug.Progress(1, bitmapsCount, 1, ref bitmapStart, bitmapStart, "bitmap: ");

            if (exportPositions)
            {
                Bitmap bitmapTreePos = new Bitmap(bitmap);
                AddTreesToBitmap(array, bitmapTreePos, true, false);
                ExportBitmap(bitmapTreePos, "tree_positions", pTileIndex);

                if (useCheckTree)
                {
                    Bitmap bitmapChecktree = new Bitmap(bitmapTreePos);
                    ExportBitmap(bitmapChecktree, "tree_check", pTileIndex);
                    CDebug.Progress(bitmapsCount - 1, bitmapsCount, 1, ref bitmapStart, bitmapStart, "bitmap: ");
                }
            }

            CDebug.Progress(2, bitmapsCount, 1, ref bitmapStart, bitmapStart, "bitmap: ");

            if (exportBorders)
            {
                Bitmap bitmapTreeBorder = new Bitmap(bitmap);
                AddTreesToBitmap(array, bitmapTreeBorder, true, true);
                ExportBitmap(bitmapTreeBorder, "tree_borders", pTileIndex);
            }

            CDebug.Progress(bitmapsCount, bitmapsCount, 1, ref bitmapStart, bitmapStart, "bitmap: ");

            CAnalytics.bitmapExportDuration = CAnalytics.GetDuration(bitmapStart);
            CDebug.Duration("bitmap export", bitmapStart);
        }
コード例 #10
0
ファイル: CProgramStarter.cs プロジェクト: ja003/ForestReco2
        public static EProcessResult Start()
        {
            CSequenceController.SetValues();

            DateTime startTime = DateTime.Now;

            CProjectData.Init();
            CTreeManager.Init();
            CAnalytics.Init();

            CDartTxt.Init();
            CLasExporter.Init();
            CBiomassController.Init(
                CParameterSetter.GetStringSettings(ESettings.dbh),
                CParameterSetter.GetStringSettings(ESettings.agb));
            CTreeRadiusCalculator.Init();
            CShpController.Init();
            CReftreeManager.Init();

            Thread.CurrentThread.CurrentCulture = new CultureInfo("en");

            //GENERAL
            CProjectData.useMaterial     = true;
            CObjExporter.simplePointsObj = false;

            CMaterialManager.Init();

            string[] workerResult = new string[2];
            workerResult[0] = "this string";
            workerResult[1] = "some other string";
            CProjectData.backgroundWorker.ReportProgress(10, workerResult);

            try
            {
                List <string> tiledFiles = CProgramLoader.GetTiledPreprocessedFilePaths();

                tilesCount = tiledFiles.Count;
                int startTile = CParameterSetter.GetIntSettings(ESettings.startIndex);
                if (startTile < 0 || startTile >= tiledFiles.Count)
                {
                    throw new Exception($"Parameter startTile = {startTile}, tiledFiles.Count = {tiledFiles.Count}");
                }

                for (int i = startTile; i < tiledFiles.Count; i++)
                {
                    string         tiledFilePath = tiledFiles[i];
                    EProcessResult tileProcess   = ProcessTile(tiledFilePath, i);
                    if (CProjectData.backgroundWorker.CancellationPending)
                    {
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                CDebug.Error(
                    $"{Environment.NewLine}exception: {e.Message} {Environment.NewLine}{Environment.NewLine}" +
                    $"StackTrace: {e.StackTrace}{Environment.NewLine}");
                OnException();
                return(EProcessResult.Exception);
            }

            if (CProjectData.backgroundWorker.CancellationPending)
            {
                CDebug.Step(EProgramStep.Cancelled);
                return(EProcessResult.Cancelled);
            }

            CDebug.Step(EProgramStep.ExportMainFiles);
            //TODO: implement this in super class for all controllers
            //dont create the main file if not needed
            if (tilesCount > 1)
            {
                CDartTxt.ExportMain();
                CLasExporter.ExportMain();
                CShpController.ExportMain();
            }
            CBitmapExporter.ExportMain();

            CDebug.Step(EProgramStep.Done);

            if (CSequenceController.IsLastSequence())
            {
                CSequenceController.OnLastSequenceEnd();
                return(EProcessResult.Done);
            }

            CSequenceController.currentConfigIndex++;
            return(Start());
        }
コード例 #11
0
ファイル: CProgramStarter.cs プロジェクト: ja003/ForestReco2
        private static EProcessResult ProcessTile(string pTilePath, int pTileIndex)
        {
            //has to reinit first for the correct progress output
            CDebug.ReInit();

            DateTime startTime = DateTime.Now;

            currentTileIndex = pTileIndex;


            List <Tuple <EClass, Vector3> > parsedLines;

            if (CRxpParser.IsRxp)
            {
                CRxpInfo rxpInfo = CRxpParser.ParseFile(pTilePath);
                parsedLines = rxpInfo.ParsedLines;
                //for now we expect only one tile in rxp processing
                CProjectData.currentTileHeader = rxpInfo.Header;
                CProjectData.mainHeader        = rxpInfo.Header;
            }
            else
            {
                string[] lines   = CProgramLoader.GetFileLines(pTilePath);
                bool     linesOk = lines != null && lines.Length > 0 && !string.IsNullOrEmpty(lines[0]);
                if (linesOk && CHeaderInfo.HasHeader(lines[0]))
                {
                    //todo: where to init header?
                    CProjectData.currentTileHeader = new CHeaderInfo(lines);
                }
                else
                {
                    const string noHeaderMsg = "Processed tile has no header";
                    CDebug.Error(noHeaderMsg);
                    throw new Exception(noHeaderMsg);
                }
                parsedLines = CProgramLoader.ParseLines(lines, true);
            }

            //has to be called after currentTileHeader is assigned
            CProjectData.ReInit(pTileIndex);             //has to reinit after each tile is processed
            CTreeManager.Reinit();

            if (CProjectData.backgroundWorker.CancellationPending)
            {
                return(EProcessResult.Cancelled);
            }

            CProgramLoader.ProcessParsedLines(parsedLines);

            if (CProjectData.backgroundWorker.CancellationPending)
            {
                return(EProcessResult.Cancelled);
            }

            CTreeManager.DebugTrees();

            CDebug.Step(EProgramStep.Export3D);
            CObjPartition.ExportPartition("", "tile" + pTileIndex);

            if (CProjectData.backgroundWorker.CancellationPending)
            {
                return(EProcessResult.Cancelled);
            }

            //has to be called after ExportPartition where final folder location is determined
            try
            {
                CDebug.Step(EProgramStep.Bitmap);
                CBitmapExporter.Export(pTileIndex);
            }
            catch (Exception e)
            {
                CDebug.Error("exception: " + e.Message);
            }

            CAnalytics.totalDuration = CAnalytics.GetDuration(startTime);
            CDebug.Duration("total time", startTime);

            CDebug.Step(EProgramStep.Dart);
            CDartTxt.ExportTile();

            CDebug.Step(EProgramStep.Shp);
            CShpController.ExportCurrent();

            CDebug.Step(EProgramStep.Las);
            CLasExporter.ExportTile();

            CDebug.Step(EProgramStep.Analytics);
            CAnalytics.Write(true);

            return(EProcessResult.Done);
        }
コード例 #12
0
        /// <summary>
        /// Assigns to each of detected trees the most suitable refTree
        /// </summary>
        public static void AssignRefTrees()
        {
            if (Trees.Count == 0)
            {
                //CDebug.Error("no reftrees loaded");
                return;
            }

            DateTime addTreeObjModelsStart = DateTime.Now;

            CDebug.WriteLine("Get reftree models");

            const int debugFrequency = 10;

            DateTime assignRefTreesStart = DateTime.Now;

            CDebug.WriteLine("AssignRefTrees");

            DateTime previousDebugStart = DateTime.Now;

            int   counter       = 0;
            float similaritySum = 0;

            foreach (CTree t in CTreeManager.Trees)
            {
                if (CProjectData.backgroundWorker.CancellationPending)
                {
                    break;
                }

                Tuple <CRefTree, STreeSimilarity> suitableRefTree = GetMostSuitableRefTree(t);
                CRefTree mostSuitableRefTree = suitableRefTree.Item1;
                if (mostSuitableRefTree == null)
                {
                    CDebug.Error("no reftrees assigned!");
                    continue;
                }

                SetRefTreeObjTransform(ref mostSuitableRefTree, t, suitableRefTree.Item2.angleOffset);
                similaritySum += suitableRefTree.Item2.similarity;
                //CDebug.WriteLine($"similaritySum += {suitableRefTree.Item2.similarity}");

                Obj suitableTreeObj = mostSuitableRefTree.Obj.Clone();
                suitableTreeObj.Name += "_" + t.treeIndex;
                t.assignedRefTreeObj  = suitableTreeObj;
                t.assignedRefTree     = mostSuitableRefTree;
                //t.RefTreeTypeName = mostSuitableRefTree.RefTreeTypeName; //copy the type name

                suitableTreeObj.UseMtl = t.assignedMaterial.Name;

                CDebug.Progress(counter, CTreeManager.Trees.Count, debugFrequency, ref previousDebugStart, assignRefTreesStart, "Assigned reftree");
                counter++;


                //CDebug.WriteLine("\n mostSuitableRefTree = " + mostSuitableRefTree);
            }


            CAnalytics.averageReftreeSimilarity = similaritySum / CTreeManager.Trees.Count;

            CAnalytics.reftreeAssignDuration = CAnalytics.GetDuration(addTreeObjModelsStart);
            CDebug.Duration("Assign reftree models", addTreeObjModelsStart);
        }