Panda3D

eggVertexPool.h

00001 // Filename: eggVertexPool.h
00002 // Created by:  drose (16Jan99)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #ifndef EGGVERTEXPOOL_H
00016 #define EGGVERTEXPOOL_H
00017 
00018 #include "pandabase.h"
00019 
00020 #include "eggVertex.h"
00021 #include "eggNode.h"
00022 #include "pt_EggVertex.h"
00023 
00024 #include "pointerTo.h"
00025 #include "pset.h"
00026 #include "pvector.h"
00027 #include "pmap.h"
00028 #include "lmatrix.h"
00029 #include "iterator_types.h"
00030 
00031 ////////////////////////////////////////////////////////////////////
00032 //       Class : EggVertexPool
00033 // Description : A collection of vertices.  There may be any number of
00034 //               vertex pools in a single egg structure.  The vertices
00035 //               in a single pool need not necessarily have any
00036 //               connection to each other, but it is necessary that
00037 //               any one primitive (e.g. a polygon) must pull all its
00038 //               vertices from the same pool.
00039 //
00040 //               An EggVertexPool is an STL-style container of
00041 //               pointers to EggVertex's.  Functions add_vertex() and
00042 //               remove_vertex() are provided to manipulate the list.
00043 //               The list may also be operated on (read-only) via
00044 //               iterators and begin()/end().
00045 ////////////////////////////////////////////////////////////////////
00046 class EXPCL_PANDAEGG EggVertexPool : public EggNode {
00047 
00048   // This is a bit of private interface stuff that must be here as a
00049   // forward reference.  This allows us to define the EggVertexPool as
00050   // an STL container.
00051 
00052 private:
00053   // IndexVertices is the main storage mechanism of the vertex pool.
00054   // It stores a reference-counting pointer to each vertex, ordered by
00055   // vertex index number.
00056   typedef pmap<int, PT_EggVertex> IndexVertices;
00057 
00058   // UniqueVertices is an auxiliary indexing mechanism.  It stores the
00059   // same vertex pointers as IndexVertices (although these pointers
00060   // are not reference-counted), this time ordered by vertex
00061   // properties.  This makes it easy to determine when one or more
00062   // vertices already exist in the pool with identical properties.
00063   typedef pmultiset<EggVertex *, UniqueEggVertices> UniqueVertices;
00064 
00065 public:
00066   typedef second_of_pair_iterator<IndexVertices::const_iterator> iterator;
00067   typedef iterator const_iterator;
00068   typedef IndexVertices::size_type size_type;
00069 
00070   // Here begins the actual public interface to EggVertexPool.
00071 
00072 PUBLISHED:
00073   EggVertexPool(const string &name);
00074   EggVertexPool(const EggVertexPool &copy);
00075   ~EggVertexPool();
00076 
00077   INLINE bool has_vertex(int index) const;
00078 
00079   bool has_forward_vertices() const;
00080   bool has_defined_vertices() const;
00081 
00082   // Returns NULL if there is no such vertex.
00083   EggVertex *get_vertex(int index) const;
00084   INLINE EggVertex *operator [](int index) const;
00085 
00086   // Returns a forward reference if there is no such vertex.
00087   EggVertex *get_forward_vertex(int index);
00088 
00089   // Returns 0 if the pool is empty.
00090   int get_highest_index() const;
00091   void set_highest_index(int highest_index);
00092 
00093   int get_num_dimensions() const;
00094   bool has_normals() const;
00095   bool has_colors() const;
00096   bool has_nonwhite_colors() const;
00097   void check_overall_color(bool &has_overall_color, Colorf &overall_color) const;
00098   bool has_uvs() const;
00099   void get_uv_names(vector_string &uv_names, vector_string &uvw_names, 
00100                     vector_string &tbn_names) const;
00101 
00102 public:
00103   // Can be used to traverse all the vertices in index number order.
00104   iterator begin() const;
00105   iterator end() const;
00106   bool empty() const;
00107 
00108 PUBLISHED:
00109   size_type size() const;
00110 
00111   // add_vertex() adds a freshly-allocated vertex.  It is up to the
00112   // user to allocate the vertex.
00113   EggVertex *add_vertex(EggVertex *vertex, int index = -1);
00114 
00115   // make_new_vertex() allocates and returns a new vertex from the
00116   // pool.
00117   INLINE EggVertex *make_new_vertex();
00118   INLINE EggVertex *make_new_vertex(double pos);
00119   INLINE EggVertex *make_new_vertex(const LPoint2d &pos);
00120   INLINE EggVertex *make_new_vertex(const LPoint3d &pos);
00121   INLINE EggVertex *make_new_vertex(const LPoint4d &pos);
00122 
00123   // create_unique_vertex() creates a new vertex if there is not
00124   // already one identical to the indicated vertex, or returns the
00125   // existing one if there is.
00126   EggVertex *create_unique_vertex(const EggVertex &copy);
00127   EggVertex *find_matching_vertex(const EggVertex &copy);
00128 
00129   void remove_vertex(EggVertex *vertex);
00130   int remove_unused_vertices();
00131 
00132   void transform(const LMatrix4d &mat);
00133 
00134   void write(ostream &out, int indent_level) const;
00135 
00136 protected:
00137   virtual void r_transform(const LMatrix4d &mat, const LMatrix4d &inv,
00138                            CoordinateSystem to_cs);
00139   virtual void r_transform_vertices(const LMatrix4d &mat);
00140 
00141 private:
00142   UniqueVertices _unique_vertices;
00143   IndexVertices _index_vertices;
00144   int _highest_index;
00145 
00146 
00147 public:
00148 
00149   static TypeHandle get_class_type() {
00150     return _type_handle;
00151   }
00152   static void init_type() {
00153     EggNode::init_type();
00154     register_type(_type_handle, "EggVertexPool",
00155                   EggNode::get_class_type());
00156   }
00157   virtual TypeHandle get_type() const {
00158     return get_class_type();
00159   }
00160   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00161 
00162 private:
00163   static TypeHandle _type_handle;
00164 
00165 friend class EggVertex;
00166 };
00167 
00168 typedef pvector< PT(EggVertexPool) > EggVertexPools;
00169 
00170 #include "eggVertexPool.I"
00171 
00172 #endif
 All Classes Functions Variables Enumerations