CMSC23700 Common Code Library
Support code for CS23700 programming projects
Loading...
Searching...
No Matches
cs237-plane.hpp
Go to the documentation of this file.
1
8/* COPYRIGHT (c) 2023 John Reppy (http://cs.uchicago.edu/~jhr)
9 * All rights reserved.
10 */
11
12#ifndef _CS237_PLANE_HPP_
13#define _CS237_PLANE_HPP_
14
15#ifndef _CS237_HPP_
16#error "cs237-plane.hpp should not be included directly"
17#endif
18
19namespace cs237 {
20
21namespace __detail {
22
24template <typename T>
25struct Plane {
26 using vec3 = glm::vec<3, T, glm::defaultp>;
27 using vec4 = glm::vec<4, T, glm::defaultp>;
28
30
31 Plane () { }
32
37 Plane (vec3 n, T d) : _nd(vec4(n, T(d))) { }
38
43 {
44 vec3 norm = glm::normalize(n);
45 double d = -glm::dot(norm, p);
46 this->_nd = vec4(norm, d);
47 }
48
50 vec3 norm () const { return vec3(this->_nd); }
51
53 T dist () const { return this->_nd.w; }
54
58 T distanceToPt (vec3 const &p) const
59 {
60 return glm::dot(this->_nd, vec4(p, 1.0));
61 }
62
66 vec3 project (vec3 const &p) const
67 {
68 return p - this->distanceToPt(p) * this->norm();
69 }
70
72 std::string toString () const
73 {
74 return "Plane(" + glm::to_string(this->norm()) + ", "
75 + to_string(this->dist()) + ")";
76 }
77
78};
79
80/***** Output *****/
81
82template <typename T>
83inline std::ostream& operator<< (std::ostream& s, Plane<T> const &plane)
84{
85 return (s << plane.toString());
86}
87
88} // namespace __detail
89
94
95} // namespace cs237
96
97#endif // !_CS237_PLANE_HPP_
std::ostream & operator<<(std::ostream &s, __detail::AABB< T > const &bb)
print the axis-aligned bounding box to the output stream
Definition cs237-aabb.hpp:22
std::string to_string(Channels ch)
convert a Channels value to a printable string
Axis-Aligned Bounding Box parameterized over the scalar type.
Definition cs237-aabb.hpp:28
representation of a oriented 3D plane
Definition cs237-plane.hpp:25
glm::vec< 3, T, glm::defaultp > vec3
Definition cs237-plane.hpp:26
std::string toString() const
the string representation of a plane
Definition cs237-plane.hpp:72
glm::vec< 4, T, glm::defaultp > vec4
Definition cs237-plane.hpp:27
Plane()
unit normal and signed distance from origin
Definition cs237-plane.hpp:31
vec4 _nd
Definition cs237-plane.hpp:29
Plane(vec3 n, T d)
specify a plane as a unit-length normal vector and signed distance from origin
Definition cs237-plane.hpp:37
T dist() const
signed distance from origin to plane
Definition cs237-plane.hpp:53
T distanceToPt(vec3 const &p) const
signed distance from a point to plane
Definition cs237-plane.hpp:58
vec3 norm() const
get the plane normal vector
Definition cs237-plane.hpp:50
vec3 project(vec3 const &p) const
Definition cs237-plane.hpp:66
Plane(vec3 n, vec3 p)
specify a plane as a normal vector and point on the plane
Definition cs237-plane.hpp:42