From ffa1a4340cdfabd83a85f0a0f5939f593ad3c9ba Mon Sep 17 00:00:00 2001 From: Garux Date: Thu, 29 Jul 2021 22:21:36 +0300 Subject: [PATCH] std::vector mapplanes --- tools/quake3/q3map2/convert_map.cpp | 28 ++++++------- tools/quake3/q3map2/facebsp.cpp | 11 ++---- tools/quake3/q3map2/map.cpp | 61 +++++++++++++---------------- tools/quake3/q3map2/model.cpp | 3 -- tools/quake3/q3map2/q3map2.h | 4 +- tools/quake3/q3map2/tree.cpp | 14 +++---- tools/quake3/q3map2/writebsp.cpp | 5 +-- 7 files changed, 53 insertions(+), 73 deletions(-) diff --git a/tools/quake3/q3map2/convert_map.cpp b/tools/quake3/q3map2/convert_map.cpp index 69c47d3f..531fb4db 100644 --- a/tools/quake3/q3map2/convert_map.cpp +++ b/tools/quake3/q3map2/convert_map.cpp @@ -55,7 +55,7 @@ void GetBestSurfaceTriangleMatchForBrushside( side_t *buildSide, bspDrawVert_t * float thisarea; bspDrawVert_t *vert[3]; winding_t *polygon; - plane_t *buildPlane = &mapplanes[buildSide->planenum]; + const plane_t& buildPlane = mapplanes[buildSide->planenum]; int matches = 0; // first, start out with NULLs @@ -76,9 +76,9 @@ void GetBestSurfaceTriangleMatchForBrushside( side_t *buildSide, bspDrawVert_t * vert[1] = &bspDrawVerts[s->firstVert + bspDrawIndexes[s->firstIndex + t + 1]]; vert[2] = &bspDrawVerts[s->firstVert + bspDrawIndexes[s->firstIndex + t + 2]]; if ( s->surfaceType == MST_PLANAR && VectorCompare( vert[0]->normal, vert[1]->normal ) && VectorCompare( vert[1]->normal, vert[2]->normal ) ) { - if ( vector3_length( vert[0]->normal - buildPlane->normal() ) >= normalEpsilon - || vector3_length( vert[1]->normal - buildPlane->normal() ) >= normalEpsilon - || vector3_length( vert[2]->normal - buildPlane->normal() ) >= normalEpsilon ) { + if ( vector3_length( vert[0]->normal - buildPlane.normal() ) >= normalEpsilon + || vector3_length( vert[1]->normal - buildPlane.normal() ) >= normalEpsilon + || vector3_length( vert[2]->normal - buildPlane.normal() ) >= normalEpsilon ) { continue; } } @@ -88,14 +88,14 @@ void GetBestSurfaceTriangleMatchForBrushside( side_t *buildSide, bspDrawVert_t * // models, there is no better way Plane3f plane; PlaneFromPoints( plane, vert[0]->xyz, vert[1]->xyz, vert[2]->xyz ); - if ( vector3_length( plane.normal() - buildPlane->normal() ) >= normalEpsilon ) { + if ( vector3_length( plane.normal() - buildPlane.normal() ) >= normalEpsilon ) { continue; } } // fixme? better distance epsilon - if ( abs( plane3_distance_to_point( buildPlane->plane, vert[0]->xyz ) ) > 1 - || abs( plane3_distance_to_point( buildPlane->plane, vert[1]->xyz ) ) > 1 - || abs( plane3_distance_to_point( buildPlane->plane, vert[2]->xyz ) ) > 1 ) { + if ( abs( plane3_distance_to_point( buildPlane.plane, vert[0]->xyz ) ) > 1 + || abs( plane3_distance_to_point( buildPlane.plane, vert[1]->xyz ) ) > 1 + || abs( plane3_distance_to_point( buildPlane.plane, vert[2]->xyz ) ) > 1 ) { continue; } // Okay. Correct surface type, correct shader, correct plane. Let's start with the business... @@ -108,7 +108,7 @@ void GetBestSurfaceTriangleMatchForBrushside( side_t *buildSide, bspDrawVert_t * const Vector3& v1 = vert[( i + 1 ) % 3]->xyz; const Vector3& v2 = vert[( i + 2 ) % 3]->xyz; // we now need to generate the plane spanned by normal and (v2 - v1). - Plane3f plane( vector3_cross( v2 - v1, buildPlane->normal() ), 0 ); + Plane3f plane( vector3_cross( v2 - v1, buildPlane.normal() ), 0 ); plane.dist() = vector3_dot( v1, plane.normal() ); ChopWindingInPlace( &polygon, plane, distanceEpsilon ); if ( !polygon ) { @@ -876,13 +876,13 @@ static void ConvertModel( FILE *f, bspModel_t *model, int modelNum, const Vector /* convert bsp planes to map planes */ - nummapplanes = numBSPPlanes; - AUTOEXPAND_BY_REALLOC( mapplanes, nummapplanes, allocatedmapplanes, 1024 ); + mapplanes.resize( numBSPPlanes ); for ( i = 0; i < numBSPPlanes; i++ ) { - mapplanes[ i ].plane = bspPlanes[ i ]; - mapplanes[ i ].type = PlaneTypeForNormal( mapplanes[ i ].normal() ); - mapplanes[ i ].hash_chain = 0; + plane_t& plane = mapplanes[i]; + plane.plane = bspPlanes[ i ]; + plane.type = PlaneTypeForNormal( plane.normal() ); + plane.hash_chain = 0; } /* allocate a build brush */ diff --git a/tools/quake3/q3map2/facebsp.cpp b/tools/quake3/q3map2/facebsp.cpp index 806f198f..d39f9431 100644 --- a/tools/quake3/q3map2/facebsp.cpp +++ b/tools/quake3/q3map2/facebsp.cpp @@ -363,24 +363,21 @@ void BuildFaceTree_r( node_t *node, face_t *list ){ ================ */ tree_t *FaceBSP( face_t *list ) { - tree_t *tree; - face_t *face; - Sys_FPrintf( SYS_VRB, "--- FaceBSP ---\n" ); - tree = AllocTree(); + tree_t *tree = AllocTree(); int count = 0; - for ( face = list; face != NULL; face = face->next ) + for ( const face_t *face = list; face != NULL; face = face->next ) { WindingExtendBounds( face->w, tree->minmax ); count++; } Sys_FPrintf( SYS_VRB, "%9d faces\n", count ); - for ( int i = 0; i < nummapplanes; i++ ) + for ( plane_t& plane : mapplanes ) { - mapplanes[ i ].counter = 0; + plane.counter = 0; } tree->headnode = AllocNode(); diff --git a/tools/quake3/q3map2/map.cpp b/tools/quake3/q3map2/map.cpp index 2181fca7..05daa89b 100644 --- a/tools/quake3/q3map2/map.cpp +++ b/tools/quake3/q3map2/map.cpp @@ -81,14 +81,11 @@ bool PlaneEqual( const plane_t& p, const Plane3f& plane ){ AddPlaneToHash() */ -void AddPlaneToHash( plane_t *p ){ - int hash; +void AddPlaneToHash( plane_t& p ){ + const int hash = ( PLANE_HASHES - 1 ) & (int) fabs( p.dist() ); - - hash = ( PLANE_HASHES - 1 ) & (int) fabs( p->dist() ); - - p->hash_chain = planehash[hash]; - planehash[hash] = p - mapplanes + 1; + p.hash_chain = planehash[hash]; + planehash[hash] = &p - mapplanes.data() + 1; } /* @@ -104,30 +101,28 @@ int CreateNewFloatPlane( const Plane3f& plane ){ } // create a new plane - AUTOEXPAND_BY_REALLOC( mapplanes, nummapplanes + 1, allocatedmapplanes, 1024 ); - - plane_t *p = &mapplanes[nummapplanes]; - p->plane = plane; - ( p + 1 )->plane = plane3_flipped( plane ); - p->type = ( p + 1 )->type = PlaneTypeForNormal( p->normal() ); - - nummapplanes += 2; + mapplanes.resize( mapplanes.size() + 2 ); + plane_t& p = *( mapplanes.end() - 2 ); + plane_t& p2 = *( mapplanes.end() - 1 ); + p.plane = plane; + p2.plane = plane3_flipped( plane ); + p.type = p2.type = PlaneTypeForNormal( p.normal() ); // always put axial planes facing positive first - if ( p->type < ePlaneNonAxial ) { - if ( p->normal()[0] < 0 || p->normal()[1] < 0 || p->normal()[2] < 0 ) { + if ( p.type < ePlaneNonAxial ) { + if ( p.normal()[0] < 0 || p.normal()[1] < 0 || p.normal()[2] < 0 ) { // flip order - std::swap( *p, *( p + 1 ) ); + std::swap( p, p2 ); AddPlaneToHash( p ); - AddPlaneToHash( p + 1 ); - return nummapplanes - 1; + AddPlaneToHash( p2 ); + return mapplanes.size() - 1; } } AddPlaneToHash( p ); - AddPlaneToHash( p + 1 ); - return nummapplanes - 2; + AddPlaneToHash( p2 ); + return mapplanes.size() - 2; } @@ -338,28 +333,25 @@ int FindFloatPlane( const Plane3f& inplane, int numPoints, const Vector3 *points #ifdef USE_HASHING { - int i, j, hash, h; - int pidx; Plane3f plane( inplane ); - #if Q3MAP2_EXPERIMENTAL_SNAP_PLANE_FIX SnapPlaneImproved( plane, numPoints, points ); #else SnapPlane( plane ); #endif /* hash the plane */ - hash = ( PLANE_HASHES - 1 ) & (int) fabs( plane.dist() ); + const int hash = ( PLANE_HASHES - 1 ) & (int) fabs( plane.dist() ); /* search the border bins as well */ - for ( i = -1; i <= 1; i++ ) + for ( int i = -1; i <= 1; i++ ) { - h = ( hash + i ) & ( PLANE_HASHES - 1 ); - for ( pidx = planehash[ h ] - 1; pidx != -1; pidx = mapplanes[pidx].hash_chain - 1 ) + const int h = ( hash + i ) & ( PLANE_HASHES - 1 ); + for ( int pidx = planehash[ h ] - 1; pidx != -1; pidx = mapplanes[pidx].hash_chain - 1 ) { - plane_t *p = &mapplanes[pidx]; + const plane_t& p = mapplanes[pidx]; /* do standard plane compare */ - if ( !PlaneEqual( *p, plane ) ) { + if ( !PlaneEqual( p, plane ) ) { continue; } @@ -367,6 +359,7 @@ int FindFloatPlane( const Plane3f& inplane, int numPoints, const Vector3 *points //% return p - mapplanes; /* ydnar: test supplied points against this plane */ + int j; for ( j = 0; j < numPoints; j++ ) { // NOTE: When dist approaches 2^16, the resolution of 32 bit floating @@ -374,7 +367,7 @@ int FindFloatPlane( const Plane3f& inplane, int numPoints, const Vector3 *points // very small when world coordinates extend to 2^16. Making the // dot product here in 64 bit land will not really help the situation // because the error will already be carried in dist. - const double d = fabs( plane3_distance_to_point( p->plane, points[ j ] ) ); + const double d = fabs( plane3_distance_to_point( p.plane, points[ j ] ) ); if ( d != 0.0 && d >= distanceEpsilon ) { break; // Point is too far from plane. } @@ -382,7 +375,7 @@ int FindFloatPlane( const Plane3f& inplane, int numPoints, const Vector3 *points /* found a matching plane */ if ( j >= numPoints ) { - return p - mapplanes; + return pidx; } } } @@ -1842,7 +1835,7 @@ void LoadMapFile( char *filename, bool onlyLights, bool noCollapseGroups ){ Sys_FPrintf( SYS_VRB, "%9d boxbevels\n", c_boxbevels ); Sys_FPrintf( SYS_VRB, "%9d edgebevels\n", c_edgebevels ); Sys_FPrintf( SYS_VRB, "%9zu entities\n", entities.size() ); - Sys_FPrintf( SYS_VRB, "%9d planes\n", nummapplanes ); + Sys_FPrintf( SYS_VRB, "%9zu planes\n", mapplanes.size() ); Sys_Printf( "%9d areaportals\n", c_areaportals ); Sys_Printf( "Size: %5.0f, %5.0f, %5.0f to %5.0f, %5.0f, %5.0f\n", g_mapMinmax.mins[0], g_mapMinmax.mins[1], g_mapMinmax.mins[2], diff --git a/tools/quake3/q3map2/model.cpp b/tools/quake3/q3map2/model.cpp index e466a76d..763e1feb 100644 --- a/tools/quake3/q3map2/model.cpp +++ b/tools/quake3/q3map2/model.cpp @@ -707,9 +707,6 @@ void InsertModel( const char *name, int skin, int frame, const Matrix4& transfor /* walk triangle list */ for ( i = 0; i < ds->numIndexes; i += 3 ) { - /* overflow hack */ - AUTOEXPAND_BY_REALLOC( mapplanes, ( nummapplanes + 64 ) << 1, allocatedmapplanes, 1024 ); - /* make points */ for ( j = 0; j < 3; j++ ) { diff --git a/tools/quake3/q3map2/q3map2.h b/tools/quake3/q3map2/q3map2.h index 9f6c7e9c..8d0f0d8c 100644 --- a/tools/quake3/q3map2/q3map2.h +++ b/tools/quake3/q3map2/q3map2.h @@ -2104,9 +2104,7 @@ Q_EXTERN std::size_t mapEntityNum Q_ASSIGN( 0 ); Q_EXTERN int entitySourceBrushes; -Q_EXTERN plane_t *mapplanes Q_ASSIGN( NULL ); /* mapplanes[ num ^ 1 ] will always be the mirror or mapplanes[ num ] */ -Q_EXTERN int nummapplanes Q_ASSIGN( 0 ); /* nummapplanes will always be even */ -Q_EXTERN int allocatedmapplanes Q_ASSIGN( 0 ); +Q_EXTERN std::vector mapplanes; /* mapplanes[ num ^ 1 ] will always be the mirror or mapplanes[ num ] */ /* nummapplanes will always be even */ Q_EXTERN int numMapPatches; Q_EXTERN MinMax g_mapMinmax; diff --git a/tools/quake3/q3map2/tree.cpp b/tools/quake3/q3map2/tree.cpp index 3c19dfce..46acfc6b 100644 --- a/tools/quake3/q3map2/tree.cpp +++ b/tools/quake3/q3map2/tree.cpp @@ -117,11 +117,7 @@ void FreeTree( tree_t *tree ){ //=============================================================== void PrintTree_r( node_t *node, int depth ){ - int i; - plane_t *plane; - brush_t *bb; - - for ( i = 0 ; i < depth ; i++ ) + for ( int i = 0; i < depth; i++ ) Sys_Printf( " " ); if ( node->planenum == PLANENUM_LEAF ) { if ( !node->brushlist ) { @@ -129,17 +125,17 @@ void PrintTree_r( node_t *node, int depth ){ } else { - for ( bb = node->brushlist ; bb ; bb = bb->next ) + for ( const brush_t *bb = node->brushlist; bb; bb = bb->next ) Sys_Printf( "%d ", bb->original->brushNum ); Sys_Printf( "\n" ); } return; } - plane = &mapplanes[node->planenum]; + const plane_t& plane = mapplanes[node->planenum]; Sys_Printf( "#%d (%5.2f %5.2f %5.2f):%5.2f\n", node->planenum, - plane->normal()[0], plane->normal()[1], plane->normal()[2], - plane->dist() ); + plane.normal()[0], plane.normal()[1], plane.normal()[2], + plane.dist() ); PrintTree_r( node->children[0], depth + 1 ); PrintTree_r( node->children[1], depth + 1 ); } diff --git a/tools/quake3/q3map2/writebsp.cpp b/tools/quake3/q3map2/writebsp.cpp index 1c943102..5529ca4f 100644 --- a/tools/quake3/q3map2/writebsp.cpp +++ b/tools/quake3/q3map2/writebsp.cpp @@ -112,11 +112,10 @@ int EmitShader( const char *shader, int *contentFlags, int *surfaceFlags ){ void EmitPlanes( void ){ /* walk plane list */ - plane_t *mp = mapplanes; - for ( int i = 0; i < nummapplanes; i++, mp++ ) + for ( size_t i = 0; i < mapplanes.size(); ++i ) { AUTOEXPAND_BY_REALLOC_BSP( Planes, 1024 ); - bspPlanes[ numBSPPlanes ] = mp->plane; + bspPlanes[ numBSPPlanes ] = mapplanes[i].plane; numBSPPlanes++; }