载入中...
搜索中...
未找到
RoadSampling.cpp
浏览该文件的文档.
1#include <algorithm>
2#include <cmath>
4
6namespace {
7double frameTurn(const SplineFrameSample& a, const SplineFrameSample& b) {
8 auto angle = [](double ax, double ay, double az, double bx, double by, double bz) {
9 const double norm = std::hypot(ax, ay, az) * std::hypot(bx, by, bz);
10 return std::acos(std::clamp((ax * bx + ay * by + az * bz) / norm, -1., 1.)) * 57.29577951308232;
11 };
12 return std::max({angle(a.forwardX, a.forwardY, a.forwardZ, b.forwardX, b.forwardY, b.forwardZ),
13 angle(a.sideX, a.sideY, a.sideZ, b.sideX, b.sideY, b.sideZ),
14 angle(a.upX, a.upY, a.upZ, b.upX, b.upY, b.upZ)});
15}
16} // namespace
18 const RoadBakeOptions& options, float length,
19 std::size_t& samplingWork) {
20 const auto& style = edge.style;
21 if (style.samplingSpacingMeters == 0.f)
22 return path.sampleFramesResult(std::max(4, options.pathSegmentsPerEdge), true, 0.f, 24);
23 if (!std::isfinite(length) || length <= 1e-7f)
24 return bakeFail<std::vector<SplineFrameSample>>(DiagnosticCode::InvalidArgument,
25 "metric road length must be finite and positive", "path");
26 if (style.samplingPolicy == RoadSamplingPolicy::ParametricDistanceTurn)
27 return sampleRoadParametricFrames(path, edge, options, samplingWork);
28 constexpr int maximum = 4096;
29 const double spacing = static_cast<double>(style.samplingSpacingMeters) * options.samplingScale;
30 const double required = std::ceil(static_cast<double>(length) / spacing);
31 if (required > maximum || path.segmentCount() > maximum / 4)
32 return bakeFail<std::vector<SplineFrameSample>>(DiagnosticCode::PreconditionViolation,
33 "metric road sampling exceeds the 4096 interval budget",
34 "style.samplingSpacingMeters");
35 int count = std::max({4, static_cast<int>(required), path.segmentCount() * 4});
36 const double target = std::min(180., static_cast<double>(style.samplingAngleDegrees) * options.samplingScale);
37 auto frames = path.sampleFramesResult(count, true, 0.f, 24, true);
38 if (!frames.ok()) return frames;
39 if (target == 0.) return frames;
40 for (;;) {
41 double largest = 0.;
42 for (std::size_t i = 1; i < frames.value().size(); ++i)
43 largest = std::max(largest, frameTurn(frames.value()[i - 1], frames.value()[i]));
44 if (largest <= target * .5) break;
45 if (count == maximum)
46 return bakeFail<std::vector<SplineFrameSample>>(
48 "metric road angle refinement exceeds the 4096 interval budget; remaining turn=" +
49 std::to_string(largest),
50 "style.samplingAngleDegrees");
51 count = std::min(maximum, count * 2);
52 frames = path.sampleFramesResult(count, true, 0.f, 24, true);
53 if (!frames.ok()) return frames;
54 }
55 std::vector<SplineFrameSample> selected;
56 selected.reserve(frames.value().size());
57 selected.push_back(frames.value().front());
58 std::size_t anchor = 0;
59 double turn = 0.;
60 for (std::size_t i = 1; i < frames.value().size(); ++i) {
61 const double step = frameTurn(frames.value()[i - 1], frames.value()[i]);
62 const double distance =
63 (frames.value()[i].sample.normalizedDistance - frames.value()[anchor].sample.normalizedDistance) * length;
64 if (i > anchor + 1 && (distance > spacing * (1. + 1e-5) || turn + step > target)) {
65 selected.push_back(frames.value()[i - 1]);
66 anchor = i - 1;
67 turn = 0.;
68 }
69 if (frames.value()[i].authoredMarker) {
70 selected.push_back(frames.value()[i]);
71 anchor = i;
72 turn = 0.;
73 } else
74 turn += step;
75 }
76 if (selected.back().sample.normalizedDistance != frames.value().back().sample.normalizedDistance)
77 selected.push_back(frames.value().back());
78 return Result<std::vector<SplineFrameSample>>::success(std::move(selected));
79}
81 const RoadEdge& edge, const RoadBakeOptions& options,
82 float length) {
83 if (const auto found = cache.frames.find(edge.id); found != cache.frames.end())
84 return ResultRef<const std::vector<SplineFrameSample>>::success(std::cref(found->second));
85 auto prepared = sampleRoadFrames(path, edge, options, length, cache.samplingWork);
86 if (!prepared.ok()) return ResultRef<const std::vector<SplineFrameSample>>::failure(prepared.status());
87 auto inserted = cache.frames.emplace(edge.id, std::move(prepared).takeValue());
88 return ResultRef<const std::vector<SplineFrameSample>>::success(std::cref(inserted.first->second));
89}
90} // namespace eve::procgen::road::detail
LogicalId target
std::string selected
float maximum[3]
float length
bool required
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
float distance
std::string path
Definition PlayHost.cpp:111
bool found
const SquirrelValueOptions & options
int count
TacticalUnit::TurnResources turn
int spacing
float step
Definition TreeMesh.cpp:451
float size
Definition TreeMesh.cpp:158
Move-only operation result carrying either a value or Status.
Definition Result.h:176
Owning multi-segment 3D spline with deterministic bounded sampling.
Definition SplinePath.h:118
Result< std::vector< SplineFrameSample > > sampleRoadFrames(const SplinePath &path, const RoadEdge &edge, const RoadBakeOptions &options, float length, std::size_t &samplingWork)
Module-private bounded sampling; zero metric spacing retains explicit fixed interval counts.
ResultRef< const std::vector< SplineFrameSample > > prepareRoadFrames(RoadFrameCache &cache, const SplinePath &path, const RoadEdge &edge, const RoadBakeOptions &options, float length)
Prepare once per immutable edge/options snapshot. Borrowed result expires with cache destruction....
Result< std::vector< SplineFrameSample > > sampleRoadParametricFrames(const SplinePath &path, const RoadEdge &edge, const RoadBakeOptions &options, std::size_t &samplingWork)
Module-private bounded parameter scan; pose construction remains owned by SplinePath.
Options for baking a road network into mesh + navigation overlays.
Definition RoadBake.h:50
Directed centerline edge with optional reverse lanes.
Definition RoadTypes.h:286
std::unordered_map< std::uint32_t, std::vector< SplineFrameSample > > frames