Skip to content

Commit

Permalink
calculation of mass center corrected
Browse files Browse the repository at this point in the history
  • Loading branch information
chrxh committed Nov 4, 2024
1 parent 45fd846 commit 2bf93b5
Show file tree
Hide file tree
Showing 14 changed files with 55 additions and 28 deletions.
25 changes: 21 additions & 4 deletions source/EngineGpuKernels/EditKernels.cu
Original file line number Diff line number Diff line change
Expand Up @@ -239,17 +239,20 @@ __global__ void cudaUpdateAngleAndAngularVelForSelection(ShallowUpdateSelectionD
}
}

__global__ void cudaCalcAccumulatedCenterAndVel(SimulationData data, float2* center, float2* velocity, int* numEntities, bool includeClusters)
__global__ void cudaCalcAccumulatedCenterAndVel(SimulationData data, int refCellIndex, float2* center, float2* velocity, int* numEntities, bool includeClusters)
{
{
float2 refPos = refCellIndex != -1 ? data.objects.cellPointers.at(refCellIndex)->pos : float2{0, 0};

auto const partition = calcAllThreadsPartition(data.objects.cellPointers.getNumEntries());

for (int index = partition.startIndex; index <= partition.endIndex; ++index) {
auto const& cell = data.objects.cellPointers.at(index);
if (isSelected(cell, includeClusters)) {
if (center) {
atomicAdd(&center->x, cell->pos.x);
atomicAdd(&center->y, cell->pos.y);
auto pos = cell->pos + data.cellMap.getCorrectionIncrement(refPos, cell->pos);
atomicAdd(&center->x, pos.x);
atomicAdd(&center->y, pos.y);
}
if (velocity) {
atomicAdd(&velocity->x, cell->vel.x);
Expand Down Expand Up @@ -574,8 +577,22 @@ __global__ void cudaResetSelectionResult(SelectionResult result)
result.reset();
}

__global__ void cudaGetSelectionShallowData(SimulationData data, float2 refPos, SelectionResult result)
__global__ void cudaCalcCellWithMinimalPosY(SimulationData data, unsigned long long int* minCellPosYAndIndex)
{
auto const cellBlock = calcAllThreadsPartition(data.objects.cellPointers.getNumEntries());

for (int index = cellBlock.startIndex; index <= cellBlock.endIndex; ++index) {
auto const& cell = data.objects.cellPointers.at(index);
if (0 != cell->selected) {
atomicMin(minCellPosYAndIndex, (static_cast<unsigned long long int>(abs(cell->pos.y)) << 32) | static_cast<unsigned long long int>(index));
}
}
}

__global__ void cudaGetSelectionShallowData(SimulationData data, int refCellIndex, SelectionResult result)
{
float2 refPos = refCellIndex != 0xffffffff ? data.objects.cellPointers.at(refCellIndex)->pos : float2{0, 0};

auto const cellBlock = calcAllThreadsPartition(data.objects.cellPointers.getNumEntries());

for (int index = cellBlock.startIndex; index <= cellBlock.endIndex; ++index) {
Expand Down
5 changes: 3 additions & 2 deletions source/EngineGpuKernels/EditKernels.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ __global__ void cudaScheduleConnectSelection(SimulationData data, bool considerW
__global__ void cudaPrepareMapForReconnection(SimulationData data);
__global__ void cudaUpdateMapForReconnection(SimulationData data);
__global__ void cudaUpdateAngleAndAngularVelForSelection(ShallowUpdateSelectionData updateData, SimulationData data, float2 center);
__global__ void cudaCalcAccumulatedCenterAndVel(SimulationData data, float2* center, float2* velocity, int* numEntities, bool includeClusters);
__global__ void cudaCalcAccumulatedCenterAndVel(SimulationData data, int refCellIndex, float2* center, float2* velocity, int* numEntities, bool includeClusters);
__global__ void cudaIncrementPosAndVelForSelection(ShallowUpdateSelectionData updateData, SimulationData data);
__global__ void cudaSetVelocityForSelection(SimulationData data, float2 velocity, bool includeClusters);
__global__ void cudaMakeSticky(SimulationData data, bool includeClusters);
Expand All @@ -47,7 +47,8 @@ __global__ void cudaSwapSelection(float2 pos, float radius, SimulationData data)
__global__ void cudaRolloutSelectionStep(SimulationData data, int* result);
__global__ void cudaApplyForce(SimulationData data, ApplyForceData applyData);
__global__ void cudaResetSelectionResult(SelectionResult result);
__global__ void cudaGetSelectionShallowData(SimulationData data, float2 refPos, SelectionResult result);
__global__ void cudaCalcCellWithMinimalPosY(SimulationData data, unsigned long long int* minCellPosYAndIndex);
__global__ void cudaGetSelectionShallowData(SimulationData data, int refCellIndex, SelectionResult result);
__global__ void cudaFinalizeSelectionResult(SelectionResult result, BaseMap map);
__global__ void cudaSetDetached(SimulationData data, bool value);
__global__ void cudaApplyCataclysm(SimulationData data);
24 changes: 16 additions & 8 deletions source/EngineGpuKernels/EditKernelsLauncher.cu
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ _EditKernelsLauncher::_EditKernelsLauncher()
CudaMemoryManager::getInstance().acquireMemory<float2>(1, _cudaCenter);
CudaMemoryManager::getInstance().acquireMemory<float2>(1, _cudaVelocity);
CudaMemoryManager::getInstance().acquireMemory<int>(1, _cudaNumEntities);
CudaMemoryManager::getInstance().acquireMemory<unsigned long long int>(1, _cudaMinCellPosYAndIndex);
_garbageCollector = std::make_shared<_GarbageCollectorKernelsLauncher>();
}

Expand All @@ -25,6 +26,7 @@ _EditKernelsLauncher::~_EditKernelsLauncher()
CudaMemoryManager::getInstance().freeMemory(_cudaCenter);
CudaMemoryManager::getInstance().freeMemory(_cudaVelocity);
CudaMemoryManager::getInstance().freeMemory(_cudaNumEntities);
CudaMemoryManager::getInstance().freeMemory(_cudaMinCellPosYAndIndex);
}

void _EditKernelsLauncher::removeSelection(GpuSettings const& gpuSettings, SimulationData const& data)
Expand Down Expand Up @@ -64,14 +66,14 @@ void _EditKernelsLauncher::updateSelection(GpuSettings const& gpuSettings, Simul
rolloutSelection(gpuSettings, data);
}

void _EditKernelsLauncher::getSelectionShallowData(
GpuSettings const& gpuSettings,
SimulationData const& data,
float2 const& refPos,
SelectionResult const& selectionResult)
void _EditKernelsLauncher::getSelectionShallowData(GpuSettings const& gpuSettings, SimulationData const& data, SelectionResult const& selectionResult)
{
KERNEL_CALL_1_1(cudaResetSelectionResult, selectionResult);
KERNEL_CALL(cudaGetSelectionShallowData, data, refPos, selectionResult);
setValueToDevice(_cudaMinCellPosYAndIndex, 0xffffffffffffffff);
KERNEL_CALL(cudaCalcCellWithMinimalPosY, data, _cudaMinCellPosYAndIndex);
cudaDeviceSynchronize();
auto refCellIndex = static_cast<int>(copyToHost(_cudaMinCellPosYAndIndex) & 0xffffffff);
KERNEL_CALL(cudaGetSelectionShallowData, data, refCellIndex, selectionResult);
KERNEL_CALL_1_1(cudaFinalizeSelectionResult, selectionResult, data.cellMap);
}

Expand Down Expand Up @@ -103,7 +105,13 @@ void _EditKernelsLauncher::shallowUpdateSelectedObjects(
if (updateData.angleDelta != 0 || updateData.angularVelDelta != 0) {
setValueToDevice(_cudaCenter, float2{0, 0});
setValueToDevice(_cudaNumEntities, 0);
KERNEL_CALL(cudaCalcAccumulatedCenterAndVel, data, _cudaCenter, nullptr, _cudaNumEntities, updateData.considerClusters);

setValueToDevice(_cudaMinCellPosYAndIndex, 0xffffffff00000000);
KERNEL_CALL(cudaCalcCellWithMinimalPosY, data, _cudaMinCellPosYAndIndex);
cudaDeviceSynchronize();
auto refCellIndex = static_cast<int>(copyToHost(_cudaMinCellPosYAndIndex) & 0xffffffff);

KERNEL_CALL(cudaCalcAccumulatedCenterAndVel, data, refCellIndex, _cudaCenter, nullptr, _cudaNumEntities, updateData.considerClusters);
cudaDeviceSynchronize();

auto numEntities = copyToHost(_cudaNumEntities);
Expand Down Expand Up @@ -158,7 +166,7 @@ void _EditKernelsLauncher::uniformVelocities(GpuSettings const& gpuSettings, Sim
{
setValueToDevice(_cudaVelocity, float2{0, 0});
setValueToDevice(_cudaNumEntities, 0);
KERNEL_CALL(cudaCalcAccumulatedCenterAndVel, data, nullptr, _cudaVelocity, _cudaNumEntities, includeClusters);
KERNEL_CALL(cudaCalcAccumulatedCenterAndVel, data, -1, nullptr, _cudaVelocity, _cudaNumEntities, includeClusters);
cudaDeviceSynchronize();

auto numEntities = copyToHost(_cudaNumEntities);
Expand Down
3 changes: 2 additions & 1 deletion source/EngineGpuKernels/EditKernelsLauncher.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public:
void setSelection(GpuSettings const& gpuSettings, SimulationData const& data, AreaSelectionData const& setData);
void updateSelection(GpuSettings const& gpuSettings, SimulationData const& data);

void getSelectionShallowData(GpuSettings const& gpuSettings, SimulationData const& data, float2 const& refPos, SelectionResult const& selectionResult);
void getSelectionShallowData(GpuSettings const& gpuSettings, SimulationData const& data, SelectionResult const& selectionResult);
void shallowUpdateSelectedObjects(
GpuSettings const& gpuSettings,
SimulationData const& data,
Expand Down Expand Up @@ -51,4 +51,5 @@ private:
float2* _cudaCenter;
float2* _cudaVelocity;
int* _cudaNumEntities;
unsigned long long int* _cudaMinCellPosYAndIndex;
};
4 changes: 2 additions & 2 deletions source/EngineGpuKernels/SimulationCudaFacade.cu
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,9 @@ void _SimulationCudaFacade::setSelection(AreaSelectionData const& selectionData)
_editKernels->setSelection(_settings.gpuSettings, getSimulationDataIntern(), selectionData);
}

SelectionShallowData _SimulationCudaFacade::getSelectionShallowData(float2 const& refPos)
SelectionShallowData _SimulationCudaFacade::getSelectionShallowData()
{
_editKernels->getSelectionShallowData(_settings.gpuSettings, getSimulationDataIntern(), refPos, * _cudaSelectionResult);
_editKernels->getSelectionShallowData(_settings.gpuSettings, getSimulationDataIntern(), *_cudaSelectionResult);
syncAndCheck();
return _cudaSelectionResult->getSelectionShallowData();
}
Expand Down
2 changes: 1 addition & 1 deletion source/EngineGpuKernels/SimulationCudaFacade.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public:
void switchSelection(PointSelectionData const& switchData);
void swapSelection(PointSelectionData const& selectionData);
void setSelection(AreaSelectionData const& selectionData);
SelectionShallowData getSelectionShallowData(float2 const& refPos);
SelectionShallowData getSelectionShallowData();
void shallowUpdateSelectedObjects(ShallowUpdateSelectionData const& shallowUpdateData);
void removeSelection();
void updateSelection();
Expand Down
4 changes: 2 additions & 2 deletions source/EngineImpl/EngineWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,10 +399,10 @@ void EngineWorker::swapSelection(RealVector2D const& pos, float radius)
_simulationCudaFacade->swapSelection(PointSelectionData{{pos.x, pos.y}, radius});
}

SelectionShallowData EngineWorker::getSelectionShallowData(RealVector2D const& refPos)
SelectionShallowData EngineWorker::getSelectionShallowData()
{
EngineWorkerGuard access(this);
return _simulationCudaFacade->getSelectionShallowData({refPos.x, refPos.y});
return _simulationCudaFacade->getSelectionShallowData();
}

void EngineWorker::setSelection(RealVector2D const& startPos, RealVector2D const& endPos)
Expand Down
2 changes: 1 addition & 1 deletion source/EngineImpl/EngineWorker.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class EngineWorker

void switchSelection(RealVector2D const& pos, float radius);
void swapSelection(RealVector2D const& pos, float radius);
SelectionShallowData getSelectionShallowData(RealVector2D const& refPos);
SelectionShallowData getSelectionShallowData();
void setSelection(RealVector2D const& startPos, RealVector2D const& endPos);
void removeSelection();
void updateSelection();
Expand Down
4 changes: 2 additions & 2 deletions source/EngineImpl/SimulationFacadeImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,9 @@ void _SimulationFacadeImpl::swapSelection(RealVector2D const& pos, float radius)
_worker.swapSelection(pos, radius);
}

SelectionShallowData _SimulationFacadeImpl::getSelectionShallowData(RealVector2D const& refPos)
SelectionShallowData _SimulationFacadeImpl::getSelectionShallowData()
{
return _worker.getSelectionShallowData(refPos);
return _worker.getSelectionShallowData();
}

void _SimulationFacadeImpl::shallowUpdateSelectedObjects(ShallowUpdateSelectionData const& updateData)
Expand Down
2 changes: 1 addition & 1 deletion source/EngineImpl/SimulationFacadeImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class _SimulationFacadeImpl : public _SimulationFacade

void switchSelection(RealVector2D const& pos, float radius) override;
void swapSelection(RealVector2D const& pos, float radius) override;
SelectionShallowData getSelectionShallowData(RealVector2D const& refPos = RealVector2D()) override;
SelectionShallowData getSelectionShallowData() override;
void shallowUpdateSelectedObjects(ShallowUpdateSelectionData const& updateData) override;
void setSelection(RealVector2D const& startPos, RealVector2D const& endPos) override;
void removeSelection() override;
Expand Down
2 changes: 1 addition & 1 deletion source/EngineInterface/SimulationFacade.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class _SimulationFacade

virtual void switchSelection(RealVector2D const& pos, float radius) = 0;
virtual void swapSelection(RealVector2D const& pos, float radius) = 0;
virtual SelectionShallowData getSelectionShallowData(RealVector2D const& refPos = RealVector2D()) = 0;
virtual SelectionShallowData getSelectionShallowData() = 0;
virtual void shallowUpdateSelectedObjects(ShallowUpdateSelectionData const& updateData) = 0;
virtual void setSelection(RealVector2D const& startPos, RealVector2D const& endPos) = 0;
virtual void removeSelection() = 0;
Expand Down
2 changes: 1 addition & 1 deletion source/Gui/EditorController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ void EditorController::onMoveSelectedObjects(

void EditorController::onFixateSelectedObjects(RealVector2D const& viewPos, RealVector2D const& prevWorldPos, RealVector2D const& selectionPositionOnClick)
{
auto shallowData = _simulationFacade->getSelectionShallowData(selectionPositionOnClick);
auto shallowData = _simulationFacade->getSelectionShallowData();
auto selectionPosition = RealVector2D{shallowData.centerPosX, shallowData.centerPosY};
auto selectionDelta = selectionPosition - selectionPositionOnClick;

Expand Down
2 changes: 1 addition & 1 deletion source/Gui/OverlayController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void OverlayController::processProgressAnimation()
ImColor::HSV(0.66f, 1.0f, 0.1f, 1.0f),
ImColor::HSV(0.66f, 1.0f, 0.1f, 0.0f),
ImColor::HSV(0.66f, 1.0f, 0.1f, 0.0f));
auto N = 6 + toInt(powf(sinf(duration / 500.0f - Const::Pi / 4) + 1, 3.0f) * 5);
auto N = 8 + toInt(powf(sinf(duration / 500.0f - Const::Pi / 4) + 1, 3.0f) * 5);
for (int i = 0; i < N; ++i) {
auto amplitude1 = sinf(toFloat(i) * 10.0f / toFloat(N) - duration / 700.0f * (1.0f + 0.3f * sinf(duration / 1000)));
auto amplitude2 = sinf(toFloat(i) * 14.0f / toFloat(N) - duration / 100.0f * (1.0f + 0.3f * cosf(duration / 1000)));
Expand Down
2 changes: 1 addition & 1 deletion source/Gui/SimulationInteractionController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ void SimulationInteractionController::leftMouseButtonPressed(IntVector2D const&
_simulationFacade->setDetached(true);
}

auto shallowData = _simulationFacade->getSelectionShallowData(*_worldPosOnClick);
auto shallowData = _simulationFacade->getSelectionShallowData();
_selectionPositionOnClick = {shallowData.centerPosX, shallowData.centerPosY};
} else {
CreatorWindow::get().onDrawing();
Expand Down

0 comments on commit 2bf93b5

Please sign in to comment.