Blender V4.3
GeomUtils.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
12#include <vector>
13
14#include "Geom.h"
15
17
18using namespace std;
19
20namespace Freestyle {
21
22using namespace Geometry;
23
24namespace GeomUtils {
25
26//
27// Templated procedures
28//
30
32template<class T> real distPointSegment(const T &P, const T &A, const T &B)
33{
34 T AB, AP, BP;
35 AB = B - A;
36 AP = P - A;
37 BP = P - B;
38
39 real c1(AB * AP);
40 if (c1 <= 0) {
41 return AP.norm();
42 }
43
44 real c2(AB * AB);
45 if (c2 <= c1) {
46 return BP.norm();
47 }
48
49 real b = c1 / c2;
50 T Pb, PPb;
51 Pb = A + b * AB;
52 PPb = P - Pb;
53
54 return PPb.norm();
55}
56
57//
58// Non-templated procedures
59//
67
69 const Vec2r &p2, // first segment
70 const Vec2r &p3,
71 const Vec2r &p4, // second segment
72 Vec2r &res); // found intersection point
73
75 const Vec2r &p2, // first segment
76 const Vec2r &p3,
77 const Vec2r &p4, // second segment
78 Vec2r &res); // found intersection point
79
81 const Vec2r &p2, // first segment
82 const Vec2r &p3,
83 const Vec2r &p4, // second segment
84 real &t, // I = P1 + t * P1P2)
85 real &u, // I = P3 + u * P3P4
86 real epsilon = M_EPSILON);
87
89bool intersect2dSeg2dArea(const Vec2r &min, const Vec2r &max, const Vec2r &A, const Vec2r &B);
90
92bool include2dSeg2dArea(const Vec2r &min, const Vec2r &max, const Vec2r &A, const Vec2r &B);
93
95bool overlapTriangleBox(const Vec3r &boxcenter, const Vec3r &boxhalfsize, const Vec3r triverts[3]);
96
98bool intersectRayTriangle(const Vec3r &orig,
99 const Vec3r &dir,
100 const Vec3r &v0,
101 const Vec3r &v1,
102 const Vec3r &v2,
103 real &t, // I = orig + t * dir
104 real &u,
105 real &v, // I = (1 - u - v) * v0 + u * v1 + v * v2
106 const real epsilon = M_EPSILON); // the epsilon to use
107
110 const Vec3r &dir, // ray origin and direction
111 // plane's normal and offset (plane = { P / P.N + d = 0 })
112 const Vec3r &norm,
113 const real d,
114 real &t, // I = orig + t * dir
115 const real epsilon = M_EPSILON); // the epsilon to use
116
121bool intersectRayBBox(const Vec3r &orig,
122 const Vec3r &dir, // ray origin and direction
123 const Vec3r &boxMin,
124 const Vec3r &boxMax, // the bbox
125 // the interval in which at least on of the intersections must happen
126 real t0,
127 real t1,
128 real &tmin, // Imin = orig + tmin * dir is the first intersection
129 real &tmax, // Imax = orig + tmax * dir is the second intersection
130 real epsilon = M_EPSILON); // the epsilon to use
131
133bool includePointTriangle(const Vec3r &P, const Vec3r &A, const Vec3r &B, const Vec3r &C);
134
135void transformVertex(const Vec3r &vert, const Matrix44r &matrix, Vec3r &res);
136
137void transformVertices(const vector<Vec3r> &vertices, const Matrix44r &trans, vector<Vec3r> &res);
138
139Vec3r rotateVector(const Matrix44r &mat, const Vec3r &v);
140
141//
142// Coordinates systems changing procedures
143//
145
160void fromWorldToImage(const Vec3r &p,
161 Vec3r &q,
162 const real model_view_matrix[4][4],
163 const real projection_matrix[4][4],
164 const int viewport[4]);
165
177void fromWorldToImage(const Vec3r &p, Vec3r &q, const real transform[4][4], const int viewport[4]);
178
190void fromWorldToCamera(const Vec3r &p, Vec3r &q, const real model_view_matrix[4][4]);
191
202void fromCameraToRetina(const Vec3r &p, Vec3r &q, const real projection_matrix[4][4]);
203
213void fromRetinaToImage(const Vec3r &p, Vec3r &q, const int viewport[4]);
214
223void fromImageToRetina(const Vec3r &p, Vec3r &q, const int viewport[4]);
224
237void fromRetinaToCamera(const Vec3r &p, Vec3r &q, real focal, const real projection_matrix[4][4]);
238
250void fromCameraToWorld(const Vec3r &p, Vec3r &q, const real model_view_matrix[4][4]);
251
252} // end of namespace GeomUtils
253
254} /* namespace Freestyle */
Configuration definitions.
Vectors and Matrices (useful type definitions)
ATTR_WARN_UNUSED_RESULT const BMVert * v2
ATTR_WARN_UNUSED_RESULT const BMVert * v
#define A
SIMD_FORCE_INLINE btScalar norm() const
Return the norm (length) of the vector.
Definition btVector3.h:263
local_group_size(16, 16) .push_constant(Type b
#define B
void fromRetinaToImage(const Vec3r &p, Vec3r &q, const int viewport[4])
void fromWorldToImage(const Vec3r &p, Vec3r &q, const real model_view_matrix[4][4], const real projection_matrix[4][4], const int viewport[4])
bool overlapTriangleBox(const Vec3r &boxcenter, const Vec3r &boxhalfsize, const Vec3r triverts[3])
void transformVertex(const Vec3r &vert, const Matrix44r &matrix, Vec3r &res)
void fromWorldToCamera(const Vec3r &p, Vec3r &q, const real model_view_matrix[4][4])
real distPointSegment(const T &P, const T &A, const T &B)
Definition GeomUtils.h:32
intersection_test intersect2dSeg2dSegParametric(const Vec2r &p1, const Vec2r &p2, const Vec2r &p3, const Vec2r &p4, real &t, real &u, real epsilon)
bool intersect2dSeg2dArea(const Vec2r &min, const Vec2r &max, const Vec2r &A, const Vec2r &B)
Definition GeomUtils.cpp:19
intersection_test intersect2dSeg2dSeg(const Vec2r &p1, const Vec2r &p2, const Vec2r &p3, const Vec2r &p4, Vec2r &res)
Definition GeomUtils.cpp:50
void fromCameraToWorld(const Vec3r &p, Vec3r &q, const real model_view_matrix[4][4])
bool intersectRayTriangle(const Vec3r &orig, const Vec3r &dir, const Vec3r &v0, const Vec3r &v1, const Vec3r &v2, real &t, real &u, real &v, const real epsilon)
bool includePointTriangle(const Vec3r &P, const Vec3r &A, const Vec3r &B, const Vec3r &C)
void fromCameraToRetina(const Vec3r &p, Vec3r &q, const real projection_matrix[4][4])
void transformVertices(const vector< Vec3r > &vertices, const Matrix44r &trans, vector< Vec3r > &res)
bool intersectRayBBox(const Vec3r &orig, const Vec3r &dir, const Vec3r &boxMin, const Vec3r &boxMax, real t0, real t1, real &tmin, real &tmax, real)
bool include2dSeg2dArea(const Vec2r &min, const Vec2r &max, const Vec2r &A, const Vec2r &B)
Definition GeomUtils.cpp:40
intersection_test intersectRayPlane(const Vec3r &orig, const Vec3r &dir, const Vec3r &norm, const real d, real &t, const real epsilon)
void fromRetinaToCamera(const Vec3r &p, Vec3r &q, real focal, const real projection_matrix[4][4])
void fromImageToRetina(const Vec3r &p, Vec3r &q, const int viewport[4])
intersection_test intersect2dLine2dLine(const Vec2r &p1, const Vec2r &p2, const Vec2r &p3, const Vec2r &p4, Vec2r &res)
Vec3r rotateVector(const Matrix44r &mat, const Vec3r &v)
inherits from class Rep
Definition AppCanvas.cpp:20
static const real M_EPSILON
Definition Precision.h:17
double real
Definition Precision.h:14
#define min(a, b)
Definition sort.c:32