Commit b552156a authored by Stephen D's avatar Stephen D
Browse files

Engine changes for readability and possibly 3D in the future

parent 48878d1c
......@@ -2,4 +2,4 @@ galaxysim: main.c qdbmp.c qdbmp.h engine.c engine.h
gcc -o galaxysim main.c qdbmp.c engine.c -lm -fopenmp -lpng
collide: collide.c
gcc -o collide collide.c
gcc -o collide collide.c -lm
......@@ -22,7 +22,7 @@ void addToTree(struct node_t *tree, RigidMass_t *masses, int n, int rlev)
{
rlev++;
}
RigidMass_t *masses_topleft;
/*RigidMass_t *masses_topleft;
int n_topleft = 0;
RigidMass_t *masses_topright;
......@@ -36,7 +36,14 @@ void addToTree(struct node_t *tree, RigidMass_t *masses, int n, int rlev)
masses_topleft = malloc(n * sizeof(RigidMass_t));
masses_topright = malloc(n * sizeof(RigidMass_t));
masses_botleft = malloc(n * sizeof(RigidMass_t));
masses_botright = malloc(n * sizeof(RigidMass_t));
masses_botright = malloc(n * sizeof(RigidMass_t));*/
RigidMass_t *massQuadrants[4]; //Array of Array of massquadrants
for(int i = 0; i < 4; i++)
{
massQuadrants[i] = malloc(sizeof(RigidMass_t) * n);
}
int massQuadCounts[] = {0, 0, 0, 0};
tree->n += n;
......@@ -65,12 +72,12 @@ void addToTree(struct node_t *tree, RigidMass_t *masses, int n, int rlev)
if(curY < centerY)
{
//Top left
masses_topleft[n_topleft++] = masses[i];
massQuadrants[0][massQuadCounts[0]++] = masses[i];
}
else
{
//Bot left
masses_botleft[n_botleft++] = masses[i];
massQuadrants[2][massQuadCounts[2]++] = masses[i];
}
}
else
......@@ -78,12 +85,12 @@ void addToTree(struct node_t *tree, RigidMass_t *masses, int n, int rlev)
if(curY < centerY)
{
//Top right
masses_topright[n_topright++] = masses[i];
massQuadrants[1][massQuadCounts[1]++] = masses[i];
}
else
{
//Bot right
masses_botright[n_botright++] = masses[i];
massQuadrants[3][massQuadCounts[3]++] = masses[i];
}
}
}
......@@ -92,45 +99,25 @@ void addToTree(struct node_t *tree, RigidMass_t *masses, int n, int rlev)
tree->CoM_y = weightedCenterY / totalMass;
tree->mass = totalMass;
//v printf("Distribution: %d, %d, %d, %d \r\n", n_topleft, n_topright, n_botleft, n_botright);
//printf("%d\r\n", rlev);
//recursion
if(rlev < 100) //can't go more than 100 levels deep.
{
if(n_topleft > 1)
{
// printf("Down an inception level\r\n");
tree->nodes[0] = malloc(sizeof(struct node_t));
initTree(tree->nodes[0], tree->xmin, centerX, tree->ymin, centerY);
addToTree(tree->nodes[0], masses_topleft, n_topleft, rlev + 1);
}
if(n_topright > 1)
{
tree->nodes[1] = malloc(sizeof(struct node_t));
initTree(tree->nodes[1], centerX, tree->xmax, tree->ymin, centerY);
addToTree(tree->nodes[1], masses_topright, n_topright, rlev + 1);
}
if(n_botleft > 1) //seems broken
{
tree->nodes[2] = malloc(sizeof(struct node_t));
initTree(tree->nodes[2], tree->xmin, centerX, centerY, tree->ymax);
addToTree(tree->nodes[2], masses_botleft, n_botleft, rlev + 1);
}
if(n_botright > 1)
for(int i = 0; i < 4; i++)
{
tree->nodes[3] = malloc(sizeof(struct node_t));
initTree(tree->nodes[3], centerX, tree->xmax, centerY, tree->ymax);
addToTree(tree->nodes[3], masses_botright, n_botright, rlev + 1);
if(massQuadCounts[i] > 1)
{
tree->nodes[i] = malloc(sizeof(struct node_t)); //This gets freed when the tree is deleted
double treeXmin = i % 2 == 0 ? tree->xmin : centerX;
double treeXmax = i % 2 == 0 ? centerX : tree->xmax;
double treeYmin = i < 2 ? tree->ymin : centerY;
double treeYmax = i < 2 ? centerY : tree->ymax;
initTree(tree->nodes[i], treeXmin, treeXmax, treeYmin, treeYmax);
addToTree(tree->nodes[i], massQuadrants[i], massQuadCounts[i], rlev + 1);
}
//Free up the quadrant memory
free(massQuadrants[i]);
}
}
free(masses_topleft);
free(masses_topright);
free(masses_botleft);
free(masses_botright);
}
void initTree(struct node_t *tree, double xmin, double xmax, double ymin, double ymax)
......@@ -169,6 +156,7 @@ void barnesIterate(RigidMass_t *masses, int n, int w, int h)
struct node_t tree;
//quad tree encapsulates an area of 5w * 5h, centered on the screen view
initTree(&tree, -2*w, 3*w, -2*h, 3*h);
addToTree(&tree, masses, n, 0);
printf("Tree done. Calculating forces...\r\n");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment