• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

openmc-dev / openmc / 21991279157

13 Feb 2026 02:53PM UTC coverage: 81.82% (-0.06%) from 81.875%
21991279157

Pull #3805

github

web-flow
Merge 0a7a80411 into bcb939520
Pull Request #3805: Remove xtensor and xtl Dependencies

17242 of 24268 branches covered (71.05%)

Branch coverage included in aggregate %.

977 of 1013 new or added lines in 39 files covered. (96.45%)

404 existing lines in 8 files now uncovered.

57420 of 66983 relevant lines covered (85.72%)

45458907.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

70.95
/src/plot.cpp
1
#include "openmc/plot.h"
2

3
#include <algorithm>
4
#define _USE_MATH_DEFINES // to make M_PI declared in Intel and MSVC compilers
5
#include <cmath>
6
#include <cstdio>
7
#include <fstream>
8
#include <sstream>
9

10
#include "openmc/tensor.h"
11
#include <fmt/core.h>
12
#include <fmt/ostream.h>
13
#ifdef USE_LIBPNG
14
#include <png.h>
15
#endif
16

17
#include "openmc/cell.h"
18
#include "openmc/constants.h"
19
#include "openmc/container_util.h"
20
#include "openmc/dagmc.h"
21
#include "openmc/error.h"
22
#include "openmc/file_utils.h"
23
#include "openmc/geometry.h"
24
#include "openmc/hdf5_interface.h"
25
#include "openmc/material.h"
26
#include "openmc/mesh.h"
27
#include "openmc/message_passing.h"
28
#include "openmc/openmp_interface.h"
29
#include "openmc/output.h"
30
#include "openmc/particle.h"
31
#include "openmc/progress_bar.h"
32
#include "openmc/random_lcg.h"
33
#include "openmc/settings.h"
34
#include "openmc/simulation.h"
35
#include "openmc/string_utils.h"
36

