private List <Vector3> CreateVillageCenters(VillageGeneratorData villageGeneratorData, int villageCount)
    {
        List <Vector3> villagePositions = new List <Vector3>();
        // TODO: Generalise spawnpoint bounds
        float lowerSpawnBoundX = 0 - chunkSize / 2;
        // When the time comes to add vertical scaling, this will have to change based on a separate vertical bound
        float lowerSpawnBoundY = 0;
        float lowerSpawnBoundZ = 0 - chunkSize / 2;

        // TODO: Generalise spawnpoint bounds
        float upperSpawnBoundX = 0 + chunkSize / 2;
        // When the time comes to add vertical scaling, this will have to change based on a separate vertical bound
        float upperSpawnBoundY = 0;
        float upperSpawnBoundZ = 0 + chunkSize / 2;

        Vector3 lowerBounds = new Vector3(lowerSpawnBoundX, lowerSpawnBoundY, lowerSpawnBoundZ);
        Vector3 upperBounds = new Vector3(upperSpawnBoundX, upperSpawnBoundY, upperSpawnBoundZ);

        // Loop over each village, generate a center for it
        for (int idx = 0; idx < villageCount; idx++)
        {
            float   newVillageRadius = villageGeneratorData.spawnRadius.RandomSample();
            Vector3 spawnPosition    = UtilityFunctions.GetRandomVector3(lowerBounds, upperBounds);

            // Create Temp Area with the above parameters
            var tempArea = new TempRegion(RegionType.circle, newVillageRadius, spawnPosition);

            // Check for any collisions
            if (IsCircularRegionFree(tempArea))
            {
                string newVillageName = $"{villageGeneratorData.villageName}.{idx}";
                // Initialize new Village Center
                GameObject newVillageObject = new GameObject(name: newVillageName);
                var        newVillageNode   = newVillageObject.AddComponent <VillageNode>();

                // Find new head count
                int newVillagerCount = villageGeneratorData.headCount.RandomSample();

                newVillageNode.SetUp(newVillageName);
                newVillageNode.SetUpRegion(newVillageRadius, spawnPosition);

                var newVillageData = new VillageData
                {
                    HeadCount = newVillagerCount,
                    // huts will be grown as per necessity in the VillageNode script
                    hutSpawnRange = villageGeneratorData.percentageHutSpawnRadius * (newVillageRadius / 100),

                    villagersPerHut = villageGeneratorData.villagersPerHut,
                    villagerPrefab  = villageGeneratorData.villagerPrefab,
                    elderHutPrefab  = villageGeneratorData.elderHutPrefab,
                    hutPrefab       = villageGeneratorData.hutPrefab
                };

                newVillageNode.SetUpVillage(newVillageData);
                newVillageNode.BeginGenerationProcess();
                villagePositions.Add(newVillageNode.CenterPosition);
                Regions.Add(newVillageNode);
                AddLink(newVillageNode);
            }
        }
        return(villagePositions);
    }
    private List <Vector3> CreateForestCenters(ForestGeneratorData forestGeneratorData, int forestCount)
    {
        List <Vector3> forestPositions = new List <Vector3>();
        // TODO: Generalise spawnpoint bounds
        float lowerSpawnBoundX = 0 - chunkSize / 2;
        // When the time comes to add vertical scaling, this will have to change based on a separate vertical bound
        float lowerSpawnBoundY = 0;
        float lowerSpawnBoundZ = 0 - chunkSize / 2;

        // TODO: Generalise spawnpoint bounds
        float upperSpawnBoundX = 0 + chunkSize / 2;
        // When the time comes to add vertical scaling, this will have to change based on a separate vertical bound
        float upperSpawnBoundY = 0;
        float upperSpawnBoundZ = 0 + chunkSize / 2;

        Vector3 lowerBounds = new Vector3(lowerSpawnBoundX, lowerSpawnBoundY, lowerSpawnBoundZ);
        Vector3 upperBounds = new Vector3(upperSpawnBoundX, upperSpawnBoundY, upperSpawnBoundZ);

        // Loop over each forest, generate a center for it
        for (int idx = 0; idx < forestCount; idx++)
        {
            float   newForestRadius = forestGeneratorData.spawnRadius.RandomSample();
            Vector3 spawnPosition   = UtilityFunctions.GetRandomVector3(lowerBounds, upperBounds);

            // Create Temp Area with the above parameters
            var tempRegion = new TempRegion(RegionType.circle, newForestRadius, spawnPosition);

            // Check for any collisions
            bool isFreeRegion = IsCircularRegionFree(tempRegion);
            // One extra chance: If region is not free, generate a new region with half the min-max radius range
            if (!isFreeRegion)
            {
                // very hacky way to reduce min-max range to half the possible maximum range, retaining same minimum
                newForestRadius = (forestGeneratorData.spawnRadius + (-forestGeneratorData.spawnRadius.min) * 0.5f + forestGeneratorData.spawnRadius.min).RandomSample();
                spawnPosition   = UtilityFunctions.GetRandomVector3(lowerBounds, upperBounds);

                // Create Temp Region with the above parameters
                tempRegion = new TempRegion(RegionType.circle, newForestRadius, spawnPosition);
            }

            // Check for any collisions
            if (IsCircularRegionFree(tempRegion))
            {
                string newForestName = $"{forestGeneratorData.forestName}.{idx}";
                // Initialize new ForestCenter
                GameObject newForestObject = new GameObject(name: newForestName);
                var        newForestNode   = newForestObject.AddComponent <ForestNode>();

                // Find new tree count
                int newTreeCount = forestGeneratorData.treeCount.RandomSample();

                newForestNode.SetUp(newForestName);
                newForestNode.SetUpRegion(newForestRadius, spawnPosition);
                newForestNode.SetUpForest(forestGeneratorData.treePrefab, newTreeCount);

                forestPositions.Add(newForestNode.CenterPosition);
                Regions.Add(newForestNode);
                AddLink(newForestNode);
            }
        }
        return(forestPositions);
    }
