OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * ***** BEGIN GPL LICENSE BLOCK ***** |
| 3 * |
| 4 * This program is free software; you can redistribute it and/or |
| 5 * modify it under the terms of the GNU General Public License |
| 6 * as published by the Free Software Foundation; either version 2 |
| 7 * of the License, or (at your option) any later version. |
| 8 * |
| 9 * This program is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with this program; if not, write to the Free Software Foundation, |
| 16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 * |
| 18 * The Original Code is Copyright (C) 2010 Blender Foundation. |
| 19 * All rights reserved. |
| 20 * |
| 21 * The Original Code is: all of this file. |
| 22 * |
| 23 * Contributor(s): none yet. |
| 24 * |
| 25 * ***** END GPL LICENSE BLOCK ***** |
| 26 */ |
| 27 |
| 28 #ifndef __POLYGON_H__ |
| 29 #define __POLYGON_H__ |
| 30 |
| 31 /** \file blender/freestyle/intern/geometry/Polygon.h |
| 32 * \ingroup freestyle |
| 33 * \brief Class to define a polygon |
| 34 * \author Stephane Grabli |
| 35 * \date 30/07/2002 |
| 36 */ |
| 37 |
| 38 #include <vector> |
| 39 |
| 40 #include "Geom.h" |
| 41 #include "GeomUtils.h" |
| 42 |
| 43 using namespace std; |
| 44 |
| 45 namespace Geometry { |
| 46 |
| 47 template <class Point> |
| 48 class Polygon |
| 49 { |
| 50 public: |
| 51 inline Polygon() |
| 52 { |
| 53 _id = 0; |
| 54 userdata = 0; |
| 55 userdata2 = 0; |
| 56 } |
| 57 |
| 58 inline Polygon(const vector<Point>& vertices) |
| 59 { |
| 60 _vertices = vertices; |
| 61 computeBBox(); |
| 62 _id = 0; |
| 63 userdata = 0; |
| 64 userdata2 = 0; |
| 65 } |
| 66 |
| 67 inline Polygon(const Polygon<Point>& poly) |
| 68 { |
| 69 Point p; |
| 70 for (typename vector<Point>::const_iterator it = poly.getVertice
s().begin(); |
| 71 it != poly.getVertices().end(); |
| 72 it++) |
| 73 { |
| 74 p = *it; |
| 75 _vertices.push_back(p); |
| 76 } |
| 77 |
| 78 _id = poly.getId(); |
| 79 poly.getBBox(_min, _max); |
| 80 userdata = 0; |
| 81 userdata2 = 0; |
| 82 } |
| 83 |
| 84 virtual ~Polygon() {} |
| 85 |
| 86 // |
| 87 // Accessors |
| 88 // |
| 89 ////////////////////////////////////////////////////////////////////////
///// |
| 90 inline const vector<Point>& getVertices() const |
| 91 { |
| 92 return _vertices; |
| 93 } |
| 94 |
| 95 inline void getBBox(Point& min, Point& max) const |
| 96 { |
| 97 min = _min; |
| 98 max = _max; |
| 99 } |
| 100 |
| 101 inline Point& getBBoxCenter() |
| 102 { |
| 103 Point result; |
| 104 result = (_min + _max) / 2; |
| 105 return result; |
| 106 } |
| 107 |
| 108 inline Point& getCenter() |
| 109 { |
| 110 Point result; |
| 111 for (typename vector<Point>::iterator it = _vertices.begin(); it
!= _vertices.end(); it++) |
| 112 result += *it; |
| 113 result /= _vertices.size(); |
| 114 return result; |
| 115 } |
| 116 |
| 117 inline unsigned getId() const |
| 118 { |
| 119 return _id; |
| 120 } |
| 121 |
| 122 // |
| 123 // Modifiers |
| 124 // |
| 125 ////////////////////////////////////////////////////////////////////////
///// |
| 126 inline void setVertices(const vector<Point>& vertices) |
| 127 { |
| 128 _vertices.clear(); |
| 129 Point p; |
| 130 for (typename vector<Point>::const_iterator it = vertices.begin(
); it != vertices.end(); it++) { |
| 131 p = *it; |
| 132 _vertices.push_back(p); |
| 133 } |
| 134 computeBBox(); |
| 135 } |
| 136 |
| 137 inline void setId(unsigned id) |
| 138 { |
| 139 _id = id; |
| 140 } |
| 141 |
| 142 // |
| 143 // Other methods |
| 144 // |
| 145 ////////////////////////////////////////////////////////////////////////
///// |
| 146 inline void computeBBox() |
| 147 { |
| 148 if (_vertices.empty()) |
| 149 return; |
| 150 |
| 151 _max = _vertices[0]; |
| 152 _min = _vertices[0]; |
| 153 |
| 154 for (typename vector<Point>::iterator it = _vertices.begin(); it
!= _vertices.end(); it++) { |
| 155 for (unsigned int i = 0; i < Point::dim(); i++) { |
| 156 if ((*it)[i] > _max[i]) |
| 157 _max[i] = (*it)[i]; |
| 158 if ((*it)[i] < _min[i]) |
| 159 _min[i] = (*it)[i]; |
| 160 } |
| 161 } |
| 162 } |
| 163 |
| 164 // FIXME Is it possible to get rid of userdatas ? |
| 165 void* userdata; |
| 166 void* userdata2; // Used during ray casting |
| 167 |
| 168 protected: |
| 169 vector<Point> _vertices; |
| 170 Point _min; |
| 171 Point _max; |
| 172 unsigned _id; |
| 173 }; |
| 174 |
| 175 |
| 176 // |
| 177 // Polygon3r class |
| 178 // |
| 179 /////////////////////////////////////////////////////////////////////////////// |
| 180 class Polygon3r : public Polygon<Vec3r> |
| 181 { |
| 182 public: |
| 183 inline Polygon3r() : Polygon<Vec3r>() {} |
| 184 |
| 185 inline Polygon3r(const vector<Vec3r>& vertices, const Vec3r& normal) : P
olygon<Vec3r>(vertices) |
| 186 { |
| 187 setNormal(normal); |
| 188 } |
| 189 |
| 190 inline Polygon3r(const Polygon3r& poly) : Polygon<Vec3r>(poly), _normal(
poly._normal) {} |
| 191 |
| 192 virtual ~Polygon3r() {} |
| 193 |
| 194 void setNormal(const Vec3r& normal) |
| 195 { |
| 196 _normal = normal; |
| 197 } |
| 198 |
| 199 inline Vec3r getNormal() const |
| 200 { |
| 201 return _normal; |
| 202 } |
| 203 |
| 204 /*! Check whether the Polygon intersects with the ray or not */ |
| 205 inline bool rayIntersect(const Vec3r& orig, const Vec3r& dir, real& t, r
eal& u, real& v, |
| 206 real epsilon = M_EPSILON) const |
| 207 { |
| 208 #if 0 |
| 209 if (_vertices.size() < 3) |
| 210 return false; |
| 211 #endif |
| 212 return GeomUtils::intersectRayTriangle(orig, dir, _vertices[0],
_vertices[1], _vertices[2], t, u, v, epsilon); |
| 213 } |
| 214 |
| 215 private: |
| 216 Vec3r _normal; |
| 217 }; |
| 218 |
| 219 } // end of namespace Geometry |
| 220 |
| 221 #endif // __POLYGON_H__ |
OLD | NEW |