Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ vector loses data in recursive function
    text
    copied!<p>I am very new to C++ and I am trying to implement a TriangleDynamic object that can recursively split itself using a function called splitTriangleProject. It splits itself into four smaller TriangleDynamic objects (and projects the vertices of the new triangles onto a sphere with a given radius and origin, but I believe this is beside the point), pushes the newly created triangles into a vector that is part of the member data of the original object. The vector member data is called subTriangles. Then each subTriangle recalls the splitTriangleProject function until a certain "level" of splits has occurred. </p> <p>The issue I am having is that only the first level of splitting is actually properly pushing to the subTriangles vector. I am certain that the issue has to do with the vectors going out of scope, or maybe the TriangleDynamic going out of scope. I imagine there is some solution with pointers. If anyone could help, it would be much appreciated.</p> <p>Here is my TriangleDynamic declaration:</p> <pre><code>class TriangleDynamic { public: TriangleDynamic(const Point &amp;P1, const Point &amp;P2, const Point &amp;P3); TriangleDynamic(); ~TriangleDynamic(){} void printTriangle(); void splitTriangleProject( int currentLevel, int maxLevel, const Point &amp;org, double radius); void init(); bool operator&lt;(const TriangleDynamic&amp;); bool operator&gt;(const TriangleDynamic&amp;); Line edge1; Line edge2; Line edge3; Point p1; Point p2; Point p3; Vect normal; bool lowestLevel; vector&lt;TriangleDynamic&gt; subTriangles; static int numTriangles; int triangleId; }; int TriangleDynamic::numTriangles = 0; </code></pre> <p>and the constructors:</p> <pre><code>// constructor for the TriangleDynamic object TriangleDynamic::TriangleDynamic(const Point &amp;P1, const Point &amp;P2, const Point &amp;P3) { p1 = P1; p2 = P2; p3 = P3; init(); } TriangleDynamic::TriangleDynamic() { p1 = Point(0,0,0); p2 = Point(0,0,1); p3 = Point(0,1,0); init(); } void TriangleDynamic::init() { edge1 = Line(p1,p2); edge2 = Line(p2,p3); edge3 = Line(p3,p1); Vect U = p2.minus( p1); Vect V = p3.minus(p1); normal = U.cross(V); lowestLevel = true; triangleId = numTriangles + 1; numTriangles = triangleId; } </code></pre> <p>Here is my splitTriangleProject function:</p> <pre><code>void TriangleDynamic::splitTriangleProject(int currentLevel, int maxLevel, const Point &amp;org, double radius) { if ( currentLevel &lt; maxLevel) { lowestLevel = false; Point worldOrigin = Point(0,0,0); double edge1MidMag = (edge1.midpoint - org).distance(worldOrigin) ; double edge2MidMag = (edge2.midpoint - org).distance(worldOrigin) ; double edge3MidMag = (edge3.midpoint - org).distance(worldOrigin) ; Point newEdge1Mid = (((edge1.midpoint) * radius )/ edge1MidMag) + org; Point newEdge2Mid = (((edge2.midpoint) * radius )/ edge2MidMag) + org; Point newEdge3Mid = (((edge3.midpoint) * radius )/ edge3MidMag) + org; TriangleDynamic t1(p1 , newEdge1Mid , newEdge3Mid); subTriangles.push_back(t1); TriangleDynamic t2(newEdge1Mid, p2, newEdge2Mid); subTriangles.push_back(t2); TriangleDynamic t3(newEdge3Mid, newEdge2Mid, p3); subTriangles.push_back(t3); TriangleDynamic t4(newEdge1Mid, newEdge2Mid, newEdge3Mid); subTriangles.push_back(t4); t1.splitTriangleProject( currentLevel + 1, maxLevel, org, radius); t2.splitTriangleProject( currentLevel + 1, maxLevel, org, radius); t3.splitTriangleProject( currentLevel + 1, maxLevel, org, radius); t4.splitTriangleProject( currentLevel + 1, maxLevel, org, radius); } } </code></pre> <p>Here is a function for recursively printing the triangleDynamic when it's done:</p> <pre><code>void TriangleDynamic::printTriangle() { cout&lt;&lt; "p1"; p1.printPoint(); cout &lt;&lt; "\np2"; p2.printPoint(); cout &lt;&lt; "\np3"; p3.printPoint(); cout &lt;&lt; "\n\n"; if(!lowestLevel) { int ctr; for (ctr=0; ctr&lt;=subTriangles.size()-1; ctr++) { cout &lt;&lt; "subTriangle\n"; subTriangles[ctr].printTriangle(); } } } </code></pre> <p>Here is how I call it in my main:</p> <pre><code>int main() { TriangleDynamic t = TriangleDynamic(); t.splitTriangleProject(0,3,Point(), 1); t.printTriangle(); cin.get(); return 0; } </code></pre> <p>I think I've posted too much at this point. Any help is appreciated, thanks in advance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload