ValenceHistogramDialog.cc 3.71 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
 * ValenceHistogramDialog.cc
 *
 *  Created on: Jan 27, 2016
 *      Author: hc
 */

#include "ValenceHistogramDialog.hh"


ValenceHistogramDialog::ValenceHistogramDialog(TriMesh &mesh, QWidget *parent)
    : QDialog(parent) {
    setupUi(this);
    init(mesh);
}

ValenceHistogramDialog::ValenceHistogramDialog(PolyMesh &mesh, QWidget *parent)
    : QDialog(parent) {
    setupUi(this);
20
    init(mesh);
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
}

static void fillHistogramTable(std::vector<size_t> &hist, QTableWidget &tw) {
    tw.clear();

    std::vector<size_t>::iterator nonzero_begin = hist.begin();
    for (; nonzero_begin != hist.end() && *nonzero_begin == 0;
            ++nonzero_begin);
    if (nonzero_begin == hist.end()) return;

    std::vector<size_t>::iterator nonzero_end = hist.end();
    for (; (nonzero_end-1) != nonzero_begin && *(nonzero_end-1) == 0;
            --nonzero_end);

    const size_t hist_size = std::distance(nonzero_begin, nonzero_end);
    const size_t ofs = std::distance(hist.begin(), nonzero_begin);

    tw.setRowCount(1);
    tw.setColumnCount(hist_size);

    QStringList vheaders;
    vheaders.push_back(QString::fromUtf8("Count"));
    tw.setVerticalHeaderLabels(vheaders);
    int i = 0;
    QStringList hheaders;
    for (std::vector<size_t>::iterator it = nonzero_begin;
            it != nonzero_end; ++it, ++i) {
        //tw.setItem(0, i, new QTableWidgetItem(QString::number(ofs + i)));
        hheaders.push_back(QString::number(ofs + i));
        tw.setItem(0, i, new QTableWidgetItem(QString::number(*it)));
    }
    tw.setHorizontalHeaderLabels(hheaders);
}

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
namespace {

class ValenceHistogram : public ACG::Histogram {
public:
    ValenceHistogram(const std::vector<size_t> &bins)
    {
        std::vector<size_t>::const_iterator nonzero_begin = bins.begin();
        for (; nonzero_begin != bins.end() && *nonzero_begin == 0;
                ++nonzero_begin);
        if (nonzero_begin == bins.end()) return;

        std::vector<size_t>::const_iterator nonzero_end = bins.end();
        for (; (nonzero_end-1) != nonzero_begin && *(nonzero_end-1) == 0;
                --nonzero_end);

        ofs_ = std::distance(bins.begin(), nonzero_begin);
        bins_.assign(nonzero_begin, nonzero_end);
        bin_widths_.assign(bins_.size() + 1, 1);
    }

    LabelType getLabelType() const override
    {
        return LabelType::PerBin;
    }

    QString getBinLabel (size_t idx) const override
    {
        return QString::number(ofs_ + idx);
    }

    double getTotalWidth() const override {
        return bins_.size();
    }
private:
    size_t ofs_ = 0;
};

} // namespace

94
95
96
97
98
99
template<class MeshT>
void ValenceHistogramDialog::init(MeshT &mesh) {
    /*
     * Vertices
     */
    vertex_valence_hist.clear();
100
    for (typename MeshT::VertexIter v_it = mesh.vertices_begin(),
101
102
103
104
105
106
107
            v_end = mesh.vertices_end(); v_it != v_end; ++v_it) {
        size_t valence = mesh.valence(*v_it);
        if (vertex_valence_hist.size() <= valence) {
            vertex_valence_hist.resize(valence + 1, 0);
        }
        vertex_valence_hist[valence] += 1;
    }
108
    vertexValenceChart_wdgt->setHistogram(new ValenceHistogram(vertex_valence_hist));
109
110
111
112
113
114
    fillHistogramTable(vertex_valence_hist, *vertexValence_tw);

    /*
     * Faces
     */
    face_valence_hist.clear();
115
    for (typename MeshT::FaceIter f_it = mesh.faces_begin(),
116
117
118
119
            f_end = mesh.faces_end(); f_it != f_end; ++f_it) {
        size_t valence = mesh.valence(*f_it);
        if (face_valence_hist.size() <= valence) {
            face_valence_hist.resize(
120
                    valence + 1, 0);
121
        }
122
        face_valence_hist[valence] += 1;
123
    }
124

125
    faceValenceChart_wdgt->setHistogram(new ValenceHistogram(face_valence_hist));
126
127
    fillHistogramTable(face_valence_hist, *faceValence_tw);
}