Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Polygon.h « geometry « intern « freestyle « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d606cd8b282071db46495de8a586659ec1e89bd1 (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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* SPDX-License-Identifier: GPL-2.0-or-later */

#pragma once

/** \file
 * \ingroup freestyle
 * \brief Class to define a polygon
 */

#include <vector>

#include "Geom.h"
#include "GeomUtils.h"

#ifdef WITH_CXX_GUARDEDALLOC
#  include "MEM_guardedalloc.h"
#endif

using namespace std;

namespace Freestyle {

namespace Geometry {

template<class Point> class Polygon {
 public:
  inline Polygon()
  {
    _id = 0;
    userdata = 0;
    userdata2 = 0;
  }

  inline Polygon(const vector<Point> &vertices)
  {
    _vertices = vertices;
    computeBBox();
    _id = 0;
    userdata = 0;
    userdata2 = 0;
  }

  inline Polygon(const Polygon<Point> &poly)
  {
    Point p;
    for (typename vector<Point>::const_iterator it = poly.getVertices().begin();
         it != poly.getVertices().end();
         it++) {
      p = *it;
      _vertices.push_back(p);
    }

    _id = poly.getId();
    poly.getBBox(_min, _max);
    userdata = 0;
    userdata2 = 0;
  }

  virtual ~Polygon()
  {
  }

  //
  // Accessors
  //
  /////////////////////////////////////////////////////////////////////////////
  inline const vector<Point> &getVertices() const
  {
    return _vertices;
  }

  inline void getBBox(Point &min, Point &max) const
  {
    min = _min;
    max = _max;
  }

  inline Point getBBoxCenter()
  {
    Point result;
    result = (_min + _max) / 2;
    return result;
  }

  inline Point getCenter()
  {
    Point result;
    for (typename vector<Point>::iterator it = _vertices.begin(); it != _vertices.end(); it++) {
      result += *it;
    }
    result /= _vertices.size();
    return result;
  }

  inline unsigned getId() const
  {
    return _id;
  }

  //
  // Modifiers
  //
  /////////////////////////////////////////////////////////////////////////////
  inline void setVertices(const vector<Point> &vertices)
  {
    _vertices.clear();
    Point p;
    for (typename vector<Point>::const_iterator it = vertices.begin(); it != vertices.end();
         it++) {
      p = *it;
      _vertices.push_back(p);
    }
    computeBBox();
  }

  inline void setId(unsigned id)
  {
    _id = id;
  }

  //
  // Other methods
  //
  /////////////////////////////////////////////////////////////////////////////
  inline void computeBBox()
  {
    if (_vertices.empty()) {
      return;
    }

    _max = _vertices[0];
    _min = _vertices[0];

    for (typename vector<Point>::iterator it = _vertices.begin(); it != _vertices.end(); it++) {
      for (unsigned int i = 0; i < Point::dim(); i++) {
        if ((*it)[i] > _max[i]) {
          _max[i] = (*it)[i];
        }
        if ((*it)[i] < _min[i]) {
          _min[i] = (*it)[i];
        }
      }
    }
  }

  // FIXME Is it possible to get rid of userdatas ?
  void *userdata;
  void *userdata2;  // Used during ray casting

 protected:
  vector<Point> _vertices;
  Point _min;
  Point _max;
  unsigned _id;

#ifdef WITH_CXX_GUARDEDALLOC
  MEM_CXX_CLASS_ALLOC_FUNCS("Freestyle:Geometry:Polygon")
#endif
};

//
// Polygon3r class
//
///////////////////////////////////////////////////////////////////////////////
class Polygon3r : public Polygon<Vec3r> {
 public:
  inline Polygon3r() : Polygon<Vec3r>()
  {
  }

  inline Polygon3r(const vector<Vec3r> &vertices, const Vec3r &normal) : Polygon<Vec3r>(vertices)
  {
    setNormal(normal);
  }

  inline Polygon3r(const Polygon3r &poly) : Polygon<Vec3r>(poly), _normal(poly._normal)
  {
  }

  virtual ~Polygon3r()
  {
  }

  void setNormal(const Vec3r &normal)
  {
    _normal = normal;
  }

  inline Vec3r getNormal() const
  {
    return _normal;
  }

  /** Check whether the Polygon intersects with the ray or not */
  inline bool rayIntersect(const Vec3r &orig,
                           const Vec3r &dir,
                           real &t,
                           real &u,
                           real &v,
                           real epsilon = M_EPSILON) const
  {
#if 0
    if (_vertices.size() < 3) {
      return false;
    }
#endif
    return GeomUtils::intersectRayTriangle(
        orig, dir, _vertices[0], _vertices[1], _vertices[2], t, u, v, epsilon);
  }

 private:
  Vec3r _normal;
};

}  // end of namespace Geometry

} /* namespace Freestyle */