SpringbackCorrection.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2007                                                    *
00003  *   Joachim Zettler <Joachim.Zettler@gmx.de>                  *
00004  *   Human Rezai <Human@web.de>                                            *
00005  *   This file is part of the FreeCAD CAx development system.              *
00006  *                                                                         *
00007  *   This library is free software; you can redistribute it and/or         *
00008  *   modify it under the terms of the GNU Library General Public           *
00009  *   License as published by the Free Software Foundation; either          *
00010  *   version 2 of the License, or (at your option) any later version.      *
00011  *                                                                         *
00012  *   This library  is distributed in the hope that it will be useful,      *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU Library General Public License for more details.                  *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU Library General Public     *
00018  *   License along with this library; see the file COPYING.LIB. If not,    *
00019  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00020  *   Suite 330, Boston, MA  02111-1307, USA                                *
00021  *                                                                         *
00022  ***************************************************************************/
00023 
00024 #include "PreCompiled.h"
00025 
00026 #include "SpringbackCorrection.h"
00027 #include "best_fit.h"
00028 
00029 
00030 #include <Mod/Mesh/App/Core/Builder.h>
00031 #include <Mod/Mesh/App/WildMagic4/Wm4MeshCurvature.h>
00032 
00033 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
00034 #include <Base/Builder3D.h>
00035 #include <GeomAPI_ProjectPointOnSurf.hxx>
00036 #include <BRepTools.hxx>
00037 #include <BRep_Builder.hxx>
00038 #include <Poly_PolygonOnTriangulation.hxx>
00039 #include <Poly_Triangulation.hxx>
00040 #include <BRepExtrema_DistShapeShape.hxx>
00041 #include <TopExp_Explorer.hxx>
00042 #include <TopExp.hxx>
00043 #include <TopoDS_Vertex.hxx>
00044 #include <BRep_Tool.hxx>
00045 #include <TColgp_Array1OfPnt2d.hxx>
00046 #include <BRepAdaptor_Surface.hxx>
00047 #include <BRepExtrema_DistShapeShape.hxx>
00048 #include <TopoDS.hxx>
00049 #include <TopTools_ListIteratorOfListOfShape.hxx>
00050 #include <Geom_Surface.hxx>
00051 
00052 
00053 
00054 
00056 #include <boost/numeric/bindings/traits/ublas_matrix.hpp>
00057 #include <boost/numeric/bindings/atlas/clapack.hpp>
00058 
00059 using namespace boost::numeric::bindings;
00060 
00061 using namespace boost::numeric;
00062 
00063 
00064 SpringbackCorrection::SpringbackCorrection()
00065 {
00066 }
00067 
00068 SpringbackCorrection::SpringbackCorrection(const TopoDS_Shape& aShape, const MeshCore::MeshKernel& aMesh)
00069         :m_Shape(aShape),m_Mesh(aMesh)
00070 {
00071     m_EdgeStruct.clear();
00072     EdgeMap.clear();
00073     MeshMap.clear();
00074     //Remove any existing Triangulation on the Shape
00075     BRepTools::Clean(m_Shape);
00076     best_fit::Tesselate_Shape(m_Shape,m_CadMesh,(float) 0.1); // Basistriangulierung des ganzen Shapes
00077 
00078     MeshCore::MeshTopoAlgorithm algo(m_CadMesh);
00079     algo.HarmonizeNormals();
00080 
00081         // flip all normals so that they are oriented in positive z-direction 
00082     MeshCore::MeshGeomFacet facet = m_CadMesh.GetFacet(0);
00083     if (facet.GetNormal().z < 0.0)
00084         algo.FlipNormals();
00085 
00086     int n = m_CadMesh.CountFacets();
00087     Base::Vector3f normal;
00088 
00089     MeshCore::MeshPointArray points = m_CadMesh.GetPoints();
00090     MeshCore::MeshFacetArray facets = m_CadMesh.GetFacets();
00091 
00092     for (int i=0; i<n; ++i)
00093     {
00094         MeshCore::MeshGeomFacet face = m_CadMesh.GetFacet(i);
00095         normal = face.GetNormal();
00096 
00097         if (normal.z < 0.0)
00098         {
00099             facets[i].FlipNormal();
00100         }
00101     }
00102 
00103     m_CadMesh.Assign(points, facets);
00104 }
00105 
00106 SpringbackCorrection::~SpringbackCorrection()
00107 {
00108 }
00109 bool SpringbackCorrection::Load(const MeshCore::MeshKernel& aMesh)
00110 {
00111         m_MeshVec.push_back(aMesh);
00112         //m_CadMesh = MeshRef;
00113 
00114         return true;
00115 }
00116 
00117 bool SpringbackCorrection::Load(const TopoDS_Shape& aShape)
00118 {
00119         m_Shape = aShape;
00120         return true;
00121 }
00122 bool SpringbackCorrection::Load(const TopoDS_Shape& aShape, const MeshCore::MeshKernel& aMesh)
00123 {
00124     m_Shape = aShape;
00125     m_Mesh  = aMesh;
00126 
00127     m_EdgeStruct.clear();
00128     EdgeMap.clear();
00129     MeshMap.clear();
00130 
00131     //Remove any existing Triangulation on the Shape
00132     BRepTools::Clean(m_Shape);
00133     best_fit::Tesselate_Shape(m_Shape,m_CadMesh,(float) 0.1); // Basistriangulierung des ganzen Shapes
00134 
00135     MeshCore::MeshTopoAlgorithm algo(m_CadMesh);
00136     algo.HarmonizeNormals();
00137 
00138     MeshCore::MeshGeomFacet facet = m_CadMesh.GetFacet(0);
00139     if (facet.GetNormal().z < 0.0)
00140         algo.FlipNormals();
00141 
00142     int n = m_CadMesh.CountFacets();
00143     Base::Vector3f normal;
00144 
00145     //MeshCore::MeshPointArray points = m_CadMesh.GetPoints();
00146     //MeshCore::MeshFacetArray facets = m_CadMesh.GetFacets();
00147 
00148     //for (int i=0; i<n; ++i)
00149     //{
00150     //    MeshCore::MeshGeomFacet face = m_CadMesh.GetFacet(i);
00151     //    normal = face.GetNormal();
00152 
00153     //    if (normal.z < 0.0)
00154     //    {
00155     //        facets[i].FlipNormal();
00156     //    }
00157     //}
00158 
00159     //m_CadMesh.Assign(points, facets);
00160 
00161     return true;
00162 }
00163 
00164 bool SpringbackCorrection::CalcCurv()
00165 {
00166     Base::Builder3D logo;
00167     BRep_Builder VertexBuild;
00168     BRepExtrema_DistShapeShape pnt2edge;
00169     pnt2edge.SetDeflection(0.1);
00170 
00171     TopExp_Explorer aExpFace;
00172     TopExp_Explorer aExpEdge;
00173     TopoDS_Vertex V;
00174 
00175     MeshPnt aMeshStruct;
00176     MeshCore::MeshPointIterator pntIt(m_CadMesh);
00177     MeshCore::MeshKernel FaceMesh;
00178     MeshCore::MeshPointArray MeshPnts, MeshPntsCop, MeshPntsCad;
00179     MeshCore::MeshPointArray MeshPntsCopy;
00180     MeshCore::MeshFacetArray MeshFacets, MeshFacets2;
00181     MeshCore::MeshFacetIterator facetIt(m_CadMesh);
00182 
00183     std::pair<Base::Vector3f, MeshPnt> inp;
00184     std::vector<int> MeanVec;
00185     std::vector<FacePnt> FacePntVector;
00186     TopLoc_Location aloc;
00187     Base::Vector3f mP;
00188     gp_Pnt e_pnt, m_pnt;
00189     double dist, distSum, revSum;;
00190     int n;
00191 
00192     std::map<TopoDS_Edge, std::vector<double>, Edge_Less>::iterator edge_it;
00193     std::map<Base::Vector3f,MeshPnt,MeshPntLess >::iterator meshIt;
00194     std::vector<MeshCore::MeshPoint>::iterator pIt;
00195 
00196     m_CurvPos.resize(m_CadMesh.CountPoints());
00197     m_CurvNeg.resize(m_CadMesh.CountPoints());
00198     m_MeshStruct.resize(m_CadMesh.CountPoints());
00199     MeanVec.resize(m_CadMesh.CountPoints(), 0);
00200 
00201     // Fülle Map mit Punkten (Key) und deren, zur Basistriangulierung korrespondierenden, Indizes
00202     MeshPnts = m_CadMesh.GetPoints();
00203         MeshPntsCop = MeshPnts;
00204         MeshFacets2 = m_CadMesh.GetFacets();
00205 
00206     for (unsigned int i=0; i<MeshPnts.size(); ++i)
00207     {
00208         aMeshStruct.pnt = MeshPnts[i];        // stores point
00209         aMeshStruct.index = i;                // stores index
00210         aMeshStruct.maxCurv = 1e+3;
00211         aMeshStruct.minCurv = -1e+3;
00212         inp.first  = aMeshStruct.pnt;
00213         inp.second = aMeshStruct;
00214         MeshMap.insert(inp);
00215     }
00216 
00217     // explores all faces  ------------  Hauptschleife
00218     for (aExpFace.Init(m_Shape,TopAbs_FACE);aExpFace.More();aExpFace.Next())
00219     {
00220         TopoDS_Face aFace = TopoDS::Face(aExpFace.Current());
00221         TransferFaceTriangulationtoFreeCAD(aFace, FaceMesh);
00222         MeshPnts.clear();
00223         MeshPnts = FaceMesh.GetPoints();
00224         MeshPntsCopy = MeshPnts;
00225         n = MeshPnts.size();
00226         TopLoc_Location aLocation;
00227         // berechne normalen -> m_MeshStruct
00228         Handle_Poly_Triangulation aTr = BRep_Tool::Triangulation(aFace,aLocation);
00229         const TColgp_Array1OfPnt& aNodes = aTr->Nodes();
00230         // create array of node points in absolute coordinate system
00231         TColgp_Array1OfPnt aPoints(1, aNodes.Length());
00232         for ( Standard_Integer i = 1; i <= aNodes.Length(); i++)
00233             aPoints(i) = aNodes(i).Transformed(aLocation);
00234 
00235         const TColgp_Array1OfPnt2d& aUVNodes = aTr->UVNodes();
00236 
00237 
00238         BRepAdaptor_Surface aSurface(aFace);
00239         Base::Vector3f Pnt1, Pnt2;
00240         gp_Pnt2d par;
00241         gp_Pnt P;
00242         gp_Vec D1U, D1V;
00243 
00244         for (int i=1; i<n+1; ++i)
00245         {
00246             par = aUVNodes.Value(i);
00247             aSurface.D1(par.X(),par.Y(),P,D1U,D1V);
00248             P = aPoints(i);
00249             Pnt1.x = (float) P.X();
00250             Pnt1.y = (float) P.Y();
00251             Pnt1.z = (float) P.Z();
00252             meshIt = MeshMap.find(Pnt1);
00253             if (meshIt == MeshMap.end())
00254             {
00255                 cout << "error";
00256                 return false;
00257             }
00258 
00259             D1U.Cross(D1V);
00260             D1U.Normalize();
00261             if (aFace.Orientation() == TopAbs_FORWARD) D1U.Scale(-1.0);
00262 
00263             Pnt2.Set(float(Pnt1.x+D1U.X()),float(Pnt1.y+D1U.Y()),float(Pnt1.z+D1U.Z()));
00264             logo.addSingleArrow(Pnt1, Pnt2);
00265 
00266             m_MeshStruct[((*meshIt).second).index].normal.x = (float) D1U.X();
00267             m_MeshStruct[((*meshIt).second).index].normal.y = (float) D1U.Y();
00268             m_MeshStruct[((*meshIt).second).index].normal.z = (float) D1U.Z();
00269 
00270         }
00271 
00272         logo.saveToFile("c:/norm.iv");
00273 
00274 
00275 
00276         // get Inner Mesh Points
00277         int innerpoints = GetBoundary(FaceMesh, MeshPnts);
00278 
00279 
00280 
00281         FacePntVector.resize(innerpoints);  // stores inner-points and sourrounding edges-distances
00282 
00283         // explores the edges of the face ----------------------
00284         for (aExpEdge.Init(aFace,TopAbs_EDGE);aExpEdge.More();aExpEdge.Next())
00285         {
00286             TopoDS_Edge aEdge = TopoDS::Edge(aExpEdge.Current());
00287 
00288             edge_it = EdgeMap.find(aEdge);
00289             if (edge_it == EdgeMap.end())
00290             {
00291                 cout << "error1";
00292                 return false;
00293             }
00294 
00295             pnt2edge.LoadS1(aEdge);
00296 
00297             int counter = 0;
00298 
00299             for (pIt = MeshPnts.begin(); pIt != MeshPnts.end(); ++pIt)
00300             {
00301                 //check if there is no boundary
00302                 if (pIt->_ulProp == 0)
00303                 {
00304                     m_pnt.SetCoord(pIt->x, pIt->y, pIt->z);
00305                     VertexBuild.MakeVertex(V,m_pnt,0.001);
00306                     pnt2edge.LoadS2(V);
00307                     pnt2edge.Perform();
00308 
00309                     if (pnt2edge.IsDone() == false)
00310                     {
00311                         throw Base::Exception("couldn't perform distance calculation pnt2edge \n");
00312                     }
00313 
00314                     dist = pnt2edge.Value();
00315 
00316                     FacePntVector[counter].pnt = *pIt;
00317                     FacePntVector[counter].distances.push_back(dist);
00318                     FacePntVector[counter].MinEdgeOff.push_back((*edge_it).second[0]);
00319                     FacePntVector[counter].MaxEdgeOff.push_back((*edge_it).second[1]);
00320                     ++counter;
00321                 }
00322 
00323             }
00324 
00325             // Knoten auf der EDGE die EdgeOffset-Werte zuweisen
00326 
00327             // Edge -> Polygon -> Nodes
00328             Handle(Poly_PolygonOnTriangulation) polyg;
00329             Handle_Poly_Triangulation aTrLoc;
00330             BRep_Tool::PolygonOnTriangulation(aEdge,polyg,aTrLoc,aloc);
00331 
00332             TColStd_Array1OfInteger IndArr(1,polyg->NbNodes());
00333             IndArr = polyg->Nodes();
00334 
00335             const TColgp_Array1OfPnt& Nodes = aTrLoc->Nodes();
00336             TColgp_Array1OfPnt TrLocPnts(1, Nodes.Length());
00337             for ( Standard_Integer i = 1; i <= Nodes.Length(); i++)
00338                 TrLocPnts(i) = Nodes(i).Transformed(aloc);
00339 
00340             for (int k=0; k<polyg->Nodes().Upper(); ++k)
00341             {
00342                 e_pnt = TrLocPnts(IndArr.Value(k+1));
00343 
00344                 mP.x = (float) e_pnt.X();
00345                 mP.y = (float) e_pnt.Y();
00346                 mP.z = (float) e_pnt.Z();
00347 
00348                 meshIt = MeshMap.find(mP);
00349                 if (meshIt == MeshMap.end())
00350                 {
00351                     cout << "error2" << endl;
00352                     return false;
00353                 }
00354 
00355 
00356                 ++MeanVec[meshIt->second.index];
00357 
00358                 // nehme stets den minimalen wert
00359                 if (((*edge_it).second[0])>((*meshIt).second).minCurv)
00360                     ((*meshIt).second).minCurv = (*edge_it).second[0];
00361 
00362                 if ((*edge_it).second[1]<((*meshIt).second).maxCurv)
00363                     ((*meshIt).second).maxCurv = (*edge_it).second[1];
00364             }
00365         }
00366 
00367         // Knoten INNERHALB eines Faces die Offset-Werte zuweisen
00368         for (unsigned int k=0; k<FacePntVector.size(); ++k)
00369         {
00370             meshIt = MeshMap.find(FacePntVector[k].pnt);
00371 
00372             if (meshIt == MeshMap.end())
00373             {
00374                 cout << "error3";
00375                 return false;
00376             }
00377 
00378             distSum = 0.0;
00379             for (unsigned int l=0; l<FacePntVector[k].distances.size(); ++l)
00380                 distSum +=  FacePntVector[k].distances[l];
00381 
00382             revSum = 0.0;
00383             for (unsigned int l=0; l<FacePntVector[k].distances.size(); ++l)
00384             {
00385                 revSum += distSum/FacePntVector[k].distances[l];
00386 
00387                 if (((*meshIt).second).minCurv == -1e+3)
00388                     ((*meshIt).second).minCurv = 0.0;
00389 
00390                 if (((*meshIt).second).maxCurv == 1e+3)
00391                     ((*meshIt).second).maxCurv = 0.0;
00392 
00393                 ((*meshIt).second).minCurv += distSum*(FacePntVector[k].MinEdgeOff[l])/FacePntVector[k].distances[l];
00394                 ((*meshIt).second).maxCurv += distSum*(FacePntVector[k].MaxEdgeOff[l])/FacePntVector[k].distances[l];
00395 
00396             }
00397 
00398             ((*meshIt).second).minCurv /= revSum;
00399             ((*meshIt).second).maxCurv /= revSum;
00400 
00401         }
00402         FacePntVector.clear();
00403     }
00404 
00405 
00406 
00407     Base::Builder3D log,log3d;
00408     Base::Vector3f pa,pb;
00409     char text[10];
00410     int w;
00411 
00412     // übergebe krümmungswerte an m_MeshStruct
00413     for (meshIt = MeshMap.begin(); meshIt != MeshMap.end(); ++meshIt)
00414     {
00415         w = meshIt->second.index;
00416         m_MeshStruct[w].pnt = (*meshIt).first;
00417 
00418         m_MeshStruct[w].minCurv = (((*meshIt).second).minCurv);
00419         m_MeshStruct[w].maxCurv = (((*meshIt).second).maxCurv);
00420 
00421         // Ausgabe
00422         if (m_MeshStruct[w].maxCurv<10000000000)
00423         {
00424             snprintf(text,10,"%f",m_MeshStruct[w].minCurv);
00425             //snprintf(text,10,"%i",w);
00426             log.addText(m_MeshStruct[w].pnt.x, m_MeshStruct[w].pnt.y, m_MeshStruct[w].pnt.z,text);
00427         }
00428 
00429         if (MeanVec[w] == 1)
00430             log3d.addSinglePoint(m_MeshStruct[w].pnt,4,0,0,0); // innere punkte - > schwarz
00431         else if (MeanVec[w] == 2)
00432             log3d.addSinglePoint(m_MeshStruct[w].pnt,4,1,1,1); // edge-punkte   - > weiß
00433         else
00434             log3d.addSinglePoint(m_MeshStruct[w].pnt,4,1,0,0); // eck-punkte    - > rot
00435 
00436         //m_MeshStruct[w].minCurv = (((*meshIt).second).minCurv)/MeanVec[w];
00437         //m_MeshStruct[w].maxCurv = (((*meshIt).second).maxCurv)/MeanVec[w];
00438 
00439         //if(m_MeshStruct[w].maxCurv < 100 || fabs(m_MeshStruct[w].minCurv) < 100)
00440         //{
00441         // if(m_MeshStruct[w].maxCurv > fabs(m_MeshStruct[w].minCurv))
00442         // {
00443         //  snprintf(text,10,"%f",m_MeshStruct[w].minCurv);
00444         //  //m_MeshStruct[w].normal.Scale(m_MeshStruct[w].minCurv,m_MeshStruct[w].minCurv,m_MeshStruct[w].minCurv);
00445         //  logo.addSingleArrow(m_MeshStruct[w].pnt, m_MeshStruct[w].pnt+m_MeshStruct[w].normal);
00446         // }
00447         // else
00448         // {
00449         //  //snprintf(text,4,"%f",m_MeshStruct[w].maxCurv);
00450         //  m_MeshStruct[w].normal.Scale(m_MeshStruct[w].maxCurv,m_MeshStruct[w].maxCurv,m_MeshStruct[w].maxCurv);
00451         //  logo.addSingleArrow(m_MeshStruct[w].pnt, m_MeshStruct[w].pnt+m_MeshStruct[w].normal);
00452         // }
00453         //}
00454 
00455         //if(m_MeshStruct[w].maxCurv < 100)
00456         //{
00457         // m_MeshStruct[w].normal.Scale(0.5*m_MeshStruct[w].maxCurv,0.5*m_MeshStruct[w].maxCurv,0.5*m_MeshStruct[w].maxCurv);
00458         // logo.addSingleArrow(m_MeshStruct[w].pnt, m_MeshStruct[w].pnt+m_MeshStruct[w].normal);
00459         //}*/
00460 
00461         //snprintf(text,10,"%f",m_MeshStruct[w].maxCurv);
00462         //snprintf(text,10,"%i",w);
00463 
00464 
00465         /*if(m_MeshStruct[w].maxCurv > fabs(m_MeshStruct[w].minCurv))
00466         {
00467          snprintf(text,10,"%f",m_MeshStruct[w].minCurv);
00468          
00469         }
00470         else
00471         {
00472          snprintf(text,10,"%f",m_MeshStruct[w].maxCurv);
00473         }*/
00474 
00475 
00476 
00477     }
00478 
00479     log.saveToFile("c:/printCurv.iv");
00480     log3d.saveToFile("c:/triPnts.iv");
00481     logo.saveToFile("c:/NormalsCurv.iv");
00482 
00483         MeshPntsCad = m_CadMesh.GetPoints();
00484         
00485         
00486          for (unsigned int j=0; j<m_FixFaces.size(); ++j)
00487          {
00488         TopoDS_Face aFace = TopoDS::Face(m_FixFaces[j]);
00489         TransferFaceTriangulationtoFreeCAD(aFace, FaceMesh);
00490                 MeshPnts = FaceMesh.GetPoints();
00491                 MeshFacets2 = m_CadMesh.GetFacets();
00492         MeshPntsCopy = MeshPnts;
00493         n = MeshPnts.size();
00494         TopLoc_Location aLocation;
00495         // berechne normalen -> m_MeshStruct
00496         Handle_Poly_Triangulation aTr = BRep_Tool::Triangulation(aFace,aLocation);
00497         const TColgp_Array1OfPnt& aNodes = aTr->Nodes();
00498         // create array of node points in absolute coordinate system
00499         TColgp_Array1OfPnt aPoints(1, aNodes.Length());
00500         for ( Standard_Integer i = 1; i <= aNodes.Length(); i++)
00501             aPoints(i) = aNodes(i).Transformed(aLocation);
00502 
00503         const TColgp_Array1OfPnt2d& aUVNodes = aTr->UVNodes();
00504 
00505         BRepAdaptor_Surface aSurface(aFace);
00506         Base::Vector3f Pnt1, Pnt2;
00507         gp_Pnt2d par;
00508         gp_Pnt P;
00509         gp_Vec D1U, D1V;
00510                 std::vector<bool> trans_check(m_MeshStruct.size(), false);
00511 
00512         for (int i=1; i<n+1; ++i)
00513         {
00514             par = aUVNodes.Value(i);
00515             aSurface.D1(par.X(),par.Y(),P,D1U,D1V);
00516             P = aPoints(i);
00517             Pnt1.x = (float) P.X();
00518             Pnt1.y = (float) P.Y();
00519             Pnt1.z = (float) P.Z();
00520             meshIt = MeshMap.find(Pnt1);
00521             if (meshIt == MeshMap.end())
00522             {
00523                 cout << "error";
00524                 return false;
00525             }
00526 
00527             D1U.Cross(D1V);
00528             D1U.Normalize();
00529             if (aFace.Orientation() == TopAbs_FORWARD) D1U.Scale(-1.0);
00530 
00531                         int g = ((*meshIt).second).index;
00532 
00533                         D1U.Scale(0.1);
00534                         if(trans_check[g] == false)
00535                         {
00536                                 m_dist_vec[g].x = - D1U.X();
00537                                 m_dist_vec[g].y = - D1U.Y();
00538                                 m_dist_vec[g].z = - D1U.Z();
00539 
00540                                 MeshPntsCad[g].Set(MeshPntsCad[g].x - D1U.X(),
00541                                                                    MeshPntsCad[g].y - D1U.Y(),
00542                                                                    MeshPntsCad[g].z - D1U.Z());
00543                                 
00544                                 (m_MeshStruct[g].pnt).Set(MeshPntsCad[g].x - D1U.X(),
00545                                                                                   MeshPntsCad[g].y - D1U.Y(),
00546                                                                           MeshPntsCad[g].z - D1U.Z());
00547 
00548                                 trans_check[g] = true;
00549                         }
00550                 }
00551          }
00552          m_CadMesh.Assign(MeshPntsCad, MeshFacets2);
00553 
00554      return true;
00555 }
00556 
00557 
00558 bool SpringbackCorrection::Init()
00559 {
00560         m_EdgeStruct.clear();
00561     EdgeMap.clear();
00562     MeshMap.clear();
00563     //Remove any existing Triangulation on the Shape
00564     BRepTools::Clean(m_Shape);
00565     best_fit::Tesselate_Shape(m_Shape,m_CadMesh,(float) 0.1); // Basistriangulierung des ganzen Shapes
00566 
00567     MeshCore::MeshTopoAlgorithm algo(m_CadMesh);
00568     algo.HarmonizeNormals();
00569 
00570     MeshCore::MeshGeomFacet facet = m_CadMesh.GetFacet(0);
00571     if (facet.GetNormal().z < 0.0)
00572         algo.FlipNormals();
00573 
00574     int n = m_CadMesh.CountFacets();
00575     Base::Vector3f normal;
00576 
00577     MeshCore::MeshPointArray points = m_CadMesh.GetPoints();
00578     MeshCore::MeshFacetArray facets = m_CadMesh.GetFacets();
00579 
00580     for (int i=0; i<n; ++i)
00581     {
00582         MeshCore::MeshGeomFacet face = m_CadMesh.GetFacet(i);
00583         normal = face.GetNormal();
00584 
00585         if (normal.z < 0.0)
00586         {
00587             facets[i].FlipNormal();
00588         }
00589     }
00590 
00591     m_CadMesh.Assign(points, facets);
00592 
00593     double max = cMin;
00594     TopTools_IndexedDataMapOfShapeListOfShape anIndex;
00595     anIndex.Clear();
00596     TopExp aMap;
00597     aMap.MapShapesAndAncestors(m_Shape,TopAbs_EDGE,TopAbs_FACE,anIndex);
00598     TopExp_Explorer anExplorer, anExplorer2;
00599     TopoDS_Face aFace;
00600     EdgeStruct tempEdgeStruct;
00601     std::vector<EdgeStruct> EdgeVec;
00602     MeshCore::MeshKernel FaceMesh;
00603     std::vector<double> curvature(2);
00604     std::pair<TopoDS_Edge, std::vector<double> > aPair;
00605 
00606     float maxOffset, minOffset;
00607 
00608     for (anExplorer.Init(m_Shape,TopAbs_EDGE);anExplorer.More();anExplorer.Next())
00609     {
00610         tempEdgeStruct.aFace.clear();
00611         tempEdgeStruct.MaxOffset = 1e+10;
00612         tempEdgeStruct.MinOffset = -1e+10;
00613         tempEdgeStruct.anEdge = TopoDS::Edge(anExplorer.Current()); // store current edge
00614 
00615         const TopTools_ListOfShape& aFaceList = anIndex.FindFromKey(tempEdgeStruct.anEdge);
00616         TopTools_ListIteratorOfListOfShape aListIterator;
00617 
00618         for (aListIterator.Initialize(aFaceList);aListIterator.More(); aListIterator.Next())
00619         {
00620             // store corresponding faces
00621             aFace = TopoDS::Face(aListIterator.Value());
00622             tempEdgeStruct.aFace.push_back(aFace);
00623             TransferFaceTriangulationtoFreeCAD(aFace, FaceMesh);
00624             curvature = MeshCurvature(aFace, FaceMesh);
00625 
00626             if (aFace.Orientation() == TopAbs_REVERSED)
00627             {
00628                 maxOffset = (float) -curvature[1];
00629                 minOffset = (float) -curvature[0];
00630             }
00631             else
00632             {
00633                 maxOffset = (float) curvature[0];
00634                 minOffset = (float) curvature[1];
00635             }
00636 
00637             if (maxOffset < tempEdgeStruct.MaxOffset)
00638                 tempEdgeStruct.MaxOffset = maxOffset;
00639 
00640             if (minOffset > tempEdgeStruct.MinOffset)
00641                 tempEdgeStruct.MinOffset = minOffset;
00642         }
00643 
00644 
00645         if (tempEdgeStruct.MaxOffset > max)
00646             curvature[1] = tempEdgeStruct.MaxOffset;
00647         else
00648             curvature[1] = cMin;
00649 
00650         if (-tempEdgeStruct.MinOffset > max)
00651             curvature[0] = tempEdgeStruct.MinOffset;
00652         else
00653             curvature[0] = -cMin;
00654 
00655         aPair.first = tempEdgeStruct.anEdge;
00656         aPair.second = curvature;
00657         EdgeMap.insert(aPair);
00658 
00659     }
00660     return true;
00661 }
00662 
00663 bool SpringbackCorrection::Init_Setting(struct CuttingToolsSettings& set)
00664 {
00665     m_set = set;
00666     return true;
00667 }
00668 
00669 bool SpringbackCorrection::SetFixEdges()
00670 {
00671     TopExp_Explorer anExplorer;
00672     TopoDS_Edge anEdge;
00673     std::map<TopoDS_Edge, std::vector<double>, Edge_Less>::iterator edge_it;
00674     std::vector<double> pair(2,0.0);
00675 
00676     for (unsigned int i=0; i<m_FixFaces.size(); ++i)
00677     {
00678         for (anExplorer.Init(m_FixFaces[i],TopAbs_EDGE); anExplorer.More(); anExplorer.Next())
00679         {
00680             anEdge  = TopoDS::Edge(anExplorer.Current());
00681             edge_it = EdgeMap.find(anEdge);
00682             edge_it->second = pair;
00683         }
00684     }
00685 
00686     return true;
00687 }
00688 
00689 bool SpringbackCorrection::TransferFaceTriangulationtoFreeCAD(const TopoDS_Face& aFace, MeshCore::MeshKernel& FaceMesh)
00690 {
00691     FaceMesh.Clear();
00692     MeshCore::MeshBuilder builder(FaceMesh);
00693     builder.Initialize(1000);
00694     Base::Vector3f Points[3];
00695 
00696     TopLoc_Location aLocation;
00697     // takes the triangulation of the face aFace:
00698     Handle_Poly_Triangulation aTr = BRep_Tool::Triangulation(aFace,aLocation);
00699     if (!aTr.IsNull()) // if this triangulation is not NULL
00700     {
00701         // takes the array of nodes for this triangulation:
00702         const TColgp_Array1OfPnt& aNodes = aTr->Nodes();
00703         // takes the array of triangles for this triangulation:
00704         const Poly_Array1OfTriangle& triangles = aTr->Triangles();
00705         // create array of node points in absolute coordinate system
00706         TColgp_Array1OfPnt aPoints(1, aNodes.Length());
00707         for ( Standard_Integer i = 1; i <= aNodes.Length(); i++)
00708             aPoints(i) = aNodes(i).Transformed(aLocation);
00709         // Takes the node points of each triangle of this triangulation.
00710         // takes a number of triangles:
00711         Standard_Integer nnn = aTr->NbTriangles();
00712         Standard_Integer nt,n1,n2,n3;
00713         for ( nt = 1 ; nt < nnn+1 ; nt++)
00714         {
00715             // takes the node indices of each triangle in n1,n2,n3:
00716             triangles(nt).Get(n1,n2,n3);
00717             // takes the node points:
00718             gp_Pnt aPnt1 = aPoints(n1);
00719             Points[0].Set(float(aPnt1.X()),float(aPnt1.Y()),float(aPnt1.Z()));
00720             gp_Pnt aPnt2 = aPoints(n2);
00721             Points[1].Set((float) aPnt2.X(),(float) aPnt2.Y(),(float) aPnt2.Z());
00722             gp_Pnt aPnt3 = aPoints(n3);
00723             Points[2].Set((float) aPnt3.X(),(float) aPnt3.Y(),(float) aPnt3.Z());
00724             // give the occ faces to the internal mesh structure of freecad
00725             MeshCore::MeshGeomFacet Face(Points[0],Points[1],Points[2]);
00726             builder.AddFacet(Face);
00727         }
00728 
00729     }
00730     // if the triangulation of only one face is not possible to get
00731     else
00732     {
00733         throw Base::Exception("Empty face triangulation\n");
00734     }
00735 
00736     // finish FreeCAD Mesh Builder and exit with new mesh
00737     builder.Finish();
00738     return true;
00739 
00740 }
00741 
00742 /*
00743 MeshCore::MeshKernel SpringbackCorrection::BuildMesh(Handle_Poly_Triangulation aTri, std::vector<Base::Vector3f> TrPoints)
00744 {
00745     MeshCore::MeshKernel FaceMesh;
00746     MeshCore::MeshBuilder builder(FaceMesh);
00747     builder.Initialize(1000);
00748     Base::Vector3f Points[3];
00749 
00750     const Poly_Array1OfTriangle& triangles = aTri->Triangles();
00751     Standard_Integer nnn = aTri->NbTriangles();
00752     Standard_Integer nt,n1,n2,n3;
00753 
00754     for ( nt = 1 ; nt < nnn+1 ; nt++)
00755     {
00756         // takes the node indices of each triangle in n1,n2,n3:
00757         triangles(nt).Get(n1,n2,n3);
00758         // takes the node points:
00759         Points[0] = TrPoints[n1];
00760         Points[1] = TrPoints[n2];
00761         Points[2] = TrPoints[n3];
00762         // give the occ faces to the internal mesh structure of freecad
00763         MeshCore::MeshGeomFacet Face(Points[0],Points[1],Points[2]);
00764         builder.AddFacet(Face);
00765     }
00766 
00767     // finish FreeCAD Mesh Builder and exit with new mesh
00768     builder.Finish();
00769 
00770     return FaceMesh;
00771 }
00772 */
00773 
00774 int SpringbackCorrection::GetBoundary(const MeshCore::MeshKernel &mesh, MeshCore::MeshPointArray &meshPnts)
00775 {
00776     // get mesh-boundary
00777     // zweck: nur für die inneren punkte wird der curvature wert über die edge abstände berechnet
00778     std::list< std::vector <unsigned long> >  BoundariesIndex;
00779     std::list< std::vector <unsigned long> >::iterator bInd;
00780 
00781 
00782     MeshCore::MeshAlgorithm algo(mesh);
00783     algo.GetMeshBorders(BoundariesIndex);
00784     //Set all MeshPoints to Property 0
00785     for (unsigned int i=0;i<meshPnts.size();++i)
00786         meshPnts[i].SetProperty(0);
00787 
00788     //Set Boundary Points to 1
00789     int inner_points = 0;
00790     for (bInd = BoundariesIndex.begin(); bInd != BoundariesIndex.end(); ++bInd)
00791     {
00792         for (unsigned int i=0;i< bInd->size();++i)
00793         {
00794             meshPnts[(*bInd).at(i)].SetProperty(1);
00795         }
00796 
00797     }
00798     for (unsigned int i=0;i<meshPnts.size();++i)
00799     {
00800         if (meshPnts[i]._ulProp == 0)
00801             inner_points++;
00802     }
00803 
00804     return (inner_points);
00805 }
00806 /*
00807 bool SpringbackCorrection::SmoothCurvature()
00808 {
00809     MeshCore::MeshPointIterator v_it(m_Mesh);
00810     MeshCore::MeshRefPointToPoints vv_it(m_Mesh);
00811     std::set<MeshCore::MeshPointArray::_TConstIterator> PntNei;
00812     std::set<MeshCore::MeshPointArray::_TConstIterator>::iterator pnt_it;
00813 
00814     int n = m_MeshStruct.size();
00815 
00816     std::vector<double> SCMax(n);
00817     std::vector<double> SCMin(n);
00818     double maxCurv = 0.0, minCurv = 0.0;
00819 
00820     for (v_it.Begin(); v_it.More(); v_it.Next())
00821     {
00822         PntNei = vv_it[v_it.Position()];
00823         maxCurv += m_MeshStruct[v_it.Position()].maxCurv;
00824         minCurv += m_MeshStruct[v_it.Position()].minCurv;
00825 
00826         for (pnt_it = PntNei.begin(); pnt_it !=PntNei.end(); ++pnt_it)
00827         {
00828             maxCurv += m_MeshStruct[(*pnt_it)[0]._ulProp].maxCurv;
00829             minCurv += m_MeshStruct[(*pnt_it)[0]._ulProp].minCurv;
00830         }
00831 
00832         SCMax[v_it.Position()] = maxCurv/(PntNei.size()+1);
00833         SCMin[v_it.Position()] = minCurv/(PntNei.size()+1);
00834 
00835         maxCurv = 0.0;
00836         minCurv = 0.0;
00837     }
00838 
00839     for (int i=0; i<n; ++i)
00840     {
00841         m_MeshStruct[i].maxCurv = SCMax[i];
00842         m_MeshStruct[i].minCurv = SCMin[i];
00843     }
00844 
00845     return true;
00846 }
00847 */
00848 
00849 bool SpringbackCorrection::SmoothMesh(MeshCore::MeshKernel &Mesh, double d_max)
00850 {
00851     Base::Builder3D log;
00852     MeshCore::MeshKernel localMesh, Meshtmp;
00853     MeshCore::MeshPoint mpnt, spnt;
00854     MeshCore::MeshPointArray locPointArray, PointArray = Mesh.GetPoints();
00855     MeshCore::MeshFacetArray locFacetArray, FacetArray = Mesh.GetFacets();
00856 
00857     MeshCore::MeshPointIterator v_it(Mesh);
00858     MeshCore::MeshRefPointToPoints vv_it(Mesh);
00859     std::set<unsigned long>::const_iterator pnt_it;
00860     MeshCore::MeshPointArray::_TConstIterator v_beg = Mesh.GetPoints().begin();
00861 
00862     Base::Vector3f N, L, coor;
00863     int n = m_MeshStruct.size();
00864     int g;
00865 
00866     for (v_it.Begin(); v_it.More(); v_it.Next())
00867     {
00868 
00869         g = v_it.Position();
00870         spnt.Set(0.0, 0.0, 0.0);
00871         locPointArray.push_back(*v_it);
00872         spnt += *v_it;
00873         const std::set<unsigned long>& PntNei = vv_it[(*v_it)._ulProp];
00874 
00875         if (PntNei.size() < 3)
00876             continue;
00877 
00878         for (pnt_it = PntNei.begin(); pnt_it !=PntNei.end(); ++pnt_it)
00879         {
00880             locPointArray.push_back(v_beg[*pnt_it]);
00881             spnt += v_beg[*pnt_it];
00882         }
00883 
00884         spnt.Scale((float) (1.0/(double(PntNei.size()) + 1.0)),(float) (1.0/(double(PntNei.size()) + 1.0)),(float) (1.0/(double(PntNei.size()) + 1.0)));
00885 
00886         localMesh.Assign(locPointArray, locFacetArray);
00887 
00888 
00889         // need at least one facet to perform the PCA
00890         MeshCore::MeshGeomFacet face;
00891         face._aclPoints[0] = locPointArray[0];
00892         face._aclPoints[1] = locPointArray[1];
00893         face._aclPoints[2] = locPointArray[2];
00894         localMesh.AddFacet(face);
00895 
00896         MeshCore::MeshEigensystem pca(localMesh);
00897         pca.Evaluate();
00898         Base::Matrix4D T1 = pca.Transform();
00899 
00900         N.x = (float) T1[0][0];
00901         N.y = (float) T1[0][1];
00902         N.z = (float) T1[0][2];
00903         L.Set(v_it->x - spnt.x, v_it->y - spnt.y, v_it->z - spnt.z);
00904         N.Normalize();
00905 
00906 
00907         if (N*L < 0.0)
00908             N.Scale(-1.0, -1.0, -1.0);
00909 
00910         if ((*v_it)._ulProp == 2286)
00911         {
00912             log.addSinglePoint(spnt,4,1,0,0);
00913             log.addSinglePoint(*v_it,4,0,0,0);
00914             for (unsigned int i=0; i<locPointArray.size(); ++i)
00915                 log.addSinglePoint(locPointArray[i],4);
00916 
00917             for (int i=0; i<3; ++i)
00918             {
00919                 coor.x = (float) T1[i][0];
00920                 coor.y = (float) T1[i][1];
00921                 coor.z = (float) T1[i][2];
00922 
00923                 coor.Normalize();
00924 
00925                 log.addSingleArrow(*v_it, *v_it + coor);
00926             }
00927 
00928             /*log.addSingleArrow(*v_it, *v_it - N);
00929             snprintf(text,10,"%i",(*v_it)._ulProp);
00930             log.addText(v_it->x, v_it->y, v_it->z,text);*/
00931         }
00932 
00933         double d = d_max;
00934         if (fabs(N*L)< d_max) d = fabs(N*L);
00935         N.Scale((float) d,(float) d,(float) d);
00936 
00937         PointArray[v_it.Position()].Set(v_it->x - N.x, v_it->y - N.y, v_it->z - N.z);
00938         locPointArray.clear();
00939         localMesh = Meshtmp;
00940     }
00941 
00942     Mesh.Assign(PointArray, FacetArray);
00943     log.saveToFile("c:/checkNormals.iv");
00944 
00945     return true;
00946 }
00947 
00948 bool SpringbackCorrection::SmoothMesh(MeshCore::MeshKernel &Mesh, std::vector<int> ind, double d_max)
00949 {
00950     Base::Builder3D log;
00951     MeshCore::MeshKernel localMesh, Meshtmp;
00952     MeshCore::MeshPoint mpnt, spnt;
00953     MeshCore::MeshPointArray locPointArray, PointArray = Mesh.GetPoints();
00954     MeshCore::MeshFacetArray locFacetArray, FacetArray = Mesh.GetFacets();
00955 
00956     MeshCore::MeshPointIterator v_it(Mesh);
00957     MeshCore::MeshRefPointToPoints vv_it(Mesh);
00958     std::set<unsigned long>::const_iterator pnt_it;
00959     MeshCore::MeshPointArray::_TConstIterator v_beg = Mesh.GetPoints().begin();
00960 
00961     Base::Vector3f N, L, coor;
00962 
00963     int n = ind.size();
00964 
00965     for (int i=0; i<n; ++i)
00966     {
00967         v_it.Set(ind[i]);
00968         spnt.Set(0.0, 0.0, 0.0);
00969         locPointArray.push_back(*v_it);
00970         spnt += *v_it;
00971         const std::set<unsigned long>& PntNei = vv_it[(*v_it)._ulProp];
00972 
00973         if (PntNei.size() < 3)
00974             continue;
00975 
00976         for (pnt_it = PntNei.begin(); pnt_it !=PntNei.end(); ++pnt_it)
00977         {
00978             locPointArray.push_back(v_beg[*pnt_it]);
00979             spnt += v_beg[*pnt_it];
00980         }
00981 
00982         spnt.Scale((float) (1.0/(double(PntNei.size()) + 1.0)),(float) (1.0/(double(PntNei.size()) + 1.0)),(float) (1.0/(double(PntNei.size()) + 1.0)));
00983 
00984         localMesh.Assign(locPointArray, locFacetArray);
00985 
00986 
00987         // need at least one facet to perform the PCA
00988         MeshCore::MeshGeomFacet face;
00989         face._aclPoints[0] = locPointArray[0];
00990         face._aclPoints[1] = locPointArray[1];
00991         face._aclPoints[2] = locPointArray[2];
00992         localMesh.AddFacet(face);
00993 
00994         MeshCore::MeshEigensystem pca(localMesh);
00995         pca.Evaluate();
00996         Base::Matrix4D T1 = pca.Transform();
00997 
00998         N.x = (float) T1[0][0];
00999         N.y = (float) T1[0][1];
01000         N.z = (float) T1[0][2];
01001         L.Set(v_it->x - spnt.x, v_it->y - spnt.y, v_it->z - spnt.z);
01002         N.Normalize();
01003 
01004 
01005         if (N*L < 0.0)
01006             N.Scale(-1.0, -1.0, -1.0);
01007 
01008         if ((*v_it)._ulProp == 2286)
01009         {
01010             log.addSinglePoint(spnt,4,1,0,0);
01011             log.addSinglePoint(*v_it,4,0,0,0);
01012             for (unsigned int i=0; i<locPointArray.size(); ++i)
01013                 log.addSinglePoint(locPointArray[i],4);
01014 
01015             for (int i=0; i<3; ++i)
01016             {
01017                 coor.x = (float) T1[i][0];
01018                 coor.y = (float) T1[i][1];
01019                 coor.z = (float) T1[i][2];
01020 
01021                 coor.Normalize();
01022 
01023                 log.addSingleArrow(*v_it, *v_it + coor);
01024             }
01025 
01026             /*log.addSingleArrow(*v_it, *v_it - N);
01027             snprintf(text,10,"%i",(*v_it)._ulProp);
01028             log.addText(v_it->x, v_it->y, v_it->z,text);*/
01029         }
01030 
01031         double d = d_max;
01032         if (fabs(N*L)< d_max) d = fabs(N*L);
01033         N.Scale((float) d,(float) d,(float) d);
01034 
01035         PointArray[v_it.Position()].Set(v_it->x - N.x, v_it->y - N.y, v_it->z - N.z);
01036         locPointArray.clear();
01037         localMesh = Meshtmp;
01038     }
01039 
01040     Mesh.Assign(PointArray, FacetArray);
01041     log.saveToFile("c:/checkNormals.iv");
01042 
01043     return true;
01044 }
01045 
01046 /*
01047 bool SpringbackCorrection::GetFaceAng(MeshCore::MeshKernel &mesh, int deg_tol)
01048 {
01049     Base::Vector3f normal, base;
01050     double deg;
01051     int n = mesh.CountFacets();
01052     bool b = true;
01053 
01054     for (int i=0; i<n; ++i)
01055     {
01056         MeshCore::MeshGeomFacet face = mesh.GetFacet(i);
01057         face.CalcNormal();
01058         normal = face.GetNormal();
01059         normal.Normalize();
01060         base = normal;
01061         base.z = 0.0;
01062         base.Normalize();
01063 
01064         if (normal.z < 0.0)
01065             deg = 90.0 + acos(normal*base)*180.0/PI;
01066         else
01067             deg = 90.0 - acos(normal*base)*180.0/PI;
01068 
01069         if (deg > deg_tol)
01070             b = false;
01071 
01072         m_FaceAng.push_back(deg);
01073     }
01074 
01075     return b;
01076 }
01077 */
01078 
01079 std::vector<int> SpringbackCorrection::InitFaceCheck(MeshCore::MeshKernel &mesh, int degree)
01080 {
01081     std::vector<int> faceInd;
01082     double tol = degree;
01083     double alpha;
01084     MeshCore::MeshFacetIterator fIt(mesh);
01085     MeshCore::MeshPointIterator mIt(mesh);
01086     MeshCore::MeshRefPointToFacets p2fIt(mesh);
01087     Base::Vector3f normal, base, gpnt;
01088     Base::Builder3D log;
01089 
01090     MeshCore::MeshPointArray mPnts   = mesh.GetPoints();
01091     MeshCore::MeshFacetArray mFacets = mesh.GetFacets();
01092     MeshCore::MeshFacetArray::_TConstIterator f_beg = mesh.GetFacets().begin();
01093 
01094     int n = mesh.CountFacets();
01095 
01096 
01097     for (int i=0; i<n; ++i)
01098     {
01099         MeshCore::MeshGeomFacet face = mesh.GetFacet(i);
01100         //face.CalcNormal();
01101         normal = face.GetNormal();
01102         normal.Normalize();
01103         base = normal;
01104         base.z = 0.0;
01105         base.Normalize();
01106 
01107         gpnt = face.GetGravityPoint();
01108 
01109 
01110         if (normal.z < 0.0)
01111         {
01112             alpha = 90.0 + acos(normal*base)*180.0/PI;
01113             //log.addSingleArrow(gpnt,gpnt+normal,6,1,0,0);
01114         }
01115         else
01116         {
01117             alpha = 90.0 - acos(normal*base)*180.0/PI;
01118             //log.addSingleArrow(gpnt,gpnt+normal);
01119         }
01120 
01121         if (alpha > tol)
01122         {
01123             // markiere kritisches face
01124             mFacets[i].SetProperty(0);
01125 
01126             for (int j=0; j<3; ++j)
01127             {
01128                 const std::set<unsigned long>& faceSet = p2fIt[mFacets[i]._aulPoints[j]];
01129 
01130                 for (std::set<unsigned long>::const_iterator it = faceSet.begin(); it != faceSet.end(); ++it)
01131                 {
01132                     f_beg[*it].SetProperty(5);
01133                 }
01134             }
01135 
01136             log.addSingleArrow(gpnt,gpnt+normal,4,1,0,0);
01137         }
01138         else
01139         {
01140             mFacets[i].SetProperty(1);
01141             faceInd.push_back(i);
01142         }
01143     }
01144 
01145     log.saveToFile("c:/normalschecker.iv");
01146 
01147     MeshCore::MeshFacetArray mFacets2 = mesh.GetFacets();
01148 
01149     for (unsigned int i=0; i<mFacets2.size(); ++i)
01150     {
01151         if (mFacets2[i]._ulProp == 5)
01152         {
01153             mFacets[i].SetProperty(0);
01154         }
01155     }
01156 
01157     mesh.Assign(mPnts, mFacets);
01158 
01159     log.saveToFile("c:/angles.iv");
01160     return faceInd;
01161 }
01162 
01163 std::vector<int> SpringbackCorrection::FaceCheck(MeshCore::MeshKernel &mesh, int degree)
01164 {
01165     std::vector<int> faceInd;
01166     double tol = degree;
01167     double alpha;
01168     MeshCore::MeshFacetIterator fIt(mesh);
01169     MeshCore::MeshPointIterator mIt(mesh);
01170     Base::Vector3f normal, base, gpnt;
01171     Base::Builder3D log;
01172 
01173 
01174     MeshCore::MeshPointArray mPnts   = mesh.GetPoints();
01175     MeshCore::MeshFacetArray mFacets = mesh.GetFacets();
01176 
01177     int n = mesh.CountFacets();
01178 
01179     for (int i=0; i<n; ++i)
01180     {
01181         MeshCore::MeshGeomFacet face = mesh.GetFacet(i);
01182         face.CalcNormal();
01183         normal = face.GetNormal();
01184         normal.Normalize();
01185         base = normal;
01186         base.z = 0.0;
01187         base.Normalize();
01188 
01189         gpnt = face.GetGravityPoint();
01190 
01191         if (normal.z < 0.0)
01192         {
01193             alpha = 90.0 + acos(normal*base)*180.0/PI;
01194             log.addSingleArrow(gpnt,gpnt+normal,2,1,0,0);
01195         }
01196         else
01197             alpha = 90.0 - acos(normal*base)*180.0/PI;
01198 
01199         if (alpha > tol)
01200         {
01201             faceInd.push_back(i);
01202                         cout << "i: " << i << ", angle: " << alpha << endl;
01203         }
01204     }
01205 
01206     log.saveToFile("c:/angles.iv");
01207     return faceInd;
01208 }
01209 
01210 /*
01211 bool SpringbackCorrection::CorrectScale(double zLim)
01212 {
01213     // VERSCHIEBUNGSVEKTOREN ...
01214     // berechnung der skalierungsfaktoren bzgl. der Cad-Normalen
01215     int n = m_CadMesh.CountPoints();
01216     double zLev;
01217     for (int i=0; i<n; ++i)
01218     {
01219         m_MeshStruct[i].normal.Normalize();
01220         zLev = m_MeshStruct[i].pnt.z + m_Offset[i]*m_MeshStruct[i].normal.z;
01221 
01222         if (zLev>zLim)
01223             m_Offset[i] = (zLim - m_MeshStruct[i].pnt.z)/m_MeshStruct[i].normal.z;
01224     }
01225 
01226     return true;
01227 }
01228 */
01229 
01230 bool SpringbackCorrection::Perform(int deg_Tol, bool out)
01231 {
01232     unsigned int NumRef;
01233         std::vector<double> dist_vec;
01234 
01235     //GetFaceAng(m_CadMesh, deg_Tol+1);
01236     //cout << "Init" << endl;
01237     //Init();      // tesseliere shape -> Basistriangulierung CAD-Mesh
01238     // EdgeMap wird hier gefüllt
01239 
01240         Base::Vector3f nullvec(0.0,0.0,0.0);
01241         m_dist_vec.resize(m_CadMesh.CountPoints(), nullvec); // fülle mit Nullvektoren
01242 
01243     cout << "SetFixEdges" << endl;
01244     
01245         SetFixEdges();
01246 
01247         const MeshCore::MeshKernel RefMesh23 = m_CadMesh;
01248 
01249     cout << "CalcCurv" << endl;
01250 
01251     if (!CalcCurv())    
01252            return false;
01253         
01254         // berechne Krümmungswerte über Edgekrümmungen
01255     // MeshMap wird hier gefüllt
01256 
01257     const MeshCore::MeshKernel RefMesh = m_CadMesh;  // übegebe CAD-Mesh vor der Verformung
01258 
01259     /*Base::Builder3D log;
01260     Base::Vector3f gpnt, normal;
01261     int numb = m_CadMesh.CountFacets();
01262 
01263     for (int i=0; i<numb; ++i)
01264        {
01265            MeshCore::MeshGeomFacet face = m_CadMesh.GetFacet(i);
01266            normal = face.GetNormal();
01267 
01268 
01269      if(normal.z < 0.0)
01270       log.saveToFile("c:/didid.iv");
01271 
01272      gpnt = face.GetGravityPoint();
01273      normal.Scale(10,10,10);
01274      log.addSingleArrow(gpnt,gpnt+normal);
01275     }
01276 
01277     log.saveToFile("c:/Facenormals.iv");*/
01278 
01279     // speichere normalen seperat
01280     m_normals.clear();
01281     m_normals.resize(m_MeshStruct.size());
01282 
01283     //Base::Builder3D logo;
01284 
01285     for (unsigned int i=0; i<m_normals.size(); ++i)
01286     {
01287         m_normals[i] = m_MeshStruct[i].normal;  // Normale am Punkt i der Basistriangulierung
01288         //logo.addSingleArrow(m_MeshStruct[i].pnt, m_MeshStruct[i].pnt + m_normals[i]);
01289     }
01290 
01291     //logo.saveToFile("c:/normals.iv");
01292 
01293     // übergebe Normalen und CAD-Mesh für Fehlerberechnng
01294     best_fit befi;
01295     befi.m_normals = m_normals;
01296     befi.m_CadMesh = m_CadMesh;
01297 
01298         m_error.resize(m_CadMesh.CountPoints(), 0.0);
01299 
01300         for(unsigned int i=0; i<m_MeshVec.size(); ++i)
01301         {
01302                 befi.CompTotalError(m_MeshVec[i]);  // Fehlerberechnung
01303         }
01304 
01305     // VERSCHIEBUNGSVEKTOREN ...
01306     // berechnung der skalierungsfaktoren bzgl. der Cad-Normalen
01307     int n = m_CadMesh.CountPoints();
01308     for (int i=0; i<n; ++i)
01309     {
01310         m_MeshStruct[i].error = befi.m_error[i];
01311 
01312         if (m_MeshStruct[i].error < 0)
01313         {
01314             if ((m_MeshStruct[i].maxCurv - m_set.master_radius) < 0.0)
01315                 m_Offset.push_back(0.0);
01316             else if (-m_MeshStruct[i].error > (m_MeshStruct[i].maxCurv - m_set.master_radius))
01317                 m_Offset.push_back(m_MeshStruct[i].maxCurv - m_set.master_radius);
01318             else
01319                 m_Offset.push_back(-m_MeshStruct[i].error);
01320         }
01321         else
01322         {
01323             if ((m_MeshStruct[i].minCurv + m_set.slave_radius) > 0.0)
01324                 m_Offset.push_back(0.0);
01325             else if (m_MeshStruct[i].error > -(m_MeshStruct[i].minCurv + m_set.slave_radius))
01326                 m_Offset.push_back(m_MeshStruct[i].minCurv + m_set.slave_radius);
01327             else
01328                 m_Offset.push_back(-m_MeshStruct[i].error);
01329         }
01330     }
01331 
01332     //Base::BoundBox3f bbox = m_CadMesh.GetBoundBox();  // hole bounding-box
01333     //CorrectScale(bbox.MaxZ);                          // korrigiere verschiebungsvektoren...
01334 
01335         //m_set.correction_factor = -0.1;
01336     // skalierung der Cad-Normalen
01337     for (int i=0; i<n; ++i)
01338     {
01339         m_normals[i].Scale((float) (m_set.correction_factor*m_Offset[i]),
01340                            (float) (m_set.correction_factor*m_Offset[i]),
01341                            (float) (m_set.correction_factor*m_Offset[i]));
01342     }
01343 
01344         for(int i=0; i<m_normals.size(); ++i)
01345         {
01346                  m_dist_vec[i] = m_normals[i];
01347         }
01348 
01349         if(out==true) //hier werden die Outputvektoren für Catia geschrieben
01350         {
01351                 MeshCore::MeshPointArray mpts = RefMesh23.GetPoints();
01352 
01353                 Base::Builder3D loo;
01354                 ofstream anOutputFile;
01355                 anOutputFile.open("c:/catia_offset.txt");
01356                 anOutputFile.precision(7);
01357 
01358                 for(int i=0; i< mpts.size(); ++i)
01359                 {
01360                         loo.addSingleArrow(mpts[i], mpts[i] + m_dist_vec[i]);
01361                         anOutputFile <<  mpts[i].x << " " << mpts[i].y << " " << mpts[i].z << " " << m_dist_vec[i].x << " " << m_dist_vec[i].y << " " << m_dist_vec[i].z << endl;
01362                 }
01363 
01364                 loo.saveToFile("c:/prpopo.iv");
01365             anOutputFile.close();
01366                 return true; //mehr braucne wir auch nicht....
01367         }
01368 
01369 
01370 
01371     std::vector<int> tmpVec;
01372     tmpVec = FaceCheck(m_CadMesh, deg_Tol+1);
01373         
01374     NumRef = (int) tmpVec.size();
01375 
01376     MeshCore::MeshPointArray pntAr = m_CadMesh.GetPoints();
01377     MeshCore::MeshFacetArray facAr = m_CadMesh.GetFacets();
01378 
01379     // Spiegelung
01380     Base::Builder3D log3d;
01381     for (int i=0; i<n; ++i)
01382     {
01383         log3d.addSingleArrow(pntAr[i], pntAr[i] + m_normals[i]);
01384         pntAr[i].Set(pntAr[i].x + m_normals[i].x, pntAr[i].y + m_normals[i].y, pntAr[i].z + m_normals[i].z);
01385     }
01386 
01387     log3d.saveToFile("c:/project2result.iv");
01388 
01389     m_CadMesh.Assign(pntAr, facAr);
01390 
01391     //std::vector<int> tmpVec;
01392     std::vector<Base::Vector3f> normalsRef = m_normals;
01393     int cc = 0;
01394 
01395 
01396 
01397     // ********************************** GLOBALE KORREKTUR ****************************************
01398     int cm = 49;
01399     InitFaceCheck(m_CadMesh, deg_Tol+1);  // kritische dreiecke -> _ulProp = 0
01400     MeshCore::MeshFacetArray facAr2 = m_CadMesh.GetFacets();
01401 
01402     while (true)
01403     {
01404         tmpVec = FaceCheck(m_CadMesh, deg_Tol+1);
01405         cout << "remaining: " << tmpVec.size() << endl;
01406         if (tmpVec.size() == NumRef || cm == 0)
01407         {
01408             cout << cm << "left" << endl;
01409             break;
01410         }
01411 
01412         --cm;
01413         MeshCore::MeshPointArray pntAr2 = RefMesh.GetPoints();
01414 
01415         for (unsigned int i=0; i<pntAr2.size(); ++i)
01416         {
01417             m_normals[i].Normalize();
01418             m_normals[i].Scale((cm*normalsRef[i].Length())/50,(cm*normalsRef[i].Length())/50, (cm*normalsRef[i].Length())/50);
01419             pntAr2[i].Set(pntAr2[i].x + m_normals[i].x, pntAr2[i].y + m_normals[i].y, pntAr2[i].z + m_normals[i].z);
01420         }
01421 
01422         m_CadMesh.Assign(pntAr2, facAr2);
01423     }
01424 
01425         for(int i=0;  i< m_normals.size(); ++i)
01426         {
01427                  m_dist_vec[i] += m_normals[i];
01428         }
01429 
01430 
01431         
01432     // *********************** REGION-GROWING **********************
01433 
01434     MeshCore::MeshFacetArray mFacets = m_CadMesh.GetFacets();
01435     MeshCore::MeshPointArray mPoints = m_CadMesh.GetPoints();
01436     int num = mFacets.size();
01437 
01438     for (int i=0; i<num; ++i) mFacets[i].ResetFlag(MeshCore::MeshFacet::VISIT);   // lösche alle VISIT-Flags
01439 
01440     m_RingCurrent = 0;
01441     std::vector<unsigned long> aRegion;
01442     std::vector< std::pair<unsigned long, double> > skals;
01443     std::vector< std::vector< std::pair<unsigned long, double> > > RegionSkals;
01444 
01445         MeshCore::MeshBuilder builder(m_Mesh_vis);
01446         builder.Initialize(10000);
01447         MeshCore::MeshBuilder builder2(m_Mesh_vis2);
01448         builder2.Initialize(10000);
01449 
01450     for (int i=0; i<num; ++i)
01451     {
01452 
01453         cout << i << " von " << num << endl;
01454         for (int m=0; m<num; ++m)
01455         {
01456             if (mFacets[m]._ulProp == 0)
01457                         {
01458                 mFacets[m].SetFlag(MeshCore::MeshFacet::VISIT);  // kritische faces kriegen ein visit-flag gesetzt
01459                         }
01460         }
01461 
01462                 if (mFacets[i]._ulProp == 0)
01463                 {
01464                                 MeshCore::MeshGeomFacet Face(mPoints[mFacets[i]._aulPoints[0]],
01465                                                                              mPoints[mFacets[i]._aulPoints[1]],
01466                                                                                  mPoints[mFacets[i]._aulPoints[2]]);
01467                         
01468                             builder.AddFacet(Face);
01469                 }
01470                 else
01471                 {
01472                             MeshCore::MeshGeomFacet Face2(mPoints[mFacets[i]._aulPoints[0]],
01473                                                                               mPoints[mFacets[i]._aulPoints[1]],
01474                                                                                   mPoints[mFacets[i]._aulPoints[2]]);
01475                         
01476                             builder2.AddFacet(Face2);
01477                 }
01478 
01479 
01480         m_CadMesh.Assign(mPoints, mFacets);
01481 
01482         cout << "a" << endl;
01483         if (mFacets[i]._ulProp == 1 && !mFacets[i].IsFlag(MeshCore::MeshFacet::VISIT)) // falls unkritisches facet
01484             // welches noch nicht einer region zugewiesen wurde
01485         {
01486 
01487             cout << "b" << endl;
01488             m_RingVector.clear();
01489             m_RingVector.push_back(i);
01490             m_RegionVector.push_back(m_RingVector);
01491             m_RingVector.clear();
01492 
01493             m_RingCurrent = 0;
01494 
01495             m_CadMesh.VisitNeighbourFacets(*this,i);
01496 
01497             for (unsigned int j=0; j<m_RegionVector.size(); ++j)
01498             {
01499                 cout << "c" << endl;
01500                 for (unsigned int k=0; k<m_RegionVector[j].size(); ++k)
01501                 {
01502                     aRegion.push_back(m_RegionVector[j][k]);
01503 
01504                 }
01505             }
01506 
01507                         /*Base::Vector3f pnt1(0,0,0);
01508                         Base::Vector3f pnt2(1,0,0);
01509                         Base::Vector3f pnt3(0,1,0);
01510                         
01511                         MeshCore::MeshGeomFacet Face(pnt1,pnt2,pnt3);
01512                         builder.AddFacet(Face);*/
01513 
01514             m_Regions.push_back(aRegion);
01515 
01516 
01517 
01518             for (int l=0; l<num; ++l)            mFacets[l]._ucFlag = 0;
01519 
01520             cout << "d" << endl;
01521 
01522             for (unsigned int l=0; l<m_Regions.size(); ++l)
01523                 for (unsigned int m=0; m<m_Regions[l].size(); ++m)
01524                     mFacets[m_Regions[l][m]].SetFlag(MeshCore::MeshFacet::VISIT);
01525 
01526     
01527 
01528             skals = RegionEvaluate(m_CadMesh, aRegion, normalsRef);
01529             RegionSkals.push_back(skals);
01530             skals.clear();
01531 
01532             m_RegionVector.clear();
01533             aRegion.clear();
01534 
01535             cout << "e" << endl;
01536 
01537         }
01538     }
01539 
01540         builder.Finish();
01541         builder2.Finish();
01542 
01543     cout << "fast" << endl;
01544 
01545     Base::Builder3D logg;
01546     double d;
01547 
01548     // stelle die regionen dar
01549 
01550     for (unsigned int i=0; i<RegionSkals.size(); ++i)
01551     {
01552         cout << i << endl;
01553         d = double(i)/double(RegionSkals.size()-1.0);
01554         cout << d << endl;
01555         for (unsigned int j=0; j<RegionSkals[i].size(); ++j)
01556         {
01557 
01558             logg.addSinglePoint((float) mPoints[RegionSkals[i][j].first].x,
01559                                 (float) mPoints[RegionSkals[i][j].first].y,
01560                                 (float) mPoints[RegionSkals[i][j].first].z,3,(float) d,(float) d,(float) d);
01561 
01562 
01563         }
01564     }
01565 
01566     logg.saveToFile("c:/regions.iv");
01567 
01568 
01569 
01570 
01571 
01572     /*
01573        // hole rand der regionen
01574        MeshCore::MeshRefPointToFacets p2fIt(tmpMesh);
01575 
01576        for(int i=0; i<m_Regions[0].size(); ++i)
01577        {
01578         for(int j=0; j<3; ++j)
01579         {
01580          std::set<MeshCore::MeshFacetArray::_TConstIterator>& faceSet = p2fIt[m_Regions[0][i]._aulPoints[j]];
01581 
01582                   for (std::set<MeshCore::MeshFacetArray::_TConstIterator>::const_iterator it = faceSet.begin(); it != faceSet.end(); ++it)
01583          {
01584           if((*it)[0]._ulProp == 0)
01585            m_RegionBounds.push_back((*it)[0]);
01586          }
01587         }
01588        }*/
01589 
01590 
01591 
01592     tmpVec = InitFaceCheck(m_CadMesh, deg_Tol+1);
01593 
01594     cout << " glob: " << tmpVec.size() << endl;
01595 
01596     // ********************************** LOKALE KORREKTUR ****************************************
01597     unsigned long ind;
01598 
01599     MeshCore::MeshPointArray tmpPnts = m_CadMesh.GetPoints();
01600     MeshCore::MeshFacetArray tmpFact = m_CadMesh.GetFacets();
01601 
01602     Base::Builder3D logic;
01603     int cm_ref = 50-cm;
01604 
01605     for (unsigned int i=0; i<RegionSkals.size(); ++i)
01606     {
01607         cm = cm_ref;
01608 
01609         while (true)
01610         {
01611             tmpVec = FaceCheck(m_CadMesh, deg_Tol+1);
01612             cout << "remaining: " << tmpVec.size() << endl;
01613 
01614             if (tmpVec.size() > NumRef || cm == 0)
01615             {
01616                 tmpPnts = m_CadMesh.GetPoints();
01617 
01618                 for (unsigned int k=0; k<tmpVec.size(); ++k)
01619                 {
01620                     for (int l=0; l<3; ++l)
01621                     {
01622                         logic.addSinglePoint(tmpPnts[tmpFact[tmpVec[k]]._aulPoints[l]].x,
01623                                              tmpPnts[tmpFact[tmpVec[k]]._aulPoints[l]].y,
01624                                              tmpPnts[tmpFact[tmpVec[k]]._aulPoints[l]].z,5,0,0,1);
01625                     }
01626                 }
01627 
01628 
01629                 logic.saveToFile("c:/tired.iv");
01630 
01631                 for (unsigned int j=0; j<RegionSkals[i].size(); ++j)
01632                 {
01633                     ind = RegionSkals[i][j].first;
01634                     m_normals[ind].Normalize();
01635                     m_normals[ind].Scale(-normalsRef[ind].Length()/50,
01636                                          -normalsRef[ind].Length()/50,
01637                                          -normalsRef[ind].Length()/50);
01638 
01639                     tmpPnts[ind].Set(tmpPnts[ind].x + m_normals[ind].x,
01640                                      tmpPnts[ind].y + m_normals[ind].y,
01641                                      tmpPnts[ind].z + m_normals[ind].z);
01642 
01643 
01644                                         m_dist_vec[ind] += m_normals[ind];
01645 
01646 
01647                 }
01648 
01649                 m_CadMesh.Assign(tmpPnts, tmpFact);
01650                 break;
01651             }
01652 
01653             tmpPnts = m_CadMesh.GetPoints();
01654 
01655             for (unsigned int j=0; j<RegionSkals[i].size(); ++j)
01656             {
01657                 ind = RegionSkals[i][j].first;
01658 
01659                 m_normals[ind].Normalize();
01660                 m_normals[ind].Scale((normalsRef[ind].Length())/50,
01661                                      (normalsRef[ind].Length())/50,
01662                                      (normalsRef[ind].Length())/50);
01663 
01664                 tmpPnts[ind].Set(tmpPnts[ind].x + m_normals[ind].x,
01665                                  tmpPnts[ind].y + m_normals[ind].y,
01666                                  tmpPnts[ind].z + m_normals[ind].z);
01667 
01668                                 m_dist_vec[ind] += m_normals[ind];
01669             }
01670 
01671             m_CadMesh.Assign(tmpPnts, tmpFact);
01672             --cm;
01673         }
01674     }
01675 
01676     //SmoothMesh(m_CadMesh, 50);
01677         //SmoothMesh(m_CadMesh, 50);
01678         /*SmoothMesh(m_CadMesh, 50);
01679         SmoothMesh(m_CadMesh, 50);
01680         SmoothMesh(m_CadMesh, 50);
01681         SmoothMesh(m_CadMesh, 50);
01682         SmoothMesh(m_CadMesh, 50);
01683         SmoothMesh(m_CadMesh, 50);*/
01684 
01685 //      MeshCore::MeshPointArray mpts = RefMesh23.GetPoints();
01686 
01687         //Base::Builder3D loooo;
01688         //ofstream anOutputFile;
01689         //anOutputFile.open("c:/catia_offset.txt");
01690  //   anOutputFile.precision(7);
01691 
01692         //for(int i=0; i< mpts.size(); ++i)
01693         //{
01694         //      loooo.addSingleArrow(mpts[i], mpts[i] + m_dist_vec[i]);
01695         //      anOutputFile <<  mpts[i].x << " " << mpts[i].y << " " << mpts[i].z << " " << m_dist_vec[i].x << " " << m_dist_vec[i].y << " " << m_dist_vec[i].z << endl;
01696         //}
01697 
01698         //loooo.saveToFile("c:/prpopo.iv");
01699         //  anOutputFile.close();
01700 
01701 
01702 
01703 
01704         //anOutputFile << 
01705         
01706 
01707 
01708 
01709     return true;
01710 }
01711 
01712 std::vector< std::pair<unsigned long, double> > SpringbackCorrection::RegionEvaluate(const MeshCore::MeshKernel &mesh, std::vector<unsigned long> &RegionFacets, std::vector<Base::Vector3f> &normals)
01713 {
01714     std::list< std::vector  <Base::Vector3f> > Borders;
01715     std::list< std::vector  <Base::Vector3f> >::iterator bIt;
01716     std::map <unsigned long, Base::Vector3f> RegionMap;
01717     std::map <unsigned long, Base::Vector3f>::iterator rIt;
01718     std::map <double, unsigned long> DistMap;
01719     std::map <double, unsigned long>::iterator dIt;
01720     std::pair<unsigned long, Base::Vector3f> Ind2Pnt;
01721     std::pair<double, unsigned long> Dist2Ind;
01722 
01723     std::vector< std::pair<double, unsigned long> >  dists;
01724     std::vector< std::pair< unsigned long, double> > skals;
01725 
01726     MeshCore::MeshAlgorithm algo(mesh);  // übergebe mesh
01727     algo.GetFacetBorders(RegionFacets, Borders); // speichert ränder der region in Borders
01728 
01729     MeshCore::MeshFacetArray facets = mesh.GetFacets();
01730     MeshCore::MeshPointArray points = mesh.GetPoints();
01731 
01732     // um doppelte indizes zu vermeiden push die punkte der region in eine map
01733     for (unsigned int i=0; i<RegionFacets.size(); ++i)
01734     {
01735         for (int j=0; j<3; ++j)
01736         {
01737             Ind2Pnt.first  = facets[RegionFacets[i]]._aulPoints[j];
01738             Ind2Pnt.second = points[Ind2Pnt.first];
01739 
01740             RegionMap.insert(Ind2Pnt);
01741         }
01742                 
01743     }
01744 
01745     // berechne distanzen der regionenpunkte zu den rändern
01746 
01747     Base::Vector3f distVec;   // speichert abstandsvektor
01748     double distVal;     // speichert maximalen abstandswert zum rand
01749 
01750     for (rIt = RegionMap.begin(); rIt != RegionMap.end(); ++rIt)
01751     {
01752         distVal = 1e+3;
01753         for (bIt = Borders.begin(); bIt != Borders.end(); ++bIt)
01754         {
01755             for (unsigned int i=0; i< bIt->size(); ++i)
01756             {
01757                 distVec =  (*rIt).second - (*bIt).at(i);
01758 
01759                 if (distVec.Length() < distVal)
01760                     distVal = distVec.Length();
01761             }
01762         }
01763 
01764         Dist2Ind.first   = distVal;
01765         Dist2Ind.second  = (*rIt).first;
01766 
01767         dists.push_back(Dist2Ind);
01768         DistMap.insert(Dist2Ind);
01769     }
01770 
01771     dIt = DistMap.end();
01772     --dIt;
01773     double d_max = (*dIt).first;
01774     std::pair<unsigned long, double> pair;
01775 
01776     for (unsigned int i=0; i<dists.size(); ++i)
01777     {
01778         pair.first  = dists[i].second;
01779 
01780         if (dists[i].first != 0.0)
01781             pair.second = ( (cos( ((dists[i].first * PI) / d_max) - PI) + 1) / 2);  // 0.5 * [ cos( (dist * PI / dist_max) - PI ) + 1 ]
01782         else
01783             pair.second = 0.0;
01784 
01785         skals.push_back(pair);
01786         normals[pair.first].Scale((float) pair.second, (float) pair.second, (float) pair.second);
01787     }
01788 
01789     return skals;
01790 }
01791 
01792 
01793 
01794 bool SpringbackCorrection::FacetRegionGrowing(MeshCore::MeshKernel &mesh,
01795         std::vector<MeshCore::MeshFacet> &FacetRegion,
01796         MeshCore::MeshFacetArray &mFacets)
01797 {
01798     MeshCore::MeshRefFacetToFacets ff_It(mesh);
01799 
01800     MeshCore::MeshFacet facet = FacetRegion.back();
01801     const std::set<unsigned long>& FacetNei = ff_It[facet._ulProp];
01802     MeshCore::MeshFacetArray::_TConstIterator f_beg = mesh.GetFacets().begin();
01803 
01804     std::set<unsigned long>::const_iterator f_it;
01805     for (f_it = FacetNei.begin(); f_it != FacetNei.end(); ++f_it)
01806     {
01807         if (f_beg[*f_it]._ucFlag == MeshCore::MeshFacet::VISIT)
01808         {
01809             FacetRegion.push_back(f_beg[*f_it]);
01810             mFacets[f_beg[*f_it]._ulProp].SetFlag(MeshCore::MeshFacet::INVALID); // markiere als schon zugewiesen
01811             FacetRegionGrowing(mesh, FacetRegion, mFacets);
01812         }
01813     }
01814 
01815     return true;
01816 }
01817 
01818 //
01819 //bool SpringbackCorrection::Init()
01820 //{
01821 // Base::Builder3D log;
01822 // Base::Vector3f  p1,p2;
01823 // double curv;
01824 // // Triangulate Shape (vorläufig)
01825 // best_fit::Tesselate_Shape(m_Shape, m_Mesh, 1);
01826 //
01827 // MeshCurvature(m_Mesh);
01828 //
01829 // std::vector<Base::Vector3f> normals = best_fit::Comp_Normals(m_Mesh);
01830 // MeshCore::MeshPointIterator p_it(m_Mesh);
01831 // int i = 0;
01832 //
01833 // for(p_it.Begin(); p_it.More(); p_it.Next()){
01834 //  normals[i].Scale(m_CurvMax[i], m_CurvMax[i], m_CurvMax[i]);
01835 //  //p1.Set(PointArray[i].x,       PointArray[i].y,       PointArray[i].z);
01836 //  p2.Set(p_it->x + normals[i].x,   p_it->y + normals[i].y,   p_it->z + normals[i].z);
01837 //
01838 //
01839 //  if(m_CurvMax[i] < 100)
01840 //  log.addSingleLine(*p_it, p2, 1 ,0, 0, 0 );
01841 //
01842 //  //cout << p_it->x << ", " << p_it->y << ", " << p_it->z << endl;
01843 //  //cout <<normals[i].x << ", " << normals[i].y << ", " << normals[i].z << endl;
01844 //  //cout << m_CurvMax[i] << endl;
01845 //  ++i;
01846 //
01847 // }
01848 //
01849 // log.saveToFile("c:/servolation.iv");
01850 //}
01851 
01852 // return true;
01853 bool SpringbackCorrection::GetCurvature(TopoDS_Face aFace)
01854 {
01855     Base::Builder3D log;
01856     Handle_Geom_Surface geom_surface;
01857     geom_surface = BRep_Tool::Surface(aFace);
01858     gp_Pnt proPnt;
01859     double u_par,v_par,k1,k2;
01860     gp_Vec D1U,D1V,D2U,D2V,D2UV;
01861     Base::Vector3f p1,p2;
01862 
01863     MeshCore::MeshPointArray PointArray = m_Mesh.GetPoints();
01864     MeshCore::MeshFacetArray FacetArray = m_Mesh.GetFacets();
01865 
01866     MeshCore::MeshPointIterator p_it(m_Mesh);
01867 
01868     m_CurvMax.resize(PointArray.size());
01869 
01870     for (unsigned int i=0; i<PointArray.size(); ++i)
01871     {
01872 
01873         proPnt.SetX(PointArray[i].x);
01874         proPnt.SetY(PointArray[i].y);
01875         proPnt.SetZ(PointArray[i].z);
01876 
01877         GeomAPI_ProjectPointOnSurf aProjection(proPnt,geom_surface);
01878         aProjection.LowerDistanceParameters(u_par,v_par);
01879 
01880         // Berechne Krümmung
01881         geom_surface->D2(u_par,v_par,proPnt,D1U,D1V,D2U,D2V,D2UV);
01882 
01883         // erste & zweite Hauptkrümmung
01884         k1 = D1U.CrossMagnitude(D2U) / D1U.Magnitude();
01885         k2 = D1V.CrossMagnitude(D2V) / D1V.Magnitude();
01886 
01887         /*if(k1>k2) PointArray[i].SetProperty(1/k1);
01888         else      PointArray[i].SetProperty(1/k2);*/
01889 
01890         if (abs(k1)>abs(k2))
01891         {
01892             m_CurvMax[i] = 1/k1;
01893         }
01894         else
01895             if (k2!=0)
01896                 m_CurvMax[i] = 1/k2;
01897             else
01898                 m_CurvMax[i] = 100;
01899 
01900 
01901         D1U.Cross(D1V);
01902         D1U.Normalize();
01903         D1U.Scale(m_CurvMax[i]);
01904 
01905         p1.x = (float) (proPnt.X() + D1U.X());
01906         p1.y = (float) (proPnt.Y() + D1U.Y());
01907         p1.z = (float) (proPnt.Z() + D1U.Z());
01908 
01909         p2.x = (float) proPnt.X();
01910         p2.y = (float) proPnt.Y();
01911         p2.z = (float) proPnt.Z();
01912 
01913         log.addSingleLine(p2, p1, 1 ,0, 0, 0 );
01914     }
01915 
01916 
01917     MeshCore::MeshPointIterator v_it(m_Mesh);
01918     MeshCore::MeshRefPointToPoints vv_it(m_Mesh);
01919     MeshCore::MeshPointArray::_TConstIterator v_beg = m_Mesh.GetPoints().begin();
01920     std::set<unsigned long> PntNei;
01921     std::set<unsigned long> PntNei2;
01922     std::set<unsigned long> PntNei3;
01923     std::set<unsigned long> PntNei4;
01924     std::set<unsigned long>::iterator pnt_it1;
01925     std::set<unsigned long>::iterator pnt_it2;
01926     std::set<unsigned long>::iterator pnt_it3;
01927     std::set<unsigned long>::iterator pnt_it4;
01928     std::vector<unsigned long> nei;
01929     double curv;
01930 
01931     std::vector<double> CurvCop = m_CurvMax;
01932 
01933     for (v_it.Begin(); v_it.More(); v_it.Next())
01934     {
01935         PntNei = vv_it[v_it.Position()];
01936         curv = m_CurvMax[v_it.Position()];
01937 
01938         for (pnt_it1 = PntNei.begin(); pnt_it1 !=PntNei.end(); ++pnt_it1)
01939         {
01940             if (m_CurvMax[v_beg[*pnt_it1]._ulProp] < curv)
01941                 curv = m_CurvMax[v_beg[*pnt_it1]._ulProp];
01942 
01943             PntNei2 = vv_it[v_beg[*pnt_it1]._ulProp];
01944             for (pnt_it2 = PntNei2.begin(); pnt_it2 !=PntNei2.end(); ++pnt_it2)
01945             {
01946                 if (m_CurvMax[v_beg[*pnt_it2]._ulProp] < curv)
01947                     curv = m_CurvMax[v_beg[*pnt_it2]._ulProp];
01948 
01949 
01950                 PntNei3 = vv_it[v_beg[*pnt_it2]._ulProp];
01951                 for (pnt_it3 = PntNei3.begin(); pnt_it3 !=PntNei3.end(); ++pnt_it3)
01952                 {
01953                     if (m_CurvMax[v_beg[*pnt_it3]._ulProp] < curv)
01954                         curv = m_CurvMax[v_beg[*pnt_it3]._ulProp];
01955 
01956                     PntNei4 = vv_it[v_beg[*pnt_it3]._ulProp];
01957                     for (pnt_it4 = PntNei4.begin(); pnt_it4 !=PntNei4.end(); ++pnt_it4)
01958                     {
01959                         if (m_CurvMax[v_beg[*pnt_it4]._ulProp] < curv)
01960                             curv = m_CurvMax[v_beg[*pnt_it4]._ulProp];
01961                     }
01962                 }
01963             }
01964         }
01965 
01966         CurvCop[v_it.Position()] = curv;
01967     }
01968 
01969     m_CurvMax = CurvCop;
01970 
01971 
01972     return true;
01973 }
01974 
01975 
01976 // 1. Computes the curvature of the mesh at the verticies
01977 // 2. Stores the minimum curvatures along the neighbours for all verticies in a vector
01978 std::vector<double> SpringbackCorrection::MeshCurvature(const TopoDS_Face& aFace, const MeshCore::MeshKernel& mesh)
01979 {
01980 
01981         TopLoc_Location aLocation;
01982     Handle_Poly_Triangulation aTr = BRep_Tool::Triangulation(aFace,aLocation);
01983         const TColgp_Array1OfPnt2d& aUVNodes = aTr->UVNodes();
01984         int n = aUVNodes.Length();
01985         BRepAdaptor_Surface aSurface(aFace);
01986     
01987     gp_Pnt2d par;
01988     gp_Pnt P;
01989     gp_Vec D1U, D1V, D2U, D2V, D2UV, nor, xvv, xuv, xuu;
01990         double H,K;   // Gaußsche- und mittlere Krümmung
01991 
01992         std::vector<double> aMaxCurve, aMinCurve;
01993         std::vector<double> curv(2);
01994     
01995         for (int i=0; i<n; ++i)
01996     {
01997         par = aUVNodes.Value(i+1);
01998                 aSurface.D2(par.X(),par.Y(),P,D1U,D1V,D2U,D2V,D2UV);
01999 
02000                 //berechne Hilfsnormale
02001                 nor = D1U;
02002                 nor.Cross(D1V);
02003                 nor.Normalize();
02004 
02005                 xvv = D2V;
02006                 xuv = D2UV;
02007                 xuu = D2U;
02008 
02009                 xvv.Multiply(D1U*D1U);
02010                 xuv.Multiply(2*(D1U*D1V));
02011                 xuu.Multiply(D1V*D1V);
02012 
02013                 H = ((xvv - xuv + xuu)*nor);
02014                 H /= 2*((D1U*D1U)*(D1V*D1V) - (D1U*D1V)*(D1U*D1V));
02015                 K = ((nor*D2U)*(nor*D2V) - (nor*D2UV)*(nor*D2UV))/((D1U*D1U)*(D1V*D1V) - (D1U*D1V)*(D1U*D1V));
02016 
02017 
02018                 aMaxCurve.push_back(-(H - sqrt(H*H - K)));
02019                 aMinCurve.push_back(-(H + sqrt(H*H - K)));
02020 
02021         }
02022                 
02023                 
02024                 
02025         double maxCurv = 0.0;
02026         double avgCurv = 0.0;
02027         double tmp1 =  1e+10;
02028         double tmp2 = -1e+10;
02029 
02030         double mean_curvMax = 0.0, mean_curvMin = 0.0;
02031     int c1 = 0, c2 = 0;
02032 
02033         m_CurvPos.clear();
02034         m_CurvNeg.clear();
02035         m_CurvMax.clear();
02036         m_CurvPos.resize(mesh.CountPoints());
02037         m_CurvNeg.resize(mesh.CountPoints());
02038         m_CurvMax.resize(mesh.CountPoints());
02039 
02040         for ( unsigned long i=0; i<n; i++ )
02041         {
02042                 if (aMaxCurve[i] > 0) m_CurvPos[i] = 1 / aMaxCurve[i];
02043                 else                  m_CurvPos[i] = 1e+10;
02044 
02045                 if (aMinCurve[i] < 0) m_CurvNeg[i] = 1 / aMinCurve[i];
02046                 else                  m_CurvNeg[i] = -1e+10;
02047 
02048                 if (m_CurvPos[i] < tmp1)
02049                         tmp1 = m_CurvPos[i];
02050 
02051                 if (m_CurvNeg[i] > tmp2)
02052                         tmp2 = m_CurvNeg[i];
02053 
02054                 if (aMaxCurve[i] > 0)
02055                 {
02056                         ++c1;
02057                         mean_curvMax += 1/aMaxCurve[i];
02058                 }
02059 
02060                 if (aMinCurve[i] < 0)
02061                 {
02062                         ++c2;
02063                         mean_curvMin += 1/aMinCurve[i];
02064                 }
02065         }
02066 
02067         if (c1==0)
02068                 mean_curvMax = 1e+3;
02069         else
02070                 mean_curvMax /= mesh.CountPoints();
02071 
02072         if (c2==0)
02073                 mean_curvMin = -1e+3;
02074         else
02075                 mean_curvMin /= mesh.CountPoints();
02076 
02077         curv[0] = tmp1;
02078         curv[1] = tmp2;
02079         
02080 
02081         
02082         
02083         //Krümmung auf Netzbasis
02084         
02085         for(int i=0; i<n; ++i)
02086         {
02087 
02088                 // get all points
02089             
02090                 //MeshCore::MeshKernel& rMesh = mesh;
02091                 std::vector< Wm4::Vector3<float> > aPnts;
02092                 std::vector<Base::Vector3f> aPnts_tmp;
02093                 MeshCore::MeshPointIterator cPIt( mesh );
02094                 for ( cPIt.Init(); cPIt.More(); cPIt.Next() )
02095                 {
02096                         Wm4::Vector3<float> cP( cPIt->x, cPIt->y, cPIt->z );
02097                         Base::Vector3f cP_tmp( cPIt->x, cPIt->y, cPIt->z );
02098                         aPnts.push_back( cP );
02099                         aPnts_tmp.push_back(cP_tmp);
02100                 }
02101 
02102                 // get all point connections
02103                 std::vector<int> anIdx;
02104                 const std::vector<MeshCore::MeshFacet>& MeshFacetArray = mesh.GetFacets();
02105                 for ( std::vector<MeshCore::MeshFacet>::const_iterator jt = MeshFacetArray.begin(); jt != MeshFacetArray.end(); ++jt )
02106                 {
02107                         for (int i=0; i<3; i++)
02108                         {
02109                                 anIdx.push_back( (int)jt->_aulPoints[i] );
02110                         }
02111                 }
02112 
02113                 // compute vertex based curvatures
02114                 Wm4::MeshCurvature<float> meshCurv(mesh.CountPoints(), &(aPnts[0]), mesh.CountFacets(), &(anIdx[0]));
02115 
02116                 // get curvature information now
02117                 const Wm4::Vector3<float>* aMaxCurvDir = meshCurv.GetMaxDirections();
02118                 const Wm4::Vector3<float>* aMinCurvDir = meshCurv.GetMinDirections();
02119                 const float* aMaxCurv = meshCurv.GetMaxCurvatures();
02120                 const float* aMinCurv = meshCurv.GetMinCurvatures();
02121 
02122                 double maxCurv = 0.0;
02123                 double avgCurv = 0.0;
02124                 double tmp1 =  1e+10;
02125                 double tmp2 = -1e+10;
02126 
02127                 double mean_curvMax = 0.0, mean_curvMin = 0.0;
02128                 int c1 = 0, c2 = 0;
02129 
02130                 m_CurvPos.clear();
02131                 m_CurvNeg.clear();
02132                 m_CurvMax.clear();
02133                 m_CurvPos.resize(mesh.CountPoints());
02134                 m_CurvNeg.resize(mesh.CountPoints());
02135                 m_CurvMax.resize(mesh.CountPoints());
02136 
02137                 for ( unsigned long i=0; i<mesh.CountPoints(); i++ )
02138                 {
02139 
02140                         if (aMaxCurv[i] > 0) m_CurvPos[i] = 1 / aMaxCurv[i];
02141                         else                 m_CurvPos[i] = 1e+10;
02142 
02143                         if (aMinCurv[i] < 0) m_CurvNeg[i] = 1 / aMinCurv[i];
02144                         else                 m_CurvNeg[i] = -1e+10;
02145 
02146                         if (m_CurvPos[i] < tmp1)
02147                                 tmp1 = m_CurvPos[i];
02148 
02149                         if (m_CurvNeg[i] > tmp2)
02150                                 tmp2 = m_CurvNeg[i];
02151 
02152                         if (aMaxCurv[i] > 0)
02153                         {
02154                                 ++c1;
02155                                 mean_curvMax += 1/aMaxCurv[i];
02156                         }
02157 
02158                         if (aMinCurv[i] < 0)
02159                         {
02160                                 ++c2;
02161                                 mean_curvMin += 1/aMinCurv[i];
02162                         }
02163                 }
02164 
02165                 if (c1==0)
02166                         mean_curvMax = 1e+3;
02167                 else
02168                         mean_curvMax /= mesh.CountPoints();
02169 
02170                 if (c2==0)
02171                         mean_curvMin = -1e+3;
02172                 else
02173                         mean_curvMin /= mesh.CountPoints();
02174 
02175                 curv[0] = tmp1;
02176                 curv[1] = tmp2;
02177         }
02178         
02179 
02180     return curv;
02181 }
02182 
02183 /*
02184 bool SpringbackCorrection::MirrorMesh(std::vector<double> error)
02185 {
02186     // Flags: 0 - not yet checked
02187     //       1 - no correction applied, but neccessary
02188     //        2 - first correction applied and still neccessary
02189     //        3 - done successfully
02190     //        4 - done without success
02191     //        5 - boundary point
02192 
02193     int n = error.size();
02194     m_error = error;
02195 
02196     std::vector<int> InterPnts;
02197     std::vector<unsigned long> InterFace;
02198     std::vector<unsigned long> IFCopy;
02199 
02200     bool boo = true;
02201 
02202     std::vector<unsigned long> ind;
02203     std::vector<unsigned long> copy;
02204 
02205     MeshRef = m_Mesh;
02206     std::vector<Base::Vector3f> NormalsRef = m_normals;
02207     std::vector<Base::Vector3f> NormalOrig = m_normals;
02208 
02209     PointArray = MeshRef.GetPoints();
02210     MeshCore::MeshPointArray PntArray   = m_Mesh.GetPoints();
02211     MeshCore::MeshFacetArray FctArray   = m_Mesh.GetFacets();
02212     MeshCore::MeshFacetArray FacetArray = MeshRef.GetFacets();
02213 
02214     MeshCore::MeshFacetIterator f_it(MeshRef);
02215     MeshCore::MeshPointIterator v_it(MeshRef);
02216     MeshCore::MeshRefPointToPoints vv_it(MeshRef);
02217     MeshCore::MeshRefPointToFacets vf_it(MeshRef);
02218 
02219     std::set<MeshCore::MeshPointArray::_TConstIterator> PntNei;
02220     std::set<MeshCore::MeshFacetArray::_TConstIterator> FacetNei;
02221 
02222     // Iterates in facets in form of MeshGeomFacets
02223     std::set<MeshCore::MeshFacetArray::_TConstIterator>::iterator face_it;
02224     std::set<MeshCore::MeshPointArray::_TConstIterator>::iterator pnt_it;
02225 
02226     std::vector<Base::Vector3f> NeiLoc;
02227     std::vector<Base::Vector3f> NorLoc;
02228     std::vector<Base::Vector3f> TmpPntVec;
02229     std::vector<unsigned long>  TmpIndVec;
02230     std::vector<unsigned long>  nei;
02231 
02232     bool k=true;
02233     int count = 0;
02234 
02235     while (k==true)
02236     {
02237         // ------ Self-Intersection Check + Correction ------------------------------------------------------
02238 
02239         if (count ==2) break;
02240         count++;
02241 
02242         Base::Builder3D log3d;
02243         Base::Builder3D loga;
02244         Base::Vector3f Pnt, NeiPnt, NeiPntNext, NeiPntPrev, TmpPnt, TmpNor;
02245 
02246         std::vector<Base::Vector3f> Neis;
02247         double minScale, tmp;
02248         bool sign;
02249 
02250         ublas::matrix<double> A(3, 3);
02251         ublas::matrix<double> b(1, 3);
02252 
02253         ind.clear();
02254         for (unsigned int i=0; i<m_Mesh.CountPoints(); ++i)
02255             ind.push_back(i);
02256 
02257 
02258         // do correction for selected points ---------------------------------
02259         for (unsigned int j=0; j<ind.size(); ++j)
02260         {
02261             sign = (error[ind[j]] > 0);  // (+) -> true , (-) -> false
02262 
02263             MeshRef.Assign(PointArray, FacetArray);
02264 
02265             v_it.Set(ind[j]);
02266 
02267             Pnt      = *v_it;
02268             PntNei   = vv_it[ind[j]];
02269             FacetNei = vf_it[ind[j]];
02270 
02271             nei.clear();
02272 
02273             // falls boundary-point tue nichts bzw. gehe zum nächsten punkt
02274             if (FacetNei.size() != PntNei.size())
02275             {
02276                 PointArray[ind[j]]._ucFlag = 5;
02277                 m_CurvMax[ind[j]] = abs(error[ind[j]]);   // vorübergehend
02278                 continue;
02279             }
02280 
02281             // ordne nachbarn
02282             ReorderNeighbourList(PntNei,FacetNei,nei,ind[j]);
02283 
02284             NeiLoc.clear();
02285             NorLoc.clear();
02286             NeiLoc.push_back(Pnt);
02287             NorLoc.push_back(NormalsRef[ind[j]]);
02288 
02289             for (unsigned int r=0; r<nei.size(); ++r)
02290             {
02291                 v_it.Set(nei[r]);
02292                 NeiLoc.push_back(*v_it);
02293                 NorLoc.push_back(NormalsRef[nei[r]]);
02294             }
02295 
02296 
02297             if (count == 2)
02298             {
02299                 if (sign ==true) minScale = -(1e+10);
02300                 else      minScale =   1e+10;
02301                 minScale = LocalCorrection(NeiLoc,NorLoc,sign,minScale);
02302 
02303                 if ((abs(0.8*minScale) - abs(error[ind[j]])) > 0)
02304                 {
02305                     PointArray[ind[j]]._ucFlag = 3;
02306                     m_CurvMax[ind[j]] = abs(minScale);
02307                     //cout << "t, ";
02308                     continue;
02309                 }
02310                 else
02311                 {
02312                     PointArray[ind[j]]._ucFlag = 4;
02313                     m_CurvMax[ind[j]] = abs(minScale);
02314                     cout << "f, ";
02315                     continue;
02316                 }
02317             }
02318 
02319 
02320             if (sign ==true) minScale = -(1e+10);
02321             else      minScale =   1e+10;
02322 
02323             minScale = LocalCorrection(NeiLoc,NorLoc,sign,minScale);
02324 
02325             if ((abs(0.8*minScale) - abs(error[ind[j]])) > 0)
02326             {
02327                 PointArray[ind[j]]._ucFlag = 3;
02328                 m_CurvMax[ind[j]] = abs(minScale);
02329                 continue;
02330             }
02331 
02332             //---------------------------- 1. Korrektur -------------------------
02333             tmp = 0;
02334             std::vector<Base::Vector3f> ConvComb = FillConvex(NeiLoc,NorLoc,20);
02335 
02336             for (unsigned int l=0; l<ConvComb.size(); ++l)
02337             {
02338                 if (sign ==true) minScale = -(1e+10);
02339                 else      minScale =   1e+10;
02340 
02341                 NeiLoc[0] = ConvComb[l];
02342                 minScale = LocalCorrection(NeiLoc,NorLoc,sign,minScale);
02343 
02344                 if (abs(minScale) > abs(tmp))
02345                 {
02346                     tmp = minScale;
02347                     TmpPnt = NeiLoc[0];
02348                 }
02349             }
02350 
02351             NeiLoc[0] = PointArray[ind[j]];
02352             cout << "new minScale: " << tmp << endl;
02353 
02354             if ((abs(0.8*tmp) - abs(error[ind[j]])) > 0)
02355             {
02356                 PointArray[ind[j]].Set(TmpPnt.x, TmpPnt.y, TmpPnt.z);
02357                 PointArray[ind[j]]._ucFlag = 3;
02358                 m_CurvMax[ind[j]] = abs(tmp);
02359                 continue;
02360             }
02361             else
02362             {
02363                 PointArray[ind[j]]._ucFlag = 2;
02364                 m_CurvMax[ind[j]] = abs(tmp);
02365             }
02366 
02367             //------------------------------- END -------------------------------
02368 
02369             // update
02370             for (unsigned int r=0; r<nei.size(); ++r)
02371             {
02372                 v_it.Set(nei[r]);
02373                 NeiLoc[r+1] = *v_it;
02374             }
02375 
02376             MeshRef.Assign(PointArray, FacetArray);
02377 
02378             //---------------------------- 2. Korrektur -------------------------
02379             tmp = GlobalCorrection(NeiLoc,NorLoc,sign,ind[j]);
02380 
02381             //MeshRef.Assign(PointArray, FacetArray);
02382             //m_Mesh = MeshRef;
02383             //return true;
02384 
02385             cout << "2: " << tmp << endl;
02386 
02387             if ((abs(0.8*tmp) - abs(error[ind[j]])) > 0)
02388             {
02389                 PointArray[ind[j]].Set(NeiLoc[0].x, NeiLoc[0].y, NeiLoc[0].z);
02390                 NormalsRef[ind[j]].Set(NorLoc[0].x, NorLoc[0].y, NorLoc[0].z);
02391                 PointArray[ind[j]]._ucFlag = 3;
02392                 m_CurvMax[ind[j]] = abs(tmp);
02393                 continue;
02394             }
02395             else
02396             {
02397                 if (m_CurvMax[ind[j]] < abs(tmp))
02398                 {
02399                     m_CurvMax[ind[j]] = abs(tmp);
02400                     PointArray[ind[j]].Set(NeiLoc[0].x, NeiLoc[0].y, NeiLoc[0].z);
02401                     NormalsRef[ind[j]].Set(NorLoc[0].x, NorLoc[0].y, NorLoc[0].z);
02402                 }
02403                 // flag bleibt 2
02404             }
02405 
02406             //------------------------------- END -------------------------------
02407 
02408         } // end for
02409 
02410         cout << "Replace Mesh..." << endl;
02411         PntArray   = PointArray;
02412         m_normals  = NormalsRef;
02413         m_Mesh = MeshRef;
02414 
02415     } // end while
02416 
02417 
02418     double ver, mx=100;
02419     for (unsigned int i=0; i<error.size(); ++i)
02420     {
02421         ver = m_CurvMax[i] / abs(error[i]);
02422         if (ver < mx)
02423             mx = ver;
02424     }
02425 
02426     //cout << "skalierung: " << mx << endl;
02427     //for(int i=0; i<error.size(); ++i)
02428     //error[i] *= mx;
02429 
02430 
02431 
02432     // deform CAD-Mesh with regard to the scaled normals
02433     MeshCore::MeshPointIterator p_it(m_Mesh);
02434     Base::Builder3D log,log2;
02435     std::vector<int> IndexVec,NumVec, NumVecCopy;
02436     int c, lastNum, lastInd;
02437 
02438     // kritische punkte plus anzahl seiner kritischen nachbarn speichern
02439     //ind.clear();
02440     for (unsigned int j=0; j<ind.size(); ++j)
02441     {
02442         if (PointArray[ind[j]]._ucFlag == 3 || PointArray[ind[j]]._ucFlag == 5)
02443         {
02444             //ind.push_back(ind[j]);
02445             continue;
02446         }
02447         else
02448         {
02449             c=0;
02450             PntNei = vv_it[ind[j]];
02451             for (pnt_it = PntNei.begin(); pnt_it != PntNei.end(); ++pnt_it)
02452             {
02453                 if (PointArray[(*pnt_it)->_ulProp]._ucFlag == 4)
02454                     c++;
02455             }
02456 
02457             IndexVec.push_back(ind[j]);
02458             NumVec.push_back(c);
02459         }
02460     }
02461 
02462     // normalenskalierung
02463     for (unsigned int j=0; j<ind.size(); ++j)
02464     {
02465         if ( ( abs(0.8*m_CurvMax[ind[j]]) - abs(error[ind[j]]) ) < 0)
02466         {
02467             if (error[ind[j]] < 0)
02468                 m_normals[ind[j]].Scale((float) (0.8*m_CurvMax[ind[j]]),(float) (0.8*m_CurvMax[ind[j]]),(float) (0.8*m_CurvMax[ind[j]]));
02469             else
02470                 m_normals[ind[j]].Scale((float) (-0.8*m_CurvMax[ind[j]]),(float) (-0.8*m_CurvMax[ind[j]]), (float) (-0.8*m_CurvMax[ind[j]]));
02471         }
02472         else
02473             m_normals[ind[j]].Scale((float) -error[ind[j]],(float)  -error[ind[j]],(float)  -error[ind[j]]);
02474     }
02475 
02476 
02477     for (unsigned int j=0; j<ind.size(); ++j)
02478     {
02479         log.addSingleLine(PointArray[ind[j]],PointArray[ind[j]] + m_normals[ind[j]],2,1,1,1);  // white arrows
02480         PntArray[ind[j]].Set(PntArray[ind[j]].x + m_normals[ind[j]].x,
02481                              PntArray[ind[j]].y + m_normals[ind[j]].y,
02482                              PntArray[ind[j]].z + m_normals[ind[j]].z);
02483     }
02484 
02485     m_Mesh.Assign(PntArray, FctArray);
02486     MeshRef = m_Mesh;
02487     log.saveToFile("c:/deformation.iv");
02488 
02489 
02490     // kritische punkte nach der anzahl seiner kritischen nachbarn sortieren
02491     NumVecCopy = NumVec;
02492     std::vector<int> SortNumVec(NumVec.size(), -1);
02493     std::vector<int> SortIndVec(NumVec.size(), -1);
02494     cout << NumVec.size() << endl;
02495 
02496     //cout << "vor dem sortieren: ";
02497     //for(int j=0; j<NumVec.size(); ++j)
02498     //{
02499     //cout << NumVec[j] << ", ";
02500     //}
02501     //cout << endl;
02502 
02503     for (unsigned int j=0; j<NumVecCopy.size(); ++j)
02504     {
02505 
02506         lastNum = NumVec.back();
02507         lastInd = IndexVec.back();
02508 
02509         NumVec.pop_back();
02510         IndexVec.pop_back();
02511 
02512         c=0;
02513         for (unsigned int i=0; i<NumVecCopy.size(); ++i)
02514         {
02515             if (NumVecCopy[i] < lastNum)
02516                 ++c;
02517         }
02518 
02519         while (SortNumVec[c] == lastNum)
02520             ++c;
02521 
02522         SortNumVec[c] = lastNum;
02523         SortIndVec[c] = lastInd;
02524     }
02525 
02526     //cout << "danach: ";
02527     //for(int j=0; j<SortNumVec.size(); ++j)
02528     //{
02529     //cout << SortNumVec[j] << ", ";
02530     //}
02531     //cout << endl;
02532 
02533     NeiLoc.clear();
02534     NorLoc.clear();
02535 
02536     Base::Builder3D log1;
02537 
02538     MeshCore::MeshKernel mesh;
02539     MeshCore::MeshPoint mpnt;
02540     MeshCore::MeshPointArray mPointArray;
02541     MeshCore::MeshFacetArray mFacetArray;
02542 
02543     float w;
02544 
02545     for (unsigned int j=0; j<SortIndVec.size(); ++j)
02546     {
02547         Base::Vector3f bvec(0.0, 0.0, 0.0);
02548         NeiLoc.clear();
02549         NeiLoc.push_back(bvec);
02550 
02551         PntNei = vv_it[SortIndVec[j]];
02552         FacetNei = vf_it[SortIndVec[j]];
02553         nei.clear();
02554 
02555         // ordne nachbarn
02556         ReorderNeighbourList(PntNei,FacetNei,nei,SortIndVec[j]);
02557 
02558         mPointArray.clear();
02559         for (unsigned int i=0; i<nei.size(); ++i)
02560         {
02561             mpnt = PntArray[nei[i]];
02562             mPointArray.push_back(mpnt);
02564             if (PntArray[nei[i]]._ucFlag != 4)
02565             {
02566                 NeiLoc.push_back(PntArray[nei[i]]);
02567                 log1.addSinglePoint(PntArray[nei[i]].x, PntArray[nei[i]].y, PntArray[nei[i]].z, 2,1,1,1);
02568                 mPointArray.push_back(mpnt);
02569             }
02570             else
02571             {
02572                 bvec += PntArray[nei[i]];
02573                 log1.addSinglePoint(PntArray[nei[i]].x, PntArray[nei[i]].y, PntArray[nei[i]].z, 2,1,0,0);
02574             }
02575         }
02576 
02577         log1.saveToFile("c:/nachbarn.iv");
02578 
02579         mesh.Assign(mPointArray, mFacetArray);
02580 
02581         // need at least one facet to perform the PCA
02582         MeshCore::MeshGeomFacet face;
02583         mesh.AddFacet(face);
02584 
02585         // berechne hauptachsen
02586         MeshCore::MeshEigensystem pca(mesh);
02587         pca.Evaluate();
02588         Base::Matrix4D T1 = pca.Transform();
02589         std::vector<Base::Vector3f> v(3);
02590 
02591         v[0].x = (float) T1[0][0];
02592         v[0].y = (float) T1[0][1];
02593         v[0].z = (float) T1[0][2];
02594         v[1].x = (float) T1[1][0];
02595         v[1].y = (float) T1[1][1];
02596         v[1].z = (float) T1[1][2];
02597         v[2].x = (float) T1[2][0];
02598         v[2].y = (float) T1[2][1];
02599         v[2].z = (float) T1[2][2];
02600 
02601         v[0].Normalize();
02602         v[1].Normalize();
02603         v[2].Normalize();
02604 
02605         double deg,temp=0;
02606         int s;
02607 
02608         for (int i=0; i<3; ++i)
02609         {
02610             deg = v[i]*NormalOrig[SortIndVec[j]];
02611             if (abs(deg)>temp)
02612             {
02613                 temp = abs(deg);
02614                 s=i;
02615             }
02616         }
02617 
02618         if (v[s].z < 0.0)
02619             v[s] *= -1;
02620 
02621         for (unsigned int i=0; i<NeiLoc.size(); ++i)
02622             NorLoc.push_back(v[s]);
02623 
02624         // gewichtung (im verhältnis 1:2)
02625         w = float(1.0) /(float(NeiLoc.size()) + float(PntNei.size()) - float(1.0));
02626         bvec.Scale(w,w,w);
02627 
02628         for (unsigned int i=1; i<NeiLoc.size(); ++i)
02629         {
02630             bvec.x = bvec.x + 2*w*NeiLoc[i].x;
02631             bvec.y = bvec.y + 2*w*NeiLoc[i].y;
02632             bvec.z = bvec.z + 2*w*NeiLoc[i].z;
02633         }
02634 
02635         NeiLoc[0] = bvec;
02636         log1.addSinglePoint(NeiLoc[0],2,0,1,0);
02637         std::vector<Base::Vector3f> Convs = FillConvex(NeiLoc, NorLoc, 20);
02638 
02639         for (unsigned int i=0; i<Convs.size(); ++i)
02640             log1.addSinglePoint(Convs[i],2,0,0,0);
02641 
02642         log1.saveToFile("c:/nachbarn.iv");
02643 
02644         PntArray[SortIndVec[j]].Set(Convs[0].x, Convs[0].y, Convs[0].z);
02645         PntArray[SortIndVec[j]]._ucFlag = 3;
02646 
02647     }
02648 
02649     m_Mesh.Assign(PntArray, FctArray);
02650 
02651     return true;
02652 
02653 
02654     for (p_it.Begin(); p_it.More(); p_it.Next())
02655     {
02656         if (PointArray[p_it.Position()]._ucFlag == 3)
02657         {
02658             log.addSingleLine(*p_it,*p_it + m_normals[p_it.Position()],2,1,1,1);  // red arrows
02659             PntArray[p_it.Position()].Set(PntArray[p_it.Position()].x + m_normals[p_it.Position()].x,
02660                                           PntArray[p_it.Position()].y + m_normals[p_it.Position()].y,
02661                                           PntArray[p_it.Position()].z + m_normals[p_it.Position()].z);
02662         }
02663 
02664         if (PointArray[p_it.Position()]._ucFlag == 4)
02665         {
02666             log.addSingleLine(*p_it,*p_it + m_normals[p_it.Position()],2,1,0,0);  // red arrows
02667             PntArray[p_it.Position()].Set(PntArray[p_it.Position()].x + m_normals[p_it.Position()].x,
02668                                           PntArray[p_it.Position()].y + m_normals[p_it.Position()].y,
02669                                           PntArray[p_it.Position()].z + m_normals[p_it.Position()].z);
02670         }
02671     }
02672 
02673     m_Mesh.Assign(PntArray, FctArray);
02674     log.saveToFile("c:/deformation.iv");
02675 
02676     return true;
02677 }
02678 */
02679 
02680 /*
02681 double SpringbackCorrection::LocalCorrection(std::vector<Base::Vector3f> Neib ,
02682         std::vector<Base::Vector3f> Normal, bool sign, double minScale)
02683 {
02684     Base::Vector3f Pnt, NeiPnt, NeiPntNext, NeiPntPrev;
02685 
02686     ublas::matrix<double> A(3, 3);
02687     ublas::matrix<double> b(1, 3);
02688 
02689     // 2. Spalte bleibt unverändert
02690     A(0,2) = -Normal[0].x;
02691     A(1,2) = -Normal[0].y;
02692     A(2,2) = -Normal[0].z;
02693 
02694     // forward
02695     for (unsigned int k=1; k<Neib.size(); ++k)
02696     {
02697         NeiPnt = Neib[k];
02698 
02699         if ((k+1) == Neib.size())
02700         {
02701             NeiPntNext = Neib[1];
02702         }
02703         else
02704         {
02705             NeiPntNext = Neib[k+1];
02706         }
02707 
02708         A(0,2) = -Normal[0].x;
02709         A(0,0) = Normal[k].x;
02710         A(0,1) = (NeiPntNext.x - NeiPnt.x);
02711         A(1,2) = -Normal[0].y;
02712         A(1,0) = Normal[k].y;
02713         A(1,1) = (NeiPntNext.y - NeiPnt.y);
02714         A(2,2) = -Normal[0].z;
02715         A(2,0) = Normal[k].z;
02716         A(2,1) = (NeiPntNext.z - NeiPnt.z);
02717 
02718         b(0,0) = Neib[0].x - NeiPnt.x;
02719         b(0,1) = Neib[0].y - NeiPnt.y;
02720         b(0,2) = Neib[0].z - NeiPnt.z;
02721 
02722         //cout << "A-Matrix: " << endl;
02723         //cout << A(0,0) << ", " << A(0,1) << ", " << A(0,2) << endl;
02724         //cout << A(1,0) << ", " << A(1,1) << ", " << A(1,2) << endl;
02725         //cout << A(2,0) << ", " << A(2,1) << ", " << A(2,2) << endl;
02726         //cout << endl;
02727 
02728         //cout << "b-Vector: " << endl;
02729         //cout << b(0,0) << ", " << b(0,1) << ", " << b(0,2) << endl;
02730         //cout << endl;
02731 
02732         atlas::gesv(A,b);
02733 
02734         //cout << "solution: " << endl;
02735         //cout << b(0,0) << ", " << b(0,1) << ", " << b(0,2) << endl;
02736         //cout << endl;
02737 
02738         if ((b(0,2)<=0) && (sign == true) && (b(0,1)>=0) && (b(0,1)<=1))
02739         {
02740             if (minScale < b(0,2))
02741                 minScale = b(0,2);
02742         }
02743         else if (b(0,2)>=0 && sign == false && (b(0,1)>=0) && (b(0,1)<=1))
02744         {
02745             if (minScale > b(0,2))
02746                 minScale = b(0,2);
02747         }
02748     }
02749 
02750     // backward
02751     for (unsigned int k=1; k<Neib.size(); ++k)
02752     {
02753         NeiPnt = Neib[k];
02754 
02755         if (k == 1)
02756         {
02757             NeiPntPrev = Neib[Neib.size()-1];
02758         }
02759         else
02760         {
02761             NeiPntPrev = Neib[k-1];
02762         }
02763 
02764         A(0,2) = -Normal[0].x;
02765         A(0,0) = Normal[k].x;
02766         A(0,1) = (NeiPntPrev.x - NeiPnt.x);
02767         A(1,2) = -Normal[0].y;
02768         A(1,0) = Normal[k].y;
02769         A(1,1) = (NeiPntPrev.y - NeiPnt.y);
02770         A(2,2) = -Normal[0].z;
02771         A(2,0) = Normal[k].z;
02772         A(2,1) = (NeiPntPrev.z - NeiPnt.z);
02773 
02774         b(0,0) = Neib[0].x - NeiPnt.x;
02775         b(0,1) = Neib[0].y - NeiPnt.y;
02776         b(0,2) = Neib[0].z - NeiPnt.z;
02777 
02778         //cout << "A-Matrix: " << endl;
02779         //cout << A(0,0) << ", " << A(0,1) << ", " << A(0,2) << endl;
02780         //cout << A(1,0) << ", " << A(1,1) << ", " << A(1,2) << endl;
02781         //cout << A(2,0) << ", " << A(2,1) << ", " << A(2,2) << endl;
02782         //cout << endl;
02783 
02784         //cout << "b-Vector: " << endl;
02785         //cout << b(0,0) << ", " << b(0,1) << ", " << b(0,2) << endl;
02786         //cout << endl;
02787 
02788         atlas::gesv(A,b);
02789 
02790         //cout << "solution: " << endl;
02791         //cout << b(0,0) << ", " << b(0,1) << ", " << b(0,2) << endl;
02792         //cout << endl;
02793 
02794         if (b(0,2)<0 && sign == true && (b(0,1)>=0) && (b(0,1)<=1))
02795         {
02796             if (minScale < b(0,2))
02797                 minScale = b(0,2);
02798         }
02799         else if (b(0,2)>0 && sign == false && (b(0,1)>=0) && (b(0,1)<=1))
02800         {
02801             if (minScale > b(0,2))
02802                 minScale = b(0,2);
02803         }
02804     }
02805 
02806     return minScale;
02807 }
02808 */
02809 
02810 /*
02811 std::vector<Base::Vector3f> SpringbackCorrection::FillConvex(std::vector<Base::Vector3f> Neib, std::vector<Base::Vector3f> Normals, int n)
02812 {
02813     std::vector<Base::Vector3f> ConvComb;
02814     Base::Vector3f tmp;
02815     for (unsigned int i=1; i<Neib.size(); ++i)
02816     {
02817         for (int j=0; j<n; ++j)
02818         {
02819             //cout << "t, " ;
02820             tmp = Neib[i] - Neib[0];
02821             tmp.Scale(float(j)/float(n), float(j)/float(n), float(j)/float(n));
02822             if (InsideCheck(Neib[0] + tmp, Normals[0], Neib) == true)
02823                 ConvComb.push_back(Neib[0] + tmp);
02824             else;
02825             //cout << "f, " ;
02826         }
02827     }
02828     cout << endl;
02829     return ConvComb;
02830 }
02831 */
02832 
02833 /*
02834 bool SpringbackCorrection::InsideCheck(Base::Vector3f pnt, Base::Vector3f normal, std::vector<Base::Vector3f> Neib)
02835 {
02836     Base::Vector3f Pnt, NeiPnt, NeiPntNext, Cross;
02837 
02838     ublas::matrix<double> A(3, 3);
02839     ublas::matrix<double> b(1, 3);
02840 
02841     bool b1 = true, b2;
02842 
02843     for (unsigned int k=1; k<Neib.size(); ++k)
02844     {
02845         NeiPnt = Neib[k];
02846 
02847         if ((k+1) == Neib.size())
02848         {
02849             NeiPntNext = Neib[1];
02850         }
02851         else
02852         {
02853             NeiPntNext = Neib[k+1];
02854         }
02855 
02856         Cross = normal%(NeiPntNext - NeiPnt);
02857 
02858         A(0,0) = normal.x;
02859         A(0,1) = (NeiPntNext.x - NeiPnt.x);
02860         A(0,2) = -Cross.x;
02861         A(1,0) = normal.y;
02862         A(1,1) = (NeiPntNext.y - NeiPnt.y);
02863         A(1,2) = -Cross.y;
02864         A(2,0) = normal.z;
02865         A(2,1) = (NeiPntNext.z - NeiPnt.z);
02866         A(2,2) = -Cross.z;
02867 
02868         b(0,0) = pnt.x - NeiPnt.x;
02869         b(0,1) = pnt.y - NeiPnt.y;
02870         b(0,2) = pnt.z - NeiPnt.z;
02871 
02872         atlas::gesv(A,b);
02873 
02874         if (b1==true)
02875         {
02876             if ( b(0,2) < 0)
02877             {
02878                 b1 = false;
02879                 b2 = true;
02880             }
02881             else if (b(0,2)>0)
02882             {
02883                 b1 = false;
02884                 b2 = false;
02885             }
02886         }
02887 
02888         if (b1==false)
02889         {
02890             if ((b2 == true)  && (b(0,2) > 0)) return false;
02891             if ((b2 == false) && (b(0,2) < 0)) return false;
02892         }
02893     }
02894     return true;
02895 }
02896 */
02897 
02898 /*
02899 double SpringbackCorrection::GlobalCorrection(std::vector<Base::Vector3f> NeiLoc, std::vector<Base::Vector3f> NorLoc,
02900         bool sign, int ind)
02901 {
02902     double minScale,mScRef,tmp = 0;
02903     Base::Vector3f TmpPnt, TmpNor;
02904 
02905     if (sign ==true) mScRef = -(1e+10);
02906     else      mScRef =   1e+10;
02907 
02908     minScale = mScRef;
02909     minScale = LocalCorrection(NeiLoc,NorLoc,sign,minScale);
02910 
02911     if ((abs(0.8*minScale) - abs(m_error[ind])) > 0)
02912     {
02913         PointArray[ind]._ucFlag = 3;
02914         m_CurvMax[ind] = abs(minScale);
02915         return minScale;
02916     }
02917 
02918     std::vector<Base::Vector3f> ConvComb = FillConvex(NeiLoc,NorLoc,20);
02919 
02920     // füge schwerpunkte der umgebenden faces hinzu
02921     std::set<MeshCore::MeshFacetArray::_TConstIterator> FacetNei;
02922     std::set<MeshCore::MeshFacetArray::_TConstIterator>::iterator face_it;
02923     MeshCore::MeshFacetIterator    f_it (MeshRef);
02924     MeshCore::MeshRefPointToFacets vf_it(MeshRef);
02925 
02926     FacetNei = vf_it[ind];
02927 
02928     for (face_it = FacetNei.begin(); face_it != FacetNei.end(); ++face_it)
02929     {
02930         TmpPnt.Set(0.0, 0.0, 0.0);
02931         TmpPnt += PointArray[(*face_it)->_aulPoints[0]];
02932         TmpPnt += PointArray[(*face_it)->_aulPoints[1]];
02933         TmpPnt += PointArray[(*face_it)->_aulPoints[2]];
02934         TmpPnt.Scale((float) (1.0/3.0),(float) (1.0/3.0),(float) (1.0/3.0));
02935         ConvComb.push_back(TmpPnt);
02936     }
02937 
02938     //Base::Builder3D logg;
02939     //for(int i=0; i<NeiLoc.size(); ++i)
02940     // logg.addSinglePoint(NeiLoc[i], 2,1,0,0);
02941 
02942     //for(int i=0; i<ConvComb.size(); ++i)
02943     // logg.addSinglePoint(ConvComb[i]);
02944 
02945     //logg.saveToFile("c:/convex.iv");
02946 
02947     for (unsigned int i=0; i<ConvComb.size(); ++i)
02948     {
02949         for (unsigned int j=1; j<NorLoc.size()-1; ++j)
02950         {
02951 
02952             minScale  = mScRef;
02953             NeiLoc[0] = ConvComb[i];
02954             NorLoc[0] = NorLoc[j];
02955             minScale  = LocalCorrection(NeiLoc,NorLoc,sign,minScale);
02956 
02957 
02958             if (minScale > tmp)
02959             {
02960                 tmp = minScale;
02961                 TmpPnt = NeiLoc[0];
02962                 TmpNor = NorLoc[0];
02963             }
02964         }
02965     }
02966 
02967     NeiLoc[0] = TmpPnt;
02968     NorLoc[0] = TmpNor;
02969 
02970     if ((abs(0.8*tmp) - abs(m_error[ind])) > 0)
02971     {
02972         PointArray[ind]._ucFlag = 3;
02973         m_CurvMax[ind] = abs(tmp);
02974     }
02975     else
02976     {
02977         PointArray[ind]._ucFlag = 4;
02978         m_CurvMax[ind] = abs(tmp);
02979     }
02980 
02981     return tmp;
02982 }
02983 /*
02984 
02992 /*
02993 void SpringbackCorrection::ReorderNeighbourList(std::set<MeshCore::MeshPointArray::_TConstIterator> &pnt,
02994         std::set<MeshCore::MeshFacetArray::_TConstIterator> &face, std::vector<unsigned long> &nei, unsigned long CurInd)
02995 {
02996     std::set<MeshCore::MeshPointArray::_TConstIterator>::iterator pnt_it;
02997     std::set<MeshCore::MeshFacetArray::_TConstIterator>::iterator face_it;
02998     std::vector<unsigned long>::iterator vec_it;
02999     std::vector<unsigned long>::iterator ulong_it;
03000     unsigned long PrevIndex;
03001 
03002     if (face.size() != pnt.size())  //this means boundary point
03003     {
03004         for (pnt_it = pnt.begin(); pnt_it != pnt.end(); ++pnt_it)
03005         {
03006             int c = 0;
03007             for (face_it = face.begin(); face_it != face.end(); ++face_it)
03008             {
03009                 if ((*pnt_it)[0]._ulProp == (*face_it)[0]._aulPoints[0])
03010                     ++c;
03011                 if ((*pnt_it)[0]._ulProp == (*face_it)[0]._aulPoints[1])
03012                     ++c;
03013                 if ((*pnt_it)[0]._ulProp == (*face_it)[0]._aulPoints[2])
03014                     ++c;
03015             }
03016 
03017             if (c==1)
03018                 break;
03019         }
03020     }
03021     else
03022         pnt_it = pnt.begin();
03023 
03024 
03025 
03026     face_it = face.begin();
03027 
03028     nei.push_back((*pnt_it)->_ulProp);  //push back first neighbour
03029     vec_it = nei.begin();
03030     PrevIndex = nei[0];  //Initialize PrevIndex
03031     for (unsigned int i = 1; i < pnt.size(); i++) //Start
03032     {
03033         while (true)
03034         {
03035             std::vector<unsigned long> facetpnt;
03036             facetpnt.push_back((*face_it)->_aulPoints[0]); //push back into a vector for easier iteration
03037             facetpnt.push_back((*face_it)->_aulPoints[1]);
03038             facetpnt.push_back((*face_it)->_aulPoints[2]);
03039             if ((ulong_it = find(facetpnt.begin(), facetpnt.end(), PrevIndex)) == facetpnt.end())  //if PrevIndex not found
03040             {
03041                 //in current facet
03042                 if (face_it != face.end())
03043                     ++face_it; //next face please
03044                 else
03045                     break;
03046 
03047                 continue;
03048             }
03049             else
03050             {
03051                 for (unsigned int k = 0 ; k < 3; k++)
03052                 {
03053                     //If current facetpnt[k] is not yet in nei_list AND it is not the CurIndex
03054                     if (((vec_it = find(nei.begin(), nei.end(), facetpnt[k])) == nei.end()) && facetpnt[k] != CurInd)
03055                     {
03056                         face.erase(face_it);   //erase this face
03057                         nei.push_back(facetpnt[k]);  //push back the index
03058                         PrevIndex = facetpnt[k];   //this index is now PrevIndex
03059                         face_it = face.begin(); //reassign the iterator
03060                         break;   //end this for-loop
03061                     }
03062                 }
03063                 break;  //end this while loop
03064             }
03065         }
03066     }
03067 }
03068 */
03069 
03070 
03071 bool SpringbackCorrection::Visit(const MeshCore::MeshFacet &rclFacet, const MeshCore::MeshFacet &rclFrom, unsigned long ulFInd, unsigned long ulLevel)
03072 {
03073     if (ulLevel != m_RingCurrent)
03074     {
03075         ++m_RingCurrent;
03076 
03077         if (m_RingVector.size() == 0)
03078         {
03079             return false;
03080         }
03081         //else if(m_RingVector.size() < 2 && m_RingCurrent > 2)
03082         //{
03083         // m_RingVector.clear();
03084         // return false;
03085         //}
03086         else
03087         {
03088             m_RegionVector.push_back(m_RingVector);
03089             m_RingVector.clear();
03090         }
03091     }
03092 
03093     if (rclFacet._ulProp == 1 && rclFrom._ulProp == 1)
03094         m_RingVector.push_back(ulFInd);
03095 
03096     return true;
03097 }

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