| | | 1 | | //======================================================================= |
| | | 2 | | // GridPlane2d.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024–present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using FixedMathSharp; |
| | | 9 | | using System.Runtime.CompilerServices; |
| | | 10 | | |
| | | 11 | | namespace GridForge.Spatial; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Provides GridForge's 2D XZ-plane projection helpers. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <remarks> |
| | | 17 | | /// In GridForge 2D query APIs, <see cref="Vector2d.X"/> maps to world X, |
| | | 18 | | /// <see cref="Vector2d.Y"/> maps to world Z, and the supplied layer maps to world Y. |
| | | 19 | | /// </remarks> |
| | | 20 | | public static class GridPlane2d |
| | | 21 | | { |
| | | 22 | | /// <summary> |
| | | 23 | | /// Converts an XZ-plane 2D position into a world-space 3D position. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="position">The 2D position whose X component maps to world X and Y component maps to world Z.</param |
| | | 26 | | /// <param name="layerY">The world Y layer to apply. Defaults to zero.</param> |
| | | 27 | | /// <returns>The equivalent world-space position.</returns> |
| | | 28 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 29 | | public static Vector3d ToWorld(Vector2d position, Fixed64 layerY = default) |
| | | 30 | | { |
| | 356 | 31 | | return new Vector3d(position.X, layerY, position.Y); |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Projects a world-space 3D position into GridForge's 2D XZ plane. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <param name="position">The world-space position to project.</param> |
| | | 38 | | /// <returns>A 2D position containing world X and world Z.</returns> |
| | | 39 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 40 | | public static Vector2d FromWorld(Vector3d position) |
| | | 41 | | { |
| | 1 | 42 | | return new Vector2d(position.X, position.Z); |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Converts XZ-plane 2D bounds into layer-locked world-space 3D bounds. |
| | | 47 | | /// </summary> |
| | | 48 | | /// <param name="min">The 2D minimum bound whose X component maps to world X and Y component maps to world Z.</param |
| | | 49 | | /// <param name="max">The 2D maximum bound whose X component maps to world X and Y component maps to world Z.</param |
| | | 50 | | /// <param name="layerY">The world Y layer to apply to both bounds. Defaults to zero.</param> |
| | | 51 | | /// <returns>The equivalent layer-locked world-space bounds.</returns> |
| | | 52 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 53 | | public static (Vector3d min, Vector3d max) ToWorldBounds( |
| | | 54 | | Vector2d min, |
| | | 55 | | Vector2d max, |
| | | 56 | | Fixed64 layerY = default) |
| | | 57 | | { |
| | 14 | 58 | | return (ToWorld(min, layerY), ToWorld(max, layerY)); |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 62 | | internal static Fixed64 DistanceSquaredXZ(Vector3d a, Vector3d b) |
| | | 63 | | { |
| | 16454 | 64 | | Fixed64 deltaX = a.X - b.X; |
| | 16454 | 65 | | Fixed64 deltaZ = a.Z - b.Z; |
| | 16454 | 66 | | return (deltaX * deltaX) + (deltaZ * deltaZ); |
| | | 67 | | } |
| | | 68 | | } |