Path: blob/devel/meshgen2d/src/include/coreGeometry.h
3203 views
#if !defined( MESH_GEOMETRY_H )1#define MESH_GEOMETRY_H2#include <math.h>3#include <float.h>4#include <algorithm>56#include "../../config.h"7#if defined(_NO_STD_MINMAX) || defined(WIN32)8#include "minmaxpatch.h"9#endif1011inline12int inCircle(double x[4], double y[4])13{14double x10 = x[1] - x[0];15double x20 = x[2] - x[0];16double x30 = x[3] - x[0];17double x21 = x[2] - x[1];18double x31 = x[3] - x[1];19double y10 = y[1] - y[0];20double y20 = y[2] - y[0];21double y30 = y[3] - y[0];22double y21 = y[2] - y[1];23double y31 = y[3] - y[1];2425double a1 = x10 * y20, b1 = x20 * y10;26double a2 = x10 * y30, b2 = x30 * y10;27double a3 = x20 * x21, b3 = y20 * y21;28double a4 = x30 * x31, b4 = y30 * y31;2930double t1 = a1 - b1;31double t2 = a2 - b2;32double t3 = a3 + b3;33double t4 = a4 + b4;34double t14 = t1 * t4, t23 = t2 * t3;35double det = t14 - t23;3637if (det >= 0.0)38return 1;3940// rounding error evaluation:41// sum: a = b + c -> a_error <= b_error + c_error + max(|a|, |b|, |c|) * DBL_EPSILON42// product: a = b * c -> a_error <= b_error * |c| + c_error * |b| + b_error * c_error + |a| * DBL_EPSILON43// the term (b_error * c_error) is very small and can therefore be ignored4445// maximum rounding error of coordinate differences46double maxcoord = std::max(std::max(std::max(fabs(x[0]), fabs(x[1])),47std::max(fabs(x[2]), fabs(x[3]))),48std::max(std::max(fabs(y[0]), fabs(y[1])),49std::max(fabs(y[2]), fabs(y[3]))));5051// maximum rounding error of t1..t452x10 = fabs(x10);53y10 = fabs(y10);5455x20 = fabs(x20); y20 = fabs(y20);56a1 = fabs(a1); b1 = fabs(b1); t1 = fabs(t1);5758double e1 = maxcoord * (x10 + x20 + y10 + y20) +59a1 + b1 + std::max(std::max(a1, b1), t1);6061x30 = fabs(x30); y30 = fabs(y30);62a2 = fabs(a2); b2 = fabs(b2); t2 = fabs(t2);6364double e2 = maxcoord * (x10 + x30 + y10 + y30) +65a2 + b2 + std::max(std::max(a2, b2), t2);6667x21 = fabs(x21); y21 = fabs(y21);68a3 = fabs(a3); b3 = fabs(b3); t3 = fabs(t3);6970double e3 = maxcoord * (x21 + x20 + y21 + y20) +71a3 + b3 + std::max(std::max(a3, b3), t3);7273x31 = fabs(x31); y31 = fabs(y31);74a4 = fabs(a4); b4 = fabs(b4); t4 = fabs(t4);7576double e4 = maxcoord * (x31 + x30 + y31 + y30) +77a4 + b4 + std::max(std::max(a4, b4), t4);7879t14 = fabs(t14); t23 = fabs(t23);80// maximum rounding error of det81double eps = e1 * t4 + e4 * t1 + e2 * t3 + e3 * t2 +82t14 + t23 + std::max(std::max(t14, t23), fabs(det));8384eps *= 2.0 * DBL_EPSILON;8586if(det < -eps)87return 0;8889return 1;90}9192inline93double sideDet(double x1,double y1,double x2,double y2,double x3,double y3, double *err = NULL)94{95double d23 = x2 * y3, d32 = x3 * y2;96double d13 = x1 * y3, d31 = x3 * y1;97double d12 = x1 * y2, d21 = x2 * y1;98double d1 = d23 - d32, d2 = d13 - d31, d3 = d12 - d21;99100if (err)101{102// maximum rounding errors of d1, d2 and d3103// product 1 product 2 difference104double e1 = fabs(d23) + fabs(d32) + std::max(std::max(fabs(d23), fabs(d32)), fabs(d1));105double e2 = fabs(d13) + fabs(d31) + std::max(std::max(fabs(d13), fabs(d31)), fabs(d2));106double e3 = fabs(d12) + fabs(d21) + std::max(std::max(fabs(d12), fabs(d21)), fabs(d3));107108// maximum rounding error of sideDet109*err = (e1 + e2 + e3 + std::max(std::max(fabs(d1), fabs(d2)), fabs(d1 - d2)) +110std::max(std::max(fabs(d1 - d2), fabs(d3)), d1 - d2 + d3)) * 2.0 * DBL_EPSILON;111}112113return d1 - d2 + d3;114}115116inline117int orientation(double x1,double y1,double x2,double y2,double x3,double y3)118{119double val;120double eps;121val = sideDet(x1,y1,x2,y2,x3,y3, &eps);122123if(val <= eps)124return 0;125return 1;126}127128inline129double distance(double x1, double y1, double x2, double y2)130{131double dx = x2 - x1;132double dy = y2 - y1;133return sqrt(dx*dx + dy*dy);134}135136inline137void vertexLocation(double p1x, double p1y, double p2x, double p2y,138double p3x, double p3y, double *vx, double *vy)139{140double a,b,c,d;141double r1,r2;142double det;143144a = 2 * (p2x - p1x);145b = 2 * (p2y - p1y);146c = 2 * (p3x - p2x);147d = 2 * (p3y - p2y);148r1 = -(-p2x * p2x + p1x * p1x - p2y * p2y + p1y * p1y);149r2 = -(-p3x * p3x + p2x * p2x - p3y * p3y + p2y * p2y);150det = a * d - b * c;151*vx = (1 / det) * (d * r1 - b * r2);152*vy = (1 / det) * (-c * r1 + a * r2);153}154155//#define MAP(a) (a)156//#define UNMAP(a) (a)157#define MAP(a) log(a)158#define UNMAP(a) exp(a)159160#endif /* MESH_GEOMETRY_H */161162163