ValenceHistogramDialog.cc 3.78 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * ValenceHistogramDialog.cc
 *
 *  Created on: Jan 27, 2016
 *      Author: hc
 */

#include "ValenceHistogramDialog.hh"

10
#include <ACG/Utils/SmartPointer.hh>
11 12 13 14 15 16 17 18 19 20

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

ValenceHistogramDialog::ValenceHistogramDialog(PolyMesh &mesh, QWidget *parent)
    : QDialog(parent) {
    setupUi(this);
21
    init(mesh);
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 55
}

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);
}

56 57 58 59
namespace {

class ValenceHistogram : public ACG::Histogram {
public:
Jan Möbius's avatar
Jan Möbius committed
60
    explicit ValenceHistogram(const std::vector<size_t> &bins)
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 94
    {
        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

95 96 97 98 99 100
template<class MeshT>
void ValenceHistogramDialog::init(MeshT &mesh) {
    /*
     * Vertices
     */
    vertex_valence_hist.clear();
101
    for (typename MeshT::VertexIter v_it = mesh.vertices_begin(),
102 103 104 105 106 107 108
            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;
    }
109
    vertexValenceChart_wdgt->setHistogram(ptr::make_unique<ValenceHistogram>(vertex_valence_hist));
110 111 112 113 114 115
    fillHistogramTable(vertex_valence_hist, *vertexValence_tw);

    /*
     * Faces
     */
    face_valence_hist.clear();
116
    for (typename MeshT::FaceIter f_it = mesh.faces_begin(),
117 118 119 120
            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(
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 128
    fillHistogramTable(face_valence_hist, *faceValence_tw);
}