ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
RayAndBox.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include "CVGeom.h"
11 
13 template <typename T>
14 struct Ray {
15  Ray(const Vector3Tpl<T>& rayAxis, const Vector3Tpl<T>& rayOrigin)
16  : dir(rayAxis), origin(rayOrigin), invDir(0, 0, 0), sign(0, 0, 0) {
17  dir.normalize();
19  1 / rayAxis.x, 1 / rayAxis.y,
20  1 / rayAxis.z); // +/-infinity is acceptable here because we
21  // are mainly interested in the sign
22  sign = Tuple3i(invDir.x < 0, invDir.y < 0, invDir.z < 0);
23  }
24 
25  inline double radialSquareDistance(const Vector3Tpl<T>& P) const {
26  Vector3Tpl<T> OP = P - origin;
27  return OP.cross(dir).norm2d();
28  }
29 
30  inline double squareDistanceToOrigin(const Vector3Tpl<T>& P) const {
31  Vector3Tpl<T> OP = P - origin;
32  return OP.norm2d();
33  }
34 
35  inline void squareDistances(const Vector3Tpl<T>& P,
36  double& radial,
37  double& toOrigin) const {
38  Vector3Tpl<T> OP = P - origin;
39  radial = OP.cross(dir).norm2d();
40  toOrigin = OP.norm2d();
41  }
42 
46 };
47 
49 template <typename T>
50 struct AABB {
51  AABB(const Vector3Tpl<T>& minCorner, const Vector3Tpl<T>& maxCorner) {
52  assert(minCorner.x <= maxCorner.x);
53  assert(minCorner.y <= maxCorner.y);
54  assert(minCorner.z <= maxCorner.z);
55 
56  corners[0] = minCorner;
57  corners[1] = maxCorner;
58  }
59 
60  /*
61  * Ray-box intersection using IEEE numerical properties to ensure that the
62  * test is both robust and efficient, as described in:
63  *
64  * Amy Williams, Steve Barrus, R. Keith Morley, and Peter Shirley
65  * "An Efficient and Robust Ray-Box Intersection Algorithm"
66  * Journal of graphics tools, 10(1):49-54, 2005
67  *
68  */
69  bool intersects(const Ray<T>& r, T* t0 = 0, T* t1 = 0) const {
70  T tmin = (corners[r.sign.x].x - r.origin.x) * r.invDir.x;
71  T tmax = (corners[1 - r.sign.x].x - r.origin.x) * r.invDir.x;
72  T tymin = (corners[r.sign.y].y - r.origin.y) * r.invDir.y;
73  T tymax = (corners[1 - r.sign.y].y - r.origin.y) * r.invDir.y;
74 
75  if (tmin > tymax || tymin > tmax) return false;
76  if (tymin > tmin) tmin = tymin;
77  if (tymax < tmax) tmax = tymax;
78 
79  T tzmin = (corners[r.sign.z].z - r.origin.z) * r.invDir.z;
80  T tzmax = (corners[1 - r.sign.z].z - r.origin.z) * r.invDir.z;
81 
82  if (tmin > tzmax || tzmin > tmax) return false;
83  if (tzmin > tmin) tmin = tzmin;
84  if (tzmax < tmax) tmax = tzmax;
85 
86  if (t0) *t0 = tmin;
87  if (t1) *t1 = tmax;
88 
89  return true;
90  }
91 
93 };
Tuple3Tpl< int > Tuple3i
Tuple of 3 int values.
Definition: CVGeom.h:217
Type y
Definition: CVGeom.h:137
Type x
Definition: CVGeom.h:137
Type z
Definition: CVGeom.h:137
void normalize()
Sets vector norm to unity.
Definition: CVGeom.h:428
double norm2d() const
Returns vector square norm (forces double precision output)
Definition: CVGeom.h:419
Vector3Tpl cross(const Vector3Tpl &v) const
Cross product.
Definition: CVGeom.h:412
Simple axis aligned box structure.
Definition: RayAndBox.h:50
AABB(const Vector3Tpl< T > &minCorner, const Vector3Tpl< T > &maxCorner)
Definition: RayAndBox.h:51
Vector3Tpl< T > corners[2]
Definition: RayAndBox.h:92
bool intersects(const Ray< T > &r, T *t0=0, T *t1=0) const
Definition: RayAndBox.h:69
Simple Ray structure.
Definition: RayAndBox.h:14
Vector3Tpl< T > invDir
Definition: RayAndBox.h:44
Tuple3i sign
Definition: RayAndBox.h:45
Vector3Tpl< T > dir
Definition: RayAndBox.h:43
Ray(const Vector3Tpl< T > &rayAxis, const Vector3Tpl< T > &rayOrigin)
Definition: RayAndBox.h:15
double squareDistanceToOrigin(const Vector3Tpl< T > &P) const
Definition: RayAndBox.h:30
Vector3Tpl< T > origin
Definition: RayAndBox.h:43
double radialSquareDistance(const Vector3Tpl< T > &P) const
Definition: RayAndBox.h:25
void squareDistances(const Vector3Tpl< T > &P, double &radial, double &toOrigin) const
Definition: RayAndBox.h:35