Merge pull request #88020 from ShnitzelX2/more-region-layouts-voronoi
More region layouts + Voronoi library
This commit is contained in:
+23
@@ -150,3 +150,26 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
JCash/voronoi is licensed under the MIT License. The full license text is as follows:
|
||||
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015 Mathias Westerdahl
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -11,6 +11,8 @@ Region layouts provide handling for assigning region settings by overmap at a wo
|
||||
}
|
||||
```
|
||||
|
||||
DISCLAIMER: as of July 2026, overmap feature generation (lakes, oceans, highways, etc.) does not take into account adjacent overmaps that may be a different region. ESPECIALLY DO NOT add multiple regions into the default dimension without sufficient testing first! Mods are fine, as long as you're willing to maintain them.
|
||||
|
||||
Region layouts have a secondary type: "generation_mode", which defines how the layout is generated.
|
||||
|
||||
There are two broad types of region layouts: STATIC and DYNAMIC.
|
||||
@@ -26,3 +28,150 @@ All overmaps are generated with a single region. See the top of this document fo
|
||||
| Identifier | Description |
|
||||
| ---------------------- | ----------------------------------------------------------------------------- |
|
||||
| `uniform_region` | region_id -- all overmaps will generate with this region. |
|
||||
|
||||
## RANDOM (Type: DYNAMIC)
|
||||
All overmaps are generated with a random region, selected from a weighted list.
|
||||
|
||||
| Identifier | Description |
|
||||
| ---------------------- | ----------------------------------------------------------------------------- |
|
||||
| `random_regions` | array of (region_id, int) pairs -- list of regions to pick from |
|
||||
|
||||
```json
|
||||
{
|
||||
"type": "dimension_region_layout",
|
||||
"id": "test_layout",
|
||||
"generation_mode": "RANDOM",
|
||||
"random_regions": [ ["default", 4],["highlands", 1],["spider_dimension", 1],["string_dimension", 1],["radiosphere", 1] ]
|
||||
}
|
||||
```
|
||||
|
||||
The above example may output a world map like so:
|
||||
|
||||
ACCCDAACC
|
||||
DBDBACCCA
|
||||
CABCBBDCC
|
||||
CACCCCCEE
|
||||
CCCCBBCEB
|
||||
CCDEBDBCC
|
||||
CDBCCCADB
|
||||
CDEDCCBCC
|
||||
CCADDCDAA
|
||||
|
||||
C = default
|
||||
A = highlands
|
||||
B = spider_dimension
|
||||
D = radiosphere
|
||||
E = string_dimension
|
||||
|
||||
## ANGLES (Type: DYNAMIC)
|
||||
Overmaps are generated based on their angle relative to overmap (0,0).
|
||||
Sectors are made from (360 / N)-degree wedges, where N is the count of regions provided.
|
||||
Regions can be provided more than once to form larger sectors.
|
||||
|
||||
| Identifier | Description |
|
||||
| ---------------------- | ------------------------------------------------------------------------------ |
|
||||
| `angles_regions` | region_id -- up to 16 region_ids, clockwise starting from north |
|
||||
| `angles_offset` | angle in degrees -- up to 180 degrees, offset from 0 degrees north, default 0 |
|
||||
|
||||
```json
|
||||
{
|
||||
"type": "dimension_region_layout",
|
||||
"id": "test_layout",
|
||||
"generation_mode": "ANGLES",
|
||||
"angle_regions": ["default", "string_dimension", "default", "string_dimension", "default", "string_dimension", "default", "string_dimension" ]
|
||||
}
|
||||
```
|
||||
|
||||
The above example may output a world map like so:
|
||||
|
||||
AAAABBBBA
|
||||
BAAABBBAA
|
||||
BBAABBAAA
|
||||
BBBABAAAA
|
||||
BBBBBBBBB
|
||||
AAAABABBB
|
||||
AAABBAABB
|
||||
AABBBAAAB
|
||||
ABBBBAAAA
|
||||
|
||||
A = string_dimension
|
||||
B = default
|
||||
|
||||
## MANUAL_VORONOI (Type: STATIC)
|
||||
Uses a Voronoi diagram generator with interchangeable point sets to generate blobs of regions.
|
||||
About Voronoi diagrams: https://en.wikipedia.org/wiki/Voronoi_diagram
|
||||
A decent drafter for your intended layout: https://alexbeutel.com/webgl/voronoi.html
|
||||
|
||||
| Identifier | Description |
|
||||
| ------------------------- | ----------------------------------------------------------------------------- |
|
||||
| `generated_bounds_min` | tripoint_abs_om -- minimum layout overmap bound |
|
||||
| `generated_bounds_max` | tripoint_abs_om -- maximum layout overmap bound |
|
||||
| `layout_out_of_bounds` | region_layout_id -- the DYNAMIC layout used for out_of_bounds generation |
|
||||
| `region_point_sets` | array of region_point_set -- the point sets used for Voronoi generation |
|
||||
|
||||
This layout mode uses `region_point_set`, which are defined as follows:
|
||||
|
||||
| Identifier | Description |
|
||||
| ------------------------- | ----------------------------------------------------------------------------- |
|
||||
| `region_points` | array of tripoint_abs_om -- all overmap points in this set |
|
||||
| `regions_weighted` | array of (region_id, int) pairs -- regions picked and assigned to points |
|
||||
| `remove_region` | boolean -- whether to remove picked regions from `regions_weighted` |
|
||||
| `default_region` | region_id -- used if `regions_weighted` is empty |
|
||||
| `region_point_variance` | int -- maximum random offset from `region_points` |
|
||||
|
||||
```json
|
||||
{
|
||||
"type": "dimension_region_layout",
|
||||
"id": "test_layout",
|
||||
"generation_mode": "MANUAL_VORONOI",
|
||||
"layout_out_of_bounds": "default",
|
||||
"generated_bounds_min": [-10,-10, 0],
|
||||
"generated_bounds_max": [10, 10, 0],
|
||||
"region_point_sets": [
|
||||
{
|
||||
"regions_weighted": [["default", 1],["highlands", 1],["spider_dimension", 1],["string_dimension", 1],["radiosphere", 1] ],
|
||||
"region_points": [[0, 0, 0], [-5, -5, 0], [5, 5, 0], [5, -5, 0], [-5, 5, 0]],
|
||||
"remove_region": true,
|
||||
"default_region": "default",
|
||||
"region_point_variance": 2
|
||||
},
|
||||
{
|
||||
"regions_weighted": [["highlands", 1],["spider_dimension", 1],["string_dimension", 1],["radiosphere", 1] ],
|
||||
"region_points": [[-8, -8, 0], [8, 8, 0], [8, -8, 0], [-8, 8, 0]],
|
||||
"remove_region": true,
|
||||
"default_region": "default",
|
||||
"region_point_variance": 1
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
The above example may output a world map like so (note that both highlands points merged!):
|
||||
|
||||
AAAAAAAABBBCCCBBBBBBB
|
||||
AAAAAAABBBBCCCBBBBBBB
|
||||
AAAAAABBBBBCCCCBBBBBB
|
||||
AAAAABBBBBBCCCCBBBBBB
|
||||
AAAABBBBBBBCCCCCBBBBB
|
||||
AAABBBBBBBBCCCCCCBBBB
|
||||
AABBBBBBBBBCCCCCCBBBB
|
||||
ABBBBBBBBBDCCCCCCCBBB
|
||||
BBBBBBBBBBDDDCCCCCBBB
|
||||
BBBBBBBBBBDDDDDCCCCBB
|
||||
CBBBBBBBBDDDDDDDDEEEE
|
||||
CCCBBBBBADDDDDDDEEEEE
|
||||
CCCCCCAAAADDDDEEEEEEE
|
||||
CCCCCCAAAAADDEEEEEEEE
|
||||
CCCCCCAAAAAAEEEEEEEEE
|
||||
CCCCCCAAAAAAEEEEEEEEE
|
||||
CCCCCCAAAAAAEEEEEEEEE
|
||||
CCCCCCAAAAAAEEEEEEEEE
|
||||
CCCCCCAAAAAAEEEEEEEEE
|
||||
CCCCCCAAAAAAEEEEEEEEE
|
||||
CCCCCCAAAAAAAEEEEEEEE
|
||||
A = radiosphere
|
||||
B = string_dimension
|
||||
C = spider_dimension
|
||||
D = default
|
||||
E = highlands
|
||||
|
||||
|
||||
@@ -36,6 +36,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "zzip", "zzip.vcxproj", "{12
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "fmt", "fmt.vcxproj", "{F2098959-AAD9-4210-A64C-FC4A09A45556}"
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "jc_voronoi", "jc_voronoi.vcxproj", "{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|ARM64EC = Debug|ARM64EC
|
||||
@@ -427,6 +429,42 @@ Global
|
||||
{F2098959-AAD9-4210-A64C-FC4A09A45556}.Release-NoTiles|x64.Build.0 = Release|x64
|
||||
{F2098959-AAD9-4210-A64C-FC4A09A45556}.Release-NoTiles|x86.ActiveCfg = Release|Win32
|
||||
{F2098959-AAD9-4210-A64C-FC4A09A45556}.Release-NoTiles|x86.Build.0 = Release|Win32
|
||||
{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}.Debug|ARM64EC.ActiveCfg = Debug|ARM64EC
|
||||
{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}.Debug|ARM64EC.Build.0 = Debug|ARM64EC
|
||||
{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}.Debug|x64.Build.0 = Debug|x64
|
||||
{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}.Debug|x86.ActiveCfg = Debug|Win32
|
||||
{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}.Debug|x86.Build.0 = Debug|Win32
|
||||
{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}.Debug-NoTiles|ARM64EC.ActiveCfg = Debug-NoTiles|ARM64EC
|
||||
{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}.Debug-NoTiles|ARM64EC.Build.0 = Debug-NoTiles|ARM64EC
|
||||
{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}.Debug-NoTiles|x64.ActiveCfg = Debug-NoTiles|x64
|
||||
{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}.Debug-NoTiles|x64.Build.0 = Debug-NoTiles|x64
|
||||
{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}.Debug-NoTiles|x86.ActiveCfg = Debug-NoTiles|Win32
|
||||
{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}.Debug-NoTiles|x86.Build.0 = Debug-NoTiles|Win32
|
||||
{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}.Quick|ARM64EC.ActiveCfg = Release|ARM64EC
|
||||
{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}.Quick|ARM64EC.Build.0 = Release|ARM64EC
|
||||
{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}.Quick|x64.ActiveCfg = Release-NoTiles|x64
|
||||
{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}.Quick|x64.Build.0 = Release-NoTiles|x64
|
||||
{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}.Quick|x86.ActiveCfg = Debug|Win32
|
||||
{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}.Quick|x86.Build.0 = Debug|Win32
|
||||
{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}.Quick-NoTiles|ARM64EC.ActiveCfg = Release-NoTiles|ARM64EC
|
||||
{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}.Quick-NoTiles|ARM64EC.Build.0 = Release-NoTiles|ARM64EC
|
||||
{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}.Quick-NoTiles|x64.ActiveCfg = Release-NoTiles|x64
|
||||
{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}.Quick-NoTiles|x64.Build.0 = Release-NoTiles|x64
|
||||
{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}.Quick-NoTiles|x86.ActiveCfg = Release-NoTiles|Win32
|
||||
{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}.Quick-NoTiles|x86.Build.0 = Release-NoTiles|Win32
|
||||
{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}.Release|ARM64EC.ActiveCfg = Release|ARM64EC
|
||||
{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}.Release|ARM64EC.Build.0 = Release|ARM64EC
|
||||
{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}.Release|x64.ActiveCfg = Release|x64
|
||||
{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}.Release|x64.Build.0 = Release|x64
|
||||
{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}.Release|x86.ActiveCfg = Release|Win32
|
||||
{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}.Release|x86.Build.0 = Release|Win32
|
||||
{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}.Release-NoTiles|ARM64EC.ActiveCfg = Release-NoTiles|ARM64EC
|
||||
{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}.Release-NoTiles|ARM64EC.Build.0 = Release-NoTiles|ARM64EC
|
||||
{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}.Release-NoTiles|x64.ActiveCfg = Release-NoTiles|x64
|
||||
{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}.Release-NoTiles|x64.Build.0 = Release-NoTiles|x64
|
||||
{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}.Release-NoTiles|x86.ActiveCfg = Release-NoTiles|Win32
|
||||
{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}.Release-NoTiles|x86.Build.0 = Release-NoTiles|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
@@ -0,0 +1,172 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug-NoTiles|ARM64EC">
|
||||
<Configuration>Debug-NoTiles</Configuration>
|
||||
<Platform>ARM64EC</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|ARM64EC">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>ARM64EC</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug-NoTiles|Win32">
|
||||
<Configuration>Debug-NoTiles</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release-NoTiles|ARM64EC">
|
||||
<Configuration>Release-NoTiles</Configuration>
|
||||
<Platform>ARM64EC</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|ARM64EC">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>ARM64EC</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release-NoTiles|Win32">
|
||||
<Configuration>Release-NoTiles</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug-NoTiles|x64">
|
||||
<Configuration>Debug-NoTiles</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release-NoTiles|x64">
|
||||
<Configuration>Release-NoTiles</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="..\src\third-party\jc_voronoi\jc_voronoi.h" />
|
||||
<ClInclude Include="..\src\third-party\jc_voronoi\jc_voronoi_clip.h" />
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>16.0</VCProjectVersion>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<ProjectGuid>{46C02CBB-D9D2-42F3-AFC7-9204944F0F02}</ProjectGuid>
|
||||
<RootNamespace>flatbuffers</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Label="Configuration">
|
||||
<ConfigurationType>StaticLibrary</ConfigurationType>
|
||||
<PlatformToolset>$(DefaultPlatformToolset)</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
<PreferredToolArchitecture>x64</PreferredToolArchitecture>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)'=='Debug'" Label="Configuration">
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)'=='Release'" Label="Configuration">
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<WholeProgramOptimization>false</WholeProgramOptimization>
|
||||
</PropertyGroup>
|
||||
<Import Project="Cataclysm.Cpp.props" />
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="Shared">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
<Import Project="Cataclysm-common.props" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)'=='Release'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Label="Vcpkg">
|
||||
<VcpkgEnabled>true</VcpkgEnabled>
|
||||
<VcpkgEnableManifest>true</VcpkgEnableManifest>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Label="Vcpkg" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<VcpkgTriplet>x64-windows-static</VcpkgTriplet>
|
||||
<VcpkgHostTriplet>$(VcpkgTriplet)</VcpkgHostTriplet>
|
||||
<VcpkgConfiguration>$(Configuration)</VcpkgConfiguration>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Label="Vcpkg" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<VcpkgTriplet>x64-windows-static</VcpkgTriplet>
|
||||
<VcpkgHostTriplet>$(VcpkgTriplet)</VcpkgHostTriplet>
|
||||
<VcpkgConfiguration>$(Configuration)</VcpkgConfiguration>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup>
|
||||
<ClCompile>
|
||||
<PreprocessorDefinitions>_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
</ClCompile>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)'=='Debug'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)'=='Debug-NoTiles'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)'=='Release'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)'=='Release-NoTiles'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Platform)'=='Win32'">
|
||||
<ClCompile>
|
||||
<PreprocessorDefinitions>WIN32;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
</ClCompile>
|
||||
</ItemDefinitionGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
@@ -18,7 +18,9 @@
|
||||
#include "coords_fwd.h" // IWYU pragma: export
|
||||
#include "cuboid_rectangle.h"
|
||||
#include "debug.h"
|
||||
#include "jc_voronoi/jc_voronoi.h"
|
||||
#include "line.h" // IWYU pragma: keep
|
||||
#include "map_iterator.h"
|
||||
#include "map_scale_constants.h"
|
||||
#include "point.h"
|
||||
|
||||
@@ -1206,6 +1208,81 @@ coords::coord_point_ob<Point, Origin, Scale> construct_min( const
|
||||
return constructed_min;
|
||||
}
|
||||
|
||||
// converts a JCV point to a CATA point
|
||||
template<typename Point, coords::origin Origin, coords::scale Scale>
|
||||
coords::coord_point_ob<Point, Origin, Scale> jcv_to_cata_point( const jcv_point &loc )
|
||||
{
|
||||
return coords::coord_point_ob<Point, Origin, Scale>( static_cast<int>( loc.x ),
|
||||
static_cast<int>( loc.y ) );
|
||||
}
|
||||
|
||||
template<typename Point, coords::origin Origin, coords::scale Scale>
|
||||
coords::coord_point_ib<Point, Origin, Scale> jcv_to_cata_point( const jcv_point &loc )
|
||||
{
|
||||
return jcv_to_cata_point<coords::coord_point_ib<Point, Origin, Scale>>( loc );
|
||||
}
|
||||
|
||||
// returns whether a single point `loc` is in the inclusive triangle defined by points `t0 -> t1 -> t2`
|
||||
// barycentric coordinate solution obtained from https://stackoverflow.com/a/14382692,
|
||||
// which itself was derived from https://en.wikipedia.org/wiki/Barycentric_coordinate_system
|
||||
template<typename Point, coords::origin Origin, coords::scale Scale>
|
||||
bool point_in_triangle_2d( const coords::coord_point_ob<Point, Origin, Scale> &loc,
|
||||
const coords::coord_point_ob<Point, Origin, Scale> &t0,
|
||||
const coords::coord_point_ob<Point, Origin, Scale> &t1,
|
||||
const coords::coord_point_ob<Point, Origin, Scale> &t2 )
|
||||
{
|
||||
const float area = 0.5f * ( -t1.y() * t2.x() + t0.y() * ( -t1.x() + t2.x() ) + t0.x() *
|
||||
( t1.y() - t2.y() ) + t1.x() * t2.y() );
|
||||
const float s = 1.0f / ( 2.0f * area ) * ( t0.y() * t2.x() - t0.x() * t2.y() +
|
||||
( t2.y() - t0.y() ) * loc.x() + ( t0.x() - t2.x() ) * loc.y() );
|
||||
const float t = 1.0f / ( 2.0f * area ) * ( t0.x() * t1.y() - t0.y() * t1.x() +
|
||||
( t0.y() - t1.y() ) * loc.x() + ( t1.x() - t0.x() ) * loc.y() );
|
||||
constexpr float positive_float_threshold = -0.001f; // what constitutes a "positive number"
|
||||
return s >= positive_float_threshold && t >= positive_float_threshold &&
|
||||
1.0f - s - t >= positive_float_threshold;
|
||||
}
|
||||
|
||||
template<typename Point, coords::origin Origin, coords::scale Scale>
|
||||
bool point_in_triangle_2d( const coords::coord_point_ib<Point, Origin, Scale> &loc,
|
||||
const coords::coord_point_ib<Point, Origin, Scale> &t0,
|
||||
const coords::coord_point_ib<Point, Origin, Scale> &t1,
|
||||
const coords::coord_point_ib<Point, Origin, Scale> &t2 )
|
||||
{
|
||||
return point_in_triangle_2d( loc, t0, t1, t2 );
|
||||
}
|
||||
|
||||
// returns ALL points in the inclusive triangle defined by points `t0 -> t1 -> t2`
|
||||
template<typename Point, coords::origin Origin, coords::scale Scale>
|
||||
std::vector<coords::coord_point_ob<Point, Origin, Scale>>
|
||||
points_in_triangle_2d( const coords::coord_point_ob<Point, Origin, Scale> &t0,
|
||||
const coords::coord_point_ob<Point, Origin, Scale> &t1,
|
||||
const coords::coord_point_ob<Point, Origin, Scale> &t2 )
|
||||
{
|
||||
|
||||
const std::vector < coords::coord_point_ob<Point, Origin, Scale>> triangle_bounds = { t0, t1, t2 };
|
||||
std::vector < coords::coord_point_ob<Point, Origin, Scale>> inside_triangle_points;
|
||||
|
||||
coords::coord_point_ob<Point, Origin, Scale> min_bound = construct_min( triangle_bounds );
|
||||
coords::coord_point_ob<Point, Origin, Scale> max_bound = construct_max( triangle_bounds );
|
||||
|
||||
tripoint_range< coords::coord_point_ob<Point, Origin, Scale>> bounds( min_bound, max_bound );
|
||||
for( const coords::coord_point_ob<Point, Origin, Scale> &p : bounds ) {
|
||||
if( point_in_triangle_2d( p, t0, t1, t2 ) ) {
|
||||
inside_triangle_points.emplace_back( p );
|
||||
}
|
||||
}
|
||||
return inside_triangle_points;
|
||||
}
|
||||
|
||||
template<typename Point, coords::origin Origin, coords::scale Scale>
|
||||
bool points_in_triangle_2d(
|
||||
const coords::coord_point_ib<Point, Origin, Scale> &t0,
|
||||
const coords::coord_point_ib<Point, Origin, Scale> &t1,
|
||||
const coords::coord_point_ib<Point, Origin, Scale> &t2 )
|
||||
{
|
||||
return points_in_triangle_2d( t0, t1, t2 );
|
||||
}
|
||||
|
||||
/* find appropriate subdivided coordinates for absolute tile coordinate.
|
||||
* This is less obvious than one might think, for negative coordinates, so this
|
||||
* was created to give a definitive answer.
|
||||
|
||||
+168
-3
@@ -1,3 +1,4 @@
|
||||
#include "cata_utility.h"
|
||||
#include "coordinates.h"
|
||||
#include "debug.h"
|
||||
#include "enum_conversions.h"
|
||||
@@ -5,13 +6,22 @@
|
||||
#include "game.h"
|
||||
#include "generic_factory.h"
|
||||
#include "imgui/imgui.h"
|
||||
|
||||
// IWYU pragma: begin_keep
|
||||
#define JC_VORONOI_IMPLEMENTATION // NOLINT(clang-diagnostic-unused-macros)
|
||||
// IWYU pragma: end_keep
|
||||
#include "jc_voronoi/jc_voronoi.h"
|
||||
#include "line.h"
|
||||
#include "map_iterator.h"
|
||||
#include "output.h"
|
||||
#include "overmapbuffer.h"
|
||||
#include "overmap_worldgen.h"
|
||||
#include "overmapbuffer.h"
|
||||
#include "rng.h"
|
||||
#include "string_formatter.h"
|
||||
#include "translations.h"
|
||||
#include "units.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <unordered_set>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
@@ -40,12 +50,101 @@ void region_voronoi_point::deserialize( const JsonObject &jo )
|
||||
jo.read( "region", region );
|
||||
}
|
||||
|
||||
// converts a JCV point to a CATA point
|
||||
// generalize with template if needed beyond regions
|
||||
static tripoint_abs_om jcv_to_cata_point( const jcv_point &loc )
|
||||
{
|
||||
return tripoint_abs_om( static_cast<int>( loc.x ), -static_cast<int>( loc.y ), 0 );
|
||||
}
|
||||
|
||||
static void generate_cata_voronoi_map( Region_map &placed_regions,
|
||||
const std::vector<region_voronoi_point> ®ion_points,
|
||||
const inclusive_cuboid<tripoint_abs_om> &generated_bounds )
|
||||
{
|
||||
jcv_diagram diagram;
|
||||
memset( &diagram, 0, sizeof( jcv_diagram ) );
|
||||
std::vector<jcv_point> jcv_points;
|
||||
|
||||
// temporary map for `region_points`
|
||||
std::unordered_map<tripoint_abs_om, region_settings_id> site_regions;
|
||||
// y is negated because jcvoronoi flips y-axis
|
||||
for( const region_voronoi_point &rv_point : region_points ) {
|
||||
const jcv_point temp_point = { static_cast<float>( rv_point.region_point.x() ),
|
||||
-static_cast<float>( rv_point.region_point.y() )
|
||||
};
|
||||
jcv_points.emplace_back( temp_point );
|
||||
site_regions.emplace( rv_point.region_point, rv_point.region );
|
||||
}
|
||||
|
||||
const jcv_point bounds_min = { static_cast<float>( generated_bounds.p_min.x() ), static_cast<float>( generated_bounds.p_min.y() ) };
|
||||
const jcv_point bounds_max = { static_cast<float>( generated_bounds.p_max.x() ), static_cast<float>( generated_bounds.p_max.y() ) };
|
||||
const jcv_rect jcv_bounds = { bounds_min, bounds_max };
|
||||
|
||||
jcv_diagram_generate( region_points.size(), &*jcv_points.begin(), &jcv_bounds, nullptr, &diagram );
|
||||
|
||||
const jcv_site *sites = jcv_diagram_get_sites( &diagram );
|
||||
for( int i = 0; i < diagram.numsites; ++i ) {
|
||||
const jcv_site *site = &sites[i];
|
||||
const tripoint_abs_om site_point = jcv_to_cata_point( site->p );
|
||||
region_settings_id site_region = site_regions.at( site_point );
|
||||
|
||||
// add all points contained in a cell polygon by drawing triangles
|
||||
// from the center point to the cell edges
|
||||
const jcv_graphedge *e = site->edges;
|
||||
while( e ) {
|
||||
std::vector<tripoint_abs_om> cell_points = points_in_triangle_2d(
|
||||
site_point, jcv_to_cata_point( e->pos[0] ), jcv_to_cata_point( e->pos[1] ) );
|
||||
for( const tripoint_abs_om &cell_point : cell_points ) {
|
||||
if( !placed_regions.count( cell_point ) ) {
|
||||
placed_regions[cell_point] = site_region;
|
||||
}
|
||||
}
|
||||
|
||||
e = e->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void dimension_region_layout_generator_uniform::generate_dynamic(
|
||||
Region_map &placed_regions, const tripoint_abs_om ¤t_om )
|
||||
{
|
||||
placed_regions.emplace( current_om, uniform_region );
|
||||
}
|
||||
|
||||
void dimension_region_layout_generator_manual::generate_entire_layout(
|
||||
Region_map &placed_regions, const tripoint_abs_om & )
|
||||
{
|
||||
std::vector<region_voronoi_point> region_points;
|
||||
for( const region_point_set &rps : region_point_sets ) {
|
||||
std::vector<region_voronoi_point> resolved_points = rps.resolve();
|
||||
for( const region_voronoi_point &rvp : resolved_points ) {
|
||||
region_points.emplace_back( rvp );
|
||||
}
|
||||
}
|
||||
generate_cata_voronoi_map( placed_regions, region_points, generated_bounds );
|
||||
}
|
||||
|
||||
void dimension_region_layout_generator_random::generate_dynamic(
|
||||
Region_map &placed_regions, const tripoint_abs_om ¤t_om )
|
||||
{
|
||||
placed_regions.emplace( current_om, *random_regions.pick() );
|
||||
}
|
||||
|
||||
void dimension_region_layout_generator_angles::generate_dynamic(
|
||||
Region_map &placed_regions, const tripoint_abs_om ¤t_om )
|
||||
{
|
||||
|
||||
units::angle angle_diff = coord_to_angle( tripoint_abs_om::zero, current_om );
|
||||
// align sectors towards north
|
||||
angle_diff += angles_offset;
|
||||
|
||||
const units::angle sector_size = units::from_degrees( 360.0f / static_cast<double>
|
||||
( get_sector_count() ) );
|
||||
int sector_index = modulo( static_cast<int>( angle_diff / sector_size ),
|
||||
get_sector_count() );
|
||||
placed_regions.emplace( current_om, angles_regions[sector_index] );
|
||||
}
|
||||
|
||||
region_settings_id dimension_region_layout_static::get_overmap_region(
|
||||
const tripoint_abs_om &om_point )
|
||||
{
|
||||
@@ -90,8 +189,8 @@ std::string enum_to_string<regions_generation_mode>( regions_generation_mode dat
|
||||
return "MANUAL_VORONOI";
|
||||
case regions_generation_mode::RANDOM:
|
||||
return "RANDOM";
|
||||
case regions_generation_mode::EIGHTHS:
|
||||
return "EIGHTHS";
|
||||
case regions_generation_mode::ANGLES:
|
||||
return "ANGLES";
|
||||
default:
|
||||
return "last";
|
||||
}
|
||||
@@ -173,6 +272,18 @@ void dimension_region_layout::load( const JsonObject &jo, const std::string_view
|
||||
load_layout_generator<dimension_region_layout_generator_uniform>( jo, layout_generator );
|
||||
break;
|
||||
}
|
||||
case regions_generation_mode::MANUAL_VORONOI: {
|
||||
load_layout_generator<dimension_region_layout_generator_manual>( jo, layout_generator );
|
||||
break;
|
||||
}
|
||||
case regions_generation_mode::RANDOM: {
|
||||
load_layout_generator<dimension_region_layout_generator_random>( jo, layout_generator );
|
||||
break;
|
||||
}
|
||||
case regions_generation_mode::ANGLES: {
|
||||
load_layout_generator<dimension_region_layout_generator_angles>( jo, layout_generator );
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -183,6 +294,29 @@ void dimension_region_layout_generator_uniform::deserialize( const JsonObject &j
|
||||
mandatory( jo, false, "uniform_region", uniform_region );
|
||||
}
|
||||
|
||||
void dimension_region_layout_generator_manual::deserialize( const JsonObject &jo )
|
||||
{
|
||||
dimension_region_layout_static::deserialize( jo );
|
||||
mandatory( jo, false, "region_point_sets", region_point_sets );
|
||||
}
|
||||
|
||||
void dimension_region_layout_generator_random::deserialize( const JsonObject &jo )
|
||||
{
|
||||
mandatory( jo, false, "random_regions", random_regions );
|
||||
}
|
||||
|
||||
void dimension_region_layout_generator_angles::deserialize( const JsonObject &jo )
|
||||
{
|
||||
mandatory( jo, false, "angles_regions", angles_regions );
|
||||
optional( jo, false, "angles_offset", angles_offset );
|
||||
if( angles_regions.size() > 16 || angles_regions.size() < 2 ) {
|
||||
debugmsg( "ANGLES layout must have between 2 and 16 entries" );
|
||||
}
|
||||
if( angles_offset < 0_degrees || angles_offset >= 180_degrees ) {
|
||||
debugmsg( "ANGLES offset must be in range [0, 180)" );
|
||||
}
|
||||
}
|
||||
|
||||
void dimension_region_layout_static::deserialize( const JsonObject &jo )
|
||||
{
|
||||
tripoint_abs_om bounds_min;
|
||||
@@ -238,3 +372,34 @@ void overmapbuffer::print_region_layout()
|
||||
popup( _( "Copied world region map to clipboard!" ) );
|
||||
}
|
||||
|
||||
std::vector<region_voronoi_point> region_point_set::resolve() const
|
||||
{
|
||||
std::vector<region_voronoi_point> returned_points;
|
||||
// make a copy of the weighted list to remove entries
|
||||
weighted_int_list<region_settings_id> regions_weighted_mod = regions_weighted;
|
||||
const int variance = region_point_variance;
|
||||
|
||||
for( const tripoint_abs_om &original_point : region_points ) {
|
||||
region_voronoi_point new_rv_point;
|
||||
region_settings_id *selected_region_ptr = regions_weighted_mod.pick();
|
||||
region_settings_id selected_region = selected_region_ptr == nullptr ? default_region :
|
||||
*selected_region_ptr;
|
||||
if( remove_region ) {
|
||||
regions_weighted_mod.remove( selected_region );
|
||||
}
|
||||
new_rv_point.region_point = original_point + point_rel_om( rng( -variance, variance ),
|
||||
rng( -variance, variance ) );
|
||||
new_rv_point.region = selected_region;
|
||||
returned_points.emplace_back( new_rv_point );
|
||||
}
|
||||
return returned_points;
|
||||
}
|
||||
|
||||
void region_point_set::deserialize( const JsonObject &jo )
|
||||
{
|
||||
optional( jo, false, "region_points", region_points );
|
||||
optional( jo, false, "regions_weighted", regions_weighted );
|
||||
optional( jo, false, "remove_region", remove_region );
|
||||
optional( jo, false, "default_region", default_region );
|
||||
optional( jo, false, "region_point_variance", region_point_variance );
|
||||
}
|
||||
|
||||
+53
-1
@@ -4,11 +4,15 @@
|
||||
#include "cuboid_rectangle.h"
|
||||
#include "point.h"
|
||||
#include "type_id.h"
|
||||
#include "units.h"
|
||||
#include "weighted_list.h"
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
|
||||
class JsonObject;
|
||||
template <typename T> struct enum_traits;
|
||||
@@ -19,7 +23,7 @@ enum class regions_generation_mode : int {
|
||||
UNIFORM = 0, // DYNAMIC; single-region
|
||||
MANUAL_VORONOI = 1, // STATIC; plot region points with JSON, where a voronoi diagram is generated from the points
|
||||
RANDOM = 2, // DYNAMIC; each overmap generated picks a random region from the provided set of regions
|
||||
EIGHTHS = 3, // DYNAMIC; divides world map into 8 cardinal/ordinal areas, each of which has a region
|
||||
ANGLES = 3, // DYNAMIC; divides world map into equal pie-chart-like sectors
|
||||
last = 4
|
||||
};
|
||||
|
||||
@@ -36,6 +40,25 @@ struct region_voronoi_point {
|
||||
void deserialize( const JsonObject &jo );
|
||||
};
|
||||
|
||||
// a set of points accompanied by a set of regions
|
||||
// different generation modes have different usages; not all use `region_points`
|
||||
struct region_point_set {
|
||||
// to-generate region points read from JSON
|
||||
std::unordered_set<tripoint_abs_om> region_points;
|
||||
// providing weights is optional
|
||||
weighted_int_list<region_settings_id> regions_weighted;
|
||||
// if true, removes a region from the weighted list when selected
|
||||
// if `regions` is empty, defaults to `default_region`
|
||||
bool remove_region = false;
|
||||
// fallback for region placement
|
||||
region_settings_id default_region;
|
||||
// maximum distance from specified point
|
||||
int region_point_variance = 0;
|
||||
|
||||
// resolves the point set, mapping regions to points
|
||||
std::vector<region_voronoi_point> resolve() const;
|
||||
void deserialize( const JsonObject &jo );
|
||||
};
|
||||
|
||||
// defines properties of an in-game dimension
|
||||
// its name is solely to distinguish from a generic `dimension`
|
||||
@@ -118,6 +141,35 @@ class dimension_region_layout_generator_uniform : public dimension_region_layout
|
||||
void deserialize( const JsonObject &jo ) override;
|
||||
};
|
||||
|
||||
class dimension_region_layout_generator_manual : public dimension_region_layout_static
|
||||
{
|
||||
std::vector<region_point_set> region_point_sets;
|
||||
public:
|
||||
void generate_entire_layout( Region_map &placed_regions,
|
||||
const tripoint_abs_om ¤t_om ) override;
|
||||
void deserialize( const JsonObject &jo ) override;
|
||||
};
|
||||
|
||||
class dimension_region_layout_generator_random : public dimension_region_layout_dynamic
|
||||
{
|
||||
weighted_int_list<region_settings_id> random_regions;
|
||||
public:
|
||||
void generate_dynamic( Region_map &placed_regions, const tripoint_abs_om ¤t_om ) override;
|
||||
void deserialize( const JsonObject &jo ) override;
|
||||
};
|
||||
|
||||
class dimension_region_layout_generator_angles : public dimension_region_layout_dynamic
|
||||
{
|
||||
// these should be loaded in order
|
||||
std::vector<region_settings_id> angles_regions;
|
||||
units::angle angles_offset = 0_degrees;
|
||||
public:
|
||||
void generate_dynamic( Region_map &placed_regions, const tripoint_abs_om ¤t_om ) override;
|
||||
void deserialize( const JsonObject &jo ) override;
|
||||
int get_sector_count() const {
|
||||
return static_cast<int>( angles_regions.size() );
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
* reads JSON data used to generate a dimension's region map
|
||||
|
||||
+1752
File diff suppressed because it is too large
Load Diff
+382
@@ -0,0 +1,382 @@
|
||||
// Copyright (c) 2019 Mathias Westerdahl
|
||||
// For full LICENSE (MIT) or USAGE, see bottom of file
|
||||
|
||||
#ifndef JC_VORONOI_CLIP_H
|
||||
#define JC_VORONOI_CLIP_H
|
||||
|
||||
#include "jc_voronoi.h"
|
||||
|
||||
#pragma pack(push, 1)
|
||||
|
||||
typedef struct jcv_clipping_polygon_ {
|
||||
jcv_point *points;
|
||||
int num_points;
|
||||
} jcv_clipping_polygon;
|
||||
|
||||
#pragma pack(pop)
|
||||
|
||||
|
||||
// Convex polygon clip functions
|
||||
int jcv_clip_polygon_test_point( const jcv_clipper *clipper, const jcv_point p );
|
||||
int jcv_clip_polygon_clip_edge( const jcv_clipper *clipper, jcv_edge *e );
|
||||
void jcv_clip_polygon_fill_gaps( const jcv_clipper *clipper, jcv_context_internal *allocator,
|
||||
jcv_site *site );
|
||||
|
||||
|
||||
#endif // JC_VORONOI_CLIP_H
|
||||
|
||||
#ifdef JC_VORONOI_CLIP_IMPLEMENTATION
|
||||
#undef JC_VORONOI_CLIP_IMPLEMENTATION
|
||||
|
||||
// These helpers will probably end up in the main library
|
||||
|
||||
static inline jcv_real jcv_cross( const jcv_point a, const jcv_point b )
|
||||
{
|
||||
return a.x * b.y - a.y * b.x;
|
||||
}
|
||||
|
||||
static inline jcv_point jcv_add( jcv_point a, jcv_point b )
|
||||
{
|
||||
jcv_point r;
|
||||
r.x = a.x + b.x;
|
||||
r.y = a.y + b.y;
|
||||
return r;
|
||||
}
|
||||
|
||||
static inline jcv_point jcv_sub( jcv_point a, jcv_point b )
|
||||
{
|
||||
jcv_point r;
|
||||
r.x = a.x - b.x;
|
||||
r.y = a.y - b.y;
|
||||
return r;
|
||||
}
|
||||
|
||||
static inline jcv_point jcv_mul( jcv_point v, jcv_real s )
|
||||
{
|
||||
jcv_point r;
|
||||
r.x = v.x * s;
|
||||
r.y = v.y * s;
|
||||
return r;
|
||||
}
|
||||
|
||||
static inline jcv_point jcv_mix( jcv_point a, jcv_point b, jcv_real t )
|
||||
{
|
||||
jcv_point r;
|
||||
r.x = a.x + ( b.x - a.x ) * t;
|
||||
r.y = a.y + ( b.y - a.y ) * t;
|
||||
return r;
|
||||
}
|
||||
|
||||
static inline jcv_real jcv_dot( jcv_point a, jcv_point b )
|
||||
{
|
||||
return a.x * b.x + a.y * b.y;
|
||||
}
|
||||
|
||||
|
||||
static inline jcv_real jcv_length( jcv_point v )
|
||||
{
|
||||
return JCV_SQRT( v.x * v.x + v.y * v.y );
|
||||
}
|
||||
|
||||
static inline jcv_real jcv_length_sq( jcv_point v )
|
||||
{
|
||||
return v.x * v.x + v.y * v.y;
|
||||
}
|
||||
|
||||
static inline jcv_real jcv_fabs( jcv_real a )
|
||||
{
|
||||
return a < 0 ? -a : a;
|
||||
}
|
||||
|
||||
// if it returns [0.0, 1.0] it's on the line segment
|
||||
static inline jcv_real jcv_point_to_line_segment_t( jcv_point p, jcv_point p0, jcv_point p1 )
|
||||
{
|
||||
jcv_point vpoint = jcv_sub( p, p0 );
|
||||
jcv_point vsegment = jcv_sub( p1, p0 );
|
||||
return jcv_dot( vsegment, vpoint ) / jcv_dot( vsegment, vsegment );
|
||||
}
|
||||
|
||||
int jcv_clip_polygon_test_point( const jcv_clipper *clipper, const jcv_point p )
|
||||
{
|
||||
jcv_clipping_polygon *polygon = ( jcv_clipping_polygon * )clipper->ctx;
|
||||
int num_points = polygon->num_points;
|
||||
|
||||
// convex polygon
|
||||
// winding CCW
|
||||
// all polygon normals point outward
|
||||
// if the point is in front of the plane, it is outside
|
||||
|
||||
int result = 1;
|
||||
for( int i = 0; i < num_points; ++i ) {
|
||||
jcv_point p0 = polygon->points[i];
|
||||
jcv_point p1 = polygon->points[( i + 1 ) % num_points];
|
||||
jcv_point n;
|
||||
n.x = p1.y - p0.y;
|
||||
n.y = p0.x - p1.x;
|
||||
jcv_point diff;
|
||||
diff.x = p.x - p0.x;
|
||||
diff.y = p.y - p0.y;
|
||||
|
||||
if( jcv_dot( n, diff ) > 0 ) {
|
||||
result = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static int jcv_ray_intersect_polygon( const jcv_clipper *clipper, jcv_point p0, jcv_point p1,
|
||||
jcv_real *out_t0, jcv_real *out_t1 )
|
||||
{
|
||||
jcv_clipping_polygon *polygon = ( jcv_clipping_polygon * )clipper->ctx;
|
||||
int num_points = polygon->num_points;
|
||||
|
||||
jcv_real t0 = ( jcv_real )0;
|
||||
jcv_real t1 = ( jcv_real )1;
|
||||
jcv_point dir = jcv_sub( p1, p0 );
|
||||
|
||||
for( int i = 0; i < num_points; ++i ) {
|
||||
jcv_point v0 = polygon->points[i];
|
||||
jcv_point v1 = polygon->points[( i + 1 ) % num_points];
|
||||
jcv_point n;
|
||||
n.x = v1.y - v0.y;
|
||||
n.y = -( v1.x - v0.x );
|
||||
|
||||
jcv_point v0p0 = jcv_sub( p0, v0 );
|
||||
|
||||
jcv_real N = -jcv_dot( v0p0, n );
|
||||
jcv_real D = jcv_dot( dir, n );
|
||||
if( jcv_fabs( D ) < 0.0001f ) { // parallel to the line
|
||||
if( N < 0 ) {
|
||||
return 0;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
jcv_real t = N / D;
|
||||
if( D < 0 ) { // -> entering
|
||||
t0 = t > t0 ? t : t0;
|
||||
if( t0 > t1 ) {
|
||||
return 0;
|
||||
}
|
||||
} else { // D > 0 -> exiting
|
||||
t1 = t < t1 ? t : t1;
|
||||
if( t1 < t0 ) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
*out_t0 = t0;
|
||||
*out_t1 = t1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int jcv_clip_polygon_clip_edge( const jcv_clipper *clipper, jcv_edge *e )
|
||||
{
|
||||
// Using the box clipper to get a finite line segment
|
||||
int result = jcv_boxshape_clip( clipper, e );
|
||||
if( !result ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
jcv_point p0 = e->pos[0];
|
||||
jcv_point p1 = e->pos[1];
|
||||
|
||||
jcv_real t0;
|
||||
jcv_real t1;
|
||||
result = jcv_ray_intersect_polygon( clipper, p0, p1, &t0, &t1 );
|
||||
|
||||
if( !result ) {
|
||||
e->pos[0] = e->pos[1];
|
||||
return 0;
|
||||
}
|
||||
|
||||
e->pos[0] = jcv_mix( p0, p1, t0 );
|
||||
e->pos[1] = jcv_mix( p0, p1, t1 );
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Find the edge which the point sits on
|
||||
static int jcv_find_polygon_edge( const jcv_clipper *clipper, jcv_point p )
|
||||
{
|
||||
jcv_clipping_polygon *polygon = ( jcv_clipping_polygon * )clipper->ctx;
|
||||
|
||||
int min_edge = -1;
|
||||
jcv_real min_dist = JCV_FLT_MAX;
|
||||
int num_points = polygon->num_points;
|
||||
for( int i = 0; i < num_points; ++i ) {
|
||||
jcv_point p0 = polygon->points[i];
|
||||
if( jcv_point_eq( &p, &p0 ) ) {
|
||||
return i;
|
||||
}
|
||||
|
||||
jcv_point p1 = polygon->points[( i + 1 ) % num_points];
|
||||
jcv_point vsegment = jcv_sub( p1, p0 );
|
||||
jcv_point vpoint = jcv_sub( p, p0 );
|
||||
|
||||
jcv_real t = jcv_dot( vsegment, vpoint ) / jcv_dot( vsegment, vsegment );
|
||||
|
||||
if( t < ( jcv_real )0.0f || t > ( jcv_real )1.0f ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
jcv_point projected = jcv_add( p0, jcv_mul( vsegment, t ) );
|
||||
jcv_real distsq = jcv_length_sq( jcv_sub( p, projected ) );
|
||||
|
||||
if( distsq < min_dist ) {
|
||||
min_dist = distsq;
|
||||
min_edge = i;
|
||||
}
|
||||
}
|
||||
assert( min_edge >= 0 );
|
||||
return min_edge;
|
||||
}
|
||||
|
||||
void jcv_clip_polygon_fill_gaps( const jcv_clipper *clipper, jcv_context_internal *allocator,
|
||||
jcv_site *site )
|
||||
{
|
||||
// They're sorted CCW, so if the current->pos[1] != next->pos[0], then we have a gap
|
||||
jcv_clipping_polygon *polygon = ( jcv_clipping_polygon * )clipper->ctx;
|
||||
int num_points = polygon->num_points;
|
||||
|
||||
jcv_graphedge *current = site->edges;
|
||||
if( !current ) {
|
||||
jcv_graphedge *gap = jcv_alloc_graphedge( allocator );
|
||||
gap->neighbor = 0;
|
||||
// Pick the first edge of the polygon (which is also CCW)
|
||||
gap->pos[0] = polygon->points[0];
|
||||
gap->pos[1] = polygon->points[1];
|
||||
gap->angle = jcv_calc_sort_metric( site, gap );
|
||||
gap->next = 0;
|
||||
gap->edge = jcv_create_gap_edge( allocator, site, gap );
|
||||
|
||||
current = gap;
|
||||
site->edges = gap;
|
||||
}
|
||||
|
||||
jcv_graphedge *next = current->next;
|
||||
if( !next ) {
|
||||
jcv_graphedge *gap = jcv_alloc_graphedge( allocator );
|
||||
|
||||
int polygon_edge = jcv_find_polygon_edge( clipper, current->pos[1] );
|
||||
if( !jcv_point_eq( ¤t->pos[1], &polygon->points[( polygon_edge + 1 ) % num_points] ) ) {
|
||||
gap->pos[0] = current->pos[1];
|
||||
gap->pos[1] = polygon->points[( polygon_edge + 1 ) % num_points];
|
||||
} else {
|
||||
gap->pos[0] = polygon->points[( polygon_edge + 1 ) % num_points];
|
||||
gap->pos[1] = polygon->points[( polygon_edge + 2 ) % num_points];
|
||||
}
|
||||
|
||||
gap->neighbor = 0;
|
||||
gap->angle = jcv_calc_sort_metric( site, gap );
|
||||
gap->next = 0;
|
||||
gap->edge = jcv_create_gap_edge( allocator, site, gap );
|
||||
|
||||
gap->next = current->next;
|
||||
current->next = gap;
|
||||
current = gap;
|
||||
next = site->edges;
|
||||
}
|
||||
|
||||
while( current && next ) {
|
||||
if( !jcv_point_eq( ¤t->pos[1], &next->pos[0] ) ) {
|
||||
int polygon_edge1 = jcv_find_polygon_edge( clipper, current->pos[1] );
|
||||
int polygon_edge2 = jcv_find_polygon_edge( clipper, next->pos[0] );
|
||||
|
||||
jcv_graphedge *gap = jcv_alloc_graphedge( allocator );
|
||||
gap->pos[0] = current->pos[1];
|
||||
|
||||
if( polygon_edge1 != polygon_edge2 ) {
|
||||
gap->pos[1] = polygon->points[( polygon_edge1 + 1 ) % num_points];
|
||||
} else {
|
||||
gap->pos[1] = next->pos[0];
|
||||
}
|
||||
|
||||
gap->neighbor = 0;
|
||||
gap->angle = jcv_calc_sort_metric( site, gap );
|
||||
gap->edge = jcv_create_gap_edge( allocator, site, gap );
|
||||
gap->next = current->next;
|
||||
current->next = gap;
|
||||
}
|
||||
|
||||
current = current->next;
|
||||
if( current ) {
|
||||
next = current->next;
|
||||
if( !next ) {
|
||||
next = site->edges;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif // JC_VORONOI_CLIP_IMPLEMENTATION
|
||||
|
||||
|
||||
/*
|
||||
|
||||
ABOUT:
|
||||
|
||||
Helper functions for clipping a vosonoi diagram against a convex polygon
|
||||
|
||||
|
||||
LICENSE:
|
||||
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2019 Mathias Westerdahl
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
|
||||
DISCLAIMER:
|
||||
|
||||
This software is supplied "AS IS" without any warranties and support
|
||||
|
||||
USAGE:
|
||||
|
||||
USAGE:
|
||||
|
||||
The function `jcv_clipper` struct allows for supplying a set of custom clipper functions to interact with the generating of the resulting diagram.
|
||||
|
||||
#define JC_VORONOI_CLIP_IMPLEMENTATION
|
||||
#include "jc_voronoi_clip.h"
|
||||
|
||||
jcv_clipping_polygon polygon;
|
||||
// Triangle
|
||||
polygon.num_points = 3;
|
||||
polygon.points = (jcv_point*)malloc(sizeof(jcv_point)*(size_t)polygon.num_points);
|
||||
|
||||
polygon.points[0].x = width/2;
|
||||
polygon.points[1].x = width - width/5;
|
||||
polygon.points[2].x = width/5;
|
||||
polygon.points[0].y = height/5;
|
||||
polygon.points[1].y = height - height/5;
|
||||
polygon.points[2].y = height - height/5;
|
||||
|
||||
jcv_clipper polygonclipper;
|
||||
polygonclipper.test_fn = jcv_clip_polygon_test_point;
|
||||
polygonclipper.clip_fn = jcv_clip_polygon_clip_edge;
|
||||
polygonclipper.fill_fn = jcv_clip_polygon_fill_gaps;
|
||||
polygonclipper.ctx = &polygon;
|
||||
|
||||
jcv_diagram diagram;
|
||||
memset(&diagram, 0, sizeof(jcv_diagram));
|
||||
jcv_diagram_generate(count, (const jcv_point*)points, 0, clipper, &diagram);
|
||||
*/
|
||||
Reference in New Issue
Block a user