collecting Faces with same Normals...

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?

AttachmentSize
untitled_camera01_0f.jpg265.46 KB

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Swordslayer's picture

I would use this method -

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
)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.