AASFile_optimize.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #pragma hdrstop
  21. #include "../idlib/precompiled.h"
  22. #include "AASFile.h"
  23. #include "AASFile_local.h"
  24. //===============================================================
  25. //
  26. // optimize file
  27. //
  28. //===============================================================
  29. /*
  30. ================
  31. idAASFileLocal::Optimize
  32. ================
  33. */
  34. void idAASFileLocal::Optimize() {
  35. int i, j, k, faceNum, edgeNum, areaFirstFace, faceFirstEdge;
  36. aasArea_t *area;
  37. aasFace_t *face;
  38. aasEdge_t *edge;
  39. idReachability *reach;
  40. idList<int> vertexRemap;
  41. idList<int> edgeRemap;
  42. idList<int> faceRemap;
  43. idList<aasVertex_t> newVertices;
  44. idList<aasEdge_t> newEdges;
  45. idList<aasIndex_t> newEdgeIndex;
  46. idList<aasFace_t> newFaces;
  47. idList<aasIndex_t> newFaceIndex;
  48. vertexRemap.AssureSize( vertices.Num(), -1 );
  49. edgeRemap.AssureSize( edges.Num(), 0 );
  50. faceRemap.AssureSize( faces.Num(), 0 );
  51. newVertices.Resize( vertices.Num() );
  52. newEdges.Resize( edges.Num() );
  53. newEdges.SetNum( 1 );
  54. newEdgeIndex.Resize( edgeIndex.Num() );
  55. newFaces.Resize( faces.Num() );
  56. newFaces.SetNum( 1 );
  57. newFaceIndex.Resize( faceIndex.Num() );
  58. for ( i = 0; i < areas.Num(); i++ ) {
  59. area = &areas[i];
  60. areaFirstFace = newFaceIndex.Num();
  61. for ( j = 0; j < area->numFaces; j++ ) {
  62. faceNum = faceIndex[area->firstFace + j];
  63. face = &faces[ abs(faceNum) ];
  64. // store face
  65. if ( !faceRemap[ abs(faceNum) ] ) {
  66. faceRemap[ abs(faceNum) ] = newFaces.Num();
  67. newFaces.Append( *face );
  68. // don't store edges for faces we don't care about
  69. if ( !( face->flags & ( FACE_FLOOR|FACE_LADDER ) ) ) {
  70. newFaces[ newFaces.Num()-1 ].firstEdge = 0;
  71. newFaces[ newFaces.Num()-1 ].numEdges = 0;
  72. } else {
  73. // store edges
  74. faceFirstEdge = newEdgeIndex.Num();
  75. for ( k = 0; k < face->numEdges; k++ ) {
  76. edgeNum = edgeIndex[ face->firstEdge + k ];
  77. edge = &edges[ abs(edgeNum) ];
  78. if ( !edgeRemap[ abs(edgeNum) ] ) {
  79. if ( edgeNum < 0 ) {
  80. edgeRemap[ abs(edgeNum) ] = -newEdges.Num();
  81. }
  82. else {
  83. edgeRemap[ abs(edgeNum) ] = newEdges.Num();
  84. }
  85. // remap vertices if not yet remapped
  86. if ( vertexRemap[ edge->vertexNum[0] ] == -1 ) {
  87. vertexRemap[ edge->vertexNum[0] ] = newVertices.Num();
  88. newVertices.Append( vertices[ edge->vertexNum[0] ] );
  89. }
  90. if ( vertexRemap[ edge->vertexNum[1] ] == -1 ) {
  91. vertexRemap[ edge->vertexNum[1] ] = newVertices.Num();
  92. newVertices.Append( vertices[ edge->vertexNum[1] ] );
  93. }
  94. newEdges.Append( *edge );
  95. newEdges[ newEdges.Num()-1 ].vertexNum[0] = vertexRemap[ edge->vertexNum[0] ];
  96. newEdges[ newEdges.Num()-1 ].vertexNum[1] = vertexRemap[ edge->vertexNum[1] ];
  97. }
  98. newEdgeIndex.Append( edgeRemap[ abs(edgeNum) ] );
  99. }
  100. newFaces[ newFaces.Num()-1 ].firstEdge = faceFirstEdge;
  101. newFaces[ newFaces.Num()-1 ].numEdges = newEdgeIndex.Num() - faceFirstEdge;
  102. }
  103. }
  104. if ( faceNum < 0 ) {
  105. newFaceIndex.Append( -faceRemap[ abs(faceNum) ] );
  106. } else {
  107. newFaceIndex.Append( faceRemap[ abs(faceNum) ] );
  108. }
  109. }
  110. area->firstFace = areaFirstFace;
  111. area->numFaces = newFaceIndex.Num() - areaFirstFace;
  112. // remap the reachability edges
  113. for ( reach = area->reach; reach; reach = reach->next ) {
  114. reach->edgeNum = abs( edgeRemap[reach->edgeNum] );
  115. }
  116. }
  117. // store new list
  118. vertices = newVertices;
  119. edges = newEdges;
  120. edgeIndex = newEdgeIndex;
  121. faces = newFaces;
  122. faceIndex = newFaceIndex;
  123. }