ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkVoronoiDiagram2DGenerator.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkVoronoiDiagram2DGenerator_h
00019 #define __itkVoronoiDiagram2DGenerator_h
00020 
00021 #include "itkMeshSource.h"
00022 #include "itkVoronoiDiagram2D.h"
00023 
00024 #include <vector>
00025 
00026 #ifndef NULL
00027 #define NULL 0
00028 #endif
00029 
00030 namespace itk
00031 {
00050 template< typename TCoordType >
00051 class ITK_EXPORT VoronoiDiagram2DGenerator:
00052   public MeshSource< VoronoiDiagram2D< TCoordType > >
00053 {
00054 public:
00055   typedef VoronoiDiagram2DGenerator                    Self;
00056   typedef MeshSource< VoronoiDiagram2D< TCoordType > > Superclass;
00057   typedef SmartPointer< Self >                         Pointer;
00058   typedef SmartPointer< const Self >                   ConstPointer;
00059 
00061   itkNewMacro(Self);
00062 
00064   itkTypeMacro(VoronoiDiagram2DGenerator, MeshSource);
00065 
00067   typedef VoronoiDiagram2D< TCoordType > VDMesh;
00068   typedef typename VDMesh::SeedsIterator SeedsIterator;
00069   typedef typename VDMesh::Pointer       OutputType;
00070   typedef typename VDMesh::PointType     PointType;
00071   typedef typename VDMesh::SeedsType     SeedsType;
00072   typedef typename VDMesh::EdgeInfo      EdgeInfo;
00073   typedef typename VDMesh::EdgeInfoDQ    EdgeInfoDQ;
00074   typedef typename VDMesh::CoordRepType  CoordRepType;
00075   typedef typename VDMesh::VoronoiEdge   VoronoiEdge;
00076 
00078   itkGetConstMacro(NumberOfSeeds, unsigned int);
00079 
00082   void SetSeeds(int num, SeedsIterator begin);
00083 
00085   void AddSeeds(int num, SeedsIterator begin);
00086 
00087   void AddOneSeed(PointType);
00088 
00090   void SortSeeds(void);
00091 
00093   virtual void GenerateOutputInformation() {}
00094 
00096   void UpdateDiagram(void);
00097 
00099   void SetBoundary(PointType vorsize);
00100 
00101   void SetOrigin(PointType vorsize);
00102 
00104   void SetRandomSeeds(int num);
00105 
00107   PointType GetSeed(int SeedID);
00108 
00109 protected:
00110   VoronoiDiagram2DGenerator();
00111   ~VoronoiDiagram2DGenerator();
00112   virtual void PrintSelf(std::ostream & os, Indent indent) const;
00113 
00115   void GenerateData(void);
00116 
00117 private:
00118   VoronoiDiagram2DGenerator(const Self &); //purposely not implemented
00119   void operator=(const Self &);            //purposely not implemented
00120 
00121   unsigned int m_NumberOfSeeds;
00122   PointType    m_VorBoundary;
00123   OutputType   m_OutputVD;
00124   SeedsType    m_Seeds;
00125 
00126   static bool comp(PointType arg1, PointType arg2);
00127 
00133   class FortuneSite;
00134   class FortuneEdge;
00135   class FortuneHalfEdge;
00136 
00139   friend class FortuneSite;
00140   friend class FortuneEdge;
00141   friend class FortuneHalfEdge;
00142 
00143   class FortuneSite
00144   {
00145 public:
00146     PointType m_Coord;
00147     int       m_Sitenbr;
00148     FortuneSite():m_Sitenbr( NumericTraits< int >::max() ) { m_Coord.Fill( NumericTraits< CoordRepType >::max() ); }
00149     ~FortuneSite(){}
00150   };
00151 
00152   class FortuneEdge
00153   {
00154 public:
00155     float        m_A, m_B, m_C;    // explicit line function: Ax + By = C;
00156     FortuneSite *m_Ep[2];
00157     FortuneSite *m_Reg[2];
00158     int          m_Edgenbr;
00159     FortuneEdge():m_A(0.0), m_B(0.0), m_C(0.0) { m_Ep[0] = m_Ep[1] = m_Reg[0] = m_Reg[1] = 0; }
00160     ~FortuneEdge(){}
00161   };
00162 
00163   class FortuneHalfEdge
00164   {
00165 public:
00166     FortuneHalfEdge *m_Left;
00167     FortuneHalfEdge *m_Right;
00168     FortuneEdge *    m_Edge;
00169     bool             m_RorL;
00170     FortuneSite *    m_Vert;
00171     double           m_Ystar;
00172     FortuneHalfEdge *m_Next;
00173     FortuneHalfEdge():m_Left(0), m_Right(0), m_Edge(0), m_RorL(false), m_Vert(0), m_Ystar(0.0), m_Next(0) {}
00174     FortuneHalfEdge(const FortuneHalfEdge & edge):m_Left(edge.m_Left),
00175       m_Right(edge.m_Right),
00176       m_Edge(edge.m_Edge),
00177       m_RorL(edge.m_RorL),
00178       m_Vert(edge.m_Vert),
00179       m_Ystar(edge.m_Ystar),
00180       m_Next(edge.m_Next) {}
00181     ~FortuneHalfEdge(){}
00182   };
00183 
00184   double m_Pxmin;
00185   double m_Pxmax;
00186   double m_Pymin;
00187   double m_Pymax;
00188   double m_Deltax;
00189   double m_Deltay;
00190   double m_SqrtNSites;
00191 
00192   unsigned int                   m_PQcount;
00193   int                            m_PQmin;
00194   unsigned int                   m_PQhashsize;
00195   unsigned int                   m_Nedges;
00196   unsigned int                   m_Nvert;
00197   FortuneSite *                  m_BottomSite;
00198   std::vector< FortuneHalfEdge > m_PQHash;
00199 
00200   unsigned int                     m_ELhashsize;
00201   FortuneHalfEdge                  m_ELleftend;
00202   FortuneHalfEdge                  m_ELrightend;
00203   std::vector< FortuneHalfEdge * > m_ELHash;
00204 
00205   FortuneEdge                m_DELETED;
00206   std::vector< FortuneSite > m_SeedSites;
00207 
00208   bool differentPoint(PointType p1, PointType p2);
00209 
00210   bool almostsame(CoordRepType p1, CoordRepType p2);
00211 
00212   unsigned char Pointonbnd(int VertID);
00213 
00214   void GenerateVDFortune(void);
00215 
00216   void ConstructDiagram(void);
00217 
00218   void createHalfEdge(FortuneHalfEdge *task, FortuneEdge *e, bool pm);
00219 
00220   void PQshowMin(PointType *task);
00221 
00222   FortuneHalfEdge * findLeftHE(PointType *p);
00223 
00224   FortuneHalfEdge * ELgethash(int b);
00225 
00226   bool right_of(FortuneHalfEdge *el, PointType *p);
00227 
00228   FortuneSite * getRightReg(FortuneHalfEdge *he);
00229 
00230   FortuneSite * getLeftReg(FortuneHalfEdge *he);
00231 
00232   void bisect(FortuneEdge *, FortuneSite *s1, FortuneSite *s2);
00233 
00234   void insertEdgeList(FortuneHalfEdge *lbase, FortuneHalfEdge *lnew);
00235 
00236   void intersect(FortuneSite *task, FortuneHalfEdge *el1, FortuneHalfEdge *el2);
00237 
00238   void deletePQ(FortuneHalfEdge *task);
00239 
00240   void deleteEdgeList(FortuneHalfEdge *task);
00241 
00242   int PQbucket(FortuneHalfEdge *task);
00243 
00244   void clip_line(FortuneEdge *task);
00245 
00246   void insertPQ(FortuneHalfEdge *he, FortuneSite *v, double offset);
00247 
00248   double dist(FortuneSite *s1, FortuneSite *s2);
00249 
00250   FortuneHalfEdge * getPQmin(void);
00251 
00252   void makeEndPoint(FortuneEdge *task, bool lr, FortuneSite *ends);
00253 };
00254 } // end namespace itk
00255 
00256 #ifndef ITK_MANUAL_INSTANTIATION
00257 #include "itkVoronoiDiagram2DGenerator.hxx"
00258 #endif
00259 
00260 #endif
00261