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

openmc-dev / openmc / 27319341588

11 Jun 2026 02:16AM UTC coverage: 81.244% (-0.04%) from 81.281%
27319341588

Pull #3969

github

web-flow
Merge 25a68b6ea into 02eb999af
Pull Request #3969: Overlap detection for plotter

18177 of 26406 branches covered (68.84%)

Branch coverage included in aggregate %.

73 of 86 new or added lines in 5 files covered. (84.88%)

2 existing lines in 2 files now uncovered.

59310 of 68970 relevant lines covered (85.99%)

48357958.5 hits per line

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

69.24
/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
#include "openmc/tallies/filter.h"
37

38
namespace openmc {
39

40
//==============================================================================
41
// Constants
42
//==============================================================================
43

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

48
IdData::IdData(size_t h_res, size_t v_res, bool /*include_filter*/)
4,873✔
49
  : data_({v_res, h_res, 3}, NOT_FOUND)
4,873✔
50
{}
4,873✔
51

52
void IdData::set_value(size_t y, size_t x, const Particle& p, int level,
35,403,192✔
53
  Filter* /*filter*/, FilterMatch* /*match*/)
54
{
55
  // set cell data
56
  if (p.n_coord() <= level) {
35,403,192!
57
    data_(y, x, 0) = NOT_FOUND;
×
58
    data_(y, x, 1) = NOT_FOUND;
×
59
  } else {
60
    data_(y, x, 0) = model::cells.at(p.coord(level).cell())->id_;
35,403,192!
61
    data_(y, x, 1) = level == p.n_coord() - 1
35,403,192✔
62
                       ? p.cell_instance()
35,403,192!
63
                       : cell_instance_at_level(p, level);
×
64
  }
65

66
  // set material data
67
  Cell* c = model::cells.at(p.lowest_coord().cell()).get();
35,403,192✔
68
  if (p.material() == MATERIAL_VOID) {
35,403,192✔
69
    data_(y, x, 2) = MATERIAL_VOID;
27,301,736✔
70
  } else if (c->type_ == Fill::MATERIAL) {
8,101,456!
71
    Material* m = model::materials.at(p.material()).get();
8,101,456✔
72
    data_(y, x, 2) = m->id_;
8,101,456✔
73
  }
74
}
35,403,192✔
75

76
void IdData::set_overlap(
28,248✔
77
  size_t y, size_t x, const std::vector<OverlapKey>& /*pairs*/)
78
{
79
  for (size_t k = 0; k < data_.shape(2); ++k)
225,984!
80
    data_(y, x, k) = OVERLAP;
84,744✔
81
}
28,248✔
82

83
PropertyData::PropertyData(size_t h_res, size_t v_res, bool /*include_filter*/)
×
84
  : data_({v_res, h_res, 2}, NOT_FOUND)
×
85
{}
×
86

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

NEW
98
void PropertyData::set_overlap(
×
99
  size_t y, size_t x, const std::vector<OverlapKey>& /*pairs*/)
100
{
101
  data_(y, x) = OVERLAP;
×
102
}
×
103

104
//==============================================================================
105
// RasterData implementation
106
//==============================================================================
107

108
RasterData::RasterData(size_t h_res, size_t v_res, bool include_filter)
432✔
109
  : id_data_({v_res, h_res, include_filter ? 4u : 3u}, NOT_FOUND),
842✔
110
    property_data_({v_res, h_res, 2}, static_cast<double>(NOT_FOUND)),
432✔
111
    include_filter_(include_filter), pixel_overlaps_(h_res * v_res)
432✔
112
{}
432✔
113

114
void RasterData::set_value(size_t y, size_t x, const Particle& p, int level,
3,626,082✔
115
  Filter* filter, FilterMatch* match)
116
{
117
  // set cell data
118
  if (p.n_coord() <= level) {
3,626,082!
119
    id_data_(y, x, 0) = NOT_FOUND;
×
120
    id_data_(y, x, 1) = NOT_FOUND;
×
121
  } else {
122
    id_data_(y, x, 0) = model::cells.at(p.coord(level).cell())->id_;
3,626,082!
123
    id_data_(y, x, 1) = level == p.n_coord() - 1
3,626,082✔
124
                          ? p.cell_instance()
3,626,082!
125
                          : cell_instance_at_level(p, level);
×
126
  }
127

128
  // set material data
129
  Cell* c = model::cells.at(p.lowest_coord().cell()).get();
3,626,082✔
130
  if (p.material() == MATERIAL_VOID) {
3,626,082✔
131
    id_data_(y, x, 2) = MATERIAL_VOID;
2,502,540✔
132
  } else if (c->type_ == Fill::MATERIAL) {
1,123,542!
133
    Material* m = model::materials.at(p.material()).get();
1,123,542✔
134
    id_data_(y, x, 2) = m->id_;
1,123,542✔
135
  }
136

137
  // set filter index (only if filter is being used)
138
  if (include_filter_ && filter) {
3,626,082!
139
    filter->get_all_bins(p, TallyEstimator::COLLISION, *match);
55,000✔
140
    if (match->bins_.empty()) {
55,000!
141
      id_data_(y, x, 3) = -1;
×
142
    } else {
143
      id_data_(y, x, 3) = match->bins_[0];
55,000✔
144
    }
145
    match->bins_.clear();
55,000!
146
    match->weights_.clear();
55,000!
147
  }
148

149
  // set temperature (in K)
150
  property_data_(y, x, 0) = (p.sqrtkT() * p.sqrtkT()) / K_BOLTZMANN;
3,626,082✔
151

152
  // set density (g/cm³)
153
  if (c->type_ != Fill::UNIVERSE && p.material() != MATERIAL_VOID) {
3,626,082!
154
    Material* m = model::materials.at(p.material()).get();
1,123,542✔
155
    property_data_(y, x, 1) = m->density_gpcc_;
1,123,542✔
156
  }
157
}
3,626,082✔
158

159
void RasterData::set_overlap(
351,274✔
160
  size_t y, size_t x, const std::vector<OverlapKey>& pairs)
161
{
162
  // Set cell, instance, and material to OVERLAP, but preserve filter bin
163
  id_data_(y, x, 0) = OVERLAP;
351,274✔
164
  id_data_(y, x, 1) = OVERLAP;
351,274✔
165
  id_data_(y, x, 2) = OVERLAP;
351,274✔
166
  // Note: id_data_(y, x, 3) is NOT overwritten - preserves filter bin for tally
167
  // plotting
168

169
  property_data_(y, x, 0) = OVERLAP;
351,274✔
170
  property_data_(y, x, 1) = OVERLAP;
351,274✔
171

172
  size_t pix = y * id_data_.shape(1) + x;
351,274!
173
  pixel_overlaps_[pix] = pairs;
351,274✔
174
}
351,274✔
175

176
//==============================================================================
177
// Global variables
178
//==============================================================================
179

180
namespace model {
181

182
std::unordered_map<int, int> plot_map;
183
vector<std::unique_ptr<PlottableInterface>> plots;
184
uint64_t plotter_seed = 1;
185

186
std::unique_ptr<RasterData> last_slice_data;
187

188
} // namespace model
189

190
//==============================================================================
191
// RUN_PLOT controls the logic for making one or many plots
192
//==============================================================================
193

194
extern "C" int openmc_plot_geometry()
121✔
195
{
196

197
  for (auto& pl : model::plots) {
407✔
198
    write_message(5, "Processing plot {}: {}...", pl->id(), pl->path_plot());
286✔
199
    pl->create_output();
286✔
200
  }
201

202
  return 0;
121✔
203
}
204

205
void PlottableInterface::write_image(const ImageData& data) const
231✔
206
{
207
#ifdef USE_LIBPNG
208
  output_png(path_plot(), data);
231✔
209
#else
210
  output_ppm(path_plot(), data);
211
#endif
212
}
231✔
213

214
void Plot::create_output() const
198✔
215
{
216
  if (PlotType::slice == type_) {
198✔
217
    // create 2D image
218
    ImageData image = create_image();
143✔
219
    write_image(image);
143✔
220
  } else if (PlotType::voxel == type_) {
198!
221
    // create voxel file for 3D viewing
222
    create_voxel();
55✔
223
  }
224
}
198✔
225

226
void Plot::print_info() const
154✔
227
{
228
  // Plot type
229
  if (PlotType::slice == type_) {
154✔
230
    fmt::print("Plot Type: Slice\n");
121✔
231
  } else if (PlotType::voxel == type_) {
33!
232
    fmt::print("Plot Type: Voxel\n");
33✔
233
  }
234

235
  // Plot parameters
236
  fmt::print("Origin: {} {} {}\n", origin_[0], origin_[1], origin_[2]);
154✔
237

238
  if (PlotType::slice == type_) {
154✔
239
    fmt::print("Width: {:4} {:4}\n", width_[0], width_[1]);
121✔
240
  } else if (PlotType::voxel == type_) {
33!
241
    fmt::print("Width: {:4} {:4} {:4}\n", width_[0], width_[1], width_[2]);
33✔
242
  }
243

244
  if (PlotColorBy::cells == color_by_) {
154✔
245
    fmt::print("Coloring: Cells\n");
88✔
246
  } else if (PlotColorBy::mats == color_by_) {
66!
247
    fmt::print("Coloring: Materials\n");
66✔
248
  }
249

250
  if (PlotType::slice == type_) {
154✔
251
    switch (basis_) {
121!
252
    case PlotBasis::xy:
77✔
253
      fmt::print("Basis: XY\n");
77✔
254
      break;
77✔
255
    case PlotBasis::xz:
22✔
256
      fmt::print("Basis: XZ\n");
22✔
257
      break;
22✔
258
    case PlotBasis::yz:
22✔
259
      fmt::print("Basis: YZ\n");
22✔
260
      break;
22✔
261
    }
262
    fmt::print("Pixels: {} {}\n", pixels()[0], pixels()[1]);
121✔
263
  } else if (PlotType::voxel == type_) {
33!
264
    fmt::print("Voxels: {} {} {}\n", pixels()[0], pixels()[1], pixels()[2]);
33✔
265
  }
266
}
154✔
267

268
void read_plots_xml()
1,399✔
269
{
270
  // Check if plots.xml exists; this is only necessary when the plot runmode is
271
  // initiated. Otherwise, we want to read plots.xml because it may be called
272
  // later via the API. In that case, its ok for a plots.xml to not exist
273
  std::string filename = settings::path_input + "plots.xml";
1,399✔
274
  if (!file_exists(filename) && settings::run_mode == RunMode::PLOTTING) {
1,399!
275
    fatal_error(fmt::format("Plots XML file '{}' does not exist!", filename));
×
276
  }
277

278
  write_message("Reading plot XML file...", 5);
1,399✔
279

280
  // Parse plots.xml file
281
  pugi::xml_document doc;
1,399✔
282
  doc.load_file(filename.c_str());
1,399✔
283

284
  pugi::xml_node root = doc.document_element();
1,399✔
285

286
  read_plots_xml(root);
1,399✔
287
}
1,399✔
288

289
void read_plots_xml(pugi::xml_node root)
1,885✔
290
{
291
  for (auto node : root.children("plot")) {
2,855✔
292
    std::string plot_desc = "<auto>";
979✔
293
    if (check_for_node(node, "id")) {
979!
294
      plot_desc = get_node_value(node, "id", true);
979✔
295
    }
296

297
    if (check_for_node(node, "type")) {
979!
298
      std::string type_str = get_node_value(node, "type", true);
979✔
299
      if (type_str == "slice") {
979✔
300
        model::plots.emplace_back(
827✔
301
          std::make_unique<Plot>(node, Plot::PlotType::slice));
1,663✔
302
      } else if (type_str == "voxel") {
143✔
303
        model::plots.emplace_back(
55✔
304
          std::make_unique<Plot>(node, Plot::PlotType::voxel));
110✔
305
      } else if (type_str == "wireframe_raytrace") {
88✔
306
        model::plots.emplace_back(
55✔
307
          std::make_unique<WireframeRayTracePlot>(node));
110✔
308
      } else if (type_str == "solid_raytrace") {
33!
309
        model::plots.emplace_back(std::make_unique<SolidRayTracePlot>(node));
33✔
310
      } else {
311
        fatal_error(fmt::format(
×
312
          "Unsupported plot type '{}' in plot {}", type_str, plot_desc));
313
      }
314
      model::plot_map[model::plots.back()->id()] = model::plots.size() - 1;
970✔
315
    } else {
970✔
316
      fatal_error(fmt::format("Must specify plot type in plot {}", plot_desc));
×
317
    }
318
  }
970✔
319
}
1,876✔
320

321
void free_memory_plot()
8,887✔
322
{
323
  model::plots.clear();
8,887✔
324
  model::plot_map.clear();
8,887✔
325
}
8,887✔
326

327
// creates an image based on user input from a plots.xml <plot>
328
// specification in the PNG/PPM format
329
ImageData Plot::create_image() const
143✔
330
{
331
  size_t width = pixels()[0];
143✔
332
  size_t height = pixels()[1];
143✔
333

334
  ImageData data({width, height}, not_found_);
143✔
335

336
  // generate ids for the plot
337
  auto ids = get_map<IdData>();
143✔
338

339
  // assign colors
340
  for (size_t y = 0; y < height; y++) {
30,063✔
341
    for (size_t x = 0; x < width; x++) {
7,622,120✔
342
      int idx = color_by_ == PlotColorBy::cells ? 0 : 2;
7,592,200✔
343
      auto id = ids.data_(y, x, idx);
7,592,200✔
344
      // no setting needed if not found
345
      if (id == NOT_FOUND) {
7,592,200✔
346
        continue;
1,082,532✔
347
      }
348
      if (id == OVERLAP) {
6,537,916✔
349
        data(x, y) = overlap_color_;
28,248✔
350
        continue;
28,248✔
351
      }
352
      if (PlotColorBy::cells == color_by_) {
6,509,668✔
353
        data(x, y) = colors_[model::cell_map[id]];
3,011,668✔
354
      } else if (PlotColorBy::mats == color_by_) {
3,498,000!
355
        if (id == MATERIAL_VOID) {
3,498,000!
356
          data(x, y) = WHITE;
×
357
          continue;
×
358
        }
359
        data(x, y) = colors_[model::material_map[id]];
3,498,000✔
360
      } // color_by if-else
361
    }
362
  }
363

364
  // draw mesh lines if present
365
  if (index_meshlines_mesh_ >= 0) {
143✔
366
    draw_mesh_lines(data);
33✔
367
  }
368

369
  return data;
143✔
370
}
143✔
371

372
void PlottableInterface::set_id(pugi::xml_node plot_node)
979✔
373
{
374
  int id {C_NONE};
979✔
375
  if (check_for_node(plot_node, "id")) {
979!
376
    id = std::stoi(get_node_value(plot_node, "id"));
979✔
377
  }
378

379
  try {
979✔
380
    set_id(id);
979✔
381
  } catch (const std::runtime_error& e) {
×
382
    fatal_error(e.what());
×
383
  }
×
384
}
979✔
385

386
void PlottableInterface::set_id(int id)
990✔
387
{
388
  if (id < 0 && id != C_NONE) {
990!
389
    throw std::runtime_error {fmt::format("Invalid plot ID: {}", id)};
×
390
  }
391

392
  if (id == C_NONE) {
990✔
393
    id = 1;
11✔
394
    for (const auto& p : model::plots) {
22✔
395
      id = std::max(id, p->id() + 1);
22!
396
    }
397
  }
398

399
  if (id_ == id)
990!
400
    return;
401

402
  // Check to make sure this ID doesn't already exist
403
  if (model::plot_map.find(id) != model::plot_map.end()) {
990!
404
    throw std::runtime_error {
×
405
      fmt::format("Two or more plots use the same unique ID: {}", id)};
×
406
  }
407

408
  id_ = id;
990✔
409
}
410

411
// Checks if png or ppm is already present
412
bool file_extension_present(
970✔
413
  const std::string& filename, const std::string& extension)
414
{
415
  std::string file_extension_if_present =
970✔
416
    filename.substr(filename.find_last_of(".") + 1);
970✔
417
  if (file_extension_if_present == extension)
970✔
418
    return true;
55✔
419
  return false;
420
}
970✔
421

422
void Plot::set_output_path(pugi::xml_node plot_node)
891✔
423
{
424
  // Set output file path
425
  std::string filename;
891✔
426

427
  if (check_for_node(plot_node, "filename")) {
891✔
428
    filename = get_node_value(plot_node, "filename");
242✔
429
  } else {
430
    filename = fmt::format("plot_{}", id());
649✔
431
  }
432
  const std::string dir_if_present =
891✔
433
    filename.substr(0, filename.find_last_of("/") + 1);
891✔
434
  if (dir_if_present.size() > 0 && !dir_exists(dir_if_present)) {
891✔
435
    fatal_error(fmt::format("Directory '{}' does not exist!", dir_if_present));
9✔
436
  }
437
  // add appropriate file extension to name
438
  switch (type_) {
882!
439
  case PlotType::slice:
827✔
440
#ifdef USE_LIBPNG
441
    if (!file_extension_present(filename, "png"))
827!
442
      filename.append(".png");
827✔
443
#else
444
    if (!file_extension_present(filename, "ppm"))
445
      filename.append(".ppm");
446
#endif
447
    break;
448
  case PlotType::voxel:
55✔
449
    if (!file_extension_present(filename, "h5"))
55!
450
      filename.append(".h5");
55✔
451
    break;
452
  }
453

454
  path_plot_ = filename;
882✔
455

456
  // Copy plot pixel size
457
  vector<int> pxls = get_node_array<int>(plot_node, "pixels");
1,764✔
458
  if (PlotType::slice == type_) {
882✔
459
    if (pxls.size() == 2) {
827!
460
      pixels()[0] = pxls[0];
827✔
461
      pixels()[1] = pxls[1];
827✔
462
    } else {
463
      fatal_error(
×
464
        fmt::format("<pixels> must be length 2 in slice plot {}", id()));
×
465
    }
466
  } else if (PlotType::voxel == type_) {
55!
467
    if (pxls.size() == 3) {
55!
468
      pixels()[0] = pxls[0];
55✔
469
      pixels()[1] = pxls[1];
55✔
470
      pixels()[2] = pxls[2];
55✔
471
    } else {
472
      fatal_error(
×
473
        fmt::format("<pixels> must be length 3 in voxel plot {}", id()));
×
474
    }
475
  }
476
}
882✔
477

478
void PlottableInterface::set_bg_color(pugi::xml_node plot_node)
979✔
479
{
480
  // Copy plot background color
481
  if (check_for_node(plot_node, "background")) {
979✔
482
    vector<int> bg_rgb = get_node_array<int>(plot_node, "background");
44✔
483
    if (bg_rgb.size() == 3) {
44!
484
      not_found_ = bg_rgb;
44✔
485
    } else {
486
      fatal_error(fmt::format("Bad background RGB in plot {}", id()));
×
487
    }
488
  }
44✔
489
}
979✔
490

491
void Plot::set_basis(pugi::xml_node plot_node)
882✔
492
{
493
  // Copy plot basis
494
  if (PlotType::slice == type_) {
882✔
495
    std::string pl_basis = "xy";
827✔
496
    if (check_for_node(plot_node, "basis")) {
827!
497
      pl_basis = get_node_value(plot_node, "basis", true);
827✔
498
    }
499
    if ("xy" == pl_basis) {
827✔
500
      basis_ = PlotBasis::xy;
753✔
501
    } else if ("xz" == pl_basis) {
74✔
502
      basis_ = PlotBasis::xz;
22✔
503
    } else if ("yz" == pl_basis) {
52!
504
      basis_ = PlotBasis::yz;
52✔
505
    } else {
506
      fatal_error(
×
507
        fmt::format("Unsupported plot basis '{}' in plot {}", pl_basis, id()));
×
508
    }
509
  }
827✔
510
}
882✔
511

512
void Plot::set_origin(pugi::xml_node plot_node)
882✔
513
{
514
  // Copy plotting origin
515
  auto pl_origin = get_node_array<double>(plot_node, "origin");
882✔
516
  if (pl_origin.size() == 3) {
882!
517
    origin_ = pl_origin;
882✔
518
  } else {
519
    fatal_error(fmt::format("Origin must be length 3 in plot {}", id()));
×
520
  }
521
}
882✔
522

523
void Plot::set_width(pugi::xml_node plot_node)
882✔
524
{
525
  // Copy plotting width
526
  vector<double> pl_width = get_node_array<double>(plot_node, "width");
882✔
527
  if (PlotType::slice == type_) {
882✔
528
    if (pl_width.size() == 2) {
827!
529
      width_.x = pl_width[0];
827✔
530
      width_.y = pl_width[1];
827✔
531
      switch (basis_) {
827!
532
      case PlotBasis::xy:
753✔
533
        u_span_ = {width_.x, 0.0, 0.0};
753✔
534
        v_span_ = {0.0, width_.y, 0.0};
753✔
535
        break;
753✔
536
      case PlotBasis::xz:
22✔
537
        u_span_ = {width_.x, 0.0, 0.0};
22✔
538
        v_span_ = {0.0, 0.0, width_.y};
22✔
539
        break;
22✔
540
      case PlotBasis::yz:
52✔
541
        u_span_ = {0.0, width_.x, 0.0};
52✔
542
        v_span_ = {0.0, 0.0, width_.y};
52✔
543
        break;
52✔
544
      default:
×
545
        UNREACHABLE();
×
546
      }
547
    } else {
548
      fatal_error(
×
549
        fmt::format("<width> must be length 2 in slice plot {}", id()));
×
550
    }
551
  } else if (PlotType::voxel == type_) {
55!
552
    if (pl_width.size() == 3) {
55!
553
      pl_width = get_node_array<double>(plot_node, "width");
110✔
554
      width_ = pl_width;
55✔
555
    } else {
556
      fatal_error(
×
557
        fmt::format("<width> must be length 3 in voxel plot {}", id()));
×
558
    }
559
  }
560
}
882✔
561

562
void PlottableInterface::set_universe(pugi::xml_node plot_node)
979✔
563
{
564
  // Copy plot universe level
565
  if (check_for_node(plot_node, "level")) {
979!
566
    level_ = std::stoi(get_node_value(plot_node, "level"));
×
567
    if (level_ < 0) {
×
568
      fatal_error(fmt::format("Bad universe level in plot {}", id()));
×
569
    }
570
  } else {
571
    level_ = PLOT_LEVEL_LOWEST;
979✔
572
  }
573
}
979✔
574

575
void PlottableInterface::set_color_by(pugi::xml_node plot_node)
979✔
576
{
577
  // Copy plot color type
578
  std::string pl_color_by = "cell";
979✔
579
  if (check_for_node(plot_node, "color_by")) {
979✔
580
    pl_color_by = get_node_value(plot_node, "color_by", true);
946✔
581
  }
582
  if ("cell" == pl_color_by) {
979✔
583
    color_by_ = PlotColorBy::cells;
287✔
584
  } else if ("material" == pl_color_by) {
692!
585
    color_by_ = PlotColorBy::mats;
692✔
586
  } else {
587
    fatal_error(fmt::format(
×
588
      "Unsupported plot color type '{}' in plot {}", pl_color_by, id()));
×
589
  }
590
}
979✔
591

592
void PlottableInterface::set_default_colors()
990✔
593
{
594
  // Copy plot color type and initialize all colors randomly
595
  if (PlotColorBy::cells == color_by_) {
990✔
596
    colors_.resize(model::cells.size());
287✔
597
  } else if (PlotColorBy::mats == color_by_) {
703!
598
    colors_.resize(model::materials.size());
703✔
599
  }
600

601
  for (auto& c : colors_) {
4,431✔
602
    c = random_color();
3,441✔
603
    // make sure we don't interfere with some default colors
604
    while (c == RED || c == WHITE) {
3,441!
605
      c = random_color();
×
606
    }
607
  }
608
}
990✔
609

610
void PlottableInterface::set_user_colors(pugi::xml_node plot_node)
979✔
611
{
612
  for (auto cn : plot_node.children("color")) {
1,166✔
613
    // Make sure 3 values are specified for RGB
614
    vector<int> user_rgb = get_node_array<int>(cn, "rgb");
187✔
615
    if (user_rgb.size() != 3) {
187!
616
      fatal_error(fmt::format("Bad RGB in plot {}", id()));
×
617
    }
618
    // Ensure that there is an id for this color specification
619
    int col_id;
187✔
620
    if (check_for_node(cn, "id")) {
187!
621
      col_id = std::stoi(get_node_value(cn, "id"));
374✔
622
    } else {
623
      fatal_error(fmt::format(
×
624
        "Must specify id for color specification in plot {}", id()));
×
625
    }
626
    // Add RGB
627
    if (PlotColorBy::cells == color_by_) {
187✔
628
      if (model::cell_map.find(col_id) != model::cell_map.end()) {
88!
629
        col_id = model::cell_map[col_id];
88✔
630
        colors_[col_id] = user_rgb;
88✔
631
      } else {
632
        warning(fmt::format(
×
633
          "Could not find cell {} specified in plot {}", col_id, id()));
×
634
      }
635
    } else if (PlotColorBy::mats == color_by_) {
99!
636
      if (model::material_map.find(col_id) != model::material_map.end()) {
99!
637
        col_id = model::material_map[col_id];
99✔
638
        colors_[col_id] = user_rgb;
99✔
639
      } else {
640
        warning(fmt::format(
×
641
          "Could not find material {} specified in plot {}", col_id, id()));
×
642
      }
643
    }
644
  } // color node loop
187✔
645
}
979✔
646

647
void Plot::set_meshlines(pugi::xml_node plot_node)
882✔
648
{
649
  // Deal with meshlines
650
  pugi::xpath_node_set mesh_line_nodes = plot_node.select_nodes("meshlines");
882✔
651

652
  if (!mesh_line_nodes.empty()) {
882✔
653
    if (PlotType::voxel == type_) {
33!
654
      warning(fmt::format("Meshlines ignored in voxel plot {}", id()));
×
655
    }
656

657
    if (mesh_line_nodes.size() == 1) {
33!
658
      // Get first meshline node
659
      pugi::xml_node meshlines_node = mesh_line_nodes[0].node();
33✔
660

661
      // Check mesh type
662
      std::string meshtype;
33✔
663
      if (check_for_node(meshlines_node, "meshtype")) {
33!
664
        meshtype = get_node_value(meshlines_node, "meshtype");
33✔
665
      } else {
666
        fatal_error(fmt::format(
×
667
          "Must specify a meshtype for meshlines specification in plot {}",
668
          id()));
×
669
      }
670

671
      // Ensure that there is a linewidth for this meshlines specification
672
      std::string meshline_width;
33✔
673
      if (check_for_node(meshlines_node, "linewidth")) {
33!
674
        meshline_width = get_node_value(meshlines_node, "linewidth");
33✔
675
        meshlines_width_ = std::stoi(meshline_width);
33✔
676
      } else {
677
        fatal_error(fmt::format(
×
678
          "Must specify a linewidth for meshlines specification in plot {}",
679
          id()));
×
680
      }
681

682
      // Check for color
683
      if (check_for_node(meshlines_node, "color")) {
33!
684
        // Check and make sure 3 values are specified for RGB
685
        vector<int> ml_rgb = get_node_array<int>(meshlines_node, "color");
×
686
        if (ml_rgb.size() != 3) {
×
687
          fatal_error(
×
688
            fmt::format("Bad RGB for meshlines color in plot {}", id()));
×
689
        }
690
        meshlines_color_ = ml_rgb;
×
UNCOV
691
      }
×
692

693
      // Set mesh based on type
694
      if ("ufs" == meshtype) {
33!
695
        if (!simulation::ufs_mesh) {
×
696
          fatal_error(
×
697
            fmt::format("No UFS mesh for meshlines on plot {}", id()));
×
698
        } else {
699
          for (int i = 0; i < model::meshes.size(); ++i) {
×
700
            if (const auto* m =
×
701
                  dynamic_cast<const RegularMesh*>(model::meshes[i].get())) {
×
702
              if (m == simulation::ufs_mesh) {
×
703
                index_meshlines_mesh_ = i;
×
704
              }
705
            }
706
          }
707
          if (index_meshlines_mesh_ == -1)
×
708
            fatal_error("Could not find the UFS mesh for meshlines plot");
×
709
        }
710
      } else if ("entropy" == meshtype) {
33✔
711
        if (!simulation::entropy_mesh) {
22!
712
          fatal_error(
×
713
            fmt::format("No entropy mesh for meshlines on plot {}", id()));
×
714
        } else {
715
          for (int i = 0; i < model::meshes.size(); ++i) {
55✔
716
            if (const auto* m =
66✔
717
                  dynamic_cast<const RegularMesh*>(model::meshes[i].get())) {
55!
718
              if (m == simulation::entropy_mesh) {
22!
719
                index_meshlines_mesh_ = i;
22✔
720
              }
721
            }
722
          }
723
          if (index_meshlines_mesh_ == -1)
22!
724
            fatal_error("Could not find the entropy mesh for meshlines plot");
×
725
        }
726
      } else if ("tally" == meshtype) {
11!
727
        // Ensure that there is a mesh id if the type is tally
728
        int tally_mesh_id;
11✔
729
        if (check_for_node(meshlines_node, "id")) {
11!
730
          tally_mesh_id = std::stoi(get_node_value(meshlines_node, "id"));
22✔
731
        } else {
732
          std::stringstream err_msg;
×
733
          fatal_error(fmt::format("Must specify a mesh id for meshlines tally "
×
734
                                  "mesh specification in plot {}",
735
            id()));
×
736
        }
×
737
        // find the tally index
738
        int idx;
11✔
739
        int err = openmc_get_mesh_index(tally_mesh_id, &idx);
11✔
740
        if (err != 0) {
11!
741
          fatal_error(fmt::format("Could not find mesh {} specified in "
×
742
                                  "meshlines for plot {}",
743
            tally_mesh_id, id()));
×
744
        }
745
        index_meshlines_mesh_ = idx;
11✔
746
      } else {
747
        fatal_error(fmt::format("Invalid type for meshlines on plot {}", id()));
×
748
      }
749
    } else {
33✔
750
      fatal_error(fmt::format("Mutliple meshlines specified in plot {}", id()));
×
751
    }
752
  }
753
}
882✔
754

755
void PlottableInterface::set_mask(pugi::xml_node plot_node)
979✔
756
{
757
  // Deal with masks
758
  pugi::xpath_node_set mask_nodes = plot_node.select_nodes("mask");
979✔
759

760
  if (!mask_nodes.empty()) {
979✔
761
    if (mask_nodes.size() == 1) {
33!
762
      // Get pointer to mask
763
      pugi::xml_node mask_node = mask_nodes[0].node();
33✔
764

765
      // Determine how many components there are and allocate
766
      vector<int> iarray = get_node_array<int>(mask_node, "components");
33✔
767
      if (iarray.size() == 0) {
33!
768
        fatal_error(
×
769
          fmt::format("Missing <components> in mask of plot {}", id()));
×
770
      }
771

772
      // First we need to change the user-specified identifiers to indices
773
      // in the cell and material arrays
774
      for (auto& col_id : iarray) {
99✔
775
        if (PlotColorBy::cells == color_by_) {
66!
776
          if (model::cell_map.find(col_id) != model::cell_map.end()) {
66!
777
            col_id = model::cell_map[col_id];
66✔
778
          } else {
779
            fatal_error(fmt::format("Could not find cell {} specified in the "
×
780
                                    "mask in plot {}",
781
              col_id, id()));
×
782
          }
783
        } else if (PlotColorBy::mats == color_by_) {
×
784
          if (model::material_map.find(col_id) != model::material_map.end()) {
×
785
            col_id = model::material_map[col_id];
×
786
          } else {
787
            fatal_error(fmt::format("Could not find material {} specified in "
×
788
                                    "the mask in plot {}",
789
              col_id, id()));
×
790
          }
791
        }
792
      }
793

794
      // Alter colors based on mask information
795
      for (int j = 0; j < colors_.size(); j++) {
132✔
796
        if (contains(iarray, j)) {
99✔
797
          if (check_for_node(mask_node, "background")) {
66!
798
            vector<int> bg_rgb = get_node_array<int>(mask_node, "background");
66✔
799
            colors_[j] = bg_rgb;
66✔
800
          } else {
66✔
801
            colors_[j] = WHITE;
×
802
          }
803
        }
804
      }
805

806
    } else {
33✔
807
      fatal_error(fmt::format("Mutliple masks specified in plot {}", id()));
×
808
    }
809
  }
810
}
979✔
811

812
void PlottableInterface::set_overlap_color(pugi::xml_node plot_node)
979✔
813
{
814
  color_overlaps_ = false;
979✔
815
  if (check_for_node(plot_node, "show_overlaps")) {
979✔
816
    color_overlaps_ = get_node_value_bool(plot_node, "show_overlaps");
22✔
817
    // check for custom overlap color
818
    if (check_for_node(plot_node, "overlap_color")) {
22✔
819
      if (!color_overlaps_) {
11!
820
        warning(fmt::format(
×
821
          "Overlap color specified in plot {} but overlaps won't be shown.",
822
          id()));
×
823
      }
824
      vector<int> olap_clr = get_node_array<int>(plot_node, "overlap_color");
11✔
825
      if (olap_clr.size() == 3) {
11!
826
        overlap_color_ = olap_clr;
11✔
827
      } else {
828
        fatal_error(fmt::format("Bad overlap RGB in plot {}", id()));
×
829
      }
830
    }
11✔
831
  }
832

833
  // make sure we allocate the vector for counting overlap checks if
834
  // they're going to be plotted
835
  if (color_overlaps_ && settings::run_mode == RunMode::PLOTTING) {
979!
836
    settings::check_overlaps = true;
22✔
837
    model::overlap_check_count.resize(model::cells.size(), 0);
22✔
838
  }
839
}
979✔
840

841
PlottableInterface::PlottableInterface(pugi::xml_node plot_node)
979✔
842
{
843
  set_id(plot_node);
979✔
844
  set_bg_color(plot_node);
979✔
845
  set_universe(plot_node);
979✔
846
  set_color_by(plot_node);
979✔
847
  set_default_colors();
979✔
848
  set_user_colors(plot_node);
979✔
849
  set_mask(plot_node);
979✔
850
  set_overlap_color(plot_node);
979✔
851
}
979✔
852

853
Plot::Plot(pugi::xml_node plot_node, PlotType type)
891✔
854
  : PlottableInterface(plot_node), type_(type), index_meshlines_mesh_ {-1}
891✔
855
{
856
  set_output_path(plot_node);
891✔
857
  set_basis(plot_node);
882✔
858
  set_origin(plot_node);
882✔
859
  set_width(plot_node);
882✔
860
  set_meshlines(plot_node);
882✔
861
  slice_level_ = level_; // Copy level employed in SlicePlotBase::get_map
882✔
862
  show_overlaps_ = color_overlaps_;
882✔
863
}
882✔
864

865
//==============================================================================
866
// OUTPUT_PPM writes out a previously generated image to a PPM file
867
//==============================================================================
868

869
void output_ppm(const std::string& filename, const ImageData& data)
×
870
{
871
  // Open PPM file for writing
872
  std::string fname = filename;
×
873
  fname = strtrim(fname);
×
874
  std::ofstream of;
×
875

876
  of.open(fname);
×
877

878
  // Write header
879
  of << "P6\n";
×
880
  of << data.shape(0) << " " << data.shape(1) << "\n";
×
881
  of << "255\n";
×
882
  of.close();
×
883

884
  of.open(fname, std::ios::binary | std::ios::app);
×
885
  // Write color for each pixel
886
  for (int y = 0; y < data.shape(1); y++) {
×
887
    for (int x = 0; x < data.shape(0); x++) {
×
888
      RGBColor rgb = data(x, y);
×
889
      of << rgb.red << rgb.green << rgb.blue;
×
890
    }
891
  }
892
  of << "\n";
×
893
}
×
894

895
//==============================================================================
896
// OUTPUT_PNG writes out a previously generated image to a PNG file
897
//==============================================================================
898

899
#ifdef USE_LIBPNG
900
void output_png(const std::string& filename, const ImageData& data)
231✔
901
{
902
  // Open PNG file for writing
903
  std::string fname = filename;
231✔
904
  fname = strtrim(fname);
231✔
905
  auto fp = std::fopen(fname.c_str(), "wb");
231✔
906

907
  // Initialize write and info structures
908
  auto png_ptr =
231✔
909
    png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
231✔
910
  auto info_ptr = png_create_info_struct(png_ptr);
231✔
911

912
  // Setup exception handling
913
  if (setjmp(png_jmpbuf(png_ptr)))
231!
914
    fatal_error("Error during png creation");
×
915

916
  png_init_io(png_ptr, fp);
231✔
917

918
  // Write header (8 bit colour depth)
919
  int width = data.shape(0);
231!
920
  int height = data.shape(1);
231!
921
  png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB,
231✔
922
    PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
923
  png_write_info(png_ptr, info_ptr);
231✔
924

925
  // Allocate memory for one row (3 bytes per pixel - RGB)
926
  std::vector<png_byte> row(3 * width);
231✔
927

928
  // Write color for each pixel
929
  for (int y = 0; y < height; y++) {
47,751✔
930
    for (int x = 0; x < width; x++) {
11,159,720✔
931
      RGBColor rgb = data(x, y);
11,112,200✔
932
      row[3 * x] = rgb.red;
11,112,200✔
933
      row[3 * x + 1] = rgb.green;
11,112,200✔
934
      row[3 * x + 2] = rgb.blue;
11,112,200✔
935
    }
936
    png_write_row(png_ptr, row.data());
47,520✔
937
  }
938

939
  // End write
940
  png_write_end(png_ptr, nullptr);
231✔
941

942
  // Clean up data structures
943
  std::fclose(fp);
231✔
944
  png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
231✔
945
  png_destroy_write_struct(&png_ptr, &info_ptr);
231✔
946
}
231✔
947
#endif
948

949
//==============================================================================
950
// DRAW_MESH_LINES draws mesh line boundaries on an image
951
//==============================================================================
952

953
void Plot::draw_mesh_lines(ImageData& data) const
33✔
954
{
955
  RGBColor rgb;
33!
956
  rgb = meshlines_color_;
33✔
957

958
  int ax1, ax2;
33✔
959
  Position expected_u {};
33✔
960
  Position expected_v {};
33✔
961
  switch (basis_) {
33!
962
  case PlotBasis::xy:
22✔
963
    ax1 = 0;
22✔
964
    ax2 = 1;
22✔
965
    expected_u = {width_[0], 0.0, 0.0};
22✔
966
    expected_v = {0.0, width_[1], 0.0};
22✔
967
    break;
22✔
968
  case PlotBasis::xz:
11✔
969
    ax1 = 0;
11✔
970
    ax2 = 2;
11✔
971
    expected_u = {width_[0], 0.0, 0.0};
11✔
972
    expected_v = {0.0, 0.0, width_[1]};
11✔
973
    break;
11✔
974
  case PlotBasis::yz:
×
975
    ax1 = 1;
×
976
    ax2 = 2;
×
977
    expected_u = {0.0, width_[0], 0.0};
×
978
    expected_v = {0.0, 0.0, width_[1]};
×
979
    break;
×
980
  default:
×
981
    UNREACHABLE();
×
982
  }
983

984
  // Meshlines rely on axis-aligned indexing in global coordinates.
985
  constexpr double rel_tol {1e-12};
33✔
986
  double span_tol = rel_tol * (1.0 + u_span_.norm() + v_span_.norm());
33✔
987
  if ((u_span_ - expected_u).norm() > span_tol ||
66!
988
      (v_span_ - expected_v).norm() > span_tol) {
33✔
989
    fatal_error("Meshlines are only supported for axis-aligned slice plots.");
×
990
  }
991

992
  Position ll_plot {origin_};
33✔
993
  Position ur_plot {origin_};
33✔
994

995
  ll_plot[ax1] -= width_[0] / 2.;
33✔
996
  ll_plot[ax2] -= width_[1] / 2.;
33✔
997
  ur_plot[ax1] += width_[0] / 2.;
33✔
998
  ur_plot[ax2] += width_[1] / 2.;
33✔
999

1000
  Position width = ur_plot - ll_plot;
33✔
1001

1002
  // Find the (axis-aligned) lines of the mesh that intersect this plot.
1003
  auto axis_lines =
33✔
1004
    model::meshes[index_meshlines_mesh_]->plot(ll_plot, ur_plot);
33✔
1005

1006
  // Find the bounds along the second axis (accounting for low-D meshes).
1007
  int ax2_min, ax2_max;
33✔
1008
  if (axis_lines.second.size() > 0) {
33!
1009
    double frac = (axis_lines.second.back() - ll_plot[ax2]) / width[ax2];
33✔
1010
    ax2_min = (1.0 - frac) * pixels()[1];
33✔
1011
    if (ax2_min < 0)
33✔
1012
      ax2_min = 0;
1013
    frac = (axis_lines.second.front() - ll_plot[ax2]) / width[ax2];
33✔
1014
    ax2_max = (1.0 - frac) * pixels()[1];
33!
1015
    if (ax2_max > pixels()[1])
33!
1016
      ax2_max = pixels()[1];
×
1017
  } else {
1018
    ax2_min = 0;
×
1019
    ax2_max = pixels()[1];
×
1020
  }
1021

1022
  // Iterate across the first axis and draw lines.
1023
  for (auto ax1_val : axis_lines.first) {
187✔
1024
    double frac = (ax1_val - ll_plot[ax1]) / width[ax1];
154✔
1025
    int ax1_ind = frac * pixels()[0];
154✔
1026
    for (int ax2_ind = ax2_min; ax2_ind < ax2_max; ++ax2_ind) {
24,948✔
1027
      for (int plus = 0; plus <= meshlines_width_; plus++) {
49,588✔
1028
        if (ax1_ind + plus >= 0 && ax1_ind + plus < pixels()[0])
24,794!
1029
          data(ax1_ind + plus, ax2_ind) = rgb;
24,794✔
1030
        if (ax1_ind - plus >= 0 && ax1_ind - plus < pixels()[0])
24,794!
1031
          data(ax1_ind - plus, ax2_ind) = rgb;
24,794✔
1032
      }
1033
    }
1034
  }
1035

1036
  // Find the bounds along the first axis.
1037
  int ax1_min, ax1_max;
33✔
1038
  if (axis_lines.first.size() > 0) {
33!
1039
    double frac = (axis_lines.first.front() - ll_plot[ax1]) / width[ax1];
33✔
1040
    ax1_min = frac * pixels()[0];
33✔
1041
    if (ax1_min < 0)
33✔
1042
      ax1_min = 0;
1043
    frac = (axis_lines.first.back() - ll_plot[ax1]) / width[ax1];
33✔
1044
    ax1_max = frac * pixels()[0];
33!
1045
    if (ax1_max > pixels()[0])
33!
1046
      ax1_max = pixels()[0];
×
1047
  } else {
1048
    ax1_min = 0;
×
1049
    ax1_max = pixels()[0];
×
1050
  }
1051

1052
  // Iterate across the second axis and draw lines.
1053
  for (auto ax2_val : axis_lines.second) {
209✔
1054
    double frac = (ax2_val - ll_plot[ax2]) / width[ax2];
176✔
1055
    int ax2_ind = (1.0 - frac) * pixels()[1];
176✔
1056
    for (int ax1_ind = ax1_min; ax1_ind < ax1_max; ++ax1_ind) {
28,336✔
1057
      for (int plus = 0; plus <= meshlines_width_; plus++) {
56,320✔
1058
        if (ax2_ind + plus >= 0 && ax2_ind + plus < pixels()[1])
28,160!
1059
          data(ax1_ind, ax2_ind + plus) = rgb;
28,160✔
1060
        if (ax2_ind - plus >= 0 && ax2_ind - plus < pixels()[1])
28,160!
1061
          data(ax1_ind, ax2_ind - plus) = rgb;
28,160✔
1062
      }
1063
    }
1064
  }
1065
}
33✔
1066

1067
/* outputs a binary file that can be input into silomesh for 3D geometry
1068
 * visualization.  It works the same way as create_image by dragging a particle
1069
 * across the geometry for the specified number of voxels. The first 3 int's in
1070
 * the binary are the number of x, y, and z voxels.  The next 3 double's are
1071
 * the widths of the voxels in the x, y, and z directions. The next 3 double's
1072
 * are the x, y, and z coordinates of the lower left point. Finally the binary
1073
 * is filled with entries of four int's each. Each 'row' in the binary contains
1074
 * four int's: 3 for x,y,z position and 1 for cell or material id.  For 1
1075
 * million voxels this produces a file of approximately 15MB.
1076
 */
1077
void Plot::create_voxel() const
55✔
1078
{
1079
  // compute voxel widths in each direction
1080
  array<double, 3> vox;
55✔
1081
  vox[0] = width_[0] / static_cast<double>(pixels()[0]);
55✔
1082
  vox[1] = width_[1] / static_cast<double>(pixels()[1]);
55✔
1083
  vox[2] = width_[2] / static_cast<double>(pixels()[2]);
55✔
1084

1085
  // initial particle position
1086
  Position ll = origin_ - width_ / 2.;
55✔
1087

1088
  // Open binary plot file for writing
1089
  std::ofstream of;
55✔
1090
  std::string fname = std::string(path_plot_);
55✔
1091
  fname = strtrim(fname);
55✔
1092
  hid_t file_id = file_open(fname, 'w');
55✔
1093

1094
  // write header info
1095
  write_attribute(file_id, "filetype", "voxel");
55✔
1096
  write_attribute(file_id, "version", VERSION_VOXEL);
55✔
1097
  write_attribute(file_id, "openmc_version", VERSION);
55✔
1098

1099
#ifdef GIT_SHA1
1100
  write_attribute(file_id, "git_sha1", GIT_SHA1);
1101
#endif
1102

1103
  // Write current date and time
1104
  write_attribute(file_id, "date_and_time", time_stamp().c_str());
110✔
1105
  array<int, 3> h5_pixels;
55✔
1106
  std::copy(pixels().begin(), pixels().end(), h5_pixels.begin());
55✔
1107
  write_attribute(file_id, "num_voxels", h5_pixels);
55✔
1108
  write_attribute(file_id, "voxel_width", vox);
55✔
1109
  write_attribute(file_id, "lower_left", ll);
55✔
1110

1111
  // Create dataset for voxel data -- note that the dimensions are reversed
1112
  // since we want the order in the file to be z, y, x
1113
  hsize_t dims[3];
55✔
1114
  dims[0] = pixels()[2];
55✔
1115
  dims[1] = pixels()[1];
55✔
1116
  dims[2] = pixels()[0];
55✔
1117
  hid_t dspace, dset, memspace;
55✔
1118
  voxel_init(file_id, &(dims[0]), &dspace, &dset, &memspace);
55✔
1119

1120
  SlicePlotBase pltbase;
55✔
1121
  pltbase.origin_ = origin_;
55✔
1122
  pltbase.u_span_ = {width_.x, 0.0, 0.0};
55✔
1123
  pltbase.v_span_ = {0.0, width_.y, 0.0};
55✔
1124
  pltbase.pixels() = pixels();
55✔
1125
  pltbase.show_overlaps_ = color_overlaps_;
55✔
1126

1127
  ProgressBar pb;
55✔
1128
  for (int z = 0; z < pixels()[2]; z++) {
4,785✔
1129
    // update z coordinate
1130
    pltbase.origin_.z = ll.z + z * vox[2];
4,730✔
1131

1132
    // generate ids using plotbase
1133
    IdData ids = pltbase.get_map<IdData>();
4,730✔
1134

1135
    // select only cell/material ID data and flip the y-axis
1136
    int idx = color_by_ == PlotColorBy::cells ? 0 : 2;
4,730!
1137
    // Extract 2D slice at index idx from 3D data
1138
    size_t rows = ids.data_.shape(0);
4,730!
1139
    size_t cols = ids.data_.shape(1);
4,730!
1140
    tensor::Tensor<int32_t> data_slice({rows, cols});
4,730✔
1141
    for (size_t r = 0; r < rows; ++r)
912,230✔
1142
      for (size_t c = 0; c < cols; ++c)
179,382,500✔
1143
        data_slice(r, c) = ids.data_(r, c, idx);
178,475,000✔
1144
    tensor::Tensor<int32_t> data_flipped = data_slice.flip(0);
4,730✔
1145

1146
    // Write to HDF5 dataset
1147
    voxel_write_slice(z, dspace, dset, memspace, data_flipped.data());
4,730✔
1148

1149
    // update progress bar
1150
    pb.set_value(
4,730✔
1151
      100. * static_cast<double>(z + 1) / static_cast<double>((pixels()[2])));
4,730✔
1152
  }
14,190✔
1153

1154
  voxel_finalize(dspace, dset, memspace);
55✔
1155
  file_close(file_id);
55✔
1156
}
55✔
1157

1158
void voxel_init(hid_t file_id, const hsize_t* dims, hid_t* dspace, hid_t* dset,
55✔
1159
  hid_t* memspace)
1160
{
1161
  // Create dataspace/dataset for voxel data
1162
  *dspace = H5Screate_simple(3, dims, nullptr);
55✔
1163
  *dset = H5Dcreate(file_id, "data", H5T_NATIVE_INT, *dspace, H5P_DEFAULT,
55✔
1164
    H5P_DEFAULT, H5P_DEFAULT);
1165

1166
  // Create dataspace for a slice of the voxel
1167
  hsize_t dims_slice[2] {dims[1], dims[2]};
55✔
1168
  *memspace = H5Screate_simple(2, dims_slice, nullptr);
55✔
1169

1170
  // Select hyperslab in dataspace
1171
  hsize_t start[3] {0, 0, 0};
55✔
1172
  hsize_t count[3] {1, dims[1], dims[2]};
55✔
1173
  H5Sselect_hyperslab(*dspace, H5S_SELECT_SET, start, nullptr, count, nullptr);
55✔
1174
}
55✔
1175

1176
void voxel_write_slice(
4,730✔
1177
  int x, hid_t dspace, hid_t dset, hid_t memspace, void* buf)
1178
{
1179
  hssize_t offset[3] {x, 0, 0};
4,730✔
1180
  H5Soffset_simple(dspace, offset);
4,730✔
1181
  H5Dwrite(dset, H5T_NATIVE_INT, memspace, dspace, H5P_DEFAULT, buf);
4,730✔
1182
}
4,730✔
1183

1184
void voxel_finalize(hid_t dspace, hid_t dset, hid_t memspace)
55✔
1185
{
1186
  H5Dclose(dset);
55✔
1187
  H5Sclose(dspace);
55✔
1188
  H5Sclose(memspace);
55✔
1189
}
55✔
1190

1191
RGBColor random_color(void)
3,441✔
1192
{
1193
  return {int(prn(&model::plotter_seed) * 255),
3,441✔
1194
    int(prn(&model::plotter_seed) * 255), int(prn(&model::plotter_seed) * 255)};
3,441✔
1195
}
1196

1197
RayTracePlot::RayTracePlot(pugi::xml_node node) : PlottableInterface(node)
88✔
1198
{
1199
  set_look_at(node);
88✔
1200
  set_camera_position(node);
88✔
1201
  set_field_of_view(node);
88✔
1202
  set_pixels(node);
88✔
1203
  set_orthographic_width(node);
88✔
1204
  set_output_path(node);
88✔
1205

1206
  if (check_for_node(node, "orthographic_width") &&
99!
1207
      check_for_node(node, "field_of_view"))
11✔
1208
    fatal_error("orthographic_width and field_of_view are mutually exclusive "
×
1209
                "parameters.");
1210
}
88✔
1211

1212
void RayTracePlot::update_view()
110✔
1213
{
1214
  // Get centerline vector for camera-to-model. We create vectors around this
1215
  // that form a pixel array, and then trace rays along that.
1216
  auto up = up_ / up_.norm();
110✔
1217
  Direction looking_direction = look_at_ - camera_position_;
110✔
1218
  looking_direction /= looking_direction.norm();
110✔
1219
  if (std::abs(std::abs(looking_direction.dot(up)) - 1.0) < 1e-9)
110!
1220
    fatal_error("Up vector cannot align with vector between camera position "
×
1221
                "and look_at!");
1222
  Direction cam_yaxis = looking_direction.cross(up);
110✔
1223
  cam_yaxis /= cam_yaxis.norm();
110✔
1224
  Direction cam_zaxis = cam_yaxis.cross(looking_direction);
110✔
1225
  cam_zaxis /= cam_zaxis.norm();
110✔
1226

1227
  // Cache the camera-to-model matrix
1228
  camera_to_model_ = {looking_direction.x, cam_yaxis.x, cam_zaxis.x,
110✔
1229
    looking_direction.y, cam_yaxis.y, cam_zaxis.y, looking_direction.z,
110✔
1230
    cam_yaxis.z, cam_zaxis.z};
110✔
1231
}
110✔
1232

1233
WireframeRayTracePlot::WireframeRayTracePlot(pugi::xml_node node)
55✔
1234
  : RayTracePlot(node)
55✔
1235
{
1236
  set_opacities(node);
55✔
1237
  set_wireframe_thickness(node);
55✔
1238
  set_wireframe_ids(node);
55✔
1239
  set_wireframe_color(node);
55✔
1240
  update_view();
55✔
1241
}
55✔
1242

1243
void WireframeRayTracePlot::set_wireframe_color(pugi::xml_node plot_node)
55✔
1244
{
1245
  // Copy plot wireframe color
1246
  if (check_for_node(plot_node, "wireframe_color")) {
55!
1247
    vector<int> w_rgb = get_node_array<int>(plot_node, "wireframe_color");
×
1248
    if (w_rgb.size() == 3) {
×
1249
      wireframe_color_ = w_rgb;
×
1250
    } else {
1251
      fatal_error(fmt::format("Bad wireframe RGB in plot {}", id()));
×
1252
    }
1253
  }
×
1254
}
55✔
1255

1256
void RayTracePlot::set_output_path(pugi::xml_node node)
88✔
1257
{
1258
  // Set output file path
1259
  std::string filename;
88✔
1260

1261
  if (check_for_node(node, "filename")) {
88✔
1262
    filename = get_node_value(node, "filename");
77✔
1263
  } else {
1264
    filename = fmt::format("plot_{}", id());
11✔
1265
  }
1266

1267
#ifdef USE_LIBPNG
1268
  if (!file_extension_present(filename, "png"))
88✔
1269
    filename.append(".png");
33✔
1270
#else
1271
  if (!file_extension_present(filename, "ppm"))
1272
    filename.append(".ppm");
1273
#endif
1274
  path_plot_ = filename;
176✔
1275
}
88✔
1276

1277
bool WireframeRayTracePlot::trackstack_equivalent(
3,041,159✔
1278
  const std::vector<TrackSegment>& track1,
1279
  const std::vector<TrackSegment>& track2) const
1280
{
1281
  if (wireframe_ids_.empty()) {
3,041,159✔
1282
    // Draw wireframe for all surfaces/cells/materials
1283
    if (track1.size() != track2.size())
2,545,070✔
1284
      return false;
1285
    for (int i = 0; i < track1.size(); ++i) {
6,707,954✔
1286
      if (track1[i].id != track2[i].id ||
4,236,771✔
1287
          track1[i].surface_index != track2[i].surface_index) {
4,236,639✔
1288
        return false;
1289
      }
1290
    }
1291
    return true;
1292
  } else {
1293
    // This runs in O(nm) where n is the intersection stack size
1294
    // and m is the number of IDs we are wireframing. A simpler
1295
    // algorithm can likely be found.
1296
    for (const int id : wireframe_ids_) {
986,194✔
1297
      int t1_i = 0;
496,089✔
1298
      int t2_i = 0;
496,089✔
1299

1300
      // Advance to first instance of the ID
1301
      while (t1_i < track1.size() && t2_i < track2.size()) {
562,430✔
1302
        while (t1_i < track1.size() && track1[t1_i].id != id)
392,832✔
1303
          t1_i++;
229,053✔
1304
        while (t2_i < track2.size() && track2[t2_i].id != id)
393,668✔
1305
          t2_i++;
229,889✔
1306

1307
        // This one is really important!
1308
        if ((t1_i == track1.size() && t2_i != track2.size()) ||
163,779✔
1309
            (t1_i != track1.size() && t2_i == track2.size()))
162,096✔
1310
          return false;
3,718✔
1311
        if (t1_i == track1.size() && t2_i == track2.size())
160,061!
1312
          break;
1313
        // Check if surface different
1314
        if (track1[t1_i].surface_index != track2[t2_i].surface_index)
68,607✔
1315
          return false;
1316

1317
        // Pretty sure this should not be used:
1318
        // if (t2_i != track2.size() - 1 &&
1319
        //     t1_i != track1.size() - 1 &&
1320
        //     track1[t1_i+1].id != track2[t2_i+1].id) return false;
1321
        if (t2_i != 0 && t1_i != 0 &&
67,122✔
1322
            track1[t1_i - 1].surface_index != track2[t2_i - 1].surface_index)
53,944✔
1323
          return false;
1324

1325
        // Check if neighboring cells are different
1326
        // if (track1[t1_i ? t1_i - 1 : 0].id != track2[t2_i ? t2_i - 1 : 0].id)
1327
        // return false; if (track1[t1_i < track1.size() - 1 ? t1_i + 1 : t1_i
1328
        // ].id !=
1329
        //    track2[t2_i < track2.size() - 1 ? t2_i + 1 : t2_i].id) return
1330
        //    false;
1331
        t1_i++, t2_i++;
66,341✔
1332
      }
1333
    }
1334
    return true;
1335
  }
1336
}
1337

1338
std::pair<Position, Direction> RayTracePlot::get_pixel_ray(
3,521,056✔
1339
  int horiz, int vert) const
1340
{
1341
  // Compute field of view in radians
1342
  constexpr double DEGREE_TO_RADIAN = M_PI / 180.0;
3,521,056✔
1343
  double horiz_fov_radians = horizontal_field_of_view_ * DEGREE_TO_RADIAN;
3,521,056✔
1344
  double p0 = static_cast<double>(pixels()[0]);
3,521,056✔
1345
  double p1 = static_cast<double>(pixels()[1]);
3,521,056✔
1346
  double vert_fov_radians = horiz_fov_radians * p1 / p0;
3,521,056✔
1347

1348
  // focal_plane_dist can be changed to alter the perspective distortion
1349
  // effect. This is in units of cm. This seems to look good most of the
1350
  // time. TODO let this variable be set through XML.
1351
  constexpr double focal_plane_dist = 10.0;
3,521,056✔
1352
  const double dx = 2.0 * focal_plane_dist * std::tan(0.5 * horiz_fov_radians);
3,521,056✔
1353
  const double dy = p1 / p0 * dx;
3,521,056✔
1354

1355
  std::pair<Position, Direction> result;
3,521,056✔
1356

1357
  // Generate the starting position/direction of the ray
1358
  if (orthographic_width_ == C_NONE) { // perspective projection
3,521,056✔
1359
    Direction camera_local_vec;
3,081,056✔
1360
    camera_local_vec.x = focal_plane_dist;
3,081,056✔
1361
    camera_local_vec.y = -0.5 * dx + horiz * dx / p0;
3,081,056✔
1362
    camera_local_vec.z = 0.5 * dy - vert * dy / p1;
3,081,056✔
1363
    camera_local_vec /= camera_local_vec.norm();
3,081,056✔
1364

1365
    result.first = camera_position_;
3,081,056✔
1366
    result.second = camera_local_vec.rotate(camera_to_model_);
3,081,056✔
1367
  } else { // orthographic projection
1368

1369
    double x_pix_coord = (static_cast<double>(horiz) - p0 / 2.0) / p0;
440,000✔
1370
    double y_pix_coord = (static_cast<double>(vert) - p1 / 2.0) / p1;
440,000✔
1371

1372
    result.first = camera_position_ +
440,000✔
1373
                   camera_y_axis() * x_pix_coord * orthographic_width_ +
440,000✔
1374
                   camera_z_axis() * y_pix_coord * orthographic_width_;
440,000✔
1375
    result.second = camera_x_axis();
440,000✔
1376
  }
1377

1378
  return result;
3,521,056✔
1379
}
1380

1381
ImageData WireframeRayTracePlot::create_image() const
55✔
1382
{
1383
  size_t width = pixels()[0];
55✔
1384
  size_t height = pixels()[1];
55✔
1385
  ImageData data({width, height}, not_found_);
55✔
1386

1387
  // This array marks where the initial wireframe was drawn. We convolve it with
1388
  // a filter that gets adjusted with the wireframe thickness in order to
1389
  // thicken the lines.
1390
  tensor::Tensor<int> wireframe_initial(
55✔
1391
    {static_cast<size_t>(width), static_cast<size_t>(height)}, 0);
55✔
1392

1393
  /* Holds all of the track segments for the current rendered line of pixels.
1394
   * old_segments holds a copy of this_line_segments from the previous line.
1395
   * By holding both we can check if the cell/material intersection stack
1396
   * differs from the left or upper neighbor. This allows a robustly drawn
1397
   * wireframe. If only checking the left pixel (which requires substantially
1398
   * less memory), the wireframe tends to be spotty and be disconnected for
1399
   * surface edges oriented horizontally in the rendering.
1400
   *
1401
   * Note that a vector of vectors is required rather than a 2-tensor,
1402
   * since the stack size varies within each column.
1403
   */
1404
  const int n_threads = num_threads();
55✔
1405
  std::vector<std::vector<std::vector<TrackSegment>>> this_line_segments(
55✔
1406
    n_threads);
55✔
1407
  for (int t = 0; t < n_threads; ++t) {
140✔
1408
    this_line_segments[t].resize(pixels()[0]);
85✔
1409
  }
1410

1411
  // The last thread writes to this, and the first thread reads from it.
1412
  std::vector<std::vector<TrackSegment>> old_segments(pixels()[0]);
55✔
1413

1414
#pragma omp parallel
30✔
1415
  {
25✔
1416
    const int n_threads = num_threads();
25✔
1417
    const int tid = thread_num();
25✔
1418

1419
    int vert = tid;
25✔
1420
    for (int iter = 0; iter <= pixels()[1] / n_threads; iter++) {
5,050✔
1421

1422
      // Save bottom line of current work chunk to compare against later. This
1423
      // used to be inside the below if block, but it causes a spurious line to
1424
      // be drawn at the bottom of the image. Not sure why, but moving it here
1425
      // fixes things.
1426
      if (tid == n_threads - 1)
5,025✔
1427
        old_segments = this_line_segments[n_threads - 1];
5,025✔
1428

1429
      if (vert < pixels()[1]) {
5,025✔
1430

1431
        for (int horiz = 0; horiz < pixels()[0]; ++horiz) {
1,005,000✔
1432

1433
          // RayTracePlot implements camera ray generation
1434
          std::pair<Position, Direction> ru = get_pixel_ray(horiz, vert);
1,000,000✔
1435

1436
          this_line_segments[tid][horiz].clear();
1,000,000✔
1437
          ProjectionRay ray(
1,000,000✔
1438
            ru.first, ru.second, *this, this_line_segments[tid][horiz]);
1,000,000✔
1439

1440
          ray.trace();
1,000,000✔
1441

1442
          // Now color the pixel based on what we have intersected...
1443
          // Loops backwards over intersections.
1444
          Position current_color(
1,000,000✔
1445
            not_found_.red, not_found_.green, not_found_.blue);
1,000,000✔
1446
          const auto& segments = this_line_segments[tid][horiz];
1,000,000✔
1447

1448
          // There must be at least two cell intersections to color, front and
1449
          // back of the cell. Maybe an infinitely thick cell could be present
1450
          // with no back, but why would you want to color that? It's easier to
1451
          // just skip that edge case and not even color it.
1452
          if (segments.size() <= 1)
1,000,000✔
1453
            continue;
616,655✔
1454

1455
          for (int i = segments.size() - 2; i >= 0; --i) {
1,072,335✔
1456
            int colormap_idx = segments[i].id;
688,990✔
1457
            RGBColor seg_color = colors_[colormap_idx];
688,990✔
1458
            Position seg_color_vec(
688,990✔
1459
              seg_color.red, seg_color.green, seg_color.blue);
688,990✔
1460
            double mixing =
688,990✔
1461
              std::exp(-xs_[colormap_idx] *
1,377,980✔
1462
                       (segments[i + 1].length - segments[i].length));
688,990✔
1463
            current_color =
688,990✔
1464
              current_color * mixing + (1.0 - mixing) * seg_color_vec;
688,990✔
1465
          }
1466

1467
          // save result converting from double-precision color coordinates to
1468
          // byte-sized
1469
          RGBColor result;
383,345✔
1470
          result.red = static_cast<uint8_t>(current_color.x);
383,345✔
1471
          result.green = static_cast<uint8_t>(current_color.y);
383,345✔
1472
          result.blue = static_cast<uint8_t>(current_color.z);
383,345✔
1473
          data(horiz, vert) = result;
383,345✔
1474

1475
          // Check to draw wireframe in horizontal direction. No inter-thread
1476
          // comm.
1477
          if (horiz > 0) {
383,345✔
1478
            if (!trackstack_equivalent(this_line_segments[tid][horiz],
382,345✔
1479
                  this_line_segments[tid][horiz - 1])) {
382,345✔
1480
              wireframe_initial(horiz, vert) = 1;
15,710✔
1481
            }
1482
          }
1483
        }
1,000,000✔
1484
      } // end "if" vert in correct range
1485

1486
      // We require a barrier before comparing vertical neighbors' intersection
1487
      // stacks. i.e. all threads must be done with their line.
1488
#pragma omp barrier
1489

1490
      // Now that the horizontal line has finished rendering, we can fill in
1491
      // wireframe entries that require comparison among all the threads. Hence
1492
      // the omp barrier being used. It has to be OUTSIDE any if blocks!
1493
      if (vert < pixels()[1]) {
5,025✔
1494
        // Loop over horizontal pixels, checking intersection stack of upper
1495
        // neighbor
1496

1497
        const std::vector<std::vector<TrackSegment>>* top_cmp = nullptr;
1498
        if (tid == 0)
1499
          top_cmp = &old_segments;
1500
        else
1501
          top_cmp = &this_line_segments[tid - 1];
1502

1503
        for (int horiz = 0; horiz < pixels()[0]; ++horiz) {
1,005,000✔
1504
          if (!trackstack_equivalent(
1,000,000✔
1505
                this_line_segments[tid][horiz], (*top_cmp)[horiz])) {
1,000,000✔
1506
            wireframe_initial(horiz, vert) = 1;
20,595✔
1507
          }
1508
        }
1509
      }
1510

1511
      // We need another barrier to ensure threads don't proceed to modify their
1512
      // intersection stacks on that horizontal line while others are
1513
      // potentially still working on the above.
1514
#pragma omp barrier
1515
      vert += n_threads;
5,025✔
1516
    }
1517
  } // end omp parallel
1518

1519
  // Now thicken the wireframe lines and apply them to our image
1520
  for (int vert = 0; vert < pixels()[1]; ++vert) {
11,055✔
1521
    for (int horiz = 0; horiz < pixels()[0]; ++horiz) {
2,211,000✔
1522
      if (wireframe_initial(horiz, vert)) {
2,200,000✔
1523
        if (wireframe_thickness_ == 1)
70,983✔
1524
          data(horiz, vert) = wireframe_color_;
30,195✔
1525
        for (int i = -wireframe_thickness_ / 2; i < wireframe_thickness_ / 2;
195,723✔
1526
             ++i)
1527
          for (int j = -wireframe_thickness_ / 2; j < wireframe_thickness_ / 2;
546,876✔
1528
               ++j)
1529
            if (i * i + j * j < wireframe_thickness_ * wireframe_thickness_) {
422,136!
1530

1531
              // Check if wireframe pixel is out of bounds
1532
              int w_i = std::max(std::min(horiz + i, pixels()[0] - 1), 0);
422,136!
1533
              int w_j = std::max(std::min(vert + j, pixels()[1] - 1), 0);
422,268✔
1534
              data(w_i, w_j) = wireframe_color_;
422,136✔
1535
            }
1536
      }
1537
    }
1538
  }
1539

1540
  return data;
110✔
1541
}
110✔
1542

1543
void WireframeRayTracePlot::create_output() const
55✔
1544
{
1545
  ImageData data = create_image();
55✔
1546
  write_image(data);
55✔
1547
}
55✔
1548

1549
void RayTracePlot::print_info() const
88✔
1550
{
1551
  fmt::print("Camera position: {} {} {}\n", camera_position_.x,
176✔
1552
    camera_position_.y, camera_position_.z);
88✔
1553
  fmt::print("Look at: {} {} {}\n", look_at_.x, look_at_.y, look_at_.z);
88✔
1554
  fmt::print(
176✔
1555
    "Horizontal field of view: {} degrees\n", horizontal_field_of_view_);
88✔
1556
  fmt::print("Pixels: {} {}\n", pixels()[0], pixels()[1]);
88✔
1557
}
88✔
1558

1559
void WireframeRayTracePlot::print_info() const
55✔
1560
{
1561
  fmt::print("Plot Type: Wireframe ray-traced\n");
55✔
1562
  RayTracePlot::print_info();
55✔
1563
}
55✔
1564

1565
void WireframeRayTracePlot::set_opacities(pugi::xml_node node)
55✔
1566
{
1567
  xs_.resize(colors_.size(), 1e6); // set to large value for opaque by default
55✔
1568

1569
  for (auto cn : node.children("color")) {
121✔
1570
    // Make sure 3 values are specified for RGB
1571
    double user_xs = std::stod(get_node_value(cn, "xs"));
132✔
1572
    int col_id = std::stoi(get_node_value(cn, "id"));
132✔
1573

1574
    // Add RGB
1575
    if (PlotColorBy::cells == color_by_) {
66!
1576
      if (model::cell_map.find(col_id) != model::cell_map.end()) {
66!
1577
        col_id = model::cell_map[col_id];
66✔
1578
        xs_[col_id] = user_xs;
66✔
1579
      } else {
1580
        warning(fmt::format(
×
1581
          "Could not find cell {} specified in plot {}", col_id, id()));
×
1582
      }
1583
    } else if (PlotColorBy::mats == color_by_) {
×
1584
      if (model::material_map.find(col_id) != model::material_map.end()) {
×
1585
        col_id = model::material_map[col_id];
×
1586
        xs_[col_id] = user_xs;
×
1587
      } else {
1588
        warning(fmt::format(
×
1589
          "Could not find material {} specified in plot {}", col_id, id()));
×
1590
      }
1591
    }
1592
  }
1593
}
55✔
1594

1595
void RayTracePlot::set_orthographic_width(pugi::xml_node node)
88✔
1596
{
1597
  if (check_for_node(node, "orthographic_width")) {
88✔
1598
    double orthographic_width =
11✔
1599
      std::stod(get_node_value(node, "orthographic_width", true));
11✔
1600
    if (orthographic_width < 0.0)
11!
1601
      fatal_error("Requires positive orthographic_width");
×
1602
    orthographic_width_ = orthographic_width;
11✔
1603
  }
1604
}
88✔
1605

1606
void WireframeRayTracePlot::set_wireframe_thickness(pugi::xml_node node)
55✔
1607
{
1608
  if (check_for_node(node, "wireframe_thickness")) {
55✔
1609
    int wireframe_thickness =
22✔
1610
      std::stoi(get_node_value(node, "wireframe_thickness", true));
22✔
1611
    if (wireframe_thickness < 0)
22!
1612
      fatal_error("Requires non-negative wireframe thickness");
×
1613
    wireframe_thickness_ = wireframe_thickness;
22✔
1614
  }
1615
}
55✔
1616

1617
void WireframeRayTracePlot::set_wireframe_ids(pugi::xml_node node)
55✔
1618
{
1619
  if (check_for_node(node, "wireframe_ids")) {
55✔
1620
    wireframe_ids_ = get_node_array<int>(node, "wireframe_ids");
11✔
1621
    // It is read in as actual ID values, but we have to convert to indices in
1622
    // mat/cell array
1623
    for (auto& x : wireframe_ids_)
22✔
1624
      x = color_by_ == PlotColorBy::mats ? model::material_map[x]
22!
1625
                                         : model::cell_map[x];
×
1626
  }
1627
  // We make sure the list is sorted in order to later use
1628
  // std::binary_search.
1629
  std::sort(wireframe_ids_.begin(), wireframe_ids_.end());
55✔
1630
}
55✔
1631

1632
void RayTracePlot::set_pixels(pugi::xml_node node)
88✔
1633
{
1634
  vector<int> pxls = get_node_array<int>(node, "pixels");
88✔
1635
  if (pxls.size() != 2)
88!
1636
    fatal_error(
×
1637
      fmt::format("<pixels> must be length 2 in projection plot {}", id()));
×
1638
  pixels()[0] = pxls[0];
88✔
1639
  pixels()[1] = pxls[1];
88✔
1640
}
88✔
1641

1642
void RayTracePlot::set_camera_position(pugi::xml_node node)
88✔
1643
{
1644
  vector<double> camera_pos = get_node_array<double>(node, "camera_position");
88✔
1645
  if (camera_pos.size() != 3) {
88!
1646
    fatal_error(fmt::format(
×
1647
      "camera_position element must have three floating point values"));
1648
  }
1649
  camera_position_.x = camera_pos[0];
88✔
1650
  camera_position_.y = camera_pos[1];
88✔
1651
  camera_position_.z = camera_pos[2];
88✔
1652
}
88✔
1653

1654
void RayTracePlot::set_look_at(pugi::xml_node node)
88✔
1655
{
1656
  vector<double> look_at = get_node_array<double>(node, "look_at");
88✔
1657
  if (look_at.size() != 3) {
88!
1658
    fatal_error("look_at element must have three floating point values");
×
1659
  }
1660
  look_at_.x = look_at[0];
88✔
1661
  look_at_.y = look_at[1];
88✔
1662
  look_at_.z = look_at[2];
88✔
1663
}
88✔
1664

1665
void RayTracePlot::set_field_of_view(pugi::xml_node node)
88✔
1666
{
1667
  // Defaults to 70 degree horizontal field of view (see .h file)
1668
  if (check_for_node(node, "horizontal_field_of_view")) {
88!
1669
    double fov =
×
1670
      std::stod(get_node_value(node, "horizontal_field_of_view", true));
×
1671
    if (fov < 180.0 && fov > 0.0) {
×
1672
      horizontal_field_of_view_ = fov;
×
1673
    } else {
1674
      fatal_error(fmt::format("Horizontal field of view for plot {} "
×
1675
                              "out-of-range. Must be in (0, 180) degrees.",
1676
        id()));
×
1677
    }
1678
  }
1679
}
88✔
1680

1681
SolidRayTracePlot::SolidRayTracePlot(pugi::xml_node node) : RayTracePlot(node)
33✔
1682
{
1683
  set_opaque_ids(node);
33✔
1684
  set_diffuse_fraction(node);
33✔
1685
  set_light_position(node);
33✔
1686
  update_view();
33✔
1687
}
33✔
1688

1689
void SolidRayTracePlot::print_info() const
33✔
1690
{
1691
  fmt::print("Plot Type: Solid ray-traced\n");
33✔
1692
  RayTracePlot::print_info();
33✔
1693
}
33✔
1694

1695
ImageData SolidRayTracePlot::create_image() const
55✔
1696
{
1697
  size_t width = pixels()[0];
55✔
1698
  size_t height = pixels()[1];
55✔
1699
  ImageData data({width, height}, not_found_);
55✔
1700

1701
#pragma omp parallel for schedule(dynamic) collapse(2)
30✔
1702
  for (int horiz = 0; horiz < pixels()[0]; ++horiz) {
3,105✔
1703
    for (int vert = 0; vert < pixels()[1]; ++vert) {
603,560✔
1704
      // RayTracePlot implements camera ray generation
1705
      std::pair<Position, Direction> ru = get_pixel_ray(horiz, vert);
600,480✔
1706
      PhongRay ray(ru.first, ru.second, *this);
600,480✔
1707
      ray.trace();
600,480✔
1708
      data(horiz, vert) = ray.result_color();
600,480✔
1709
    }
600,480✔
1710
  }
1711

1712
  return data;
55✔
1713
}
1714

1715
void SolidRayTracePlot::create_output() const
33✔
1716
{
1717
  ImageData data = create_image();
33✔
1718
  write_image(data);
33✔
1719
}
33✔
1720

1721
void SolidRayTracePlot::set_opaque_ids(pugi::xml_node node)
33✔
1722
{
1723
  if (check_for_node(node, "opaque_ids")) {
33!
1724
    auto opaque_ids_tmp = get_node_array<int>(node, "opaque_ids");
33✔
1725

1726
    // It is read in as actual ID values, but we have to convert to indices in
1727
    // mat/cell array
1728
    for (auto& x : opaque_ids_tmp)
99✔
1729
      x = color_by_ == PlotColorBy::mats ? model::material_map[x]
132!
1730
                                         : model::cell_map[x];
×
1731

1732
    opaque_ids_.insert(opaque_ids_tmp.begin(), opaque_ids_tmp.end());
33✔
1733
  }
33✔
1734
}
33✔
1735

1736
void SolidRayTracePlot::set_light_position(pugi::xml_node node)
33✔
1737
{
1738
  if (check_for_node(node, "light_position")) {
33✔
1739
    auto light_pos_tmp = get_node_array<double>(node, "light_position");
11✔
1740

1741
    if (light_pos_tmp.size() != 3)
11!
1742
      fatal_error("Light position must be given as 3D coordinates");
×
1743

1744
    light_location_.x = light_pos_tmp[0];
11✔
1745
    light_location_.y = light_pos_tmp[1];
11✔
1746
    light_location_.z = light_pos_tmp[2];
11✔
1747
  } else {
11✔
1748
    light_location_ = camera_position();
22✔
1749
  }
1750
}
33✔
1751

1752
void SolidRayTracePlot::set_diffuse_fraction(pugi::xml_node node)
33✔
1753
{
1754
  if (check_for_node(node, "diffuse_fraction")) {
33✔
1755
    diffuse_fraction_ = std::stod(get_node_value(node, "diffuse_fraction"));
11✔
1756
    if (diffuse_fraction_ < 0.0 || diffuse_fraction_ > 1.0) {
11!
1757
      fatal_error("Must have 0 <= diffuse fraction <= 1");
×
1758
    }
1759
  }
1760
}
33✔
1761

1762
void ProjectionRay::on_intersection()
2,359,148✔
1763
{
1764
  // This records a tuple with the following info
1765
  //
1766
  // 1) ID (material or cell depending on color_by_)
1767
  // 2) Distance traveled by the ray through that ID
1768
  // 3) Index of the intersected surface (starting from 1)
1769

1770
  line_segments_.emplace_back(
2,359,148✔
1771
    plot_.color_by_ == PlottableInterface::PlotColorBy::mats
2,359,148✔
1772
      ? material()
545,919✔
1773
      : lowest_coord().cell(),
1,813,229✔
1774
    traversal_distance_, boundary().surface_index());
2,359,148✔
1775
}
2,359,148✔
1776

1777
void PhongRay::on_intersection()
905,971✔
1778
{
1779
  // Check if we hit an opaque material or cell
1780
  int hit_id = plot_.color_by_ == PlottableInterface::PlotColorBy::mats
905,971✔
1781
                 ? material()
905,971!
1782
                 : lowest_coord().cell();
×
1783

1784
  // If we are reflected and have advanced beyond the camera,
1785
  // the ray is done. This is checked here because we should
1786
  // kill the ray even if the material is not opaque.
1787
  if (reflected_ && (r() - plot_.camera_position()).dot(u()) >= 0.0) {
905,971!
1788
    stop();
×
1789
    return;
164,340✔
1790
  }
1791

1792
  // Anything that's not opaque has zero impact on the plot.
1793
  if (plot_.opaque_ids_.find(hit_id) == plot_.opaque_ids_.end())
905,971✔
1794
    return;
1795

1796
  if (!reflected_) {
741,631✔
1797
    // reflect the particle and set the color to be colored by
1798
    // the normal or the diffuse lighting contribution
1799
    reflected_ = true;
706,068✔
1800
    result_color_ = plot_.colors_[hit_id];
706,068✔
1801
    // The ray has been advanced slightly past the boundary. Use an
1802
    // approximation to the actual hit point for stable normal/lighting.
1803
    Position r_hit = r() - TINY_BIT * u();
706,068✔
1804
    Direction to_light = plot_.light_location_ - r_hit;
706,068✔
1805
    to_light /= to_light.norm();
706,068✔
1806

1807
    // TODO
1808
    // Not sure what can cause a surface token to be invalid here, although it
1809
    // sometimes happens for a few pixels. It's very very rare, so proceed by
1810
    // coloring the pixel with the overlap color. It seems to happen only for a
1811
    // few pixels on the outer boundary of a hex lattice.
1812
    //
1813
    // We cannot detect it in the outer loop, and it only matters here, so
1814
    // that's why the error handling is a little different than for a lost
1815
    // ray.
1816
    if (surface() == 0) {
706,068!
1817
      result_color_ = plot_.overlap_color_;
×
1818
      stop();
×
1819
      return;
×
1820
    }
1821

1822
    // Get surface pointer
1823
    const auto& surf = model::surfaces.at(surface_index());
706,068✔
1824

1825
    // The crossed surface may be on a higher coordinate level than the
1826
    // innermost local coordinates, so we check the surface's coordinate level
1827
    // to find the appropriate coordinate level to use for the normal
1828
    // calculation
1829
    int surf_level = boundary().coord_level() - 1;
706,068!
1830
    // ensure surface level is within bounds of current coordinate stack
1831
    surf_level = std::max(0, std::min(surf_level, n_coord() - 1));
706,068!
1832

1833
    Position r_hit_level =
706,068✔
1834
      coord(surf_level).r() - TINY_BIT * coord(surf_level).u();
706,068✔
1835
    Direction normal = surf->normal(r_hit_level);
706,068✔
1836
    normal /= normal.norm();
706,068✔
1837

1838
    // Need to apply rotations to find the normal vector in
1839
    // the base level universe's coordinate system.
1840
    for (int lev = surf_level - 1; lev >= 0; --lev) {
706,068!
1841
      if (coord(lev + 1).rotated()) {
×
1842
        const Cell& c {*model::cells[coord(lev).cell()]};
×
1843
        normal = normal.inverse_rotate(c.rotation_);
×
1844
      }
1845
    }
1846

1847
    // use the normal opposed to the ray direction
1848
    if (normal.dot(u()) > 0.0) {
706,068✔
1849
      normal *= -1.0;
63,789✔
1850
    }
1851

1852
    // Facing away from the light means no lighting
1853
    double dotprod = normal.dot(to_light);
706,068✔
1854
    dotprod = std::max(0.0, dotprod);
706,068✔
1855

1856
    double modulation =
706,068✔
1857
      plot_.diffuse_fraction_ + (1.0 - plot_.diffuse_fraction_) * dotprod;
706,068✔
1858
    result_color_ *= modulation;
706,068✔
1859

1860
    // Now point the particle to the camera. We now begin
1861
    // checking to see if it's occluded by another surface
1862
    u() = to_light;
706,068✔
1863

1864
    orig_hit_id_ = hit_id;
706,068✔
1865

1866
    // OpenMC native CSG and DAGMC surfaces have some slight differences
1867
    // in how they interpret particles that are sitting on a surface.
1868
    // I don't know exactly why, but this makes everything work beautifully.
1869
    if (surf->geom_type() == GeometryType::DAG) {
706,068!
1870
      surface() = 0;
×
1871
    } else {
1872
      surface() = -surface(); // go to other side
706,068✔
1873
    }
1874

1875
    // Must fully restart coordinate search. Why? Not sure.
1876
    clear();
706,068✔
1877

1878
    // Note this could likely be faster if we cached the previous
1879
    // cell we were in before the reflection. This is the easiest
1880
    // way to fully initialize all the sub-universe coordinates and
1881
    // directions though.
1882
    bool found = exhaustive_find_cell(*this);
706,068✔
1883
    if (!found) {
706,068!
1884
      fatal_error("Lost particle after reflection.");
×
1885
    }
1886

1887
    // Must recalculate distance to boundary due to the
1888
    // direction change
1889
    compute_distance();
706,068✔
1890

1891
  } else {
1892
    // If it's not facing the light, we color with the diffuse contribution, so
1893
    // next we check if we're going to occlude the last reflected surface. if
1894
    // so, color by the diffuse contribution instead
1895

1896
    if (orig_hit_id_ == -1)
35,563!
1897
      fatal_error("somehow a ray got reflected but not original ID set?");
×
1898

1899
    result_color_ = plot_.colors_[orig_hit_id_];
35,563✔
1900
    result_color_ *= plot_.diffuse_fraction_;
35,563✔
1901
    stop();
741,631✔
1902
  }
1903
}
1904

1905
extern "C" int openmc_id_map(const void* plot, int32_t* data_out)
×
1906
{
1907
  static bool warned {false};
×
1908
  if (!warned) {
×
1909
    warning("openmc_id_map is deprecated and will be removed in a future "
×
1910
            "release. Use openmc_slice_data.");
1911
    warned = true;
×
1912
  }
1913

1914
  auto plt = reinterpret_cast<const SlicePlotBase*>(plot);
×
1915
  if (!plt) {
×
1916
    set_errmsg("Invalid slice pointer passed to openmc_id_map");
×
1917
    return OPENMC_E_INVALID_ARGUMENT;
×
1918
  }
1919

1920
  if (plt->show_overlaps_ && model::overlap_check_count.size() == 0) {
×
1921
    model::overlap_check_count.resize(model::cells.size());
×
1922
  }
1923

1924
  auto ids = plt->get_map<IdData>();
×
1925

1926
  // write id data to array
1927
  std::copy(ids.data_.begin(), ids.data_.end(), data_out);
×
1928

1929
  return 0;
×
1930
}
×
1931

1932
extern "C" int openmc_property_map(const void* plot, double* data_out)
×
1933
{
1934
  static bool warned {false};
×
1935
  if (!warned) {
×
1936
    warning("openmc_property_map is deprecated and will be removed in a future "
×
1937
            "release. Use openmc_slice_data.");
1938
    warned = true;
×
1939
  }
1940

1941
  auto plt = reinterpret_cast<const SlicePlotBase*>(plot);
×
1942
  if (!plt) {
×
1943
    set_errmsg("Invalid slice pointer passed to openmc_property_map");
×
1944
    return OPENMC_E_INVALID_ARGUMENT;
×
1945
  }
1946

1947
  if (plt->show_overlaps_ && model::overlap_check_count.size() == 0) {
×
1948
    model::overlap_check_count.resize(model::cells.size());
×
1949
  }
1950

1951
  auto props = plt->get_map<PropertyData>();
×
1952

1953
  // write id data to array
1954
  std::copy(props.data_.begin(), props.data_.end(), data_out);
×
1955

1956
  return 0;
×
1957
}
×
1958

1959
extern "C" int openmc_slice_data(const double origin[3], const double u_span[3],
432✔
1960
  const double v_span[3], const size_t pixels[2], bool color_overlaps,
1961
  int level, int32_t filter_index, int32_t* geom_data, double* property_data)
1962
{
1963
  // Validate span vectors
1964
  Direction u_span_pos {u_span[0], u_span[1], u_span[2]};
432✔
1965
  Direction v_span_pos {v_span[0], v_span[1], v_span[2]};
432✔
1966
  double u_norm = u_span_pos.norm();
432✔
1967
  double v_norm = v_span_pos.norm();
432✔
1968
  if (u_norm == 0.0 || v_norm == 0.0) {
432!
1969
    set_errmsg("Slice span vectors must be non-zero.");
×
1970
    return OPENMC_E_INVALID_ARGUMENT;
×
1971
  }
1972

1973
  constexpr double ORTHO_REL_TOL = 1e-10;
432✔
1974
  double dot = u_span_pos.dot(v_span_pos);
432!
1975
  if (std::abs(dot) > ORTHO_REL_TOL * u_norm * v_norm) {
432!
1976
    set_errmsg("Slice span vectors must be orthogonal.");
×
1977
    return OPENMC_E_INVALID_ARGUMENT;
×
1978
  }
1979

1980
  // Validate filter index if provided
1981
  if (filter_index >= 0) {
432✔
1982
    if (int err = verify_filter(filter_index))
22!
1983
      return err;
1984
  }
1985

1986
  // Initialize overlap check vector if needed
1987
  if (color_overlaps && model::overlap_check_count.size() == 0) {
432!
1988
    model::overlap_check_count.resize(model::cells.size());
77✔
1989
  }
1990

1991
  if (color_overlaps) {
432✔
1992
    settings::check_overlaps = true;
77✔
1993
  }
1994

1995
  try {
432✔
1996
    // Create a temporary SlicePlotBase object to reuse get_map logic
1997
    SlicePlotBase plot_params;
432✔
1998
    plot_params.origin_ = Position {origin[0], origin[1], origin[2]};
432✔
1999
    plot_params.u_span_ = u_span_pos;
432✔
2000
    plot_params.v_span_ = v_span_pos;
432✔
2001
    plot_params.pixels_[0] = pixels[0];
432✔
2002
    plot_params.pixels_[1] = pixels[1];
432✔
2003
    plot_params.show_overlaps_ = color_overlaps;
432✔
2004
    plot_params.slice_level_ = level;
432✔
2005

2006
    // Use get_map<RasterData> to generate data
2007
    auto data = plot_params.get_map<RasterData>(filter_index);
432✔
2008

2009
    // Copy overlap data to unique_ptr structure to be saved for plotting
2010
    model::last_slice_data = std::make_unique<RasterData>(std::move(data));
864✔
2011

2012
    std::copy(model::last_slice_data->id_data_.begin(),
432✔
2013
      model::last_slice_data->id_data_.end(), geom_data);
432✔
2014

2015
    if (property_data != nullptr) {
432✔
2016
      std::copy(model::last_slice_data->property_data_.begin(),
66✔
2017
        model::last_slice_data->property_data_.end(), property_data);
66✔
2018
    }
2019
  } catch (const std::exception& e) {
432!
2020
    set_errmsg(e.what());
×
2021
    return OPENMC_E_UNASSIGNED;
×
2022
  }
×
2023

2024
  return 0;
432✔
2025
}
2026

2027
// Gets the number of overlaps for a specific pixel
2028
extern "C" int openmc_slice_data_overlap_count(
66✔
2029
  int32_t x, int32_t y, int32_t* count)
2030
{
2031
  if (!model::last_slice_data) {
66!
NEW
2032
    set_errmsg("No slice plot data available.");
×
NEW
2033
    return OPENMC_E_UNASSIGNED;
×
2034
  }
2035
  if (!count) {
66!
NEW
2036
    set_errmsg("Null pointer passed for overlap count.");
×
NEW
2037
    return OPENMC_E_INVALID_ARGUMENT;
×
2038
  }
2039

2040
  size_t width = model::last_slice_data->id_data_.shape(1);
66!
2041
  size_t height = model::last_slice_data->id_data_.shape(0);
66!
2042

2043
  if (x < 0 || y < 0 || static_cast<size_t>(x) >= width ||
66!
2044
      static_cast<size_t>(y) >= height) {
66!
NEW
2045
    set_errmsg("Pixel indices out of bounds.");
×
NEW
2046
    return OPENMC_E_OUT_OF_BOUNDS;
×
2047
  }
2048

2049
  size_t pix = static_cast<size_t>(y) * width + static_cast<size_t>(x);
66✔
2050
  *count =
66✔
2051
    static_cast<int32_t>(model::last_slice_data->pixel_overlaps_[pix].size());
66✔
2052

2053
  return 0;
66✔
2054
}
2055

2056
// Gets the overlap data for a specific pixel
2057
// Plotter pre-allocates array sizes based on what is returned with
2058
// overlap_count
2059
extern "C" int openmc_slice_data_overlap_info(
22✔
2060
  int32_t x, int32_t y, int32_t* cell1, int32_t* cell2, int32_t* universe)
2061
{
2062
  if (!model::last_slice_data) {
22!
NEW
2063
    set_errmsg("No slice plot data available.");
×
NEW
2064
    return OPENMC_E_UNASSIGNED;
×
2065
  }
2066
  if (!cell1 || !cell2 || !universe) {
22!
NEW
2067
    set_errmsg("Null pointer passed for overlap data.");
×
NEW
2068
    return OPENMC_E_INVALID_ARGUMENT;
×
2069
  }
2070

2071
  size_t width = model::last_slice_data->id_data_.shape(1);
22!
2072
  size_t height = model::last_slice_data->id_data_.shape(0);
22!
2073

2074
  if (x < 0 || y < 0 || static_cast<size_t>(x) >= width ||
22!
2075
      static_cast<size_t>(y) >= height) {
22!
NEW
2076
    set_errmsg("Pixel indices out of bounds.");
×
NEW
2077
    return OPENMC_E_OUT_OF_BOUNDS;
×
2078
  }
2079

2080
  size_t pix = static_cast<size_t>(y) * width + static_cast<size_t>(x);
22✔
2081
  const auto& pairs = model::last_slice_data->pixel_overlaps_[pix];
22✔
2082

2083
  for (size_t i = 0; i < pairs.size(); ++i) {
55✔
2084
    cell1[i] = pairs[i].cell1_id;
33✔
2085
    cell2[i] = pairs[i].cell2_id;
33✔
2086
    universe[i] = pairs[i].universe_id;
33✔
2087
  }
2088

2089
  return 0;
2090
}
2091

2092
extern "C" int openmc_get_plot_index(int32_t id, int32_t* index)
22✔
2093
{
2094
  auto it = model::plot_map.find(id);
22!
2095
  if (it == model::plot_map.end()) {
22!
2096
    set_errmsg("No plot exists with ID=" + std::to_string(id) + ".");
×
2097
    return OPENMC_E_INVALID_ID;
×
2098
  }
2099

2100
  *index = it->second;
22✔
2101
  return 0;
22✔
2102
}
2103

2104
extern "C" int openmc_plot_get_id(int32_t index, int32_t* id)
55✔
2105
{
2106
  if (index < 0 || index >= model::plots.size()) {
55!
2107
    set_errmsg("Index in plots array is out of bounds.");
×
2108
    return OPENMC_E_OUT_OF_BOUNDS;
×
2109
  }
2110

2111
  *id = model::plots[index]->id();
55✔
2112
  return 0;
55✔
2113
}
2114

2115
extern "C" int openmc_plot_set_id(int32_t index, int32_t id)
×
2116
{
2117
  if (index < 0 || index >= model::plots.size()) {
×
2118
    set_errmsg("Index in plots array is out of bounds.");
×
2119
    return OPENMC_E_OUT_OF_BOUNDS;
×
2120
  }
2121

2122
  if (id < 0 && id != C_NONE) {
×
2123
    set_errmsg("Invalid plot ID.");
×
2124
    return OPENMC_E_INVALID_ARGUMENT;
×
2125
  }
2126

2127
  auto* plot = model::plots[index].get();
×
2128
  int32_t old_id = plot->id();
×
2129
  if (id == old_id)
×
2130
    return 0;
2131

2132
  model::plot_map.erase(old_id);
×
2133
  try {
×
2134
    plot->set_id(id);
×
2135
  } catch (const std::runtime_error& e) {
×
2136
    model::plot_map[old_id] = index;
×
2137
    set_errmsg(e.what());
×
2138
    return OPENMC_E_INVALID_ID;
×
2139
  }
×
2140
  model::plot_map[plot->id()] = index;
×
2141
  return 0;
×
2142
}
2143

2144
extern "C" size_t openmc_plots_size()
22✔
2145
{
2146
  return model::plots.size();
22✔
2147
}
2148

2149
int map_phong_domain_id(
55✔
2150
  const SolidRayTracePlot* plot, int32_t id, int32_t* index_out)
2151
{
2152
  if (!plot || !index_out) {
55!
2153
    set_errmsg("Invalid plot pointer passed to map_phong_domain_id");
×
2154
    return OPENMC_E_INVALID_ARGUMENT;
×
2155
  }
2156

2157
  if (plot->color_by_ == PlottableInterface::PlotColorBy::mats) {
55!
2158
    auto it = model::material_map.find(id);
55!
2159
    if (it == model::material_map.end()) {
55!
2160
      set_errmsg("Invalid material ID for SolidRayTracePlot");
×
2161
      return OPENMC_E_INVALID_ID;
×
2162
    }
2163
    *index_out = it->second;
55✔
2164
    return 0;
55✔
2165
  }
2166

2167
  if (plot->color_by_ == PlottableInterface::PlotColorBy::cells) {
×
2168
    auto it = model::cell_map.find(id);
×
2169
    if (it == model::cell_map.end()) {
×
2170
      set_errmsg("Invalid cell ID for SolidRayTracePlot");
×
2171
      return OPENMC_E_INVALID_ID;
×
2172
    }
2173
    *index_out = it->second;
×
2174
    return 0;
×
2175
  }
2176

2177
  set_errmsg("Unsupported color_by for SolidRayTracePlot");
×
2178
  return OPENMC_E_INVALID_TYPE;
×
2179
}
2180

2181
int get_solidraytrace_plot_by_index(int32_t index, SolidRayTracePlot** plot)
308✔
2182
{
2183
  if (!plot) {
308!
2184
    set_errmsg("Null output pointer passed to get_solidraytrace_plot_by_index");
×
2185
    return OPENMC_E_INVALID_ARGUMENT;
×
2186
  }
2187

2188
  if (index < 0 || index >= model::plots.size()) {
308!
2189
    set_errmsg("Index in plots array is out of bounds.");
×
2190
    return OPENMC_E_OUT_OF_BOUNDS;
×
2191
  }
2192

2193
  auto* plottable = model::plots[index].get();
308!
2194
  auto* solid_plot = dynamic_cast<SolidRayTracePlot*>(plottable);
308!
2195
  if (!solid_plot) {
308!
2196
    set_errmsg("Plot at index=" + std::to_string(index) +
×
2197
               " is not a solid raytrace plot.");
2198
    return OPENMC_E_INVALID_TYPE;
×
2199
  }
2200

2201
  *plot = solid_plot;
308✔
2202
  return 0;
308✔
2203
}
2204

2205
extern "C" int openmc_solidraytrace_plot_create(int32_t* index)
11✔
2206
{
2207
  if (!index) {
11!
2208
    set_errmsg(
×
2209
      "Null output pointer passed to openmc_solidraytrace_plot_create");
2210
    return OPENMC_E_INVALID_ARGUMENT;
×
2211
  }
2212

2213
  try {
11✔
2214
    auto new_plot = std::make_unique<SolidRayTracePlot>();
11✔
2215
    new_plot->set_id();
11✔
2216
    int32_t new_plot_id = new_plot->id();
11✔
2217
#ifdef USE_LIBPNG
2218
    new_plot->path_plot() = fmt::format("plot_{}.png", new_plot_id);
11✔
2219
#else
2220
    new_plot->path_plot() = fmt::format("plot_{}.ppm", new_plot_id);
2221
#endif
2222
    int32_t new_plot_index = model::plots.size();
11✔
2223
    model::plots.emplace_back(std::move(new_plot));
11✔
2224
    model::plot_map[new_plot_id] = new_plot_index;
11✔
2225
    *index = new_plot_index;
11✔
2226
  } catch (const std::exception& e) {
11!
2227
    set_errmsg(e.what());
×
2228
    return OPENMC_E_ALLOCATE;
×
2229
  }
×
2230

2231
  return 0;
11✔
2232
}
2233

2234
extern "C" int openmc_solidraytrace_plot_get_pixels(
33✔
2235
  int32_t index, int32_t* width, int32_t* height)
2236
{
2237
  if (!width || !height) {
33!
2238
    set_errmsg(
×
2239
      "Invalid arguments passed to openmc_solidraytrace_plot_get_pixels");
2240
    return OPENMC_E_INVALID_ARGUMENT;
×
2241
  }
2242

2243
  SolidRayTracePlot* plt = nullptr;
33✔
2244
  int err = get_solidraytrace_plot_by_index(index, &plt);
33✔
2245
  if (err)
33!
2246
    return err;
2247

2248
  *width = plt->pixels()[0];
33✔
2249
  *height = plt->pixels()[1];
33✔
2250
  return 0;
33✔
2251
}
2252

2253
extern "C" int openmc_solidraytrace_plot_set_pixels(
11✔
2254
  int32_t index, int32_t width, int32_t height)
2255
{
2256
  if (width <= 0 || height <= 0) {
11!
2257
    set_errmsg(
×
2258
      "Invalid arguments passed to openmc_solidraytrace_plot_set_pixels");
2259
    return OPENMC_E_INVALID_ARGUMENT;
×
2260
  }
2261

2262
  SolidRayTracePlot* plt = nullptr;
11✔
2263
  int err = get_solidraytrace_plot_by_index(index, &plt);
11✔
2264
  if (err)
11!
2265
    return err;
2266

2267
  plt->pixels()[0] = width;
11✔
2268
  plt->pixels()[1] = height;
11✔
2269
  return 0;
11✔
2270
}
2271

2272
extern "C" int openmc_solidraytrace_plot_get_color_by(
11✔
2273
  int32_t index, int32_t* color_by)
2274
{
2275
  if (!color_by) {
11!
2276
    set_errmsg(
×
2277
      "Invalid arguments passed to openmc_solidraytrace_plot_get_color_by");
2278
    return OPENMC_E_INVALID_ARGUMENT;
×
2279
  }
2280

2281
  SolidRayTracePlot* plt = nullptr;
11✔
2282
  int err = get_solidraytrace_plot_by_index(index, &plt);
11✔
2283
  if (err)
11!
2284
    return err;
2285

2286
  if (plt->color_by_ == PlottableInterface::PlotColorBy::mats) {
11!
2287
    *color_by = 0;
11✔
2288
  } else if (plt->color_by_ == PlottableInterface::PlotColorBy::cells) {
×
2289
    *color_by = 1;
×
2290
  } else {
2291
    set_errmsg("Unsupported color_by for SolidRayTracePlot");
×
2292
    return OPENMC_E_INVALID_TYPE;
×
2293
  }
2294

2295
  return 0;
2296
}
2297

2298
extern "C" int openmc_solidraytrace_plot_set_color_by(
11✔
2299
  int32_t index, int32_t color_by)
2300
{
2301
  SolidRayTracePlot* plt = nullptr;
11✔
2302
  int err = get_solidraytrace_plot_by_index(index, &plt);
11✔
2303
  if (err)
11!
2304
    return err;
2305

2306
  if (color_by == 0) {
11!
2307
    plt->color_by_ = PlottableInterface::PlotColorBy::mats;
11✔
2308
  } else if (color_by == 1) {
×
2309
    plt->color_by_ = PlottableInterface::PlotColorBy::cells;
×
2310
  } else {
2311
    set_errmsg("Invalid color_by value for SolidRayTracePlot");
×
2312
    return OPENMC_E_INVALID_ARGUMENT;
×
2313
  }
2314

2315
  return 0;
2316
}
2317

2318
extern "C" int openmc_solidraytrace_plot_set_default_colors(int32_t index)
11✔
2319
{
2320
  SolidRayTracePlot* plt = nullptr;
11✔
2321
  int err = get_solidraytrace_plot_by_index(index, &plt);
11✔
2322
  if (err)
11!
2323
    return err;
2324

2325
  plt->set_default_colors();
11✔
2326
  return 0;
2327
}
2328

2329
extern "C" int openmc_solidraytrace_plot_set_all_opaque(int32_t index)
×
2330
{
2331
  SolidRayTracePlot* plt = nullptr;
×
2332
  int err = get_solidraytrace_plot_by_index(index, &plt);
×
2333
  if (err)
×
2334
    return err;
2335

2336
  plt->opaque_ids().clear();
×
2337
  if (plt->color_by_ == PlottableInterface::PlotColorBy::mats) {
×
2338
    for (int32_t i = 0; i < model::materials.size(); ++i) {
×
2339
      plt->opaque_ids().insert(i);
×
2340
    }
2341
    return 0;
×
2342
  }
2343

2344
  if (plt->color_by_ == PlottableInterface::PlotColorBy::cells) {
×
2345
    for (int32_t i = 0; i < model::cells.size(); ++i) {
×
2346
      plt->opaque_ids().insert(i);
×
2347
    }
2348
    return 0;
×
2349
  }
2350

2351
  set_errmsg("Unsupported color_by for SolidRayTracePlot");
×
2352
  return OPENMC_E_INVALID_TYPE;
×
2353
}
2354

2355
extern "C" int openmc_solidraytrace_plot_set_opaque(
22✔
2356
  int32_t index, int32_t id, bool visible)
2357
{
2358
  SolidRayTracePlot* plt = nullptr;
22✔
2359
  int err = get_solidraytrace_plot_by_index(index, &plt);
22✔
2360
  if (err)
22!
2361
    return err;
2362

2363
  int32_t domain_index = -1;
22✔
2364
  err = map_phong_domain_id(plt, id, &domain_index);
22✔
2365
  if (err)
22!
2366
    return err;
2367

2368
  if (visible) {
22✔
2369
    plt->opaque_ids().insert(domain_index);
11✔
2370
  } else {
2371
    plt->opaque_ids().erase(domain_index);
11✔
2372
  }
2373

2374
  return 0;
2375
}
2376

2377
extern "C" int openmc_solidraytrace_plot_set_color(
22✔
2378
  int32_t index, int32_t id, uint8_t r, uint8_t g, uint8_t b)
2379
{
2380
  SolidRayTracePlot* plt = nullptr;
22✔
2381
  int err = get_solidraytrace_plot_by_index(index, &plt);
22✔
2382
  if (err)
22!
2383
    return err;
2384

2385
  int32_t domain_index = -1;
22✔
2386
  err = map_phong_domain_id(plt, id, &domain_index);
22✔
2387
  if (err)
22!
2388
    return err;
2389

2390
  if (domain_index < 0 ||
22!
2391
      static_cast<size_t>(domain_index) >= plt->colors_.size()) {
22!
2392
    set_errmsg("Color index out of range for SolidRayTracePlot");
×
2393
    return OPENMC_E_OUT_OF_BOUNDS;
×
2394
  }
2395

2396
  plt->colors_[domain_index] = RGBColor(r, g, b);
22✔
2397
  return 0;
22✔
2398
}
2399

2400
extern "C" int openmc_solidraytrace_plot_get_camera_position(
11✔
2401
  int32_t index, double* x, double* y, double* z)
2402
{
2403
  if (!x || !y || !z) {
11!
2404
    set_errmsg("Invalid arguments passed to "
×
2405
               "openmc_solidraytrace_plot_get_camera_position");
2406
    return OPENMC_E_INVALID_ARGUMENT;
×
2407
  }
2408

2409
  SolidRayTracePlot* plt = nullptr;
11✔
2410
  int err = get_solidraytrace_plot_by_index(index, &plt);
11✔
2411
  if (err)
11!
2412
    return err;
2413

2414
  const auto& camera_position = plt->camera_position();
11✔
2415
  *x = camera_position.x;
11✔
2416
  *y = camera_position.y;
11✔
2417
  *z = camera_position.z;
11✔
2418
  return 0;
11✔
2419
}
2420

2421
extern "C" int openmc_solidraytrace_plot_set_camera_position(
11✔
2422
  int32_t index, double x, double y, double z)
2423
{
2424
  SolidRayTracePlot* plt = nullptr;
11✔
2425
  int err = get_solidraytrace_plot_by_index(index, &plt);
11✔
2426
  if (err)
11!
2427
    return err;
2428

2429
  plt->camera_position() = {x, y, z};
11✔
2430
  return 0;
11✔
2431
}
2432

2433
extern "C" int openmc_solidraytrace_plot_get_look_at(
11✔
2434
  int32_t index, double* x, double* y, double* z)
2435
{
2436
  if (!x || !y || !z) {
11!
2437
    set_errmsg(
×
2438
      "Invalid arguments passed to openmc_solidraytrace_plot_get_look_at");
2439
    return OPENMC_E_INVALID_ARGUMENT;
×
2440
  }
2441

2442
  SolidRayTracePlot* plt = nullptr;
11✔
2443
  int err = get_solidraytrace_plot_by_index(index, &plt);
11✔
2444
  if (err)
11!
2445
    return err;
2446

2447
  const auto& look_at = plt->look_at();
11✔
2448
  *x = look_at.x;
11✔
2449
  *y = look_at.y;
11✔
2450
  *z = look_at.z;
11✔
2451
  return 0;
11✔
2452
}
2453

2454
extern "C" int openmc_solidraytrace_plot_set_look_at(
11✔
2455
  int32_t index, double x, double y, double z)
2456
{
2457
  SolidRayTracePlot* plt = nullptr;
11✔
2458
  int err = get_solidraytrace_plot_by_index(index, &plt);
11✔
2459
  if (err)
11!
2460
    return err;
2461

2462
  plt->look_at() = {x, y, z};
11✔
2463
  return 0;
11✔
2464
}
2465

2466
extern "C" int openmc_solidraytrace_plot_get_up(
11✔
2467
  int32_t index, double* x, double* y, double* z)
2468
{
2469
  if (!x || !y || !z) {
11!
2470
    set_errmsg("Invalid arguments passed to openmc_solidraytrace_plot_get_up");
×
2471
    return OPENMC_E_INVALID_ARGUMENT;
×
2472
  }
2473

2474
  SolidRayTracePlot* plt = nullptr;
11✔
2475
  int err = get_solidraytrace_plot_by_index(index, &plt);
11✔
2476
  if (err)
11!
2477
    return err;
2478

2479
  const auto& up = plt->up();
11✔
2480
  *x = up.x;
11✔
2481
  *y = up.y;
11✔
2482
  *z = up.z;
11✔
2483
  return 0;
11✔
2484
}
2485

2486
extern "C" int openmc_solidraytrace_plot_set_up(
11✔
2487
  int32_t index, double x, double y, double z)
2488
{
2489
  SolidRayTracePlot* plt = nullptr;
11✔
2490
  int err = get_solidraytrace_plot_by_index(index, &plt);
11✔
2491
  if (err)
11!
2492
    return err;
2493

2494
  plt->up() = {x, y, z};
11✔
2495
  return 0;
11✔
2496
}
2497

2498
extern "C" int openmc_solidraytrace_plot_get_light_position(
11✔
2499
  int32_t index, double* x, double* y, double* z)
2500
{
2501
  if (!x || !y || !z) {
11!
2502
    set_errmsg("Invalid arguments passed to "
×
2503
               "openmc_solidraytrace_plot_get_light_position");
2504
    return OPENMC_E_INVALID_ARGUMENT;
×
2505
  }
2506

2507
  SolidRayTracePlot* plt = nullptr;
11✔
2508
  int err = get_solidraytrace_plot_by_index(index, &plt);
11✔
2509
  if (err)
11!
2510
    return err;
2511

2512
  const auto& light_position = plt->light_location();
11✔
2513
  *x = light_position.x;
11✔
2514
  *y = light_position.y;
11✔
2515
  *z = light_position.z;
11✔
2516
  return 0;
11✔
2517
}
2518

2519
extern "C" int openmc_solidraytrace_plot_set_light_position(
11✔
2520
  int32_t index, double x, double y, double z)
2521
{
2522
  SolidRayTracePlot* plt = nullptr;
11✔
2523
  int err = get_solidraytrace_plot_by_index(index, &plt);
11✔
2524
  if (err)
11!
2525
    return err;
2526

2527
  plt->light_location() = {x, y, z};
11✔
2528
  return 0;
11✔
2529
}
2530

2531
extern "C" int openmc_solidraytrace_plot_get_fov(int32_t index, double* fov)
11✔
2532
{
2533
  if (!fov) {
11!
2534
    set_errmsg("Invalid arguments passed to openmc_solidraytrace_plot_get_fov");
×
2535
    return OPENMC_E_INVALID_ARGUMENT;
×
2536
  }
2537

2538
  SolidRayTracePlot* plt = nullptr;
11✔
2539
  int err = get_solidraytrace_plot_by_index(index, &plt);
11✔
2540
  if (err)
11!
2541
    return err;
2542

2543
  *fov = plt->horizontal_field_of_view();
11✔
2544
  return 0;
11✔
2545
}
2546

2547
extern "C" int openmc_solidraytrace_plot_set_fov(int32_t index, double fov)
11✔
2548
{
2549
  SolidRayTracePlot* plt = nullptr;
11✔
2550
  int err = get_solidraytrace_plot_by_index(index, &plt);
11✔
2551
  if (err)
11!
2552
    return err;
2553

2554
  plt->horizontal_field_of_view() = fov;
11✔
2555
  return 0;
11✔
2556
}
2557

2558
extern "C" int openmc_solidraytrace_plot_update_view(int32_t index)
22✔
2559
{
2560
  SolidRayTracePlot* plt = nullptr;
22✔
2561
  int err = get_solidraytrace_plot_by_index(index, &plt);
22✔
2562
  if (err)
22!
2563
    return err;
2564

2565
  plt->update_view();
22✔
2566
  return 0;
2567
}
2568

2569
extern "C" int openmc_solidraytrace_plot_create_image(
22✔
2570
  int32_t index, uint8_t* data_out, int32_t width, int32_t height)
2571
{
2572
  if (!data_out || width <= 0 || height <= 0) {
22!
2573
    set_errmsg(
×
2574
      "Invalid arguments passed to openmc_solidraytrace_plot_create_image");
2575
    return OPENMC_E_INVALID_ARGUMENT;
×
2576
  }
2577

2578
  SolidRayTracePlot* plt = nullptr;
22✔
2579
  int err = get_solidraytrace_plot_by_index(index, &plt);
22✔
2580
  if (err)
22!
2581
    return err;
2582

2583
  if (plt->pixels()[0] != width || plt->pixels()[1] != height) {
22!
2584
    set_errmsg(
×
2585
      "Requested image size does not match SolidRayTracePlot pixel settings");
2586
    return OPENMC_E_INVALID_SIZE;
×
2587
  }
2588

2589
  ImageData data = plt->create_image();
22✔
2590
  if (static_cast<int32_t>(data.shape()[0]) != width ||
22!
2591
      static_cast<int32_t>(data.shape()[1]) != height) {
22!
2592
    set_errmsg("Unexpected image size from SolidRayTracePlot create_image");
×
2593
    return OPENMC_E_INVALID_SIZE;
×
2594
  }
2595

2596
  for (int32_t y = 0; y < height; ++y) {
154✔
2597
    for (int32_t x = 0; x < width; ++x) {
1,188✔
2598
      const auto& color = data(x, y);
1,056✔
2599
      size_t idx = (static_cast<size_t>(y) * width + x) * 3;
1,056✔
2600
      data_out[idx + 0] = color.red;
1,056✔
2601
      data_out[idx + 1] = color.green;
1,056✔
2602
      data_out[idx + 2] = color.blue;
1,056✔
2603
    }
2604
  }
2605

2606
  return 0;
2607
}
22✔
2608

2609
extern "C" int openmc_solidraytrace_plot_get_color(
11✔
2610
  int32_t index, int32_t id, uint8_t* r, uint8_t* g, uint8_t* b)
2611
{
2612
  if (!r || !g || !b) {
11!
2613
    set_errmsg(
×
2614
      "Invalid arguments passed to openmc_solidraytrace_plot_get_color");
2615
    return OPENMC_E_INVALID_ARGUMENT;
×
2616
  }
2617

2618
  SolidRayTracePlot* plt = nullptr;
11✔
2619
  int err = get_solidraytrace_plot_by_index(index, &plt);
11✔
2620
  if (err)
11!
2621
    return err;
2622

2623
  int32_t domain_index = -1;
11✔
2624
  err = map_phong_domain_id(plt, id, &domain_index);
11✔
2625
  if (err)
11!
2626
    return err;
2627

2628
  if (domain_index < 0 ||
11!
2629
      static_cast<size_t>(domain_index) >= plt->colors_.size()) {
11!
2630
    set_errmsg("Color index out of range for SolidRayTracePlot");
×
2631
    return OPENMC_E_OUT_OF_BOUNDS;
×
2632
  }
2633

2634
  const auto& color = plt->colors_[domain_index];
11✔
2635
  *r = color.red;
11✔
2636
  *g = color.green;
11✔
2637
  *b = color.blue;
11✔
2638
  return 0;
11✔
2639
}
2640

2641
extern "C" int openmc_solidraytrace_plot_get_diffuse_fraction(
11✔
2642
  int32_t index, double* diffuse_fraction)
2643
{
2644
  if (!diffuse_fraction) {
11!
2645
    set_errmsg("Invalid arguments passed to "
×
2646
               "openmc_solidraytrace_plot_get_diffuse_fraction");
2647
    return OPENMC_E_INVALID_ARGUMENT;
×
2648
  }
2649

2650
  SolidRayTracePlot* plt = nullptr;
11✔
2651
  int err = get_solidraytrace_plot_by_index(index, &plt);
11✔
2652
  if (err)
11!
2653
    return err;
2654

2655
  *diffuse_fraction = plt->diffuse_fraction();
11✔
2656
  return 0;
11✔
2657
}
2658

2659
extern "C" int openmc_solidraytrace_plot_set_diffuse_fraction(
11✔
2660
  int32_t index, double diffuse_fraction)
2661
{
2662
  SolidRayTracePlot* plt = nullptr;
11✔
2663
  int err = get_solidraytrace_plot_by_index(index, &plt);
11✔
2664
  if (err)
11!
2665
    return err;
2666

2667
  if (diffuse_fraction < 0.0 || diffuse_fraction > 1.0) {
11!
2668
    set_errmsg("Diffuse fraction must be between 0 and 1");
×
2669
    return OPENMC_E_INVALID_ARGUMENT;
×
2670
  }
2671

2672
  plt->diffuse_fraction() = diffuse_fraction;
11✔
2673
  return 0;
11✔
2674
}
2675

2676
} // 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