ValenceHistogramDialog.cc 2.67 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 55 56 57 58 59 60
}

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

template<class MeshT>
void ValenceHistogramDialog::init(MeshT &mesh) {
    /*
     * Vertices
     */
    vertex_valence_hist.clear();
61
    for (typename MeshT::VertexIter v_it = mesh.vertices_begin(),
62 63 64 65 66 67 68 69 70 71 72 73 74 75
            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;
    }
    vertexValenceChart_wdgt->setHistogram(&vertex_valence_hist);
    fillHistogramTable(vertex_valence_hist, *vertexValence_tw);

    /*
     * Faces
     */
    face_valence_hist.clear();
76
    for (typename MeshT::FaceIter f_it = mesh.faces_begin(),
77 78 79 80
            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(
81
                    valence + 1, 0);
82
        }
83
        face_valence_hist[valence] += 1;
84
    }
85

86 87 88
    faceValenceChart_wdgt->setHistogram(&face_valence_hist);
    fillHistogramTable(face_valence_hist, *faceValence_tw);
}