The spica renderer
normal3d_detail.h
1 #ifdef _MSC_VER
2 #pragma once
3 #endif
4 
5 #ifndef _SPICA_NORMAL3D_DETAIL_H_
6 #define _SPICA_NORMAL3D_DETAIL_H_
7 
8 #include <iostream>
9 #include <iomanip>
10 #include <sstream>
11 
12 namespace spica {
13 
14  template <class T>
15  Normal3_<T>::Normal3_()
16  : x_{ 0 }
17  , y_{ 0 }
18  , z_{ 0 } {
19  }
20 
21  template <class T>
22  Normal3_<T>::Normal3_(T x, T y, T z)
23  : x_{ x }
24  , y_{ y }
25  , z_{ z } {
26  }
27 
28  template <class T>
29  Normal3_<T>::Normal3_(const Vector3_<T>& v)
30  : x_{ v.x() }
31  , y_{ v.y() }
32  , z_{ v.z() } {
33  }
34 
35  template <class T>
36  Normal3_<T>::Normal3_(const Normal3_<T>& n)
37  : x_{ n.x_ }
38  , y_{ n.y_ }
39  , z_{ n.z_ } {
40  }
41 
42  template <class T>
43  Normal3_<T>::~Normal3_() {
44  }
45 
46  template <class T>
47  Normal3_<T>& Normal3_<T>::operator=(const Normal3_<T>& n) {
48  x_ = n.x_;
49  y_ = n.y_;
50  z_ = n.z_;
51  return *this;
52  }
53 
54  template <class T>
55  bool Normal3_<T>::operator==(const Normal3_<T>& n) const {
56  return (x_ == n.x_ && y_ == n.y_ && z_ == n.z_);
57  }
58 
59  template <class T>
60  bool Normal3_<T>::operator!=(const Normal3_<T>& n) const {
61  return !this->operator==(n);
62  }
63 
64  template <class T>
65  Normal3_<T> Normal3_<T>::operator-() const {
66  return { -x_, -y_, -z_ };
67  }
68 
69  template <class T>
70  Normal3_<T> Normal3_<T>::operator+=(const Normal3_<T>& n) {
71  this->x_ += n.x_;
72  this->y_ += n.y_;
73  this->z_ += n.z_;
74  return *this;
75  }
76 
77  template <class T>
78  Normal3_<T> Normal3_<T>::operator-=(const Normal3_<T>& n) {
79  this->x_ -= n.x_;
80  this->y_ -= n.y_;
81  this->z_ -= n.z_;
82  return *this;
83  }
84 
85  template <class T>
86  Normal3_<T> Normal3_<T>::operator*=(T s) {
87  this->x_ *= s;
88  this->y_ *= s;
89  this->z_ *= s;
90  return *this;
91  }
92 
93  template <class T>
94  Normal3_<T> Normal3_<T>::operator/=(T s) {
95  Assertion(s != 0.0, "Zero division!!");
96  return this->operator*=(1.0 / s);
97  }
98 
99  template <class T>
100  T Normal3_<T>::operator[](int i) const {
101  Assertion(0 <= i && i <= 2, "Index out of bounds!!");
102  if (i == 0) return x_;
103  if (i == 1) return y_;
104  return z_;
105  }
106 
107  template <class T>
108  Normal3_<T>::operator Vector3_<T>() const {
109  return { x_, y_, z_ };
110  }
111 
112  template <class T>
113  std::string Normal3_<T>::toString() const {
114  std::stringstream ss;
115  ss << std::fixed;
116  ss << std::setprecision(8);
117  ss << "(" << x_ << ", " << y_ << ", " << z_ << ")";
118  return std::move(ss.str());
119  }
120 
121  template <class T>
122  double Normal3_<T>::norm() const {
123  return sqrt(squaredNorm());
124  }
125 
126  template <class T>
127  double Normal3_<T>::squaredNorm() const {
128  return x_ * x_ + y_ * y_ + z_ * z_;
129  }
130 
131  template <class T>
132  Normal3_<T> Normal3_<T>::normalized() const {
133  return (*this) / this->norm();
134  }
135 
136 } // namespace spica
137 
138 template <class T>
139 std::ostream& operator<<(std::ostream& os, const spica::Normal3_<T>& n) {
140  os << n.toString();
141  return os;
142 }
143 
144 template <class T>
145 spica::Normal3_<T> operator+(const spica::Normal3_<T>& n1, const spica::Normal3_<T>& n2) {
146  spica::Normal3_<T> ret = n1;
147  ret += n2;
148  return ret;
149 }
150 
151 template <class T>
152 spica::Normal3_<T> operator-(const spica::Normal3_<T>& n1, const spica::Normal3_<T>& n2) {
153  spica::Normal3_<T> ret = n1;
154  ret -= n2;
155  return ret;
156 }
157 
158 template <class T>
159 spica::Normal3_<T> operator*(const spica::Normal3_<T>& n, T s) {
160  spica::Normal3_<T> ret = n;
161  ret *= s;
162  return ret;
163 }
164 
165 template <class T>
166 spica::Normal3_<T> operator*(T s, const spica::Normal3_<T>& n) {
167  spica::Normal3_<T> ret = n;
168  ret *= s;
169  return ret;
170 }
171 
172 template <class T>
173 spica::Normal3_<T> operator/(const spica::Normal3_<T>& n, T s) {
174  spica::Normal3_<T> ret = n;
175  ret /= s;
176  return ret;
177 }
178 
179 
180 #endif // _SPICA_NORMAL3D_DETAIL_H_
Definition: core.hpp:74