From 12de8dce0b5d0634285ec91788eb5173bfc57337 Mon Sep 17 00:00:00 2001 From: Pablo Garzon Date: Fri, 9 Jun 2023 13:34:39 +0200 Subject: [PATCH] #3507 Undo and Redo Meshes --- .../src/bbcreaVtkCreateMeshFromPoints.cxx | 78 ++++++++++-------- lib/creaVtk/MeshManagerModel.cpp | 81 ++++++++++++------- 2 files changed, 100 insertions(+), 59 deletions(-) diff --git a/bbtk_creaVtk_PKG/src/bbcreaVtkCreateMeshFromPoints.cxx b/bbtk_creaVtk_PKG/src/bbcreaVtkCreateMeshFromPoints.cxx index 7431bad..abee3e6 100644 --- a/bbtk_creaVtk_PKG/src/bbcreaVtkCreateMeshFromPoints.cxx +++ b/bbtk_creaVtk_PKG/src/bbcreaVtkCreateMeshFromPoints.cxx @@ -62,39 +62,53 @@ void CreateMeshFromPoints::Process() pointsCentroid[2] /= sizeLstX; if ((bbGetInputCloseSurface()==true) && (lstIndexs.size()>=2) ) - { + { + //check if initial and end points are the same in all splines (splines are in line shape and not U shape) + //to not apply changes because clean polydata will take care of it. + int count = 0; + bool differentPoints = false; + for(i=0; i < lstIndexs.size() && !differentPoints;i++){ + bool sameStart = lstX[count] != lstX[count+lstIndexs[i]-1] && lstY[count] != lstY[count+lstIndexs[i]-1] && lstZ[count] != lstZ[count+lstIndexs[i]-1]; + bool sameEnd = lstX[count] != lstX[count+lstIndexs[i]-1] && lstY[count] != lstY[count+lstIndexs[i]-1] && lstZ[count] != lstZ[count+lstIndexs[i]-1]; + if(!(sameStart && sameEnd)) differentPoints = true; + count += lstIndexs[i]; + } //Correct surface normals if needed - double pointSurf1[3], pointSurf2[3], pointSurf3[3]; - double vect1[3], vect2[3]; - double surfNormal[3], vectorCenter[3]; - double dotNormalSurf = 0; - for(int pIndex = 0; pIndex < lstIndexs[0]-1; pIndex++) - { - pointSurf1[0] = lstX[pIndex]; - pointSurf1[1] = lstY[pIndex]; - pointSurf1[2] = lstZ[pIndex]; - vtkMath::Subtract(pointsCentroid, pointSurf1, vectorCenter); - pointSurf2[0] = lstX[pIndex+lstIndexs[1]]; - pointSurf2[1] = lstY[pIndex+lstIndexs[1]]; - pointSurf2[2] = lstZ[pIndex+lstIndexs[1]]; - pointSurf3[0] = lstX[pIndex+1]; - pointSurf3[1] = lstY[pIndex+1]; - pointSurf3[2] = lstZ[pIndex+1]; - vtkMath::Subtract(pointSurf2, pointSurf1, vect1); - vtkMath::Subtract(pointSurf3, pointSurf1, vect2); - vtkMath::Cross(vect1, vect2, surfNormal); - dotNormalSurf += vtkMath::Dot(surfNormal, vectorCenter); - } // for pIndex - if(dotNormalSurf > 0){ - points->Delete(); - points = vtkPoints::New(); - for(int splineI = 0; splineI < lstIndexs.size(); splineI++){ - for (i=lstIndexs[splineI]-1; i >= 0;i--) - { - points->InsertNextPoint(lstX[splineI*lstIndexs[0]+i],lstY[splineI*lstIndexs[0]+i],lstZ[splineI*lstIndexs[0]+i]); - } // for i - } // for splineI - } // if dotNormalSurf + if(differentPoints) + { + double pointSurf1[3], pointSurf2[3], pointSurf3[3]; + double vect1[3], vect2[3]; + double surfNormal[3], vectorCenter[3]; + double dotNormalSurf = 0; + for(int pIndex = 0; pIndex < lstIndexs[0]-1; pIndex++) + { + pointSurf1[0] = lstX[pIndex]; + pointSurf1[1] = lstY[pIndex]; + pointSurf1[2] = lstZ[pIndex]; + vtkMath::Subtract(pointsCentroid, pointSurf1, vectorCenter); + pointSurf2[0] = lstX[pIndex+lstIndexs[1]]; + pointSurf2[1] = lstY[pIndex+lstIndexs[1]]; + pointSurf2[2] = lstZ[pIndex+lstIndexs[1]]; + pointSurf3[0] = lstX[pIndex+1]; + pointSurf3[1] = lstY[pIndex+1]; + pointSurf3[2] = lstZ[pIndex+1]; + vtkMath::Subtract(pointSurf2, pointSurf1, vect1); + vtkMath::Subtract(pointSurf3, pointSurf1, vect2); + vtkMath::Cross(vect1, vect2, surfNormal); + dotNormalSurf += vtkMath::Dot(surfNormal, vectorCenter); + } // for pIndex + + if(dotNormalSurf > 0){ + points->Delete(); + points = vtkPoints::New(); + for(int splineI = 0; splineI < lstIndexs.size(); splineI++){ + for (i=lstIndexs[splineI]-1; i >= 0;i--) + { + points->InsertNextPoint(lstX[splineI*lstIndexs[0]+i],lstY[splineI*lstIndexs[0]+i],lstZ[splineI*lstIndexs[0]+i]); + } // for i + } // for splineI + } // if dotNormalSurf + }// if differentPoints } // diff --git a/lib/creaVtk/MeshManagerModel.cpp b/lib/creaVtk/MeshManagerModel.cpp index 6af6e93..41b9770 100644 --- a/lib/creaVtk/MeshManagerModel.cpp +++ b/lib/creaVtk/MeshManagerModel.cpp @@ -88,6 +88,11 @@ template void HistoryHandler::Save(T* state) { undoStack.push_back(state); + if(undoStack.size() > maxElements) + { + delete undoStack.front(); + undoStack.pop_front(); + } if(!redoStack.empty()) { for (T* element : redoStack) @@ -101,7 +106,8 @@ void HistoryHandler::Save(T* state) template T* HistoryHandler::GetPrevious() { - if(!undoStack.empty()){ + if(!undoStack.empty()) + { return undoStack.back(); } return NULL; @@ -110,7 +116,8 @@ T* HistoryHandler::GetPrevious() template T* HistoryHandler::GetNext() { - if(!redoStack.empty()){ + if(!redoStack.empty()) + { return redoStack.back(); } return NULL; @@ -142,7 +149,8 @@ MeshModel::MeshModel(int id) MeshModel::MeshModel(vtkPolyData* mesh, int id) { - if(mesh != NULL){ + if(mesh != NULL) + { _meshBase = vtkPolyData::New();//mesh; _meshBase->DeepCopy(mesh); _meshTemp = vtkPolyData::New(); @@ -166,11 +174,14 @@ MeshModel::MeshModel(MeshModel* meshModel) _name = "mesh-" + std::to_string(meshModel->GetId()); } -MeshModel::~MeshModel(){ - if(_meshBase != NULL){ +MeshModel::~MeshModel() +{ + if(_meshBase != NULL) + { _meshBase->Delete(); } - if(_meshTemp != NULL){ + if(_meshTemp != NULL) + { _meshTemp->Delete(); } } @@ -198,7 +209,8 @@ void MeshModel::SetMeshBase(vtkPolyData* mesh) { if (mesh!=NULL) { - if(_meshBase != NULL){ + if(_meshBase != NULL) + { _meshBase->Delete(); } _meshBase = vtkPolyData::New(); @@ -265,7 +277,7 @@ MeshManagerModel::MeshManagerModel() { currentMesh = 0; meshId = 0; - history = new HistoryHandler(20); + history = new HistoryHandler(15); lastModified = 0; } @@ -291,11 +303,11 @@ int MeshManagerModel::GetNumberOfMeshes() void MeshManagerModel::AddMesh_(vtkPolyData* mesh) { - if(mesh != NULL){ + if(mesh != NULL) + { _meshes.push_back(std::make_shared(mesh, meshId)); meshId++; - } - else{ + }else{ printf("PG MeshManagerModel::AddMesh Mesh is null \n"); } } @@ -370,10 +382,12 @@ void MeshManagerModel::InsertMeshesAtCurrent(std::vector meshList) void MeshManagerModel::SelectMesh(int i) { - if(i >= 0 && i < _meshes.size()){ + if(i >= 0 && i < _meshes.size()) + { int prevCurrent = currentMesh; currentMesh = i; - if(prevCurrent != i){ + if(prevCurrent != i) + { RefreshOutputs(true); } } @@ -384,10 +398,12 @@ void MeshManagerModel::SelectMesh(int i) void MeshManagerModel::SelectMeshByName(std::string meshName) { - if(!_meshes.empty()){ + if(!_meshes.empty()) + { bool found = false; for(int i = 0; i < _meshes.size() && !found; i++){ - if(_meshes.at(i)->GetName() == meshName){ + if(_meshes.at(i)->GetName() == meshName) + { found = true; SelectMesh(i); } @@ -397,10 +413,12 @@ void MeshManagerModel::SelectMeshByName(std::string meshName) void MeshManagerModel::DeleteMesh_(int position) { - if(position >= 0 && position < _meshes.size()){ + if(position >= 0 && position < _meshes.size()) + { _meshes.erase(_meshes.begin() + position); currentMesh = currentMesh + (position <= currentMesh?-1:0); - if(currentMesh < 0){ + if(currentMesh < 0) + { currentMesh = 0; } } @@ -416,10 +434,12 @@ void MeshManagerModel::DeleteMesh(int position) void MeshManagerModel::DeleteMeshByName(std::string meshName) { - if(!_meshes.empty()){ + if(!_meshes.empty()) + { bool found = false; for(int i = 0; i < _meshes.size() && !found; i++){ - if(_meshes.at(i)->GetName() == meshName){ + if(_meshes.at(i)->GetName() == meshName) + { found = true; DeleteMesh_(i); RefreshOutputs(true); @@ -430,7 +450,8 @@ void MeshManagerModel::DeleteMeshByName(std::string meshName) void MeshManagerModel::DeleteCurrentMesh() { - if(!_meshes.empty()){ + if(!_meshes.empty()) + { Save(); DeleteMesh_(currentMesh); lastModified = currentMesh; @@ -471,7 +492,8 @@ void MeshManagerModel::DeleteAll() void MeshManagerModel::NextMesh() { currentMesh++; - if(currentMesh >= _meshes.size()){ + if(currentMesh >= _meshes.size()) + { currentMesh = _meshes.size()-1; } RefreshOutputs(true); @@ -480,7 +502,8 @@ void MeshManagerModel::NextMesh() void MeshManagerModel::PreviousMesh() { currentMesh--; - if(currentMesh < 0){ + if(currentMesh < 0) + { currentMesh = 0; } RefreshOutputs(true); @@ -493,7 +516,8 @@ std::shared_ptr MeshManagerModel::GetMeshModel() vtkPolyData* MeshManagerModel::GetMeshBase() { - if(!_meshes.empty()){ + if(!_meshes.empty()) + { return _meshes.at(currentMesh)->GetMeshBase(); } else{ @@ -503,7 +527,8 @@ vtkPolyData* MeshManagerModel::GetMeshBase() vtkPolyData* MeshManagerModel::GetMeshTemp() { - if(!_meshes.empty()){ + if(!_meshes.empty()) + { return _meshes.at(currentMesh)->GetMeshTemp(); } else{ @@ -513,7 +538,8 @@ vtkPolyData* MeshManagerModel::GetMeshTemp() void MeshManagerModel::SetMeshBase(vtkPolyData* mesh) { - if(!_meshes.empty()){ + if(!_meshes.empty()) + { Save(); _meshes.at(currentMesh) = std::make_shared(_meshes.at(currentMesh).get()); _meshes.at(currentMesh)->SetMeshBase(mesh); @@ -589,7 +615,8 @@ int MeshManagerModel::GetCurrentMesh() void MeshManagerModel::Undo() { - if(history->UndoSize() > 0){ + if(history->UndoSize() > 0) + { RestoreState(history->Undo(new ManagerState(_meshes, meshId, lastModified))); RefreshOutputs(true); } @@ -618,7 +645,6 @@ void MeshManagerModel::RestoreState(ManagerState* state) meshId = state->GetMeshId(); currentMesh = state->GetModifiedPos(); lastModified = state->GetModifiedPos(); -// cout << "PG MeshManagerModel::RestoreState currentMesh: " << currentMesh << endl; } } @@ -634,6 +660,7 @@ MeshManagerModel::ManagerState::ManagerState(std::vector>& MeshManagerModel::ManagerState::GetMeshes() -- 2.45.1