BRL-CAD
ray.h
Go to the documentation of this file.
1/* R A Y . H
2 * BRL-CAD
3 *
4 * Copyright (c) 2004-2023 United States Government as represented by
5 * the U.S. Army Research Laboratory.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * version 2.1 as published by the Free Software Foundation.
10 *
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this file; see the file named COPYING for more
18 * information.
19 */
20
21/** @addtogroup on_ray
22 *
23 * @brief
24 * Implement the concept of a geometry ray in terms of OpenNURBS
25 * data types.
26 *
27 */
28#ifndef BREP_RAY_H
29#define BREP_RAY_H
30
31#include "common.h"
32#include "brep/defines.h"
33
34#ifdef __cplusplus
35
36#include "bn/dvec.h"
37
38extern "C++" {
39
40/** @{ */
41/** @file brep/ray.h */
42
43__BEGIN_DECLS
44
45class plane_ray {
46public:
49
52};
53
54/**
55 * These definitions were added to opennurbs_curve.h - they are
56 * extensions of openNURBS, so add them to libbrep instead.
57 */
58class ON_Ray {
59public:
60 ON_3dPoint m_origin;
61 ON_3dVector m_dir;
62
63 ON_Ray(ON_3dPoint &origin, ON_3dVector &dir);
64 ON_Ray(ON_2dPoint &origin, ON_2dVector &dir);
65 ON_Ray(const ON_Ray &r);
66
67 ON_Ray &operator=(const ON_Ray &r);
68 ON_3dPoint PointAt(double t) const;
69 double DistanceTo(const ON_3dPoint &pt, double *out_t = NULL) const;
70
71 /**
72 * * Intersect two 2d Rays
73 * * @param v [in] other ray to intersect with
74 * * @param isect [out] point of intersection
75 * * @return true if single point of intersection is found
76 * */
77 bool IntersectRay(const ON_Ray &v, ON_2dPoint &isect) const;
78};
79
80inline
81ON_Ray::ON_Ray(ON_3dPoint &origin, ON_3dVector &dir)
82 : m_origin(origin), m_dir(dir)
83{
84}
85
86inline
87ON_Ray::ON_Ray(ON_2dPoint &origin, ON_2dVector &dir)
88 : m_origin(origin), m_dir(dir)
89{
90}
91
92inline
94 : m_origin(r.m_origin), m_dir(r.m_dir)
95{
96}
97
98inline
99ON_Ray &
101{
102 m_origin = r.m_origin;
103 m_dir = r.m_dir;
104 return *this;
105}
106
107 inline
108 ON_3dPoint
109 ON_Ray::PointAt(double t) const
110 {
111 return m_origin + m_dir * t;
112 }
113
114
115inline
116double
117ON_Ray::DistanceTo(const ON_3dPoint &pt, double *out_t /* = NULL */) const
118{
119 ON_3dVector w = pt - m_origin;
120 double c1 = w * m_dir;
121 if (c1 <= 0) {
122 return pt.DistanceTo(m_origin);
123 }
124 double c2 = m_dir * m_dir;
125 double b = c1 / c2;
126 ON_3dPoint p = m_dir * b + m_origin;
127 if (out_t != NULL) {
128 *out_t = b;
129 }
130 return p.DistanceTo(pt);
131}
132
133inline
134bool
135ON_Ray::IntersectRay(const ON_Ray &v, ON_2dPoint &isect) const
136{
137 double uxv, q_pxv;
138 /* consider parallel and collinear cases */
139 if (ZERO(uxv = V2CROSS(m_dir, v.m_dir)) ||
140 (ZERO(q_pxv = V2CROSS(v.m_origin - m_origin, v.m_dir))))
141 {
142 return false;
143 }
144 isect = PointAt(q_pxv / uxv);
145 return true;
146}
147
148BREP_EXPORT int brep_get_plane_ray(const ON_Ray &r, plane_ray &pr);
149BREP_EXPORT void brep_r(const ON_Surface* surf, const plane_ray& pr, pt2d_t uv, ON_3dPoint& pt, ON_3dVector& su, ON_3dVector& sv, pt2d_t R);
150BREP_EXPORT void brep_newton_iterate(const plane_ray& pr, pt2d_t R, const ON_3dVector& su, const ON_3dVector& sv, pt2d_t uv, pt2d_t out_uv);
151BREP_EXPORT void utah_ray_planes(const ON_Ray &r, ON_3dVector &p1, double &p1d, ON_3dVector &p2, double &p2d);
152
153__END_DECLS
154
155} /* extern C++ */
156#endif
157
158/** @} */
159
160#endif /* BREP_RAY_H */
161
162/*
163 * Local Variables:
164 * mode: C
165 * tab-width: 8
166 * indent-tabs-mode: t
167 * c-file-style: "stroustrup"
168 * End:
169 * ex: shiftwidth=4 tabstop=8
170 */
Definition: ray.h:58
ON_3dVector m_dir
Definition: ray.h:61
ON_3dPoint m_origin
Definition: ray.h:60
Definition: ray.h:45
vect_t n2
Definition: ray.h:50
vect_t n1
Definition: ray.h:47
fastf_t d1
Definition: ray.h:48
fastf_t d2
Definition: ray.h:51
Header file for the BRL-CAD common definitions.
void brep_newton_iterate(const plane_ray &pr, pt2d_t R, const ON_3dVector &su, const ON_3dVector &sv, pt2d_t uv, pt2d_t out_uv)
void utah_ray_planes(const ON_Ray &r, ON_3dVector &p1, double &p1d, ON_3dVector &p2, double &p2d)
ON_3dPoint PointAt(double t) const
Definition: ray.h:109
ON_Ray(ON_3dPoint &origin, ON_3dVector &dir)
Definition: ray.h:81
double DistanceTo(const ON_3dPoint &pt, double *out_t=NULL) const
Definition: ray.h:117
ON_Ray & operator=(const ON_Ray &r)
Definition: ray.h:100
int brep_get_plane_ray(const ON_Ray &r, plane_ray &pr)
bool IntersectRay(const ON_Ray &v, ON_2dPoint &isect) const
Definition: ray.h:135
void brep_r(const ON_Surface *surf, const plane_ray &pr, pt2d_t uv, ON_3dPoint &pt, ON_3dVector &su, ON_3dVector &sv, pt2d_t R)
fastf_t vect_t[ELEMENTS_PER_VECT]
3-tuple vector
Definition: vmath.h:345
double fastf_t
fastest 64-bit (or larger) floating point type
Definition: vmath.h:330
#define V2CROSS(a, b)
Definition: vmath.h:1461
#define ZERO(_a)
Definition: vmath.h:494