PageRenderTime 29ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/pgl/algotools/Lattice.h

#
C Header | 630 lines | 310 code | 66 blank | 254 comment | 8 complexity | ea142bb54294af753c8e1cc23058ecd6 MD5 | raw file
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Microsoft Public License. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Microsoft Public License, please send an email to
  8. * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Microsoft Public License.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  14. * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  16. * PARTICULAR PURPOSE.
  17. * ***************************************************************************/
  18. #if !defined(AFX_TLATTICE_H__D32DD97D_BD03_4DBF_8B35_50B1E697A1E0__INCLUDED_)
  19. #define AFX_TLATTICE_H__D32DD97D_BD03_4DBF_8B35_50B1E697A1E0__INCLUDED_
  20. #if _MSC_VER > 1000
  21. #pragma once
  22. #endif // _MSC_VER > 1000
  23. namespace contour
  24. {
  25. /*!
  26. \defgroup LatticeGroup Lattices and mesh
  27. \ingroup ContourLibGroup
  28. */
  29. /*! \brief Interface of the lattice objects
  30. \ingroup LatticeGroup
  31. */
  32. template <class T>
  33. class TLattice
  34. {
  35. public:
  36. //! \name Structures and Typedefs
  37. //@{
  38. //! a 2D vertex
  39. typedef std::pair<T,T> Vertex;
  40. //! a vector of Vertex
  41. typedef std::vector< Vertex> VertexContainer;
  42. //! a vector of Vertex index
  43. typedef std::vector<size_t> IndexContainer;
  44. //! a rectangle region
  45. struct Rect
  46. {
  47. T left;
  48. T right;
  49. T bottom;
  50. T top;
  51. };
  52. //@}
  53. //! \name Constructors
  54. //@{
  55. //! Default constructor
  56. TLattice():m_bModified(true){};
  57. virtual ~TLattice(){};
  58. //@}
  59. //! \name Bounding box
  60. //@{
  61. //! Computes the bounding box of the lattice
  62. virtual void GetBoundingBox(Rect& r)=0;
  63. //@}
  64. //! \name Modified flag
  65. //@{
  66. //! set the modified flag to true
  67. void SetModifiedFlag() { m_bModifiedFlag=true;};
  68. //! return true if modified
  69. bool IsModified() const { return m_bModified;};
  70. //@}
  71. //! \name vertex coordinates
  72. //@{
  73. //! returns the number of poitns
  74. virtual size_t GetVertexSize() const=0;
  75. //! returns the x coordinate of the i-th vertex
  76. virtual T GetXVertex(size_t i) const=0;
  77. //! returns the y coordinate of the i-th vertex
  78. virtual T GetYVertex(size_t i) const=0;
  79. //@}
  80. //! \name Faces
  81. //@{
  82. //! return the number of side per face
  83. virtual size_t GetFaceSideSize() const = 0;
  84. //! returns the number of faces
  85. virtual size_t GetFaceSize() const = 0;
  86. //! return the summits of the face
  87. virtual void GetFacePos( size_t iFace, VertexContainer& vVertex ) const=0;
  88. //! return the index summits of the face
  89. virtual void GetFaceIndex( size_t iFace, IndexContainer& vIndex ) const=0;
  90. //! return the index summits of the face
  91. void GetFace( size_t iFace, IndexContainer& vIndex, VertexContainer& vVertex ) const
  92. {
  93. GetFaceIndex(iFace,vIndex);
  94. GetFacePos(iFace,vVertex);
  95. };
  96. //@}
  97. protected:
  98. //! \name Attributes
  99. //@{
  100. bool m_bModified;
  101. Rect m_rect;
  102. //@}
  103. };
  104. /*! \brief Scatter of point lattice, with no face
  105. \ingroup LatticeGroup
  106. */
  107. template<class T>
  108. class TScatterLattice : public TLattice<T>
  109. {
  110. public:
  111. //! \name Vertex setter and getter
  112. //@{
  113. //! return the vertex container
  114. VertexContainer& GetVertices() { return m_vertices;};
  115. //! return the vertex container, const
  116. const VertexContainer& GetVertices() const { return m_vertices;};
  117. //! sets the vertex container
  118. void SetVertices( const VertexContainer& v) { m_vertices=v; SetModifiedFlag();};
  119. //@}
  120. //! \name Vertex coordinates
  121. //@{
  122. //! returns the number of poitns
  123. virtual size_t GetVertexSize() const { return m_vertices.size();};
  124. //! returns the x coordinate of the i-th vertex
  125. virtual T GetXVertex(size_t i) const { return m_vertices[i].first;};
  126. //! returns the y coordinate of the i-th vertex
  127. virtual T GetYVertex(size_t i) const { return m_vertices[i].second;};
  128. //@}
  129. //! \name Faces
  130. //@{
  131. //! return the number of side per face
  132. virtual size_t GetFaceSideSize() const { return 0;};
  133. //! returns the number of faces
  134. virtual size_t GetFaceSize() const { return 0;};
  135. //! return the summits of the face
  136. virtual void GetFacePos( size_t iFace, VertexContainer& vVertex ) const { vVertex.clear();};
  137. //! return the index summits of the face
  138. virtual void GetFaceIndex( size_t iFace, IndexContainer& vIndex ) const { vIndex.clear();};
  139. //@}
  140. protected:
  141. //! \name Attributes
  142. //@{
  143. //! Vertices
  144. VertexContainer m_vertices;
  145. //@}
  146. };
  147. /*! \brief Triangular mesh
  148. \ingroup LatticeGroup
  149. */
  150. template<class T>
  151. class TTriMeshLattice : public TLattice<T>
  152. {
  153. public:
  154. //! \name Structures and typedefs
  155. //@{
  156. //! index of the triangle
  157. struct Face
  158. {
  159. //! index of the first vertex
  160. size_t m_p0;
  161. //! index of the second vertex
  162. size_t m_p1;
  163. //! index of the third vertex
  164. size_t m_p2;
  165. };
  166. //! vector of triangle corner index
  167. typedef std::vector<Face> FaceContainer;
  168. //@}
  169. //! \name Bounding box
  170. //@{
  171. //! Computes the bounding box of the lattice
  172. virtual void GetBoundingBox(Rect& r);
  173. //@}
  174. //! \name vertex coordinates
  175. //@{
  176. //! returns the number of vertices
  177. virtual size_t GetVertexSize() const { return m_vVertices.size();};
  178. //! returns the x coordinate of the i-th vertex
  179. virtual T GetXVertex(size_t i) const { ASSERT(i<m_vVertices.size()); return m_vVertices[i].first;};
  180. //! returns the y coordinate of the i-th vertex
  181. virtual T GetYVertex(size_t i) const { ASSERT(i<m_vVertices.size()); return m_vVertices[i].second;};
  182. //@}
  183. //! \name Faces and Vertices
  184. //@{
  185. //! Sets the vector of Vertices
  186. void SetVertices( const VertexContainer& vVertices) { m_vVertices = vVertices; SetModifiedFlag();};
  187. //! returns the vector of Vertices
  188. VertexContainer& GetVertices() { return m_vVertices;};
  189. //! returns the vector of Vertices, const
  190. const VertexContainer& GetVertices() const { return m_vVertices;};
  191. //! Sets the vector of triangle
  192. void SetFaces( const VertexContainer& vFaces) { m_vFaces = vFaces; SetModifiedFlag();};
  193. //! return the vector of faces index
  194. FaceContainer& GetFaces() { return m_vFaces;};
  195. //! return the vector of faces index, const
  196. const FaceContainer& GetFaces() const { return m_vFaces;};
  197. //@}
  198. //! \name Faces
  199. //@{
  200. //! return the number of side per face
  201. virtual size_t GetFaceSideSize() const { return 3;};
  202. //! returns the number of faces
  203. virtual size_t GetFaceSize() const { return m_vFaces.size();};
  204. //! return the summits of the face
  205. virtual void GetFacePos( size_t iFace, VertexContainer& vPos ) const;
  206. //! return the index summits of the face
  207. virtual void GetFaceIndex( size_t iFace, IndexContainer& vIndex ) const;
  208. //@}
  209. protected:
  210. //! \name Attributes
  211. //@{
  212. //! vertices description
  213. VertexContainer m_vVertices;
  214. //! Face description
  215. FaceContainer m_vFaces;
  216. //@}
  217. };
  218. template<class T>
  219. void TTriMeshLattice<T>::GetBoundingBox(TLattice<T>::Rect& r)
  220. {
  221. if (m_mVertices.size()==0)
  222. return;
  223. if (IsModified())
  224. {
  225. m_rect.left=m_vVertices[0].first;
  226. m_rect.right=m_vVertices[0].first;
  227. m_rect.bottom=m_vVertices[0].second;
  228. m_rect.top=m_vVertices[0].second;
  229. for (size_t i=1;i<m_vVertices.size();i++)
  230. {
  231. m_rect.left=__min(r.left, m_vVertices[i].first);
  232. m_rect.right=__min(r.right, m_vVertices[i].first);
  233. m_rect.bottom=__min(r.bottom, m_vVertices[i].second);
  234. m_rect.top=__min(r.top, m_vVertices[i].second);
  235. }
  236. m_bModified=false;
  237. }
  238. r=m_rect;
  239. };
  240. template<class T>
  241. void TTriMeshLattice<T>::GetFacePos(size_t iFace, VertexContainer& vPos ) const
  242. {
  243. ASSERT(iFace < GetFaceSize());
  244. ASSERT( m_vFaces[iFace].m_p0 < GetVertexSize());
  245. ASSERT( m_vFaces[iFace].m_p1 < GetVertexSize());
  246. ASSERT( m_vFaces[iFace].m_p2 < GetVertexSize());
  247. vPos.resize(3);
  248. vPos[0]=m_vVertices[ m_vFaces[iFace].m_p0];
  249. vPos[1]=m_vVertices[ m_vFaces[iFace].m_p1];
  250. vPos[2]=m_vVertices[ m_vFaces[iFace].m_p2];
  251. }
  252. template<class T>
  253. void TTriMeshLattice<T>::GetFaceIndex(size_t iFace, IndexContainer& vIndex ) const
  254. {
  255. ASSERT(iFace < GetFaceSize());
  256. vIndex.resize(3);
  257. vIndex[0]=m_vFaces[iFace].m_p0;
  258. vIndex[1]=m_vFaces[iFace].m_p1;
  259. vIndex[2]=m_vFaces[iFace].m_p2;
  260. }
  261. /*! \brief Regular grid of quads
  262. \ingroup LatticeGroup
  263. */
  264. template<class T>
  265. class TQuadGridLattice : public TLattice<T>
  266. {
  267. public:
  268. //! \name Rows and columns count
  269. //@{
  270. //! return the number of columns
  271. virtual size_t GetColSize() const=0;
  272. //! return the number of rows
  273. virtual size_t GetRowSize() const=0;
  274. //@}
  275. //! \name Faces
  276. //! return the number of side (4)
  277. size_t GetFaceSideSize() const { return 4;};
  278. //! returns the number of faces
  279. virtual size_t GetFaceSize() const { return (GetColSize()-1)*(GetRowSize()-1);};
  280. //! return the summits of the face
  281. virtual void GetFacePos( size_t iFace, VertexContainer& vFace ) const;
  282. //! return the index summits of the face
  283. virtual void GetFaceIndex( size_t iFace, IndexContainer& vFace ) const;
  284. //@}
  285. protected:
  286. //! Compute the index of the upper-left vertex of the face iFace
  287. size_t GetUpperLeftvertexIndexFromFaceIndex(size_t iFace) const
  288. {
  289. size_t colSize=GetColSize();
  290. return iFace/(colSize-1)*colSize+iFace%(colSize-1);
  291. };
  292. };
  293. template<class T>
  294. void TQuadGridLattice<T>::GetFacePos( size_t iFace, TLattice<T>::VertexContainer& vFace ) const
  295. {
  296. iFace= __min( GetFaceSize()-1, iFace);
  297. size_t i;
  298. vFace.resize(4);
  299. // first line
  300. i=GetUpperLeftvertexIndexFromFaceIndex(iFace);
  301. vFace[0].first=GetXVertex(i); vFace[0].second=GetYVertex(i);
  302. i++;
  303. vFace[1].first=GetXVertex(i); vFace[1].second=GetYVertex(i);
  304. i+=GetColSize();
  305. vFace[2].first=GetXVertex(i); vFace[2].second=GetYVertex(i);
  306. i--;
  307. vFace[3].first=GetXVertex(i); vFace[3].second=GetYVertex(i);
  308. };
  309. template<class T>
  310. void TQuadGridLattice<T>::GetFaceIndex( size_t iFace, TLattice<T>::IndexContainer& vFace ) const
  311. {
  312. iFace= __min( GetFaceSize()-1, iFace);
  313. size_t i;
  314. vFace.resize(4);
  315. // first line
  316. i=GetUpperLeftvertexIndexFromFaceIndex(iFace);
  317. vFace[0]=i;
  318. i++;
  319. vFace[1]=i;
  320. i+=GetColSize();
  321. vFace[2]=i;
  322. i--;
  323. vFace[3]=i;
  324. };
  325. /*! \brief Linearly spaced lattice of quads
  326. \ingroup LatticeGroup
  327. */
  328. template <class T>
  329. class TQuadLinGridLattice : public TQuadGridLattice<T>
  330. {
  331. public:
  332. //! \name Constructor
  333. //@{
  334. /*! \brief Default constructor
  335. \param uNy number of vertex rows, default is 2
  336. \param uNx number of vertex columns, default is 2,
  337. \param tLeft x coordinate of the first vertex column, default is 0,
  338. \param tTop y coordinate of the first vertex row, default is 1,
  339. \param tDx spatial step between vertex columns, default is 1,
  340. \param tDy spatial step between vertex rows, default is 1
  341. */
  342. TQuadLinGridLattice(size_t uNy=2, size_t uNx=2, T tLeft= 0, T tTop = 1, T tDx = 1, T tDy = 1)
  343. : m_uNx(uNx), m_uNy(uNy), m_tLeft(tLeft), m_tTop(tTop), m_tDx(tDx), m_tDy(tDy){};
  344. virtual ~TQuadLinGridLattice(){};
  345. //@}
  346. //! \name Regular lattice settings...
  347. //@{
  348. //! sets the number of vertex columns
  349. void SetColSize(size_t uColSize) { m_uNx = __max(2, uColSize); SetModifiedFlag();};
  350. //! sets the number of vertex rows
  351. void SetRowSize(size_t uRowSize) { m_uNy = __max(2, uRowSize); SetModifiedFlag();};
  352. //! sets the spatial step between vertex columns
  353. void SetColStep(T tColStep) { m_tDx = uColStep; SetModifiedFlag();};
  354. //! sets the spatial step between vertex rows
  355. void SetRowStep(T tRowStep) { m_tDy = uRowStep; SetModifiedFlag();};
  356. //! sets x coordinate of the first column
  357. void SetLeft(T tLeft) { m_tLeft = tLeft; SetModifiedFlag();};
  358. //! sets y coordinate of the first row
  359. void SetTop(T tTop) { m_tTop=tTop; SetModifiedFlag();};
  360. //! sets the lattice bounding box
  361. void SetBoundingBox( T tpx1, T tpy1, T tpx2, T tpy2);
  362. //! return the number of vertex columns
  363. size_t GetColSize() const { return m_uNx;};
  364. //! return the number of vertex rows
  365. size_t GetRowSize() const { return m_uNy;};
  366. //! return the spatial step between vertex columns
  367. T GetColStep() const { return m_tDx;};
  368. //! return the spatial step between vertex rows
  369. T GetRowStep() const { return m_tDy;};
  370. T GetWidth() const { return (m_uNx-1)*m_tDx;};
  371. T GetHeight() const { return (m_uNy-1)*m_tDy;};
  372. T GetLeft() const { return m_tLeft;};
  373. T GetTop() const { return m_tTop;};
  374. T GetBottom() const { return m_tTop-GetHeight();};
  375. T GetRight() const { return m_tLeft+GetWidth();};
  376. //@}
  377. //! \name Bounding box
  378. //@{
  379. void GetBoundingBox(Rect& r);
  380. //@}
  381. //! \name vertex coordinates
  382. //@{
  383. //! returns the number of Vertices
  384. virtual size_t GetVertexSize() const { return m_uNy*m_uNx;};
  385. //! return the x coordinate of the i-th vertex
  386. virtual T GetXVertex(size_t i) const { return m_tLeft+(i%m_uNx)*m_tDx;};
  387. //! return the y coordinate of the i-th vertex
  388. virtual T GetYVertex(size_t i) const { return m_tTop-(i/m_uNx)*m_tDy;};
  389. //@}
  390. protected:
  391. //! \name Attributes
  392. //@{
  393. //! number of vertex columns
  394. size_t m_uNx;
  395. //! number of vertex rows
  396. size_t m_uNy;
  397. T m_tLeft;
  398. T m_tTop;
  399. T m_tDx;
  400. T m_tDy;
  401. //@}
  402. };
  403. template<class T>
  404. void TQuadLinGridLattice<T>::GetBoundingBox(TLattice<T>::Rect& r )
  405. {
  406. if (IsModified())
  407. {
  408. m_rect.left=GetLeft();
  409. m_rect.top=GetTop();
  410. m_rect.right=GetRight();
  411. m_rect.bottom=GetBottom();
  412. m_bModified=false;
  413. }
  414. r=m_rect;
  415. }
  416. template<class T>
  417. void TQuadLinGridLattice<T>::SetBoundingBox( T tpx1, T tpy1, T tpx2, T tpy2)
  418. {
  419. m_tLeft = __min(tpx1, tpx2);
  420. m_tTop = __min(tpy1, tpy2);
  421. m_tDx=fabs(tpx1-tpx2)/(m_uNx-1);
  422. m_tDy=fabs(tpy1-tpy2)/(m_uNx-1);
  423. SetModifiedFlag();
  424. };
  425. /*! \brief Quad Regular lattice
  426. \ingroup LatticeGroup
  427. */
  428. template<class T>
  429. class TQuadRegGridLattice : public TQuadGridLattice<T>
  430. {
  431. public:
  432. //! \name Typedefs
  433. //@{
  434. typedef std::vector<T> TContainer;
  435. //@}
  436. //! \name Grid methods
  437. //@{
  438. //! return the x coordinates container, const
  439. const TContainer& GetXVertices() const { return m_vXVertices;};
  440. //! return the x coordinates container
  441. TContainer& GetXVertices() { return m_vXVertices;};
  442. //! return the y coordinates container, const
  443. const TContainer& GetYVertices() const { return m_vYVertices;};
  444. //! return the y coordinates container
  445. TContainer& GetYVertices() { return m_vYVertices;};
  446. //! sets a x Vertex,
  447. void SetXVertex( const TContainer& vX) { m_vXVertices=vX;};
  448. //! sets a y Vertex,
  449. void SetYVertex( const TContainer& vY) { m_vYVertices=vY;};
  450. //@}
  451. //! \name Grid dimension
  452. //@{
  453. //! return the number of columns
  454. virtual size_t GetColSize() const { return m_vXVertices.size();};
  455. //! return the number of rows
  456. virtual size_t GetRowSize() const { return m_vYVertices.size();};
  457. //@}
  458. //! \name Bounding box
  459. //@{
  460. void GetBoundingBox(Rect& r);
  461. //@}
  462. //! \name vertex coordinates
  463. //@{
  464. //! returns the number of Vertices
  465. virtual size_t GetVertexSize() const { return m_vXVertices.size()*m_vYVertices.size();};
  466. //! return the x coordinate of the i-th vertex
  467. virtual T GetXVertex(size_t i) const { ASSERT(i%GetColSize()<m_vXVertices.size()); return m_vXVertices[i%GetColSize()];};
  468. //! return the y coordinate of the i-th vertex
  469. virtual T GetYVertex(size_t i) const { ASSERT(i/GetColSize()<m_vYVertices.size()); return m_vYVertices[i/GetColSize()];};
  470. //@}
  471. protected:
  472. //! \name Attributes
  473. //@{
  474. TContainer m_vXVertices;
  475. TContainer m_vYVertices;
  476. //@}
  477. };
  478. template<class T>
  479. void TQuadRegGridLattice<T>::GetBoundingBox(TLattice<T>::Rect& r)
  480. {
  481. if (IsModified())
  482. {
  483. m_rect.left=m_vXVertices[0];
  484. m_rect.right=m_vXVertices[m_vXVertices.size()-1];
  485. m_rect.top=m_vYVertices[0];
  486. m_rect.bottom=m_vYVertices[m_vYVertices.size()-1];
  487. m_bModified=false;
  488. }
  489. r=m_rect;
  490. }
  491. /*! Regualar mesh of quads
  492. \ingroup LatticeGroup
  493. */
  494. template<class T>
  495. class TQuadMeshGridLattice : public TQuadGridLattice<T>
  496. {
  497. public:
  498. //! \name Typedefs
  499. //@{
  500. //! Matrix of vertices
  501. typedef std::TSTLMatrix<Vertex> VertexMatrix;
  502. //@}
  503. //! \name Grid dimension
  504. //@{
  505. //! return the number of columns
  506. virtual size_t GetColSize() const { return m_mVertices.ncols();};
  507. //! return the number of rows
  508. virtual size_t GetRowSize() const { return m_mVertices.nrows();};
  509. //@}
  510. //! \name Bounding box
  511. //@{
  512. void GetBoundingBox(Rect& r);
  513. //@}
  514. //! \name vertex coordinates
  515. //@{
  516. //! returns the number of Vertices
  517. virtual size_t GetVertexSize() const { return m_mVertices.size();};
  518. //! return the x coordinate of the i-th vertex
  519. virtual T GetXVertex(size_t i) const { ASSERT(i<m_mVertices.size()); return m_mVertices[i].first;};
  520. //! return the y coordinate of the i-th vertex
  521. virtual T GetYVertex(size_t i) const { ASSERT(i<m_mVertices.size()); return m_mVertices[i].second;};
  522. //@}
  523. //! \name Mesh methods
  524. //@{
  525. //! return the vertex coordinates, const
  526. const VertexMatrix& GetVertices() const { return m_mVertices;};
  527. //! return the vertex coordinates
  528. VertexMatrix& GetVertices() { return m_mVertices;};
  529. //@}
  530. protected:
  531. VertexMatrix m_mVertices;
  532. };
  533. template<class T>
  534. void TQuadMeshGridLattice<T>::GetBoundingBox(TLattice<T>::Rect& r)
  535. {
  536. if (m_mVertices.size()==0)
  537. return;
  538. if (IsModified())
  539. {
  540. m_rect.left=m_mVertices[0].first;
  541. m_rect.right=m_mVertices[0].first;
  542. m_rect.bottom=m_mVertices[0].second;
  543. m_rect.top=m_mVertices[0].second;
  544. for (size_t i=1;i<m_mVertices.size();i++)
  545. {
  546. m_rect.left=__min(r.left, m_mVertices[i].first);
  547. m_rect.right=__min(r.right, m_mVertices[i].first);
  548. m_rect.bottom=__min(r.bottom, m_mVertices[i].second);
  549. m_rect.top=__min(r.top, m_mVertices[i].second);
  550. }
  551. m_bModified=false;
  552. }
  553. r=m_rect;
  554. };
  555. };
  556. #endif // !defined(AFX_TLATTICE_H__D32DD97D_BD03_4DBF_8B35_50B1E697A1E0__INCLUDED_)