Cluster.cpp 3.03 KB
Newer Older
Owen Arnold's avatar
Owen Arnold committed
1
2
3
#include "MantidCrystal/Cluster.h"
#include "MantidAPI/IMDHistoWorkspace.h"

4
5
namespace {
typedef std::vector<Mantid::Crystal::DisjointElement> VecElements;
Owen Arnold's avatar
Owen Arnold committed
6
7
}

8
9
namespace Mantid {
namespace Crystal {
Owen Arnold's avatar
Owen Arnold committed
10

11
12
13
14
15
16
17
18
/**
 * Constructor
 * @param label : Label (taken as original) for Cluster
 */
Cluster::Cluster(const size_t &label)
    : m_originalLabel(label), m_rootCluster(this) {
  m_indexes.reserve(1000);
}
Owen Arnold's avatar
Owen Arnold committed
19

20
21
22
23
24
25
26
27
28
29
30
/**
 * Get the label
 * @return : Return label.
 */
size_t Cluster::getLabel() const {
  if (m_rootCluster != this) {
    return m_rootCluster->getLabel();
  }

  return m_originalLabel;
}
Owen Arnold's avatar
Owen Arnold committed
31

32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
 * Get the original label
 * @return : Original label
 */
size_t Cluster::getOriginalLabel() const { return m_originalLabel; }

/**
 * Get the number of indexes which are part of this cluster. i.e. the size of
 * the cluster
 * @return : Cluster size
 */
size_t Cluster::size() const { return m_indexes.size(); }

/**
 * Add the index to the cluster
 * @param index : index to add
 */
void Cluster::addIndex(const size_t &index) { m_indexes.push_back(index); }

/**
 * Apply the cluster to the image
 * @param ws : Image to stencil itself onto
 */
void Cluster::writeTo(Mantid::API::IMDHistoWorkspace_sptr ws) const {
  const size_t label = this->getLabel();
57
58
59
  for (auto index : m_indexes) {
    ws->setSignalAt(index, static_cast<Mantid::signal_t>(label));
    ws->setErrorSquaredAt(index, 0);
60
61
  }
}
Owen Arnold's avatar
Owen Arnold committed
62

63
64
65
66
67
68
69
70
71
72
/**
 * Integrate the image over cluster region
 * @param ws : Image
 * @return : Integrated values
 */
ICluster::ClusterIntegratedValues
Cluster::integrate(Mantid::API::IMDHistoWorkspace_const_sptr ws) const {
  double errorIntSQ = 0;
  double sigInt = 0;
  // Integrate accross indexes owned by this workspace.
Hahn, Steven's avatar
Hahn, Steven committed
73
74
75
  for (auto index : m_indexes) {
    sigInt += ws->getSignalAt(index);
    double errorSQ = ws->getErrorAt(index);
76
77
78
79
80
    errorSQ *= errorSQ;
    errorIntSQ += errorSQ;
  }
  return ClusterIntegratedValues(sigInt, errorIntSQ);
}
Owen Arnold's avatar
Owen Arnold committed
81

82
83
84
85
86
/**
 * Allow the cluster to adopt the uniform minimum value.
 * @param disjointSet
 */
void Cluster::toUniformMinimum(VecElements &disjointSet) {
87
  if (!m_indexes.empty()) {
88
    size_t parentIndex = m_rootCluster->getRepresentitiveIndex();
Owen Arnold's avatar
Owen Arnold committed
89

90
91
    for (size_t i = 1; i < m_indexes.size(); ++i) {
      disjointSet[m_indexes[i]].unionWith(disjointSet[parentIndex].getParent());
Owen Arnold's avatar
Owen Arnold committed
92
    }
93
94
  }
}
Owen Arnold's avatar
Owen Arnold committed
95

96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
 * Get a representative index which is part of this cluster
 * @return
 */
size_t Cluster::getRepresentitiveIndex() const { return m_indexes.front(); }

/**
 * Set the root cluster
 * @param root : Root cluster
 */
void Cluster::setRootCluster(ICluster const *root) { m_rootCluster = root; }

/**
 * Overlaoded equals operator
 * @param other : To compare with
 * @return True only if the objects are equal
 */
bool Cluster::operator==(const Cluster &other) const {
  return getLabel() == other.getLabel();
}
Owen Arnold's avatar
Owen Arnold committed
116

117
118
119
120
121
122
123
124
/**
 * Does the cluster contain the label.
 * @param label : Label to find
 * @return : True only if the label exists.
 */
bool Cluster::containsLabel(const size_t &label) const {
  return (label == this->getLabel());
}
125

126
} // namespace Crystal
127
} // namespace Mantid