Mod/Mesh/App/Core/Tools.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2005 Imetric 3D GmbH                                    *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #ifndef MESH_TOOLS_H
00025 #define MESH_TOOLS_H
00026 
00027 #include <functional>
00028 
00029 #include <Mod/Mesh/App/WildMagic4/Wm4DistVector3Triangle3.h>
00030 #include <Mod/Mesh/App/WildMagic4/Wm4Sphere3.h>
00031 #include <Mod/Mesh/App/WildMagic4/Wm4Triangle3.h>
00032 
00033 #include "MeshKernel.h"
00034 #include "Algorithm.h"
00035 #include "Iterator.h"
00036 
00037 namespace MeshCore {
00038 
00043 class MeshSearchNeighbours 
00044 {
00045 public:
00046   MeshSearchNeighbours ( const MeshKernel &rclM, float fSampleDistance = 1.0f);
00047   virtual ~MeshSearchNeighbours () {}
00049   void Reinit (float fSampleDistance);
00054   unsigned long  NeighboursFromFacet (unsigned long ulFacetIdx, float fDistance, unsigned long ulMinPoints, std::vector<Base::Vector3f> &raclResultPoints);
00056   unsigned long  NeighboursFromSampledFacets (unsigned long ulFacetIdx, float fDistance, std::vector<Base::Vector3f> &raclResultPoints);
00058   unsigned long  NeighboursFacetFromFacet (unsigned long ulFacetIdx, float fDistance, std::vector<Base::Vector3f> &raclResultPoints,
00059                                            std::vector<unsigned long> &raclResultFacets);
00060 
00061 protected:
00063   void SampleAllFacets (void);
00064   inline bool CheckDistToFacet (const MeshFacet &rclF);     // check distance to facet, add points inner radius
00065   bool AccumulateNeighbours (const MeshFacet &rclF, unsigned long ulFIdx); // accumulate the sample neighbours facet
00066   inline bool InnerPoint (const Base::Vector3f &rclPt) const;
00067   inline bool TriangleCutsSphere (const MeshFacet &rclF) const;
00068   bool ExpandRadius (unsigned long ulMinPoints);
00069 
00070   struct CDistRad : public std::binary_function<const Base::Vector3f&, const Base::Vector3f&, bool>
00071   {
00072     CDistRad (const Base::Vector3f clCenter) : _clCenter(clCenter) {}
00073     bool operator()(const Base::Vector3f &rclPt1, const Base::Vector3f &rclPt2) { return Base::DistanceP2(_clCenter, rclPt1) < Base::DistanceP2(_clCenter, rclPt2); }
00074     Base::Vector3f  _clCenter;
00075   };
00076 
00077 protected:
00078   const MeshKernel  &_rclMesh;
00079   const MeshFacetArray &_rclFAry;
00080   const MeshPointArray &_rclPAry;
00081   MeshRefPointToFacets _clPt2Fa;
00082   float _fMaxDistanceP2;   // square distance 
00083   Base::Vector3f _clCenter;         // center points of start facet
00084   std::set<unsigned long> _aclResult;        // result container (point indices)
00085   std::set<unsigned long> _aclOuter;         // next searching points
00086   std::vector<Base::Vector3f> _aclPointsResult;  // result as vertex
00087   std::vector<std::vector<Base::Vector3f> > _aclSampledFacets; // sample points from each facet
00088   float _fSampleDistance;  // distance between two sampled points
00089   Wm4::Sphere3<float> _akSphere;
00090   bool _bTooFewPoints;    
00091 
00092 private:
00093   MeshSearchNeighbours (const MeshSearchNeighbours&);
00094   void operator = (const MeshSearchNeighbours&);
00095 };
00096 
00097 inline bool MeshSearchNeighbours::CheckDistToFacet (const MeshFacet &rclF)
00098 {
00099   bool bFound = false;
00100 
00101   for (int i = 0; i < 3; i++)
00102   {
00103     unsigned long ulPIdx = rclF._aulPoints[i];
00104     if (_rclPAry[ulPIdx].IsFlag(MeshPoint::MARKED) == false)
00105     {
00106       if (Base::DistanceP2(_clCenter, _rclPAry[ulPIdx]) < _fMaxDistanceP2)
00107       {
00108         bFound = true;
00109         {
00110           _aclResult.insert(ulPIdx);
00111           _rclPAry[ulPIdx].SetFlag(MeshPoint::MARKED);
00112         }
00113       }
00114       _aclOuter.insert(ulPIdx);
00115     }
00116   }
00117 
00118   return bFound;
00119 }
00120 
00121 inline bool MeshSearchNeighbours::InnerPoint (const Base::Vector3f &rclPt) const
00122 {
00123   return Base::DistanceP2(_clCenter, rclPt) < _fMaxDistanceP2;
00124 }
00125 
00126 inline bool MeshSearchNeighbours::TriangleCutsSphere (const MeshFacet &rclF) const
00127 {
00128   Base::Vector3f cP0 = _rclPAry[rclF._aulPoints[0]];
00129   Base::Vector3f cP1 = _rclPAry[rclF._aulPoints[1]];
00130   Base::Vector3f cP2 = _rclPAry[rclF._aulPoints[2]];
00131 
00132   Wm4::Vector3<float> akP0(cP0.x, cP0.y, cP0.z);
00133   Wm4::Vector3<float> akP1(cP1.x, cP1.y, cP1.z);
00134   Wm4::Vector3<float> akP2(cP2.x, cP2.y, cP2.z);
00135 
00136   Wm4::Triangle3<float> akTri(akP0, akP1, akP2);
00137   Wm4::DistVector3Triangle3<float> akDistVecTri(_akSphere.Center, akTri);
00138 
00139   float fSqrDist = akDistVecTri.GetSquared();
00140   float fRSqr = _akSphere.Radius*_akSphere.Radius;
00141   return fSqrDist < fRSqr;
00142 }
00143 
00144 class MeshFaceIterator : public std::unary_function<unsigned long, Base::Vector3f>
00145 {
00146 public:
00147     MeshFaceIterator(const MeshKernel& mesh)
00148         : it(mesh) {}
00149     Base::Vector3f operator() (unsigned long index)
00150     {
00151         it.Set(index);
00152         return it->GetGravityPoint();
00153     }
00154 
00155 private:
00156     MeshFacetIterator it;
00157 };
00158 
00159 class MeshVertexIterator : public std::unary_function<unsigned long, Base::Vector3f>
00160 {
00161 public:
00162     MeshVertexIterator(const MeshKernel& mesh)
00163         : it(mesh) {}
00164     Base::Vector3f operator() (unsigned long index)
00165     {
00166         it.Set(index);
00167         return *it;
00168     }
00169 
00170 private:
00171     MeshPointIterator it;
00172 };
00173 
00174 template <class T>
00175 class MeshNearestIndexToPlane : public std::unary_function<unsigned long, void>
00176 {
00177 public:
00178     MeshNearestIndexToPlane(const MeshKernel& mesh, const Base::Vector3f& b, const Base::Vector3f& n)
00179         : nearest_index(ULONG_MAX),nearest_dist(FLOAT_MAX), it(mesh), base(b), normal(n) {}
00180     void operator() (unsigned long index)
00181     {
00182         float dist = (float)fabs(it(index).DistanceToPlane(base, normal));
00183         if (dist < nearest_dist) {
00184             nearest_dist = dist;
00185             nearest_index = index;
00186         }
00187     }
00188 
00189     unsigned long nearest_index;
00190     float nearest_dist;
00191 
00192 private:
00193     T it;
00194     Base::Vector3f base, normal;
00195 };
00196 
00197 } // namespace MeshCore
00198 
00199 
00200 #endif  // MESH_TOOLS_H
00201 

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