From 8018b56100ddee033af873a5f1231c18b77e7fc3 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Mon, 29 Oct 2018 09:47:49 -0500 Subject: [PATCH] Updating structure of RGB color data. --- include/openmc/plot.h | 19 +++----- src/plot.cpp | 108 ++++++++++++++++++------------------------ 2 files changed, 54 insertions(+), 73 deletions(-) diff --git a/include/openmc/plot.h b/include/openmc/plot.h index 69d8c1cf8..0c5a24484 100644 --- a/include/openmc/plot.h +++ b/include/openmc/plot.h @@ -29,7 +29,7 @@ extern int n_plots; //!< number of plots in openmc run class Plot; extern std::vector plots; //!< Plot instance container -typedef std::vector>> ImageData; +typedef std::vector>> ImageData; enum class plot_type { slice = 1, @@ -51,10 +51,8 @@ enum class plot_color_by { // RGBColor holds color information for plotted objects //=============================================================================== -struct RGBColor { - int rgb[3]; //!< RGB color values -}; - +typedef std::array RGBColor; + //=============================================================================== // Plot class //=============================================================================== @@ -66,9 +64,6 @@ public: // Constructor Plot(pugi::xml_node plot); - // Destructor - ~Plot(); - // Methods private: void set_id(); @@ -98,7 +93,7 @@ public: int index_meshlines_mesh; //!< Index of the mesh to draw on the plot RGBColor meshlines_color; //!< Color of meshlines on the plot RGBColor not_found; //!< Plot background color - std::vector colors; //!< Plot colors + std::vector colors; //!< Plot colors std::string path_plot; //!< Plot output filename pugi::xml_node _plot_node; }; @@ -111,20 +106,20 @@ public: //! \param[in] plot object //! \param[out] image data associated with the plot object void draw_mesh_lines(Plot* pl, - std::vector< std::vector< std::vector > > &data); + ImageData &data); //! Write a ppm image to file using a plot object's image data //! \param[in] plot object //! \param[out] image data associated with the plot object void output_ppm(Plot* pl, - const std::vector< std::vector< std::vector > > &data); + const ImageData &data); //! Get the rgb color for a given particle position in a plot //! \param[in] particle with position for current pixel //! \param[in] plot object //! \param[out] rgb color //! \param[out] cell or material id for particle position -void position_rgb(Particle* p, Plot* pl, int rgb[3], int &id); +void position_rgb(Particle* p, Plot* pl, RGBColor &rgb, int &id); //! Initialize a voxel file diff --git a/src/plot.cpp b/src/plot.cpp index 12aab9242..89bd936d6 100644 --- a/src/plot.cpp +++ b/src/plot.cpp @@ -29,8 +29,8 @@ int n_plots; std::vector plots; -const int WHITE[3] = {255, 255, 255}; -const int NULLRGB[3] = {0, 0, 0}; +const RGBColor WHITE = {255, 255, 255}; +const RGBColor NULLRGB = {0, 0, 0}; //=============================================================================== // RUN_PLOT controls the logic for making one or many plots @@ -134,7 +134,7 @@ void create_ppm(Plot* pl) p->coord[0].universe = openmc_root_universe; // local variables - int rgb[3]; + RGBColor rgb; int id; for (int y = 0; y < height; y++) { p->coord[0].xyz[out_i] = xyz[out_i] - out_pixel * y; @@ -266,9 +266,9 @@ Plot::set_bg_color() } if (node_word_count(_plot_node, "background") == 3) { bg_rgb = get_node_array(_plot_node, "background"); - not_found.rgb[RED] = bg_rgb[RED]; - not_found.rgb[GREEN] = bg_rgb[GREEN]; - not_found.rgb[BLUE] = bg_rgb[BLUE]; + not_found[RED] = bg_rgb[RED]; + not_found[GREEN] = bg_rgb[GREEN]; + not_found[BLUE] = bg_rgb[BLUE]; } else { std::stringstream err_msg; err_msg << "Bad background RGB in plot " @@ -277,9 +277,9 @@ Plot::set_bg_color() } } else { // default to a white background - not_found.rgb[RED] = 255; - not_found.rgb[GREEN] = 255; - not_found.rgb[BLUE] = 255; + not_found[RED] = 255; + not_found[GREEN] = 255; + not_found[BLUE] = 255; } } @@ -383,20 +383,20 @@ Plot::set_default_colors() } if ("cell" == pl_color_by) { color_by = plot_color_by::cells; + colors.resize(n_cells); for(int i = 0; i < n_cells; i++) { - colors.push_back(new RGBColor()); - colors[i]->rgb[RED] = int(prn()*255); - colors[i]->rgb[GREEN] = int(prn()*255); - colors[i]->rgb[BLUE] = int(prn()*255); + colors[i][RED] = int(prn()*255); + colors[i][GREEN] = int(prn()*255); + colors[i][BLUE] = int(prn()*255); } } else if("material" == pl_color_by) { color_by = plot_color_by::mats; + colors.resize(n_materials); for(int i = 0; i < materials.size(); i++) { - colors.push_back(new RGBColor()); - colors[i]->rgb[RED] = int(prn()*255); - colors[i]->rgb[GREEN] = int(prn()*255); - colors[i]->rgb[BLUE] = int(prn()*255); + colors[i][RED] = int(prn()*255); + colors[i][GREEN] = int(prn()*255); + colors[i][BLUE] = int(prn()*255); } } else { std::stringstream err_msg; @@ -448,9 +448,9 @@ Plot::set_user_colors() if (cell_map.find(col_id) != cell_map.end()) { col_id = cell_map[col_id]; cell_rgb = get_node_array(cn, "rgb"); - colors[col_id]->rgb[RED] = cell_rgb[RED]; - colors[col_id]->rgb[GREEN] = cell_rgb[GREEN]; - colors[col_id]->rgb[BLUE] = cell_rgb[BLUE]; + colors[col_id][RED] = cell_rgb[RED]; + colors[col_id][GREEN] = cell_rgb[GREEN]; + colors[col_id][BLUE] = cell_rgb[BLUE]; } else { std::stringstream err_msg; err_msg << "Could not find cell " << col_id @@ -462,9 +462,9 @@ Plot::set_user_colors() if (material_map.find(col_id) != material_map.end()) { col_id = material_map[col_id]; mat_rgb = get_node_array(cn, "rgb"); - colors[col_id]->rgb[RED] = mat_rgb[RED]; - colors[col_id]->rgb[GREEN] = mat_rgb[GREEN]; - colors[col_id]->rgb[BLUE] = mat_rgb[BLUE]; + colors[col_id][RED] = mat_rgb[RED]; + colors[col_id][GREEN] = mat_rgb[GREEN]; + colors[col_id][BLUE] = mat_rgb[BLUE]; } else { std::stringstream err_msg; err_msg << "Could not find material " << col_id @@ -527,13 +527,13 @@ Plot::set_meshlines() fatal_error(err_msg); } ml_rgb = get_node_array(meshlines_node, "color"); - meshlines_color.rgb[0] = ml_rgb[0]; - meshlines_color.rgb[1] = ml_rgb[1]; - meshlines_color.rgb[2] = ml_rgb[2]; + meshlines_color[0] = ml_rgb[0]; + meshlines_color[1] = ml_rgb[1]; + meshlines_color[2] = ml_rgb[2]; } else { - meshlines_color.rgb[0] = 0; - meshlines_color.rgb[1] = 0; - meshlines_color.rgb[2] = 0; + meshlines_color[0] = 0; + meshlines_color[1] = 0; + meshlines_color[2] = 0; } // Set mesh based on type @@ -660,13 +660,13 @@ Plot::set_mask() if (std::find(iarray.begin(), iarray.end(), j) == iarray.end()) { if (check_for_node(mask_node, "background")) { std::vector bg_rgb = get_node_array(mask_node, "background"); - colors[j]->rgb[RED] = bg_rgb[RED]; - colors[j]->rgb[GREEN] = bg_rgb[GREEN]; - colors[j]->rgb[BLUE] = bg_rgb[BLUE]; + colors[j][RED] = bg_rgb[RED]; + colors[j][GREEN] = bg_rgb[GREEN]; + colors[j][BLUE] = bg_rgb[BLUE]; } else { - colors[j]->rgb[RED] = 255; - colors[j]->rgb[GREEN] = 255; - colors[j]->rgb[BLUE] = 255; + colors[j][RED] = 255; + colors[j][GREEN] = 255; + colors[j][BLUE] = 255; } } } @@ -698,19 +698,12 @@ index_meshlines_mesh(-1) _plot_node = pugi::xml_node(); // set to null node after construction } // End Plot constructor -Plot::~Plot() { - // cleanup color pointers - for (auto c : colors) { - if (c) {delete c;} - } -} - //=============================================================================== // POSITION_RGB computes the red/green/blue values for a given plot with the // current particle's position //=============================================================================== -void position_rgb(Particle* p, Plot* pl, int rgb[3], int &id) +void position_rgb(Particle* p, Plot* pl, RGBColor &rgb, int &id) { p->n_coord = 1; @@ -725,9 +718,7 @@ void position_rgb(Particle* p, Plot* pl, int rgb[3], int &id) if (!found_cell) { // If no cell, revert to default color - std::copy(pl->not_found.rgb, - pl->not_found.rgb + 3, - rgb); + rgb = pl->not_found; id = -1; } else { if (plot_color_by::mats == pl->color_by) { @@ -735,29 +726,23 @@ void position_rgb(Particle* p, Plot* pl, int rgb[3], int &id) Cell* c = cells[p->coord[j].cell]; if (c->type_ == FILL_UNIVERSE) { // If we stopped on a middle universe level, treat as if not found - std::copy(pl->not_found.rgb, - pl->not_found.rgb + 3, - rgb); + rgb = pl->not_found; id = -1; } else if (p->material == MATERIAL_VOID) { // By default, color void cells white - std::copy(WHITE, WHITE+3, rgb); + rgb = WHITE; id = -1; } else { - std::copy(pl->colors[p->material - 1]->rgb, - pl->colors[p->material - 1]->rgb + 3, - rgb); + rgb = pl->colors[p->material - 1]; id = materials[p->material - 1]->id_; } } else if (plot_color_by::cells == pl->color_by) { // Assign color based on cell - std::copy(pl->colors[p->coord[j].cell]->rgb, - pl->colors[p->coord[j].cell]->rgb + 3, - rgb); + rgb = pl->colors[p->coord[j].cell]; id = cells[p->coord[j].cell]->id_; } else { - std::copy(NULLRGB, NULLRGB+3, rgb); + rgb = NULLRGB; id = -1; } @@ -806,9 +791,9 @@ void output_ppm(Plot* pl, const ImageData &data) void draw_mesh_lines(Plot *pl, ImageData &data) { std::vector rgb; rgb.resize(3); - rgb[RED] = pl->meshlines_color.rgb[RED]; - rgb[GREEN] = pl->meshlines_color.rgb[GREEN]; - rgb[BLUE] = pl->meshlines_color.rgb[BLUE]; + rgb[RED] = pl->meshlines_color[RED]; + rgb[GREEN] = pl->meshlines_color[GREEN]; + rgb[BLUE] = pl->meshlines_color[BLUE]; int outer, inner; switch(pl->basis) { @@ -973,7 +958,8 @@ void create_voxel(Plot *pl) int data[pl->pixels[1]][pl->pixels[2]]; - int rgb[3], id; + RGBColor rgb; + int id; for (int x = 0; x < pl->pixels[0]; x++) { // TODO: progress bar here for (int y = 0; y < pl->pixels[1]; y++) {