Utilities.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "PreCompiled.h"
00025 #ifndef _PreComp_
00026 # include <algorithm>
00027 # include <Inventor/SbMatrix.h>
00028 # include <Inventor/SbTesselator.h>
00029 # include <QAbstractItemModel>
00030 # include <QAbstractItemView>
00031 # include <QItemSelection>
00032 # include <QItemSelectionModel>
00033 #endif
00034 #include "Utilities.h"
00035 #include <App/DocumentObject.h>
00036
00037 using namespace Gui;
00038
00039
00040 ViewVolumeProjection::ViewVolumeProjection (const SbViewVolume &vv)
00041 : viewVolume(vv)
00042 {
00043 }
00044
00045 Base::Vector3f ViewVolumeProjection::operator()(const Base::Vector3f &pt) const
00046 {
00047 SbVec3f pt3d(pt.x,pt.y,pt.z);
00048 viewVolume.projectToScreen(pt3d,pt3d);
00049 return Base::Vector3f(pt3d[0],pt3d[1],pt3d[2]);
00050 }
00051
00052 Base::Vector3d ViewVolumeProjection::operator()(const Base::Vector3d &pt) const
00053 {
00054 Base::Vector3f ptf = Base::convertTo<Base::Vector3f>(pt);
00055 ptf = operator()(ptf);
00056 return Base::convertTo<Base::Vector3d>(ptf);
00057 }
00058
00059 Base::Vector3f ViewVolumeProjection::inverse (const Base::Vector3f &pt) const
00060 {
00061 #if 1
00062 SbVec3f pt3d(2.0f*pt.x-1.0f, 2.0f*pt.y-1.0f, 2.0f*pt.z-1.0f);
00063 viewVolume.getMatrix().inverse().multVecMatrix(pt3d, pt3d);
00064 #elif 1
00065 SbLine line; SbVec3f pt3d;
00066 SbPlane distPlane = viewVolume.getPlane(viewVolume.getNearDist());
00067 viewVolume.projectPointToLine(SbVec2f(pt.x,pt.x), line);
00068 distPlane.intersect(line, pt3d);
00069 #else
00070 SbVec3f pt3d = viewVolume.getPlanePoint(viewVolume.getNearDist(), SbVec2f(pt.x,pt.y));
00071 #endif
00072 return Base::Vector3f(pt3d[0],pt3d[1],pt3d[2]);
00073 }
00074
00075 Base::Vector3d ViewVolumeProjection::inverse (const Base::Vector3d &pt) const
00076 {
00077 Base::Vector3f ptf = Base::convertTo<Base::Vector3f>(pt);
00078 ptf = inverse(ptf);
00079 return Base::convertTo<Base::Vector3d>(ptf);
00080 }
00081
00082 Base::Matrix4D ViewVolumeProjection::getProjectionMatrix () const
00083 {
00084
00085 Base::Matrix4D mat;
00086 SbMatrix affine, proj;
00087 viewVolume.getMatrices(affine, proj);
00088 for (int i=0; i<4; i++) {
00089 for (int j=0; j<4; j++)
00090 mat[i][j] = proj[j][i];
00091 }
00092
00093 return mat;
00094 }
00095
00096
00097
00098 void Tessellator::tessCB(void * v0, void * v1, void * v2, void * cbdata)
00099 {
00100 int * vtx0 = (int *)v0;
00101 int * vtx1 = (int *)v1;
00102 int * vtx2 = (int *)v2;
00103
00104 std::vector<int>* array = (std::vector<int> *)cbdata;
00105 array->push_back(*vtx0);
00106 array->push_back(*vtx1);
00107 array->push_back(*vtx2);
00108 array->push_back(-1);
00109 }
00110
00111 Tessellator::Tessellator(const std::vector<SbVec2f>& poly) : polygon(poly)
00112 {
00113 }
00114
00115 std::vector<int> Tessellator::tessellate() const
00116 {
00117 std::vector<int> indices(polygon.size());
00118 std::vector<int> face_indices;
00119
00120 SbTesselator tessellator(tessCB, &face_indices);
00121 tessellator.beginPolygon();
00122
00123 int index = 0;
00124 for (std::vector<SbVec2f>::const_iterator it = polygon.begin(); it != polygon.end(); ++it, index++) {
00125 indices[index] = index;
00126 tessellator.addVertex(SbVec3f((*it)[0], (*it)[1], 0.0f), &(indices[index]));
00127 }
00128
00129
00130 tessellator.endPolygon();
00131 return face_indices;
00132 }
00133
00134
00135
00136 class ItemViewSelection::MatchName {
00137 public:
00138 MatchName(const QString& n) : name(n)
00139 {}
00140 bool operator() (const App::DocumentObject* obj) {
00141 return name == QLatin1String(obj->getNameInDocument());
00142 }
00143 private:
00144 QString name;
00145 };
00146
00147 ItemViewSelection::ItemViewSelection(QAbstractItemView* view)
00148 : view(view)
00149 {
00150 }
00151
00152 void ItemViewSelection::applyFrom(const std::vector<App::DocumentObject*> objs)
00153 {
00154 QAbstractItemModel* model = view->model();
00155 QItemSelection range;
00156 for (int i=0; i<model->rowCount(); i++) {
00157 QModelIndex item = model->index(i,0);
00158 if (item.isValid()) {
00159 QVariant name = model->data(item, Qt::UserRole);
00160 std::vector<App::DocumentObject*>::const_iterator it;
00161 it = std::find_if(objs.begin(), objs.end(), MatchName(name.toString()));
00162 if (it != objs.end())
00163 range.select(item, item);
00164 }
00165 }
00166
00167 view->selectionModel()->select(range, QItemSelectionModel::Select);
00168 }