[vtk-developers] Object/Graphics Factories

Marcus D. Hanwell marcus.hanwell at kitware.com
Wed May 9 11:02:07 EDT 2012


Hi,

On Sun, May 6, 2012 at 7:47 AM, Sason Ohanian Saki <sason at kth.se> wrote:
> Hi!
> I have a question about object factories in VTK. I basically want to change what graphics objects are created in vtkGraphicsFactory. From what I can tell, in the createInstance method of GraphicsFactory, the rendering implementations are chosen depending on what macros are defined. An then you simply compare strings and choose an implementation.

The thing you may not have noticed is the first few lines of CreateInstance,

  // first check the object factory
  vtkObject *ret = vtkObjectFactory::CreateInstance(vtkclassname);
  if (ret)
    {
    return ret;
    }

That will first check the object factory overrides, and return if a
valid override is found. If not, then it continues on into the method.
In VTK master/VTK 6.0+ we have switched to use object factory
overrides and this class is being removed in favor of using the
generalized object factory framework.
>
> Lets say I want to create a new implementation of vtkRenderWindow, vtkGLES2RenderWindow and vtkIOSGLES2RenderWindow that subclass in that order. Do I have to add new macros and create those object in the Graphics Factory, or do I make another GraphicsFactory or something? The question is what is the "preferred" way of doing it? From what I understand so far I would prefer to make a new factory that GraphicsFactory delegates to, given that a macro, say "VTK_USE_GLES2" or "VTK_USE_IOS" is defined.

You should derive from vtkObjectFactory and register the new object
factory with its overrides. You can see this being done in
Rendering/OpenGL in VTK master for many of the OpenGL classes. So long
as your program ensures the object factory is registered then the
vtkGraphicsFactory will return the override you specify.
>
> Also, when it comes to vtkRenderWindow the factory calls vtkXOpenGLRenderWindow::New() where the X i presume stands for the specific platform, e.g. vtkCocaOpenGLRenderWindow. How does this work, how does it go from vtkXOpenGLRenderWindow::New() to vtkCocoaOpenGLRenderWindow::New()?

Using the CreateInstance method in vtkGraphicsFactory, or the object
factory mechanism if that has been registered. When you call
vtkRenderWindow::New() it will return the correct render window for
your platform or null if none was set up.
>
> I come from a java background so I'm used to injecting stuff with component servers, so this seems a bit unflexible and hardcoded to me, but I might be missing something and/or don't have enough c++ skills. Any thoughts?

Hopefully the above makes things clearer.

Marcus



More information about the vtk-developers mailing list