Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef _rt_H
- #define _rt_H
- #include <cmath>
- #include <vector>
- #include <GL/glut.h>
- using namespace std;
- //
- // Sample code for physics simulation
- //
- // Implements cloth simulation
- class Vector3f;
- class Triangle;
- class TriangleMesh;
- class Vector3f {
- double _item[3];
- public:
- double & operator [](int i) {
- return _item[i];
- }
- Vector3f(float x, float y, float z) {
- _item[0] = x;
- _item[1] = y;
- _item[2] = z;
- }
- ;
- Vector3f() {
- }
- ;
- bool operator ==(Vector3f & obj) {
- return _item[0] == obj[0] && _item[1] == obj[1] && _item[2] == obj[2];
- }
- ;
- Vector3f & operator =(Vector3f & obj) {
- _item[0] = obj[0];
- _item[1] = obj[1];
- _item[2] = obj[2];
- return *this;
- }
- ;
- Vector3f & operator +=(Vector3f & obj) {
- _item[0] += obj[0];
- _item[1] += obj[1];
- _item[2] += obj[2];
- return *this;
- }
- ;
- Vector3f & operator -=(Vector3f & obj) {
- _item[0] -= obj[0];
- _item[1] -= obj[1];
- _item[2] -= obj[2];
- return *this;
- }
- ;
- Vector3f & operator *=(float zoom) {
- _item[0] *= zoom;
- _item[1] *= zoom;
- _item[2] *= zoom;
- return *this;
- }
- ;
- //use it only to normalize norm vector
- Vector3f & operator /=(double len) {
- _item[0] /= len;
- _item[1] /= len;
- _item[2] /= len;
- return *this;
- }
- ;
- Vector3f operator -(Vector3f b) {
- return Vector3f(_item[0] - b[0], _item[1] - b[1], _item[2] - b[2]);
- };
- Vector3f operator +(Vector3f b) {
- return Vector3f(_item[0] + b[0], _item[1] + b[1], _item[2] + b[2]);
- };
- Vector3f operator *(int a) {
- return Vector3f(_item[0] * a, _item[1] * a, _item[2] * a);
- };
- void rotX(float alpha) {
- float oldY = _item[1];
- float oldZ = _item[2];
- _item[1] = oldY * cos(alpha) - oldZ * sin(alpha);
- _item[2] = oldY * sin(alpha) + oldZ * cos(alpha);
- }
- void rotY(float alpha) {
- float oldX = _item[0];
- float oldZ = _item[2];
- _item[0] = oldX * cos(alpha) + oldZ * sin(alpha);
- _item[2] = -oldX * sin(alpha) + oldZ * cos(alpha);
- }
- void rotZ(float alpha) {
- float oldX = _item[0];
- float oldY = _item[1];
- _item[0] = oldX * cos(alpha) - oldY * sin(alpha);
- _item[1] = oldX * sin(alpha) + oldY * cos(alpha);
- }
- double len() {
- return sqrt(pow(_item[0],2) + pow(_item[1],2) + pow(_item[2],2));
- }
- };
- ostream & operator <<(ostream & stream, Vector3f & obj) {
- stream << obj[0] << ' ' << obj[1] << ' ' << obj[2] << ' ';
- }
- ;
- class Triangle {
- friend class TriangleMesh;
- int _vertex[3];
- Vector3f normal;
- Vector3f normal1, normal2, normal3;
- public:
- Triangle(int v1, int v2, int v3) {
- _vertex[0] = v1;
- _vertex[1] = v2;
- _vertex[2] = v3;
- }
- ;
- void setNormal(Vector3f v1, Vector3f v2, Vector3f v3){
- Vector3f a = v2-v1, b = v3-v1;
- Vector3f n = Vector3f(a[1]*b[2] - a[2]*b[1], a[2]*b[0] - a[0]*b[2], a[0]*b[1] - a[1]*b[0]);
- n /= n.len();
- normal = n;
- }
- void setAVnormal(Vector3f av1, Vector3f av2, Vector3f av3){
- normal1 = av1;
- normal2 = av2;
- normal3 = av3;
- }
- Vector3f getNormal(){
- return normal;
- }
- Vector3f getAVnormal(int i){
- if(i == 1)
- return normal1;
- else if(i == 2)
- return normal2;
- else
- return normal3;
- }
- };
- float fmax(float f1, float f2, float f3) {
- float f = f1;
- if (f < f2)
- f = f2;
- if (f < f3)
- f = f3;
- return f;
- }
- ;
- float fmin(float f1, float f2, float f3) {
- float f = f1;
- if (f > f2)
- f = f2;
- if (f > f3)
- f = f3;
- return f;
- }
- ;
- class TriangleMesh {
- vector<Vector3f> _v;
- vector<Triangle> _trig;
- float _xmax, _xmin, _ymax, _ymin, _zmin, _zmax;
- public:
- TriangleMesh(char * filename) {
- loadFile(filename);
- }
- ;
- TriangleMesh() {
- }
- ;
- void loadFile(char * filename);
- int trigNum() {
- return _trig.size();
- }
- ;
- int vNum() {
- return _v.size();
- }
- ;
- Vector3f v(int i) {
- return _v[i];
- }
- ;
- void getTriangleVertices(int i, Vector3f & v1, Vector3f & v2, Vector3f & v3) {
- v1 = _v[_trig[i]._vertex[0]];
- v2 = _v[_trig[i]._vertex[1]];
- v3 = _v[_trig[i]._vertex[2]];
- }
- //this might be done only once
- void setAVnorm(int j, Vector3f & v1, Vector3f & v2, Vector3f & v3){
- Vector3f norm1 = Vector3f(0,0,0);
- Vector3f norm2 = Vector3f(0,0,0);
- Vector3f norm3 = Vector3f(0,0,0);
- int count1 = 0;
- int count2 = 0;
- int count3 = 0;
- for(int i = 0; i < _trig.size(); i++){
- Vector3f v1t = _v[_trig[i]._vertex[0]];
- Vector3f v2t = _v[_trig[i]._vertex[1]];
- Vector3f v3t = _v[_trig[i]._vertex[2]];
- if(v1 == v1t || v1 == v2t || v1 == v3t){
- Vector3f normal = _trig[i].normal;
- norm1 += normal;
- count1++;
- }
- if(v2 == v1t || v2 == v2t || v2 == v3t){
- Vector3f normal = _trig[i].normal;
- norm2 += normal;
- count2++;
- }
- if(v3 == v1t || v3 == v2t || v3 == v3t){
- Vector3f normal = _trig[i].normal;
- norm3 += normal;
- count3++;
- }
- }
- norm1 /= norm1.len();
- norm2 /= norm2.len();
- norm3 /= norm3.len();
- _trig[j].setAVnormal(norm1, norm2, norm3);
- }
- void getTriangleNormal(int i, Vector3f & norm){
- norm = (_trig[i].normal);
- }
- void getTriangleAVnorms(int i, Vector3f & n1, Vector3f & n2, Vector3f & n3){
- n1 = (_trig[i].normal1);
- n2 = (_trig[i].normal2);
- n3 = (_trig[i].normal3);
- }
- };
- #endif //_rt_H
Add Comment
Please, Sign In to add comment