thomthom / solid-inspector

Inspect and repair solid groups and components in SketchUp.
https://extensions.sketchup.com/en/content/solid-inspector%C2%B2
MIT License
18 stars 2 forks source link

comparison of Sketchup::Edge with Sketchup::Edge failed #37

Open thomthom opened 9 years ago

thomthom commented 9 years ago

Original report by me.


http://sketchup.thomthom.net/extensions/report/15

#<ArgumentError: comparison of Sketchup::Edge with Sketchup::Edge failed>

C:/Users/MajorTwip/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/tt_solid_inspector2/shell.rb:204:in `each'
C:/Users/MajorTwip/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/tt_solid_inspector2/shell.rb:204:in `min'
C:/Users/MajorTwip/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/tt_solid_inspector2/shell.rb:204:in `find_start_face'
C:/Users/MajorTwip/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/tt_solid_inspector2/shell.rb:43:in `block in resolve'
C:/Users/MajorTwip/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/tt_solid_inspector2/shell.rb:117:in `find_geometry_groups'
C:/Users/MajorTwip/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/tt_solid_inspector2/shell.rb:42:in `resolve'
[...]

Whaaat?

    # @param [Sketchup::Entities] entities
    # @param [Boolean] outside
    #
    # @return [Sketchup::Face, Nil]
    def find_start_face(entities, outside)
      # Ignore vertices with no faces attached.
      vertices = Set.new
      entities.grep(Sketchup::Edge) { |edge|
        vertices.merge(edge.vertices)
      }
      vertices.delete_if { |vertex| get_faces(vertex).empty? }
      return nil if vertices.empty?

      # 1) Pick the vertex (v) with max z component.
      max_z_vertex = vertices.max { |a, b|
        a.position.z <=> b.position.z
      }

      # 2) For v, pick the attached edge (e) least aligned with the z axis.
      edges = max_z_vertex.edges.delete_if { |edge| get_faces(edge).empty? }
      edge = edges.min { |a, b|
        edge_normal_z_component(a) <=> edge_normal_z_component(b)
      }

      # 3) For e, pick the face attached with maximum |z| normal component.
      face = get_faces(edge).max { |a, b|
        face_normal(a).z.abs <=> face_normal(b).z.abs
      }

      # 4) reverse face if necessary.
      if outside
        reverse_face(face) if face_normal(face).z < 0
      else
        reverse_face(face) if face_normal(face).z > 0
      end

      face
    end

    # @param [Sketchup::Edge] edge
    #
    # @return [Float]
    def edge_normal_z_component(edge)
      edge.line[1].z.abs
    end
thomthom commented 9 years ago

http://stackoverflow.com/a/12802427

If one of both Floats is NaN then you get this message. So, prevent the NaN case.


http://rubywtf.com/

When [a,b].min != a < b ? a : b

$ irb
1.9.3p327 :001 > Float::NAN < 1.1
=> false 
1.9.3p327 :002 > [Float::NAN, 1.1].min
ArgumentError: comparison of Float with Float failed
It would be more robust if min used <….