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