3D Shapes : polygon

I’m very sorry, although I asked this question before, I still don’t know how to draw thousands of 3D polygons using root. I saw the TPolyLine3D class, but it is for drawing lines. I want to know why there is no TPolygon3D class, or is there any other way to achieve 3D polygon drawing? Sorry for asking again.


ROOT supports the drawing of sophisticated detector geometries.
Can these code examples be of inspiration ROOT: Geometry tutorials ?


I have looked at examples, but I don’t know which one suits me! I only found the TGeoPolygon class, but it is a 2D polygon class!

I used multiple TGraph2D to draw multiple triangles, but there was a memory leak and the resulting image was a bit strange

I don’t understand why there are so many dotted lines that seem to come out from the origin of the windows

    auto c = new TCanvas("c", "Graph2D example", 0, 0, 600, 400);

    for each (QString line in l)
        QStringList s = line.split(" ", Qt::SkipEmptyParts);

        TH2F* h1 = new TH2F("h1", "h1", 10, minx-0.5, maxx+0.5, 10, miny-0.5, maxy+0.5);
        auto dt = new TGraph2D(3);
        dt->SetPoint(0, s[0].toDouble(), s[1].toDouble(), s[2].toDouble());
        dt->SetPoint(1, s[3].toDouble(), s[4].toDouble(), s[5].toDouble());
        dt->SetPoint(2, s[6].toDouble(), s[7].toDouble(), s[8].toDouble());
        if (N == 0)
            dt->Draw("TRI SAME");

And it seems that this way of writing cannot be exported to PDF format files

To draw 3D shapes, the best is to use TGeo shapes. Do you have an example/picture showing what you want to achieve ? @agheata can help you with TGeo.

Thank you for the reminder. I have the final result chart with two effects

One is to draw a face based on the coordinates of a 3D triangle, and the other is to draw a face based on the coordinates of a 3D rectangle. Although the colors of each face in the two result maps are now the same, I want to achieve the goal of drawing a face color based on the color band. I’m not sure if it’s feasible.

That’s not TGraph2D data style. TGraph2D is a 3D curve … not a shape. I let your see with @agheata to provide you examples.

Ok thanks, looking forward to examples

Is there a specific example that can be provided?

Hi, I don’t know if you want to draw many rectangles with 3D vertices, or many 3D boxes. If you just rectangles, you should use TPolyLine3D like:

auto pl = new TPolyLine3D(5); // need to repeat first point for closure
pl->SetNextPoint(-1, -1, 0);
pl->SetNextPoint(-1, 1, 0);
pl->SetNextPoint(1, 1, 0);
pl->SetNextPoint(1, -1, 0);
pl->SetNextPoint(-1, -1, 0);

If you want filled boxes, you need to create a TGeoManager geometry and position boxes defined as volumes. There are examples in tutorials/geom such as rootgeom.C. You can also have a look in the Users Guide for more details on how to construct such geometry.

1 Like

Yes, I want to draw a rectangle with five vertices or a triangle with four vertices, but of course, I need to fill in the colors into the rectangle or triangle. I took a look at the TGeoVolume class, which mainly focuses on drawing cubes, but I don’t think it fits my needs. TPolyLine3D cannot fill colors.

There is no way do draw filled plans in 3D. Only 3D polylines. The reason is that with filled plans you
need to implement some algorithm to draw them in the right order. This in not there.

Perhaps I didn’t realize the essence of the problem. According to my understanding, we can obtain a closed polygon by having the same starting and ending coordinates, and then set the color of each polygon through SetFillColor. Isn’t that enough?

By the way, can I extend and implement a TPolygon3D class myself? Is there a similar example?

No it is not … as it is filled you need to draw the polygons in the right order … back to front …

We do not need that for lines of courses

You can try by you will face the ordering problem.

I thought about it for a moment. If Polygon does seem quite complex, do we need to do triangulation? But my current needs are actually in two situations: rectangular and triangular. Do I need to modify the source code or can I extend it through inheritance

Even for triangles or rectangles you will need anyway to draw the polygons in the right order (back to front) otherwise you may have some polygons in the back hiding some in front.

In fact, both of the above figures are implemented using Matlab’s Patch function, which can draw polygons based on given polygon vertices and assign colors. I don’t know what the original implementation is, is there a similar way to implement root? If not, do you know of any other open source libraries that can implement similar functions.

No I do not know.