Skip to content

Commit

Permalink
Make more changes according to PR#293, Fix clang-tidy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Capital-Asterisk committed Jul 1, 2024
1 parent b4141b7 commit dd7db8b
Show file tree
Hide file tree
Showing 17 changed files with 123 additions and 101 deletions.
2 changes: 2 additions & 0 deletions src/osp/core/array_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ namespace osp
using Corrade::Containers::ArrayView;
using Corrade::Containers::arrayView;

using Corrade::Containers::arrayCast;

/**
* @brief Wraps a Corrade ArrayView or StridedArrayView to use as a 2D array of equally sized rows
*/
Expand Down
4 changes: 2 additions & 2 deletions src/osp/core/buffer_format.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ struct BufAttribFormat
{
using View_t = Corrade::Containers::StridedArrayView1D<T>;
using ViewConst_t = Corrade::Containers::StridedArrayView1D<T const>;
using Data_t = Corrade::Containers::ArrayView<unsigned char>;
using DataConst_t = Corrade::Containers::ArrayView<unsigned char const>;
using Data_t = Corrade::Containers::ArrayView<std::byte>;
using DataConst_t = Corrade::Containers::ArrayView<std::byte const>;

constexpr View_t view(Data_t data, std::size_t count) const noexcept
{
Expand Down
9 changes: 4 additions & 5 deletions src/osp/core/math_int64.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace osp
/**
* @brief int64 abs(lhs - rhs) with no risk of overflow
*/
constexpr std::uint64_t absdelta(std::int64_t lhs, std::int64_t rhs) noexcept
constexpr std::uint64_t abs_difference(std::int64_t lhs, std::int64_t rhs) noexcept
{
// TODO: maybe use int128 for compilers that support it?

Expand All @@ -53,13 +53,12 @@ constexpr std::uint64_t absdelta(std::int64_t lhs, std::int64_t rhs) noexcept

/**
* @brief (distance between a and b) > threshold
*
*/
constexpr bool is_distance_near(Vector3l const a, Vector3l const b, double const threshold) noexcept
{
double const dx( absdelta(a.x(), b.x()) );
double const dy( absdelta(a.y(), b.y()) );
double const dz( absdelta(a.z(), b.z()) );
double const dx = abs_difference(a.x(), b.x());
double const dy = abs_difference(a.y(), b.y());
double const dz = abs_difference(a.z(), b.z());

return (dx*dx + dy*dy + dz*dz) < threshold*threshold;
}
Expand Down
2 changes: 1 addition & 1 deletion src/planet-a/chunk_generate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void ChunkScratchpad::resize(ChunkSkeleton const& rChSk)
auto const maxSharedVrtx = rChSk.m_sharedIds.capacity();
auto const maxChunks = rChSk.m_chunkIds.capacity();

edgeVertices .resize((rChSk.m_chunkEdgeVrtxCount - 1) * 3);
edgeVertices .resize(std::size_t(rChSk.m_chunkEdgeVrtxCount - 1) * 3);
stitchCmds .resize(maxChunks, {});
chunksAdded .resize(maxChunks);
chunksRemoved .resize(maxChunks);
Expand Down
4 changes: 2 additions & 2 deletions src/planet-a/chunk_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ void ChunkFillSubdivLUT::subdiv_line_recurse(
{
Vector2us const mid = (a + b) / 2;

int const out = xy_to_triangular(mid.x() - 1, mid.y() - 2);
std::uint32_t const out = xy_to_triangular(mid.x() - 1, mid.y() - 2);
ChunkLocalSharedId const sharedA = coord_to_shared(a.x(), a.y(), m_edgeVrtxCount);
ChunkLocalSharedId const sharedB = coord_to_shared(b.x(), b.y(), m_edgeVrtxCount);

m_data.emplace_back(ToSubdiv{
.vrtxA = std::uint32_t(sharedA.has_value() ? sharedA.value : xy_to_triangular(a.x() - 1, a.y() - 2)),
.vrtxB = std::uint32_t(sharedB.has_value() ? sharedB.value : xy_to_triangular(b.x() - 1, b.y() - 2)),
.fillOut = std::uint32_t(out),
.fillOut = out,
.aIsShared = sharedA.has_value(),
.bIsShared = sharedB.has_value()
});
Expand Down
4 changes: 2 additions & 2 deletions src/planet-a/chunk_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ constexpr ChunkMeshBufferInfo make_chunk_mesh_buffer_info(ChunkSkeleton const &s
* @param y [in]
* @return triangular number
*/
constexpr int xy_to_triangular(int const x, int const y) noexcept
constexpr std::uint32_t xy_to_triangular(std::uint32_t const x, std::uint32_t const y) noexcept
{
return ( y * (y + 1) ) / 2 + x;
};
Expand Down Expand Up @@ -164,7 +164,7 @@ constexpr ChunkLocalSharedId coord_to_shared(
}
}

constexpr VertexIdx fill_to_vrtx(ChunkMeshBufferInfo const& info, ChunkId const chunkId, int const triangular)
constexpr VertexIdx fill_to_vrtx(ChunkMeshBufferInfo const& info, ChunkId const chunkId, std::uint32_t const triangular)
{
return info.vbufFillOffset + info.fillVrtxCount * chunkId.value + triangular;
}
Expand Down
2 changes: 1 addition & 1 deletion src/planet-a/geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void BasicChunkMeshGeometry::resize(ChunkSkeleton const& skCh, ChunkMeshBufferIn
vbufPositions = formatBuilder.insert_block<osp::Vector3>(info.vrtxTotal);
vbufNormals = formatBuilder.insert_block<osp::Vector3>(info.vrtxTotal);

vrtxBuffer = Array<unsigned char>(Corrade::ValueInit, formatBuilder.total_size());
vrtxBuffer = Array<std::byte> (Corrade::ValueInit, formatBuilder.total_size());
indxBuffer = Array<osp::Vector3u>(Corrade::ValueInit, info.faceTotal);

chunkFanNormalContrib .resize(maxChunks * info.fanMaxSharedCount);
Expand Down
2 changes: 1 addition & 1 deletion src/planet-a/geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ struct BasicChunkMeshGeometry
{
void resize(ChunkSkeleton const& skCh, ChunkMeshBufferInfo const& info);

Corrade::Containers::Array<unsigned char> vrtxBuffer; ///< Output vertex buffer
Corrade::Containers::Array<std::byte> vrtxBuffer; ///< Output vertex buffer
Corrade::Containers::Array<osp::Vector3u> indxBuffer; ///< Output index buffer

osp::BufAttribFormat<osp::Vector3> vbufPositions; ///< Describes Position data in vrtxBuffer
Expand Down
19 changes: 8 additions & 11 deletions src/planet-a/icosahedron.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ SubdivTriangleSkeleton create_skeleton_icosahedron(

rSkData.positions.resize(skeleton.vrtx_ids().capacity());
rSkData.normals.resize (skeleton.vrtx_ids().capacity());
double const totalScale = radius * std::pow<double>(2.0, rSkData.precision);
double const totalScale = radius * std::exp2(rSkData.precision);
for (int i = 0; i < gc_icoVrtxCount; i ++)
{
rSkData.positions[vrtxIds[i]] = osp::Vector3l(gc_icoVrtxPos[i] * totalScale);
Expand Down Expand Up @@ -116,7 +116,7 @@ static void calc_midpoint_spherical(
double const curvature = radius - midLen;

rSkData.normals[mid] = osp::Vector3(midPosDbl / midLen);
rSkData.positions[mid] = midPos + osp::Vector3l(rSkData.normals[mid] * curvature * scale);
rSkData.positions[mid] = midPos + osp::Vector3l(osp::Vector3d(rSkData.normals[mid]) * (curvature * scale));
}


Expand All @@ -126,7 +126,7 @@ void ico_calc_middles(
std::array<osp::MaybeNewId<SkVrtxId>, 3> const vrtxMid,
SkeletonVertexData &rSkData)
{
float const scale = std::pow(2.0f, rSkData.precision);
double const scale = std::exp2(double(rSkData.precision));

if (vrtxMid[0].isNew)
{
Expand Down Expand Up @@ -157,7 +157,7 @@ void ico_calc_chunk_edge(
return;
}

float const scale = std::pow(2.0f, rSkData.precision);
float const scale = std::exp2(float(rSkData.precision));

auto const recurse = [scale, radius, &rSkData] (auto &&self, SkVrtxId const a, SkVrtxId const b, int const currentLevel, ChunkEdgeView_t const view) noexcept -> void
{
Expand All @@ -182,18 +182,16 @@ void ico_calc_chunk_edge(

void ico_calc_sphere_tri_center(
SkTriGroupId const groupId,
float const maxRadius,
float const height,
double const maxRadius,
double const height,
SubdivTriangleSkeleton const &rSkel,
SkeletonVertexData& rSkData)
{
using osp::math::int_2pow;

SkTriGroup const &group = rSkel.tri_group_at(groupId);
LGRN_ASSERT(group.depth < gc_icoTowerOverHorizonVsLevel.size());

double const terrainMaxHeight = height + maxRadius * gc_icoTowerOverHorizonVsLevel[group.depth];
int const scale = int_2pow<int>(rSkData.precision);
float const scale = std::exp2(float(rSkData.precision));

for (int i = 0; i < 4; ++i)
{
Expand All @@ -213,8 +211,7 @@ void ico_calc_sphere_tri_center(
+ rSkData.normals[vb]
+ rSkData.normals[vc]) / 3.0;


osp::Vector3l const highestPoint = osp::Vector3l(nrmAverage * terrainMaxHeight * scale);
osp::Vector3l const highestPoint = osp::Vector3l(nrmAverage * float(terrainMaxHeight * scale));

rSkData.centers[sktriId] = posAverage + highestPoint;
}
Expand Down
4 changes: 2 additions & 2 deletions src/planet-a/icosahedron.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ void ico_calc_chunk_edge(
*/
void ico_calc_sphere_tri_center(
SkTriGroupId groupId,
float maxRadius,
float height,
double maxRadius,
double height,
SubdivTriangleSkeleton const &rSkel,
SkeletonVertexData &rSkData);

Expand Down
98 changes: 61 additions & 37 deletions src/planet-a/skeleton_subdiv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,11 @@ void unsubdivide_select_by_distance(
// Floodfill by checking neighbors next
SkeletonTriangle const& sktri = rSkel.tri_at(sktriId);
for (SkTriId const neighbor : sktri.neighbors)
if (neighbor.has_value())
{
maybe_distance_check(neighbor);
if (neighbor.has_value())
{
maybe_distance_check(neighbor);
}
}
}
}
Expand All @@ -128,8 +130,12 @@ void unsubdivide_deselect_invariant_violations(
{
int subdivedNeighbors = 0;
for (SkTriId const neighbor : sktri.neighbors)
if (neighbor.has_value())
{
if (!neighbor.has_value())
{
continue;
}

SkeletonTriangle const &rNeighbor = rSkel.tri_at(neighbor);
// Pretend neighbor is unsubdivided when it's in tryUnsubdiv, overrided
// by cantUnsubdiv
Expand All @@ -147,17 +153,19 @@ void unsubdivide_deselect_invariant_violations(
switch (neighborEdge)
{
case 0:
if (neighborGroup.triangles[0].children.has_value()) return true;
if (neighborGroup.triangles[1].children.has_value()) return true;
if (neighborGroup.triangles[0].children.has_value()) { return true; }
if (neighborGroup.triangles[1].children.has_value()) { return true; }
break;
case 1:
if (neighborGroup.triangles[1].children.has_value()) return true;
if (neighborGroup.triangles[2].children.has_value()) return true;
if (neighborGroup.triangles[1].children.has_value()) { return true; }
if (neighborGroup.triangles[2].children.has_value()) { return true; }
break;
case 2:
if (neighborGroup.triangles[2].children.has_value()) return true;
if (neighborGroup.triangles[0].children.has_value()) return true;
if (neighborGroup.triangles[2].children.has_value()) { return true; }
if (neighborGroup.triangles[0].children.has_value()) { return true; }
break;
default:
LGRN_ASSERTM(false, "This should never happen. Triangles only have 3 sides!");
}
}
}
Expand All @@ -175,12 +183,16 @@ void unsubdivide_deselect_invariant_violations(
{
SkeletonTriangle const& sktri = rSkel.tri_at(sktriId);

if (violates_invariants(sktriId, sktri))
if ( ! violates_invariants(sktriId, sktri) )
{
rSP.cantUnsubdiv.insert(sktriId);
return;
}

// Recurse into neighbors if they're also tryUnsubdiv
for (SkTriId const neighbor : sktri.neighbors)
rSP.cantUnsubdiv.insert(sktriId);

// Recurse into neighbors if they're also tryUnsubdiv
for (SkTriId const neighbor : sktri.neighbors)
{
if (rSP.tryUnsubdiv.contains(neighbor) && ! rSP.cantUnsubdiv.contains(neighbor))
{
self(self, neighbor);
Expand Down Expand Up @@ -212,15 +224,23 @@ void unsubdivide_level(
SubdivScratchpadLevel &rLvlSP = rSP .levels[lvl];

for (SkTriId const sktriId : rSP.tryUnsubdiv)
if ( ! rSP.cantUnsubdiv.contains(sktriId) )
{
if ( rSP.cantUnsubdiv.contains(sktriId) )
{
continue;
}

// All checks passed, 100% confirmed sktri will be unsubdivided
SkeletonTriangle &rTri = rSkel.tri_at(sktriId);

LGRN_ASSERT(!rLvl.hasSubdivedNeighbor.contains(sktriId));
for (SkTriId const neighborId : rTri.neighbors)
if ( neighborId.has_value() && wont_unsubdivide(neighborId) )
{
if ( ! ( neighborId.has_value() && wont_unsubdivide(neighborId) ) )
{
continue;
}

SkeletonTriangle const& rNeighborTri = rSkel.tri_at(neighborId);
if ( rNeighborTri.children.has_value() )
{
Expand All @@ -229,18 +249,18 @@ void unsubdivide_level(
}
else
{
bool neighborHasSubdivedNeighbor = false;
for (SkTriId const neighborNeighborId : rNeighborTri.neighbors)
if ( neighborNeighborId.has_value()
&& neighborNeighborId != sktriId
&& wont_unsubdivide(neighborNeighborId)
&& rSkel.is_tri_subdivided(neighborNeighborId) )
auto const neighbor_has_subdivided_neighbor = [sktriId, &wont_unsubdivide, &rSkel]
(SkTriId const neighborNeighborId)
{
neighborHasSubdivedNeighbor = true;
break;
}

if (neighborHasSubdivedNeighbor)
return neighborNeighborId.has_value()
&& neighborNeighborId != sktriId
&& wont_unsubdivide(neighborNeighborId)
&& rSkel.is_tri_subdivided(neighborNeighborId);
};

if ( neighbor_has_subdivided_neighbor(rNeighborTri.neighbors[0])
|| neighbor_has_subdivided_neighbor(rNeighborTri.neighbors[1])
|| neighbor_has_subdivided_neighbor(rNeighborTri.neighbors[2]) )
{
rLvl.hasSubdivedNeighbor.insert(neighborId);
}
Expand Down Expand Up @@ -362,9 +382,13 @@ SkTriGroupId subdivide(

// Check neighbours along all 3 edges
for (int selfEdgeIdx = 0; selfEdgeIdx < 3; ++selfEdgeIdx)
if ( SkTriId const neighborId = neighbors[selfEdgeIdx];
neighborId.has_value() )
{
SkTriId const neighborId = neighbors[selfEdgeIdx];
if ( ! neighborId.has_value() )
{
continue; // Neighbor does not exist
}

SkeletonTriangle& rNeighbor = rSkel.tri_at(neighborId);
if (rNeighbor.children.has_value())
{
Expand Down Expand Up @@ -394,17 +418,17 @@ SkTriGroupId subdivide(
}
}

bool neighborHasNonSubdivedNeighbor = false;
for (SkTriId const neighborNeighborId : rNeighbor.neighbors)
if ( neighborNeighborId.has_value()
&& neighborNeighborId != sktriId
&& ! rSkel.is_tri_subdivided(neighborNeighborId) )
auto const neighbor_has_subdivided_neighbor = [sktriId, &rSkel]
(SkTriId const neighborNeighborId)
{
neighborHasNonSubdivedNeighbor = true;
break;
}
return neighborNeighborId.has_value()
&& neighborNeighborId != sktriId
&& ! rSkel.is_tri_subdivided(neighborNeighborId);
};

if (neighborHasNonSubdivedNeighbor)
if ( neighbor_has_subdivided_neighbor(rNeighbor.neighbors[0])
|| neighbor_has_subdivided_neighbor(rNeighbor.neighbors[1])
|| neighbor_has_subdivided_neighbor(rNeighbor.neighbors[2]))
{
rLvl.hasNonSubdivedNeighbor.insert(neighborId);
}
Expand Down
4 changes: 2 additions & 2 deletions src/planet-a/skeleton_subdiv.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ struct SkeletonSubdivScratchpad

void resize(SubdivTriangleSkeleton &rSkel);

std::array<std::uint64_t, gc_maxSubdivLevels> distanceThresholdSubdiv{{}};
std::array<std::uint64_t, gc_maxSubdivLevels> distanceThresholdUnsubdiv{{}};
std::array<double, gc_maxSubdivLevels> distanceThresholdSubdiv{{}};
std::array<double, gc_maxSubdivLevels> distanceThresholdUnsubdiv{{}};

std::array<SubdivScratchpadLevel, gc_maxSubdivLevels> levels;

Expand Down
Loading

0 comments on commit dd7db8b

Please sign in to comment.