ScriptSpot

Forum topic · General Scripting

collecting Faces with same Normals...

By HP6830s · 2012-03-13

Description

Hi!

I want to loop over all faces and collect those who have same normal into an array then append this one to an other array..

for exemple in the image I shawn it is a simple cube as you see face 1,2,3,4 have same normal 5,6,7,8 have same normal...Iwant to obtain 2D array like this...

((1,2,3,4),(5,6,7,8)....)

I couldn' fin a good algo to do this...since the face normal is unknow...before loop...especilally on a complex object...that have a lot of differents normals, any Ideas how to do this?

Attachments
Comments (1)

Swordslayer · 2012-03-15

I would use this method - i.e. collect string addresses of face normals as you go (or maybe names instead of strings, pick one). When that address already exists, use the corresponding subarray (although I'd suggest using bitarrays here), if not, store the address and a new subarray.

(
	local decimalPrecision = 3 -- change this value to your liking
	local formatString = "1." + decimalPrecision as string + "f"

	local obj = selection[1]
	local faces = obj.faces as bitArray

	local normalAddresses = #()
	local normalFaces = #()

	local getPolyNormal = polyop.getFaceNormal

	fn getAddress normal =
	(
		formattedPrint normal.x format:formatString + "|" +
		formattedPrint normal.y format:formatString + "|" +
		formattedPrint normal.z format:formatString
	)

	for face in faces do
	(
		local normalAddress = getAddress (normalize (getPolyNormal obj face))
		if (local index = findItem normalAddresses normalAddress) > 0 then
			append normalFaces[index] face
		else
		(
			append normalAddresses normalAddress
			append normalFaces #(face)
		)
	)

	normalFaces
)