MIDSX 0.1
A x-ray transport code system for dosimetry
Loading...
Searching...
No Matches
voxel_grid.h
1#ifndef VOXELGRID_H
2#define VOXELGRID_H
3
4#include <vector>
5#include "voxel.h"
6#include "interaction_data.h"
7#include <Eigen/Dense>
8#include <pybind11/embed.h>
9#include <pybind11/numpy.h>
10#include <unordered_map>
11#include <iostream>
12#include <utility>
13
14namespace py = pybind11;
15
16namespace VoxelGridHelpers {
17 template <typename T, std::size_t N>
18 Eigen::Vector<T, N> pyTupleToEigenVector(const py::tuple &tuple);
19 double covertUnitNameToCm(const std::string& unitName);
20 int dimensionStringToIndex(const std::string& dimension);
21}
22
28class VoxelGrid {
29public:
30 VoxelGrid() = default;
31
38 explicit VoxelGrid(std::string filename, bool is_python_environment = false);
39
46 Voxel& getVoxel(const Eigen::Vector3i& voxel_index);
47
53 void setVoxel(const Eigen::Vector3i& voxel_index, Voxel& value);
54
60 Eigen::Vector3d getVoxelPosition(const Eigen::Vector3i& voxel_index);
61
67 bool withinGrid(const Eigen::Vector3d& position) const;
68
74 Eigen::Vector3i getVoxelIndex(const Eigen::Vector3d& position) const;
75
81 std::vector<std::string> getMaterialNames() const;
82
89
95 std::unordered_map<int, VectorValue> getEnergyDepositedInMaterials();
96
100 void addExit() {
101 numExits_++;
102 }
103
108 int getNumExits() const {
109 return numExits_;
110 }
111
116 Eigen::Vector3d getDimSpace() const {
117 return dim_space_;
118 }
119
120private:
121 Eigen::Vector3i dim_vox_;
122 Eigen::Vector3d spacing_; // in cm
123 Eigen::Vector3d dim_space_; // in cm
124 int numOfVoxels_ = 0;
125 int numExits_ = 0;
126 std::vector<Voxel> voxels_;
127 std::string filename_;
128 double units_ = 1.0; // in cm
129 bool is_python_environment_;
130
131 // initialize voxels according to the nifti file
132 void initializeVoxels();
133
134 // calculate the index of the voxel at (i, j, k)
135 int voxelNumber(const Eigen::Vector3i& voxel_index) const;
136
137 void handleOutOfBounds(const Eigen::Vector3d& position) const;
138
139 void setVoxelProperties(const py::array_t<uint8_t>& array, const py::object &header);
140
141 static py::array_t<uint8_t> getNumPyArrayFromImg(const py::object& img);
142 void setDimVox(const py::array_t<uint8_t>& array);
143 void setUnits(const py::object& header);
144 void setSpacing(const py::object& header);
145 void setNumOfVoxels();
146 void setDimSpace();
147 void setVoxelMaterialIDs(const py::array_t<uint8_t>& array);
148};
149
150#endif // VOXELGRID_H
Class which represents a voxel grid.
Definition voxel_grid.h:28
Eigen::Vector3d getDimSpace() const
Gets the spatial dimensions of the voxel grid.
Definition voxel_grid.h:116
bool withinGrid(const Eigen::Vector3d &position) const
Checks if a spatial position is within the voxel grid.
void addExit()
Adds to the number of photons that have exited the voxel grid.
Definition voxel_grid.h:100
VoxelGrid(std::string filename, bool is_python_environment=false)
Constructor for the VoxelGrid class.
Eigen::Vector3d getVoxelPosition(const Eigen::Vector3i &voxel_index)
Gets the spatial position of the voxel at (i, j, k).
std::unordered_map< int, VectorValue > getEnergyDepositedInMaterials()
Gets the total energy deposited in the voxel grid by material.
double getTotalEnergyDeposited()
Gets the total energy deposited in the voxel grid.
std::vector< std::string > getMaterialNames() const
Gets vector of material names in voxel grid.
void setVoxel(const Eigen::Vector3i &voxel_index, Voxel &value)
Sets the voxel at (i, j, k) to specified voxel.
int getNumExits() const
Gets the number of photons that have exited the voxel grid.
Definition voxel_grid.h:108
Eigen::Vector3i getVoxelIndex(const Eigen::Vector3d &position) const
Gets the voxel index of a spatial position.
Voxel & getVoxel(const Eigen::Vector3i &voxel_index)
Gets the voxel at (i, j, k).
Struct which represents a voxel.
Definition voxel.h:12