workingNodePath.cxx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Filename: workingNodePath.cxx
  2. // Created by: drose (16Mar02)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved
  8. //
  9. // All use of this software is subject to the terms of the Panda 3d
  10. // Software license. You should have received a copy of this license
  11. // along with this source code; you will also find a current copy of
  12. // the license at http://etc.cmu.edu/panda3d/docs/license/ .
  13. //
  14. // To contact the maintainers of this program write to
  15. // [email protected] .
  16. //
  17. ////////////////////////////////////////////////////////////////////
  18. #include "workingNodePath.h"
  19. ////////////////////////////////////////////////////////////////////
  20. // Function: WorkingNodePath::is_valid
  21. // Access: Public
  22. // Description: Returns true if the WorkingNodePath object appears to
  23. // be a valid NodePath reference, false otherwise.
  24. ////////////////////////////////////////////////////////////////////
  25. bool WorkingNodePath::
  26. is_valid() const {
  27. if (_node == (PandaNode *)NULL) {
  28. return false;
  29. }
  30. if (_next == (WorkingNodePath *)NULL) {
  31. return (_start != (NodePathComponent *)NULL);
  32. }
  33. return _next->is_valid();
  34. }
  35. ////////////////////////////////////////////////////////////////////
  36. // Function: WorkingNodePath::get_num_nodes
  37. // Access: Public
  38. // Description: Returns the number of nodes in the path from the root
  39. // to the current node.
  40. //
  41. // Since a WorkingNodePath always consists of, at
  42. // minimum, a nonempty parent NodePath and one child
  43. // node, this method will always return at least 2.
  44. ////////////////////////////////////////////////////////////////////
  45. int WorkingNodePath::
  46. get_num_nodes() const {
  47. if (_next == (WorkingNodePath *)NULL) {
  48. return _start->get_length();
  49. }
  50. return _next->get_num_nodes() + 1;
  51. }
  52. ////////////////////////////////////////////////////////////////////
  53. // Function: WorkingNodePath::get_node
  54. // Access: Public
  55. // Description: Returns the nth node of the path, where 0 is the
  56. // referenced (bottom) node and get_num_nodes() - 1 is
  57. // the top node. This requires iterating through the
  58. // path.
  59. ////////////////////////////////////////////////////////////////////
  60. PandaNode *WorkingNodePath::
  61. get_node(int index) const {
  62. nassertr(index >= 0, NULL);
  63. if (index == 0) {
  64. return _node;
  65. }
  66. if (_next == (WorkingNodePath *)NULL) {
  67. return get_node_path().get_node(index - 1);
  68. }
  69. return _next->get_node(index - 1);
  70. }
  71. ////////////////////////////////////////////////////////////////////
  72. // Function: WorkingNodePath::output
  73. // Access: Public
  74. // Description:
  75. ////////////////////////////////////////////////////////////////////
  76. void WorkingNodePath::
  77. output(ostream &out) const {
  78. // Cheesy and slow, but when you're outputting the thing, presumably
  79. // you're not in a hurry.
  80. get_node_path().output(out);
  81. }
  82. ////////////////////////////////////////////////////////////////////
  83. // Function: WorkingNodePath::r_get_node_path
  84. // Access: Private
  85. // Description: The private, recursive implementation of
  86. // get_node_path(), this returns the NodePathComponent
  87. // representing the NodePath.
  88. ////////////////////////////////////////////////////////////////////
  89. PT(NodePathComponent) WorkingNodePath::
  90. r_get_node_path() const {
  91. if (_next == (WorkingNodePath *)NULL) {
  92. nassertr(_start != (NodePathComponent *)NULL, NULL);
  93. return _start;
  94. }
  95. nassertr(_start == (NodePathComponent *)NULL, NULL);
  96. nassertr(_node != (PandaNode *)NULL, NULL);
  97. PT(NodePathComponent) comp = _next->r_get_node_path();
  98. nassertr(comp != (NodePathComponent *)NULL, NULL);
  99. PT(NodePathComponent) result = PandaNode::get_component(comp, _node);
  100. if (result == (NodePathComponent *)NULL) {
  101. // This means we found a disconnected chain in the
  102. // WorkingNodePath's ancestry: the node above this node isn't
  103. // connected. In this case, don't attempt to go higher; just
  104. // truncate the NodePath at the bottom of the disconnect.
  105. return PandaNode::get_top_component(_node, true);
  106. }
  107. return result;
  108. }