I am having an issue with 2 classes that were once nicely separated, but now they want to couple.
Without getting too much into the specifics of the problem, here it is:
I used to have a class Triangle that contained 3 space-position vertices.
class Triangle
{
Vertex a,b,c ; // vertices a, b and c
} ;
There were many Triangle instances in the program, so each had retained their own copy of their vertices. Member functions such as getArea()
, getCentroid()
etc were written in class Triangle
, and since each Triangle
instance had copies of Vertex a, b and c, finding the area or centroid had no dependence on other classes. As it should be!
Then I wanted to move to a vertex-array/index buffer style representation, for other reasons. This means all vertices are stored in a single array located in a Scene
object, and each Triangle
retains only REFERENCES to the vertices in Scene
, not copies of the vertices themselves. At first, I tried switching out for pointers:
class Scene
{
std::vector<Vertex> masterVertexList ;
} ;
class Triangle
{
Vertex *a,*b,*c ; // vertices a, b and c are pointers
// into the Scene object's master vertex list
} ;
(In case you're wondering about the benefits, I did it for reasons mostly to do with triangles that share vertices. If *a moves then all triangles that use that vertex are updated automatically).
This would have been a really good solution! But it didn't work reliably, because std::vector invalidates pointers, and I was using a std::vector for the master vertex list in class Scene
.
So I had to use integers:
class Triangle
{
int a,b,c ; // integer index values
// into the Scene object's master vertex list
} ;
But now I have this new coupling problem: to find its own area or centroid, class Triangle
needs access to class Scene
where before it did not. It seems like I've fsck`d something up, but not really.
WWYD?