11#ifndef EASY3D_CORE_TYPES_H
12#define EASY3D_CORE_TYPES_H
19#include <easy3d/core/vec.h>
20#include <easy3d/core/mat.h>
21#include <easy3d/core/rect.h>
22#include <easy3d/core/line.h>
23#include <easy3d/core/oriented_line.h>
24#include <easy3d/core/segment.h>
25#include <easy3d/core/plane.h>
26#include <easy3d/core/box.h>
27#include <easy3d/core/quat.h>
28#include <easy3d/core/polygon.h>
29#include <easy3d/core/constant.h>
30#include <easy3d/core/export.h>
121 template <
typename FT>
123 return (std::isnan(v) || std::isinf(v));
130 inline T
clamp(T x, T lower, T upper) {
131 return std::min(upper, std::max(x, lower));
145 while (rval < a) rval <<= 1;
152 FT tmp = std::pow(10.0, num);
153 auto des =
static_cast<long long>((v < 0) ? (v * tmp - 0.5) : (v * tmp + 0.5));
154 return FT(des) / tmp;
164 const float absx = std::fabs(v.x);
165 const float absy = std::fabs(v.y);
166 const float absz = std::fabs(v.z);
168 if ((absy >= absx) && (absz >= absx))
169 return vec3(0.0f, -v.z, v.y);
171 if ((absx >= absy) && (absz >= absy))
172 return vec3(-v.z, 0.0f, v.x);
174 return vec3(-v.y, v.x, 0.0f);
179 template<
typename Box,
typename Container>
182 for (
const auto& p : points) {
189 template<
typename Vec,
typename Container>
192 for (
const auto& p : points)
194 return v / points.
size();
198 template<
typename Vec>
200 return (p1 + p2) * 0.5f;
204 template<
typename Vec>
206 return (p1 + p2 + p3) / 3.0f;
210 template<
typename Vec>
212 return (p1 + p2 + p3 + p4) * 0.25f;
217 template<
typename FT>
231 const double bound = 19.1;
232 return (v < -bound ? -bound : (v > bound ? bound : v));
237 const double bound = 0.9986;
238 return (v < -bound ? -bound : (v > bound ? bound : v));
242 template<
typename Vec>
248 template<
typename Vec>
254 template<
typename Vec>
260 template<
typename Vec>
266 template<
typename FT>
268 return degrees *
static_cast<FT
>(0.01745329251994329576923690768489);
272 template<
typename FT>
274 return radians *
static_cast<FT
>(57.295779513082320876798154814105);
284 return 0.5f *
det(p2 - p1, p3 - p1);
311 inline int encode(
int r,
int g,
int b) {
312 return (0xff << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
316 inline int encode(
int r,
int g,
int b,
int a) {
317 return ((a & 0xff) << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
323 inline void decode(
int value,
int &r,
int &g,
int &b) {
324 r = ((value >> 16) & 0xff);
325 g = ((value >> 8) & 0xff);
330 inline void decode(
int value,
int &r,
int &g,
int &b,
int &a) {
331 r = ((value >> 16) & 0xff);
332 g = ((value >> 8) & 0xff);
341 inline int red(
int color) {
return ((color >> 16) & 0xff); }
344 inline int green(
int color) {
return ((color >> 8) & 0xff); }
347 inline int blue(
int color) {
return (color & 0xff); }
350 inline int alpha(
int color) {
return color >> 24; }
365 template<
typename FT>
369 const Vec<3, FT> &w) {
370 Vec<3, FT> result(1.0 / 3.0);
372 Vec<3, FT> vu = v - u, wu = w - u, pu = p - u;
375 FT nx = vu[1] * wu[2] - vu[2] * wu[1],
376 ny = vu[2] * wu[0] - vu[0] * wu[2],
377 nz = vu[0] * wu[1] - vu[1] * wu[0], ax = fabs(nx), ay = fabs(ny),
380 unsigned char maxCoord;
397 if (1.0 + ax != 1.0) {
398 result[1] = 1.0 + (pu[1] * wu[2] - pu[2] * wu[1]) / nx - 1.0;
399 result[2] = 1.0 + (vu[1] * pu[2] - vu[2] * pu[1]) / nx - 1.0;
400 result[0] = 1.0 - result[1] - result[2];
406 if (1.0 + ay != 1.0) {
407 result[1] = 1.0 + (pu[2] * wu[0] - pu[0] * wu[2]) / ny - 1.0;
408 result[2] = 1.0 + (vu[2] * pu[0] - vu[0] * pu[2]) / ny - 1.0;
409 result[0] = 1.0 - result[1] - result[2];
415 if (1.0 + az != 1.0) {
416 result[1] = 1.0 + (pu[0] * wu[1] - pu[1] * wu[0]) / nz - 1.0;
417 result[2] = 1.0 + (vu[0] * pu[1] - vu[1] * pu[0]) / nz - 1.0;
418 result[0] = 1.0 - result[1] - result[2];
433 std::size_t n = polygon.size();
434 for (std::size_t i = 0, j = n - 1; i < n; j = i, ++i) {
435 const vec2 &u0 = polygon[i];
436 const vec2 &u1 = polygon[j];
438 if (((u0.y <= p.y) && (p.y < u1.y)) ||
439 ((u1.y <= p.y) && (p.y < u0.y)))
443 double x = u0.x + (p.y - u0.y) * (u1.x - u0.x) / (u1.y - u0.y);
458 float t =
dot(d2, d2);
463 d1 = p - (min_v = v1);
465 d1 = p - (min_v = v0 + d2 * t);
468 nearest_point = min_v;
482 if (fabs(len2) < FLT_MIN) {
501 float inv_d = 1.0f / len2;
507 float a =
dot(t, v0v2) * -inv_d;
508 float b =
dot(t, v0v1) * inv_d;
518 }
else if (s01 >= 1.0) {
521 (v0p = v0) += (v0v1 *= s01);
523 }
else if (s02 > 1.0) {
527 }
else if (s12 <= 0.0) {
530 (v0p = v1) += (v1v2 *= s12);
533 (v0p = v0) += (v0v2 *= s02);
544 }
else if (s02 >= 1.0) {
547 (v0p = v0) += (v0v2 *= s02);
549 }
else if (s01 > 1.0) {
553 }
else if (s12 <= 0.0) {
556 (v0p = v1) += (v1v2 *= s12);
559 (v0p = v0) += (v0v1 *= s01);
564 else if (a + b > 1.0) {
570 }
else if (s02 >= 1.0) {
573 (v0p = v0) += (v0v2 *= s02);
575 }
else if (s12 <= 0.0) {
579 }
else if (s01 >= 1.0) {
582 (v0p = v0) += (v0v1 *= s01);
585 (v0p = v1) += (v1v2 *= s12);
591 n *= (
dot(n, v0p) * inv_d);
630 assert(std::abs(den) > 1e-30f);
635 p.x +
float(num_x / den),
636 p.y -
float(num_y / den),
637 p.z +
float(num_z / den)
GenericBox represents the bounding box of shapes.
Definition: box.h:47
A generic line representation, which supports both 2D and 3D lines.
Definition: line.h:40
OrientedLine implements plucker coordinates, which enables oriented lines to be compared.
Definition: oriented_line.h:43
A 2D polygon representation.
Definition: polygon.h:41
The GenericRect class defines a rectangle in the 2D space.
Definition: rect.h:42
A generic segmentation representation, which supports both 2D and 3D line segments.
Definition: segment.h:43
2 by 2 matrix. Extends Mat with 2D-specific functionality and constructors.
Definition: mat.h:1460
3x3 matrix. Extends Mat with 3D-specific functionality and constructors.
Definition: mat.h:1620
Base class for matrix types.
Definition: mat.h:66
Base class for vector types. It provides generic functionality for N dimensional vectors.
Definition: vec.h:34
size_t size() const
Returns the dimension/size of this vector.
Definition: vec.h:78
double cos_angle(const Vec &a, const Vec &b)
Computes cosine of angle between two (un-normalized) vectors.
Definition: types.h:243
vec3 centroid(const SurfaceMesh *mesh, SurfaceMesh::Face f)
barycenter/centroid of a face
Definition: surface_mesh_geometry.cpp:67
vec3 tetra_circum_center(const vec3 &p, const vec3 &q, const vec3 &r, const vec3 &s)
Computes the circum center of a tetrahedron.
Box bounding_box(const Container &points)
Computes the bounding box of a set of points.
Definition: types.h:180
double angle(const Vec &a, const Vec &b)
Computes angle between two (un-normalized) vectors.
Definition: types.h:261
float dist_point_triangle(const vec3 &p, const vec3 &v0, const vec3 &v1, const vec3 &v2, vec3 &nearest_point)
Computes the distance of a point p to the triangle given by vec3s (v0, v1, v2).
Vec< 3, FT > barycentric_coordinates(const Vec< 3, FT > &p, const Vec< 3, FT > &u, const Vec< 3, FT > &v, const Vec< 3, FT > &w)
Computes the barycentric coordinates of a point p with respect to three points u, v,...
float triangle_area(const SurfaceMesh *mesh, SurfaceMesh::Face f)
compute area of triangle f
Definition: surface_mesh_geometry.cpp:22
Vec barycenter(const Vec &p1, const Vec &p2)
Computes the barycenter of two points.
Definition: types.h:199
float triangle_signed_area(const vec2 &p1, const vec2 &p2, const vec2 &p3)
Computes signed area of a triangle given by three points.
Definition: types.h:283
double clamp_cot(const double v)
Clamps cotangent values as if angles are in [1, 179]
Definition: types.h:230
FT to_radians(FT degrees)
Converts an angle from degrees to radians.
Definition: types.h:267
Vec::FT cotan_angle(const Vec &a, const Vec &b)
Computes cotangent of angle between two (un-normalized) vectors.
Definition: types.h:255
vec3 triangle_normal(const vec3 &p1, const vec3 &p2, const vec3 &p3)
Computes the normal vector of a triangle given by three points.
Definition: types.h:288
float dist_point_line_segment(const vec3 &p, const vec3 &v0, const vec3 &v1, vec3 &nearest_point)
Computes the distance of a point p to a line segment given by vec3s (v0,v1).
bool point_in_polygon(const vec2 &p, const std::vector< vec2 > &polygon)
Tests if a point p lies inside or outside of a polygon. This function is robust to handle general pol...
vec3 orthogonal(const vec3 &v)
Returns a vector orthogonal to v. Its norm() depends on v, but is zero only for a null v.
Definition: types.h:163
double clamp_cos(const double v)
Clamps cosine values as if angles are in [1, 179]
Definition: types.h:236
FT to_degrees(FT radians)
Converts an angle from radians to degrees.
Definition: types.h:273
double sin_angle(const Vec &a, const Vec &b)
Computes sine of angle between two (un-normalized) vectors.
Definition: types.h:249
Definition: collider.cpp:182
Mat2< double > dmat2
A 2 by 2 matrix of double type.
Definition: types.h:75
Mat< 4, 3, float > mat43
A 4 by 3 matrix of float type.
Definition: types.h:72
Vec< 3, float > vec3
A 3D point/vector of float type.
Definition: types.h:45
Mat< 3, 4, double > dmat34
A 3 by 4 matrix of double type.
Definition: types.h:81
GenericOrientedLine< float > OrientedLine3
A 3D oriented line of float type.
Definition: types.h:96
GenericPolygon< float > Polygon2
A 2D polygon of float type.
Definition: types.h:117
T determinant(const Mat< N, N, T > &m)
Return the determinant of N x N (square) matrix m.
GenericRect< float > Rect
A 2D axis-aligned rectangle of float type.
Definition: types.h:112
GenericBox< 2, float > Box2
A 2D axis-aligned bounding box of float type.
Definition: types.h:107
Vec< 3, double > dvec3
A 3D point/vector of double type.
Definition: types.h:52
Vec< 2, int32_t > ivec2
A 2D point/vector of int32_t type.
Definition: types.h:57
Mat4< double > dmat4
A 4 by 4 matrix of double type.
Definition: types.h:79
int next_pow2(int a)
Calculates the next larger power of 2. If the input is already a power of 2, it will return itself.
Definition: types.h:143
Vec< 4, int32_t > ivec4
A 4D point/vector of int32_t type.
Definition: types.h:61
GenericBox< 3, float > Box3
A 3D axis-aligned bounding box of float type.
Definition: types.h:109
Vec< 2, double > dvec2
A 2D point/vector of double type.
Definition: types.h:50
GenericRect< int32_t > iRect
A 2D axis-aligned rectangle of int32_t type.
Definition: types.h:114
T clamp(T x, T lower, T upper)
Clamps a num to be within a given range.
Definition: types.h:130
Quat< float > quat
A quaternion of float type.
Definition: types.h:86
Mat2< float > mat2
A 2 by 2 matrix of float type.
Definition: types.h:64
T length(const Vec< N, T > &v)
Computes the length/magnitude of a vector.
Definition: vec.h:289
GenericPlane< float > Plane3
A 3D plane of float type.
Definition: types.h:104
Vec< 3, int32_t > ivec3
A 3D point/vector of int32_t type.
Definition: types.h:59
GenericLine< 3, float > Line3
A 3D line of float type.
Definition: types.h:93
GenericSegment< 3, float > Segment3
A 3D line segment of float type.
Definition: types.h:101
T det(const Vec< 2, T > &v1, const Vec< 2, T > &v2)
Compute the determinant of the 2x2 matrix formed by the two 2D vectors as the two rows.
Definition: vec.h:405
Quat< double > dquat
A quaternion of double type.
Definition: types.h:88
Mat4< float > mat4
A 4 by 4 matrix of float type.
Definition: types.h:68
Mat3< float > mat3
A 3 by 3 matrix of float type.
Definition: types.h:66
Vec< 4, float > vec4
A 4D point/vector of float type.
Definition: types.h:47
Mat3< double > dmat3
A 3 by 3 matrix of double type.
Definition: types.h:77
Vec< 2, float > vec2
A 2D point/vector of float type.
Definition: types.h:43
GenericSegment< 2, float > Segment2
A 2D line segment of float type.
Definition: types.h:99
Vec< 3, T > cross(const Vec< 3, T > &v1, const Vec< 3, T > &v2)
Compute the cross product of two 3D vectors.
Definition: vec.h:534
GenericLine< 2, float > Line2
A 2D line of float type.
Definition: types.h:91
FT dot(const std::vector< FT > &, const std::vector< FT > &)
Inner product for vectors.
Definition: matrix.h:1803
Vec< 4, double > dvec4
A 4D point/vector of double type.
Definition: types.h:54
Mat< 4, 3, double > dmat43
A 4 by 3 matrix of double type.
Definition: types.h:83
FT norm(const Matrix< FT > &)
utilities
Definition: matrix.h:1424
Vec< N, T > normalize(const Vec< N, T > &v)
Computes and returns the normalized vector (Note: the input vector is not modified).
Definition: vec.h:299
bool is_nan(FT v)
Is this NaN?
Definition: types.h:122
FT truncate_digits(const FT &v, int num)
Rounds the given floating point number v to have num digits.
Definition: types.h:151
Mat< 3, 4, float > mat34
A 3 by 4 matrix of float type.
Definition: types.h:70
T length2(const Vec< N, T > &v)
Computes the squared length/magnitude of a vector.
Definition: vec.h:293