なぜ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 = #() -- 縦方向(行間)の距離を保存する配列
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 =
(
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
)
)
-- ヘルパーを生成&グリッド配列に配置
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 = #() -- ★ クリア
)
)
-- adjustGrid(BFS + 正確な距離対応)
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 -- デフォルト値
local nbRow = undefined
local nbCol = undefined
-- nbの位置を特定
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
(
-- 横方向の隣接 (同じ行)
if nbRow == row then
(
if nbCol < col and g_boneLengths[row].count >= nbCol then
targetDist = g_boneLengths[row][nbCol]
else if col < nbCol and g_boneLengths[row].count >= col then
targetDist = g_boneLengths[row][col]
)
-- 縦方向の隣接 (同じ列) ★ g_vertLengths を使用
else if nbCol == col then
(
if nbRow < row and g_vertLengths[nbRow] != undefined and g_vertLengths[nbRow][col] != undefined then
targetDist = g_vertLengths[nbRow][col]
else if row < nbRow and g_vertLengths[row] != undefined and g_vertLengths[row][col] != undefined then
targetDist = g_vertLengths[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
)
)
)
-- 行方向の距離調整(縦、保存された縦距離使用) ★ g_vertLengths を使用
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 and isValidNode prev and isValidNode curr do (
local targetDist = 20.0 -- デフォルト
-- 保存された縦距離を使用
if g_vertLengths[i-1] != undefined and g_vertLengths[i-1][j] != undefined then
targetDist = g_vertLengths[i-1][j]
local dir = safeNormalize(curr.pos - prev.pos)
curr.pos = prev.pos + dir * targetDist
)
)
)
)
catch (
format "? adjustGrid exception: %\n" (getCurrentException())
)
rebuildGridLineCache()
isUpdating = false
)
computeVerticalLengths 関数の追加: ヘルパー生成後に縦距離を実測して保存
createHelpersFromBones で縦距離計算: ヘルパー生成直後に computeVerticalLengths() を呼び出し
deleteHelpers で縦距離クリア: g_vertLengths = #() でクリア
adjustGrid で正確な距離使用:
g_vertLengths[row][col] を使用g_vertLengths[i-1][j] を使用これで、横方向はボーンの実際の長さ、縦方向はヘルパー間の実測距離 を正確に保持するようになります。