VTK
dox/Interaction/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 =========================================================================*/
00059 #ifndef __vtkContourRepresentation_h
00060 #define __vtkContourRepresentation_h
00061 
00062 #include "vtkInteractionWidgetsModule.h" // For export macro
00063 #include "vtkWidgetRepresentation.h"
00064 #include <vector> // STL Header; Required for vector
00065 
00066 class vtkContourLineInterpolator;
00067 class vtkIncrementalOctreePointLocator;
00068 class vtkPointPlacer;
00069 class vtkPolyData;
00070 class vtkIdList;
00071 
00072 //----------------------------------------------------------------------
00073 //BTX
00074 class vtkContourRepresentationPoint
00075 {
00076 public:
00077   double        WorldPosition[3];
00078   double        NormalizedDisplayPosition[2];
00079 
00080   // The point id. This is blank except in the case of
00081   // vtkPolygonalSurfaceContourLineInterpolator
00082   vtkIdType     PointId;
00083 };
00084 
00085 class vtkContourRepresentationNode
00086 {
00087 public:
00088   double        WorldPosition[3];
00089   double        WorldOrientation[9];
00090   double        NormalizedDisplayPosition[2];
00091   int           Selected;
00092   std::vector<vtkContourRepresentationPoint*> Points;
00093 
00094   // The point id. This is blank except in the case of
00095   // vtkPolygonalSurfaceContourLineInterpolator
00096   vtkIdType     PointId;
00097 };
00098 
00099 class vtkContourRepresentationInternals
00100 {
00101 public:
00102   std::vector<vtkContourRepresentationNode*> Nodes;
00103   void ClearNodes()
00104   {
00105     for(unsigned int i=0;i<this->Nodes.size();i++)
00106       {
00107       for (unsigned int j=0;j<this->Nodes[i]->Points.size();j++)
00108         {
00109         delete this->Nodes[i]->Points[j];
00110         }
00111       this->Nodes[i]->Points.clear();
00112       delete this->Nodes[i];
00113       }
00114     this->Nodes.clear();
00115   }
00116 };
00117 //ETX
00118 
00119 class VTKINTERACTIONWIDGETS_EXPORT vtkContourRepresentation : public vtkWidgetRepresentation
00120 {
00121   //BTX
00122   friend class vtkContourWidget;
00123   //ETX
00124 public:
00126 
00127   vtkTypeMacro(vtkContourRepresentation,vtkWidgetRepresentation);
00128   void PrintSelf(ostream& os, vtkIndent indent);
00130 
00132 
00134   virtual int AddNodeAtWorldPosition( double x, double y, double z);
00135   virtual int AddNodeAtWorldPosition( double worldPos[3] );
00136   virtual int AddNodeAtWorldPosition( double worldPos[3],
00137                                       double worldOrient[9] );
00139 
00141 
00144   virtual int AddNodeAtDisplayPosition( double displayPos[2] );
00145   virtual int AddNodeAtDisplayPosition( int displayPos[2] );
00146   virtual int AddNodeAtDisplayPosition( int X, int Y );
00148 
00150 
00153   virtual int ActivateNode( double displayPos[2] );
00154   virtual int ActivateNode( int displayPos[2] );
00155   virtual int ActivateNode( int X, int Y );
00157 
00158   // Descirption:
00159   // Move the active node to a specified world position.
00160   // Will return 0 if there is no active node or the node
00161   // could not be moved to that position. 1 will be returned
00162   // on success.
00163   virtual int SetActiveNodeToWorldPosition( double pos[3] );
00164   virtual int SetActiveNodeToWorldPosition( double pos[3],
00165                                             double orient[9] );
00166 
00168 
00172   virtual int SetActiveNodeToDisplayPosition( double pos[2] );
00173   virtual int SetActiveNodeToDisplayPosition( int pos[2] );
00174   virtual int SetActiveNodeToDisplayPosition( int X, int Y );
00176 
00178 
00179   virtual int ToggleActiveNodeSelected();
00180   virtual int GetActiveNodeSelected();
00181   virtual int GetNthNodeSelected(int);
00182   virtual int SetNthNodeSelected(int);
00184 
00187   virtual int GetActiveNodeWorldPosition( double pos[3] );
00188 
00191   virtual int GetActiveNodeWorldOrientation( double orient[9] );
00192 
00195   virtual int GetActiveNodeDisplayPosition( double pos[2] );
00196 
00198   virtual int GetNumberOfNodes();
00199 
00202   virtual int GetNthNodeDisplayPosition( int n, double pos[2] );
00203 
00206   virtual int GetNthNodeWorldPosition( int n, double pos[3] );
00207 
00208   //BTX
00210 
00211   virtual vtkContourRepresentationNode *GetNthNode(int n);
00212   //ETX
00214 
00217   virtual int GetNthNodeWorldOrientation( int n, double orient[9] );
00218 
00220 
00225   virtual int SetNthNodeDisplayPosition( int n, int X, int Y );
00226   virtual int SetNthNodeDisplayPosition( int n, int pos[2] );
00227   virtual int SetNthNodeDisplayPosition( int n, double pos[2] );
00229 
00231 
00234   virtual int SetNthNodeWorldPosition( int n, double pos[3] );
00235   virtual int SetNthNodeWorldPosition( int n, double pos[3],
00236                                        double orient[9] );
00238 
00241   virtual int  GetNthNodeSlope( int idx, double slope[3] );
00242 
00243   // Descirption:
00244   // For a given node n, get the number of intermediate
00245   // points between this node and the node at
00246   // (n+1). If n is the last node and the loop is
00247   // closed, this is the number of intermediate points
00248   // between node n and node 0. 0 is returned if n is
00249   // out of range.
00250   virtual int GetNumberOfIntermediatePoints( int n );
00251 
00253 
00256   virtual int GetIntermediatePointWorldPosition( int n,
00257                                                  int idx, double point[3] );
00259 
00261 
00264   virtual int AddIntermediatePointWorldPosition( int n,
00265                                                  double point[3] );
00267 
00269 
00272   virtual int AddIntermediatePointWorldPosition( int n,
00273                                double point[3], vtkIdType ptId );
00275 
00278   virtual int DeleteLastNode();
00279 
00282   virtual int DeleteActiveNode();
00283 
00285   virtual int DeleteNthNode( int n );
00286 
00288   virtual void ClearAllNodes();
00289 
00292   virtual int AddNodeOnContour( int X, int Y );
00293 
00295 
00297   vtkSetClampMacro(PixelTolerance,int,1,100);
00298   vtkGetMacro(PixelTolerance,int);
00300 
00302 
00304   vtkSetClampMacro(WorldTolerance, double, 0.0, VTK_DOUBLE_MAX);
00305   vtkGetMacro(WorldTolerance, double);
00307 
00308 //BTX -- used to communicate about the state of the representation
00309   enum {
00310     Outside=0,
00311     Nearby
00312   };
00313 
00314   enum {
00315     Inactive = 0,
00316     Translate,
00317     Shift,
00318     Scale
00319   };
00320 //ETX
00321 
00323 
00325   vtkGetMacro( CurrentOperation, int );
00326   vtkSetClampMacro( CurrentOperation, int,
00327                     vtkContourRepresentation::Inactive,
00328                     vtkContourRepresentation::Scale );
00329   void SetCurrentOperationToInactive()
00330     { this->SetCurrentOperation( vtkContourRepresentation::Inactive ); }
00331   void SetCurrentOperationToTranslate()
00332     { this->SetCurrentOperation( vtkContourRepresentation::Translate ); }
00333   void SetCurrentOperationToShift()
00334     {this->SetCurrentOperation( vtkContourRepresentation::Shift ); }
00335   void SetCurrentOperationToScale()
00336     {this->SetCurrentOperation( vtkContourRepresentation::Scale ); }
00338 
00339   // Descirption:
00340   // Set / get the Point Placer. The point placer is
00341   // responsible for converting display coordinates into
00342   // world coordinates according to some constraints, and
00343   // for validating world positions.
00344   void SetPointPlacer( vtkPointPlacer * );
00345   vtkGetObjectMacro( PointPlacer, vtkPointPlacer );
00346 
00348 
00350   void SetLineInterpolator( vtkContourLineInterpolator *);
00351   vtkGetObjectMacro( LineInterpolator, vtkContourLineInterpolator );
00353 
00355 
00356   virtual void BuildRepresentation()=0;
00357   virtual int ComputeInteractionState(int X, int Y, int modified=0)=0;
00358   virtual void StartWidgetInteraction(double e[2])=0;
00359   virtual void WidgetInteraction(double e[2])=0;
00361 
00363 
00364   virtual void ReleaseGraphicsResources(vtkWindow *w)=0;
00365   virtual int RenderOverlay(vtkViewport *viewport)=0;
00366   virtual int RenderOpaqueGeometry(vtkViewport *viewport)=0;
00367   virtual int RenderTranslucentPolygonalGeometry(vtkViewport *viewport)=0;
00368   virtual int HasTranslucentPolygonalGeometry()=0;
00370 
00372 
00374   void SetClosedLoop( int val );
00375   vtkGetMacro( ClosedLoop, int );
00376   vtkBooleanMacro( ClosedLoop, int );
00378 
00380 
00382   virtual void SetShowSelectedNodes(int);
00383   vtkGetMacro( ShowSelectedNodes, int );
00384   vtkBooleanMacro( ShowSelectedNodes, int );
00386 
00387 //BTX
00389 
00390   virtual vtkPolyData* GetContourRepresentationAsPolyData() = 0;
00391 //ETX
00393 
00396   void GetNodePolyData( vtkPolyData* poly );
00397 
00398   vtkSetMacro(RebuildLocator,bool);
00399 
00400 protected:
00401   vtkContourRepresentation();
00402   ~vtkContourRepresentation();
00403 
00404   // Selection tolerance for the handles
00405   int    PixelTolerance;
00406   double WorldTolerance;
00407 
00408   vtkPointPlacer             *PointPlacer;
00409   vtkContourLineInterpolator *LineInterpolator;
00410 
00411   int ActiveNode;
00412 
00413   int CurrentOperation;
00414   int ClosedLoop;
00415 
00416   // A flag to indicate whether to show the Selected nodes
00417   int                   ShowSelectedNodes;
00418 
00419   vtkContourRepresentationInternals *Internal;
00420 
00421   void AddNodeAtPositionInternal( double worldPos[3],
00422                                   double worldOrient[9], int displayPos[2] );
00423   void AddNodeAtPositionInternal( double worldPos[3],
00424                                   double worldOrient[9], double displayPos[2] );
00425   void SetNthNodeWorldPositionInternal( int n, double worldPos[3],
00426                                         double worldOrient[9] );
00427 
00429 
00431   void GetRendererComputedDisplayPositionFromWorldPosition( double worldPos[3],
00432                                     double worldOrient[9], int displayPos[2] );
00433   void GetRendererComputedDisplayPositionFromWorldPosition( double worldPos[3],
00434                                     double worldOrient[9], double displayPos[2] );
00436 
00437   virtual void UpdateLines( int index );
00438   void UpdateLine( int idx1, int idx2 );
00439 
00440   virtual int FindClosestPointOnContour( int X, int Y,
00441                                  double worldPos[3],
00442                                  int *idx );
00443 
00444   virtual void BuildLines()=0;
00445 
00446   // This method is called when something changes in the point
00447   // placer. It will cause all points to
00448   // be updates, and all lines to be regenerated.
00449   // Should be extended to detect changes in the line interpolator
00450   // too.
00451   virtual int  UpdateContour();
00452   vtkTimeStamp ContourBuildTime;
00453 
00454   void ComputeMidpoint( double p1[3], double p2[3], double mid[3] )
00455     {
00456       mid[0] = (p1[0] + p2[0])/2;
00457       mid[1] = (p1[1] + p2[1])/2;
00458       mid[2] = (p1[2] + p2[2])/2;
00459     }
00460 
00470   virtual void Initialize( vtkPolyData *, vtkIdList *);
00471 
00474   virtual void Initialize( vtkPolyData *);
00475 
00479   virtual void InitializeContour( vtkPolyData *, vtkIdList * );
00480 
00483   vtkIncrementalOctreePointLocator *Locator;
00484 
00487   void ResetLocator();
00488 
00489   void BuildLocator();
00490 
00491   bool RebuildLocator;
00492 
00493 
00494 private:
00495   vtkContourRepresentation(const vtkContourRepresentation&);  //Not implemented
00496   void operator=(const vtkContourRepresentation&);  //Not implemented
00497 };
00498 
00499 #endif
00500