[vtkusers] vtkActor2D can not display at right display point!

scofield zhu scofieldzhu.zcg at gmail.com
Sun Sep 30 23:51:44 EDT 2018


hi vtk guys:
    I write a Point2DBubble view class in order to display attached world
point at display coordinator, and the point will be seen whatever you
operate the active camera in renderer. the codes is as following:

  // head file
   class VX_LOVELYBUBBLE_API Point2DBubble : public Bubble
{
    BUBBLE_DECL(Point2DBubble, Bubble)
public:
    void setAttachedPoint(const Point3& pt) { attachedpoint_ = pt; }
    const Point3& attachedPoint()const { return attachedpoint_; }
    Double_t radius() { return radius_; }
    Point2DBubble(const Point3& worldpt, Double_t radius);
    ~Point2DBubble();

private:
    bool onRenderEvent(vtkObject*, unsigned long, void*);
    void makeActors(RenderPane& pane);
    bool addToRenderPane(RenderPane& pane);
    void removeFromRenderPane(RenderPane& pane);
    Double_t radius_;
    Point3 attachedpoint_;
    std::map<vtkRenderer*, uInt32_t> rendererdict_;
};

//source file
#include "point2dbubble.h"
#include "vtkRenderer.h"
#include "vtkSphereSource.h"
#include "vtkPolyDataMapper2D.h"
#include "vtkCoordinate.h"
#include "vtkActor2D.h"
#include "vtkCommand.h"
#include "coordconv.h"
#include "renderpane.h"
VX_NAMESPACE_BEGIN

Point2DBubble::Point2DBubble(const Point3& worldpt, Double_t radius)
    :attachedpoint_(worldpt),
    radius_(radius)
{}

Point2DBubble::~Point2DBubble()
{}

bool Point2DBubble::addToRenderPane(RenderPane& pane)
{
    if(Bubble::addToRenderPane(pane)){
        vtkRenderer* ren = pane.getRenderer().GetPointer();
        rendererdict_[ren] = ren->AddObserver(vtkCommand::StartEvent, this,
&Point2DBubble::onRenderEvent);
        return true;
    }
    return false;
}

void Point2DBubble::removeFromRenderPane(RenderPane& pane)
{
    Bubble::removeFromRenderPane(pane);
    vtkRenderer* ren = pane.getRenderer().GetPointer();
    auto it = rendererdict_.find(ren);
    if(it != rendererdict_.end()){
        ren->RemoveObserver(it->second);
        rendererdict_.erase(it);
    }
}

bool Point2DBubble::onRenderEvent(vtkObject* source, unsigned long eventid,
void* data)
{
    vtkRenderer* ren = vtkRenderer::SafeDownCast(source);
    if(ren == nullptr)
        return false;
    RenderPane* thepane = RenderPane::FindPane(ren);
    if(thepane == nullptr)
        return false;
    if(!checkConnective(*thepane))
        return false;
    Point3 displaypt = utils::WorldToDisplay(attachedpoint_, *ren);
    vtkActor2DList actors = actor2d_collector_.getActors(thepane);
    actors[0]->SetDisplayPosition(displaypt[0], displaypt[1]);
    return true;
}

void Point2DBubble::makeActors(RenderPane& pane)
{
    vtkSmartPointer<vtkSphereSource> source =
vtkSmartPointer<vtkSphereSource>::New();
    source->SetRadius(radius_);
    source->SetCenter(attachedpoint_);
    vtkSmartPointer<vtkCoordinate> coordinate =
vtkSmartPointer<vtkCoordinate>::New();
    coordinate->SetCoordinateSystemToDisplay();
    vtkSmartPointer<vtkPolyDataMapper2D> mapper2d =
vtkSmartPointer<vtkPolyDataMapper2D>::New();
    mapper2d->SetTransformCoordinate(coordinate);
    mapper2d->SetInputConnection(source->GetOutputPort());
    vtkSmartPointer<vtkActor2D> actor2d =
vtkSmartPointer<vtkActor2D>::New();
    actor2d->SetMapper(mapper2d);
    addPane2DActor(pane, newActor2DInfo(actor2d, "point2d"));
}
__________________________________________________________________________________________________________________________

now the problem is : the Actor2D does not display at position calculated by
WorldToDisplay, and it seems than the display point keep a certain display
distance offset with right display point!
develop environment: VS2015+VTK7.1.0+WIN10(64bit)
I cannot solve this problem, please give me some advice! that's will be
very appreciate for you!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://public.kitware.com/pipermail/vtkusers/attachments/20181001/75885043/attachment.html>


More information about the vtkusers mailing list