How to get the number of points in an object?
-
I have this code to check if an object op is a polygonal object and if it is a plane object.
PointObject* polyop; Bool all_ok = false; Bool isPoly = (op->GetType() == Opolygon); if (isPoly) { polyop = static_cast<PointObject*>(op); all_ok = true; } Bool isPlane = op->GetType() == Oplane; if (isPlane) { polyop = ToPoint(op); all_ok = true; } if (all_ok == false) return Vector(0); Int32 pcnt = polyop->GetPointCount(); GePrint(maxon::ToString(pcnt, nullptr, false)); if (pcnt == 4) return Vector(1); ...
If it is a polygonal object, I can easily check the number of points. But if it is a plane, I always get a count of 0 (zero) points, even if I convert the plane into a polygonal object, with ToPoly.
How can I get a polygonal version of the object? -
Hi @rui_mac ,
you can distinguish between different types of the
BaseObject* op
using GetType() function, this part is correct in your code.If
op->GetType() == Opolygon
gives youtrue
, it means that your op is actually a polygon object, so you can easily cast it to PolygonObject* using one of the following approaches:- static_cast<PolygonObject*>(op)
- ToPoly(op), which is just an alias for the 1.
Since PolygonObject is inherited from the PointObject, the same stands for PointObject class too:
- static_cast<PointObject*>(op)
- ToPoint(op), just an alias for the 1.
In case your op is effectively Oplane (or any other object that's not a raw polygon), you're not able to easily access its points, because sometimes there's an entire hierarchy hidden behind the op. In this case you'd need to bake your object to a single polygon object. There're actually two ways you can follow from this point:
- The naive and very expensive one. You can use the SendModelingCommand to execute "Current State to Object" command on your object. This is slow and inefficient, but is a low hanging fruit, since these couple of lines would cover all the edge cases.
- Access objects geometry cache yourself. By this I mean using GetCache() and GetDeformCache() functions. This approach requires proper coding, since you can have interesting structures there and easily hit some edge cases. However, it will be way faster and more efficient, comparing to 1., especially if you expect it to work with simple objects like Oplane.
Both approaches have been already extensively discussed on the forum, so you shouldn't have problems searching for example code snippet. Additionally, we have great examples (although using Python API, which in this case will be very similar to C++ API) for both scenarios:
SendModelingCommand: Current State to Object
Geometry CachesCheers,
Ilia -
Thank you very much, Ilia.
I will look into both approaches.
I only have to check it at the begining of the calculations of the shader, so the best way should be to make in the InitRender method, right? That way, all the possibly "slow" stuff only gets calculated once.