doc/web: Place examples section more visibly.
[Ale.git] / d2 / pixel.h
blobdd147b0944752889557efaebd974d2213a50ea56
1 // Copyright 2002 David Hilvert <dhilvert@auricle.dyndns.org>,
2 // <dhilvert@ugcs.caltech.edu>
4 /* This file is part of the Anti-Lamenessing Engine.
6 The Anti-Lamenessing Engine is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 The Anti-Lamenessing Engine is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with the Anti-Lamenessing Engine; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #ifndef __pixel_h__
22 #define __pixel_h__
25 * Structure to describe a pixel
28 class pixel {
29 private:
30 ale_real x[3];
32 public:
33 pixel() {
34 x[0] = 0;
35 x[1] = 0;
36 x[2] = 0;
39 pixel(ale_real x0, ale_real x1, ale_real x2) {
40 x[0] = x0;
41 x[1] = x1;
42 x[2] = x2;
45 pixel(const pixel &p) {
46 x[0] = p[0];
47 x[1] = p[1];
48 x[2] = p[2];
51 pixel &operator=(const pixel &p) {
52 x[0] = p[0];
53 x[1] = p[1];
54 x[2] = p[2];
56 return (*this);
60 // Due to automatic typecasts and automatic int <==> ale_real *
61 // conversions, this can cause some really weird bugs.
63 // pixel(ale_real *_x) {
64 // x[0] = _x[0];
65 // x[1] = _x[1];
66 // x[2] = _x[2];
67 // }
69 const ale_real &operator[](unsigned int i) const {
70 #if 0
72 * This may be expensive.
74 assert (i < 3);
75 #endif
76 return x[i];
79 ale_real &operator[](unsigned int i) {
80 #if 0
82 * This may be expensive.
84 assert (i < 3);
85 #endif
86 return x[i];
89 pixel operator+(pixel p) const {
90 return pixel(p[0] + x[0], p[1] + x[1], p[2] + x[2]);
93 pixel operator-(pixel p) const {
94 return pixel(x[0] - p[0], x[1] - p[1], x[2] - p[2]);
97 pixel operator-() const {
98 return pixel(-x[0], -x[1], -x[2]);
101 pixel operator/(pixel p) const {
102 return pixel(x[0] / p[0], x[1] / p[1], x[2] / p[2]);
105 pixel operator/(ale_real d) const {
106 return pixel(x[0] / d, x[1] / d, x[2] / d);
109 pixel mult(pixel p) const {
110 return pixel(x[0] * p[0], x[1] * p[1], x[2] * p[2]);
113 pixel mult(ale_real d) const {
114 return pixel(x[0] * d, x[1] * d, x[2] * d);
117 pixel operator+=(pixel p) {
118 return pixel(x[0] += p[0], x[1] += p[1], x[2] += p[2]);
121 pixel operator*=(pixel p) {
122 return pixel(x[0] *= p[0], x[1] *= p[1], x[2] *= p[2]);
125 pixel operator*=(ale_real d) {
126 return pixel(x[0] *= d, x[1] *= d, x[2] *= d);
129 pixel operator/=(pixel p) {
130 return pixel(x[0] /= p[0], x[1] /= p[1], x[2] /= p[2]);
133 pixel operator/=(ale_real d) {
134 return pixel(x[0] /= d, x[1] /= d, x[2] /= d);
137 pixel clamp() const {
138 pixel result;
140 for (int i = 0; i < 3; i++)
141 if (x[i] > 1.0)
142 result[i] = 1.0;
143 else if (x[i] < 0.0)
144 result[i] = 0.0;
145 else
146 result[i] = x[i];
148 return result;
152 pixel abs() {
153 return pixel(fabs(x[0]), fabs(x[1]), fabs(x[2]));
156 ale_real normsq() {
157 return x[0] * x[0] + x[1] * x[1] + x[2] * x[2];
160 ale_real norm() {
161 return sqrt(normsq());
164 ale_real lnorm() {
165 return x[0] + x[1] + x[2];
168 ale_real maxabs_norm() {
169 ale_real m = fabs(x[0]);
170 if (fabs(x[1]) > m)
171 m = fabs(x[1]);
172 if (fabs(x[2]) > m)
173 m = fabs(x[2]);
175 return m;
178 ale_real minabs_norm() {
179 ale_real m = fabs(x[0]);
180 if (fabs(x[1]) < m)
181 m = fabs(x[1]);
182 if (fabs(x[2]) < m)
183 m = fabs(x[2]);
185 return m;
188 ale_real min_norm() const {
189 ale_real m = x[0];
190 if (x[1] < m)
191 m = x[1];
192 if (x[2] < m)
193 m = x[2];
195 return m;
198 ale_real max_norm() {
199 ale_real m = x[0];
200 if (x[1] > m)
201 m = x[1];
202 if (x[2] > m)
203 m = x[2];
205 return m;
208 static pixel zero() {
209 return pixel(0, 0, 0);
212 static pixel one() {
213 return pixel(1, 1, 1);
216 int operator==(const pixel &p) {
217 return x[0] == p[0]
218 && x[1] == p[1]
219 && x[2] == p[2];
222 int operator!=(const pixel &p) {
223 return !operator==(p);
226 int finite() {
227 return ::finite(x[0]) && ::finite(x[1]) && ::finite(x[2]);
230 static pixel undefined() {
231 ale_real zero = 0;
232 return pixel(zero / zero, zero / zero, zero / zero);
237 inline pixel operator*(const pixel &p, const pixel &q) {
238 return p.mult(q);
241 template<typename T>
242 inline pixel operator*(T d, const pixel &p) {
243 return p.mult(d);
245 template<typename T>
246 inline pixel operator*(const pixel &p, T d) {
247 return p.mult(d);
250 inline std::ostream &operator<<(std::ostream &o, const pixel &p) {
251 o << "[" << (double) p[0] << " " << (double) p[1] << " " << (double) p[2] << "]";
252 return o;
255 template<typename T>
256 inline pixel ppow(pixel p, T d) {
257 return pixel(
258 pow(p[0], d),
259 pow(p[1], d),
260 pow(p[2], d));
263 inline pixel pexp(pixel p) {
264 return pixel(
265 exp((double) p[0]),
266 exp((double) p[1]),
267 exp((double) p[2]));
270 inline pixel psqrt(pixel p) {
271 return pixel(
272 sqrt(p[0]),
273 sqrt(p[1]),
274 sqrt(p[2]));
277 #endif