40 #ifndef GEOGRAM_BASIC_GEOMETRY
41 #define GEOGRAM_BASIC_GEOMETRY
154 inline double det(
const mat3& M) {
156 M(0,0), M(0,1), M(0,2),
157 M(1,0), M(1,1), M(1,2),
158 M(2,0), M(2,1), M(2,2)
167 inline double det(
const mat4& M) {
169 M(0,0), M(0,1), M(0,2), M(0,3),
170 M(1,0), M(1,1), M(1,2), M(1,3),
171 M(2,0), M(2,1), M(2,2), M(2,3),
172 M(3,0), M(3,1), M(3,2), M(3,3)
221 (p1.x + p2.x + p3.x) / 3.0,
222 (p1.y + p2.y + p3.y) / 3.0,
223 (p1.z + p2.z + p3.z) / 3.0
238 (p1.x + p2.x + p3.x) / 3.0,
239 (p1.y + p2.y + p3.y) / 3.0
250 double lab = ::sqrt(length2(a)*length2(b));
251 double result = (lab > 1e-50) ? (dot(a, b) / lab) : 1.0;
277 double lab = ::sqrt(length2(a)*length2(b));
278 double result = (lab > 1e-20) ? (dot(a, b) / lab) : 1.0;
293 return a.x * b.y - a.y * b.x;
304 return det(a, b) > 0 ?
318 return cross(p2 - p1, p3 - p1);
327 const double* p1,
const double* p2,
const double* p3
329 double Ux = p2[0] - p1[0];
330 double Uy = p2[1] - p1[1];
331 double Uz = p2[2] - p1[2];
333 double Vx = p3[0] - p1[0];
334 double Vy = p3[1] - p1[1];
335 double Vz = p3[2] - p1[2];
337 double Nx = Uy*Vz - Uz*Vy;
338 double Ny = Uz*Vx - Ux*Vz;
339 double Nz = Ux*Vy - Uy*Vx;
340 return 0.5 * ::sqrt(Nx*Nx+Ny*Ny+Nz*Nz);
363 const double* p1,
const double* p2,
const double* p3
365 double a = p2[0]-p1[0];
366 double b = p3[0]-p1[0];
367 double c = p2[1]-p1[1];
368 double d = p3[1]-p1[1];
369 return 0.5*(a*d-b*c);
383 return 0.5 *
det(p2 - p1, p3 - p1);
407 const double* p1,
const double* p2,
const double* p3
467 return dot(p2 - p1, cross(p3 - p1, p4 - p1)) / 6.0;
480 const double* p1,
const double* p2,
481 const double* p3,
const double* p4
484 *
reinterpret_cast<const vec3*
>(p1),
485 *
reinterpret_cast<const vec3*
>(p2),
486 *
reinterpret_cast<const vec3*
>(p3),
487 *
reinterpret_cast<const vec3*
>(p4)
536 double a,
double b,
double c,
539 double abc = a + b + c;
541 V = area / 3.0 * abc;
545 double s = area / 12.0;
546 Vg.x = s * (wp * p.x + wq * q.x + wr * r.x);
547 Vg.y = s * (wp * p.y + wq * q.y + wr * r.y);
548 Vg.z = s * (wp * p.z + wq * q.z + wr * r.z);
565 double a,
double b,
double c
568 sqrt(::fabs(a)) + sqrt(::fabs(b)) + sqrt(::fabs(c))
593 double u = 1.0 - s - t;
595 u * p1.x + s * p2.x + t * p3.x,
596 u * p1.y + s * p2.y + t * p3.y,
597 u * p1.z + s * p2.z + t * p3.z
616 vec3 n = cross(p2 - p1, p3 - p1);
620 d = -(a * p1.x + b * p1.y + c * p1.z);
633 d = -(a * p.x + b * p.y + c * p.z);
640 double a_in,
double b_in,
double c_in,
double d_in
658 return vec3(a, b, c);
681 if(b[c] < xyz_min[c]) {
684 if(b[c] > xyz_max[c]) {
703 if(B1.xyz_max[c] < B2.xyz_min[c]) {
706 if(B1.xyz_min[c] > B2.xyz_max[c]) {
722 target.xyz_min[c] = std::min(B1.xyz_min[c], B2.xyz_min[c]);
723 target.xyz_max[c] = std::max(B1.xyz_max[c], B2.xyz_max[c]);
744 if(b[c] < xy_min[c]) {
747 if(b[c] > xy_max[c]) {
765 if(B1.xy_max[c] < B2.xy_min[c]) {
768 if(B1.xy_min[c] > B2.xy_max[c]) {
784 target.xy_min[c] = std::min(B1.xy_min[c], B2.xy_min[c]);
785 target.xy_max[c] = std::max(B1.xy_max[c], B2.xy_max[c]);
816 result[i] += v[j] * m(j,i) ;
821 result[0], result[1], result[2]
852 result[i] += v[j] * m(j,i) ;
858 result[0] / result[3],
859 result[1] / result[3],
860 result[2] / result[3]
892 result[i] += v[j] * m(i,j) ;
898 result[0] / result[3],
899 result[1] / result[3],
900 result[2] / result[3]
919 FT res[4] = {FT(0), FT(0), FT(0), FT(0)};
923 res[i] += v[j] * m(j,i) ;
927 return vecng<4,FT>(res[0], res[1], res[2], res[3]) ;
Axis-aligned bounding box.
bool contains(const vec2 &b) const
Tests whether a box contains a point.
Axis-aligned bounding box.
bool contains(const vec3 &b) const
Tests whether a box contains a point.
void load_identity()
Sets the matrix to identity.
T * data()
Gets modifiable vector data.
Common include file, providing basic definitions. Should be included before anything else by all head...
double det(const vec2 &a, const vec2 &b)
Computes the determinant of two vectors.
double triangle_signed_area(const vec2 &p1, const vec2 &p2, const vec2 &p3)
Computes the area of a 2d triangle.
vec3 tetra_circum_center(const vec3 &p1, const vec3 &p2, const vec3 &p3, const vec3 &p4)
Computes the center of the circumscribed sphere of 3d tetrahedron.
vec3 random_point_in_triangle(const vec3 &p1, const vec3 &p2, const vec3 &p3)
Generates a random point in a 3d triangle.
double tetra_volume(const vec3 &p1, const vec3 &p2, const vec3 &p3, const vec3 &p4)
Computes the volume of a 3d tetrahedron.
vec3 perpendicular(const vec3 &V)
Computes a 3d vector orthogonal to another one.
vec3 triangle_normal(const vec3 &p1, const vec3 &p2, const vec3 &p3)
Computes the normal of a 3d triangle.
double triangle_mass(const vec3 &p, const vec3 &q, const vec3 &r, double a, double b, double c)
Computes the mass of a 3d triangle with weighted points.
double cos_angle(const vec3 &a, const vec3 &b)
Computes the cosine of the angle between two 3d vectors.
double triangle_area(const vec3 &p1, const vec3 &p2, const vec3 &p3)
Computes the area of a 3d triangle.
double triangle_signed_area_2d(const double *p1, const double *p2, const double *p3)
Computes the area of a 2d triangle.
void triangle_centroid(const vec3 &p, const vec3 &q, const vec3 &r, double a, double b, double c, vec3 &Vg, double &V)
Computes the centroid of a 3d triangle with weighted points.
bool has_nan(const vec3 &v)
Tests whether a 3d vector has a NaN (not a number) coordinate.
double triangle_area_2d(const double *p1, const double *p2, const double *p3)
Computes the area of a 2d triangle.
double angle(const vec3 &a, const vec3 &b)
Computes the angle between two 3d vectors.
double triangle_area_3d(const double *p1, const double *p2, const double *p3)
Computes the area of a 3d triangle.
double tetra_signed_volume(const vec3 &p1, const vec3 &p2, const vec3 &p3, const vec3 &p4)
Computes the signed volume of a 3d tetrahedron.
vec3 barycenter(const vec3 &p1, const vec3 &p2)
Computes the barycenter of two points in 3d.
vec2 triangle_circumcenter(const vec2 &p1, const vec2 &p2, const vec2 &p3)
Computes the center of the circumscribed circle of a 2d triangle.
float64 random_float64()
Returns a 64 bits float between 0 and 1.
bool is_nan(float32 x)
Checks whether a 32 bits float is "not a number".
Global Vorpaline namespace.
Matrix< 4, Numeric::float64 > mat4
Represents a 4x4 matrix.
bool bboxes_overlap(const Box &B1, const Box &B2)
Tests whether two Boxes have a non-empty intersection.
vecng< 3, Numeric::float32 > vec3f
Represents points and vectors in 3d with single-precision coordinates.
vecng< 4, Numeric::float32 > vec4f
Represents points and vectors in 4d with single-precision coordinates.
void bbox_union(Box &target, const Box &B1, const Box &B2)
Computes the smallest Box that encloses two Boxes.
vecng< 3, Numeric::float64 > vec3
Represents points and vectors in 3d.
T det3x3(const T &a11, const T &a12, const T &a13, const T &a21, const T &a22, const T &a23, const T &a31, const T &a32, const T &a33)
Computes a three-by-three determinant.
T det4x4(const T &a11, const T &a12, const T &a13, const T &a14, const T &a21, const T &a22, const T &a23, const T &a24, const T &a31, const T &a32, const T &a33, const T &a34, const T &a41, const T &a42, const T &a43, const T &a44)
Computes a four-by-four determinant.
void geo_clamp(T &x, T min, T max)
Clamps a value to a range.
mat4 create_scaling_matrix(double s)
Creates a scaling matrix.
vecng< 3, FT > transform_vector(const vecng< 3, FT > &v, const Matrix< 4, FT > &m)
Applies a 3d transform to a 3d vector.
vecng< 3, Numeric::int32 > vec3i
Represents points and vectors in 3d with integer coordinates.
geo_index_t index_t
The type for storing and manipulating indices.
double det(const mat2 &M)
Computes the determinant of a 2x2 matrix.
vecng< 4, Numeric::float64 > vec4
Represents points and vectors in 4d.
mat4 create_translation_matrix(const vec3 &T)
Creates a translation matrix from a vector.
vecng< 2, Numeric::int32 > vec2i
Represents points and vectors in 2d with integer coordinates.
T det2x2(const T &a11, const T &a12, const T &a21, const T &a22)
Computes a two-by-two determinant.
Matrix< 3, Numeric::float64 > mat3
Represents a 3x3 matrix.
vecng< 2, Numeric::float32 > vec2f
Represents points and vectors in 2d with single-precision coordinates.
vecng< 2, Numeric::float64 > vec2
Represents points and vectors in 2d.
geo_coord_index_t coord_index_t
The type for storing coordinate indices, and iterating on the coordinates of a point.
vecng< 4, Numeric::int32 > vec4i
Represents points and vectors in 4d with integer coordinates.
Matrix< 2, Numeric::float64 > mat2
Represents a 2x2 matrix.
vecng< 3, FT > transform_point(const vecng< 3, FT > &v, const Matrix< 4, FT > &m)
Applies a 3d transform to a 3d point.
Plane(const vec3 &p1, const vec3 &p2, const vec3 &p3)
Constructs the plane passing through three points.
Plane(const vec3 &p, const vec3 &n)
Constructs a plane passign through a point and orthogonal to a vector.
Plane(double a_in, double b_in, double c_in, double d_in)
Constructs a plane from the coefficients of its equation.
Plane()
Constructs an uninitialized plane.
vec3 normal() const
Gets the normal vector of the plane.
A Ray, in parametric form.
Ray(vec3 O, vec3 D)
Ray constructor.