implementation of subclass from triangulation

조회 수: 2 (최근 30일)
Fabio Freschi
Fabio Freschi 2016년 1월 18일
댓글: Carl Holmberg 2018년 10월 2일
Hi everyone,
I am trying to implement a derived class from the 'triangulation' class available in Matlab 2014. The aim is to introduce additional properties and methods. I tried to mimic the example available in the Matlab documentation, but I failed even in creating a simple constructor. The minimal example is the following
classdef mymesh < triangulation
properties
Obj = [];
end
methods
% constructor
function primal = mymesh(P,T,M) % M is the object code
primal = primal@triangulation(T,P);
primal.Obj = M;
end
end
end
But trying to call it with the code
P = [0 0 0; 0 1 0; 1 1 0; 1 1 1];
T = [1 2 3 4];
M = ones(size(T,1),1);
c = mymesh(P,T,M);
makes Matlab crash. Can anyone point my were I am wrong?
Thank you in advance
Fabio
  댓글 수: 1
Fabio Freschi
Fabio Freschi 2016년 1월 19일
The crash happens in Matlab 2014b. With version 2015b the error message is:
Class 'mymesh' is not an allowed subclass of class 'triangulation'.

댓글을 달려면 로그인하십시오.

채택된 답변

Tom Clark
Tom Clark 2016년 7월 6일
편집: Tom Clark 2016년 7월 6일
Fabio,
To follow up on Rebecca's (correct, IMO) answer, you can overload properties and methods of the triangulation class, to make your new class look exactly like a triangulation.
classdef MyTri
properties(Hidden = true)
TR
end
properties(Dependent)
Points
ConnectivityList
end
methods
function obj = MyTri(varargin)
% your constructor here, which creates a triangulation and stores it in obj.TR
end
function p = get.Points(obj)
% Property getter method, so that MyTri.Points behaves like triangulation.Points
p = obj.TR.Points;
end
function c = get.ConnectivityList(obj)
% Another property getter
c = obj.TR.ConnectivityList;
end
end
end
If you're familiar with python, using the dependent property with a getter is similar to using python's @property decorator on a class method (I'm a MATLAB guy but have to admit that python deals with this way more elegantly).
You may also wish to add a custom isa() method, to return true for both 'MyTri' and 'triangulation' objects in a fashion consistent with the application of isa() to a subclass. I've left that out here, because of course this isn't strictly a subclass.
Adding set.Points and set.ConnectivityList methods (very similar syntax to the getters example above) will allow you to modify your triangulation as normal, if that's what you require.
Finally, having done this myself, I have a bunch of methods to overload the triangulation class methods, just by passing variables through. To save anyone the unbelievable nuisance of duplicating this work, here they are (Caveat emptor: My unit tests don't have 100% coverage yet, so please test them as you use them!):
% Overload all the triangulation class methods for this class
function PC = barycentricToCartesian(obj, ti, B)
PC = barycentricToCartesian(obj.TR, ti, B);
end
function B = cartesianToBarycentric(obj, ti, PC)
B = cartesianToBarycentric(obj.TR, ti, PC);
end
function [CC, r] = circumcenters(obj, varargin)
[CC,r] = circumcenters(obj.TR, varargin{:});
end
function ti = edgeAttachments(obj, varargin)
ti = edgeAttachments(obj.TR, varargin{:});
end
function E = edges(obj)
E = edges(obj.TR);
end
function FN = faceNormal(obj, varargin)
FN = faceNormals(obj.TR, varargin{:});
end
function FE = featureEdges(obj, filterangle)
FE = featureEdges(obj.TR, filterangle);
end
function [FBtri, FBpoints] = freeBoundary(obj)
[FBtri, FBpoints] = freeBoundary(obj.TR);
end
function [IC,r] = incenter(obj,ti)
[IC,r] = incenter(obj.TR,ti);
end
function [tf] = isConnected(obj,varargin)
tf = isConnected(obj.TR,varargin{:});
end
function [vi,d] = nearestNeighbor(obj,varargin)
[vi,d] = nearestNeighbour(obj.TR,varargin{:});
end
function N = neighbors(obj, varargin)
N = neighbors(obj.TR, varargin{:});
end
function SZ = size(obj)
SZ = size(obj.TR);
end
function ti = vertexAttachments(obj, varargin)
ti = vertexAttachments(obj.TR, varargin{:});
end
function VN = vertexNormal(obj,varargin)
VN = vertexNormal(obj.TR,varargin{:});
end
Hope this helps, please upvote me if it does :)
Tom
  댓글 수: 1
Carl Holmberg
Carl Holmberg 2018년 10월 2일
I have used this code and greatly apreaciate it! One can note that there is an "s" added to the end of the function call:
faceNormals(obj.TR, varargin{:});

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Rebecca Krosnick
Rebecca Krosnick 2016년 1월 20일
It seems that we cannot create a subclass of "triangulation". Instead, you can create a class that has a "triangulation" object as a property rather than creating a subclass of "triangulation".
  댓글 수: 2
Fabio Freschi
Fabio Freschi 2016년 1월 20일
Thanks Rebecca, my question is: to access the points the syntax will be
mymesh.TR.Points
won't it?
Tom Clark
Tom Clark 2016년 7월 6일
Yes it will. I've extended Rebecca's answer below to address this with a full example.

댓글을 달려면 로그인하십시오.


per isakson
per isakson 2016년 1월 21일
편집: per isakson 2016년 1월 21일
You are too kind to Matlab. Matlab shall not crash whether or not you make a mistake. Now I crashed R2013b a couple of time with your code. I assume you need to ask support.
However, you might want to try this
P = [0 0 0; 0 1 0; 1 1 0; 1 1 1];
T = [1 2 3 4];
M = ones(size(T,1),1);
tr = triangulation(T,P);
mym = mymesh(tr,M);
mym.TR.Points
outputs
ans =
0 0 0
0 1 0
1 1 0
1 1 1
where
classdef mymesh < handle
properties
Obj = [];
TR
end
methods
% constructor
function primal = mymesh( tr, M ) % M is the object code
primal.Obj = M;
primal.TR = tr;
end
end
end
  댓글 수: 3
per isakson
per isakson 2016년 2월 12일
편집: per isakson 2016년 2월 12일
It's not "weird"; it's composition
Arabarra
Arabarra 2016년 11월 8일
it's still weird that one should need to use composition instead of subclassing. TriRep (the predecessor of triangulation) used to allow subclassing (till R2016b)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Timing and presenting 2D and 3D stimuli에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by