shithub: nanobsp

Download patch

ref: 9322adf2b212245e8f742b1fbbd4ea2c87f58364
parent: 5052250318e1ac0e85b4f9e8773d0334d630e615
author: Andrew Apted <ajapted@gmail.com>
date: Fri Dec 15 11:20:45 EST 2023

removed `bbox` field of nanode_t, just compute it while we copy stuff.

--- a/nano_bsp.c
+++ b/nano_bsp.c
@@ -70,9 +70,6 @@
 	// partition line (start coord, delta to end)
 	fixed_t  x, y, dx, dy;
 
-	// bounding box for this node / leaf    [ TODO: REMOVE ]
-	fixed_t  bbox[4];
-
 	// right and left children
 	struct Nanode * right;
 	struct Nanode * left;
@@ -116,10 +113,6 @@
 
 	printf ("%snode %p\n", spaces, N);
 
-	printf ("%sbbox (%d %d) .. (%d %d)\n", spaces,
-		N->bbox[BOXLEFT] >> 16, N->bbox[BOXBOTTOM] >> 16,
-		N->bbox[BOXRIGHT] >> 16, N->bbox[BOXTOP] >> 16);
-
 	if (N->segs == NULL)
 	{
 		printf ("%spartition (%d %d) --> (%d %d)\n", spaces,
@@ -168,35 +161,30 @@
 {
 	// Note: not using M_AddToBox() here, because it is broken!
 
-	bbox[BOXLEFT  ] = INT_MAX; bbox[BOXRIGHT ] = INT_MIN;
-	bbox[BOXBOTTOM] = INT_MAX; bbox[BOXTOP   ] = INT_MIN;
+	bbox[BOXLEFT]   = INT_MAX; bbox[BOXRIGHT]  = INT_MIN;
+	bbox[BOXBOTTOM] = INT_MAX; bbox[BOXTOP]    = INT_MIN;
 
 	seg_t * S;
 	for (S = soup ; S != NULL ; S = S->next)
 	{
-		bbox[BOXLEFT  ] = MIN (bbox[BOXLEFT  ], S->v1->x);
-		bbox[BOXLEFT  ] = MIN (bbox[BOXLEFT  ], S->v2->x);
-
+		bbox[BOXLEFT]   = MIN (bbox[BOXLEFT],   S->v1->x);
+		bbox[BOXLEFT]   = MIN (bbox[BOXLEFT],   S->v2->x);
 		bbox[BOXBOTTOM] = MIN (bbox[BOXBOTTOM], S->v1->y);
 		bbox[BOXBOTTOM] = MIN (bbox[BOXBOTTOM], S->v2->y);
 
-		bbox[BOXRIGHT ] = MAX (bbox[BOXRIGHT ], S->v1->x);
-		bbox[BOXRIGHT ] = MAX (bbox[BOXRIGHT ], S->v2->x);
-
-		bbox[BOXTOP   ] = MAX (bbox[BOXTOP   ], S->v1->y);
-		bbox[BOXTOP   ] = MAX (bbox[BOXTOP   ], S->v2->y);
+		bbox[BOXRIGHT]  = MAX (bbox[BOXRIGHT],  S->v1->x);
+		bbox[BOXRIGHT]  = MAX (bbox[BOXRIGHT],  S->v2->x);
+		bbox[BOXTOP]    = MAX (bbox[BOXTOP],    S->v1->y);
+		bbox[BOXTOP]    = MAX (bbox[BOXTOP],    S->v2->y);
 	}
 }
 
-void BSP_MergeBounds (nanode_t * node)
+void BSP_MergeBounds (fixed_t * out, fixed_t * box1, fixed_t * box2)
 {
-	fixed_t * L_bbox = node->left ->bbox;
-	fixed_t * R_bbox = node->right->bbox;
-
-	node->bbox[BOXLEFT  ] = MIN (L_bbox[BOXLEFT  ], R_bbox[BOXLEFT  ]);
-	node->bbox[BOXBOTTOM] = MIN (L_bbox[BOXBOTTOM], R_bbox[BOXBOTTOM]);
-	node->bbox[BOXRIGHT ] = MAX (L_bbox[BOXRIGHT ], R_bbox[BOXRIGHT ]);
-	node->bbox[BOXTOP   ] = MAX (L_bbox[BOXTOP   ], R_bbox[BOXTOP   ]);
+	out[BOXLEFT]   = MIN (box1[BOXLEFT],   box2[BOXLEFT]);
+	out[BOXBOTTOM] = MIN (box1[BOXBOTTOM], box2[BOXBOTTOM]);
+	out[BOXRIGHT]  = MAX (box1[BOXRIGHT],  box2[BOXRIGHT]);
+	out[BOXTOP]    = MAX (box1[BOXTOP],    box2[BOXTOP]);
 }
 
 void BSP_SegForLineSide (int i, int side, seg_t ** list_var)
@@ -248,8 +236,6 @@
 
 	node->segs = soup;
 
-	BSP_BoundingBox (soup, node->bbox);
-
 	return node;
 }
 
@@ -695,8 +681,6 @@
 	N->right = BSP_SubdivideSegs (rights);
 	N->left  = BSP_SubdivideSegs (lefts);
 
-	BSP_MergeBounds (N);
-
 	return N;
 }
 
@@ -754,15 +738,15 @@
 	}
 }
 
-unsigned int BSP_WriteNode (nanode_t * N)
+unsigned int BSP_WriteNode (nanode_t * N, fixed_t * bbox)
 {
 	unsigned int index = N->index;
 
 	if (N->segs != NULL)
+	{
 		index |= NF_SUBSECTOR;
 
-	if (N->segs != NULL)
-	{
+		BSP_BoundingBox (N->segs, bbox);
 		BSP_WriteSubsector (N);
 	}
 	else
@@ -779,12 +763,10 @@
 		{
 			nanode_t * child = (c == 0) ? N->right : N->left;
 
-			out->children[c] = BSP_WriteNode (child);
-
-			int k;
-			for (k = 0 ; k < 4 ; k++)
-				out->bbox[c][k] = child->bbox[k];
+			out->children[c] = BSP_WriteNode (child, out->bbox[c]);
 		}
+
+		BSP_MergeBounds (bbox, out->bbox[0], out->bbox[1]);
 	}
 
 	Z_Free (N);
@@ -815,6 +797,8 @@
 
 	nano_seg_index = 0;
 
+	fixed_t bbox[4];
+
 	// this also frees stuff as it goes
-	BSP_WriteNode (root);
+	BSP_WriteNode (root, bbox);
 }
--