MorphoGraphX
Geometry.hpp
Go to the documentation of this file.
1 //
2 // This file is part of MorphoGraphX - http://www.MorphoGraphX.org
3 // Copyright (C) 2012-2015 Richard S. Smith and collaborators.
4 //
5 // If you use MorphoGraphX in your work, please cite:
6 // http://dx.doi.org/10.7554/eLife.05864
7 //
8 // MorphoGraphX is free software, and is licensed under under the terms of the
9 // GNU General (GPL) Public License version 2.0, http://www.gnu.org/licenses.
10 //
11 #ifndef GEOMETRY_H
12 #define GEOMETRY_H
13 
20 #include <Config.hpp>
21 
22 #include <Util.hpp>
23 #include <Vector.hpp>
24 #include <Matrix.hpp>
25 #include <BoundingBox.hpp>
26 
27 #include <cmath>
28 #include <cuda/CudaGlobal.hpp>
29 #include <float.h>
30 #include <math.h>
31 
32 namespace mgx {
33 
40  using util::min;
41  using util::max;
42  using util::trim;
43  typedef unsigned char ubyte;
44  typedef unsigned int uint;
45  typedef unsigned short ushort;
46  typedef unsigned long ulong;
47 
48  typedef util::Vector<2, float> Point2f;
49  typedef util::Vector<3, float> Point3f;
50  typedef util::Vector<4, float> Point4f;
51  typedef util::Vector<5, float> Point5f;
52  typedef util::Vector<6, float> Point6f;
53  typedef util::Vector<12, float> Point12f;
54 
55  typedef util::Vector<2, double> Point2d;
56  typedef util::Vector<3, double> Point3d;
57  typedef util::Vector<4, double> Point4d;
58  typedef util::Vector<5, double> Point5d;
59  typedef util::Vector<6, double> Point6d;
60  typedef util::Vector<16, double> Point16d;
61 
62  typedef util::Vector<2, int> Point2i;
63  typedef util::Vector<3, int> Point3i;
64  typedef util::Vector<4, int> Point4i;
65  typedef util::Vector<5, int> Point5i;
66  typedef util::Vector<6, int> Point6i;
67 
68  typedef util::Vector<2, uint> Point2u;
69  typedef util::Vector<3, uint> Point3u;
70  typedef util::Vector<4, uint> Point4u;
71  typedef util::Vector<5, uint> Point5u;
72  typedef util::Vector<6, uint> Point6u;
73 
74  typedef util::Vector<2, size_t> Point2s;
75  typedef util::Vector<3, size_t> Point3s;
76  typedef util::Vector<4, size_t> Point4s;
77  typedef util::Vector<5, size_t> Point5s;
78  typedef util::Vector<6, size_t> Point6s;
79 
80  typedef util::Vector<3, ushort> Point3us;
81 
82  typedef util::Matrix<2, 2, float> Matrix2f;
83  typedef util::Matrix<3, 3, float> Matrix3f;
84  typedef util::Matrix<4, 4, float> Matrix4f;
85 
86  typedef util::Matrix<2, 2, double> Matrix2d;
87  typedef util::Matrix<3, 3, double> Matrix3d;
88  typedef util::Matrix<4, 4, double> Matrix4d;
89 
90  typedef util::BoundingBox<3, uint> BoundingBox3u;
91  typedef util::BoundingBox<3, int> BoundingBox3i;
92  typedef util::BoundingBox<3, float> BoundingBox3f;
93 
94  CU_HOST_DEVICE
95  Point3u toVoxelsCeil(const Point3f& p, const Point3f& step)
96  {
97  Point3u r(0, 0, 0);
98  if(p.x() > 0 and step.x() > 0)
99  r.x() = uint(ceil(p.x() / step.x()));
100  if(p.y() > 0 and step.y() > 0)
101  r.y() = uint(ceil(p.y() / step.y()));
102  if(p.z() > 0 and step.z() > 0)
103  r.z() = uint(ceil(p.z() / step.z()));
104  return r;
105  }
106 
107  // Multiple openGL 4D matrix by 3D point and return 3D point
108  template <typename T>
109  CU_HOST_DEVICE util::Vector<3, T> multMatrix4Point3(const util::Matrix<4, 4, T>& m, const util::Vector<3, T>& p)
110  {
111  util::Vector<4, T> t(p.x(), p.y(), p.z(), T(1.0));
112  t = m * t;
113  util::Vector<3, T> res(t.x() / t.t(), t.y() / t.t(), t.z() / t.t());
114  return res;
115  }
116 
117  template <typename T> T CU_HOST_DEVICE interpolate(const T a, const T b, const T s)
118  {
119  T t = trim(s, (T)0.0, (T)1.0);
120  return ((1.0 - t) * a + t * b);
121  }
122 
123  // CU_HOST_DEVICE
124  // Point3f getEulerAngles(Matrix4d &m)
125  //{
126  // Point3f r(0,0,0);
127  // if(fabs(m(1,0)) > .1)
128  // r.x() = atan2(m(0,2), m(2,2)) / M_PI * 180;
129  // else
130  // r.x() = atan2(-m(2,0), m(0,0)) / M_PI * 180;
131  // r.y() = atan2(-m(1,2), m(2,2)) / M_PI * 180;
132  // r.z() = asin(m(1,0)) / M_PI * 180;
133  // return(r);
134  //}
135 
136  // Plane-Line Intersection, 0 <= s <= 1, on line, s >= 0 ray intersect
137  CU_HOST_DEVICE
138  bool planeLineIntersect(Point3f p, Point3f n, Point3f u1, Point3f u2, float& s, Point3f& u)
139  {
140  n.normalize();
141  Point3f u1u2 = u2 - u1;
142  float t = n * u1u2;
143  // Check if parallel
144  if(t == 0)
145  return (false);
146  s = n * (p - u1) / t;
147  u = u1 + s * u1u2;
148  return (true);
149  }
150 
151  // Triangle area
152  CU_HOST_DEVICE
153  float triangleArea(Point3f a, Point3f b, Point3f c) {
154  return ((a - b).cross(a - c).norm() / 2.0);
155  }
156 
157  // Signed volume of a tetrahedron
158  CU_HOST_DEVICE
159  float signedTetraVolume(Point3f a, Point3f b, Point3f c)
160  {
161  return ((-c.x() * b.y() * a.z() + b.x() * c.y() * a.z() + c.x() * a.y() * b.z() - a.x() * c.y() * b.z()
162  - b.x() * a.y() * c.z() + a.x() * b.y() * c.z()) / 6.0f);
163  }
164 
165  // Get (a set of) basis vectors from a plane
166  CU_HOST_DEVICE
167  void getBasisFromPlane(const Point3f& nrml, Point3f& x, Point3f& y, Point3f& z)
168  {
169  Point3f n = nrml;
170  n.normalize();
171  x = Point3f(1, 0, 0);
172  y = Point3f(0, 1, 0);
173  z = Point3f(0, 0, 1);
174  if(z.cross(n).norm() != 0) {
175  y = z.cross(n);
176  y.normalize();
177  x = y.cross(n);
178  x.normalize();
179  } else
180  x = n * z * x;
181  z = n;
182  }
183 
184  // Intersect a ray with a 3D triangle
185  // Input: ray r1,r2 triangle t0,t1,t2
186  // Output: intp = intersection point (when it exists)
187  // Return: -1 = triangle is degenerate (a segment or point)
188  // 0 = disjoint (no intersect)
189  // 1 = intersect in unique point I1
190  // 2 = are in the same plane
191  CU_HOST_DEVICE
192  int rayTriangleIntersect(const Point3f& r0, const Point3f& r1, const Point3f& t0, const Point3f& t1, const Point3f& t2,
193  Point3f& intp)
194  {
195  // get triangle edge vectors and plane normal
196  Point3f u = t1 - t0;
197  Point3f v = t2 - t0;
198  Point3f n = u.cross(v);
199  if(n.x() == 0 && n.y() == 0 && n.z() == 0) // triangle is degenerate
200  return -1;
201 
202  Point3f dir = r1 - r0; // ray direction vector
203  Point3f w0 = r0 - t0;
204  float a = -n * w0;
205  float b = n * dir;
206  if(b == 0) { // ray is parallel to triangle plane
207  if(a == 0) // ray lies in triangle plane
208  return 2;
209  else
210  return 0; // ray disjoint from plane
211  }
212 
213  // get intersect point of ray with triangle plane
214  float r = a / b;
215  if(r < 0.0f) // ray goes away from triangle
216  return 0; // => no intersect
217  // for a segment, also test if (r > 1.0) => no intersect
218 
219  intp = r0 + r * dir; // intersect point of ray and plane
220 
221  // is intersect point inside the triangle?
222  float uu, uv, vv, wu, wv, d;
223  uu = u * u;
224  uv = u * v;
225  vv = v * v;
226  Point3f w = intp - t0;
227  wu = w * u;
228  wv = w * v;
229  d = uv * uv - uu * vv;
230 
231  // get and test parametric coords
232  float s, t;
233  s = (uv * wv - vv * wu) / d;
234  if(s < 0.0f || s > 1.0f) // outside
235  return 0;
236  t = (uv * wu - uu * wv) / d;
237  if(t < 0.0f || (s + t) > 1.0f) // outside
238  return 0;
239 
240  return 1; // in
241  }
242 
243  // Return distance from a point to a line (or segment)
244  CU_HOST_DEVICE
245  float distLinePoint(Point3f v1, Point3f v2, Point3f p, bool segment)
246  {
247  v2 -= v1;
248  float n = norm(v2);
249  // Points are the same
250  if(n == 0)
251  return norm(p - v1);
252 
253  Point3f vn = v2 / n;
254  p -= v1;
255  Point3f q = vn * (p * vn);
256  float dist = norm(p - q);
257  if(!segment)
258  return dist;
259 
260  // Projected point on line segment
261  float qn = p * vn;
262  if(qn >= 0 and qn <= n)
263  return dist;
264 
265  // Otherwise pick closest endpoint
266  dist = norm(p);
267  float d = norm(p - v2);
268  if(d < dist)
269  dist = d;
270 
271  return dist;
272  }
273 
274 }
275 #endif
This namespace contains all the API of MorphoGraphX.
Definition: Assert.hpp:26
Common definitions and utilities This file is shared by cuda, do not include headers that nvcc can&#39;t ...
Defines the Matrix class template This file is shared by cuda, do not include headers that nvcc can&#39;t...
Defines the Vector class template This file is shared by cuda, do not include headers that nvcc can&#39;t...