[Closed] Can't create fast split/crack algorithm
How about using gc: lite or gc() in the loop? Would it fix it?
Are you using a 32bit version of 3dsmax?
(
delete objects
node = teapot radius:400 segments:40
converttomesh node
cell_size = 80
t1 = timestamp()
m1 = heapfree
faceCenter = meshop.getFaceCenter -- save memory by avoiding of calling functions through the interface
detachFaces = meshop.detachFaces
data = #()
for f in (node.faces as bitarray) do
(
c = ((faceCenter node f) - node.min)/cell_size + 1
if data[c.y] == undefined do data[c.y] = #()
if data[c.y][c.x] == undefined then data[c.y][c.x] = #{f} else append data[c.y][c.x] f
) -- save memory by avoiding of creation array items for empty space; avoid [b]ceil[/b] and[b] floor[/b] operations (they are slow)
chunks = #()
for row in data where row != underined do for ff in row where ff != undefined do
(
chunk = Editable_Mesh name:(uniqueName "Chunk_")
chunk.mesh = detachFaces node ff delete:false asMesh:on -- delete == OFF to avoid update node's mesh
maxops.colorByID chunks.count &chunk.wirecolor -- it binds chunk's wirecolor and ID and updates chunk's mesh at the same time
append chunks chunk
)
delete node
format "time:% leak:% chunks:%
" (timestamp()-t1) (m1-heapfree) chunks.count
gc light:on
chunks
)
the undoable version is 2s on my machine and very low memory leaking.
I wanted use meshop.detach before but I was afraid of losing node properties.
But I already applying reset X form, so only property I have to transfer myself is material.
Denis your code is amazing fast and no crashing
Just small bug need to be fixed:
for row in data where row != undefined and row.count > 0 do for ff in row where ff != undefined do
Dirty float-> integer conversion instead of ceil\floor… clever one
I didn’t know things like that are possible:
faceCenter = meshop.getFaceCenter
Thanks
You are great example for learning;)
unfortunately the material transfer is not the biggest problem for you. The real problem is to keep/transfer original normals on seems.
where is the bug?
oops… it should be
for row in data where row != undefined do for ff in row where ff != undefined do
the checking for count number is not necessary. if row is defined it can’t be empty.
forgot to fix it in the sample
I made the algorithm about 15-20% faster and added material transfer at one pass. Also new algorithm doesn’t need reset XForm for the source object.
…
<deleted>
…
I’ve thought a little more and here is a version that does the same for less then 1s (0.9s on my machine):
(
delete objects
node = teapot radius:400 segments:40 material:(Standard name:"Source_Mat")
-- node = teapot radius:400 segments:40 transform:(translate (rotate (scalematrix [1,1.5,0.75]) (eulerangles 40 30 70)) [120,0,0])
converttomesh node
cell_size = 80
t1 = timestamp()
m1 = heapfree
faceCenter = meshop.getFaceCenter
detachFaces = meshop.detachFaces
data = #()
tm = node.transform
mesh = copy node.mesh
node.transform *= translate (scalematrix ([1,1,1]/cell_size)) (-node.min+cell_size)
snap = snapshotasmesh node
faces = node.faces as bitarray
for f in faces do
(
c = faceCenter snap f
if data[c.y] == undefined then
(
data[c.y] = #()
data[c.y][c.x] = #{f}
)
else if data[c.y][c.x] == undefined then data[c.y][c.x] = #{f} else append data[c.y][c.x] f
)
chunks = #()
for row in data where row != undefined do for ff in row where ff != undefined do
(
chunk = Editable_Mesh name:(uniqueName "Chunk_") transform:tm material:node.mat
chunk.mesh = detachFaces mesh ff delete:off asMesh:on
maxops.colorByID chunks.count &chunk.wirecolor
append chunks chunk
)
delete node
delete mesh
delete snap
format "time:% leak:% chunks:%
" (timestamp()-t1) (m1-heapfree) chunks.count
gc light:on
chunks
)