1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
/* data.cpp
* The data structure which will represents the points of our surface.
*/
#include <GL/gl.h>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
// This class represents a vertex in 3-space
class Vertex
{
private:// The coordinates of a vertex in 3-space
float x, y, z;
public:
// Default constructor
Vertex(void):x(0.0), y(0.0), z(0.0) {}
// Parameterized constructor
Vertex(float x_, float y_, float z_)
{
x = x_;
y = y_;
z = z_;
}
// Destructor
~Vertex() { /* nothing on the heap to free */ }
// Copy constructor
Vertex(const Vertex& v)
{
x = v.x;
y = v.y;
z = v.z;
}
// Assignment operator
void operator=(const Vertex& v)
{
x = v.x;
y = v.y;
z = v.z;
}
// Print
void print()
{
printf("<%1.1f,%1.1f,%1.1f>", x, y, z);
}
// Tell openGL to render this vertex
// Call only between glBegin(*) and glEnd()!
void draw()
{
glVertex3f(x, y, z);
}
//Vertex-vertex addition
Vertex operator+(Vertex v)
{
return Vertex(x+v.x, y+v.y, z+v.z);
}
//Vertex-scalar multiplication
Vertex operator*(float f)
{
return Vertex(f*x, f*y, f*z);
}
};
// This class represents a row-major 2D "grid" of vertices
class Matrix
{
private:// Dimensons of the matrix
int rows, cols;
// STL vector of vectors of Verticies
vector< vector<Vertex> > data;
public: // Constructor
Matrix(int rows_, int cols_)
{
rows = rows_;
cols = cols_;
for(int r=0; r<rows; r++)
{
data.push_back(vector<Vertex>());
for(int c=0; c<cols; c++)
{
data.back().push_back(Vertex());
}
}
}
// Destructor
~Matrix() //this won't work for some reason =(
{
//vector<Vertex>::iterator i;
//for(i = data.begin(); i != data.end(); i++) i.clear();
}
// Copy constructor
Matrix(const Matrix& old)
{
rows = old.rows;
cols = old.cols;
for(int r=0; r<rows; r++)
{
data.push_back(vector<Vertex>());
for(int c=0; c<cols; c++)
{
Vertex temp = old.data[r][c]; //segfault!
data.back().push_back(temp);
}
}
}
// Print
void print()
{
// Should use iterators instead but I can't get them to work =(
for(int r=0; r<rows; r++)
{
for(int c=0; r<cols; c++)
{
data[r][c].print();
}
printf("\n");
}
}
// Access cols and rows
int numCols() { return cols; }
int numRows() { return rows; }
// v returns an element of the matrix by reference
Vertex& v(int r, int c)
{
if( 0 > r || r > rows || 0 > c || c > cols)
{
printf("Matrix boundary error!\nr:%i rows:%i, c:%i, cols:%i\n", r, rows, c, cols);
exit(-1);
}
return data[r][c];
}
};
| |