1## Copyright (C) 2021 David Legland 2## All rights reserved. 3## 4## Redistribution and use in source and binary forms, with or without 5## modification, are permitted provided that the following conditions are met: 6## 7## 1 Redistributions of source code must retain the above copyright notice, 8## this list of conditions and the following disclaimer. 9## 2 Redistributions in binary form must reproduce the above copyright 10## notice, this list of conditions and the following disclaimer in the 11## documentation and/or other materials provided with the distribution. 12## 13## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS'' 14## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16## ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 17## ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18## DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 19## SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 20## CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 21## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 22## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23## 24## The views and conclusions contained in the software and documentation are 25## those of the authors and should not be interpreted as representing official 26## policies, either expressed or implied, of the copyright holders. 27 28function [refMesh, distListIters, refVerticesIters] = averageMesh(meshList, varargin) 29% Compute average mesh from a list of meshes. 30% 31% AVG = averageMesh(MESHLIST) 32% 33% Example 34% averageMesh 35% 36% See also 37% meshes3d 38 39% ------ 40% Author: David Legland 41% e-mail: david.legland@inrae.fr 42% INRAE - BIA Research Unit - BIBS Platform (Nantes) 43% Created: 2020-01-31, using Matlab 9.7.0.1247435 (R2019b) Update 2 44% Copyright 2020 INRAE. 45 46 47%% Parse input values 48 49% default values 50nIters = 10; 51verbose = false; 52 53% parse input arguments 54while length(varargin) > 1 55 name = varargin{1}; 56 if ~ischar(name) 57 error('require parameter name-value pairs'); 58 end 59 60 if strcmpi(name, 'verbose') 61 verbose = varargin{2}; 62 elseif strcmpi(name, 'nIters') 63 nIters = varargin{2}; 64 else 65 error(['Unknown parameter name: ' name]); 66 end 67 varargin(1:2) = []; 68end 69 70 71%% Initialisations 72 73nMeshes = length(meshList); 74 75% initialize kd-trees to accelerate nearest-neighbor searches 76treeList = cell(nMeshes, 1); 77for iMesh = 1:nMeshes 78 treeList{iMesh} = KDTreeSearcher(meshList{iMesh}.vertices); 79end 80 81% choose arbitrary initial mesh 82refMesh = struct('vertices', meshList{1}.vertices, 'faces', meshList{1}.faces); 83 84refVerticesIters = cell(1, nIters); 85distListIters = cell(1, nIters); 86 87 88%% Main iteration 89 90for iIter = 1:nIters 91 if verbose 92 fprintf('iter %d/%d\n', iIter, nIters); 93 end 94 refMesh = smoothMesh(refMesh); 95 96 % create new array for average vertices 97 newVerts = zeros(size(refMesh.vertices)); 98 distList = zeros(size(refMesh.vertices, 1), 1); 99 100 % iterate over all meshes 101 for iMesh = 1:nMeshes 102 if verbose 103 fprintf(' mesh %d/%d\n', iMesh, nMeshes); 104 end 105 106% mesh = meshList{iMesh}; 107 inds = knnsearch(treeList{iMesh}, refMesh.vertices); 108 109% closest = mesh.vertices(inds,:); 110 closest = treeList{iMesh}.X(inds,:); 111 newVerts = newVerts + closest; 112 distList = distList + sum((closest - refMesh.vertices).^2, 2); 113 end 114 115 % update new vertices 116 newVerts = newVerts / nMeshes; 117 refVerticesIters{iIter} = newVerts; 118 refMesh.vertices = newVerts; 119 120 % keep list of distances 121 distList = sqrt(distList / nMeshes); 122 distListIters{iIter} = distList; 123end 124 125 126% figure; drawMesh(refMesh, 'lineStyle', 'none', 'faceColor', [.5 .5 .5]) 127% axis equal; view(3); hold on; axis([-2.5 2.5 -2 2 -3.5 3.5]); light; 128% lighting gouraud 129% title('Average mesh'); 130% print(gcf, 'averageMesh_initial.png', '-dpng'); 131 132