1 /* 2 Copyright (C) 2010-2014 Kristian Duske 3 4 This file is part of TrenchBroom. 5 6 TrenchBroom is free software: you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 TrenchBroom is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with TrenchBroom. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #include "VertexCommand.h" 21 22 #include "Model/Brush.h" 23 #include "Model/BrushFace.h" 24 #include "Model/BrushGeometry.h" 25 #include "Model/Snapshot.h" 26 #include "View/MapDocumentCommandFacade.h" 27 #include "View/VertexHandleManager.h" 28 29 namespace TrenchBroom { 30 namespace View { VertexCommand(const CommandType type,const String & name,const Model::BrushList & brushes)31 VertexCommand::VertexCommand(const CommandType type, const String& name, const Model::BrushList& brushes) : 32 DocumentCommand(type, name), 33 m_brushes(brushes), 34 m_snapshot(NULL) {} 35 ~VertexCommand()36 VertexCommand::~VertexCommand() { 37 if (m_snapshot != NULL) 38 deleteSnapshot(); 39 } 40 extractVertexMap(const Model::VertexToBrushesMap & vertices,Model::BrushList & brushes,Model::BrushVerticesMap & brushVertices,Vec3::List & vertexPositions)41 void VertexCommand::extractVertexMap(const Model::VertexToBrushesMap& vertices, Model::BrushList& brushes, Model::BrushVerticesMap& brushVertices, Vec3::List& vertexPositions) { 42 43 typedef std::pair<Model::BrushVerticesMap::iterator, bool> BrushVerticesMapInsertResult; 44 Model::VertexToBrushesMap::const_iterator vIt, vEnd; 45 Model::BrushSet::const_iterator bIt, bEnd; 46 47 for (vIt = vertices.begin(), vEnd = vertices.end(); vIt != vEnd; ++vIt) { 48 const Vec3& position = vIt->first; 49 const Model::BrushSet& vertexBrushes = vIt->second; 50 for (bIt = vertexBrushes.begin(), bEnd = vertexBrushes.end(); bIt != bEnd; ++bIt) { 51 Model::Brush* brush = *bIt; 52 BrushVerticesMapInsertResult result = brushVertices.insert(std::make_pair(brush, Vec3::List())); 53 if (result.second) 54 brushes.push_back(brush); 55 result.first->second.push_back(position); 56 } 57 vertexPositions.push_back(position); 58 } 59 } 60 extractEdgeMap(const Model::VertexToEdgesMap & edges,Model::BrushList & brushes,Model::BrushEdgesMap & brushEdges,Edge3::List & edgePositions)61 void VertexCommand::extractEdgeMap(const Model::VertexToEdgesMap& edges, Model::BrushList& brushes, Model::BrushEdgesMap& brushEdges, Edge3::List& edgePositions) { 62 typedef std::pair<Model::BrushEdgesMap::iterator, bool> BrushEdgesMapInsertResult; 63 Model::VertexToEdgesMap::const_iterator vIt, vEnd; 64 for (vIt = edges.begin(), vEnd = edges.end(); vIt != vEnd; ++vIt) { 65 const Model::BrushEdgeSet& mappedEdges = vIt->second; 66 Model::BrushEdgeSet::const_iterator eIt, eEnd; 67 for (eIt = mappedEdges.begin(), eEnd = mappedEdges.end(); eIt != eEnd; ++eIt) { 68 Model::BrushEdge* edge = *eIt; 69 Model::Brush* brush = edge->firstFace()->payload()->brush(); 70 const Edge3 edgePosition(edge->firstVertex()->position(), edge->secondVertex()->position()); 71 72 BrushEdgesMapInsertResult result = brushEdges.insert(std::make_pair(brush, Edge3::List())); 73 if (result.second) 74 brushes.push_back(brush); 75 result.first->second.push_back(edgePosition); 76 edgePositions.push_back(edgePosition); 77 } 78 } 79 80 assert(!brushes.empty()); 81 assert(brushes.size() == brushEdges.size()); 82 } 83 extractFaceMap(const Model::VertexToFacesMap & faces,Model::BrushList & brushes,Model::BrushFacesMap & brushFaces,Polygon3::List & facePositions)84 void VertexCommand::extractFaceMap(const Model::VertexToFacesMap& faces, Model::BrushList& brushes, Model::BrushFacesMap& brushFaces, Polygon3::List& facePositions) { 85 typedef std::pair<Model::BrushFacesMap::iterator, bool> BrushFacesMapInsertResult; 86 Model::VertexToFacesMap::const_iterator vIt, vEnd; 87 for (vIt = faces.begin(), vEnd = faces.end(); vIt != vEnd; ++vIt) { 88 const Model::BrushFaceSet& mappedFaces = vIt->second; 89 Model::BrushFaceSet::const_iterator eIt, eEnd; 90 for (eIt = mappedFaces.begin(), eEnd = mappedFaces.end(); eIt != eEnd; ++eIt) { 91 Model::BrushFace* face = *eIt; 92 Model::Brush* brush = face->brush(); 93 const Polygon3 facePosition(Vec3::asList(face->vertices().begin(), face->vertices().end(), Model::BrushGeometry::GetVertexPosition())); 94 95 BrushFacesMapInsertResult result = brushFaces.insert(std::make_pair(brush, Polygon3::List())); 96 if (result.second) 97 brushes.push_back(brush); 98 result.first->second.push_back(facePosition); 99 facePositions.push_back(facePosition); 100 } 101 } 102 103 VectorUtils::sort(facePositions); 104 105 assert(!brushes.empty()); 106 assert(brushes.size() == brushFaces.size()); 107 } 108 doPerformDo(MapDocumentCommandFacade * document)109 bool VertexCommand::doPerformDo(MapDocumentCommandFacade* document) { 110 if (!doCanDoVertexOperation(document)) 111 return false; 112 113 takeSnapshot(); 114 return doVertexOperation(document); 115 } 116 doPerformUndo(MapDocumentCommandFacade * document)117 bool VertexCommand::doPerformUndo(MapDocumentCommandFacade* document) { 118 assert(m_snapshot != NULL); 119 document->restoreSnapshot(m_snapshot); 120 deleteSnapshot(); 121 return true; 122 } 123 doIsRepeatable(MapDocumentCommandFacade * document) const124 bool VertexCommand::doIsRepeatable(MapDocumentCommandFacade* document) const { 125 return false; 126 } 127 takeSnapshot()128 void VertexCommand::takeSnapshot() { 129 assert(m_snapshot == NULL); 130 m_snapshot = new Model::Snapshot(m_brushes.begin(), m_brushes.end()); 131 } 132 deleteSnapshot()133 void VertexCommand::deleteSnapshot() { 134 assert(m_snapshot != NULL); 135 delete m_snapshot; 136 m_snapshot = NULL; 137 } 138 removeBrushes(VertexHandleManager & manager)139 void VertexCommand::removeBrushes(VertexHandleManager& manager) { 140 manager.removeBrushes(m_brushes.begin(), m_brushes.end()); 141 } 142 addBrushes(VertexHandleManager & manager)143 void VertexCommand::addBrushes(VertexHandleManager& manager) { 144 manager.addBrushes(m_brushes.begin(), m_brushes.end()); 145 } 146 selectNewHandlePositions(VertexHandleManager & manager)147 void VertexCommand::selectNewHandlePositions(VertexHandleManager& manager) { 148 doSelectNewHandlePositions(manager, m_brushes); 149 } 150 selectOldHandlePositions(VertexHandleManager & manager)151 void VertexCommand::selectOldHandlePositions(VertexHandleManager& manager) { 152 doSelectOldHandlePositions(manager, m_brushes); 153 } 154 } 155 } 156