Skip to content

Commit

Permalink
Clean things up, add more complex test tileset, and fix bugs with cle…
Browse files Browse the repository at this point in the history
…aring/temperature!
  • Loading branch information
heyx3 committed Dec 21, 2024
1 parent 8f574d7 commit 85dde4a
Show file tree
Hide file tree
Showing 21 changed files with 262 additions and 90 deletions.
2 changes: 1 addition & 1 deletion WFC++.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ xcopy "$(ProjectDir)WFC++\*.hpp" "$(TargetDir)include" /sy</Command>
<ClInclude Include="WFC++\EnumFlags.h" />
<ClInclude Include="WFC++\HelperClasses.h" />
<ClInclude Include="WFC++\List.hpp" />
<ClInclude Include="WFC++\MemoryChecks.h" />
<ClInclude Include="WFC++\MemoryChecks.inl" />
<ClInclude Include="WFC++\Tiled3D\StandardRunner.h" />
<ClInclude Include="WFC++\WFCppStreamPrinting.hpp" />
<ClInclude Include="WFC++\Tiled3D\Grid.h" />
Expand Down
2 changes: 1 addition & 1 deletion WFC++.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
<ClInclude Include="WFC++\xoshiro.hpp">
<Filter>Helper Classes</Filter>
</ClInclude>
<ClInclude Include="WFC++\MemoryChecks.h">
<ClInclude Include="WFC++\MemoryChecks.inl">
<Filter>Helper Classes</Filter>
</ClInclude>
</ItemGroup>
Expand Down
19 changes: 14 additions & 5 deletions WFC++/Array2D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ namespace WFC
Array2D(Array2D&& toMove) : arrayVals(nullptr) { *this = std::move(toMove); }
Array2D& operator=(Array2D&& toMove)
{
if (&toMove == this)
return *this;

if (arrayVals != nullptr)
delete[] arrayVals;

Expand All @@ -64,6 +67,9 @@ namespace WFC
Array2D(const Array2D<ArrayType>& copy) { *this = copy; }
Array2D& operator=(const Array2D<ArrayType>& other)
{
if (&other == this)
return *this;

if (arrayVals != nullptr)
delete[] arrayVals;

Expand All @@ -72,8 +78,11 @@ namespace WFC
arrayVals = new ArrayType[GetNumbElements()];

size_t size = (size_t)(GetNumbElements());
for (size_t i = 0; i < size; ++i)
arrayVals[i] = other.arrayVals[i];
for (size_t i = 0; i < size; ++i)
{
WFCPP_ASSERT(i < other.GetNumbElements());
arrayVals[i] = other.arrayVals[i];
}

return *this;
}
Expand All @@ -89,12 +98,12 @@ namespace WFC

ArrayType& operator[](Vector2i l)
{
assert(Region2i(GetDimensions()).Contains(l));
WFCPP_ASSERT(Region2i(GetDimensions()).Contains(l));
return arrayVals[GetIndex(l.x, l.y)];
}
const ArrayType& operator[](Vector2i l) const
{
assert(Region2i(GetDimensions()).Contains(l));
WFCPP_ASSERT(Region2i(GetDimensions()).Contains(l));
return arrayVals[GetIndex(l.x, l.y)];
}

Expand Down Expand Up @@ -236,7 +245,7 @@ namespace WFC
});
break;

default: assert(false);
default: WFCPP_ASSERT(false);
}
}

Expand Down
19 changes: 14 additions & 5 deletions WFC++/Array3D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ namespace WFC
Array3D(Vector3i size, const ArrayType& defaultValue) : Array3D(size.x, size.y, size.z, defaultValue) { }

//Move operator.
Array3D(Array3D&& toMove) : arrayVals(nullptr) { *this = std::move(toMove); }
Array3D& operator=(Array3D&& toMove)
Array3D(Array3D&& toMove) noexcept : arrayVals(nullptr) { *this = std::move(toMove); }
Array3D& operator=(Array3D&& toMove) noexcept
{
if (&toMove == this)
return *this;

if (arrayVals != nullptr)
delete[] arrayVals;

Expand All @@ -72,6 +75,9 @@ namespace WFC
Array3D(const Array3D<ArrayType>& copy) { *this = copy; }
Array3D& operator=(const Array3D<ArrayType>& other)
{
if (&other == this)
return *this;

if (arrayVals != nullptr)
delete[] arrayVals;

Expand All @@ -81,7 +87,10 @@ namespace WFC
arrayVals = new ArrayType[GetNumbElements()];

for (size_t i = 0; i < (size_t)GetNumbElements(); ++i)
arrayVals[i] = other.arrayVals[i];
{
WFCPP_ASSERT(i < other.GetNumbElements());
arrayVals[i] = other.arrayVals[i];
}

return *this;
}
Expand All @@ -97,12 +106,12 @@ namespace WFC

ArrayType& operator[](const Vector3i& l)
{
assert(IsIndexValid(l));
WFCPP_ASSERT(IsIndexValid(l));
return arrayVals[GetIndex(l.x, l.y, l.z)];
}
const ArrayType& operator[](const Vector3i& l) const
{
assert(IsIndexValid(l));
WFCPP_ASSERT(IsIndexValid(l));
return arrayVals[GetIndex(l.x, l.y, l.z)];
}

Expand Down
19 changes: 14 additions & 5 deletions WFC++/Array4D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ namespace WFC
}

