[Insight-users] Data Representation Objects

Ofri Sadowsky ofri@cs.jhu.edu
Thu, 21 Nov 2002 16:47:13 -0500


>From looking at the code, it seems that you can update the PointSet of a Mesh in any
way, regardless of cells etc. See PointSet::SetPoint() and PointSet::SetPoints(). All
that happens there is PointSet calling Modified().

The first adapter calls Modified() when it's destroyed, and thus guarantees that
afterwards all the necessary notifications were sent. Still, you can modify the
non-const vector that you obtained from the Adapter and try to access the cells without
the PointSet::Modified().
That is not so wise, bu it's possible. However, as long as you separate vector
modification from accessing the cells by at least a {block},
Modified() wil be called automatically.

As for the const reference, it's possible to follow your direction. I added the
STLConstVectorAdapter for consistency with the STLVectorAdapter. But it's possible, and
maybe simpler, to do without.

Luis Ibanez wrote:

> Hi Ofri,
>
> The code of the STL adpator looks good.
>
> It seems however that we could take better advantage
> of the natural derivation of the VectorContainer from
> the std::vector.
>
> In principle a simple casting to
>
>           const   std::vector<TElement>   &
>
> could provide the same functionality.
>
> Something like adding the following to the
> VectorContainer class:
>
>      typedef std::vector< TElement >  STLContainerType;
>
>      const STLContainerType & GetSTLContainer() const {
>        return static_cast<const STLContainerType &>( *this );
>        }
>
> With this lines you can now do:
>
>     typedef itk::VectorContainer<
>                       unsigned int,
>                       double         > VectorContainerType;
>
>     VectorContainerType::Pointer vector = VectorContainerType::New();
>
>     const std::vector<double>  & vectorref = vector->GetSTLContainer();
>
> and now use the "vectorref" as to access the content
> of the itk::VectorContainer.
>
> Note that only the "const" access is allowed here. The reason
> is that the itk container should maintain the association between
> identifiers and elements. This is quite important in the Mesh
> class for example since the identifiers to points are supplied
> to the cells. Allowing access to the std::vector under the
> PointsContainer could facilitate the removal or insertion of a
> point and this will corrupt all the identifiers stored in the
> Mesh cells.
>
> Do you see use cases in the proposed adaptor that
> may not be covered by the casting above ?
>
> Thanks
>
>     Luis
>
> ==================================================================
>
> Ofri Sadowsky wrote:
>
> > I added the following code in the public section of my copy of itkVectorContainer.h
> >
> > <<< BEGIN >>>
> >  /* forward declaration */
> >  class STLVectorAdapter;
> >  class STLConstVectorAdapter;
> >
> >  /** \class STLVectorAdapter
> >   * An adapter object that casts a VectorContainer into std::vector
> >   * and enables access to the underlying data structure. When the STLVectorAdapter
> >   * is destroyed, it automatically calls VectorContainer::Modified().
> >   * Here's a usage example of STLVectorAdapter
> >   *     VectorContainer::STLVectorAdapter vecAdapter =
> > aContainer.GetStlVectorAdapter();
> >   *     std::vector<ElementType> & vec = vecAdapter.GetStlVectorRef();
> >   *     // do things with vec ...
> >   *     // upon return from function, vecAdapter is destroyed and aContainer is
> > Modified()
> >   */
> >   class STLVectorAdapter
> >   {
> >   public:
> >    VectorType & GetStlVectorRef()
> >    {
> >     return (VectorType &)m_VectorRef;
> >    }
> >
> >    ~STLVectorAdapter()
> >    {
> >     m_VectorRef.Modified();
> >    }
> >
> >    /** public copy ctor to enable
> >     *   VectorContainer::STLVectorAdapter vecAdapter =
> > aContainer.GetStlVectorAdapter();
> >     */
> >    STLVectorAdapter(const STLVectorAdapter & r)
> >     : m_VectorRef(r.m_VectorRef) {}
> >   private:
> >    VectorContainer & m_VectorRef;
> >    friend class VectorContainer;
> >    friend class STLConstVectorAdapter;
> >
> >    /* hide ctor */
> >    STLVectorAdapter(VectorContainer & vc)
> >     : m_VectorRef(vc) {}
> >
> >    /* hide and avoid operator= */
> >    const STLVectorAdapter & operator=(const STLVectorAdapter & r);
> >   };
> >
> >
> >   /** Create a STLVectorAdapter that adapts this object to std::vector<ElementType>
> >   */
> >   STLVectorAdapter GetStlVectorAdapter()
> >   {
> >    return STLVectorAdapter(*this);
> >   }
> >
> >   /** \class STLConstVectorAdapter
> >    * An adapter object that casts a VectorContainer into const std::vector
> >    * and enables access to the underlying data structure. STLConstVectorAdapter
> >    * does not call VectorContainer::Modified(), because it is cast to const.
> >    * Here's a usage example of STLConstVectorAdapter
> >    *     VectorContainer::STLConstVectorAdapter vecAdapter =
> >    *                aContainer.GetConstStlVectorAdapter();
> >    *     const std::vector<ElementType> & vec = vecAdapter.GetStlConstVectorRef();
> >    *     // do things with vec ... but it can't be modified.
> >    */
> >   class STLConstVectorAdapter
> >   {
> >   public:
> >    const VectorType & GetStlConstVectorRef() const
> >    {
> >     return (const VectorType &)m_VectorRef;
> >    }
> >
> >    /* no need to declare dtor, as the Adapter is const */
> >
> >    /* public copy ctor. See StlVectorAdapter */
> >    STLConstVectorAdapter(const STLConstVectorAdapter & r)
> >     : m_ConstVectorRef(r.m_ConstVectorRef) {}
> >
> >    /* "copy" ctor from the non-const STLVectorAdapter */
> >    STLConstVectorAdapter(const STLVectorAdapter & r)
> >     : m_ConstVectorRef(r.m_VectorRef) {}
> >
> >   private:
> >    const VectorContainer & m_ConstVectorRef;
> >    friend class VectorContainer;
> >
> >    /* hide main ctor */
> >    STLConstVectorAdapter(const VectorContainer & vc)
> >     : m_ConstVectorRef(vc) {}
> >
> >    /* hide and avoid operator= */
> >    const STLConstVectorAdapter & operator=(const STLConstVectorAdapter & r);
> >   };
> >
> >   STLConstVectorAdapter GetStlConstVectorAdapter() const
> >   {
> >    return STLConstVectorAdapter(*this);
> >   }
> >
> > <<< END >>>
> >