40#ifndef GEOGRAM_BASIC_GEOMETRY
41#define GEOGRAM_BASIC_GEOMETRY
176 M(0,0), M(0,1), M(0,2),
177 M(1,0), M(1,1), M(1,2),
178 M(2,0), M(2,1), M(2,2)
189 M(0,0), M(0,1), M(0,2), M(0,3),
190 M(1,0), M(1,1), M(1,2), M(1,3),
191 M(2,0), M(2,1), M(2,2), M(2,3),
192 M(3,0), M(3,1), M(3,2), M(3,3)
241 (p1.x + p2.x + p3.x) / 3.0,
242 (p1.y + p2.y + p3.y) / 3.0,
243 (p1.z + p2.z + p3.z) / 3.0
258 (p1.x + p2.x + p3.x) / 3.0,
259 (p1.y + p2.y + p3.y) / 3.0
271 double result = (lab > 1e-50) ? (
dot(a, b) / lab) : 1.0;
298 double result = (lab > 1e-20) ? (
dot(a, b) / lab) : 1.0;
313 return a.x * b.y - a.y * b.x;
324 return det(a, b) > 0 ?
338 return cross(p2 - p1, p3 - p1);
347 const double* p1,
const double* p2,
const double* p3
349 double Ux = p2[0] - p1[0];
350 double Uy = p2[1] - p1[1];
351 double Uz = p2[2] - p1[2];
353 double Vx = p3[0] - p1[0];
354 double Vy = p3[1] - p1[1];
355 double Vz = p3[2] - p1[2];
357 double Nx = Uy*Vz - Uz*Vy;
358 double Ny = Uz*Vx - Ux*Vz;
359 double Nz = Ux*Vy - Uy*Vx;
360 return 0.5 * ::sqrt(Nx*Nx+Ny*Ny+Nz*Nz);
383 const double* p1,
const double* p2,
const double* p3
385 double a = p2[0]-p1[0];
386 double b = p3[0]-p1[0];
387 double c = p2[1]-p1[1];
388 double d = p3[1]-p1[1];
389 return 0.5*(a*d-b*c);
403 return 0.5 *
det(p2 - p1, p3 - p1);
427 const double* p1,
const double* p2,
const double* p3
487 return dot(p2 - p1,
cross(p3 - p1, p4 - p1)) / 6.0;
500 const double* p1,
const double* p2,
501 const double* p3,
const double* p4
504 *
reinterpret_cast<const vec3*
>(p1),
505 *
reinterpret_cast<const vec3*
>(p2),
506 *
reinterpret_cast<const vec3*
>(p3),
507 *
reinterpret_cast<const vec3*
>(p4)
556 double a,
double b,
double c,
559 double abc = a + b + c;
561 V = area / 3.0 * abc;
565 double s = area / 12.0;
566 Vg.x = s * (wp * p.x + wq * q.x + wr * r.x);
567 Vg.y = s * (wp * p.y + wq * q.y + wr * r.y);
568 Vg.z = s * (wp * p.z + wq * q.z + wr * r.z);
585 double a,
double b,
double c
588 sqrt(::fabs(a)) + sqrt(::fabs(b)) + sqrt(::fabs(c))
613 double u = 1.0 - s - t;
615 u * p1.x + s * p2.x + t * p3.x,
616 u * p1.y + s * p2.y + t * p3.y,
617 u * p1.z + s * p2.z + t * p3.z
640 d = -(a * p1.x + b * p1.y + c * p1.z);
653 d = -(a * p.x + b * p.y + c * p.z);
660 double a_in,
double b_in,
double c_in,
double d_in
678 return vec3(a, b, c);
707 Box(
double x1,
double y1,
double z1,
double x2,
double y2,
double z2) {
735 return vec3(xyz_min);
743 return vec3(xyz_max);
753 if(b[c] < xyz_min[c]) {
756 if(b[c] > xyz_max[c]) {
798 xyz_min[c] = std::min(xyz_min[c], p[c]);
799 xyz_max[c] = std::max(xyz_max[c], p[c]);
816 if(B1.xyz_max[c] < B2.xyz_min[c]) {
819 if(B1.xyz_min[c] > B2.xyz_max[c]) {
835 target.xyz_min[c] = std::min(B1.xyz_min[c], B2.xyz_min[c]);
836 target.xyz_max[c] = std::max(B1.xyz_max[c], B2.xyz_max[c]);
863 Box2d(
double x1,
double y1,
double x2,
double y2) {
905 if(b[c] < xy_min[c]) {
908 if(b[c] > xy_max[c]) {
948 xy_min[c] = std::min(xy_min[c], p[c]);
949 xy_max[c] = std::max(xy_max[c], p[c]);
964 if(B1.xy_max[c] < B2.xy_min[c]) {
967 if(B1.xy_min[c] > B2.xy_max[c]) {
983 target.xy_min[c] = std::min(B1.xy_min[c], B2.xy_min[c]);
984 target.xy_max[c] = std::max(B1.xy_max[c], B2.xy_max[c]);
1005 template <
class FT> [[deprecated(
"use operators and vec3/4 conversions")]]
1027 template <
class FT> [[deprecated(
"use operators and vec3/4 conversions")]]
1051 template <
class FT> [[deprecated(
"use operators and vec3/4 conversions")]]
1069 template <
class FT> [[deprecated(
"use operators and vec3/4 conversions")]]
Axis-aligned bounding box.
vec2 hi() const
Gets the higher bounds.
void add(const vec2 &p)
Adds a point to this box.
vec2 lo() const
Gets the lower bounds.
void clear()
Makes this box empty.
bool contains(const vec2 &b) const
Tests whether a box contains a point.
void enlarge(double d)
Enlarges the box.
Box2d(const vec2 &lo, const vec2 &hi)
Constructs a box from lower and upper bounds.
Box2d(double x1, double y1, double x2, double y2)
Constructs a box from lower and upper bounds.
Box2d()
Constructs an uninitialized Box2d.
Axis-aligned bounding box.
Box(const vec3 &lo, const vec3 &hi)
Constructs a box from lower and upper bounds.
void add(const vec3 &p)
Adds a point to this box.
vec3 lo() const
Gets the lower bounds.
void clear()
Makes this box empty.
vec3 hi() const
Gets the higher bounds.
Box()
Constructs an uninitialized box.
bool contains(const vec3 &b) const
Tests whether a box contains a point.
void enlarge(double d)
Enlarges the box.
Box(double x1, double y1, double z1, double x2, double y2, double z2)
Constructs a box from lower and upper bounds.
void load_identity()
Sets the matrix to identity.
vecng< 3, T > cross(const vecng< 3, T > &v1, const vecng< 3, T > &v2)
Computes the cross product of 2 vectors.
T length2(const vecng< DIM, T > &v)
Gets the square norm of a vector.
T * data()
Gets modifiable vector data.
Common include file, providing basic definitions. Should be included before anything else by all head...
Geometric functions and utilities.
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.
constexpr float64 max_float64()
Gets 64 bits float maximum positive value.
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.
T dot(const vecng< 3, T > &v1, const vecng< 3, T > &v2)
Computes the dot product of 2 vectors. \relates vecng
vecng< 4, Numeric::float32 > vec4f
Represents points and vectors in 4d with single-precision coordinates.
vecng< 3, FT > transform_vector(const vecng< 3, FT > &v, const Matrix< 4, FT > &M)
Applies a 3d transform to a 3d vector.
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.
vecng< 3, FT > transform_point(const vecng< 3, FT > &p, const Matrix< 4, FT > &M)
Applies a 3d transform to a 3d point.
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.
vecng< 3, Numeric::uint32 > vec3u
Represents points and vectors in 3d with unsigned integer coordinates.
mat4 create_scaling_matrix(double s)
Creates a scaling matrix.
vecng< 3, Numeric::int32 > vec3i
Represents points and vectors in 3d with integer coordinates.
vecng< 2, Numeric::uint32 > vec2u
Represents points and vectors in 2d with unsigned 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< 4, Numeric::uint32 > vec4u
Represents points and vectors in 4d with unsigned integer coordinates.
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.
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.
Ray(vec3 O, vec3 D)
Ray constructor.