2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #if !defined(INCLUDED_BRUSH_H)
23 #define INCLUDED_BRUSH_H
26 /// \brief The brush primitive.
28 /// A collection of planes that define a convex polyhedron.
29 /// The Boundary-Representation of this primitive is a manifold polygonal mesh.
30 /// Each face polygon is represented by a list of vertices in a \c Winding.
31 /// Each vertex is associated with another face that is adjacent to the edge
32 /// formed by itself and the next vertex in the winding. This information can
33 /// be used to find edge-pairs and vertex-rings.
36 #include "debugging/debugging.h"
40 #include "iselection.h"
47 #include "moduleobserver.h"
52 #include "renderable.h"
53 #include "selectable.h"
57 #include "math/frustum.h"
58 #include "selectionlib.h"
60 #include "texturelib.h"
61 #include "container/container.h"
62 #include "generic/bitfield.h"
63 #include "signal/signalfwd.h"
66 #include "brush_primit.h"
68 const unsigned int BRUSH_DETAIL_FLAG = 27;
69 const unsigned int BRUSH_DETAIL_MASK = (1 << BRUSH_DETAIL_FLAG);
83 #define BRUSH_CONNECTIVITY_DEBUG 0
84 #define BRUSH_DEGENERATE_DEBUG 0
86 template<typename TextOuputStreamType>
87 inline TextOuputStreamType& ostream_write(TextOuputStreamType& ostream, const Matrix4& m)
89 return ostream << "(" << m[0] << " " << m[1] << " " << m[2] << " " << m[3] << ", "
90 << m[4] << " " << m[5] << " " << m[6] << " " << m[7] << ", "
91 << m[8] << " " << m[9] << " " << m[10] << " " << m[11] << ", "
92 << m[12] << " " << m[13] << " " << m[14] << " " << m[15] << ")";
95 inline void print_vector3(const Vector3& v)
97 globalOutputStream() << "( " << v.x() << " " << v.y() << " " << v.z() << " )\n";
100 inline void print_3x3(const Matrix4& m)
102 globalOutputStream() << "( " << m.xx() << " " << m.xy() << " " << m.xz() << " ) "
103 << "( " << m.yx() << " " << m.yy() << " " << m.yz() << " ) "
104 << "( " << m.zx() << " " << m.zy() << " " << m.zz() << " )\n";
109 inline bool texdef_sane(const texdef_t& texdef)
111 return fabs(texdef.shift[0]) < (1 << 16)
112 && fabs(texdef.shift[1]) < (1 << 16);
115 inline void Winding_DrawWireframe(const Winding& winding)
117 glVertexPointer(3, GL_FLOAT, sizeof(WindingVertex), &winding.points.data()->vertex);
118 glDrawArrays(GL_LINE_LOOP, 0, GLsizei(winding.numpoints));
121 inline void Winding_Draw(const Winding& winding, const Vector3& normal, RenderStateFlags state)
123 glVertexPointer(3, GL_FLOAT, sizeof(WindingVertex), &winding.points.data()->vertex);
125 if((state & RENDER_BUMP) != 0)
127 Vector3 normals[c_brush_maxFaces];
128 typedef Vector3* Vector3Iter;
129 for(Vector3Iter i = normals, end = normals + winding.numpoints; i != end; ++i)
133 if(GlobalShaderCache().useShaderLanguage())
135 glNormalPointer(GL_FLOAT, sizeof(Vector3), normals);
136 glVertexAttribPointerARB(c_attr_TexCoord0, 2, GL_FLOAT, 0, sizeof(WindingVertex), &winding.points.data()->texcoord);
137 glVertexAttribPointerARB(c_attr_Tangent, 3, GL_FLOAT, 0, sizeof(WindingVertex), &winding.points.data()->tangent);
138 glVertexAttribPointerARB(c_attr_Binormal, 3, GL_FLOAT, 0, sizeof(WindingVertex), &winding.points.data()->bitangent);
142 glVertexAttribPointerARB(11, 3, GL_FLOAT, 0, sizeof(Vector3), normals);
143 glVertexAttribPointerARB(8, 2, GL_FLOAT, 0, sizeof(WindingVertex), &winding.points.data()->texcoord);
144 glVertexAttribPointerARB(9, 3, GL_FLOAT, 0, sizeof(WindingVertex), &winding.points.data()->tangent);
145 glVertexAttribPointerARB(10, 3, GL_FLOAT, 0, sizeof(WindingVertex), &winding.points.data()->bitangent);
150 if (state & RENDER_LIGHTING)
152 Vector3 normals[c_brush_maxFaces];
153 typedef Vector3* Vector3Iter;
154 for(Vector3Iter i = normals, last = normals + winding.numpoints; i != last; ++i)
158 glNormalPointer(GL_FLOAT, sizeof(Vector3), normals);
161 if (state & RENDER_TEXTURE)
163 glTexCoordPointer(2, GL_FLOAT, sizeof(WindingVertex), &winding.points.data()->texcoord);
167 if (state & RENDER_FILL)
169 glDrawArrays(GL_TRIANGLE_FAN, 0, GLsizei(winding.numpoints));
173 glDrawArrays(GL_LINE_LOOP, 0, GLsizei(winding.numpoints));
176 glDrawArrays(GL_POLYGON, 0, GLsizei(winding.numpoints));
180 const Winding& winding = winding;
182 if(state & RENDER_FILL)
188 glBegin(GL_LINE_LOOP);
191 if (state & RENDER_LIGHTING)
194 for(int i = 0; i < winding.numpoints; ++i)
196 if (state & RENDER_TEXTURE)
197 glTexCoord2fv(&winding.points[i][3]);
198 glVertex3fv(winding.points[i]);
204 const Colour4b colour_vertex(0, 255, 0, 255);
207 #include "shaderlib.h"
209 typedef DoubleVector3 PlanePoints[3];
211 inline bool planepts_equal(const PlanePoints planepts, const PlanePoints other)
213 return planepts[0] == other[0] && planepts[1] == other[1] && planepts[2] == other[2];
216 inline void planepts_assign(PlanePoints planepts, const PlanePoints other)
218 planepts[0] = other[0];
219 planepts[1] = other[1];
220 planepts[2] = other[2];
223 inline void planepts_quantise(PlanePoints planepts, double snap)
225 vector3_snap(planepts[0], snap);
226 vector3_snap(planepts[1], snap);
227 vector3_snap(planepts[2], snap);
230 inline float vector3_max_component(const Vector3& vec3)
232 return std::max(fabsf(vec3[0]), std::max(fabsf(vec3[1]), fabsf(vec3[2])));
235 inline void edge_snap(Vector3& edge, double snap)
237 float scale = static_cast<float>(ceil(fabs(snap / vector3_max_component(edge))));
240 vector3_scale(edge, scale);
242 vector3_snap(edge, snap);
245 inline void planepts_snap(PlanePoints planepts, double snap)
247 Vector3 edge01(vector3_subtracted(planepts[1], planepts[0]));
248 Vector3 edge12(vector3_subtracted(planepts[2], planepts[1]));
249 Vector3 edge20(vector3_subtracted(planepts[0], planepts[2]));
251 double length_squared_01 = vector3_dot(edge01, edge01);
252 double length_squared_12 = vector3_dot(edge12, edge12);
253 double length_squared_20 = vector3_dot(edge20, edge20);
255 vector3_snap(planepts[0], snap);
257 if(length_squared_01 < length_squared_12)
259 if(length_squared_12 < length_squared_20)
261 edge_snap(edge01, snap);
262 edge_snap(edge12, snap);
263 planepts[1] = vector3_added(planepts[0], edge01);
264 planepts[2] = vector3_added(planepts[1], edge12);
268 edge_snap(edge20, snap);
269 edge_snap(edge01, snap);
270 planepts[1] = vector3_added(planepts[0], edge20);
271 planepts[2] = vector3_added(planepts[1], edge01);
276 if(length_squared_01 < length_squared_20)
278 edge_snap(edge01, snap);
279 edge_snap(edge12, snap);
280 planepts[1] = vector3_added(planepts[0], edge01);
281 planepts[2] = vector3_added(planepts[1], edge12);
285 edge_snap(edge12, snap);
286 edge_snap(edge20, snap);
287 planepts[1] = vector3_added(planepts[0], edge12);
288 planepts[2] = vector3_added(planepts[1], edge20);
293 inline PointVertex pointvertex_for_planept(const DoubleVector3& point, const Colour4b& colour)
297 static_cast<float>(point.x()),
298 static_cast<float>(point.y()),
299 static_cast<float>(point.z())
305 inline PointVertex pointvertex_for_windingpoint(const Vector3& point, const Colour4b& colour)
308 vertex3f_for_vector3(point),
313 inline bool check_plane_is_integer(const PlanePoints& planePoints)
315 return !float_is_integer(planePoints[0][0])
316 || !float_is_integer(planePoints[0][1])
317 || !float_is_integer(planePoints[0][2])
318 || !float_is_integer(planePoints[1][0])
319 || !float_is_integer(planePoints[1][1])
320 || !float_is_integer(planePoints[1][2])
321 || !float_is_integer(planePoints[2][0])
322 || !float_is_integer(planePoints[2][1])
323 || !float_is_integer(planePoints[2][2]);
326 inline void brush_check_shader(const char* name)
328 if(!shader_valid(name))
330 globalErrorStream() << "brush face has invalid texture name: '" << name << "'\n";
334 class FaceShaderObserver
337 virtual void realiseShader() = 0;
338 virtual void unrealiseShader() = 0;
341 class FaceShaderObserverRealise
344 void operator()(FaceShaderObserver& observer) const
346 observer.realiseShader();
350 class FaceShaderObserverUnrealise
353 void operator()(FaceShaderObserver& observer) const
355 observer.unrealiseShader();
359 typedef ReferencePair<FaceShaderObserver> FaceShaderObserverPair;
362 class ContentsFlagsValue
368 ContentsFlagsValue(int surfaceFlags, int contentFlags, int value, bool specified) :
369 m_surfaceFlags(surfaceFlags),
370 m_contentFlags(contentFlags),
372 m_specified(specified)
381 inline void ContentsFlagsValue_assignMasked(ContentsFlagsValue& flags, const ContentsFlagsValue& other)
383 bool detail = bitfield_enabled(flags.m_contentFlags, BRUSH_DETAIL_MASK);
387 flags.m_contentFlags = bitfield_enable(flags.m_contentFlags, BRUSH_DETAIL_MASK);
391 flags.m_contentFlags = bitfield_disable(flags.m_contentFlags, BRUSH_DETAIL_MASK);
396 class FaceShader : public ModuleObserver
402 CopiedString m_shader;
403 ContentsFlagsValue m_flags;
405 SavedState(const FaceShader& faceShader)
407 m_shader = faceShader.getShader();
408 m_flags = faceShader.m_flags;
411 void exportState(FaceShader& faceShader) const
413 faceShader.setShader(m_shader.c_str());
414 faceShader.setFlags(m_flags);
418 CopiedString m_shader;
420 ContentsFlagsValue m_flags;
421 FaceShaderObserverPair m_observers;
425 FaceShader(const char* shader, const ContentsFlagsValue& flags = ContentsFlagsValue(0, 0, 0, false)) :
438 // copy-construction not supported
439 FaceShader(const FaceShader& other);
441 void instanceAttach()
444 m_state->incrementUsed();
446 void instanceDetach()
448 m_state->decrementUsed();
454 ASSERT_MESSAGE(m_state == 0, "shader cannot be captured");
455 brush_check_shader(m_shader.c_str());
456 m_state = GlobalShaderCache().capture(m_shader.c_str());
457 m_state->attach(*this);
461 ASSERT_MESSAGE(m_state != 0, "shader cannot be released");
462 m_state->detach(*this);
463 GlobalShaderCache().release(m_shader.c_str());
469 ASSERT_MESSAGE(!m_realised, "FaceTexdef::realise: already realised");
471 m_observers.forEach(FaceShaderObserverRealise());
475 ASSERT_MESSAGE(m_realised, "FaceTexdef::unrealise: already unrealised");
476 m_observers.forEach(FaceShaderObserverUnrealise());
480 void attach(FaceShaderObserver& observer)
482 m_observers.attach(observer);
485 observer.realiseShader();
489 void detach(FaceShaderObserver& observer)
493 observer.unrealiseShader();
495 m_observers.detach(observer);
498 const char* getShader() const
500 return m_shader.c_str();
502 void setShader(const char* name)
506 m_state->decrementUsed();
513 m_state->incrementUsed();
516 ContentsFlagsValue getFlags() const
518 ASSERT_MESSAGE(m_realised, "FaceShader::getFlags: flags not valid when unrealised");
519 if(!m_flags.m_specified)
521 return ContentsFlagsValue(
522 m_state->getTexture().surfaceFlags,
523 m_state->getTexture().contentFlags,
524 m_state->getTexture().value,
530 void setFlags(const ContentsFlagsValue& flags)
532 ASSERT_MESSAGE(m_realised, "FaceShader::setFlags: flags not valid when unrealised");
533 ContentsFlagsValue_assignMasked(m_flags, flags);
536 Shader* state() const
541 std::size_t width() const
545 return m_state->getTexture().width;
549 std::size_t height() const
553 return m_state->getTexture().height;
557 unsigned int shaderFlags() const
561 return m_state->getFlags();
570 class FaceTexdef : public FaceShaderObserver
573 FaceTexdef(const FaceTexdef& other);
575 FaceTexdef& operator=(const FaceTexdef& other);
580 TextureProjection m_projection;
582 SavedState(const FaceTexdef& faceTexdef)
584 m_projection = faceTexdef.m_projection;
587 void exportState(FaceTexdef& faceTexdef) const
589 Texdef_Assign(faceTexdef.m_projection, m_projection);
593 FaceShader& m_shader;
594 TextureProjection m_projection;
595 bool m_projectionInitialised;
600 const TextureProjection& projection,
601 bool projectionInitialised = true
604 m_projection(projection),
605 m_projectionInitialised(projectionInitialised),
606 m_scaleApplied(false)
608 m_shader.attach(*this);
612 m_shader.detach(*this);
617 ASSERT_MESSAGE(!m_scaleApplied, "texture scale aready added");
618 m_scaleApplied = true;
619 m_projection.m_brushprimit_texdef.addScale(m_shader.width(), m_shader.height());
623 ASSERT_MESSAGE(m_scaleApplied, "texture scale aready removed");
624 m_scaleApplied = false;
625 m_projection.m_brushprimit_texdef.removeScale(m_shader.width(), m_shader.height());
630 if(m_projectionInitialised && !m_scaleApplied)
635 void unrealiseShader()
637 if(m_projectionInitialised && m_scaleApplied)
643 void setTexdef(const TextureProjection& projection)
646 Texdef_Assign(m_projection, projection);
650 void shift(float s, float t)
652 ASSERT_MESSAGE(texdef_sane(m_projection.m_texdef), "FaceTexdef::shift: bad texdef");
654 Texdef_Shift(m_projection, s, t);
658 void scale(float s, float t)
661 Texdef_Scale(m_projection, s, t);
665 void rotate(float angle)
668 Texdef_Rotate(m_projection, angle);
672 void fit(const Vector3& normal, const Winding& winding, float s_repeat, float t_repeat)
674 Texdef_FitTexture(m_projection, m_shader.width(), m_shader.height(), normal, winding, s_repeat, t_repeat);
677 void emitTextureCoordinates(Winding& winding, const Vector3& normal, const Matrix4& localToWorld)
679 Texdef_EmitTextureCoordinates(m_projection, m_shader.width(), m_shader.height(), winding, normal, localToWorld);
682 void transform(const Plane3& plane, const Matrix4& matrix)
685 Texdef_transformLocked(m_projection, m_shader.width(), m_shader.height(), plane, matrix);
689 TextureProjection normalised() const
691 brushprimit_texdef_t tmp(m_projection.m_brushprimit_texdef);
692 tmp.removeScale(m_shader.width(), m_shader.height());
693 return TextureProjection(m_projection.m_texdef, tmp, m_projection.m_basis_s, m_projection.m_basis_t);
695 void setBasis(const Vector3& normal)
698 Normal_GetTransform(normal, basis);
699 m_projection.m_basis_s = Vector3(basis.xx(), basis.yx(), basis.zx());
700 m_projection.m_basis_t = Vector3(-basis.xy(), -basis.yy(), -basis.zy());
704 inline void planepts_print(const PlanePoints& planePoints, TextOutputStream& ostream)
706 ostream << "( " << planePoints[0][0] << " " << planePoints[0][1] << " " << planePoints[0][2] << " ) "
707 << "( " << planePoints[1][0] << " " << planePoints[1][1] << " " << planePoints[1][2] << " ) "
708 << "( " << planePoints[2][0] << " " << planePoints[2][1] << " " << planePoints[2][2] << " )";
712 inline Plane3 Plane3_applyTranslation(const Plane3& plane, const Vector3& translation)
714 Plane3 tmp(plane3_translated(Plane3(plane.normal(), -plane.dist()), translation));
715 return Plane3(tmp.normal(), -tmp.dist());
718 inline Plane3 Plane3_applyTransform(const Plane3& plane, const Matrix4& matrix)
720 Plane3 tmp(plane3_transformed(Plane3(plane.normal(), -plane.dist()), matrix));
721 return Plane3(tmp.normal(), -tmp.dist());
726 PlanePoints m_planepts;
727 Plane3 m_planeCached;
730 Vector3 m_funcStaticOrigin;
732 static EBrushType m_type;
734 static bool isDoom3Plane()
736 return FacePlane::m_type == eBrushTypeDoom3 || FacePlane::m_type == eBrushTypeQuake4;
742 PlanePoints m_planepts;
745 SavedState(const FacePlane& facePlane)
747 if(facePlane.isDoom3Plane())
749 m_plane = facePlane.m_plane;
753 planepts_assign(m_planepts, facePlane.planePoints());
757 void exportState(FacePlane& facePlane) const
759 if(facePlane.isDoom3Plane())
761 facePlane.m_plane = m_plane;
762 facePlane.updateTranslated();
766 planepts_assign(facePlane.planePoints(), m_planepts);
767 facePlane.MakePlane();
772 FacePlane() : m_funcStaticOrigin(0, 0, 0)
775 FacePlane(const FacePlane& other) : m_funcStaticOrigin(0, 0, 0)
779 planepts_assign(m_planepts, other.m_planepts);
784 m_plane = other.m_plane;
794 if(check_plane_is_integer(m_planepts))
796 globalErrorStream() << "non-integer planepts: ";
797 planepts_print(m_planepts, globalErrorStream());
798 globalErrorStream() << "\n";
801 m_planeCached = plane3_for_points(m_planepts);
809 vector3_swap(m_planepts[0], m_planepts[2]);
814 m_planeCached = plane3_flipped(m_plane);
818 void transform(const Matrix4& matrix, bool mirror)
824 bool off = check_plane_is_integer(planePoints());
827 matrix4_transform_point(matrix, m_planepts[0]);
828 matrix4_transform_point(matrix, m_planepts[1]);
829 matrix4_transform_point(matrix, m_planepts[2]);
837 if(check_plane_is_integer(planePoints()))
841 globalErrorStream() << "caused by transform\n";
849 m_planeCached = Plane3_applyTransform(m_planeCached, matrix);
853 void offset(float offset)
857 Vector3 move(vector3_scaled(m_planeCached.normal(), -offset));
859 vector3_subtract(m_planepts[0], move);
860 vector3_subtract(m_planepts[1], move);
861 vector3_subtract(m_planepts[2], move);
867 m_planeCached.d += offset;
872 void updateTranslated()
874 m_planeCached = Plane3_applyTranslation(m_plane, m_funcStaticOrigin);
878 m_plane = Plane3_applyTranslation(m_planeCached, vector3_negated(m_funcStaticOrigin));
882 PlanePoints& planePoints()
886 const PlanePoints& planePoints() const
890 const Plane3& plane3() const
892 return m_planeCached;
894 void setDoom3Plane(const Plane3& plane)
899 const Plane3& getDoom3Plane() const
904 void copy(const FacePlane& other)
908 planepts_assign(m_planepts, other.m_planepts);
913 m_planeCached = other.m_plane;
917 void copy(const Vector3& p0, const Vector3& p1, const Vector3& p2)
928 m_planeCached = plane3_for_points(p2, p1, p0);
934 inline void Winding_testSelect(Winding& winding, SelectionTest& test, SelectionIntersection& best)
936 test.TestPolygon(VertexPointer(reinterpret_cast<VertexPointer::pointer>(&winding.points.data()->vertex), sizeof(WindingVertex)), winding.numpoints, best);
939 const double GRID_MIN = 0.125;
941 inline double quantiseInteger(double f)
943 return float_to_integer(f);
946 inline double quantiseFloating(double f)
948 return float_snapped(f, 1.f / (1 << 16));
951 typedef double (*QuantiseFunc)(double f);
958 virtual bool filter(const Face& face) const = 0;
961 bool face_filtered(Face& face);
962 void add_face_filter(FaceFilter& filter, int mask, bool invert = false);
964 void Brush_addTextureChangedCallback(const SignalHandler& callback);
965 void Brush_textureChanged();
968 extern bool g_brush_texturelock_enabled;
973 virtual void planeChanged() = 0;
974 virtual void connectivityChanged() = 0;
975 virtual void shaderChanged() = 0;
976 virtual void evaluateTransform() = 0;
980 public OpenGLRenderable,
983 public FaceShaderObserver
985 std::size_t m_refcount;
987 class SavedState : public UndoMemento
990 FacePlane::SavedState m_planeState;
991 FaceTexdef::SavedState m_texdefState;
992 FaceShader::SavedState m_shaderState;
994 SavedState(const Face& face) : m_planeState(face.getPlane()), m_texdefState(face.getTexdef()), m_shaderState(face.getShader())
998 void exportState(Face& face) const
1000 m_planeState.exportState(face.getPlane());
1001 m_shaderState.exportState(face.getShader());
1002 m_texdefState.exportState(face.getTexdef());
1012 static QuantiseFunc m_quantise;
1013 static EBrushType m_type;
1015 PlanePoints m_move_planepts;
1016 PlanePoints m_move_planeptsTransformed;
1019 FacePlane m_planeTransformed;
1020 FaceShader m_shader;
1021 FaceTexdef m_texdef;
1022 TextureProjection m_texdefTransformed;
1028 FaceObserver* m_observer;
1029 UndoObserver* m_undoable_observer;
1032 // assignment not supported
1033 Face& operator=(const Face& other);
1034 // copy-construction not supported
1035 Face(const Face& other);
1039 Face(FaceObserver* observer) :
1041 m_shader(texdef_name_default()),
1042 m_texdef(m_shader, TextureProjection(), false),
1044 m_observer(observer),
1045 m_undoable_observer(0),
1048 m_shader.attach(*this);
1049 m_plane.copy(Vector3(0, 0, 0), Vector3(64, 0, 0), Vector3(0, 64, 0));
1050 m_texdef.setBasis(m_plane.plane3().normal());
1058 const TextureProjection& projection,
1059 FaceObserver* observer
1063 m_texdef(m_shader, projection),
1064 m_observer(observer),
1065 m_undoable_observer(0),
1068 m_shader.attach(*this);
1069 m_plane.copy(p0, p1, p2);
1070 m_texdef.setBasis(m_plane.plane3().normal());
1074 Face(const Face& other, FaceObserver* observer) :
1076 m_shader(other.m_shader.getShader(), other.m_shader.m_flags),
1077 m_texdef(m_shader, other.getTexdef().normalised()),
1078 m_observer(observer),
1079 m_undoable_observer(0),
1082 m_shader.attach(*this);
1083 m_plane.copy(other.m_plane);
1084 planepts_assign(m_move_planepts, other.m_move_planepts);
1085 m_texdef.setBasis(m_plane.plane3().normal());
1091 m_shader.detach(*this);
1097 m_observer->planeChanged();
1100 void realiseShader()
1102 m_observer->shaderChanged();
1104 void unrealiseShader()
1108 void instanceAttach(MapFile* map)
1110 m_shader.instanceAttach();
1112 m_undoable_observer = GlobalUndoSystem().observer(this);
1113 GlobalFilterSystem().registerFilterable(*this);
1115 void instanceDetach(MapFile* map)
1117 GlobalFilterSystem().unregisterFilterable(*this);
1118 m_undoable_observer = 0;
1119 GlobalUndoSystem().release(this);
1121 m_shader.instanceDetach();
1124 void render(RenderStateFlags state) const
1126 Winding_Draw(m_winding, m_planeTransformed.plane3().normal(), state);
1129 void updateFiltered()
1131 m_filtered = face_filtered(*this);
1133 bool isFiltered() const
1144 if(m_undoable_observer != 0)
1146 m_undoable_observer->save(this);
1151 UndoMemento* exportState() const
1153 return new SavedState(*this);
1155 void importState(const UndoMemento* data)
1159 static_cast<const SavedState*>(data)->exportState(*this);
1162 m_observer->connectivityChanged();
1164 m_observer->shaderChanged();
1174 if(--m_refcount == 0)
1184 bool intersectVolume(const VolumeTest& volume, const Matrix4& localToWorld) const
1186 return volume.TestPlane(Plane3(plane3().normal(), -plane3().dist()), localToWorld);
1189 void render(Renderer& renderer, const Matrix4& localToWorld) const
1191 renderer.SetState(m_shader.state(), Renderer::eFullMaterials);
1192 renderer.addRenderable(*this, localToWorld);
1195 void transform(const Matrix4& matrix, bool mirror)
1197 if(g_brush_texturelock_enabled)
1199 Texdef_transformLocked(m_texdefTransformed, m_shader.width(), m_shader.height(), m_plane.plane3(), matrix);
1202 m_planeTransformed.transform(matrix, mirror);
1205 ASSERT_MESSAGE(projectionaxis_for_normal(normal) == projectionaxis_for_normal(plane3().normal()), "bleh");
1207 m_observer->planeChanged();
1210 void assign_planepts(const PlanePoints planepts)
1212 m_planeTransformed.copy(planepts[0], planepts[1], planepts[2]);
1213 m_observer->planeChanged();
1216 /// \brief Reverts the transformable state of the brush to identity.
1217 void revertTransform()
1219 m_planeTransformed = m_plane;
1220 planepts_assign(m_move_planeptsTransformed, m_move_planepts);
1221 m_texdefTransformed = m_texdef.m_projection;
1223 void freezeTransform()
1226 m_plane = m_planeTransformed;
1227 planepts_assign(m_move_planepts, m_move_planeptsTransformed);
1228 m_texdef.m_projection = m_texdefTransformed;
1231 void update_move_planepts_vertex(std::size_t index, PlanePoints planePoints)
1233 std::size_t numpoints = getWinding().numpoints;
1234 ASSERT_MESSAGE(index < numpoints, "update_move_planepts_vertex: invalid index");
1236 std::size_t opposite = Winding_Opposite(getWinding(), index);
1237 std::size_t adjacent = Winding_wrap(getWinding(), opposite+numpoints-1);
1238 planePoints[0] = getWinding()[opposite].vertex;
1239 planePoints[1] = getWinding()[index].vertex;
1240 planePoints[2] = getWinding()[adjacent].vertex;
1241 // winding points are very inaccurate, so they must be quantised before using them to generate the face-plane
1242 planepts_quantise(planePoints, GRID_MIN);
1245 void snapto(float snap)
1250 ASSERT_MESSAGE(plane3_valid(m_plane.plane3()), "invalid plane before snap to grid");
1251 planepts_snap(m_plane.planePoints(), snap);
1252 ASSERT_MESSAGE(plane3_valid(m_plane.plane3()), "invalid plane after snap to grid");
1254 PlanePoints planePoints;
1255 update_move_planepts_vertex(0, planePoints);
1256 vector3_snap(planePoints[0], snap);
1257 vector3_snap(planePoints[1], snap);
1258 vector3_snap(planePoints[2], snap);
1259 assign_planepts(planePoints);
1262 SceneChangeNotify();
1263 if(!plane3_valid(m_plane.plane3()))
1265 globalErrorStream() << "WARNING: invalid plane after snap to grid\n";
1270 void testSelect(SelectionTest& test, SelectionIntersection& best)
1272 Winding_testSelect(m_winding, test, best);
1275 void testSelect_centroid(SelectionTest& test, SelectionIntersection& best)
1277 test.TestPoint(m_centroid, best);
1280 void shaderChanged()
1282 EmitTextureCoordinates();
1283 Brush_textureChanged();
1284 m_observer->shaderChanged();
1286 SceneChangeNotify();
1289 const char* GetShader() const
1291 return m_shader.getShader();
1293 void SetShader(const char* name)
1296 m_shader.setShader(name);
1302 m_texdefTransformed = m_texdef.m_projection;
1304 void texdefChanged()
1307 EmitTextureCoordinates();
1308 Brush_textureChanged();
1311 void GetTexdef(TextureProjection& projection) const
1313 projection = m_texdef.normalised();
1315 void SetTexdef(const TextureProjection& projection)
1318 m_texdef.setTexdef(projection);
1322 void GetFlags(ContentsFlagsValue& flags) const
1324 flags = m_shader.getFlags();
1326 void SetFlags(const ContentsFlagsValue& flags)
1329 m_shader.setFlags(flags);
1330 m_observer->shaderChanged();
1334 void ShiftTexdef(float s, float t)
1337 m_texdef.shift(s, t);
1341 void ScaleTexdef(float s, float t)
1344 m_texdef.scale(s, t);
1348 void RotateTexdef(float angle)
1351 m_texdef.rotate(angle);
1355 void FitTexture(float s_repeat, float t_repeat)
1358 m_texdef.fit(m_plane.plane3().normal(), m_winding, s_repeat, t_repeat);
1362 void EmitTextureCoordinates()
1364 Texdef_EmitTextureCoordinates(m_texdefTransformed, m_shader.width(), m_shader.height(), m_winding, plane3().normal(), g_matrix4_identity);
1368 const Vector3& centroid() const
1373 void construct_centroid()
1375 Winding_Centroid(m_winding, plane3(), m_centroid);
1378 const Winding& getWinding() const
1382 Winding& getWinding()
1387 const Plane3& plane3() const
1389 m_observer->evaluateTransform();
1390 return m_planeTransformed.plane3();
1392 FacePlane& getPlane()
1396 const FacePlane& getPlane() const
1400 FaceTexdef& getTexdef()
1404 const FaceTexdef& getTexdef() const
1408 FaceShader& getShader()
1412 const FaceShader& getShader() const
1417 bool isDetail() const
1419 return (m_shader.m_flags.m_contentFlags & BRUSH_DETAIL_MASK) != 0;
1421 void setDetail(bool detail)
1424 if(detail && !isDetail())
1426 m_shader.m_flags.m_contentFlags |= BRUSH_DETAIL_MASK;
1428 else if(!detail && isDetail())
1430 m_shader.m_flags.m_contentFlags &= ~BRUSH_DETAIL_MASK;
1432 m_observer->shaderChanged();
1435 bool contributes() const
1437 return m_winding.numpoints > 2;
1439 bool is_bounded() const
1441 for(Winding::const_iterator i = m_winding.begin(); i != m_winding.end(); ++i)
1443 if((*i).adjacent == c_brush_maxFaces)
1456 std::size_t m_vertex;
1459 FaceVertexId(std::size_t face, std::size_t vertex)
1460 : m_face(face), m_vertex(vertex)
1464 std::size_t getFace() const
1468 std::size_t getVertex() const
1474 typedef std::size_t faceIndex_t;
1476 struct EdgeRenderIndices
1482 : first(0), second(0)
1485 EdgeRenderIndices(const RenderIndex _first, const RenderIndex _second)
1486 : first(_first), second(_second)
1497 : first(c_brush_maxFaces), second(c_brush_maxFaces)
1500 EdgeFaces(const faceIndex_t _first, const faceIndex_t _second)
1501 : first(_first), second(_second)
1506 class RenderableWireframe : public OpenGLRenderable
1509 void render(RenderStateFlags state) const
1512 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(PointVertex), &m_vertices->colour);
1513 glVertexPointer(3, GL_FLOAT, sizeof(PointVertex), &m_vertices->vertex);
1514 glDrawElements(GL_LINES, GLsizei(m_size<<1), RenderIndexTypeID, m_faceVertex.data());
1517 for(std::size_t i = 0; i < m_size; ++i)
1519 glVertex3fv(&m_vertices[m_faceVertex[i].first].vertex.x);
1520 glVertex3fv(&m_vertices[m_faceVertex[i].second].vertex.x);
1526 Array<EdgeRenderIndices> m_faceVertex;
1528 const PointVertex* m_vertices;
1532 typedef std::vector<Brush*> brush_vector_t;
1537 virtual bool filter(const Brush& brush) const = 0;
1540 bool brush_filtered(Brush& brush);
1541 void add_brush_filter(BrushFilter& filter, int mask, bool invert = false);
1544 /// \brief Returns true if 'self' takes priority when building brush b-rep.
1545 inline bool plane3_inside(const Plane3& self, const Plane3& other)
1547 if(vector3_equal_epsilon(self.normal(), other.normal(), 0.001))
1549 return self.dist() < other.dist();
1554 typedef SmartPointer<Face> FaceSmartPointer;
1555 typedef std::vector<FaceSmartPointer> Faces;
1557 /// \brief Returns the unique-id of the edge adjacent to \p faceVertex in the edge-pair for the set of \p faces.
1558 inline FaceVertexId next_edge(const Faces& faces, FaceVertexId faceVertex)
1560 std::size_t adjacent_face = faces[faceVertex.getFace()]->getWinding()[faceVertex.getVertex()].adjacent;
1561 std::size_t adjacent_vertex = Winding_FindAdjacent(faces[adjacent_face]->getWinding(), faceVertex.getFace());
1563 ASSERT_MESSAGE(adjacent_vertex != c_brush_maxFaces, "connectivity data invalid");
1564 if(adjacent_vertex == c_brush_maxFaces)
1569 return FaceVertexId(adjacent_face, adjacent_vertex);
1572 /// \brief Returns the unique-id of the vertex adjacent to \p faceVertex in the vertex-ring for the set of \p faces.
1573 inline FaceVertexId next_vertex(const Faces& faces, FaceVertexId faceVertex)
1575 FaceVertexId nextEdge = next_edge(faces, faceVertex);
1576 return FaceVertexId(nextEdge.getFace(), Winding_next(faces[nextEdge.getFace()]->getWinding(), nextEdge.getVertex()));
1579 class SelectableEdge
1581 Vector3 getEdge() const
1583 const Winding& winding = getFace().getWinding();
1584 return vector3_mid(winding[m_faceVertex.getVertex()].vertex, winding[Winding_next(winding, m_faceVertex.getVertex())].vertex);
1589 FaceVertexId m_faceVertex;
1591 SelectableEdge(Faces& faces, FaceVertexId faceVertex)
1592 : m_faces(faces), m_faceVertex(faceVertex)
1595 SelectableEdge& operator=(const SelectableEdge& other)
1597 m_faceVertex = other.m_faceVertex;
1601 Face& getFace() const
1603 return *m_faces[m_faceVertex.getFace()];
1606 void testSelect(SelectionTest& test, SelectionIntersection& best)
1608 test.TestPoint(getEdge(), best);
1612 class SelectableVertex
1614 Vector3 getVertex() const
1616 return getFace().getWinding()[m_faceVertex.getVertex()].vertex;
1621 FaceVertexId m_faceVertex;
1623 SelectableVertex(Faces& faces, FaceVertexId faceVertex)
1624 : m_faces(faces), m_faceVertex(faceVertex)
1627 SelectableVertex& operator=(const SelectableVertex& other)
1629 m_faceVertex = other.m_faceVertex;
1633 Face& getFace() const
1635 return *m_faces[m_faceVertex.getFace()];
1638 void testSelect(SelectionTest& test, SelectionIntersection& best)
1640 test.TestPoint(getVertex(), best);
1647 virtual void reserve(std::size_t size) = 0;
1648 virtual void clear() = 0;
1649 virtual void push_back(Face& face) = 0;
1650 virtual void pop_back() = 0;
1651 virtual void erase(std::size_t index) = 0;
1652 virtual void connectivityChanged() = 0;
1654 virtual void edge_clear() = 0;
1655 virtual void edge_push_back(SelectableEdge& edge) = 0;
1657 virtual void vertex_clear() = 0;
1658 virtual void vertex_push_back(SelectableVertex& vertex) = 0;
1660 virtual void DEBUG_verify() const = 0;
1666 virtual void visit(Face& face) const = 0;
1670 public TransformNode,
1675 public FaceObserver,
1681 scene::Node* m_node;
1682 typedef UniqueSet<BrushObserver*> Observers;
1683 Observers m_observers;
1684 UndoObserver* m_undoable_observer;
1691 // cached data compiled from state
1692 Array<PointVertex> m_faceCentroidPoints;
1693 RenderablePointArray m_render_faces;
1695 Array<PointVertex> m_uniqueVertexPoints;
1696 typedef std::vector<SelectableVertex> SelectableVertices;
1697 SelectableVertices m_select_vertices;
1698 RenderablePointArray m_render_vertices;
1700 Array<PointVertex> m_uniqueEdgePoints;
1701 typedef std::vector<SelectableEdge> SelectableEdges;
1702 SelectableEdges m_select_edges;
1703 RenderablePointArray m_render_edges;
1705 Array<EdgeRenderIndices> m_edge_indices;
1706 Array<EdgeFaces> m_edge_faces;
1711 Callback m_evaluateTransform;
1712 Callback m_boundsChanged;
1714 mutable bool m_planeChanged; // b-rep evaluation required
1715 mutable bool m_transformChanged; // transform evaluation required
1719 STRING_CONSTANT(Name, "Brush");
1721 Callback m_lightsChanged;
1724 static Shader* m_state_point;
1727 static EBrushType m_type;
1728 static double m_maxWorldCoord;
1730 Brush(scene::Node& node, const Callback& evaluateTransform, const Callback& boundsChanged) :
1732 m_undoable_observer(0),
1734 m_render_faces(m_faceCentroidPoints, GL_POINTS),
1735 m_render_vertices(m_uniqueVertexPoints, GL_POINTS),
1736 m_render_edges(m_uniqueEdgePoints, GL_POINTS),
1737 m_evaluateTransform(evaluateTransform),
1738 m_boundsChanged(boundsChanged),
1739 m_planeChanged(false),
1740 m_transformChanged(false)
1744 Brush(const Brush& other, scene::Node& node, const Callback& evaluateTransform, const Callback& boundsChanged) :
1746 m_undoable_observer(0),
1748 m_render_faces(m_faceCentroidPoints, GL_POINTS),
1749 m_render_vertices(m_uniqueVertexPoints, GL_POINTS),
1750 m_render_edges(m_uniqueEdgePoints, GL_POINTS),
1751 m_evaluateTransform(evaluateTransform),
1752 m_boundsChanged(boundsChanged),
1753 m_planeChanged(false),
1754 m_transformChanged(false)
1758 Brush(const Brush& other) :
1759 TransformNode(other),
1764 FaceObserver(other),
1769 m_undoable_observer(0),
1771 m_render_faces(m_faceCentroidPoints, GL_POINTS),
1772 m_render_vertices(m_uniqueVertexPoints, GL_POINTS),
1773 m_render_edges(m_uniqueEdgePoints, GL_POINTS),
1774 m_planeChanged(false),
1775 m_transformChanged(false)
1781 ASSERT_MESSAGE(m_observers.empty(), "Brush::~Brush: observers still attached");
1784 // assignment not supported
1785 Brush& operator=(const Brush& other);
1787 void setDoom3GroupOrigin(const Vector3& origin)
1789 //globalOutputStream() << "func_static origin before: " << m_funcStaticOrigin << " after: " << origin << "\n";
1790 for(Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i)
1792 (*i)->getPlane().m_funcStaticOrigin = origin;
1793 (*i)->getPlane().updateTranslated();
1794 (*i)->planeChanged();
1799 void attach(BrushObserver& observer)
1801 for(Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i)
1803 observer.push_back(*(*i));
1806 for(SelectableEdges::iterator i = m_select_edges.begin(); i !=m_select_edges.end(); ++i)
1808 observer.edge_push_back(*i);
1811 for(SelectableVertices::iterator i = m_select_vertices.begin(); i != m_select_vertices.end(); ++i)
1813 observer.vertex_push_back(*i);
1816 m_observers.insert(&observer);
1818 void detach(BrushObserver& observer)
1820 m_observers.erase(&observer);
1823 void forEachFace(const BrushVisitor& visitor) const
1825 for(Faces::const_iterator i = m_faces.begin(); i != m_faces.end(); ++i)
1827 visitor.visit(*(*i));
1831 void forEachFace_instanceAttach(MapFile* map) const
1833 for(Faces::const_iterator i = m_faces.begin(); i != m_faces.end(); ++i)
1835 (*i)->instanceAttach(map);
1838 void forEachFace_instanceDetach(MapFile* map) const
1840 for(Faces::const_iterator i = m_faces.begin(); i != m_faces.end(); ++i)
1842 (*i)->instanceDetach(map);
1846 InstanceCounter m_instanceCounter;
1847 void instanceAttach(const scene::Path& path)
1849 if(++m_instanceCounter.m_count == 1)
1851 m_map = path_find_mapfile(path.begin(), path.end());
1852 m_undoable_observer = GlobalUndoSystem().observer(this);
1853 GlobalFilterSystem().registerFilterable(*this);
1854 forEachFace_instanceAttach(m_map);
1858 ASSERT_MESSAGE(path_find_mapfile(path.begin(), path.end()) == m_map, "node is instanced across more than one file");
1861 void instanceDetach(const scene::Path& path)
1863 if(--m_instanceCounter.m_count == 0)
1865 forEachFace_instanceDetach(m_map);
1866 GlobalFilterSystem().unregisterFilterable(*this);
1868 m_undoable_observer = 0;
1869 GlobalUndoSystem().release(this);
1874 const char* name() const
1878 void attach(const NameCallback& callback)
1881 void detach(const NameCallback& callback)
1886 void updateFiltered()
1890 if(brush_filtered(*this))
1892 m_node->enable(scene::Node::eFiltered);
1896 m_node->disable(scene::Node::eFiltered);
1904 m_planeChanged = true;
1908 void shaderChanged()
1913 void evaluateBRep() const
1917 m_planeChanged = false;
1918 const_cast<Brush*>(this)->buildBRep();
1922 void transformChanged()
1924 m_transformChanged = true;
1927 typedef MemberCaller<Brush, &Brush::transformChanged> TransformChangedCaller;
1929 void evaluateTransform()
1931 if(m_transformChanged)
1933 m_transformChanged = false;
1935 m_evaluateTransform();
1938 const Matrix4& localToParent() const
1940 return g_matrix4_identity;
1946 const AABB& localAABB() const
1949 return m_aabb_local;
1952 VolumeIntersectionValue intersectVolume(const VolumeTest& test, const Matrix4& localToWorld) const
1954 return test.TestAABB(m_aabb_local, localToWorld);
1957 void renderComponents(SelectionSystem::EComponentMode mode, Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld) const
1961 case SelectionSystem::eVertex:
1962 renderer.addRenderable(m_render_vertices, localToWorld);
1964 case SelectionSystem::eEdge:
1965 renderer.addRenderable(m_render_edges, localToWorld);
1967 case SelectionSystem::eFace:
1968 renderer.addRenderable(m_render_faces, localToWorld);
1975 void transform(const Matrix4& matrix)
1977 bool mirror = matrix4_handedness(matrix) == MATRIX4_LEFTHANDED;
1979 for(Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i)
1981 (*i)->transform(matrix, mirror);
1984 void snapto(float snap)
1986 for(Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i)
1991 void revertTransform()
1993 for(Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i)
1995 (*i)->revertTransform();
1998 void freezeTransform()
2000 for(Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i)
2002 (*i)->freezeTransform();
2006 /// \brief Returns the absolute index of the \p faceVertex.
2007 std::size_t absoluteIndex(FaceVertexId faceVertex)
2009 std::size_t index = 0;
2010 for(std::size_t i = 0; i < faceVertex.getFace(); ++i)
2012 index += m_faces[i]->getWinding().numpoints;
2014 return index + faceVertex.getVertex();
2017 void appendFaces(const Faces& other)
2020 for(Faces::const_iterator i = other.begin(); i != other.end(); ++i)
2026 /// \brief The undo memento for a brush stores only the list of face references - the faces are not copied.
2027 class BrushUndoMemento : public UndoMemento
2030 BrushUndoMemento(const Faces& faces) : m_faces(faces)
2047 if(m_undoable_observer != 0)
2049 m_undoable_observer->save(this);
2053 UndoMemento* exportState() const
2055 return new BrushUndoMemento(m_faces);
2058 void importState(const UndoMemento* state)
2061 appendFaces(static_cast<const BrushUndoMemento*>(state)->m_faces);
2064 for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i)
2066 (*i)->DEBUG_verify();
2072 return !m_faces.empty() && m_faces.front()->isDetail();
2075 /// \brief Appends a copy of \p face to the end of the face list.
2076 Face* addFace(const Face& face)
2078 if(m_faces.size() == c_brush_maxFaces)
2083 push_back(FaceSmartPointer(new Face(face, this)));
2084 m_faces.back()->setDetail(isDetail());
2086 return m_faces.back();
2089 /// \brief Appends a new face constructed from the parameters to the end of the face list.
2090 Face* addPlane(const Vector3& p0, const Vector3& p1, const Vector3& p2, const char* shader, const TextureProjection& projection)
2092 if(m_faces.size() == c_brush_maxFaces)
2097 push_back(FaceSmartPointer(new Face(p0, p1, p2, shader, projection, this)));
2098 m_faces.back()->setDetail(isDetail());
2100 return m_faces.back();
2103 static void constructStatic(EBrushType type)
2106 Face::m_type = type;
2107 FacePlane::m_type = type;
2109 g_bp_globals.m_texdefTypeId = TEXDEFTYPEID_QUAKE;
2110 if(m_type == eBrushTypeQuake3BP || m_type == eBrushTypeDoom3 || m_type == eBrushTypeQuake4)
2112 g_bp_globals.m_texdefTypeId = TEXDEFTYPEID_BRUSHPRIMITIVES;
2113 g_brush_texturelock_enabled = true;
2115 else if(m_type == eBrushTypeHalfLife)
2117 g_bp_globals.m_texdefTypeId = TEXDEFTYPEID_HALFLIFE;
2118 g_brush_texturelock_enabled = true;
2121 Face::m_quantise = (m_type == eBrushTypeQuake) ? quantiseInteger : quantiseFloating;
2123 m_state_point = GlobalShaderCache().capture("$POINT");
2125 static void destroyStatic()
2127 GlobalShaderCache().release("$POINT");
2130 std::size_t DEBUG_size()
2132 return m_faces.size();
2135 typedef Faces::const_iterator const_iterator;
2137 const_iterator begin() const
2139 return m_faces.begin();
2141 const_iterator end() const
2143 return m_faces.end();
2148 return m_faces.back();
2150 const Face* back() const
2152 return m_faces.back();
2154 void reserve(std::size_t count)
2156 m_faces.reserve(count);
2157 for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i)
2159 (*i)->reserve(count);
2162 void push_back(Faces::value_type face)
2164 m_faces.push_back(face);
2165 if(m_instanceCounter.m_count != 0)
2167 m_faces.back()->instanceAttach(m_map);
2169 for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i)
2171 (*i)->push_back(*face);
2172 (*i)->DEBUG_verify();
2177 if(m_instanceCounter.m_count != 0)
2179 m_faces.back()->instanceDetach(m_map);
2182 for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i)
2185 (*i)->DEBUG_verify();
2188 void erase(std::size_t index)
2190 if(m_instanceCounter.m_count != 0)
2192 m_faces[index]->instanceDetach(m_map);
2194 m_faces.erase(m_faces.begin() + index);
2195 for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i)
2198 (*i)->DEBUG_verify();
2201 void connectivityChanged()
2203 for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i)
2205 (*i)->connectivityChanged();
2213 if(m_instanceCounter.m_count != 0)
2215 forEachFace_instanceDetach(m_map);
2218 for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i)
2221 (*i)->DEBUG_verify();
2224 std::size_t size() const
2226 return m_faces.size();
2230 return m_faces.empty();
2233 /// \brief Returns true if any face of the brush contributes to the final B-Rep.
2234 bool hasContributingFaces() const
2236 for(const_iterator i = begin(); i != end(); ++i)
2238 if((*i)->contributes())
2246 /// \brief Removes faces that do not contribute to the brush. This is useful for cleaning up after CSG operations on the brush.
2247 /// 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.
2248 void removeEmptyFaces()
2254 while(i < m_faces.size())
2256 if(!m_faces[i]->contributes())
2269 /// \brief Constructs \p winding from the intersection of \p plane with the other planes of the brush.
2270 void windingForClipPlane(Winding& winding, const Plane3& plane) const
2272 FixedWinding buffer[2];
2275 // get a poly that covers an effectively infinite area
2276 Winding_createInfinite(buffer[swap], plane, m_maxWorldCoord + 1);
2278 // chop the poly by all of the other faces
2280 for (std::size_t i = 0; i < m_faces.size(); ++i)
2282 const Face& clip = *m_faces[i];
2284 if(plane3_equal(clip.plane3(), plane)
2285 || !plane3_valid(clip.plane3()) || !plane_unique(i)
2286 || plane3_opposing(plane, clip.plane3()))
2291 buffer[!swap].clear();
2293 #if BRUSH_CONNECTIVITY_DEBUG
2294 globalOutputStream() << "clip vs face: " << i << "\n";
2298 // flip the plane, because we want to keep the back side
2299 Plane3 clipPlane(vector3_negated(clip.plane3().normal()), -clip.plane3().dist());
2300 Winding_Clip(buffer[swap], plane, clipPlane, i, buffer[!swap]);
2303 #if BRUSH_CONNECTIVITY_DEBUG
2304 for(FixedWinding::Points::iterator k = buffer[!swap].points.begin(), j = buffer[!swap].points.end() - 1; k != buffer[!swap].points.end(); j = k, ++k)
2306 if(vector3_length_squared(vector3_subtracted((*k).vertex, (*j).vertex)) < 1)
2308 globalOutputStream() << "v: " << std::distance(buffer[!swap].points.begin(), j) << " tiny edge adjacent to face " << (*j).adjacent << "\n";
2313 //ASSERT_MESSAGE(buffer[!swap].numpoints != 1, "created single-point winding");
2319 Winding_forFixedWinding(winding, buffer[swap]);
2321 #if BRUSH_CONNECTIVITY_DEBUG
2322 Winding_printConnectivity(winding);
2324 for(Winding::iterator i = winding.begin(), j = winding.end() - 1; i != winding.end(); j = i, ++i)
2326 if(vector3_length_squared(vector3_subtracted((*i).vertex, (*j).vertex)) < 1)
2328 globalOutputStream() << "v: " << std::distance(winding.begin(), j) << " tiny edge adjacent to face " << (*j).adjacent << "\n";
2334 void update_wireframe(RenderableWireframe& wire, const bool* faces_visible) const
2336 wire.m_faceVertex.resize(m_edge_indices.size());
2337 wire.m_vertices = m_uniqueVertexPoints.data();
2339 for(std::size_t i = 0; i < m_edge_faces.size(); ++i)
2341 if(faces_visible[m_edge_faces[i].first]
2342 || faces_visible[m_edge_faces[i].second])
2344 wire.m_faceVertex[wire.m_size++] = m_edge_indices[i];
2350 void update_faces_wireframe(Array<PointVertex>& wire, const bool* faces_visible) const
2352 std::size_t count = 0;
2353 for(std::size_t i = 0; i < m_faceCentroidPoints.size(); ++i)
2355 if(faces_visible[i])
2362 Array<PointVertex>::iterator p = wire.begin();
2363 for(std::size_t i = 0; i < m_faceCentroidPoints.size(); ++i)
2365 if(faces_visible[i])
2367 *p++ = m_faceCentroidPoints[i];
2372 /// \brief Makes this brush a deep-copy of the \p other.
2373 void copy(const Brush& other)
2375 for(Faces::const_iterator i = other.m_faces.begin(); i != other.m_faces.end(); ++i)
2383 void edge_push_back(FaceVertexId faceVertex)
2385 m_select_edges.push_back(SelectableEdge(m_faces, faceVertex));
2386 for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i)
2388 (*i)->edge_push_back(m_select_edges.back());
2393 m_select_edges.clear();
2394 for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i)
2399 void vertex_push_back(FaceVertexId faceVertex)
2401 m_select_vertices.push_back(SelectableVertex(m_faces, faceVertex));
2402 for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i)
2404 (*i)->vertex_push_back(m_select_vertices.back());
2409 m_select_vertices.clear();
2410 for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i)
2412 (*i)->vertex_clear();
2416 /// \brief Returns true if the face identified by \p index is preceded by another plane that takes priority over it.
2417 bool plane_unique(std::size_t index) const
2420 for(std::size_t i = 0; i < m_faces.size(); ++i)
2422 if(index != i && !plane3_inside(m_faces[index]->plane3(), m_faces[i]->plane3()))
2430 /// \brief Removes edges that are smaller than the tolerance used when generating brush windings.
2431 void removeDegenerateEdges()
2433 for (std::size_t i = 0; i < m_faces.size(); ++i)
2435 Winding& winding = m_faces[i]->getWinding();
2436 for(Winding::iterator j = winding.begin(); j != winding.end();)
2438 std::size_t index = std::distance(winding.begin(), j);
2439 std::size_t next = Winding_next(winding, index);
2440 if(Edge_isDegenerate(winding[index].vertex, winding[next].vertex))
2442 #if BRUSH_DEGENERATE_DEBUG
2443 globalOutputStream() << "Brush::buildWindings: face " << i << ": degenerate edge adjacent to " << winding[index].adjacent << "\n";
2445 Winding& other = m_faces[winding[index].adjacent]->getWinding();
2446 std::size_t adjacent = Winding_FindAdjacent(other, i);
2447 if(adjacent != c_brush_maxFaces)
2449 other.erase(other.begin() + adjacent);
2461 /// \brief Invalidates faces that have only two vertices in their winding, while preserving edge-connectivity information.
2462 void removeDegenerateFaces()
2464 // save adjacency info for degenerate faces
2465 for (std::size_t i = 0; i < m_faces.size(); ++i)
2467 Winding& degen = m_faces[i]->getWinding();
2469 if(degen.numpoints == 2)
2471 #if BRUSH_DEGENERATE_DEBUG
2472 globalOutputStream() << "Brush::buildWindings: face " << i << ": degenerate winding adjacent to " << degen[0].adjacent << ", " << degen[1].adjacent << "\n";
2474 // this is an "edge" face, where the plane touches the edge of the brush
2476 Winding& winding = m_faces[degen[0].adjacent]->getWinding();
2477 std::size_t index = Winding_FindAdjacent(winding, i);
2478 if(index != c_brush_maxFaces)
2480 #if BRUSH_DEGENERATE_DEBUG
2481 globalOutputStream() << "Brush::buildWindings: face " << degen[0].adjacent << ": remapping adjacent " << winding[index].adjacent << " to " << degen[1].adjacent << "\n";
2483 winding[index].adjacent = degen[1].adjacent;
2488 Winding& winding = m_faces[degen[1].adjacent]->getWinding();
2489 std::size_t index = Winding_FindAdjacent(winding, i);
2490 if(index != c_brush_maxFaces)
2492 #if BRUSH_DEGENERATE_DEBUG
2493 globalOutputStream() << "Brush::buildWindings: face " << degen[1].adjacent << ": remapping adjacent " << winding[index].adjacent << " to " << degen[0].adjacent << "\n";
2495 winding[index].adjacent = degen[0].adjacent;
2504 /// \brief Removes edges that have the same adjacent-face as their immediate neighbour.
2505 void removeDuplicateEdges()
2507 // verify face connectivity graph
2508 for(std::size_t i = 0; i < m_faces.size(); ++i)
2510 //if(m_faces[i]->contributes())
2512 Winding& winding = m_faces[i]->getWinding();
2513 for(std::size_t j = 0; j != winding.numpoints;)
2515 std::size_t next = Winding_next(winding, j);
2516 if(winding[j].adjacent == winding[next].adjacent)
2518 #if BRUSH_DEGENERATE_DEBUG
2519 globalOutputStream() << "Brush::buildWindings: face " << i << ": removed duplicate edge adjacent to face " << winding[j].adjacent << "\n";
2521 winding.erase(winding.begin() + next);
2532 /// \brief Removes edges that do not have a matching pair in their adjacent-face.
2533 void verifyConnectivityGraph()
2535 // verify face connectivity graph
2536 for(std::size_t i = 0; i < m_faces.size(); ++i)
2538 //if(m_faces[i]->contributes())
2540 Winding& winding = m_faces[i]->getWinding();
2541 for(Winding::iterator j = winding.begin(); j != winding.end();)
2543 #if BRUSH_CONNECTIVITY_DEBUG
2544 globalOutputStream() << "Brush::buildWindings: face " << i << ": adjacent to face " << (*j).adjacent << "\n";
2546 // remove unidirectional graph edges
2547 if((*j).adjacent == c_brush_maxFaces
2548 || Winding_FindAdjacent(m_faces[(*j).adjacent]->getWinding(), i) == c_brush_maxFaces)
2550 #if BRUSH_CONNECTIVITY_DEBUG
2551 globalOutputStream() << "Brush::buildWindings: face " << i << ": removing unidirectional connectivity graph edge adjacent to face " << (*j).adjacent << "\n";
2564 /// \brief Returns true if the brush is a finite volume. A brush without a finite volume extends past the maximum world bounds and is not valid.
2567 for(const_iterator i = begin(); i != end(); ++i)
2569 if(!(*i)->is_bounded())
2577 /// \brief Constructs the polygon windings for each face of the brush. Also updates the brush bounding-box and face texture-coordinates.
2578 bool buildWindings()
2582 m_aabb_local = AABB();
2584 for (std::size_t i = 0; i < m_faces.size(); ++i)
2586 Face& f = *m_faces[i];
2588 if(!plane3_valid(f.plane3()) || !plane_unique(i))
2590 f.getWinding().resize(0);
2594 #if BRUSH_CONNECTIVITY_DEBUG
2595 globalOutputStream() << "face: " << i << "\n";
2597 windingForClipPlane(f.getWinding(), f.plane3());
2599 // update brush bounds
2600 const Winding& winding = f.getWinding();
2601 for(Winding::const_iterator i = winding.begin(); i != winding.end(); ++i)
2603 aabb_extend_by_point_safe(m_aabb_local, (*i).vertex);
2606 // update texture coordinates
2607 f.EmitTextureCoordinates();
2612 bool degenerate = !isBounded();
2616 // clean up connectivity information.
2617 // these cleanups must be applied in a specific order.
2618 removeDegenerateEdges();
2619 removeDegenerateFaces();
2620 removeDuplicateEdges();
2621 verifyConnectivityGraph();
2627 /// \brief Constructs the face windings and updates anything that depends on them.
2635 class FaceInstanceSet
2637 typedef SelectionList<FaceInstance> FaceInstances;
2638 FaceInstances m_faceInstances;
2640 void insert(FaceInstance& faceInstance)
2642 m_faceInstances.append(faceInstance);
2644 void erase(FaceInstance& faceInstance)
2646 m_faceInstances.erase(faceInstance);
2649 template<typename Functor>
2650 void foreach(Functor functor)
2652 for(FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
2660 return m_faceInstances.empty();
2662 FaceInstance& last() const
2664 return m_faceInstances.back();
2668 extern FaceInstanceSet g_SelectedFaceInstances;
2670 typedef std::list<std::size_t> VertexSelection;
2672 inline VertexSelection::iterator VertexSelection_find(VertexSelection& self, std::size_t value)
2674 return std::find(self.begin(), self.end(), value);
2677 inline VertexSelection::const_iterator VertexSelection_find(const VertexSelection& self, std::size_t value)
2679 return std::find(self.begin(), self.end(), value);
2682 inline VertexSelection::iterator VertexSelection_insert(VertexSelection& self, std::size_t value)
2684 VertexSelection::iterator i = VertexSelection_find(self, value);
2687 self.push_back(value);
2688 return --self.end();
2692 inline void VertexSelection_erase(VertexSelection& self, std::size_t value)
2694 VertexSelection::iterator i = VertexSelection_find(self, value);
2701 inline bool triangle_reversed(std::size_t x, std::size_t y, std::size_t z)
2703 return !((x < y && y < z) || (z < x && x < y) || (y < z && z < x));
2705 template<typename Element>
2706 inline Vector3 triangle_cross(const BasicVector3<Element>& x, const BasicVector3<Element> y, const BasicVector3<Element>& z)
2708 return vector3_cross(y - x, z - x);
2710 template<typename Element>
2711 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)
2713 return vector3_dot(triangle_cross(x1, y1, z1), triangle_cross(x2, y2, z2)) > 0;
2717 typedef const Plane3* PlanePointer;
2718 typedef PlanePointer* PlanesIterator;
2720 class VectorLightList : public LightList
2722 typedef std::vector<const RendererLight*> Lights;
2725 void addLight(const RendererLight& light)
2727 m_lights.push_back(&light);
2733 void evaluateLights() const
2736 void lightsChanged() const
2739 void forEachLight(const RendererLightCallback& callback) const
2741 for(Lights::const_iterator i = m_lights.begin(); i != m_lights.end(); ++i)
2751 ObservedSelectable m_selectable;
2752 ObservedSelectable m_selectableVertices;
2753 ObservedSelectable m_selectableEdges;
2754 SelectionChangeCallback m_selectionChanged;
2756 VertexSelection m_vertexSelection;
2757 VertexSelection m_edgeSelection;
2760 mutable VectorLightList m_lights;
2762 FaceInstance(Face& face, const SelectionChangeCallback& observer) :
2764 m_selectable(SelectedChangedCaller(*this)),
2765 m_selectableVertices(observer),
2766 m_selectableEdges(observer),
2767 m_selectionChanged(observer)
2770 FaceInstance(const FaceInstance& other) :
2771 m_face(other.m_face),
2772 m_selectable(SelectedChangedCaller(*this)),
2773 m_selectableVertices(other.m_selectableVertices),
2774 m_selectableEdges(other.m_selectableEdges),
2775 m_selectionChanged(other.m_selectionChanged)
2778 FaceInstance& operator=(const FaceInstance& other)
2780 m_face = other.m_face;
2788 const Face& getFace() const
2793 void selectedChanged(const Selectable& selectable)
2795 if(selectable.isSelected())
2797 g_SelectedFaceInstances.insert(*this);
2801 g_SelectedFaceInstances.erase(*this);
2803 m_selectionChanged(selectable);
2805 typedef MemberCaller1<FaceInstance, const Selectable&, &FaceInstance::selectedChanged> SelectedChangedCaller;
2807 bool selectedVertices() const
2809 return !m_vertexSelection.empty();
2811 bool selectedEdges() const
2813 return !m_edgeSelection.empty();
2815 bool isSelected() const
2817 return m_selectable.isSelected();
2820 bool selectedComponents() const
2822 return selectedVertices() || selectedEdges() || isSelected();
2824 bool selectedComponents(SelectionSystem::EComponentMode mode) const
2828 case SelectionSystem::eVertex:
2829 return selectedVertices();
2830 case SelectionSystem::eEdge:
2831 return selectedEdges();
2832 case SelectionSystem::eFace:
2833 return isSelected();
2838 void setSelected(SelectionSystem::EComponentMode mode, bool select)
2842 case SelectionSystem::eFace:
2843 m_selectable.setSelected(select);
2845 case SelectionSystem::eVertex:
2846 ASSERT_MESSAGE(!select, "select-all not supported");
2848 m_vertexSelection.clear();
2849 m_selectableVertices.setSelected(false);
2851 case SelectionSystem::eEdge:
2852 ASSERT_MESSAGE(!select, "select-all not supported");
2854 m_edgeSelection.clear();
2855 m_selectableEdges.setSelected(false);
2862 template<typename Functor>
2863 void SelectedVertices_foreach(Functor functor) const
2865 for(VertexSelection::const_iterator i = m_vertexSelection.begin(); i != m_vertexSelection.end(); ++i)
2867 std::size_t index = Winding_FindAdjacent(getFace().getWinding(), *i);
2868 if(index != c_brush_maxFaces)
2870 functor(getFace().getWinding()[index].vertex);
2874 template<typename Functor>
2875 void SelectedEdges_foreach(Functor functor) const
2877 for(VertexSelection::const_iterator i = m_edgeSelection.begin(); i != m_edgeSelection.end(); ++i)
2879 std::size_t index = Winding_FindAdjacent(getFace().getWinding(), *i);
2880 if(index != c_brush_maxFaces)
2882 const Winding& winding = getFace().getWinding();
2883 std::size_t adjacent = Winding_next(winding, index);
2884 functor(vector3_mid(winding[index].vertex, winding[adjacent].vertex));
2888 template<typename Functor>
2889 void SelectedFaces_foreach(Functor functor) const
2893 functor(centroid());
2897 template<typename Functor>
2898 void SelectedComponents_foreach(Functor functor) const
2900 SelectedVertices_foreach(functor);
2901 SelectedEdges_foreach(functor);
2902 SelectedFaces_foreach(functor);
2905 void iterate_selected(AABB& aabb) const
2907 SelectedComponents_foreach(AABBExtendByPoint(aabb));
2910 class RenderablePointVectorPushBack
2912 RenderablePointVector& m_points;
2914 RenderablePointVectorPushBack(RenderablePointVector& points) : m_points(points)
2917 void operator()(const Vector3& point) const
2919 const Colour4b colour_selected(0, 0, 255, 255);
2920 m_points.push_back(pointvertex_for_windingpoint(point, colour_selected));
2924 void iterate_selected(RenderablePointVector& points) const
2926 SelectedComponents_foreach(RenderablePointVectorPushBack(points));
2929 bool intersectVolume(const VolumeTest& volume, const Matrix4& localToWorld) const
2931 return m_face->intersectVolume(volume, localToWorld);
2934 void render(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld) const
2936 if(!m_face->isFiltered() && m_face->contributes() && intersectVolume(volume, localToWorld))
2938 renderer.PushState();
2939 if(selectedComponents())
2941 renderer.Highlight(Renderer::eFace);
2943 m_face->render(renderer, localToWorld);
2944 renderer.PopState();
2948 void testSelect(SelectionTest& test, SelectionIntersection& best)
2950 if(!m_face->isFiltered())
2952 m_face->testSelect(test, best);
2955 void testSelect(Selector& selector, SelectionTest& test)
2957 SelectionIntersection best;
2958 testSelect(test, best);
2961 Selector_add(selector, m_selectable, best);
2964 void testSelect_centroid(Selector& selector, SelectionTest& test)
2966 if(m_face->contributes() && !m_face->isFiltered())
2968 SelectionIntersection best;
2969 m_face->testSelect_centroid(test, best);
2972 Selector_add(selector, m_selectable, best);
2977 void selectPlane(Selector& selector, const Line& line, PlanesIterator first, PlanesIterator last, const PlaneCallback& selectedPlaneCallback)
2979 for(Winding::const_iterator i = getFace().getWinding().begin(); i != getFace().getWinding().end(); ++i)
2981 Vector3 v(vector3_subtracted(line_closest_point(line, (*i).vertex), (*i).vertex));
2982 double dot = vector3_dot(getFace().plane3().normal(), v);
2989 Selector_add(selector, m_selectable);
2991 selectedPlaneCallback(getFace().plane3());
2993 void selectReversedPlane(Selector& selector, const SelectedPlanes& selectedPlanes)
2995 if(selectedPlanes.contains(plane3_flipped(getFace().plane3())))
2997 Selector_add(selector, m_selectable);
3001 void transformComponents(const Matrix4& matrix)
3005 m_face->transform(matrix, false);
3007 if(selectedVertices())
3009 if(m_vertexSelection.size() == 1)
3011 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[1]);
3012 m_face->assign_planepts(m_face->m_move_planeptsTransformed);
3014 else if(m_vertexSelection.size() == 2)
3016 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[1]);
3017 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[2]);
3018 m_face->assign_planepts(m_face->m_move_planeptsTransformed);
3020 else if(m_vertexSelection.size() >= 3)
3022 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[0]);
3023 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[1]);
3024 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[2]);
3025 m_face->assign_planepts(m_face->m_move_planeptsTransformed);
3030 if(m_edgeSelection.size() == 1)
3032 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[0]);
3033 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[1]);
3034 m_face->assign_planepts(m_face->m_move_planeptsTransformed);
3036 else if(m_edgeSelection.size() >= 2)
3038 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[0]);
3039 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[1]);
3040 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[2]);
3041 m_face->assign_planepts(m_face->m_move_planeptsTransformed);
3046 void snapto(float snap)
3048 m_face->snapto(snap);
3051 void snapComponents(float snap)
3057 if(selectedVertices())
3059 vector3_snap(m_face->m_move_planepts[0], snap);
3060 vector3_snap(m_face->m_move_planepts[1], snap);
3061 vector3_snap(m_face->m_move_planepts[2], snap);
3062 m_face->assign_planepts(m_face->m_move_planepts);
3063 planepts_assign(m_face->m_move_planeptsTransformed, m_face->m_move_planepts);
3064 m_face->freezeTransform();
3068 vector3_snap(m_face->m_move_planepts[0], snap);
3069 vector3_snap(m_face->m_move_planepts[1], snap);
3070 vector3_snap(m_face->m_move_planepts[2], snap);
3071 m_face->assign_planepts(m_face->m_move_planepts);
3072 planepts_assign(m_face->m_move_planeptsTransformed, m_face->m_move_planepts);
3073 m_face->freezeTransform();
3076 void update_move_planepts_vertex(std::size_t index)
3078 m_face->update_move_planepts_vertex(index, m_face->m_move_planepts);
3080 void update_move_planepts_vertex2(std::size_t index, std::size_t other)
3082 const std::size_t numpoints = m_face->getWinding().numpoints;
3083 ASSERT_MESSAGE(index < numpoints, "select_vertex: invalid index");
3085 const std::size_t opposite = Winding_Opposite(m_face->getWinding(), index, other);
3087 if(triangle_reversed(index, other, opposite))
3089 std::swap(index, other);
3093 triangles_same_winding(
3094 m_face->getWinding()[opposite].vertex,
3095 m_face->getWinding()[index].vertex,
3096 m_face->getWinding()[other].vertex,
3097 m_face->getWinding()[0].vertex,
3098 m_face->getWinding()[1].vertex,
3099 m_face->getWinding()[2].vertex
3101 "update_move_planepts_vertex2: error"
3104 m_face->m_move_planepts[0] = m_face->getWinding()[opposite].vertex;
3105 m_face->m_move_planepts[1] = m_face->getWinding()[index].vertex;
3106 m_face->m_move_planepts[2] = m_face->getWinding()[other].vertex;
3107 planepts_quantise(m_face->m_move_planepts, GRID_MIN); // winding points are very inaccurate
3109 void update_selection_vertex()
3111 if(m_vertexSelection.size() == 0)
3113 m_selectableVertices.setSelected(false);
3117 m_selectableVertices.setSelected(true);
3119 if(m_vertexSelection.size() == 1)
3121 std::size_t index = Winding_FindAdjacent(getFace().getWinding(), *m_vertexSelection.begin());
3123 if(index != c_brush_maxFaces)
3125 update_move_planepts_vertex(index);
3128 else if(m_vertexSelection.size() == 2)
3130 std::size_t index = Winding_FindAdjacent(getFace().getWinding(), *m_vertexSelection.begin());
3131 std::size_t other = Winding_FindAdjacent(getFace().getWinding(), *(++m_vertexSelection.begin()));
3133 if(index != c_brush_maxFaces
3134 && other != c_brush_maxFaces)
3136 update_move_planepts_vertex2(index, other);
3141 void select_vertex(std::size_t index, bool select)
3145 VertexSelection_insert(m_vertexSelection, getFace().getWinding()[index].adjacent);
3149 VertexSelection_erase(m_vertexSelection, getFace().getWinding()[index].adjacent);
3152 SceneChangeNotify();
3153 update_selection_vertex();
3156 bool selected_vertex(std::size_t index) const
3158 return VertexSelection_find(m_vertexSelection, getFace().getWinding()[index].adjacent) != m_vertexSelection.end();
3161 void update_move_planepts_edge(std::size_t index)
3163 std::size_t numpoints = m_face->getWinding().numpoints;
3164 ASSERT_MESSAGE(index < numpoints, "select_edge: invalid index");
3166 std::size_t adjacent = Winding_next(m_face->getWinding(), index);
3167 std::size_t opposite = Winding_Opposite(m_face->getWinding(), index);
3168 m_face->m_move_planepts[0] = m_face->getWinding()[index].vertex;
3169 m_face->m_move_planepts[1] = m_face->getWinding()[adjacent].vertex;
3170 m_face->m_move_planepts[2] = m_face->getWinding()[opposite].vertex;
3171 planepts_quantise(m_face->m_move_planepts, GRID_MIN); // winding points are very inaccurate
3173 void update_selection_edge()
3175 if(m_edgeSelection.size() == 0)
3177 m_selectableEdges.setSelected(false);
3181 m_selectableEdges.setSelected(true);
3183 if(m_edgeSelection.size() == 1)
3185 std::size_t index = Winding_FindAdjacent(getFace().getWinding(), *m_edgeSelection.begin());
3187 if(index != c_brush_maxFaces)
3189 update_move_planepts_edge(index);
3194 void select_edge(std::size_t index, bool select)
3198 VertexSelection_insert(m_edgeSelection, getFace().getWinding()[index].adjacent);
3202 VertexSelection_erase(m_edgeSelection, getFace().getWinding()[index].adjacent);
3205 SceneChangeNotify();
3206 update_selection_edge();
3209 bool selected_edge(std::size_t index) const
3211 return VertexSelection_find(m_edgeSelection, getFace().getWinding()[index].adjacent) != m_edgeSelection.end();
3214 const Vector3& centroid() const
3216 return m_face->centroid();
3219 void connectivityChanged()
3221 // This occurs when a face is added or removed.
3222 // The current vertex and edge selections no longer valid and must be cleared.
3223 m_vertexSelection.clear();
3224 m_selectableVertices.setSelected(false);
3225 m_edgeSelection.clear();
3226 m_selectableEdges.setSelected(false);
3230 class BrushClipPlane : public OpenGLRenderable
3234 static Shader* m_state;
3236 static void constructStatic()
3238 m_state = GlobalShaderCache().capture("$CLIPPER_OVERLAY");
3240 static void destroyStatic()
3242 GlobalShaderCache().release("$CLIPPER_OVERLAY");
3245 void setPlane(const Brush& brush, const Plane3& plane)
3248 if(plane3_valid(m_plane))
3250 brush.windingForClipPlane(m_winding, m_plane);
3254 m_winding.resize(0);
3258 void render(RenderStateFlags state) const
3260 if((state & RENDER_FILL) != 0)
3262 Winding_Draw(m_winding, m_plane.normal(), state);
3266 Winding_DrawWireframe(m_winding);
3270 void render(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld) const
3272 renderer.SetState(m_state, Renderer::eWireframeOnly);
3273 renderer.SetState(m_state, Renderer::eFullMaterials);
3274 renderer.addRenderable(*this, localToWorld);
3278 inline void Face_addLight(const FaceInstance& face, const Matrix4& localToWorld, const RendererLight& light)
3280 const Plane3& facePlane = face.getFace().plane3();
3281 const Vector3& origin = light.aabb().origin;
3282 Plane3 tmp(plane3_transformed(Plane3(facePlane.normal(), -facePlane.dist()), localToWorld));
3283 if(!plane3_test_point(tmp, origin)
3284 || !plane3_test_point(tmp, vector3_added(origin, light.offset())))
3286 face.m_lights.addLight(light);
3292 typedef std::vector<FaceInstance> FaceInstances;
3294 class EdgeInstance : public Selectable
3296 FaceInstances& m_faceInstances;
3297 SelectableEdge* m_edge;
3299 void select_edge(bool select)
3301 FaceVertexId faceVertex = m_edge->m_faceVertex;
3302 m_faceInstances[faceVertex.getFace()].select_edge(faceVertex.getVertex(), select);
3303 faceVertex = next_edge(m_edge->m_faces, faceVertex);
3304 m_faceInstances[faceVertex.getFace()].select_edge(faceVertex.getVertex(), select);
3306 bool selected_edge() const
3308 FaceVertexId faceVertex = m_edge->m_faceVertex;
3309 if(!m_faceInstances[faceVertex.getFace()].selected_edge(faceVertex.getVertex()))
3313 faceVertex = next_edge(m_edge->m_faces, faceVertex);
3314 if(!m_faceInstances[faceVertex.getFace()].selected_edge(faceVertex.getVertex()))
3323 EdgeInstance(FaceInstances& faceInstances, SelectableEdge& edge)
3324 : m_faceInstances(faceInstances), m_edge(&edge)
3327 EdgeInstance& operator=(const EdgeInstance& other)
3329 m_edge = other.m_edge;
3333 void setSelected(bool select)
3335 select_edge(select);
3337 bool isSelected() const
3339 return selected_edge();
3343 void testSelect(Selector& selector, SelectionTest& test)
3345 SelectionIntersection best;
3346 m_edge->testSelect(test, best);
3349 Selector_add(selector, *this, best);
3354 class VertexInstance : public Selectable
3356 FaceInstances& m_faceInstances;
3357 SelectableVertex* m_vertex;
3359 void select_vertex(bool select)
3361 FaceVertexId faceVertex = m_vertex->m_faceVertex;
3364 m_faceInstances[faceVertex.getFace()].select_vertex(faceVertex.getVertex(), select);
3365 faceVertex = next_vertex(m_vertex->m_faces, faceVertex);
3367 while(faceVertex.getFace() != m_vertex->m_faceVertex.getFace());
3369 bool selected_vertex() const
3371 FaceVertexId faceVertex = m_vertex->m_faceVertex;
3374 if(!m_faceInstances[faceVertex.getFace()].selected_vertex(faceVertex.getVertex()))
3378 faceVertex = next_vertex(m_vertex->m_faces, faceVertex);
3380 while(faceVertex.getFace() != m_vertex->m_faceVertex.getFace());
3385 VertexInstance(FaceInstances& faceInstances, SelectableVertex& vertex)
3386 : m_faceInstances(faceInstances), m_vertex(&vertex)
3389 VertexInstance& operator=(const VertexInstance& other)
3391 m_vertex = other.m_vertex;
3395 void setSelected(bool select)
3397 select_vertex(select);
3399 bool isSelected() const
3401 return selected_vertex();
3404 void testSelect(Selector& selector, SelectionTest& test)
3406 SelectionIntersection best;
3407 m_vertex->testSelect(test, best);
3410 Selector_add(selector, *this, best);
3415 class BrushInstanceVisitor
3418 virtual void visit(FaceInstance& face) const = 0;
3421 class BrushInstance :
3422 public BrushObserver,
3423 public scene::Instance,
3426 public SelectionTestable,
3427 public ComponentSelectionTestable,
3428 public ComponentEditable,
3429 public ComponentSnappable,
3430 public PlaneSelectable,
3431 public LightCullable
3435 InstanceTypeCastTable m_casts;
3439 InstanceStaticCast<BrushInstance, Selectable>::install(m_casts);
3440 InstanceContainedCast<BrushInstance, Bounded>::install(m_casts);
3441 InstanceContainedCast<BrushInstance, Cullable>::install(m_casts);
3442 InstanceStaticCast<BrushInstance, Renderable>::install(m_casts);
3443 InstanceStaticCast<BrushInstance, SelectionTestable>::install(m_casts);
3444 InstanceStaticCast<BrushInstance, ComponentSelectionTestable>::install(m_casts);
3445 InstanceStaticCast<BrushInstance, ComponentEditable>::install(m_casts);
3446 InstanceStaticCast<BrushInstance, ComponentSnappable>::install(m_casts);
3447 InstanceStaticCast<BrushInstance, PlaneSelectable>::install(m_casts);
3448 InstanceIdentityCast<BrushInstance>::install(m_casts);
3449 InstanceContainedCast<BrushInstance, Transformable>::install(m_casts);
3451 InstanceTypeCastTable& get()
3460 FaceInstances m_faceInstances;
3462 typedef std::vector<EdgeInstance> EdgeInstances;
3463 EdgeInstances m_edgeInstances;
3464 typedef std::vector<VertexInstance> VertexInstances;
3465 VertexInstances m_vertexInstances;
3467 ObservedSelectable m_selectable;
3469 mutable RenderableWireframe m_render_wireframe;
3470 mutable RenderablePointVector m_render_selected;
3471 mutable AABB m_aabb_component;
3472 mutable Array<PointVertex> m_faceCentroidPointsCulled;
3473 RenderablePointArray m_render_faces_wireframe;
3474 mutable bool m_viewChanged; // requires re-evaluation of view-dependent cached data
3476 BrushClipPlane m_clipPlane;
3478 static Shader* m_state_selpoint;
3480 const LightList* m_lightList;
3482 TransformModifier m_transform;
3484 BrushInstance(const BrushInstance& other); // NOT COPYABLE
3485 BrushInstance& operator=(const BrushInstance& other); // NOT ASSIGNABLE
3487 static Counter* m_counter;
3489 typedef LazyStatic<TypeCasts> StaticTypeCasts;
3491 void lightsChanged()
3493 m_lightList->lightsChanged();
3495 typedef MemberCaller<BrushInstance, &BrushInstance::lightsChanged> LightsChangedCaller;
3497 STRING_CONSTANT(Name, "BrushInstance");
3499 BrushInstance(const scene::Path& path, scene::Instance* parent, Brush& brush) :
3500 Instance(path, parent, this, StaticTypeCasts::instance().get()),
3502 m_selectable(SelectedChangedCaller(*this)),
3503 m_render_selected(GL_POINTS),
3504 m_render_faces_wireframe(m_faceCentroidPointsCulled, GL_POINTS),
3505 m_viewChanged(false),
3506 m_transform(Brush::TransformChangedCaller(m_brush), ApplyTransformCaller(*this))
3508 m_brush.instanceAttach(Instance::path());
3509 m_brush.attach(*this);
3510 m_counter->increment();
3512 m_lightList = &GlobalShaderCache().attach(*this);
3513 m_brush.m_lightsChanged = LightsChangedCaller(*this); ///\todo Make this work with instancing.
3515 Instance::setTransformChangedCallback(LightsChangedCaller(*this));
3519 Instance::setTransformChangedCallback(Callback());
3521 m_brush.m_lightsChanged = Callback();
3522 GlobalShaderCache().detach(*this);
3524 m_counter->decrement();
3525 m_brush.detach(*this);
3526 m_brush.instanceDetach(Instance::path());
3533 const Brush& getBrush() const
3538 Bounded& get(NullType<Bounded>)
3542 Cullable& get(NullType<Cullable>)
3546 Transformable& get(NullType<Transformable>)
3551 void selectedChanged(const Selectable& selectable)
3553 GlobalSelectionSystem().getObserver(SelectionSystem::ePrimitive)(selectable);
3554 GlobalSelectionSystem().onSelectedChanged(*this, selectable);
3556 Instance::selectedChanged();
3558 typedef MemberCaller1<BrushInstance, const Selectable&, &BrushInstance::selectedChanged> SelectedChangedCaller;
3560 void selectedChangedComponent(const Selectable& selectable)
3562 GlobalSelectionSystem().getObserver(SelectionSystem::eComponent)(selectable);
3563 GlobalSelectionSystem().onComponentSelection(*this, selectable);
3565 typedef MemberCaller1<BrushInstance, const Selectable&, &BrushInstance::selectedChangedComponent> SelectedChangedComponentCaller;
3567 const BrushInstanceVisitor& forEachFaceInstance(const BrushInstanceVisitor& visitor)
3569 for(FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3576 static void constructStatic()
3578 m_state_selpoint = GlobalShaderCache().capture("$SELPOINT");
3580 static void destroyStatic()
3582 GlobalShaderCache().release("$SELPOINT");
3587 m_faceInstances.clear();
3589 void reserve(std::size_t size)
3591 m_faceInstances.reserve(size);
3594 void push_back(Face& face)
3596 m_faceInstances.push_back(FaceInstance(face, SelectedChangedComponentCaller(*this)));
3600 ASSERT_MESSAGE(!m_faceInstances.empty(), "erasing invalid element");
3601 m_faceInstances.pop_back();
3603 void erase(std::size_t index)
3605 ASSERT_MESSAGE(index < m_faceInstances.size(), "erasing invalid element");
3606 m_faceInstances.erase(m_faceInstances.begin() + index);
3608 void connectivityChanged()
3610 for(FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3612 (*i).connectivityChanged();
3618 m_edgeInstances.clear();
3620 void edge_push_back(SelectableEdge& edge)
3622 m_edgeInstances.push_back(EdgeInstance(m_faceInstances, edge));
3627 m_vertexInstances.clear();
3629 void vertex_push_back(SelectableVertex& vertex)
3631 m_vertexInstances.push_back(VertexInstance(m_faceInstances, vertex));
3634 void DEBUG_verify() const
3636 ASSERT_MESSAGE(m_faceInstances.size() == m_brush.DEBUG_size(), "FATAL: mismatch");
3639 bool isSelected() const
3641 return m_selectable.isSelected();
3643 void setSelected(bool select)
3645 m_selectable.setSelected(select);
3648 void update_selected() const
3650 m_render_selected.clear();
3651 for(FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3653 if((*i).getFace().contributes())
3655 (*i).iterate_selected(m_render_selected);
3660 void evaluateViewDependent(const VolumeTest& volume, const Matrix4& localToWorld) const
3664 m_viewChanged = false;
3666 bool faces_visible[c_brush_maxFaces];
3668 bool* j = faces_visible;
3669 for(FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i, ++j)
3671 *j = (*i).intersectVolume(volume, localToWorld);
3675 m_brush.update_wireframe(m_render_wireframe, faces_visible);
3676 m_brush.update_faces_wireframe(m_faceCentroidPointsCulled, faces_visible);
3680 void renderComponentsSelected(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld) const
3682 m_brush.evaluateBRep();
3685 if(!m_render_selected.empty())
3687 renderer.Highlight(Renderer::ePrimitive, false);
3688 renderer.SetState(m_state_selpoint, Renderer::eWireframeOnly);
3689 renderer.SetState(m_state_selpoint, Renderer::eFullMaterials);
3690 renderer.addRenderable(m_render_selected, localToWorld);
3694 void renderComponents(Renderer& renderer, const VolumeTest& volume) const
3696 m_brush.evaluateBRep();
3698 const Matrix4& localToWorld = Instance::localToWorld();
3700 renderer.SetState(m_brush.m_state_point, Renderer::eWireframeOnly);
3701 renderer.SetState(m_brush.m_state_point, Renderer::eFullMaterials);
3703 if(volume.fill() && GlobalSelectionSystem().ComponentMode() == SelectionSystem::eFace)
3705 evaluateViewDependent(volume, localToWorld);
3706 renderer.addRenderable(m_render_faces_wireframe, localToWorld);
3710 m_brush.renderComponents(GlobalSelectionSystem().ComponentMode(), renderer, volume, localToWorld);
3714 void renderClipPlane(Renderer& renderer, const VolumeTest& volume) const
3716 if(GlobalSelectionSystem().ManipulatorMode() == SelectionSystem::eClip && isSelected())
3718 m_clipPlane.render(renderer, volume, localToWorld());
3722 void renderCommon(Renderer& renderer, const VolumeTest& volume) const
3724 bool componentMode = GlobalSelectionSystem().Mode() == SelectionSystem::eComponent;
3726 if(componentMode && isSelected())
3728 renderComponents(renderer, volume);
3731 if(parentSelected())
3735 renderer.Highlight(Renderer::eFace);
3737 renderer.Highlight(Renderer::ePrimitive);
3741 void renderSolid(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld) const
3743 //renderCommon(renderer, volume);
3745 m_lightList->evaluateLights();
3747 for(FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3749 renderer.setLights((*i).m_lights);
3750 (*i).render(renderer, volume, localToWorld);
3753 renderComponentsSelected(renderer, volume, localToWorld);
3756 void renderWireframe(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld) const
3758 //renderCommon(renderer, volume);
3760 evaluateViewDependent(volume, localToWorld);
3762 if(m_render_wireframe.m_size != 0)
3764 renderer.addRenderable(m_render_wireframe, localToWorld);
3767 renderComponentsSelected(renderer, volume, localToWorld);
3770 void renderSolid(Renderer& renderer, const VolumeTest& volume) const
3772 m_brush.evaluateBRep();
3774 renderClipPlane(renderer, volume);
3776 renderSolid(renderer, volume, localToWorld());
3779 void renderWireframe(Renderer& renderer, const VolumeTest& volume) const
3781 m_brush.evaluateBRep();
3783 renderClipPlane(renderer, volume);
3785 renderWireframe(renderer, volume, localToWorld());
3788 void viewChanged() const
3790 m_viewChanged = true;
3793 void testSelect(Selector& selector, SelectionTest& test)
3795 test.BeginMesh(localToWorld());
3797 SelectionIntersection best;
3798 for(FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3800 (*i).testSelect(test, best);
3804 selector.addIntersection(best);
3808 bool isSelectedComponents() const
3810 for(FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3812 if((*i).selectedComponents())
3819 void setSelectedComponents(bool select, SelectionSystem::EComponentMode mode)
3821 for(FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3823 (*i).setSelected(mode, select);
3826 void testSelectComponents(Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode)
3828 test.BeginMesh(localToWorld());
3832 case SelectionSystem::eVertex:
3834 for(VertexInstances::iterator i = m_vertexInstances.begin(); i != m_vertexInstances.end(); ++i)
3836 (*i).testSelect(selector, test);
3840 case SelectionSystem::eEdge:
3842 for(EdgeInstances::iterator i = m_edgeInstances.begin(); i != m_edgeInstances.end(); ++i)
3844 (*i).testSelect(selector, test);
3848 case SelectionSystem::eFace:
3850 if(test.getVolume().fill())
3852 for(FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3854 (*i).testSelect(selector, test);
3859 for(FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3861 (*i).testSelect_centroid(selector, test);
3871 void selectPlanes(Selector& selector, SelectionTest& test, const PlaneCallback& selectedPlaneCallback)
3873 test.BeginMesh(localToWorld());
3875 PlanePointer brushPlanes[c_brush_maxFaces];
3876 PlanesIterator j = brushPlanes;
3878 for(Brush::const_iterator i = m_brush.begin(); i != m_brush.end(); ++i)
3880 *j++ = &(*i)->plane3();
3883 for(FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3885 (*i).selectPlane(selector, Line(test.getNear(), test.getFar()), brushPlanes, j, selectedPlaneCallback);
3888 void selectReversedPlanes(Selector& selector, const SelectedPlanes& selectedPlanes)
3890 for(FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3892 (*i).selectReversedPlane(selector, selectedPlanes);
3897 void transformComponents(const Matrix4& matrix)
3899 for(FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3901 (*i).transformComponents(matrix);
3904 const AABB& getSelectedComponentsBounds() const
3906 m_aabb_component = AABB();
3908 for(FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3910 (*i).iterate_selected(m_aabb_component);
3913 return m_aabb_component;
3916 void snapComponents(float snap)
3918 for(FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3920 (*i).snapComponents(snap);
3923 void evaluateTransform()
3925 Matrix4 matrix(m_transform.calculateTransform());
3926 //globalOutputStream() << "matrix: " << matrix << "\n";
3928 if(m_transform.getType() == TRANSFORM_PRIMITIVE)
3930 m_brush.transform(matrix);
3934 transformComponents(matrix);
3937 void applyTransform()
3939 m_brush.revertTransform();
3940 evaluateTransform();
3941 m_brush.freezeTransform();
3943 typedef MemberCaller<BrushInstance, &BrushInstance::applyTransform> ApplyTransformCaller;
3945 void setClipPlane(const Plane3& plane)
3947 m_clipPlane.setPlane(m_brush, plane);
3950 bool testLight(const RendererLight& light) const
3952 return light.testAABB(worldAABB());
3954 void insertLight(const RendererLight& light)
3956 const Matrix4& localToWorld = Instance::localToWorld();
3957 for(FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3959 Face_addLight(*i, localToWorld, light);
3964 for(FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3966 (*i).m_lights.clear();
3971 inline BrushInstance* Instance_getBrush(scene::Instance& instance)
3973 return InstanceTypeCast<BrushInstance>::cast(instance);
3977 template<typename Functor>
3978 class BrushSelectedVisitor : public SelectionSystem::Visitor
3980 const Functor& m_functor;
3982 BrushSelectedVisitor(const Functor& functor) : m_functor(functor)
3985 void visit(scene::Instance& instance) const
3987 BrushInstance* brush = Instance_getBrush(instance);
3995 template<typename Functor>
3996 inline const Functor& Scene_forEachSelectedBrush(const Functor& functor)
3998 GlobalSelectionSystem().foreachSelected(BrushSelectedVisitor<Functor>(functor));
4002 template<typename Functor>
4003 class BrushVisibleSelectedVisitor : public SelectionSystem::Visitor
4005 const Functor& m_functor;
4007 BrushVisibleSelectedVisitor(const Functor& functor) : m_functor(functor)
4010 void visit(scene::Instance& instance) const
4012 BrushInstance* brush = Instance_getBrush(instance);
4014 && instance.path().top().get().visible())
4021 template<typename Functor>
4022 inline const Functor& Scene_forEachVisibleSelectedBrush(const Functor& functor)
4024 GlobalSelectionSystem().foreachSelected(BrushVisibleSelectedVisitor<Functor>(functor));
4028 class BrushForEachFace
4030 const BrushInstanceVisitor& m_visitor;
4032 BrushForEachFace(const BrushInstanceVisitor& visitor) : m_visitor(visitor)
4035 void operator()(BrushInstance& brush) const
4037 brush.forEachFaceInstance(m_visitor);
4041 template<class Functor>
4042 class FaceInstanceVisitFace : public BrushInstanceVisitor
4044 const Functor& functor;
4046 FaceInstanceVisitFace(const Functor& functor)
4050 void visit(FaceInstance& face) const
4052 functor(face.getFace());
4056 template<typename Functor>
4057 inline const Functor& Brush_forEachFace(BrushInstance& brush, const Functor& functor)
4059 brush.forEachFaceInstance(FaceInstanceVisitFace<Functor>(functor));
4063 template<class Functor>
4064 class FaceVisitAll : public BrushVisitor
4066 const Functor& functor;
4068 FaceVisitAll(const Functor& functor)
4072 void visit(Face& face) const
4078 template<typename Functor>
4079 inline const Functor& Brush_forEachFace(const Brush& brush, const Functor& functor)
4081 brush.forEachFace(FaceVisitAll<Functor>(functor));
4085 template<typename Functor>
4086 inline const Functor& Brush_forEachFace(Brush& brush, const Functor& functor)
4088 brush.forEachFace(FaceVisitAll<Functor>(functor));
4092 template<class Functor>
4093 class FaceInstanceVisitAll : public BrushInstanceVisitor
4095 const Functor& functor;
4097 FaceInstanceVisitAll(const Functor& functor)
4101 void visit(FaceInstance& face) const
4107 template<typename Functor>
4108 inline const Functor& Brush_ForEachFaceInstance(BrushInstance& brush, const Functor& functor)
4110 brush.forEachFaceInstance(FaceInstanceVisitAll<Functor>(functor));
4114 template<typename Functor>
4115 inline const Functor& Scene_forEachBrush(scene::Graph& graph, const Functor& functor)
4117 graph.traverse(InstanceWalker< InstanceApply<BrushInstance, Functor> >(functor));
4121 template<typename Type, typename Functor>
4122 class InstanceIfVisible : public Functor
4125 InstanceIfVisible(const Functor& functor) : Functor(functor)
4128 void operator()(scene::Instance& instance)
4130 if(instance.path().top().get().visible())
4132 Functor::operator()(instance);
4137 template<typename Functor>
4138 class BrushVisibleWalker : public scene::Graph::Walker
4140 const Functor& m_functor;
4142 BrushVisibleWalker(const Functor& functor) : m_functor(functor)
4145 bool pre(const scene::Path& path, scene::Instance& instance) const
4147 if(path.top().get().visible())
4149 BrushInstance* brush = Instance_getBrush(instance);
4159 template<typename Functor>
4160 inline const Functor& Scene_forEachVisibleBrush(scene::Graph& graph, const Functor& functor)
4162 graph.traverse(BrushVisibleWalker<Functor>(functor));
4166 template<typename Functor>
4167 inline const Functor& Scene_ForEachBrush_ForEachFace(scene::Graph& graph, const Functor& functor)
4169 Scene_forEachBrush(graph, BrushForEachFace(FaceInstanceVisitFace<Functor>(functor)));
4173 template<typename Functor>
4174 inline const Functor& Scene_ForEachSelectedBrush_ForEachFace(scene::Graph& graph, const Functor& functor)
4176 Scene_forEachSelectedBrush(BrushForEachFace(FaceInstanceVisitFace<Functor>(functor)));
4180 template<typename Functor>
4181 inline const Functor& Scene_ForEachSelectedBrush_ForEachFaceInstance(scene::Graph& graph, const Functor& functor)
4183 Scene_forEachSelectedBrush(BrushForEachFace(FaceInstanceVisitAll<Functor>(functor)));
4187 template<typename Functor>
4188 class FaceVisitorWrapper
4190 const Functor& functor;
4192 FaceVisitorWrapper(const Functor& functor) : functor(functor)
4196 void operator()(FaceInstance& faceInstance) const
4198 functor(faceInstance.getFace());
4202 template<typename Functor>
4203 inline const Functor& Scene_ForEachSelectedBrushFace(scene::Graph& graph, const Functor& functor)
4205 g_SelectedFaceInstances.foreach(FaceVisitorWrapper<Functor>(functor));