3Dモデルを計算し、同一の頂点を組み合わせるスクリプトを見つけました。これには次のロジックがあります。私の理解によれば、頂点は頂点クラスのハッシュマップです。
unless vertices.key?(vertex)
new_vertices << vertex
vertices[ vertex ] = @vertex_index
@vertex_index += 1
end
vertex
一意のが見つかった場合は、それをnew_vertices
配列に追加します。
ハッシュマップのキーが頂点とマテリアルの組み合わせになるようにこれを変更したいと思います(どちらも、このスクリプトが実行されるソフトウェアであるSketchupのクラスです)。各キーが1つではなく2つのクラスの組み合わせになるようにこれを行うための最良の方法は何ですか?頂点とマテリアルの両方を保持しているある種の複製またはクラス?それはハッシュマップでサポートされていますか?
Rubyでは、ハッシュキーとして何でも使用できます。
hash = {
42 => "an integer",
[42, "forty two"] => "an array",
Class => "a class"
}
#⇒ {42=>"an integer", [42, "forty two"]=>"an array", Class=>"a class"}
hash[[42, "forty two"]]
#⇒ "an array"
そうは言っても、あなたの場合、配列[vertex, material]
をキーとして使用するかもしれません:
unless vertices.key?([vertex, material])
new_vertices_and_materials << [vertex, material]
vertices[[vertex, material]] = @vertex_index
@vertex_index += 1
end
よりルビーなアプローチはEnumerable#uniq
、入力を呼び出して次のことを行うことです。
input = [ # example input data
[:vertex1, :material1],
[:vertex2, :material1],
[:vertex2, :material1],
[:vertex2, :material2],
[:vertex2, :material2]
]
new_vertices_and_materials = input.uniq
vertices_and_materials_with_index =
new_vertices_and_materials.
zip(1..new_vertices_and_materials.size).
to_h
#⇒ {[:vertex1, :material1]=>1,
# [:vertex2, :material1]=>2,
# [:vertex2, :material2]=>3}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加