Using a custom class member variable

From KitwarePublic
Revision as of 20:17, 11 November 2009 by Daviddoria (talk | contribs) (New page: I typically do not like examples that are this long, but it demonstrates several concepts that need to work together. This example is of two classes, vtkLidarPoint and vtkRay. vtkLidarPoin...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

I typically do not like examples that are this long, but it demonstrates several concepts that need to work together. This example is of two classes, vtkLidarPoint and vtkRay. vtkLidarPoint has a vtkRay member variable. We want to store a 2d array (vector of vectors) of vtkLidarPoints. These classes have both been stripped of all functionality to try to contain only pertinent information to the workings of reference counting and object storage. If you are having trouble with a bizarre error that is likely not being caused by something in the functional part of your code, this example should be a good place to start to make sure you have done all of the "VTK things" correctly.

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)

PROJECT(CustomClassMemberVariable)

FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})

ADD_EXECUTABLE(CustomClassMemberVariable Example.cxx vtkLidarPoint.cxx vtkRay.cxx)
TARGET_LINK_LIBRARIES(CustomClassMemberVariable vtkHybrid)

vtkLidarPoint.cxx

#include "vtkLidarPoint.h"
#include "vtkRay.h"

#include "vtkSmartPointer.h"
#include "vtkObjectFactory.h" //for new() macro

vtkCxxRevisionMacro(vtkLidarPoint, "$Revision: 1.1 $");
vtkStandardNewMacro(vtkLidarPoint);

vtkCxxSetObjectMacro(vtkLidarPoint, Ray, vtkRay);

vtkLidarPoint::vtkLidarPoint()
{
  this->Ray = NULL;
}

vtkLidarPoint::~vtkLidarPoint() 
{
  if (this->Ray)
    {
    this->Ray->Delete();
    }
}


void vtkLidarPoint::PrintSelf(vtkstd::ostream &os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os,indent);
}

vtkLidarPoint.h

#ifndef __vtkLidarPoint_h
#define __vtkLidarPoint_h

#include "vtkObject.h" //super class

class vtkRay;

class vtkLidarPoint : public vtkObject
{
public:
  static vtkLidarPoint *New();
  vtkTypeRevisionMacro(vtkLidarPoint,vtkObject);

  void PrintSelf(vtkstd::ostream &os, vtkIndent indent);

  virtual void SetRay(vtkRay *);
  vtkGetObjectMacro(Ray, vtkRay);
		
protected:
  vtkLidarPoint();
  ~vtkLidarPoint();

private:
  vtkLidarPoint(const vtkLidarPoint&); //not implemented
  void operator=(const vtkLidarPoint&); //not implemented
		
  vtkRay* Ray; //the direction and location from which the point was scanned
};

#endif

vtkRay.cxx

#include "vtkRay.h"

#include "vtkObjectFactory.h" //for new() macro
#include "vtkMath.h"
#include "vtkTransform.h"


vtkCxxRevisionMacro(vtkRay, "$Revision: 1.1 $");
vtkStandardNewMacro(vtkRay);

vtkRay::vtkRay()
{
	
}

vtkRay::~vtkRay() 
{
}

void vtkRay::PrintSelf(vtkstd::ostream &os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os,indent);
}

vtkRay.h

#ifndef __vtkRay_h
#define __vtkRay_h

class vtkTransform;
#include "vtkObject.h" //superclass

class vtkRay : public vtkObject
{
public:
  static vtkRay *New();
  vtkTypeRevisionMacro(vtkRay,vtkObject);
    
  void PrintSelf(vtkstd::ostream &os, vtkIndent indent);
		
protected:
  vtkRay();
  ~vtkRay();
private:
  vtkRay(const vtkRay&);  // Not implemented.
  void operator=(const vtkRay&);  // Not implemented.
  
};

#endif

Example.cxx

#include "vtkLidarPoint.h"
#include "vtkRay.h"

#include "vtkSmartPointer.h"

#include <vector>

int main()
{
 
  vtkstd::vector<vtkstd::vector<vtkSmartPointer<vtkLidarPoint> > > Grid;
  Grid.resize(10);
  
  for(unsigned int i = 0; i < 2; i++)
  {
    vtkstd::vector<vtkSmartPointer<vtkLidarPoint> > Column;
    Column.clear();
    Column.resize(10);
  
    for(unsigned int j = 0; j < 2; j++)
    {
      vtkSmartPointer<vtkRay> Ray = vtkSmartPointer<vtkRay>::New();
      
      vtkSmartPointer<vtkLidarPoint> LidarPoint = vtkSmartPointer<vtkLidarPoint>::New();
      LidarPoint->SetRay(Ray);
      
      vtkstd::cout << "Ref Before: " << LidarPoint->GetReferenceCount() << vtkstd::endl;
      Column[j] = LidarPoint;
      vtkstd::cout << "Ref After: " << LidarPoint->GetReferenceCount() << vtkstd::endl;
      
    }
    
    Grid[i] = Column;
    vtkstd::cout << "Ref after column added to grid: " << Grid[0][0]->GetReferenceCount() << vtkstd::endl;
  }
  
  return 0;
}