Beispiel #3
0
        private void Execute_Click(object sender, EventArgs e)
        {
            HTuple  Width, Height, AbsoluteHisto;
            HObject Rectangle, Image;

            Image = HGraphics.allObj["pic"].Clone();
            //Calculate absolute histogram of the image
            HOperatorSet.GetImageSize(HGraphics.allObj["pic"], out Width, out Height);
            HOperatorSet.GenRectangle1(out Rectangle, 0, 0, Height - 1, Width - 1);
            HOperatorSet.GrayHistoAbs(Rectangle, HGraphics.allObj["pic"], 1, out AbsoluteHisto);


            //find the gray value with the greatest frequency
            HTuple Indices, BackgroundGrayValue;

            HOperatorSet.TupleSortIndex(AbsoluteHisto, out Indices);
            HOperatorSet.TupleInverse(Indices, out Indices);

            BackgroundGrayValue = Indices.I;
            if (BackgroundGrayValue > 255 - 21)
            {
                BackgroundGrayValue = 255 - 20;
            }

            //Thresholding
            HTuple  DilationRadius = 5;
            HObject TempRegion, TempContour;

            HOperatorSet.Threshold(HGraphics.allObj["pic"], out TempRegion, new HTuple(BackgroundGrayValue.I + 20, 0), new HTuple(255, BackgroundGrayValue.I - 20));
            HOperatorSet.Connection(TempRegion, out TempRegion);
            HOperatorSet.SelectShape(TempRegion, out TempRegion, "area", "and", 30, 999999);
            HOperatorSet.Union1(TempRegion, out TempRegion);
            HOperatorSet.DilationCircle(TempRegion, out TempRegion, DilationRadius);
            HOperatorSet.FillUp(TempRegion, out TempRegion);
            HOperatorSet.GenContourRegionXld(TempRegion, out TempContour, "border");
            HOperatorSet.SmoothContoursXld(TempContour, out TempContour, 5);
            HOperatorSet.GenRegionContourXld(TempContour, out TempRegion, "filled");
            HOperatorSet.ErosionCircle(TempRegion, out TempRegion, 5);
            HGraphics.allObj["1_defectregion_erosion_red_margin_thick"] = TempRegion.Clone();
            HOperatorSet.Union1(TempRegion, out TempRegion);

            HOperatorSet.ReduceDomain(Image, TempRegion, out Image);

            HObject DiffOfGauss, RegionCrossings;

            HOperatorSet.DiffOfGauss(Image, out DiffOfGauss, 4, 50);
            HOperatorSet.DualThreshold(DiffOfGauss, out RegionCrossings, 20, 11, 11);
            HGraphics.allObj["crossingregion_colored_margin"] = RegionCrossings;
            HGraphics.display();

            HTuple Number, Area, Row, Column, Convexity, Min, Max, Range, Mean, Deviation, Circularity;
            HTuple DefectInfo = new HTuple("XY", "Size", "Max", "Min", "Mean", "Deviation", "Circularity", "Convexity");

            HOperatorSet.CountObj(HGraphics.allObj["1_defectregion_erosion_red_margin_thick"], out Number);
            HOperatorSet.SetFont(hWindowControl.HalconWindow, "-Arial-12-*-0.5-*-*-1-ANSI_CHARSET-");
            for (int Index = 1; Index <= Number; Index++)
            {
                HOperatorSet.SelectObj(HGraphics.allObj["1_defectregion_erosion_red_margin_thick"], out TempRegion, Index);
                HOperatorSet.AreaCenter(TempRegion, out Area, out Row, out Column);
                HOperatorSet.Convexity(TempRegion, out Convexity);
                HOperatorSet.MinMaxGray(TempRegion, Image, 0, out Min, out Max, out Range);
                HOperatorSet.Intensity(TempRegion, Image, out Mean, out Deviation);
                HOperatorSet.RegionFeatures(TempRegion, "circularity", out Circularity);

                HTuple Unit = new HTuple("(" + Row + ", " + Column + ")", Area, Max, Min, Mean, Deviation, Circularity, Convexity);

                HDevelopExport.disp_message(hWindowControl.HalconWindow, DefectInfo + ": " + Unit, "image", Row, Column, "green", "false");
            }

            HOperatorSet.SetFont(hWindowControl.HalconWindow, "-Courier New-18-*-*-*-*-1-");
            HDevelopExport.disp_message(hWindowControl.HalconWindow, "Number of Defect: " + Number, "window", 12, 12, "green", "false");
        }