I need a function that rotates and translates a huge amount of line segments.
For example, I have a set of line segments in the form {{x0,y0,z0},{x1,y1,z1}}
data = RandomReal[{-1, 1}, {20000, 2, 3}]
and three vectors
n = {n1,n2,n3}
s = {s1,s2,s3} (* Orthogonal to n *)
p = {p1,p2,p3}
I want to perform a rigid (right handed) transformation that sends p
to the origin, n
to the $z$ axis and $s$ to the $y$ axis, in order to apply it to data
.
I know about RotationMatrix
, RotationTransform
, and I have coded a (very ugly) solution but, due to inexperience and the amount of data, I'm struggling with performance.
I would be very grateful if someone can provide a fast solution and, if possible, to explain why is fast.
EDIT
Here is the ugly code, for my own embarrassment
rotZ = RotationTransform[{n, {0, 0, 1}}];
rotY = RotationTransform[{s, {0, 1, 0}}];
RT[v_]:= rotY[rotZ[-p + #]] & /@ v;
and then
dataTrans = RT /@ data
Taking
n = RandomReal[{-1, 1}, 3];
s = {-n[[2]], n[[1]], 0}; (* n and s are orthogonal *)
p = RandomReal[{-1, 1}, 3];
then
Timing[RT /@ data][[1]]
(* 3.60422 *)
In an Intel Core2 Duo CPU T8100 @2.10GHz, 4gb ram Ubuntu 12.04, Mathematica 8 distribution.
n
ands
are. $\endgroup$