title: Orienting 2D shape face with normal to 3D trajectory
body: <p>I have line, or trajectory in 3D space. I also have a 2D shape. I want to take this shape and move it along the curve keeping the surface normal parallel to the tangent of the curve. Based on a post <a href="http://uk.mathworks.com/matlabcentral/newsreader/view_thread/317492" rel="nofollow noreferrer">here</a> I have successfully done this that produces something which 'looks right' for shapes with rotational symmetry like a circle. See the code and figure below for the example.</p>
  
  <pre class="lang-matlab prettyprint-override"><code>% create data
  npts = 30;
  tend = 8*pi;
  t = linspace(0,tend,npts)';
  z = linspace(-1,1,npts)';
  omz = sqrt(1-z.^2);
  x = cos(t).*omz;
  y = sin(t).*omz;
  
  scatter3 (x,y,z, 'x');
  
  hold on
  % fit 3 slms to data in each direction
  
  xslm = slmengine (t, x, 'knots', ceil(npts/1.5));
  yslm = slmengine (t, y, 'knots', ceil(npts/1.5));
  zslm = slmengine (t, z, 'knots', ceil(npts/1.5));
  
  % test points
  tq = linspace(0,tend,200 * npts)';
  
  dx = slmeval (t, xslm, 1, false);
  dy = slmeval (t, yslm, 1, false);
  dz = slmeval (t, zslm, 1, false);
  
  quiver3(x,y,z,dx,dy,dz);
  
  plot3 (slmeval(tq, xslm, 0, false), slmeval(tq, yslm, 0, false), slmeval(tq, zslm, 0, false));
  
  hold off
  
  axis equal
  
  % The following taken from post on matlab central
  %
  % http://uk.mathworks.com/matlabcentral/newsreader/view_thread/159522
  %
  
  % P10 = P1-P0;
  % 
  % P20 = P2-P0;
  
  % N = dot(P10,P10)*P20-dot(P20,P20)*P10; % &lt;-- Approx. tangent direction
  
  R = 0.05;
  
  P0 = [x,y,z];
  
  N = [dx, dy, dz];
  
  % circle points
  % theta = linspace(0,2*pi).';
  box_x = [ -R; R; R; -R; -R ];
  box_y = [ -R/2; -R; R; R/2; -R/2 ];
  
  for ind = 1:size(P0,1)
  
      T = null(N(ind,:)).'; % Get two orthogonal unit vectors which are orthog. to N
  
  %     V = bsxfun ( @plus, ...
  %                  R * ( cos(theta) * T(1,:) + sin(theta) * T(2,:) ), ...
  %                  P0(ind,:) );
  
      V = bsxfun ( @plus, ...
                   box_x * T(1,:) + box_y * T(2,:), ...
                   P0(ind,:) );
  
      hold on
      quiver3(P0(ind,1),P0(ind,2),P0(ind,3),T(1,1),T(1,2),T(1,3), 0.5, 'b');
      quiver3(P0(ind,1),P0(ind,2),P0(ind,3),T(2,1),T(2,2),T(2,3), 0.5, 'b');
      plot3(V(:,1),V(:,2),V(:,3));
      hold off
  
  end
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/YSwO4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YSwO4.png" alt="best attempt"></a></p>
  
  <p>Note that this code makes use of the <a href="http://uk.mathworks.com/matlabcentral/fileexchange/24443-slm-shape-language-modeling" rel="nofollow noreferrer">Shape Language Modelling</a> functions from the Matlab file exchange to make the curve and get it's tangent at various points, but this isn't crucial to the problem.</p>
  
  <p>However, as you can see, the rotation of the shape flips as you move around the curve with this method. I need to keep the rotation of the shape consistent as I actually wish to sample values from scattered 3D data on the shape surface which represents the inside of a 'tube' or whatever the shape is.</p>
  
  <p>So how can I control the orientation of my shape as I move along the curve?</p>