15 size_t dim,
const std::pair<util::Point, util::Point> &box) {
18 return {box.first, box.second};
25 double a = box.second.
d_x - box.first.d_x;
26 double b = box.second.d_y - box.first.d_y;
27 double c = box.second.d_z - box.first.d_z;
38 std::cerr <<
"Error: Check dimension = " << dim <<
".\n";
43std::vector<std::pair<util::Point, util::Point>>
util::getEdges(
size_t dim,
const
44std::pair<util::Point, util::Point> &box) {
46 std::vector<std::pair<util::Point, util::Point>> data;
48 data.emplace_back(box);
50 }
else if (dim == 2) {
55 data.emplace_back(corner_pts[0], corner_pts[1]);
56 data.emplace_back(corner_pts[1], corner_pts[2]);
57 data.emplace_back(corner_pts[2], corner_pts[3]);
58 data.emplace_back(corner_pts[3], corner_pts[0]);
60 }
else if (dim == 3) {
67 data.emplace_back(corner_pts[0], corner_pts[1]);
68 data.emplace_back(corner_pts[1], corner_pts[2]);
69 data.emplace_back(corner_pts[2], corner_pts[3]);
70 data.emplace_back(corner_pts[3], corner_pts[0]);
73 data.emplace_back(corner_pts[4], corner_pts[5]);
74 data.emplace_back(corner_pts[5], corner_pts[6]);
75 data.emplace_back(corner_pts[6], corner_pts[7]);
76 data.emplace_back(corner_pts[7], corner_pts[4]);
79 data.emplace_back(corner_pts[0], corner_pts[4]);
80 data.emplace_back(corner_pts[1], corner_pts[5]);
81 data.emplace_back(corner_pts[2], corner_pts[6]);
82 data.emplace_back(corner_pts[3], corner_pts[7]);
86 std::cerr <<
"getEdges(): Function implemented for dim = 1,2,3 only.\n";
92 const std::pair<util::Point, util::Point> &box) {
95 return {0.5 * box.second.
d_x + 0.5 * box.first.d_x, 0., 0.};
97 return {0.5 * box.second.d_x + 0.5 * box.first.d_x,
98 0.5 * box.second.d_y + 0.5 * box.first.d_y, 0.};
100 return {0.5 * box.second.d_x + 0.5 * box.first.d_x,
101 0.5 * box.second.d_y + 0.5 * box.first.d_y,
102 0.5 * box.second.d_z + 0.5 * box.first.d_z};
104 std::cerr <<
"Error: Check dimension = " << dim <<
".\n";
111 const std::pair<util::Point, util::Point> &b2,
126 for (
auto pp : cp2) {
160 const std::pair<util::Point, util::Point> &box) {
178 std::cerr <<
"isPointInsideBox(): Function implemented for dim = 1,2,3 only.\n";
184 const std::pair<util::Point, util::Point> &box) {
186 double r = 0.5 * std::abs(box.second.d_x - box.first.d_x);
190 if (
util::isGreater(r, 0.5 * std::abs(box.second.d_y - box.first.d_y)))
191 return 0.5 * std::abs(box.second.d_y - box.first.d_y);
194 }
else if (dim == 3) {
195 if (
util::isGreater(r, 0.5 * std::abs(box.second.d_y - box.first.d_y)))
196 r = 0.5 * std::abs(box.second.d_y - box.first.d_y);
198 if (
util::isGreater(r, 0.5 * std::abs(box.second.d_z - box.first.d_z)))
199 return 0.5 * std::abs(box.second.d_z - box.first.d_z);
203 std::cerr <<
"inscribedRadiusInBox(): Function implemented for dim = 1,2,3 only.\n";
209 const std::pair<util::Point, util::Point> &box) {
214 auto dx = cp[0] - xc;
215 auto r = dx.length();
232 double x_max,
double y_min,
250 double x2,
double y1,
251 double y2,
double theta) {
299 double p_dot_a = p * axis;
300 if (p_dot_a > length or p_dot_a < 0.)
304 auto p_parallel = p - p_dot_a * axis;
306 return p_parallel.
lengthSq() < radius * radius;
315 double p_dot_a = p_new * a;
318 if (p_dot_a > 1. or p_dot_a < 0.)
322 auto p_parallel = p_new - p_dot_a * a;
324 return p_parallel.
lengthSq() < radius * radius;
329std::vector<double> &radius_vec,
unsigned int dim) {
333 for (
unsigned int i=0; i<dim; i++)
334 d += x[i] * x[i] / (radius_vec[i] * radius_vec[i]);
340std::vector<double> &radius_vec,
unsigned int dim,
double &d) {
344 for (
unsigned int i=0; i<dim; i++)
345 d += x[i] * x[i] / (radius_vec[i] * radius_vec[i]);
353 return (1. - s) * p1 + s * p2;
357 const std::pair<util::Point, util::Point> &line_2) {
364 auto a = line_1.second - line_1.first;
365 auto b = line_2.first - line_1.first;
366 auto c = line_2.second - line_2.first;
369 if (
util::angle(a / a.length(), c / c.length()) < 1.0E-8)
373 double a_dot_b = a * b;
374 double a_dot_c = a * c;
375 double b_dot_c = b * c;
376 double c_dot_c = c.lengthSq();
378 double r = (a_dot_a * b_dot_c - a_dot_b * a_dot_c) /
379 (a_dot_c * a_dot_c - c_dot_c * a_dot_a);
384 return r > 0. and r < 1.;
388 const std::pair<util::Point, util::Point>
396 auto u = line_1.second - line_1.first;
397 auto v = line_2.second - line_2.first;
398 auto w0 = line_1.first - line_2.first;
406 auto dp = w0 + ((b * e - c * d) * u + (a * e - b * d) * v) / (a * c - b * b);
413 const std::pair<util::Point, util::Point> &line_2) {
420 auto u = line_1.second - line_1.first;
421 auto v = line_2.second - line_2.first;
422 auto w0 = line_1.first - line_2.first;
429 double D = a * c - b * b;
430 double sc, sN, sD = D;
431 double tc, tN, tD = D;
449 }
else if (sN > sD) {
468 }
else if (tN > tD) {
482 sc = std::abs(sN) < 1.0E-12 ? 0. : sN / sD;
483 tc = std::abs(tN) < 1.0E-12 ? 0. : tN / tD;
485 auto dp = w0 + sc * u - tc * v;
491 const std::pair<util::Point, util::Point>
495 if (
util::angle(plane_1.first, plane_2.first) < 1.0E-8)
498 return std::abs(plane_1.first * (plane_1.second - plane_2.second)) /
499 plane_1.first.length();
503 const std::pair<util::Point, util::Point> &line) {
506 auto v = line.second - line.first;
509 auto w = p - line.first;
512 auto w_on_line = line.first + (w * v) * v / v.
lengthSq();
514 return (p - w_on_line).length();
518 const std::pair<util::Point, util::Point> &line) {
521 auto v = line.second - line.first;
524 auto w = p - line.first;
527 double w_dot_v = w * v;
528 if (w_dot_v < 1.0E-12)
529 return (p - line.first).
length();
531 if (w_dot_v > v.lengthSq() - 1.0E-12)
532 return (p - line.second).length();
535 auto w_on_line = line.first + w_dot_v * v / v.lengthSq();
537 return (p - w_on_line).length();
541 const std::pair<util::Point, util::Point> &plane) {
547 auto pa = p - plane.second;
548 return std::abs(pa * plane.first) / plane.first.
length();
554 if (nodes.size() < 2)
557 guess = (nodes[0] - nodes[1]).length();
558 for (
size_t i = 0; i < nodes.size(); i++)
559 for (
size_t j = 0; j < nodes.size(); j++)
561 double val = nodes[i].dist(nodes[j]);
565 std::cout <<
"Check nodes are too close = "
566 << util::io::printStr<util::Point>({nodes[i],
569 std::cout <<
"Distance = " << val <<
", guess = " << guess <<
"\n";
582 if (nodes.size() < 2 or (end - start) < 2)
585 guess = (nodes[start] - nodes[start + 1]).length();
586 for (
size_t i = start; i < end; i++)
587 for (
size_t j = start; j < end; j++)
589 double val = nodes[i].dist(nodes[j]);
593 std::cout <<
"Check nodes are too close = "
594 << util::io::printStr<util::Point>({nodes[i],
597 std::cout <<
"Distance = " << val <<
", guess = " << guess <<
"\n";
610 for (
const auto& x : nodes) {
631 return 0.5 * (box.first - box.second).length();
635 &p1,
const std::vector<double>
bool isPointInsideBox(util::Point x, size_t dim, const std::pair< util::Point, util::Point > &box)
Returns true if point is inside box.
double pointDistanceLine(const util::Point &p, const std::pair< util::Point, util::Point > &line)
Compute distance between point and line.
double computeMeshSize(const std::vector< util::Point > &nodes)
Computes minimum distance between any two nodes.
bool isPointInsideEllipse(const util::Point &p, const util::Point ¢er, const std::vector< double > &radius_vec, unsigned int dim)
Returns true if point is inside the ellipsoid.
double pointDistancePlane(const util::Point &p, const std::pair< util::Point, util::Point > &plane)
Compute distance between point and plane.
bool areBoxesNear(const std::pair< util::Point, util::Point > &b1, const std::pair< util::Point, util::Point > &b2, const double &tol, size_t dim)
Checks if given two boxes are within given distance from each other.
bool isGreater(const double &a, const double &b)
Returns true if a > b.
bool doLinesIntersect(const std::pair< util::Point, util::Point > &line_1, const std::pair< util::Point, util::Point > &line_2)
Do lines intersect.
double distanceBetweenPlanes(const std::pair< util::Point, util::Point > &plane_1, const std::pair< util::Point, util::Point > &plane_2)
Compute distance between planes.
bool isPointInsideCuboid(util::Point x, util::Point x_lbb, util::Point x_rtf)
Checks if point is inside a cuboid.
std::vector< std::pair< util::Point, util::Point > > getEdges(size_t dim, const std::pair< util::Point, util::Point > &box)
Returns all corner points in the box.
double angle(util::Point a, util::Point b)
Computes angle between two vectors.
std::vector< util::Point > getCornerPoints(size_t dim, const std::pair< util::Point, util::Point > &box)
Returns all corner points in the box.
util::Point getPointOnLine(const util::Point &p1, const util::Point &p2, const double &s)
Returns point in line formed by points p1 and p2.
double distanceBetweenLines(const std::pair< util::Point, util::Point > &line_1, const std::pair< util::Point, util::Point > &line_2)
Compute distance between lines.
bool isLess(const double &a, const double &b)
Returns true if a < b.
std::pair< util::Point, util::Point > computeBBox(const std::vector< util::Point > &nodes)
Computes bounding box for vector nodes.
util::Point getCenter(size_t dim, const std::pair< util::Point, util::Point > &box)
Returns center point.
double distanceBetweenSegments(const std::pair< util::Point, util::Point > &line_1, const std::pair< util::Point, util::Point > &line_2)
Compute distance between lines.
double inscribedRadiusInBox(size_t dim, const std::pair< util::Point, util::Point > &box)
Computes the radius of biggest circle/sphere completely within the object.
bool isPointInsideAngledRectangle(util::Point x, double x_min, double x_max, double y_min, double y_max, double theta)
Checks if point is inside an angled rectangle.
bool isPointInsideCylinder(const util::Point &p, const double &length, const double &radius, const util::Point &axis)
Returns true if point is inside the cylinder.
double pointDistanceSegment(const util::Point &p, const std::pair< util::Point, util::Point > &line)
Compute distance between point and line.
std::pair< util::Point, util::Point > toPointBox(const std::vector< double > &p1, const std::vector< double > &p2)
Create box from two coordinate data.
std::vector< double > rotateCW2D(const std::vector< double > &x, const double &theta)
Rotates a vector in xy-plane in clockwise direction.
double triangleArea(const util::Point &x1, const util::Point &x2, const util::Point &x3)
Compute area of triangle.
double computeInscribedRadius(const std::pair< util::Point, util::Point > &box)
Computes maximum radius of circle/sphere within a given box.
bool isPointInsideRectangle(util::Point x, double x_min, double x_max, double y_min, double y_max)
Checks if point is inside a rectangle.
double circumscribedRadiusInBox(size_t dim, const std::pair< util::Point, util::Point > &box)
Computes the radius of smallest circle/sphere which can have the box inside.
A structure to represent 3d vectors.
double d_y
the y coordinate
double d_z
the z coordinate
double length() const
Computes the Euclidean length of the vector.
double lengthSq() const
Computes the Euclidean length of the vector.
double d_x
the x coordinate