tree.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1999-2005 Id Software, Inc.
  4. This file is part of Quake III Arena source code.
  5. Quake III Arena source code is free software; you can redistribute it
  6. and/or modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. Quake III Arena source code is distributed in the hope that it will be
  10. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Foobar; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. #include "qbsp.h"
  19. extern int c_nodes;
  20. void RemovePortalFromNode (portal_t *portal, node_t *l);
  21. node_t *NodeForPoint (node_t *node, vec3_t origin)
  22. {
  23. plane_t *plane;
  24. vec_t d;
  25. while (node->planenum != PLANENUM_LEAF)
  26. {
  27. plane = &mapplanes[node->planenum];
  28. d = DotProduct (origin, plane->normal) - plane->dist;
  29. if (d >= 0)
  30. node = node->children[0];
  31. else
  32. node = node->children[1];
  33. }
  34. return node;
  35. }
  36. /*
  37. =============
  38. FreeTreePortals_r
  39. =============
  40. */
  41. void FreeTreePortals_r (node_t *node)
  42. {
  43. portal_t *p, *nextp;
  44. int s;
  45. // free children
  46. if (node->planenum != PLANENUM_LEAF)
  47. {
  48. FreeTreePortals_r (node->children[0]);
  49. FreeTreePortals_r (node->children[1]);
  50. }
  51. // free portals
  52. for (p=node->portals ; p ; p=nextp)
  53. {
  54. s = (p->nodes[1] == node);
  55. nextp = p->next[s];
  56. RemovePortalFromNode (p, p->nodes[!s]);
  57. FreePortal (p);
  58. }
  59. node->portals = NULL;
  60. }
  61. /*
  62. =============
  63. FreeTree_r
  64. =============
  65. */
  66. void FreeTree_r (node_t *node)
  67. {
  68. // free children
  69. if (node->planenum != PLANENUM_LEAF)
  70. {
  71. FreeTree_r (node->children[0]);
  72. FreeTree_r (node->children[1]);
  73. }
  74. // free bspbrushes
  75. FreeBrushList (node->brushlist);
  76. // free the node
  77. if (node->volume)
  78. FreeBrush (node->volume);
  79. if (numthreads == 1)
  80. c_nodes--;
  81. free (node);
  82. }
  83. /*
  84. =============
  85. FreeTree
  86. =============
  87. */
  88. void FreeTree (tree_t *tree)
  89. {
  90. FreeTreePortals_r (tree->headnode);
  91. FreeTree_r (tree->headnode);
  92. free (tree);
  93. }
  94. //===============================================================
  95. void PrintTree_r (node_t *node, int depth)
  96. {
  97. int i;
  98. plane_t *plane;
  99. bspbrush_t *bb;
  100. for (i=0 ; i<depth ; i++)
  101. _printf (" ");
  102. if (node->planenum == PLANENUM_LEAF)
  103. {
  104. if (!node->brushlist)
  105. _printf ("NULL\n");
  106. else
  107. {
  108. for (bb=node->brushlist ; bb ; bb=bb->next)
  109. _printf ("%i ", bb->original->brushnum);
  110. _printf ("\n");
  111. }
  112. return;
  113. }
  114. plane = &mapplanes[node->planenum];
  115. _printf ("#%i (%5.2f %5.2f %5.2f):%5.2f\n", node->planenum,
  116. plane->normal[0], plane->normal[1], plane->normal[2],
  117. plane->dist);
  118. PrintTree_r (node->children[0], depth+1);
  119. PrintTree_r (node->children[1], depth+1);
  120. }