RyuMaster
02-16-2012, 01:51 PM
Hi! I wrote script, which finds similar elements between LOD3 UVW Map, and LOD2 UVW Map.
But it works very slow, I wonder can I improve it somehow here and there?
The first thing I do is element extraction from UVW into arrays ( I store their indicies)
_obj = $LOD3
select _obj
_uvw = _obj.modifiers[1]
_uvw.edit()
_elementsLOD3 = process _uvw
_obj = $LOD2
select _obj
_uvw2 = _obj.modifiers[1]
_uvw2.edit()
_elementsLOD2 = process _uvw2
And that is actual function:
fn process _uvw =
(
_elements = #()
_collectVerticies = #()
for i = 1 to _uvw.NumberVertices() do
(
append _collectVerticies i
)
while _collectVerticies.count > 0 do
(
_uvw.selectVertices #{_collectVerticies[1]}
_uvw.selectElement()
_selection = _uvw.getSelectedVertices()
_selectionArr = _selection as array
for i = 1 to _selectionArr.count do
(
for z = 1 to _collectVerticies.count do
(
if (_selectionArr[i] == _collectVerticies[z]) then
(
deleteItem _collectVerticies z
)
)
)
if _selectionArr.count >1 then append _elements _selectionArr
)
return _elements
)
Then I do this:
1. I take every point (p1) for UVW LOD2 element, and find closest point to it (p2) of UVW on LOD3.
2. I see, which elements this closest point (p2) of LOD3 belongs to.
3. Based on this "weight element" info, I can predict, which element of LOD2 has closest shape to element of LOD3.
This part of code is responsible for this process:
_pares = #()
for i = 1 to _elementsLOD2.count do
(
_cArr = #()
for es = 1 to _elementsLOD2.count do append _CArr 0
for z = 1 to _elementsLOD2[i].count do
(
_n = getNearestPoint _uvw _uvw2 _elementsLOD3 _elementsLOD2[i][z]
_cArr[_n] = _cArr[_n] + 1
)
_most = -1
_indx = -1
for es = 1 to _cArr.count do
(
if _cArr[es] > _most then
(
_most = _cArr[es]
_indx = es
)
)
_tmparr = #(i, _indx)
deleteItem _elementsLOD3 _indx
append _pares _tmparr
)
And I get nearest point this way:
fn getNearestPoint _uvw _uvw2 _elementArray _point =
(
_ourPosition = _uvw2.getVertexPosition 1 _point
_curDistance = 999999
_curCandidate = -1
for i = 1 to _elementArray.count do
(
for k = 1 to _elementArray[i].count do
(
tempPos = _uvw.getVertexPosition 1 _elementArray[i][k]
_pp = distance _ourPosition tempPos
if _pp < _curDistance then
(
_curCandidate = i
_curDistance = _pp
)
)
)
return _curCandidate
)
Any general or specific advices are welcome
But it works very slow, I wonder can I improve it somehow here and there?
The first thing I do is element extraction from UVW into arrays ( I store their indicies)
_obj = $LOD3
select _obj
_uvw = _obj.modifiers[1]
_uvw.edit()
_elementsLOD3 = process _uvw
_obj = $LOD2
select _obj
_uvw2 = _obj.modifiers[1]
_uvw2.edit()
_elementsLOD2 = process _uvw2
And that is actual function:
fn process _uvw =
(
_elements = #()
_collectVerticies = #()
for i = 1 to _uvw.NumberVertices() do
(
append _collectVerticies i
)
while _collectVerticies.count > 0 do
(
_uvw.selectVertices #{_collectVerticies[1]}
_uvw.selectElement()
_selection = _uvw.getSelectedVertices()
_selectionArr = _selection as array
for i = 1 to _selectionArr.count do
(
for z = 1 to _collectVerticies.count do
(
if (_selectionArr[i] == _collectVerticies[z]) then
(
deleteItem _collectVerticies z
)
)
)
if _selectionArr.count >1 then append _elements _selectionArr
)
return _elements
)
Then I do this:
1. I take every point (p1) for UVW LOD2 element, and find closest point to it (p2) of UVW on LOD3.
2. I see, which elements this closest point (p2) of LOD3 belongs to.
3. Based on this "weight element" info, I can predict, which element of LOD2 has closest shape to element of LOD3.
This part of code is responsible for this process:
_pares = #()
for i = 1 to _elementsLOD2.count do
(
_cArr = #()
for es = 1 to _elementsLOD2.count do append _CArr 0
for z = 1 to _elementsLOD2[i].count do
(
_n = getNearestPoint _uvw _uvw2 _elementsLOD3 _elementsLOD2[i][z]
_cArr[_n] = _cArr[_n] + 1
)
_most = -1
_indx = -1
for es = 1 to _cArr.count do
(
if _cArr[es] > _most then
(
_most = _cArr[es]
_indx = es
)
)
_tmparr = #(i, _indx)
deleteItem _elementsLOD3 _indx
append _pares _tmparr
)
And I get nearest point this way:
fn getNearestPoint _uvw _uvw2 _elementArray _point =
(
_ourPosition = _uvw2.getVertexPosition 1 _point
_curDistance = 999999
_curCandidate = -1
for i = 1 to _elementArray.count do
(
for k = 1 to _elementArray[i].count do
(
tempPos = _uvw.getVertexPosition 1 _elementArray[i][k]
_pp = distance _ourPosition tempPos
if _pp < _curDistance then
(
_curCandidate = i
_curDistance = _pp
)
)
)
return _curCandidate
)
Any general or specific advices are welcome
