@@ -6,17 +6,17 @@ void xPartitionReset()
66{
77}
88
9- _tagPartLink* PartitionGetFreeLink ()
9+ static _tagPartLink* PartitionGetFreeLink ()
1010{
1111 return (_tagPartLink*)xMemAllocSize (sizeof (_tagPartLink));
1212}
1313
14- void PartitionSpaceReset (_tagPartSpace* space)
14+ static void PartitionSpaceReset (_tagPartSpace* space)
1515{
1616 memset (space, 0 , sizeof (_tagPartSpace));
1717}
1818
19- void PartitionSpaceInsert (_tagPartSpace* space, void * data)
19+ static void PartitionSpaceInsert (_tagPartSpace* space, void * data)
2020{
2121 space->total ++;
2222 _tagPartLink* head = &space->head ;
@@ -31,10 +31,159 @@ void PartitionSpaceInsert(_tagPartSpace* space, void* data)
3131 tmp->next ->next = NULL ;
3232}
3333
34- // Need to figure out the correct order.
3534S32 xPartitionGetTrueIdx (_tagPartition* part, S32 x_spaces, S32 y_spaces, S32 z_spaces)
3635{
37- return part->total_x * z_spaces + part->total_x * y_spaces * part->total_z + x_spaces;
36+ S32 idx = (y_spaces * (part->total_x * part->total_z ));
37+ idx += part->total_x * z_spaces;
38+ idx += x_spaces;
39+ return idx;
40+ }
41+
42+ void xPartitionVolume (_tagPartition* part, xVolume* volume, S32 x_spaces, S32 y_spaces,
43+ S32 z_spaces)
44+ {
45+ memset (part, 0 , sizeof (_tagPartition));
46+ xBound* xb = volume->GetBound ();
47+ xBox* bb = &xb->box .box ;
48+ part->min = bb->lower ;
49+ part->max = bb->upper ;
50+
51+ F32 dx = part->max .x - part->min .x ;
52+ F32 dy = part->max .y - part->min .y ;
53+ F32 dz = part->max .z - part->min .z ;
54+
55+ part->total_x = x_spaces;
56+ part->total_y = y_spaces;
57+ part->total_z = z_spaces;
58+
59+ part->space_dim .x = dx / part->total_x ;
60+ part->space_dim .y = dy / part->total_y ;
61+ part->space_dim .z = dz / part->total_z ;
62+
63+ part->space = (_tagPartSpace*)xMemAlloc (
64+ gActiveHeap , sizeof (_tagPartSpace) * part->total_x * part->total_y * part->total_z , 0 );
65+
66+ for (S32 z = 0 ; z < z_spaces; z++)
67+ {
68+ for (S32 y = 0 ; y < y_spaces; y++)
69+ {
70+ for (S32 x = 0 ; x < x_spaces; x++)
71+ {
72+ S32 idx = xPartitionGetTrueIdx (part, x, y, z);
73+ PartitionSpaceReset (&part->space [idx]);
74+ }
75+ }
76+ }
77+
78+ PartitionSpaceReset (&part->global );
79+ }
80+
81+ void xPartitionWorld (_tagPartition* part, xEnv* env, S32 x_spaces, S32 y_spaces, S32 z_spaces)
82+ {
83+ memset (part, 0 , sizeof (_tagPartition));
84+ xBox* bb = (xBox*)xEnvGetBBox (env);
85+ part->min = bb->lower ;
86+ part->max = bb->upper ;
87+
88+ F32 dx = part->max .x - part->min .x ;
89+ F32 dy = part->max .y - part->min .y ;
90+ F32 dz = part->max .z - part->min .z ;
91+
92+ part->total_x = x_spaces;
93+ part->total_y = y_spaces;
94+ part->total_z = z_spaces;
95+
96+ part->space_dim .x = dx / part->total_x ;
97+ part->space_dim .y = dy / part->total_y ;
98+ part->space_dim .z = dz / part->total_z ;
99+
100+ part->space = (_tagPartSpace*)xMemAlloc (
101+ gActiveHeap , sizeof (_tagPartSpace) * part->total_x * part->total_y * part->total_z , 0 );
102+
103+ for (S32 z = 0 ; z < z_spaces; z++)
104+ {
105+ for (S32 y = 0 ; y < y_spaces; y++)
106+ {
107+ for (S32 x = 0 ; x < x_spaces; x++)
108+ {
109+ S32 idx = xPartitionGetTrueIdx (part, x, y, z);
110+ PartitionSpaceReset (&part->space [idx]);
111+ }
112+ }
113+ }
114+
115+ PartitionSpaceReset (&part->global );
116+ }
117+
118+ S32 xPartitionInsert (_tagPartition* part, void * insert_data, xVec3* insert_pos)
119+ {
120+ if (insert_pos->x < part->min .x || insert_pos->x > part->max .x || insert_pos->y < part->min .y ||
121+ insert_pos->y > part->max .y || insert_pos->z < part->min .z || insert_pos->z > part->max .z )
122+ {
123+ PartitionSpaceInsert (&part->global , insert_data);
124+ return -1 ;
125+ }
126+
127+ S32 idx = xPartitionGetTrueIdx (part, (insert_pos->x - part->min .x ) / part->space_dim .x ,
128+ (insert_pos->y - part->min .y ) / part->space_dim .y ,
129+ (insert_pos->z - part->min .z ) / part->space_dim .z );
130+ PartitionSpaceInsert (&part->space [idx], insert_data);
131+
132+ return idx;
133+ }
134+
135+ S32 xPartitionUpdate (_tagPartition* part, void * data, S32 old_idx, xVec3* current_pos)
136+ {
137+ S32 cur_idx;
138+
139+ if (current_pos->x < part->min .x || current_pos->x > part->max .x ||
140+ current_pos->y < part->min .y || current_pos->y > part->max .y ||
141+ current_pos->z < part->min .z || current_pos->z > part->max .z )
142+ {
143+ cur_idx = -1 ;
144+ }
145+ else
146+ {
147+ cur_idx = xPartitionGetTrueIdx (part, (current_pos->x - part->min .x ) / part->space_dim .x ,
148+ (current_pos->y - part->min .y ) / part->space_dim .y ,
149+ (current_pos->z - part->min .z ) / part->space_dim .z );
150+ }
151+
152+ if (old_idx == cur_idx)
153+ return cur_idx;
154+
155+ _tagPartSpace* src = old_idx == -1 ? &part->global : &part->space [old_idx];
156+ xPartitionSpaceMove (src, cur_idx == -1 ? &part->global : &part->space [cur_idx], (U32)data);
157+ return cur_idx;
158+ }
159+
160+ void xPartitionSpaceMove (_tagPartSpace* src, _tagPartSpace* dest, U32 data)
161+ {
162+ _tagPartLink* dest_lnk = &dest->head ;
163+ _tagPartLink* src_lnk;
164+ _tagPartLink* src_pre;
165+
166+ while (dest_lnk->next != NULL )
167+ {
168+ dest_lnk = dest_lnk->next ;
169+ }
170+
171+ src_lnk = &src->head ;
172+ src_pre = &src->head ;
173+
174+ while (src_lnk->data != (void *)data)
175+ {
176+ src_pre = src_lnk;
177+ src_lnk = src_lnk->next ;
178+ }
179+
180+ src_pre->next = src_lnk->next ;
181+
182+ dest_lnk->next = src_lnk;
183+ dest_lnk->next ->next = NULL ;
184+
185+ src->total --;
186+ dest->total ++;
38187}
39188
40189void xPartitionDump (_tagPartition*, char *)
0 commit comments