18 namespace DDSegmentation {
23 using std::runtime_error;
24 using std::stringstream;
29 _name(
"Segmentation"), _type(
"Segmentation"), _decoder(new
BitField64(cellEncoding)), _ownsDecoder(true) {
35 _name(
"Segmentation"), _type(
"Segmentation"), _decoder(newDecoder), _ownsDecoder(false) {
43 map<std::string, SegmentationParameter*>::iterator it;
55 throw std::runtime_error(
"This segmentation type:"+
_type+
" does not support sub-segmentations.");
60 map<std::string, StringParameter>::const_iterator it;
63 std::string identifier = it->second->typedValue();
64 (*_decoder)[identifier] = 0;
71 map<std::string, StringParameter>::const_iterator it;
73 const std::string& identifier = it->second->typedValue();
75 int currentValue = (*_decoder)[identifier];
78 (*_decoder)[identifier] = currentValue - 1;
80 }
catch (runtime_error&
e) {
84 (*_decoder)[identifier] = currentValue + 1;
86 }
catch (runtime_error& e) {
104 map<std::string, Parameter>::const_iterator it;
110 s <<
"Unknown parameter " << parameterName <<
" for segmentation type " <<
_type;
111 throw std::runtime_error(s.str());
117 map<std::string, Parameter>::const_iterator it;
119 pars.push_back(it->second);
126 Parameters::const_iterator it;
127 for (it = pars.begin(); it != pars.end(); ++it) {
135 const std::string& defaultValue) {
145 return bin * cellSize + offset;
150 if (cellSize <= 1
e-10) {
151 throw runtime_error(
"Invalid cell size: 0.0");
153 return int(floor((position + 0.5 * cellSize - offset) / cellSize));
158 return (cellBoundaries[bin+1] + cellBoundaries[bin])*0.5 + offset;
164 if(fabs(position/cellBoundaries.front()-1.0) < 3
e-12)
return 0;
167 if(fabs(position/cellBoundaries.back()-1.0) < 3
e-12)
return int(cellBoundaries.size()-2);
170 if(position < cellBoundaries.front()) {
171 std::stringstream err;
172 err << std::setprecision(20) << std::scientific;
173 err <<
"Hit Position (" << position <<
") is below the acceptance"
174 <<
" (" << cellBoundaries.front() <<
") "
175 <<
"of the segmentation";
176 throw std::runtime_error(err.str());
178 if(position > cellBoundaries.back() ) {
179 std::stringstream err;
180 err << std::setprecision(20) << std::scientific;
181 err <<
"Hit Position (" << position <<
") is above the acceptance"
182 <<
" (" << cellBoundaries.back() <<
") "
183 <<
"of the segmentation";
184 throw std::runtime_error(err.str());
188 std::vector<double>::const_iterator bin = std::upper_bound(cellBoundaries.begin(),
189 cellBoundaries.end(),
193 return bin - cellBoundaries.begin() - 1 ;
198 std::stringstream errorMessage;
199 errorMessage << __func__ <<
" is not implemented for " <<
_type;
200 throw std::logic_error(errorMessage.str());
long long int CellID
Useful typedefs to differentiate cell IDs and volume IDs.
void registerIdentifier(const std::string &nam, const std::string &desc, std::string &ident, const std::string &defaultVal)
Add a cell identifier to this segmentation. Used by derived classes to define their required identifi...
virtual VolumeID volumeID(const CellID &cellID) const
Determine the volume ID from the full cell ID by removing all local fields.
std::map< std::string, StringParameter > _indexIdentifiers
The indices used for the encoding.
virtual void neighbours(const CellID &cellID, std::set< CellID > &neighbours) const
Calculates the neighbours of the given cell ID and adds them to the list of neighbours.
const std::string & name() const
Access to the parameter name.
virtual Parameters parameters() const
Access to all parameters.
BitField64 * _decoder
The cell ID encoder and decoder.
std::vector< Parameter > Parameters
virtual void setParameters(const Parameters ¶meters)
Set all parameters from an existing set of parameters.
Segmentation(const std::string &cellEncoding="")
Default constructor used by derived classes passing the encoding string.
virtual std::vector< double > cellDimensions(const CellID &cellID) const
Returns a vector<double> of the cellDimensions of the given cell ID in natural order of dimensions...
bool _ownsDecoder
Keeps track of the decoder ownership.
std::map< std::string, Parameter > _parameters
The parameters for this segmentation.
virtual std::string value() const =0
Access to the parameter value in string representation.
Base class for all segmentations.
static int positionToBin(double position, double cellSize, double offset=0.)
Helper method to convert a 1D position to a cell ID.
virtual Parameter parameter(const std::string ¶meterName) const
Access to parameter by name.
std::string _type
The segmentation type.
virtual void addSubsegmentation(long key_min, long key_max, Segmentation *entry)
Add subsegmentation. Call only valid for Multi-segmentations. Default implementation throws an except...
virtual void setDecoder(BitField64 *decoder)
Set the underlying decoder.
void setValue(long64 value)
virtual ~Segmentation()
Destructor.
Class to hold a segmentation parameter with its description.
static double binToPosition(CellID bin, double cellSize, double offset=0.)
Helper method to convert a bin number to a 1D position.