⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fsbf_saveassrf.m

📁 toolbox of BVQX, This is the access between BV and matlab. It will help you to analysis data from BV
💻 M
字号:
function hfile2 = fsbf_SaveAsSRF(hfile, srffilename, options)
% FSBF::SaveAsSRF  - convert a FreeSurfer surface into BVQX's SRF
%
% FORMAT:       srf = fsbf.SaveAsSRF(srffilename [, options]);
%
% Input fields:
%
%       srffilename SRF filename
%       options     if given must be 1x1 struct with optional fields
%        .MeshCenter   1x3 double, default: [128, 128, 128]
%        .ConvexRGBA   1x4 double, default: [0.333, 0.677, 0.980, 0.400]
%        .ConcaveRGBA  1x4 double, default: [0.100, 0.240, 0.333, 0.400]
%        .VertexColor  Vx4 double (see SRF file format)
%        .RefFile      char, referenced file
%
% Output fields:
%
%       srf         SRF object

% Version:  v0.7b
% Build:    7083010
% Date:     Aug-30 2007, 10:32 AM CEST
% Author:   Jochen Weber, Brain Innovation, B.V., Maastricht, NL
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% argument check
if nargin < 2 || ...
    numel(hfile) ~= 1 || ...
   ~isBVQXfile(hfile, 'fsbf') || ...
   ~ischar(srffilename) || ...
    isempty(srffilename)
    error( ...
        'BVQXfile:BadArgument', ...
        'Invalid call to %s.', ...
        mfilename ...
    );
end

% get sizes
bc = bvqxfile_getcont(hfile.L);
vcrd = bc.VertexCoordinate;
numv = size(vcrd, 1);
triv = bc.TriangleVertex;
numt = size(triv, 1);

% check options
if nargin < 3 || ...
   ~isstruct(options) || ...
    numel(options) ~= 1
    options = struct;
end
if isfield(options, 'MeshCenter') && ...
    isa(options.MeshCenter, 'double') && ...
    numel(options.MeshCenter) == 3 && ...
    ~any(isinf(options.MeshCenter) | isnan(options.MeshCenter))
    mshc = options.MeshCenter(:)';
else
    mshc = [128, 128, 128];
end
if isfield(options, 'ConvexRGBA') && ...
    isa(options.ConvexRGBA, 'double') && ...
    numel(options.ConvexRGBA) == 4 && ...
    ~any(isinf(options.ConvexRGBA(:)) | isnan(options.ConvexRGBA(:)) | ...
         options.ConvexRGBA(:) < 0 | options.ConvexRGBA(:) > 1)
    cvxr = options.ConvexRGBA(:)';
else
    cvxr = [0.333, 0.677, 0.98, 0.4];
end
if isfield(options, 'ConcaveRGBA') && ...
    isa(options.ConcaveRGBA, 'double') && ...
    numel(options.ConcaveRGBA) == 4 && ...
    ~any(isinf(options.ConcaveRGBA(:)) | isnan(options.ConcaveRGBA(:)) | ...
         options.ConcaveRGBA(:) < 0 | options.ConcaveRGBA(:) > 1)
    ccvr = options.ConcaveRGBA(:)';
else
    ccvr = [0.1, 0.24, 0.333, 0.4];
end
if isfield(options, 'VertexColor') && ...
    isa(options.VertexColor, 'double') && ...
    size(options.VertexColor, 1) == numv && ...
    size(options.VertexColor, 2) == 4 && ...
    numel(options.VertexColor) == (4 * numv)
    vcol = options.VertexColor;
else
    vcol = zeros(numv, 4);
end
if isfield(options, 'RefFile') && ...
    ischar(options.RefFile) && ...
   ~isempty(options.RefFile)
    rfl = options.RefFile(:)';
else
    rfl = '';
end

% create empty SRF file in memory
hfile2 = BVQXfile('new:srf');

% check file writability
try
    aft_SaveAs(hfile2, srffilename);
catch
    BVQXfile(0, 'clearobj', hfile2.L);
    error( ...
        'BVQXfile:FileNotWritable', ...
        'File not writable: ''%s''.', ...
        srffilename ...
    );
end
bc2 = bvqxfile_getcont(hfile2.L);

% building connections (neighborhood information)
cnx = cell(numv, 2);
tril = cell(numv, 1);
vnrm = zeros(numv, 3);
vnrm(:, 1) = 1;
for tc = 1:numt
    trid = triv(tc, :);
    tril{trid(1)}(end+1) = tc;
    tril{trid(2)}(end+1) = tc;
    tril{trid(3)}(end+1) = tc;
end
for vc = 1:numv
    tric = unique(tril{vc});
    trin = numel(tric);
    trid = triv(tric, :);
    if any(trid(:, 1) == vc)
        matc = find(trid(:, 1) == vc);
        vcnx = trid(matc(1), [2, 3]);
    elseif any(trid(:, 2) == vc)
        matc = find(trid(:, 2) == vc);
        vcnx = trid(matc(1), [3, 1]);
    else
        matc = 1;
        vcnx = trid(1, [1, 2]);
    end
    trid(matc(1), :) = [];
    while size(trid, 1) > 1
        ntc = vcnx(end);
        matc = 0;
        for tc = 1:size(trid, 1)
            if any(trid(tc, :) == ntc)
                matc = tc;
                break;
            end
        end
        if matc == 0
            error( ...
                'BVQXfile:BadObject', ...
                'Missing triangle connection in surface.' ...
            );
        end
        ttri = trid(matc, :);
        trid(matc, :) = [];
        ttri(ttri == vc | ttri == ntc) = [];
        vcnx(end+1) = ttri(1);
    end
    
    cnx{vc, 2} = vcnx;
    cnx{vc, 1} = numel(cnx{vc, 2});
    if cnx{vc, 1} ~= trin
        error( ...
            'BVQXfile:BadObject', ...
            'Bad number of neighbors for vertex %d.', ...
            vc ...
        );
    end
end

% set options
bc2.NrOfVertices = numv;
bc2.NrOfTriangles = numt;
bc2.MeshCenter = mshc;
bc2.VertexCoordinate = ones(numv, 1) * mshc - vcrd(:, [2, 3, 1]);
bc2.VertexNormal = vnrm;
bc2.ConvexRGBA = cvxr;
bc2.ConcaveRGBA = ccvr;
bc2.VertexColor = vcol;
bc2.Neighbors = cnx;
bc2.TriangleVertex = triv;
bc2.AutoLinkedMTC = rfl;

% save to file
bvqxfile_setcont(hfile2.L, bc);
aft_SaveAs(hfile2, srffilename);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -