Question

I am trying to set the values for the 2nd and 3rd corners on three objects of class triangle. If done explicitly, this looks like this:

triangle_mesh[0].set_vector_point2(vector_anchors[1]);
triangle_mesh[1].set_vector_point3(vector_anchors[1]);
triangle_mesh[1].set_vector_point2(vector_anchors[2]);
triangle_mesh[2].set_vector_point3(vector_anchors[2]);
triangle_mesh[2].set_vector_point2(vector_anchors[3]);
triangle_mesh[0].set_vector_point3(vector_anchors[3]);

and this works! Printing these triangles, I get (n.b. the first corner has already been set - this is not an issue):

triangle0 is ( 0, 0, -1) (1, 0, 0) (-0.5, -0.866025, 0)
triangle1 is ( 0, 0, -1) (-0.5, 0.866025, 0) (1, 0, 0)
triangle2 is ( 0, 0, -1) (-0.5, -0.866025, 0) (-0.5, 0.866025, 0)

Firstly, though, this is ugly, and secondly it must generalise to cases where I have more than three triangles to set up. My code for this is:

for (int longitude = 0; longitude < num_longitudes; longitude++){
  SurfaceVector current_anchor = vector_anchors[1 + longitude];
    triangle_mesh[longitude].set_vector_point2(current_anchor);
    triangle_mesh[(longitude + 1) % num_longitudes].set_vector_point3(current_anchor);
}

*n.b. num_longitudes is 3*

I've checked everything I can think of, but now when I print out my triangles I get:

triangle0 is ( 0, 0, -1) (-0.5, -0.866025, 0) (-0.5, -0.866025, 0)
triangle1 is ( 0, 0, -1) (-0.5, -0.866025, 0) (-0.5, -0.866025, 0)
triangle2 is ( 0, 0, -1) (-0.5, -0.866025, 0) (-0.5, -0.866025, 0)

Does anyone have any idea what could be going wrong?!

EDIT

The vector_point variables on the triangle are pointers, and are set like:

void set_vector_point1(SurfaceVector vector_point) { vector_point1 = &vector_point; }
Was it helpful?

Solution

There lies your problem:

void set_vector_point1(SurfaceVector vector_point) { vector_point1 = &vector_point; }

You are pointing to a temporary (vector_point ceases to exist once the function call completes). Change it so you copy SurfaceVector properly.

OTHER TIPS

I'd change:

void set_vector_point1(SurfaceVector vector_point) { vector_point1 = &vector_point; }

to

void set_vector_point1(SurfaceVector& vector_point) { vector_point1 = &vector_point; }

Or something like that.

In the current version vector_point will be a copy of whatever you pass, and will no longer exist after, you're storing a pointer to an object that no longer exists.

In the second one, vector_point is a reference to a longer lived object external to the function. Storing a pointer to that is fine as the object will still be there when you use the pointer.

The trick is to make sure the object lives longer than all the pointers to it.

ADDITION:

Thanks @Nim in the below comment:

Also in the for loop where the line is:

SurfaceVector current_anchor = vector_anchors[1 + longitude];

That should probable be a reference too .. at the moment it too is a copy. That way you'll be editing the actual objects in the array, rather than playing around with copies and throwing them away. So I would change that line to:

SurfaceVector& current_anchor = vector_anchors[1 + longitude];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top