载入中...
搜索中...
未找到
VesselGenerator.cpp
浏览该文件的文档.
2#include <algorithm>
3#include <cmath>
4#include <cstdint>
5namespace eve::procgen {
6namespace {
7double cross(VesselSectionPoint a, VesselSectionPoint b, VesselSectionPoint c) {
8 return (double(b.radius) - a.radius) * (double(c.height) - a.height) -
9 (double(b.height) - a.height) * (double(c.radius) - a.radius);
10}
11bool intersects(VesselSectionPoint a, VesselSectionPoint b, VesselSectionPoint c, VesselSectionPoint d) {
12 if (std::max(a.radius, b.radius) < std::min(c.radius, d.radius) ||
13 std::max(c.radius, d.radius) < std::min(a.radius, b.radius) ||
14 std::max(a.height, b.height) < std::min(c.height, d.height) ||
15 std::max(c.height, d.height) < std::min(a.height, b.height))
16 return false;
17 return cross(a, b, c) * cross(a, b, d) <= 0. && cross(c, d, a) * cross(c, d, b) <= 0.;
18}
19} // namespace
20
21eve::Result<MeshBuild> revolveVesselSection(const std::vector<VesselSectionPoint>& section, int radialSegments) {
22 auto invalid = [] {
25 "VesselSectionInvalid: Expected a finite simple counterclockwise solid section and "
26 "3..512 radial segments"));
27 };
28 const auto count = section.size();
29 if (count < 3 || count > 1024 || radialSegments < 3 || radialSegments > 512) return invalid();
30 if (section.front().radius != 0.f || section.back().radius != 0.f) return invalid();
31 double area = 0.;
32 for (size_t i = 0; i < count; ++i) {
33 const auto a = section[i], b = section[(i + 1) % count];
34 if (!std::isfinite(a.radius) || !std::isfinite(a.height) || a.radius < 0.f || std::abs(a.height) > 10000.f ||
35 a.radius > 10000.f || (i > 0 && i + 1 < count && a.radius < 0.00001f))
36 return invalid();
37 if (std::hypot(double(b.radius) - a.radius, double(b.height) - a.height) < 0.00001) return invalid();
38 area += double(a.radius) * b.height - double(b.radius) * a.height;
39 for (size_t j = i + 2; j < count; ++j) {
40 if (i == 0 && j + 1 == count) continue;
41 if (intersects(a, b, section[j], section[(j + 1) % count])) return invalid();
42 }
43 }
44 if (area <= 0.00000001) return invalid();
46 mesh.reserve(int((count - 1) * 2 * (radialSegments + 1)), int((count - 1) * radialSegments * 6));
47 mesh.setActiveGroup("ceramic");
48 double total = 0.;
49 for (size_t i = 1; i < count; ++i)
50 total += std::hypot(double(section[i].radius) - section[i - 1].radius,
51 double(section[i].height) - section[i - 1].height);
52 double distance = 0.;
53 for (size_t i = 1; i < count; ++i) {
54 const auto a = section[i - 1], b = section[i];
55 const double dr = double(b.radius) - a.radius, dy = double(b.height) - a.height;
56 const double length = std::hypot(dr, dy);
57 const auto base = static_cast<uint32_t>(mesh.getVertexCount());
58 // Blend nearby section tangents; retain creases at the base and sharp shoulders.
59 double nr[2] = {dy / length, dy / length};
60 double ny[2] = {-dr / length, -dr / length};
61 for (int end = 0; end < 2; ++end) {
62 const size_t point = i - 1 + size_t(end);
63 if (point == 0 || point + 1 == count) continue;
64 const auto c = end == 0 ? section[point - 1] : section[point];
65 const auto d = end == 0 ? section[point] : section[point + 1];
66 const double cr = double(d.radius) - c.radius, cy = double(d.height) - c.height;
67 const double cl = std::hypot(cr, cy);
68 if ((dr * cr + dy * cy) / (length * cl) > 0.8) {
69 const double x = nr[end] + cy / cl, y = ny[end] - cr / cl;
70 const double norm = std::hypot(x, y);
71 nr[end] = x / norm;
72 ny[end] = y / norm;
73 }
74 }
75 for (int j = 0; j <= radialSegments; ++j) {
76 const double angle = j == radialSegments ? 0. : 6.283185307179586 * j / radialSegments;
77 const float c = float(std::cos(angle)), s = float(std::sin(angle));
78 for (int end = 0; end < 2; ++end) {
79 const auto p = end == 0 ? a : b;
80 mesh.addVertex(p.radius * c, p.height, p.radius * s, float(nr[end]) * c, float(ny[end]),
81 float(nr[end]) * s, float(j) / radialSegments, float((distance + end * length) / total));
82 }
83 }
84 for (int j = 0; j < radialSegments; ++j) {
85 const auto k = base + uint32_t(j * 2);
86 if (a.radius > 0.f) mesh.addTriangle(k, k + 1, k + 2);
87 if (b.radius > 0.f) mesh.addTriangle(k + 1, k + 3, k + 2);
88 }
90 }
91 mesh.setMeta("generator", "mesh.vessel");
92 mesh.setMeta("axis", "Y");
93 return eve::Result<MeshBuild>::success(std::move(mesh));
94}
95} // namespace eve::procgen
double y
double x
const std::string & s
float cy
Definition CardTypes.cpp:37
glm::vec4 p[6]
float area
Definition Grass.cpp:62
std::int32_t c
std::uint32_t height
float length
MeleePoint3 b
Definition MeleeHit.cpp:41
double radius
Definition MeleeHit.cpp:42
MeleePoint3 a
Definition MeleeHit.cpp:40
float distance
OceanMeshSettings mesh
float d
size_t end
float dy
Definition Shadow.cpp:151
int count
glm::vec3 point
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
CPU triangle mesh from procedural mesh recipes (e.g. marching cubes). Positions/normals are xyz-packe...
Definition MeshBuild.h:20
void reserve(int vertexCount, int indexCount)
Reserve.
Definition MeshBuild.cpp:22
Result< int > invalid(std::string message)
Invalid.
V3 cross(V3 a, V3 b)
Module-private road bake implementation helper.
Definition RoadBake.cpp:29
eve::Result< MeshBuild > revolveVesselSection(const std::vector< VesselSectionPoint > &section, int radialSegments)
Revolve a solid section: underside axis, outside, rim, inside, floor axis.