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 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
|
#include "FaceInstance.h"
#include "ifilter.h"
#include "ibrush.h"
#include "irenderable.h"
#include "iscenegraph.h"
#include "math/Frustum.h"
#include <functional>
inline bool triangle_reversed(std::size_t x, std::size_t y, std::size_t z) {
return !((x < y && y < z) || (z < x && x < y) || (y < z && z < x));
}
template<typename Element>
inline Vector3 triangle_cross(const BasicVector3<Element>& x, const BasicVector3<Element> y, const BasicVector3<Element>& z) {
return (y - x).cross(z - x);
}
template<typename Element>
inline bool triangles_same_winding(const BasicVector3<Element>& x1, const BasicVector3<Element> y1, const BasicVector3<Element>& z1,
const BasicVector3<Element>& x2, const BasicVector3<Element> y2, const BasicVector3<Element>& z2)
{
return triangle_cross(x1, y1, z1).dot(triangle_cross(x2, y2, z2)) > 0;
}
// -------------- FaceInstance implementation ---------------------------------------
// Static member definition
FaceInstanceSet FaceInstance::_selectedFaceInstances;
FaceInstance::FaceInstance(Face& face, const SelectionChangedSlot& observer) :
m_face(&face),
m_selectionChanged(observer),
m_selectable(std::bind(&FaceInstance::selectedChanged, this, std::placeholders::_1)),
m_selectableVertices(observer),
m_selectableEdges(observer)
{}
FaceInstance::FaceInstance(const FaceInstance& other) :
m_face(other.m_face),
m_selectionChanged(other.m_selectionChanged),
m_selectable(std::bind(&FaceInstance::selectedChanged, this, std::placeholders::_1)),
m_selectableVertices(other.m_selectableVertices),
m_selectableEdges(other.m_selectableEdges)
{}
FaceInstance& FaceInstance::operator=(const FaceInstance& other) {
m_face = other.m_face;
return *this;
}
Face& FaceInstance::getFace() {
return *m_face;
}
const Face& FaceInstance::getFace() const {
return *m_face;
}
void FaceInstance::selectedChanged(const ISelectable& selectable)
{
if (selectable.isSelected())
{
Selection().push_back(this);
}
else
{
FaceInstanceSet::reverse_iterator found = std::find(Selection().rbegin(), Selection().rend(), this);
// Emit an error if the instance is not in the list
ASSERT_MESSAGE(found != Selection().rend(), "selection-tracking error");
Selection().erase(--found.base());
}
if (m_selectionChanged)
{
m_selectionChanged(selectable);
}
}
bool FaceInstance::selectedVertices() const {
return !m_vertexSelection.empty();
}
bool FaceInstance::selectedEdges() const {
return !m_edgeSelection.empty();
}
bool FaceInstance::isSelected() const {
return m_selectable.isSelected();
}
bool FaceInstance::selectedComponents() const
{
return !m_vertexSelection.empty() || !m_edgeSelection.empty() || m_selectable.isSelected();
}
bool FaceInstance::selectedComponents(selection::ComponentSelectionMode mode) const
{
switch (mode)
{
case selection::ComponentSelectionMode::Vertex:
return selectedVertices();
case selection::ComponentSelectionMode::Edge:
return selectedEdges();
case selection::ComponentSelectionMode::Face:
return isSelected();
default:
return false;
}
}
void FaceInstance::setSelected(selection::ComponentSelectionMode mode, bool select)
{
switch (mode)
{
case selection::ComponentSelectionMode::Face:
m_selectable.setSelected(select);
break;
case selection::ComponentSelectionMode::Vertex:
ASSERT_MESSAGE(!select, "select-all not supported");
m_vertexSelection.clear();
m_selectableVertices.setSelected(false);
break;
case selection::ComponentSelectionMode::Edge:
ASSERT_MESSAGE(!select, "select-all not supported");
m_edgeSelection.clear();
m_selectableEdges.setSelected(false);
break;
default:
break;
}
}
void FaceInstance::invertSelected()
{
switch (GlobalSelectionSystem().ComponentMode())
{
case selection::ComponentSelectionMode::Face:
m_selectable.setSelected(!m_selectable.isSelected());
break;
case selection::ComponentSelectionMode::Vertex:
break;
case selection::ComponentSelectionMode::Edge:
break;
default:
break;
}
}
void FaceInstance::iterate_selected(AABB& aabb) const {
SelectedComponents_foreach(AABBExtendByPoint(aabb));
}
bool FaceInstance::intersectVolume(const VolumeTest& volume) const
{
return m_face->intersectVolume(volume);
}
bool FaceInstance::intersectVolume(const VolumeTest& volume, const Matrix4& localToWorld) const
{
return m_face->intersectVolume(volume, localToWorld);
}
void FaceInstance::testSelect(SelectionTest& test, SelectionIntersection& best) {
if (getFace().getFaceShader().getGLShader()->getMaterial()->isVisible()) {
m_face->testSelect(test, best);
}
}
void FaceInstance::testSelect(Selector& selector, SelectionTest& test) {
SelectionIntersection best;
testSelect(test, best);
if (best.isValid()) {
selector.addWithIntersection(m_selectable, best);
}
}
void FaceInstance::testSelect_centroid(Selector& selector, SelectionTest& test) {
if (m_face->contributes()) {
SelectionIntersection best;
m_face->testSelect_centroid(test, best);
if (best.isValid()) {
selector.addWithIntersection(m_selectable, best);
}
}
}
void FaceInstance::selectPlane(Selector& selector, const Line& line, PlanesIterator first, PlanesIterator last, const PlaneCallback& selectedPlaneCallback)
{
for (Winding::const_iterator i = getFace().getWinding().begin(); i != getFace().getWinding().end(); ++i) {
Vector3 v(line.getClosestPoint(i->vertex) - i->vertex);
auto dot = getFace().plane3().normal().dot(v);
if (dot <= 0) {
return;
}
}
selector.addWithNullIntersection(m_selectable);
selectedPlaneCallback(getFace().plane3());
}
void FaceInstance::selectReversedPlane(Selector& selector, const SelectedPlanes& selectedPlanes) {
if (selectedPlanes.contains(-(getFace().plane3()))) {
selector.addWithNullIntersection(m_selectable);
}
}
void FaceInstance::transformComponents(const Matrix4& matrix) {
if (isSelected()) {
m_face->transform(matrix);
}
if (selectedVertices())
{
if (m_vertexSelection.size() == 1)
{
m_face->m_move_planeptsTransformed[1] = matrix.transformPoint(m_face->m_move_planeptsTransformed[1]);
m_face->assign_planepts(m_face->m_move_planeptsTransformed);
}
else if (m_vertexSelection.size() == 2)
{
m_face->m_move_planeptsTransformed[1] = matrix.transformPoint(m_face->m_move_planeptsTransformed[1]);
m_face->m_move_planeptsTransformed[2] = matrix.transformPoint(m_face->m_move_planeptsTransformed[2]);
m_face->assign_planepts(m_face->m_move_planeptsTransformed);
}
else if (m_vertexSelection.size() >= 3)
{
m_face->m_move_planeptsTransformed[0] = matrix.transformPoint(m_face->m_move_planeptsTransformed[0]);
m_face->m_move_planeptsTransformed[1] = matrix.transformPoint(m_face->m_move_planeptsTransformed[1]);
m_face->m_move_planeptsTransformed[2] = matrix.transformPoint(m_face->m_move_planeptsTransformed[2]);
m_face->assign_planepts(m_face->m_move_planeptsTransformed);
}
}
if (selectedEdges())
{
if (m_edgeSelection.size() == 1)
{
m_face->m_move_planeptsTransformed[0] = matrix.transformPoint(m_face->m_move_planeptsTransformed[0]);
m_face->m_move_planeptsTransformed[1] = matrix.transformPoint(m_face->m_move_planeptsTransformed[1]);
m_face->assign_planepts(m_face->m_move_planeptsTransformed);
}
else if (m_edgeSelection.size() >= 2)
{
m_face->m_move_planeptsTransformed[0] = matrix.transformPoint(m_face->m_move_planeptsTransformed[0]);
m_face->m_move_planeptsTransformed[1] = matrix.transformPoint(m_face->m_move_planeptsTransformed[1]);
m_face->m_move_planeptsTransformed[2] = matrix.transformPoint(m_face->m_move_planeptsTransformed[2]);
m_face->assign_planepts(m_face->m_move_planeptsTransformed);
}
}
}
void FaceInstance::snapto(float snap) {
m_face->snapto(snap);
}
void FaceInstance::snapComponents(float snap) {
if (isSelected()) {
snapto(snap);
}
if (selectedVertices()) {
m_face->m_move_planepts[0].snap(snap);
m_face->m_move_planepts[1].snap(snap);
m_face->m_move_planepts[2].snap(snap);
m_face->assign_planepts(m_face->m_move_planepts);
planepts_assign(m_face->m_move_planeptsTransformed, m_face->m_move_planepts);
m_face->freezeTransform();
}
if (selectedEdges()) {
m_face->m_move_planepts[0].snap(snap);
m_face->m_move_planepts[1].snap(snap);
m_face->m_move_planepts[2].snap(snap);
m_face->assign_planepts(m_face->m_move_planepts);
planepts_assign(m_face->m_move_planeptsTransformed, m_face->m_move_planepts);
m_face->freezeTransform();
}
}
void FaceInstance::update_move_planepts_vertex(std::size_t index) {
m_face->update_move_planepts_vertex(index, m_face->m_move_planepts);
}
void FaceInstance::update_move_planepts_vertex2(std::size_t index, std::size_t other)
{
ASSERT_MESSAGE(index < m_face->getWinding().size(), "select_vertex: invalid index");
const std::size_t opposite = m_face->getWinding().opposite(index, other);
if (triangle_reversed(index, other, opposite)) {
std::swap(index, other);
}
ASSERT_MESSAGE(
triangles_same_winding(
m_face->getWinding()[opposite].vertex,
m_face->getWinding()[index].vertex,
m_face->getWinding()[other].vertex,
m_face->getWinding()[0].vertex,
m_face->getWinding()[1].vertex,
m_face->getWinding()[2].vertex
),
"update_move_planepts_vertex2: error"
)
m_face->m_move_planepts[0] = m_face->getWinding()[opposite].vertex;
m_face->m_move_planepts[1] = m_face->getWinding()[index].vertex;
m_face->m_move_planepts[2] = m_face->getWinding()[other].vertex;
planepts_quantise(m_face->m_move_planepts, GRID_MIN); // winding points are very inaccurate
}
void FaceInstance::update_selection_vertex() {
if (m_vertexSelection.size() == 0) {
m_selectableVertices.setSelected(false);
}
else {
m_selectableVertices.setSelected(true);
if (m_vertexSelection.size() == 1) {
std::size_t index = getFace().getWinding().findAdjacent(*m_vertexSelection.begin());
if (index != brush::c_brush_maxFaces) {
update_move_planepts_vertex(index);
}
}
else if (m_vertexSelection.size() == 2) {
std::size_t index = getFace().getWinding().findAdjacent(*m_vertexSelection.begin());
std::size_t other = getFace().getWinding().findAdjacent(*(++m_vertexSelection.begin()));
if (index != brush::c_brush_maxFaces
&& other != brush::c_brush_maxFaces) {
update_move_planepts_vertex2(index, other);
}
}
}
}
void FaceInstance::select_vertex(std::size_t index, bool select) {
if (select) {
VertexSelection_insert(m_vertexSelection, getFace().getWinding()[index].adjacent);
}
else {
VertexSelection_erase(m_vertexSelection, getFace().getWinding()[index].adjacent);
}
SceneChangeNotify();
update_selection_vertex();
}
bool FaceInstance::selected_vertex(std::size_t index) const {
return VertexSelection_find(m_vertexSelection, getFace().getWinding()[index].adjacent) != m_vertexSelection.end();
}
void FaceInstance::update_move_planepts_edge(std::size_t index)
{
ASSERT_MESSAGE(index < m_face->getWinding().size(), "select_edge: invalid index");
std::size_t adjacent = m_face->getWinding().next(index);
std::size_t opposite = m_face->getWinding().opposite(index);
m_face->m_move_planepts[0] = m_face->getWinding()[index].vertex;
m_face->m_move_planepts[1] = m_face->getWinding()[adjacent].vertex;
m_face->m_move_planepts[2] = m_face->getWinding()[opposite].vertex;
planepts_quantise(m_face->m_move_planepts, GRID_MIN); // winding points are very inaccurate
}
void FaceInstance::update_selection_edge() {
if (m_edgeSelection.size() == 0) {
m_selectableEdges.setSelected(false);
}
else {
m_selectableEdges.setSelected(true);
if (m_edgeSelection.size() == 1) {
std::size_t index = getFace().getWinding().findAdjacent(*m_edgeSelection.begin());
if (index != brush::c_brush_maxFaces) {
update_move_planepts_edge(index);
}
}
}
}
void FaceInstance::select_edge(std::size_t index, bool select) {
if (select) {
VertexSelection_insert(m_edgeSelection, getFace().getWinding()[index].adjacent);
}
else {
VertexSelection_erase(m_edgeSelection, getFace().getWinding()[index].adjacent);
}
SceneChangeNotify();
update_selection_edge();
}
bool FaceInstance::selected_edge(std::size_t index) const {
return VertexSelection_find(m_edgeSelection, getFace().getWinding()[index].adjacent) != m_edgeSelection.end();
}
const Vector3& FaceInstance::centroid() const {
return m_face->centroid();
}
void FaceInstance::connectivityChanged() {
// This occurs when a face is added or removed.
// The current vertex and edge selections no longer valid and must be cleared.
m_vertexSelection.clear();
m_selectableVertices.setSelected(false);
m_edgeSelection.clear();
m_selectableEdges.setSelected(false);
}
void FaceInstance::updateFaceVisibility()
{
getFace().updateFaceVisibility();
}
FaceInstanceSet& FaceInstance::Selection()
{
return _selectedFaceInstances;
}
|