//Move operator.
Array4D(Array4D&& toMove) : arrayVals(nullptr) { *this = std::move(toMove); }
Array4D& operator=(Array4D&& toMove)
Array4D(Array4D&& toMove) noexcept : arrayVals(nullptr) { *this = std::move(toMove); }
Array4D& operator=(Array4D&& toMove) noexcept
{
if (&toMove == this)
return *this;

if (arrayVals != nullptr)
delete[] arrayVals;

Expand All @@ -53,14 +56,20 @@ namespace WFC
Array4D(const Array4D<ArrayType>& copy) { *this = copy; }
Array4D& operator=(const Array4D<ArrayType>& other)
{
if (&other == this)
return *this;

if (arrayVals != nullptr)
delete[] arrayVals;

size = other.size;
arrayVals = new ArrayType[GetNumbElements()];

for (size_t i = 0; i < (size_t)GetNumbElements(); ++i)
arrayVals[i] = other.arrayVals[i];
{
WFCPP_ASSERT(i < other.GetNumbElements());
arrayVals[i] = other.arrayVals[i];
}

return *this;
}
Expand All @@ -76,12 +85,12 @@ namespace WFC

ArrayType& operator[](const Vector4i& l)
{
assert(Region4i(GetDimensions()).Contains(l));
WFCPP_ASSERT(Region4i(GetDimensions()).Contains(l));
return arrayVals[GetIndex(l.x, l.y, l.z, l.w)];
}
const ArrayType& operator[](const Vector4i& l) const
{
assert(Region4i(GetDimensions()).Contains(l));
WFCPP_ASSERT(Region4i(GetDimensions()).Contains(l));
return arrayVals[GetIndex(l.x, l.y, l.z, l.w)];
}

Expand Down
2 changes: 1 addition & 1 deletion WFC++/HelperClasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ namespace WFC
if (remainingBudget <= 0)
return i;
}
return weights.GetSize() - 1;
return static_cast<size_t>(weights.GetSize() - 1);
}
}
10 changes: 5 additions & 5 deletions WFC++/HelperSrc/Vector2i.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Transformations WFC::Invert(Transformations t)

case Transformations::None: return Transformations::None;

default: assert(false); return Transformations::None;
default: WFCPP_ASSERT(false); return Transformations::None;
}
}
bool WFC::WillSwapAxes(Transformations t)
Expand All @@ -39,7 +39,7 @@ bool WFC::WillSwapAxes(Transformations t)
case Transformations::FlipDiag2:
return true;

default: assert(false); return false;
default: WFCPP_ASSERT(false); return false;
}
}
const char* WFC::ToString(Transformations t)
Expand All @@ -55,7 +55,7 @@ const char* WFC::ToString(Transformations t)
case Transformations::FlipDiag2: return "FlipDiag2";
case Transformations::None: return "None";

default: assert(false); return "UNKNOWN";
default: WFCPP_ASSERT(false); return "UNKNOWN";
}
}
bool WFC::IsReflection(Transformations t)
Expand All @@ -74,7 +74,7 @@ bool WFC::IsReflection(Transformations t)
case Transformations::FlipDiag2:
return true;

default: assert(false); return false;
default: WFCPP_ASSERT(false); return false;
}
}

Expand Down Expand Up @@ -114,6 +114,6 @@ Vector2i Vector2i::Transform(Transformations trnsf, Vector2i size) const
case Transformations::None:
return *this;

default: assert(false); return *this;
default: WFCPP_ASSERT(false); return *this;
}
}
6 changes: 4 additions & 2 deletions WFC++/MemoryChecks.h → WFC++/MemoryChecks.inl
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@

