/
General.cpp
1089 lines (903 loc) · 27.8 KB
/
General.cpp
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "General.h"
#include "imodel.h"
#include "iselection.h"
#include "iundo.h"
#include "igrid.h"
#include "imodelsurface.h"
#include "scenelib.h"
#include "iselectiontest.h"
#include "itraceable.h"
#include "math/Ray.h"
#include "selection/shaderclipboard/ShaderClipboard.h"
#include "string/convert.h"
#include "selectionlib.h"
#include "entitylib.h"
#include "SelectionPolicies.h"
#include "selection/SceneWalkers.h"
#include "selection/algorithm/Primitives.h"
#include "selection/algorithm/Transformation.h"
#include "selection/algorithm/Group.h"
#include "selection/clipboard/Clipboard.h"
#include "selection/algorithm/Curves.h"
#include "selection/algorithm/Entity.h"
#include "selection/algorithm/GroupCycle.h"
#include "brush/BrushVisit.h"
#include "patch/PatchSceneWalk.h"
#include "patch/Patch.h"
#include "patch/PatchNode.h"
#include <stack>
namespace selection
{
namespace algorithm
{
EntitySelectByClassnameWalker::EntitySelectByClassnameWalker(const ClassnameList& classnames) :
_classnames(classnames)
{}
bool EntitySelectByClassnameWalker::pre(const scene::INodePtr& node) {
// don't traverse invisible nodes
if (!node->visible()) return false;
Entity* entity = Node_getEntity(node);
if (entity != NULL) {
if (entityMatches(entity)) {
// Got a matching entity
Node_setSelected(node, true);
}
// Don't traverse entities
return false;
}
// Not an entity, traverse
return true;
}
bool EntitySelectByClassnameWalker::entityMatches(Entity* entity) const {
for (ClassnameList::const_iterator i = _classnames.begin();
i != _classnames.end(); ++i)
{
if (entity->getKeyValue("classname") == *i) {
return true;
}
}
return false;
}
void selectAllOfType(const cmd::ArgumentList& args)
{
if (GlobalSelectionSystem().getSelectionInfo().componentCount > 0 &&
!FaceInstance::Selection().empty())
{
std::set<std::string> shaders;
// SELECT DISTINCT shader FROM selected_faces
forEachSelectedFaceComponent([&] (Face& face)
{
shaders.insert(face.getShader());
});
// fall back to the one selected in the texture browser
if (shaders.empty())
{
shaders.insert(GlobalShaderClipboard().getSource().getShader());
}
// Deselect all faces
GlobalSelectionSystem().setSelectedAllComponents(false);
// Select all faces carrying any of the shaders in the set
scene::foreachVisibleFaceInstance([&] (FaceInstance& instance)
{
if (shaders.find(instance.getFace().getShader()) != shaders.end())
{
instance.setSelected(SelectionSystem::eFace, true);
}
});
// Select all visible patches carrying any of the shaders in the set
scene::foreachVisiblePatch([&] (Patch& patch)
{
if (shaders.find(patch.getShader()) != shaders.end())
{
patch.getPatchNode().setSelected(true);
}
});
}
else
{
// Find any classnames of selected entities
ClassnameList classnames;
GlobalSelectionSystem().foreachSelected([&] (const scene::INodePtr& node)
{
Entity* entity = Node_getEntity(node);
if (entity != NULL)
{
classnames.push_back(entity->getKeyValue("classname"));
}
});
// De-select everything
GlobalSelectionSystem().setSelectedAll(false);
if (!classnames.empty())
{
// Instantiate a selector class
EntitySelectByClassnameWalker classnameSelector(classnames);
// Traverse the scenegraph, select all matching the classname list
GlobalSceneGraph().root()->traverse(classnameSelector);
}
else
{
// No entities found, select all elements with textures
// matching the one in the texture browser
auto shader = GlobalShaderClipboard().getSource().getShader();
scene::foreachVisibleBrush([&] (Brush& brush)
{
if (brush.hasShader(shader))
{
brush.getBrushNode().setSelected(true);
}
});
// Select all visible patches carrying any of the shaders in the set
scene::foreachVisiblePatch([&] (Patch& patch)
{
if (patch.getShader() == shader)
{
patch.getPatchNode().setSelected(true);
}
});
}
}
SceneChangeNotify();
}
class HideSubgraphWalker :
public scene::NodeVisitor
{
public:
bool pre(const scene::INodePtr& node)
{
node->enable(scene::Node::eHidden);
return true;
}
};
class ShowSubgraphWalker :
public scene::NodeVisitor
{
public:
bool pre(const scene::INodePtr& node)
{
node->disable(scene::Node::eHidden);
return true;
}
};
inline void hideSubgraph(const scene::INodePtr& node, bool hide)
{
if (hide)
{
HideSubgraphWalker walker;
node->traverse(walker);
}
else
{
ShowSubgraphWalker walker;
node->traverse(walker);
}
}
inline void hideNode(const scene::INodePtr& node, bool hide)
{
if (hide)
{
node->enable(scene::Node::eHidden);
}
else
{
node->disable(scene::Node::eHidden);
}
}
// If the given node has any components of any kind, set the component selection status to the given flag
inline void setComponentSelection(const scene::INodePtr& node, bool selected)
{
ComponentSelectionTestablePtr componentSelectionTestable = Node_getComponentSelectionTestable(node);
if (componentSelectionTestable)
{
componentSelectionTestable->setSelectedComponents(selected, SelectionSystem::eVertex);
componentSelectionTestable->setSelectedComponents(selected, SelectionSystem::eEdge);
componentSelectionTestable->setSelectedComponents(selected, SelectionSystem::eFace);
}
}
void hideSelected(const cmd::ArgumentList& args)
{
// Traverse the selection, hiding all nodes
GlobalSelectionSystem().foreachSelected([] (const scene::INodePtr& node)
{
hideSubgraph(node, true);
// De-select all components of the node that is going to be hidden (#5054)
setComponentSelection(node, false);
});
// Then de-select the hidden nodes
GlobalSelectionSystem().setSelectedAll(false);
SceneChangeNotify();
}
// Hides all nodes that are not selected
class HideDeselectedWalker :
public scene::NodeVisitor
{
bool _hide;
std::stack<bool> _stack;
public:
HideDeselectedWalker(bool hide) :
_hide(hide)
{}
bool pre(const scene::INodePtr& node)
{
// Check the selection status
bool isSelected = Node_isSelected(node);
// greebo: Don't check root nodes for selected state
if (!node->isRoot() && isSelected)
{
// We have a selected instance, "remember" this by setting the parent
// stack element to TRUE
if (!_stack.empty())
{
_stack.top() = true;
}
}
// We are going one level deeper, add a new stack element for this subtree
_stack.push(false);
// Try to go deeper, but don't do this for deselected instances
return !isSelected;
}
void post(const scene::INodePtr& node)
{
// greebo: We've traversed this subtree, now check if we had selected children
if (!node->isRoot() &&
!_stack.empty() && _stack.top() == false &&
!Node_isSelected(node))
{
// No selected child nodes, hide this node
hideSubgraph(node, _hide);
}
// Go upwards again, one level
_stack.pop();
}
};
void hideDeselected(const cmd::ArgumentList& args) {
HideDeselectedWalker walker(true);
GlobalSceneGraph().root()->traverse(walker);
// Hide all components, there might be faces selected
GlobalSelectionSystem().setSelectedAllComponents(false);
SceneChangeNotify();
}
class HideAllWalker :
public scene::NodeVisitor
{
bool _hide;
public:
HideAllWalker(bool hide) :
_hide(hide)
{}
bool pre(const scene::INodePtr& node) {
hideNode(node, _hide);
return true;
}
};
void showAllHidden(const cmd::ArgumentList& args) {
HideAllWalker walker(false);
GlobalSceneGraph().root()->traverse(walker);
SceneChangeNotify();
}
/**
* Invert Selection walker
* Worldspawn brushes and patches will be considered, unless
* entity selection mode is active.
* An entity with or without any child nodes will be considered.
* The worldspawn node itself will be ignored.
* func_static children will be ignored.
*/
class InvertSelectionWalker :
public scene::NodeVisitor
{
private:
SelectionSystem::EMode _mode;
public:
InvertSelectionWalker(SelectionSystem::EMode mode) :
_mode(mode)
{}
bool pre(const scene::INodePtr& node)
{
// Ignore hidden nodes (including their whole subgraph)
if (!node->visible()) return false;
Entity* entity = Node_getEntity(node);
// Check if we have a selectable
ISelectablePtr selectable = Node_getSelectable(node);
if (selectable)
{
switch (_mode)
{
case SelectionSystem::eEntity:
// Only consider non-worldspawn entities
if (entity != nullptr && !entity->isWorldspawn())
{
selectable->setSelected(!selectable->isSelected());
}
break;
case SelectionSystem::ePrimitive:
// Never select the worldspawn entity
if (entity == nullptr || !entity->isWorldspawn())
{
selectable->setSelected(!selectable->isSelected());
}
break;
case SelectionSystem::eGroupPart:
// All child primitives can be selected (worldspawn entity is filtered out)
if (entity == nullptr && Node_isEntity(node->getParent()))
{
selectable->setSelected(!selectable->isSelected());
}
break;
case SelectionSystem::eComponent:
break; // not handled here
}
}
// Determine whether we want to traverse child objects of entities
if (entity != nullptr)
{
if (_mode == SelectionSystem::eGroupPart)
{
// In group part mode, we want to traverse all entities but worldspawn
return !entity->isWorldspawn();
}
else if (_mode == SelectionSystem::eEntity)
{
// In Entity selection mode, we don't need to traverse entity subgraphs
// as we only want the parent entity
return false;
}
// In primitive mode, we want to traverse worldspawn only
return entity->isWorldspawn();
}
// We can traverse all non-entities by default
return true;
}
};
class InvertComponentSelectionWalker :
public scene::NodeVisitor
{
SelectionSystem::EComponentMode _mode;
ComponentSelectionTestablePtr _selectable;
public:
InvertComponentSelectionWalker(SelectionSystem::EComponentMode mode) :
_mode(mode)
{}
bool pre(const scene::INodePtr& node)
{
// Ignore hidden nodes
if (!node->visible()) return false;
Entity* entity = Node_getEntity(node);
// Check if we have a selectable
ISelectablePtr selectable = Node_getSelectable(node);
if (selectable != NULL)
{
// Check if we have a componentselectiontestable instance
ComponentSelectionTestablePtr compSelTestable =
Node_getComponentSelectionTestable(node);
// Only add it to the list if the instance has components and is already selected
if (compSelTestable && selectable->isSelected())
{
_selectable = compSelTestable;
}
}
// Do we have a groupnode? If yes, don't traverse the children
if (entity != NULL && scene::hasChildPrimitives(node) && !entity->isWorldspawn())
{
// Don't traverse the children of this groupnode
return false;
}
return true;
}
void post(const scene::INodePtr& node)
{
if (_selectable)
{
_selectable->invertSelectedComponents(_mode);
_selectable.reset();
}
}
};
void invertSelection(const cmd::ArgumentList& args)
{
if (GlobalSelectionSystem().Mode() == SelectionSystem::eComponent)
{
InvertComponentSelectionWalker walker(GlobalSelectionSystem().ComponentMode());
GlobalSceneGraph().root()->traverse(walker);
}
else
{
InvertSelectionWalker walker(GlobalSelectionSystem().Mode());
GlobalSceneGraph().root()->traverse(walker);
}
}
void deleteSelection()
{
std::set<scene::INodePtr> eraseList;
// Traverse the scene, collecting all selected nodes
GlobalSelectionSystem().foreachSelected([&] (const scene::INodePtr& node)
{
// Check for selected nodes whose parent is not NULL and are not root
if (node->getParent() != NULL && !node->isRoot())
{
// Found a candidate
eraseList.insert(node);
}
});
std::for_each(eraseList.begin(), eraseList.end(), [] (const scene::INodePtr& node)
{
scene::INodePtr parent = node->getParent();
// Check for NULL parents. It's possible that both parent and child are in the eraseList
// and the parent has been deleted already.
if (parent)
{
// Remove the childnodes
scene::removeNodeFromParent(node);
if (!parent->hasChildNodes())
{
// Remove the parent as well
scene::removeNodeFromParent(parent);
}
}
});
SceneChangeNotify();
}
void deleteSelectionCmd(const cmd::ArgumentList& args)
{
UndoableCommand undo("deleteSelected");
deleteSelection();
}
/**
* Selects all objects that intersect one of the bounding AABBs.
* The exact intersection-method is specified through TSelectionPolicy,
* which must implement an evalute() method taking an AABB and the scene::INodePtr.
*/
template<class TSelectionPolicy>
class SelectByBounds :
public scene::NodeVisitor
{
AABB* _aabbs; // selection aabbs
std::size_t _count; // number of aabbs in _aabbs
TSelectionPolicy policy; // type that contains a custom intersection method aabb<->aabb
public:
SelectByBounds(AABB* aabbs, std::size_t count) :
_aabbs(aabbs),
_count(count)
{}
bool pre(const scene::INodePtr& node) {
// Don't traverse hidden nodes
if (!node->visible()) {
return false;
}
ISelectablePtr selectable = Node_getSelectable(node);
// ignore worldspawn
Entity* entity = Node_getEntity(node);
if (entity != NULL) {
if (entity->isWorldspawn()) {
return true;
}
}
bool selected = false;
if (selectable != NULL && node->getParent() != NULL && !node->isRoot()) {
for (std::size_t i = 0; i < _count; ++i) {
// Check if the selectable passes the AABB test
if (policy.evaluate(_aabbs[i], node)) {
selectable->setSelected(true);
selected = true;
break;
}
}
}
// Only traverse the children of this node, if the node itself couldn't be selected
return !selected;
}
/**
* Performs selection operation on the global scenegraph.
* If delete_bounds_src is true, then the objects which were
* used as source for the selection aabbs will be deleted.
*/
static void DoSelection(bool deleteBoundsSrc = true)
{
if (GlobalSelectionSystem().Mode() != SelectionSystem::ePrimitive)
{
return; // Wrong selection mode
}
// we may not need all AABBs since not all selected objects have to be brushes
const std::size_t max = GlobalSelectionSystem().countSelected();
std::unique_ptr<AABB[]> aabbs(new AABB[max]);
// Loops over all selected brushes and stores their
// world AABBs in the specified array.
std::size_t aabbCount = 0; // number of aabbs in aabbs
GlobalSelectionSystem().foreachSelected([&] (const scene::INodePtr& node)
{
ASSERT_MESSAGE(aabbCount <= max, "Invalid _count in CollectSelectedBrushesBounds");
// stop if the array is already full
if (aabbCount == max) return;
if (Node_isSelected(node) && Node_isBrush(node))
{
aabbs[aabbCount] = node->worldAABB();
++aabbCount;
}
});
// nothing usable in selection
if (!aabbCount)
{
return;
}
// delete selected objects?
if (deleteBoundsSrc)
{
UndoableCommand undo("deleteSelected");
deleteSelection();
}
// Instantiate a "self" object SelectByBounds and use it as visitor
SelectByBounds<TSelectionPolicy> walker(aabbs.get(), aabbCount);
GlobalSceneGraph().root()->traverse(walker);
SceneChangeNotify();
}
};
void selectInside(const cmd::ArgumentList& args)
{
SelectByBounds<SelectionPolicy_Inside>::DoSelection();
}
void selectTouching(const cmd::ArgumentList& args)
{
SelectByBounds<SelectionPolicy_Touching>::DoSelection(false);
}
void selectCompleteTall(const cmd::ArgumentList& args)
{
SelectByBounds<SelectionPolicy_Complete_Tall>::DoSelection();
}
AABB getCurrentComponentSelectionBounds()
{
AABB bounds;
GlobalSelectionSystem().foreachSelected([&] (const scene::INodePtr& node)
{
ComponentEditablePtr componentEditable = Node_getComponentEditable(node);
if (componentEditable != NULL)
{
bounds.includeAABB(AABB::createFromOrientedAABBSafe(
componentEditable->getSelectedComponentsBounds(), node->localToWorld()));
}
});
return bounds;
}
// This is the same as for selection
AABB getCurrentSelectionBounds(bool considerLightVolumes)
{
AABB bounds;
GlobalSelectionSystem().foreachSelected([&](const scene::INodePtr& node)
{
if (considerLightVolumes)
{
bounds.includeAABB(node->worldAABB());
return;
}
// We were asked to ignore light volumes, so for lights we'll only
// sum up the small diamond AABB to calculate the selection bounds (#4578)
ILightNodePtr lightNode = Node_getLightNode(node);
if (lightNode)
{
bounds.includeAABB(lightNode->getSelectAABB());
}
else
{
bounds.includeAABB(node->worldAABB());
}
});
return bounds;
}
AABB getCurrentSelectionBounds()
{
// Consider light volumes by default
return getCurrentSelectionBounds(true);
}
Vector3 getCurrentSelectionCenter()
{
return getCurrentSelectionBounds().getOrigin().getSnapped();
}
class PrimitiveFindIndexWalker :
public scene::NodeVisitor
{
scene::INodePtr _node;
std::size_t& _count;
public:
PrimitiveFindIndexWalker(const scene::INodePtr& node, std::size_t& count) :
_node(node),
_count(count)
{}
bool pre(const scene::INodePtr& node)
{
if (Node_isPrimitive(node))
{
// Have we found the node?
if (_node == node)
{
// Yes, found, set needle to NULL
_node = scene::INodePtr();
}
// As long as the needle is non-NULL, increment the counter
if (_node != NULL)
{
++_count;
}
}
return true;
}
};
class EntityFindIndexWalker :
public scene::NodeVisitor
{
scene::INodePtr _node;
std::size_t& _count;
public:
EntityFindIndexWalker(const scene::INodePtr& node, std::size_t& count) :
_node(node),
_count(count)
{}
bool pre(const scene::INodePtr& node)
{
if (Node_isEntity(node))
{
// Have we found the node?
if (_node == node)
{
// Yes, found, set needle to NULL
_node = scene::INodePtr();
}
// As long as the needle is non-NULL, increment the counter
if (_node != NULL)
{
++_count;
}
}
return true;
}
};
void getSelectionIndex(std::size_t& ent, std::size_t& brush)
{
if (GlobalSelectionSystem().countSelected() != 0)
{
scene::INodePtr node = GlobalSelectionSystem().ultimateSelected();
if (Node_isEntity(node))
{
// Selection is an entity, find its index
EntityFindIndexWalker walker(node, ent);
GlobalSceneGraph().root()->traverse(walker);
}
else if (Node_isPrimitive(node))
{
scene::INodePtr parent = node->getParent();
// Node is a primitive, find parent entity and child index
EntityFindIndexWalker walker(parent, ent);
GlobalSceneGraph().root()->traverse(walker);
PrimitiveFindIndexWalker brushWalker(node, brush);
parent->traverse(brushWalker);
}
}
}
void snapSelectionToGrid(const cmd::ArgumentList& args)
{
float gridSize = GlobalGrid().getGridSize();
UndoableCommand undo("snapSelected -grid " + string::to_string(gridSize));
if (GlobalSelectionSystem().Mode() == SelectionSystem::eComponent)
{
// Component mode
GlobalSelectionSystem().foreachSelectedComponent([&] (const scene::INodePtr& node)
{
// Don't do anything with hidden nodes
if (!node->visible()) return;
// Check if the visited instance is componentSnappable
ComponentSnappablePtr componentSnappable = Node_getComponentSnappable(node);
if (componentSnappable != NULL)
{
componentSnappable->snapComponents(gridSize);
}
});
}
else
{
// Non-component mode
GlobalSelectionSystem().foreachSelected([&] (const scene::INodePtr& node)
{
// Don't do anything with hidden nodes
if (!node->visible()) return;
SnappablePtr snappable = Node_getSnappable(node);
if (snappable != NULL)
{
snappable->snapto(gridSize);
}
});
}
}
class IntersectionFinder :
public scene::NodeVisitor
{
private:
const Ray& _ray;
Vector3 _bestPoint;
const scene::INodePtr& _self;
public:
IntersectionFinder(const Ray& ray, const scene::INodePtr& self) :
_ray(ray),
_bestPoint(_ray.origin),
_self(self)
{}
const Vector3& getIntersection() const
{
return _bestPoint;
}
bool pre(const scene::INodePtr& node)
{
if (node == _self) return false;
if (!node->visible()) return true;
const AABB& aabb = node->worldAABB();
Vector3 intersection;
if (_ray.intersectAABB(aabb, intersection))
{
rMessage() << "Ray intersects with node " << node->name() << " at " << intersection;
// We have an intersection, let's attempt a full trace against the object
ITraceablePtr traceable = std::dynamic_pointer_cast<ITraceable>(node);
if (traceable && traceable->getIntersection(_ray, intersection))
{
rMessage() << " impacting at " << intersection;
}
else
{
// rMessage() << " (no detailed intersection)";
return true; // ignore this node
}
float oldDistSquared = (_bestPoint - _ray.origin).getLengthSquared();
float newDistSquared = (intersection - _ray.origin).getLengthSquared();
if ((oldDistSquared == 0 && newDistSquared > 0) || newDistSquared < oldDistSquared)
{
_bestPoint = intersection;
}
rMessage() << std::endl;
}
return true;
}
};
Vector3 getLowestVertexOfModel(const model::IModel& model, const Matrix4& localToWorld)
{
Vector3 bestValue = Vector3(0,0,1e16);
for (int surfaceIndex = 0; surfaceIndex < model.getSurfaceCount(); ++surfaceIndex)
{
const model::IModelSurface& surface = model.getSurface(surfaceIndex);
for (int v = 0; v < surface.getNumVertices(); ++v)
{
Vector3 candidate = localToWorld.transformPoint(surface.getVertex(v).vertex);
if (candidate.z() < bestValue.z())
{
bestValue = candidate;
}
}
}
return bestValue;
}
class ChildModelFinder :
public scene::NodeVisitor
{
model::ModelNodePtr _modelNode;
public:
const model::ModelNodePtr& getModelNode() const
{
return _modelNode;
}
bool pre(const scene::INodePtr& node)
{
model::ModelNodePtr modelNode = Node_getModel(node);
if (modelNode != NULL)
{
_modelNode = modelNode;
return false;
}
return true;
}
};
Vector3 getOriginForFloorTrace(const scene::INodePtr& node)
{
Vector3 origin = node->worldAABB().getOrigin();
Entity* entity = Node_getEntity(node);
if (entity != NULL)
{
// Next iteration: use the entity's origin
origin = string::convert<Vector3>(entity->getKeyValue("origin"));
// Finally, find the lowest vertex (with regard to the z direction) of models
ChildModelFinder modelFinder;
node->traverseChildren(modelFinder);
if (modelFinder.getModelNode())
{
origin = getLowestVertexOfModel(modelFinder.getModelNode()->getIModel(), node->localToWorld());
}
}
return origin;
}
void floorNode(const scene::INodePtr& node)
{
Vector3 objectOrigin = getOriginForFloorTrace(node);
// Move up the trace start point by 1 unit to avoid falling further down
// when hitting "floor" multiple times in a row
Ray ray(objectOrigin + Vector3(0, 0, 1), Vector3(0, 0, -1));
IntersectionFinder finder(ray, node);
GlobalSceneGraph().root()->traverseChildren(finder);
if ((finder.getIntersection() - ray.origin).getLengthSquared() > 0)
{
Vector3 translation = finder.getIntersection() - objectOrigin;
ITransformablePtr transformable = Node_getTransformable(node);
if (transformable)
{
transformable->setType(TRANSFORM_PRIMITIVE);
transformable->setTranslation(translation);
transformable->freezeTransform();
}
}
else
{
rMessage() << "No suitable floor points found." << std::endl;
}
}
void floorSelection(const cmd::ArgumentList& args)
{
UndoableCommand undo("floorSelected");