Guest User

Untitled

a guest
Apr 20th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.23 KB | None | 0 0
  1. #ifndef _rt_H
  2. #define _rt_H
  3.  
  4. #include <cmath>
  5. #include <vector>
  6. #include <GL/glut.h>
  7.  
  8. using namespace std;
  9.  
  10. //
  11. // Sample code for physics simulation
  12. //
  13.  
  14.  
  15. // Implements cloth simulation
  16.  
  17.  
  18. class Vector3f;
  19. class Triangle;
  20. class TriangleMesh;
  21.  
  22. class Vector3f {
  23.  
  24.     double _item[3];
  25.  
  26. public:
  27.  
  28.     double & operator [](int i) {
  29.         return _item[i];
  30.     }
  31.  
  32.     Vector3f(float x, float y, float z) {
  33.         _item[0] = x;
  34.         _item[1] = y;
  35.         _item[2] = z;
  36.     }
  37.     ;
  38.  
  39.     Vector3f() {
  40.     }
  41.     ;
  42.  
  43.     bool operator ==(Vector3f & obj) {
  44.             return _item[0] == obj[0] && _item[1] == obj[1] && _item[2] == obj[2];
  45.     }
  46.     ;
  47.  
  48.     Vector3f & operator =(Vector3f & obj) {
  49.             _item[0] = obj[0];
  50.             _item[1] = obj[1];
  51.             _item[2] = obj[2];
  52.  
  53.             return *this;
  54.     }
  55.     ;
  56.  
  57.     Vector3f & operator +=(Vector3f & obj) {
  58.         _item[0] += obj[0];
  59.         _item[1] += obj[1];
  60.         _item[2] += obj[2];
  61.  
  62.         return *this;
  63.     }
  64.     ;
  65.     Vector3f & operator -=(Vector3f & obj) {
  66.         _item[0] -= obj[0];
  67.         _item[1] -= obj[1];
  68.         _item[2] -= obj[2];
  69.  
  70.         return *this;
  71.     }
  72.     ;
  73.     Vector3f & operator *=(float zoom) {
  74.         _item[0] *= zoom;
  75.         _item[1] *= zoom;
  76.         _item[2] *= zoom;
  77.  
  78.         return *this;
  79.     }
  80.     ;
  81.     //use it only to normalize norm vector
  82.     Vector3f & operator /=(double len) {
  83.         _item[0] /= len;
  84.         _item[1] /= len;
  85.         _item[2] /= len;
  86.  
  87.         return *this;
  88.     }
  89.     ;
  90.     Vector3f operator -(Vector3f b) {
  91.         return Vector3f(_item[0] - b[0], _item[1] - b[1], _item[2] - b[2]);
  92.     };
  93.     Vector3f operator +(Vector3f b) {
  94.         return Vector3f(_item[0] + b[0], _item[1] + b[1], _item[2] + b[2]);
  95.     };
  96.  
  97.     Vector3f operator *(int a) {
  98.         return Vector3f(_item[0] * a, _item[1] * a, _item[2] * a);
  99.     };
  100.  
  101.  
  102.     void rotX(float alpha) {
  103.         float oldY = _item[1];
  104.         float oldZ = _item[2];
  105.         _item[1] = oldY * cos(alpha) - oldZ * sin(alpha);
  106.         _item[2] = oldY * sin(alpha) + oldZ * cos(alpha);
  107.  
  108.     }
  109.     void rotY(float alpha) {
  110.         float oldX = _item[0];
  111.         float oldZ = _item[2];
  112.         _item[0] = oldX * cos(alpha) + oldZ * sin(alpha);
  113.         _item[2] = -oldX * sin(alpha) + oldZ * cos(alpha);
  114.  
  115.     }
  116.     void rotZ(float alpha) {
  117.         float oldX = _item[0];
  118.         float oldY = _item[1];
  119.         _item[0] = oldX * cos(alpha) - oldY * sin(alpha);
  120.         _item[1] = oldX * sin(alpha) + oldY * cos(alpha);
  121.  
  122.     }
  123.  
  124.     double len() {
  125.         return sqrt(pow(_item[0],2) + pow(_item[1],2) + pow(_item[2],2));
  126.     }
  127.  
  128. };
  129.  
  130. ostream & operator <<(ostream & stream, Vector3f & obj) {
  131.     stream << obj[0] << ' ' << obj[1] << ' ' << obj[2] << ' ';
  132. }
  133. ;
  134.  
  135. class Triangle {
  136.     friend class TriangleMesh;
  137.  
  138.     int _vertex[3];
  139.     Vector3f normal;
  140.     Vector3f normal1, normal2, normal3;
  141. public:
  142.  
  143.     Triangle(int v1, int v2, int v3) {
  144.         _vertex[0] = v1;
  145.         _vertex[1] = v2;
  146.         _vertex[2] = v3;
  147.     }
  148.     ;
  149.     void setNormal(Vector3f v1, Vector3f v2, Vector3f v3){
  150.         Vector3f a = v2-v1, b = v3-v1;
  151.  
  152.         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]);
  153.         n /= n.len();
  154.         normal = n;
  155.     }
  156.     void setAVnormal(Vector3f av1, Vector3f av2, Vector3f av3){
  157.         normal1 = av1;
  158.         normal2 = av2;
  159.         normal3 = av3;
  160.     }
  161.     Vector3f getNormal(){
  162.             return normal;
  163.     }
  164.     Vector3f getAVnormal(int i){
  165.         if(i == 1)
  166.             return normal1;
  167.         else if(i == 2)
  168.             return normal2;
  169.         else
  170.             return normal3;
  171.  
  172.     }
  173.  
  174. };
  175.  
  176.  
  177. float fmax(float f1, float f2, float f3) {
  178.     float f = f1;
  179.  
  180.     if (f < f2)
  181.         f = f2;
  182.     if (f < f3)
  183.         f = f3;
  184.  
  185.     return f;
  186. }
  187. ;
  188.  
  189. float fmin(float f1, float f2, float f3) {
  190.     float f = f1;
  191.  
  192.     if (f > f2)
  193.         f = f2;
  194.     if (f > f3)
  195.         f = f3;
  196.  
  197.     return f;
  198. }
  199. ;
  200.  
  201. class TriangleMesh {
  202.     vector<Vector3f> _v;
  203.     vector<Triangle> _trig;
  204.     float _xmax, _xmin, _ymax, _ymin, _zmin, _zmax;
  205.  
  206. public:
  207.     TriangleMesh(char * filename) {
  208.         loadFile(filename);
  209.     }
  210.     ;
  211.     TriangleMesh() {
  212.     }
  213.     ;
  214.     void loadFile(char * filename);
  215.  
  216.     int trigNum() {
  217.         return _trig.size();
  218.     }
  219.     ;
  220.     int vNum() {
  221.         return _v.size();
  222.     }
  223.     ;
  224.     Vector3f v(int i) {
  225.         return _v[i];
  226.     }
  227.     ;
  228.  
  229.     void getTriangleVertices(int i, Vector3f & v1, Vector3f & v2, Vector3f & v3) {
  230.         v1 = _v[_trig[i]._vertex[0]];
  231.         v2 = _v[_trig[i]._vertex[1]];
  232.         v3 = _v[_trig[i]._vertex[2]];
  233.  
  234.     }
  235.     //this might be done only once
  236.     void setAVnorm(int j, Vector3f & v1, Vector3f & v2, Vector3f & v3){
  237.         Vector3f norm1 = Vector3f(0,0,0);
  238.         Vector3f norm2 = Vector3f(0,0,0);
  239.         Vector3f norm3 = Vector3f(0,0,0);
  240.         int count1 = 0;
  241.         int count2 = 0;
  242.         int count3 = 0;
  243.         for(int i = 0; i < _trig.size(); i++){
  244.             Vector3f v1t = _v[_trig[i]._vertex[0]];
  245.             Vector3f v2t = _v[_trig[i]._vertex[1]];
  246.             Vector3f v3t = _v[_trig[i]._vertex[2]];
  247.             if(v1 == v1t || v1 == v2t || v1 == v3t){
  248.                 Vector3f normal = _trig[i].normal;
  249.                 norm1 += normal;
  250.                 count1++;
  251.             }
  252.             if(v2 == v1t || v2 == v2t || v2 == v3t){
  253.                             Vector3f normal = _trig[i].normal;
  254.                             norm2 += normal;
  255.                             count2++;
  256.             }
  257.             if(v3 == v1t || v3 == v2t || v3 == v3t){
  258.                             Vector3f normal = _trig[i].normal;
  259.                             norm3 += normal;
  260.                             count3++;
  261.             }
  262.         }
  263.         norm1 /= norm1.len();
  264.         norm2 /= norm2.len();
  265.         norm3 /= norm3.len();
  266.  
  267.         _trig[j].setAVnormal(norm1, norm2, norm3);
  268.     }
  269.  
  270.     void getTriangleNormal(int i, Vector3f & norm){
  271.         norm = (_trig[i].normal);
  272.     }
  273.     void getTriangleAVnorms(int i, Vector3f & n1, Vector3f & n2, Vector3f & n3){
  274.         n1 = (_trig[i].normal1);
  275.         n2 = (_trig[i].normal2);
  276.         n3 = (_trig[i].normal3);
  277.     }
  278.  
  279. };
  280.  
  281. #endif //_rt_H
Add Comment
Please, Sign In to add comment