Advertisement
Guest User

Untitled

a guest
Nov 15th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. filename = "/Users/andy2/Documents/work/Rear Right.stl"
  2. ps = 72/25.4 % conversion from mm to points
  3. thickness = 0.302 % thickness of layers
  4. [F V C] = stl_read(filename);
  5.  
  6. X = [V(F(:,1),1) V(F(:,2),1) V(F(:,3),1)]; % X coords for each face
  7. Y = [V(F(:,1),2) V(F(:,2),2) V(F(:,3),2)]; %Y
  8. Z = [V(F(:,1),3) V(F(:,2),3) V(F(:,3),3)]; %Z
  9.  
  10. T = Z %quick and dirty rotation
  11. Z = X
  12. X = T
  13.  
  14. clear T
  15.  
  16. % find extents
  17.  
  18. xmin= min(X(:))
  19. xmax = max(X(:))
  20. ymin= min(Y(:))
  21. ymax = max(Y(:))
  22. zmin= min(Z(:))
  23. zmax = max(Z(:))
  24.  
  25. %for z = zmin:.005:zmax
  26.  
  27. z = mean([zmax zmin])% test porpoises
  28.  
  29. I1 = find(sum((Z>z),2) == 1); % Index of faces with 1 vertex greater than z
  30. I2 = find(sum((Z>z),2) == 2); % index of faces with 2 > z
  31.  
  32. clg
  33. plot([0;0;xmax-xmin;xmax-xmin;0] , [ymax-ymin;0;0;ymax-ymin;ymax-ymin])
  34. hold on
  35. legend("off")
  36.  
  37. C = [];
  38.  
  39. for a = 1:length(I1)
  40. [zs i] = sort(Z(I1(a),:)) ;% put biggest Z last, and store order
  41. xs = X(I1(a),i);
  42. ys = Y(I1(a),i);
  43.  
  44. x1 = xs(1)+(xs(3)-xs(1))*(z-zs(1))/(zs(3)-zs(1))-xmin;
  45. x2 = xs(2)+(xs(3)-xs(2))*(z-zs(2))/(zs(3)-zs(2))-xmin;
  46.  
  47. y1 = ys(1)+(ys(3)-ys(1))*(z-zs(1))/(zs(3)-zs(1))-ymin;
  48. y2 = ys(2)+(ys(3)-ys(2))*(z-zs(2))/(zs(3)-zs(2))-ymin;
  49.  
  50. C = [C; x1 y1 x2 y2];
  51.  
  52. end
  53.  
  54. for a = 1:length(I2)
  55.  
  56. [zs i] = sort(Z(I2(a),:)) ;% put biggest Z last, and store order
  57. xs = X(I2(a),i);
  58. ys = Y(I2(a),i);
  59.  
  60. x1 = xs(2)+(xs(1)-xs(2))*(z-zs(2))/(zs(1)-zs(2))-xmin;
  61. x2 = xs(3)+(xs(1)-xs(3))*(z-zs(3))/(zs(1)-zs(3))-xmin;
  62.  
  63. y1 = ys(2)+(ys(1)-ys(2))*(z-zs(2))/(zs(1)-zs(2))-ymin;
  64. y2 = ys(3)+(ys(1)-ys(3))*(z-zs(3))/(zs(1)-zs(3))-ymin;
  65.  
  66. C = [C; x1 y1 x2 y2];
  67.  
  68.  
  69. end
  70.  
  71. plot([C(:,1) C(:,3)]' , [C(:,2) C(:,4)]')
  72. hold off
  73.  
  74. C = C * ps; % Convert to points
  75.  
  76. f = fopen(['/Users/andy2/Documents/gash' num2str(z) '.eps'], 'wt');
  77. fprintf(f, '%%!PS-Adobe-3.0 EPSF-3.0\n')
  78. fprintf(f, '%%%%BoundingBox: %0.2f %0.2f %0.2f %0.2f\n', 0, 0, (xmax-xmin)*ps*1.02, (ymax-ymin)*ps*1.02)
  79. fprintf(f, '0.75 setlinewidth\n')
  80.  
  81. %Try to string line segments together into loops.
  82.  
  83. g = 1:length(C); % use this vector to store indices to the currently unassigned segments
  84.  
  85. while (any(g))
  86.  
  87.  
  88. gi = 1; % starting position
  89.  
  90. fprintf(f, '%0.2f %0.2f moveto\n', C(g(gi), 1)-xmin*ps, C(g(gi), 2)-ymin*ps);
  91. fprintf(f, '%0.2f %0.2f lineto\n', C(g(gi), 3)-xmin*ps, C(g(gi), 4)-ymin*ps);
  92. lastx = C(g(gi),3) ; lasty = C(g(gi), 4);
  93.  
  94. do
  95.  
  96. g = [g(1:gi-1) g(gi+1:end)]; % remove used index from list
  97.  
  98. % find x and y match looking in both start and end
  99. gi = [find((C(g,1) == lastx) & (C(g,2) == lasty)) ; ...
  100. find((C(g,3) == lastx) & (C(g,4) == lasty))];
  101.  
  102. if any(gi)
  103. gi = gi(1); % choose the first match
  104. xi = [3 1](find(C(g(gi),[1 3]) == lastx)); % find the new positions
  105. yi = [4 2](find(C(g(gi),[2 4]) == lasty));
  106. if (length(xi) == 1 | length(yi) == 1) % needed for zero-length segments
  107. fprintf(f, '%0.2f %0.2f lineto\n', ...
  108. C(g(gi), xi(1))-xmin*ps, C(g(gi), yi(1))-ymin*ps);
  109. lastx = C(g(gi),xi(1)) ; lasty = C(g(gi), yi(1));
  110. endif
  111. endif
  112.  
  113. if !any(gi)
  114. fprintf(f, 'stroke\n')
  115. endif
  116.  
  117. until !any(gi) % ie no matches for last end point
  118.  
  119.  
  120. endwhile
  121.  
  122.  
  123.  
  124. fprintf(f, '%%%%EOF\n')
  125.  
  126. fclose(f)
  127.  
  128. %end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement