DOLFIN
DOLFIN C++ interface
GeometryTools.h
1 // Copyright (C) 2017 Anders Logg
2 //
3 // This file is part of DOLFIN.
4 //
5 // DOLFIN is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // DOLFIN is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // First added: 2017-02-11
19 // Last changed: 2017-02-17
20 
21 #ifndef __GEOMETRY_TOOLS_H
22 #define __GEOMETRY_TOOLS_H
23 
24 #include "predicates.h"
25 #include "Point.h"
26 
27 namespace dolfin
28 {
29 
31 
33  {
34  public:
35 
37  static inline Point cross_product(const Point& a, const Point& b, const Point& c)
38  {
39  // See Shewchuk Lecture Notes on Geometric Robustness
40  double ayz[2] = {a.y(), a.z()};
41  double byz[2] = {b.y(), b.z()};
42  double cyz[2] = {c.y(), c.z()};
43  double azx[2] = {a.z(), a.x()};
44  double bzx[2] = {b.z(), b.x()};
45  double czx[2] = {c.z(), c.x()};
46  double axy[2] = {a.x(), a.y()};
47  double bxy[2] = {b.x(), b.y()};
48  double cxy[2] = {c.x(), c.y()};
49  return Point (_orient2d(ayz, byz, cyz),
50  _orient2d(azx, bzx, czx),
51  _orient2d(axy, bxy, cxy));
52  }
53 
55  inline double determinant(const Point& ab, const Point& dc, const Point& ec)
56  {
57  const double a = ab.x(), b = ab.y(), c = ab.z();
58  const double d = dc.x(), e = dc.y(), f = dc.z();
59  const double g = ec.x(), h = ec.y(), i = ec.z();
60  return a * (e * i - f * h)
61  + b * (f * g - d * i)
62  + c * (d * h - e * g);
63  }
64 
66  static inline std::size_t major_axis_2d(const Point& v)
67  {
68  return (std::abs(v.x()) >= std::abs(v.y()) ? 0 : 1);
69  }
70 
72  static inline std::size_t major_axis_3d(const Point& v)
73  {
74  const double vx = std::abs(v.x());
75  const double vy = std::abs(v.y());
76  const double vz = std::abs(v.z());
77  if (vx >= vy && vx >= vz)
78  return 0;
79  if (vy >= vz)
80  return 1;
81  return 2;
82  }
83 
85  static inline double project_to_axis_2d(const Point& p, std::size_t axis)
86  {
87  dolfin_assert(axis <= 1);
88  return p[axis];
89  }
90 
92  static inline Point project_to_plane_3d(const Point& p, std::size_t axis)
93  {
94  dolfin_assert(axis <= 2);
95  switch (axis)
96  {
97  case 0: return Point(p.y(), p.z());
98  case 1: return Point(p.x(), p.z());
99  case 2: return Point(p.x(), p.y());
100  }
101  return p;
102  }
103 
105  static inline bool contains(double a, double b, double x)
106  {
107  return a <= x and x <= b;
108  }
109 
111  static inline bool contains_strict(double a, double b, double x)
112  {
113  return a < x and x < b;
114  }
115 
116  };
117 
118 }
119 
120 #endif
This class provides useful tools (functions) for computational geometry.
Definition: GeometryTools.h:32
static double project_to_axis_2d(const Point &p, std::size_t axis)
Project point to axis (2D)
Definition: GeometryTools.h:85
double y() const
Definition: Point.h:118
static std::size_t major_axis_3d(const Point &v)
Compute major (largest) axis of vector (3D)
Definition: GeometryTools.h:72
double z() const
Definition: Point.h:125
Definition: adapt.h:29
Definition: Point.h:40
double _orient2d(const double *a, const double *b, const double *c)
double x() const
Definition: Point.h:111
static Point project_to_plane_3d(const Point &p, std::size_t axis)
Project point to plane (3D)
Definition: GeometryTools.h:92
static bool contains_strict(double a, double b, double x)
Check whether x in (a, b)
Definition: GeometryTools.h:111
static Point cross_product(const Point &a, const Point &b, const Point &c)
Compute numerically stable cross product (a - c) x (b - c)
Definition: GeometryTools.h:37
static bool contains(double a, double b, double x)
Check whether x in [a, b].
Definition: GeometryTools.h:105
static std::size_t major_axis_2d(const Point &v)
Compute major (largest) axis of vector (2D)
Definition: GeometryTools.h:66
double determinant(const Point &ab, const Point &dc, const Point &ec)
Compute determinant of 3 x 3 matrix defined by vectors, ab, dc, ec.
Definition: GeometryTools.h:55