Notifications
Clear all

[Closed] Detecting cylinder orientation

Hi,

I’m working on an exporter at the moment and have come up against a problem.

The files I’m exporting only consist of boxes and cylinders, and to make things easy they have to line up to the xyz co-ordinates, so nothing going off an any angles. Exporting the co-ordinates of boxes is easy as I just need to get hold of the bounding box.

However cylinders are proving to be a bit of a problem as I need to know which axis they are orientated along (the cylinders are not parametric and have been converted to a mesh/poly object).

How can I script a test to determine the orientation?

Thanks in advance.
Garry

5 Replies

Well there is the Dir property, $.dir that tells which way its local Z is pointing.

Otherwise u could create your own vector, getting the longest edge on the Cylinder (side edge), and using those verts, and getting a vector from those 2 points.

Thanks for the suggestions.

I think .dir will work 95% of the time so I’ll get that working first, thanks.

However sometimes I have to work with models imported from CAD and the local co-ordinates can be a bit messed up, so for a truly bulletproof exporter I’ll need some other way of determining orentation.

if we are talking about MAX cylinders as source mesh, the Z axis is vector from vertex#first to vertex#last, the X axis is vector from vertex#first to vertex#second, the Y axis is a cross product of X and Z

here is a general way of doing that:

(
	local obj = $
	local objMesh = obj.mesh
	local thresh = 0.001
	local vectorsArray = #()
	local vectorCount = #()
	
	for i = 1 to (meshop.getEdgesUsingVert objMesh #all).count do (
		local x = (meshop.getVertsUsingEdge objMesh i) as array
		local v1 = getVert objMesh x[1]
		local v2 = getVert objMesh x[2]
		local v = normalize (v2 - v1)
		
		local k = 0
		for j = 1 to vectorsArray.count do
			if amin (distance vectorsArray[j] v) (distance vectorsArray[j] (-v)) < thresh then
				k = j
		
		if k == 0 then (
			append vectorsArray v
			append vectorCount 1
		) else
			vectorCount[k] += 1
	)
	
	local j = 1
	for i = 2 to vectorCount.count do
		if vectorCount[i] > vectorCount[j] then
			j = i
	
	format "z axis = %
" (vectorsArray[j] * obj.transform * inverse (matrix3 [1,0,0] [0,1,0] [0,0,1] obj.pos))
)

the idea is to find the vector that most edges align to.

Brilliant, I’d have never worked that out myself, many thanks.

  • Garry