简体   繁体   English

有人可以解释 vertices.flatten 吗? 在 SketchUp Ruby 给我?

[英]Could someone explain vertices.flatten! in SketchUp Ruby to me?

Below is a piece of code (credit to Rafael Rivera) which plots points at the vertices of a model in SketchUp.下面是一段代码(来自 Rafael Rivera),它在 SketchUp 中绘制了 model 顶点处的点。

def pointplot
    model = Sketchup.active_model
    entities = model.active_entities
    selection = model.selection
    edges = selection.grep(Sketchup::Edge)

    if edges.empty?
      msg = 'Select one or more edges before using this tool.'
      UI.messagebox(msg)
      return
    end

    vertices = []
    edges.each { |edge| vertices << edge.vertices }
    vertices.flatten!
    vertices.uniq!
    vertices.each { |vertex| entities.add_cpoint vertex.position }
end

def check_line
    sel = Sketchup.active_model.selection
    ok = sel.find { |e| e.typename == "Edge" }
    ok ? MF_ENABLED : MF_GRAYED
end

UI.add_context_menu_handler do |menu|

menu.add_separator
item = menu.add_item("Point Plot") { pointplot }

menu.set_validation_proc(item) {check_line}
end

Could someone please explain to me this line of code, what it actually does and why it's necessary for the code to work.有人可以向我解释这行代码,它实际上做了什么以及为什么代码工作是必要的。

vertices.flatten!

I am aware what ".flatten."我知道什么是“.flatten”。 does under normal circumstances.正常情况下做的。 I understand this example perfectly from the rubyapi.org我从 rubyapi.org 完全理解这个例子

a = [ 0, [ 1, [2, 3], 4 ], 5 ]
a.flatten!(1) # => [0, 1, [2, 3], 4, 5]
a = [ 0, [ 1, [2, 3], 4 ], 5 ]
a.flatten!(2) # => [0, 1, 2, 3, 4, 5]
a = [ 0, [ 1, [2, 3], 4 ], 5 ]
a.flatten!(3) # => [0, 1, 2, 3, 4, 5]
[0, 1, 2].flatten!(1) # => nil

But in the world of SketchUp, what does ".flatten?"但在 SketchUp 的世界里,“.flatten”是什么意思? actually do?实际上呢?

I 'put' the vertices array to my console and I see this as output.我将顶点数组“放入”我的控制台,我看到它是 output。

#<Sketchup::Vertex:0x00000180a0788440>
#<Sketchup::Vertex:0x00000180a0788418>
#<Sketchup::Vertex:0x00000180a07883c8>
#<Sketchup::Vertex:0x00000180a07883a0>
#<Sketchup::Vertex:0x00000180a0788440>
#<Sketchup::Vertex:0x00000180a0788418>
#<Sketchup::Vertex:0x00000180a07883c8>

So what am I 'flattening' exactly?那么我到底在“扁平化”什么?

Thanks!谢谢!

It does exactly the same as the behavior you already observed with flatten with the only difference that it changes the object on which it is called instead of returning a changed object.它与您已经观察到的flatten行为完全相同,唯一的区别是它更改了调用它的 object,而不是返回更改后的 object。

Let's have a look at these three lines:让我们看看这三行:

vertices = []
edges.each { |edge| vertices << edge.vertices }
vertices.flatten!

First, there is an empty array created.首先,创建一个空数组。 Then by iterating over all edges the edges' vertices (which are very likely are stored in an array) are added to the array.然后通过遍历所有边,将边的顶点(很可能存储在数组中)添加到数组中。 That means after this line you have a nested array of vertices that looks like this (pseudo-code):这意味着在这一行之后你有一个嵌套的顶点数组,看起来像这样(伪代码):

[[vertice_1, vertice_2], [vertice_3, vertice_4], [vertice_1, vertice_4]]

vertices.flatten! will then flatten the vertices to:然后会将vertices展平为:

[vertice_1, vertice_2, vertice_3, vertice_4, vertice_1, vertice_4]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM