Vertex Normals... ?
-
@oajfh said:
ThomThom, doesn't the add_polygon method simply add any new points at the end of the point array ? That's the behaviour it's exhibited for me so far, so wouldn't applying what Dan said be alright ?
PolygonMesh.add_point will add a new point only if there isn't an existing point for the same location (within tolerance). This means it looks up the existing points (which unfortunately is a linear look up.)
PolygonMesh.add_polygon will internally call this same function.The linear nature of add_point I assume is because internally this class is for use with individual faces - so you're not going to have that many points nor duplicates.
To get max performance when building a PolygonMesh you add all the points first - collecting the indices you get in return. Then add polygons using indices instead of points - this reduce the number of linear lookups.
I usually use a Hash to build a source => polygon_point_index lookup. This can be vertex => index for instance.
-
Btw - if you build your own PolygonMesh from scratch the vertex normals won't be correct I believe...
-
I'm building it from an existing component, so I'd simply be copying existing normals. Why would the vertex normals be incorrect ?
Also, are you sure about the point to point comparison ? If you create different groups within a component, won't it duplicate the points ? I was getting different results after exploding groups and creating the mesh after making a component out of the exploded ex-groups. So the comparison might not only be coordinate-based.
Thanks for the tip on performance, too. Will come in handy.
-
@oajfh said:
I'm building it from an existing component, so I'd simply be copying existing normals. Why would the vertex normals be incorrect ?
If you query mesh.normal_at(i) on a mesh you created yourself (Geom::PolygonMesh.new) vs face.mesh the normals won't be computed for you.
@oajfh said:
Also, are you sure about the point to point comparison ? If you create different groups within a component, won't it duplicate the points ?
You can observer this in action by inspecting the indices and point count:
<span class="syntaxdefault"><br />mesh </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">PolygonMesh</span><span class="syntaxkeyword">.new<br /></span><span class="syntaxcomment">#<Geom;;PolygonMesh;0x0000000936e850><br /><br /></span><span class="syntaxdefault">index1 </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">mesh</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_point</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Point3d</span><span class="syntaxkeyword">.new(</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">2</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">3</span><span class="syntaxkeyword">))<br /></span><span class="syntaxdefault">1<br /><br />index2 </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">mesh</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_point</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Point3d</span><span class="syntaxkeyword">.new(</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">2</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">3</span><span class="syntaxkeyword">))<br /></span><span class="syntaxdefault">1<br /><br />index3 </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">mesh</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_point</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Point3d</span><span class="syntaxkeyword">.new(</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">2</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">3.5</span><span class="syntaxkeyword">))<br /></span><span class="syntaxdefault">2<br /><br />mesh</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">count_points<br />2<br /></span>
Or by adding polygons using 3d points:
<span class="syntaxdefault"><br />mesh </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">PolygonMesh</span><span class="syntaxkeyword">.new<br /></span><span class="syntaxcomment">#<Geom;;PolygonMesh;0x0000000a70ec48><br /><br /></span><span class="syntaxdefault">mesh</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_polygon</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Point3d</span><span class="syntaxkeyword">.new(</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">), </span><span class="syntaxdefault">Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Point3d</span><span class="syntaxkeyword">.new(</span><span class="syntaxdefault">5</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">), </span><span class="syntaxdefault">Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Point3d</span><span class="syntaxkeyword">.new(</span><span class="syntaxdefault">5</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">5</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">))<br /></span><span class="syntaxdefault">1<br /><br />mesh</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_polygon</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Point3d</span><span class="syntaxkeyword">.new(</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">), </span><span class="syntaxdefault">Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Point3d</span><span class="syntaxkeyword">.new(</span><span class="syntaxdefault">5</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">), </span><span class="syntaxdefault">Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Point3d</span><span class="syntaxkeyword">.new(</span><span class="syntaxdefault">5</span><span class="syntaxkeyword">,-</span><span class="syntaxdefault">5</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">))<br /></span><span class="syntaxdefault">2<br /><br />mesh</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">count_polygons<br />2<br /><br />mesh</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">count_points<br />4<br /></span>
-
@oajfh said:
I was getting different results after exploding groups and creating the mesh after making a component out of the exploded ex-groups. So the comparison might not only be coordinate-based.
When you explode the coordinates of the vertices are transformed to it's parents - so you will see different values then before they they where local to the component definition.
(Also, entities in model.active_entities) are transformed into global coordinates.) -
Well, after more experimenting, taking into account what's been said :
# Original credit ; Anton_S # Get group/component entities. # @param [Sketchup;;Group, Sketchup;;ComponentInstance] entity # @return [Sketchup;;Entities] def get_entities(entity) if entity.is_a?(Sketchup;;ComponentInstance) return entity.definition.entities else return entity.entities end end # Original credit ; Anton_S # Get triangular mesh of the group. # @param [Sketchup;;Group, Sketchup;;ComponentInstance] entity # @param [Boolean] recursive Whether to include all the child groups and # components. # @param [Boolean] transform Whether to give points in global coordinates. # @param [Integer] Keep face counter so as not to mess up normal exporting # @yield A procedure to determine whether particular child group/component # should be considered a part of the collection. # @yieldparam [Sketchup;;Group, Sketchup;;ComponentInstance] entity # @yieldreturn [Boolean] Pass true to consider an entity as part of the # collection. Pass false to not consider an entity as part of the # collection. # @return [Geom;;PolygonMesh] def get_triangular_mesh(entity, recursive = true, transform = false,normal_array, &entity_validation) mesh = Geom;;PolygonMesh.new if( entity.is_a?(;;Sketchup;;Face)||entity.is_a?(;;Sketchup;;ComponentInstance)||entity.is_a?(;;Sketchup;;Group)) get_entities(entity).each { |e| if e.is_a?(;;Sketchup;;Face) e.mesh.polygons.each_index{ |i| pts = e.mesh.polygon_points_at(i+1) mesh_b = e.mesh 4 #Temp face with normals index = mesh.add_polygon(pts) #puts "e" for k in 1..mesh_b.count_points #puts "k" #puts mesh_b.point_at(k) #Not global coordinates #puts mesh_b.normal_at(k) index = mesh.point_index(mesh_b.point_at(k)) # Get index of current point being examined #puts index #puts mesh.point_at(index) #puts "k" normal_array[index] = mesh_b.normal_at(k) # Set that point's normal to the current point's normal's value end #puts "j" } elsif recursive && (e.is_a?(;;Sketchup;;Group) || e.is_a?(;;Sketchup;;ComponentInstance)) && entity_validation.call(e) new_array = [] mesh2 = get_triangular_mesh(e, true, true, new_array, &entity_validation) mesh2.polygons.each_index { |i| index = mesh.add_polygon(mesh2.polygon_points_at(i+1)) mesh2.points.each_with_index{|point, index| ind = mesh.point_index(point) normal_array[ind]= new_array[index] } } end } mesh.transform!(entity.transformation) if transform for l in 1..normal_array.length-1 #index 0 not used so length is 1 more than expected #puts l normal_array[l].transform!(entity.transformation) if transform end end return mesh end normal_array = [] # Contains the VERTEX normals. mesh = get_triangular_mesh(group, true, true,normal_array){ |e| true }
(This code is bad and unoptimised, I'm aware of that. I'm a terrible programmer)
I'm not sure what to think of this.
If I look at what vertex normals are returned during recursion, I occasionally get different normals for what appears to be the same vertex, and I don't know why. For a cube, I only get global axis-aligned vectors, which isn't really what I was expecting.
-
I just wanted to correct my post. A PolygoMesh can calculate normals if the mesh is generated from a face. Read further for more details.
So, what were you expecting?
A PolygonMesh is just a container for points and triangles. It does not calculate normals.
You can retrieve the vertex normals of a mesh when the mesh is made from an existing Face, but those normals will be the normal of the Face and not any calculated value. This is why you get multiple normals from a vertex in your code and why they are all aligned with each Face normal. The normals being aligned to the global axis is because your cube faces are aligned to the global axes, am I right?
model = Sketchup.active_model sel = model.selection ents = model.active_entities faces = ents.grep(Sketchup;;Face) faces.each do |face| mesh = face.mesh(4) for idx in (1..mesh.count_points) norm = mesh.normal_at idx pt = mesh.point_at idx pt2 = pt.offset norm ents.add_cline(pt, pt2) end end
-
Yes, you are absolutely right. I hadn't quite understood what Thomthom meant. Sorry for dragging this topic on for so long.
-
Sorry - I should have clarified - the vertex normals depend on whether the edge between the faces are Smooth. Soft doesn't matter - because only Smooth affect shading. The normal is a representation of the surface shading.
@jim said:
[...] but those normals will be the normal of the Face and not any calculated value. This is why you get multiple normals from a vertex in your code and why they are all aligned with each Face normal.
They are calculated if and only if the mesh came from
face.mesh
. When building your own there will be no calculation. This is because PolygonMesh itself doesn't do the calculation, but SketchUp will set the vertex normals as it builds the mesh returned toface.mesh
.Under the hood there is a set_normal function similar to set_uv. But this has never been exposed - probably because entities.add_faces_from_mesh and entitites.fill_from_mesh would never use it.
-
Well that's interesting. Thanks.
-
@oajfh said:
Sorry for dragging this topic on for so long.
Don't worry about it.
This is the kind of nitty-gritty dirty details us consummate geeks really love to ponder.
Advertisement