Hermes
sphere.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_SPHERE_H
26 #define HERMES_GEOMETRY_SPHERE_H
27 
28 #include <hermes/geometry/parametric_surface.h>
29 #include <hermes/geometry/shape.h>
31 
32 namespace hermes {
33 
34 class Circle : public Shape {
35 public:
36  Circle() { r = 0; }
37  Circle(point2 center, real_t radius) : c(center), r(radius) {
38  this->type = ShapeType::SPHERE;
39  }
40  virtual ~Circle() {}
41 
42  point2 c;
43  real_t r;
44 };
45 
46 class ParametricCircle final : public Circle, public ParametricCurveInterface {
47 public:
48  ParametricCircle() { r = 0.f; }
49  ParametricCircle(point2 center, real_t radius) : Circle(center, radius) {}
54  point2 operator()(real_t t) const override {
55  real_t angle = t * Constants::two_pi;
56  return this->c + this->r * vec2(cosf(angle), sinf(angle));
57  }
58 };
59 
60 class ImplicitCircle final : public ImplicitCurveInterface {
61 public:
62  ImplicitCircle() : r(0.f) {}
63 
64  ImplicitCircle(point2 center, real_t radius) : c(center), r(radius) {}
65  ~ImplicitCircle() {}
66 
67  point2 closestPoint(const point2 &p) const override {
68  return c + r * vec2(this->closestNormal(p));
69  }
70  normal2 closestNormal(const point2 &p) const override {
71  if (c == p)
72  return normal2(1, 0);
73  vec2 n = normalize(c - p);
74  return normal2(n.x, n.y);
75  }
76  bbox2 boundingBox() const override { return bbox2(c - r, c + r); }
77  void closestIntersection(const Ray2 &r,
78  CurveRayIntersection *i) const override {
81  }
82  double signedDistance(const point2 &p) const override {
83  return distance(c, p) - r;
84  }
85 
86  point2 c;
87  real_t r;
88 };
89 
90 class Sphere : public SurfaceInterface {
91 public:
92  Sphere() : r(0.f) {}
93 
94  Sphere(point3 center, real_t radius) : c(center), r(radius) {}
95  virtual ~Sphere() {}
96 
97  point3 closestPoint(const point3 &p) const override {
98  return c + r * vec3(this->closestNormal(p));
99  }
100  normal3 closestNormal(const point3 &p) const override {
101  if (c == p)
102  return normal3(1, 0, 0);
103  vec3 n = normalize(c - p);
104  return normal3(n.x, n.y, n.z);
105  }
106  bbox3 boundingBox() const override { return bbox3(c - r, c + r); }
107  void closestIntersection(const Ray3 &r,
108  SurfaceRayIntersection *i) const override {
111  }
112  point3 c;
113  real_t r;
114 };
115 
117 public:
118  ImplicitSphere() : r(0.f) {}
119 
120  ImplicitSphere(point3 center, real_t radius) : c(center), r(radius) {}
121  ~ImplicitSphere() {}
122 
123  point3 closestPoint(const point3 &p) const override {
124  return c + r * vec3(this->closestNormal(p));
125  }
126  normal3 closestNormal(const point3 &p) const override {
127  if (c == p)
128  return normal3(1, 0, 0);
129  vec3 n = normalize(c - p);
130  return normal3(n.x, n.y, n.z);
131  }
132  bbox3 boundingBox() const override { return bbox3(c - r, c + r); }
133  void closestIntersection(const Ray3 &r,
134  SurfaceRayIntersection *i) const override {
137  }
138  double signedDistance(const point3 &p) const override {
139  return distance(c, p) - r;
140  }
141 
142  point3 c;
143  real_t r;
144 };
145 
146 inline bbox2 compute_bbox(const Circle &po, const Transform2 *t = nullptr) {
147  bbox2 b;
148  hermes::point2 center = po.c;
149  if (t != nullptr) {
150  b = make_union(b, (*t)(center + hermes::vec2(po.r, 0)));
151  b = make_union(b, (*t)(center + hermes::vec2(-po.r, 0)));
152  b = make_union(b, (*t)(center + hermes::vec2(0, po.r)));
153  b = make_union(b, (*t)(center + hermes::vec2(0, -po.r)));
154  } else {
155  b = make_union(b, center + hermes::vec2(po.r, 0));
156  b = make_union(b, center + hermes::vec2(-po.r, 0));
157  b = make_union(b, center + hermes::vec2(0, po.r));
158  b = make_union(b, center + hermes::vec2(0, -po.r));
159  }
160  return b;
161 }
162 
163 } // namespace hermes
164 
165 #endif
Definition: bbox.h:84
Definition: bbox.h:187
Definition: sphere.h:34
Definition: sphere.h:60
Definition: surface.h:59
Definition: sphere.h:116
Definition: surface.h:92
Definition: sphere.h:46
point2 operator()(real_t t) const override
Definition: sphere.h:54
Definition: parametric_surface.h:34
Definition: ray.h:39
Definition: ray.h:70
Definition: shape.h:32
Definition: sphere.h:90
Definition: surface.h:73
Represents a 2-dimensional transformation.
Definition: transform.h:90
T x
0-th component
Definition: vector.h:155
T y
1-th component
Definition: vector.h:156
T x
0-th component
Definition: vector.h:415
T z
2-th component
Definition: vector.h:417
T y
1-th component
Definition: vector.h:416
#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
HERMES_DEVICE_CALLABLE Normal3< T > normalize(const Normal3< T > &normal)
Computes normalized copy.
Definition: normal.h:201
HERMES_DEVICE_CALLABLE real_t distance(const Point2< T > &a, const Point2< T > &b)
Computes the Euclidean distance between two points.
Definition: point.h:295
Definition: surface.h:33
Definition: surface.h:66
Geometric transform classes.