Fix petit bogue de position

This commit is contained in:
Timothée Leclaire-Fournier 2024-01-29 15:00:08 -05:00
parent d81c757bd2
commit 69461a3f6f

View File

@ -362,7 +362,7 @@ bool GOLTeamH::setFromPattern(std::string const& pattern)
size_t centerX = mData.width() / 2 - (sq.value().width / 2); size_t centerX = mData.width() / 2 - (sq.value().width / 2);
size_t centerY = mData.height() / 2 - (sq.value().height / 2); size_t centerY = mData.height() / 2 - (sq.value().height / 2);
fillDataFromPattern(pattern, sq.value(), centerX, centerY); fillDataFromPattern(pattern, sq.value(), centerX + 1, centerY + 1);
mIteration = 0; mIteration = 0;
countLifeStatusCells(); countLifeStatusCells();
@ -422,7 +422,8 @@ void GOLTeamH::processOneStep()
size_t neighborsAliveCount{}, aliveCount{}; size_t neighborsAliveCount{}, aliveCount{};
// On commence à la première case qui n'est pas dans le border // On commence à la première case qui n'est pas dans le border pour sauver une opération
// par cycle.
// Pointeur du tableau intermédiaire. // Pointeur du tableau intermédiaire.
auto* ptrGridInt{ reinterpret_cast<uint8_t*>(mData.intData()) + (offset + 1) }; auto* ptrGridInt{ reinterpret_cast<uint8_t*>(mData.intData()) + (offset + 1) };
@ -607,6 +608,7 @@ void GOLTeamH::fillDataFromPattern(std::string const& pattern, sizeQueried& sq,
// Remplissage de la grille aux positions spécifiées par le patron // Remplissage de la grille aux positions spécifiées par le patron
for (size_t y = 0; y < sq.height; ++y) { for (size_t y = 0; y < sq.height; ++y) {
for (size_t x = 0; x < sq.width; ++x) { for (size_t x = 0; x < sq.width; ++x) {
// TODO: Check si in bounds et vérifie que ça répare le problème de quand on ferme, erreur.
State cellState = (pattern[sq.pos++] == '0') ? State::dead : State::alive; State cellState = (pattern[sq.pos++] == '0') ? State::dead : State::alive;
mData.setAt(centerX + x, centerY + y, cellState); mData.setAt(centerX + x, centerY + y, cellState);
} }
@ -627,6 +629,8 @@ void GOLTeamH::countLifeStatusCells()
mData.setAliveCount(aliveCount); mData.setAliveCount(aliveCount);
} }
// TODO: combiner avec fillBorder
void GOLTeamH::modifyBorderIfNecessary(uint8_t* ptrGrid, uint8_t* ptrGridInt) void GOLTeamH::modifyBorderIfNecessary(uint8_t* ptrGrid, uint8_t* ptrGridInt)
{ {
auto bm = mBorderManagement.value_or(BorderManagement::immutableAsIs); auto bm = mBorderManagement.value_or(BorderManagement::immutableAsIs);
@ -641,13 +645,13 @@ void GOLTeamH::modifyBorderIfNecessary(uint8_t* ptrGrid, uint8_t* ptrGridInt)
auto* e_ptr = ptrGrid + (width - 1); auto* e_ptr = ptrGrid + (width - 1);
// Lambda pour une opération courante. // Lambda pour une opération courante.
auto op = [rule](size_t count, uint8_t* ptrGrid) { auto getFutureStatus = [rule](size_t count, uint8_t* ptrGrid) {
return static_cast<bool>((rule >> *(ptrGrid) * 16) & (1u << count)); return static_cast<bool>((rule >> *(ptrGrid) * 16) & (1u << count));
}; };
// TOP // TOP
while (ptrGrid < e_ptr) { while (ptrGrid < e_ptr) {
*ptrGridInt = op(countNeighbors(ptrGrid, bm), ptrGrid); *ptrGridInt = getFutureStatus(countNeighbors(ptrGrid, bm), ptrGrid);
ptrGrid++; ptrGrid++;
ptrGridInt++; ptrGridInt++;
@ -656,7 +660,7 @@ void GOLTeamH::modifyBorderIfNecessary(uint8_t* ptrGrid, uint8_t* ptrGridInt)
// DROITE // DROITE
e_ptr += width * (height - 1); e_ptr += width * (height - 1);
while (ptrGrid < e_ptr) { while (ptrGrid < e_ptr) {
*ptrGridInt = op(countNeighbors(ptrGrid, bm), ptrGrid); *ptrGridInt = getFutureStatus(countNeighbors(ptrGrid, bm), ptrGrid);
ptrGrid += width; ptrGrid += width;
ptrGridInt += width; ptrGridInt += width;
@ -665,7 +669,7 @@ void GOLTeamH::modifyBorderIfNecessary(uint8_t* ptrGrid, uint8_t* ptrGridInt)
// DESSOUS // DESSOUS
e_ptr -= (width - 1); e_ptr -= (width - 1);
while (ptrGrid > e_ptr) { while (ptrGrid > e_ptr) {
*ptrGridInt = op(countNeighbors(ptrGrid, bm), ptrGrid); *ptrGridInt = getFutureStatus(countNeighbors(ptrGrid, bm), ptrGrid);
ptrGrid--; ptrGrid--;
ptrGridInt--; ptrGridInt--;
@ -674,7 +678,7 @@ void GOLTeamH::modifyBorderIfNecessary(uint8_t* ptrGrid, uint8_t* ptrGridInt)
// GAUCHE // GAUCHE
e_ptr -= width * (height - 1); e_ptr -= width * (height - 1);
while (ptrGrid > e_ptr) { while (ptrGrid > e_ptr) {
*ptrGridInt = op(countNeighbors(ptrGrid, bm), ptrGrid); *ptrGridInt = getFutureStatus(countNeighbors(ptrGrid, bm), ptrGrid);
ptrGrid -= width; ptrGrid -= width;
ptrGridInt -= width; ptrGridInt -= width;
@ -690,14 +694,14 @@ size_t GOLTeamH::countNeighbors(uint8_t* ptrGrid, BorderManagement bm) const
// Petit lambda pour simplifier les opérations. // Petit lambda pour simplifier les opérations.
auto putInBounds = [ptrGrid, width, height, bm](uint8_t* ptr, uint8_t const* cellPtr) { auto putInBounds = [ptrGrid, width, height, bm](uint8_t* ptr, uint8_t const* cellPtr) {
if (ptr < ptrGrid) if (ptr < ptrGrid)
ptr += width * (bm == GOL::BorderManagement::mirror) ? 2 : height; ptr += width * (bm == GOL::BorderManagement::mirror) ? 2 : (height - 1);
else if (ptr > ptrGrid + (width * height)) else if (ptr > ptrGrid + (width * height))
ptr -= width * (bm == GOL::BorderManagement::mirror) ? 2 : height; ptr -= width * (bm == GOL::BorderManagement::mirror) ? 2 : (height - 1);
if ((cellPtr - ptrGrid) % width == 0) if ((cellPtr - ptrGrid) % width == 0)
ptr += (bm == GOL::BorderManagement::mirror) ? 2 : width; ptr += (bm == GOL::BorderManagement::mirror) ? 2 : (width - 1);
else if ((cellPtr - ptrGrid) % width == width - 1) else if ((cellPtr - ptrGrid) % width == width - 1)
ptr -= (bm == GOL::BorderManagement::mirror) ? 2 : width; ptr -= (bm == GOL::BorderManagement::mirror) ? 2 : (width - 1);
return ptr; return ptr;
}; };
@ -707,14 +711,12 @@ size_t GOLTeamH::countNeighbors(uint8_t* ptrGrid, BorderManagement bm) const
tempPtr++; tempPtr++;
neighborsAliveCount += *(putInBounds(tempPtr, ptrGrid)); neighborsAliveCount += *(putInBounds(tempPtr, ptrGrid));
// Milieu
tempPtr += (width - 2); tempPtr += (width - 2);
neighborsAliveCount += *(putInBounds(tempPtr, ptrGrid)); neighborsAliveCount += *(putInBounds(tempPtr, ptrGrid));
tempPtr += 2; tempPtr += 2;
neighborsAliveCount += *(putInBounds(tempPtr, ptrGrid)); neighborsAliveCount += *(putInBounds(tempPtr, ptrGrid));
// Dessous
tempPtr += (width - 2); tempPtr += (width - 2);
neighborsAliveCount += *(putInBounds(tempPtr, ptrGrid)); neighborsAliveCount += *(putInBounds(tempPtr, ptrGrid));
tempPtr++; tempPtr++;