Panda3D
eggVertexPool.h
1 // Filename: eggVertexPool.h
2 // Created by: drose (16Jan99)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef EGGVERTEXPOOL_H
16 #define EGGVERTEXPOOL_H
17 
18 #include "pandabase.h"
19 
20 #include "eggVertex.h"
21 #include "eggNode.h"
22 #include "pt_EggVertex.h"
23 
24 #include "pointerTo.h"
25 #include "pset.h"
26 #include "pvector.h"
27 #include "pmap.h"
28 #include "lmatrix.h"
29 #include "iterator_types.h"
30 
31 ////////////////////////////////////////////////////////////////////
32 // Class : EggVertexPool
33 // Description : A collection of vertices. There may be any number of
34 // vertex pools in a single egg structure. The vertices
35 // in a single pool need not necessarily have any
36 // connection to each other, but it is necessary that
37 // any one primitive (e.g. a polygon) must pull all its
38 // vertices from the same pool.
39 //
40 // An EggVertexPool is an STL-style container of
41 // pointers to EggVertex's. Functions add_vertex() and
42 // remove_vertex() are provided to manipulate the list.
43 // The list may also be operated on (read-only) via
44 // iterators and begin()/end().
45 ////////////////////////////////////////////////////////////////////
46 class EXPCL_PANDAEGG EggVertexPool : public EggNode {
47 
48  // This is a bit of private interface stuff that must be here as a
49  // forward reference. This allows us to define the EggVertexPool as
50  // an STL container.
51 
52 private:
53  // IndexVertices is the main storage mechanism of the vertex pool.
54  // It stores a reference-counting pointer to each vertex, ordered by
55  // vertex index number.
57 
58  // UniqueVertices is an auxiliary indexing mechanism. It stores the
59  // same vertex pointers as IndexVertices (although these pointers
60  // are not reference-counted), this time ordered by vertex
61  // properties. This makes it easy to determine when one or more
62  // vertices already exist in the pool with identical properties.
64 
65 public:
67  typedef iterator const_iterator;
68  typedef IndexVertices::size_type size_type;
69 
70  // Here begins the actual public interface to EggVertexPool.
71 
72 PUBLISHED:
73  EggVertexPool(const string &name);
74  EggVertexPool(const EggVertexPool &copy);
75  ~EggVertexPool();
76 
77  INLINE bool has_vertex(int index) const;
78 
79  bool has_forward_vertices() const;
80  bool has_defined_vertices() const;
81 
82  // Returns NULL if there is no such vertex.
83  EggVertex *get_vertex(int index) const;
84  INLINE EggVertex *operator [](int index) const;
85 
86  // Returns a forward reference if there is no such vertex.
87  EggVertex *get_forward_vertex(int index);
88 
89  // Returns 0 if the pool is empty.
90  int get_highest_index() const;
91  void set_highest_index(int highest_index);
92 
93  int get_num_dimensions() const;
94  bool has_normals() const;
95  bool has_colors() const;
96  bool has_nonwhite_colors() const;
97  void check_overall_color(bool &has_overall_color, LColor &overall_color) const;
98  bool has_uvs() const;
99  bool has_aux() const;
100  void get_uv_names(vector_string &uv_names, vector_string &uvw_names,
101  vector_string &tbn_names) const;
102  void get_aux_names(vector_string &aux_names) const;
103 
104 public:
105  // Can be used to traverse all the vertices in index number order.
106  iterator begin() const;
107  iterator end() const;
108  bool empty() const;
109 
110 PUBLISHED:
111  size_type size() const;
112 
113  // add_vertex() adds a freshly-allocated vertex. It is up to the
114  // user to allocate the vertex.
115  EggVertex *add_vertex(EggVertex *vertex, int index = -1);
116 
117  // make_new_vertex() allocates and returns a new vertex from the
118  // pool.
119  INLINE EggVertex *make_new_vertex();
120  INLINE EggVertex *make_new_vertex(double pos);
121  INLINE EggVertex *make_new_vertex(const LPoint2d &pos);
122  INLINE EggVertex *make_new_vertex(const LPoint3d &pos);
123  INLINE EggVertex *make_new_vertex(const LPoint4d &pos);
124 
125  // create_unique_vertex() creates a new vertex if there is not
126  // already one identical to the indicated vertex, or returns the
127  // existing one if there is.
128  EggVertex *create_unique_vertex(const EggVertex &copy);
129  EggVertex *find_matching_vertex(const EggVertex &copy);
130 
131  void remove_vertex(EggVertex *vertex);
132  int remove_unused_vertices();
133  void add_unused_vertices_to_prim(EggPrimitive *prim);
134 
135  void transform(const LMatrix4d &mat);
136  void sort_by_external_index();
137 
138  void write(ostream &out, int indent_level) const;
139 
140 protected:
141  virtual void r_transform(const LMatrix4d &mat, const LMatrix4d &inv,
142  CoordinateSystem to_cs);
143  virtual void r_transform_vertices(const LMatrix4d &mat);
144 
145 private:
146  UniqueVertices _unique_vertices;
147  IndexVertices _index_vertices;
148  int _highest_index;
149 
150 
151 public:
152 
153  static TypeHandle get_class_type() {
154  return _type_handle;
155  }
156  static void init_type() {
157  EggNode::init_type();
158  register_type(_type_handle, "EggVertexPool",
159  EggNode::get_class_type());
160  }
161  virtual TypeHandle get_type() const {
162  return get_class_type();
163  }
164  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
165 
166 private:
167  static TypeHandle _type_handle;
168 
169 friend class EggVertex;
170 };
171 
173 
174 #include "eggVertexPool.I"
175 
176 #endif
A base class for any of a number of kinds of geometry primitives: polygons, point lights...
Definition: eggPrimitive.h:51
void transform(const LMatrix4d &mat)
Applies the indicated transformation to the node and all of its descendants.
Definition: eggNode.I:313
This is an iterator adaptor that converts any iterator that returns a pair (e.g.
This is a 4-by-4 transform matrix.
Definition: lmatrix.h:4716
This is a four-component point in space.
Definition: lpoint4.h:457
This is a two-component point in space.
Definition: lpoint2.h:424
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:39
Any one-, two-, three-, or four-component vertex, possibly with attributes such as a normal...
Definition: eggVertex.h:41
This is the base class for all three-component vectors and points.
Definition: lvecBase4.h:111
This is a three-component point in space (as opposed to a three-component vector, which represents a ...
Definition: lpoint3.h:544
A base class for things that may be directly added into the egg hierarchy.
Definition: eggNode.h:38
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
A collection of vertices.
Definition: eggVertexPool.h:46