ViewProviderExt.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2011 Juergen Riegel <juergen.riegel@web.de>             *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #include "PreCompiled.h"
00025 
00026 #ifndef _PreComp_
00027 # include <sstream>
00028 # include <Poly_Polygon3D.hxx>
00029 # include <BRepBndLib.hxx>
00030 # include <BRepMesh.hxx>
00031 # include <BRepMesh_IncrementalMesh.hxx>
00032 # include <BRep_Tool.hxx>
00033 # include <BRepTools.hxx>
00034 # include <BRepAdaptor_Curve.hxx>
00035 # include <BRepAdaptor_Surface.hxx>
00036 # include <GeomAbs_CurveType.hxx>
00037 # include <GeomAbs_SurfaceType.hxx>
00038 # include <Geom_BezierCurve.hxx>
00039 # include <Geom_BSplineCurve.hxx>
00040 # include <Geom_BezierSurface.hxx>
00041 # include <Geom_BSplineSurface.hxx>
00042 # include <GeomAPI_ProjectPointOnSurf.hxx>
00043 # include <GeomLProp_SLProps.hxx>
00044 # include <gp_Trsf.hxx>
00045 # include <Handle_Poly_Triangulation.hxx>
00046 # include <Poly_Array1OfTriangle.hxx>
00047 # include <Poly_Triangulation.hxx>
00048 # include <TColgp_Array1OfPnt.hxx>
00049 # include <TopoDS.hxx>
00050 # include <TopoDS_Edge.hxx>
00051 # include <TopoDS_Wire.hxx>
00052 # include <TopoDS_Face.hxx>
00053 # include <TopoDS_Shape.hxx>
00054 # include <TopoDS_Iterator.hxx>
00055 # include <TopExp_Explorer.hxx>
00056 # include <TopExp.hxx>
00057 # include <TopTools_IndexedMapOfShape.hxx>
00058 # include <Poly_PolygonOnTriangulation.hxx>
00059 # include <TColStd_Array1OfInteger.hxx>
00060 # include <TopTools_ListOfShape.hxx>
00061 # include <Inventor/SoPickedPoint.h>
00062 # include <Inventor/details/SoFaceDetail.h>
00063 # include <Inventor/details/SoLineDetail.h>
00064 # include <Inventor/details/SoPointDetail.h>
00065 # include <Inventor/events/SoMouseButtonEvent.h>
00066 # include <Inventor/nodes/SoBaseColor.h>
00067 # include <Inventor/nodes/SoCoordinate3.h>
00068 # include <Inventor/nodes/SoDrawStyle.h>
00069 # include <Inventor/nodes/SoIndexedFaceSet.h>
00070 # include <Inventor/nodes/SoIndexedLineSet.h>
00071 # include <Inventor/nodes/SoLocateHighlight.h>
00072 # include <Inventor/nodes/SoMaterial.h>
00073 # include <Inventor/nodes/SoMaterialBinding.h>
00074 # include <Inventor/nodes/SoNormal.h>
00075 # include <Inventor/nodes/SoNormalBinding.h>
00076 # include <Inventor/nodes/SoPointSet.h>
00077 # include <Inventor/nodes/SoPolygonOffset.h>
00078 # include <Inventor/nodes/SoShapeHints.h>
00079 # include <Inventor/nodes/SoSwitch.h>
00080 # include <Inventor/nodes/SoGroup.h>
00081 # include <Inventor/nodes/SoSphere.h>
00082 # include <Inventor/nodes/SoScale.h>
00083 # include <Inventor/nodes/SoLightModel.h>
00084 # include <QAction>
00085 # include <QMenu>
00086 #endif
00087 
00089 #include <Base/Console.h>
00090 #include <Base/Parameter.h>
00091 #include <Base/Exception.h>
00092 #include <Base/TimeInfo.h>
00093 
00094 #include <App/Application.h>
00095 #include <App/Document.h>
00096 
00097 #include <Gui/SoFCUnifiedSelection.h>
00098 #include <Gui/Selection.h>
00099 #include <Gui/View3DInventorViewer.h>
00100 #include <Gui/Utilities.h>
00101 #include <Gui/Control.h>
00102 
00103 #include "ViewProviderExt.h"
00104 #include "SoBrepShape.h"
00105 #include "TaskFaceColors.h"
00106 
00107 #include <Mod/Part/App/PartFeature.h>
00108 #include <Mod/Part/App/PrimitiveFeature.h>
00109 
00110 
00111 using namespace PartGui;
00112 
00113 PROPERTY_SOURCE(PartGui::ViewProviderPartExt, Gui::ViewProviderGeometryObject)
00114 
00115 
00116 //**************************************************************************
00117 // Construction/Destruction
00118 
00119 App::PropertyFloatConstraint::Constraints ViewProviderPartExt::sizeRange = {1.0f,64.0f,1.0f};
00120 App::PropertyFloatConstraint::Constraints ViewProviderPartExt::tessRange = {0.0001f,100.0f,0.01f};
00121 const char* ViewProviderPartExt::LightingEnums[]= {"One side","Two side",NULL};
00122 
00123 ViewProviderPartExt::ViewProviderPartExt() 
00124 {
00125     VisualTouched = true;
00126 
00127     App::Material mat;
00128     mat.ambientColor.set(0.2f,0.2f,0.2f);
00129     mat.diffuseColor.set(0.1f,0.1f,0.1f);
00130     mat.specularColor.set(0.0f,0.0f,0.0f);
00131     mat.emissiveColor.set(0.0f,0.0f,0.0f);
00132     mat.shininess = 1.0f;
00133     mat.transparency = 0.0f;
00134     ADD_PROPERTY(LineMaterial,(mat));
00135     ADD_PROPERTY(PointMaterial,(mat));
00136     ADD_PROPERTY(LineColor,(mat.diffuseColor));
00137     ADD_PROPERTY(PointColor,(mat.diffuseColor));
00138     ADD_PROPERTY(DiffuseColor,(ShapeColor.getValue()));
00139     ADD_PROPERTY(LineWidth,(2.0f));
00140     LineWidth.setConstraints(&sizeRange);
00141     PointSize.setConstraints(&sizeRange);
00142     ADD_PROPERTY(PointSize,(2.0f));
00143     ADD_PROPERTY(Deviation,(0.5f));
00144     Deviation.setConstraints(&tessRange);
00145     ADD_PROPERTY(ControlPoints,(false));
00146     ADD_PROPERTY(Lighting,(1));
00147     Lighting.setEnums(LightingEnums);
00148 
00149     coords = new SoCoordinate3();
00150     coords->ref();
00151     faceset = new SoBrepFaceSet();
00152     faceset->ref();
00153     norm = new SoNormal;
00154     norm->ref();
00155     normb = new SoNormalBinding;
00156     normb->value = SoNormalBinding::PER_VERTEX_INDEXED;
00157     normb->ref();
00158     lineset = new SoBrepEdgeSet();
00159     lineset->ref();
00160     nodeset = new SoBrepPointSet();
00161     nodeset->ref();
00162 
00163     pcShapeBind = new SoMaterialBinding();
00164     pcShapeBind->ref();
00165 
00166     pcLineMaterial = new SoMaterial;
00167     pcLineMaterial->ref();
00168     LineMaterial.touch();
00169 
00170     pcPointMaterial = new SoMaterial;
00171     pcPointMaterial->ref();
00172     PointMaterial.touch();
00173 
00174     pcLineStyle = new SoDrawStyle();
00175     pcLineStyle->ref();
00176     pcLineStyle->style = SoDrawStyle::LINES;
00177     pcLineStyle->lineWidth = LineWidth.getValue();
00178 
00179     pcPointStyle = new SoDrawStyle();
00180     pcPointStyle->ref();
00181     pcPointStyle->style = SoDrawStyle::POINTS;
00182     pcPointStyle->pointSize = PointSize.getValue();
00183 
00184     pShapeHints = new SoShapeHints;
00185     pShapeHints->shapeType = SoShapeHints::UNKNOWN_SHAPE_TYPE;
00186     pShapeHints->ref();
00187     Lighting.touch();
00188 
00189     sPixmap = "Tree_Part";
00190     loadParameter();
00191 }
00192 
00193 ViewProviderPartExt::~ViewProviderPartExt()
00194 {
00195     pcShapeBind->unref();
00196     pcLineMaterial->unref();
00197     pcPointMaterial->unref();
00198     pcLineStyle->unref();
00199     pcPointStyle->unref();
00200     pShapeHints->unref();
00201     coords->unref();
00202     faceset->unref();
00203     norm->unref();
00204     lineset->unref();
00205     nodeset->unref();
00206 }
00207 
00208 void ViewProviderPartExt::onChanged(const App::Property* prop)
00209 {
00210     if (prop == &Deviation) {
00211         VisualTouched = true;
00212     }
00213     if (prop == &LineWidth) {
00214         pcLineStyle->lineWidth = LineWidth.getValue();
00215     }
00216     else if (prop == &PointSize) {
00217         pcPointStyle->pointSize = PointSize.getValue();
00218     }
00219     else if (prop == &LineColor) {
00220         const App::Color& c = LineColor.getValue();
00221         pcLineMaterial->diffuseColor.setValue(c.r,c.g,c.b);
00222         if (c != LineMaterial.getValue().diffuseColor)
00223         LineMaterial.setDiffuseColor(c);
00224     }
00225     else if (prop == &PointColor) {
00226         const App::Color& c = PointColor.getValue();
00227         pcPointMaterial->diffuseColor.setValue(c.r,c.g,c.b);
00228         if (c != PointMaterial.getValue().diffuseColor)
00229         PointMaterial.setDiffuseColor(c);
00230     }
00231     else if (prop == &LineMaterial) {
00232         const App::Material& Mat = LineMaterial.getValue();
00233         if (LineColor.getValue() != Mat.diffuseColor)
00234         LineColor.setValue(Mat.diffuseColor);
00235         pcLineMaterial->ambientColor.setValue(Mat.ambientColor.r,Mat.ambientColor.g,Mat.ambientColor.b);
00236         pcLineMaterial->diffuseColor.setValue(Mat.diffuseColor.r,Mat.diffuseColor.g,Mat.diffuseColor.b);
00237         pcLineMaterial->specularColor.setValue(Mat.specularColor.r,Mat.specularColor.g,Mat.specularColor.b);
00238         pcLineMaterial->emissiveColor.setValue(Mat.emissiveColor.r,Mat.emissiveColor.g,Mat.emissiveColor.b);
00239         pcLineMaterial->shininess.setValue(Mat.shininess);
00240         pcLineMaterial->transparency.setValue(Mat.transparency);
00241     }
00242     else if (prop == &PointMaterial) {
00243         const App::Material& Mat = PointMaterial.getValue();
00244         if (PointColor.getValue() != Mat.diffuseColor)
00245         PointColor.setValue(Mat.diffuseColor);
00246         pcPointMaterial->ambientColor.setValue(Mat.ambientColor.r,Mat.ambientColor.g,Mat.ambientColor.b);
00247         pcPointMaterial->diffuseColor.setValue(Mat.diffuseColor.r,Mat.diffuseColor.g,Mat.diffuseColor.b);
00248         pcPointMaterial->specularColor.setValue(Mat.specularColor.r,Mat.specularColor.g,Mat.specularColor.b);
00249         pcPointMaterial->emissiveColor.setValue(Mat.emissiveColor.r,Mat.emissiveColor.g,Mat.emissiveColor.b);
00250         pcPointMaterial->shininess.setValue(Mat.shininess);
00251         pcPointMaterial->transparency.setValue(Mat.transparency);
00252     }
00253     // For testing
00254     else if (prop == &DiffuseColor) {
00255         const std::vector<App::Color>& c = DiffuseColor.getValues();
00256         int size = (int)c.size();
00257         if (size > 1 && size == this->faceset->partIndex.getNum()) {
00258             pcShapeBind->value = SoMaterialBinding::PER_PART;
00259             pcShapeMaterial->diffuseColor.setNum(c.size());
00260             SbColor* ca = pcShapeMaterial->diffuseColor.startEditing();
00261             for (unsigned int i=0; i < c.size(); i++)
00262                 ca[i].setValue(c[i].r,c[i].g,c[i].b);
00263             pcShapeMaterial->diffuseColor.finishEditing();
00264         }
00265         else if ((int)c.size() == 1) {
00266             pcShapeBind->value = SoMaterialBinding::OVERALL;
00267             pcShapeMaterial->diffuseColor.setValue(c[0].r,c[0].g,c[0].b);
00268         }
00269     }
00270     else if (prop == &ShapeMaterial || prop == &ShapeColor) {
00271         pcShapeBind->value = SoMaterialBinding::OVERALL;
00272         ViewProviderGeometryObject::onChanged(prop);
00273         DiffuseColor.setValue(ShapeColor.getValue());
00274     }
00275     else if (prop == &Lighting) {
00276         if (Lighting.getValue() == 0)
00277             pShapeHints->vertexOrdering = SoShapeHints::UNKNOWN_ORDERING;
00278         else
00279             pShapeHints->vertexOrdering = SoShapeHints::COUNTERCLOCKWISE;
00280     }
00281     else {
00282         // if the object was invisible and has been changed, recreate the visual
00283         if (prop == &Visibility && Visibility.getValue() && VisualTouched) 
00284             updateVisual(dynamic_cast<Part::Feature*>(pcObject)->Shape.getValue());
00285 
00286         ViewProviderGeometryObject::onChanged(prop);
00287     }
00288 }
00289 
00290 void ViewProviderPartExt::attach(App::DocumentObject *pcFeat)
00291 {
00292     // call parent attach method
00293     ViewProviderGeometryObject::attach(pcFeat);
00294 
00295     // Workaround for #0000433, i.e. use SoSeparator instead of SoGroup
00296     SoGroup* pcNormalRoot = new SoSeparator();
00297     SoGroup* pcFlatRoot = new SoSeparator();
00298     SoGroup* pcWireframeRoot = new SoSeparator();
00299     SoGroup* pcPointsRoot = new SoSeparator();
00300 
00301     // enable two-side rendering
00302     pShapeHints->vertexOrdering = SoShapeHints::COUNTERCLOCKWISE;
00303     pShapeHints->shapeType = SoShapeHints::UNKNOWN_SHAPE_TYPE;
00304 
00305     // Avoid any Z-buffer artefacts, so that the lines always appear on top of the faces
00306     // The correct order is Edges, Polygon offset, Faces.
00307     SoPolygonOffset* offset = new SoPolygonOffset();
00308 
00309     // normal viewing with edges and points
00310     pcNormalRoot->addChild(pcWireframeRoot);
00311     pcNormalRoot->addChild(offset);
00312     pcNormalRoot->addChild(pcFlatRoot);
00313     pcNormalRoot->addChild(pcPointsRoot);
00314 
00315     // just faces with no edges or points
00316     pcFlatRoot->addChild(pShapeHints);
00317     pcFlatRoot->addChild(pcShapeBind);
00318     pcFlatRoot->addChild(pcShapeMaterial);
00319     SoDrawStyle* pcFaceStyle = new SoDrawStyle();
00320     pcFaceStyle->style = SoDrawStyle::FILLED;
00321     pcFlatRoot->addChild(pcFaceStyle);
00322     pcFlatRoot->addChild(norm);
00323     pcFlatRoot->addChild(normb);
00324     pcFlatRoot->addChild(faceset);
00325 
00326     // only edges
00327     pcWireframeRoot->addChild(pcLineMaterial);
00328     pcWireframeRoot->addChild(pcLineStyle);
00329     pcWireframeRoot->addChild(lineset);
00330 
00331     // normal viewing with edges and points
00332     pcPointsRoot->addChild(pcPointMaterial);
00333     pcPointsRoot->addChild(pcPointStyle);
00334     pcPointsRoot->addChild(nodeset);
00335 
00336     // Move 'coords' before the switch
00337     pcRoot->insertChild(coords,pcRoot->findChild(pcModeSwitch));
00338 
00339     // putting all together with the switch
00340     addDisplayMaskMode(pcNormalRoot, "Flat Lines");
00341     addDisplayMaskMode(pcFlatRoot, "Shaded");
00342     addDisplayMaskMode(pcWireframeRoot, "Wireframe");
00343     addDisplayMaskMode(pcPointsRoot, "Point");
00344 }
00345 
00346 void ViewProviderPartExt::setDisplayMode(const char* ModeName)
00347 {
00348     if ( strcmp("Flat Lines",ModeName)==0 )
00349         setDisplayMaskMode("Flat Lines");
00350     else if ( strcmp("Shaded",ModeName)==0 )
00351         setDisplayMaskMode("Shaded");
00352     else if ( strcmp("Wireframe",ModeName)==0 )
00353         setDisplayMaskMode("Wireframe");
00354     else if ( strcmp("Points",ModeName)==0 )
00355         setDisplayMaskMode("Point");
00356 
00357     ViewProviderGeometryObject::setDisplayMode( ModeName );
00358 }
00359 
00360 std::vector<std::string> ViewProviderPartExt::getDisplayModes(void) const
00361 {
00362     // get the modes of the father
00363     std::vector<std::string> StrList = ViewProviderGeometryObject::getDisplayModes();
00364 
00365     // add your own modes
00366     StrList.push_back("Flat Lines");
00367     StrList.push_back("Shaded");
00368     StrList.push_back("Wireframe");
00369     StrList.push_back("Points");
00370 
00371     return StrList;
00372 }
00373 
00374 std::string ViewProviderPartExt::getElement(const SoPickedPoint* pp) const
00375 {
00376     std::stringstream str;
00377     const SoDetail* detail = pp->getDetail();
00378     if (detail) {
00379         if (detail->getTypeId() == SoFaceDetail::getClassTypeId()) {
00380             const SoFaceDetail* face_detail = static_cast<const SoFaceDetail*>(detail);
00381             int face = face_detail->getPartIndex() + 1;
00382             str << "Face" << face;
00383         }
00384         else if (detail->getTypeId() == SoLineDetail::getClassTypeId()) {
00385             const SoLineDetail* line_detail = static_cast<const SoLineDetail*>(detail);
00386             int edge = line_detail->getPartIndex() + 1;
00387             str << "Edge" << edge;
00388         }
00389         else if (detail->getTypeId() == SoPointDetail::getClassTypeId()) {
00390             const SoPointDetail* point_detail = static_cast<const SoPointDetail*>(detail);
00391             int vertex = point_detail->getCoordinateIndex() - nodeset->startIndex.getValue() + 1;
00392             str << "Vertex" << vertex;
00393         }
00394     }
00395 
00396     return str.str();
00397 }
00398 
00399 std::vector<Base::Vector3d> ViewProviderPartExt::getSelectionShape(const char* Element) const
00400 {
00401     return std::vector<Base::Vector3d>();
00402 }
00403 
00404 bool ViewProviderPartExt::loadParameter()
00405 {
00406     bool changed = false;
00407     ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath
00408         ("User parameter:BaseApp/Preferences/Mod/Part");
00409     float deviation = hGrp->GetFloat("MeshDeviation",0.2);
00410     bool novertexnormals = hGrp->GetBool("NoPerVertexNormals",false);
00411     bool qualitynormals = hGrp->GetBool("QualityNormals",false);
00412 
00413     if (Deviation.getValue() != deviation) {
00414         Deviation.setValue(deviation);
00415         changed = true;
00416     }
00417     if (this->noPerVertexNormals != novertexnormals) {
00418         this->noPerVertexNormals = novertexnormals;
00419         changed = true;
00420     }
00421     if (this->qualityNormals != qualitynormals) {
00422         this->qualityNormals = qualitynormals;
00423         changed = true;
00424     }
00425 
00426     return changed;
00427 }
00428 
00429 void ViewProviderPartExt::reload()
00430 {
00431     if (loadParameter()) {
00432         App::Property* shape     = pcObject->getPropertyByName("Shape");
00433         if (shape) update(shape);
00434     }
00435 }
00436 
00437 void ViewProviderPartExt::updateData(const App::Property* prop)
00438 {
00439     if (prop->getTypeId() == Part::PropertyPartShape::getClassTypeId()) {
00440         // get the shape to show
00441         const TopoDS_Shape &cShape = static_cast<const Part::PropertyPartShape*>(prop)->getValue();
00442 
00443         // calculate the visual only if visible
00444         if (Visibility.getValue())
00445             updateVisual(cShape);
00446         else
00447             VisualTouched = true;
00448 
00449         if (!VisualTouched) {
00450             if (this->faceset->partIndex.getNum() > 
00451                 this->pcShapeMaterial->diffuseColor.getNum()) {
00452                 this->pcShapeBind->value = SoMaterialBinding::OVERALL;
00453             }
00454         }
00455     }
00456     Gui::ViewProviderGeometryObject::updateData(prop);
00457 }
00458 
00459 void ViewProviderPartExt::setupContextMenu(QMenu* menu, QObject* receiver, const char* member)
00460 {
00461     Gui::ViewProviderGeometryObject::setupContextMenu(menu, receiver, member);
00462     QAction* act = menu->addAction(QObject::tr("Set colors..."), receiver, member);
00463     act->setData(QVariant((int)ViewProvider::Color));
00464 }
00465 
00466 bool ViewProviderPartExt::setEdit(int ModNum)
00467 {
00468     if (ModNum == ViewProvider::Color) {
00469         // When double-clicking on the item for this pad the
00470         // object unsets and sets its edit mode without closing
00471         // the task panel
00472         Gui::TaskView::TaskDialog *dlg = Gui::Control().activeDialog();
00473         if (dlg) {
00474             Gui::Control().showDialog(dlg);
00475             return false;
00476         }
00477 
00478         Gui::Selection().clearSelection();
00479         Gui::Control().showDialog(new TaskFaceColors(this));
00480         return true;
00481     }
00482     else {
00483         return Gui::ViewProviderGeometryObject::setEdit(ModNum);
00484     }
00485 }
00486 
00487 void ViewProviderPartExt::unsetEdit(int ModNum)
00488 {
00489     if (ModNum == ViewProvider::Color) {
00490     }
00491     else {
00492         Gui::ViewProviderGeometryObject::unsetEdit(ModNum);
00493     }
00494 }
00495 
00496 void ViewProviderPartExt::updateVisual(const TopoDS_Shape& inputShape)
00497 {
00498     // Clear selection
00499     Gui::SoSelectionElementAction action(Gui::SoSelectionElementAction::None);
00500     action.apply(this->faceset);
00501     action.apply(this->lineset);
00502     action.apply(this->nodeset);
00503 
00504     TopoDS_Shape cShape(inputShape);
00505     if (cShape.IsNull()) {
00506         coords  ->point      .setNum(0);
00507         norm    ->vector     .setNum(0);
00508         faceset ->coordIndex .setNum(0);
00509         faceset ->partIndex  .setNum(0);
00510         lineset ->coordIndex .setNum(0);
00511         VisualTouched = false;
00512         return;
00513     }
00514 
00515     // time measurement and book keeping
00516     Base::TimeInfo start_time;
00517     int nbrTriangles=0,nbrNodes=0,nbrNorms=0,nbrFaces=0,nbrEdges=0,nbrLines=0;
00518     std::set<int> faceEdges;
00519 
00520     try {
00521         // calculating the deflection value
00522         Bnd_Box bounds;
00523         BRepBndLib::Add(cShape, bounds);
00524         bounds.SetGap(0.0);
00525         Standard_Real xMin, yMin, zMin, xMax, yMax, zMax;
00526         bounds.Get(xMin, yMin, zMin, xMax, yMax, zMax);
00527         Standard_Real deflection = ((xMax-xMin)+(yMax-yMin)+(zMax-zMin))/300.0 *
00528             Deviation.getValue();
00529 
00530         // create or use the mesh on the data structure
00531         BRepMesh_IncrementalMesh myMesh(cShape,deflection);
00532         // We must reset the location here because the transformation data
00533         // are set in the placement property
00534         TopLoc_Location aLoc;
00535         cShape.Location(aLoc);
00536 
00537         // count triangles and nodes in the mesh
00538         TopExp_Explorer Ex;
00539         for (Ex.Init(cShape,TopAbs_FACE);Ex.More();Ex.Next()) {
00540             Handle (Poly_Triangulation) mesh = BRep_Tool::Triangulation(TopoDS::Face(Ex.Current()), aLoc);
00541             // Note: we must also count empty faces
00542             if (!mesh.IsNull()) {
00543                 nbrTriangles += mesh->NbTriangles();
00544                 nbrNodes     += mesh->NbNodes();
00545                 nbrNorms     += mesh->NbNodes();
00546             }
00547 
00548             TopExp_Explorer xp;
00549             for (xp.Init(Ex.Current(),TopAbs_EDGE);xp.More();xp.Next())
00550                 faceEdges.insert(xp.Current().HashCode(INT_MAX));
00551             nbrFaces++;
00552         }
00553 
00554         // get an indexed map of edges
00555         TopTools_IndexedMapOfShape M;
00556         TopExp::MapShapes(cShape, TopAbs_EDGE, M);
00557 
00558         std::set<int>         edgeIdxSet;
00559         std::vector<int32_t>  indxVector;
00560         std::vector<int32_t>  edgeVector;
00561 
00562         // count and index the edges
00563         for (int i=1; i <= M.Extent(); i++) {
00564             edgeIdxSet.insert(i);
00565             nbrEdges++;
00566 
00567             const TopoDS_Edge& aEdge = TopoDS::Edge(M(i));
00568             TopLoc_Location aLoc;
00569 
00570             // handling of the free edge that are not associated to a face
00571             // Note: The assumption that if for an edge BRep_Tool::Polygon3D
00572             // returns a valid object is wrong. This e.g. happens for ruled
00573             // surfaces which gets created by two edges or wires.
00574             // So, we have to store the hashes of the edges associated to a face.
00575             // If the hash of a given edge is not in this list we know it's really
00576             // a free edge.
00577             int hash = aEdge.HashCode(INT_MAX);
00578             if (faceEdges.find(hash) == faceEdges.end()) {
00579                 Handle(Poly_Polygon3D) aPoly = BRep_Tool::Polygon3D(aEdge, aLoc);
00580                 if (!aPoly.IsNull()) {
00581                     int nbNodesInEdge = aPoly->NbNodes();
00582                     nbrNodes += nbNodesInEdge;
00583                 }
00584             }
00585         }
00586         // reserve some memory
00587         indxVector.reserve(nbrEdges*8);
00588 
00589         // handling of the vertices
00590         TopTools_IndexedMapOfShape V;
00591         TopExp::MapShapes(cShape, TopAbs_VERTEX, V);
00592         nbrNodes += V.Extent();
00593 
00594         // create memory for the nodes and indexes
00595         coords  ->point      .setNum(nbrNodes);
00596         norm    ->vector     .setNum(nbrNorms);
00597         faceset ->coordIndex .setNum(nbrTriangles*4);
00598         faceset ->partIndex  .setNum(nbrFaces);
00599         // get the raw memory for fast fill up
00600         SbVec3f* verts = coords  ->point       .startEditing();
00601         SbVec3f* norms = norm    ->vector      .startEditing();
00602         int32_t* index = faceset ->coordIndex  .startEditing();
00603         int32_t* parts = faceset ->partIndex   .startEditing();
00604 
00605         // preset the normal vector with null vector
00606         for (int i=0;i < nbrNorms;i++) 
00607             norms[i]= SbVec3f(0.0,0.0,0.0);
00608 
00609         int ii = 0,FaceNodeOffset=0,FaceTriaOffset=0;
00610         for (Ex.Init(cShape, TopAbs_FACE); Ex.More(); Ex.Next(),ii++) {
00611             TopLoc_Location aLoc;
00612             const TopoDS_Face &actFace = TopoDS::Face(Ex.Current());
00613             // get the mesh of the shape
00614             Handle (Poly_Triangulation) mesh = BRep_Tool::Triangulation(actFace,aLoc);
00615             if (mesh.IsNull()) continue;
00616 
00617             // getting the transformation of the shape/face
00618             gp_Trsf myTransf;
00619             Standard_Boolean identity = true;
00620             if (!aLoc.IsIdentity()) {
00621                 identity = false;
00622                 myTransf = aLoc.Transformation();
00623             }
00624 
00625             // getting size of node and triangle array of this face
00626             int nbNodesInFace = mesh->NbNodes();
00627             int nbTriInFace   = mesh->NbTriangles();
00628             // check orientation
00629             TopAbs_Orientation orient = actFace.Orientation();
00630 
00631 
00632             // cycling through the poly mesh
00633             const Poly_Array1OfTriangle& Triangles = mesh->Triangles();
00634             const TColgp_Array1OfPnt& Nodes = mesh->Nodes();
00635             for (int g=1;g<=nbTriInFace;g++) {
00636                 // Get the triangle
00637                 Standard_Integer N1,N2,N3;
00638                 Triangles(g).Get(N1,N2,N3);
00639 
00640                 // change orientation of the triangle if the face is reversed
00641                 if ( orient != TopAbs_FORWARD ) {
00642                     Standard_Integer tmp = N1;
00643                     N1 = N2;
00644                     N2 = tmp;
00645                 }
00646 
00647                 // get the 3 points of this triangle
00648                 gp_Pnt V1(Nodes(N1)), V2(Nodes(N2)), V3(Nodes(N3));
00649 
00650                 // transform the vertices to the place of the face
00651                 if (!identity) {
00652                     V1.Transform(myTransf);
00653                     V2.Transform(myTransf);
00654                     V3.Transform(myTransf);
00655                 }
00656                 
00657                 // calculating per vertex normals                    
00658                 // Calculate triangle normal
00659                 gp_Vec v1(V1.X(),V1.Y(),V1.Z()),v2(V2.X(),V2.Y(),V2.Z()),v3(V3.X(),V3.Y(),V3.Z());
00660                 gp_Vec Normal = (v2-v1)^(v3-v1); 
00661 
00662                 // add the triangle normal to the vertex normal for all points of this triangle
00663                 norms[FaceNodeOffset+N1-1] += SbVec3f(Normal.X(),Normal.Y(),Normal.Z());
00664                 norms[FaceNodeOffset+N2-1] += SbVec3f(Normal.X(),Normal.Y(),Normal.Z());
00665                 norms[FaceNodeOffset+N3-1] += SbVec3f(Normal.X(),Normal.Y(),Normal.Z());                 
00666 
00667                 // set the vertices
00668                 verts[FaceNodeOffset+N1-1].setValue((float)(V1.X()),(float)(V1.Y()),(float)(V1.Z()));
00669                 verts[FaceNodeOffset+N2-1].setValue((float)(V2.X()),(float)(V2.Y()),(float)(V2.Z()));
00670                 verts[FaceNodeOffset+N3-1].setValue((float)(V3.X()),(float)(V3.Y()),(float)(V3.Z()));
00671 
00672                 // set the index vector with the 3 point indexes and the end delimiter
00673                 index[FaceTriaOffset*4+4*(g-1)]   = FaceNodeOffset+N1-1; 
00674                 index[FaceTriaOffset*4+4*(g-1)+1] = FaceNodeOffset+N2-1; 
00675                 index[FaceTriaOffset*4+4*(g-1)+2] = FaceNodeOffset+N3-1; 
00676                 index[FaceTriaOffset*4+4*(g-1)+3] = SO_END_FACE_INDEX;
00677             }
00678 
00679             parts[ii] = nbTriInFace; // new part
00680 
00681             // handling the edges lying on this face
00682             TopExp_Explorer Exp;
00683             for(Exp.Init(actFace,TopAbs_EDGE);Exp.More();Exp.Next()) {
00684                 const TopoDS_Edge &actEdge = TopoDS::Edge(Exp.Current());
00685                 // get the overall index of this edge
00686                 int idx = M.FindIndex(actEdge);
00687                 edgeVector.push_back((int32_t)idx-1);
00688                 // already processed this index ?
00689                 if (edgeIdxSet.find(idx)!=edgeIdxSet.end()) {
00690                     
00691                     // this holds the indices of the edge's triangulation to the current polygon
00692                     Handle(Poly_PolygonOnTriangulation) aPoly = BRep_Tool::PolygonOnTriangulation(actEdge, mesh, aLoc);
00693                     if (aPoly.IsNull())
00694                         continue; // polygon does not exist
00695                     
00696                     // getting the indexes of the edge polygon
00697                     const TColStd_Array1OfInteger& indices = aPoly->Nodes();
00698                     for (Standard_Integer i=indices.Lower();i <= indices.Upper();i++) {
00699                         int inx = indices(i);
00700                         indxVector.push_back(FaceNodeOffset+inx-1);
00701 
00702                         // usually the coordinates for this edge are already set by the
00703                         // triangles of the face this edge belongs to. However, there are
00704                         // rare cases where some points are only referenced by the polygon
00705                         // but not by any triangle. Thus, we must apply the coordinates to
00706                         // make sure that everything is properly set.
00707                         gp_Pnt p(Nodes(inx));
00708                         if (!identity)
00709                             p.Transform(myTransf);
00710                         verts[FaceNodeOffset+inx-1].setValue((float)(p.X()),(float)(p.Y()),(float)(p.Z()));
00711                     }
00712                     indxVector.push_back(-1);
00713 
00714                     // remove the handled edge index from the set
00715                     edgeIdxSet.erase(idx);
00716                 }
00717             }
00718 
00719             edgeVector.push_back(-1);
00720             
00721             // counting up the per Face offsets
00722             FaceNodeOffset += nbNodesInFace;
00723             FaceTriaOffset += nbTriInFace;
00724         }
00725 
00726         // handling of the free edges
00727         for (int i=1; i <= M.Extent(); i++) {
00728             const TopoDS_Edge& aEdge = TopoDS::Edge(M(i));
00729             Standard_Boolean identity = true;
00730             gp_Trsf myTransf;
00731             TopLoc_Location aLoc;
00732 
00733             // handling of the free edge that are not associated to a face
00734             int hash = aEdge.HashCode(INT_MAX);
00735             if (faceEdges.find(hash) == faceEdges.end()) {
00736                 Handle(Poly_Polygon3D) aPoly = BRep_Tool::Polygon3D(aEdge, aLoc);
00737                 if (!aPoly.IsNull()) {
00738                     if (!aLoc.IsIdentity()) {
00739                         identity = false;
00740                         myTransf = aLoc.Transformation();
00741                     }
00742 
00743                     const TColgp_Array1OfPnt& aNodes = aPoly->Nodes();
00744                     int nbNodesInEdge = aPoly->NbNodes();
00745 
00746                     gp_Pnt pnt;
00747                     for (Standard_Integer j=1;j <= nbNodesInEdge;j++) {
00748                         pnt = aNodes(j);
00749                         if (!identity)
00750                             pnt.Transform(myTransf);
00751                         verts[FaceNodeOffset+j-1].setValue((float)(pnt.X()),(float)(pnt.Y()),(float)(pnt.Z()));
00752                         indxVector.push_back(FaceNodeOffset+j-1);
00753                     }
00754 
00755                     indxVector.push_back(-1);
00756                     FaceNodeOffset += nbNodesInEdge;
00757                 }
00758             }
00759         }
00760 
00761         nodeset->startIndex.setValue(FaceNodeOffset);
00762         for (int i=0; i<V.Extent(); i++) {
00763             const TopoDS_Vertex& aVertex = TopoDS::Vertex(V(i+1));
00764             gp_Pnt pnt = BRep_Tool::Pnt(aVertex);
00765             verts[FaceNodeOffset+i].setValue((float)(pnt.X()),(float)(pnt.Y()),(float)(pnt.Z()));
00766         }
00767 
00768         // normalize all normals 
00769         for (int i = 0; i< nbrNorms ;i++)
00770             norms[i].normalize();
00771         
00772         // preset the index vector size
00773         nbrLines =  indxVector.size();
00774         lineset ->coordIndex .setNum(nbrLines);
00775         int32_t* lines = lineset ->coordIndex  .startEditing();
00776 
00777         int l=0;
00778         for (std::vector<int32_t>::const_iterator it=indxVector.begin();it!=indxVector.end();++it,l++)
00779             lines[l] = *it;
00780 
00781         // end the editing of the nodes
00782         coords  ->point       .finishEditing();
00783         norm    ->vector      .finishEditing();
00784         faceset ->coordIndex  .finishEditing();
00785         faceset ->partIndex   .finishEditing();
00786         lineset ->coordIndex  .finishEditing();
00787     }
00788     catch (...) {
00789         Base::Console().Error("Cannot compute Inventor representation for the shape of %s.\n",pcObject->getNameInDocument());
00790     }
00791 
00792     // printing some informations
00793     Base::Console().Log("ViewProvider update time: %f s\n",Base::TimeInfo::diffTimeF(start_time,Base::TimeInfo()));
00794     Base::Console().Log("Shape tria info: Faces:%d Edges:%d Nodes:%d Triangles:%d IdxVec:%d\n",nbrFaces,nbrEdges,nbrNodes,nbrTriangles,nbrLines);
00795 
00796     VisualTouched = false;
00797 }

Generated on Wed Nov 23 19:00:59 2011 for FreeCAD by  doxygen 1.6.1