/
Brush.cpp
1395 lines (1143 loc) · 39 KB
/
Brush.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 "Brush.h"
#include "math/Frustum.h"
#include "irenderable.h"
#include "itextstream.h"
#include "iuimanager.h"
#include "shaderlib.h"
#include "BrushModule.h"
#include "BrushNode.h"
#include "Face.h"
#include "FixedWinding.h"
#include "math/Ray.h"
#include <functional>
namespace {
/// \brief Returns true if edge (\p x, \p y) is smaller than the epsilon used to classify winding points against a plane.
inline bool Edge_isDegenerate(const Vector3& x, const Vector3& y) {
return (y - x).getLengthSquared() < (ON_EPSILON * ON_EPSILON);
}
inline float max_extent_2d(const Vector3& extents, int axis)
{
switch(axis)
{
case 0: return std::max(extents[1], extents[2]);
case 1: return std::max(extents[0], extents[2]);
default: return std::max(extents[0], extents[1]);
}
}
inline float max_extent(const Vector3& extents)
{
return std::max(std::max(extents[0], extents[1]), extents[2]);
}
}
const std::size_t Brush::PRISM_MIN_SIDES = 3;
const std::size_t Brush::PRISM_MAX_SIDES = c_brush_maxFaces - 2;
const std::size_t Brush::CONE_MIN_SIDES = 3;
const std::size_t Brush::CONE_MAX_SIDES = 32;
const std::size_t Brush::SPHERE_MIN_SIDES = 3;
const std::size_t Brush::SPHERE_MAX_SIDES = 7;
Brush::Brush(BrushNode& owner) :
_owner(owner),
_undoStateSaver(nullptr),
_mapFileChangeTracker(nullptr),
_faceCentroidPoints(GL_POINTS),
_uniqueVertexPoints(GL_POINTS),
_uniqueEdgePoints(GL_POINTS),
m_planeChanged(false),
m_transformChanged(false),
_detailFlag(Structural)
{
onFacePlaneChanged();
}
Brush::Brush(BrushNode& owner, const Brush& other) :
_owner(owner),
_undoStateSaver(nullptr),
_mapFileChangeTracker(nullptr),
_faceCentroidPoints(GL_POINTS),
_uniqueVertexPoints(GL_POINTS),
_uniqueEdgePoints(GL_POINTS),
m_planeChanged(false),
m_transformChanged(false),
_detailFlag(Structural)
{
copy(other);
}
Brush::~Brush()
{
ASSERT_MESSAGE(m_observers.empty(), "Brush::~Brush: observers still attached");
}
BrushNode& Brush::getBrushNode()
{
return _owner;
}
IFace& Brush::getFace(std::size_t index)
{
assert(index < m_faces.size());
return *m_faces[index];
}
const IFace& Brush::getFace(std::size_t index) const
{
assert(index < m_faces.size());
return *m_faces[index];
}
IFace& Brush::addFace(const Plane3& plane)
{
// Allocate a new Face
undoSave();
push_back(FacePtr(new Face(*this, plane)));
return *m_faces.back();
}
IFace& Brush::addFace(const Plane3& plane, const Matrix4& texDef, const std::string& shader)
{
// Allocate a new Face
undoSave();
push_back(FacePtr(new Face(*this, plane, texDef, shader)));
return *m_faces.back();
}
void Brush::attach(BrushObserver& observer) {
for (Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i) {
observer.push_back(*(*i));
}
for(SelectableEdges::iterator i = m_select_edges.begin(); i !=m_select_edges.end(); ++i) {
observer.edge_push_back(*i);
}
for(SelectableVertices::iterator i = m_select_vertices.begin(); i != m_select_vertices.end(); ++i) {
observer.vertex_push_back(*i);
}
m_observers.insert(&observer);
}
void Brush::detach(BrushObserver& observer)
{
m_observers.erase(&observer);
}
void Brush::forEachFace(const std::function<void(Face&)>& functor) const
{
for (const FacePtr& face : m_faces) functor(*face);
}
void Brush::connectUndoSystem(IMapFileChangeTracker& changeTracker)
{
assert(_undoStateSaver == nullptr);
// Keep a reference around, we need it when faces are changing
_mapFileChangeTracker = &changeTracker;
_undoStateSaver = GlobalUndoSystem().getStateSaver(*this, changeTracker);
// Notify each face that we have a tracker
forEachFace([&](Face& face) { face.connectUndoSystem(changeTracker); });
}
void Brush::disconnectUndoSystem(IMapFileChangeTracker& changeTracker)
{
assert(_undoStateSaver != nullptr);
// Notify each face
forEachFace([&](Face& face) { face.disconnectUndoSystem(changeTracker); });
_mapFileChangeTracker = nullptr;
_undoStateSaver = nullptr;
GlobalUndoSystem().releaseStateSaver(*this);
}
void Brush::setShader(const std::string& newShader) {
undoSave();
for (Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i) {
(*i)->setShader(newShader);
}
}
bool Brush::hasShader(const std::string& name) {
// Traverse the faces
for (Faces::const_iterator i = m_faces.begin(); i != m_faces.end(); ++i) {
if (shader_equal((*i)->getShader(), name)) {
return true;
}
}
// not found
return false;
}
bool Brush::hasVisibleMaterial() const
{
// Traverse the faces
for (Faces::const_iterator i = m_faces.begin(); i != m_faces.end(); ++i)
{
if ((*i)->getFaceShader().getGLShader()->getMaterial()->isVisible())
{
return true; // return true on first visible material
}
}
// no visible material
return false;
}
void Brush::updateFaceVisibility()
{
_owner.updateFaceVisibility();
}
Brush::DetailFlag Brush::getDetailFlag() const
{
return _detailFlag;
}
void Brush::setDetailFlag(DetailFlag newValue)
{
undoSave();
_detailFlag = newValue;
}
BrushSplitType Brush::classifyPlane(const Plane3& plane) const
{
evaluateBRep();
BrushSplitType split;
for (auto face : *this)
{
if (face->contributes())
{
split += face->getWinding().classifyPlane(plane);
}
}
return split;
}
void Brush::evaluateBRep() const {
if(m_planeChanged) {
m_planeChanged = false;
const_cast<Brush*>(this)->buildBRep();
}
}
void Brush::transformChanged() {
m_transformChanged = true;
onFacePlaneChanged();
}
void Brush::evaluateTransform()
{
if (m_transformChanged)
{
m_transformChanged = false;
revertTransform();
_owner.evaluateTransform();
}
}
void Brush::aabbChanged()
{
_owner.boundsChanged();
}
const AABB& Brush::localAABB() const {
evaluateBRep();
return m_aabb_local;
}
void Brush::renderComponents(SelectionSystem::EComponentMode mode, RenderableCollector& collector,
const VolumeTest& volume, const Matrix4& localToWorld) const
{
switch (mode)
{
case SelectionSystem::eVertex:
collector.addRenderable(m_state_point, _uniqueVertexPoints, localToWorld);
break;
case SelectionSystem::eEdge:
collector.addRenderable(m_state_point, _uniqueEdgePoints, localToWorld);
break;
case SelectionSystem::eFace:
collector.addRenderable(m_state_point, _faceCentroidPoints, localToWorld);
break;
default:
break;
}
}
void Brush::translate(const Vector3& translation)
{
std::for_each(m_faces.begin(), m_faces.end(),
std::bind(&Face::translate, std::placeholders::_1, translation));
freezeTransform();
}
void Brush::transform(const Matrix4& matrix)
{
for (const FacePtr& face : m_faces)
{
face->transform(matrix);
}
}
void Brush::snapto(float snap) {
for(Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i) {
(*i)->snapto(snap);
}
}
void Brush::revertTransform() {
for(Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i) {
(*i)->revertTransform();
}
}
void Brush::freezeTransform() {
for(Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i) {
(*i)->freezeTransform();
}
}
/// \brief Returns the absolute index of the \p faceVertex.
std::size_t Brush::absoluteIndex(FaceVertexId faceVertex) {
std::size_t index = 0;
for(std::size_t i = 0; i < faceVertex.getFace(); ++i) {
index += m_faces[i]->getWinding().size();
}
return index + faceVertex.getVertex();
}
void Brush::appendFaces(const Faces& other) {
clear();
for(Faces::const_iterator i = other.begin(); i != other.end(); ++i) {
push_back(*i);
}
}
void Brush::undoSave()
{
if (_undoStateSaver != nullptr)
{
_undoStateSaver->save(*this);
}
}
IUndoMementoPtr Brush::exportState() const
{
return IUndoMementoPtr(new BrushUndoMemento(m_faces, _detailFlag));
}
void Brush::importState(const IUndoMementoPtr& state)
{
undoSave();
BrushUndoMemento& memento = *std::static_pointer_cast<BrushUndoMemento>(state);
_detailFlag = memento._detailFlag;
appendFaces(memento._faces);
onFacePlaneChanged();
for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i) {
(*i)->DEBUG_verify();
}
}
/// \brief Appends a copy of \p face to the end of the face list.
FacePtr Brush::addFace(const Face& face) {
if (m_faces.size() == c_brush_maxFaces) {
return FacePtr();
}
undoSave();
push_back(FacePtr(new Face(*this, face)));
onFacePlaneChanged();
return m_faces.back();
}
/// \brief Appends a new face constructed from the parameters to the end of the face list.
FacePtr Brush::addPlane(const Vector3& p0, const Vector3& p1, const Vector3& p2, const std::string& shader, const TextureProjection& projection) {
if(m_faces.size() == c_brush_maxFaces) {
return FacePtr();
}
undoSave();
push_back(FacePtr(new Face(*this, p0, p1, p2, shader, projection)));
onFacePlaneChanged();
return m_faces.back();
}
void Brush::setRenderSystem(const RenderSystemPtr& renderSystem)
{
if (renderSystem)
{
m_state_point = renderSystem->capture("$POINT");
}
else
{
m_state_point.reset();
}
for (Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i)
{
(*i)->setRenderSystem(renderSystem);
}
}
std::size_t Brush::DEBUG_size() {
return m_faces.size();
}
Brush::const_iterator Brush::begin() const {
return m_faces.begin();
}
Brush::const_iterator Brush::end() const {
return m_faces.end();
}
FacePtr Brush::back() {
return m_faces.back();
}
const FacePtr Brush::back() const {
return m_faces.back();
}
void Brush::reserve(std::size_t count) {
m_faces.reserve(count);
for (Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i) {
(*i)->reserve(count);
}
}
void Brush::push_back(Faces::value_type face) {
m_faces.push_back(face);
if (_undoStateSaver)
{
m_faces.back()->connectUndoSystem(*_mapFileChangeTracker);
}
for (Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i) {
(*i)->push_back(*face);
(*i)->DEBUG_verify();
}
}
void Brush::pop_back()
{
if (_undoStateSaver)
{
m_faces.back()->disconnectUndoSystem(*_mapFileChangeTracker);
}
m_faces.pop_back();
for (Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i) {
(*i)->pop_back();
(*i)->DEBUG_verify();
}
}
void Brush::erase(std::size_t index)
{
if (_undoStateSaver)
{
m_faces[index]->disconnectUndoSystem(*_mapFileChangeTracker);
}
m_faces.erase(m_faces.begin() + index);
for (Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i) {
(*i)->erase(index);
(*i)->DEBUG_verify();
}
}
void Brush::onFacePlaneChanged()
{
m_planeChanged = true;
aabbChanged();
_owner.lightsChanged();
}
void Brush::onFaceShaderChanged()
{
onFacePlaneChanged();
// Queue an UI update of the texture tools if any of them is listening
signal_faceShaderChanged().emit();
}
void Brush::onFaceConnectivityChanged()
{
for (auto i : m_observers)
{
i->connectivityChanged();
}
}
void Brush::onFaceEvaluateTransform()
{
evaluateTransform();
}
void Brush::clear()
{
undoSave();
if (_undoStateSaver)
{
forEachFace([&](Face& face) { face.disconnectUndoSystem(*_mapFileChangeTracker); });
}
m_faces.clear();
for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i) {
(*i)->clear();
(*i)->DEBUG_verify();
}
}
std::size_t Brush::getNumFaces() const
{
return m_faces.size();
}
bool Brush::empty() const {
return m_faces.empty();
}
/// \brief Returns true if any face of the brush contributes to the final B-Rep.
bool Brush::hasContributingFaces() const {
for (const_iterator i = begin(); i != end(); ++i) {
if ((*i)->contributes()) {
return true;
}
}
return false;
}
/// \brief Removes faces that do not contribute to the brush. This is useful for cleaning up after CSG operations on the brush.
/// Note: removal of empty faces is not performed during direct brush manipulations, because it would make a manipulation irreversible if it created an empty face.
void Brush::removeEmptyFaces() {
evaluateBRep();
std::size_t i = 0;
while (i < m_faces.size()) {
if (!m_faces[i]->contributes()) {
erase(i);
onFacePlaneChanged();
}
else {
++i;
}
}
}
/// \brief Constructs \p winding from the intersection of \p plane with the other planes of the brush.
void Brush::windingForClipPlane(Winding& winding, const Plane3& plane) const {
FixedWinding buffer[2];
bool swap = false;
// get a poly that covers an effectively infinite area
buffer[swap].createInfinite(plane, m_maxWorldCoord + 1);
// chop the poly by all of the other faces
{
for (std::size_t i = 0; i < m_faces.size(); ++i) {
const Face& clip = *m_faces[i];
if (clip.plane3() == plane
|| !clip.plane3().isValid() || !plane_unique(i)
|| plane == -clip.plane3())
{
continue;
}
buffer[!swap].clear();
{
// flip the plane, because we want to keep the back side
Plane3 clipPlane(-clip.plane3().normal(), -clip.plane3().dist());
buffer[swap].clip(plane, clipPlane, i, buffer[!swap]);
}
swap = !swap;
}
}
buffer[swap].writeToWinding(winding);
}
void Brush::update_wireframe(RenderableWireframe& wire, const bool* faces_visible) const
{
wire.m_faceVertex.resize(_edgeIndices.size());
wire.m_vertices = _uniqueVertexPoints.size() > 0 ? &_uniqueVertexPoints.front() : NULL;
wire.m_size = 0;
for (std::size_t i = 0; i < _edgeFaces.size(); ++i)
{
if (faces_visible[_edgeFaces[i].first] || faces_visible[_edgeFaces[i].second])
{
wire.m_faceVertex[wire.m_size++] = _edgeIndices[i];
}
}
}
void Brush::update_faces_wireframe(RenderablePointVector& wire,
const std::size_t* visibleFaceIndices,
std::size_t numVisibleFaces) const
{
if (numVisibleFaces > _faceCentroidPoints.size())
{
wire.clear();
return;
}
// Assure that the pointvector can carry as many faces as are visible
wire.resize(numVisibleFaces);
const std::size_t* visibleFaceIter = visibleFaceIndices;
// Pick all the visible face centroids from the vector
for (std::size_t i = 0; i < numVisibleFaces; ++i)
{
wire[i] = _faceCentroidPoints[*visibleFaceIter++];
}
}
/// \brief Makes this brush a deep-copy of the \p other.
void Brush::copy(const Brush& other)
{
_detailFlag = other._detailFlag;
for (Faces::const_iterator i = other.m_faces.begin(); i != other.m_faces.end(); ++i)
{
addFace(*(*i));
}
onFacePlaneChanged();
}
void Brush::constructCuboid(const AABB& bounds, const std::string& shader)
{
const unsigned char box[3][2] = { { 0, 1 }, { 2, 0 }, { 1, 2 } };
Vector3 mins(bounds.origin - bounds.extents);
Vector3 maxs(bounds.origin + bounds.extents);
TextureProjection projection;
clear();
reserve(6);
{
for (int i = 0; i < 3; ++i)
{
Vector3 planepts1(maxs);
Vector3 planepts2(maxs);
planepts2[box[i][0]] = mins[box[i][0]];
planepts1[box[i][1]] = mins[box[i][1]];
addPlane(maxs, planepts1, planepts2, shader, projection);
}
}
{
for (int i = 0; i < 3; ++i)
{
Vector3 planepts1(mins);
Vector3 planepts2(mins);
planepts1[box[i][0]] = maxs[box[i][0]];
planepts2[box[i][1]] = maxs[box[i][1]];
addPlane(mins, planepts1, planepts2, shader, projection);
}
}
// Passing in the default-constructed projection will result in a very tiny texture scale, fix that
for (const FacePtr& face : m_faces)
{
face->applyDefaultTextureScale();
}
}
void Brush::constructPrism(const AABB& bounds, std::size_t sides, int axis, const std::string& shader)
{
TextureProjection projection;
if (sides < PRISM_MIN_SIDES)
{
rError() << "brushPrism: sides " << sides << ": too few sides, minimum is " << PRISM_MIN_SIDES << std::endl;
return;
}
if (sides > PRISM_MAX_SIDES)
{
rError() << "brushPrism: sides " << sides << ": too many sides, maximum is " << PRISM_MAX_SIDES << std::endl;
return;
}
clear();
reserve(sides+2);
Vector3 mins(bounds.origin - bounds.extents);
Vector3 maxs(bounds.origin + bounds.extents);
float radius = max_extent_2d(bounds.extents, axis);
const Vector3& mid = bounds.origin;
Vector3 planepts[3];
planepts[2][(axis+1)%3] = mins[(axis+1)%3];
planepts[2][(axis+2)%3] = mins[(axis+2)%3];
planepts[2][axis] = maxs[axis];
planepts[1][(axis+1)%3] = maxs[(axis+1)%3];
planepts[1][(axis+2)%3] = mins[(axis+2)%3];
planepts[1][axis] = maxs[axis];
planepts[0][(axis+1)%3] = maxs[(axis+1)%3];
planepts[0][(axis+2)%3] = maxs[(axis+2)%3];
planepts[0][axis] = maxs[axis];
addPlane(planepts[0], planepts[1], planepts[2], shader, projection);
planepts[0][(axis+1)%3] = mins[(axis+1)%3];
planepts[0][(axis+2)%3] = mins[(axis+2)%3];
planepts[0][axis] = mins[axis];
planepts[1][(axis+1)%3] = maxs[(axis+1)%3];
planepts[1][(axis+2)%3] = mins[(axis+2)%3];
planepts[1][axis] = mins[axis];
planepts[2][(axis+1)%3] = maxs[(axis+1)%3];
planepts[2][(axis+2)%3] = maxs[(axis+2)%3];
planepts[2][axis] = mins[axis];
addPlane(planepts[0], planepts[1], planepts[2], shader, projection);
for (std::size_t i = 0 ; i < sides ; ++i)
{
float sv = sin(i*static_cast<float>(c_pi)*2/sides);
float cv = cos(i*static_cast<float>(c_pi)*2/sides);
planepts[0][(axis+1)%3] = floor(mid[(axis+1) % 3] + radius*cv + 0.5f);
planepts[0][(axis+2)%3] = floor(mid[(axis+2) % 3] + radius*sv + 0.5f);
planepts[0][axis] = mins[axis];
planepts[1][(axis+1)%3] = planepts[0][(axis+1)%3];
planepts[1][(axis+2)%3] = planepts[0][(axis+2)%3];
planepts[1][axis] = maxs[axis];
planepts[2][(axis+1)%3] = floor(planepts[0][(axis+1)%3] - radius*sv + 0.5f);
planepts[2][(axis+2)%3] = floor(planepts[0][(axis+2)%3] + radius*cv + 0.5f);
planepts[2][axis] = maxs[axis];
addPlane(planepts[0], planepts[1], planepts[2], shader, projection);
}
// Passing in the default-constructed projection will result in a very tiny texture scale, fix that
for (const FacePtr& face : m_faces)
{
face->applyDefaultTextureScale();
}
}
void Brush::constructCone(const AABB& bounds, std::size_t sides, const std::string& shader)
{
TextureProjection projection;
if (sides < CONE_MIN_SIDES)
{
rError() << "brushCone: sides " << sides << ": too few sides, minimum is " << CONE_MIN_SIDES << std::endl;
return;
}
if (sides > CONE_MAX_SIDES)
{
rError() << "brushCone: sides " << sides << ": too many sides, maximum is " << CONE_MAX_SIDES << std::endl;
return;
}
clear();
reserve(sides+1);
Vector3 mins(bounds.origin - bounds.extents);
Vector3 maxs(bounds.origin + bounds.extents);
float radius = max_extent(bounds.extents);
const Vector3& mid = bounds.origin;
Vector3 planepts[3];
planepts[0][0] = mins[0];planepts[0][1] = mins[1];planepts[0][2] = mins[2];
planepts[1][0] = maxs[0];planepts[1][1] = mins[1];planepts[1][2] = mins[2];
planepts[2][0] = maxs[0];planepts[2][1] = maxs[1];planepts[2][2] = mins[2];
addPlane(planepts[0], planepts[1], planepts[2], shader, projection);
for (std::size_t i = 0 ; i < sides ; ++i)
{
float sv = sin (i*static_cast<float>(c_pi)*2/sides);
float cv = cos (i*static_cast<float>(c_pi)*2/sides);
planepts[0][0] = floor(mid[0] + radius*cv + 0.5f);
planepts[0][1] = floor(mid[1] + radius*sv + 0.5f);
planepts[0][2] = mins[2];
planepts[1][0] = mid[0];
planepts[1][1] = mid[1];
planepts[1][2] = maxs[2];
planepts[2][0] = floor(planepts[0][0] - radius*sv + 0.5f);
planepts[2][1] = floor(planepts[0][1] + radius*cv + 0.5f);
planepts[2][2] = maxs[2];
addPlane(planepts[0], planepts[1], planepts[2], shader, projection);
}
// Passing in the default-constructed projection will result in a very tiny texture scale, fix that
for (const FacePtr& face : m_faces)
{
face->applyDefaultTextureScale();
}
}
void Brush::constructSphere(const AABB& bounds, std::size_t sides, const std::string& shader)
{
TextureProjection projection;
if (sides < SPHERE_MIN_SIDES)
{
rError() << "brushSphere: sides " << sides << ": too few sides, minimum is " << SPHERE_MIN_SIDES << std::endl;
return;
}
if (sides > SPHERE_MAX_SIDES)
{
rError() << "brushSphere: sides " << sides << ": too many sides, maximum is " << SPHERE_MAX_SIDES << std::endl;
return;
}
clear();
reserve(sides*sides);
float radius = max_extent(bounds.extents);
const Vector3& mid = bounds.origin;
Vector3 planepts[3];
float dt = 2 * static_cast<float>(c_pi) / sides;
float dp = static_cast<float>(c_pi) / sides;
for (std::size_t i = 0; i < sides; i++)
{
for (std::size_t j = 0; j < sides - 1; j++)
{
float t = i * dt;
float p = static_cast<float>(j * dp - c_pi / 2);
planepts[0] = mid + Vector3::createForSpherical(t, p)*radius;
planepts[1] = mid + Vector3::createForSpherical(t, p + dp)*radius;
planepts[2] = mid + Vector3::createForSpherical(t + dt, p + dp)*radius;
addPlane(planepts[0], planepts[1], planepts[2], shader, projection);
}
}
{
float p = (sides - 1) * dp - static_cast<float>(c_pi) / 2;
for (std::size_t i = 0; i < sides; i++)
{
float t = i * dt;
planepts[0] = mid + Vector3::createForSpherical(t, p)*radius;
planepts[1] = mid + Vector3::createForSpherical(t + dt, p + dp)*radius;
planepts[2] = mid + Vector3::createForSpherical(t + dt, p)*radius;
addPlane(planepts[0], planepts[1], planepts[2], shader, projection);
}
}
// Passing in the default-constructed projection will result in a very tiny texture scale, fix that
for (const FacePtr& face : m_faces)
{
face->applyDefaultTextureScale();
}
}
// greebo: this code is modeled after http://geomalgorithms.com/a13-_intersect-4.html
bool Brush::getIntersection(const Ray& ray, Vector3& intersection)
{
float tEnter = 0; // maximum entering segment parameter
float tLeave = 5000; // minimum leaving segment parameter (let's assume 5000 units for now)
Vector3 direction = ray.direction.getNormalised(); // normalise the ray direction
for (Faces::const_iterator i = m_faces.begin(); i != m_faces.end(); ++i)
{
const Face& face = *(*i);
if (!face.contributes()) continue; // skip non-contributing faces
float n = -(ray.origin - face.getWinding().front().vertex).dot(face.getPlane3().normal());
float d = direction.dot(face.getPlane3().normal());
if (d == 0) // is the ray parallel to the face?
{
if (n < 0)
{
return false; // since the ray cannot intersect the brush;
}
else
{
// the ray cannot enter or leave the brush across this face
continue;
}
}
float t = n / d;
if (d < 0)
{
// ray is entering the brush across this face
tEnter = std::max(tEnter, t);
if (tEnter > tLeave)
{
return false; // the ray enters the brush after leaving => cannot intersect
}
}
else if (d > 0)
{
// ray is leaving the brush across this face
tLeave = std::min(tLeave, t);
if (tLeave < tEnter)
{
return false; // the ray leaves the brush before entering => cannot intersect
}
}
}
assert(tEnter <= tLeave);
intersection = ray.origin + direction * tEnter;
return true;
}
sigc::signal<void>& Brush::signal_faceShaderChanged()
{
static sigc::signal<void> _sigFaceShaderChanged;
return _sigFaceShaderChanged;
}
void Brush::edge_push_back(FaceVertexId faceVertex) {
m_select_edges.push_back(SelectableEdge(m_faces, faceVertex));
for (Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i) {
(*i)->edge_push_back(m_select_edges.back());
}
}
void Brush::edge_clear() {
m_select_edges.clear();
for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i) {
(*i)->edge_clear();
}
}
void Brush::vertex_push_back(FaceVertexId faceVertex) {
m_select_vertices.push_back(SelectableVertex(m_faces, faceVertex));
for (Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i) {
(*i)->vertex_push_back(m_select_vertices.back());
}
}
void Brush::vertex_clear() {
m_select_vertices.clear();
for (Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i) {
(*i)->vertex_clear();
}
}
/// \brief Returns true if the face identified by \p index is preceded by another plane that takes priority over it.
bool Brush::plane_unique(std::size_t index) const {
// duplicate plane
for (std::size_t i = 0; i < m_faces.size(); ++i) {
if (index != i && !plane3_inside(m_faces[index]->plane3(), m_faces[i]->plane3())) {
return false;
}
}
return true;
}
/// \brief Removes edges that are smaller than the tolerance used when generating brush windings.
void Brush::removeDegenerateEdges() {
for (std::size_t i = 0; i < m_faces.size(); ++i) {
Winding& winding = m_faces[i]->getWinding();
for (std::size_t index = 0; index < winding.size();) {
//std::size_t index = std::distance(winding.begin(), j);
std::size_t next = winding.next(index);
if (Edge_isDegenerate(winding[index].vertex, winding[next].vertex)) {
Winding& other = m_faces[winding[index].adjacent]->getWinding();
std::size_t adjacent = other.findAdjacent(i);
if (adjacent != c_brush_maxFaces) {
other.erase(other.begin() + adjacent);
}
// Delete and leave index where it is
winding.erase(winding.begin() + index);
}
else {