RobinTrace
shape_part.h
Go to the documentation of this file.
1 
2 #ifndef SHAPE_PART_H
3 #define SHAPE_PART_H
4 
5 #include "lpart/lpart.h"
6 #include "poaky.h"
7 
10 template <class T>
11 class shape_reflect_part: public lpart {
12  public:
16  T shp;
17 
21  shape_reflect_part (T _shp) {
22  shp = _shp;
23  };
24 
34  virtual void apply(bun &b) override {
35  for (auto &r : b.rays) {
36  if (not(r.is_valid())) {return;}
37  shp.intersect(r);
38  if (not(r.is_valid())) {return;}
39  auto N = shp.normal(r);
40  if (not(r.is_valid())) {return;}
41  reflect(r, N);
42  }
43  };
44 
45  virtual void intersect (bun &b) override {
46  for (auto &r : b.rays) {
47  if (not(r.is_valid())) { return; }
48  shp.intersect(r);
49  }
50  };
51 
52  virtual std::unique_ptr<lpart> clone () const override {
53  return std::make_unique<shape_reflect_part>(*this);
54  };
55 
56  virtual bool is_transfer () override {
57  return false;
58  };
59  virtual bool is_renderable () override {
60  return true;
61  };
62 
63  private:
64  virtual std::string print_str () const override {
65  std::stringstream ss;
66  ss << "shape_reflect_part<shape: " << shp << ">";
67  return ss.str();
68  };
69 
70  static_assert(std::is_base_of<shape, T>::value,
71  "T must be derived from shape.");
72 };
73 
76 template <class T>
77 class shape_refract_part: public lpart {
78  public:
82  T shp;
84  double nr;
85 
91  shape_refract_part (T _shp, double _nr) {
92  shp = _shp;
93  nr = _nr;
94  };
95 
105  virtual void apply(bun &b) override {
106  for (auto &r : b.rays) {
107  if (not(r.is_valid())) {return;}
108  shp.intersect(r);
109  if (not(r.is_valid())) {return;}
110  auto N = shp.normal(r);
111  if (not(r.is_valid())) {return;}
112  refract(r, N, nr);
113  }
114  };
115 
116  virtual void intersect (bun &b) override {
117  for (auto &r : b.rays) {
118  if (not(r.is_valid())) { return; }
119  shp.intersect(r);
120  }
121  };
122 
123  virtual std::unique_ptr<lpart> clone () const override {
124  return std::make_unique<shape_refract_part>(*this);
125  };
126 
127  virtual bool is_transfer () override {
128  return false;
129  };
130  virtual bool is_renderable () override {
131  return true;
132  };
133 
134  private:
135  virtual std::string print_str () const override {
136  std::stringstream ss;
137  ss << "shape_refract_part<shape: " << shp << "," << std::endl
138  << "nr: " << nr << ">";
139  return ss.str();
140  };
141 
142  static_assert(std::is_base_of<shape, T>::value,
143  "T must be derived from shape.");
144 };
145 
146 #endif // SHAPE_PART_H
Class for ray bundles.
Definition: bun.h:13
std::vector< ray > rays
Vector of rays.
Definition: bun.h:16
Virtual class for elementary optical parts which can be applied to ray bundles.
Definition: lpart.h:12
Class template implementing the specialized lparts for reflective shape parts.
Definition: shape_part.h:11
virtual void apply(bun &b) override
Apply reflective shape lpart operations to the ray bundle b.
Definition: shape_part.h:34
virtual bool is_transfer() override
Is the part a transfer?
Definition: shape_part.h:56
T shp
The shape of the part.
Definition: shape_part.h:16
shape_reflect_part(T _shp)
Constructor with initialization to the shape _shp.
Definition: shape_part.h:21
virtual std::unique_ptr< lpart > clone() const override
Obtain a copy of the lpart.
Definition: shape_part.h:52
virtual bool is_renderable() override
Is the part renderable?
Definition: shape_part.h:59
virtual void intersect(bun &b) override
Apply only the eventual intersection of the lpart with ray bundle b.
Definition: shape_part.h:45
virtual std::string print_str() const override
String for printing the object.
Definition: shape_part.h:64
Class template implementing the specialized lparts for refractive shape parts.
Definition: shape_part.h:77
virtual void intersect(bun &b) override
Apply only the eventual intersection of the lpart with ray bundle b.
Definition: shape_part.h:116
virtual std::string print_str() const override
String for printing the object.
Definition: shape_part.h:135
virtual std::unique_ptr< lpart > clone() const override
Obtain a copy of the lpart.
Definition: shape_part.h:123
shape_refract_part(T _shp, double _nr)
Constructor with initialization to the shape _shp and relative refractive index _nr.
Definition: shape_part.h:91
double nr
Part's relative refractive index.
Definition: shape_part.h:84
virtual void apply(bun &b) override
Apply refractive shape lpart operations to the ray bundle b.
Definition: shape_part.h:105
virtual bool is_transfer() override
Is the part a transfer?
Definition: shape_part.h:127
virtual bool is_renderable() override
Is the part renderable?
Definition: shape_part.h:130
T shp
The shape of the part.
Definition: shape_part.h:82
void reflect(ray &r, const Vec3 &N)
Reflect ray operation.
Definition: rop.cpp:9
void refract(ray &r, const Vec3 &N, double nr)
Refract ray operation.
Definition: rop.cpp:23