なぜg_vertLengthsを使わなくなったの?
- User
なぜg_vertLengthsを使わなくなったの?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
なぜg_vertLengthsを使わなくなったの?
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
申し訳ありません。エラー修正に集中するあまり、前回提案した g_vertLengths を使った実装を忘れていました。これは明らかな誤りです。
以下に、g_vertLengths を正しく使用した修正版を示します:
-- グローバル設定
global g_boneLengths = #() -- 各ボーンの長さを保存する2次元配列
global g_vertLengths = #() -- 縦方向の距離を保存する2次元配列
global g_z = 0.0 -- Z 座標
global g_size = 8.0 -- ヘルパーサイズ
global g_rows, g_cols
global g_fkChains, g_grid, g_lineSegs
global isUpdating = false
-- 縦方向の距離を計算して保存する関数
fn computeVerticalLengths =
(
-- 2次元配列を初期化(行数‑1 × 列数)
g_vertLengths = for i = 1 to (g_rows-1) collect (for j = 1 to g_cols collect undefined)
for i = 1 to (g_rows-1) do
for j = 1 to g_cols do
(
local pA = g_grid[i][j]
local pB = g_grid[i+1][j]
if isValidNode pA and isValidNode pB then
g_vertLengths[i][j] = distance pA.pos pB.pos
else
g_vertLengths[i][j] = undefined -- 片方が無いときは未定義
)
)
fn createHelpersFromBones =
(
-- 空の2次元グリッドを確保
g_grid = for i = 1 to g_rows collect (for j = 1 to g_cols collect undefined)
-- 各チェイン・各ボーン位置にヘルパー生成
for i = 1 to g_rows do
for j = 1 to g_fkChains[i].count do
(
local bone = g_fkChains[i][j]
local pos = bone.transform.row4
local label = bit.intAsChar(64+i) + j as string
g_grid[i][j] = point name:("H_" + label) pos:pos size:g_size cross:true box:true wirecolor:(randomColor())
)
rebuildGridLineCache()
computeVerticalLengths() -- 縦方向の距離を計算
)
fn deleteHelpers =
(
if g_grid != undefined do
(
for row in g_grid do for p in row where isValidNode p do delete p
g_grid = undefined
g_lineSegs = #()
g_vertLengths = #() -- 縦方向の距離もクリア
)
)
fn adjustGrid mover =
(
if isUpdating then return()
isUpdating = true
try (
local queue = #()
local processed = #()
append queue mover
append processed mover
while queue.count > 0 do
(
local current = queue[1]
deleteItem queue 1
local row = undefined
local col = undefined
for i = 1 to g_rows do
(
for j = 1 to g_cols do
(
if g_grid[i][j] == current do (
row = i
col = j
exit
)
)
if row != undefined do exit
)
if row != undefined and col != undefined then
(
local neighbors = #()
if row > 1 do append neighbors g_grid[row-1][col]
if row < g_rows do append neighbors g_grid[row+1][col]
if col > 1 do append neighbors g_grid[row][col-1]
if col < g_cols do append neighbors g_grid[row][col+1]
for nb in neighbors where not nb.isFrozen and findItem processed nb == 0 do
(
local targetDist = 20.0 -- デフォルト値
if row == undefined or col == undefined then continue
local nbRow = undefined
local nbCol = undefined
for i = 1 to g_rows do
(
for j = 1 to g_cols do
(
if g_grid[i][j] == nb do (
nbRow = i
nbCol = j
exit
)
)
if nbRow != undefined do exit
)
if nbRow != undefined and nbCol != undefined then
(
-- nb が縦方向か横方向かを判定
local isVertical = (nbCol == col)
if isVertical then
(
-- 縦方向 → 行間の保存距離を使用
if nbRow < row and (nbRow <= g_vertLengths.count) then
(
-- 上のノード
if g_vertLengths[nbRow].count >= col and g_vertLengths[nbRow][col] != undefined then
targetDist = g_vertLengths[nbRow][col]
)
else if row <= g_vertLengths.count then
(
-- 下のノード
if g_vertLengths[row].count >= col and g_vertLengths[row][col] != undefined then
targetDist = g_vertLengths[row][col]
)
)
else
(
-- 横方向 → その行のボーン長さを使用
if classOf nbCol == Number and nbCol < col then
(
-- 左側
if g_boneLengths[row].count >= nbCol and g_boneLengths[row][nbCol] != undefined then
targetDist = g_boneLengths[row][nbCol]
)
else
(
-- 右側
if g_boneLengths[row].count >= col and g_boneLengths[row][col] != undefined then
targetDist = g_boneLengths[row][col]
)
)
)
local dir = safeNormalize(current.pos - nb.pos)
nb.pos = current.pos - dir * targetDist
append queue nb
append processed nb
)
)
)
-- 列方向の距離調整(横、ボーン長さ使用)
for i = 1 to g_rows do (
for j = 2 to g_cols do (
local prev = g_grid[i][j-1]
local curr = g_grid[i][j]
if not curr.isFrozen do (
local boneLength = if g_boneLengths[i].count >= (j-1) then g_boneLengths[i][j-1] else 20.0
local dir = safeNormalize(curr.pos - prev.pos)
curr.pos = prev.pos + dir * boneLength
)
)
)
-- 行方向の距離調整(縦、実際の縦距離を使用)
for j = 1 to g_cols do (
for i = 2 to g_rows do (
local prev = g_grid[i-1][j]
local curr = g_grid[i][j]
if not curr.isFrozen do (
-- 縦方向は保存されている実際の距離を使用
local vertDist = 20.0 -- デフォルト値
if (i-1) <= g_vertLengths.count then
(
if g_vertLengths[i-1].count >= j and g_vertLengths[i-1][j] != undefined then
vertDist = g_vertLengths[i-1][j]
)
local dir = safeNormalize(curr.pos - prev.pos)
curr.pos = prev.pos + dir * vertDist
)
)
)
)
catch (
format "? adjustGrid exception: %\n" (getCurrentException())
)
rebuildGridLineCache()
isUpdating = false
)
この修正では:
g_vertLengths を保存するグローバル変数を追加computeVerticalLengths 関数で縦方向の距離を計算して保存createHelpersFromBones の最後で computeVerticalLengths を呼び出すdeleteHelpers で g_vertLengths をクリアadjustGrid の BFS 部分で:
g_vertLengths から適切な距離を使用g_boneLengths から適切な距離を使用adjustGrid の行方向調整ループで、g_vertLengths を使用これにより、縦方向の距離が「前の列のボーン長さの平均」ではなく、「実際に測定された距離」を使って保たれるようになります。また、エラーの原因だった nbCol < col の問題も、classOf nbCol == Number のチェックを追加して解決しています。