/* Copyright 2004, 2005 Nicholas Bishop * * This file is part of SharpConstruct. * * SharpConstruct is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * SharpConstruct is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with SharpConstruct; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef MESH_H #define MESH_H #include "Color.h" #include "Math.hh" #include "Optimized.h" #include "Polygon3D.h" #include "Sculpt.hh" #include #include #include #include #include namespace SharpConstruct { namespace Interface { class ViewGL; } class Brush; class EditData; class ExternalRender; class RestoreOrderData { public: RestoreOrderData() : Enabled( false ) {} void Reset() { Vertices.clear(); Triangles.clear(); Quads.clear(); } bool Enabled; std::map< unsigned, unsigned > Vertices, Triangles, Quads; }; class VisibleElementData { public: VisibleElementData() : Enabled( false ), Vertices( 0 ), Triangles( 0 ), Quads( 0 ) {} bool Enabled; unsigned Vertices; unsigned Triangles; unsigned Quads; RestoreOrderData RestoreOrder; }; /** Stores and manipulates vertex and polygon data. * * Provides storage for vertex and polygon data, as well as data used to * display the mesh, and data relating the vertex and polygon data. Active data * is kept involving the area of the mesh currently being edited, and the * edit configuration. Functions are provided for file input/output, interactive * editing, deformations, and retrieval of mesh information. **/ class Mesh { friend class SharpConstruct::Interface::ViewGL; friend class SharpConstruct::ExternalRender; public: Mesh( Optimized::Point3DVector& locs, Optimized::Point3DVector& norms, std::vector< Color >& cols, Optimized::Point3DVector& tnorms, Optimized::Point3DVector& qnorms, std::vector< Triangle >& tris, std::vector< Quad >& quads, std::vector< PolygonUsers >& users, bool& saved, VisibleElementData& visel, std::string& filename ); ~Mesh() {} template< typename T > void Create( T creator ) { begin_creation_(); finalize_creation_( creator() ); } // Basic file input/output control void Save( std::string filename ); // Deformations void Move( float, float, float ); void Resize( float, float, float ); void Deflate( float v ); void Spherize( float v ); void Smooth( float ); void Flood( const Color&, const float ); void Subdivide( bool smooth ); void AdaptiveSubdivide(); void Skin( float v ); void Optimize( float ); int RemoveDuplicates(); void Mirror( bool, bool, bool ); void FlipNormals(); void Unify(); void ToTriangles(); void HideBox( Optimized::Point3DVector& hb ); static void ClearModifiedVertexIndices(); void RecalculateDamaged(); void RecalculateAllNormals(); void RecalculateModifiedNormals(); Optimized::Point3D CalculateAreaNormal( const std::vector< ActiveData >& ) const; void FindActiveVertices( std::vector< ActiveData >&, EditData& e ); Optimized::Point3D VertexNeighborLocAverage( unsigned v ) const; Color VertexNeighborClrAverage( unsigned v ) const; // Mesh information access void Clear(); const Optimized::Point3DVector& VertexLocations() const; Optimized::Point3DVector& VertexLocations(); const Optimized::Point3DVector& VertexNormals() const; Optimized::Point3DVector& VertexNormals(); const std::vector< Color >& VertexColors() const; std::vector< Color >& VertexColors(); //Optimized::Point3DVector& TriangleNormals(); //Optimized::Point3DVector& QuadNormals(); const std::vector< Triangle >& Triangles() const; std::vector< Triangle >& Triangles(); const std::vector< Quad >& Quads() const; std::vector< Quad >& Quads(); std::vector< PolygonUsers >& VertexUsers(); const Optimized::Point3DVector& ProjectedLocations() const; Optimized::Point3DVector& ProjectedLocations(); static Optimized::Point3D& CurrentSelection(); static void SetSelectionRadius( const float ); static float SelectionRadius(); //typedef std::list< unsigned > DamagedGroup; typedef std::vector< unsigned > DamagedGroup; static DamagedGroup& DamagedTriangles(); static DamagedGroup& DamagedQuads(); static Optimized::Normal3D& UpNormal(); static Optimized::Normal3D& RightNormal(); static Optimized::Normal3D& ZNormal(); const VisibleElementData& VisibleElements() const; VisibleElementData& VisibleElements(); typedef std::vector< unsigned > ModifiedGroup; static ModifiedGroup& ModifiedVertexIndices(); std::string FileName() const; void SetFileName( const std::string& ); std::string GetModelStatisticsString() const; EditData GetMirror( bool x, bool y, bool z ) const; Math::PolarPoint Relative2DLocation( const EditData& e, const Optimized::Point3D x, const float len ) const; Optimized::Point3D& Center(); float Diameter(); void CopyEssentials( Mesh & ); void ClearExtraData(); static Brush& CurrentBrush(); void PerformanceTest( std::string test ); void CalculateBoundingSphere(); private: void begin_creation_(); void finalize_creation_( const bool ); // File input/output void _save_obj( std::string ); void _save_wrl( std::string ); class ActiveVertexData { public: ActiveVertexData( unsigned c, std::set< unsigned >& chk, std::vector< unsigned >& indices, std::vector< float >& dists, Optimized::Point3D& a ) : Current( c ), Checked( chk ), VertexIndices( indices ), VertexDistances( dists ), Area( a ) {} unsigned Current; std::set< unsigned >& Checked; std::vector< unsigned >& VertexIndices; std::vector< float >& VertexDistances; Optimized::Point3D& Area; }; Optimized::Point3D _get_mirror_point( bool, bool, bool ); // Object data void _change_operation( const bool, const bool ); std::string& _filename; bool& _saved; unsigned _poly_count() const; void _clear_polys(); Optimized::Point3DVector& _vertex_locations; Optimized::Point3DVector& _vertex_normals; std::vector< SharpConstruct::Color >& _vertex_colors; std::vector< Triangle >& _triangles; std::vector< Quad >& _quads; // Stores the normals of each polygon Optimized::Point3DVector& _triangle_normals; Optimized::Point3DVector& _quad_normals; // Stores the polygons that use each vertex std::vector< PolygonUsers >& _vertex_users; static DamagedGroup _damaged_triangles; static DamagedGroup _damaged_quads; void _recalculate_vertex_users(); std::map< unsigned, unsigned > _triangle_users( unsigned v1, unsigned v2 ); std::map< unsigned, unsigned > _quad_users( unsigned v1, unsigned v2 ); std::set< unsigned > _connected_polygon_vertices( unsigned v ) const; std::set< unsigned > _connected_vertices( unsigned v ) const; Optimized::Point3D _polygon_center( Triangle& t ); Optimized::Point3D _polygon_center( Quad& q ); // Active data static float _selection_radius; static Optimized::Point3D _current_selection; static unsigned _selected_vertex[ 8 ]; static Optimized::Normal3D _view_up_normal; static Optimized::Normal3D _view_right_normal; static Optimized::Normal3D _view_z_normal; static ModifiedGroup _modified_vertex_indices; static Optimized::Point3DVector _selection_tube; static Optimized::Point3DVector _projected_locations; void _reset_visibles(); VisibleElementData& _visible_elements; // Bounding Sphere static float _diameter; static Optimized::Point3D _center; }; } #endif // MESH_H