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