Panda3D
 All Classes Functions Variables Enumerations
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, LColor &overall_color) const;
00098   bool has_uvs() const;
00099   bool has_aux() const;
00100   void get_uv_names(vector_string &uv_names, vector_string &uvw_names, 
00101                     vector_string &tbn_names) const;
00102   void get_aux_names(vector_string &aux_names) const;
00103 
00104 public:
00105   // Can be used to traverse all the vertices in index number order.
00106   iterator begin() const;
00107   iterator end() const;
00108   bool empty() const;
00109 
00110 PUBLISHED:
00111   size_type size() const;
00112 
00113   // add_vertex() adds a freshly-allocated vertex.  It is up to the
00114   // user to allocate the vertex.
00115   EggVertex *add_vertex(EggVertex *vertex, int index = -1);
00116 
00117   // make_new_vertex() allocates and returns a new vertex from the
00118   // pool.
00119   INLINE EggVertex *make_new_vertex();
00120   INLINE EggVertex *make_new_vertex(double pos);
00121   INLINE EggVertex *make_new_vertex(const LPoint2d &pos);
00122   INLINE EggVertex *make_new_vertex(const LPoint3d &pos);
00123   INLINE EggVertex *make_new_vertex(const LPoint4d &pos);
00124 
00125   // create_unique_vertex() creates a new vertex if there is not
00126   // already one identical to the indicated vertex, or returns the
00127   // existing one if there is.
00128   EggVertex *create_unique_vertex(const EggVertex &copy);
00129   EggVertex *find_matching_vertex(const EggVertex &copy);
00130 
00131   void remove_vertex(EggVertex *vertex);
00132   int remove_unused_vertices();
00133   void add_unused_vertices_to_prim(EggPrimitive *prim);
00134 
00135   void transform(const LMatrix4d &mat);
00136   void sort_by_external_index();
00137 
00138   void write(ostream &out, int indent_level) const;
00139 
00140 protected:
00141   virtual void r_transform(const LMatrix4d &mat, const LMatrix4d &inv,
00142                            CoordinateSystem to_cs);
00143   virtual void r_transform_vertices(const LMatrix4d &mat);
00144 
00145 private:
00146   UniqueVertices _unique_vertices;
00147   IndexVertices _index_vertices;
00148   int _highest_index;
00149 
00150 
00151 public:
00152 
00153   static TypeHandle get_class_type() {
00154     return _type_handle;
00155   }
00156   static void init_type() {
00157     EggNode::init_type();
00158     register_type(_type_handle, "EggVertexPool",
00159                   EggNode::get_class_type());
00160   }
00161   virtual TypeHandle get_type() const {
00162     return get_class_type();
00163   }
00164   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00165 
00166 private:
00167   static TypeHandle _type_handle;
00168 
00169 friend class EggVertex;
00170 };
00171 
00172 typedef pvector< PT(EggVertexPool) > EggVertexPools;
00173 
00174 #include "eggVertexPool.I"
00175 
00176 #endif
 All Classes Functions Variables Enumerations