deviation.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002 *   Copyright (c) 2007                                                    *
00003 *   Joachim Zettler <Joachim.Zettler@gmx.de>                              *
00004 *   Human Rezai <human@mytum.de>                                          *
00005 *                                                                         *
00006 *   This file is part of the FreeCAD CAx development system.              *
00007 *                                                                         *
00008 *   This library is free software; you can redistribute it and/or         *
00009 *   modify it under the terms of the GNU Library General Public           *
00010 *   License as published by the Free Software Foundation; either          *
00011 *   version 2 of the License, or (at your option) any later version.      *
00012 *                                                                         *
00013 *   This library  is distributed in the hope that it will be useful,      *
00014 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00015 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00016 *   GNU Library General Public License for more details.                  *
00017 *                                                                         *
00018 *   You should have received a copy of the GNU Library General Public     *
00019 *   License along with this library; see the file COPYING.LIB. If not,    *
00020 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00021 *   Suite 330, Boston, MA  02111-1307, USA                                *
00022 *                                                                         *
00023 ***************************************************************************/
00024 
00025 #include "PreCompiled.h"
00026 #include "deviation.h"
00027 
00028 #include <Mod/Mesh/App/Core/MeshKernel.h>
00029 #include <Mod/Mesh/App/Core/Builder.h>
00030 #include <Mod/Mesh/App/Core/Grid.h>
00031 
00032 #include <BRepAdaptor_Surface.hxx>
00033 #include <Base/Builder3D.h>
00034 #include <BRep_Builder.hxx>
00035 #include <BRep_Tool.hxx>
00036 #include <GeomAPI_ProjectPointOnSurf.hxx>
00037 #include <Geom_Surface.hxx>
00038 #include <Poly_PolygonOnTriangulation.hxx>
00039 #include <Poly_Triangulation.hxx>
00040 #include <TColgp_Array1OfPnt2d.hxx>
00041 #include <TopExp_Explorer.hxx>
00042 #include <TopoDS.hxx>
00043 
00044 
00045 Deviation::Deviation() 
00046 {}
00047 
00048 Deviation::~Deviation()
00049 {}
00050 
00051 void Deviation::ImportGeometry(const TopoDS_Shape& aShape, const MeshCore::MeshKernel& aMesh)
00052 {
00053         m_Mesh = aMesh;
00054         m_Cad  = aShape;
00055 }
00056 
00057 bool Deviation::GenNormals()
00058 {
00059         Base::Builder3D log;
00060         TopExp_Explorer aExpFace;
00061 
00062         MeshPnt aMeshStruct;
00063         std::pair<Base::Vector3f, MeshPnt> inp;
00064         std::map<Base::Vector3f,MeshPnt,MeshPntLess >::iterator meshIt;
00065         
00066         MeshCore::MeshKernel FaceMesh;
00067         MeshCore::MeshPointArray MeshPnts;
00068         
00069         int n;
00070 
00071         MeshPnts = m_MeshCad.GetPoints();
00072         m_pnts.resize(m_MeshCad.CountPoints());
00073         m_nlvec.resize((m_MeshCad.CountPoints()));
00074         
00075         for (unsigned int i=0; i<MeshPnts.size(); ++i)
00076         {
00077                 aMeshStruct.pnt = MeshPnts[i];        // stores point
00078                 aMeshStruct.index = i;                // stores index
00079                 inp.first  = aMeshStruct.pnt;
00080                 inp.second = aMeshStruct;
00081                 MeshMap.insert(inp);
00082         }
00083 
00084         // explores all faces  ------------  Hauptschleife
00085         for (aExpFace.Init(m_Cad,TopAbs_FACE);aExpFace.More();aExpFace.Next())
00086         {
00087                 TopoDS_Face aFace = TopoDS::Face(aExpFace.Current());
00088                 TransferFaceTriangulationtoFreeCAD(aFace, FaceMesh);
00089                 
00090                 MeshPnts.clear();
00091                 MeshPnts = FaceMesh.GetPoints();
00092                 n = MeshPnts.size();
00093 
00094                 TopLoc_Location aLocation;
00095                 
00096                 Handle_Poly_Triangulation aTr = BRep_Tool::Triangulation(aFace,aLocation);
00097                 const TColgp_Array1OfPnt& aNodes = aTr->Nodes();
00098                 
00099                 // create array of node points in absolute coordinate system
00100                 TColgp_Array1OfPnt aPoints(1, aNodes.Length());
00101                 for (Standard_Integer i = 1; i <= aNodes.Length(); i++)
00102                         aPoints(i) = aNodes(i).Transformed(aLocation);
00103 
00104                 const TColgp_Array1OfPnt2d& aUVNodes = aTr->UVNodes();
00105 
00106                 BRepAdaptor_Surface aSurface(aFace);
00107                 Base::Vector3f pnt, normal;
00108                 gp_Pnt2d par;
00109                 gp_Pnt P;
00110                 gp_Vec D1U, D1V;
00111 
00112                 for (int i=1; i<n+1; ++i)
00113                 {
00114                         par = aUVNodes.Value(i);
00115                         aSurface.D1(par.X(),par.Y(),P,D1U,D1V);
00116                         P = aPoints(i);
00117                         pnt.x = (float) P.X();
00118                         pnt.y = (float) P.Y();
00119                         pnt.z = (float) P.Z();
00120 
00121                         meshIt = MeshMap.find(pnt);
00122                         if (meshIt == MeshMap.end())
00123                         {
00124                                 cout << "error";
00125                                 return false;
00126                         }
00127                         
00128                         D1U.Cross(D1V);
00129                         D1U.Normalize();
00130                         
00131                         if (aFace.Orientation() == TopAbs_FORWARD) D1U.Scale(-1.0);
00132 
00133                         
00134                         normal.x = (float) D1U.X();
00135                         normal.y = (float) D1U.Y();
00136                         normal.z = (float) D1U.Z();
00137                         
00138                         m_pnts[((*meshIt).second).index] = pnt;
00139                         m_nlvec[((*meshIt).second).index] = normal;
00140 
00141                         log.addSingleArrow(pnt,pnt+normal);
00142                 }
00143         }
00144 
00145         log.saveToFile("c:/deviation.iv");
00146         return true;
00147 }
00148 
00149 bool Deviation::Compute()
00150 {
00151         Base::Builder3D log;
00152 
00153         best_fit::Tesselate_Shape(m_Cad, m_MeshCad, float(0.1));
00154         GenNormals();
00155 
00156         //return true;
00157 
00158         std::vector<int> FailProj;
00159 
00160         MeshCore::MeshFacetGrid aFacetGrid(m_Mesh,10);
00161         MeshCore::MeshAlgorithm malg(m_Mesh);
00162         MeshCore::MeshAlgorithm malg2(m_Mesh);
00163         MeshCore::MeshPointIterator p_it(m_MeshCad);
00164 
00165         Base::Vector3f projPoint, distVec, nvec(0,0,0), projPoint2;
00166         unsigned long  facetIndex;
00167         std::stringstream text;
00168 
00169         unsigned int c=0;
00170         int i=0;
00171 
00172         for (p_it.Begin(); p_it.More(); p_it.Next())
00173         {
00174                 if (malg.NearestFacetOnRay(*p_it, m_nlvec[i], aFacetGrid, projPoint, facetIndex))   // gridoptimiert
00175                 {
00176                         distVec  = projPoint - *p_it;
00177                         m_nlvec[i] = distVec;   // überschreibt normalenvektor
00178                 }
00179                 else
00180                 {
00181                         if (!malg2.NearestFacetOnRay(*p_it, m_nlvec[i], projPoint, facetIndex))   // nicht gridoptimiert
00182                         {
00183                                 c++;
00184                                 FailProj.push_back(i);
00185                                 m_nlvec[i] = nvec;
00186                         }
00187                         else
00188                         {
00189                                 distVec  = projPoint - *p_it;
00190                                 m_nlvec[i] = distVec;   // überschreibt normalenvektor
00191                         }
00192                 }
00193 
00194                 ++i;
00195         }
00196 
00197         for(int i=0; i<m_nlvec.size(); i++)
00198         {
00199                 log.addSingleArrow(m_pnts[i], m_pnts[i] + m_nlvec[i]);
00200         }
00201 
00202 
00203         log.saveToFile("c:/deviation2.iv");
00204         return true;
00205 }
00206 
00207 #include <QFile>
00208 #include <QTextStream>
00209 void Deviation::WriteOutput(const QString &dateiname)
00210 {
00211         QFile anOutputFile(dateiname);
00212         if (!anOutputFile.open(QIODevice::WriteOnly | QIODevice::Text))
00213                 return;
00214         QTextStream out(&anOutputFile);
00215 
00216         out << m_nlvec.size() << endl;
00217 
00218         for(int i=0; i<m_nlvec.size(); i++)
00219         {
00220                                 out  <<  m_pnts[i].x    << "," 
00221                                  <<  m_pnts[i].y    << "," 
00222                                          <<  m_pnts[i].z    << "," 
00223                                          <<  m_nlvec[i].x   << "," 
00224                                          <<  m_nlvec[i].y   << "," 
00225                                          <<  m_nlvec[i].z   << endl;
00226         }
00227 
00228         anOutputFile.close();
00229 }

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