コード例 #1
0
        private static void AddTreesToBitmap(CVegeArray pArray, Bitmap pBitmap, bool pTreePostition, bool pTreeBorder)
        {
            List <CTree> allTrees = new List <CTree>();

            allTrees.AddRange(CTreeManager.Trees);
            allTrees.AddRange(CTreeManager.InvalidTrees);

            foreach (CTree tree in allTrees)
            {
                try
                {
                    CVegeField fieldWithTree = pArray.GetFieldContainingPoint(tree.peak.Center);
                    if (fieldWithTree == null)
                    {
                        CDebug.Error($"tree {tree.treeIndex} field = null");
                        continue;
                    }

                    int x = fieldWithTree.indexInField.Item1;
                    int y = fieldWithTree.indexInField.Item2;

                    if (IsOOB(x, y, pBitmap))
                    {
                        CDebug.Error($"{x},{y} is OOB {pBitmap.Width}x{pBitmap.Height}");
                        continue;
                    }

                    //draw branch extents
                    if (pTreeBorder && tree.isValid)
                    {
                        List <Vector3> furthestPoints = tree.GetFurthestPoints();
                        //	new List<Vector3>();
                        //foreach(CBranch branch in tree.Branches)
                        //{
                        //	furthestPoints.Add(branch.furthestPoint);
                        //}
                        for (int i = 0; i < furthestPoints.Count; i++)
                        {
                            Vector3 furthestPoint     = furthestPoints[i];
                            Vector3 nextFurthestPoint = furthestPoints[(i + 1) % furthestPoints.Count];

                            CVegeField fieldWithFP1 = pArray.GetFieldContainingPoint(furthestPoint);
                            CVegeField fieldWithFP2 = pArray.GetFieldContainingPoint(nextFurthestPoint);
                            if (fieldWithFP1 == null || fieldWithFP2 == null)
                            {
                                CDebug.Error($"futhest points {furthestPoint} + {nextFurthestPoint} - no field assigned");
                                continue;
                            }

                            int x1 = fieldWithFP1.indexInField.Item1;
                            int y1 = fieldWithFP1.indexInField.Item2;
                            int x2 = fieldWithFP2.indexInField.Item1;
                            int y2 = fieldWithFP2.indexInField.Item2;

                            using (Graphics g = Graphics.FromImage(pBitmap))
                            {
                                g.DrawLine(treeBorderPen, x1, y1, x2, y2);
                            }
                        }

                        foreach (CBranch branch in tree.Branches)
                        {
                            CVegeField fieldWithBranch = pArray.GetFieldContainingPoint(branch.furthestPoint);
                            if (fieldWithBranch == null)
                            {
                                CDebug.Error($"branch {branch} is OOB");
                                continue;
                            }

                            int _x = fieldWithBranch.indexInField.Item1;
                            int _y = fieldWithBranch.indexInField.Item2;

                            using (Graphics g = Graphics.FromImage(pBitmap))
                            {
                                g.DrawLine(branchPen, x, y, _x, _y);
                            }
                        }
                    }
                    //mark tree position
                    if (pTreePostition)
                    {
                        DrawTreeOnBitmap(pBitmap, tree, x, y);

                        bool isAtBufferZone = CTreeMath.IsAtBufferZone(tree);
                        if (exportMain && !isAtBufferZone)
                        {
                            //Tuple<int, int> posInMain = CGroundArray.GetPositionInArray(
                            //	tree.peak.Center, CProjectData.mainHeader.TopLeftCorner, mainMapStepSize);

                            //CUtils.TransformArrayIndexToBitmapIndex(ref posInMain,
                            //	CProjectData.mainHeader, mainMapStepSize, mainMap);
                            Tuple <int, int> posInMain = GetIndexInMainBitmap(tree.peak.Center);
                            x = posInMain.Item1;
                            y = posInMain.Item2;
                            if (posInMain == null)
                            {
                                continue;
                            }

                            Color color = mainMap.GetPixel(x, y);
                            if (!IsTreeColoured(color))
                            {
                                DrawTreeOnBitmap(mainMap, tree, x, y);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    CDebug.Error(e.Message);
                }
            }
        }
コード例 #2
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);
        }
コード例 #3
0
        private static Tuple <CRefTree, STreeSimilarity> GetMostSuitableRefTree(CTree pTree)
        {
            if (Trees.Count == 0)
            {
                CDebug.Error("no reftrees defined!");
                return(null);
            }

            CRefTree        mostSuitableTree = Trees[0];
            STreeSimilarity treeSimilarity   = new STreeSimilarity();
            STreeSimilarity bestSimilarity   = new STreeSimilarity();

            if (Trees.Count == 1 && !forceAlgorithm)
            {
                return(new Tuple <CRefTree, STreeSimilarity>(mostSuitableTree, treeSimilarity));
            }
            bool forceRandom = false;            //pTree.treeIndex != debugTree;

            bool randomReftree = CParameterSetter.GetBoolSettings(ESettings.assignRefTreesRandom) &&
                                 pTree.treeIndex != debugTree;

            if (forceRandom || randomReftree)
            {
                //int random = new Random().Next(0, Trees.Count);
                int rnd = random.Next(0, Trees.Count);

                //if (debugSimilarites) { CDebug.WriteLine($"random = {random}"); }
                return(new Tuple <CRefTree, STreeSimilarity>(Trees[rnd], treeSimilarity));
            }
            if (debugSimilarites)
            {
                CDebug.WriteLine("\n" + pTree.treeIndex + " similarities = ");
            }


            foreach (CRefTree refTree in Trees)
            {
                if (CProjectData.backgroundWorker.CancellationPending)
                {
                    break;
                }

                treeSimilarity = CTreeMath.GetSimilarityWith(refTree, pTree);
                //float similarity = treeSimilarity.similarity;
                if (debugSimilarites)
                {
                    CDebug.WriteLine($"{refTree.fileName} similarity = {treeSimilarity.similarity}");
                }

                if (treeSimilarity.similarity > bestSimilarity.similarity)
                {
                    mostSuitableTree = refTree;
                    bestSimilarity   = treeSimilarity;
                }
                if (bestSimilarity.similarity > 0.9f && !forceAlgorithm)
                {
                    break;
                }
            }

            if (debugSimilarites)
            {
                CDebug.WriteLine("Most suitable reftree = " + mostSuitableTree.Obj.Name + ". similarity = " + bestSimilarity.similarity);
                CDebug.WriteLine($"tree height = {pTree.GetTreeHeight()}");
                CDebug.WriteLine($"reftree height = {mostSuitableTree.GetTreeHeight()}");
                CDebug.WriteLine($"angle offset = {bestSimilarity.angleOffset}");
            }

            return(new Tuple <CRefTree, STreeSimilarity>(mostSuitableTree, bestSimilarity));
        }