//Make sure there is an error handler.
#if WFCPP_CHECK_MEMORY && !defined(WFCPP_CHECK_MEMORY_ERROR)
#define WFCPP_CHECK_MEMORY_ERROR(s, ...) \
fprintf(stderr, "WFCPP: heap corruption detected! " s, ##__VA_ARGS__)
#define WFCPP_CHECK_MEMORY_ERROR(s, ...) { \
fprintf(stderr, "WFCPP: heap corruption detected! " s "\n", ##__VA_ARGS__); \
fflush(stderr); \
}
#endif

namespace WFC::DEBUGMEM
Expand Down
5 changes: 4 additions & 1 deletion WFC++/Platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,7 @@


//Forcefully include debugging headers here so that all files will see them.
#include "MemoryChecks.h"
#if !defined(WFCPP_ASSERT)
#define WFCPP_ASSERT(c, ...) assert(c)
#endif
#include "MemoryChecks.inl"
4 changes: 2 additions & 2 deletions WFC++/Tiled/Tile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace WFC
{
switch (e)
{
default: assert(false);
default: WFCPP_ASSERT(false);

case EdgeDirs::MaxX: return Vector2i(1, 0);
case EdgeDirs::MaxY: return Vector2i(0, 1);
Expand All @@ -42,7 +42,7 @@ namespace WFC
{
switch (e)
{
default: assert(false);
default: WFCPP_ASSERT(false);

case EdgeDirs::MinX: return EdgeDirs::MaxX;
case EdgeDirs::MaxX: return EdgeDirs::MinX;
Expand Down
4 changes: 2 additions & 2 deletions WFC++/Tiled/TilePermutator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ TilePermutator::TilePermutator(const List<Tile>& originalTiles,
}

//Make each permutation.
assert(!_permutations.Contains(Transformations::None));
WFCPP_ASSERT(!_permutations.Contains(Transformations::None));
for (Transformations tr : permutations)
{
for (TileID parentID = 0; parentID < (TileID)nOriginalTiles; ++parentID)
Expand Down Expand Up @@ -117,7 +117,7 @@ TilePermutator::TilePermutator(const List<Tile>& originalTiles,
#undef EDGES

default:
assert(false);
WFCPP_ASSERT(false);
}

//Register the tile.
Expand Down
21 changes: 11 additions & 10 deletions WFC++/Tiled3D/Grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Grid::Grid(const List<Tile>& inputTiles, const Vector3i& outputSize)
Cells(outputSize),
PossiblePermutations({ (int)inputTiles.GetSize(), outputSize })
{
assert(inputTiles.GetSize() < TileIdx_INVALID); //The last index is reserved for [null]
WFCPP_ASSERT(inputTiles.GetSize() < TileIdx_INVALID); //The last index is reserved for [null]

//Set up FaceIndices.
int32_t nextID = 0;
Expand Down Expand Up @@ -90,7 +90,7 @@ void Grid::SetCell(const Vector3i& pos, TileIdx tile, Transform3D tilePermutatio
Report* report, bool doubleCheckLegalFit, bool canBeChangedInFuture)
{
if (doubleCheckLegalFit)
assert(IsLegalPlacement(pos, tile, tilePermutation));
WFCPP_ASSERT(IsLegalPlacement(pos, tile, tilePermutation));

//If the cell is being *replaced* rather than going from unset to set,
// then neighbor data is harder to update seamlessly because
Expand Down Expand Up @@ -134,8 +134,9 @@ void Grid::ClearCells(const Region3i& region, Report* report,
else
{
cell.IsChangeable |= clearedImmutableCellsAreMutableNow;
cell.ChosenPermutation = { }; //Not important, but it keeps unset cells
// consistent for debugging
#if !defined(NDEBUG)
cell.ChosenPermutation = { }; //Give unset cells a standardized value.
#endif
ResetCellPossibilities(cellPos, cell, report);
}
}
Expand Down Expand Up @@ -205,7 +206,7 @@ void Grid::ClearCells(const Region3i& region, Report* report,
for (const Vector3i& cellPos : unclearedCellsInRegion)
{
const auto& cell = Cells[cellPos];
assert(cell.IsSet());
WFCPP_ASSERT(cell.IsSet());

for (const auto& neighborData : GetNeighbors(cellPos))
{
Expand Down Expand Up @@ -238,20 +239,20 @@ void Grid::ApplyFilter(const Vector3i& cellPos,
//It's possible, if uncommon, that a tileset has no match for a particular face.
if (!FaceIndices.Contains(face))
{
TransformSet::ClearRow(&PossiblePermutations[{0, cellPos}],
InputTiles.GetSize());
for (int i = 0; i < InputTiles.GetSize(); ++i)
PossiblePermutations[{ i, cellPos }] = { };
cell.NPossibilities = 0;
}
else
{
auto faceIdx = FaceIndices[face];
for (int tileI = 0; tileI < InputTiles.GetSize(); ++tileI)
{
const auto& supported = MatchingFaces[{tileI, faceIdx}];
auto& available = PossiblePermutations[{tileI, cellPos}];
const auto& supported = MatchingFaces[{ tileI, faceIdx }];
auto& available = PossiblePermutations[{ tileI, cellPos }];
auto nChoicesLost = available.Intersect(supported);

assert(nChoicesLost <= cell.NPossibilities);
WFCPP_ASSERT(nChoicesLost <= cell.NPossibilities);
cell.NPossibilities -= nChoicesLost;
}
}
Expand Down
Loading

0 comments on commit 85dde4a

Please sign in to comment.