Debian Codegraph @

src/Gui/Utilities.cpp

raw
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
24 #include <Inventor/SbTesselator.h>
25 #include <QAbstractItemModel>
26 #include <QAbstractItemView>
27 #include <QItemSelection>
28 #include <QItemSelectionModel>
29
30
31 #include <App/DocumentObject.h>
32
33 #include "Utilities.h"
34
35
36 using namespace Gui;
37
38
39 ViewVolumeProjection::ViewVolumeProjection(const SbViewVolume& vv)
40 : viewVolume(vv)
41 {
42 matrix = viewVolume.getMatrix();
43 invert = matrix.inverse();
44 }
45
46 Base::Vector3f ViewVolumeProjection::operator()(const Base::Vector3f& pt) const
47 {
48 Base::Vector3f src;
49 transformInput(pt, src);
50
51 SbVec3f pt3d(src.x, src.y, src.z);
52
53 // See SbViewVolume::projectToScreen
54 matrix.multVecMatrix(pt3d, pt3d);
55
56 return Base::Vector3f(0.5 * pt3d[0] + 0.5, 0.5 * pt3d[1] + 0.5, 0.5 * pt3d[2] + 0.5);
57 }
58
59 Base::Vector3d ViewVolumeProjection::operator()(const Base::Vector3d& pt) const
60 {
61 auto ptf = Base::convertTo<Base::Vector3f>(pt);
62 ptf = operator()(ptf);
63 return Base::convertTo<Base::Vector3d>(ptf);
64 }
65
66 Base::Vector3f ViewVolumeProjection::inverse(const Base::Vector3f& pt) const
67 {
68 SbVec3f pt3d(2.0f * pt.x - 1.0f, 2.0f * pt.y - 1.0f, 2.0f * pt.z - 1.0f);
69 invert.multVecMatrix(pt3d, pt3d);
70 return Base::Vector3f(pt3d[0], pt3d[1], pt3d[2]);
71 }
72
73 Base::Vector3d ViewVolumeProjection::inverse(const Base::Vector3d& pt) const
74 {
75 auto ptf = Base::convertTo<Base::Vector3f>(pt);
76 ptf = inverse(ptf);
77 return Base::convertTo<Base::Vector3d>(ptf);
78 }
79
80 Base::Matrix4D ViewVolumeProjection::getProjectionMatrix() const
81 {
82 // Inventor stores the transposed matrix
83 Base::Matrix4D mat;
84
85 for (int i = 0; i < 4; i++) {
86 for (int j = 0; j < 4; j++) {
87 mat[i][j] = matrix[j][i];
88 }
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)
110 : polygon(poly)
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();
123 ++it, index++) {
124 indices[index] = index;
125 tessellator.addVertex(SbVec3f((*it)[0], (*it)[1], 0.0f), &(indices[index]));
126 }
127
128 // run the triangulation now
129 tessellator.endPolygon();
130 return face_indices;
131 }
132
133 // ----------------------------------------------------------------------------
134
135 class ItemViewSelection::MatchName
136 {
137 public:
138 explicit MatchName(const QString& n)
139 : name(n)
140 {}
141 bool operator()(const App::DocumentObject* obj)
142 {
143 return name == QLatin1String(obj->getNameInDocument());
144 }
145
146 private:
147 QString name;
148 };
149
150 ItemViewSelection::ItemViewSelection(QAbstractItemView* view)
151 : view(view)
152 {}
153
154 void ItemViewSelection::applyFrom(const std::vector<App::DocumentObject*> objs)
155 {
156 QAbstractItemModel* model = view->model();
157 QItemSelection range;
158 for (int i = 0; i < model->rowCount(); i++) {
159 QModelIndex item = model->index(i, 0);
160 if (item.isValid()) {
161 QVariant name = model->data(item, Qt::UserRole);
162 std::vector<App::DocumentObject*>::const_iterator it;
163 it = std::find_if(objs.begin(), objs.end(), MatchName(name.toString()));
164 if (it != objs.end()) {
165 range.select(item, item);
166 }
167 }
168 }
169
170 view->selectionModel()->select(range, QItemSelectionModel::Select);
171 }