How to generate a point cloud inside a shape?

 How to generate a point cloud inside a shape?

Point cloud

If you are familiar with CAD programming and meshing or you are willing to learn about those things you may hear about point clouds. Points are the most simple thing that can be represented in a 3D domain. You cannot create any 2D object or 3D object without using points. Are points very important in CAD programming? Well, I think you already know the reason for this trivial question. If not think about this. You cannot create a line without two points; a start point and an endpoint. You cannot define a 3D object without vertices; i.e. points. You got it, right! Let’s move on to the topic.

There are different needs and use cases for point clouds. As far as I meet those requirements, point clouds are mostly used in meshing. Imagine you have to create a mesh on a surface, in other words, surface meshing. Then you need to generate a set of points on the surface by triangulating them. There are different approaches and different algorithms for that. I am not going to discuss that here. Let’s discuss that in a separate article.

Status of the point

Points inside of an object is basically need when we are trying to generate volume mesh. There are several constraints we need to think about. First, imagine this object (or shape) is a closed shell or a solid. If the object is a solid, a closed shell can be generated using that. A good example is BOX. Imagine there is a box with a hollow inside. That object is completely closed. Because of that we can fill the void inside with material and make that solid object. We can determine the status of the point as “Inside” of the object when the object is solid or can be converted into a solid. Otherwise, we are in trouble!.

Hence we can define any point in a 3D coordinate system containing the above-mentioned object (Box). There are 3 possibilities for the status of the point.
1. Point is on the out side of the object.
2. Point is on the face of the object
3. Point is inside the object.


Status of the point

If the object is not a closed shell or a solid, how can we determine whether the point is inside or outside of the object? This would be a much more tricky problem. That would be changed from the observer. I am going to avoid this kind of situation and only concentrate on closed shells and solids.

Simple solution

Our primary goal is to generate a point cloud inside the shape. But what should be the first step? Well, my steps are as follows.
I am trying to generate sample points based on the bounding box of the object and check the status of the point; inside of the object, on the face or outside of the object. Based on that I can select a set of points inside the shape and they can be used for further processing. So the approach is simple. Let’s move on sample point generation method.

Bounding box generation

Before we are going to generate sample points, we have to define the space where we want to check. The best way is to get the bounding box of the given shape. If you are not familiar with the bounding box, there are plenty of examples on the internet. There are different types of bounding boxes and their uses of them. Go through this link for more information.

Back to the point. We are using the axis-aligned bounding box for this explanation. Other types of bounding boxes may give better results. That is up to you to check the pros and cons of them. Homework!

We can get the maximum corner value and minimum corner value of the bounding box. That will help us to get an idea of the space where we need to generate points to check.


Corner points

In this image, the bounding box is the same as the given object. Can you think what is the reason for that? You are correct! Axis aligned bounding box is the same as the box which is also aligned with the axis system. To avoid confusion, I will switch to another shape, a sphere.

Sphere and bounding box

After that, we have to determine how many points need in each direction. This would be the most critical part because refinement of the mesh totally depends on these values. It is up to you to decide that. Here let's select 20 points in each direction. Then we can generate 20 x 20 x 20 = 8,000 total points.

Generating point cloud to check status


Now, we have the object, bounding box, and sample point cloud. Cool! We have everything we want. The final step is to check the status of each point and determine what are the points “Inside” the shape. We can filter them and get the points that we want.

Above is a simple explanation of generating the point cloud. Let’s move on to coding. Here I am using the Open Cascade library which is open source that use in CAD and CAE programming. The required functions for the above logic are explained hereafter.

Bounding box generation — Code
For that, you can use the following OpenCASCADE classes.
1. Bnd_Box
2. BRepBndLib

// generate bounding box
Bnd_Box bndBox;
BrepBndLib::AddOptimal(mainShape, bndBox);
double xMin = 0, yMin = 0, zMin = 0, xMax = 0, yMax = 0, zMax = 0;
bndBox.Get(xMin, yMin, zMin, xmax, yMax, zmax);


Get sample point cloud — Code
This is very easy compared to other parts. You have to logically think a little bit.

// generate point
// x, y, z division
int xPntCnt = 10, yPntCnt = 10, zPntCnt = 10;
for (int i = 0; i <= xPntCnt; i++)
{
    for (int j = 0; j <= yPntCnt; j++)
    {
        for (int j = 0; j <= yPntCnt; j++)
        {
            gp_Pnt point(
              xMin + (xMax - xMin) / xPntCnt * i,
              yMin + (yMax - yMin) / yPntCnt * j,
              zMin + (zMax - zMin) / zPntCnt * k,
              );
        }
    }
}

If you cannot understand this, try again! Do not give up easily.

Check the status of the point — Code
For that, the following OpenCASCADE class can be used. If you are more curious about those, search for yourself!
BRepClass3d_SolidClassifier

asdf

BRepClass3d_SolidClassifier classifier(topods_shape);
classifier.Perform(gp_pnt, tolerance);
TopAbs_State state = classifier.State();
if (state = TopAbs_State::TopAbs_IN)
{
    // this point is inside the shape
}

Final result

After implementing the code, we can get a point cloud inside the given shape. Keep in mind that this is not the best way to generate a point cloud inside the shape. But if you are willing to do more study on point clouds and point generation for meshing this would be a good starting point.


Post a Comment

Previous Post Next Post