I
ivan.leben
I want to write a Mesh class using half-edges. This class uses three
other classes: Vertex, HalfEdge and Face. These classes should be
linked properly in the process of building up the mesh by calling Mesh
class functions. Let's say they point to each other like this:
class Vertex {
HalfEdge *edge;
};
class HalfEdge {
Vertex* vert;
HalfEdge *twin;
HalfEdge *next;
Face* face;
};
class Face {
HalfEdge *edge;
};
Now I want to be able to derive from these classes in order to add more
functionality to them (e.g. normal). However, I want that as I derive
from a class, all the pointers among the three classes use the new
derived class instead of base one, so I don't have to cast Vertex* to
MyVertex* everytime I search for an edge->next->vert vertex of next
edge in the loop.
What I try to achieve would look like the three classes being nested
inside the Mesh class, which is a template class and takes template
arguments for both three classes. Let me illustrate:
templace <class VertType, class HalfEdgeType, class FaceType>
class Mesh {
public:
class Vertex {
HalfEdgeType *edge;
};
class HalfEdge {
VertType *vert;
HalfEdgeType *next;
[and so on....]
};
Now, even though this code compiles perfectly it is impossible to use
it as I want because in order to use any of the three nested classes I
would have to specify template arguments for the Mesh class first, but
these arguments have to be the same nested classes I am trying to
instantiate at the same time.
Of course I could solve this by defining base Vertex, HalfEdge and Face
classes outside the Mesh class, then make Mesh class template and
define nested "iterator" classes, that would walk over links and
automatically cast pointers to my derived classes that I pass as
template arguments to the Mesh class. But I see no practical sense in
constructing and using an "iterator" just to jump from one edge to its
next edge.
Is there any other possibility of how I could inter-link several
classes by using pointers of their derives? Or do I have no other way
than to either perform annoying casting of pointer types or construct
and "++" an iterator each time I follow an edge's next link?
other classes: Vertex, HalfEdge and Face. These classes should be
linked properly in the process of building up the mesh by calling Mesh
class functions. Let's say they point to each other like this:
class Vertex {
HalfEdge *edge;
};
class HalfEdge {
Vertex* vert;
HalfEdge *twin;
HalfEdge *next;
Face* face;
};
class Face {
HalfEdge *edge;
};
Now I want to be able to derive from these classes in order to add more
functionality to them (e.g. normal). However, I want that as I derive
from a class, all the pointers among the three classes use the new
derived class instead of base one, so I don't have to cast Vertex* to
MyVertex* everytime I search for an edge->next->vert vertex of next
edge in the loop.
What I try to achieve would look like the three classes being nested
inside the Mesh class, which is a template class and takes template
arguments for both three classes. Let me illustrate:
templace <class VertType, class HalfEdgeType, class FaceType>
class Mesh {
public:
class Vertex {
HalfEdgeType *edge;
};
class HalfEdge {
VertType *vert;
HalfEdgeType *next;
[and so on....]
};
Now, even though this code compiles perfectly it is impossible to use
it as I want because in order to use any of the three nested classes I
would have to specify template arguments for the Mesh class first, but
these arguments have to be the same nested classes I am trying to
instantiate at the same time.
Of course I could solve this by defining base Vertex, HalfEdge and Face
classes outside the Mesh class, then make Mesh class template and
define nested "iterator" classes, that would walk over links and
automatically cast pointers to my derived classes that I pass as
template arguments to the Mesh class. But I see no practical sense in
constructing and using an "iterator" just to jump from one edge to its
next edge.
Is there any other possibility of how I could inter-link several
classes by using pointers of their derives? Or do I have no other way
than to either perform annoying casting of pointer types or construct
and "++" an iterator each time I follow an edge's next link?