# HELP_ellipsoidFit_centered

Below is a demonstration of the features of the ellipsoidFit_centered function

## Syntax

[M,ellipStretch,R,MU]=ellipsoidFit_centered(X,MU);

## Description

The ellipsoidFit_centered function fits an ellipsoid to data when the ellipsoid centre is known. If the centre is not provided the mean of the input point set will be assumed to be the centre.

## Examples

```clear; close all; clc;
```

Plot settings

```figColor='w';
figColorDef='white';
fontSize=11;
```

## Example: Using ellipsoidFit_centered to fit an ellipsoid to a point cloud with known centre

Simulating an ellipsoid with known directions

```% Ellipsoid axis stretch factors
ellipStretchTrue=[pi 2 0.5];
MU_true=[1 6 pi];

% Create ellipsoid patch data
[F,X,~]=geoSphere(3,1);
x=X(:,1);
FX=mean(x(F),2);
logicKeep=FX>0;
F=F(logicKeep,:);
indKeep=unique(F(:));
indFix=nan(size(X,1),1);
indFix(indKeep)=1:numel(indKeep);
X=X(indKeep,:);
F=indFix(F);
X=X.*ellipStretchTrue(ones(size(X,1),1),:);

%Create Euler angles to set directions
E=[0.25*pi 0.25*pi -0.25*pi];
[R_true,~]=euler2DCM(E); %The true directions for X, Y and Z axis
X=(R_true*X')'; %Rotate polyhedron

X=X+MU_true(ones(size(X,1),1),:); %Centre points around mean

n_std=0.2;  %Standard deviation
Xn=X+n_std.*randn(size(X));
```

This is the true axis system

```R_true
```
```R_true =

0.5000    0.5000    0.7071
-0.1464    0.8536   -0.5000
-0.8536    0.1464    0.5000

```

These are the true stretch factors

```ellipStretchTrue
```
```ellipStretchTrue =

3.1416    2.0000    0.5000

```
```[M,ellipStretchFit,R_fit,MU]=ellipsoidFit_centered(Xn,MU_true);
```

This is the fitted axis system. The system axes should be colinear with the true axes but can be oposite in direction.

```R_fit=R_fit(1:3,1:3)
```
```R_fit =

0.5064   -0.5000   -0.7026
-0.1425   -0.8521    0.5037
-0.8505   -0.1549   -0.5027

```

These are the fitted stretch factors

```ellipStretchFit
```
```ellipStretchFit =

3.0240    2.0093    0.6032

```

Building a fitted (clean) ellipsoid for visualization

```%Create sphere
[F_fit,V_fit,~]=geoSphere(4,1);

%Transforming sphere to ellipsoid
V_fit_t=V_fit;
V_fit_t(:,end+1)=1;
V_fit_t=(M*V_fit_t')'; %Rotate polyhedron
V_fit=V_fit_t(:,1:end-1);
```

Visualizing results

```MU=mean(X,1); %Origin for vectors
a=[7 7]; %Vector size

[Fq,Vq,Cq]=quiver3Dpatch(MU(1)*ones(1,3),MU(2)*ones(1,3),MU(3)*ones(1,3),R_fit(1,:),R_fit(2,:),R_fit(3,:),eye(3,3),a); %Fitted vectors
[Fq2,Vq2,Cq2]=quiver3Dpatch(MU(1)*ones(1,3),MU(2)*ones(1,3),MU(3)*ones(1,3),R_true(1,:),R_true(2,:),R_true(3,:),eye(3,3),a); %True vectors

figuremax(figColor,figColorDef);
title('The true (green) and fitted ellipsoid (red) and axis directions (solid, transparant respectively)','FontSize',fontSize);
xlabel('X','FontSize',fontSize); ylabel('Y','FontSize',fontSize); zlabel('Z','FontSize',fontSize);
hold on;

plotV(Xn,'k.','MarkerSize',15);

hp=patch('Faces',F,'Vertices',X);
set(hp,'FaceColor','g','FaceAlpha',1,'EdgeColor','k');

hp=patch('Faces',F_fit,'Vertices',V_fit);
set(hp,'FaceColor','r','FaceAlpha',0.2,'EdgeColor','none');

patch('Faces',Fq,'Vertices',Vq,'FaceColor','flat','FaceVertexCData',Cq,'FaceAlpha',1);
patch('Faces',Fq2,'Vertices',Vq2,'FaceColor','flat','FaceVertexCData',Cq2,'FaceAlpha',0.2,'EdgeColor','none');
axis equal; view(3); axis vis3d; axis tight;  grid on;
set(gca,'FontSize',fontSize);
drawnow;
```

GIBBON www.gibboncode.org

GIBBON footer text

GIBBON: The Geometry and Image-based Bioengineering add-On. A toolbox for image segmentation, image-based modeling, meshing, and finite element analysis.

Copyright (C) 2017 Kevin Mattheus Moerman

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.