workingNodePath.cxx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Filename: workingNodePath.cxx
  2. // Created by: drose (16Mar02)
  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. #include "workingNodePath.h"
  15. ////////////////////////////////////////////////////////////////////
  16. // Function: WorkingNodePath::is_valid
  17. // Access: Public
  18. // Description: Returns true if the WorkingNodePath object appears to
  19. // be a valid NodePath reference, false otherwise.
  20. ////////////////////////////////////////////////////////////////////
  21. bool WorkingNodePath::
  22. is_valid() const {
  23. if (_node == (PandaNode *)NULL) {
  24. return false;
  25. }
  26. if (_next == (WorkingNodePath *)NULL) {
  27. return (_start != (NodePathComponent *)NULL);
  28. }
  29. nassertr(_node != _next->_node, false);
  30. return _next->is_valid();
  31. }
  32. ////////////////////////////////////////////////////////////////////
  33. // Function: WorkingNodePath::get_num_nodes
  34. // Access: Public
  35. // Description: Returns the number of nodes in the path from the root
  36. // to the current node.
  37. //
  38. // Since a WorkingNodePath always consists of, at
  39. // minimum, a nonempty parent NodePath and one child
  40. // node, this method will always return at least 2.
  41. ////////////////////////////////////////////////////////////////////
  42. int WorkingNodePath::
  43. get_num_nodes() const {
  44. if (_next == (WorkingNodePath *)NULL) {
  45. Thread *current_thread = Thread::get_current_thread();
  46. int pipeline_stage = current_thread->get_pipeline_stage();
  47. return _start->get_length(pipeline_stage, current_thread);
  48. }
  49. return _next->get_num_nodes() + 1;
  50. }
  51. ////////////////////////////////////////////////////////////////////
  52. // Function: WorkingNodePath::get_node
  53. // Access: Public
  54. // Description: Returns the nth node of the path, where 0 is the
  55. // referenced (bottom) node and get_num_nodes() - 1 is
  56. // the top node. This requires iterating through the
  57. // path.
  58. ////////////////////////////////////////////////////////////////////
  59. PandaNode *WorkingNodePath::
  60. get_node(int index) const {
  61. nassertr(index >= 0, NULL);
  62. if (index == 0) {
  63. return _node;
  64. }
  65. if (_next == (WorkingNodePath *)NULL) {
  66. return get_node_path().get_node(index - 1);
  67. }
  68. return _next->get_node(index - 1);
  69. }
  70. ////////////////////////////////////////////////////////////////////
  71. // Function: WorkingNodePath::output
  72. // Access: Public
  73. // Description:
  74. ////////////////////////////////////////////////////////////////////
  75. void WorkingNodePath::
  76. output(ostream &out) const {
  77. // Cheesy and slow, but when you're outputting the thing, presumably
  78. // you're not in a hurry.
  79. get_node_path().output(out);
  80. }
  81. ////////////////////////////////////////////////////////////////////
  82. // Function: WorkingNodePath::r_get_node_path
  83. // Access: Private
  84. // Description: The private, recursive implementation of
  85. // get_node_path(), this returns the NodePathComponent
  86. // representing the NodePath.
  87. ////////////////////////////////////////////////////////////////////
  88. PT(NodePathComponent) WorkingNodePath::
  89. r_get_node_path() const {
  90. if (_next == (WorkingNodePath *)NULL) {
  91. nassertr(_start != (NodePathComponent *)NULL, NULL);
  92. return _start;
  93. }
  94. nassertr(_start == (NodePathComponent *)NULL, NULL);
  95. nassertr(_node != (PandaNode *)NULL, NULL);
  96. PT(NodePathComponent) comp = _next->r_get_node_path();
  97. nassertr(comp != (NodePathComponent *)NULL, NULL);
  98. Thread *current_thread = Thread::get_current_thread();
  99. int pipeline_stage = current_thread->get_pipeline_stage();
  100. PT(NodePathComponent) result =
  101. PandaNode::get_component(comp, _node, pipeline_stage, current_thread);
  102. if (result == (NodePathComponent *)NULL) {
  103. // This means we found a disconnected chain in the
  104. // WorkingNodePath's ancestry: the node above this node isn't
  105. // connected. In this case, don't attempt to go higher; just
  106. // truncate the NodePath at the bottom of the disconnect.
  107. return PandaNode::get_top_component(_node, true, pipeline_stage, current_thread);
  108. }
  109. return result;
  110. }