CRACKPATTERN Create a (bounded) crack pattern tessellation. E = crackPattern(BOX, POINTS, ALPHA) create a crack propagation pattern wit following parameters : - pattern is bounded by area BOX given by [xmin xmax ymin ymax]. - each crack originates from points given in POINTS - direction of each crack is given by array ALPHA - a crack stop when it reaches another already created crack. - all cracks stop when they reach the border of the frame, given by box (a serie of 4 points). The result is a collection of edges, in the form [x1 y1 x2 y2]. E = crackPattern(BOX, POINTS, ALPHA, SPEED) Also specify speed of propagation of each crack. See the result with : figure; drawEdge(E); See also drawEdge
0001 function edges = crackPattern(box, points, alpha, varargin) 0002 %CRACKPATTERN Create a (bounded) crack pattern tessellation. 0003 % 0004 % E = crackPattern(BOX, POINTS, ALPHA) 0005 % create a crack propagation pattern wit following parameters : 0006 % - pattern is bounded by area BOX given by [xmin xmax ymin ymax]. 0007 % - each crack originates from points given in POINTS 0008 % - direction of each crack is given by array ALPHA 0009 % - a crack stop when it reaches another already created crack. 0010 % - all cracks stop when they reach the border of the frame, given by box 0011 % (a serie of 4 points). 0012 % The result is a collection of edges, in the form [x1 y1 x2 y2]. 0013 % 0014 % E = crackPattern(BOX, POINTS, ALPHA, SPEED) 0015 % Also specify speed of propagation of each crack. 0016 % 0017 % 0018 % See the result with : 0019 % figure; 0020 % drawEdge(E); 0021 % 0022 % See also 0023 % drawEdge 0024 % 0025 0026 % --------- 0027 % 0028 % author : David Legland 0029 % INRA - TPV URPOI - BIA IMASTE 0030 % created the 25/05/2004. 0031 % 0032 0033 % HISTORY : 0034 0035 if ~isempty(varargin) 0036 speed = varargin{1}; 0037 else 0038 speed = ones(size(points, 1), 1); 0039 end 0040 0041 % Compute line equations for each initial crack. 0042 % The two 'Inf' at the end correspond to the position of the limit. 0043 % If an intersection point is found with another line, but whose position 0044 % is after this value, this means that another crack stopped it before it 0045 % reach the intersection point. 0046 % There is one 'end position' for each side of the crack. 0047 lines = [points speed.*cos(alpha) speed.*sin(alpha) Inf*ones(size(points, 1), 2)]; 0048 0049 % initialize lines for borders, but assign a very high speed, to be sure 0050 % borders will stop all cracks. 0051 dx = (box([2 3 4 1],1)-box([1 2 3 4],1))*max(speed)*5; 0052 dy = (box([2 3 4 1],2)-box([1 2 3 4],2))*max(speed)*5; 0053 0054 % add borders to the lines set 0055 lines = [lines ; createLine(box, dx, dy) Inf*ones(4,2)]; 0056 0057 edges = zeros(0, 4); 0058 0059 0060 while true 0061 modif = 0; 0062 0063 % try to update each line 0064 for i=1:size(points, 1) 0065 0066 % compute intersections with all other lines 0067 pi = intersectLines(lines(i,:), lines); 0068 0069 % compute position of all intersection points on the current line 0070 pos = linePosition(pi, lines(i,:)); 0071 0072 % consider points to the right (positive position), and sort them 0073 indr = find(pos>=0 & pos~=Inf); 0074 [posr, indr2] = sort(pos(indr)); 0075 0076 0077 % look for the closest intersection to the right 0078 for i2=1:length(indr2) 0079 0080 % index of intersected line 0081 il = indr(indr2(i2)); 0082 0083 % position of point relative to intersected line 0084 pos2 = linePosition(pi(il, :), lines(il, :)); 0085 0086 % depending on the sign of position, tests if the line2 can 0087 % stop the current line, or if it was stopped before 0088 if pos2>0 0089 if pos2<abs(posr(i2)) && pos2<lines(il, 5) 0090 if lines(i, 5) ~= posr(i2) 0091 edges(i, 3:4) = pi(il,:); 0092 lines(i, 5) = posr(i2); 0093 modif = 1; 0094 end 0095 break; 0096 end 0097 else 0098 if abs(pos2)<abs(posr(i2)) && abs(pos2)<lines(il, 6) 0099 if lines(i, 5) ~= posr(i2) 0100 edges(i, 3:4) = pi(il,:); 0101 lines(i, 5) = posr(i2); 0102 modif = 1; 0103 end 0104 break; 0105 end 0106 end 0107 0108 end % end processing of right points of the line 0109 0110 0111 % consider points to the left (negative position), and sort them 0112 indl = find(pos<=0 && pos~=Inf); 0113 [posl, indl2] = sort(abs(pos(indl))); 0114 0115 % look for the closest intersection to the right 0116 for i2 = 1:length(indl2) 0117 % index of intersected line 0118 il = indl(indl2(i2)); 0119 0120 % position of point relative to intersected line 0121 pos2 = linePosition(pi(il, :), lines(il, :)); 0122 0123 % depending on the sign of position, tests if the line2 can 0124 % stop the current line, or if it was stopped before 0125 if pos2>0 0126 if pos2<abs(posl(i2)) && pos2<lines(il, 5) 0127 if lines(i, 6) ~= abs(posl(i2)) 0128 edges(i, 1:2) = pi(il, :); 0129 lines(i, 6) = abs(posl(i2)); 0130 modif = 1; 0131 end 0132 break; 0133 end 0134 else 0135 if abs(pos2)<abs(posl(i2)) && abs(pos2)<lines(il, 6) 0136 if lines(i, 6) ~= abs(posl(i2)) 0137 edges(i, 1:2) = pi(il, :); 0138 lines(i, 6) = abs(posl(i2)); 0139 modif = 1; 0140 end 0141 break; 0142 end 0143 end 0144 0145 end % end processing of left points of the line 0146 0147 0148 end % end processing of all lines 0149 0150 % break the infinite loop if no more modification was made 0151 if ~modif 0152 break; 0153 end 0154 end 0155 0156 % add edges of the surronding box. 0157 edges = [edges ; box(1,:) box(2,:) ; box(2,:) box(3,:); ... 0158 box(3,:) box(4,:) ; box(4,:) box(1,:) ]; 0159