Skip to content

Commit

Permalink
Clustering fixed, AMAZING RESULTS (1 hr 16 min -> 1min14 for the stoo…
Browse files Browse the repository at this point in the history
…l 800x600 10bounces, 100 rays = factor 62 improvement !)
  • Loading branch information
alexblanche committed Mar 22, 2024
1 parent 32477ca commit ed3326f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
4 changes: 4 additions & 0 deletions include/scene/objects/bounding.hpp
Expand Up @@ -64,6 +64,10 @@ class bounding {
return children;
}

inline bool is_terminal_bd() const {
return is_terminal;
}

/* Auxiliary function to scene::find_closest_object_bounding :
Places the children of the bounding on the bounding_stack if the box is hit,
or determines the closest to the objects from the content if the bounding is terminal
Expand Down
10 changes: 5 additions & 5 deletions scene.txt
@@ -1,16 +1,16 @@
resolution width:800 height:600
camera position:(0, 4.5, 8) direction:(0, -0.35, -1) rightdir:(1, 0, 0) fov_width:2.5 distance:3
resolution width:1920 height:1080
camera position:(0, 5, 8) direction:(0, -0.4, -1) rightdir:(1, 0, 0) fov_width:2.5 distance:3
background_color 190 235 255
polygons_per_bounding 1
polygons_per_bounding 60

material white_light (color:(255, 255, 255) emitted_color:(255, 255, 255) reflectivity:0 emission:2 specular_p:0 reflects_color:false transparency:0 scattering:0 refraction_index:1)
# material red_light (color:(255, 255, 255) emitted_color:(255, 0, 0) reflectivity:0 emission:5 specular_p:0 reflects_color:false transparency:0 scattering:0 refraction_index:1)
# material blue_light (color:(255, 255, 255) emitted_color:(0, 0, 255) reflectivity:0 emission:5 specular_p:0 reflects_color:false transparency:0 scattering:0 refraction_index:1)
# material green_light (color:(255, 255, 255) emitted_color:(0, 255, 0) reflectivity:0 emission:5 specular_p:0 reflects_color:false transparency:0 scattering:0 refraction_index:1)
# material yellow_light (color:(255, 255, 255) emitted_color:(255, 255, 0) reflectivity:0 emission:5 specular_p:0 reflects_color:false transparency:0 scattering:0 refraction_index:1)

# load_texture wood_floor ../../../raytracer_project/texture_assets/parquet.bmp
load_texture wood_floor ../../../raytracer_project/texture_assets/stripes.bmp
load_texture wood_floor ../../../raytracer_project/texture_assets/parquet.bmp
# load_texture wood_floor ../../../raytracer_project/texture_assets/stripes.bmp
load_texture stool_wood ../../../raytracer_project/3d_models/stool/Textures/wooden_stool_texture(F).bmp
material Wooden_stool_texture (color:(255, 255, 255) emitted_color:(0, 0, 0) reflectivity:0.8 emission:0 specular_p:0.2 reflects_color:false transparency:0 scattering:0 refraction_index:1)

Expand Down
31 changes: 23 additions & 8 deletions src/auxiliary/clustering.cpp
Expand Up @@ -196,10 +196,14 @@ const bounding* create_bounding_hierarchy(const std::vector<const object*>& cont

/* Splitting the objects into groups of polygons_per_bounding polygons (on average) */
const unsigned int k = 1 + content.size() / polygons_per_bounding;

// printf("1 Calling k_means: %u elements, k = %u\n", content.size(), k);
const std::vector<std::vector<element>> groups = k_means(get_element_vector(content), k);

/** Creating the hierarchy **/

// unsigned int cpt = 0;

/* Creating terminal nodes */
std::vector<const bounding*> term_nodes;
for (unsigned int i = 0; i < k; i++) {
Expand All @@ -210,28 +214,37 @@ const bounding* create_bounding_hierarchy(const std::vector<const object*>& cont
else {
term_nodes.push_back(containing_objects(get_object_vector(groups.at(i))));
}
// cpt ++;
}
}
// printf("1 non-empty nodes: %u, empty nodes: %u\n", cpt, k - cpt);

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

printf("nodes.size() = %u\n", nodes.size());
// 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;

// printf("2 Calling k_means: %u elements, k = %u\n", nodes.size(), k);
const std::vector<std::vector<element>> groups = k_means(nodes, k);

std::vector<const bounding*> new_bd_nodes;

// cpt = 0;
for (unsigned int i = 0; i < k; i++) {

if (groups.at(i).size() != 0) {
new_bd_nodes.push_back(containing_bounding_any(get_bounding_vector(groups.at(i))));
// cpt ++;
}
}
// printf("2 non-empty nodes: %u, empty nodes: %u\n", cpt, k - cpt);

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

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

return containing_bounding_any(get_bounding_vector(nodes));
Expand All @@ -251,7 +264,7 @@ void display_hierarchy_properties(const bounding* bd0) {
printf("Level 0: arity = %u\n", bd0->get_children().size());

while (not bds.empty()) {
printf("bds.size() = %u\n", bds.size());
// 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. */
Expand All @@ -264,7 +277,10 @@ void display_hierarchy_properties(const bounding* bd0) {
while(not bds.empty()) {
const bounding* bd = bds.top();
bds.pop();
if (bd->get_content().size() != 0) {
if (bd->is_terminal_bd()) {
terminal_nodes ++;
}
else {
unsigned int arity = bd->get_children().size();
if (arity > max) {max = arity;}
if (arity < min) {min = arity;}
Expand All @@ -273,9 +289,6 @@ void display_hierarchy_properties(const bounding* bd0) {
next_bds.push(bd->get_children().at(j));
}
}
else {
terminal_nodes ++;
}
}

/* Displaying the statistics */
Expand All @@ -292,6 +305,8 @@ void display_hierarchy_properties(const bounding* bd0) {
bds.push(next_bds.top());
next_bds.pop();
}

level++;
}
printf("===============================================================================\n");
}
5 changes: 5 additions & 0 deletions src/file_readers/obj_parser.cpp
Expand Up @@ -87,6 +87,10 @@ bool parse_obj_file(const char* file_name, std::vector<const object*>& obj_set,
is placed in a box that is added to the children vector */
if (bounding_enabled && strcmp(s, "o") == 0 && content.size() != 0) {

// First method: put the whole group in one bounding
// const bounding* bd = containing_objects(content);

// Second method: create a bounding hierarchy containing all the nodes
/* 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);
Expand Down Expand Up @@ -304,6 +308,7 @@ bool parse_obj_file(const char* file_name, std::vector<const object*>& obj_set,

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

0 comments on commit ed3326f

Please sign in to comment.