summaryrefslogtreecommitdiffstats
path: root/src/MathUtil.h
blob: 9e6efc7a239e3f3051e618d25e5677211e98967c (plain)
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
/*
 * MathUtil.h
 *
 * Copyright (C) 2009 Matthias Schiffer <matthias@gamezock.de>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef ZOOM_MATHUTIL_H_
#define ZOOM_MATHUTIL_H_

#include "Triangle.h"

#include <vmmlib/matrix.hpp>

namespace Zoom {

class MathUtil {
  public:
    static const float EPSILON;

    class Plane {
      public:
        Plane(const vmml::vec3f &n = vmml::vec3f::ZERO, float d0 = 0) : normal(n), d(d0) {}
        Plane(const Triangle &t) : normal(t.computeNormal()), d(t.getVertex(0).dot(normal)) {}

        bool contains(const vmml::vec3f &v) const {
          return (fabsf(normal.dot(v) - d) < EPSILON);
        }

        bool isBehind(const vmml::vec3f &v) const {
          return (normal.dot(v) - d) < 0;
        }

        bool isInFront(const vmml::vec3f &v) const {
          return (normal.dot(v) - d) > 0;
        }


        bool contains(const Triangle &t) const {
          for(int i = 0; i < 3; ++i) {
            if(!contains(t.getVertex(i)))
              return false;
          }

          return true;
        }

        bool isBehind(const Triangle &t) const {
          for(int i = 0; i < 3; ++i) {
            if(!isBehind(t.getVertex(i)) && !contains(t.getVertex(i)))
              return false;
          }

          return true;
        }

        bool isInFront(const Triangle &t) const {
          for(int i = 0; i < 3; ++i) {
            if(!isInFront(t.getVertex(i)) && !contains(t.getVertex(i)))
              return false;
          }

          return true;
        }


        const vmml::vec3f& getNormal() const {
          return normal;
        }

        vmml::vec3f intersection(const vmml::vec3f &p, const vmml::vec3f &dir) const;

      private:
        vmml::vec3f normal;
        float d;
    };

    static vmml::mat4f perspective(float fovy, float aspect, float zNear);

  private:
    MathUtil();
};

}

#endif /* ZOOM_MATHUTIL_H_ */