114 lines
4.7 KiB
C++
114 lines
4.7 KiB
C++
#pragma once
|
|
|
|
#include "map_camera.hpp"
|
|
#include "settlement/core/voxel_region.hpp"
|
|
#include <Void/Core/Uuid.hpp>
|
|
#include <Void/Math/Vec.hpp>
|
|
#include <expected>
|
|
#include <memory>
|
|
#include <span>
|
|
#include <vector>
|
|
|
|
namespace settlement::viewer {
|
|
|
|
using VoxelPoint = Void::Math::Vec3<double>;
|
|
|
|
struct VoxelCamera {
|
|
int width = 0, height = 0;
|
|
// Screen position of the lower corner of the prepared display bounds.
|
|
double origin_x = 0, origin_y = 0;
|
|
double scale = 1;
|
|
double tile_half_width = 12, tile_half_height = 7;
|
|
};
|
|
struct VoxelSceneLimits {
|
|
std::size_t max_cells = 2 * 1024 * 1024;
|
|
std::size_t max_faces = 262144;
|
|
std::size_t max_read_chunks = 1024;
|
|
// Faces, read directory capacity and expanded CPU vertices. Shared source
|
|
// pages, allocator overhead, retained older scenes and GPU memory are separate.
|
|
std::size_t max_bytes = 64 * 1024 * 1024;
|
|
std::int64_t max_axis = 4096;
|
|
};
|
|
struct VoxelSceneOptions {
|
|
// An explicit display volume bounds work even in an enormous sparse region.
|
|
VoxelBounds bounds;
|
|
std::int32_t slice = 0; // Highest displayed cell, inclusive.
|
|
VoxelSceneLimits limits;
|
|
};
|
|
enum class VoxelSceneError {
|
|
InvalidInput, LimitExceeded, AllocationFailed
|
|
};
|
|
struct VoxelFace {
|
|
VoxelPosition cell;
|
|
VoxelPosition normal;
|
|
MaterialId material = MaterialId::Air;
|
|
bool unavailable = false;
|
|
bool operator==(const VoxelFace&) const = default;
|
|
};
|
|
|
|
class VoxelScene {
|
|
public:
|
|
[[nodiscard]] const VoxelRegion& region() const noexcept { return region_; }
|
|
[[nodiscard]] const Void::Uuid::Uuid& session() const noexcept { return session_; }
|
|
[[nodiscard]] const VoxelSceneOptions& options() const noexcept { return options_; }
|
|
[[nodiscard]] const VoxelBounds& visible_bounds() const noexcept { return visible_; }
|
|
[[nodiscard]] std::span<const VoxelFace> faces() const noexcept { return faces_; }
|
|
[[nodiscard]] std::span<const ChunkStamp> reads() const noexcept { return reads_; }
|
|
[[nodiscard]] bool current(const VoxelRegion& region, const Void::Uuid::Uuid& session) const noexcept;
|
|
private:
|
|
friend std::expected<std::shared_ptr<const VoxelScene>, VoxelSceneError>
|
|
prepare_voxel_scene(const VoxelRegion&, const Void::Uuid::Uuid&, VoxelSceneOptions);
|
|
VoxelScene(VoxelRegion region, Void::Uuid::Uuid session, VoxelSceneOptions options, VoxelBounds visible);
|
|
VoxelRegion region_;
|
|
Void::Uuid::Uuid session_;
|
|
VoxelSceneOptions options_;
|
|
VoxelBounds visible_;
|
|
std::vector<VoxelFace> faces_;
|
|
std::vector<ChunkStamp> reads_;
|
|
};
|
|
|
|
// Missing cells are opaque gray terrain boundaries. Picks stop there with
|
|
// Unavailable so loaded geometry cannot be selected through unknown space.
|
|
[[nodiscard]] std::expected<std::shared_ptr<const VoxelScene>, VoxelSceneError>
|
|
prepare_voxel_scene(const VoxelRegion& region, const Void::Uuid::Uuid& session, VoxelSceneOptions options);
|
|
[[nodiscard]] bool valid_voxel_camera(const VoxelCamera& camera) noexcept;
|
|
[[nodiscard]] std::optional<ScreenPoint> project_voxel_point(
|
|
const VoxelScene& scene, const VoxelCamera& camera, VoxelPoint point) noexcept;
|
|
[[nodiscard]] std::uint32_t voxel_face_color(const VoxelFace& face) noexcept;
|
|
|
|
struct VoxelRay {
|
|
VoxelPoint origin;
|
|
VoxelPoint direction;
|
|
double max_distance = 0;
|
|
};
|
|
struct VoxelPickLimits {
|
|
std::size_t max_steps = 16384;
|
|
std::size_t max_read_chunks = 1024;
|
|
};
|
|
enum class VoxelPickStatus { Miss, Hit, Inside, Unavailable };
|
|
struct VoxelPick {
|
|
RegionId region = 0;
|
|
Void::Uuid::Uuid session;
|
|
VoxelPickStatus status = VoxelPickStatus::Miss;
|
|
VoxelPosition cell;
|
|
VoxelPosition normal; // Zero for an initial occupied cell with no crossed face.
|
|
std::optional<VoxelPosition> adjacent; // May be outside the region. Validate before placement.
|
|
MaterialId material = MaterialId::Air;
|
|
double distance = 0;
|
|
std::vector<ChunkStamp> reads;
|
|
[[nodiscard]] bool current(const VoxelRegion& world, const Void::Uuid::Uuid& current_session) const noexcept;
|
|
};
|
|
|
|
// Cells are [x,x+1). At an exact plane the direction selects the forward cell.
|
|
// A zero component stays in the half-open cell owning that coordinate.
|
|
// Simultaneous plane crossings advance all tied axes, selecting normal X/Y/Z
|
|
// in that priority order. Cells touched only along an edge are skipped.
|
|
[[nodiscard]] std::expected<VoxelPick, VoxelSceneError> trace_voxels(
|
|
const VoxelScene& scene, VoxelRay ray, VoxelPickLimits limits = {});
|
|
[[nodiscard]] std::expected<VoxelRay, VoxelSceneError> voxel_screen_ray(
|
|
const VoxelScene& scene, const VoxelCamera& camera, ScreenPoint point);
|
|
[[nodiscard]] std::expected<VoxelPick, VoxelSceneError> pick_voxel_screen(
|
|
const VoxelScene& scene, const VoxelCamera& camera, ScreenPoint point, VoxelPickLimits limits = {});
|
|
|
|
} // namespace settlement::viewer
|