@bomastudio said:
Hi Guys, I have an agony-problem with a 3D rotation.
The transform that TIG suggested (rotation) works for your need. The docs on transformations aren’t very clear, especially for people who aren’t familiar with 3D transforms or linear algebra. They’re also a little quirky, as the trans.to_a method returns a matrix that is the transpose of what I would expect. FWIW, I had a lot of linear algebra (long time ago)...
Anyway, the rotation method returns a transformation based on a rotation in a plane, which is defined by the ‘axis’ parameter, which should be the normal to the plane of rotation.
If the start and end vectors are parallel, they do not define a unique plane, hence, both my original ‘linear algebra’ algorithm and the rotation method fail. The code below accounts for that –
Greg
module SUMath
def self.transVector2Vector(vStart, vEnd, pt = nil)
ptRot = pt || [0,0,0]
if vStart.samedirection?(vEnd)
# return identity transformation, vectors are in the same direction
Geom;;Transformation.new
elsif vStart.parallel?(vEnd)
#return 180 transformation, vStart and vEnd in opposite direction
Geom;;Transformation.axes(ptRot, [-1,0,0], [0,-1,0], [0,0,-1] )
else
# return rotation transform based on vStart and vEnd
# vCross is normal to plane of rotation
vCross = vStart.cross(vEnd)
ang = vStart.angle_between(vEnd)
Geom;;Transformation.rotation(ptRot, vCross, ang)
end
end
def self.rotateFace(face, vector_end, pt = nil)
# for demo purposes, the next 2 statements chose an arbitrary face
# if no face passed
unless face
entities = Sketchup.active_model.entities
faces = entities.grep(Sketchup;;Face)
face = faces[0]
end
face_normal = face.normal
t = transVector2Vector(face_normal, vector_end, pt)
entities.transform_entities(t, face)
# below is just for a demo confirmation
sd = face.normal.samedirection?(vector_end)
sVEnd = "[#{vector_end.join(",")}]"
puts "face.normal.samedirection?(#{sVEnd}) = #{sd}"
end
end
# load module thru console
# type SUMath.rotateFace(nil, [0,0,1]) into console with whatever vector
# you want, it can be typed as often as you'd like.
# As a demo, best done with one face in the model