FreeCAD

Форк
0
/
Utilities.cpp 
166 строк · 5.5 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2010 Werner Mayer <wmayer[at]users.sourceforge.net>     *
3
 *                                                                         *
4
 *   This file is part of the FreeCAD CAx development system.              *
5
 *                                                                         *
6
 *   This library is free software; you can redistribute it and/or         *
7
 *   modify it under the terms of the GNU Library General Public           *
8
 *   License as published by the Free Software Foundation; either          *
9
 *   version 2 of the License, or (at your option) any later version.      *
10
 *                                                                         *
11
 *   This library  is distributed in the hope that it will be useful,      *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 *   GNU Library General Public License for more details.                  *
15
 *                                                                         *
16
 *   You should have received a copy of the GNU Library General Public     *
17
 *   License along with this library; see the file COPYING.LIB. If not,    *
18
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
19
 *   Suite 330, Boston, MA  02111-1307, USA                                *
20
 *                                                                         *
21
 ***************************************************************************/
22

23
#include "PreCompiled.h"
24
#ifndef _PreComp_
25
# include <Inventor/SbTesselator.h>
26
# include <QAbstractItemModel>
27
# include <QAbstractItemView>
28
# include <QItemSelection>
29
# include <QItemSelectionModel>
30
#endif
31

32
#include <App/DocumentObject.h>
33

34
#include "Utilities.h"
35

36

37
using namespace Gui;
38

39

40
ViewVolumeProjection::ViewVolumeProjection (const SbViewVolume &vv)
41
  : viewVolume(vv)
42
{
43
    matrix = viewVolume.getMatrix();
44
    invert = matrix.inverse();
45
}
46

47
Base::Vector3f ViewVolumeProjection::operator()(const Base::Vector3f &pt) const
48
{
49
    Base::Vector3f src;
50
    transformInput(pt, src);
51

52
    SbVec3f pt3d(src.x,src.y,src.z);
53

54
    // See SbViewVolume::projectToScreen
55
    matrix.multVecMatrix(pt3d, pt3d);
56

57
    return Base::Vector3f(0.5*pt3d[0]+0.5, 0.5*pt3d[1]+0.5, 0.5*pt3d[2]+0.5);
58
}
59

60
Base::Vector3d ViewVolumeProjection::operator()(const Base::Vector3d &pt) const
61
{
62
    auto ptf = Base::convertTo<Base::Vector3f>(pt);
63
    ptf = operator()(ptf);
64
    return Base::convertTo<Base::Vector3d>(ptf);
65
}
66

67
Base::Vector3f ViewVolumeProjection::inverse (const Base::Vector3f &pt) const
68
{
69
    SbVec3f pt3d(2.0f*pt.x-1.0f, 2.0f*pt.y-1.0f, 2.0f*pt.z-1.0f);
70
    invert.multVecMatrix(pt3d, pt3d);
71
    return Base::Vector3f(pt3d[0],pt3d[1],pt3d[2]);
72
}
73

74
Base::Vector3d ViewVolumeProjection::inverse (const Base::Vector3d &pt) const
75
{
76
    auto ptf = Base::convertTo<Base::Vector3f>(pt);
77
    ptf = inverse(ptf);
78
    return Base::convertTo<Base::Vector3d>(ptf);
79
}
80

81
Base::Matrix4D ViewVolumeProjection::getProjectionMatrix () const
82
{
83
    // Inventor stores the transposed matrix
84
    Base::Matrix4D mat;
85

86
    for (int i=0; i<4; i++) {
87
        for (int j=0; j<4; j++)
88
            mat[i][j] = matrix[j][i];
89
    }
90

91
    return mat;
92
}
93

94
// ----------------------------------------------------------------------------
95

96
void Tessellator::tessCB(void * v0, void * v1, void * v2, void * cbdata)
97
{
98
    int * vtx0 = (int *)v0;
99
    int * vtx1 = (int *)v1;
100
    int * vtx2 = (int *)v2;
101

102
    auto array = (std::vector<int> *)cbdata;
103
    array->push_back(*vtx0);
104
    array->push_back(*vtx1);
105
    array->push_back(*vtx2);
106
    array->push_back(-1);
107
}
108

109
Tessellator::Tessellator(const std::vector<SbVec2f>& poly) : polygon(poly)
110
{
111
}
112

113
std::vector<int> Tessellator::tessellate() const
114
{
115
    std::vector<int> indices(polygon.size());
116
    std::vector<int> face_indices;
117

118
    SbTesselator tessellator(tessCB, &face_indices);
119
    tessellator.beginPolygon();
120

121
    int index = 0;
122
    for (std::vector<SbVec2f>::const_iterator it = polygon.begin(); it != polygon.end(); ++it, index++) {
123
        indices[index] = index;
124
        tessellator.addVertex(SbVec3f((*it)[0], (*it)[1], 0.0f), &(indices[index]));
125
    }
126

127
    // run the triangulation now
128
    tessellator.endPolygon();
129
    return face_indices;
130
}
131

132
// ----------------------------------------------------------------------------
133

134
class ItemViewSelection::MatchName {
135
public:
136
    explicit MatchName(const QString& n) : name(n)
137
    {}
138
    bool operator() (const App::DocumentObject* obj) {
139
        return name == QLatin1String(obj->getNameInDocument());
140
    }
141
private:
142
    QString name;
143
};
144

145
ItemViewSelection::ItemViewSelection(QAbstractItemView* view)
146
  : view(view)
147
{
148
}
149

150
void ItemViewSelection::applyFrom(const std::vector<App::DocumentObject*> objs)
151
{
152
    QAbstractItemModel* model = view->model();
153
    QItemSelection range;
154
    for (int i=0; i<model->rowCount(); i++) {
155
        QModelIndex item = model->index(i,0);
156
        if (item.isValid()) {
157
            QVariant name = model->data(item, Qt::UserRole);
158
            std::vector<App::DocumentObject*>::const_iterator it;
159
            it = std::find_if(objs.begin(), objs.end(), MatchName(name.toString()));
160
            if (it != objs.end())
161
                range.select(item, item);
162
        }
163
    }
164

165
    view->selectionModel()->select(range, QItemSelectionModel::Select);
166
}
167

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.