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 "SnapBrushVerticesCommand.h"
21 
22 #include "Model/Brush.h"
23 #include "Model/BrushGeometry.h"
24 #include "Model/Snapshot.h"
25 #include "View/MapDocument.h"
26 #include "View/MapDocumentCommandFacade.h"
27 #include "View/VertexHandleManager.h"
28 
29 namespace TrenchBroom {
30     namespace View {
31         const Command::CommandType SnapBrushVerticesCommand::Type = Command::freeType();
32 
snap(const Model::VertexToBrushesMap & vertices,const size_t snapTo)33         SnapBrushVerticesCommand::Ptr SnapBrushVerticesCommand::snap(const Model::VertexToBrushesMap& vertices, const size_t snapTo) {
34             Model::BrushList brushes;
35             Model::BrushVerticesMap brushVertices;
36             Vec3::List vertexPositions;
37             extractVertexMap(vertices, brushes, brushVertices, vertexPositions);
38 
39             return Ptr(new SnapBrushVerticesCommand(brushes, brushVertices, vertexPositions, snapTo));
40         }
41 
snap(const Model::BrushList & brushes,const size_t snapTo)42         SnapBrushVerticesCommand::Ptr SnapBrushVerticesCommand::snap(const Model::BrushList& brushes, const size_t snapTo) {
43             Model::BrushVerticesMap brushVertices;
44             Vec3::List vertexPositions;
45 
46             Model::BrushList::const_iterator bIt, bEnd;
47             Model::Brush::VertexList::const_iterator vIt, vEnd;
48 
49             for (bIt = brushes.begin(), bEnd = brushes.end(); bIt != bEnd; ++bIt) {
50                 Model::Brush* brush = *bIt;
51                 const Model::Brush::VertexList vertices = brush->vertices();
52                 for (vIt = vertices.begin(), vEnd = vertices.end(); vIt != vEnd; ++vIt) {
53                     const Model::BrushVertex* vertex = *vIt;
54                     brushVertices[brush].push_back(vertex->position());
55                     vertexPositions.push_back(vertex->position());
56                 }
57             }
58 
59             return Ptr(new SnapBrushVerticesCommand(brushes, brushVertices, vertexPositions, snapTo));
60         }
61 
SnapBrushVerticesCommand(const Model::BrushList & brushes,const Model::BrushVerticesMap & vertices,const Vec3::List & vertexPositions,const size_t snapTo)62         SnapBrushVerticesCommand::SnapBrushVerticesCommand(const Model::BrushList& brushes, const Model::BrushVerticesMap& vertices, const Vec3::List& vertexPositions, const size_t snapTo) :
63         VertexCommand(Type, "Snap Brush Vertices", brushes),
64         m_vertices(vertices),
65         m_oldVertexPositions(vertexPositions),
66         m_snapTo(snapTo) {}
67 
doCanDoVertexOperation(const MapDocument * document) const68         bool SnapBrushVerticesCommand::doCanDoVertexOperation(const MapDocument* document) const {
69             return true;
70         }
71 
doVertexOperation(MapDocumentCommandFacade * document)72         bool SnapBrushVerticesCommand::doVertexOperation(MapDocumentCommandFacade* document) {
73             m_newVertexPositions = document->performSnapVertices(m_vertices, m_snapTo);
74             return true;
75         }
76 
doSelectNewHandlePositions(VertexHandleManager & manager,const Model::BrushList & brushes)77         void SnapBrushVerticesCommand::doSelectNewHandlePositions(VertexHandleManager& manager, const Model::BrushList& brushes) {
78             const Model::BrushSet brushSet(brushes.begin(), brushes.end());
79             manager.reselectVertexHandles(brushSet, m_newVertexPositions, 0.01);
80         }
81 
doSelectOldHandlePositions(VertexHandleManager & manager,const Model::BrushList & brushes)82         void SnapBrushVerticesCommand::doSelectOldHandlePositions(VertexHandleManager& manager, const Model::BrushList& brushes) {
83             manager.selectVertexHandles(m_oldVertexPositions);
84         }
85 
doCollateWith(UndoableCommand::Ptr command)86         bool SnapBrushVerticesCommand::doCollateWith(UndoableCommand::Ptr command) {
87             return false;
88         }
89     }
90 }
91