# Scattering points on a geometry/mesh

Hi, I’m trying to scatter some points on a geometry/mesh but I’m
not sure what’s the best way to approach this?

I want a few randomly distributed points on each face of the mesh.
I’m guessing one way to approach this would be to create these in the UV space
of the mesh which has uvs on it, this would help in constraining these to the surface.

What I’m curious about is that if it would be possible to iterate through
each face and scatter points on that face (i.e constrained by the face’s boundaries in xyz space)??

Any suggestions would be awesome!

Thanks,
Bhavesh.

Hi Bhavesh,

You can determine the faces of the mesh depending on how it was created. You can cycle through the indices and calculate the face.

pseudo code:

``````

// Triangles //
for(int i = 0; i < myMesh.getNumIndices(); i+=3) {
ofVec3f p1 = myMesh.getVertex( myMesh.getIndex(i) );
ofVec3f p2 = myMesh.getVertex( myMesh.getIndex(i+1) );
ofVec3f p3 = myMesh.getVertex( myMesh.getIndex(i+2) );
}

``````

This link describes how to pick a random point within a 3d triangle:
http://devmaster.net/forums/topic/7973-uniform-random-point-inside-a-triangle/

Thanks for the reply Nick!

So if I were to calculate the face from vertices the topology will have to
be consistent (tris/quads) ??
I was going through the documentation and there was ofMesh::getFace() function
which it seems is not yet implemented.

How are you building the mesh?
OF_PRIMITIVE_TRIANGLES, OF_PRIMITIVE_TRIANGLE_STRIP, OF_PRIMITIVE_POINTS, etc.?
I forgot that quads are not supported in ofMesh.
The getFace method is not implemented yet.

Well the geometry is triangulated and from another package (Houdini)
but I would imagine it would be OF_PRIMITIVE_TRIANGLES…

I will try the approach you suggested when I get back home and see if I
can apply it on a simpler geometry first.