Hermes
plane.h
1 /*
2  * Copyright (c) 2017 FilipeCN
3  *
4  * The MIT License (MIT)
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21  * THE SOFTWARE.
22  *
23  */
24 
25 #ifndef HERMES_GEOMETRY_PLANE_H
26 #define HERMES_GEOMETRY_PLANE_H
27 
28 #include <hermes/geometry/point.h>
29 #include <hermes/geometry/normal.h>
30 #include <hermes/geometry/surface.h>
31 #include <hermes/geometry/vector.h>
32 
33 #include <iostream>
34 
35 namespace hermes {
36 
39 class Plane {
40 public:
42  Plane() { offset = 0; }
48  normal = n;
49  offset = o;
50  }
51  Plane(normal3 n, point3 p) {
52  normal = n;
53  offset = dot(n, (vec3) p);
54  }
57  static Plane XY(bool invert_normal = false) {
58  return {normal3(0, 0, invert_normal ? -1 : 1), 0};
59  }
62  static Plane XZ(bool invert_normal = false) {
63  return {normal3(0, invert_normal ? -1 : 1, 0), 0};
64  }
67  static Plane YZ(bool invert_normal = false) {
68  return {
69  normal3(invert_normal ? -1 : 1, 0, 0), 0};
70  }
71 
72  [[nodiscard]] point3 closestPoint(const point3 &p) const {
73  float t = (dot(vec3(normal), vec3(p)) - offset) / vec3(normal).length2();
74  return p - t * vec3(normal);
75  }
80  [[nodiscard]] vec3 project(const vec3 &v) const { return hermes::project(v, normal); }
85  [[nodiscard]] vec3 reflect(const vec3 &v) const { return hermes::reflect(v, normal); }
86  [[nodiscard]] bool onNormalSide(const point3 &p) const {
87  return dot(vec3(normal), p - closestPoint(p)) >= 0;
88  }
89 
90  friend std::ostream &operator<<(std::ostream &os, const Plane &p) {
91  os << "[Plane] offset " << p.offset << " " << p.normal;
92  return os;
93  }
94 
95  normal3 normal;
96  real_t offset;
97 };
98 
102 public:
104  ImplicitPlane2D() { offset = 0.f; }
110  normal = n;
111  offset = o;
112  }
114  normal = n;
115  offset = dot(vec2(normal), vec2(p));
116  }
121  [[nodiscard]] vec2 project(const vec2 &v) const { return hermes::project(v, normal); }
126  [[nodiscard]] vec2 reflect(const vec2 &v) const { return hermes::reflect(v, normal); }
127  [[nodiscard]] point2 closestPoint(const point2 &p) const override {
128  real_t t = (dot(vec2(normal), vec2(p)) - offset) / vec2(normal).length2();
129  return p - t * vec2(normal);
130  }
131  [[nodiscard]] normal2 closestNormal(const point2 &p) const override {
132  if (dot(vec2(normal), vec2(p)) < 0.f)
133  return -normal;
134  return normal;
135  }
136  [[nodiscard]] bbox2 boundingBox() const override { return bbox2(); }
137  void closestIntersection(const Ray2 &r,
138  CurveRayIntersection *i) const override {
141  }
142  [[nodiscard]] double signedDistance(const point2 &p) const override {
143  return (dot(vec2(p), vec2(normal)) - offset) / vec2(normal).length();
144  }
145 
146  friend std::ostream &operator<<(std::ostream &os, const ImplicitPlane2D &p) {
147  os << "[Plane] offset " << p.offset << " " << p.normal;
148  return os;
149  }
150 
151  normal2 normal;
152  real_t offset;
153 };
154 
155 } // namespace hermes
156 
157 #endif
Definition: surface.h:59
Definition: plane.h:101
vec2 reflect(const vec2 &v) const
reflects v fron plane
Definition: plane.h:126
ImplicitPlane2D(normal2 n, real_t o)
Definition: plane.h:109
vec2 project(const vec2 &v) const
projects v on plane
Definition: plane.h:121
ImplicitPlane2D()
default_color constructor
Definition: plane.h:104
Definition: plane.h:39
Plane(normal3 n, real_t o)
Definition: plane.h:47
vec3 project(const vec3 &v) const
projects v on plane
Definition: plane.h:80
static Plane XY(bool invert_normal=false)
Definition: plane.h:57
vec3 reflect(const vec3 &v) const
reflects v fron plane
Definition: plane.h:85
static Plane XZ(bool invert_normal=false)
Definition: plane.h:62
Plane()
default_color constructor
Definition: plane.h:42
static Plane YZ(bool invert_normal=false)
Definition: plane.h:67
HERMES_DEVICE_CALLABLE T length2() const
Computes squared magnitude.
Definition: vector.h:129
HERMES_DEVICE_CALLABLE T length() const
Computes magnitude.
Definition: vector.h:132
HERMES_DEVICE_CALLABLE T length2() const
Computes vector squared magnitude.
Definition: vector.h:326
#define HERMES_UNUSED_VARIABLE(x)
Specifies that variable is not used in this scope.
Definition: debug.h:62
float real_t
default floating point type
Definition: defs.h:75
Geometric normal classes.
HERMES_DEVICE_CALLABLE Vector2< T > project(const Vector2< T > &v, const Normal2< T > &n)
projects v on the surface with normal n
Definition: normal.h:193
HERMES_DEVICE_CALLABLE T dot(const Normal3< T > &n, const Vector3< T > &v)
Computes dot product with vector.
Definition: normal.h:237
HERMES_DEVICE_CALLABLE Vector2< T > reflect(const Vector2< T > &a, const Normal2< T > &n)
reflects a on n
Definition: normal.h:185
Geometric point classes.
Geometric vector classes.