Skip to content

Commit

Permalink
Display a bounding hierarchy's properties, k_means seems bugged (when…
Browse files Browse the repository at this point in the history
… elements are boundings, there is always only one non-empty group at the end)
  • Loading branch information
alexblanche committed Mar 22, 2024
1 parent eb3e7c0 commit 32477ca
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 7 deletions.
8 changes: 7 additions & 1 deletion include/auxiliary/clustering.hpp
Expand Up @@ -50,4 +50,10 @@ std::vector<std::vector<element>> k_means(const std::vector<element>& elts, cons
/* Returns a bounding* containing the objects of content, split into a hierarchy of boundings if their number
exceeds MIN_NUMBER_OF_POLYGONS_FOR_BOX */
const bounding* create_bounding_hierarchy(const std::vector<const object*>& content,
const unsigned int polygons_per_bounding);
const unsigned int polygons_per_bounding);


/** Tests **/

/* Displays the depth of the hierarchy, as well as the minimum, maximum and average arity of each depth */
void display_hierarchy_properties(const bounding* bd);
65 changes: 65 additions & 0 deletions src/auxiliary/clustering.cpp
Expand Up @@ -4,6 +4,8 @@
#include "scene/objects/object.hpp"
#include "scene/objects/bounding.hpp"

#include <stack>

#include<limits>
numeric_limits<double> realclu;
const double infinity = realclu.infinity();
Expand Down Expand Up @@ -213,7 +215,10 @@ const bounding* create_bounding_hierarchy(const std::vector<const object*>& cont

std::vector<element> nodes = get_element_vector(term_nodes);

printf("nodes.size() = %u\n", nodes.size());

while (nodes.size() > CARDINAL_OF_BOX_GROUP) {

const unsigned int k = 1 + nodes.size() / CARDINAL_OF_BOX_GROUP;
const std::vector<std::vector<element>> groups = k_means(nodes, k);

Expand All @@ -226,7 +231,67 @@ const bounding* create_bounding_hierarchy(const std::vector<const object*>& cont

nodes.clear();
nodes = get_element_vector(new_bd_nodes);
printf("nodes.size() = %u\n", nodes.size());
}

return containing_bounding_any(get_bounding_vector(nodes));
}


/** Test function **/

/* Displays the depth of the hierarchy, as well as the minimum, maximum and average arity of each depth */
void display_hierarchy_properties(const bounding* bd0) {

printf("============================= HIERARCHY STATISTICS =============================\n");

unsigned int level = 0;
std::stack<const bounding*> bds;
bds.push(bd0);
printf("Level 0: arity = %u\n", bd0->get_children().size());

while (not bds.empty()) {
printf("bds.size() = %u\n", bds.size());

/* Computing min, max and average arity of the nodes on the stack
If one node is terminal, its arity counts as zero. */
unsigned int terminal_nodes = 0;
unsigned int min = 4294967295;
unsigned int max = 0;
unsigned int total = 0;
const unsigned int number_of_nodes = bds.size();
std::stack<const bounding*> next_bds;
while(not bds.empty()) {
const bounding* bd = bds.top();
bds.pop();
if (bd->get_content().size() != 0) {
unsigned int arity = bd->get_children().size();
if (arity > max) {max = arity;}
if (arity < min) {min = arity;}
total += arity;
for (unsigned int j = 0; j < arity; j++) {
next_bds.push(bd->get_children().at(j));
}
}
else {
terminal_nodes ++;
}
}

/* Displaying the statistics */
if (terminal_nodes == number_of_nodes) {
printf("Level %u: nodes: %u, all terminal\n", level, number_of_nodes);
}
else {
printf("Level %u: nodes: %u, terminal: %u, minimum arity: %u, maximum: %u, average: %lf\n",
level, number_of_nodes, terminal_nodes, min, max, ((double) total) / number_of_nodes);
}

// bds = next_bds;
while (not next_bds.empty()) {
bds.push(next_bds.top());
next_bds.pop();
}
}
printf("===============================================================================\n");
}
15 changes: 9 additions & 6 deletions src/file_readers/obj_parser.cpp
Expand Up @@ -89,6 +89,7 @@ bool parse_obj_file(const char* file_name, std::vector<const object*>& obj_set,

/* Heuristic: each group is a depth 1 node in the global bounding box hierarchy */
const bounding* bd = create_bounding_hierarchy(content, polygons_per_bounding);
display_hierarchy_properties(bd);
children.push_back(bd);
content.clear();
}
Expand Down Expand Up @@ -300,15 +301,11 @@ bool parse_obj_file(const char* file_name, std::vector<const object*>& obj_set,
}

fclose(file);

printf("\r%s successfully loaded:\n", file_name);
printf("%u vertices, %u polygons (%u triangles, %u quads)\n",
number_of_vertices, number_of_polygons, number_of_triangles, number_of_quads);
fflush(stdout);


if (bounding_enabled) {
/* Placing the last group into a bounding */
const bounding* bd = create_bounding_hierarchy(content, polygons_per_bounding);
display_hierarchy_properties(bd);
children.push_back(bd);

/* Setting the final bounding */
Expand All @@ -321,6 +318,12 @@ bool parse_obj_file(const char* file_name, std::vector<const object*>& obj_set,
}
}

printf("\r%s successfully loaded:\n", file_name);
printf("%u vertices, %u polygons (%u triangles, %u quads)\n",
number_of_vertices, number_of_polygons, number_of_triangles, number_of_quads);
fflush(stdout);


return true;
}

0 comments on commit 32477ca

Please sign in to comment.