Class: MeshNormalAnalyzer
- Inherits:
-
Object
- Object
- MeshNormalAnalyzer
- Defined in:
- lib/ruby3mf/mesh_normal_analyzer.rb
Instance Method Summary collapse
- #compare_normals(triangle, hit_direction) ⇒ Object
- #found_inward_triangle ⇒ Object
-
#initialize(mesh) ⇒ MeshNormalAnalyzer
constructor
A new instance of MeshNormalAnalyzer.
- #intersect(point, direction, triangle) ⇒ Object
- #process_triangle(point, direction, triangle) ⇒ Object
Constructor Details
#initialize(mesh) ⇒ MeshNormalAnalyzer
Returns a new instance of MeshNormalAnalyzer.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/ruby3mf/mesh_normal_analyzer.rb', line 3 def initialize(mesh) @vertices = [] @intersections = [] vertices_node = mesh.css("vertices") vertices_node.children.each do |vertex_node| if vertex_node.attributes.count > 0 x = vertex_node.attributes['x'].to_s.to_f y = vertex_node.attributes['y'].to_s.to_f z = vertex_node.attributes['z'].to_s.to_f @vertices << [x, y, z] end end @triangles = [] triangles_node = mesh.css("triangles") triangles_node.children.each do |triangle_node| if triangle_node.attributes.count > 0 v1 = triangle_node.attributes['v1'].to_s.to_i v2 = triangle_node.attributes['v2'].to_s.to_i v3 = triangle_node.attributes['v3'].to_s.to_i @triangles << [v1, v2, v3] end end end |
Instance Method Details
#compare_normals(triangle, hit_direction) ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/ruby3mf/mesh_normal_analyzer.rb', line 98 def compare_normals(triangle, hit_direction) oriented_normal = cross_product( vector_to(triangle[0], triangle[1]), vector_to(triangle[1], triangle[2])) angle = angle_between(oriented_normal, hit_direction) angle < Math::PI / 2.0 end |
#found_inward_triangle ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/ruby3mf/mesh_normal_analyzer.rb', line 29 def found_inward_triangle # Trace a ray toward the center of the vertex points. This will hopefully # maximize our chances of hitting the object's trianges on the first try. center = point_cloud_center(@vertices) @point = [0.0, 0.0, 0.0] @direction = vector_to(@point, center) # Make sure that we have a reasonably sized direction. # Might end up with a zero length vector if the center is also # at the origin. if magnitude(@direction) < 0.1 @direction = [0.57, 0.57, 0.57] end # make the direction a unit vector just to make the # debug info easier to understand @direction = normalize(@direction) attempts = 0 begin # Get all of the intersections from the ray and put them in order of distance. # The triangle we hit that's farthest from the start of the ray should always be # a triangle that points away from us (otherwise we would hit a triangle even # further away, assuming the mesh is closed). # # One special case is when the set of triangles we hit at that distance is greater # than one. In that case we might have hit a "corner" of the model and so we don't # know which of the two (or more) points away from us. In that case, cast a random # ray from the center of the object and try again. @triangles.each do |tri| v1 = @vertices[tri[0]] v2 = @vertices[tri[1]] v3 = @vertices[tri[2]] process_triangle(@point, @direction, [v1, v2, v3]) end if @intersections.count > 0 # Sort the intersections so we can find the hits that are furthest away. @intersections.sort! {|left, right| left[0] <=> right[0]} max_distance = @intersections.last[0] furthest_hits = @intersections.select{|hit| (hit[0]-max_distance).abs < 0.0001} # Print out the hits # furthest_hits.each {|hit| puts hit[1].to_s} found_good_hit = furthest_hits.count == 1 end if found_good_hit outside_triangle = furthest_hits.last[2] else @intersections = [] attempts = attempts + 1 target = [Random.rand(10)/10.0, Random.rand(10)/10.0, Random.rand(10)/10.0] @point = center @direction = normalize(vector_to(@point, target)) end end until found_good_hit || attempts >= 10 # return true if we hit a triangle with an inward pointing normal # (according to counter-clockwise normal orientation) found_good_hit && !compare_normals(outside_triangle, @direction) end |
#intersect(point, direction, triangle) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/ruby3mf/mesh_normal_analyzer.rb', line 121 def intersect(point, direction, triangle) v0 = triangle[0] v1 = triangle[1] v2 = triangle[2] return [false, 0] if v0.nil? || v1.nil? || v2.nil? e1 = vector_to(v0, v1) e2 = vector_to(v0, v2) h = cross_product(direction, e2) a = dot_product(e1, h) if a.abs < 0.00001 return false, 0 end f = 1.0/a s = vector_to(v0, point) u = f * dot_product(s, h) if u < 0.0 || u > 1.0 return false, 0 end q = cross_product(s, e1) v = f * dot_product(direction, q) if v < 0.0 || u + v > 1.0 return false, 0 end t = f * dot_product(e2, q) [t > 0, t] end |
#process_triangle(point, direction, triangle) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/ruby3mf/mesh_normal_analyzer.rb', line 108 def process_triangle(point, direction, triangle) found_intersection, t = intersect(point, direction, triangle) if t > 0 intersection = [] intersection[0] = point[0] + t * direction[0] intersection[1] = point[1] + t * direction[1] intersection[2] = point[2] + t * direction[2] @intersections << [t, intersection, triangle] end end |