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 #define CONTENTS_DETAIL 0x8000000
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 unsigned int ContentFlags_assignable(unsigned int contentFlags)
383 return contentFlags & ~CONTENTS_DETAIL;
386 inline ContentsFlagsValue ContentsFlagsValue_maskDetail(const ContentsFlagsValue& other)
388 return ContentsFlagsValue(other.m_surfaceFlags, ContentFlags_assignable(other.m_contentFlags), other.m_value, other.m_specified);
392 class FaceShader : public ModuleObserver
398 CopiedString m_shader;
399 ContentsFlagsValue m_flags;
401 SavedState(const FaceShader& faceShader)
403 m_shader = faceShader.getShader();
404 m_flags = faceShader.m_flags;
407 void exportState(FaceShader& faceShader) const
409 faceShader.setShader(m_shader.c_str());
410 faceShader.setFlags(m_flags);
414 CopiedString m_shader;
416 ContentsFlagsValue m_flags;
417 FaceShaderObserverPair m_observers;
421 FaceShader(const char* shader, const ContentsFlagsValue& flags = ContentsFlagsValue(0, 0, 0, false)) :
434 // copy-construction not supported
435 FaceShader(const FaceShader& other);
437 void instanceAttach()
440 m_state->incrementUsed();
442 void instanceDetach()
444 m_state->decrementUsed();
450 ASSERT_MESSAGE(m_state == 0, "shader cannot be captured");
451 brush_check_shader(m_shader.c_str());
452 m_state = GlobalShaderCache().capture(m_shader.c_str());
453 m_state->attach(*this);
457 ASSERT_MESSAGE(m_state != 0, "shader cannot be released");
458 m_state->detach(*this);
459 GlobalShaderCache().release(m_shader.c_str());
465 ASSERT_MESSAGE(!m_realised, "FaceTexdef::realise: already realised");
467 m_observers.forEach(FaceShaderObserverRealise());
471 ASSERT_MESSAGE(m_realised, "FaceTexdef::unrealise: already unrealised");
472 m_observers.forEach(FaceShaderObserverUnrealise());
476 void attach(FaceShaderObserver& observer)
478 m_observers.attach(observer);
481 observer.realiseShader();
485 void detach(FaceShaderObserver& observer)
489 observer.unrealiseShader();
491 m_observers.detach(observer);
494 const char* getShader() const
496 return m_shader.c_str();
498 void setShader(const char* name)
502 m_state->decrementUsed();
509 m_state->incrementUsed();
512 ContentsFlagsValue getFlags() const
514 ASSERT_MESSAGE(m_realised, "FaceShader::getFlags: flags not valid when unrealised");
515 if(!m_flags.m_specified)
517 return ContentsFlagsValue(
518 m_state->getTexture().surfaceFlags,
519 m_state->getTexture().contentFlags,
520 m_state->getTexture().value,
526 void setFlags(const ContentsFlagsValue& flags)
528 ASSERT_MESSAGE(m_realised, "FaceShader::setFlags: flags not valid when unrealised");
529 m_flags = ContentsFlagsValue_maskDetail(flags);
532 Shader* state() const
537 std::size_t width() const
541 return m_state->getTexture().width;
545 std::size_t height() const
549 return m_state->getTexture().height;
553 unsigned int shaderFlags() const
557 return m_state->getFlags();
566 class FaceTexdef : public FaceShaderObserver
569 FaceTexdef(const FaceTexdef& other);
571 FaceTexdef& operator=(const FaceTexdef& other);
576 TextureProjection m_projection;
578 SavedState(const FaceTexdef& faceTexdef)
580 m_projection = faceTexdef.m_projection;
583 void exportState(FaceTexdef& faceTexdef) const
585 Texdef_Assign(faceTexdef.m_projection, m_projection);
589 FaceShader& m_shader;
590 TextureProjection m_projection;
591 bool m_projectionInitialised;
596 const TextureProjection& projection,
597 bool projectionInitialised = true
600 m_projection(projection),
601 m_projectionInitialised(projectionInitialised),
602 m_scaleApplied(false)
604 m_shader.attach(*this);
608 m_shader.detach(*this);
613 ASSERT_MESSAGE(!m_scaleApplied, "texture scale aready added");
614 m_scaleApplied = true;
615 m_projection.m_brushprimit_texdef.addScale(m_shader.width(), m_shader.height());
619 ASSERT_MESSAGE(m_scaleApplied, "texture scale aready removed");
620 m_scaleApplied = false;
621 m_projection.m_brushprimit_texdef.removeScale(m_shader.width(), m_shader.height());
626 if(m_projectionInitialised && !m_scaleApplied)
631 void unrealiseShader()
633 if(m_projectionInitialised && m_scaleApplied)
639 void setTexdef(const TextureProjection& projection)
642 Texdef_Assign(m_projection, projection);
646 void shift(float s, float t)
648 ASSERT_MESSAGE(texdef_sane(m_projection.m_texdef), "FaceTexdef::shift: bad texdef");
650 Texdef_Shift(m_projection, s, t);
654 void scale(float s, float t)
657 Texdef_Scale(m_projection, s, t);
661 void rotate(float angle)
664 Texdef_Rotate(m_projection, angle);
668 void fit(const Vector3& normal, const Winding& winding, float s_repeat, float t_repeat)
670 Texdef_FitTexture(m_projection, m_shader.width(), m_shader.height(), normal, winding, s_repeat, t_repeat);
673 void emitTextureCoordinates(Winding& winding, const Vector3& normal, const Matrix4& localToWorld)
675 Texdef_EmitTextureCoordinates(m_projection, m_shader.width(), m_shader.height(), winding, normal, localToWorld);
678 void transform(const Plane3& plane, const Matrix4& matrix)
681 Texdef_transformLocked(m_projection, m_shader.width(), m_shader.height(), plane, matrix);
685 TextureProjection normalised() const
687 brushprimit_texdef_t tmp(m_projection.m_brushprimit_texdef);
688 tmp.removeScale(m_shader.width(), m_shader.height());
689 return TextureProjection(m_projection.m_texdef, tmp, m_projection.m_basis_s, m_projection.m_basis_t);
691 void setBasis(const Vector3& normal)
694 Normal_GetTransform(normal, basis);
695 m_projection.m_basis_s = Vector3(basis.xx(), basis.yx(), basis.zx());
696 m_projection.m_basis_t = Vector3(-basis.xy(), -basis.yy(), -basis.zy());
700 inline void planepts_print(const PlanePoints& planePoints, TextOutputStream& ostream)
702 ostream << "( " << planePoints[0][0] << " " << planePoints[0][1] << " " << planePoints[0][2] << " ) "
703 << "( " << planePoints[1][0] << " " << planePoints[1][1] << " " << planePoints[1][2] << " ) "
704 << "( " << planePoints[2][0] << " " << planePoints[2][1] << " " << planePoints[2][2] << " )";
708 inline Plane3 Plane3_applyTranslation(const Plane3& plane, const Vector3& translation)
710 Plane3 tmp(plane3_translated(Plane3(plane.normal(), -plane.dist()), translation));
711 return Plane3(tmp.normal(), -tmp.dist());
714 inline Plane3 Plane3_applyTransform(const Plane3& plane, const Matrix4& matrix)
716 Plane3 tmp(plane3_transformed(Plane3(plane.normal(), -plane.dist()), matrix));
717 return Plane3(tmp.normal(), -tmp.dist());
722 PlanePoints m_planepts;
723 Plane3 m_planeCached;
726 Vector3 m_funcStaticOrigin;
728 static EBrushType m_type;
730 static bool isDoom3Plane()
732 return FacePlane::m_type == eBrushTypeDoom3 || FacePlane::m_type == eBrushTypeQuake4;
738 PlanePoints m_planepts;
741 SavedState(const FacePlane& facePlane)
743 if(facePlane.isDoom3Plane())
745 m_plane = facePlane.m_plane;
749 planepts_assign(m_planepts, facePlane.planePoints());
753 void exportState(FacePlane& facePlane) const
755 if(facePlane.isDoom3Plane())
757 facePlane.m_plane = m_plane;
758 facePlane.updateTranslated();
762 planepts_assign(facePlane.planePoints(), m_planepts);
763 facePlane.MakePlane();
768 FacePlane() : m_funcStaticOrigin(0, 0, 0)
771 FacePlane(const FacePlane& other) : m_funcStaticOrigin(0, 0, 0)
775 planepts_assign(m_planepts, other.m_planepts);
780 m_plane = other.m_plane;
790 if(check_plane_is_integer(m_planepts))
792 globalErrorStream() << "non-integer planepts: ";
793 planepts_print(m_planepts, globalErrorStream());
794 globalErrorStream() << "\n";
797 m_planeCached = plane3_for_points(m_planepts);
805 vector3_swap(m_planepts[0], m_planepts[2]);
810 m_planeCached = plane3_flipped(m_plane);
814 void transform(const Matrix4& matrix, bool mirror)
820 bool off = check_plane_is_integer(planePoints());
823 matrix4_transform_point(matrix, m_planepts[0]);
824 matrix4_transform_point(matrix, m_planepts[1]);
825 matrix4_transform_point(matrix, m_planepts[2]);
833 if(check_plane_is_integer(planePoints()))
837 globalErrorStream() << "caused by transform\n";
845 m_planeCached = Plane3_applyTransform(m_planeCached, matrix);
849 void offset(float offset)
853 Vector3 move(vector3_scaled(m_planeCached.normal(), -offset));
855 vector3_subtract(m_planepts[0], move);
856 vector3_subtract(m_planepts[1], move);
857 vector3_subtract(m_planepts[2], move);
863 m_planeCached.d += offset;
868 void updateTranslated()
870 m_planeCached = Plane3_applyTranslation(m_plane, m_funcStaticOrigin);
874 m_plane = Plane3_applyTranslation(m_planeCached, vector3_negated(m_funcStaticOrigin));
878 PlanePoints& planePoints()
882 const PlanePoints& planePoints() const
886 const Plane3& plane3() const
888 return m_planeCached;
890 void setDoom3Plane(const Plane3& plane)
895 const Plane3& getDoom3Plane() const
900 void copy(const FacePlane& other)
904 planepts_assign(m_planepts, other.m_planepts);
909 m_planeCached = other.m_plane;
913 void copy(const Vector3& p0, const Vector3& p1, const Vector3& p2)
924 m_planeCached = plane3_for_points(p2, p1, p0);
930 inline void Winding_testSelect(Winding& winding, SelectionTest& test, SelectionIntersection& best)
932 test.TestPolygon(VertexPointer(reinterpret_cast<VertexPointer::pointer>(&winding.points.data()->vertex), sizeof(WindingVertex)), winding.numpoints, best);
935 const double GRID_MIN = 0.125;
937 inline double quantiseInteger(double f)
939 return float_to_integer(f);
942 inline double quantiseFloating(double f)
944 return float_snapped(f, 1.f / (1 << 16));
947 typedef double (*QuantiseFunc)(double f);
954 virtual bool filter(const Face& face) const = 0;
957 bool face_filtered(Face& face);
959 void Brush_addTextureChangedCallback(const SignalHandler& callback);
960 void Brush_textureChanged();
963 extern bool g_brush_texturelock_enabled;
968 virtual void planeChanged() = 0;
969 virtual void connectivityChanged() = 0;
970 virtual void shaderChanged() = 0;
971 virtual void evaluateTransform() = 0;
975 public OpenGLRenderable,
978 public FaceShaderObserver
980 std::size_t m_refcount;
982 class SavedState : public UndoMemento
985 FacePlane::SavedState m_planeState;
986 FaceTexdef::SavedState m_texdefState;
987 FaceShader::SavedState m_shaderState;
989 SavedState(const Face& face) : m_planeState(face.getPlane()), m_texdefState(face.getTexdef()), m_shaderState(face.getShader())
993 void exportState(Face& face) const
995 m_planeState.exportState(face.getPlane());
996 m_shaderState.exportState(face.getShader());
997 m_texdefState.exportState(face.getTexdef());
1007 static QuantiseFunc m_quantise;
1008 static EBrushType m_type;
1010 PlanePoints m_move_planepts;
1011 PlanePoints m_move_planeptsTransformed;
1014 FacePlane m_planeTransformed;
1015 FaceShader m_shader;
1016 FaceTexdef m_texdef;
1017 TextureProjection m_texdefTransformed;
1023 FaceObserver* m_observer;
1024 UndoObserver* m_undoable_observer;
1027 // assignment not supported
1028 Face& operator=(const Face& other);
1029 // copy-construction not supported
1030 Face(const Face& other);
1034 Face(FaceObserver* observer) :
1036 m_shader(texdef_name_default()),
1037 m_texdef(m_shader, TextureProjection(), false),
1039 m_observer(observer),
1040 m_undoable_observer(0),
1043 m_shader.attach(*this);
1044 m_plane.copy(Vector3(0, 0, 0), Vector3(64, 0, 0), Vector3(0, 64, 0));
1045 m_texdef.setBasis(m_plane.plane3().normal());
1053 const TextureProjection& projection,
1054 FaceObserver* observer
1058 m_texdef(m_shader, projection),
1059 m_observer(observer),
1060 m_undoable_observer(0),
1063 m_shader.attach(*this);
1064 m_plane.copy(p0, p1, p2);
1065 m_texdef.setBasis(m_plane.plane3().normal());
1069 Face(const Face& other, FaceObserver* observer) :
1071 m_shader(other.m_shader.getShader(), other.m_shader.m_flags),
1072 m_texdef(m_shader, other.getTexdef().normalised()),
1073 m_observer(observer),
1074 m_undoable_observer(0),
1077 m_shader.attach(*this);
1078 m_plane.copy(other.m_plane);
1079 planepts_assign(m_move_planepts, other.m_move_planepts);
1080 m_texdef.setBasis(m_plane.plane3().normal());
1086 m_shader.detach(*this);
1092 m_observer->planeChanged();
1095 void realiseShader()
1097 m_observer->shaderChanged();
1099 void unrealiseShader()
1103 void instanceAttach(MapFile* map)
1105 m_shader.instanceAttach();
1107 m_undoable_observer = GlobalUndoSystem().observer(this);
1108 GlobalFilterSystem().registerFilterable(*this);
1110 void instanceDetach(MapFile* map)
1112 GlobalFilterSystem().unregisterFilterable(*this);
1113 m_undoable_observer = 0;
1114 GlobalUndoSystem().release(this);
1116 m_shader.instanceDetach();
1119 void render(RenderStateFlags state) const
1121 Winding_Draw(m_winding, m_planeTransformed.plane3().normal(), state);
1124 void updateFiltered()
1126 m_filtered = face_filtered(*this);
1128 bool isFiltered() const
1139 if(m_undoable_observer != 0)
1141 m_undoable_observer->save(this);
1146 UndoMemento* exportState() const
1148 return new SavedState(*this);
1150 void importState(const UndoMemento* data)
1154 static_cast<const SavedState*>(data)->exportState(*this);
1157 m_observer->connectivityChanged();
1159 m_observer->shaderChanged();
1169 if(--m_refcount == 0)
1179 bool intersectVolume(const VolumeTest& volume, const Matrix4& localToWorld) const
1181 return volume.TestPlane(Plane3(plane3().normal(), -plane3().dist()), localToWorld);
1184 void render(Renderer& renderer, const Matrix4& localToWorld) const
1186 renderer.SetState(m_shader.state(), Renderer::eFullMaterials);
1187 renderer.addRenderable(*this, localToWorld);
1190 void transform(const Matrix4& matrix, bool mirror)
1192 if(g_brush_texturelock_enabled)
1194 Texdef_transformLocked(m_texdefTransformed, m_shader.width(), m_shader.height(), m_plane.plane3(), matrix);
1197 m_planeTransformed.transform(matrix, mirror);
1200 ASSERT_MESSAGE(projectionaxis_for_normal(normal) == projectionaxis_for_normal(plane3().normal()), "bleh");
1202 m_observer->planeChanged();
1205 void assign_planepts(const PlanePoints planepts)
1207 m_planeTransformed.copy(planepts[0], planepts[1], planepts[2]);
1208 m_observer->planeChanged();
1211 /// \brief Reverts the transformable state of the brush to identity.
1212 void revertTransform()
1214 m_planeTransformed = m_plane;
1215 planepts_assign(m_move_planeptsTransformed, m_move_planepts);
1216 m_texdefTransformed = m_texdef.m_projection;
1218 void freezeTransform()
1221 m_plane = m_planeTransformed;
1222 planepts_assign(m_move_planepts, m_move_planeptsTransformed);
1223 m_texdef.m_projection = m_texdefTransformed;
1226 void update_move_planepts_vertex(std::size_t index, PlanePoints planePoints)
1228 std::size_t numpoints = getWinding().numpoints;
1229 ASSERT_MESSAGE(index < numpoints, "update_move_planepts_vertex: invalid index");
1231 std::size_t opposite = Winding_Opposite(getWinding(), index);
1232 std::size_t adjacent = Winding_wrap(getWinding(), opposite+numpoints-1);
1233 planePoints[0] = getWinding()[opposite].vertex;
1234 planePoints[1] = getWinding()[index].vertex;
1235 planePoints[2] = getWinding()[adjacent].vertex;
1236 // winding points are very inaccurate, so they must be quantised before using them to generate the face-plane
1237 planepts_quantise(planePoints, GRID_MIN);
1240 void snapto(float snap)
1245 ASSERT_MESSAGE(plane3_valid(m_plane.plane3()), "invalid plane before snap to grid");
1246 planepts_snap(m_plane.planePoints(), snap);
1247 ASSERT_MESSAGE(plane3_valid(m_plane.plane3()), "invalid plane after snap to grid");
1249 PlanePoints planePoints;
1250 update_move_planepts_vertex(0, planePoints);
1251 vector3_snap(planePoints[0], snap);
1252 vector3_snap(planePoints[1], snap);
1253 vector3_snap(planePoints[2], snap);
1254 assign_planepts(planePoints);
1257 SceneChangeNotify();
1258 if(!plane3_valid(m_plane.plane3()))
1260 globalErrorStream() << "WARNING: invalid plane after snap to grid\n";
1265 void testSelect(SelectionTest& test, SelectionIntersection& best)
1267 Winding_testSelect(m_winding, test, best);
1270 void testSelect_centroid(SelectionTest& test, SelectionIntersection& best)
1272 test.TestPoint(m_centroid, best);
1275 void shaderChanged()
1277 EmitTextureCoordinates();
1278 Brush_textureChanged();
1279 m_observer->shaderChanged();
1281 SceneChangeNotify();
1284 const char* GetShader() const
1286 return m_shader.getShader();
1288 void SetShader(const char* name)
1291 m_shader.setShader(name);
1297 m_texdefTransformed = m_texdef.m_projection;
1299 void texdefChanged()
1302 EmitTextureCoordinates();
1303 Brush_textureChanged();
1306 void GetTexdef(TextureProjection& projection) const
1308 projection = m_texdef.normalised();
1310 void SetTexdef(const TextureProjection& projection)
1313 m_texdef.setTexdef(projection);
1317 void GetFlags(ContentsFlagsValue& flags) const
1319 flags = m_shader.getFlags();
1321 void SetFlags(const ContentsFlagsValue& flags)
1324 m_shader.setFlags(flags);
1325 m_observer->shaderChanged();
1329 void ShiftTexdef(float s, float t)
1332 m_texdef.shift(s, t);
1336 void ScaleTexdef(float s, float t)
1339 m_texdef.scale(s, t);
1343 void RotateTexdef(float angle)
1346 m_texdef.rotate(angle);
1350 void FitTexture(float s_repeat, float t_repeat)
1353 m_texdef.fit(m_plane.plane3().normal(), m_winding, s_repeat, t_repeat);
1357 void EmitTextureCoordinates()
1359 Texdef_EmitTextureCoordinates(m_texdefTransformed, m_shader.width(), m_shader.height(), m_winding, plane3().normal(), g_matrix4_identity);
1363 const Vector3& centroid() const
1368 void construct_centroid()
1370 Winding_Centroid(m_winding, plane3(), m_centroid);
1373 const Winding& getWinding() const
1377 Winding& getWinding()
1382 const Plane3& plane3() const
1384 m_observer->evaluateTransform();
1385 return m_planeTransformed.plane3();
1387 FacePlane& getPlane()
1391 const FacePlane& getPlane() const
1395 FaceTexdef& getTexdef()
1399 const FaceTexdef& getTexdef() const
1403 FaceShader& getShader()
1407 const FaceShader& getShader() const
1412 bool isDetail() const
1414 return (m_shader.m_flags.m_contentFlags & CONTENTS_DETAIL) != 0;
1416 void setDetail(bool detail)
1419 if(detail && !isDetail())
1421 m_shader.m_flags.m_contentFlags |= CONTENTS_DETAIL;
1423 else if(!detail && isDetail())
1425 m_shader.m_flags.m_contentFlags &= ~CONTENTS_DETAIL;
1427 m_observer->shaderChanged();
1430 bool contributes() const
1432 return m_winding.numpoints > 2;
1434 bool is_bounded() const
1436 for(Winding::const_iterator i = m_winding.begin(); i != m_winding.end(); ++i)
1438 if((*i).adjacent == c_brush_maxFaces)
1451 std::size_t m_vertex;
1454 FaceVertexId(std::size_t face, std::size_t vertex)
1455 : m_face(face), m_vertex(vertex)
1459 std::size_t getFace() const
1463 std::size_t getVertex() const
1469 typedef std::size_t faceIndex_t;
1471 struct EdgeRenderIndices
1477 : first(0), second(0)
1480 EdgeRenderIndices(const RenderIndex _first, const RenderIndex _second)
1481 : first(_first), second(_second)
1492 : first(c_brush_maxFaces), second(c_brush_maxFaces)
1495 EdgeFaces(const faceIndex_t _first, const faceIndex_t _second)
1496 : first(_first), second(_second)
1501 class RenderableWireframe : public OpenGLRenderable
1504 void render(RenderStateFlags state) const
1507 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(PointVertex), &m_vertices->colour);
1508 glVertexPointer(3, GL_FLOAT, sizeof(PointVertex), &m_vertices->vertex);
1509 glDrawElements(GL_LINES, GLsizei(m_size<<1), RenderIndexTypeID, m_faceVertex.data());
1512 for(std::size_t i = 0; i < m_size; ++i)
1514 glVertex3fv(&m_vertices[m_faceVertex[i].first].vertex.x);
1515 glVertex3fv(&m_vertices[m_faceVertex[i].second].vertex.x);
1521 Array<EdgeRenderIndices> m_faceVertex;
1523 const PointVertex* m_vertices;
1527 typedef std::vector<Brush*> brush_vector_t;
1532 virtual bool filter(const Brush& brush) const = 0;
1535 bool brush_filtered(Brush& brush);
1536 void add_brush_filter(BrushFilter& filter, int mask, bool invert = false);
1539 /// \brief Returns true if 'self' takes priority when building brush b-rep.
1540 inline bool plane3_inside(const Plane3& self, const Plane3& other)
1542 if(vector3_equal_epsilon(self.normal(), other.normal(), 0.001))
1544 return self.dist() < other.dist();
1549 typedef SmartPointer<Face> FaceSmartPointer;
1550 typedef std::vector<FaceSmartPointer> Faces;
1552 /// \brief Returns the unique-id of the edge adjacent to \p faceVertex in the edge-pair for the set of \p faces.
1553 inline FaceVertexId next_edge(const Faces& faces, FaceVertexId faceVertex)
1555 std::size_t adjacent_face = faces[faceVertex.getFace()]->getWinding()[faceVertex.getVertex()].adjacent;
1556 std::size_t adjacent_vertex = Winding_FindAdjacent(faces[adjacent_face]->getWinding(), faceVertex.getFace());
1558 ASSERT_MESSAGE(adjacent_vertex != c_brush_maxFaces, "connectivity data invalid");
1559 if(adjacent_vertex == c_brush_maxFaces)
1564 return FaceVertexId(adjacent_face, adjacent_vertex);
1567 /// \brief Returns the unique-id of the vertex adjacent to \p faceVertex in the vertex-ring for the set of \p faces.
1568 inline FaceVertexId next_vertex(const Faces& faces, FaceVertexId faceVertex)
1570 FaceVertexId nextEdge = next_edge(faces, faceVertex);
1571 return FaceVertexId(nextEdge.getFace(), Winding_next(faces[nextEdge.getFace()]->getWinding(), nextEdge.getVertex()));
1574 class SelectableEdge
1576 Vector3 getEdge() const
1578 const Winding& winding = getFace().getWinding();
1579 return vector3_mid(winding[m_faceVertex.getVertex()].vertex, winding[Winding_next(winding, m_faceVertex.getVertex())].vertex);
1584 FaceVertexId m_faceVertex;
1586 SelectableEdge(Faces& faces, FaceVertexId faceVertex)
1587 : m_faces(faces), m_faceVertex(faceVertex)
1590 SelectableEdge& operator=(const SelectableEdge& other)
1592 m_faceVertex = other.m_faceVertex;
1596 Face& getFace() const
1598 return *m_faces[m_faceVertex.getFace()];
1601 void testSelect(SelectionTest& test, SelectionIntersection& best)
1603 test.TestPoint(getEdge(), best);
1607 class SelectableVertex
1609 Vector3 getVertex() const
1611 return getFace().getWinding()[m_faceVertex.getVertex()].vertex;
1616 FaceVertexId m_faceVertex;
1618 SelectableVertex(Faces& faces, FaceVertexId faceVertex)
1619 : m_faces(faces), m_faceVertex(faceVertex)
1622 SelectableVertex& operator=(const SelectableVertex& other)
1624 m_faceVertex = other.m_faceVertex;
1628 Face& getFace() const
1630 return *m_faces[m_faceVertex.getFace()];
1633 void testSelect(SelectionTest& test, SelectionIntersection& best)
1635 test.TestPoint(getVertex(), best);
1642 virtual void reserve(std::size_t size) = 0;
1643 virtual void clear() = 0;
1644 virtual void push_back(Face& face) = 0;
1645 virtual void pop_back() = 0;
1646 virtual void erase(std::size_t index) = 0;
1647 virtual void connectivityChanged() = 0;
1649 virtual void edge_clear() = 0;
1650 virtual void edge_push_back(SelectableEdge& edge) = 0;
1652 virtual void vertex_clear() = 0;
1653 virtual void vertex_push_back(SelectableVertex& vertex) = 0;
1655 virtual void DEBUG_verify() const = 0;
1661 virtual void visit(Face& face) const = 0;
1665 public TransformNode,
1670 public FaceObserver,
1676 scene::Node* m_node;
1677 typedef UniqueSet<BrushObserver*> Observers;
1678 Observers m_observers;
1679 UndoObserver* m_undoable_observer;
1686 // cached data compiled from state
1687 Array<PointVertex> m_faceCentroidPoints;
1688 RenderablePointArray m_render_faces;
1690 Array<PointVertex> m_uniqueVertexPoints;
1691 typedef std::vector<SelectableVertex> SelectableVertices;
1692 SelectableVertices m_select_vertices;
1693 RenderablePointArray m_render_vertices;
1695 Array<PointVertex> m_uniqueEdgePoints;
1696 typedef std::vector<SelectableEdge> SelectableEdges;
1697 SelectableEdges m_select_edges;
1698 RenderablePointArray m_render_edges;
1700 Array<EdgeRenderIndices> m_edge_indices;
1701 Array<EdgeFaces> m_edge_faces;
1706 Callback m_evaluateTransform;
1707 Callback m_boundsChanged;
1709 mutable bool m_planeChanged; // b-rep evaluation required
1710 mutable bool m_transformChanged; // transform evaluation required
1714 STRING_CONSTANT(Name, "Brush");
1716 Callback m_lightsChanged;
1719 static Shader* m_state_point;
1722 static EBrushType m_type;
1723 static double m_maxWorldCoord;
1725 Brush(scene::Node& node, const Callback& evaluateTransform, const Callback& boundsChanged) :
1727 m_undoable_observer(0),
1729 m_render_faces(m_faceCentroidPoints, GL_POINTS),
1730 m_render_vertices(m_uniqueVertexPoints, GL_POINTS),
1731 m_render_edges(m_uniqueEdgePoints, GL_POINTS),
1732 m_planeChanged(false),
1733 m_transformChanged(false),
1734 m_evaluateTransform(evaluateTransform),
1735 m_boundsChanged(boundsChanged)
1739 Brush(const Brush& other, scene::Node& node, const Callback& evaluateTransform, const Callback& boundsChanged) :
1741 m_undoable_observer(0),
1743 m_render_faces(m_faceCentroidPoints, GL_POINTS),
1744 m_render_vertices(m_uniqueVertexPoints, GL_POINTS),
1745 m_render_edges(m_uniqueEdgePoints, GL_POINTS),
1746 m_planeChanged(false),
1747 m_transformChanged(false),
1748 m_evaluateTransform(evaluateTransform),
1749 m_boundsChanged(boundsChanged)
1753 Brush(const Brush& other) :
1754 TransformNode(other),
1758 FaceObserver(other),
1763 m_undoable_observer(0),
1765 m_render_faces(m_faceCentroidPoints, GL_POINTS),
1766 m_render_vertices(m_uniqueVertexPoints, GL_POINTS),
1767 m_render_edges(m_uniqueEdgePoints, GL_POINTS),
1768 m_planeChanged(false),
1769 m_transformChanged(false)
1775 ASSERT_MESSAGE(m_observers.empty(), "Brush::~Brush: observers still attached");
1778 // assignment not supported
1779 Brush& operator=(const Brush& other);
1781 void setDoom3GroupOrigin(const Vector3& origin)
1783 //globalOutputStream() << "func_static origin before: " << m_funcStaticOrigin << " after: " << origin << "\n";
1784 for(Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i)
1786 (*i)->getPlane().m_funcStaticOrigin = origin;
1787 (*i)->getPlane().updateTranslated();
1788 (*i)->planeChanged();
1793 void attach(BrushObserver& observer)
1795 for(Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i)
1797 observer.push_back(*(*i));
1800 for(SelectableEdges::iterator i = m_select_edges.begin(); i !=m_select_edges.end(); ++i)
1802 observer.edge_push_back(*i);
1805 for(SelectableVertices::iterator i = m_select_vertices.begin(); i != m_select_vertices.end(); ++i)
1807 observer.vertex_push_back(*i);
1810 m_observers.insert(&observer);
1812 void detach(BrushObserver& observer)
1814 m_observers.erase(&observer);
1817 void forEachFace(const BrushVisitor& visitor) const
1819 for(Faces::const_iterator i = m_faces.begin(); i != m_faces.end(); ++i)
1821 visitor.visit(*(*i));
1825 void forEachFace_instanceAttach(MapFile* map) const
1827 for(Faces::const_iterator i = m_faces.begin(); i != m_faces.end(); ++i)
1829 (*i)->instanceAttach(map);
1832 void forEachFace_instanceDetach(MapFile* map) const
1834 for(Faces::const_iterator i = m_faces.begin(); i != m_faces.end(); ++i)
1836 (*i)->instanceDetach(map);
1840 InstanceCounter m_instanceCounter;
1841 void instanceAttach(const scene::Path& path)
1843 if(++m_instanceCounter.m_count == 1)
1845 m_map = path_find_mapfile(path.begin(), path.end());
1846 m_undoable_observer = GlobalUndoSystem().observer(this);
1847 GlobalFilterSystem().registerFilterable(*this);
1848 forEachFace_instanceAttach(m_map);
1852 ASSERT_MESSAGE(path_find_mapfile(path.begin(), path.end()) == m_map, "node is instanced across more than one file");
1855 void instanceDetach(const scene::Path& path)
1857 if(--m_instanceCounter.m_count == 0)
1859 forEachFace_instanceDetach(m_map);
1860 GlobalFilterSystem().unregisterFilterable(*this);
1862 m_undoable_observer = 0;
1863 GlobalUndoSystem().release(this);
1868 const char* name() const
1872 void attach(const NameCallback& callback)
1875 void detach(const NameCallback& callback)
1880 void updateFiltered()
1884 if(brush_filtered(*this))
1886 m_node->enable(scene::Node::eFiltered);
1890 m_node->disable(scene::Node::eFiltered);
1898 m_planeChanged = true;
1902 void shaderChanged()
1907 void evaluateBRep() const
1911 m_planeChanged = false;
1912 const_cast<Brush*>(this)->buildBRep();
1916 void transformChanged()
1918 m_transformChanged = true;
1921 typedef MemberCaller<Brush, &Brush::transformChanged> TransformChangedCaller;
1923 void evaluateTransform()
1925 if(m_transformChanged)
1927 m_transformChanged = false;
1929 m_evaluateTransform();
1932 const Matrix4& localToParent() const
1934 return g_matrix4_identity;
1940 const AABB& localAABB() const
1943 return m_aabb_local;
1946 VolumeIntersectionValue intersectVolume(const VolumeTest& test, const Matrix4& localToWorld) const
1948 return test.TestAABB(m_aabb_local, localToWorld);
1951 void renderComponents(SelectionSystem::EComponentMode mode, Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld) const
1955 case SelectionSystem::eVertex:
1956 renderer.addRenderable(m_render_vertices, localToWorld);
1958 case SelectionSystem::eEdge:
1959 renderer.addRenderable(m_render_edges, localToWorld);
1961 case SelectionSystem::eFace:
1962 renderer.addRenderable(m_render_faces, localToWorld);
1969 void transform(const Matrix4& matrix)
1971 bool mirror = matrix4_handedness(matrix) == MATRIX4_LEFTHANDED;
1973 for(Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i)
1975 (*i)->transform(matrix, mirror);
1978 void snapto(float snap)
1980 for(Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i)
1985 void revertTransform()
1987 for(Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i)
1989 (*i)->revertTransform();
1992 void freezeTransform()
1994 for(Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i)
1996 (*i)->freezeTransform();
2000 /// \brief Returns the absolute index of the \p faceVertex.
2001 std::size_t absoluteIndex(FaceVertexId faceVertex)
2003 std::size_t index = 0;
2004 for(std::size_t i = 0; i < faceVertex.getFace(); ++i)
2006 index += m_faces[i]->getWinding().numpoints;
2008 return index + faceVertex.getVertex();
2011 void appendFaces(const Faces& other)
2014 for(Faces::const_iterator i = other.begin(); i != other.end(); ++i)
2020 /// \brief The undo memento for a brush stores only the list of face references - the faces are not copied.
2021 class BrushUndoMemento : public UndoMemento
2024 BrushUndoMemento(const Faces& faces) : m_faces(faces)
2041 if(m_undoable_observer != 0)
2043 m_undoable_observer->save(this);
2047 UndoMemento* exportState() const
2049 return new BrushUndoMemento(m_faces);
2052 void importState(const UndoMemento* state)
2055 appendFaces(static_cast<const BrushUndoMemento*>(state)->m_faces);
2058 for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i)
2060 (*i)->DEBUG_verify();
2066 return !m_faces.empty() && m_faces.front()->isDetail();
2069 /// \brief Appends a copy of \p face to the end of the face list.
2070 Face* addFace(const Face& face)
2072 if(m_faces.size() == c_brush_maxFaces)
2077 push_back(FaceSmartPointer(new Face(face, this)));
2078 m_faces.back()->setDetail(isDetail());
2080 return m_faces.back();
2083 /// \brief Appends a new face constructed from the parameters to the end of the face list.
2084 Face* addPlane(const Vector3& p0, const Vector3& p1, const Vector3& p2, const char* shader, const TextureProjection& projection)
2086 if(m_faces.size() == c_brush_maxFaces)
2091 push_back(FaceSmartPointer(new Face(p0, p1, p2, shader, projection, this)));
2092 m_faces.back()->setDetail(isDetail());
2094 return m_faces.back();
2097 static void constructStatic(EBrushType type)
2100 Face::m_type = type;
2101 FacePlane::m_type = type;
2103 g_bp_globals.m_texdefTypeId = TEXDEFTYPEID_QUAKE;
2104 if(m_type == eBrushTypeQuake3BP || m_type == eBrushTypeDoom3 || m_type == eBrushTypeQuake4)
2106 g_bp_globals.m_texdefTypeId = TEXDEFTYPEID_BRUSHPRIMITIVES;
2107 g_brush_texturelock_enabled = true;
2109 else if(m_type == eBrushTypeHalfLife)
2111 g_bp_globals.m_texdefTypeId = TEXDEFTYPEID_HALFLIFE;
2112 g_brush_texturelock_enabled = true;
2115 Face::m_quantise = (m_type == eBrushTypeQuake) ? quantiseInteger : quantiseFloating;
2117 m_state_point = GlobalShaderCache().capture("$POINT");
2119 static void destroyStatic()
2121 GlobalShaderCache().release("$POINT");
2124 std::size_t DEBUG_size()
2126 return m_faces.size();
2129 typedef Faces::const_iterator const_iterator;
2131 const_iterator begin() const
2133 return m_faces.begin();
2135 const_iterator end() const
2137 return m_faces.end();
2142 return m_faces.back();
2144 const Face* back() const
2146 return m_faces.back();
2148 void reserve(std::size_t count)
2150 m_faces.reserve(count);
2151 for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i)
2153 (*i)->reserve(count);
2156 void push_back(Faces::value_type face)
2158 m_faces.push_back(face);
2159 if(m_instanceCounter.m_count != 0)
2161 m_faces.back()->instanceAttach(m_map);
2163 for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i)
2165 (*i)->push_back(*face);
2166 (*i)->DEBUG_verify();
2171 if(m_instanceCounter.m_count != 0)
2173 m_faces.back()->instanceDetach(m_map);
2176 for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i)
2179 (*i)->DEBUG_verify();
2182 void erase(std::size_t index)
2184 if(m_instanceCounter.m_count != 0)
2186 m_faces[index]->instanceDetach(m_map);
2188 m_faces.erase(m_faces.begin() + index);
2189 for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i)
2192 (*i)->DEBUG_verify();
2195 void connectivityChanged()
2197 for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i)
2199 (*i)->connectivityChanged();
2207 if(m_instanceCounter.m_count != 0)
2209 forEachFace_instanceDetach(m_map);
2212 for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i)
2215 (*i)->DEBUG_verify();
2218 std::size_t size() const
2220 return m_faces.size();
2224 return m_faces.empty();
2227 /// \brief Returns true if any face of the brush contributes to the final B-Rep.
2228 bool hasContributingFaces() const
2230 for(const_iterator i = begin(); i != end(); ++i)
2232 if((*i)->contributes())
2240 /// \brief Removes faces that do not contribute to the brush. This is useful for cleaning up after CSG operations on the brush.
2241 /// 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.
2242 void removeEmptyFaces()
2248 while(i < m_faces.size())
2250 if(!m_faces[i]->contributes())
2263 /// \brief Constructs \p winding from the intersection of \p plane with the other planes of the brush.
2264 void windingForClipPlane(Winding& winding, const Plane3& plane) const
2266 FixedWinding buffer[2];
2269 // get a poly that covers an effectively infinite area
2270 Winding_createInfinite(buffer[swap], plane, m_maxWorldCoord + 1);
2272 // chop the poly by all of the other faces
2274 for (std::size_t i = 0; i < m_faces.size(); ++i)
2276 const Face& clip = *m_faces[i];
2278 if(plane3_equal(clip.plane3(), plane)
2279 || !plane3_valid(clip.plane3()) || !plane_unique(i)
2280 || plane3_opposing(plane, clip.plane3()))
2285 buffer[!swap].clear();
2287 #if BRUSH_CONNECTIVITY_DEBUG
2288 globalOutputStream() << "clip vs face: " << i << "\n";
2292 // flip the plane, because we want to keep the back side
2293 Plane3 clipPlane(vector3_negated(clip.plane3().normal()), -clip.plane3().dist());
2294 Winding_Clip(buffer[swap], plane, clipPlane, i, buffer[!swap]);
2297 #if BRUSH_CONNECTIVITY_DEBUG
2298 for(FixedWinding::Points::iterator k = buffer[!swap].points.begin(), j = buffer[!swap].points.end() - 1; k != buffer[!swap].points.end(); j = k, ++k)
2300 if(vector3_length_squared(vector3_subtracted((*k).vertex, (*j).vertex)) < 1)
2302 globalOutputStream() << "v: " << std::distance(buffer[!swap].points.begin(), j) << " tiny edge adjacent to face " << (*j).adjacent << "\n";
2307 //ASSERT_MESSAGE(buffer[!swap].numpoints != 1, "created single-point winding");
2313 Winding_forFixedWinding(winding, buffer[swap]);
2315 #if BRUSH_CONNECTIVITY_DEBUG
2316 Winding_printConnectivity(winding);
2318 for(Winding::iterator i = winding.begin(), j = winding.end() - 1; i != winding.end(); j = i, ++i)
2320 if(vector3_length_squared(vector3_subtracted((*i).vertex, (*j).vertex)) < 1)
2322 globalOutputStream() << "v: " << std::distance(winding.begin(), j) << " tiny edge adjacent to face " << (*j).adjacent << "\n";
2328 void update_wireframe(RenderableWireframe& wire, const bool* faces_visible) const
2330 wire.m_faceVertex.resize(m_edge_indices.size());
2331 wire.m_vertices = m_uniqueVertexPoints.data();
2333 for(std::size_t i = 0; i < m_edge_faces.size(); ++i)
2335 if(faces_visible[m_edge_faces[i].first]
2336 || faces_visible[m_edge_faces[i].second])
2338 wire.m_faceVertex[wire.m_size++] = m_edge_indices[i];
2344 void update_faces_wireframe(Array<PointVertex>& wire, const bool* faces_visible) const
2346 std::size_t count = 0;
2347 for(std::size_t i = 0; i < m_faceCentroidPoints.size(); ++i)
2349 if(faces_visible[i])
2356 Array<PointVertex>::iterator p = wire.begin();
2357 for(std::size_t i = 0; i < m_faceCentroidPoints.size(); ++i)
2359 if(faces_visible[i])
2361 *p++ = m_faceCentroidPoints[i];
2366 /// \brief Makes this brush a deep-copy of the \p other.
2367 void copy(const Brush& other)
2369 for(Faces::const_iterator i = other.m_faces.begin(); i != other.m_faces.end(); ++i)
2377 void edge_push_back(FaceVertexId faceVertex)
2379 m_select_edges.push_back(SelectableEdge(m_faces, faceVertex));
2380 for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i)
2382 (*i)->edge_push_back(m_select_edges.back());
2387 m_select_edges.clear();
2388 for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i)
2393 void vertex_push_back(FaceVertexId faceVertex)
2395 m_select_vertices.push_back(SelectableVertex(m_faces, faceVertex));
2396 for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i)
2398 (*i)->vertex_push_back(m_select_vertices.back());
2403 m_select_vertices.clear();
2404 for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i)
2406 (*i)->vertex_clear();
2410 /// \brief Returns true if the face identified by \p index is preceded by another plane that takes priority over it.
2411 bool plane_unique(std::size_t index) const
2414 for(std::size_t i = 0; i < m_faces.size(); ++i)
2416 if(index != i && !plane3_inside(m_faces[index]->plane3(), m_faces[i]->plane3()))
2424 /// \brief Removes edges that are smaller than the tolerance used when generating brush windings.
2425 void removeDegenerateEdges()
2427 for (std::size_t i = 0; i < m_faces.size(); ++i)
2429 Winding& winding = m_faces[i]->getWinding();
2430 for(Winding::iterator j = winding.begin(); j != winding.end();)
2432 std::size_t index = std::distance(winding.begin(), j);
2433 std::size_t next = Winding_next(winding, index);
2434 if(Edge_isDegenerate(winding[index].vertex, winding[next].vertex))
2436 #if BRUSH_DEGENERATE_DEBUG
2437 globalOutputStream() << "Brush::buildWindings: face " << i << ": degenerate edge adjacent to " << winding[index].adjacent << "\n";
2439 Winding& other = m_faces[winding[index].adjacent]->getWinding();
2440 std::size_t adjacent = Winding_FindAdjacent(other, i);
2441 if(adjacent != c_brush_maxFaces)
2443 other.erase(other.begin() + adjacent);
2455 /// \brief Invalidates faces that have only two vertices in their winding, while preserving edge-connectivity information.
2456 void removeDegenerateFaces()
2458 // save adjacency info for degenerate faces
2459 for (std::size_t i = 0; i < m_faces.size(); ++i)
2461 Winding& degen = m_faces[i]->getWinding();
2463 if(degen.numpoints == 2)
2465 #if BRUSH_DEGENERATE_DEBUG
2466 globalOutputStream() << "Brush::buildWindings: face " << i << ": degenerate winding adjacent to " << degen[0].adjacent << ", " << degen[1].adjacent << "\n";
2468 // this is an "edge" face, where the plane touches the edge of the brush
2470 Winding& winding = m_faces[degen[0].adjacent]->getWinding();
2471 std::size_t index = Winding_FindAdjacent(winding, i);
2472 if(index != c_brush_maxFaces)
2474 #if BRUSH_DEGENERATE_DEBUG
2475 globalOutputStream() << "Brush::buildWindings: face " << degen[0].adjacent << ": remapping adjacent " << winding[index].adjacent << " to " << degen[1].adjacent << "\n";
2477 winding[index].adjacent = degen[1].adjacent;
2482 Winding& winding = m_faces[degen[1].adjacent]->getWinding();
2483 std::size_t index = Winding_FindAdjacent(winding, i);
2484 if(index != c_brush_maxFaces)
2486 #if BRUSH_DEGENERATE_DEBUG
2487 globalOutputStream() << "Brush::buildWindings: face " << degen[1].adjacent << ": remapping adjacent " << winding[index].adjacent << " to " << degen[0].adjacent << "\n";
2489 winding[index].adjacent = degen[0].adjacent;
2498 /// \brief Removes edges that have the same adjacent-face as their immediate neighbour.
2499 void removeDuplicateEdges()
2501 // verify face connectivity graph
2502 for(std::size_t i = 0; i < m_faces.size(); ++i)
2504 //if(m_faces[i]->contributes())
2506 Winding& winding = m_faces[i]->getWinding();
2507 for(std::size_t j = 0; j != winding.numpoints;)
2509 std::size_t next = Winding_next(winding, j);
2510 if(winding[j].adjacent == winding[next].adjacent)
2512 #if BRUSH_DEGENERATE_DEBUG
2513 globalOutputStream() << "Brush::buildWindings: face " << i << ": removed duplicate edge adjacent to face " << winding[j].adjacent << "\n";
2515 winding.erase(winding.begin() + next);
2526 /// \brief Removes edges that do not have a matching pair in their adjacent-face.
2527 void verifyConnectivityGraph()
2529 // verify face connectivity graph
2530 for(std::size_t i = 0; i < m_faces.size(); ++i)
2532 //if(m_faces[i]->contributes())
2534 Winding& winding = m_faces[i]->getWinding();
2535 for(Winding::iterator j = winding.begin(); j != winding.end();)
2537 #if BRUSH_CONNECTIVITY_DEBUG
2538 globalOutputStream() << "Brush::buildWindings: face " << i << ": adjacent to face " << (*j).adjacent << "\n";
2540 // remove unidirectional graph edges
2541 if((*j).adjacent == c_brush_maxFaces
2542 || Winding_FindAdjacent(m_faces[(*j).adjacent]->getWinding(), i) == c_brush_maxFaces)
2544 #if BRUSH_CONNECTIVITY_DEBUG
2545 globalOutputStream() << "Brush::buildWindings: face " << i << ": removing unidirectional connectivity graph edge adjacent to face " << (*j).adjacent << "\n";
2558 /// \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.
2561 for(const_iterator i = begin(); i != end(); ++i)
2563 if(!(*i)->is_bounded())
2571 /// \brief Constructs the polygon windings for each face of the brush. Also updates the brush bounding-box and face texture-coordinates.
2572 bool buildWindings()
2576 m_aabb_local = AABB();
2578 for (std::size_t i = 0; i < m_faces.size(); ++i)
2580 Face& f = *m_faces[i];
2582 if(!plane3_valid(f.plane3()) || !plane_unique(i))
2584 f.getWinding().resize(0);
2588 #if BRUSH_CONNECTIVITY_DEBUG
2589 globalOutputStream() << "face: " << i << "\n";
2591 windingForClipPlane(f.getWinding(), f.plane3());
2593 // update brush bounds
2594 const Winding& winding = f.getWinding();
2595 for(Winding::const_iterator i = winding.begin(); i != winding.end(); ++i)
2597 aabb_extend_by_point_safe(m_aabb_local, (*i).vertex);
2600 // update texture coordinates
2601 f.EmitTextureCoordinates();
2606 bool degenerate = !isBounded();
2610 // clean up connectivity information.
2611 // these cleanups must be applied in a specific order.
2612 removeDegenerateEdges();
2613 removeDegenerateFaces();
2614 removeDuplicateEdges();
2615 verifyConnectivityGraph();
2621 /// \brief Constructs the face windings and updates anything that depends on them.
2629 class FaceInstanceSet
2631 typedef SelectionList<FaceInstance> FaceInstances;
2632 FaceInstances m_faceInstances;
2634 void insert(FaceInstance& faceInstance)
2636 m_faceInstances.append(faceInstance);
2638 void erase(FaceInstance& faceInstance)
2640 m_faceInstances.erase(faceInstance);
2643 template<typename Functor>
2644 void foreach(Functor functor)
2646 for(FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
2654 return m_faceInstances.empty();
2656 FaceInstance& last() const
2658 return m_faceInstances.back();
2662 extern FaceInstanceSet g_SelectedFaceInstances;
2664 typedef std::list<std::size_t> VertexSelection;
2666 inline VertexSelection::iterator VertexSelection_find(VertexSelection& self, std::size_t value)
2668 return std::find(self.begin(), self.end(), value);
2671 inline VertexSelection::const_iterator VertexSelection_find(const VertexSelection& self, std::size_t value)
2673 return std::find(self.begin(), self.end(), value);
2676 inline VertexSelection::iterator VertexSelection_insert(VertexSelection& self, std::size_t value)
2678 VertexSelection::iterator i = VertexSelection_find(self, value);
2681 self.push_back(value);
2682 return --self.end();
2686 inline void VertexSelection_erase(VertexSelection& self, std::size_t value)
2688 VertexSelection::iterator i = VertexSelection_find(self, value);
2695 inline bool triangle_reversed(std::size_t x, std::size_t y, std::size_t z)
2697 return !((x < y && y < z) || (z < x && x < y) || (y < z && z < x));
2699 template<typename Element>
2700 inline Vector3 triangle_cross(const BasicVector3<Element>& x, const BasicVector3<Element> y, const BasicVector3<Element>& z)
2702 return vector3_cross(y - x, z - x);
2704 template<typename Element>
2705 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)
2707 return vector3_dot(triangle_cross(x1, y1, z1), triangle_cross(x2, y2, z2)) > 0;
2711 typedef const Plane3* PlanePointer;
2712 typedef PlanePointer* PlanesIterator;
2714 class VectorLightList : public LightList
2716 typedef std::vector<const RendererLight*> Lights;
2719 void addLight(const RendererLight& light)
2721 m_lights.push_back(&light);
2727 void evaluateLights() const
2730 void lightsChanged() const
2733 void forEachLight(const RendererLightCallback& callback) const
2735 for(Lights::const_iterator i = m_lights.begin(); i != m_lights.end(); ++i)
2745 ObservedSelectable m_selectable;
2746 ObservedSelectable m_selectableVertices;
2747 ObservedSelectable m_selectableEdges;
2748 SelectionChangeCallback m_selectionChanged;
2750 VertexSelection m_vertexSelection;
2751 VertexSelection m_edgeSelection;
2754 mutable VectorLightList m_lights;
2756 FaceInstance(Face& face, const SelectionChangeCallback& observer) :
2758 m_selectable(SelectedChangedCaller(*this)),
2759 m_selectableVertices(observer),
2760 m_selectableEdges(observer),
2761 m_selectionChanged(observer)
2764 FaceInstance(const FaceInstance& other) :
2765 m_face(other.m_face),
2766 m_selectable(SelectedChangedCaller(*this)),
2767 m_selectableVertices(other.m_selectableVertices),
2768 m_selectableEdges(other.m_selectableEdges),
2769 m_selectionChanged(other.m_selectionChanged)
2772 FaceInstance& operator=(const FaceInstance& other)
2774 m_face = other.m_face;
2782 const Face& getFace() const
2787 void selectedChanged(const Selectable& selectable)
2789 if(selectable.isSelected())
2791 g_SelectedFaceInstances.insert(*this);
2795 g_SelectedFaceInstances.erase(*this);
2797 m_selectionChanged(selectable);
2799 typedef MemberCaller1<FaceInstance, const Selectable&, &FaceInstance::selectedChanged> SelectedChangedCaller;
2801 bool selectedVertices() const
2803 return !m_vertexSelection.empty();
2805 bool selectedEdges() const
2807 return !m_edgeSelection.empty();
2809 bool isSelected() const
2811 return m_selectable.isSelected();
2814 bool selectedComponents() const
2816 return selectedVertices() || selectedEdges() || isSelected();
2818 bool selectedComponents(SelectionSystem::EComponentMode mode) const
2822 case SelectionSystem::eVertex:
2823 return selectedVertices();
2824 case SelectionSystem::eEdge:
2825 return selectedEdges();
2826 case SelectionSystem::eFace:
2827 return isSelected();
2832 void setSelected(SelectionSystem::EComponentMode mode, bool select)
2836 case SelectionSystem::eFace:
2837 m_selectable.setSelected(select);
2839 case SelectionSystem::eVertex:
2840 ASSERT_MESSAGE(!select, "select-all not supported");
2842 m_vertexSelection.clear();
2843 m_selectableVertices.setSelected(false);
2845 case SelectionSystem::eEdge:
2846 ASSERT_MESSAGE(!select, "select-all not supported");
2848 m_edgeSelection.clear();
2849 m_selectableEdges.setSelected(false);
2856 template<typename Functor>
2857 void SelectedVertices_foreach(Functor functor) const
2859 for(VertexSelection::const_iterator i = m_vertexSelection.begin(); i != m_vertexSelection.end(); ++i)
2861 std::size_t index = Winding_FindAdjacent(getFace().getWinding(), *i);
2862 if(index != c_brush_maxFaces)
2864 functor(getFace().getWinding()[index].vertex);
2868 template<typename Functor>
2869 void SelectedEdges_foreach(Functor functor) const
2871 for(VertexSelection::const_iterator i = m_edgeSelection.begin(); i != m_edgeSelection.end(); ++i)
2873 std::size_t index = Winding_FindAdjacent(getFace().getWinding(), *i);
2874 if(index != c_brush_maxFaces)
2876 const Winding& winding = getFace().getWinding();
2877 std::size_t adjacent = Winding_next(winding, index);
2878 functor(vector3_mid(winding[index].vertex, winding[adjacent].vertex));
2882 template<typename Functor>
2883 void SelectedFaces_foreach(Functor functor) const
2887 functor(centroid());
2891 template<typename Functor>
2892 void SelectedComponents_foreach(Functor functor) const
2894 SelectedVertices_foreach(functor);
2895 SelectedEdges_foreach(functor);
2896 SelectedFaces_foreach(functor);
2899 void iterate_selected(AABB& aabb) const
2901 SelectedComponents_foreach(AABBExtendByPoint(aabb));
2904 class RenderablePointVectorPushBack
2906 RenderablePointVector& m_points;
2908 RenderablePointVectorPushBack(RenderablePointVector& points) : m_points(points)
2911 void operator()(const Vector3& point) const
2913 const Colour4b colour_selected(0, 0, 255, 255);
2914 m_points.push_back(pointvertex_for_windingpoint(point, colour_selected));
2918 void iterate_selected(RenderablePointVector& points) const
2920 SelectedComponents_foreach(RenderablePointVectorPushBack(points));
2923 bool intersectVolume(const VolumeTest& volume, const Matrix4& localToWorld) const
2925 return m_face->intersectVolume(volume, localToWorld);
2928 void render(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld) const
2930 if(!m_face->isFiltered() && m_face->contributes() && intersectVolume(volume, localToWorld))
2932 renderer.PushState();
2933 if(selectedComponents())
2935 renderer.Highlight(Renderer::eFace);
2937 m_face->render(renderer, localToWorld);
2938 renderer.PopState();
2942 void testSelect(SelectionTest& test, SelectionIntersection& best)
2944 if(!m_face->isFiltered())
2946 m_face->testSelect(test, best);
2949 void testSelect(Selector& selector, SelectionTest& test)
2951 SelectionIntersection best;
2952 testSelect(test, best);
2955 Selector_add(selector, m_selectable, best);
2958 void testSelect_centroid(Selector& selector, SelectionTest& test)
2960 if(m_face->contributes() && !m_face->isFiltered())
2962 SelectionIntersection best;
2963 m_face->testSelect_centroid(test, best);
2966 Selector_add(selector, m_selectable, best);
2971 void selectPlane(Selector& selector, const Line& line, PlanesIterator first, PlanesIterator last, const PlaneCallback& selectedPlaneCallback)
2973 for(Winding::const_iterator i = getFace().getWinding().begin(); i != getFace().getWinding().end(); ++i)
2975 Vector3 v(vector3_subtracted(line_closest_point(line, (*i).vertex), (*i).vertex));
2976 double dot = vector3_dot(getFace().plane3().normal(), v);
2983 Selector_add(selector, m_selectable);
2985 selectedPlaneCallback(getFace().plane3());
2987 void selectReversedPlane(Selector& selector, const SelectedPlanes& selectedPlanes)
2989 if(selectedPlanes.contains(plane3_flipped(getFace().plane3())))
2991 Selector_add(selector, m_selectable);
2995 void transformComponents(const Matrix4& matrix)
2999 m_face->transform(matrix, false);
3001 if(selectedVertices())
3003 if(m_vertexSelection.size() == 1)
3005 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[1]);
3006 m_face->assign_planepts(m_face->m_move_planeptsTransformed);
3008 else if(m_vertexSelection.size() == 2)
3010 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[1]);
3011 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[2]);
3012 m_face->assign_planepts(m_face->m_move_planeptsTransformed);
3014 else if(m_vertexSelection.size() >= 3)
3016 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[0]);
3017 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[1]);
3018 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[2]);
3019 m_face->assign_planepts(m_face->m_move_planeptsTransformed);
3024 if(m_edgeSelection.size() == 1)
3026 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[0]);
3027 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[1]);
3028 m_face->assign_planepts(m_face->m_move_planeptsTransformed);
3030 else if(m_edgeSelection.size() >= 2)
3032 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[0]);
3033 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[1]);
3034 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[2]);
3035 m_face->assign_planepts(m_face->m_move_planeptsTransformed);
3040 void snapto(float snap)
3042 m_face->snapto(snap);
3045 void snapComponents(float snap)
3051 if(selectedVertices())
3053 vector3_snap(m_face->m_move_planepts[0], snap);
3054 vector3_snap(m_face->m_move_planepts[1], snap);
3055 vector3_snap(m_face->m_move_planepts[2], snap);
3056 m_face->assign_planepts(m_face->m_move_planepts);
3057 planepts_assign(m_face->m_move_planeptsTransformed, m_face->m_move_planepts);
3058 m_face->freezeTransform();
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();
3070 void update_move_planepts_vertex(std::size_t index)
3072 m_face->update_move_planepts_vertex(index, m_face->m_move_planepts);
3074 void update_move_planepts_vertex2(std::size_t index, std::size_t other)
3076 const std::size_t numpoints = m_face->getWinding().numpoints;
3077 ASSERT_MESSAGE(index < numpoints, "select_vertex: invalid index");
3079 const std::size_t opposite = Winding_Opposite(m_face->getWinding(), index, other);
3081 if(triangle_reversed(index, other, opposite))
3083 std::swap(index, other);
3087 triangles_same_winding(
3088 m_face->getWinding()[opposite].vertex,
3089 m_face->getWinding()[index].vertex,
3090 m_face->getWinding()[other].vertex,
3091 m_face->getWinding()[0].vertex,
3092 m_face->getWinding()[1].vertex,
3093 m_face->getWinding()[2].vertex
3095 "update_move_planepts_vertex2: error"
3098 m_face->m_move_planepts[0] = m_face->getWinding()[opposite].vertex;
3099 m_face->m_move_planepts[1] = m_face->getWinding()[index].vertex;
3100 m_face->m_move_planepts[2] = m_face->getWinding()[other].vertex;
3101 planepts_quantise(m_face->m_move_planepts, GRID_MIN); // winding points are very inaccurate
3103 void update_selection_vertex()
3105 if(m_vertexSelection.size() == 0)
3107 m_selectableVertices.setSelected(false);
3111 m_selectableVertices.setSelected(true);
3113 if(m_vertexSelection.size() == 1)
3115 std::size_t index = Winding_FindAdjacent(getFace().getWinding(), *m_vertexSelection.begin());
3117 if(index != c_brush_maxFaces)
3119 update_move_planepts_vertex(index);
3122 else if(m_vertexSelection.size() == 2)
3124 std::size_t index = Winding_FindAdjacent(getFace().getWinding(), *m_vertexSelection.begin());
3125 std::size_t other = Winding_FindAdjacent(getFace().getWinding(), *(++m_vertexSelection.begin()));
3127 if(index != c_brush_maxFaces
3128 && other != c_brush_maxFaces)
3130 update_move_planepts_vertex2(index, other);
3135 void select_vertex(std::size_t index, bool select)
3139 VertexSelection_insert(m_vertexSelection, getFace().getWinding()[index].adjacent);
3143 VertexSelection_erase(m_vertexSelection, getFace().getWinding()[index].adjacent);
3146 SceneChangeNotify();
3147 update_selection_vertex();
3150 bool selected_vertex(std::size_t index) const
3152 return VertexSelection_find(m_vertexSelection, getFace().getWinding()[index].adjacent) != m_vertexSelection.end();
3155 void update_move_planepts_edge(std::size_t index)
3157 std::size_t numpoints = m_face->getWinding().numpoints;
3158 ASSERT_MESSAGE(index < numpoints, "select_edge: invalid index");
3160 std::size_t adjacent = Winding_next(m_face->getWinding(), index);
3161 std::size_t opposite = Winding_Opposite(m_face->getWinding(), index);
3162 m_face->m_move_planepts[0] = m_face->getWinding()[index].vertex;
3163 m_face->m_move_planepts[1] = m_face->getWinding()[adjacent].vertex;
3164 m_face->m_move_planepts[2] = m_face->getWinding()[opposite].vertex;
3165 planepts_quantise(m_face->m_move_planepts, GRID_MIN); // winding points are very inaccurate
3167 void update_selection_edge()
3169 if(m_edgeSelection.size() == 0)
3171 m_selectableEdges.setSelected(false);
3175 m_selectableEdges.setSelected(true);
3177 if(m_edgeSelection.size() == 1)
3179 std::size_t index = Winding_FindAdjacent(getFace().getWinding(), *m_edgeSelection.begin());
3181 if(index != c_brush_maxFaces)
3183 update_move_planepts_edge(index);
3188 void select_edge(std::size_t index, bool select)
3192 VertexSelection_insert(m_edgeSelection, getFace().getWinding()[index].adjacent);
3196 VertexSelection_erase(m_edgeSelection, getFace().getWinding()[index].adjacent);
3199 SceneChangeNotify();
3200 update_selection_edge();
3203 bool selected_edge(std::size_t index) const
3205 return VertexSelection_find(m_edgeSelection, getFace().getWinding()[index].adjacent) != m_edgeSelection.end();
3208 const Vector3& centroid() const
3210 return m_face->centroid();
3213 void connectivityChanged()
3215 // This occurs when a face is added or removed.
3216 // The current vertex and edge selections no longer valid and must be cleared.
3217 m_vertexSelection.clear();
3218 m_selectableVertices.setSelected(false);
3219 m_edgeSelection.clear();
3220 m_selectableEdges.setSelected(false);
3224 class BrushClipPlane : public OpenGLRenderable
3228 static Shader* m_state;
3230 static void constructStatic()
3232 m_state = GlobalShaderCache().capture("$CLIPPER_OVERLAY");
3234 static void destroyStatic()
3236 GlobalShaderCache().release("$CLIPPER_OVERLAY");
3239 void setPlane(const Brush& brush, const Plane3& plane)
3242 if(plane3_valid(m_plane))
3244 brush.windingForClipPlane(m_winding, m_plane);
3248 m_winding.resize(0);
3252 void render(RenderStateFlags state) const
3254 if((state & RENDER_FILL) != 0)
3256 Winding_Draw(m_winding, m_plane.normal(), state);
3260 Winding_DrawWireframe(m_winding);
3264 void render(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld) const
3266 renderer.SetState(m_state, Renderer::eWireframeOnly);
3267 renderer.SetState(m_state, Renderer::eFullMaterials);
3268 renderer.addRenderable(*this, localToWorld);
3272 inline void Face_addLight(const FaceInstance& face, const Matrix4& localToWorld, const RendererLight& light)
3274 const Plane3& facePlane = face.getFace().plane3();
3275 const Vector3& origin = light.aabb().origin;
3276 Plane3 tmp(plane3_transformed(Plane3(facePlane.normal(), -facePlane.dist()), localToWorld));
3277 if(!plane3_test_point(tmp, origin)
3278 || !plane3_test_point(tmp, vector3_added(origin, light.offset())))
3280 face.m_lights.addLight(light);
3286 typedef std::vector<FaceInstance> FaceInstances;
3288 class EdgeInstance : public Selectable
3290 FaceInstances& m_faceInstances;
3291 SelectableEdge* m_edge;
3293 void select_edge(bool select)
3295 FaceVertexId faceVertex = m_edge->m_faceVertex;
3296 m_faceInstances[faceVertex.getFace()].select_edge(faceVertex.getVertex(), select);
3297 faceVertex = next_edge(m_edge->m_faces, faceVertex);
3298 m_faceInstances[faceVertex.getFace()].select_edge(faceVertex.getVertex(), select);
3300 bool selected_edge() const
3302 FaceVertexId faceVertex = m_edge->m_faceVertex;
3303 if(!m_faceInstances[faceVertex.getFace()].selected_edge(faceVertex.getVertex()))
3307 faceVertex = next_edge(m_edge->m_faces, faceVertex);
3308 if(!m_faceInstances[faceVertex.getFace()].selected_edge(faceVertex.getVertex()))
3317 EdgeInstance(FaceInstances& faceInstances, SelectableEdge& edge)
3318 : m_faceInstances(faceInstances), m_edge(&edge)
3321 EdgeInstance& operator=(const EdgeInstance& other)
3323 m_edge = other.m_edge;
3327 void setSelected(bool select)
3329 select_edge(select);
3331 bool isSelected() const
3333 return selected_edge();
3337 void testSelect(Selector& selector, SelectionTest& test)
3339 SelectionIntersection best;
3340 m_edge->testSelect(test, best);
3343 Selector_add(selector, *this, best);
3348 class VertexInstance : public Selectable
3350 FaceInstances& m_faceInstances;
3351 SelectableVertex* m_vertex;
3353 void select_vertex(bool select)
3355 FaceVertexId faceVertex = m_vertex->m_faceVertex;
3358 m_faceInstances[faceVertex.getFace()].select_vertex(faceVertex.getVertex(), select);
3359 faceVertex = next_vertex(m_vertex->m_faces, faceVertex);
3361 while(faceVertex.getFace() != m_vertex->m_faceVertex.getFace());
3363 bool selected_vertex() const
3365 FaceVertexId faceVertex = m_vertex->m_faceVertex;
3368 if(!m_faceInstances[faceVertex.getFace()].selected_vertex(faceVertex.getVertex()))
3372 faceVertex = next_vertex(m_vertex->m_faces, faceVertex);
3374 while(faceVertex.getFace() != m_vertex->m_faceVertex.getFace());
3379 VertexInstance(FaceInstances& faceInstances, SelectableVertex& vertex)
3380 : m_faceInstances(faceInstances), m_vertex(&vertex)
3383 VertexInstance& operator=(const VertexInstance& other)
3385 m_vertex = other.m_vertex;
3389 void setSelected(bool select)
3391 select_vertex(select);
3393 bool isSelected() const
3395 return selected_vertex();
3398 void testSelect(Selector& selector, SelectionTest& test)
3400 SelectionIntersection best;
3401 m_vertex->testSelect(test, best);
3404 Selector_add(selector, *this, best);
3409 class BrushInstanceVisitor
3412 virtual void visit(FaceInstance& face) const = 0;
3415 class BrushInstance :
3416 public BrushObserver,
3417 public scene::Instance,
3420 public SelectionTestable,
3421 public ComponentSelectionTestable,
3422 public ComponentEditable,
3423 public ComponentSnappable,
3424 public PlaneSelectable,
3425 public LightCullable
3429 InstanceTypeCastTable m_casts;
3433 InstanceStaticCast<BrushInstance, Selectable>::install(m_casts);
3434 InstanceContainedCast<BrushInstance, Bounded>::install(m_casts);
3435 InstanceContainedCast<BrushInstance, Cullable>::install(m_casts);
3436 InstanceStaticCast<BrushInstance, Renderable>::install(m_casts);
3437 InstanceStaticCast<BrushInstance, SelectionTestable>::install(m_casts);
3438 InstanceStaticCast<BrushInstance, ComponentSelectionTestable>::install(m_casts);
3439 InstanceStaticCast<BrushInstance, ComponentEditable>::install(m_casts);
3440 InstanceStaticCast<BrushInstance, ComponentSnappable>::install(m_casts);
3441 InstanceStaticCast<BrushInstance, PlaneSelectable>::install(m_casts);
3442 InstanceIdentityCast<BrushInstance>::install(m_casts);
3443 InstanceContainedCast<BrushInstance, Transformable>::install(m_casts);
3445 InstanceTypeCastTable& get()
3454 FaceInstances m_faceInstances;
3456 typedef std::vector<EdgeInstance> EdgeInstances;
3457 EdgeInstances m_edgeInstances;
3458 typedef std::vector<VertexInstance> VertexInstances;
3459 VertexInstances m_vertexInstances;
3461 ObservedSelectable m_selectable;
3463 mutable RenderableWireframe m_render_wireframe;
3464 mutable RenderablePointVector m_render_selected;
3465 mutable AABB m_aabb_component;
3466 mutable Array<PointVertex> m_faceCentroidPointsCulled;
3467 RenderablePointArray m_render_faces_wireframe;
3468 mutable bool m_viewChanged; // requires re-evaluation of view-dependent cached data
3470 BrushClipPlane m_clipPlane;
3472 static Shader* m_state_selpoint;
3474 const LightList* m_lightList;
3476 TransformModifier m_transform;
3478 BrushInstance(const BrushInstance& other); // NOT COPYABLE
3479 BrushInstance& operator=(const BrushInstance& other); // NOT ASSIGNABLE
3481 static Counter* m_counter;
3483 typedef LazyStatic<TypeCasts> StaticTypeCasts;
3485 void lightsChanged()
3487 m_lightList->lightsChanged();
3489 typedef MemberCaller<BrushInstance, &BrushInstance::lightsChanged> LightsChangedCaller;
3491 STRING_CONSTANT(Name, "BrushInstance");
3493 BrushInstance(const scene::Path& path, scene::Instance* parent, Brush& brush) :
3494 Instance(path, parent, this, StaticTypeCasts::instance().get()),
3496 m_selectable(SelectedChangedCaller(*this)),
3497 m_render_selected(GL_POINTS),
3498 m_render_faces_wireframe(m_faceCentroidPointsCulled, GL_POINTS),
3499 m_viewChanged(false),
3500 m_transform(Brush::TransformChangedCaller(m_brush), ApplyTransformCaller(*this))
3502 m_brush.instanceAttach(Instance::path());
3503 m_brush.attach(*this);
3504 m_counter->increment();
3506 m_lightList = &GlobalShaderCache().attach(*this);
3507 m_brush.m_lightsChanged = LightsChangedCaller(*this); ///\todo Make this work with instancing.
3509 Instance::setTransformChangedCallback(LightsChangedCaller(*this));
3513 Instance::setTransformChangedCallback(Callback());
3515 m_brush.m_lightsChanged = Callback();
3516 GlobalShaderCache().detach(*this);
3518 m_counter->decrement();
3519 m_brush.detach(*this);
3520 m_brush.instanceDetach(Instance::path());
3527 const Brush& getBrush() const
3532 Bounded& get(NullType<Bounded>)
3536 Cullable& get(NullType<Cullable>)
3540 Transformable& get(NullType<Transformable>)
3545 void selectedChanged(const Selectable& selectable)
3547 GlobalSelectionSystem().getObserver(SelectionSystem::ePrimitive)(selectable);
3548 GlobalSelectionSystem().onSelectedChanged(*this, selectable);
3550 Instance::selectedChanged();
3552 typedef MemberCaller1<BrushInstance, const Selectable&, &BrushInstance::selectedChanged> SelectedChangedCaller;
3554 void selectedChangedComponent(const Selectable& selectable)
3556 GlobalSelectionSystem().getObserver(SelectionSystem::eComponent)(selectable);
3557 GlobalSelectionSystem().onComponentSelection(*this, selectable);
3559 typedef MemberCaller1<BrushInstance, const Selectable&, &BrushInstance::selectedChangedComponent> SelectedChangedComponentCaller;
3561 const BrushInstanceVisitor& forEachFaceInstance(const BrushInstanceVisitor& visitor)
3563 for(FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3570 static void constructStatic()
3572 m_state_selpoint = GlobalShaderCache().capture("$SELPOINT");
3574 static void destroyStatic()
3576 GlobalShaderCache().release("$SELPOINT");
3581 m_faceInstances.clear();
3583 void reserve(std::size_t size)
3585 m_faceInstances.reserve(size);
3588 void push_back(Face& face)
3590 m_faceInstances.push_back(FaceInstance(face, SelectedChangedComponentCaller(*this)));
3594 ASSERT_MESSAGE(!m_faceInstances.empty(), "erasing invalid element");
3595 m_faceInstances.pop_back();
3597 void erase(std::size_t index)
3599 ASSERT_MESSAGE(index < m_faceInstances.size(), "erasing invalid element");
3600 m_faceInstances.erase(m_faceInstances.begin() + index);
3602 void connectivityChanged()
3604 for(FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3606 (*i).connectivityChanged();
3612 m_edgeInstances.clear();
3614 void edge_push_back(SelectableEdge& edge)
3616 m_edgeInstances.push_back(EdgeInstance(m_faceInstances, edge));
3621 m_vertexInstances.clear();
3623 void vertex_push_back(SelectableVertex& vertex)
3625 m_vertexInstances.push_back(VertexInstance(m_faceInstances, vertex));
3628 void DEBUG_verify() const
3630 ASSERT_MESSAGE(m_faceInstances.size() == m_brush.DEBUG_size(), "FATAL: mismatch");
3633 bool isSelected() const
3635 return m_selectable.isSelected();
3637 void setSelected(bool select)
3639 m_selectable.setSelected(select);
3642 void update_selected() const
3644 m_render_selected.clear();
3645 for(FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3647 if((*i).getFace().contributes())
3649 (*i).iterate_selected(m_render_selected);
3654 void evaluateViewDependent(const VolumeTest& volume, const Matrix4& localToWorld) const
3658 m_viewChanged = false;
3660 bool faces_visible[c_brush_maxFaces];
3662 bool* j = faces_visible;
3663 for(FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i, ++j)
3665 *j = (*i).intersectVolume(volume, localToWorld);
3669 m_brush.update_wireframe(m_render_wireframe, faces_visible);
3670 m_brush.update_faces_wireframe(m_faceCentroidPointsCulled, faces_visible);
3674 void renderComponentsSelected(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld) const
3676 m_brush.evaluateBRep();
3679 if(!m_render_selected.empty())
3681 renderer.Highlight(Renderer::ePrimitive, false);
3682 renderer.SetState(m_state_selpoint, Renderer::eWireframeOnly);
3683 renderer.SetState(m_state_selpoint, Renderer::eFullMaterials);
3684 renderer.addRenderable(m_render_selected, localToWorld);
3688 void renderComponents(Renderer& renderer, const VolumeTest& volume) const
3690 m_brush.evaluateBRep();
3692 const Matrix4& localToWorld = Instance::localToWorld();
3694 renderer.SetState(m_brush.m_state_point, Renderer::eWireframeOnly);
3695 renderer.SetState(m_brush.m_state_point, Renderer::eFullMaterials);
3697 if(volume.fill() && GlobalSelectionSystem().ComponentMode() == SelectionSystem::eFace)
3699 evaluateViewDependent(volume, localToWorld);
3700 renderer.addRenderable(m_render_faces_wireframe, localToWorld);
3704 m_brush.renderComponents(GlobalSelectionSystem().ComponentMode(), renderer, volume, localToWorld);
3708 void renderClipPlane(Renderer& renderer, const VolumeTest& volume) const
3710 if(GlobalSelectionSystem().ManipulatorMode() == SelectionSystem::eClip && isSelected())
3712 m_clipPlane.render(renderer, volume, localToWorld());
3716 void renderCommon(Renderer& renderer, const VolumeTest& volume) const
3718 bool componentMode = GlobalSelectionSystem().Mode() == SelectionSystem::eComponent;
3720 if(componentMode && isSelected())
3722 renderComponents(renderer, volume);
3725 if(parentSelected())
3729 renderer.Highlight(Renderer::eFace);
3731 renderer.Highlight(Renderer::ePrimitive);
3735 void renderSolid(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld) const
3737 //renderCommon(renderer, volume);
3739 m_lightList->evaluateLights();
3741 for(FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3743 renderer.setLights((*i).m_lights);
3744 (*i).render(renderer, volume, localToWorld);
3747 renderComponentsSelected(renderer, volume, localToWorld);
3750 void renderWireframe(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld) const
3752 //renderCommon(renderer, volume);
3754 evaluateViewDependent(volume, localToWorld);
3756 if(m_render_wireframe.m_size != 0)
3758 renderer.addRenderable(m_render_wireframe, localToWorld);
3761 renderComponentsSelected(renderer, volume, localToWorld);
3764 void renderSolid(Renderer& renderer, const VolumeTest& volume) const
3766 m_brush.evaluateBRep();
3768 renderClipPlane(renderer, volume);
3770 renderSolid(renderer, volume, localToWorld());
3773 void renderWireframe(Renderer& renderer, const VolumeTest& volume) const
3775 m_brush.evaluateBRep();
3777 renderClipPlane(renderer, volume);
3779 renderWireframe(renderer, volume, localToWorld());
3782 void viewChanged() const
3784 m_viewChanged = true;
3787 void testSelect(Selector& selector, SelectionTest& test)
3789 test.BeginMesh(localToWorld());
3791 SelectionIntersection best;
3792 for(FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3794 (*i).testSelect(test, best);
3798 selector.addIntersection(best);
3802 bool isSelectedComponents() const
3804 for(FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3806 if((*i).selectedComponents())
3813 void setSelectedComponents(bool select, SelectionSystem::EComponentMode mode)
3815 for(FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3817 (*i).setSelected(mode, select);
3820 void testSelectComponents(Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode)
3822 test.BeginMesh(localToWorld());
3826 case SelectionSystem::eVertex:
3828 for(VertexInstances::iterator i = m_vertexInstances.begin(); i != m_vertexInstances.end(); ++i)
3830 (*i).testSelect(selector, test);
3834 case SelectionSystem::eEdge:
3836 for(EdgeInstances::iterator i = m_edgeInstances.begin(); i != m_edgeInstances.end(); ++i)
3838 (*i).testSelect(selector, test);
3842 case SelectionSystem::eFace:
3844 if(test.getVolume().fill())
3846 for(FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3848 (*i).testSelect(selector, test);
3853 for(FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3855 (*i).testSelect_centroid(selector, test);
3865 void selectPlanes(Selector& selector, SelectionTest& test, const PlaneCallback& selectedPlaneCallback)
3867 test.BeginMesh(localToWorld());
3869 PlanePointer brushPlanes[c_brush_maxFaces];
3870 PlanesIterator j = brushPlanes;
3872 for(Brush::const_iterator i = m_brush.begin(); i != m_brush.end(); ++i)
3874 *j++ = &(*i)->plane3();
3877 for(FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3879 (*i).selectPlane(selector, Line(test.getNear(), test.getFar()), brushPlanes, j, selectedPlaneCallback);
3882 void selectReversedPlanes(Selector& selector, const SelectedPlanes& selectedPlanes)
3884 for(FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3886 (*i).selectReversedPlane(selector, selectedPlanes);
3891 void transformComponents(const Matrix4& matrix)
3893 for(FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3895 (*i).transformComponents(matrix);
3898 const AABB& getSelectedComponentsBounds() const
3900 m_aabb_component = AABB();
3902 for(FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3904 (*i).iterate_selected(m_aabb_component);
3907 return m_aabb_component;
3910 void snapComponents(float snap)
3912 for(FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3914 (*i).snapComponents(snap);
3917 void evaluateTransform()
3919 Matrix4 matrix(m_transform.calculateTransform());
3920 //globalOutputStream() << "matrix: " << matrix << "\n";
3922 if(m_transform.getType() == TRANSFORM_PRIMITIVE)
3924 m_brush.transform(matrix);
3928 transformComponents(matrix);
3931 void applyTransform()
3933 m_brush.revertTransform();
3934 evaluateTransform();
3935 m_brush.freezeTransform();
3937 typedef MemberCaller<BrushInstance, &BrushInstance::applyTransform> ApplyTransformCaller;
3939 void setClipPlane(const Plane3& plane)
3941 m_clipPlane.setPlane(m_brush, plane);
3944 bool testLight(const RendererLight& light) const
3946 return light.testAABB(worldAABB());
3948 void insertLight(const RendererLight& light)
3950 const Matrix4& localToWorld = Instance::localToWorld();
3951 for(FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3953 Face_addLight(*i, localToWorld, light);
3958 for(FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i)
3960 (*i).m_lights.clear();
3965 inline BrushInstance* Instance_getBrush(scene::Instance& instance)
3967 return InstanceTypeCast<BrushInstance>::cast(instance);
3971 template<typename Functor>
3972 class BrushSelectedVisitor : public SelectionSystem::Visitor
3974 const Functor& m_functor;
3976 BrushSelectedVisitor(const Functor& functor) : m_functor(functor)
3979 void visit(scene::Instance& instance) const
3981 BrushInstance* brush = Instance_getBrush(instance);
3989 template<typename Functor>
3990 inline const Functor& Scene_forEachSelectedBrush(const Functor& functor)
3992 GlobalSelectionSystem().foreachSelected(BrushSelectedVisitor<Functor>(functor));
3996 template<typename Functor>
3997 class BrushVisibleSelectedVisitor : public SelectionSystem::Visitor
3999 const Functor& m_functor;
4001 BrushVisibleSelectedVisitor(const Functor& functor) : m_functor(functor)
4004 void visit(scene::Instance& instance) const
4006 BrushInstance* brush = Instance_getBrush(instance);
4008 && instance.path().top().get().visible())
4015 template<typename Functor>
4016 inline const Functor& Scene_forEachVisibleSelectedBrush(const Functor& functor)
4018 GlobalSelectionSystem().foreachSelected(BrushVisibleSelectedVisitor<Functor>(functor));
4022 class BrushForEachFace
4024 const BrushInstanceVisitor& m_visitor;
4026 BrushForEachFace(const BrushInstanceVisitor& visitor) : m_visitor(visitor)
4029 void operator()(BrushInstance& brush) const
4031 brush.forEachFaceInstance(m_visitor);
4035 template<class Functor>
4036 class FaceInstanceVisitFace : public BrushInstanceVisitor
4038 const Functor& functor;
4040 FaceInstanceVisitFace(const Functor& functor)
4044 void visit(FaceInstance& face) const
4046 functor(face.getFace());
4050 template<typename Functor>
4051 inline const Functor& Brush_forEachFace(BrushInstance& brush, const Functor& functor)
4053 brush.forEachFaceInstance(FaceInstanceVisitFace<Functor>(functor));
4057 template<class Functor>
4058 class FaceVisitAll : public BrushVisitor
4060 const Functor& functor;
4062 FaceVisitAll(const Functor& functor)
4066 void visit(Face& face) const
4072 template<typename Functor>
4073 inline const Functor& Brush_forEachFace(const Brush& brush, const Functor& functor)
4075 brush.forEachFace(FaceVisitAll<Functor>(functor));
4079 template<typename Functor>
4080 inline const Functor& Brush_forEachFace(Brush& brush, const Functor& functor)
4082 brush.forEachFace(FaceVisitAll<Functor>(functor));
4086 template<class Functor>
4087 class FaceInstanceVisitAll : public BrushInstanceVisitor
4089 const Functor& functor;
4091 FaceInstanceVisitAll(const Functor& functor)
4095 void visit(FaceInstance& face) const
4101 template<typename Functor>
4102 inline const Functor& Brush_ForEachFaceInstance(BrushInstance& brush, const Functor& functor)
4104 brush.forEachFaceInstance(FaceInstanceVisitAll<Functor>(functor)));
4108 template<typename Functor>
4109 inline const Functor& Scene_forEachBrush(scene::Graph& graph, const Functor& functor)
4111 graph.traverse(InstanceWalker< InstanceApply<BrushInstance, Functor> >(functor));
4115 template<typename Type, typename Functor>
4116 class InstanceIfVisible : public Functor
4119 InstanceIfVisible(const Functor& functor) : Functor(functor)
4122 void operator()(scene::Instance& instance)
4124 if(instance.path().top().get().visible())
4126 Functor::operator()(instance);
4131 template<typename Functor>
4132 class BrushVisibleWalker : public scene::Graph::Walker
4134 const Functor& m_functor;
4136 BrushVisibleWalker(const Functor& functor) : m_functor(functor)
4139 bool pre(const scene::Path& path, scene::Instance& instance) const
4141 if(path.top().get().visible())
4143 BrushInstance* brush = Instance_getIBrush(instance);
4153 template<typename Functor>
4154 inline const Functor& Scene_forEachVisibleBrush(scene::Graph& graph, const Functor& functor)
4156 graph.traverse(BrushVisibleWalker<Functor>(functor));
4160 template<typename Functor>
4161 inline const Functor& Scene_ForEachBrush_ForEachFace(scene::Graph& graph, const Functor& functor)
4163 Scene_forEachBrush(graph, BrushForEachFace(FaceInstanceVisitFace<Functor>(functor)));
4167 template<typename Functor>
4168 inline const Functor& Scene_ForEachSelectedBrush_ForEachFace(scene::Graph& graph, const Functor& functor)
4170 Scene_forEachSelectedBrush(BrushForEachFace(FaceInstanceVisitFace<Functor>(functor)));
4174 template<typename Functor>
4175 inline const Functor& Scene_ForEachSelectedBrush_ForEachFaceInstance(scene::Graph& graph, const Functor& functor)
4177 Scene_forEachSelectedBrush(BrushForEachFace(FaceInstanceVisitAll<Functor>(functor)));
4181 template<typename Functor>
4182 class FaceVisitorWrapper
4184 const Functor& functor;
4186 FaceVisitorWrapper(const Functor& functor) : functor(functor)
4190 void operator()(FaceInstance& faceInstance) const
4192 functor(faceInstance.getFace());
4196 template<typename Functor>
4197 inline const Functor& Scene_ForEachSelectedBrushFace(scene::Graph& graph, const Functor& functor)
4199 g_SelectedFaceInstances.foreach(FaceVisitorWrapper<Functor>(functor));