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 __GEOMUTILS_H__ |
| 29 #define __GEOMUTILS_H__ |
| 30 |
| 31 /** \file blender/freestyle/intern/geometry/GeomUtils.h |
| 32 * \ingroup freestyle |
| 33 * \brief Various tools for geometry |
| 34 * \author Stephane Grabli |
| 35 * \date 12/04/2002 |
| 36 */ |
| 37 |
| 38 #include <vector> |
| 39 |
| 40 #include "Geom.h" |
| 41 |
| 42 #include "../system/FreestyleConfig.h" |
| 43 |
| 44 using namespace std; |
| 45 using namespace Geometry; |
| 46 |
| 47 namespace GeomUtils { |
| 48 |
| 49 // |
| 50 // Templated procedures |
| 51 // |
| 52 ///////////////////////////////////////////////////////////////////////////// |
| 53 |
| 54 /*! Computes the distance from a point P to a segment AB */ |
| 55 template<class T> |
| 56 real distPointSegment( const T& P, const T& A , const T& B) |
| 57 { |
| 58 T AB, AP, BP; |
| 59 AB = B - A; |
| 60 AP = P - A; |
| 61 BP = P - B; |
| 62 |
| 63 real c1(AB * AP); |
| 64 if (c1 <= 0) |
| 65 return AP.norm(); |
| 66 |
| 67 real c2(AB * AB); |
| 68 if (c2 <= c1) |
| 69 return BP.norm(); |
| 70 |
| 71 real b = c1 / c2; |
| 72 T Pb, PPb; |
| 73 Pb = A + b * AB; |
| 74 PPb = P - Pb; |
| 75 |
| 76 return PPb.norm(); |
| 77 } |
| 78 |
| 79 // |
| 80 // Non-templated procedures |
| 81 // |
| 82 ///////////////////////////////////////////////////////////////////////////// |
| 83 typedef enum { |
| 84 DONT_INTERSECT, |
| 85 DO_INTERSECT, |
| 86 COLINEAR, |
| 87 COINCIDENT, |
| 88 } intersection_test; |
| 89 |
| 90 LIB_GEOMETRY_EXPORT |
| 91 intersection_test intersect2dSeg2dSeg(const Vec2r& p1, const Vec2r& p2, // first
segment |
| 92 const Vec2r& p3, const Vec2r& p4, // secon
d segment |
| 93 Vec2r& res); // found
intersection point |
| 94 |
| 95 LIB_GEOMETRY_EXPORT |
| 96 intersection_test intersect2dLine2dLine(const Vec2r& p1, const Vec2r& p2, // fir
st segment |
| 97 const Vec2r& p3, const Vec2r& p4, // sec
ond segment |
| 98 Vec2r& res); // fou
nd intersection point |
| 99 |
| 100 LIB_GEOMETRY_EXPORT |
| 101 intersection_test intersect2dSeg2dSegParametric(const Vec2r& p1, const Vec2r& p2
, // first segment |
| 102 const Vec2r& p3, const Vec2r& p4
, // second segment |
| 103 real& t,
// I = P1 + t * P1P2) |
| 104 real& u,
// I = P3 + u * P3P4 |
| 105 real epsilon = M_EPSILON); |
| 106 |
| 107 /*! check whether a 2D segment intersect a 2D region or not */ |
| 108 LIB_GEOMETRY_EXPORT |
| 109 bool intersect2dSeg2dArea(const Vec2r& min, const Vec2r& max, const Vec2r& A, co
nst Vec2r& B); |
| 110 |
| 111 /*! check whether a 2D segment is included in a 2D region or not */ |
| 112 LIB_GEOMETRY_EXPORT |
| 113 bool include2dSeg2dArea(const Vec2r& min, const Vec2r& max, const Vec2r& A, cons
t Vec2r& B); |
| 114 |
| 115 /*! Box-triangle overlap test, adapted from Tomas Akenine-Möller code */ |
| 116 LIB_GEOMETRY_EXPORT |
| 117 bool overlapTriangleBox(Vec3r& boxcenter, Vec3r& boxhalfsize, Vec3r triverts[3])
; |
| 118 |
| 119 /*! Fast, Minimum Storage Ray-Triangle Intersection, adapted from Tomas Möller a
nd Ben Trumbore code. */ |
| 120 LIB_GEOMETRY_EXPORT |
| 121 bool intersectRayTriangle(const Vec3r& orig, const Vec3r& dir, const Vec3r& v0,
const Vec3r& v1, const Vec3r& v2, |
| 122 real& t, // I = orig + t * dir |
| 123 real& u, real& v, // I = (1 - u - v) *
v0 + u * v1 + v * v2 |
| 124 const real epsilon = M_EPSILON); // the epsilon to use |
| 125 |
| 126 /*! Intersection between plane and ray adapted from Graphics Gems, Didier Badoue
l */ |
| 127 LIB_GEOMETRY_EXPORT |
| 128 intersection_test intersectRayPlane(const Vec3r& orig, const Vec3r& dir, // ray
origin and direction |
| 129 // plane's normal and offset (plane = { P /
P.N + d = 0 }) |
| 130 const Vec3r& norm, const real d, |
| 131 real& t, // I = orig
+ t * dir |
| 132 const real epsilon = M_EPSILON); // the epsi
lon to use |
| 133 |
| 134 /*! Intersection Ray-Bounding box (axis aligned). |
| 135 * Adapted from Williams et al, "An Efficient Robust Ray-Box Intersection Algor
ithm", JGT 10:1 (2005), pp. 49-54. |
| 136 */ |
| 137 LIB_GEOMETRY_EXPORT |
| 138 bool intersectRayBBox(const Vec3r& orig, const Vec3r& dir, // ray origin an
d direction |
| 139 const Vec3r& boxMin, const Vec3r& boxMax, // the bbox |
| 140 // the interval in which at least on of the intersections
must happen |
| 141 real t0, real t1, |
| 142 real& tmin, // Imin = orig +
tmin * dir is the first intersection |
| 143 real& tmax, // Imax = orig +
tmax * dir is the second intersection |
| 144 real epsilon = M_EPSILON); // the epsilon t
o use |
| 145 |
| 146 /*! Checks whether 3D point P lies inside or outside of the triangle ABC */ |
| 147 LIB_GEOMETRY_EXPORT |
| 148 bool includePointTriangle(const Vec3r& P, const Vec3r& A, const Vec3r& B, const
Vec3r& C); |
| 149 |
| 150 LIB_GEOMETRY_EXPORT |
| 151 void transformVertex(const Vec3r& vert, const Matrix44r& matrix, Vec3r& res); |
| 152 |
| 153 LIB_GEOMETRY_EXPORT |
| 154 void transformVertices(const vector<Vec3r>& vertices, const Matrix44r& trans, ve
ctor<Vec3r>& res); |
| 155 |
| 156 LIB_GEOMETRY_EXPORT |
| 157 Vec3r rotateVector(const Matrix44r& mat, const Vec3r& v); |
| 158 |
| 159 // |
| 160 // Coordinates systems changing procedures |
| 161 // |
| 162 ///////////////////////////////////////////////////////////////////////////// |
| 163 |
| 164 /*! From world to image |
| 165 * p |
| 166 * point's coordinates expressed in world coordinates system |
| 167 * q |
| 168 * vector in which the result will be stored |
| 169 * model_view_matrix |
| 170 * The model view matrix expressed in line major order (OpenGL |
| 171 * matrices are column major ordered) |
| 172 * projection_matrix |
| 173 * The projection matrix expressed in line major order (OpenGL |
| 174 * matrices are column major ordered) |
| 175 * viewport |
| 176 * The viewport: x,y coordinates followed by width and height (OpenGL like vi
ewport) |
| 177 */ |
| 178 LIB_GEOMETRY_EXPORT |
| 179 void fromWorldToImage(const Vec3r& p, Vec3r& q, const real model_view_matrix[4][
4], const real projection_matrix[4][4], |
| 180 const int viewport[4]); |
| 181 |
| 182 /*! From world to image |
| 183 * p |
| 184 * point's coordinates expressed in world coordinates system |
| 185 * q |
| 186 * vector in which the result will be stored |
| 187 * transform |
| 188 * The transformation matrix (gathering model view and projection),· |
| 189 * expressed in line major order (OpenGL matrices are column major ordered) |
| 190 * viewport |
| 191 * The viewport: x,y coordinates followed by width and height (OpenGL like vi
ewport) |
| 192 */ |
| 193 LIB_GEOMETRY_EXPORT |
| 194 void fromWorldToImage(const Vec3r& p, Vec3r& q, const real transform[4][4], cons
t int viewport[4]); |
| 195 |
| 196 /*! Projects from world coordinates to camera coordinates· |
| 197 * Returns the point's coordinates expressed in the camera's· |
| 198 * coordinates system. |
| 199 * p |
| 200 * point's coordinates expressed in world coordinates system |
| 201 * q |
| 202 * vector in which the result will be stored |
| 203 * model_view_matrix |
| 204 * The model view matrix expressed in line major order (OpenGL |
| 205 * matrices are column major ordered) |
| 206 */ |
| 207 LIB_GEOMETRY_EXPORT |
| 208 void fromWorldToCamera(const Vec3r& p, Vec3r& q, const real model_view_matrix[4]
[4]); |
| 209 |
| 210 /*! Projects from World Coordinates to retina coordinates |
| 211 * Returns the point's coordinates expressed in Retina system. |
| 212 * p |
| 213 * point's coordinates expressed in camera system |
| 214 * q |
| 215 * vector in which the result will be stored |
| 216 * projection_matrix |
| 217 * The projection matrix expressed in line major order (OpenGL |
| 218 * matrices are column major ordered) |
| 219 */ |
| 220 LIB_GEOMETRY_EXPORT |
| 221 void fromCameraToRetina(const Vec3r& p, Vec3r& q, const real projection_matrix[4
][4]); |
| 222 |
| 223 /*! From retina to image. |
| 224 * Returns the coordinates expressed in Image coorinates system. |
| 225 * p |
| 226 * point's coordinates expressed in retina system |
| 227 * q |
| 228 * vector in which the result will be stored |
| 229 * viewport |
| 230 * The viewport: x,y coordinates followed by width and height (OpenGL like vi
ewport). |
| 231 */ |
| 232 LIB_GEOMETRY_EXPORT |
| 233 void fromRetinaToImage(const Vec3r& p, Vec3r& q, const int viewport[4]); |
| 234 |
| 235 /*! From image to retina |
| 236 * p |
| 237 * point's coordinates expressed in image system |
| 238 * q |
| 239 * vector in which the result will be stored |
| 240 * viewport |
| 241 * The viewport: x,y coordinates followed by width and height (OpenGL like vi
ewport). |
| 242 */ |
| 243 LIB_GEOMETRY_EXPORT |
| 244 void fromImageToRetina(const Vec3r& p, Vec3r& q, const int viewport[4]); |
| 245 |
| 246 /*! computes the coordinates of q in the camera coordinates system,· |
| 247 * using the known z coordinates of the 3D point.· |
| 248 * That means that this method does not inverse any matrices,· |
| 249 * it only computes X and Y from x,y and Z) |
| 250 * p |
| 251 * point's coordinates expressed in retina system |
| 252 * q |
| 253 * vector in which the result will be stored |
| 254 * projection_matrix |
| 255 * The projection matrix expressed in line major order (OpenGL |
| 256 * matrices are column major ordered) |
| 257 */ |
| 258 LIB_GEOMETRY_EXPORT |
| 259 void fromRetinaToCamera(const Vec3r& p, Vec3r& q, real z, const real projection_
matrix[4][4]); |
| 260 |
| 261 /*! Projects from camera coordinates to world coordinates |
| 262 * Returns the point's coordinates expressed in the world's· |
| 263 * coordinates system. |
| 264 * p |
| 265 * point's coordinates expressed in the camera coordinates system |
| 266 * q |
| 267 * vector in which the result will be stored |
| 268 * model_view_matrix |
| 269 * The model view matrix expressed in line major order (OpenGL |
| 270 * matrices are column major ordered) |
| 271 */ |
| 272 LIB_GEOMETRY_EXPORT |
| 273 void fromCameraToWorld(const Vec3r& p, Vec3r& q, const real model_view_matrix[4]
[4]); |
| 274 |
| 275 } // end of namespace GeomUtils |
| 276 |
| 277 #endif // __GEOMUTILS_H__ |
OLD | NEW |