1 | function md=modelmerge(md1,md2)
|
---|
2 | %MODELMERGE - merge two models by merging their meshes
|
---|
3 | %
|
---|
4 | % Usage:
|
---|
5 | % md=modelmerge(md1,md2);
|
---|
6 |
|
---|
7 | if nargin~=2
|
---|
8 | help meshconvert
|
---|
9 | error('meshconvert error message: bad usage');
|
---|
10 | end
|
---|
11 |
|
---|
12 | md=model();
|
---|
13 |
|
---|
14 | %first ,copy md1 mesh into md.mesh to initialize:
|
---|
15 | md.mesh=md1.mesh;
|
---|
16 |
|
---|
17 | %some initializatoin:
|
---|
18 | elements1=md1.mesh.elements;
|
---|
19 | x1=md1.mesh.x;
|
---|
20 | y1=md1.mesh.y;
|
---|
21 | nods1=md1.mesh.numberofvertices;
|
---|
22 | nel1=md1.mesh.numberofelements;
|
---|
23 |
|
---|
24 | elements2=md2.mesh.elements;
|
---|
25 | x2=md2.mesh.x;
|
---|
26 | y2=md2.mesh.y;
|
---|
27 | nods2=md2.mesh.numberofvertices;
|
---|
28 | nel2=md2.mesh.numberofelements;
|
---|
29 | segs2=md2.mesh.segments;
|
---|
30 |
|
---|
31 | %offset elements2 by nods1:
|
---|
32 | elements2=elements2+nods1;
|
---|
33 |
|
---|
34 | tolerance=1e-5;
|
---|
35 | %go into the vertices on boundary of mesh 1, and figure out which ones are common to mesh2:
|
---|
36 | verticesonboundary=find(md1.mesh.vertexonboundary);
|
---|
37 | for i=1:length(verticesonboundary),
|
---|
38 | node1=verticesonboundary(i); xnode1=x1(node1); ynode1=y1(node1);
|
---|
39 | %is there another node with these coordinates in mesh2?
|
---|
40 | ind=find(sqrt(((x2-xnode1).^2+(y2-ynode1).^2))<tolerance);
|
---|
41 | if ~isempty(ind),
|
---|
42 | x2(ind)=NaN;
|
---|
43 | y2(ind)=NaN;
|
---|
44 | pos=find(elements2==(ind+nods1)); elements2(pos)=node1;
|
---|
45 | end
|
---|
46 | end
|
---|
47 |
|
---|
48 | %go through elements2 and drop counter on each vertex that is above the x2 and y2 vertices being dropped:
|
---|
49 | while( ~isempty(find(isnan(x2)))),
|
---|
50 | for i=1:length(x2),
|
---|
51 | if isnan(x2(i)),
|
---|
52 | pos=find(elements2>(i+nods1));
|
---|
53 | elements2(pos)=elements2(pos)-1;
|
---|
54 | x2(i)=[];
|
---|
55 | y2(i)=[];
|
---|
56 | break;
|
---|
57 | end
|
---|
58 | end
|
---|
59 | end
|
---|
60 |
|
---|
61 | %merge elements:
|
---|
62 | elements=[elements1;elements2];
|
---|
63 |
|
---|
64 | %merge vertices:
|
---|
65 | x=[x1;x2];
|
---|
66 | y=[y1;y2];
|
---|
67 |
|
---|
68 | %output:
|
---|
69 | md.mesh.x=x;
|
---|
70 | md.mesh.y=y;
|
---|
71 | md.mesh.elements=elements;
|
---|
72 | md.mesh.numberofvertices=length(x);
|
---|
73 | md.mesh.numberofelements=size(elements,1);
|
---|
74 |
|
---|
75 | %connectivities:
|
---|
76 | md.mesh.vertexconnectivity=NodeConnectivity(md.mesh.elements,md.mesh.numberofvertices);
|
---|
77 | md.mesh.elementconnectivity=ElementConnectivity(md.mesh.elements,md.mesh.vertexconnectivity);
|
---|
78 |
|
---|
79 | %find segments:
|
---|
80 | md.mesh.segments=findsegments(md);
|
---|
81 |
|
---|
82 | %vertex on boundary:
|
---|
83 | md.mesh.vertexonboundary=zeros(md.mesh.numberofvertices,1);
|
---|
84 | md.mesh.vertexonboundary(md.mesh.segments(:,1:2))=1;
|
---|
85 |
|
---|
86 | %some checks:
|
---|
87 | if max(md.mesh.elements)>md.mesh.numberofvertices,
|
---|
88 | error('issue in modelmerge, one of the element ids is > number of vertices!');
|
---|
89 | end
|
---|