Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/pgraph/nodePath.h

Go to the documentation of this file.
00001 // Filename: nodePath.h
00002 // Created by:  drose (25Feb02)
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 NODEPATH_H
00020 #define NODEPATH_H
00021 
00022 #include "pandabase.h"
00023 
00024 #include "pandaNode.h"
00025 #include "renderState.h"
00026 #include "transformState.h"
00027 #include "nodePathComponent.h"
00028 
00029 #include "pointerTo.h"
00030 #include "referenceCount.h"
00031 #include "notify.h"
00032 #include "typedObject.h"
00033 
00034 class NodePathCollection;
00035 class TextureCollection;
00036 class FindApproxLevel;
00037 class FindApproxPath;
00038 class Texture;
00039 class Material;
00040 class Fog;
00041 class GlobPattern;
00042 
00043 //
00044 // A NodePath is the fundamental unit of high-level interaction with
00045 // the scene graph.  It encapsulates the complete path down to a node
00046 // from some other node, usually the root of the scene graph.  This is
00047 // used to resolve ambiguities associated with instancing.
00048 //
00049 // NodePath also contains a number of handy high-level methods for
00050 // common scene-graph manipulations, such as reparenting, and common
00051 // state changes, such as repositioning.
00052 //
00053 // There are also a number of NodePath methods for finding nodes deep
00054 // within the tree by name or by type.  These take a path string,
00055 // which at its simplest consists of a series of node names separated
00056 // by slashes, like a directory pathname.
00057 //
00058 // Each component of the path string may optionally consist of one of
00059 // the following special names, instead of a node name:
00060 //
00061 //   *          -- matches exactly one node, with any name.
00062 //   **         -- matches any sequence of zero or more nodes.
00063 //   +typename  -- matches any node that is or derives from the given type.
00064 //   -typename  -- matches any node that is the given type exactly.
00065 //
00066 // Furthermore, a node name may itself contain standard filename
00067 // globbing characters, like *, ?, and [a-z], that will be accepted as
00068 // a partial match.  (In fact, the '*' special name may be seen as
00069 // just a special case of this.)  The globbing characters may not be
00070 // used with the typename matches.
00071 //
00072 // The special characters "@@", appearing at the beginning of a node
00073 // name, indicate a stashed node.  Normally, stashed nodes are not
00074 // returned by a find (but see the special flags, below), but a
00075 // stashed node may be found if it is explicitly named with its
00076 // leading @@ characters.  By extension, "@@*" may be used to identify
00077 // any stashed node.
00078 //
00079 // Examples:
00080 //
00081 // "room//graph" will look for a node named "graph", which is a child
00082 // of an unnamed node, which is a child of a node named "room", which
00083 // is a child of the starting path.
00084 //
00085 // "**/red*" will look for any node anywhere in the tree (below the
00086 // starting path) with a name that begins with "red".
00087 //
00088 // "**/+PartBundleNode/**/head" will look for a node named "head",
00089 // somewhere below a PartBundleNode anywhere in the tree.
00090 //
00091 //
00092 // The search is always potentially ambiguous, even if the special
00093 // wildcard operators are not used, because there may be multiple
00094 // nodes in the tree with the same name.  In general, in the case of
00095 // an ambiguity, the shortest path is preferred; when a method (such
00096 // as extend_by) must choose only only one of several possible paths,
00097 // it will choose the shortest available; on the other hand, when a
00098 // method (such as find_all_matches) is to return all of the matching
00099 // paths, it will sort them so that the shortest paths appear first in
00100 // the output.
00101 //
00102 //
00103 // Special flags.  The entire string may optionally be followed by the
00104 // ";" character, followed by one or more of the following special
00105 // control flags, with no intervening spaces or punctuation:
00106 //
00107 //    -h    Do not return hidden nodes.
00108 //    +h    Do return hidden nodes.
00109 //    -s    Do not return stashed nodes unless explicitly referenced with @@.
00110 //    +s    Return stashed nodes even without any explicit @@ characters.
00111 //
00112 // The default flags are +h-s.
00113 //
00114 
00115 
00116 ////////////////////////////////////////////////////////////////////
00117 //       Class : NodePath
00118 // Description : NodePath is the fundamental system for disambiguating
00119 //               instances, and also provides a higher-level interface
00120 //               for manipulating the scene graph.
00121 //
00122 //               A NodePath is a list of connected nodes from the root
00123 //               of the graph to any sub-node.  Each NodePath
00124 //               therefore unqiuely describes one instance of a node.
00125 //
00126 //               NodePaths themselves are lightweight objects that may
00127 //               easily be copied and passed by value.  Their data is
00128 //               stored as a series of NodePathComponents that are
00129 //               stored on the nodes.  Holding a NodePath will keep a
00130 //               reference count to all the nodes in the path.
00131 //               However, if any node in the path is removed or
00132 //               reparented (perhaps through a different NodePath),
00133 //               the NodePath will automatically be updated to reflect
00134 //               the changes.
00135 ////////////////////////////////////////////////////////////////////
00136 class EXPCL_PANDA NodePath {
00137 PUBLISHED:
00138   // This enumeration is returned by get_error_type() for an empty
00139   // NodePath to report the reason it's empty.
00140   enum ErrorType {
00141     ET_ok = 0,     // i.e. not empty, or never assigned to anything.
00142     ET_not_found,  // returned from a failed find() or similar function.
00143     ET_removed,    // remove_node() was previously called on this NodePath.
00144     ET_fail,       // general failure return from some function.
00145 
00146     // Also see NodePathComponent::_next_key, which initializes
00147     // itself to the last enumerated type here plus one.
00148   };
00149 
00150   INLINE NodePath();
00151   INLINE NodePath(const string &top_node_name);
00152   INLINE NodePath(PandaNode *node);
00153   INLINE static NodePath any_path(PandaNode *node);
00154   INLINE NodePath(const NodePath &parent, PandaNode *child_node);
00155 
00156   INLINE NodePath(const NodePath &copy);
00157   INLINE void operator = (const NodePath &copy);
00158 
00159   INLINE static NodePath not_found();
00160   INLINE static NodePath removed();
00161   INLINE static NodePath fail();
00162 
00163   INLINE static void set_max_search_depth(int max_search_depth);
00164   INLINE static int get_max_search_depth();
00165 
00166   // Methods to query a NodePath's contents.
00167   INLINE bool is_empty() const;
00168   INLINE bool is_singleton() const;
00169   int get_num_nodes() const;
00170   PandaNode *get_node(int index) const;
00171 
00172   INLINE ErrorType get_error_type() const;
00173 
00174   PandaNode *get_top_node() const;
00175   INLINE PandaNode *node() const;
00176 
00177   INLINE int get_key() const;
00178 
00179   // Methods that return collections of NodePaths derived from or
00180   // related to this one.
00181 
00182   NodePathCollection get_children() const;
00183   INLINE int get_num_children() const;
00184   INLINE NodePath get_child(int n) const;
00185 
00186   INLINE bool has_parent() const;
00187   INLINE NodePath get_parent() const;
00188 
00189   NodePath find(const string &path) const;
00190   NodePath find_path_to(PandaNode *node) const;
00191   NodePathCollection find_all_matches(const string &path) const;
00192   NodePathCollection find_all_paths_to(PandaNode *node) const;
00193 
00194   // Methods that actually move nodes around in the scene graph.  The
00195   // optional "sort" parameter can be used to force a particular
00196   // ordering between sibling nodes, useful when dealing with LOD's
00197   // and similar switch nodes.  If the sort value is the same, nodes
00198   // will be arranged in the order they were added.
00199   void reparent_to(const NodePath &other, int sort = 0);
00200   void wrt_reparent_to(const NodePath &other, int sort = 0);
00201   NodePath instance_to(const NodePath &other, int sort = 0) const;
00202   NodePath instance_under_node(const NodePath &other, const string &name,
00203                                  int sort = 0) const;
00204   NodePath copy_to(const NodePath &other, int sort = 0) const;
00205   NodePath attach_new_node(PandaNode *node, int sort = 0) const;
00206   INLINE NodePath attach_new_node(const string &name, int sort = 0) const;
00207   void remove_node();
00208   void detach_node();
00209 
00210   // Handy ways to look at what's there, and other miscellaneous
00211   // operations.
00212 
00213   void output(ostream &out) const;
00214 
00215   INLINE void ls() const;
00216   INLINE void ls(ostream &out, int indent_level = 0) const;
00217 
00218 
00219   // Aggregate transform and state information.
00220   INLINE const RenderState *get_state() const;
00221   INLINE void set_state(const RenderState *state) const;
00222   CPT(RenderState) get_state(const NodePath &other) const;
00223   void set_state(const NodePath &other, const RenderState *state) const;
00224   INLINE CPT(RenderState) get_net_state() const;
00225 
00226   INLINE const TransformState *get_transform() const;
00227   INLINE void set_transform(const TransformState *transform) const;
00228   CPT(TransformState) get_transform(const NodePath &other) const;
00229   void set_transform(const NodePath &other, const TransformState *transform) const;
00230   INLINE CPT(TransformState) get_net_transform() const;
00231 
00232 
00233   // Methods that get and set the matrix transform: pos, hpr, scale,
00234   // in the local coordinate system.
00235 
00236   INLINE void set_pos(float x, float y, float z);
00237   void set_pos(const LVecBase3f &pos);
00238   void set_x(float x);
00239   void set_y(float y);
00240   void set_z(float z);
00241   LPoint3f get_pos() const;
00242   INLINE float get_x() const;
00243   INLINE float get_y() const;
00244   INLINE float get_z() const;
00245 
00246   INLINE void set_hpr(float h, float p, float r);
00247   void set_hpr(const LVecBase3f &hpr);
00248   void set_h(float h);
00249   void set_p(float p);
00250   void set_r(float r);
00251   LVecBase3f get_hpr() const;
00252   LVecBase3f get_hpr(float roll) const;
00253   INLINE float get_h() const;
00254   INLINE float get_p() const;
00255   INLINE float get_r() const;
00256 
00257   void set_quat(const LQuaternionf &quat);
00258   LQuaternionf get_quat() const;
00259 
00260   INLINE void set_scale(float scale);
00261   INLINE void set_scale(float sx, float sy, float sz);
00262   void set_scale(const LVecBase3f &scale);
00263   void set_sx(float sx);
00264   void set_sy(float sy);
00265   void set_sz(float sz);
00266   LVecBase3f get_scale() const;
00267   INLINE float get_sx() const;
00268   INLINE float get_sy() const;
00269   INLINE float get_sz() const;
00270 
00271   INLINE void set_pos_hpr(float x, float y, float z,
00272                           float h, float p, float r);
00273   void set_pos_hpr(const LVecBase3f &pos,
00274                    const LVecBase3f &hpr);
00275 
00276   INLINE void set_hpr_scale(float h, float p, float r,
00277                             float sx, float sy, float sz);
00278   void set_hpr_scale(const LVecBase3f &hpr,
00279                      const LVecBase3f &scale);
00280   INLINE void set_pos_hpr_scale(float x, float y, float z,
00281                                 float h, float p, float r,
00282                                 float sx, float sy, float sz);
00283   void set_pos_hpr_scale(const LVecBase3f &pos,
00284                          const LVecBase3f &hpr,
00285                          const LVecBase3f &scale);
00286   void set_pos_quat_scale(const LVecBase3f &pos,
00287                           const LQuaternionf &quat,
00288                           const LVecBase3f &scale);
00289 
00290   void set_mat(const LMatrix4f &mat);
00291   INLINE void clear_mat();
00292   INLINE bool has_mat() const;
00293   INLINE const LMatrix4f &get_mat() const;
00294 
00295   bool has_color_scale() const;
00296   void clear_color_scale();
00297   void set_color_scale(const LVecBase4f &scale);
00298   INLINE void set_color_scale(float sx, float sy, float sz, float sa);
00299   INLINE void set_sr(float sr);
00300   INLINE void set_sg(float sg);
00301   INLINE void set_sb(float sb);
00302   INLINE void set_sa(float sa);
00303 
00304   const LVecBase4f &get_color_scale() const;
00305   INLINE float get_sr() const;
00306   INLINE float get_sg() const;
00307   INLINE float get_sb() const;
00308   INLINE float get_sa() const;
00309 
00310   INLINE void look_at(float x, float y, float z);
00311   void look_at(const LPoint3f &point, const LVector3f &up = LVector3f::up());
00312   INLINE void heads_up(float x, float y, float z);
00313   void heads_up(const LPoint3f &point, const LVector3f &up = LVector3f::up());
00314 
00315   // Methods that get and set the matrix transforms relative to some
00316   // other node in the scene graph.  These perform an implicit wrt().
00317 
00318   INLINE void set_pos(const NodePath &other, float x, float y, float z);
00319   void set_pos(const NodePath &other, const LVecBase3f &pos);
00320   void set_x(const NodePath &other, float x);
00321   void set_y(const NodePath &other, float y);
00322   void set_z(const NodePath &other, float z);
00323   LPoint3f get_pos(const NodePath &other) const;
00324   INLINE float get_x(const NodePath &other) const;
00325   INLINE float get_y(const NodePath &other) const;
00326   INLINE float get_z(const NodePath &other) const;
00327 
00328   INLINE void set_hpr(const NodePath &other, float h, float p, float r);
00329   void set_hpr(const NodePath &other, const LVecBase3f &hpr);
00330   void set_h(const NodePath &other, float h);
00331   void set_p(const NodePath &other, float p);
00332   void set_r(const NodePath &other, float r);
00333   LVecBase3f get_hpr(const NodePath &other) const;
00334   LVecBase3f get_hpr(const NodePath &other, float roll) const;
00335   INLINE float get_h(const NodePath &other) const;
00336   INLINE float get_p(const NodePath &other) const;
00337   INLINE float get_r(const NodePath &other) const;
00338 
00339   void set_quat(const NodePath &other, const LQuaternionf &quat);
00340   LQuaternionf get_quat(const NodePath &other) const;
00341 
00342   INLINE void set_scale(const NodePath &other, float sx, float sy, float sz);
00343   void set_scale(const NodePath &other, const LVecBase3f &scale);
00344   void set_sx(const NodePath &other, float sx);
00345   void set_sy(const NodePath &other, float sy);
00346   void set_sz(const NodePath &other, float sz);
00347   LVecBase3f get_scale(const NodePath &other) const;
00348   INLINE float get_sx(const NodePath &other) const;
00349   INLINE float get_sy(const NodePath &other) const;
00350   INLINE float get_sz(const NodePath &other) const;
00351 
00352   INLINE void set_pos_hpr(const NodePath &other,
00353                           float x, float y, float z,
00354                           float h, float p, float r);
00355   void set_pos_hpr(const NodePath &other,
00356                    const LVecBase3f &pos,
00357                    const LVecBase3f &hpr);
00358   INLINE void set_hpr_scale(const NodePath &other,
00359                 float h, float p, float r,
00360                 float sx, float sy, float sz);
00361   void set_hpr_scale(const NodePath &other,
00362              const LVecBase3f &hpr,
00363              const LVecBase3f &scale);
00364   INLINE void set_pos_hpr_scale(const NodePath &other,
00365                                 float x, float y, float z,
00366                                 float h, float p, float r,
00367                                 float sx, float sy, float sz);
00368   void set_pos_hpr_scale(const NodePath &other,
00369                          const LVecBase3f &pos,
00370                          const LVecBase3f &hpr,
00371                          const LVecBase3f &scale);
00372   void set_pos_quat_scale(const NodePath &other,
00373                           const LVecBase3f &pos,
00374                           const LQuaternionf &quat,
00375                           const LVecBase3f &scale);
00376 
00377   const LMatrix4f &get_mat(const NodePath &other) const;
00378   void set_mat(const NodePath &other, const LMatrix4f &mat);
00379 
00380   LPoint3f get_relative_point(const NodePath &other, const LVecBase3f &point);
00381 
00382   INLINE void look_at(const NodePath &other,
00383                       float x, float y, float z);
00384   void look_at(const NodePath &other,
00385                const LPoint3f &point = LPoint3f(0.0, 0.0, 0.0),
00386                const LVector3f &up = LVector3f::up());
00387   INLINE void heads_up(const NodePath &other,
00388                        float x, float y, float z);
00389   void heads_up(const NodePath &other,
00390                 const LPoint3f &point = LPoint3f(0.0, 0.0, 0.0),
00391                 const LVector3f &up = LVector3f::up());
00392 
00393   INLINE float get_distance(const NodePath &other) const;
00394 
00395 
00396   // Methods that affect appearance of geometry: color, texture, etc.
00397   // These affect the state at the bottom level only.
00398 
00399   void set_color(float r, float g, float b, float a = 1.0,
00400                  int priority = 0);
00401   void set_color(const Colorf &color, int priority = 0);
00402   void set_color_off(int priority = 0);
00403   void clear_color();
00404   bool has_color() const;
00405   Colorf get_color() const;
00406 
00407   void set_bin(const string &bin_name, int draw_order, int priority = 0);
00408   void clear_bin();
00409   bool has_bin() const;
00410   string get_bin_name() const;
00411   int get_bin_draw_order() const;
00412 
00413   void set_texture(Texture *tex, int priority = 0);
00414   void set_texture_off(int priority = 0);
00415   void clear_texture();
00416   bool has_texture() const;
00417   bool has_texture_off() const;
00418   Texture *get_texture() const;
00419 
00420   Texture *find_texture(const string &name) const;
00421   TextureCollection find_all_textures() const;
00422   TextureCollection find_all_textures(const string &name) const;
00423 
00424   void set_material(Material *tex, int priority = 0);
00425   void set_material_off(int priority = 0);
00426   void clear_material();
00427   bool has_material() const;
00428   PT(Material) get_material() const;
00429 
00430   void set_fog(Fog *fog, int priority = 0);
00431   void set_fog_off(int priority = 0);
00432   void clear_fog();
00433   bool has_fog() const;
00434   bool has_fog_off() const;
00435   Fog *get_fog() const;
00436 
00437   void set_render_mode_wireframe(int priority = 0);
00438   void set_render_mode_filled(int priority = 0);
00439   void clear_render_mode();
00440   bool has_render_mode() const;
00441 
00442   void set_two_sided(bool two_sided, int priority = 0);
00443   void clear_two_sided();
00444   bool has_two_sided() const;
00445   bool get_two_sided() const;
00446 #if 0
00447   void set_alpha_test(RenderAttrib::PandaCompareFunc alpha_test_mode,float reference_alpha,int priority = 0);
00448   void clear_alpha_test();
00449   bool has_alpha_test() const;
00450   bool get_alpha_test() const;
00451 #endif
00452   void set_depth_test(bool depth_test, int priority = 0);
00453   void clear_depth_test();
00454   bool has_depth_test() const;
00455   bool get_depth_test() const;
00456 
00457   void set_depth_write(bool depth_write, int priority = 0);
00458   void clear_depth_write();
00459   bool has_depth_write() const;
00460   bool get_depth_write() const;
00461 
00462   void do_billboard_axis(const NodePath &camera, float offset);
00463   void do_billboard_point_eye(const NodePath &camera, float offset);
00464   void do_billboard_point_world(const NodePath &camera, float offset);
00465   INLINE void set_billboard_axis(float offset = 0.0);
00466   INLINE void set_billboard_point_eye(float offset = 0.0);
00467   INLINE void set_billboard_point_world(float offset = 0.0);
00468   void set_billboard_axis(const NodePath &camera, float offset);
00469   void set_billboard_point_eye(const NodePath &camera, float offset);
00470   void set_billboard_point_world(const NodePath &camera, float offset);
00471   void clear_billboard();
00472   bool has_billboard() const;
00473 
00474   void set_compass(const NodePath &reference = NodePath());
00475   void clear_compass();
00476   bool has_compass() const;
00477 
00478   void set_transparency(bool transparency, int priority = 0);
00479   void clear_transparency();
00480   bool has_transparency() const;
00481   bool get_transparency() const;
00482 
00483   INLINE void adjust_all_priorities(int adjustment);
00484 
00485   // Variants on show and hide
00486   INLINE void show();
00487   INLINE void show(DrawMask camera_mask);
00488   INLINE void hide();
00489   INLINE void hide(DrawMask camera_mask);
00490   INLINE bool is_hidden(DrawMask camera_mask = DrawMask::all_on()) const;
00491   NodePath get_hidden_ancestor(DrawMask camera_mask = DrawMask::all_on()) const;
00492 
00493   INLINE bool stash();
00494   INLINE bool unstash();
00495   INLINE bool is_stashed() const;
00496   NodePath get_stashed_ancestor() const;
00497 
00498   // Comparison methods
00499   INLINE bool operator == (const NodePath &other) const;
00500   INLINE bool operator != (const NodePath &other) const;
00501   INLINE bool operator < (const NodePath &other) const;
00502   INLINE int compare_to(const NodePath &other) const;
00503 
00504   // Miscellaneous
00505   bool verify_complete() const;
00506 
00507   void prepare_scene(GraphicsStateGuardianBase *gsg, bool force_retained_mode = false);
00508 
00509   void show_bounds();
00510   void hide_bounds();
00511   PT(BoundingVolume) get_bounds() const;
00512   void force_recompute_bounds();
00513   void write_bounds(ostream &out) const;
00514   bool calc_tight_bounds(LPoint3f &min_point, LPoint3f &max_point);
00515 
00516   int flatten_light();
00517   int flatten_medium();
00518   int flatten_strong();
00519 
00520   bool write_bam_file(const string &filename) const;
00521 
00522 private:
00523   void uncollapse_head() const;
00524   static void find_common_ancestor(const NodePath &a, const NodePath &b,
00525                                    int &a_count, int &b_count);
00526 
00527   CPT(RenderState) r_get_net_state(NodePathComponent *comp) const;
00528   CPT(RenderState) r_get_partial_state(NodePathComponent *comp, int n) const;
00529   CPT(TransformState) r_get_net_transform(NodePathComponent *comp) const;
00530   CPT(TransformState) r_get_partial_transform(NodePathComponent *comp, int n) const;
00531 
00532   void find_matches(NodePathCollection &result,
00533                     const string &approx_path_str,
00534                     int max_matches) const;
00535   void find_matches(NodePathCollection &result,
00536                     FindApproxPath &approx_path,
00537                     int max_matches) const;
00538   void r_find_matches(NodePathCollection &result,
00539                       const FindApproxLevel &level,
00540                       int max_matches, int num_levels_remaining) const;
00541 
00542   void r_adjust_all_priorities(PandaNode *node, int adjustment);
00543 
00544   void r_force_recompute_bounds(PandaNode *node);
00545 
00546   typedef pset<Texture *> Textures;
00547   Texture *r_find_texture(PandaNode *node, const RenderState *state,
00548                           const GlobPattern &glob) const;
00549   void r_find_all_textures(PandaNode *node, const RenderState *state,
00550                            Textures &textures) const;
00551   void r_prepare_scene(PandaNode *node, const RenderState *state,
00552                        GraphicsStateGuardianBase *gsg, bool do_retained_mode);
00553 
00554   PT(NodePathComponent) _head;
00555   ErrorType _error_type;
00556   static int _max_search_depth;
00557 
00558 public:
00559   static TypeHandle get_class_type() {
00560     return _type_handle;
00561   }
00562   static void init_type() {
00563     register_type(_type_handle, "NodePath");
00564   }
00565 
00566 private:
00567   static TypeHandle _type_handle;
00568 
00569   friend class NodePathCollection;
00570   friend class WorkingNodePath;
00571 };
00572 
00573 INLINE ostream &operator << (ostream &out, const NodePath &node_path);
00574 
00575 #include "nodePath.I"
00576 
00577 #endif

Generated on Fri May 2 00:41:58 2003 for Panda by doxygen1.3