Developer Documentation
ValenceHistogramDialog.cc
1 /*
2  * ValenceHistogramDialog.cc
3  *
4  * Created on: Jan 27, 2016
5  * Author: hc
6  */
7 
8 #include "ValenceHistogramDialog.hh"
9 
10 #include <ACG/Utils/SmartPointer.hh>
11 
12 ValenceHistogramDialog::ValenceHistogramDialog(TriMesh &mesh, QWidget *parent)
13  : QDialog(parent) {
14  setupUi(this);
15  init(mesh);
16 }
17 
18 ValenceHistogramDialog::ValenceHistogramDialog(PolyMesh &mesh, QWidget *parent)
19  : QDialog(parent) {
20  setupUi(this);
21  init(mesh);
22 }
23 
24 static void fillHistogramTable(std::vector<size_t> &hist, QTableWidget &tw) {
25  tw.clear();
26 
27  std::vector<size_t>::iterator nonzero_begin = hist.begin();
28  for (; nonzero_begin != hist.end() && *nonzero_begin == 0;
29  ++nonzero_begin);
30  if (nonzero_begin == hist.end()) return;
31 
32  std::vector<size_t>::iterator nonzero_end = hist.end();
33  for (; (nonzero_end-1) != nonzero_begin && *(nonzero_end-1) == 0;
34  --nonzero_end);
35 
36  const size_t hist_size = std::distance(nonzero_begin, nonzero_end);
37  const size_t ofs = std::distance(hist.begin(), nonzero_begin);
38 
39  tw.setRowCount(1);
40  tw.setColumnCount(hist_size);
41 
42  QStringList vheaders;
43  vheaders.push_back(QString::fromUtf8("Count"));
44  tw.setVerticalHeaderLabels(vheaders);
45  int i = 0;
46  QStringList hheaders;
47  for (std::vector<size_t>::iterator it = nonzero_begin;
48  it != nonzero_end; ++it, ++i) {
49  //tw.setItem(0, i, new QTableWidgetItem(QString::number(ofs + i)));
50  hheaders.push_back(QString::number(ofs + i));
51  tw.setItem(0, i, new QTableWidgetItem(QString::number(*it)));
52  }
53  tw.setHorizontalHeaderLabels(hheaders);
54 }
55 
56 namespace {
57 
58 class ValenceHistogram : public ACG::Histogram {
59 public:
60  explicit ValenceHistogram(const std::vector<size_t> &bins)
61  {
62  std::vector<size_t>::const_iterator nonzero_begin = bins.begin();
63  for (; nonzero_begin != bins.end() && *nonzero_begin == 0;
64  ++nonzero_begin);
65  if (nonzero_begin == bins.end()) return;
66 
67  std::vector<size_t>::const_iterator nonzero_end = bins.end();
68  for (; (nonzero_end-1) != nonzero_begin && *(nonzero_end-1) == 0;
69  --nonzero_end);
70 
71  ofs_ = std::distance(bins.begin(), nonzero_begin);
72  bins_.assign(nonzero_begin, nonzero_end);
73  bin_widths_.assign(bins_.size() + 1, 1);
74  }
75 
76  LabelType getLabelType() const override
77  {
78  return LabelType::PerBin;
79  }
80 
81  QString getBinLabel (size_t idx) const override
82  {
83  return QString::number(ofs_ + idx);
84  }
85 
86  double getTotalWidth() const override {
87  return bins_.size();
88  }
89 private:
90  size_t ofs_ = 0;
91 };
92 
93 } // namespace
94 
95 template<class MeshT>
96 void ValenceHistogramDialog::init(MeshT &mesh) {
97  /*
98  * Vertices
99  */
100  vertex_valence_hist.clear();
101  for (typename MeshT::VertexIter v_it = mesh.vertices_begin(),
102  v_end = mesh.vertices_end(); v_it != v_end; ++v_it) {
103  size_t valence = mesh.valence(*v_it);
104  if (vertex_valence_hist.size() <= valence) {
105  vertex_valence_hist.resize(valence + 1, 0);
106  }
107  vertex_valence_hist[valence] += 1;
108  }
109  vertexValenceChart_wdgt->setHistogram(ptr::make_unique<ValenceHistogram>(vertex_valence_hist));
110  fillHistogramTable(vertex_valence_hist, *vertexValence_tw);
111 
112  /*
113  * Faces
114  */
115  face_valence_hist.clear();
116  for (typename MeshT::FaceIter f_it = mesh.faces_begin(),
117  f_end = mesh.faces_end(); f_it != f_end; ++f_it) {
118  size_t valence = mesh.valence(*f_it);
119  if (face_valence_hist.size() <= valence) {
120  face_valence_hist.resize(
121  valence + 1, 0);
122  }
123  face_valence_hist[valence] += 1;
124  }
125 
126  faceValenceChart_wdgt->setHistogram(ptr::make_unique<ValenceHistogram>(face_valence_hist));
127  fillHistogramTable(face_valence_hist, *faceValence_tw);
128 }