Using a custom class member variable

From KitwarePublic
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

<source lang="text"> 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)

</source>

vtkLidarPoint.cxx

<source lang="cpp">

  1. include "vtkLidarPoint.h"
  2. include "vtkRay.h"
  1. include "vtkSmartPointer.h"
  2. 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);

}

</source>

vtkLidarPoint.h

<source lang="cpp">

  1. ifndef __vtkLidarPoint_h
  2. define __vtkLidarPoint_h
  1. 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

};

  1. endif

</source>

vtkRay.cxx

<source lang="cpp">

  1. include "vtkRay.h"
  1. include "vtkObjectFactory.h" //for new() macro
  2. include "vtkMath.h"
  3. 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);

}

</source>

vtkRay.h

<source lang="cpp">

  1. ifndef __vtkRay_h
  2. define __vtkRay_h

class vtkTransform;

  1. 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.
 

};

  1. endif

</source>

Example.cxx

<source lang="cpp">

  1. include "vtkLidarPoint.h"
  2. include "vtkRay.h"
  1. include "vtkSmartPointer.h"
  1. 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;

} </source>