Question: Who here uses PolygonMesh?

Ref: Class: Geom::PolygonMesh — SketchUp Ruby API Documentation

If you use Geom::PolygonMesh, what do you use it for?

  • Getting the triangulation of a face?
  • Generating meshes via entities.fill_from_mesh?
    • Do you create your mesh by providing the number of points and polygons you will populate the mesh with?
    • What extensions do you use this with?

I’ve never used it.

I have used it rarely… but not lately…
see my extrudeEdgesByEdges - used to add a triangulated mesh…
see my OBJ_importer - used when user chooses to import it as a mesh…

I used it to highlight irregular faces with holes in some of my unreleased plugins
Roughly so like:

class SomeOfMyTool
  def somemethod
    #some code
    @face = forexample_picked_face
  end

  def draw(view)
    mesh = @face.mesh
    (1..mesh.count_polygons).each{|i|
      points = mesh.polygon_points_at(i)
      view.drawing_color = "red"
      view.draw(GL_POLYGON, points)
    }
  end
end

I use it, for everything that is import/export related.
In 3DBI I use it to get the triangulation of each Face.
In SoundShaper, for example, I use mesh = Geom::PolygonMesh.new(point_count, poly_count) and mesh.add_polygon(index_a, index_b, index_c) and finally entities.add_faces_from_mesh(mesh, 0, nil, nil)

I assume that every ruby geometry importer will use it at some point, since lots of these files are mesh based.

I use it for everything and everywhere because it’s very fast (except for add_point which I’m given to understand is optimized in the c API).

Flowify for example builds the target geometry from scratch using a polygonMesh (which is probably a mistake all things considered…).

A typical and interesting example which I’m currently struggle with is for terrains. Let’s say we have random 100x100 quad terrain and we want to flip diagonals in order to smooth out the mesh. If the terrain is “random” we may need to flip ~50% of the diagonals. This takes roughly a minute using entities.erase and entities.add.

Now, if we just throw away the whole terrain and rebuild it from scratch using a PolygonMesh we get a 30x performance increase.

These kinds of performance gains makes it very compelling to use.

Do you create the mesh by providing the number of points and polygons you expect to populate it with?

They might not, because PolygonMesh has it’s own limitations of not making it easy to control per-face properties. Non-seamless UV mapping is also not possible. One would have to figure out afterwards what each created face maps to and adjust accordingly. This can cause people to use entities.add_face since it has more control despite its performance issues.

1 Like

I use it a lot

  1. For accessing the triangulation of faces. This is useful when you need to draw3d a face in the viewport, because the OpenGL primitive for drawing polygons assumes the polygon is convex.
  2. Whenever I need to create geometry, because it is a lot faster. I usually use triangle polygons for the mesh.

On this latter point, it would be good to find a way to cross-reference the generated faces and the original polygon in the mesh. Currently, I assume that the list of polygons and of faces are in the same order. But this may not be always true (when for whatever reason a face cannot be created), and furthermore, is not contractual in the API. This would help a lot (as would anything helping in being efficient to create pre-calculated geometry).

1 Like

+1000

1 Like

Yes, if the quantities are known.

1 Like

Yea, I’ve run into the same myself. In SUbD I’m also relying on the order of creation, which scares me. And I have the issue of when small faces fail to create then I’m not able to gracefully recover.

And do you initialize the mesh with the number of points you will be creating?

Some times one can do a rough estimate, not not a 100% accurate estimate. What do you do then. Provide a ballpark value, or omit it?

I usually omit it unless the quantities are known. Internally I suppose it works something like an array list - in case of overflow, allocate a new array twice the size and copy over all elements - which performes in a few ms even on large arrays, so in the grand scheme of things I’ve never been able to detect any performance gains from providing the numbers.

However, add_point is the main bottleneck and should really be optimized (which I believe it is in the c API).

We use it extensively in several of our plugins for both triangulation and generating geometry.