29 float asphaltHalf = 0.f;
30 float curbWidth = 0.f;
31 float curbHeight = 0.f;
32 float sidewalkW = 0.f;
33 float sidewalkH = 0.f;
36 std::vector<ArmTip> arms;
37 float maxAsphalt = 0.f;
38 float maxCurbWidth = 0.f;
39 float maxCurbHeight = 0.f;
40 float maxSidewalkWidth = 0.f;
41 float maxSidewalkHeight = 0.f;
43 for (
const auto&
edge : network.
edges()) {
46 if (!
spline.ok())
continue;
47 auto len =
spline.value().lengthResult(24);
48 if (!len.ok())
continue;
50 const float d =
edge.to ==
node.id ? len.value() - trim : trim;
51 auto frame =
spline.value().travelFrameResult(
d,
"clamp", 24);
52 if (!
frame.ok())
continue;
55 if (!profile.ok())
continue;
57 const auto&
f =
frame.value();
59 tip.origin =
V3{
f.sample.
x,
f.sample.y,
f.sample.z};
61 V3 tangentOut{
f.forwardX,
f.forwardY,
f.forwardZ};
62 if (
edge.to ==
node.id) tangentOut = tangentOut * -1.f;
64 V3{tangentOut.
x, junctionPlane.
gradeX * tangentOut.x + junctionPlane.
gradeZ * tangentOut.z, tangentOut.z});
66 const V3 canonicalSide{-outward.
z, 0.f, outward.
x};
71 if (
dot(
tip.side, canonicalSide) < 0.f)
tip.side =
tip.side * -1.f;
73 if (
tip.up.y < 0.f)
tip.up =
tip.up * -1.f;
74 tip.asphaltHalf = asphaltHalf *
f.sample.scaleX;
75 const bool plainCustom = profile.value().explicitMaterials && profile.value().points.size() == 2u;
76 tip.curbWidth = plainCustom ? 0.f :
edge.style.curbWidth;
77 tip.curbHeight = plainCustom ? 0.f :
edge.style.curbHeight;
78 tip.sidewalkW = plainCustom ? 0.f :
edge.style.sidewalkWidth;
79 tip.sidewalkH = plainCustom ? 0.f :
edge.style.sidewalkHeight;
80 tip.uvMeters =
edge.style.uvMeters;
82 maxAsphalt = std::max(maxAsphalt,
tip.asphaltHalf);
83 maxCurbWidth = std::max(maxCurbWidth,
tip.curbWidth);
84 maxCurbHeight = std::max(maxCurbHeight,
tip.curbHeight);
85 maxSidewalkWidth = std::max(maxSidewalkWidth,
tip.sidewalkW);
86 maxSidewalkHeight = std::max(maxSidewalkHeight,
tip.sidewalkH);
90 float jr =
node.junctionRadius;
95 bool axisCross = arms.size() == 4;
98 for (
const auto&
tip : arms) {
99 const float dx = std::fabs(
tip.origin.x -
node.x);
100 const float dz = std::fabs(
tip.origin.z -
node.z);
101 if ((dx < 0.35f && dz > jr * 0.4f) || (dz < 0.35f && dx > jr * 0.4f)) ++axisHits;
103 const auto&
first = arms.front();
104 auto same = [](
float a,
float b) {
return std::fabs(
a -
b) < 1e-4f; };
105 axisCross = axisHits == 4 && std::all_of(arms.begin(), arms.end(), [&](
const ArmTip&
tip) {
106 return same(tip.asphaltHalf, first.asphaltHalf) && same(tip.curbWidth, first.curbWidth) &&
107 same(tip.curbHeight, first.curbHeight) && same(tip.sidewalkW, first.sidewalkW) &&
108 same(tip.sidewalkH, first.sidewalkH);
117 const float ah = maxAsphalt;
118 const float cornerR = std::max(0.75f, jr - ah);
120 const V3 upN = junctionPlane.
up;
121 auto surfacePoint = [&](
float x,
float z,
float offset = 0.f) {
127 std::vector<std::pair<float, float>> rim;
128 rim.reserve(
static_cast<std::size_t
>(4 * (segs + 3)));
129 auto pushPt = [&](
float x,
float z) {
130 if (!rim.empty() && std::fabs(rim.back().first -
x) < 1e-4f && std::fabs(rim.back().second -
z) < 1e-4f)
132 rim.emplace_back(
x,
z);
134 auto pushCornerArc = [&](
int sx,
int sz,
bool reverse) {
135 const float sxf =
static_cast<float>(
sx);
136 const float szf =
static_cast<float>(
sz);
137 const float cx =
node.
x + sxf * jr;
138 const float cz =
node.z + szf * jr;
139 for (
int i = 0; i <= segs; ++i) {
140 const int ii = reverse ? (segs - i) : i;
141 const float t =
static_cast<float>(ii) /
static_cast<float>(segs);
142 const float th =
t * 1.5707963f;
143 pushPt(
cx - sxf * cornerR * std::sin(th), cz - szf * cornerR * std::cos(th));
148 pushCornerArc(+1, +1,
false);
150 pushCornerArc(-1, +1,
true);
152 pushCornerArc(-1, -1,
false);
154 pushCornerArc(+1, -1,
true);
157 for (std::size_t i = 0; i < rim.size(); ++i) {
158 const auto& p0 = rim[i];
159 const auto& p1 = rim[(i + 1) % rim.size()];
160 appendOrientedTri(
mesh, hub, surfacePoint(p0.first, p0.second), surfacePoint(p1.first, p1.second), upN,
165 const float seal = 0.12f;
166 auto addSeal = [&](
float x0,
float z0,
float x1,
float z1,
float x2,
float z2,
float x3,
float z3) {
180 const float curbW = maxCurbWidth;
181 const float walkW = maxSidewalkWidth;
182 const float curbH = maxCurbHeight;
183 const float walkH = maxSidewalkHeight;
184 const float r0 = cornerR;
185 const float r1 = std::max(0.2f, cornerR - curbW);
186 const float r2 = std::max(0.15f, cornerR - curbW - walkW);
187 for (
int sx : {-1, 1}) {
188 for (
int sz : {-1, 1}) {
189 const float sxf =
static_cast<float>(
sx);
190 const float szf =
static_cast<float>(
sz);
191 const float cx =
node.x + sxf * jr;
192 const float cz =
node.z + szf * jr;
193 auto arcPt = [&](
float radius,
float t) {
194 const float th =
t * 1.5707963f;
195 return V3{
cx - sxf *
radius * std::sin(th), 0.f, cz - szf *
radius * std::cos(th)};
199 surfacePoint(p1.x, p1.z, offset1), surfacePoint(p0.
x, p0.
z, offset1),
n, 0.f,
202 surfacePoint(p1.x, p1.z, offset1), surfacePoint(p0.
x, p0.
z, offset1),
n * -1.f,
203 0.f, 1.f, 0.f, 1.f, mat);
205 for (
int i = 0; i < segs; ++i) {
206 const float t0 =
static_cast<float>(i) /
static_cast<float>(segs);
207 const float t1 =
static_cast<float>(i + 1) /
static_cast<float>(segs);
208 const V3 a0 = arcPt(r0, t0);
209 const V3 a1 = arcPt(r0, t1);
210 const V3 b0 = arcPt(r1, t0);
211 const V3 b1 = arcPt(r1, t1);
212 const V3 s0 = arcPt(r2, t0);
213 const V3 s1 = arcPt(r2, t1);
215 const V3 c0 = arcPt(std::max(0.05f, r2 - 0.06f), t0);
216 const V3 c1 = arcPt(std::max(0.05f, r2 - 0.06f), t1);
217 const V3 b0i = arcPt(r1 - 0.02f, t0);
218 const V3 b1i = arcPt(r1 - 0.02f, t1);
219 const V3 b0o = arcPt(r1 + 0.02f, t0);
220 const V3 b1o = arcPt(r1 + 0.02f, t1);
222 const V3 outToRoad = inToCorner * -1.f;
226 surfacePoint(b1o.
x, b1o.
z, curbH), surfacePoint(a1.
x, a1.
z, curbH), upN, 0.f,
233 surfacePoint(c1.
x, c1.
z, walkH), surfacePoint(b1i.
x, b1i.
z, walkH), upN, 0.f,
246 std::sort(arms.begin(), arms.end(), [&](
const ArmTip&
a,
const ArmTip&
b) {
247 return std::atan2(a.origin.z - node.z, a.origin.x - node.x) <
248 std::atan2(b.origin.z - node.z, b.origin.x - node.x);
254 float sidewalkHeight;
257 std::vector<V3> rimUps;
258 std::vector<RimStyle> rimStyles;
259 rim.reserve(arms.size() * 8);
260 rimUps.reserve(arms.size() * 8);
261 rimStyles.reserve(arms.size() * 8);
262 auto pushRim = [&](
V3 point, RimStyle style) {
264 if (!rim.empty() &&
length(
point - rim.back()) < 1e-4f)
return;
265 rim.push_back(
point);
266 rimUps.push_back(junctionPlane.
up);
267 rimStyles.push_back(style);
269 auto armStyle = [](
const ArmTip&
tip) {
270 return RimStyle{
tip.curbWidth,
tip.curbHeight,
tip.sidewalkW,
tip.sidewalkH};
272 auto mixStyle = [](RimStyle
a, RimStyle
b,
float t) {
273 const float s =
t *
t * (3.f - 2.f *
t);
274 return RimStyle{
a.curbWidth + (
b.curbWidth -
a.curbWidth) *
s,
275 a.curbHeight + (
b.curbHeight -
a.curbHeight) *
s,
276 a.sidewalkWidth + (
b.sidewalkWidth -
a.sidewalkWidth) *
s,
277 a.sidewalkHeight + (
b.sidewalkHeight -
a.sidewalkHeight) *
s};
279 for (std::size_t armIndex = 0; armIndex < arms.size(); ++armIndex) {
280 const auto&
tip = arms[armIndex];
281 const auto& next = arms[(armIndex + 1) % arms.size()];
282 V3 clockwise =
tip.origin -
tip.side *
tip.asphaltHalf;
283 V3 counterClockwise =
tip.origin +
tip.side *
tip.asphaltHalf;
284 const RimStyle fromStyle = armStyle(
tip);
285 const RimStyle toStyle = armStyle(next);
286 pushRim(clockwise, fromStyle);
287 pushRim(counterClockwise, fromStyle);
291 const V3 end = next.origin - next.side * next.asphaltHalf;
292 const float denominator =
tip.outward.x * next.outward.z -
tip.outward.z * next.outward.x;
293 if (arms.size() >= 3 && std::fabs(denominator) > 1e-4f) {
294 const V3 delta =
end - counterClockwise;
295 const float alongTip = (delta.
x * next.outward.z - delta.
z * next.outward.x) / denominator;
296 const float alongNext = (delta.
x *
tip.outward.z - delta.
z *
tip.outward.x) / denominator;
297 const float maximumMiter =
node.junctionRadius * 2.f +
tip.asphaltHalf + next.asphaltHalf;
298 if (alongTip < 0.f && alongNext < 0.f && -alongTip < maximumMiter && -alongNext < maximumMiter) {
299 V3 control = counterClockwise +
tip.outward * alongTip;
301 const float angle = std::acos(std::clamp(
dot(
tip.outward, next.outward), -1.f, 1.f));
304 for (
int segment = 1; segment <
segments; ++segment) {
305 const float t =
static_cast<float>(segment) /
static_cast<float>(
segments);
306 const float it = 1.f -
t;
307 const V3 point = counterClockwise * (it * it) + control * (2.f * it *
t) +
end * (
t *
t);
308 pushRim(
point, mixStyle(fromStyle, toStyle,
t));
316 const V3 upN = junctionPlane.
up;
317 std::vector<std::array<std::size_t, 3>>
triangles;
327 for (
const auto& arm : arms) {
328 const RimStyle style = armStyle(arm);
329 pushRim(arm.origin - arm.side * arm.asphaltHalf, style);
330 pushRim(arm.origin + arm.side * arm.asphaltHalf, style);
335 "junction sockets form a self-intersecting boundary",
"junction");
346 const auto&
a = arms[
first];
348 if (
dot(
a.authoredOutward,
b.authoredOutward) > -0.999f)
continue;
349 std::array<V3, 4>
corners = {
a.origin -
a.side *
a.asphaltHalf,
a.origin +
a.side *
a.asphaltHalf,
350 b.origin -
b.side *
b.asphaltHalf,
b.origin +
b.side *
b.asphaltHalf};
354 return std::atan2(lhs.z - center.z, lhs.x - center.x) <
355 std::atan2(rhs.z - center.z, rhs.x - center.x);
357 appendOrientedQuad(
mesh,
corners[0],
corners[1],
corners[2],
corners[3], upN, 0.f, 1.f, 0.f, 1.f,
365 auto sameXZ = [](
const V3&
a,
const V3&
b) {
366 return std::fabs(
a.x -
b.x) < 1e-3f && std::fabs(
a.z -
b.z) < 1e-3f;
368 auto isMouth = [&](
const V3&
a,
const V3&
b) {
369 for (
const auto& arm : arms) {
370 const V3 left = arm.origin - arm.side * arm.asphaltHalf;
371 const V3 right = arm.origin + arm.side * arm.asphaltHalf;
376 const float orientation =
signedAreaXZ(rim) >= 0.f ? 1.f : -1.f;
377 auto edgeOutward = [&](std::size_t i) {
378 const V3 edge = rim[(i + 1) % rim.size()] - rim[i];
380 return orientation >= 0.f ? outward : outward * -1.f;
382 auto offsetRing = [&](
const std::vector<float>& distances) {
384 result.reserve(rim.size());
385 for (std::size_t i = 0; i < rim.size(); ++i) {
386 bool alignedToMouth =
false;
387 for (
const auto& arm : arms) {
388 const V3 left = arm.origin - arm.side * arm.asphaltHalf;
389 const V3 right = arm.origin + arm.side * arm.asphaltHalf;
390 if (sameXZ(rim[i],
left)) {
391 V3 point = arm.origin - arm.side * (arm.asphaltHalf + distances[i]);
393 alignedToMouth =
true;
396 if (sameXZ(rim[i],
right)) {
397 V3 point = arm.origin + arm.side * (arm.asphaltHalf + distances[i]);
399 alignedToMouth =
true;
403 if (alignedToMouth)
continue;
404 const V3 previous = edgeOutward((i + rim.size() - 1) % rim.size());
405 const V3 next = edgeOutward(i);
407 const float projection = std::max(0.25f,
dot(miter, next));
408 result.push_back(rim[i] + miter * (distances[i] / projection));
412 std::vector<float> curbDistances;
413 std::vector<float> walkDistances;
414 curbDistances.reserve(rim.size());
415 walkDistances.reserve(rim.size());
416 for (
const auto& style : rimStyles) {
417 curbDistances.push_back(style.curbWidth);
418 walkDistances.push_back(style.curbWidth + style.sidewalkWidth);
420 const auto curbRing = offsetRing(curbDistances);
421 const auto walkRing = offsetRing(walkDistances);
422 for (std::size_t i = 0; i < rim.size(); ++i) {
423 const std::size_t next = (i + 1) % rim.size();
424 if (isMouth(rim[i], rim[next]))
continue;
425 const V3 outward = edgeOutward(i);
426 const V3 upA = rimUps[i];
427 const V3 upB = rimUps[next];
428 const auto& styleA = rimStyles[i];
429 const auto& styleB = rimStyles[next];
430 const V3 innerA{rim[i].
x, rim[i].y, rim[i].z};
431 const V3 innerB{rim[next].
x, rim[next].y, rim[next].z};
432 const V3 surfaceA = innerA;
433 const V3 surfaceB = innerB;
434 const V3 innerTopA = surfaceA + upA * styleA.curbHeight;
435 const V3 innerTopB = surfaceB + upB * styleB.curbHeight;
436 const V3 curbTopA = curbRing[i] + upA * styleA.curbHeight;
437 const V3 curbTopB = curbRing[next] + upB * styleB.curbHeight;
438 const V3 curbWalkA = curbRing[i] + upA * styleA.sidewalkHeight;
439 const V3 curbWalkB = curbRing[next] + upB * styleB.sidewalkHeight;
440 const V3 walkA = walkRing[i] + upA * styleA.sidewalkHeight;
441 const V3 walkB = walkRing[next] + upB * styleB.sidewalkHeight;
442 if (styleA.curbWidth > 1e-4f || styleB.curbWidth > 1e-4f) {
443 appendOrientedQuad(
mesh, innerA, innerB, innerTopB, innerTopA, outward * -1.f, 0.f, 1.f, 0.f, 1.f,
445 appendOrientedQuad(
mesh, innerTopA, innerTopB, curbTopB, curbTopA, upN, 0.f, 1.f, 0.f, 1.f,
447 appendOrientedQuad(
mesh, curbWalkA, curbWalkB, curbTopB, curbTopA, outward, 0.f, 1.f, 0.f, 1.f,
450 if (styleA.sidewalkWidth > 1e-4f || styleB.sidewalkWidth > 1e-4f) {
451 appendOrientedQuad(
mesh, curbWalkA, curbWalkB, walkB, walkA, upN, 0.f, 1.f, 0.f, 1.f,
453 appendOrientedQuad(
mesh, walkRing[i], walkRing[next], walkB, walkA, outward, 0.f, 1.f, 0.f, 1.f,
462 for (
const auto&
edge : network.
edges()) {
465 if (!
spline.ok())
continue;
466 auto len =
spline.value().lengthResult(16);
467 if (!len.ok())
continue;
469 if (len.value() < trim + 1.5f)
continue;
470 const float alongDist =
edge.to ==
node.id ? (len.value() - trim - 1.6f) : (trim + 1.6f);
471 auto frame =
spline.value().travelFrameResult(alongDist,
"clamp", 16);
472 if (!
frame.ok())
continue;
473 const auto&
f =
frame.value();
475 const V3 side{
f.sideX,
f.sideY,
f.sideZ};
477 V3 fwd{
f.forwardX,
f.forwardY,
f.forwardZ};
478 if (
edge.from ==
node.id) fwd = fwd * -1.f;
480 const float stripeW =
edge.style.laneWidth *
static_cast<float>(
edge.lanesForward +
edge.lanesBackward) *
481 frame.value().sample.scaleX * 0.45f;
482 for (
int s = 0;
s < 4; ++
s) {
483 const float along =
static_cast<float>(
s) * 0.5f;
484 const V3 c =
origin - fwd * along + nrm * 0.04f;
485 const V3 a =
c + side * -stripeW;
486 const V3 b =
c + side * stripeW;
487 const V3 d =
a - fwd * 0.25f;
488 const V3 e =
b - fwd * 0.25f;
489 appendOrientedQuad(
mesh,
a,
b, e,
d, nrm, 0.f, 1.f, 0.f, 1.f,
RoadMaterial::Marking);