/
FaceNode.h
306 lines (246 loc) · 8.96 KB
/
FaceNode.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#pragma once
#include "ibrush.h"
#include "NodeBase.h"
#include "math/Matrix3.h"
#include "SelectableVertex.h"
namespace textool
{
class FaceNode :
public NodeBase,
public IFaceNode
{
private:
IFace& _face;
mutable AABB _bounds;
public:
FaceNode(IFace& face) :
_face(face)
{
for (auto& vertex : _face.getWinding())
{
_vertices.emplace_back(vertex.vertex, vertex.texcoord);
}
}
IFace& getFace() override
{
return _face;
}
void beginTransformation() override
{
_face.undoSave();
}
void revertTransformation() override
{
_face.revertTransform();
}
void transform(const Matrix3& transform) override
{
for (auto& vertex : _face.getWinding())
{
vertex.texcoord = transform * vertex.texcoord;
}
Vector3 vertices[3] = { _face.getWinding().at(0).vertex, _face.getWinding().at(1).vertex, _face.getWinding().at(2).vertex };
Vector2 texcoords[3] = { _face.getWinding().at(0).texcoord, _face.getWinding().at(1).texcoord, _face.getWinding().at(2).texcoord };
_face.setTexDefFromPoints(vertices, texcoords);
}
void transformComponents(const Matrix3& transform) override
{
transformSelectedAndRecalculateTexDef([&](Vector2& selectedTexcoord)
{
selectedTexcoord = transform * selectedTexcoord;
});
}
void commitTransformation() override
{
_face.freezeTransform();
}
const AABB& localAABB() const
{
_bounds = AABB();
for (const auto& vertex : _face.getWinding())
{
_bounds.includePoint({ vertex.texcoord.x(), vertex.texcoord.y(), 0 });
}
return _bounds;
}
void testSelect(Selector& selector, SelectionTest& test) override
{
// Arrange the UV coordinates in a Vector3 array for testing
std::vector<Vector3> uvs;
uvs.reserve(_face.getWinding().size());
for (const auto& vertex : _face.getWinding())
{
uvs.emplace_back(vertex.texcoord.x(), vertex.texcoord.y(), 0);
}
test.BeginMesh(Matrix4::getIdentity(), true);
SelectionIntersection best;
test.TestPolygon(VertexPointer(uvs.data(), sizeof(Vector3)), uvs.size(), best);
if (best.isValid())
{
Selector_add(selector, *this);
}
}
void render(SelectionMode mode) override
{
glEnable(GL_BLEND);
glBlendColor(0, 0, 0, 0.3f);
glBlendFunc(GL_CONSTANT_ALPHA_EXT, GL_ONE_MINUS_CONSTANT_ALPHA_EXT);
if (mode == SelectionMode::Surface && isSelected())
{
glColor3f(1, 0.5f, 0);
}
else if (mode == SelectionMode::Vertex)
{
glColor3f(0.6f, 0.6f, 0.6f);
}
else
{
glColor3f(0.8f, 0.8f, 0.8f);
}
glBegin(GL_TRIANGLE_FAN);
for (const auto& vertex : _face.getWinding())
{
glVertex2d(vertex.texcoord[0], vertex.texcoord[1]);
}
glEnd();
glDisable(GL_BLEND);
if (mode == SelectionMode::Vertex)
{
renderComponents();
}
}
void expandSelectionToRelated() override
{
if (!isSelected())
{
return;
}
// Expand the selection to all faces with the same brush
auto& brush = _face.getBrush();
GlobalTextureToolSceneGraph().foreachNode([&](const INode::Ptr& node)
{
auto face = std::dynamic_pointer_cast<FaceNode>(node);
if (face && &(face->getFace().getBrush()) == &brush)
{
face->setSelected(true);
}
return true;
});
}
void snapto(float snap) override
{
for (auto& vertex : _vertices)
{
auto& texcoord = vertex.getTexcoord();
texcoord.x() = float_snapped(texcoord.x(), snap);
texcoord.y() = float_snapped(texcoord.y(), snap);
}
// Take three vertices and calculate a new tex def
Vector3 vertices[3];
Vector2 texcoords[3];
for (std::size_t i = 0; i < 3; ++i)
{
vertices[i] = _vertices[i].getVertex();
texcoords[i] = _vertices[i].getTexcoord();
}
_face.setTexDefFromPoints(vertices, texcoords);
}
void snapComponents(float snap) override
{
transformSelectedAndRecalculateTexDef([&](Vector2& selectedTexcoord)
{
// Snap the selection to the grid
selectedTexcoord.x() = float_snapped(selectedTexcoord.x(), snap);
selectedTexcoord.y() = float_snapped(selectedTexcoord.y(), snap);
});
}
private:
void transformSelectedAndRecalculateTexDef(const std::function<void(Vector2&)>& transform)
{
std::vector<std::size_t> selectedIndices;
// We need to remember the old texture coordinates to determine the fixed points
std::vector<Vector2> unchangedTexcoords;
AABB selectionBounds;
// Manipulate every selected vertex using the given transform
for (std::size_t i = 0; i < _vertices.size(); ++i)
{
auto& vertex = _vertices[i];
unchangedTexcoords.push_back(vertex.getTexcoord());
if (!vertex.isSelected()) continue;
selectionBounds.includePoint({ vertex.getTexcoord().x(), vertex.getTexcoord().y(), 0 });
selectedIndices.push_back(i);
// Apply the transform to the selected texcoord
transform(vertex.getTexcoord());
}
if (selectedIndices.empty()) return; // nothing happened
// Now we need to pick three vertices to calculate the tex def from
// we have certain options, depending on the number of selected vertices
auto selectionCount = selectedIndices.size();
Vector3 vertices[3];
Vector2 texcoords[3];
const auto& winding = _face.getWinding();
if (selectionCount >= 3)
{
// Manipulating 3+ vertices means that the whole face is transformed
// the same way. We can pick any of the three selected vertices.
for (std::size_t i = 0; i < 3; ++i)
{
vertices[i] = _vertices[selectedIndices[i]].getVertex();
texcoords[i] = _vertices[selectedIndices[i]].getTexcoord();
}
}
else if (selectionCount == 2)
{
// Calculate the center point of the selection and pick the vertex that is farthest from it
auto farthestIndex = findIndexFarthestFrom(
{ selectionBounds.origin.x(), selectionBounds.origin.y() },
unchangedTexcoords, selectedIndices);
for (std::size_t i = 0; i < 2; ++i)
{
vertices[i] = _vertices[selectedIndices[i]].getVertex();
texcoords[i] = _vertices[selectedIndices[i]].getTexcoord();
}
vertices[2] = _vertices[farthestIndex].getVertex();
texcoords[2] = _vertices[farthestIndex].getTexcoord();
}
else // selectionCount == 1
{
assert(selectionCount == 1);
std::vector<std::size_t> fixedVerts{ selectedIndices[0] };
auto secondIndex = findIndexFarthestFrom(unchangedTexcoords[selectedIndices[0]],
unchangedTexcoords, fixedVerts);
fixedVerts.push_back(secondIndex);
// Now we've got two vertices, calculate the center and take the farthest of that one
auto center = (unchangedTexcoords[secondIndex] + unchangedTexcoords[selectedIndices[0]]) * 0.5;
auto thirdIndex = findIndexFarthestFrom(center, unchangedTexcoords, fixedVerts);
fixedVerts.push_back(thirdIndex);
for (std::size_t i = 0; i < 3; ++i)
{
vertices[i] = _vertices[fixedVerts[i]].getVertex();
texcoords[i] = _vertices[fixedVerts[i]].getTexcoord();
}
}
_face.setTexDefFromPoints(vertices, texcoords);
}
// Locates the index of the vertex that is farthest away from the given texcoord
// the indices contained in exludedIndices are not returned
static std::size_t findIndexFarthestFrom(const Vector2& texcoord,
const std::vector<Vector2>& allCoords, const std::vector<std::size_t>& excludedIndices)
{
assert(!allCoords.empty());
std::size_t farthestIndex = 0;
double largestDistanceSquared = 0;
for (std::size_t i = 0; i < allCoords.size(); ++i)
{
if (std::find(excludedIndices.begin(), excludedIndices.end(), i) != excludedIndices.end()) continue;
auto candidateDistanceSquared = (allCoords[i] - texcoord).getLengthSquared();
if (candidateDistanceSquared > largestDistanceSquared)
{
farthestIndex = i;
largestDistanceSquared = candidateDistanceSquared;
}
}
return farthestIndex;
}
};
}