Getting 2d coordinates from 3d point?

Hello, I have some additional points that I’ve added into my own implementation of the vtkChartXYZ. Actually, they are the mid points of all of the vertices of the 3d box that bounds the chart. What I would like to be able to do is work out the x,y 2d coodinates of those points (from the perspective of the viewer).

Anyone got any suggestions on how I should do this?

I have this method that I call from the vtkChartXYZ::Paint method. I want to get the missing bit that I’ve written a comment for:

void vtkChartXYZ::DrawAxesMidPoints(vtkContext2D* painter) {
    vtkContext3D* context = painter->GetContext3D();

    context->ApplyPen(this->Pen);

    for (int axis = 0; axis < 3; axis++) {

        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                float midpoint[3];
                midpoint[0] = (axis == 0) ? 0.5 : i;
                midpoint[1] = (axis == 1) ? 0.5 : (axis == 0) ? i : j;
                midpoint[2] = (axis == 2) ? 0.5 : j;

                this->Box->TransformPoint(midpoint, midpoint);

                context->DrawPoint(vtkVector3f(midpoint));

                // x, y, z coordinates (3d)
                cout << "midpoint " << midpoint[0] << " " << midpoint[1] << " " << midpoint[2] << endl;
                
                // I want to work out the 2d viewer's perspective x and y coordinates...
            }
        }
    }

    context->ApplyPen(this->AxisPen);
}

Hi, Matthew,

I tried the opposite of you want before (picking). But for getting screen coordinates from world coordinates, likewise, I suggest taking a look at the documentation of vtkViewport class: https://vtk.org/doc/nightly/html/classvtkViewport.html . You can set the world coordinates with, say SetWorldPoint(), convert by calling WorldToDisplay() and collect the result with GetDisplayPoint(). You can access those methods via an instance of vtkRenderer (it’s a subclass of vtkViewport) that certainly exists somewhere in your program.

There are API for other types of coordinates there. See wich pair of them suits you.

regards,

Paulo

Hi Paulo,

Thanks very much for the suggestion. I shall give that a try.

Kind regards,

Matt

Hi again, your suggestion certainly helped; and I also remembered the important point: ‘read the f$%@ing manual’. What did was transform my float xyz world coordinate to a 2d view by using vtkCoordinate like this:

vtkNew coord;
coord->SetCoordinateSystemToWorld();
coord->SetViewport(this->GetScene()->GetRenderer());
coord->SetValue(world_xyz[0], world_xyz[1], world_xyz[2]);
coord->SetCoordinateSystemToDisplay();

double* xy = coord->GetValue();

Hello,

I don’t think RTFM applies here :slightly_smiling_face:. I had to scratch my head over the vtkViewport's docs for a while to figure that out. It’s likely an older design not-so-obvious to use. Picking (the opposite opperation), for instance, is more straightforward.

cheers,

Paulo