VTK
dox/Widgets/vtkContourRepresentation.h
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    vtkContourRepresentation.h
00005 
00006   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
00007   All rights reserved.
00008   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
00009 
00010      This software is distributed WITHOUT ANY WARRANTY; without even
00011      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00012      PURPOSE.  See the above copyright notice for more information.
00013 
00014 =========================================================================*/
00056 #ifndef __vtkContourRepresentation_h
00057 #define __vtkContourRepresentation_h
00058 
00059 #include "vtkWidgetRepresentation.h"
00060 #include <vtkstd/vector> // STL Header; Required for vector
00061 
00062 class vtkContourLineInterpolator;
00063 class vtkIncrementalOctreePointLocator;
00064 class vtkPointPlacer;
00065 class vtkPolyData;
00066 
00067 //----------------------------------------------------------------------
00068 //BTX
00069 class vtkContourRepresentationPoint
00070 {
00071 public:
00072   double        WorldPosition[3];
00073   double        NormalizedDisplayPosition[2];
00074 };
00075 
00076 class vtkContourRepresentationNode
00077 {
00078 public:
00079   double        WorldPosition[3];
00080   double        WorldOrientation[9];
00081   double        NormalizedDisplayPosition[2];
00082   int           Selected;
00083   vtkstd::vector<vtkContourRepresentationPoint*> Points;
00084 };
00085 
00086 class vtkContourRepresentationInternals
00087 {
00088 public:
00089   vtkstd::vector<vtkContourRepresentationNode*> Nodes;
00090   void ClearNodes()
00091   {
00092     for(unsigned int i=0;i<this->Nodes.size();i++)
00093       {
00094       for (unsigned int j=0;j<this->Nodes[i]->Points.size();j++)
00095         {
00096         delete this->Nodes[i]->Points[j];
00097         }
00098       this->Nodes[i]->Points.clear();
00099       delete this->Nodes[i];
00100       }
00101     this->Nodes.clear(); 
00102   }   
00103 };
00104 //ETX
00105 
00106 class VTK_WIDGETS_EXPORT vtkContourRepresentation : public vtkWidgetRepresentation
00107 {
00108   //BTX  
00109   friend class vtkContourWidget;
00110   //ETX
00111 public:
00113 
00114   vtkTypeMacro(vtkContourRepresentation,vtkWidgetRepresentation);
00115   void PrintSelf(ostream& os, vtkIndent indent);
00117 
00119 
00121   virtual int AddNodeAtWorldPosition( double x, double y, double z);
00122   virtual int AddNodeAtWorldPosition( double worldPos[3] );
00123   virtual int AddNodeAtWorldPosition( double worldPos[3],
00124                                       double worldOrient[9] );
00126   
00128 
00131   virtual int AddNodeAtDisplayPosition( double displayPos[2] );
00132   virtual int AddNodeAtDisplayPosition( int displayPos[2] );
00133   virtual int AddNodeAtDisplayPosition( int X, int Y );
00135 
00137 
00140   virtual int ActivateNode( double displayPos[2] );
00141   virtual int ActivateNode( int displayPos[2] );
00142   virtual int ActivateNode( int X, int Y );
00144   
00145   // Descirption:
00146   // Move the active node to a specified world position.
00147   // Will return 0 if there is no active node or the node
00148   // could not be moved to that position. 1 will be returned
00149   // on success.
00150   virtual int SetActiveNodeToWorldPosition( double pos[3] );
00151   virtual int SetActiveNodeToWorldPosition( double pos[3],
00152                                             double orient[9] );
00153   
00155 
00159   virtual int SetActiveNodeToDisplayPosition( double pos[2] );
00160   virtual int SetActiveNodeToDisplayPosition( int pos[2] );
00161   virtual int SetActiveNodeToDisplayPosition( int X, int Y );
00163   
00165 
00166   virtual int ToggleActiveNodeSelected();
00167   virtual int GetActiveNodeSelected();
00168   virtual int GetNthNodeSelected(int);
00169   virtual int SetNthNodeSelected(int);
00171 
00174   virtual int GetActiveNodeWorldPosition( double pos[3] );
00175   
00178   virtual int GetActiveNodeWorldOrientation( double orient[9] );
00179   
00182   virtual int GetActiveNodeDisplayPosition( double pos[2] );
00183 
00185   virtual int GetNumberOfNodes();
00186   
00189   virtual int GetNthNodeDisplayPosition( int n, double pos[2] );
00190   
00193   virtual int GetNthNodeWorldPosition( int n, double pos[3] );
00194   
00197   virtual int GetNthNodeWorldOrientation( int n, double orient[9] );
00198   
00200 
00205   virtual int SetNthNodeDisplayPosition( int n, int X, int Y );
00206   virtual int SetNthNodeDisplayPosition( int n, int pos[2] );
00207   virtual int SetNthNodeDisplayPosition( int n, double pos[2] );
00209   
00211 
00214   virtual int SetNthNodeWorldPosition( int n, double pos[3] );
00215   virtual int SetNthNodeWorldPosition( int n, double pos[3],
00216                                        double orient[9] );
00218   
00221   virtual int  GetNthNodeSlope( int idx, double slope[3] );
00222   
00223   // Descirption:
00224   // For a given node n, get the number of intermediate
00225   // points between this node and the node at
00226   // (n+1). If n is the last node and the loop is
00227   // closed, this is the number of intermediate points
00228   // between node n and node 0. 0 is returned if n is
00229   // out of range.
00230   virtual int GetNumberOfIntermediatePoints( int n );
00231   
00233 
00236   virtual int GetIntermediatePointWorldPosition( int n, 
00237                                                  int idx, double point[3] );
00239   
00241 
00244   virtual int AddIntermediatePointWorldPosition( int n, 
00245                                                  double point[3] );
00247 
00250   virtual int DeleteLastNode();
00251   
00254   virtual int DeleteActiveNode();
00255   
00257   virtual int DeleteNthNode( int n );
00258   
00260   virtual void ClearAllNodes();
00261   
00264   virtual int AddNodeOnContour( int X, int Y );
00265   
00267 
00269   vtkSetClampMacro(PixelTolerance,int,1,100);
00270   vtkGetMacro(PixelTolerance,int);
00272 
00274 
00276   vtkSetClampMacro(WorldTolerance, double, 0.0, VTK_DOUBLE_MAX);
00277   vtkGetMacro(WorldTolerance, double);
00279 
00280 //BTX -- used to communicate about the state of the representation
00281   enum {
00282     Outside=0,
00283     Nearby
00284   };
00285   
00286   enum {
00287     Inactive = 0,
00288     Translate,
00289     Shift,
00290     Scale
00291   };
00292 //ETX
00293 
00295 
00297   vtkGetMacro( CurrentOperation, int );
00298   vtkSetClampMacro( CurrentOperation, int, 
00299                     vtkContourRepresentation::Inactive,
00300                     vtkContourRepresentation::Scale );
00301   void SetCurrentOperationToInactive()
00302     { this->SetCurrentOperation( vtkContourRepresentation::Inactive ); }
00303   void SetCurrentOperationToTranslate()
00304     { this->SetCurrentOperation( vtkContourRepresentation::Translate ); }
00305   void SetCurrentOperationToShift()
00306     {this->SetCurrentOperation( vtkContourRepresentation::Shift ); }
00307   void SetCurrentOperationToScale()
00308     {this->SetCurrentOperation( vtkContourRepresentation::Scale ); }
00310 
00311   // Descirption:
00312   // Set / get the Point Placer. The point placer is
00313   // responsible for converting display coordinates into
00314   // world coordinates according to some constraints, and
00315   // for validating world positions.
00316   void SetPointPlacer( vtkPointPlacer * );
00317   vtkGetObjectMacro( PointPlacer, vtkPointPlacer );
00318   
00320 
00322   void SetLineInterpolator( vtkContourLineInterpolator *);
00323   vtkGetObjectMacro( LineInterpolator, vtkContourLineInterpolator );
00325   
00327 
00328   virtual void BuildRepresentation()=0;
00329   virtual int ComputeInteractionState(int X, int Y, int modified=0)=0;
00330   virtual void StartWidgetInteraction(double e[2])=0;
00331   virtual void WidgetInteraction(double e[2])=0;
00333 
00335 
00336   virtual void ReleaseGraphicsResources(vtkWindow *w)=0;
00337   virtual int RenderOverlay(vtkViewport *viewport)=0;
00338   virtual int RenderOpaqueGeometry(vtkViewport *viewport)=0;
00339   virtual int RenderTranslucentPolygonalGeometry(vtkViewport *viewport)=0;
00340   virtual int HasTranslucentPolygonalGeometry()=0;
00342   
00344 
00346   void SetClosedLoop( int val );
00347   vtkGetMacro( ClosedLoop, int );
00348   vtkBooleanMacro( ClosedLoop, int );
00350   
00352 
00354   virtual void SetShowSelectedNodes(int);
00355   vtkGetMacro( ShowSelectedNodes, int );
00356   vtkBooleanMacro( ShowSelectedNodes, int );
00358 
00359 //BTX
00361 
00362   virtual vtkPolyData* GetContourRepresentationAsPolyData() = 0;
00363 //ETX
00365 
00368   void GetNodePolyData( vtkPolyData* poly );
00369 
00370   vtkSetMacro(RebuildLocator,bool);
00371 
00372 protected:
00373   vtkContourRepresentation();
00374   ~vtkContourRepresentation();
00375   
00376   // Selection tolerance for the handles
00377   int    PixelTolerance;
00378   double WorldTolerance;
00379 
00380   vtkPointPlacer             *PointPlacer;
00381   vtkContourLineInterpolator *LineInterpolator;
00382   
00383   int ActiveNode;
00384   
00385   int CurrentOperation;
00386   int ClosedLoop;
00387 
00388   // A flag to indicate whether to show the Selected nodes
00389   int                   ShowSelectedNodes;
00390   
00391   vtkContourRepresentationInternals *Internal;
00392 
00393   void AddNodeAtPositionInternal( double worldPos[3],
00394                                   double worldOrient[9], int displayPos[2] );
00395   void AddNodeAtPositionInternal( double worldPos[3],
00396                                   double worldOrient[9], double displayPos[2] );
00397   void SetNthNodeWorldPositionInternal( int n, double worldPos[3],
00398                                         double worldOrient[9] );
00399 
00401 
00403   void GetRendererComputedDisplayPositionFromWorldPosition( double worldPos[3],
00404                                     double worldOrient[9], int displayPos[2] );
00405   void GetRendererComputedDisplayPositionFromWorldPosition( double worldPos[3],
00406                                     double worldOrient[9], double displayPos[2] );
00408   
00409   virtual void UpdateLines( int index );
00410   void UpdateLine( int idx1, int idx2 );
00411 
00412   virtual int FindClosestPointOnContour( int X, int Y, 
00413                                  double worldPos[3],
00414                                  int *idx );
00415   
00416   virtual void BuildLines()=0;
00417 
00418   // This method is called when something changes in the point
00419   // placer. It will cause all points to
00420   // be updates, and all lines to be regenerated.
00421   // Should be extended to detect changes in the line interpolator
00422   // too.
00423   virtual int  UpdateContour();
00424   vtkTimeStamp ContourBuildTime;
00425   
00426   void ComputeMidpoint( double p1[3], double p2[3], double mid[3] )
00427     {
00428       mid[0] = (p1[0] + p2[0])/2;
00429       mid[1] = (p1[1] + p2[1])/2;
00430       mid[2] = (p1[2] + p2[2])/2;
00431     }
00432 
00439   virtual void Initialize( vtkPolyData * );
00440 
00443   vtkIncrementalOctreePointLocator *Locator;
00444 
00447   void ResetLocator();
00448 
00449   void BuildLocator();
00450 
00451   bool RebuildLocator;
00452 
00453 
00454 private:
00455   vtkContourRepresentation(const vtkContourRepresentation&);  //Not implemented
00456   void operator=(const vtkContourRepresentation&);  //Not implemented
00457 };
00458 
00459 #endif
00460