1
/***************************************************************************
2
* Copyright (c) 2010 Werner Mayer <wmayer[at]users.sourceforge.net> *
4
* This file is part of the FreeCAD CAx development system. *
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. *
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. *
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 *
21
***************************************************************************/
23
#include "PreCompiled.h"
25
# include <Inventor/SbTesselator.h>
26
# include <QAbstractItemModel>
27
# include <QAbstractItemView>
28
# include <QItemSelection>
29
# include <QItemSelectionModel>
32
#include <App/DocumentObject.h>
40
ViewVolumeProjection::ViewVolumeProjection (const SbViewVolume &vv)
43
matrix = viewVolume.getMatrix();
44
invert = matrix.inverse();
47
Base::Vector3f ViewVolumeProjection::operator()(const Base::Vector3f &pt) const
50
transformInput(pt, src);
52
SbVec3f pt3d(src.x,src.y,src.z);
54
// See SbViewVolume::projectToScreen
55
matrix.multVecMatrix(pt3d, pt3d);
57
return Base::Vector3f(0.5*pt3d[0]+0.5, 0.5*pt3d[1]+0.5, 0.5*pt3d[2]+0.5);
60
Base::Vector3d ViewVolumeProjection::operator()(const Base::Vector3d &pt) const
62
auto ptf = Base::convertTo<Base::Vector3f>(pt);
63
ptf = operator()(ptf);
64
return Base::convertTo<Base::Vector3d>(ptf);
67
Base::Vector3f ViewVolumeProjection::inverse (const Base::Vector3f &pt) const
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]);
74
Base::Vector3d ViewVolumeProjection::inverse (const Base::Vector3d &pt) const
76
auto ptf = Base::convertTo<Base::Vector3f>(pt);
78
return Base::convertTo<Base::Vector3d>(ptf);
81
Base::Matrix4D ViewVolumeProjection::getProjectionMatrix () const
83
// Inventor stores the transposed matrix
86
for (int i=0; i<4; i++) {
87
for (int j=0; j<4; j++)
88
mat[i][j] = matrix[j][i];
94
// ----------------------------------------------------------------------------
96
void Tessellator::tessCB(void * v0, void * v1, void * v2, void * cbdata)
98
int * vtx0 = (int *)v0;
99
int * vtx1 = (int *)v1;
100
int * vtx2 = (int *)v2;
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);
109
Tessellator::Tessellator(const std::vector<SbVec2f>& poly) : polygon(poly)
113
std::vector<int> Tessellator::tessellate() const
115
std::vector<int> indices(polygon.size());
116
std::vector<int> face_indices;
118
SbTesselator tessellator(tessCB, &face_indices);
119
tessellator.beginPolygon();
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]));
127
// run the triangulation now
128
tessellator.endPolygon();
132
// ----------------------------------------------------------------------------
134
class ItemViewSelection::MatchName {
136
explicit MatchName(const QString& n) : name(n)
138
bool operator() (const App::DocumentObject* obj) {
139
return name == QLatin1String(obj->getNameInDocument());
145
ItemViewSelection::ItemViewSelection(QAbstractItemView* view)
150
void ItemViewSelection::applyFrom(const std::vector<App::DocumentObject*> objs)
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);
165
view->selectionModel()->select(range, QItemSelectionModel::Select);