00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef MESH_KERNEL_H
00025 #define MESH_KERNEL_H
00026
00027 #include <assert.h>
00028 #include <iostream>
00029
00030 #include "Elements.h"
00031 #include "Helpers.h"
00032
00033 #include <Base/BoundBox.h>
00034 #include <Base/Vector3D.h>
00035 #include <Base/Matrix.h>
00036
00037 namespace Base{
00038 class Polygon2D;
00039 class ViewProjMethod;
00040 }
00041
00042 namespace MeshCore {
00043
00044
00045 class MeshFacetIterator;
00046 class MeshPointIterator;
00047 class MeshGeomFacet;
00048 class MeshFacet;
00049 class MeshFacetVisitor;
00050 class MeshPointVisitor;
00051 class MeshFacetGrid;
00052
00053
00064 class MeshExport MeshKernel
00065 {
00066 public:
00068 MeshKernel (void);
00070 MeshKernel (const MeshKernel &rclMesh);
00072 ~MeshKernel (void)
00073 { Clear(); }
00074
00077
00078 void Write (std::ostream &rclOut) const;
00079 void Read (std::istream &rclIn);
00081
00084
00085 unsigned long CountFacets (void) const
00086 { return (unsigned long)(_aclFacetArray.size()); }
00088 unsigned long CountEdges (void) const;
00089
00090 unsigned long CountPoints (void) const
00091 { return (unsigned long)(_aclPointArray.size()); }
00093 unsigned int GetMemSize (void) const
00094 { return _aclPointArray.size() * sizeof(MeshPoint) +
00095 _aclFacetArray.size() * sizeof(MeshFacet); }
00097 const Base::BoundBox3f& GetBoundBox (void) const
00098 { return _clBoundBox; }
00099
00103 void RecalcBoundBox (void);
00104
00109 inline MeshPoint GetPoint (unsigned long ulIndex) const;
00110
00114 std::vector<Base::Vector3f> CalcVertexNormals() const;
00115
00120 inline MeshGeomFacet GetFacet (unsigned long ulIndex) const;
00121 inline MeshGeomFacet GetFacet (const MeshFacet &rclFacet) const;
00122
00124 inline void GetFacetPoints (unsigned long ulFaIndex, unsigned long &rclP0,
00125 unsigned long &rclP1, unsigned long &rclP2) const;
00127 std::vector<unsigned long> GetFacetPoints(const std::vector<unsigned long>&) const;
00129 inline void GetFacetNeighbours (unsigned long ulIndex, unsigned long &rulNIdx0,
00130 unsigned long &rulNIdx1, unsigned long &rulNIdx2) const;
00131
00135 std::vector<unsigned long> HasFacets (const MeshPointIterator &rclIter) const;
00136
00138 bool IsValid (void) const
00139 { return _bValid; }
00140
00142 const MeshPointArray& GetPoints (void) const { return _aclPointArray; }
00143
00145 const MeshFacetArray& GetFacets (void) const { return _aclFacetArray; }
00149 MeshFacetArray GetFacets(const std::vector<unsigned long>&) const;
00150
00155 void GetEdges (std::vector<MeshGeomEdge>&) const;
00157
00161 float GetSurface() const;
00163 float GetSurface( const std::vector<unsigned long>& aSegment ) const;
00167 float GetVolume () const;
00169 bool HasOpenEdges() const;
00173 bool HasNonManifolds() const;
00175 bool HasSelfIntersections() const;
00177
00200 unsigned long VisitNeighbourFacets (MeshFacetVisitor &rclFVisitor, unsigned long ulStartFacet) const;
00205 unsigned long VisitNeighbourFacetsOverCorners (MeshFacetVisitor &rclFVisitor, unsigned long ulStartFacet) const;
00207
00227 unsigned long VisitNeighbourPoints (MeshPointVisitor &rclPVisitor, unsigned long ulStartPoint) const;
00229
00250 MeshFacetIterator FacetIterator() const;
00252 MeshPointIterator PointIterator() const;
00254
00260 MeshKernel& operator += (const MeshGeomFacet &rclSFacet);
00264 void AddFacet(const MeshGeomFacet &rclSFacet);
00268 MeshKernel& operator += (const std::vector<MeshGeomFacet> &rclFAry);
00272 void AddFacets(const std::vector<MeshGeomFacet> &rclFAry);
00281 unsigned long AddFacets(const std::vector<MeshFacet> &rclFAry);
00296 unsigned long AddFacets(const std::vector<MeshFacet> &rclFAry,
00297 const std::vector<Base::Vector3f>& rclPAry);
00307 void Merge(const MeshKernel& rKernel);
00314 void Merge(const MeshPointArray& rPoints, const MeshFacetArray& rFaces);
00325 bool DeleteFacet (const MeshFacetIterator &rclIter);
00329 bool DeleteFacet (unsigned long ulInd);
00336 void DeleteFacets (const std::vector<unsigned long> &raulFacets);
00345 bool DeletePoint (const MeshPointIterator &rclIter);
00349 bool DeletePoint (unsigned long ulInd);
00353 void DeletePoints (const std::vector<unsigned long> &raulPoints);
00355 void RebuildNeighbours (void);
00357 void Clear (void);
00361 MeshKernel& operator = (const std::vector<MeshGeomFacet> &rclFAry);
00363 MeshKernel& operator = (const MeshKernel &rclMesh);
00367 void Assign(const MeshPointArray& rPoints, const MeshFacetArray& rFaces, bool checkNeighbourHood=false);
00373 void Adopt(MeshPointArray& rPoints, MeshFacetArray& rFaces, bool checkNeighbourHood=false);
00375 void Swap(MeshKernel& mesh);
00377 void operator *= (const Base::Matrix4D &rclMat);
00381 void Transform (const Base::Matrix4D &rclMat);
00383 inline void MovePoint (unsigned long ulPtIndex, const Base::Vector3f &rclTrans);
00385 inline void SetPoint (unsigned long ulPtIndex, const Base::Vector3f &rPoint);
00387 inline void SetPoint (unsigned long ulPtIndex, float x, float y, float z);
00389 void Smooth(int iterations, float d_max);
00395 void CutFacets (const MeshFacetGrid& rclGrid, const Base::ViewProjMethod *pclP, const Base::Polygon2D& rclPoly,
00396 bool bCutInner, std::vector<MeshGeomFacet> &raclFacets);
00401 void CutFacets (const MeshFacetGrid& rclGrid, const Base::ViewProjMethod* pclP, const Base::Polygon2D& rclPoly,
00402 bool bCutInner, std::vector<unsigned long> &raclCutted);
00404
00405 protected:
00407 void RebuildNeighbours (unsigned long);
00409 void RemoveInvalids ();
00416 void ErasePoint (unsigned long ulIndex, unsigned long ulFacetIndex, bool bOnlySetInvalid = false);
00417
00419 inline void AdjustNormal (MeshFacet &rclFacet, const Base::Vector3f &rclNormal);
00421 inline Base::Vector3f GetNormal (const MeshFacet &rclFacet) const;
00423 inline Base::Vector3f GetGravityPoint (const MeshFacet &rclFacet) const;
00424
00425 MeshPointArray _aclPointArray;
00426 MeshFacetArray _aclFacetArray;
00427 Base::BoundBox3f _clBoundBox;
00428 bool _bValid;
00430
00431 friend class MeshPointIterator;
00432 friend class MeshFacetIterator;
00433 friend class MeshFastFacetIterator;
00434 friend class MeshAlgorithm;
00435 friend class MeshTopoAlgorithm;
00436 friend class MeshFixNeighbourhood;
00437 friend class MeshFixDegenerations;
00438 friend class MeshFixSingleFacet;
00439 friend class MeshFixInvalids;
00440 friend class MeshFixDegeneratedFacets;
00441 friend class MeshFixDuplicatePoints;
00442 friend class MeshBuilder;
00443 };
00444
00445 inline MeshPoint MeshKernel::GetPoint (unsigned long ulIndex) const
00446 {
00447 assert(ulIndex < _aclPointArray.size());
00448 return _aclPointArray[ulIndex];
00449 }
00450
00451 inline MeshGeomFacet MeshKernel::GetFacet (unsigned long ulIndex) const
00452 {
00453 assert(ulIndex < _aclFacetArray.size());
00454
00455 const MeshFacet *pclF = &_aclFacetArray[ulIndex];
00456 MeshGeomFacet clFacet;
00457
00458 clFacet._aclPoints[0] = _aclPointArray[pclF->_aulPoints[0]];
00459 clFacet._aclPoints[1] = _aclPointArray[pclF->_aulPoints[1]];
00460 clFacet._aclPoints[2] = _aclPointArray[pclF->_aulPoints[2]];
00461 clFacet._ulProp = pclF->_ulProp;
00462 clFacet._ucFlag = pclF->_ucFlag;
00463 clFacet.CalcNormal();
00464 return clFacet;
00465 }
00466
00467 inline MeshGeomFacet MeshKernel::GetFacet (const MeshFacet &rclFacet) const
00468 {
00469 assert(rclFacet._aulPoints[0] < _aclPointArray.size());
00470 assert(rclFacet._aulPoints[1] < _aclPointArray.size());
00471 assert(rclFacet._aulPoints[2] < _aclPointArray.size());
00472
00473 MeshGeomFacet clFacet;
00474 clFacet._aclPoints[0] = _aclPointArray[rclFacet._aulPoints[0]];
00475 clFacet._aclPoints[1] = _aclPointArray[rclFacet._aulPoints[1]];
00476 clFacet._aclPoints[2] = _aclPointArray[rclFacet._aulPoints[2]];
00477 clFacet._ulProp = rclFacet._ulProp;
00478 clFacet._ucFlag = rclFacet._ucFlag;
00479 clFacet.CalcNormal();
00480 return clFacet;
00481 }
00482
00483 inline void MeshKernel::GetFacetNeighbours (unsigned long ulIndex, unsigned long &rulNIdx0,
00484 unsigned long &rulNIdx1, unsigned long &rulNIdx2) const
00485 {
00486 assert(ulIndex < _aclFacetArray.size());
00487
00488 rulNIdx0 = _aclFacetArray[ulIndex]._aulNeighbours[0];
00489 rulNIdx1 = _aclFacetArray[ulIndex]._aulNeighbours[1];
00490 rulNIdx2 = _aclFacetArray[ulIndex]._aulNeighbours[2];
00491 }
00492
00493 inline void MeshKernel::MovePoint (unsigned long ulPtIndex, const Base::Vector3f &rclTrans)
00494 {
00495 _aclPointArray[ulPtIndex] += rclTrans;
00496 }
00497
00498 inline void MeshKernel::SetPoint (unsigned long ulPtIndex, const Base::Vector3f &rPoint)
00499 {
00500 _aclPointArray[ulPtIndex] = rPoint;
00501 }
00502
00503 inline void MeshKernel::SetPoint (unsigned long ulPtIndex, float x, float y, float z)
00504 {
00505 _aclPointArray[ulPtIndex].Set(x,y,z);
00506 }
00507
00508 inline void MeshKernel::AdjustNormal (MeshFacet &rclFacet, const Base::Vector3f &rclNormal)
00509 {
00510 Base::Vector3f clN = (_aclPointArray[rclFacet._aulPoints[1]] - _aclPointArray[rclFacet._aulPoints[0]]) %
00511 (_aclPointArray[rclFacet._aulPoints[2]] - _aclPointArray[rclFacet._aulPoints[0]]);
00512 if ((clN * rclNormal) < 0.0f) {
00513 rclFacet.FlipNormal();
00514 }
00515 }
00516
00517 inline Base::Vector3f MeshKernel::GetNormal (const MeshFacet &rclFacet) const
00518 {
00519 Base::Vector3f clN = (_aclPointArray[rclFacet._aulPoints[1]] - _aclPointArray[rclFacet._aulPoints[0]]) %
00520 (_aclPointArray[rclFacet._aulPoints[2]] - _aclPointArray[rclFacet._aulPoints[0]]);
00521 clN.Normalize();
00522 return clN;
00523 }
00524
00525 inline Base::Vector3f MeshKernel::GetGravityPoint (const MeshFacet &rclFacet) const
00526 {
00527 const Base::Vector3f& p0 = _aclPointArray[rclFacet._aulPoints[0]];
00528 const Base::Vector3f& p1 = _aclPointArray[rclFacet._aulPoints[1]];
00529 const Base::Vector3f& p2 = _aclPointArray[rclFacet._aulPoints[2]];
00530 return Base::Vector3f((p0.x+p1.x+p2.x)/3.0f,
00531 (p0.y+p1.y+p2.y)/3.0f,
00532 (p0.z+p1.z+p2.z)/3.0f);
00533 }
00534
00535 inline void MeshKernel::GetFacetPoints (unsigned long ulFaIndex, unsigned long &rclP0,
00536 unsigned long &rclP1, unsigned long &rclP2) const
00537 {
00538 assert(ulFaIndex < _aclFacetArray.size());
00539 const MeshFacet& rclFacet = _aclFacetArray[ulFaIndex];
00540 rclP0 = rclFacet._aulPoints[0];
00541 rclP1 = rclFacet._aulPoints[1];
00542 rclP2 = rclFacet._aulPoints[2];
00543 }
00544
00545
00546 }
00547
00548 #endif // MESH_KERNEL_H