File: surfToMesh.m

package info (click to toggle)
octave-matgeom 1.2.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,584 kB
  • sloc: objc: 469; makefile: 10
file content (145 lines) | stat: -rw-r--r-- 4,290 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
## Copyright (C) 2024 David Legland
## All rights reserved.
## 
## Redistribution and use in source and binary forms, with or without
## modification, are permitted provided that the following conditions are met:
## 
##     1 Redistributions of source code must retain the above copyright notice,
##       this list of conditions and the following disclaimer.
##     2 Redistributions in binary form must reproduce the above copyright
##       notice, this list of conditions and the following disclaimer in the
##       documentation and/or other materials provided with the distribution.
## 
## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS''
## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
## ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
## ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
## DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
## SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
## CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
## 
## The views and conclusions contained in the software and documentation are
## those of the authors and should not be interpreted as representing official
## policies, either expressed or implied, of the copyright holders.

function varargout = surfToMesh(x, y, varargin)
%SURFTOMESH Convert surface grids into face-vertex mesh.
%
%   [V, F] = surfToMesh(X, Y)
%   [V, F] = surfToMesh(X, Y, Z)
%   Converts the surface grid given by two or three coordinate arrays into
%   a face-vertex quad mesh.
%
%   Example
%     % transform a surface into a mesh
%     [X, Y] = meshgrid(-2:.2:2, -2:.2:2);                                
%     Z = X .* exp(-X.^2 - Y.^2);
%     [V, F] = surfToMesh(X, Y, Z);
%     figure;
%     drawMesh(V, F); view(3);
%
%     % Transform surface of a cylinder as a mesh
%     [x, y, z] = cylinder(5*ones(1, 10));
%     [v, f] = surfToMesh(x, y, z, 'xPeriodic', true);
%     figure;
%     drawMesh(v, f);
%     view(3); axis equal;
%
%   See also 
%     meshes3d, meshgrid, drawMesh, torusMesh, sphereMesh

% ------
% Author: David Legland
% E-mail: david.legland@inrae.fr
% Created: 2012-10-25, using Matlab 7.9.0.529 (R2009b)
% Copyright 2012-2023 INRA - Cepia Software Platform

%% Parse inputs

% check if z-value is present
if ~isempty(varargin) && isnumeric(varargin{1})
    z = varargin{1};
    varargin(1) = [];
end

% default periodicities
xPeriodic = false;
yPeriodic = false;

% parse input options
while length(varargin) > 1
    paramName = lower(varargin{1});
    switch paramName
        case 'xperiodic'
            xPeriodic = varargin{2};
        case 'yperiodic'
            yPeriodic = varargin{2};
        otherwise
            error(['Unknown parameter name: ' paramName]);
    end

    varargin(1:2) = [];
end


%% Compute vertex indices

% size along each direction (arrays are (y,x)-indexed)
n1 = size(x, 1);
n2 = size(x, 2);

% in case of periodicity, the last vertex of the grid is drop (it is
% assumed to be the same as the first one)
if xPeriodic
    n2 = n2 - 1;
end
if yPeriodic
    n1 = n1 - 1;
end

% new size of vertex grid
dim = [n1 n2];
nv = n1 * n2;


%% Create vertex array

% eventually remove boundary vertices
x = x(1:n1, 1:n2);
y = y(1:n1, 1:n2);

% create vertex array
if ~exist('z', 'var')
    vertices = [x(:) y(:)];
else
    z = z(1:n1, 1:n2);
    vertices = [x(:) y(:) z(:)];
end


%% Create face array

% vertex indices in grid
inds = reshape(1:nv, dim);
if xPeriodic
    inds = inds(:, [1:end 1]);
end
if yPeriodic
    inds = inds([1:end 1], :);
end

% vertex indices for each face
v1 = inds(1:end-1, 1:end-1);
v2 = inds(1:end-1, 2:end);
v3 = inds(2:end, 2:end);
v4 = inds(2:end, 1:end-1);

% concatenate indices
faces = [v1(:) v2(:) v3(:) v4(:)];


%% format output
varargout = formatMeshOutput(nargout, vertices, faces);