| 1 |
% |
| 2 |
% function [mesh] = triangle_mesh_reader( filename ) |
| 3 |
% |
| 4 |
% PURPOSE: Read mesh information from filename.node, filename.ele |
| 5 |
% and filename.edge files generated by the 2D mesh generator |
| 6 |
% TRIANGLE |
| 7 |
% |
| 8 |
% Input: |
| 9 |
% filename string containing the base of the filenames |
| 10 |
% generated by TRIANGLE. (This Matlab functtion |
| 11 |
% will read from filename.node, filename.ele |
| 12 |
% and filename.edge). |
| 13 |
% |
| 14 |
% Output: |
| 15 |
% mesh structure array with the following fields |
| 16 |
% |
| 17 |
% mesh.p Real np x 2 |
| 18 |
% array containing the x- and y- coordinates |
| 19 |
% of the nodes |
| 20 |
% |
| 21 |
% mesh.t Integer nt x 3 or nt x 6 |
| 22 |
% t(i,1:4) contains the indices of the vertices of |
| 23 |
% triangle i. |
| 24 |
% If t is a nt x 6 array, t(i,4:6) contains |
| 25 |
% the indices of the edge mid-points of triangle i. |
| 26 |
% |
| 27 |
% mesh.e Integer ne x 3 or ne x 4 |
| 28 |
% e(i,1:2) contains the indices of the vertices of |
| 29 |
% edge i. |
| 30 |
% If mesh.e is of size ne x 3, then |
| 31 |
% edge(i,3) contains the boundary marker of edge i. |
| 32 |
% e(i,3) = 1 Dirichlet bdry conds are imposed on edge i |
| 33 |
% e(i,3) = 2 Neumann bdry conds are imposed on edge i |
| 34 |
% e(i,3) = 3 Robin bdry conds are imposed on edge i |
| 35 |
% If mesh.e is of size ne x 4, then |
| 36 |
% e(i,3) contains the index of the midpoint of edge i. |
| 37 |
% e(i,4) contains the boundary marker |
| 38 |
% e(i,4) = 1 Dirichlet bdry conds are imposed on edge i |
| 39 |
% e(i,4) = 2 Neumann bdry conds are imposed on edge i |
| 40 |
% |
| 41 |
% AUTHOR: Denis Ridzal |
| 42 |
% Matthias Heinkenschloss |
| 43 |
% Department of Computational and Applied Mathematics |
| 44 |
% Rice University |
| 45 |
% November 23, 2005 |
| 46 |
% |
| 47 |
function [mesh] = triangle_mesh_reader( filename ) |
| 48 |
|
| 49 |
% open node file for reading |
| 50 |
fid = fopen( [ filename '.node'] ); |
| 51 |
% read first line (number of vertices) |
| 52 |
tmp = fscanf( fid, ' %d %d %d %d ', [1, 4]); |
| 53 |
nv = tmp(1); dim = tmp(2); nattrib = tmp(3); bdrymarker = tmp(4); |
| 54 |
% read node coorddinates (ignore attributes, i.e. assumed to be 0) |
| 55 |
if (nattrib==0) |
| 56 |
tmp = fscanf( fid, ' %d %f %f %d', [3+nattrib+bdrymarker,nv]); |
| 57 |
% discard numbering indices and boundary markers |
| 58 |
mesh.p = tmp(2:3,:)'; |
| 59 |
else |
| 60 |
fprintf('Vertices have additional attributes!\n'); |
| 61 |
fprintf('Modify triangle_mesh_reader.m accordingly.\n'); |
| 62 |
return; |
| 63 |
end |
| 64 |
fclose(fid); |
| 65 |
|
| 66 |
% open element file for reading |
| 67 |
fid = fopen( [ filename '.ele'] ); |
| 68 |
% read first line (number of vertices) |
| 69 |
tmp = fscanf( fid, ' %d %d %d ', [1, 3]); |
| 70 |
nt = tmp(1); nnodes = tmp(2); nattrib = tmp(3); |
| 71 |
% read node indices for triangle i (ignore attributes) |
| 72 |
if( nnodes == 3 ) |
| 73 |
tmp = fscanf( fid, ' %d %d %d %d ', [1+nnodes,nt]); |
| 74 |
end |
| 75 |
if( nnodes == 6 ) |
| 76 |
tmp = fscanf( fid, ' %d %d %d %d %d %d %d', [1+nnodes,nt]); |
| 77 |
end |
| 78 |
mesh.t = tmp(2:1+nnodes,:)'; |
| 79 |
fclose(fid); |
| 80 |
|
| 81 |
% open edge file for reading |
| 82 |
fid = fopen( [ filename '.edge'] ); |
| 83 |
% read first line (number of vertices) |
| 84 |
tmp = fscanf( fid, ' %d %d ', [1, 2]); |
| 85 |
ne = tmp(1); bdrymarker = tmp(2); |
| 86 |
% read node indices for edge i and boundary marker |
| 87 |
tmp = fscanf( fid, ' %d %d %d %d ', [4, ne]); |
| 88 |
fclose(fid); |
| 89 |
|
| 90 |
if( size(mesh.t,1) == 3 ) |
| 91 |
% get boundary edges only |
| 92 |
% (assume that all boundary edges have maker > 0!) |
| 93 |
mesh.e = tmp(2:4,:)'; |
| 94 |
mesh.e = mesh.e(mesh.e(:,3) > 0,:); |
| 95 |
else |
| 96 |
|
| 97 |
% get boundary edges only |
| 98 |
% (assume that all boundary edges have maker > 0!) |
| 99 |
ind = (tmp(4,:) > 0); |
| 100 |
ne = sum(ind); |
| 101 |
mesh.e = zeros(ne, 4); |
| 102 |
mesh.e(:,[1,2,4]) = tmp(2:4,ind)'; |
| 103 |
|
| 104 |
% determine mid points |
| 105 |
%%% Get the number of nodes and vertices. Mid points have indices |
| 106 |
%%% nv+1,...,nn |
| 107 |
nn = size(mesh.p,1); |
| 108 |
nv = max( max(mesh.t(:,1:3)) ); |
| 109 |
for i = 1:ne |
| 110 |
mid = ones(nn-nv,1)*( 0.5*(mesh.p(mesh.e(i,1),:)+mesh.p(mesh.e(i,2),:)) ); |
| 111 |
[x,k] = min( sum(abs(mesh.p(nv+1:nn,:)-mid),2) ); |
| 112 |
mesh.e(i,3) = nv+k; |
| 113 |
end |
| 114 |
end |
| 115 |
|