Notifications
Clear all

[Closed] Improve speed of "Create box()"

the most clear ‘fast attach’ function:

 	fn fastAttach nodes =
	(
		k = 1
		attach = polyop.attach
		while nodes.count > 1 do
		(
			k += 1
			attach nodes[k-1] nodes[k]
			deleteItem nodes k
			if k >= nodes.count do k = 1
		)
		nodes[1]
	)


My currently fastest version: (n15 / 3375boxes) [Time: 0.073 sec]

updated: [EDIT4]
I[/I]

fn fn_genBoxesMesh = (
	gc()
	local timeStart = timestamp()
	with redraw off(
		undo off(
			sliderTime = 0
			local n = 15
			local bScript = 
			("
				on ChannelsUsed pCont do
				(
					pCont.useScale = true
					pCont.usePosition = true
				)
				on Proceed pCont do 
				(
					n = ((pCont.NumParticles())^(1/3.0))
					i = 1
					rng
					for z = 0 to (n - 1) do (
						for y = 0 to (n - 1) do (
							for x = 0 to (n - 1) do (
								rng = (random 0.1 1)
								pCont.particleIndex = i
								pCont.particleScale = rng
								pCont.particlePosition = [x,y,z] 
								i += 1
							)
						)
					)
				)
			")

			local pf = PF_Source Quantity_Viewport:100 Particle_Amount_Limit:1000000
			ParticleFlow.BeginEdit()
			pf.AppendAction (RenderParticles())
			local e1 = Event name:"genBoxesEvent"
			e1.AppendAction (Birth Amount: (n^3) Emit_Start:0 Emit_Stop:0)
			e1.AppendAction (Script_Operator Proceed_Script: bScript)
			e1.AppendAction (ShapeLibrary '3D_Type':0 size:1)
			e1.AppendAction (DisplayParticles type:6)
			ParticleFlow.EndEdit()
			pf.appendInitialActionList e1
			local m = Mesher()
			m.pick = pf
			snapshot m
			particleFlow.BeginEdit()
			particleFlow.delete $'genBoxesEvent'
			particleFlow.EndEdit()
			delete m 	
			delete pf
		)
	)
	local timeStop = timestamp()
	print ("Time: " + (((timeStop - timeStart)/1000.0)as string) + " sec")
)
fn_genBoxesMesh()

[Edit]: Hmm pflow is messing with me, one minute it worked, now the birth script operator wont run or something for some reason :hmm:

[EDIT2:] Think I have it working now

[EDIT3:] Speed improvements

[EDIT4:] added upper limit of 1M and custom event name

Perhaps you should try building mesh from scratch:


	timeStart = timestamp()

	-- Verts:
	theElementVerts = #([-0.5,-0.5,-0.5], [0.5,-0.5,-0.5], [-0.5,0.5,-0.5], [0.5,0.5,-0.5], [-0.5,-0.5,0.5], [0.5,-0.5,0.5], [-0.5,0.5,0.5], [0.5,0.5,0.5])
	-- Faces:
	theElementFaces = #([1,3,4], [4,2,1], [5,6,8], [8,7,5], [1,2,6], [6,5,1], [2,4,8], [8,6,2], [4,3,7], [7,8,4], [3,1,5], [5,7,3])
	-- Smooth
	theElementSmooth = #(2, 2, 4, 4, 8, 8, 16, 16, 32, 32, 64, 64)
	
	colorb = (color (random 0 255) (random 0 255) (random 0 255))
	countFaces = 0
	vertsArray = #()
	facesArray = #()
	n = 15
	for z = 0 to (n-1) do (
		for y = 0 to (n-1) do (
			for x = 0 to (n-1) do (
				rng =  (random 0.1 1)
				VertPos = for v in theElementVerts collect (posLocal = v*rng; [posLocal.x + x, posLocal.y + y, posLocal.z + z])
				join vertsArray VertPos 
				Faces = for f in theElementFaces collect (f + (8 * countFaces))
				join facesArray Faces
				countFaces += 1
			)
		)
	)
	
	oo = mesh vertices:vertsArray faces:facesArray
	oo.wirecolor = colorb
	oo.name =  "SuperCube"
	
	for i = 1 to facesArray.count by 12 do
	(
		for j = 0 to 11 do
		(
			setFaceSmoothGroup oo (i+j) theElementSmooth[j+1]
		)
	)
	update oo
	
	print ("Time: " + (((timestamp() - timeStart)/1000.0)as string) + " sec")
	

Your 15x15x15 cube already attached in 0.25sec or 20x20x20 in 0.43sec.

Thanks, that’s pretty fast and it was the next thing I was gonna try, you saving me a bit of time :keenly: , but now when I got the pflow method to work [Updated version above Edit3] its only 0.073sec for 15^3 and 0.162sec 20^3 don’t think its gonna get much faster than that, also pflow is nice with the flexibility.

Wow! :applause:
So from your initial 10sec you have now 0.073sec… +137% speed!!!
That’s a great job. Congratulations.

I know nothing about particles. I’ll have to study a little.

1 Reply
(@patan77)
Joined: 11 months ago

Posts: 0

Thank you, tho from 10sec to 0.073sec is 137x faster aka 13700% speed (;

delete objects 
(
	
gc()

with redraw off (
with undo off (	
t1=timestamp()
hf = heapfree

	
size = 15
	
w = 10
h = 10
l = 10

m = mesh numverts:(size^3) numfaces:(size^3)

pts = #()
for x=0 to size-1 do for y=0 to size-1 do for z=0 to size-1 do append pts [ x*w, y*l, z*h ]


for i=1 to pts.count do setvert m i pts[i]	

p = PArray()
p.Emitter_Stop = 0f
p.quantityMethod = 1
p.Total_Number = size^3
p.formation = 2
p.Growth_Time = 0f
p.Fade_Time = 0f
p.size = w				
p.standardParticle = 1
p.viewPercent = 100
p.viewType = 2
p.Size_Variation = 0.5
p.emitter = m

mm = Mesher()
mm.pick = p
snapshot mm


delete mm
delete p
delete m


format "Time: %sec. Mem: %
" ((timestamp()-t1)/1000 as float) (hf-heapfree)
)

))

a little improvement to the code above:

--snapshot mm
--delete mm

converttomesh mm


for some reason converttomesh didn’t work for me at first. now it’s ok. thx

Nice didn’t think it was gonna get faster but still got about 2x faster
Made a few changes to your code to make it more like the original code:

p.size = w/2.0
p.Size_Variation = 100

p.seed = (random 0 99999)
– denisT :
converttomesh mm

[EDIT2 ]
– Serejah
i = 1
i += 1

Updated:

fn fn_genBoxesMesh = (
	delete objects 
	(
		gc()
		with redraw off (
			with undo off (	
				t1=timestamp()
				hf = heapfree
				size = 15
				w = 10
				h = 10
				l = 10
				ps = (w/2.0)
				m = mesh numverts:(size^3) numfaces:(size^3)
				i = 1
				for x=0 to size-1 do(
					for y=0 to size-1 do(
						for z=0 to size-1 do(
							setvert m i [ x*w, y*l, z*h ]
							i += 1
						)
					)
				)
				p = PArray()
				p.seed = (random 0 99999)
				p.Emitter_Stop = 0f
				p.quantityMethod = 1
				p.Total_Number = size^3
				p.formation = 2
				p.Growth_Time = 0f
				p.Fade_Time = 0f
				p.size = ps 
				p.standardParticle = 1
				p.viewPercent = 100
				p.viewType = 2
				p.Size_Variation = 100
				p.emitter = m
				mm = Mesher()
				mm.pick = p
				converttomesh mm
				delete p
				delete m
				format "Time: %sec. Mem: %
" ((timestamp()-t1)/1000 as float) (hf-heapfree)
			)
		)
	)
)
fn_genBoxesMesh()

--pts = #()
i=1
for x=0 to size-1 do(
	for y=0 to size-1 do(
		for z=0 to size-1 do(
			setvert m i [ x*w, y*l, z*h ]
                        i = i + 1
		)
	)
)
--for i=1 to pts.count do setvert m i pts[i]	

some memory improvements

2 Replies
(@patan77)
Joined: 11 months ago

Posts: 0

Updated the code with that.

(@denist)
Joined: 11 months ago

Posts: 0
i = [0,0]
for x=0 to size-1 do for y=0 to size-1 do for z=0 to size-1 do setvert m (i.x += 1) [ x*w, y*l, z*h ]
Page 2 / 5