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]);
205 #include "shaderlib.h"
207 typedef DoubleVector3 PlanePoints[3];
209 inline bool planepts_equal(const PlanePoints planepts, const PlanePoints other)
211 return planepts[0] == other[0] && planepts[1] == other[1] && planepts[2] == other[2];
214 inline void planepts_assign(PlanePoints planepts, const PlanePoints other)
216 planepts[0] = other[0];
217 planepts[1] = other[1];
218 planepts[2] = other[2];
221 inline void planepts_quantise(PlanePoints planepts, double snap)
223 vector3_snap(planepts[0], snap);
224 vector3_snap(planepts[1], snap);
225 vector3_snap(planepts[2], snap);
228 inline float vector3_max_component(const Vector3& vec3)
230 return std::max(fabsf(vec3[0]), std::max(fabsf(vec3[1]), fabsf(vec3[2])));
233 inline void edge_snap(Vector3& edge, double snap)
235 float scale = static_cast<float>(ceil(fabs(snap / vector3_max_component(edge))));
238 vector3_scale(edge, scale);
240 vector3_snap(edge, snap);
243 inline void planepts_snap(PlanePoints planepts, double snap)
245 Vector3 edge01(vector3_subtracted(planepts[1], planepts[0]));
246 Vector3 edge12(vector3_subtracted(planepts[2], planepts[1]));
247 Vector3 edge20(vector3_subtracted(planepts[0], planepts[2]));
249 double length_squared_01 = vector3_dot(edge01, edge01);
250 double length_squared_12 = vector3_dot(edge12, edge12);
251 double length_squared_20 = vector3_dot(edge20, edge20);
253 vector3_snap(planepts[0], snap);
255 if(length_squared_01 < length_squared_12)
257 if(length_squared_12 < length_squared_20)
259 edge_snap(edge01, snap);
260 edge_snap(edge12, snap);
261 planepts[1] = vector3_added(planepts[0], edge01);
262 planepts[2] = vector3_added(planepts[1], edge12);
266 edge_snap(edge20, snap);
267 edge_snap(edge01, snap);
268 planepts[1] = vector3_added(planepts[0], edge20);
269 planepts[2] = vector3_added(planepts[1], edge01);
274 if(length_squared_01 < length_squared_20)
276 edge_snap(edge01, snap);
277 edge_snap(edge12, snap);
278 planepts[1] = vector3_added(planepts[0], edge01);
279 planepts[2] = vector3_added(planepts[1], edge12);
283 edge_snap(edge12, snap);
284 edge_snap(edge20, snap);
285 planepts[1] = vector3_added(planepts[0], edge12);
286 planepts[2] = vector3_added(planepts[1], edge20);
291 inline PointVertex pointvertex_for_planept(const DoubleVector3& point, const Colour4b& colour)
295 static_cast<float>(point.x()),
296 static_cast<float>(point.y()),
297 static_cast<float>(point.z())
303 inline PointVertex pointvertex_for_windingpoint(const Vector3& point, const Colour4b& colour)
306 vertex3f_for_vector3(point),
311 inline bool check_plane_is_integer(const PlanePoints& planePoints)
313 return !float_is_integer(planePoints[0][0])
314 || !float_is_integer(planePoints[0][1])
315 || !float_is_integer(planePoints[0][2])
316 || !float_is_integer(planePoints[1][0])
317 || !float_is_integer(planePoints[1][1])
318 || !float_is_integer(planePoints[1][2])
319 || !float_is_integer(planePoints[2][0])
320 || !float_is_integer(planePoints[2][1])
321 || !float_is_integer(planePoints[2][2]);
324 inline void brush_check_shader(const char* name)
326 if(!shader_valid(name))
328 globalErrorStream() << "brush face has invalid texture name: '" << name << "'\n";
332 class FaceShaderObserver
335 virtual void realiseShader() = 0;
336 virtual void unrealiseShader() = 0;
339 class FaceShaderObserverRealise
342 void operator()(FaceShaderObserver& observer) const
344 observer.realiseShader();
348 class FaceShaderObserverUnrealise
351 void operator()(FaceShaderObserver& observer) const
353 observer.unrealiseShader();
357 typedef ReferencePair<FaceShaderObserver> FaceShaderObserverPair;
360 class ContentsFlagsValue
366 ContentsFlagsValue(int surfaceFlags, int contentFlags, int value, bool specified) :
367 m_surfaceFlags(surfaceFlags),
368 m_contentFlags(contentFlags),
370 m_specified(specified)
379 inline void ContentsFlagsValue_assignMasked(ContentsFlagsValue& flags, const ContentsFlagsValue& other)
381 bool detail = bitfield_enabled(flags.m_contentFlags, BRUSH_DETAIL_MASK);
385 flags.m_contentFlags = bitfield_enable(flags.m_contentFlags, BRUSH_DETAIL_MASK);
389 flags.m_contentFlags = bitfield_disable(flags.m_contentFlags, BRUSH_DETAIL_MASK);
394 class FaceShader : public ModuleObserver
400 CopiedString m_shader;
401 ContentsFlagsValue m_flags;
403 SavedState(const FaceShader& faceShader)
405 m_shader = faceShader.getShader();
406 m_flags = faceShader.m_flags;
409 void exportState(FaceShader& faceShader) const
411 faceShader.setShader(m_shader.c_str());
412 faceShader.setFlags(m_flags);
416 CopiedString m_shader;
418 ContentsFlagsValue m_flags;
419 FaceShaderObserverPair m_observers;
423 FaceShader(const char* shader, const ContentsFlagsValue& flags = ContentsFlagsValue(0, 0, 0, false)) :
436 // copy-construction not supported
437 FaceShader(const FaceShader& other);
439 void instanceAttach()
442 m_state->incrementUsed();
444 void instanceDetach()
446 m_state->decrementUsed();
452 ASSERT_MESSAGE(m_state == 0, "shader cannot be captured");
453 brush_check_shader(m_shader.c_str());
454 m_state = GlobalShaderCache().capture(m_shader.c_str());
455 m_state->attach(*this);
459 ASSERT_MESSAGE(m_state != 0, "shader cannot be released");
460 m_state->detach(*this);
461 GlobalShaderCache().release(m_shader.c_str());
467 ASSERT_MESSAGE(!m_realised, "FaceTexdef::realise: already realised");
469 m_observers.forEach(FaceShaderObserverRealise());
473 ASSERT_MESSAGE(m_realised, "FaceTexdef::unrealise: already unrealised");
474 m_observers.forEach(FaceShaderObserverUnrealise());
478 void attach(FaceShaderObserver& observer)
480 m_observers.attach(observer);
483 observer.realiseShader();
487 void detach(FaceShaderObserver& observer)
491 observer.unrealiseShader();
493 m_observers.detach(observer);
496 const char* getShader() const
498 return m_shader.c_str();
500 void setShader(const char* name)
504 m_state->decrementUsed();
511 m_state->incrementUsed();
514 ContentsFlagsValue getFlags() const
516 ASSERT_MESSAGE(m_realised, "FaceShader::getFlags: flags not valid when unrealised");
517 if(!m_flags.m_specified)
519 return ContentsFlagsValue(
520 m_state->getTexture().surfaceFlags,
521 m_state->getTexture().contentFlags,
522 m_state->getTexture().value,
528 void setFlags(const ContentsFlagsValue& flags)
530 ASSERT_MESSAGE(m_realised, "FaceShader::setFlags: flags not valid when unrealised");
531 ContentsFlagsValue_assignMasked(m_flags, flags);
534 Shader* state() const
539 std::size_t width() const
543 return m_state->getTexture().width;
547 std::size_t height() const
551 return m_state->getTexture().height;
555 unsigned int shaderFlags() const
559 return m_state->getFlags();
568 class FaceTexdef : public FaceShaderObserver
571 FaceTexdef(const FaceTexdef& other);
573 FaceTexdef& operator=(const FaceTexdef& other);
578 TextureProjection m_projection;
580 SavedState(const FaceTexdef& faceTexdef)
582 m_projection = faceTexdef.m_projection;
585 void exportState(FaceTexdef& faceTexdef) const
587 Texdef_Assign(faceTexdef.m_projection, m_projection);
591 FaceShader& m_shader;
592 TextureProjection m_projection;
593 bool m_projectionInitialised;
598 const TextureProjection& projection,
599 bool projectionInitialised = true
602 m_projection(projection),
603 m_projectionInitialised(projectionInitialised),
604 m_scaleApplied(false)
606 m_shader.attach(*this);
610 m_shader.detach(*this);
615 ASSERT_MESSAGE(!m_scaleApplied, "texture scale aready added");
616 m_scaleApplied = true;
617 m_projection.m_brushprimit_texdef.addScale(m_shader.width(), m_shader.height());
621 ASSERT_MESSAGE(m_scaleApplied, "texture scale aready removed");
622 m_scaleApplied = false;
623 m_projection.m_brushprimit_texdef.removeScale(m_shader.width(), m_shader.height());
628 if(m_projectionInitialised && !m_scaleApplied)
633 void unrealiseShader()
635 if(m_projectionInitialised && m_scaleApplied)
641 void setTexdef(const TextureProjection& projection)
644 Texdef_Assign(m_projection, projection);
648 void shift(float s, float t)
650 ASSERT_MESSAGE(texdef_sane(m_projection.m_texdef), "FaceTexdef::shift: bad texdef");
652 Texdef_Shift(m_projection, s, t);
656 void scale(float s, float t)
659 Texdef_Scale(m_projection, s, t);
663 void rotate(float angle)
666 Texdef_Rotate(m_projection, angle);
670 void fit(const Vector3& normal, const Winding& winding, float s_repeat, float t_repeat)
672 Texdef_FitTexture(m_projection, m_shader.width(), m_shader.height(), normal, winding, s_repeat, t_repeat);
675 void emitTextureCoordinates(Winding& winding, const Vector3& normal, const Matrix4& localToWorld)
677 Texdef_EmitTextureCoordinates(m_projection, m_shader.width(), m_shader.height(), winding, normal, localToWorld);
680 void transform(const Plane3& plane, const Matrix4& matrix)
683 Texdef_transformLocked(m_projection, m_shader.width(), m_shader.height(), plane, matrix);
687 TextureProjection normalised() const
689 brushprimit_texdef_t tmp(m_projection.m_brushprimit_texdef);
690 tmp.removeScale(m_shader.width(), m_shader.height());
691 return TextureProjection(m_projection.m_texdef, tmp, m_projection.m_basis_s, m_projection.m_basis_t);
693 void setBasis(const Vector3& normal)
696 Normal_GetTransform(normal, basis);
697 m_projection.m_basis_s = Vector3(basis.xx(), basis.yx(), basis.zx());
698 m_projection.m_basis_t = Vector3(-basis.xy(), -basis.yy(), -basis.zy());
702 inline void planepts_print(const PlanePoints& planePoints, TextOutputStream& ostream)
704 ostream << "( " << planePoints[0][0] << " " << planePoints[0][1] << " " << planePoints[0][2] << " ) "
705 << "( " << planePoints[1][0] << " " << planePoints[1][1] << " " << planePoints[1][2] << " ) "
706 << "( " << planePoints[2][0] << " " << planePoints[2][1] << " " << planePoints[2][2] << " )";
710 inline Plane3 Plane3_applyTranslation(const Plane3& plane, const Vector3& translation)
712 Plane3 tmp(plane3_translated(Plane3(plane.normal(), -plane.dist()), translation));
713 return Plane3(tmp.normal(), -tmp.dist());
716 inline Plane3 Plane3_applyTransform(const Plane3& plane, const Matrix4& matrix)
718 Plane3 tmp(plane3_transformed(Plane3(plane.normal(), -plane.dist()), matrix));
719 return Plane3(tmp.normal(), -tmp.dist());
724 PlanePoints m_planepts;
725 Plane3 m_planeCached;
728 Vector3 m_funcStaticOrigin;
730 static EBrushType m_type;
732 static bool isDoom3Plane()
734 return FacePlane::m_type == eBrushTypeDoom3 || FacePlane::m_type == eBrushTypeQuake4;
740 PlanePoints m_planepts;
743 SavedState(const FacePlane& facePlane)
745 if(facePlane.isDoom3Plane())
747 m_plane = facePlane.m_plane;
751 planepts_assign(m_planepts, facePlane.planePoints());
755 void exportState(FacePlane& facePlane) const
757 if(facePlane.isDoom3Plane())
759 facePlane.m_plane = m_plane;
760 facePlane.updateTranslated();
764 planepts_assign(facePlane.planePoints(), m_planepts);
765 facePlane.MakePlane();
770 FacePlane() : m_funcStaticOrigin(0, 0, 0)
773 FacePlane(const FacePlane& other) : m_funcStaticOrigin(0, 0, 0)
777 planepts_assign(m_planepts, other.m_planepts);
782 m_plane = other.m_plane;
792 if(check_plane_is_integer(m_planepts))
794 globalErrorStream() << "non-integer planepts: ";
795 planepts_print(m_planepts, globalErrorStream());
796 globalErrorStream() << "\n";
799 m_planeCached = plane3_for_points(m_planepts);
807 vector3_swap(m_planepts[0], m_planepts[2]);
812 m_planeCached = plane3_flipped(m_plane);
816 void transform(const Matrix4& matrix, bool mirror)
822 bool off = check_plane_is_integer(planePoints());
825 matrix4_transform_point(matrix, m_planepts[0]);
826 matrix4_transform_point(matrix, m_planepts[1]);
827 matrix4_transform_point(matrix, m_planepts[2]);
835 if(check_plane_is_integer(planePoints()))
839 globalErrorStream() << "caused by transform\n";
847 m_planeCached = Plane3_applyTransform(m_planeCached, matrix);
851 void offset(float offset)
855 Vector3 move(vector3_scaled(m_planeCached.normal(), -offset));
857 vector3_subtract(m_planepts[0], move);
858 vector3_subtract(m_planepts[1], move);
859 vector3_subtract(m_planepts[2], move);
865 m_planeCached.d += offset;
870 void updateTranslated()
872 m_planeCached = Plane3_applyTranslation(m_plane, m_funcStaticOrigin);
876 m_plane = Plane3_applyTranslation(m_planeCached, vector3_negated(m_funcStaticOrigin));
880 PlanePoints& planePoints()
884 const PlanePoints& planePoints() const
888 const Plane3& plane3() const
890 return m_planeCached;
892 void setDoom3Plane(const Plane3& plane)
897 const Plane3& getDoom3Plane() const
902 void copy(const FacePlane& other)
906 planepts_assign(m_planepts, other.m_planepts);
911 m_planeCached = other.m_plane;
915 void copy(const Vector3& p0, const Vector3& p1, const Vector3& p2)
926 m_planeCached = plane3_for_points(p2, p1, p0);
932 inline void Winding_testSelect(Winding& winding, SelectionTest& test, SelectionIntersection& best)
934 test.TestPolygon(VertexPointer(reinterpret_cast<VertexPointer::pointer>(&winding.points.data()->vertex), sizeof(WindingVertex)), winding.numpoints, best);
937 const double GRID_MIN = 0.125;
939 inline double quantiseInteger(double f)
941 return float_to_integer(f);
944 inline double quantiseFloating(double f)
946 return float_snapped(f, 1.f / (1 << 16));
949 typedef double (*QuantiseFunc)(double f);
956 virtual bool filter(const Face& face) const = 0;
959 bool face_filtered(Face& face);
960 void add_face_filter(FaceFilter& filter, int mask, bool invert = false);
962 void Brush_addTextureChangedCallback(const SignalHandler& callback);
963 void Brush_textureChanged();
966 extern bool g_brush_texturelock_enabled;
971 virtual void planeChanged() = 0;
972 virtual void connectivityChanged() = 0;
973 virtual void shaderChanged() = 0;
974 virtual void evaluateTransform() = 0;
978 public OpenGLRenderable,
981 public FaceShaderObserver
983 std::size_t m_refcount;
985 class SavedState : public UndoMemento
988 FacePlane::SavedState m_planeState;
989 FaceTexdef::SavedState m_texdefState;
990 FaceShader::SavedState m_shaderState;
992 SavedState(const Face& face) : m_planeState(face.getPlane()), m_texdefState(face.getTexdef()), m_shaderState(face.getShader())
996 void exportState(Face& face) const
998 m_planeState.exportState(face.getPlane());
999 m_shaderState.exportState(face.getShader());
1000 m_texdefState.exportState(face.getTexdef());
1010 static QuantiseFunc m_quantise;
1011 static EBrushType m_type;
1013 PlanePoints m_move_planepts;
1014 PlanePoints m_move_planeptsTransformed;
1017 FacePlane m_planeTransformed;
1018 FaceShader m_shader;
1019 FaceTexdef m_texdef;
1020 TextureProjection m_texdefTransformed;
1026 FaceObserver* m_observer;
1027 UndoObserver* m_undoable_observer;
1030 // assignment not supported
1031 Face& operator=(const Face& other);
1032 // copy-construction not supported
1033 Face(const Face& other);
1037 Face(FaceObserver* observer) :
1039 m_shader(texdef_name_default()),
1040 m_texdef(m_shader, TextureProjection(), false),
1042 m_observer(observer),
1043 m_undoable_observer(0),
1046 m_shader.attach(*this);
1047 m_plane.copy(Vector3(0, 0, 0), Vector3(64, 0, 0), Vector3(0, 64, 0));
1048 m_texdef.setBasis(m_plane.plane3().normal());
1056 const TextureProjection& projection,
1057 FaceObserver* observer
1061 m_texdef(m_shader, projection),
1062 m_observer(observer),
1063 m_undoable_observer(0),
1066 m_shader.attach(*this);
1067 m_plane.copy(p0, p1, p2);
1068 m_texdef.setBasis(m_plane.plane3().normal());
1072 Face(const Face& other, FaceObserver* observer) :
1074 m_shader(other.m_shader.getShader(), other.m_shader.m_flags),
1075 m_texdef(m_shader, other.getTexdef().normalised()),
1076 m_observer(observer),
1077 m_undoable_observer(0),
1080 m_shader.attach(*this);
1081 m_plane.copy(other.m_plane);
1082 planepts_assign(m_move_planepts, other.m_move_planepts);
1083 m_texdef.setBasis(m_plane.plane3().normal());
1089 m_shader.detach(*this);
1095 m_observer->planeChanged();
1098 void realiseShader()
1100 m_observer->shaderChanged();
1102 void unrealiseShader()
1106 void instanceAttach(MapFile* map)
1108 m_shader.instanceAttach();
1110 m_undoable_observer = GlobalUndoSystem().observer(this);
1111 GlobalFilterSystem().registerFilterable(*this);
1113 void instanceDetach(MapFile* map)
1115 GlobalFilterSystem().unregisterFilterable(*this);
1116 m_undoable_observer = 0;
1117 GlobalUndoSystem().release(this);
1119 m_shader.instanceDetach();
1122 void render(RenderStateFlags state) const
1124 Winding_Draw(m_winding, m_planeTransformed.plane3().normal(), state);
1127 void updateFiltered()
1129 m_filtered = face_filtered(*this);
1131 bool isFiltered() const
1142 if(m_undoable_observer != 0)
1144 m_undoable_observer->save(this);
1149 UndoMemento* exportState() const
1151 return new SavedState(*this);
1153 void importState(const UndoMemento* data)
1157 static_cast<const SavedState*>(data)->exportState(*this);
1160 m_observer->connectivityChanged();
1162 m_observer->shaderChanged();
1172 if(--m_refcount == 0)
1182 bool intersectVolume(const VolumeTest& volume, const Matrix4& localToWorld) const
1184 return volume.TestPlane(Plane3(plane3().normal(), -plane3().dist()), localToWorld);
1187 void render(Renderer& renderer, const Matrix4& localToWorld) const
1189 renderer.SetState(m_shader.state(), Renderer::eFullMaterials);
1190 renderer.addRenderable(*this, localToWorld);
1193 void transform(const Matrix4& matrix, bool mirror)
1195 if(g_brush_texturelock_enabled)
1197 Texdef_transformLocked(m_texdefTransformed, m_shader.width(), m_shader.height(), m_plane.plane3(), matrix);
1200 m_planeTransformed.transform(matrix, mirror);
1203 ASSERT_MESSAGE(projectionaxis_for_normal(normal) == projectionaxis_for_normal(plane3().normal()), "bleh");
1205 m_observer->planeChanged();
1207 if(g_brush_texturelock_enabled)
1208 Brush_textureChanged();
1211 void assign_planepts(const PlanePoints planepts)
1213 m_planeTransformed.copy(planepts[0], planepts[1], planepts[2]);
1214 m_observer->planeChanged();
1217 /// \brief Reverts the transformable state of the brush to identity.
1218 void revertTransform()
1220 m_planeTransformed = m_plane;
1221 planepts_assign(m_move_planeptsTransformed, m_move_planepts);
1222 m_texdefTransformed = m_texdef.m_projection;
1224 void freezeTransform()
1227 m_plane = m_planeTransformed;
1228 planepts_assign(m_move_planepts, m_move_planeptsTransformed);
1229 m_texdef.m_projection = m_texdefTransformed;
1232 void update_move_planepts_vertex(std::size_t index, PlanePoints planePoints)
1234 std::size_t numpoints = getWinding().numpoints;
1235 ASSERT_MESSAGE(index < numpoints, "update_move_planepts_vertex: invalid index");
1237 std::size_t opposite = Winding_Opposite(getWinding(), index);
1238 std::size_t adjacent = Winding_wrap(getWinding(), opposite+numpoints-1);
1239 planePoints[0] = getWinding()[opposite].vertex;
1240 planePoints[1] = getWinding()[index].vertex;
1241 planePoints[2] = getWinding()[adjacent].vertex;
1242 // winding points are very inaccurate, so they must be quantised before using them to generate the face-plane
1243 planepts_quantise(planePoints, GRID_MIN);
1246 void snapto(float snap)
1251 ASSERT_MESSAGE(plane3_valid(m_plane.plane3()), "invalid plane before snap to grid");
1252 planepts_snap(m_plane.planePoints(), snap);
1253 ASSERT_MESSAGE(plane3_valid(m_plane.plane3()), "invalid plane after snap to grid");
1255 PlanePoints planePoints;
1256 update_move_planepts_vertex(0, planePoints);
1257 vector3_snap(planePoints[0], snap);
1258 vector3_snap(planePoints[1], snap);
1259 vector3_snap(planePoints[2], snap);
1260 assign_planepts(planePoints);
1263 SceneChangeNotify();
1264 if(!plane3_valid(m_plane.plane3()))
1266 globalErrorStream() << "WARNING: invalid plane after snap to grid\n";
1271 void testSelect(SelectionTest& test, SelectionIntersection& best)
1273 Winding_testSelect(m_winding, test, best);
1276 void testSelect_centroid(SelectionTest& test, SelectionIntersection& best)
1278 test.TestPoint(m_centroid, best);
1281 void shaderChanged()
1283 EmitTextureCoordinates();
1284 Brush_textureChanged();
1285 m_observer->shaderChanged();
1288 SceneChangeNotify();
1291 const char* GetShader() const
1293 return m_shader.getShader();
1295 void SetShader(const char* name)
1298 m_shader.setShader(name);
1304 m_texdefTransformed = m_texdef.m_projection;
1306 void texdefChanged()
1309 EmitTextureCoordinates();
1310 Brush_textureChanged();
1313 void GetTexdef(TextureProjection& projection) const
1315 projection = m_texdef.normalised();
1317 void SetTexdef(const TextureProjection& projection)
1320 m_texdef.setTexdef(projection);
1324 void GetFlags(ContentsFlagsValue& flags) const
1326 flags = m_shader.getFlags();
1328 void SetFlags(const ContentsFlagsValue& flags)
1331 m_shader.setFlags(flags);
1332 m_observer->shaderChanged();
1336 void ShiftTexdef(float s, float t)
1339 m_texdef.shift(s, t);
1343 void ScaleTexdef(float s, float t)
1346 m_texdef.scale(s, t);
1350 void RotateTexdef(float angle)
1353 m_texdef.rotate(angle);
1357 void FitTexture(float s_repeat, float t_repeat)
1360 m_texdef.fit(m_plane.plane3().normal(), m_winding, s_repeat, t_repeat);
1364 void EmitTextureCoordinates()
1366 Texdef_EmitTextureCoordinates(m_texdefTransformed, m_shader.width(), m_shader.height(), m_winding, plane3().normal(), g_matrix4_identity);
1370 const Vector3& centroid() const
1375 void construct_centroid()
1377 Winding_Centroid(m_winding, plane3(), m_centroid);
1380 const Winding& getWinding() const
1384 Winding& getWinding()
1389 const Plane3& plane3() const
1391 m_observer->evaluateTransform();
1392 return m_planeTransformed.plane3();
1394 FacePlane& getPlane()
1398 const FacePlane& getPlane() const
1402 FaceTexdef& getTexdef()
1406 const FaceTexdef& getTexdef() const
1410 FaceShader& getShader()
1414 const FaceShader& getShader() const
1419 bool isDetail() const
1421 return (m_shader.m_flags.m_contentFlags & BRUSH_DETAIL_MASK) != 0;
1423 void setDetail(bool detail)
1426 if(detail && !isDetail())
1428 m_shader.m_flags.m_contentFlags |= BRUSH_DETAIL_MASK;
1430 else if(!detail && isDetail())
1432 m_shader.m_flags.m_contentFlags &= ~BRUSH_DETAIL_MASK;
1434 m_observer->shaderChanged();
1437 bool contributes() const
1439 return m_winding.numpoints > 2;
1441 bool is_bounded() const
1443 for(Winding::const_iterator i = m_winding.begin(); i != m_winding.end(); ++i)
1445 if((*i).adjacent == c_brush_maxFaces)
1458 std::size_t m_vertex;
1461 FaceVertexId(std::size_t face, std::size_t vertex)
1462 : m_face(face), m_vertex(vertex)
1466 std::size_t getFace() const
1470 std::size_t getVertex() const
1476 typedef std::size_t faceIndex_t;
1478 struct EdgeRenderIndices
1484 : first(0), second(0)
1487 EdgeRenderIndices(const RenderIndex _first, const RenderIndex _second)
1488 : first(_first), second(_second)
1499 : first(c_brush_maxFaces), second(c_brush_maxFaces)
1502 EdgeFaces(const faceIndex_t _first, const faceIndex_t _second)
1503 : first(_first), second(_second)
1508 class RenderableWireframe : public OpenGLRenderable
1511 void render(RenderStateFlags state) const
1514 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(PointVertex), &m_vertices->colour);
1515 glVertexPointer(3, GL_FLOAT, sizeof(PointVertex), &m_vertices->vertex);
1516 glDrawElements(GL_LINES, GLsizei(m_size<<1), RenderIndexTypeID, m_faceVertex.data());
1519 for(std::size_t i = 0; i < m_size; ++i)
1521 glVertex3fv(&m_vertices[m_faceVertex[i].first].vertex.x);
1522 glVertex3fv(&m_vertices[m_faceVertex[i].second].vertex.x);
1528 Array<EdgeRenderIndices> m_faceVertex;
1530 const PointVertex* m_vertices;
1534 typedef std::vector<Brush*> brush_vector_t;
1539 virtual bool filter(const Brush& brush) const = 0;
1542 bool brush_filtered(Brush& brush);
1543 void add_brush_filter(BrushFilter& filter, int mask, bool invert = false);
1546 /// \brief Returns true if 'self' takes priority when building brush b-rep.
1547 inline bool plane3_inside(const Plane3& self, const Plane3& other)
1549 if(vector3_equal_epsilon(self.normal(), other.normal(), 0.001))
1551 return self.dist() < other.dist();
1556 typedef SmartPointer<Face> FaceSmartPointer;
1557 typedef std::vector<FaceSmartPointer> Faces;
1559 /// \brief Returns the unique-id of the edge adjacent to \p faceVertex in the edge-pair for the set of \p faces.
1560 inline FaceVertexId next_edge(const Faces& faces, FaceVertexId faceVertex)
1562 std::size_t adjacent_face = faces[faceVertex.getFace()]->getWinding()[faceVertex.getVertex()].adjacent;
1563 std::size_t adjacent_vertex = Winding_FindAdjacent(faces[adjacent_face]->getWinding(), faceVertex.getFace());
1565 ASSERT_MESSAGE(adjacent_vertex != c_brush_maxFaces, "connectivity data invalid");
1566 if(adjacent_vertex == c_brush_maxFaces)
1571 return FaceVertexId(adjacent_face, adjacent_vertex);
1574 /// \brief Returns the unique-id of the vertex adjacent to \p faceVertex in the vertex-ring for the set of \p faces.
1575 inline FaceVertexId next_vertex(const Faces& faces, FaceVertexId faceVertex)
1577 FaceVertexId nextEdge = next_edge(faces, faceVertex);
1578 return FaceVertexId(nextEdge.getFace(), Winding_next(faces[nextEdge.getFace()]->getWinding(), nextEdge.getVertex()));
1581 class SelectableEdge
1583 Vector3 getEdge() const
1585 const Winding& winding = getFace().getWinding();
1586 return vector3_mid(winding[m_faceVertex.getVertex()].vertex, winding[Winding_next(winding, m_faceVertex.getVertex())].vertex);
1591 FaceVertexId m_faceVertex;
1593 SelectableEdge(Faces& faces, FaceVertexId faceVertex)
1594 : m_faces(faces), m_faceVertex(faceVertex)
1597 SelectableEdge& operator=(const SelectableEdge& other)
1599 m_faceVertex = other.m_faceVertex;
1603 Face& getFace() const
1605 return *m_faces[m_faceVertex.getFace()];
1608 void testSelect(SelectionTest& test, SelectionIntersection& best)
1610 test.TestPoint(getEdge(), best);
1614 class SelectableVertex
1616 Vector3 getVertex() const
1618 return getFace().getWinding()[m_faceVertex.getVertex()].vertex;
1623 FaceVertexId m_faceVertex;
1625 SelectableVertex(Faces& faces, FaceVertexId faceVertex)
1626 : m_faces(faces), m_faceVertex(faceVertex)
1629 SelectableVertex& operator=(const SelectableVertex& other)
1631 m_faceVertex = other.m_faceVertex;
1635 Face& getFace() const
1637 return *m_faces[m_faceVertex.getFace()];
1640 void testSelect(SelectionTest& test, SelectionIntersection& best)
1642 test.TestPoint(getVertex(), best);
1649 virtual void reserve(std::size_t size) = 0;
1650 virtual void clear() = 0;
1651 virtual void push_back(Face& face) = 0;
1652 virtual void pop_back() = 0;
1653 virtual void erase(std::size_t index) = 0;
1654 virtual void connectivityChanged() = 0;
1656 virtual void edge_clear() = 0;
1657 virtual void edge_push_back(SelectableEdge& edge) = 0;
1659 virtual void vertex_clear() = 0;
1660 virtual void vertex_push_back(SelectableVertex& vertex) = 0;
1662 virtual void DEBUG_verify() const = 0;
1668 virtual void visit(Face& face) const = 0;
1672 public TransformNode,
1677 public FaceObserver,
1683 scene::Node* m_node;
1684 typedef UniqueSet<BrushObserver*> Observers;
1685 Observers m_observers;
1686 UndoObserver* m_undoable_observer;
1693 // cached data compiled from state
1694 Array<PointVertex> m_faceCentroidPoints;
1695 RenderablePointArray m_render_faces;
1697 Array<PointVertex> m_uniqueVertexPoints;
1698 typedef std::vector<SelectableVertex> SelectableVertices;
1699 SelectableVertices m_select_vertices;
1700 RenderablePointArray m_render_vertices;
1702 Array<PointVertex> m_uniqueEdgePoints;
1703 typedef std::vector<SelectableEdge> SelectableEdges;
1704 SelectableEdges m_select_edges;
1705 RenderablePointArray m_render_edges;
1707 Array<EdgeRenderIndices> m_edge_indices;
1708 Array<EdgeFaces> m_edge_faces;
1713 Callback m_evaluateTransform;
1714 Callback m_boundsChanged;
1716 mutable bool m_planeChanged; // b-rep evaluation required
1717 mutable bool m_transformChanged; // transform evaluation required
1721 STRING_CONSTANT(Name, "Brush");
1723 Callback m_lightsChanged;
1726 static Shader* m_state_point;
1729 static EBrushType m_type;
1730 static double m_maxWorldCoord;
1732 Brush(scene::Node& node, const Callback& evaluateTransform, const Callback& boundsChanged) :
1734 m_undoable_observer(0),
1736 m_render_faces(m_faceCentroidPoints, GL_POINTS),
1737 m_render_vertices(m_uniqueVertexPoints, GL_POINTS),
1738 m_render_edges(m_uniqueEdgePoints, GL_POINTS),
1739 m_evaluateTransform(evaluateTransform),
1740 m_boundsChanged(boundsChanged),
1741 m_planeChanged(false),
1742 m_transformChanged(false)
1746 Brush(const Brush& other, scene::Node& node, const Callback& evaluateTransform, const Callback& boundsChanged) :
1748 m_undoable_observer(0),
1750 m_render_faces(m_faceCentroidPoints, GL_POINTS),
1751 m_render_vertices(m_uniqueVertexPoints, GL_POINTS),
1752 m_render_edges(m_uniqueEdgePoints, GL_POINTS),
1753 m_evaluateTransform(evaluateTransform),
1754 m_boundsChanged(boundsChanged),
1755 m_planeChanged(false),
1756 m_transformChanged(false)
1760 Brush(const Brush& other) :
1761 TransformNode(other),
1766 FaceObserver(other),
1771 m_undoable_observer(0),
1773 m_render_faces(m_faceCentroidPoints, GL_POINTS),
1774 m_render_vertices(m_uniqueVertexPoints, GL_POINTS),
1775 m_render_edges(m_uniqueEdgePoints, GL_POINTS),
1776 m_planeChanged(false),
1777 m_transformChanged(false)
1783 ASSERT_MESSAGE(m_observers.empty(), "Brush::~Brush: observers still attached");
1786 // assignment not supported
1787 Brush& operator=(const Brush& other);
1789 void setDoom3GroupOrigin(const Vector3& origin)
1791 //globalOutputStream() << "func_static origin before: " << m_funcStaticOrigin << " after: " << origin << "\n";
1792 for(Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i)
1794 (*i)->getPlane().m_funcStaticOrigin = origin;
1795 (*i)->getPlane().updateTranslated();
1796 (*i)->planeChanged();
1801 void attach(BrushObserver& observer)
1803 for(Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i)
1805 observer.push_back(*(*i));
1808 for(SelectableEdges::iterator i = m_select_edges.begin(); i !=m_select_edges.end(); ++i)
1810 observer.edge_push_back(*i);
1813 for(SelectableVertices::iterator i = m_select_vertices.begin(); i != m_select_vertices.end(); ++i)
1815 observer.vertex_push_back(*i);
1818 m_observers.insert(&observer);
1820 void detach(BrushObserver& observer)
1822 m_observers.erase(&observer);
1825 void forEachFace(const BrushVisitor& visitor) const
1827 for(Faces::const_iterator i = m_faces.begin(); i != m_faces.end(); ++i)
1829 visitor.visit(*(*i));
1833 void forEachFace_instanceAttach(MapFile* map) const
1835 for(Faces::const_iterator i = m_faces.begin(); i != m_faces.end(); ++i)
1837 (*i)->instanceAttach(map);
1840 void forEachFace_instanceDetach(MapFile* map) const
1842 for(Faces::const_iterator i = m_faces.begin(); i != m_faces.end(); ++i)
1844 (*i)->instanceDetach(map);
1848 InstanceCounter m_instanceCounter;
1849 void instanceAttach(const scene::Path& path)
1851 if(++m_instanceCounter.m_count == 1)
1853 m_map = path_find_mapfile(path.begin(), path.end());
1854 m_undoable_observer = GlobalUndoSystem().observer(this);
1855 GlobalFilterSystem().registerFilterable(*this);
1856 forEachFace_instanceAttach(m_map);
1860 ASSERT_MESSAGE(path_find_mapfile(path.begin(), path.end()) == m_map, "node is instanced across more than one file");
1863 void instanceDetach(const scene::Path& path)
1865 if(--m_instanceCounter.m_count == 0)
1867 forEachFace_instanceDetach(m_map);
1868 GlobalFilterSystem().unregisterFilterable(*this);
1870 m_undoable_observer = 0;
1871 GlobalUndoSystem().release(this);
1876 const char* name() const
1880 void attach(const NameCallback& callback)
1883 void detach(const NameCallback& callback)
1888 void updateFiltered()
1892 if(brush_filtered(*this))
1894 m_node->enable(scene::Node::eFiltered);
1898 m_node->disable(scene::Node::eFiltered);
1906 m_planeChanged = true;
1910 void shaderChanged()
1916 void evaluateBRep() const
1920 m_planeChanged = false;
1921 const_cast<Brush*>(this)->buildBRep();
1925 void transformChanged()
1927 m_transformChanged = true;
1930 typedef MemberCaller<Brush, &Brush::transformChanged> TransformChangedCaller;
1932 void evaluateTransform()
1934 if(m_transformChanged)
1936 m_transformChanged = false;
1938 m_evaluateTransform();
1941 const Matrix4& localToParent() const
1943 return g_matrix4_identity;
1949 const AABB& localAABB() const
1952 return m_aabb_local;
1955 VolumeIntersectionValue intersectVolume(const VolumeTest& test, const Matrix4& localToWorld) const
1957 return test.TestAABB(m_aabb_local, localToWorld);
1960 void renderComponents(SelectionSystem::EComponentMode mode, Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld) const
1964 case SelectionSystem::eVertex:
1965 renderer.addRenderable(m_render_vertices, localToWorld);
1967 case SelectionSystem::eEdge:
1968 renderer.addRenderable(m_render_edges, localToWorld);
1970 case SelectionSystem::eFace:
1971 renderer.addRenderable(m_render_faces, localToWorld);
1978 void transform(const Matrix4& matrix)
1980 bool mirror = matrix4_handedness(matrix) == MATRIX4_LEFTHANDED;
1982 for(Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i)
1984 (*i)->transform(matrix, mirror);
1987 void snapto(float snap)
1989 for(Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i)
1994 void revertTransform()
1996 for(Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i)
1998 (*i)->revertTransform();
2001 void freezeTransform()
2003 for(Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i)
2005 (*i)->freezeTransform();
2009 /// \brief Returns the absolute index of the \p faceVertex.
2010 std::size_t absoluteIndex(FaceVertexId faceVertex)
2012 std::size_t index = 0;
2013 for(std::size_t i = 0; i < faceVertex.getFace(); ++i)
2015 index += m_faces[i]->getWinding().numpoints;
2017 return index + faceVertex.getVertex();
2020 void appendFaces(const Faces& other)
2023 for(Faces::const_iterator i = other.begin(); i != other.end(); ++i)
2029 /// \brief The undo memento for a brush stores only the list of face references - the faces are not copied.
2030 class BrushUndoMemento : public UndoMemento
2033 BrushUndoMemento(const Faces& faces) : m_faces(faces)
2050 if(m_undoable_observer != 0)
2052 m_undoable_observer->save(this);
2056 UndoMemento* exportState() const
2058 return new BrushUndoMemento(m_faces);
2061 void importState(const UndoMemento* state)
2064 appendFaces(static_cast<const BrushUndoMemento*>(state)->m_faces);
2067 for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i)
2069 (*i)->DEBUG_verify();
2075 return !m_faces.empty() && m_faces.front()->isDetail();
2078 /// \brief Appends a copy of \p face to the end of the face list.
2079 Face* addFace(const Face& face)
2081 if(m_faces.size() == c_brush_maxFaces)
2086 push_back(FaceSmartPointer(new Face(face, this)));
2087 m_faces.back()->setDetail(isDetail());
2089 return m_faces.back();
2092 /// \brief Appends a new face constructed from the parameters to the end of the face list.
2093 Face* addPlane(const Vector3& p0, const Vector3& p1, const Vector3& p2, const char* shader, const TextureProjection& projection)
2095 if(m_faces.size() == c_brush_maxFaces)
2100 push_back(FaceSmartPointer(new Face(p0, p1, p2, shader, projection, this)));
2101 m_faces.back()->setDetail(isDetail());
2103 return m_faces.back();
2106 static void constructStatic(EBrushType type)
2109 Face::m_type = type;
2110 FacePlane::m_type = type;
2112 g_bp_globals.m_texdefTypeId = TEXDEFTYPEID_QUAKE;
2113 if(m_type == eBrushTypeQuake3BP || m_type == eBrushTypeDoom3 || m_type == eBrushTypeQuake4)
2115 g_bp_globals.m_texdefTypeId = TEXDEFTYPEID_BRUSHPRIMITIVES;
2116 g_brush_texturelock_enabled = true;
2118 else if(m_type == eBrushTypeHalfLife)
2120 g_bp_globals.m_texdefTypeId = TEXDEFTYPEID_HALFLIFE;
2121 g_brush_texturelock_enabled = true;
2124 Face::m_quantise = (m_type == eBrushTypeQuake) ? quantiseInteger : quantiseFloating;
2126 m_state_point = GlobalShaderCache().capture("$POINT");
2128 static void destroyStatic()
2130 GlobalShaderCache().release("$POINT");
2133 std::size_t DEBUG_size()
2135 return m_faces.size();
2138 typedef Faces::const_iterator const_iterator;
2140 const_iterator begin() const
2142 return m_faces.begin();
2144 const_iterator end() const
2146 return m_faces.end();
2151 return m_faces.back();
2153 const Face* back() const
2155 return m_faces.back();
2157 void reserve(std::size_t count)
2159 m_faces.reserve(count);
2160 for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i)
2162 (*i)->reserve(count);
2165 void push_back(Faces::value_type face)
2167 m_faces.push_back(face);
2168 if(m_instanceCounter.m_count != 0)
2170 m_faces.back()->instanceAttach(m_map);
2172 for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i)
2174 (*i)->push_back(*face);
2175 (*i)->DEBUG_verify();
2180 if(m_instanceCounter.m_count != 0)
2182 m_faces.back()->instanceDetach(m_map);
2185 for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i)
2188 (*i)->DEBUG_verify();
2191 void erase(std::size_t index)
2193 if(m_instanceCounter.m_count != 0)
2195 m_faces[index]->instanceDetach(m_map);
2197 m_faces.erase(m_faces.begin() + index);
2198 for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i)
2201 (*i)->DEBUG_verify();
2204 void connectivityChanged()
2206 for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i)
2208 (*i)->connectivityChanged();
2216 if(m_instanceCounter.m_count != 0)
2218 forEachFace_instanceDetach(m_map);
2221 for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i)
2224 (*i)->DEBUG_verify();
2227 std::size_t size() const
2229 return m_faces.size();
2233 return m_faces.empty();
2236 /// \brief Returns true if any face of the brush contributes to the final B-Rep.
2237 bool hasContributingFaces() const
2239 for(const_iterator i = begin(); i != end(); ++i)
2241 if((*i)->contributes())
2249 /// \brief Removes faces that do not contribute to the brush. This is useful for cleaning up after CSG operations on the brush.
2250 /// 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.
2251 void removeEmptyFaces()
2257 while(i < m_faces.size())
2259 if(!m_faces[i]->contributes())
2272 /// \brief Constructs \p winding from the intersection of \p plane with the other planes of the brush.
2273 void windingForClipPlane(Winding& winding, const Plane3& plane) const
2275 FixedWinding buffer[2];
2278 // get a poly that covers an effectively infinite area
2279 Winding_createInfinite(buffer[swap], plane, m_maxWorldCoord + 1);
2281 // chop the poly by all of the other faces
2283 for (std::size_t i = 0; i < m_faces.size(); ++i)
2285 const Face& clip = *m_faces[i];
2287 if(plane3_equal(clip.plane3(), plane)
2288 || !plane3_valid(clip.plane3()) || !plane_unique(i)
2289 || plane3_opposing(plane, clip.plane3()))
2294 buffer[!swap].clear();
2296 #if BRUSH_CONNECTIVITY_DEBUG
2297 globalOutputStream() << "clip vs face: " << i << "\n";
2301 // flip the plane, because we want to keep the back side
2302 Plane3 clipPlane(vector3_negated(clip.plane3().normal()), -clip.plane3().dist());
2303 Winding_Clip(buffer[swap], plane, clipPlane, i, buffer[!swap]);
2306 #if BRUSH_CONNECTIVITY_DEBUG
2307 for(FixedWinding::Points::iterator k = buffer[!swap].points.begin(), j = buffer[!swap].points.end() - 1; k != buffer[!swap].points.end(); j = k, ++k)
2309 if(vector3_length_squared(vector3_subtracted((*k).vertex, (*j).vertex)) < 1)
2311 globalOutputStream() << "v: " << std::distance(buffer[!swap].points.begin(), j) << " tiny edge adjacent to face " << (*j).adjacent << "\n";
2316 //ASSERT_MESSAGE(buffer[!swap].numpoints != 1, "created single-point winding");
2322 Winding_forFixedWinding(winding, buffer[swap]);
2324 #if BRUSH_CONNECTIVITY_DEBUG
2325 Winding_printConnectivity(winding);
2327 for(Winding::iterator i = winding.begin(), j = winding.end() - 1; i != winding.end(); j = i, ++i)
2329 if(vector3_length_squared(vector3_subtracted((*i).vertex, (*j).vertex)) < 1)
2331 globalOutputStream() << "v: " << std::distance(winding.begin(), j) << " tiny edge adjacent to face " << (*j).adjacent << "\n";
2337 void update_wireframe(RenderableWireframe& wire, const bool* faces_visible) const
2339 wire.m_faceVertex.resize(m_edge_indices.size());
2340 wire.m_vertices = m_uniqueVertexPoints.data();
2342 for(std::size_t i = 0; i < m_edge_faces.size(); ++i)
2344 if(faces_visible[m_edge_faces[i].first]
2345 || faces_visible[m_edge_faces[i].second])
2347 wire.m_faceVertex[wire.m_size++] = m_edge_indices[i];
2353 void update_faces_wireframe(Array<PointVertex>& wire, const bool* faces_visible) const
2355 std::size_t count = 0;
2356 for(std::size_t i = 0; i < m_faceCentroidPoints.size(); ++i)
2358 if(faces_visible[i])
2365 Array<PointVertex>::iterator p = wire.begin();
2366 for(std::size_t i = 0; i < m_faceCentroidPoints.size(); ++i)
2368 if(faces_visible[i])
2370 *p++ = m_faceCentroidPoints[i];
2375 /// \brief Makes this brush a deep-copy of the \p other.
2376 void copy(const Brush& other)
2378 for(Faces::const_iterator i = other.m_faces.begin(); i != other.m_faces.end(); ++i)
2386 void edge_push_back(FaceVertexId faceVertex)
2388 m_select_edges.push_back(SelectableEdge(m_faces, faceVertex));
2389 for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i)
2391 (*i)->edge_push_back(m_select_edges.back());
2396 m_select_edges.clear();
2397 for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i)
2402 void vertex_push_back(FaceVertexId faceVertex)
2404 m_select_vertices.push_back(SelectableVertex(m_faces, faceVertex));
2405 for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i)
2407 (*i)->vertex_push_back(m_select_vertices.back());
2412 m_select_vertices.clear();
2413 for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i)
2415 (*i)->vertex_clear();
2419 /// \brief Returns true if the face identified by \p index is preceded by another plane that takes priority over it.
2420 bool plane_unique(std::size_t index) const
2423 for(std::size_t i = 0; i < m_faces.size(); ++i)
2425 if(index != i && !plane3_inside(m_faces[index]->plane3(), m_faces[i]->plane3()))
2433 /// \brief Removes edges that are smaller than the tolerance used when generating brush windings.
2434 void removeDegenerateEdges()
2436 for (std::size_t i = 0; i < m_faces.size(); ++i)
2438 Winding& winding = m_faces[i]->getWinding();
2439 for(Winding::iterator j = winding.begin(); j != winding.end();)
2441 std::size_t index = std::distance(winding.begin(), j);
2442 std::size_t next = Winding_next(winding, index);
2443 if(Edge_isDegenerate(winding[index].vertex, winding[next].vertex))
2445 #if BRUSH_DEGENERATE_DEBUG
2446 globalOutputStream() << "Brush::buildWindings: face " << i << ": degenerate edge adjacent to " << winding[index].adjacent << "\n";
2448 Winding& other = m_faces[winding[index].adjacent]->getWinding();
2449 std::size_t adjacent = Winding_FindAdjacent(other, i);
2450 if(adjacent != c_brush_maxFaces)
2452 other.erase(other.begin() + adjacent);
2464 /// \brief Invalidates faces that have only two vertices in their winding, while preserving edge-connectivity information.
2465 void removeDegenerateFaces()
2467 // save adjacency info for degenerate faces
2468 for (std::size_t i = 0; i < m_faces.size(); ++i)
2470 Winding& degen = m_faces[i]->getWinding();
2472 if(degen.numpoints == 2)
2474 #if BRUSH_DEGENERATE_DEBUG
2475 globalOutputStream() << "Brush::buildWindings: face " << i << ": degenerate winding adjacent to " << degen[0].adjacent << ", " << degen[1].adjacent << "\n";
2477 // this is an "edge" face, where the plane touches the edge of the brush
2479 Winding& winding = m_faces[degen[0].adjacent]->getWinding();
2480 std::size_t index = Winding_FindAdjacent(winding, i);
2481 if(index != c_brush_maxFaces)
2483 #if BRUSH_DEGENERATE_DEBUG
2484 globalOutputStream() << "Brush::buildWindings: face " << degen[0].adjacent << ": remapping adjacent " << winding[index].adjacent << " to " << degen[1].adjacent << "\n";
2486 winding[index].adjacent = degen[1].adjacent;
2491 Winding& winding = m_faces[degen[1].adjacent]->getWinding();
2492 std::size_t index = Winding_FindAdjacent(winding, i);
2493 if(index != c_brush_maxFaces)
2495 #if BRUSH_DEGENERATE_DEBUG
2496 globalOutputStream() << "Brush::buildWindings: face " << degen[1].adjacent << ": remapping adjacent " << winding[index].adjacent << " to " << degen[0].adjacent << "\n";
2498 winding[index].adjacent = degen[0].adjacent;
2507 /// \brief Removes edges that have the same adjacent-face as their immediate neighbour.
2508 void removeDuplicateEdges()
2510 // verify face connectivity graph
2511 for(std::size_t i = 0; i < m_faces.size(); ++i)
2513 //if(m_faces[i]->contributes())
2515 Winding& winding = m_faces[i]->getWinding();
2516 for(std::size_t j = 0; j != winding.numpoints;)
2518 std::size_t next = Winding_next(winding, j);
2519 if(winding[j].adjacent == winding[next].adjacent)
2521 #if BRUSH_DEGENERATE_DEBUG
2522 globalOutputStream() << "Brush::buildWindings: face " << i << ": removed duplicate edge adjacent to face " << winding[j].adjacent << "\n";
2524 winding.erase(winding.begin() + next);
2535 /// \brief Removes edges that do not have a matching pair in their adjacent-face.
2536 void verifyConnectivityGraph()
2538 // verify face connectivity graph
2539 for(std::size_t i = 0; i < m_faces.size(); ++i)
2541 //if(m_faces[i]->contributes())
2543 Winding& winding = m_faces[i]->getWinding();
2544 for(Winding::iterator j = winding.begin(); j != winding.end();)
2546 #if BRUSH_CONNECTIVITY_DEBUG
2547 globalOutputStream() << "Brush::buildWindings: face " << i << ": adjacent to face " << (*j).adjacent << "\n";
2549 // remove unidirectional graph edges
2550 if((*j).adjacent == c_brush_maxFaces
2551 || Winding_FindAdjacent(m_faces[(*j).adjacent]->getWinding(), i) == c_brush_maxFaces)
2553 #if BRUSH_CONNECTIVITY_DEBUG
2554 globalOutputStream() << "Brush::buildWindings: face " << i << ": removing unidirectional connectivity graph edge adjacent to face " << (*j).adjacent << "\n";
2567 /// \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.
2570 for(const_iterator i = begin(); i != end(); ++i)
2572 if(!(*i)->is_bounded())
2580 /// \brief Constructs the polygon windings for each face of the brush. Also updates the brush bounding-box and face texture-coordinates.
2581 bool buildWindings()
2585 m_aabb_local = AABB();
2587 for (std::size_t i = 0; i < m_faces.size(); ++i)
2589 Face& f = *m_faces[i];
2591 if(!plane3_valid(f.plane3()) || !plane_unique(i))
2593 f.getWinding().resize(0);
2597 #if BRUSH_CONNECTIVITY_DEBUG
2598 globalOutputStream() << "face: " << i << "\n";
2600 windingForClipPlane(f.getWinding(), f.plane3());
2602 // update brush bounds
2603 const Winding& winding = f.getWinding();
2604 for(Winding::const_iterator i = winding.begin(); i != winding.end(); ++i)
2606 aabb_extend_by_point_safe(m_aabb_local, (*i).vertex);
2609 // update texture coordinates
2610 f.EmitTextureCoordinates();
2615 bool degenerate = !isBounded();
2619 // clean up connectivity information.
2620 // these cleanups must be applied in a specific order.
2621 removeDegenerateEdges();
2622 removeDegenerateFaces();
2623 removeDuplicateEdges();
2624 verifyConnectivityGraph();
2630 /// \brief Constructs the face windings and updates anything that depends on them.
2638 class FaceInstanceSet
2640 typedef SelectionList<FaceInstance> FaceInstances;
2641 FaceInstances m_faceInstances;
2643 void insert(FaceInstance& faceInstance)
2645 m_faceInstances.append(faceInstance);
2647 void erase(FaceInstance& faceInstance)
2649 m_faceInstances.erase(faceInstance);
2652 template<typename Functor>
2653 void foreach(Functor functor)
2655 for(FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
2663 return m_faceInstances.empty();
2665 FaceInstance& last() const
2667 return m_faceInstances.back();
2671 extern FaceInstanceSet g_SelectedFaceInstances;
2673 typedef std::list<std::size_t> VertexSelection;
2675 inline VertexSelection::iterator VertexSelection_find(VertexSelection& self, std::size_t value)
2677 return std::find(self.begin(), self.end(), value);
2680 inline VertexSelection::const_iterator VertexSelection_find(const VertexSelection& self, std::size_t value)
2682 return std::find(self.begin(), self.end(), value);
2685 inline VertexSelection::iterator VertexSelection_insert(VertexSelection& self, std::size_t value)
2687 VertexSelection::iterator i = VertexSelection_find(self, value);
2690 self.push_back(value);
2691 return --self.end();
2695 inline void VertexSelection_erase(VertexSelection& self, std::size_t value)
2697 VertexSelection::iterator i = VertexSelection_find(self, value);
2704 inline bool triangle_reversed(std::size_t x, std::size_t y, std::size_t z)
2706 return !((x < y && y < z) || (z < x && x < y) || (y < z && z < x));
2708 template<typename Element>
2709 inline Vector3 triangle_cross(const BasicVector3<Element>& x, const BasicVector3<Element> y, const BasicVector3<Element>& z)
2711 return vector3_cross(y - x, z - x);
2713 template<typename Element>
2714 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)
2716 return vector3_dot(triangle_cross(x1, y1, z1), triangle_cross(x2, y2, z2)) > 0;
2720 typedef const Plane3* PlanePointer;
2721 typedef PlanePointer* PlanesIterator;
2723 class VectorLightList : public LightList
2725 typedef std::vector<const RendererLight*> Lights;
2728 void addLight(const RendererLight& light)
2730 m_lights.push_back(&light);
2736 void evaluateLights() const
2739 void lightsChanged() const
2742 void forEachLight(const RendererLightCallback& callback) const
2744 for(Lights::const_iterator i = m_lights.begin(); i != m_lights.end(); ++i)
2754 ObservedSelectable m_selectable;
2755 ObservedSelectable m_selectableVertices;
2756 ObservedSelectable m_selectableEdges;
2757 SelectionChangeCallback m_selectionChanged;
2759 VertexSelection m_vertexSelection;
2760 VertexSelection m_edgeSelection;
2763 mutable VectorLightList m_lights;
2765 FaceInstance(Face& face, const SelectionChangeCallback& observer) :
2767 m_selectable(SelectedChangedCaller(*this)),
2768 m_selectableVertices(observer),
2769 m_selectableEdges(observer),
2770 m_selectionChanged(observer)
2773 FaceInstance(const FaceInstance& other) :
2774 m_face(other.m_face),
2775 m_selectable(SelectedChangedCaller(*this)),
2776 m_selectableVertices(other.m_selectableVertices),
2777 m_selectableEdges(other.m_selectableEdges),
2778 m_selectionChanged(other.m_selectionChanged)
2781 FaceInstance& operator=(const FaceInstance& other)
2783 m_face = other.m_face;
2791 const Face& getFace() const
2796 void selectedChanged(const Selectable& selectable)
2798 if(selectable.isSelected())
2800 g_SelectedFaceInstances.insert(*this);
2804 g_SelectedFaceInstances.erase(*this);
2806 m_selectionChanged(selectable);
2808 typedef MemberCaller1<FaceInstance, const Selectable&, &FaceInstance::selectedChanged> SelectedChangedCaller;
2810 bool selectedVertices() const
2812 return !m_vertexSelection.empty();
2814 bool selectedEdges() const
2816 return !m_edgeSelection.empty();
2818 bool isSelected() const
2820 return m_selectable.isSelected();
2823 bool selectedComponents() const
2825 return selectedVertices() || selectedEdges() || isSelected();
2827 bool selectedComponents(SelectionSystem::EComponentMode mode) const
2831 case SelectionSystem::eVertex:
2832 return selectedVertices();
2833 case SelectionSystem::eEdge:
2834 return selectedEdges();
2835 case SelectionSystem::eFace:
2836 return isSelected();
2841 void setSelected(SelectionSystem::EComponentMode mode, bool select)
2845 case SelectionSystem::eFace:
2846 m_selectable.setSelected(select);
2848 case SelectionSystem::eVertex:
2849 ASSERT_MESSAGE(!select, "select-all not supported");
2851 m_vertexSelection.clear();
2852 m_selectableVertices.setSelected(false);
2854 case SelectionSystem::eEdge:
2855 ASSERT_MESSAGE(!select, "select-all not supported");
2857 m_edgeSelection.clear();
2858 m_selectableEdges.setSelected(false);
2865 template<typename Functor>
2866 void SelectedVertices_foreach(Functor functor) const
2868 for(VertexSelection::const_iterator i = m_vertexSelection.begin(); i != m_vertexSelection.end(); ++i)
2870 std::size_t index = Winding_FindAdjacent(getFace().getWinding(), *i);
2871 if(index != c_brush_maxFaces)
2873 functor(getFace().getWinding()[index].vertex);
2877 template<typename Functor>
2878 void SelectedEdges_foreach(Functor functor) const
2880 for(VertexSelection::const_iterator i = m_edgeSelection.begin(); i != m_edgeSelection.end(); ++i)
2882 std::size_t index = Winding_FindAdjacent(getFace().getWinding(), *i);
2883 if(index != c_brush_maxFaces)
2885 const Winding& winding = getFace().getWinding();
2886 std::size_t adjacent = Winding_next(winding, index);
2887 functor(vector3_mid(winding[index].vertex, winding[adjacent].vertex));
2891 template<typename Functor>
2892 void SelectedFaces_foreach(Functor functor) const
2896 functor(centroid());
2900 template<typename Functor>
2901 void SelectedComponents_foreach(Functor functor) const
2903 SelectedVertices_foreach(functor);
2904 SelectedEdges_foreach(functor);
2905 SelectedFaces_foreach(functor);
2908 void iterate_selected(AABB& aabb) const
2910 SelectedComponents_foreach(AABBExtendByPoint(aabb));
2913 class RenderablePointVectorPushBack
2915 RenderablePointVector& m_points;
2917 RenderablePointVectorPushBack(RenderablePointVector& points) : m_points(points)
2920 void operator()(const Vector3& point) const
2922 const Colour4b colour_selected(0, 0, 255, 255);
2923 m_points.push_back(pointvertex_for_windingpoint(point, colour_selected));
2927 void iterate_selected(RenderablePointVector& points) const
2929 SelectedComponents_foreach(RenderablePointVectorPushBack(points));
2932 bool intersectVolume(const VolumeTest& volume, const Matrix4& localToWorld) const
2934 return m_face->intersectVolume(volume, localToWorld);
2937 void render(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld) const
2939 if(!m_face->isFiltered() && m_face->contributes() && intersectVolume(volume, localToWorld))
2941 renderer.PushState();
2942 if(selectedComponents())
2944 renderer.Highlight(Renderer::eFace);
2946 m_face->render(renderer, localToWorld);
2947 renderer.PopState();
2951 void testSelect(SelectionTest& test, SelectionIntersection& best)
2953 if(!m_face->isFiltered())
2955 m_face->testSelect(test, best);
2958 void testSelect(Selector& selector, SelectionTest& test)
2960 SelectionIntersection best;
2961 testSelect(test, best);
2964 Selector_add(selector, m_selectable, best);
2967 void testSelect_centroid(Selector& selector, SelectionTest& test)
2969 if(m_face->contributes() && !m_face->isFiltered())
2971 SelectionIntersection best;
2972 m_face->testSelect_centroid(test, best);
2975 Selector_add(selector, m_selectable, best);
2980 void selectPlane(Selector& selector, const Line& line, PlanesIterator first, PlanesIterator last, const PlaneCallback& selectedPlaneCallback)
2982 for(Winding::const_iterator i = getFace().getWinding().begin(); i != getFace().getWinding().end(); ++i)
2984 Vector3 v(vector3_subtracted(line_closest_point(line, (*i).vertex), (*i).vertex));
2985 double dot = vector3_dot(getFace().plane3().normal(), v);
2992 Selector_add(selector, m_selectable);
2994 selectedPlaneCallback(getFace().plane3());
2996 void selectReversedPlane(Selector& selector, const SelectedPlanes& selectedPlanes)
2998 if(selectedPlanes.contains(plane3_flipped(getFace().plane3())))
3000 Selector_add(selector, m_selectable);
3004 void transformComponents(const Matrix4& matrix)
3008 m_face->transform(matrix, false);
3010 if(selectedVertices())
3012 if(m_vertexSelection.size() == 1)
3014 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[1]);
3015 m_face->assign_planepts(m_face->m_move_planeptsTransformed);
3017 else if(m_vertexSelection.size() == 2)
3019 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[1]);
3020 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[2]);
3021 m_face->assign_planepts(m_face->m_move_planeptsTransformed);
3023 else if(m_vertexSelection.size() >= 3)
3025 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[0]);
3026 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[1]);
3027 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[2]);
3028 m_face->assign_planepts(m_face->m_move_planeptsTransformed);
3033 if(m_edgeSelection.size() == 1)
3035 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[0]);
3036 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[1]);
3037 m_face->assign_planepts(m_face->m_move_planeptsTransformed);
3039 else if(m_edgeSelection.size() >= 2)
3041 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[0]);
3042 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[1]);
3043 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[2]);
3044 m_face->assign_planepts(m_face->m_move_planeptsTransformed);
3049 void snapto(float snap)
3051 m_face->snapto(snap);
3054 void snapComponents(float snap)
3060 if(selectedVertices())
3062 vector3_snap(m_face->m_move_planepts[0], snap);
3063 vector3_snap(m_face->m_move_planepts[1], snap);
3064 vector3_snap(m_face->m_move_planepts[2], snap);
3065 m_face->assign_planepts(m_face->m_move_planepts);
3066 planepts_assign(m_face->m_move_planeptsTransformed, m_face->m_move_planepts);
3067 m_face->freezeTransform();
3071 vector3_snap(m_face->m_move_planepts[0], snap);
3072 vector3_snap(m_face->m_move_planepts[1], snap);
3073 vector3_snap(m_face->m_move_planepts[2], snap);
3074 m_face->assign_planepts(m_face->m_move_planepts);
3075 planepts_assign(m_face->m_move_planeptsTransformed, m_face->m_move_planepts);
3076 m_face->freezeTransform();
3079 void update_move_planepts_vertex(std::size_t index)
3081 m_face->update_move_planepts_vertex(index, m_face->m_move_planepts);
3083 void update_move_planepts_vertex2(std::size_t index, std::size_t other)
3085 const std::size_t numpoints = m_face->getWinding().numpoints;
3086 ASSERT_MESSAGE(index < numpoints, "select_vertex: invalid index");
3088 const std::size_t opposite = Winding_Opposite(m_face->getWinding(), index, other);
3090 if(triangle_reversed(index, other, opposite))
3092 std::swap(index, other);
3096 triangles_same_winding(
3097 m_face->getWinding()[opposite].vertex,
3098 m_face->getWinding()[index].vertex,
3099 m_face->getWinding()[other].vertex,
3100 m_face->getWinding()[0].vertex,
3101 m_face->getWinding()[1].vertex,
3102 m_face->getWinding()[2].vertex
3104 "update_move_planepts_vertex2: error"
3107 m_face->m_move_planepts[0] = m_face->getWinding()[opposite].vertex;
3108 m_face->m_move_planepts[1] = m_face->getWinding()[index].vertex;
3109 m_face->m_move_planepts[2] = m_face->getWinding()[other].vertex;
3110 planepts_quantise(m_face->m_move_planepts, GRID_MIN); // winding points are very inaccurate
3112 void update_selection_vertex()
3114 if(m_vertexSelection.size() == 0)
3116 m_selectableVertices.setSelected(false);
3120 m_selectableVertices.setSelected(true);
3122 if(m_vertexSelection.size() == 1)
3124 std::size_t index = Winding_FindAdjacent(getFace().getWinding(), *m_vertexSelection.begin());
3126 if(index != c_brush_maxFaces)
3128 update_move_planepts_vertex(index);
3131 else if(m_vertexSelection.size() == 2)
3133 std::size_t index = Winding_FindAdjacent(getFace().getWinding(), *m_vertexSelection.begin());
3134 std::size_t other = Winding_FindAdjacent(getFace().getWinding(), *(++m_vertexSelection.begin()));
3136 if(index != c_brush_maxFaces
3137 && other != c_brush_maxFaces)
3139 update_move_planepts_vertex2(index, other);
3144 void select_vertex(std::size_t index, bool select)
3148 VertexSelection_insert(m_vertexSelection, getFace().getWinding()[index].adjacent);
3152 VertexSelection_erase(m_vertexSelection, getFace().getWinding()[index].adjacent);
3155 SceneChangeNotify();
3156 update_selection_vertex();
3159 bool selected_vertex(std::size_t index) const
3161 return VertexSelection_find(m_vertexSelection, getFace().getWinding()[index].adjacent) != m_vertexSelection.end();
3164 void update_move_planepts_edge(std::size_t index)
3166 std::size_t numpoints = m_face->getWinding().numpoints;
3167 ASSERT_MESSAGE(index < numpoints, "select_edge: invalid index");
3169 std::size_t adjacent = Winding_next(m_face->getWinding(), index);
3170 std::size_t opposite = Winding_Opposite(m_face->getWinding(), index);
3171 m_face->m_move_planepts[0] = m_face->getWinding()[index].vertex;
3172 m_face->m_move_planepts[1] = m_face->getWinding()[adjacent].vertex;
3173 m_face->m_move_planepts[2] = m_face->getWinding()[opposite].vertex;
3174 planepts_quantise(m_face->m_move_planepts, GRID_MIN); // winding points are very inaccurate
3176 void update_selection_edge()
3178 if(m_edgeSelection.size() == 0)
3180 m_selectableEdges.setSelected(false);
3184 m_selectableEdges.setSelected(true);
3186 if(m_edgeSelection.size() == 1)
3188 std::size_t index = Winding_FindAdjacent(getFace().getWinding(), *m_edgeSelection.begin());
3190 if(index != c_brush_maxFaces)
3192 update_move_planepts_edge(index);
3197 void select_edge(std::size_t index, bool select)
3201 VertexSelection_insert(m_edgeSelection, getFace().getWinding()[index].adjacent);
3205 VertexSelection_erase(m_edgeSelection, getFace().getWinding()[index].adjacent);
3208 SceneChangeNotify();
3209 update_selection_edge();
3212 bool selected_edge(std::size_t index) const
3214 return VertexSelection_find(m_edgeSelection, getFace().getWinding()[index].adjacent) != m_edgeSelection.end();
3217 const Vector3& centroid() const
3219 return m_face->centroid();
3222 void connectivityChanged()
3224 // This occurs when a face is added or removed.
3225 // The current vertex and edge selections no longer valid and must be cleared.
3226 m_vertexSelection.clear();
3227 m_selectableVertices.setSelected(false);
3228 m_edgeSelection.clear();
3229 m_selectableEdges.setSelected(false);
3233 class BrushClipPlane : public OpenGLRenderable
3237 static Shader* m_state;
3239 static void constructStatic()
3241 m_state = GlobalShaderCache().capture("$CLIPPER_OVERLAY");
3243 static void destroyStatic()
3245 GlobalShaderCache().release("$CLIPPER_OVERLAY");
3248 void setPlane(const Brush& brush, const Plane3& plane)
3251 if(plane3_valid(m_plane))
3253 brush.windingForClipPlane(m_winding, m_plane);
3257 m_winding.resize(0);
3261 void render(RenderStateFlags state) const
3263 if((state & RENDER_FILL) != 0)
3265 Winding_Draw(m_winding, m_plane.normal(), state);
3269 Winding_DrawWireframe(m_winding);
3271 // also draw a line indicating the direction of the cut
3272 Vector3 lineverts[2];
3273 Winding_Centroid(m_winding, m_plane, lineverts[0]);
3274 lineverts[1] = vector3_added(lineverts[0], vector3_scaled(m_plane.normal(), Brush::m_maxWorldCoord * 4));
3276 glVertexPointer(3, GL_FLOAT, sizeof(Vector3), &lineverts[0]);
3277 glDrawArrays(GL_LINES, 0, GLsizei(2));
3281 void render(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld) const
3283 renderer.SetState(m_state, Renderer::eWireframeOnly);
3284 renderer.SetState(m_state, Renderer::eFullMaterials);
3285 renderer.addRenderable(*this, localToWorld);
3289 inline void Face_addLight(const FaceInstance& face, const Matrix4& localToWorld, const RendererLight& light)
3291 const Plane3& facePlane = face.getFace().plane3();
3292 const Vector3& origin = light.aabb().origin;
3293 Plane3 tmp(plane3_transformed(Plane3(facePlane.normal(), -facePlane.dist()), localToWorld));
3294 if(!plane3_test_point(tmp, origin)
3295 || !plane3_test_point(tmp, vector3_added(origin, light.offset())))
3297 face.m_lights.addLight(light);
3303 typedef std::vector<FaceInstance> FaceInstances;
3305 class EdgeInstance : public Selectable
3307 FaceInstances& m_faceInstances;
3308 SelectableEdge* m_edge;
3310 void select_edge(bool select)
3312 FaceVertexId faceVertex = m_edge->m_faceVertex;
3313 m_faceInstances[faceVertex.getFace()].select_edge(faceVertex.getVertex(), select);
3314 faceVertex = next_edge(m_edge->m_faces, faceVertex);
3315 m_faceInstances[faceVertex.getFace()].select_edge(faceVertex.getVertex(), select);
3317 bool selected_edge() const
3319 FaceVertexId faceVertex = m_edge->m_faceVertex;
3320 if(!m_faceInstances[faceVertex.getFace()].selected_edge(faceVertex.getVertex()))
3324 faceVertex = next_edge(m_edge->m_faces, faceVertex);
3325 if(!m_faceInstances[faceVertex.getFace()].selected_edge(faceVertex.getVertex()))
3334 EdgeInstance(FaceInstances& faceInstances, SelectableEdge& edge)
3335 : m_faceInstances(faceInstances), m_edge(&edge)
3338 EdgeInstance& operator=(const EdgeInstance& other)
3340 m_edge = other.m_edge;
3344 void setSelected(bool select)
3346 select_edge(select);
3348 bool isSelected() const
3350 return selected_edge();
3354 void testSelect(Selector& selector, SelectionTest& test)
3356 SelectionIntersection best;
3357 m_edge->testSelect(test, best);
3360 Selector_add(selector, *this, best);
3365 class VertexInstance : public Selectable
3367 FaceInstances& m_faceInstances;
3368 SelectableVertex* m_vertex;
3370 void select_vertex(bool select)
3372 FaceVertexId faceVertex = m_vertex->m_faceVertex;
3375 m_faceInstances[faceVertex.getFace()].select_vertex(faceVertex.getVertex(), select);
3376 faceVertex = next_vertex(m_vertex->m_faces, faceVertex);
3378 while(faceVertex.getFace() != m_vertex->m_faceVertex.getFace());
3380 bool selected_vertex() const
3382 FaceVertexId faceVertex = m_vertex->m_faceVertex;
3385 if(!m_faceInstances[faceVertex.getFace()].selected_vertex(faceVertex.getVertex()))
3389 faceVertex = next_vertex(m_vertex->m_faces, faceVertex);
3391 while(faceVertex.getFace() != m_vertex->m_faceVertex.getFace());
3396 VertexInstance(FaceInstances& faceInstances, SelectableVertex& vertex)
3397 : m_faceInstances(faceInstances), m_vertex(&vertex)
3400 VertexInstance& operator=(const VertexInstance& other)
3402 m_vertex = other.m_vertex;
3406 void setSelected(bool select)
3408 select_vertex(select);
3410 bool isSelected() const
3412 return selected_vertex();
3415 void testSelect(Selector& selector, SelectionTest& test)
3417 SelectionIntersection best;
3418 m_vertex->testSelect(test, best);
3421 Selector_add(selector, *this, best);
3426 class BrushInstanceVisitor
3429 virtual void visit(FaceInstance& face) const = 0;
3432 class BrushInstance :
3433 public BrushObserver,
3434 public scene::Instance,
3437 public SelectionTestable,
3438 public ComponentSelectionTestable,
3439 public ComponentEditable,
3440 public ComponentSnappable,
3441 public PlaneSelectable,
3442 public LightCullable
3446 InstanceTypeCastTable m_casts;
3450 InstanceStaticCast<BrushInstance, Selectable>::install(m_casts);
3451 InstanceContainedCast<BrushInstance, Bounded>::install(m_casts);
3452 InstanceContainedCast<BrushInstance, Cullable>::install(m_casts);
3453 InstanceStaticCast<BrushInstance, Renderable>::install(m_casts);
3454 InstanceStaticCast<BrushInstance, SelectionTestable>::install(m_casts);
3455 InstanceStaticCast<BrushInstance, ComponentSelectionTestable>::install(m_casts);
3456 InstanceStaticCast<BrushInstance, ComponentEditable>::install(m_casts);
3457 InstanceStaticCast<BrushInstance, ComponentSnappable>::install(m_casts);
3458 InstanceStaticCast<BrushInstance, PlaneSelectable>::install(m_casts);
3459 InstanceIdentityCast<BrushInstance>::install(m_casts);
3460 InstanceContainedCast<BrushInstance, Transformable>::install(m_casts);
3462 InstanceTypeCastTable& get()
3471 FaceInstances m_faceInstances;
3473 typedef std::vector<EdgeInstance> EdgeInstances;
3474 EdgeInstances m_edgeInstances;
3475 typedef std::vector<VertexInstance> VertexInstances;
3476 VertexInstances m_vertexInstances;
3478 ObservedSelectable m_selectable;
3480 mutable RenderableWireframe m_render_wireframe;
3481 mutable RenderablePointVector m_render_selected;
3482 mutable AABB m_aabb_component;
3483 mutable Array<PointVertex> m_faceCentroidPointsCulled;
3484 RenderablePointArray m_render_faces_wireframe;
3485 mutable bool m_viewChanged; // requires re-evaluation of view-dependent cached data
3487 BrushClipPlane m_clipPlane;
3489 static Shader* m_state_selpoint;
3491 const LightList* m_lightList;
3493 TransformModifier m_transform;
3495 BrushInstance(const BrushInstance& other); // NOT COPYABLE
3496 BrushInstance& operator=(const BrushInstance& other); // NOT ASSIGNABLE
3498 static Counter* m_counter;
3500 typedef LazyStatic<TypeCasts> StaticTypeCasts;
3502 void lightsChanged()
3504 m_lightList->lightsChanged();
3506 typedef MemberCaller<BrushInstance, &BrushInstance::lightsChanged> LightsChangedCaller;
3508 STRING_CONSTANT(Name, "BrushInstance");
3510 BrushInstance(const scene::Path& path, scene::Instance* parent, Brush& brush) :
3511 Instance(path, parent, this, StaticTypeCasts::instance().get()),
3513 m_selectable(SelectedChangedCaller(*this)),
3514 m_render_selected(GL_POINTS),
3515 m_render_faces_wireframe(m_faceCentroidPointsCulled, GL_POINTS),
3516 m_viewChanged(false),
3517 m_transform(Brush::TransformChangedCaller(m_brush), ApplyTransformCaller(*this))
3519 m_brush.instanceAttach(Instance::path());
3520 m_brush.attach(*this);
3521 m_counter->increment();
3523 m_lightList = &GlobalShaderCache().attach(*this);
3524 m_brush.m_lightsChanged = LightsChangedCaller(*this); ///\todo Make this work with instancing.
3526 Instance::setTransformChangedCallback(LightsChangedCaller(*this));
3530 Instance::setTransformChangedCallback(Callback());
3532 m_brush.m_lightsChanged = Callback();
3533 GlobalShaderCache().detach(*this);
3535 m_counter->decrement();
3536 m_brush.detach(*this);
3537 m_brush.instanceDetach(Instance::path());
3544 const Brush& getBrush() const
3549 Bounded& get(NullType<Bounded>)
3553 Cullable& get(NullType<Cullable>)
3557 Transformable& get(NullType<Transformable>)
3562 void selectedChanged(const Selectable& selectable)
3564 GlobalSelectionSystem().getObserver(SelectionSystem::ePrimitive)(selectable);
3565 GlobalSelectionSystem().onSelectedChanged(*this, selectable);
3567 Instance::selectedChanged();
3569 typedef MemberCaller1<BrushInstance, const Selectable&, &BrushInstance::selectedChanged> SelectedChangedCaller;
3571 void selectedChangedComponent(const Selectable& selectable)
3573 GlobalSelectionSystem().getObserver(SelectionSystem::eComponent)(selectable);
3574 GlobalSelectionSystem().onComponentSelection(*this, selectable);
3576 typedef MemberCaller1<BrushInstance, const Selectable&, &BrushInstance::selectedChangedComponent> SelectedChangedComponentCaller;
3578 const BrushInstanceVisitor& forEachFaceInstance(const BrushInstanceVisitor& visitor)
3580 for(FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3587 static void constructStatic()
3589 m_state_selpoint = GlobalShaderCache().capture("$SELPOINT");
3591 static void destroyStatic()
3593 GlobalShaderCache().release("$SELPOINT");
3598 m_faceInstances.clear();
3600 void reserve(std::size_t size)
3602 m_faceInstances.reserve(size);
3605 void push_back(Face& face)
3607 m_faceInstances.push_back(FaceInstance(face, SelectedChangedComponentCaller(*this)));
3611 ASSERT_MESSAGE(!m_faceInstances.empty(), "erasing invalid element");
3612 m_faceInstances.pop_back();
3614 void erase(std::size_t index)
3616 ASSERT_MESSAGE(index < m_faceInstances.size(), "erasing invalid element");
3617 m_faceInstances.erase(m_faceInstances.begin() + index);
3619 void connectivityChanged()
3621 for(FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3623 (*i).connectivityChanged();
3629 m_edgeInstances.clear();
3631 void edge_push_back(SelectableEdge& edge)
3633 m_edgeInstances.push_back(EdgeInstance(m_faceInstances, edge));
3638 m_vertexInstances.clear();
3640 void vertex_push_back(SelectableVertex& vertex)
3642 m_vertexInstances.push_back(VertexInstance(m_faceInstances, vertex));
3645 void DEBUG_verify() const
3647 ASSERT_MESSAGE(m_faceInstances.size() == m_brush.DEBUG_size(), "FATAL: mismatch");
3650 bool isSelected() const
3652 return m_selectable.isSelected();
3654 void setSelected(bool select)
3656 m_selectable.setSelected(select);
3659 void update_selected() const
3661 m_render_selected.clear();
3662 for(FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3664 if((*i).getFace().contributes())
3666 (*i).iterate_selected(m_render_selected);
3671 void evaluateViewDependent(const VolumeTest& volume, const Matrix4& localToWorld) const
3675 m_viewChanged = false;
3677 bool faces_visible[c_brush_maxFaces];
3679 bool* j = faces_visible;
3680 for(FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i, ++j)
3682 *j = (*i).intersectVolume(volume, localToWorld);
3686 m_brush.update_wireframe(m_render_wireframe, faces_visible);
3687 m_brush.update_faces_wireframe(m_faceCentroidPointsCulled, faces_visible);
3691 void renderComponentsSelected(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld) const
3693 m_brush.evaluateBRep();
3696 if(!m_render_selected.empty())
3698 renderer.Highlight(Renderer::ePrimitive, false);
3699 renderer.SetState(m_state_selpoint, Renderer::eWireframeOnly);
3700 renderer.SetState(m_state_selpoint, Renderer::eFullMaterials);
3701 renderer.addRenderable(m_render_selected, localToWorld);
3705 void renderComponents(Renderer& renderer, const VolumeTest& volume) const
3707 m_brush.evaluateBRep();
3709 const Matrix4& localToWorld = Instance::localToWorld();
3711 renderer.SetState(m_brush.m_state_point, Renderer::eWireframeOnly);
3712 renderer.SetState(m_brush.m_state_point, Renderer::eFullMaterials);
3714 if(volume.fill() && GlobalSelectionSystem().ComponentMode() == SelectionSystem::eFace)
3716 evaluateViewDependent(volume, localToWorld);
3717 renderer.addRenderable(m_render_faces_wireframe, localToWorld);
3721 m_brush.renderComponents(GlobalSelectionSystem().ComponentMode(), renderer, volume, localToWorld);
3725 void renderClipPlane(Renderer& renderer, const VolumeTest& volume) const
3727 if(GlobalSelectionSystem().ManipulatorMode() == SelectionSystem::eClip && isSelected())
3729 m_clipPlane.render(renderer, volume, localToWorld());
3733 void renderCommon(Renderer& renderer, const VolumeTest& volume) const
3735 bool componentMode = GlobalSelectionSystem().Mode() == SelectionSystem::eComponent;
3737 if(componentMode && isSelected())
3739 renderComponents(renderer, volume);
3742 if(parentSelected())
3746 renderer.Highlight(Renderer::eFace);
3748 renderer.Highlight(Renderer::ePrimitive);
3752 void renderSolid(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld) const
3754 //renderCommon(renderer, volume);
3756 m_lightList->evaluateLights();
3758 for(FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3760 renderer.setLights((*i).m_lights);
3761 (*i).render(renderer, volume, localToWorld);
3764 renderComponentsSelected(renderer, volume, localToWorld);
3767 void renderWireframe(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld) const
3769 //renderCommon(renderer, volume);
3771 evaluateViewDependent(volume, localToWorld);
3773 if(m_render_wireframe.m_size != 0)
3775 renderer.addRenderable(m_render_wireframe, localToWorld);
3778 renderComponentsSelected(renderer, volume, localToWorld);
3781 void renderSolid(Renderer& renderer, const VolumeTest& volume) const
3783 m_brush.evaluateBRep();
3785 renderClipPlane(renderer, volume);
3787 renderSolid(renderer, volume, localToWorld());
3790 void renderWireframe(Renderer& renderer, const VolumeTest& volume) const
3792 m_brush.evaluateBRep();
3794 renderClipPlane(renderer, volume);
3796 renderWireframe(renderer, volume, localToWorld());
3799 void viewChanged() const
3801 m_viewChanged = true;
3804 void testSelect(Selector& selector, SelectionTest& test)
3806 test.BeginMesh(localToWorld());
3808 SelectionIntersection best;
3809 for(FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3811 (*i).testSelect(test, best);
3815 selector.addIntersection(best);
3819 bool isSelectedComponents() const
3821 for(FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3823 if((*i).selectedComponents())
3830 void setSelectedComponents(bool select, SelectionSystem::EComponentMode mode)
3832 for(FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3834 (*i).setSelected(mode, select);
3837 void testSelectComponents(Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode)
3839 test.BeginMesh(localToWorld());
3843 case SelectionSystem::eVertex:
3845 for(VertexInstances::iterator i = m_vertexInstances.begin(); i != m_vertexInstances.end(); ++i)
3847 (*i).testSelect(selector, test);
3851 case SelectionSystem::eEdge:
3853 for(EdgeInstances::iterator i = m_edgeInstances.begin(); i != m_edgeInstances.end(); ++i)
3855 (*i).testSelect(selector, test);
3859 case SelectionSystem::eFace:
3861 if(test.getVolume().fill())
3863 for(FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3865 (*i).testSelect(selector, test);
3870 for(FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3872 (*i).testSelect_centroid(selector, test);
3882 void selectPlanes(Selector& selector, SelectionTest& test, const PlaneCallback& selectedPlaneCallback)
3884 test.BeginMesh(localToWorld());
3886 PlanePointer brushPlanes[c_brush_maxFaces];
3887 PlanesIterator j = brushPlanes;
3889 for(Brush::const_iterator i = m_brush.begin(); i != m_brush.end(); ++i)
3891 *j++ = &(*i)->plane3();
3894 for(FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3896 (*i).selectPlane(selector, Line(test.getNear(), test.getFar()), brushPlanes, j, selectedPlaneCallback);
3899 void selectReversedPlanes(Selector& selector, const SelectedPlanes& selectedPlanes)
3901 for(FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3903 (*i).selectReversedPlane(selector, selectedPlanes);
3908 void transformComponents(const Matrix4& matrix)
3910 for(FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3912 (*i).transformComponents(matrix);
3915 const AABB& getSelectedComponentsBounds() const
3917 m_aabb_component = AABB();
3919 for(FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3921 (*i).iterate_selected(m_aabb_component);
3924 return m_aabb_component;
3927 void snapComponents(float snap)
3929 for(FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3931 (*i).snapComponents(snap);
3934 void evaluateTransform()
3936 Matrix4 matrix(m_transform.calculateTransform());
3937 //globalOutputStream() << "matrix: " << matrix << "\n";
3939 if(m_transform.getType() == TRANSFORM_PRIMITIVE)
3941 m_brush.transform(matrix);
3945 transformComponents(matrix);
3948 void applyTransform()
3950 m_brush.revertTransform();
3951 evaluateTransform();
3952 m_brush.freezeTransform();
3954 typedef MemberCaller<BrushInstance, &BrushInstance::applyTransform> ApplyTransformCaller;
3956 void setClipPlane(const Plane3& plane)
3958 m_clipPlane.setPlane(m_brush, plane);
3961 bool testLight(const RendererLight& light) const
3963 return light.testAABB(worldAABB());
3965 void insertLight(const RendererLight& light)
3967 const Matrix4& localToWorld = Instance::localToWorld();
3968 for(FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3970 Face_addLight(*i, localToWorld, light);
3975 for(FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3977 (*i).m_lights.clear();
3982 inline BrushInstance* Instance_getBrush(scene::Instance& instance)
3984 return InstanceTypeCast<BrushInstance>::cast(instance);
3988 template<typename Functor>
3989 class BrushSelectedVisitor : public SelectionSystem::Visitor
3991 const Functor& m_functor;
3993 BrushSelectedVisitor(const Functor& functor) : m_functor(functor)
3996 void visit(scene::Instance& instance) const
3998 BrushInstance* brush = Instance_getBrush(instance);
4006 template<typename Functor>
4007 inline const Functor& Scene_forEachSelectedBrush(const Functor& functor)
4009 GlobalSelectionSystem().foreachSelected(BrushSelectedVisitor<Functor>(functor));
4013 template<typename Functor>
4014 class BrushVisibleSelectedVisitor : public SelectionSystem::Visitor
4016 const Functor& m_functor;
4018 BrushVisibleSelectedVisitor(const Functor& functor) : m_functor(functor)
4021 void visit(scene::Instance& instance) const
4023 BrushInstance* brush = Instance_getBrush(instance);
4025 && instance.path().top().get().visible())
4032 template<typename Functor>
4033 inline const Functor& Scene_forEachVisibleSelectedBrush(const Functor& functor)
4035 GlobalSelectionSystem().foreachSelected(BrushVisibleSelectedVisitor<Functor>(functor));
4039 class BrushForEachFace
4041 const BrushInstanceVisitor& m_visitor;
4043 BrushForEachFace(const BrushInstanceVisitor& visitor) : m_visitor(visitor)
4046 void operator()(BrushInstance& brush) const
4048 brush.forEachFaceInstance(m_visitor);
4052 template<class Functor>
4053 class FaceInstanceVisitFace : public BrushInstanceVisitor
4055 const Functor& functor;
4057 FaceInstanceVisitFace(const Functor& functor)
4061 void visit(FaceInstance& face) const
4063 functor(face.getFace());
4067 template<typename Functor>
4068 inline const Functor& Brush_forEachFace(BrushInstance& brush, const Functor& functor)
4070 brush.forEachFaceInstance(FaceInstanceVisitFace<Functor>(functor));
4074 template<class Functor>
4075 class FaceVisitAll : public BrushVisitor
4077 const Functor& functor;
4079 FaceVisitAll(const Functor& functor)
4083 void visit(Face& face) const
4089 template<typename Functor>
4090 inline const Functor& Brush_forEachFace(const Brush& brush, const Functor& functor)
4092 brush.forEachFace(FaceVisitAll<Functor>(functor));
4096 template<typename Functor>
4097 inline const Functor& Brush_forEachFace(Brush& brush, const Functor& functor)
4099 brush.forEachFace(FaceVisitAll<Functor>(functor));
4103 template<class Functor>
4104 class FaceInstanceVisitAll : public BrushInstanceVisitor
4106 const Functor& functor;
4108 FaceInstanceVisitAll(const Functor& functor)
4112 void visit(FaceInstance& face) const
4118 template<typename Functor>
4119 inline const Functor& Brush_ForEachFaceInstance(BrushInstance& brush, const Functor& functor)
4121 brush.forEachFaceInstance(FaceInstanceVisitAll<Functor>(functor));
4125 template<typename Functor>
4126 inline const Functor& Scene_forEachBrush(scene::Graph& graph, const Functor& functor)
4128 graph.traverse(InstanceWalker< InstanceApply<BrushInstance, Functor> >(functor));
4132 template<typename Type, typename Functor>
4133 class InstanceIfVisible : public Functor
4136 InstanceIfVisible(const Functor& functor) : Functor(functor)
4139 void operator()(scene::Instance& instance)
4141 if(instance.path().top().get().visible())
4143 Functor::operator()(instance);
4148 template<typename Functor>
4149 class BrushVisibleWalker : public scene::Graph::Walker
4151 const Functor& m_functor;
4153 BrushVisibleWalker(const Functor& functor) : m_functor(functor)
4156 bool pre(const scene::Path& path, scene::Instance& instance) const
4158 if(path.top().get().visible())
4160 BrushInstance* brush = Instance_getBrush(instance);
4170 template<typename Functor>
4171 inline const Functor& Scene_forEachVisibleBrush(scene::Graph& graph, const Functor& functor)
4173 graph.traverse(BrushVisibleWalker<Functor>(functor));
4177 template<typename Functor>
4178 inline const Functor& Scene_ForEachBrush_ForEachFace(scene::Graph& graph, const Functor& functor)
4180 Scene_forEachBrush(graph, BrushForEachFace(FaceInstanceVisitFace<Functor>(functor)));
4185 template<typename Functor>
4186 inline const Functor& Scene_ForEachBrush_ForEachFaceInstance(scene::Graph& graph, const Functor& functor)
4188 Scene_forEachBrush(graph, BrushForEachFace(FaceInstanceVisitAll<Functor>(functor)));
4192 template<typename Functor>
4193 inline const Functor& Scene_ForEachSelectedBrush_ForEachFace(scene::Graph& graph, const Functor& functor)
4195 Scene_forEachSelectedBrush(BrushForEachFace(FaceInstanceVisitFace<Functor>(functor)));
4199 template<typename Functor>
4200 inline const Functor& Scene_ForEachSelectedBrush_ForEachFaceInstance(scene::Graph& graph, const Functor& functor)
4202 Scene_forEachSelectedBrush(BrushForEachFace(FaceInstanceVisitAll<Functor>(functor)));
4206 template<typename Functor>
4207 class FaceVisitorWrapper
4209 const Functor& functor;
4211 FaceVisitorWrapper(const Functor& functor) : functor(functor)
4215 void operator()(FaceInstance& faceInstance) const
4217 functor(faceInstance.getFace());
4221 template<typename Functor>
4222 inline const Functor& Scene_ForEachSelectedBrushFace(scene::Graph& graph, const Functor& functor)
4224 g_SelectedFaceInstances.foreach(FaceVisitorWrapper<Functor>(functor));