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