37
namespace openmc {
38

39
//==============================================================================
40
// Constants
41
//==============================================================================
42

43
constexpr int PLOT_LEVEL_LOWEST {-1}; //!< lower bound on plot universe level
44
constexpr int32_t NOT_FOUND {-2};
45
constexpr int32_t OVERLAP {-3};
46

47
IdData::IdData(size_t h_res, size_t v_res) : data_({v_res, h_res, 3}, NOT_FOUND)
4,673✔
48
{}
4,673✔
49

50
void IdData::set_value(size_t y, size_t x, const GeometryState& p, int level)
35,206,718✔
51
{
52
  // set cell data
53
  if (p.n_coord() <= level) {
35,206,718!
54
    data_(y, x, 0) = NOT_FOUND;
×
55
    data_(y, x, 1) = NOT_FOUND;
×
56
  } else {
57
    data_(y, x, 0) = model::cells.at(p.coord(level).cell())->id_;
35,206,718✔
58
    data_(y, x, 1) = level == p.n_coord() - 1
70,413,436✔
59
                       ? p.cell_instance()
35,206,718!
60
                       : cell_instance_at_level(p, level);
×
61
  }
62

63
  // set material data
64
  Cell* c = model::cells.at(p.lowest_coord().cell()).get();
35,206,718✔
65
  if (p.material() == MATERIAL_VOID) {
35,206,718✔
66
    data_(y, x, 2) = MATERIAL_VOID;
27,102,344✔
67
    return;
27,102,344✔
68
  } else if (c->type_ == Fill::MATERIAL) {
8,104,374!
69
    Material* m = model::materials.at(p.material()).get();
8,104,374✔
70
    data_(y, x, 2) = m->id_;
8,104,374✔
71
  }
72
}
73

74
void IdData::set_overlap(size_t y, size_t x)
340,280✔
75
{
76
  for (size_t k = 0; k < data_.shape(2); ++k)
1,361,120✔
77
    data_(y, x, k) = OVERLAP;
1,020,840✔
78
}
340,280✔
79

80
PropertyData::PropertyData(size_t h_res, size_t v_res)
10✔
81
  : data_({v_res, h_res, 2}, NOT_FOUND)
10✔
82
{}
10✔
83

84
void PropertyData::set_value(
90✔
85
  size_t y, size_t x, const GeometryState& p, int level)
86
{
87
  Cell* c = model::cells.at(p.lowest_coord().cell()).get();
90✔
88
  data_(y, x, 0) = (p.sqrtkT() * p.sqrtkT()) / K_BOLTZMANN;
90✔
89
  if (c->type_ != Fill::UNIVERSE && p.material() != MATERIAL_VOID) {
90!
90
    Material* m = model::materials.at(p.material()).get();
90✔
91
    data_(y, x, 1) = m->density_gpcc_;
90✔
92
  }
93
}
90✔
94

95
void PropertyData::set_overlap(size_t y, size_t x)
×
96
{
97
  data_(y, x) = OVERLAP;
×
98
}
×
99

100
//==============================================================================
101
// Global variables
102
//==============================================================================
103

104
namespace model {
105

106
std::unordered_map<int, int> plot_map;
107
vector<std::unique_ptr<PlottableInterface>> plots;
108
uint64_t plotter_seed = 1;
109

110
} // namespace model
111

112
//==============================================================================
113
// RUN_PLOT controls the logic for making one or many plots
114
//==============================================================================
115

116
extern "C" int openmc_plot_geometry()
110✔
117
{
118

119
  for (auto& pl : model::plots) {
370✔
120
    write_message(5, "Processing plot {}: {}...", pl->id(), pl->path_plot());
260✔
121
    pl->create_output();
260✔
122
  }
123

124
  return 0;
110✔
125
}
126

127
void PlottableInterface::write_image(const ImageData& data) const
210✔
128
{
129
#ifdef USE_LIBPNG
130
  output_png(path_plot(), data);
210✔
131
#else
132
  output_ppm(path_plot(), data);
133
#endif
134
}
210✔
135

136
void Plot::create_output() const
180✔
137
{
138
  if (PlotType::slice == type_) {
180✔
139
    // create 2D image
140
    ImageData image = create_image();
130✔
141
    write_image(image);
130✔
142
  } else if (PlotType::voxel == type_) {
180!
143
    // create voxel file for 3D viewing
144
    create_voxel();
50✔
145
  }
146
}
180✔
147

148
void Plot::print_info() const
140✔
149
{
150
  // Plot type
151
  if (PlotType::slice == type_) {
140✔
152
    fmt::print("Plot Type: Slice\n");
110✔
153
  } else if (PlotType::voxel == type_) {
30!
154
    fmt::print("Plot Type: Voxel\n");
30✔
155
  }
156

157
  // Plot parameters
158
  fmt::print("Origin: {} {} {}\n", origin_[0], origin_[1], origin_[2]);
252✔
159

160
  if (PlotType::slice == type_) {
140✔
161
    fmt::print("Width: {:4} {:4}\n", width_[0], width_[1]);
220✔
162
  } else if (PlotType::voxel == type_) {
30!
163
    fmt::print("Width: {:4} {:4} {:4}\n", width_[0], width_[1], width_[2]);
60✔
164
  }
165

166
  if (PlotColorBy::cells == color_by_) {
140✔
167
    fmt::print("Coloring: Cells\n");
80✔
168
  } else if (PlotColorBy::mats == color_by_) {
60!
169
    fmt::print("Coloring: Materials\n");
60✔
170
  }
171

172
  if (PlotType::slice == type_) {
140✔
173
    switch (basis_) {
110!
174
    case PlotBasis::xy:
70✔
175
      fmt::print("Basis: XY\n");
56✔
176
      break;
70✔
177
    case PlotBasis::xz:
20✔
178
      fmt::print("Basis: XZ\n");
16✔
179
      break;
20✔
180
    case PlotBasis::yz:
20✔
181
      fmt::print("Basis: YZ\n");
16✔
182
      break;
20✔
183
    }
184
    fmt::print("Pixels: {} {}\n", pixels()[0], pixels()[1]);
220✔
185
  } else if (PlotType::voxel == type_) {
30!
186
    fmt::print("Voxels: {} {} {}\n", pixels()[0], pixels()[1], pixels()[2]);
60✔
187
  }
188
}
140✔
189

190
void read_plots_xml()
1,234✔
191
{
192
  // Check if plots.xml exists; this is only necessary when the plot runmode is
193
  // initiated. Otherwise, we want to read plots.xml because it may be called
194
  // later via the API. In that case, its ok for a plots.xml to not exist
195
  std::string filename = settings::path_input + "plots.xml";
1,234✔
196
  if (!file_exists(filename) && settings::run_mode == RunMode::PLOTTING) {
1,234!
197
    fatal_error(fmt::format("Plots XML file '{}' does not exist!", filename));
×
198
  }
199

200
  write_message("Reading plot XML file...", 5);
1,234✔
201

202
  // Parse plots.xml file
203
  pugi::xml_document doc;
1,234✔
204
  doc.load_file(filename.c_str());
1,234✔
205

206
  pugi::xml_node root = doc.document_element();
1,234✔
207

208
  read_plots_xml(root);
1,234✔
209
}
1,234✔
210

211
void read_plots_xml(pugi::xml_node root)
1,546✔
212
{
213
  for (auto node : root.children("plot")) {
2,300✔
214
    std::string plot_desc = "<auto>";
762✔
215
    if (check_for_node(node, "id")) {
762!
216
      plot_desc = get_node_value(node, "id", true);
762✔
217
    }
218

219
    if (check_for_node(node, "type")) {
762!
220
      std::string type_str = get_node_value(node, "type", true);
762✔
221
      if (type_str == "slice") {
762✔
222
        model::plots.emplace_back(
624✔
223
          std::make_unique<Plot>(node, Plot::PlotType::slice));
1,256✔
224
      } else if (type_str == "voxel") {
130✔
225
        model::plots.emplace_back(
50✔
226
          std::make_unique<Plot>(node, Plot::PlotType::voxel));
100✔
227
      } else if (type_str == "wireframe_raytrace") {
80✔
228
        model::plots.emplace_back(
50✔
229
          std::make_unique<WireframeRayTracePlot>(node));
100✔
230
      } else if (type_str == "solid_raytrace") {
30!
231
        model::plots.emplace_back(std::make_unique<SolidRayTracePlot>(node));
30✔
232
      } else {
UNCOV
233
        fatal_error(fmt::format(
×
234
          "Unsupported plot type '{}' in plot {}", type_str, plot_desc));
235
      }
236
      model::plot_map[model::plots.back()->id()] = model::plots.size() - 1;
754✔
237
    } else {
754✔
UNCOV
238
      fatal_error(fmt::format("Must specify plot type in plot {}", plot_desc));
×
239
    }
240
  }
754✔
241
}
1,538✔
242

243
void free_memory_plot()
7,507✔
244
{
245
  model::plots.clear();
7,507✔
246
  model::plot_map.clear();
7,507✔
247
}
7,507✔
248

249
// creates an image based on user input from a plots.xml <plot>
250
// specification in the PNG/PPM format
251
ImageData Plot::create_image() const
130✔
252
{
253
  size_t width = pixels()[0];
130✔
254
  size_t height = pixels()[1];
130✔
255

256
  ImageData data({width, height}, not_found_);
130✔
257

258
  // generate ids for the plot
259
  auto ids = get_map<IdData>();
130✔
260

261
  // assign colors
262
  for (size_t y = 0; y < height; y++) {
27,330✔
263
    for (size_t x = 0; x < width; x++) {
6,929,200✔
264
      int idx = color_by_ == PlotColorBy::cells ? 0 : 2;
6,902,000✔
265
      auto id = ids.data_(y, x, idx);
6,902,000✔
266
      // no setting needed if not found
267
      if (id == NOT_FOUND) {
6,902,000✔
268
        continue;
984,120✔
269
      }
270
      if (id == OVERLAP) {
5,943,560✔
271
        data(x, y) = overlap_color_;
25,680✔
272
        continue;
25,680✔
273
      }
274
      if (PlotColorBy::cells == color_by_) {
5,917,880✔
275
        data(x, y) = colors_[model::cell_map[id]];
2,737,880✔
276
      } else if (PlotColorBy::mats == color_by_) {
3,180,000!
277
        if (id == MATERIAL_VOID) {
3,180,000!
UNCOV
278
          data(x, y) = WHITE;
×
UNCOV
279
          continue;
×
280
        }
281
        data(x, y) = colors_[model::material_map[id]];
3,180,000✔
282
      } // color_by if-else
283
    }
284
  }
285

286
  // draw mesh lines if present
287
  if (index_meshlines_mesh_ >= 0) {
130✔
288
    draw_mesh_lines(data);
30✔
289
  }
290

291
  return data;
260✔
292
}
130✔
293

294
void PlottableInterface::set_id(pugi::xml_node plot_node)
762✔
295
{
296
  int id {C_NONE};
762✔
297
  if (check_for_node(plot_node, "id")) {
762!
298
    id = std::stoi(get_node_value(plot_node, "id"));
762✔
299
  }
300

301
  try {
302
    set_id(id);
762✔
303
  } catch (const std::runtime_error& e) {
×
UNCOV
304
    fatal_error(e.what());
×
UNCOV
305
  }
×
306
}
762✔
307

308
void PlottableInterface::set_id(int id)
772✔
309
{
310
  if (id < 0 && id != C_NONE) {
772!
UNCOV
311
    throw std::runtime_error {fmt::format("Invalid plot ID: {}", id)};
×
312
  }
313

314
  if (id == C_NONE) {
772✔
315
    id = 1;
10✔
316
    for (const auto& p : model::plots) {
20✔
317
      id = std::max(id, p->id() + 1);
10✔
318
    }
319
  }
320

321
  if (id_ == id)
772!
UNCOV
322
    return;
×
323

324
  // Check to make sure this ID doesn't already exist
325
  if (model::plot_map.find(id) != model::plot_map.end()) {
772!
UNCOV
326
    throw std::runtime_error {
×
UNCOV
327
      fmt::format("Two or more plots use the same unique ID: {}", id)};
×
328
  }
329

330
  id_ = id;
772✔
331
}
332

333
// Checks if png or ppm is already present
334
bool file_extension_present(
754✔
335
  const std::string& filename, const std::string& extension)
336
{
337
  std::string file_extension_if_present =
338
    filename.substr(filename.find_last_of(".") + 1);
754✔
339
  if (file_extension_if_present == extension)
754✔
340
    return true;
50✔
341
  return false;
704✔
342
}
754✔
343

344
void Plot::set_output_path(pugi::xml_node plot_node)
682✔
345
{
346
  // Set output file path
347
  std::string filename;
682✔
348

349
  if (check_for_node(plot_node, "filename")) {
682✔
350
    filename = get_node_value(plot_node, "filename");
222✔
351
  } else {
352
    filename = fmt::format("plot_{}", id());
920✔
353
  }
354
  const std::string dir_if_present =
355
    filename.substr(0, filename.find_last_of("/") + 1);
682✔
356
  if (dir_if_present.size() > 0 && !dir_exists(dir_if_present)) {
682✔
357
    fatal_error(fmt::format("Directory '{}' does not exist!", dir_if_present));
8✔
358
  }
359
  // add appropriate file extension to name
360
  switch (type_) {
674!
361
  case PlotType::slice:
624✔
362
#ifdef USE_LIBPNG
363
    if (!file_extension_present(filename, "png"))
624!
364
      filename.append(".png");
624✔
365
#else
366
    if (!file_extension_present(filename, "ppm"))
367
      filename.append(".ppm");
368
#endif
369
    break;
624✔
370
  case PlotType::voxel:
50✔
371
    if (!file_extension_present(filename, "h5"))
50!
372
      filename.append(".h5");
50✔
373
    break;
50✔
374
  }
375

376
  path_plot_ = filename;
674✔
377

378
  // Copy plot pixel size
379
  vector<int> pxls = get_node_array<int>(plot_node, "pixels");
1,348✔
380
  if (PlotType::slice == type_) {
674✔
381
    if (pxls.size() == 2) {
624!
382
      pixels()[0] = pxls[0];
624✔
383
      pixels()[1] = pxls[1];
624✔
384
    } else {
UNCOV
385
      fatal_error(
×
UNCOV
386
        fmt::format("<pixels> must be length 2 in slice plot {}", id()));
×
387
    }
388
  } else if (PlotType::voxel == type_) {
50!
389
    if (pxls.size() == 3) {
50!
390
      pixels()[0] = pxls[0];
50✔
391
      pixels()[1] = pxls[1];
50✔
392
      pixels()[2] = pxls[2];
50✔
393
    } else {
UNCOV
394
      fatal_error(
×
UNCOV
395
        fmt::format("<pixels> must be length 3 in voxel plot {}", id()));
×
396
    }
397
  }
398
}
674✔
399

400
void PlottableInterface::set_bg_color(pugi::xml_node plot_node)
762✔
401
{
402
  // Copy plot background color
403
  if (check_for_node(plot_node, "background")) {
762✔
404
    vector<int> bg_rgb = get_node_array<int>(plot_node, "background");
40✔
405
    if (bg_rgb.size() == 3) {
40!
406
      not_found_ = bg_rgb;
40✔
407
    } else {
UNCOV
408
      fatal_error(fmt::format("Bad background RGB in plot {}", id()));
×
409
    }
410
  }
40✔
411
}
762✔
412

413
void Plot::set_basis(pugi::xml_node plot_node)
674✔
414
{
415
  // Copy plot basis
416
  if (PlotType::slice == type_) {
674✔
417
    std::string pl_basis = "xy";
624✔
418
    if (check_for_node(plot_node, "basis")) {
624!
419
      pl_basis = get_node_value(plot_node, "basis", true);
624✔
420
    }
421
    if ("xy" == pl_basis) {
624✔
422
      basis_ = PlotBasis::xy;
556✔
423
    } else if ("xz" == pl_basis) {
68✔
424
      basis_ = PlotBasis::xz;
20✔
425
    } else if ("yz" == pl_basis) {
48!
426
      basis_ = PlotBasis::yz;
48✔
427
    } else {
428
      fatal_error(
×
429
        fmt::format("Unsupported plot basis '{}' in plot {}", pl_basis, id()));
×
430
    }
431
  }
624✔
432
}
674✔
433

434
void Plot::set_origin(pugi::xml_node plot_node)
674✔
435
{
436
  // Copy plotting origin
437
  auto pl_origin = get_node_array<double>(plot_node, "origin");
674✔
438
  if (pl_origin.size() == 3) {
674!
439
    origin_ = pl_origin;
674✔
440
  } else {
UNCOV
441
    fatal_error(fmt::format("Origin must be length 3 in plot {}", id()));
×
442
  }
443
}
674✔
444

445
void Plot::set_width(pugi::xml_node plot_node)
674✔
446
{
447
  // Copy plotting width
448
  vector<double> pl_width = get_node_array<double>(plot_node, "width");
674✔
449
  if (PlotType::slice == type_) {
674✔
450
    if (pl_width.size() == 2) {
624!
451
      width_.x = pl_width[0];
624✔
452
      width_.y = pl_width[1];
624✔
453
    } else {
UNCOV
454
      fatal_error(
×
UNCOV
455
        fmt::format("<width> must be length 2 in slice plot {}", id()));
×
456
    }
457
  } else if (PlotType::voxel == type_) {
50!
458
    if (pl_width.size() == 3) {
50!
459
      pl_width = get_node_array<double>(plot_node, "width");
50✔
460
      width_ = pl_width;
50✔
461
    } else {
UNCOV
462
      fatal_error(
×
UNCOV
463
        fmt::format("<width> must be length 3 in voxel plot {}", id()));
×
464
    }
465
  }
466
}
674✔
467

468
void PlottableInterface::set_universe(pugi::xml_node plot_node)
762✔
469
{
470
  // Copy plot universe level
471
  if (check_for_node(plot_node, "level")) {
762!
UNCOV
472
    level_ = std::stoi(get_node_value(plot_node, "level"));
×
UNCOV
473
    if (level_ < 0) {
×
UNCOV
474
      fatal_error(fmt::format("Bad universe level in plot {}", id()));
×
475
    }
476
  } else {
477
    level_ = PLOT_LEVEL_LOWEST;
762✔
478
  }
479
}
762✔
480

481
void PlottableInterface::set_color_by(pugi::xml_node plot_node)
762✔
482
{
483
  // Copy plot color type
484
  std::string pl_color_by = "cell";
762✔
485
  if (check_for_node(plot_node, "color_by")) {
762✔
486
    pl_color_by = get_node_value(plot_node, "color_by", true);
732✔
487
  }
488
  if ("cell" == pl_color_by) {
762✔
489
    color_by_ = PlotColorBy::cells;
262✔
490
  } else if ("material" == pl_color_by) {
500!
491
    color_by_ = PlotColorBy::mats;
500✔
492
  } else {
UNCOV
493
    fatal_error(fmt::format(
×
UNCOV
494
      "Unsupported plot color type '{}' in plot {}", pl_color_by, id()));
×
495
  }
496
}
762✔
497

498
void PlottableInterface::set_default_colors()
772✔
499
{
500
  // Copy plot color type and initialize all colors randomly
501
  if (PlotColorBy::cells == color_by_) {
772✔
502
    colors_.resize(model::cells.size());
262✔
503
  } else if (PlotColorBy::mats == color_by_) {
510!
504
    colors_.resize(model::materials.size());
510✔
505
  }
506

507
  for (auto& c : colors_) {
3,526✔
508
    c = random_color();
2,754✔
509
    // make sure we don't interfere with some default colors
510
    while (c == RED || c == WHITE) {
2,754!
UNCOV
511
      c = random_color();
×
512
    }
513
  }
514
}
772✔
515

516
void PlottableInterface::set_user_colors(pugi::xml_node plot_node)
762✔
517
{
518
  for (auto cn : plot_node.children("color")) {
932✔
519
    // Make sure 3 values are specified for RGB
520
    vector<int> user_rgb = get_node_array<int>(cn, "rgb");
170✔
521
    if (user_rgb.size() != 3) {
170!
UNCOV
522
      fatal_error(fmt::format("Bad RGB in plot {}", id()));
×
523
    }
524
    // Ensure that there is an id for this color specification
525
    int col_id;
526
    if (check_for_node(cn, "id")) {
170!
527
      col_id = std::stoi(get_node_value(cn, "id"));
170✔
528
    } else {
UNCOV
529
      fatal_error(fmt::format(
×
UNCOV
530
        "Must specify id for color specification in plot {}", id()));
×
531
    }
532
    // Add RGB
533
    if (PlotColorBy::cells == color_by_) {
170✔
534
      if (model::cell_map.find(col_id) != model::cell_map.end()) {
80!
535
        col_id = model::cell_map[col_id];
80✔
536
        colors_[col_id] = user_rgb;
80✔
537
      } else {
UNCOV
538
        warning(fmt::format(
×
UNCOV
539
          "Could not find cell {} specified in plot {}", col_id, id()));
×
540
      }
541
    } else if (PlotColorBy::mats == color_by_) {
90!
542
      if (model::material_map.find(col_id) != model::material_map.end()) {
90!
543
        col_id = model::material_map[col_id];
90✔
544
        colors_[col_id] = user_rgb;
90✔
545
      } else {
546
        warning(fmt::format(
×
UNCOV
547
          "Could not find material {} specified in plot {}", col_id, id()));
×
548
      }
549
    }
550
  } // color node loop
170✔
551
}
762✔
552

553
void Plot::set_meshlines(pugi::xml_node plot_node)
674✔
554
{
555
  // Deal with meshlines
556
  pugi::xpath_node_set mesh_line_nodes = plot_node.select_nodes("meshlines");
674✔
557

558
  if (!mesh_line_nodes.empty()) {
674✔
559
    if (PlotType::voxel == type_) {
30!
UNCOV
560
      warning(fmt::format("Meshlines ignored in voxel plot {}", id()));
×
561
    }
562

563
    if (mesh_line_nodes.size() == 1) {
30!
564
      // Get first meshline node
565
      pugi::xml_node meshlines_node = mesh_line_nodes[0].node();
30✔
566

567
      // Check mesh type
568
      std::string meshtype;
30✔
569
      if (check_for_node(meshlines_node, "meshtype")) {
30!
570
        meshtype = get_node_value(meshlines_node, "meshtype");
30✔
571
      } else {
UNCOV
572
        fatal_error(fmt::format(
×
573
          "Must specify a meshtype for meshlines specification in plot {}",
UNCOV
574
          id()));
×
575
      }
576

577
      // Ensure that there is a linewidth for this meshlines specification
578
      std::string meshline_width;
30✔
579
      if (check_for_node(meshlines_node, "linewidth")) {
30!
580
        meshline_width = get_node_value(meshlines_node, "linewidth");
30✔
581
        meshlines_width_ = std::stoi(meshline_width);
30✔
582
      } else {
583
        fatal_error(fmt::format(
×
584
          "Must specify a linewidth for meshlines specification in plot {}",
UNCOV
585
          id()));
×
586
      }
587

588
      // Check for color
589
      if (check_for_node(meshlines_node, "color")) {
30!
590
        // Check and make sure 3 values are specified for RGB
UNCOV
591
        vector<int> ml_rgb = get_node_array<int>(meshlines_node, "color");
×
592
        if (ml_rgb.size() != 3) {
×
593
          fatal_error(
×
UNCOV
594
            fmt::format("Bad RGB for meshlines color in plot {}", id()));
×
595
        }
UNCOV
596
        meshlines_color_ = ml_rgb;
×
UNCOV
597
      }
×
598

599
      // Set mesh based on type
600
      if ("ufs" == meshtype) {
30!
UNCOV
601
        if (!simulation::ufs_mesh) {
×
UNCOV
602
          fatal_error(
×
UNCOV
603
            fmt::format("No UFS mesh for meshlines on plot {}", id()));
×
604
        } else {
UNCOV
605
          for (int i = 0; i < model::meshes.size(); ++i) {
×
UNCOV
606
            if (const auto* m =
×
UNCOV
607
                  dynamic_cast<const RegularMesh*>(model::meshes[i].get())) {
×
UNCOV
608
              if (m == simulation::ufs_mesh) {
×
UNCOV
609
                index_meshlines_mesh_ = i;
×
610
              }
611
            }
612
          }
613
          if (index_meshlines_mesh_ == -1)
×
UNCOV
614
            fatal_error("Could not find the UFS mesh for meshlines plot");
×
615
        }
616
      } else if ("entropy" == meshtype) {
30✔
617
        if (!simulation::entropy_mesh) {
20!
UNCOV
618
          fatal_error(
×
UNCOV
619
            fmt::format("No entropy mesh for meshlines on plot {}", id()));
×
620
        } else {
621
          for (int i = 0; i < model::meshes.size(); ++i) {
50✔
622
            if (const auto* m =
30✔
623
                  dynamic_cast<const RegularMesh*>(model::meshes[i].get())) {
30!
624
              if (m == simulation::entropy_mesh) {
20!
625
                index_meshlines_mesh_ = i;
20✔
626
              }
627
            }
628
          }
629
          if (index_meshlines_mesh_ == -1)
20!
630
            fatal_error("Could not find the entropy mesh for meshlines plot");
×
631
        }
632
      } else if ("tally" == meshtype) {
10!
633
        // Ensure that there is a mesh id if the type is tally
634
        int tally_mesh_id;
635
        if (check_for_node(meshlines_node, "id")) {
10!
636
          tally_mesh_id = std::stoi(get_node_value(meshlines_node, "id"));
10✔
637
        } else {
UNCOV
638
          std::stringstream err_msg;
×
UNCOV
639
          fatal_error(fmt::format("Must specify a mesh id for meshlines tally "
×
640
                                  "mesh specification in plot {}",
UNCOV
641
            id()));
×
UNCOV
642
        }
×
643
        // find the tally index
644
        int idx;
645
        int err = openmc_get_mesh_index(tally_mesh_id, &idx);
10✔
646
        if (err != 0) {
10!
UNCOV
647
          fatal_error(fmt::format("Could not find mesh {} specified in "
×
648
                                  "meshlines for plot {}",
649
            tally_mesh_id, id()));
×
650
        }
651
        index_meshlines_mesh_ = idx;
10✔
652
      } else {
UNCOV
653
        fatal_error(fmt::format("Invalid type for meshlines on plot {}", id()));
×
654
      }
655
    } else {
30✔
UNCOV
656
      fatal_error(fmt::format("Mutliple meshlines specified in plot {}", id()));
×
657
    }
658
  }
659
}
674✔
660

661
void PlottableInterface::set_mask(pugi::xml_node plot_node)
762✔
662
{
663
  // Deal with masks
664
  pugi::xpath_node_set mask_nodes = plot_node.select_nodes("mask");
762✔
665

666
  if (!mask_nodes.empty()) {
762✔
667
    if (mask_nodes.size() == 1) {
30!
668
      // Get pointer to mask
669
      pugi::xml_node mask_node = mask_nodes[0].node();
30✔
670

671
      // Determine how many components there are and allocate
672
      vector<int> iarray = get_node_array<int>(mask_node, "components");
30✔
673
      if (iarray.size() == 0) {
30!
UNCOV
674
        fatal_error(
×
UNCOV
675
          fmt::format("Missing <components> in mask of plot {}", id()));
×
676
      }
677

678
      // First we need to change the user-specified identifiers to indices
679
      // in the cell and material arrays
680
      for (auto& col_id : iarray) {
90✔
681
        if (PlotColorBy::cells == color_by_) {
60!
682
          if (model::cell_map.find(col_id) != model::cell_map.end()) {
60!
683
            col_id = model::cell_map[col_id];
60✔
684
          } else {
UNCOV
685
            fatal_error(fmt::format("Could not find cell {} specified in the "
×
686
                                    "mask in plot {}",
687
              col_id, id()));
×
688
          }
UNCOV
689
        } else if (PlotColorBy::mats == color_by_) {
×
UNCOV
690
          if (model::material_map.find(col_id) != model::material_map.end()) {
×
UNCOV
691
            col_id = model::material_map[col_id];
×
692
          } else {
UNCOV
693
            fatal_error(fmt::format("Could not find material {} specified in "
×
694
                                    "the mask in plot {}",
UNCOV
695
              col_id, id()));
×
696
          }
697
        }
698
      }
699

700
      // Alter colors based on mask information
701
      for (int j = 0; j < colors_.size(); j++) {
120✔
702
        if (contains(iarray, j)) {
90✔
703
          if (check_for_node(mask_node, "background")) {
60!
704
            vector<int> bg_rgb = get_node_array<int>(mask_node, "background");
60✔
705
            colors_[j] = bg_rgb;
60✔
706
          } else {
60✔
UNCOV
707
            colors_[j] = WHITE;
×
708
          }
709
        }
710
      }
711

712
    } else {
30✔
UNCOV
713
      fatal_error(fmt::format("Mutliple masks specified in plot {}", id()));
×
714
    }
715
  }
716
}
762✔
717

718
void PlottableInterface::set_overlap_color(pugi::xml_node plot_node)
762✔
719
{
720
  color_overlaps_ = false;
762✔
721
  if (check_for_node(plot_node, "show_overlaps")) {
762✔
722
    color_overlaps_ = get_node_value_bool(plot_node, "show_overlaps");
20✔
723
    // check for custom overlap color
724
    if (check_for_node(plot_node, "overlap_color")) {
20✔
725
      if (!color_overlaps_) {
10!
UNCOV
726
        warning(fmt::format(
×
727
          "Overlap color specified in plot {} but overlaps won't be shown.",
UNCOV
728
          id()));
×
729
      }
730
      vector<int> olap_clr = get_node_array<int>(plot_node, "overlap_color");
10✔
731
      if (olap_clr.size() == 3) {
10!
732
        overlap_color_ = olap_clr;
10✔
733
      } else {
UNCOV
734
        fatal_error(fmt::format("Bad overlap RGB in plot {}", id()));
×
735
      }
736
    }
10✔
737
  }
738

739
  // make sure we allocate the vector for counting overlap checks if
740
  // they're going to be plotted
741
  if (color_overlaps_ && settings::run_mode == RunMode::PLOTTING) {
762!
742
    settings::check_overlaps = true;
20✔
743
    model::overlap_check_count.resize(model::cells.size(), 0);
20✔
744
  }
745
}
762✔
746

747
PlottableInterface::PlottableInterface(pugi::xml_node plot_node)
762✔
748
{
749
  set_id(plot_node);
762✔
750
  set_bg_color(plot_node);
762✔
751
  set_universe(plot_node);
762✔
752
  set_color_by(plot_node);
762✔
753
  set_default_colors();
762✔
754
  set_user_colors(plot_node);
762✔
755
  set_mask(plot_node);
762✔
756
  set_overlap_color(plot_node);
762✔
757
}
762✔
758

759
Plot::Plot(pugi::xml_node plot_node, PlotType type)
682✔
760
  : PlottableInterface(plot_node), type_(type), index_meshlines_mesh_ {-1}
682✔
761
{
762
  set_output_path(plot_node);
682✔
763
  set_basis(plot_node);
674✔
764
  set_origin(plot_node);
674✔
765
  set_width(plot_node);
674✔
766
  set_meshlines(plot_node);
674✔
767
  slice_level_ = level_; // Copy level employed in SlicePlotBase::get_map
674✔
768
  slice_color_overlaps_ = color_overlaps_;
674✔
769
}
674✔
770

771
//==============================================================================
772
// OUTPUT_PPM writes out a previously generated image to a PPM file
773
//==============================================================================
774

UNCOV
775
void output_ppm(const std::string& filename, const ImageData& data)
×
776
{
777
  // Open PPM file for writing
UNCOV
778
  std::string fname = filename;
×
UNCOV
779
  fname = strtrim(fname);
×
UNCOV
780
  std::ofstream of;
×
781

UNCOV
782
  of.open(fname);
×
783

784
  // Write header
UNCOV
785
  of << "P6\n";
×
UNCOV
786
  of << data.shape(0) << " " << data.shape(1) << "\n";
×
UNCOV
787
  of << "255\n";
×
UNCOV
788
  of.close();
×
789

UNCOV
790
  of.open(fname, std::ios::binary | std::ios::app);
×
791
  // Write color for each pixel
UNCOV
792
  for (int y = 0; y < data.shape(1); y++) {
×
UNCOV
793
    for (int x = 0; x < data.shape(0); x++) {
×
794
      RGBColor rgb = data(x, y);
×
UNCOV
795
      of << rgb.red << rgb.green << rgb.blue;
×
796
    }
797
  }
UNCOV
798
  of << "\n";
×
NEW
799
}
×
800

801
//==============================================================================
802
// OUTPUT_PNG writes out a previously generated image to a PNG file
803
//==============================================================================
804

805
#ifdef USE_LIBPNG
806
void output_png(const std::string& filename, const ImageData& data)
210✔
807
{
808
  // Open PNG file for writing
809
  std::string fname = filename;
210✔
810
  fname = strtrim(fname);
210✔
811
  auto fp = std::fopen(fname.c_str(), "wb");
210✔
812

813
  // Initialize write and info structures
814
  auto png_ptr =
815
    png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
210✔
816
  auto info_ptr = png_create_info_struct(png_ptr);
210✔
817

818
  // Setup exception handling
819
  if (setjmp(png_jmpbuf(png_ptr)))
210!
UNCOV
820
    fatal_error("Error during png creation");
×
821

822
  png_init_io(png_ptr, fp);
210✔
823

824
  // Write header (8 bit colour depth)
825
  int width = data.shape(0);
210✔
826
  int height = data.shape(1);
210✔
827
  png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB,
210✔
828
    PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
829
  png_write_info(png_ptr, info_ptr);
210✔
830

831
  // Allocate memory for one row (3 bytes per pixel - RGB)
832
  std::vector<png_byte> row(3 * width);
210✔
833

834
  // Write color for each pixel
835
  for (int y = 0; y < height; y++) {
43,410✔
836
    for (int x = 0; x < width; x++) {
10,145,200✔
837
      RGBColor rgb = data(x, y);
10,102,000✔
838
      row[3 * x] = rgb.red;
10,102,000✔
839
      row[3 * x + 1] = rgb.green;
10,102,000✔
840
      row[3 * x + 2] = rgb.blue;
10,102,000✔
841
    }
842
    png_write_row(png_ptr, row.data());
43,200✔
843
  }
844

845
  // End write
846
  png_write_end(png_ptr, nullptr);
210✔
847

848
  // Clean up data structures
849
  std::fclose(fp);
210✔
850
  png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
210✔
851
  png_destroy_write_struct(&png_ptr, &info_ptr);
210✔
852
}
210✔
853
#endif
854

855
//==============================================================================
856
// DRAW_MESH_LINES draws mesh line boundaries on an image
857
//==============================================================================
858

859
void Plot::draw_mesh_lines(ImageData& data) const
30✔
860
{
861
  RGBColor rgb;
30✔
862
  rgb = meshlines_color_;
30✔
863

864
  int ax1, ax2;
865
  switch (basis_) {
30!
866
  case PlotBasis::xy:
20✔
867
    ax1 = 0;
20✔
868
    ax2 = 1;
20✔
869
    break;
20✔
870
  case PlotBasis::xz:
10✔
871
    ax1 = 0;
10✔
872
    ax2 = 2;
10✔
873
    break;
10✔
UNCOV
874
  case PlotBasis::yz:
×
UNCOV
875
    ax1 = 1;
×
876
    ax2 = 2;
×
UNCOV
877
    break;
×
UNCOV
878
  default:
×
UNCOV
879
    UNREACHABLE();
×
880
  }
881

882
  Position ll_plot {origin_};
30✔
883
  Position ur_plot {origin_};
30✔
884

885
  ll_plot[ax1] -= width_[0] / 2.;
30✔
886
  ll_plot[ax2] -= width_[1] / 2.;
30✔
887
  ur_plot[ax1] += width_[0] / 2.;
30✔
888
  ur_plot[ax2] += width_[1] / 2.;
30✔
889

890
  Position width = ur_plot - ll_plot;
30✔
891

892
  // Find the (axis-aligned) lines of the mesh that intersect this plot.
893
  auto axis_lines =
894
    model::meshes[index_meshlines_mesh_]->plot(ll_plot, ur_plot);
30✔
895

896
  // Find the bounds along the second axis (accounting for low-D meshes).
897
  int ax2_min, ax2_max;
898
  if (axis_lines.second.size() > 0) {
30!
899
    double frac = (axis_lines.second.back() - ll_plot[ax2]) / width[ax2];
30✔
900
    ax2_min = (1.0 - frac) * pixels()[1];
30✔
901
    if (ax2_min < 0)
30!
UNCOV
902
      ax2_min = 0;
×
903
    frac = (axis_lines.second.front() - ll_plot[ax2]) / width[ax2];
30✔
904
    ax2_max = (1.0 - frac) * pixels()[1];
30✔
905
    if (ax2_max > pixels()[1])
30!
906
      ax2_max = pixels()[1];
×
907
  } else {
UNCOV
908
    ax2_min = 0;
×
UNCOV
909
    ax2_max = pixels()[1];
×
910
  }
911

912
  // Iterate across the first axis and draw lines.
913
  for (auto ax1_val : axis_lines.first) {
170✔
914
    double frac = (ax1_val - ll_plot[ax1]) / width[ax1];
140✔
915
    int ax1_ind = frac * pixels()[0];
140✔
916
    for (int ax2_ind = ax2_min; ax2_ind < ax2_max; ++ax2_ind) {
22,680✔
917
      for (int plus = 0; plus <= meshlines_width_; plus++) {
45,080✔
918
        if (ax1_ind + plus >= 0 && ax1_ind + plus < pixels()[0])
22,540!
919
          data(ax1_ind + plus, ax2_ind) = rgb;
22,540✔
920
        if (ax1_ind - plus >= 0 && ax1_ind - plus < pixels()[0])
22,540!
921
          data(ax1_ind - plus, ax2_ind) = rgb;
22,540✔
922
      }
923
    }
924
  }
925

926
  // Find the bounds along the first axis.
927
  int ax1_min, ax1_max;
928
  if (axis_lines.first.size() > 0) {
30!
929
    double frac = (axis_lines.first.front() - ll_plot[ax1]) / width[ax1];
30✔
930
    ax1_min = frac * pixels()[0];
30✔
931
    if (ax1_min < 0)
30!
UNCOV
932
      ax1_min = 0;
×
933
    frac = (axis_lines.first.back() - ll_plot[ax1]) / width[ax1];
30✔
934
    ax1_max = frac * pixels()[0];
30✔
935
    if (ax1_max > pixels()[0])
30!
UNCOV
936
      ax1_max = pixels()[0];
×
937
  } else {
UNCOV
938
    ax1_min = 0;
×
UNCOV
939
    ax1_max = pixels()[0];
×
940
  }
941

942
  // Iterate across the second axis and draw lines.
943
  for (auto ax2_val : axis_lines.second) {
190✔
944
    double frac = (ax2_val - ll_plot[ax2]) / width[ax2];
160✔
945
    int ax2_ind = (1.0 - frac) * pixels()[1];
160✔
946
    for (int ax1_ind = ax1_min; ax1_ind < ax1_max; ++ax1_ind) {
25,760✔
947
      for (int plus = 0; plus <= meshlines_width_; plus++) {
51,200✔
948
        if (ax2_ind + plus >= 0 && ax2_ind + plus < pixels()[1])
25,600!
949
          data(ax1_ind, ax2_ind + plus) = rgb;
25,600✔
950
        if (ax2_ind - plus >= 0 && ax2_ind - plus < pixels()[1])
25,600!
951
          data(ax1_ind, ax2_ind - plus) = rgb;
25,600✔
952
      }
953
    }
954
  }
955
}
30✔
956

957
/* outputs a binary file that can be input into silomesh for 3D geometry
958
 * visualization.  It works the same way as create_image by dragging a particle
959
 * across the geometry for the specified number of voxels. The first 3 int's in
960
 * the binary are the number of x, y, and z voxels.  The next 3 double's are
961
 * the widths of the voxels in the x, y, and z directions. The next 3 double's
962
 * are the x, y, and z coordinates of the lower left point. Finally the binary
963
 * is filled with entries of four int's each. Each 'row' in the binary contains
964
 * four int's: 3 for x,y,z position and 1 for cell or material id.  For 1
965
 * million voxels this produces a file of approximately 15MB.
966
 */
967
void Plot::create_voxel() const
50✔
968
{
969
  // compute voxel widths in each direction
970
  array<double, 3> vox;
971
  vox[0] = width_[0] / static_cast<double>(pixels()[0]);
50✔
972
  vox[1] = width_[1] / static_cast<double>(pixels()[1]);
50✔
973
  vox[2] = width_[2] / static_cast<double>(pixels()[2]);
50✔
974

975
  // initial particle position
976
  Position ll = origin_ - width_ / 2.;
50✔
977

978
  // Open binary plot file for writing
979
  std::ofstream of;
50✔
980
  std::string fname = std::string(path_plot_);
50✔
981
  fname = strtrim(fname);
50✔
982
  hid_t file_id = file_open(fname, 'w');
50✔
983

984
  // write header info
985
  write_attribute(file_id, "filetype", "voxel");
50✔
986
  write_attribute(file_id, "version", VERSION_VOXEL);
50✔
987
  write_attribute(file_id, "openmc_version", VERSION);
50✔
988

989
#ifdef GIT_SHA1
990
  write_attribute(file_id, "git_sha1", GIT_SHA1);
991
#endif
992

993
  // Write current date and time
994
  write_attribute(file_id, "date_and_time", time_stamp().c_str());
50✔
995
  array<int, 3> h5_pixels;
996
  std::copy(pixels().begin(), pixels().end(), h5_pixels.begin());
50✔
997
  write_attribute(file_id, "num_voxels", h5_pixels);
50✔
998
  write_attribute(file_id, "voxel_width", vox);
50✔
999
  write_attribute(file_id, "lower_left", ll);
50✔
1000

1001
  // Create dataset for voxel data -- note that the dimensions are reversed
1002
  // since we want the order in the file to be z, y, x
1003
  hsize_t dims[3];
1004
  dims[0] = pixels()[2];
50✔
1005
  dims[1] = pixels()[1];
50✔
1006
  dims[2] = pixels()[0];
50✔
1007
  hid_t dspace, dset, memspace;
1008
  voxel_init(file_id, &(dims[0]), &dspace, &dset, &memspace);
50✔
1009

1010
  SlicePlotBase pltbase;
50✔
1011
  pltbase.width_ = width_;
50✔
1012
  pltbase.origin_ = origin_;
50✔
1013
  pltbase.basis_ = PlotBasis::xy;
50✔
1014
  pltbase.pixels() = pixels();
50✔
1015
  pltbase.slice_color_overlaps_ = color_overlaps_;
50✔
1016

1017
  ProgressBar pb;
50✔
1018
  for (int z = 0; z < pixels()[2]; z++) {
4,350✔
1019
    // update z coordinate
1020
    pltbase.origin_.z = ll.z + z * vox[2];
4,300✔
1021

1022
    // generate ids using plotbase
1023
    IdData ids = pltbase.get_map<IdData>();
4,300✔
1024

1025
    // select only cell/material ID data and flip the y-axis
1026
    int idx = color_by_ == PlotColorBy::cells ? 0 : 2;
4,300!
1027
    // Extract 2D slice at index idx from 3D data
1028
    size_t rows = ids.data_.shape(0);
4,300✔
1029
    size_t cols = ids.data_.shape(1);
4,300✔
1030
    tensor::Tensor<int32_t> data_slice({rows, cols});
4,300✔
1031
    for (size_t r = 0; r < rows; ++r)
829,300✔
1032
      for (size_t c = 0; c < cols; ++c)
163,075,000✔
1033
        data_slice(r, c) = ids.data_(r, c, idx);
162,250,000✔
1034
    tensor::Tensor<int32_t> data_flipped = data_slice.flip(0);
4,300✔
1035

1036
    // Write to HDF5 dataset
1037
    voxel_write_slice(z, dspace, dset, memspace, data_flipped.data());
4,300✔
1038

1039
    // update progress bar
1040
    pb.set_value(
4,300✔
1041
      100. * static_cast<double>(z + 1) / static_cast<double>((pixels()[2])));
4,300✔
1042
  }
4,300✔
1043

1044
  voxel_finalize(dspace, dset, memspace);
50✔
1045
  file_close(file_id);
50✔
1046
}
50✔
1047

1048
void voxel_init(hid_t file_id, const hsize_t* dims, hid_t* dspace, hid_t* dset,
50✔
1049
  hid_t* memspace)
1050
{
1051
  // Create dataspace/dataset for voxel data
1052
  *dspace = H5Screate_simple(3, dims, nullptr);
50✔
1053
  *dset = H5Dcreate(file_id, "data", H5T_NATIVE_INT, *dspace, H5P_DEFAULT,
50✔
1054
    H5P_DEFAULT, H5P_DEFAULT);
1055

1056
  // Create dataspace for a slice of the voxel
1057
  hsize_t dims_slice[2] {dims[1], dims[2]};
50✔
1058
  *memspace = H5Screate_simple(2, dims_slice, nullptr);
50✔
1059

1060
  // Select hyperslab in dataspace
1061
  hsize_t start[3] {0, 0, 0};
50✔
1062
  hsize_t count[3] {1, dims[1], dims[2]};
50✔
1063
  H5Sselect_hyperslab(*dspace, H5S_SELECT_SET, start, nullptr, count, nullptr);
50✔
1064
}
50✔
1065

1066
void voxel_write_slice(
4,300✔
1067
  int x, hid_t dspace, hid_t dset, hid_t memspace, void* buf)
1068
{
1069
  hssize_t offset[3] {x, 0, 0};
4,300✔
1070
  H5Soffset_simple(dspace, offset);
4,300✔
1071
  H5Dwrite(dset, H5T_NATIVE_INT, memspace, dspace, H5P_DEFAULT, buf);
4,300✔
1072
}
4,300✔
1073

1074
void voxel_finalize(hid_t dspace, hid_t dset, hid_t memspace)
50✔
1075
{
1076
  H5Dclose(dset);
50✔
1077
  H5Sclose(dspace);
50✔
1078
  H5Sclose(memspace);
50✔
1079
}
50✔
1080

1081
RGBColor random_color(void)
2,754✔
1082
{
1083
  return {int(prn(&model::plotter_seed) * 255),
2,754✔
1084
    int(prn(&model::plotter_seed) * 255), int(prn(&model::plotter_seed) * 255)};
2,754✔
1085
}
1086

1087
RayTracePlot::RayTracePlot(pugi::xml_node node) : PlottableInterface(node)
80✔
1088
{
1089
  set_look_at(node);
80✔
1090
  set_camera_position(node);
80✔
1091
  set_field_of_view(node);
80✔
1092
  set_pixels(node);
80✔
1093
  set_orthographic_width(node);
80✔
1094
  set_output_path(node);
80✔
1095

1096
  if (check_for_node(node, "orthographic_width") &&
90!
1097
      check_for_node(node, "field_of_view"))
10!
UNCOV
1098
    fatal_error("orthographic_width and field_of_view are mutually exclusive "
×
1099
                "parameters.");
1100
}
80✔
1101

1102
void RayTracePlot::update_view()
100✔
1103
{
1104
  // Get centerline vector for camera-to-model. We create vectors around this
1105
  // that form a pixel array, and then trace rays along that.
1106
  auto up = up_ / up_.norm();
100✔
1107
  Direction looking_direction = look_at_ - camera_position_;
100✔
1108
  looking_direction /= looking_direction.norm();
100✔
1109
  if (std::abs(std::abs(looking_direction.dot(up)) - 1.0) < 1e-9)
100!
UNCOV
1110
    fatal_error("Up vector cannot align with vector between camera position "
×
1111
                "and look_at!");
1112
  Direction cam_yaxis = looking_direction.cross(up);
100✔
1113
  cam_yaxis /= cam_yaxis.norm();
100✔
1114
  Direction cam_zaxis = cam_yaxis.cross(looking_direction);
100✔
1115
  cam_zaxis /= cam_zaxis.norm();
100✔
1116

1117
  // Cache the camera-to-model matrix
1118
  camera_to_model_ = {looking_direction.x, cam_yaxis.x, cam_zaxis.x,
100✔
1119
    looking_direction.y, cam_yaxis.y, cam_zaxis.y, looking_direction.z,
100✔
1120
    cam_yaxis.z, cam_zaxis.z};
100✔
1121
}
100✔
1122

1123
WireframeRayTracePlot::WireframeRayTracePlot(pugi::xml_node node)
50✔
1124
  : RayTracePlot(node)
50✔
1125
{
1126
  set_opacities(node);
50✔
1127
  set_wireframe_thickness(node);
50✔
1128
  set_wireframe_ids(node);
50✔
1129
  set_wireframe_color(node);
50✔
1130
  update_view();
50✔
1131
}
50✔
1132

1133
void WireframeRayTracePlot::set_wireframe_color(pugi::xml_node plot_node)
50✔
1134
{
1135
  // Copy plot wireframe color
1136
  if (check_for_node(plot_node, "wireframe_color")) {
50!
UNCOV
1137
    vector<int> w_rgb = get_node_array<int>(plot_node, "wireframe_color");
×
UNCOV
1138
    if (w_rgb.size() == 3) {
×
UNCOV
1139
      wireframe_color_ = w_rgb;
×
1140
    } else {
UNCOV
1141
      fatal_error(fmt::format("Bad wireframe RGB in plot {}", id()));
×
1142
    }
UNCOV
1143
  }
×
1144
}
50✔
1145

1146
void RayTracePlot::set_output_path(pugi::xml_node node)
80✔
1147
{
1148
  // Set output file path
1149
  std::string filename;
80✔
1150

1151
  if (check_for_node(node, "filename")) {
80✔
1152
    filename = get_node_value(node, "filename");
70✔
1153
  } else {
1154
    filename = fmt::format("plot_{}", id());
20✔
1155
  }
1156

1157
#ifdef USE_LIBPNG
1158
  if (!file_extension_present(filename, "png"))
80✔
1159
    filename.append(".png");
30✔
1160
#else
1161
  if (!file_extension_present(filename, "ppm"))
1162
    filename.append(".ppm");
1163
#endif
1164
  path_plot_ = filename;
80✔
1165
}
80✔
1166

1167
bool WireframeRayTracePlot::trackstack_equivalent(
2,764,690✔
1168
  const std::vector<TrackSegment>& track1,
1169
  const std::vector<TrackSegment>& track2) const
1170
{
1171
  if (wireframe_ids_.empty()) {
2,764,690✔
1172
    // Draw wireframe for all surfaces/cells/materials
1173
    if (track1.size() != track2.size())
2,313,700✔
1174
      return false;
49,070✔
1175
    for (int i = 0; i < track1.size(); ++i) {
6,098,140✔
1176
      if (track1[i].id != track2[i].id ||
7,703,100✔
1177
          track1[i].surface_index != track2[i].surface_index) {
3,851,490✔
1178
        return false;
18,100✔
1179
      }
1180
    }
1181
    return true;
2,246,530✔
1182
  } else {
1183
    // This runs in O(nm) where n is the intersection stack size
1184
    // and m is the number of IDs we are wireframing. A simpler
1185
    // algorithm can likely be found.
1186
    for (const int id : wireframe_ids_) {
896,540✔
1187
      int t1_i = 0;
450,990✔
1188
      int t2_i = 0;
450,990✔
1189

1190
      // Advance to first instance of the ID
1191
      while (t1_i < track1.size() && t2_i < track2.size()) {
511,300✔
1192
        while (t1_i < track1.size() && track1[t1_i].id != id)
357,120✔
1193
          t1_i++;
208,230✔
1194
        while (t2_i < track2.size() && track2[t2_i].id != id)
357,880✔
1195
          t2_i++;
208,990✔
1196

1197
        // This one is really important!
1198
        if ((t1_i == track1.size() && t2_i != track2.size()) ||
360,470✔
1199
            (t1_i != track1.size() && t2_i == track2.size()))
211,580✔
1200
          return false;
5,440✔
1201
        if (t1_i == track1.size() && t2_i == track2.size())
145,510!
1202
          break;
83,140✔
1203
        // Check if surface different
1204
        if (track1[t1_i].surface_index != track2[t2_i].surface_index)
62,370✔
1205
          return false;
1,350✔
1206

1207
        // Pretty sure this should not be used:
1208
        // if (t2_i != track2.size() - 1 &&
1209
        //     t1_i != track1.size() - 1 &&
1210
        //     track1[t1_i+1].id != track2[t2_i+1].id) return false;
1211
        if (t2_i != 0 && t1_i != 0 &&
110,060!
1212
            track1[t1_i - 1].surface_index != track2[t2_i - 1].surface_index)
49,040✔
1213
          return false;
710✔
1214

1215
        // Check if neighboring cells are different
1216
        // if (track1[t1_i ? t1_i - 1 : 0].id != track2[t2_i ? t2_i - 1 : 0].id)
1217
        // return false; if (track1[t1_i < track1.size() - 1 ? t1_i + 1 : t1_i
1218
        // ].id !=
1219
        //    track2[t2_i < track2.size() - 1 ? t2_i + 1 : t2_i].id) return
1220
        //    false;
1221
        t1_i++, t2_i++;
60,310✔
1222
      }
1223
    }
1224
    return true;
445,550✔
1225
  }
1226
}
1227

1228
std::pair<Position, Direction> RayTracePlot::get_pixel_ray(
3,200,960✔
1229
  int horiz, int vert) const
1230
{
1231
  // Compute field of view in radians
1232
  constexpr double DEGREE_TO_RADIAN = M_PI / 180.0;
3,200,960✔
1233
  double horiz_fov_radians = horizontal_field_of_view_ * DEGREE_TO_RADIAN;
3,200,960✔
1234
  double p0 = static_cast<double>(pixels()[0]);
3,200,960✔
1235
  double p1 = static_cast<double>(pixels()[1]);
3,200,960✔
1236
  double vert_fov_radians = horiz_fov_radians * p1 / p0;
3,200,960✔
1237

1238
  // focal_plane_dist can be changed to alter the perspective distortion
1239
  // effect. This is in units of cm. This seems to look good most of the
1240
  // time. TODO let this variable be set through XML.
1241
  constexpr double focal_plane_dist = 10.0;
3,200,960✔
1242
  const double dx = 2.0 * focal_plane_dist * std::tan(0.5 * horiz_fov_radians);
3,200,960✔
1243
  const double dy = p1 / p0 * dx;
3,200,960✔
1244

1245
  std::pair<Position, Direction> result;
3,200,960✔
1246

1247
  // Generate the starting position/direction of the ray
1248
  if (orthographic_width_ == C_NONE) { // perspective projection
3,200,960✔
1249
    Direction camera_local_vec;
2,800,960✔
1250
    camera_local_vec.x = focal_plane_dist;
2,800,960✔
1251
    camera_local_vec.y = -0.5 * dx + horiz * dx / p0;
2,800,960✔
1252
    camera_local_vec.z = 0.5 * dy - vert * dy / p1;
2,800,960✔
1253
    camera_local_vec /= camera_local_vec.norm();
2,800,960✔
1254

1255
    result.first = camera_position_;
2,800,960✔
1256
    result.second = camera_local_vec.rotate(camera_to_model_);
2,800,960✔
1257
  } else { // orthographic projection
1258

1259
    double x_pix_coord = (static_cast<double>(horiz) - p0 / 2.0) / p0;
400,000✔
1260
    double y_pix_coord = (static_cast<double>(vert) - p1 / 2.0) / p1;
400,000✔
1261

1262
    result.first = camera_position_ +
1263
                   camera_y_axis() * x_pix_coord * orthographic_width_ +
400,000✔
1264
                   camera_z_axis() * y_pix_coord * orthographic_width_;
400,000✔
1265
    result.second = camera_x_axis();
400,000✔
1266
  }
1267

1268
  return result;
3,200,960✔
1269
}
1270

1271
ImageData WireframeRayTracePlot::create_image() const
50✔
1272
{
1273
  size_t width = pixels()[0];
50✔
1274
  size_t height = pixels()[1];
50✔
1275
  ImageData data({width, height}, not_found_);
50✔
1276

1277
  // This array marks where the initial wireframe was drawn. We convolve it with
1278
  // a filter that gets adjusted with the wireframe thickness in order to
1279
  // thicken the lines.
1280
  tensor::Tensor<int> wireframe_initial(
1281
    {static_cast<size_t>(width), static_cast<size_t>(height)}, 0);
50✔
1282

1283
  /* Holds all of the track segments for the current rendered line of pixels.
1284
   * old_segments holds a copy of this_line_segments from the previous line.
1285
   * By holding both we can check if the cell/material intersection stack
1286
   * differs from the left or upper neighbor. This allows a robustly drawn
1287
   * wireframe. If only checking the left pixel (which requires substantially
1288
   * less memory), the wireframe tends to be spotty and be disconnected for
1289
   * surface edges oriented horizontally in the rendering.
1290
   *
1291
   * Note that a vector of vectors is required rather than a 2-tensor,
1292
   * since the stack size varies within each column.
1293
   */
1294
  const int n_threads = num_threads();
50✔
1295
  std::vector<std::vector<std::vector<TrackSegment>>> this_line_segments(
1296
    n_threads);
50✔
1297
  for (int t = 0; t < n_threads; ++t) {
130✔
1298
    this_line_segments[t].resize(pixels()[0]);
80✔
1299
  }
1300

1301
  // The last thread writes to this, and the first thread reads from it.
1302
  std::vector<std::vector<TrackSegment>> old_segments(pixels()[0]);
50✔
1303

1304
#pragma omp parallel
30✔
1305
  {
1306
    const int n_threads = num_threads();
20✔
1307
    const int tid = thread_num();
20✔
1308

1309
    int vert = tid;
20✔
1310
    for (int iter = 0; iter <= pixels()[1] / n_threads; iter++) {
4,040✔
1311

1312
      // Save bottom line of current work chunk to compare against later. This
1313
      // used to be inside the below if block, but it causes a spurious line to
1314
      // be drawn at the bottom of the image. Not sure why, but moving it here
1315
      // fixes things.
1316
      if (tid == n_threads - 1)
4,020!
1317
        old_segments = this_line_segments[n_threads - 1];
4,020✔
1318

1319
      if (vert < pixels()[1]) {
4,020✔
1320

1321
        for (int horiz = 0; horiz < pixels()[0]; ++horiz) {
804,000✔
1322

1323
          // RayTracePlot implements camera ray generation
1324
          std::pair<Position, Direction> ru = get_pixel_ray(horiz, vert);
800,000✔
1325

1326
          this_line_segments[tid][horiz].clear();
800,000✔
1327
          ProjectionRay ray(
1328
            ru.first, ru.second, *this, this_line_segments[tid][horiz]);
800,000✔
1329

1330
          ray.trace();
800,000✔
1331

1332
          // Now color the pixel based on what we have intersected...
1333
          // Loops backwards over intersections.
1334
          Position current_color(
1335
            not_found_.red, not_found_.green, not_found_.blue);
800,000✔
1336
          const auto& segments = this_line_segments[tid][horiz];
800,000✔
1337

1338
          // There must be at least two cell intersections to color, front and
1339
          // back of the cell. Maybe an infinitely thick cell could be present
1340
          // with no back, but why would you want to color that? It's easier to
1341
          // just skip that edge case and not even color it.
1342
          if (segments.size() <= 1)
800,000✔
1343
            continue;
493,324✔
1344

1345
          for (int i = segments.size() - 2; i >= 0; --i) {
857,868✔
1346
            int colormap_idx = segments[i].id;
551,192✔
1347
            RGBColor seg_color = colors_[colormap_idx];
551,192✔
1348
            Position seg_color_vec(
1349
              seg_color.red, seg_color.green, seg_color.blue);
551,192✔
1350
            double mixing =
1351
              std::exp(-xs_[colormap_idx] *
551,192✔
1352
                       (segments[i + 1].length - segments[i].length));
551,192✔
1353
            current_color =
1354
              current_color * mixing + (1.0 - mixing) * seg_color_vec;
551,192✔
1355
          }
1356

1357
          // save result converting from double-precision color coordinates to
1358
          // byte-sized
1359
          RGBColor result;
306,676✔
1360
          result.red = static_cast<uint8_t>(current_color.x);
306,676✔
1361
          result.green = static_cast<uint8_t>(current_color.y);
306,676✔
1362
          result.blue = static_cast<uint8_t>(current_color.z);
306,676✔
1363
          data(horiz, vert) = result;
306,676✔
1364

1365
          // Check to draw wireframe in horizontal direction. No inter-thread
1366
          // comm.
1367
          if (horiz > 0) {
306,676✔
1368
            if (!trackstack_equivalent(this_line_segments[tid][horiz],
305,876✔
1369
                  this_line_segments[tid][horiz - 1])) {
305,876✔
1370
              wireframe_initial(horiz, vert) = 1;
12,568✔
1371
            }
1372
          }
1373
        }
800,000✔
1374
      } // end "if" vert in correct range
1375

1376
      // We require a barrier before comparing vertical neighbors' intersection
1377
      // stacks. i.e. all threads must be done with their line.
1378
#pragma omp barrier
1379

1380
      // Now that the horizontal line has finished rendering, we can fill in
1381
      // wireframe entries that require comparison among all the threads. Hence
1382
      // the omp barrier being used. It has to be OUTSIDE any if blocks!
1383
      if (vert < pixels()[1]) {
4,020✔
1384
        // Loop over horizontal pixels, checking intersection stack of upper
1385
        // neighbor
1386

1387
        const std::vector<std::vector<TrackSegment>>* top_cmp = nullptr;
4,000✔
1388
        if (tid == 0)
4,000!
1389
          top_cmp = &old_segments;
4,000✔
1390
        else
1391
          top_cmp = &this_line_segments[tid - 1];
1392

1393
        for (int horiz = 0; horiz < pixels()[0]; ++horiz) {
804,000✔
1394
          if (!trackstack_equivalent(
800,000✔
1395
                this_line_segments[tid][horiz], (*top_cmp)[horiz])) {
800,000✔
1396
            wireframe_initial(horiz, vert) = 1;
16,476✔
1397
          }
1398
        }
1399
      }
1400

1401
      // We need another barrier to ensure threads don't proceed to modify their
1402
      // intersection stacks on that horizontal line while others are
1403
      // potentially still working on the above.
1404
#pragma omp barrier
1405
      vert += n_threads;
4,020✔
1406
    }
1407
  } // end omp parallel
1408

1409
  // Now thicken the wireframe lines and apply them to our image
1410
  for (int vert = 0; vert < pixels()[1]; ++vert) {
10,050✔
1411
    for (int horiz = 0; horiz < pixels()[0]; ++horiz) {
2,010,000✔
1412
      if (wireframe_initial(horiz, vert)) {
2,000,000✔
1413
        if (wireframe_thickness_ == 1)
64,530✔
1414
          data(horiz, vert) = wireframe_color_;
27,450✔
1415
        for (int i = -wireframe_thickness_ / 2; i < wireframe_thickness_ / 2;
177,930✔
1416
             ++i)
1417
          for (int j = -wireframe_thickness_ / 2; j < wireframe_thickness_ / 2;
497,160✔
1418
               ++j)
1419
            if (i * i + j * j < wireframe_thickness_ * wireframe_thickness_) {
383,760!
1420

1421
              // Check if wireframe pixel is out of bounds
1422
              int w_i = std::max(std::min(horiz + i, pixels()[0] - 1), 0);
383,760✔
1423
              int w_j = std::max(std::min(vert + j, pixels()[1] - 1), 0);
383,760✔
1424
              data(w_i, w_j) = wireframe_color_;
383,760✔
1425
            }
1426
      }
1427
    }
1428
  }
1429

1430
  return data;
100✔
1431
}
50✔
1432

1433
void WireframeRayTracePlot::create_output() const
50✔
1434
{
1435
  ImageData data = create_image();
50✔
1436
  write_image(data);
50✔
1437
}
50✔
1438

1439
void RayTracePlot::print_info() const
80✔
1440
{
1441
  fmt::print("Camera position: {} {} {}\n", camera_position_.x,
64✔
1442
    camera_position_.y, camera_position_.z);
80✔
1443
  fmt::print("Look at: {} {} {}\n", look_at_.x, look_at_.y, look_at_.z);
144✔
1444
  fmt::print(
64✔
1445
    "Horizontal field of view: {} degrees\n", horizontal_field_of_view_);
80✔
1446
  fmt::print("Pixels: {} {}\n", pixels()[0], pixels()[1]);
144✔
1447
}
80✔
1448

1449
void WireframeRayTracePlot::print_info() const
50✔
1450
{
1451
  fmt::print("Plot Type: Wireframe ray-traced\n");
50✔
1452
  RayTracePlot::print_info();
50✔
1453
}
50✔
1454

1455
void WireframeRayTracePlot::set_opacities(pugi::xml_node node)
50✔
1456
{
1457
  xs_.resize(colors_.size(), 1e6); // set to large value for opaque by default
50✔
1458

1459
  for (auto cn : node.children("color")) {
110✔
1460
    // Make sure 3 values are specified for RGB
1461
    double user_xs = std::stod(get_node_value(cn, "xs"));
60✔
1462
    int col_id = std::stoi(get_node_value(cn, "id"));
60✔
1463

1464
    // Add RGB
1465
    if (PlotColorBy::cells == color_by_) {
60!
1466
      if (model::cell_map.find(col_id) != model::cell_map.end()) {
60!
1467
        col_id = model::cell_map[col_id];
60✔
1468
        xs_[col_id] = user_xs;
60✔
1469
      } else {
UNCOV
1470
        warning(fmt::format(
×
UNCOV
1471
          "Could not find cell {} specified in plot {}", col_id, id()));
×
1472
      }
UNCOV
1473
    } else if (PlotColorBy::mats == color_by_) {
×
UNCOV
1474
      if (model::material_map.find(col_id) != model::material_map.end()) {
×
UNCOV
1475
        col_id = model::material_map[col_id];
×
1476
        xs_[col_id] = user_xs;
×
1477
      } else {
UNCOV
1478
        warning(fmt::format(
×
UNCOV
1479
          "Could not find material {} specified in plot {}", col_id, id()));
×
1480
      }
1481
    }
1482
  }
1483
}
50✔
1484

1485
void RayTracePlot::set_orthographic_width(pugi::xml_node node)
80✔
1486
{
1487
  if (check_for_node(node, "orthographic_width")) {
80✔
1488
    double orthographic_width =
1489
      std::stod(get_node_value(node, "orthographic_width", true));
10✔
1490
    if (orthographic_width < 0.0)
10!
UNCOV
1491
      fatal_error("Requires positive orthographic_width");
×
1492
    orthographic_width_ = orthographic_width;
10✔
1493
  }
1494
}
80✔
1495

1496
void WireframeRayTracePlot::set_wireframe_thickness(pugi::xml_node node)
50✔
1497
{
1498
  if (check_for_node(node, "wireframe_thickness")) {
50✔
1499
    int wireframe_thickness =
1500
      std::stoi(get_node_value(node, "wireframe_thickness", true));
20✔
1501
    if (wireframe_thickness < 0)
20!
UNCOV
1502
      fatal_error("Requires non-negative wireframe thickness");
×
1503
    wireframe_thickness_ = wireframe_thickness;
20✔
1504
  }
1505
}
50✔
1506

1507
void WireframeRayTracePlot::set_wireframe_ids(pugi::xml_node node)
50✔
1508
{
1509
  if (check_for_node(node, "wireframe_ids")) {
50✔
1510
    wireframe_ids_ = get_node_array<int>(node, "wireframe_ids");
10✔
1511
    // It is read in as actual ID values, but we have to convert to indices in
1512
    // mat/cell array
1513
    for (auto& x : wireframe_ids_)
20✔
1514
      x = color_by_ == PlotColorBy::mats ? model::material_map[x]
10!
UNCOV
1515
                                         : model::cell_map[x];
×
1516
  }
1517
  // We make sure the list is sorted in order to later use
1518
  // std::binary_search.
1519
  std::sort(wireframe_ids_.begin(), wireframe_ids_.end());
50✔
1520
}
50✔
1521

1522
void RayTracePlot::set_pixels(pugi::xml_node node)
80✔
1523
{
1524
  vector<int> pxls = get_node_array<int>(node, "pixels");
80✔
1525
  if (pxls.size() != 2)
80!
UNCOV
1526
    fatal_error(
×
UNCOV
1527
      fmt::format("<pixels> must be length 2 in projection plot {}", id()));
×
1528
  pixels()[0] = pxls[0];
80✔
1529
  pixels()[1] = pxls[1];
80✔
1530
}
80✔
1531

1532
void RayTracePlot::set_camera_position(pugi::xml_node node)
80✔
1533
{
1534
  vector<double> camera_pos = get_node_array<double>(node, "camera_position");
80✔
1535
  if (camera_pos.size() != 3) {
80!
1536
    fatal_error(fmt::format(
×
1537
      "camera_position element must have three floating point values"));
1538
  }
1539
  camera_position_.x = camera_pos[0];
80✔
1540
  camera_position_.y = camera_pos[1];
80✔
1541
  camera_position_.z = camera_pos[2];
80✔
1542
}
80✔
1543

1544
void RayTracePlot::set_look_at(pugi::xml_node node)
80✔
1545
{
1546
  vector<double> look_at = get_node_array<double>(node, "look_at");
80✔
1547
  if (look_at.size() != 3) {
80!
UNCOV
1548
    fatal_error("look_at element must have three floating point values");
×
1549
  }
1550
  look_at_.x = look_at[0];
80✔
1551
  look_at_.y = look_at[1];
80✔
1552
  look_at_.z = look_at[2];
80✔
1553
}
80✔
1554

1555
void RayTracePlot::set_field_of_view(pugi::xml_node node)
80✔
1556
{
1557
  // Defaults to 70 degree horizontal field of view (see .h file)
1558
  if (check_for_node(node, "horizontal_field_of_view")) {
80!
1559
    double fov =
UNCOV
1560
      std::stod(get_node_value(node, "horizontal_field_of_view", true));
×
UNCOV
1561
    if (fov < 180.0 && fov > 0.0) {
×
UNCOV
1562
      horizontal_field_of_view_ = fov;
×
1563
    } else {
UNCOV
1564
      fatal_error(fmt::format("Horizontal field of view for plot {} "
×
1565
                              "out-of-range. Must be in (0, 180) degrees.",
UNCOV
1566
        id()));
×
1567
    }
1568
  }
1569
}
80✔
1570

1571
SolidRayTracePlot::SolidRayTracePlot(pugi::xml_node node) : RayTracePlot(node)
30✔
1572
{
1573
  set_opaque_ids(node);
30✔
1574
  set_diffuse_fraction(node);
30✔
1575
  set_light_position(node);
30✔
1576
  update_view();
30✔
1577
}
30✔
1578

1579
void SolidRayTracePlot::print_info() const
30✔
1580
{
1581
  fmt::print("Plot Type: Solid ray-traced\n");
30✔
1582
  RayTracePlot::print_info();
30✔
1583
}
30✔
1584

1585
ImageData SolidRayTracePlot::create_image() const
50✔
1586
{
1587
  size_t width = pixels()[0];
50✔
1588
  size_t height = pixels()[1];
50✔
1589
  ImageData data({width, height}, not_found_);
50✔
1590

1591
#pragma omp parallel for schedule(dynamic) collapse(2)
30✔
1592
  for (int horiz = 0; horiz < pixels()[0]; ++horiz) {
2,484✔
1593
    for (int vert = 0; vert < pixels()[1]; ++vert) {
482,848✔
1594
      // RayTracePlot implements camera ray generation
1595
      std::pair<Position, Direction> ru = get_pixel_ray(horiz, vert);
480,384✔
1596
      PhongRay ray(ru.first, ru.second, *this);
480,384✔
1597
      ray.trace();
480,384✔
1598
      data(horiz, vert) = ray.result_color();
480,384✔
1599
    }
480,384✔
1600
  }
1601

1602
  return data;
50✔
1603
}
1604

1605
void SolidRayTracePlot::create_output() const
30✔
1606
{
1607
  ImageData data = create_image();
30✔
1608
  write_image(data);
30✔
1609
}
30✔
1610

1611
void SolidRayTracePlot::set_opaque_ids(pugi::xml_node node)
30✔
1612
{
1613
  if (check_for_node(node, "opaque_ids")) {
30!
1614
    auto opaque_ids_tmp = get_node_array<int>(node, "opaque_ids");
30✔
1615

1616
    // It is read in as actual ID values, but we have to convert to indices in
1617
    // mat/cell array
1618
    for (auto& x : opaque_ids_tmp)
90✔
1619
      x = color_by_ == PlotColorBy::mats ? model::material_map[x]
60!
UNCOV
1620
                                         : model::cell_map[x];
×
1621

1622
    opaque_ids_.insert(opaque_ids_tmp.begin(), opaque_ids_tmp.end());
30✔
1623
  }
30✔
1624
}
30✔
1625

1626
void SolidRayTracePlot::set_light_position(pugi::xml_node node)
30✔
1627
{
1628
  if (check_for_node(node, "light_position")) {
30✔
1629
    auto light_pos_tmp = get_node_array<double>(node, "light_position");
10✔
1630

1631
    if (light_pos_tmp.size() != 3)
10!
UNCOV
1632
      fatal_error("Light position must be given as 3D coordinates");
×
1633

1634
    light_location_.x = light_pos_tmp[0];
10✔
1635
    light_location_.y = light_pos_tmp[1];
10✔
1636
    light_location_.z = light_pos_tmp[2];
10✔
1637
  } else {
10✔
1638
    light_location_ = camera_position();
20✔
1639
  }
1640
}
30✔
1641

1642
void SolidRayTracePlot::set_diffuse_fraction(pugi::xml_node node)
30✔
1643
{
1644
  if (check_for_node(node, "diffuse_fraction")) {
30✔
1645
    diffuse_fraction_ = std::stod(get_node_value(node, "diffuse_fraction"));
10✔
1646
    if (diffuse_fraction_ < 0.0 || diffuse_fraction_ > 1.0) {
10!
UNCOV
1647
      fatal_error("Must have 0 <= diffuse fraction <= 1");
×
1648
    }
1649
  }
1650
}
30✔
1651

1652
void Ray::compute_distance()
2,740,580✔
1653
{
1654
  boundary() = distance_to_boundary(*this);
2,740,580✔
1655
}
2,740,580✔
1656

1657
void Ray::trace()
3,200,960✔
1658
{
1659
  // To trace the ray from its origin all the way through the model, we have
1660
  // to proceed in two phases. In the first, the ray may or may not be found
1661
  // inside the model. If the ray is already in the model, phase one can be
1662
  // skipped. Otherwise, the ray has to be advanced to the boundary of the
1663
  // model where all the cells are defined. Importantly, this is assuming that
1664
  // the model is convex, which is a very reasonable assumption for any
1665
  // radiation transport model.
1666
  //
1667
  // After phase one is done, we can starting tracing from cell to cell within
1668
  // the model. This step can use neighbor lists to accelerate the ray tracing.
1669

1670
  // Attempt to initialize the particle. We may have to enter a loop to move
1671
  // it up to the edge of the model.
1672
  bool inside_cell = exhaustive_find_cell(*this, settings::verbosity >= 10);
3,200,960✔
1673

1674
  // Advance to the boundary of the model
1675
  while (!inside_cell) {
14,198,580!
1676
    advance_to_boundary_from_void();
14,198,580✔
1677
    inside_cell = exhaustive_find_cell(*this, settings::verbosity >= 10);
14,198,580✔
1678

1679
    // If true this means no surface was intersected. See cell.cpp and search
1680
    // for numeric_limits to see where we return it.
1681
    if (surface() == std::numeric_limits<int>::max()) {
14,198,580!
UNCOV
1682
      warning(fmt::format("Lost a ray, r = {}, u = {}", r(), u()));
×
UNCOV
1683
      return;
×
1684
    }
1685

1686
    // Exit this loop and enter into cell-to-cell ray tracing (which uses
1687
    // neighbor lists)
1688
    if (inside_cell)
14,198,580✔
1689
      break;
1,408,940✔
1690

1691
    // if there is no intersection with the model, we're done
1692
    if (boundary().surface() == SURFACE_NONE)
12,789,640✔
1693
      return;
1,792,020✔
1694

1695
    event_counter_++;
10,997,620✔
1696
    if (event_counter_ > MAX_INTERSECTIONS) {
10,997,620!
UNCOV
1697
      warning("Likely infinite loop in ray traced plot");
×
UNCOV
1698
      return;
×
1699
    }
1700
  }
1701

1702
  // Call the specialized logic for this type of ray. This is for the
1703
  // intersection for the first intersection if we had one.
1704
  if (boundary().surface() != SURFACE_NONE) {
1,408,940!
1705
    // set the geometry state's surface attribute to be used for
1706
    // surface normal computation
1707
    surface() = boundary().surface();
1,408,940✔
1708
    on_intersection();
1,408,940✔
1709
    if (stop_)
1,408,940!
UNCOV
1710
      return;
×
1711
  }
1712

1713
  // reset surface attribute to zero after the first intersection so that it
1714
  // doesn't perturb surface crossing logic from here on out
1715
  surface() = 0;
1,408,940✔
1716

1717
  // This is the ray tracing loop within the model. It exits after exiting
1718
  // the model, which is equivalent to assuming that the model is convex.
1719
  // It would be nice to factor out the on_intersection at the end of this
1720
  // loop and then do "while (inside_cell)", but we can't guarantee it's
1721
  // on a surface in that case. There might be some other way to set it
1722
  // up that is perhaps a little more elegant, but this is what works just
1723
  // fine.
1724
  while (true) {
1725

1726
    compute_distance();
2,098,700✔
1727

1728
    // There are no more intersections to process
1729
    // if we hit the edge of the model, so stop
1730
    // the particle in that case. Also, just exit
1731
    // if a negative distance was somehow computed.
1732
    if (boundary().distance() == INFTY || boundary().distance() == INFINITY ||
4,197,400!
1733
        boundary().distance() < 0) {
2,098,700!
UNCOV
1734
      return;
×
1735
    }
1736

1737
    // See below comment where call_on_intersection is checked in an
1738
    // if statement for an explanation of this.
1739
    bool call_on_intersection {true};
2,098,700✔
1740
    if (boundary().distance() < 10 * TINY_BIT) {
2,098,700✔
1741
      call_on_intersection = false;
539,350✔
1742
    }
1743

1744
    // DAGMC surfaces expect us to go a little bit further than the advance
1745
    // distance to properly check cell inclusion.
1746
    boundary().distance() += TINY_BIT;
2,098,700✔
1747

1748
    // Advance particle, prepare for next intersection
1749
    for (int lev = 0; lev < n_coord(); ++lev) {
4,197,400✔
1750
      coord(lev).r() += boundary().distance() * coord(lev).u();
2,098,700✔
1751
    }
1752
    surface() = boundary().surface();
2,098,700✔
1753
    n_coord_last() = n_coord();
2,098,700✔
1754
    n_coord() = boundary().coord_level();
2,098,700✔
1755
    if (boundary().lattice_translation()[0] != 0 ||
2,098,700✔
1756
        boundary().lattice_translation()[1] != 0 ||
4,197,400!
1757
        boundary().lattice_translation()[2] != 0) {
2,098,700!
1758
      cross_lattice(*this, boundary(), settings::verbosity >= 10);
×
1759
    }
1760

1761
    // Record how far the ray has traveled
1762
    traversal_distance_ += boundary().distance();
2,098,700✔
1763
    inside_cell = neighbor_list_find_cell(*this, settings::verbosity >= 10);
2,098,700✔
1764

1765
    // Call the specialized logic for this type of ray. Note that we do not
1766
    // call this if the advance distance is very small. Unfortunately, it seems
1767
    // darn near impossible to get the particle advanced to the model boundary
1768
    // and through it without sometimes accidentally calling on_intersection
1769
    // twice. This incorrectly shades the region as occluded when it might not
1770
    // actually be. By screening out intersection distances smaller than a
1771
    // threshold 10x larger than the scoot distance used to advance up to the
1772
    // model boundary, we can avoid that situation.
1773
    if (call_on_intersection) {
2,098,700✔
1774
      on_intersection();
1,559,350✔
1775
      if (stop_)
1,559,350✔
1776
        return;
32,330✔
1777
    }
1778

1779
    if (!inside_cell)
2,066,370✔
1780
      return;
1,376,610✔
1781

1782
    event_counter_++;
689,760✔
1783
    if (event_counter_ > MAX_INTERSECTIONS) {
689,760!
1784
      warning("Likely infinite loop in ray traced plot");
×
UNCOV
1785
      return;
×
1786
    }
1787
  }
689,760✔
1788
}
1789

1790
void ProjectionRay::on_intersection()
2,144,680✔
1791
{
1792
  // This records a tuple with the following info
1793
  //
1794
  // 1) ID (material or cell depending on color_by_)
1795
  // 2) Distance traveled by the ray through that ID
1796
  // 3) Index of the intersected surface (starting from 1)
1797

1798
  line_segments_.emplace_back(
2,144,680✔
1799
    plot_.color_by_ == PlottableInterface::PlotColorBy::mats
2,144,680✔
1800
      ? material()
496,290✔
1801
      : lowest_coord().cell(),
1,648,390✔
1802
    traversal_distance_, boundary().surface_index());
2,144,680✔
1803
}
2,144,680✔
1804

1805
void PhongRay::on_intersection()
823,610✔
1806
{
1807
  // Check if we hit an opaque material or cell
1808
  int hit_id = plot_.color_by_ == PlottableInterface::PlotColorBy::mats
823,610✔
1809
                 ? material()
823,610!
UNCOV
1810
                 : lowest_coord().cell();
×
1811

1812
  // If we are reflected and have advanced beyond the camera,
1813
  // the ray is done. This is checked here because we should
1814
  // kill the ray even if the material is not opaque.
1815
  if (reflected_ && (r() - plot_.camera_position()).dot(u()) >= 0.0) {
823,610!
1816
    stop();
×
1817
    return;
149,400✔
1818
  }
1819

1820
  // Anything that's not opaque has zero impact on the plot.
1821
  if (plot_.opaque_ids_.find(hit_id) == plot_.opaque_ids_.end())
823,610✔
1822
    return;
149,400✔
1823

1824
  if (!reflected_) {
674,210✔
1825
    // reflect the particle and set the color to be colored by
1826
    // the normal or the diffuse lighting contribution
1827
    reflected_ = true;
641,880✔
1828
    result_color_ = plot_.colors_[hit_id];
641,880✔
1829
    Direction to_light = plot_.light_location_ - r();
641,880✔
1830
    to_light /= to_light.norm();
641,880✔
1831

1832
    // TODO
1833
    // Not sure what can cause a surface token to be invalid here, although it
1834
    // sometimes happens for a few pixels. It's very very rare, so proceed by
1835
    // coloring the pixel with the overlap color. It seems to happen only for a
1836
    // few pixels on the outer boundary of a hex lattice.
1837
    //
1838
    // We cannot detect it in the outer loop, and it only matters here, so
1839
    // that's why the error handling is a little different than for a lost
1840
    // ray.
1841
    if (surface() == 0) {
641,880!
UNCOV
1842
      result_color_ = plot_.overlap_color_;
×
UNCOV
1843
      stop();
×
UNCOV
1844
      return;
×
1845
    }
1846

1847
    // Get surface pointer
1848
    const auto& surf = model::surfaces.at(surface_index());
641,880✔
1849

1850
    Direction normal = surf->normal(r_local());
641,880✔
1851
    normal /= normal.norm();
641,880✔
1852

1853
    // Need to apply translations to find the normal vector in
1854
    // the base level universe's coordinate system.
1855
    for (int lev = n_coord() - 2; lev >= 0; --lev) {
641,880!
UNCOV
1856
      if (coord(lev + 1).rotated()) {
×
UNCOV
1857
        const Cell& c {*model::cells[coord(lev).cell()]};
×
UNCOV
1858
        normal = normal.inverse_rotate(c.rotation_);
×
1859
      }
1860
    }
1861

1862
    // use the normal opposed to the ray direction
1863
    if (normal.dot(u()) > 0.0) {
641,880✔
1864
      normal *= -1.0;
57,990✔
1865
    }
1866

1867
    // Facing away from the light means no lighting
1868
    double dotprod = normal.dot(to_light);
641,880✔
1869
    dotprod = std::max(0.0, dotprod);
641,880✔
1870

1871
    double modulation =
641,880✔
1872
      plot_.diffuse_fraction_ + (1.0 - plot_.diffuse_fraction_) * dotprod;
641,880✔
1873
    result_color_ *= modulation;
641,880✔
1874

1875
    // Now point the particle to the camera. We now begin
1876
    // checking to see if it's occluded by another surface
1877
    u() = to_light;
641,880✔
1878

1879
    orig_hit_id_ = hit_id;
641,880✔
1880

1881
    // OpenMC native CSG and DAGMC surfaces have some slight differences
1882
    // in how they interpret particles that are sitting on a surface.
1883
    // I don't know exactly why, but this makes everything work beautifully.
1884
    if (surf->geom_type() == GeometryType::DAG) {
641,880!
UNCOV
1885
      surface() = 0;
×
1886
    } else {
1887
      surface() = -surface(); // go to other side
641,880✔
1888
    }
1889

1890
    // Must fully restart coordinate search. Why? Not sure.
1891
    clear();
641,880✔
1892

1893
    // Note this could likely be faster if we cached the previous
1894
    // cell we were in before the reflection. This is the easiest
1895
    // way to fully initialize all the sub-universe coordinates and
1896
    // directions though.
1897
    bool found = exhaustive_find_cell(*this);
641,880✔
1898
    if (!found) {
641,880!
1899
      fatal_error("Lost particle after reflection.");
×
1900
    }
1901

1902
    // Must recalculate distance to boundary due to the
1903
    // direction change
1904
    compute_distance();
641,880✔
1905

1906
  } else {
1907
    // If it's not facing the light, we color with the diffuse contribution, so
1908
    // next we check if we're going to occlude the last reflected surface. if
1909
    // so, color by the diffuse contribution instead
1910

1911
    if (orig_hit_id_ == -1)
32,330!
UNCOV
1912
      fatal_error("somehow a ray got reflected but not original ID set?");
×
1913

1914
    result_color_ = plot_.colors_[orig_hit_id_];
32,330✔
1915
    result_color_ *= plot_.diffuse_fraction_;
32,330✔
1916
    stop();
32,330✔
1917
  }
1918
}
1919

1920
extern "C" int openmc_id_map(const void* plot, int32_t* data_out)
243✔
1921
{
1922

1923
  auto plt = reinterpret_cast<const SlicePlotBase*>(plot);
243✔
1924
  if (!plt) {
243!
1925
    set_errmsg("Invalid slice pointer passed to openmc_id_map");
×
UNCOV
1926
    return OPENMC_E_INVALID_ARGUMENT;
×
1927
  }
1928

1929
  if (plt->slice_color_overlaps_ && model::overlap_check_count.size() == 0) {
243!
1930
    model::overlap_check_count.resize(model::cells.size());
20✔
1931
  }
1932

1933
  auto ids = plt->get_map<IdData>();
243✔
1934

1935
  // write id data to array
1936
  std::copy(ids.data_.begin(), ids.data_.end(), data_out);
243✔
1937

1938
  return 0;
243✔
1939
}
243✔
1940

1941
extern "C" int openmc_property_map(const void* plot, double* data_out)
10✔
1942
{
1943

1944
  auto plt = reinterpret_cast<const SlicePlotBase*>(plot);
10✔
1945
  if (!plt) {
10!
UNCOV
1946
    set_errmsg("Invalid slice pointer passed to openmc_id_map");
×
UNCOV
1947
    return OPENMC_E_INVALID_ARGUMENT;
×
1948
  }
1949

1950
  if (plt->slice_color_overlaps_ && model::overlap_check_count.size() == 0) {
10!
UNCOV
1951
    model::overlap_check_count.resize(model::cells.size());
×
1952
  }
1953

1954
  auto props = plt->get_map<PropertyData>();
10✔
1955

1956
  // write id data to array
1957
  std::copy(props.data_.begin(), props.data_.end(), data_out);
10✔
1958

1959
  return 0;
10✔
1960
}
10✔
1961

1962
extern "C" int openmc_get_plot_index(int32_t id, int32_t* index)
20✔
1963
{
1964
  auto it = model::plot_map.find(id);
20✔
1965
  if (it == model::plot_map.end()) {
20!
UNCOV
1966
    set_errmsg("No plot exists with ID=" + std::to_string(id) + ".");
×
UNCOV
1967
    return OPENMC_E_INVALID_ID;
×
1968
  }
1969

1970
  *index = it->second;
20✔
1971
  return 0;
20✔
1972
}
1973

1974
extern "C" int openmc_plot_get_id(int32_t index, int32_t* id)
50✔
1975
{
1976
  if (index < 0 || index >= model::plots.size()) {
50!
UNCOV
1977
    set_errmsg("Index in plots array is out of bounds.");
×
UNCOV
1978
    return OPENMC_E_OUT_OF_BOUNDS;
×
1979
  }
1980

1981
  *id = model::plots[index]->id();
50✔
1982
  return 0;
50✔
1983
}
1984

UNCOV
1985
extern "C" int openmc_plot_set_id(int32_t index, int32_t id)
×
1986
{
UNCOV
1987
  if (index < 0 || index >= model::plots.size()) {
×
UNCOV
1988
    set_errmsg("Index in plots array is out of bounds.");
×
UNCOV
1989
    return OPENMC_E_OUT_OF_BOUNDS;
×
1990
  }
1991

UNCOV
1992
  if (id < 0 && id != C_NONE) {
×
UNCOV
1993
    set_errmsg("Invalid plot ID.");
×
UNCOV
1994
    return OPENMC_E_INVALID_ARGUMENT;
×
1995
  }
1996

UNCOV
1997
  auto* plot = model::plots[index].get();
×
UNCOV
1998
  int32_t old_id = plot->id();
×
UNCOV
1999
  if (id == old_id)
×
UNCOV
2000
    return 0;
×
2001

UNCOV
2002
  model::plot_map.erase(old_id);
×
2003
  try {
UNCOV
2004
    plot->set_id(id);
×
UNCOV
2005
  } catch (const std::runtime_error& e) {
×
UNCOV
2006
    model::plot_map[old_id] = index;
×
UNCOV
2007
    set_errmsg(e.what());
×
UNCOV
2008
    return OPENMC_E_INVALID_ID;
×
UNCOV
2009
  }
×
UNCOV
2010
  model::plot_map[plot->id()] = index;
×
UNCOV
2011
  return 0;
×
2012
}
2013

2014
extern "C" size_t openmc_plots_size()
20✔
2015
{
2016
  return model::plots.size();
20✔
2017
}
2018

2019
int map_phong_domain_id(
50✔
2020
  const SolidRayTracePlot* plot, int32_t id, int32_t* index_out)
2021
{
2022
  if (!plot || !index_out) {
50!
UNCOV
2023
    set_errmsg("Invalid plot pointer passed to map_phong_domain_id");
×
UNCOV
2024
    return OPENMC_E_INVALID_ARGUMENT;
×
2025
  }
2026

2027
  if (plot->color_by_ == PlottableInterface::PlotColorBy::mats) {
50!
2028
    auto it = model::material_map.find(id);
50✔
2029
    if (it == model::material_map.end()) {
50!
UNCOV
2030
      set_errmsg("Invalid material ID for SolidRayTracePlot");
×
UNCOV
2031
      return OPENMC_E_INVALID_ID;
×
2032
    }
2033
    *index_out = it->second;
50✔
2034
    return 0;
50✔
2035
  }
2036

UNCOV
2037
  if (plot->color_by_ == PlottableInterface::PlotColorBy::cells) {
×
UNCOV
2038
    auto it = model::cell_map.find(id);
×
UNCOV
2039
    if (it == model::cell_map.end()) {
×
UNCOV
2040
      set_errmsg("Invalid cell ID for SolidRayTracePlot");
×
UNCOV
2041
      return OPENMC_E_INVALID_ID;
×
2042
    }
UNCOV
2043
    *index_out = it->second;
×
UNCOV
2044
    return 0;
×
2045
  }
2046

UNCOV
2047
  set_errmsg("Unsupported color_by for SolidRayTracePlot");
×
UNCOV
2048
  return OPENMC_E_INVALID_TYPE;
×
2049
}
2050

2051
int get_solidraytrace_plot_by_index(int32_t index, SolidRayTracePlot** plot)
280✔
2052
{
2053
  if (!plot) {
280!
UNCOV
2054
    set_errmsg("Null output pointer passed to get_solidraytrace_plot_by_index");
×
UNCOV
2055
    return OPENMC_E_INVALID_ARGUMENT;
×
2056
  }
2057

2058
  if (index < 0 || index >= model::plots.size()) {
280!
UNCOV
2059
    set_errmsg("Index in plots array is out of bounds.");
×
UNCOV
2060
    return OPENMC_E_OUT_OF_BOUNDS;
×
2061
  }
2062

2063
  auto* plottable = model::plots[index].get();
280✔
2064
  auto* solid_plot = dynamic_cast<SolidRayTracePlot*>(plottable);
280!
2065
  if (!solid_plot) {
280!
UNCOV
2066
    set_errmsg("Plot at index=" + std::to_string(index) +
×
2067
               " is not a solid raytrace plot.");
UNCOV
2068
    return OPENMC_E_INVALID_TYPE;
×
2069
  }
2070

2071
  *plot = solid_plot;
280✔
2072
  return 0;
280✔
2073
}
2074

2075
extern "C" int openmc_solidraytrace_plot_create(int32_t* index)
10✔
2076
{
2077
  if (!index) {
10!
UNCOV
2078
    set_errmsg(
×
2079
      "Null output pointer passed to openmc_solidraytrace_plot_create");
UNCOV
2080
    return OPENMC_E_INVALID_ARGUMENT;
×
2081
  }
2082

2083
  try {
2084
    auto new_plot = std::make_unique<SolidRayTracePlot>();
10✔
2085
    new_plot->set_id();
10✔
2086
    int32_t new_plot_id = new_plot->id();
10✔
2087
#ifdef USE_LIBPNG
2088
    new_plot->path_plot() = fmt::format("plot_{}.png", new_plot_id);
10✔
2089
#else
2090
    new_plot->path_plot() = fmt::format("plot_{}.ppm", new_plot_id);
2091
#endif
2092
    int32_t new_plot_index = model::plots.size();
10✔
2093
    model::plots.emplace_back(std::move(new_plot));
10✔
2094
    model::plot_map[new_plot_id] = new_plot_index;
10✔
2095
    *index = new_plot_index;
10✔
2096
  } catch (const std::exception& e) {
10!
UNCOV
2097
    set_errmsg(e.what());
×
UNCOV
2098
    return OPENMC_E_ALLOCATE;
×
UNCOV
2099
  }
×
2100

2101
  return 0;
10✔
2102
}
2103

2104
extern "C" int openmc_solidraytrace_plot_get_pixels(
30✔
2105
  int32_t index, int32_t* width, int32_t* height)
2106
{
2107
  if (!width || !height) {
30!
UNCOV
2108
    set_errmsg(
×
2109
      "Invalid arguments passed to openmc_solidraytrace_plot_get_pixels");
UNCOV
2110
    return OPENMC_E_INVALID_ARGUMENT;
×
2111
  }
2112

2113
  SolidRayTracePlot* plt = nullptr;
30✔
2114
  int err = get_solidraytrace_plot_by_index(index, &plt);
30✔
2115
  if (err)
30!
UNCOV
2116
    return err;
×
2117

2118
  *width = plt->pixels()[0];
30✔
2119
  *height = plt->pixels()[1];
30✔
2120
  return 0;
30✔
2121
}
2122

2123
extern "C" int openmc_solidraytrace_plot_set_pixels(
10✔
2124
  int32_t index, int32_t width, int32_t height)
2125
{
2126
  if (width <= 0 || height <= 0) {
10!
UNCOV
2127
    set_errmsg(
×
2128
      "Invalid arguments passed to openmc_solidraytrace_plot_set_pixels");
UNCOV
2129
    return OPENMC_E_INVALID_ARGUMENT;
×
2130
  }
2131

2132
  SolidRayTracePlot* plt = nullptr;
10✔
2133
  int err = get_solidraytrace_plot_by_index(index, &plt);
10✔
2134
  if (err)
10!
UNCOV
2135
    return err;
×
2136

2137
  plt->pixels()[0] = width;
10✔
2138
  plt->pixels()[1] = height;
10✔
2139
  return 0;
10✔
2140
}
2141

2142
extern "C" int openmc_solidraytrace_plot_get_color_by(
10✔
2143
  int32_t index, int32_t* color_by)
2144
{
2145
  if (!color_by) {
10!
UNCOV
2146
    set_errmsg(
×
2147
      "Invalid arguments passed to openmc_solidraytrace_plot_get_color_by");
UNCOV
2148
    return OPENMC_E_INVALID_ARGUMENT;
×
2149
  }
2150

2151
  SolidRayTracePlot* plt = nullptr;
10✔
2152
  int err = get_solidraytrace_plot_by_index(index, &plt);
10✔
2153
  if (err)
10!
UNCOV
2154
    return err;
×
2155

2156
  if (plt->color_by_ == PlottableInterface::PlotColorBy::mats) {
10!
2157
    *color_by = 0;
10✔
UNCOV
2158
  } else if (plt->color_by_ == PlottableInterface::PlotColorBy::cells) {
×
UNCOV
2159
    *color_by = 1;
×
2160
  } else {
UNCOV
2161
    set_errmsg("Unsupported color_by for SolidRayTracePlot");
×
UNCOV
2162
    return OPENMC_E_INVALID_TYPE;
×
2163
  }
2164

2165
  return 0;
10✔
2166
}
2167

2168
extern "C" int openmc_solidraytrace_plot_set_color_by(
10✔
2169
  int32_t index, int32_t color_by)
2170
{
2171
  SolidRayTracePlot* plt = nullptr;
10✔
2172
  int err = get_solidraytrace_plot_by_index(index, &plt);
10✔
2173
  if (err)
10!
UNCOV
2174
    return err;
×
2175

2176
  if (color_by == 0) {
10!
2177
    plt->color_by_ = PlottableInterface::PlotColorBy::mats;
10✔
UNCOV
2178
  } else if (color_by == 1) {
×
UNCOV
2179
    plt->color_by_ = PlottableInterface::PlotColorBy::cells;
×
2180
  } else {
UNCOV
2181
    set_errmsg("Invalid color_by value for SolidRayTracePlot");
×
UNCOV
2182
    return OPENMC_E_INVALID_ARGUMENT;
×
2183
  }
2184

2185
  return 0;
10✔
2186
}
2187

2188
extern "C" int openmc_solidraytrace_plot_set_default_colors(int32_t index)
10✔
2189
{
2190
  SolidRayTracePlot* plt = nullptr;
10✔
2191
  int err = get_solidraytrace_plot_by_index(index, &plt);
10✔
2192
  if (err)
10!
UNCOV
2193
    return err;
×
2194

2195
  plt->set_default_colors();
10✔
2196
  return 0;
10✔
2197
}
2198

UNCOV
2199
extern "C" int openmc_solidraytrace_plot_set_all_opaque(int32_t index)
×
2200
{
UNCOV
2201
  SolidRayTracePlot* plt = nullptr;
×
UNCOV
2202
  int err = get_solidraytrace_plot_by_index(index, &plt);
×
UNCOV
2203
  if (err)
×
UNCOV
2204
    return err;
×
2205

UNCOV
2206
  plt->opaque_ids().clear();
×
UNCOV
2207
  if (plt->color_by_ == PlottableInterface::PlotColorBy::mats) {
×
UNCOV
2208
    for (int32_t i = 0; i < model::materials.size(); ++i) {
×
UNCOV
2209
      plt->opaque_ids().insert(i);
×
2210
    }
UNCOV
2211
    return 0;
×
2212
  }
2213

UNCOV
2214
  if (plt->color_by_ == PlottableInterface::PlotColorBy::cells) {
×
UNCOV
2215
    for (int32_t i = 0; i < model::cells.size(); ++i) {
×
UNCOV
2216
      plt->opaque_ids().insert(i);
×
2217
    }
UNCOV
2218
    return 0;
×
2219
  }
2220

UNCOV
2221
  set_errmsg("Unsupported color_by for SolidRayTracePlot");
×
UNCOV
2222
  return OPENMC_E_INVALID_TYPE;
×
2223
}
2224

2225
extern "C" int openmc_solidraytrace_plot_set_opaque(
20✔
2226
  int32_t index, int32_t id, bool visible)
2227
{
2228
  SolidRayTracePlot* plt = nullptr;
20✔
2229
  int err = get_solidraytrace_plot_by_index(index, &plt);
20✔
2230
  if (err)
20!
UNCOV
2231
    return err;
×
2232

2233
  int32_t domain_index = -1;
20✔
2234
  err = map_phong_domain_id(plt, id, &domain_index);
20✔
2235
  if (err)
20!
UNCOV
2236
    return err;
×
2237

2238
  if (visible) {
20✔
2239
    plt->opaque_ids().insert(domain_index);
10✔
2240
  } else {
2241
    plt->opaque_ids().erase(domain_index);
10✔
2242
  }
2243

2244
  return 0;
20✔
2245
}
2246

2247
extern "C" int openmc_solidraytrace_plot_set_color(
20✔
2248
  int32_t index, int32_t id, uint8_t r, uint8_t g, uint8_t b)
2249
{
2250
  SolidRayTracePlot* plt = nullptr;
20✔
2251
  int err = get_solidraytrace_plot_by_index(index, &plt);
20✔
2252
  if (err)
20!
UNCOV
2253
    return err;
×
2254

2255
  int32_t domain_index = -1;
20✔
2256
  err = map_phong_domain_id(plt, id, &domain_index);
20✔
2257
  if (err)
20!
UNCOV
2258
    return err;
×
2259

2260
  if (domain_index < 0 ||
40!
2261
      static_cast<size_t>(domain_index) >= plt->colors_.size()) {
20✔
UNCOV
2262
    set_errmsg("Color index out of range for SolidRayTracePlot");
×
UNCOV
2263
    return OPENMC_E_OUT_OF_BOUNDS;
×
2264
  }
2265

2266
  plt->colors_[domain_index] = RGBColor(r, g, b);
20✔
2267
  return 0;
20✔
2268
}
2269

2270
extern "C" int openmc_solidraytrace_plot_get_camera_position(
10✔
2271
  int32_t index, double* x, double* y, double* z)
2272
{
2273
  if (!x || !y || !z) {
10!
UNCOV
2274
    set_errmsg("Invalid arguments passed to "
×
2275
               "openmc_solidraytrace_plot_get_camera_position");
UNCOV
2276
    return OPENMC_E_INVALID_ARGUMENT;
×
2277
  }
2278

2279
  SolidRayTracePlot* plt = nullptr;
10✔
2280
  int err = get_solidraytrace_plot_by_index(index, &plt);
10✔
2281
  if (err)
10!
UNCOV
2282
    return err;
×
2283

2284
  const auto& camera_position = plt->camera_position();
10✔
2285
  *x = camera_position.x;
10✔
2286
  *y = camera_position.y;
10✔
2287
  *z = camera_position.z;
10✔
2288
  return 0;
10✔
2289
}
2290

2291
extern "C" int openmc_solidraytrace_plot_set_camera_position(
10✔
2292
  int32_t index, double x, double y, double z)
2293
{
2294
  SolidRayTracePlot* plt = nullptr;
10✔
2295
  int err = get_solidraytrace_plot_by_index(index, &plt);
10✔
2296
  if (err)
10!
UNCOV
2297
    return err;
×
2298

2299
  plt->camera_position() = {x, y, z};
10✔
2300
  return 0;
10✔
2301
}
2302

2303
extern "C" int openmc_solidraytrace_plot_get_look_at(
10✔
2304
  int32_t index, double* x, double* y, double* z)
2305
{
2306
  if (!x || !y || !z) {
10!
UNCOV
2307
    set_errmsg(
×
2308
      "Invalid arguments passed to openmc_solidraytrace_plot_get_look_at");
UNCOV
2309
    return OPENMC_E_INVALID_ARGUMENT;
×
2310
  }
2311

2312
  SolidRayTracePlot* plt = nullptr;
10✔
2313
  int err = get_solidraytrace_plot_by_index(index, &plt);
10✔
2314
  if (err)
10!
UNCOV
2315
    return err;
×
2316

2317
  const auto& look_at = plt->look_at();
10✔
2318
  *x = look_at.x;
10✔
2319
  *y = look_at.y;
10✔
2320
  *z = look_at.z;
10✔
2321
  return 0;
10✔
2322
}
2323

2324
extern "C" int openmc_solidraytrace_plot_set_look_at(
10✔
2325
  int32_t index, double x, double y, double z)
2326
{
2327
  SolidRayTracePlot* plt = nullptr;
10✔
2328
  int err = get_solidraytrace_plot_by_index(index, &plt);
10✔
2329
  if (err)
10!
UNCOV
2330
    return err;
×
2331

2332
  plt->look_at() = {x, y, z};
10✔
2333
  return 0;
10✔
2334
}
2335

2336
extern "C" int openmc_solidraytrace_plot_get_up(
10✔
2337
  int32_t index, double* x, double* y, double* z)
2338
{
2339
  if (!x || !y || !z) {
10!
UNCOV
2340
    set_errmsg("Invalid arguments passed to openmc_solidraytrace_plot_get_up");
×
UNCOV
2341
    return OPENMC_E_INVALID_ARGUMENT;
×
2342
  }
2343

2344
  SolidRayTracePlot* plt = nullptr;
10✔
2345
  int err = get_solidraytrace_plot_by_index(index, &plt);
10✔
2346
  if (err)
10!
UNCOV
2347
    return err;
×
2348

2349
  const auto& up = plt->up();
10✔
2350
  *x = up.x;
10✔
2351
  *y = up.y;
10✔
2352
  *z = up.z;
10✔
2353
  return 0;
10✔
2354
}
2355

2356
extern "C" int openmc_solidraytrace_plot_set_up(
10✔
2357
  int32_t index, double x, double y, double z)
2358
{
2359
  SolidRayTracePlot* plt = nullptr;
10✔
2360
  int err = get_solidraytrace_plot_by_index(index, &plt);
10✔
2361
  if (err)
10!
UNCOV
2362
    return err;
×
2363

2364
  plt->up() = {x, y, z};
10✔
2365
  return 0;
10✔
2366
}
2367

2368
extern "C" int openmc_solidraytrace_plot_get_light_position(
10✔
2369
  int32_t index, double* x, double* y, double* z)
2370
{
2371
  if (!x || !y || !z) {
10!
UNCOV
2372
    set_errmsg("Invalid arguments passed to "
×
2373
               "openmc_solidraytrace_plot_get_light_position");
UNCOV
2374
    return OPENMC_E_INVALID_ARGUMENT;
×
2375
  }
2376

2377
  SolidRayTracePlot* plt = nullptr;
10✔
2378
  int err = get_solidraytrace_plot_by_index(index, &plt);
10✔
2379
  if (err)
10!
UNCOV
2380
    return err;
×
2381

2382
  const auto& light_position = plt->light_location();
10✔
2383
  *x = light_position.x;
10✔
2384
  *y = light_position.y;
10✔
2385
  *z = light_position.z;
10✔
2386
  return 0;
10✔
2387
}
2388

2389
extern "C" int openmc_solidraytrace_plot_set_light_position(
10✔
2390
  int32_t index, double x, double y, double z)
2391
{
2392
  SolidRayTracePlot* plt = nullptr;
10✔
2393
  int err = get_solidraytrace_plot_by_index(index, &plt);
10✔
2394
  if (err)
10!
UNCOV
2395
    return err;
×
2396

2397
  plt->light_location() = {x, y, z};
10✔
2398
  return 0;
10✔
2399
}
2400

2401
extern "C" int openmc_solidraytrace_plot_get_fov(int32_t index, double* fov)
10✔
2402
{
2403
  if (!fov) {
10!
UNCOV
2404
    set_errmsg("Invalid arguments passed to openmc_solidraytrace_plot_get_fov");
×
UNCOV
2405
    return OPENMC_E_INVALID_ARGUMENT;
×
2406
  }
2407

2408
  SolidRayTracePlot* plt = nullptr;
10✔
2409
  int err = get_solidraytrace_plot_by_index(index, &plt);
10✔
2410
  if (err)
10!
UNCOV
2411
    return err;
×
2412

2413
  *fov = plt->horizontal_field_of_view();
10✔
2414
  return 0;
10✔
2415
}
2416

2417
extern "C" int openmc_solidraytrace_plot_set_fov(int32_t index, double fov)
10✔
2418
{
2419
  SolidRayTracePlot* plt = nullptr;
10✔
2420
  int err = get_solidraytrace_plot_by_index(index, &plt);
10✔
2421
  if (err)
10!
UNCOV
2422
    return err;
×
2423

2424
  plt->horizontal_field_of_view() = fov;
10✔
2425
  return 0;
10✔
2426
}
2427

2428
extern "C" int openmc_solidraytrace_plot_update_view(int32_t index)
20✔
2429
{
2430
  SolidRayTracePlot* plt = nullptr;
20✔
2431
  int err = get_solidraytrace_plot_by_index(index, &plt);
20✔
2432
  if (err)
20!
UNCOV
2433
    return err;
×
2434

2435
  plt->update_view();
20✔
2436
  return 0;
20✔
2437
}
2438

2439
extern "C" int openmc_solidraytrace_plot_create_image(
20✔
2440
  int32_t index, uint8_t* data_out, int32_t width, int32_t height)
2441
{
2442
  if (!data_out || width <= 0 || height <= 0) {
20!
UNCOV
2443
    set_errmsg(
×
2444
      "Invalid arguments passed to openmc_solidraytrace_plot_create_image");
UNCOV
2445
    return OPENMC_E_INVALID_ARGUMENT;
×
2446
  }
2447

2448
  SolidRayTracePlot* plt = nullptr;
20✔
2449
  int err = get_solidraytrace_plot_by_index(index, &plt);
20✔
2450
  if (err)
20!
UNCOV
2451
    return err;
×
2452

2453
  if (plt->pixels()[0] != width || plt->pixels()[1] != height) {
20!
UNCOV
2454
    set_errmsg(
×
2455
      "Requested image size does not match SolidRayTracePlot pixel settings");
UNCOV
2456
    return OPENMC_E_INVALID_SIZE;
×
2457
  }
2458

2459
  ImageData data = plt->create_image();
20✔
2460
  if (static_cast<int32_t>(data.shape()[0]) != width ||
40!
2461
      static_cast<int32_t>(data.shape()[1]) != height) {
20!
UNCOV
2462
    set_errmsg("Unexpected image size from SolidRayTracePlot create_image");
×
UNCOV
2463
    return OPENMC_E_INVALID_SIZE;
×
2464
  }
2465

2466
  for (int32_t y = 0; y < height; ++y) {
140✔
2467
    for (int32_t x = 0; x < width; ++x) {
1,080✔
2468
      const auto& color = data(x, y);
960✔
2469
      size_t idx = (static_cast<size_t>(y) * width + x) * 3;
960✔
2470
      data_out[idx + 0] = color.red;
960✔
2471
      data_out[idx + 1] = color.green;
960✔
2472
      data_out[idx + 2] = color.blue;
960✔
2473
    }
2474
  }
2475

2476
  return 0;
20✔
2477
}
20✔
2478

2479
extern "C" int openmc_solidraytrace_plot_get_color(
10✔
2480
  int32_t index, int32_t id, uint8_t* r, uint8_t* g, uint8_t* b)
2481
{
2482
  if (!r || !g || !b) {
10!
UNCOV
2483
    set_errmsg(
×
2484
      "Invalid arguments passed to openmc_solidraytrace_plot_get_color");
UNCOV
2485
    return OPENMC_E_INVALID_ARGUMENT;
×
2486
  }
2487

2488
  SolidRayTracePlot* plt = nullptr;
10✔
2489
  int err = get_solidraytrace_plot_by_index(index, &plt);
10✔
2490
  if (err)
10!
UNCOV
2491
    return err;
×
2492

2493
  int32_t domain_index = -1;
10✔
2494
  err = map_phong_domain_id(plt, id, &domain_index);
10✔
2495
  if (err)
10!
UNCOV
2496
    return err;
×
2497

2498
  if (domain_index < 0 ||
20!
2499
      static_cast<size_t>(domain_index) >= plt->colors_.size()) {
10✔
UNCOV
2500
    set_errmsg("Color index out of range for SolidRayTracePlot");
×
UNCOV
2501
    return OPENMC_E_OUT_OF_BOUNDS;
×
2502
  }
2503

2504
  const auto& color = plt->colors_[domain_index];
10✔
2505
  *r = color.red;
10✔
2506
  *g = color.green;
10✔
2507
  *b = color.blue;
10✔
2508
  return 0;
10✔
2509
}
2510

2511
extern "C" int openmc_solidraytrace_plot_get_diffuse_fraction(
10✔
2512
  int32_t index, double* diffuse_fraction)
2513
{
2514
  if (!diffuse_fraction) {
10!
UNCOV
2515
    set_errmsg("Invalid arguments passed to "
×
2516
               "openmc_solidraytrace_plot_get_diffuse_fraction");
UNCOV
2517
    return OPENMC_E_INVALID_ARGUMENT;
×
2518
  }
2519

2520
  SolidRayTracePlot* plt = nullptr;
10✔
2521
  int err = get_solidraytrace_plot_by_index(index, &plt);
10✔
2522
  if (err)
10!
UNCOV
2523
    return err;
×
2524

2525
  *diffuse_fraction = plt->diffuse_fraction();
10✔
2526
  return 0;
10✔
2527
}
2528

2529
extern "C" int openmc_solidraytrace_plot_set_diffuse_fraction(
10✔
2530
  int32_t index, double diffuse_fraction)
2531
{
2532
  SolidRayTracePlot* plt = nullptr;
10✔
2533
  int err = get_solidraytrace_plot_by_index(index, &plt);
10✔
2534
  if (err)
10!
UNCOV
2535
    return err;
×
2536

2537
  if (diffuse_fraction < 0.0 || diffuse_fraction > 1.0) {
10!
UNCOV
2538
    set_errmsg("Diffuse fraction must be between 0 and 1");
×
UNCOV
2539
    return OPENMC_E_INVALID_ARGUMENT;
×
2540
  }
2541

2542
  plt->diffuse_fraction() = diffuse_fraction;
10✔
2543
  return 0;
10✔
2544
}
2545

2546
} // namespace openmc
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc