pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

C++ pastebin - collaborative debugging tool View Help


Posted by Fourth on Mon 17 Nov 15:15
report abuse | download | new post

  1. /*
  2.  * shape.cpp
  3.  *
  4.  * Tessellation algorithms by Nicholas Forysinski
  5.  *
  6.  * This file performs tessellations on 4 different shapes: cones, spheres,
  7.  * cubes, and cylinders. It uses various iterative methods for creating
  8.  * triangle tessellations in these 4 shapes.
  9.  *
  10.  */
  11.  
  12.  
  13. #include "shape.h"
  14. #include "draw_routines.h"
  15. #include <fstream>
  16. #include <math.h>
  17.  
  18. // you must write this file
  19.  
  20. const double PI = 3.14159265359;
  21.  
  22. Shape::~Shape() {
  23. }
  24.  
  25. void Shape::draw() {
  26.   for (unsigned int i = 0; i < vertices.size(); i += 3)
  27.     DrawRoutines::drawTriangle(vertices[i], vertices[i + 1], vertices[i + 2]);
  28. }
  29.  
  30. Shape::Shape() : vertices() {
  31. }
  32.  
  33. void Shape::addTriangle(const Point3& p1, const Point3& p2, const Point3& p3) {
  34.   vertices.push_back(p1);
  35.   vertices.push_back(p2);
  36.   vertices.push_back(p3);
  37. }
  38.  
  39. void Shape::addSquare(const Point3& p1, const Point3& p2,
  40.                       const Point3& p3, const Point3& p4) {
  41.  
  42.   addTriangle(p1,p2,p3);
  43.   addTriangle(p3,p4,p1);
  44.  
  45. }
  46.  
  47. //container is the set of vertices that create the polygons
  48. void Shape::subdivide( vector<Point3>& container, const Point3& p0,
  49.                        const Point3& p1, const Point3& p2, const int level ) {
  50.  
  51.   vector<Point3> tempPoly = container; //holds the old sets of polygons
  52.  
  53.   //subdivide each original triangle by 'n' number of levels
  54.   for( int l = 1; l < level; l++ ) {
  55.     vector<Point3> newPoly = tempPoly;
  56.  
  57.     //split a single triangle into 4 new triangles
  58.     for( int i = 0; i < tempPoly.size(); i++ ) {
  59.  
  60.       Point3 a,b,c;
  61.  
  62.       a = a;
  63.     }
  64.  
  65.   }
  66.  
  67. }
  68.  
  69. Cube::Cube(int n) : Shape() {
  70.  
  71.   double x1,x2,y1,y2,z1,z2;
  72.   x1 = -0.5;
  73.   x2 = 0.5;
  74.   y1 = -0.5;
  75.   y2 = 0.5;
  76.   z1 = .5;       // front
  77.   z2 = -.5;      // back
  78.  
  79.   Point3 op3(x2,y2,z1);
  80.   Point3 op2(x1,y2,z1);
  81.   Point3 op1(x1,y1,z1);
  82.   Point3 p3 = op3;
  83.   Point3 p2 = op2;
  84.   Point3 p1 = op1;
  85.   float fx = n-1;
  86.   float fy = n-1;
  87.   float fz = n-1;
  88.   Point3 temp = p3;
  89.  
  90.   //front and back
  91.   //iterate through the square creating smaller squares made up of 2 triangles
  92.   for( int x = 0; x < 2; x++ ) {
  93.  
  94.     for( int i = 0; i < n; i++ ) { //rows
  95.       float facty = fy/n;
  96.       float npy = (1-facty)*p1.y + facty*p2.y;
  97.  
  98.       for( int j = 0; j < n; j++ ) { //cols
  99.         float factx = fx/n;
  100.         float npx = (1-factx)*p2.x + factx*p3.x;
  101.  
  102.         Point3 topL( npx, temp.y, temp.z);
  103.         Point3 lowR( temp.x, npy, temp.z);
  104.         Point3 midP( npx, npy, temp.z);
  105.  
  106.         addSquare( temp, topL, midP, lowR );
  107.  
  108.         temp = topL;
  109.         fx--;
  110.       }
  111.  
  112.       fx = n-1;
  113.       temp.x = p3.x;
  114.       temp.z = p3.z;
  115.       temp.y = npy;
  116.       fy--;
  117.     }
  118.     fy = n-1;
  119.     //reset for the back
  120.     p2 = op3;
  121.     p3 = op2;
  122.     p1.x = p2.x;
  123.     p3.z = z2;
  124.     p2.z = z2;
  125.     p1.z = z2;
  126.     temp = p3;
  127.   }
  128.  
  129.   //set up for the top
  130.   p3 = op3;
  131.   p2 = op2;
  132.   p1 = op1;
  133.   p3.z = z2;
  134.   p2.z = z2;
  135.   p1.y = p2.y;
  136.   fx = n-1;
  137.   temp = p3;
  138.  
  139.   //top and bottom
  140.   for( int x = 0; x < 2; x++ ) {
  141.  
  142.     for( int i = 0; i < n; i++ ) { //rows
  143.       float factz = fz/n;
  144.       float npz = (1-factz)*p1.z + factz*p2.z;
  145.  
  146.       for( int j = 0; j < n; j++ ) { //cols
  147.         float factx = fx/n;
  148.         float npx = (1-factx)*p2.x + factx*p3.x;
  149.  
  150.         Point3 topL( npx, temp.y, temp.z);
  151.         Point3 lowR( temp.x, temp.y, npz);
  152.         Point3 midP( npx, temp.y, npz);
  153.  
  154.         addSquare( temp, topL, midP, lowR );
  155.  
  156.         temp = topL;
  157.         fx--;
  158.       }
  159.  
  160.       fx = n-1;
  161.       temp.x = p3.x;
  162.       temp.z = npz;
  163.       temp.y = p3.y;
  164.       fz--;
  165.     }
  166.     fz = n-1;
  167.     //reset for the bottom
  168.     p3 = op3;
  169.     p2 = op1;
  170.     p1 = op1;
  171.     p1.z = z2;
  172.     p3.y = p1.y;
  173.     temp = p3;
  174.   }
  175.  
  176.   //set up for the right
  177.   p3 = op3;
  178.   p2 = op3;
  179.   p1.x = p2.x;
  180.   p3.z = z2;
  181.   fz = n-1;
  182.   fy = n-1;
  183.   temp = p3;
  184.  
  185.   //top and bottom
  186.   for( int x = 0; x < 2; x++ ) {
  187.  
  188.     for( int i = 0; i < n; i++ ) { //rows
  189.       float facty = fy/n;
  190.       float npy = (1-facty)*p1.y + facty*p2.y;
  191.  
  192.       for( int j = 0; j < n; j++ ) { //cols
  193.         float factz = fz/n;
  194.         float npz = (1-factz)*p2.z + factz*p3.z;
  195.  
  196.         Point3 topL( temp.x, temp.y, npz);
  197.         Point3 lowR( temp.x, npy, temp.z);
  198.         Point3 midP( temp.x, npy, npz);
  199.  
  200.         addSquare( temp, topL, midP, lowR );
  201.  
  202.         temp = topL;
  203.         fz--;
  204.       }
  205.  
  206.       fz = n-1;
  207.       temp.x = p3.x;
  208.       temp.z = p3.z;
  209.       temp.y = npy;
  210.       fy--;
  211.     }
  212.     fy = n-1;
  213.     //reset for the left
  214.     p3 = op2;
  215.     p2 = op2;
  216.     p2.z = z2;
  217.     p1 = op1;
  218.     p1.z = z2;
  219.     temp = p3;
  220.   }
  221.  
  222. }
  223.  
  224. Cone::Cone(int n, int m) : Shape() {
  225.   if (n < 3)
  226.     n = 3;
  227.  
  228.   double x1,y1,z1,x2,y2,z2;
  229.   x1 = -0.5;
  230.   x2 = 0.5;
  231.   y1 = -0.5;
  232.   y2 = 0.5;
  233.   z1 = .5;
  234.   z2 = -.5;
  235.  
  236.   Point3 fixedPB( 0, y1, 0 ); //fixed point at the bottom
  237.   Point3 fixedPT( 0, y2, 0 ); //fixed point at the top
  238.   vector<Point3> circumPoints; //vector to hold circumference points
  239.  
  240.   //do the bottom first
  241.   //begin by solving the circumference points
  242.   float angle = 360/n; //get the angle interval
  243.   for( double i = 0; i < 360; i+=angle ) {
  244.  
  245.     double px = .5*cos( i * (PI/180) );
  246.     double pz = .5*sin( i * (PI/180) );
  247.  
  248.     Point3 circP( px, y2, pz );
  249.     circumPoints.push_back(circP);
  250.  
  251.   }
  252.  
  253.   //iterate through to create the bottom of the cone
  254.   for( int i = 0; i < n; i++ ) {
  255.  
  256.     int t = i;
  257.  
  258.     Point3 p1 = circumPoints.at(t);
  259.     if( t+1 == n )
  260.       t = -1;
  261.     Point3 p2 = circumPoints.at(t+1);
  262.  
  263.     p1.y = y1;
  264.     p2.y = y1;
  265.  
  266.     addTriangle( p1, p2, fixedPB );
  267.  
  268.   }
  269.  
  270.   /*
  271.    * to make the cone part, we start from the fixed point at the top
  272.    * and move down iterations of fy. the first iteration will not
  273.    * make a square, but all others will.
  274.    */
  275.   float f = m-1;
  276.   for( int i = 0; i < n; i++ ) {
  277.  
  278.     int t = i;
  279.     //grab the first 2 circumference points
  280.     Point3 p1 = circumPoints.at(t);
  281.     if(t+1 == n) {
  282.       t = -1; //go back to the beginning of the vector
  283.     }
  284.     Point3 p2 = circumPoints.at(t+1);
  285.     p1.y = y1;
  286.     p2.y = y1;
  287.     Point3 tempr = p1;
  288.     Point3 templ = p2;
  289.  
  290.     for( int i = 0; i < m; i++ ) {
  291.       float fact = f/m;
  292.       float npy = (1-fact)*p1.y + fact*fixedPT.y;
  293.       float npzr = (1-fact)*p1.z + fact*fixedPT.z;
  294.       float npzl = (1-fact)*p2.z + fact*fixedPT.z;
  295.       float npxr = (1-fact)*p1.x + fact*fixedPT.x;
  296.       float npxl = (1-fact)*p2.x + fact*fixedPT.x;
  297.  
  298.       Point3 uppR( npxr, npy, (-1)*npzr );
  299.       Point3 uppL( npxl, npy, (-1)*npzl );
  300.  
  301.       if( i == 0 ) { //first iteration, so build a triangle at the top
  302.         addTriangle( uppL, fixedPT, uppR );
  303.       } else {            //all other iterations make squares
  304.         addSquare( uppR, uppL, templ, tempr );
  305.       }
  306.  
  307.       tempr = uppR;
  308.       templ = uppL;
  309.       f--;
  310.     }
  311.     f = m-1;
  312.   }
  313.  
  314. }
  315.  
  316. Cylinder::Cylinder(int n, int m) : Shape() {
  317.   if (n < 3)
  318.     n = 3;
  319.  
  320.   double x1,y1,z1,x2,y2,z2;
  321.   x1 = -0.5;
  322.   x2 = 0.5;
  323.   y1 = -0.5;
  324.   y2 = 0.5;
  325.   z1 = .5;
  326.   z2 = -.5;
  327.  
  328.   Point3 fixedPT( 0, y2, 0 );
  329.   Point3 fixedPB( 0, y1, 0 );
  330.   vector<Point3> circumPoints; //vector to hold circumference points
  331.  
  332.   //begin with the top and bottom
  333.   //while n > 0, add points to the circumference and try triangles
  334.   float angle = 360/n; //get the angle interval
  335.  
  336.   for( double i = 0; i < 360; i+=angle ) {
  337.  
  338.     double px = .5*cos( i * (PI/180) );
  339.     double pz = .5*sin( i * (PI/180) );
  340.  
  341.     Point3 circP( px, y2, pz );
  342.     circumPoints.push_back(circP);
  343.  
  344.   }
  345.  
  346.  
  347.   //iterate through to make the top and the bottom
  348.   for( int i = 0; i < n; i++ ) {
  349.  
  350.     int t = i;
  351.  
  352.     Point3 p1 = circumPoints.at(t);
  353.     if( t+1 == n )
  354.       t = -1;
  355.     Point3 p2 = circumPoints.at(t+1);
  356.  
  357.     addTriangle( p2, p1, fixedPT );
  358.  
  359.     p1.y = y1;
  360.     p2.y = y1;
  361.  
  362.     addTriangle( p1, p2, fixedPB );
  363.  
  364.   }
  365.  
  366.   //for each circumference point and the next circumference point
  367.   //draw squares all the way down
  368.   float fy = m-1;
  369.   for( int x = 0; x < n; x++ ) { //number of faces
  370.  
  371.     int t = x;
  372.     //grab the first 2 circumference points
  373.     Point3 p1 = circumPoints.at(t);
  374.     if(t+1 == n) {
  375.       t = -1; //go back to the beginning of the vector
  376.     }
  377.     Point3 p2 = circumPoints.at(t+1);
  378.     Point3 p3 = p2;
  379.     Point3 temp = p2;
  380.     p3.y = y1;
  381.  
  382.     for( int i = 0; i < m; i++ ) {
  383.       float facty = fy/m;
  384.       float npy = (1-facty)*p3.y + facty*p2.y;
  385.  
  386.       Point3 lowR( p1.x, npy, p1.z );
  387.       Point3 midP( temp.x, npy, temp.z );
  388.  
  389.       addSquare( p1, temp, midP, lowR );
  390.  
  391.       p1 = lowR;
  392.       temp = midP;
  393.       fy--;
  394.     }
  395.     fy = m-1;
  396.   }
  397.  
  398. }
  399.  
  400. Sphere::Sphere(int n) : Shape() {
  401.  
  402.   //your code for tessellating a sphere goes here
  403.  
  404.   if( n > 5 )
  405.     n = 5;
  406.  
  407.   float five = 5.0;
  408.   float a = 2.0 / ( 1.0 + sqrt(five) );
  409.  
  410.   Vector3 origin(0,0,0);
  411.  
  412.   //icosahedron vertices
  413.   Vector3 tv0(0 , a, -1);
  414.   Vector3 tv1(-a, 1, 0);
  415.   Vector3 tv2(a, 1, 0);
  416.   Vector3 tv3(0, a, 1);
  417.   Vector3 tv4(-1, 0, a);
  418.   Vector3 tv5(0, -a, 1);
  419.   Vector3 tv6(1, 0, a);
  420.   Vector3 tv7(1, 0, -a);
  421.   Vector3 tv8(0, -a, -1);
  422.   Vector3 tv9(-1, 0, -a);
  423.   Vector3 tv10(-a, -1, 0);
  424.   Vector3 tv11(a, -1, 0);
  425.  
  426.   //create the triangles in the original mesh
  427.   vector< vector<Vector3> > triangles;
  428.  
  429.   //make triangle 0 and add it
  430.   vector<Vector3> triangle0;
  431.   triangle0.push_back(tv0);
  432.   triangle0.push_back(tv1);
  433.   triangle0.push_back(tv2);
  434.   triangles.push_back(triangle0);
  435.   //make triangle 1 and add it
  436.   triangle0.clear();
  437.   triangle0.push_back(tv3);
  438.   triangle0.push_back(tv2);
  439.   triangle0.push_back(tv1);
  440.   triangles.push_back(triangle0);
  441.   //make triangle 2 and add it
  442.   triangle0.clear();
  443.   triangle0.push_back(tv3);
  444.   triangle0.push_back(tv4);
  445.   triangle0.push_back(tv5);
  446.   triangles.push_back(triangle0);
  447.   //make triangle 3 and add it
  448.   triangle0.clear();
  449.   triangle0.push_back(tv3);
  450.   triangle0.push_back(tv5);
  451.   triangle0.push_back(tv6);
  452.   triangles.push_back(triangle0);
  453.   //make triangle 3 and add it
  454.   triangle0.clear();
  455.   triangle0.push_back(tv0);
  456.   triangle0.push_back(tv7);
  457.   triangle0.push_back(tv8);
  458.   triangles.push_back(triangle0);
  459.   //make triangle 4 and add it
  460.   triangle0.clear();
  461.   triangle0.push_back(tv0);
  462.   triangle0.push_back(tv8);
  463.   triangle0.push_back(tv9);
  464.   triangles.push_back(triangle0);
  465.   //make triangle 5 and add it
  466.   triangle0.clear();
  467.   triangle0.push_back(tv5);
  468.   triangle0.push_back(tv10);
  469.   triangle0.push_back(tv11);
  470.   triangles.push_back(triangle0);
  471.   //make triangle 6 and add it
  472.   triangle0.clear();
  473.   triangle0.push_back(tv8);
  474.   triangle0.push_back(tv11);
  475.   triangle0.push_back(tv10);
  476.   triangles.push_back(triangle0);
  477.   //make triangle 7 and add it
  478.   triangle0.clear();
  479.   triangle0.push_back(tv1);
  480.   triangle0.push_back(tv9);
  481.   triangle0.push_back(tv4);
  482.   triangles.push_back(triangle0);
  483.   //make triangle 8 and add it
  484.   triangle0.clear();
  485.   triangle0.push_back(tv10);
  486.   triangle0.push_back(tv4);
  487.   triangle0.push_back(tv9);
  488.   triangles.push_back(triangle0);
  489.   //make triangle 9 and add it
  490.   triangle0.clear();
  491.   triangle0.push_back(tv2);
  492.   triangle0.push_back(tv6);
  493.   triangle0.push_back(tv7);
  494.   triangles.push_back(triangle0);
  495.   //make triangle 10 and add it
  496.   triangle0.clear();
  497.   triangle0.push_back(tv11);
  498.   triangle0.push_back(tv7);
  499.   triangle0.push_back(tv6);
  500.   triangles.push_back(triangle0);
  501.   //make triangle 11 and add it
  502.   triangle0.clear();
  503.   triangle0.push_back(tv3);
  504.   triangle0.push_back(tv1);
  505.   triangle0.push_back(tv4);
  506.   triangles.push_back(triangle0);
  507.   //make triangle 12 and add it
  508.   triangle0.clear();
  509.   triangle0.push_back(tv3);
  510.   triangle0.push_back(tv6);
  511.   triangle0.push_back(tv2);
  512.   triangles.push_back(triangle0);
  513.   //make triangle 13 and add it
  514.   triangle0.clear();
  515.   triangle0.push_back(tv0);
  516.   triangle0.push_back(tv9);
  517.   triangle0.push_back(tv1);
  518.   triangles.push_back(triangle0);
  519.   //make triangle 14 and add it
  520.   triangle0.clear();
  521.   triangle0.push_back(tv0);
  522.   triangle0.push_back(tv2);
  523.   triangle0.push_back(tv7);
  524.   triangles.push_back(triangle0);
  525.   //make triangle 15 and add it
  526.   triangle0.clear();
  527.   triangle0.push_back(tv8);
  528.   triangle0.push_back(tv10);
  529.   triangle0.push_back(tv9);
  530.   triangles.push_back(triangle0);
  531.   //make triangle 16 and add it
  532.   triangle0.clear();
  533.   triangle0.push_back(tv8);
  534.   triangle0.push_back(tv7);
  535.   triangle0.push_back(tv11);
  536.   triangles.push_back(triangle0);
  537.   //make triangle 17 and add it
  538.   triangle0.clear();
  539.   triangle0.push_back(tv5);
  540.   triangle0.push_back(tv4);
  541.   triangle0.push_back(tv10);
  542.   triangles.push_back(triangle0);
  543.   //make triangle 18 and add it
  544.   triangle0.clear();
  545.   triangle0.push_back(tv5);
  546.   triangle0.push_back(tv11);
  547.   triangle0.push_back(tv6);
  548.   triangles.push_back(triangle0);
  549.  
  550.   //so we have all of the vertices in there original coordinates
  551.   //with that, we subdivide each polygon into 4 new ones and
  552.   //do this n number of times
  553.   for( int i = 1; i < n; i++ ) {
  554.     vector< vector<Vector3> > tempTriangles;
  555.     for( int j = 0; j < triangles.size(); j++ ) {
  556.       //now we subdivide
  557.       vector<Vector3> triangle = triangles.at(j);
  558.       Vector3 two = triangle.at(2); //point 2 of the triangle
  559.       Vector3 one = triangle.at(1); //point 1 of the triangle
  560.       Vector3 zero = triangle.at(0); //point 0 of the triangle
  561.       Vector3 a( ((two+zero)*.5) ); //intermediate point a
  562.       Vector3 b( ((one+zero)*.5) ); //intermediate point b
  563.       Vector3 c( ((two+one)*.5) ); //intermediate point c
  564.  
  565.       //add first new triangle
  566.       triangle.clear();
  567.       triangle.push_back( two );
  568.       triangle.push_back( c );
  569.       triangle.push_back( a );
  570.       tempTriangles.push_back( triangle );
  571.  
  572.       //add second new triangle
  573.       triangle.clear();
  574.       triangle.push_back( c );
  575.       triangle.push_back( one );
  576.       triangle.push_back( b );
  577.       tempTriangles.push_back( triangle );
  578.  
  579.       //add third new triangle
  580.       triangle.clear();
  581.       triangle.push_back( c );
  582.       triangle.push_back( b );
  583.       triangle.push_back( a );
  584.       tempTriangles.push_back( triangle );
  585.  
  586.       //add fourth new triangle
  587.       triangle.clear();
  588.       triangle.push_back( a );
  589.       triangle.push_back( b );
  590.       triangle.push_back( zero );
  591.       tempTriangles.push_back( triangle );
  592.  
  593.     }
  594.     triangles = tempTriangles;
  595.   }
  596.  
  597.   //loop to normalize each triangle vertex
  598.   vector< vector<Vector3> > tempTriangles;
  599.   for( int i = 0; i < triangles.size(); i++ ) {
  600.     vector<Vector3> triangle = triangles.at(i);
  601.     Vector3 two = triangle.at(0);
  602.     Vector3 one = triangle.at(1);
  603.     Vector3 zero = triangle.at(2);
  604.  
  605.     //now normalize
  606.     two.normalize();
  607.     one.normalize();
  608.     zero.normalize();
  609.     two*=.5;
  610.     one*=.5;
  611.     zero*=.5;
  612.     Vector3 twoN = two;
  613.     Vector3 oneN = one;
  614.     Vector3 zeroN = zero;
  615.  
  616.     triangle.clear();
  617.     triangle.push_back(twoN);
  618.     triangle.push_back(oneN);
  619.     triangle.push_back(zeroN);
  620.     tempTriangles.push_back(triangle);
  621.  
  622.   }
  623.   triangles = tempTriangles;
  624.  
  625.   /*//so we have now created all the proper triangles
  626.   //now we must normalize, which requires all of the vertices
  627.   int totalV = verts.size();
  628.   for( int i = 0; i < totalV; i++ ) {
  629.     //compute the vector that goes from the origin to the vertex and normalize it
  630.     Vector3 temp = verts.at(i);
  631.     temp.normalize();
  632.     //multiply that normalized vector by .5 and add it to the origin to get the replacement vertex
  633.     temp*=.5;
  634.     Point3 np = origin+temp;
  635.     normVerts.push_back(np);
  636.     }*/
  637.  
  638.   //now iterate through, creating triangles in order
  639.   for( int i = 0; i < triangles.size(); i++ ) {
  640.     Point3 origin(0,0,0);
  641.     vector<Vector3> triangle = triangles.at(i);
  642.     Point3 a,b,c;
  643.     a = origin+triangle.at(0);
  644.     b = origin+triangle.at(1);
  645.     c = origin+triangle.at(2);
  646.     addTriangle(a,b,c);
  647.   }
  648.  
  649. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post