[vtkusers] Re: deriving from vtkActor to add an ID (was: A question)
Wiebke Timm
wiebke.timm at uni-bielefeld.de
Fri Apr 30 04:05:50 EDT 2004
Hi again!
Replying to my own post because just now I found out my answer was kind
of wrong although in the first place the solution might have worked. I
hope Luke Hua is still reading...
The question was how to extend a vtkActor to have an int more to hold
an ID. Maybe some more experienced users could have a look because I'm
still not sure how to extend a vtkSomething in an orderly manner.
My answer to that question was the following:
On 09.03.2004, at 10:31, Wiebke Timm wrote:
> You can extend vtkActor to have an ID. Did that and it works
> flawlessly. Just use the extended vtkActor instead of the original.
> When your picker/renderer returns an actor you can get the ID easily.
> If you don't want to set that ID yourself you can add a static
> variable that "counts" actors, i.e. it has to be incremented inside
> the New() method, then set the ID accordingly.
This is still true. But the way _how_ this has to be done was wrong - I
wonder why nobody spoke up... :
> vtkMyActor.h looks like this:
>
> #ifndef MYACT_H
> #define MYACT_H
>
> #include <vtkActor.h>
>
> class vtkMyActor : public vtkActor {
> public:
> int GetID();
> void SetID(int id);
> static vtkMyActor* New();
> void Delete();
>
> private:
> int id; // id of this glyph's actor
>
> };
> #endif
>
>
>
> vtkMyActor.cc looks like this:
>
> #include "vtkMyActor.h"
>
> vtkMyActor* vtkMyActor::New() {
> vtkActor::New();
> }
>
> void vtkMyActor::Delete() {
> vtkActor::Delete();
> }
>
> int vtkMyActor::GetID() {
> return this->id;
> }
>
> void vtkMyActor::SetID(int id) {
> this->id = id;
> }
Got a few seg faults at the end of my vtkqt program, valgrind reported
memory leaks of 4 byte size (that's an int...) when I used the GetID()
and SetID(int id) methods. That's because above
vtkActor::New();
inside the overwritten New() method doesn't allocate memory for that
int!
Luke, did your version you posted have that leaks (should have if I
read it correctly)?
Also I read in the vtk Users Guide that I have to derive a new factory
from vtkObjectFactory, register the override of vtkActor with
vtkMyActor and use some macros to get a create method to work. Isn't
there an easier and shorter way to just add an int to a class... ??? I
don't need any dynmically loaded classes during runtime.
I also thought about writing a wrapper class that holds the int ID
_and_ the vtkActor without extending vtkActor. That would be shorter
but kind of dirty in my understanding.
The book also says you have to implement the copy constructor, the
operator=, and some other methods when deriving from a vtk class, but
in the "own factory" example these methods aren't implemented. So now
I'm confused _which_ methods really _have to_ be implemented and which
are optional. (In general the book is a little cursory IMO.)
There have to be people who already derived from vtk classes, I'm sure.
So does any of you have any recommendations...?
Ciao!
Wiebke
More information about the vtkusers
mailing list