Created
December 22, 2010 21:06
-
-
Save vanne02135/752096 to your computer and use it in GitHub Desktop.
Will contain visibility functionality for triangle soup some day
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* File: main.cpp | |
* Author: vanne | |
* | |
* Created on December 22, 2010, 10:00 PM | |
*/ | |
#include <cstdlib> | |
#include <iostream> | |
#include <vector> | |
#include <fstream> | |
#include <stdexcept> | |
#include <algorithm> | |
using namespace std; | |
template <typename T> | |
class Point3 { | |
public: | |
T x, y, z; | |
Point3() : x(0), y(0), z(0) {}; | |
~Point3() {}; | |
Point3(T x_, T y_, T z_) : x(x_), y(y_), z(z_) {}; | |
T& operator[](unsigned int i) { | |
if (i == 0) return this->x; | |
if (i == 1) return this->y; | |
if (i == 2) return this->z; | |
} | |
Point3(T *array) { | |
x = array[0]; | |
y = array[1]; | |
z = array[2]; | |
} | |
T distance(const Point3& other) { | |
return (x-other.x)*(x-other.x) + (y-other.y)*(y-other.y) + (z-other.z)*(z-other.z); | |
} | |
}; | |
template <typename T> | |
ostream &operator<< (ostream &outs, Point3<T>& v) { | |
outs << "(" << v.x << ", " << v.y << ", " << v.z << ")"; | |
return outs; | |
} | |
class Triangle { | |
public: | |
int v1, v2, v3; | |
Triangle() { | |
v1 = v2 = v3 = 0; | |
} | |
Triangle(int v1_, int v2_, int v3_) : v1(v1_), v2(v2_), v3(v3_) {}; | |
}; | |
ostream &operator << (ostream & outs, Triangle &t) { | |
outs << "[" << t.v1 << ", " << t.v2 << ", " << t.v3 << "]"; | |
return outs; | |
} | |
class TriangleSoup { | |
public: | |
vector<Point3<double> > vertices; | |
vector<Triangle> triangles; | |
TriangleSoup() {}; | |
~TriangleSoup() {}; | |
void load(const string& filename) { | |
cout << "Loading from " << filename << endl; | |
ifstream in(filename.c_str()); | |
char charbuf[1024]; | |
in.getline(charbuf, 1024); | |
string buf(charbuf); | |
if (buf.find(string("[nodes]")) == string::npos) | |
throw runtime_error("Not a known triangle soup format"); | |
int nNodes = 0; | |
in >> nNodes; | |
cout << "# of nodes: " << nNodes << endl; | |
vertices.resize(nNodes); | |
int n, vi; | |
double x, y, z; | |
try { | |
for (vi = 0; vi < nNodes; vi++) { | |
in >> n >> x >> y >> z; | |
vertices.at(vi) = Point3<double>(x, y, z); | |
} | |
} catch (...) { | |
cerr << "Error while reading vertices at vi = " << vi << endl; | |
} | |
in.getline(charbuf, 1024); // skip \n | |
in.getline(charbuf, 1024); | |
buf.assign(charbuf, 1024); | |
if (buf.find(string("[surface elements]")) == string::npos) { | |
cerr << "ifstream at " << in.tellg() << endl; | |
cerr << "Buf: " << buf << endl; | |
throw runtime_error("Could not find surface elements"); | |
} | |
int nTriangles = 0, dummy = 0; | |
in >> nTriangles >> dummy; | |
cout << "# of triangles: " << nTriangles << endl; | |
triangles.resize(nTriangles); | |
int si, v1, v2, v3; | |
try { | |
for (si = 0; si < nTriangles; si++) { | |
in >> n >> v1 >> v2 >> v3; | |
triangles.at(si) = Triangle(v1-1, v2-1, v3-1); | |
} | |
} catch (...) { | |
cerr << "Error while reading triangles at si = " << vi << endl; | |
} | |
} | |
}; | |
class Visibility { | |
public: | |
Point3<double> focus; | |
const TriangleSoup *triSoup; | |
vector<int> zbuffer; | |
Visibility(const Point3<double> &focus, const TriangleSoup &ts) { | |
cout << "Initializing visibility ... "; | |
this->focus = focus; | |
this->triSoup = &ts; | |
updateZbuffer(); | |
cout << " ... done" << endl; | |
} | |
void updateZbuffer() { | |
vector<double> distances(triSoup->triangles.size()); | |
for (int i = 0 ; i < triSoup->triangles.size(); i++) { | |
const Point3<double> &triangleLocation = triSoup->vertices.at(triSoup->triangles.at(i).v1); | |
distances.at(i) = focus.distance(triangleLocation); | |
} | |
// @TODO how to sort a copy of triangle sequence accordingly? | |
sort(distances.begin(), distances.end()); | |
cerr << "Zbuffer actually not implemented yet" << endl; | |
} | |
void backFaceCulling() { | |
// @TODO build a PVS using backface culling | |
} | |
}; | |
/* | |
* | |
*/ | |
int main(int argc, char** argv) { | |
TriangleSoup ts; | |
ts.load(string(argv[1])); | |
cout << ts.vertices.at(1) << endl << ts.vertices.at(2) << endl; | |
cout << ts.triangles.at(0) << endl; | |
Visibility myvis (Point3<double>(0, 0, 1000.0), ts); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment