1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
|
/*
* camera.cc -- ePiX::camera functions
*
* This file is part of ePiX, a C++ library for creating high-quality
* figures in LaTeX
*
* Version 1.1.21
* Last Change: September 22, 2007
*/
/*
* Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
* Andrew D. Hwang <ahwang -at- holycross -dot- edu>
* Department of Mathematics and Computer Science
* College of the Holy Cross
* Worcester, MA, 01610-2395, USA
*/
/*
* ePiX is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* ePiX is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ePiX; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <cmath>
#include "errors.h"
#include "constants.h"
#include "pairs.h"
#include "triples.h"
#include "frame.h"
#include "functions.h"
#include "halfspace.h"
#include "lens.h"
#include "camera.h"
namespace ePiX {
// Need to initialize frame argument for Apple gcc; otherwise cam()
// gets null vectors in frame()...
Camera::Camera()
: the_viewpt(P(0,0,EPIX_INFTY)), the_target(P(0,0,0)),
the_orient(frame(E_1, E_2, E_3)),
the_distance(EPIX_INFTY), the_clip_range(MIN_CLIP),
the_filter(Neutral()), the_lens(new Perspective()) { }
// viewpt, target, orient
Camera::Camera(const P& vpt)
: the_viewpt(vpt), the_target(P(0,0,0)), the_clip_range(MIN_CLIP),
the_filter(Neutral())
{
if (the_viewpt == P(0,0,0))
{
epix_warning("Cannot initialize Camera at Origin, using (0, 0, oo)");
the_viewpt = P(0, 0, EPIX_INFTY);
}
P viewer(the_viewpt - the_target);
the_distance = norm(viewer);
viewer *= recip(norm(viewer));
// viewer is "eye", if possible E_3 is "sky"
P tmp_sea(E_3*viewer);
if (norm(tmp_sea) < EPIX_EPSILON) // we're on the "z-axis through vpt"
tmp_sea = E_1;
// eye*sea = sky
the_orient = frame(tmp_sea, viewer*tmp_sea, viewer);
adjust();
the_lens = new Perspective();
}
Camera::Camera(const Camera& cam)
: the_viewpt(cam.the_viewpt), the_target(cam.the_target),
the_orient(cam.the_orient), the_distance(cam.the_distance),
the_clip_range(cam.the_clip_range), the_filter(cam.the_filter),
the_lens(cam.the_lens->clone()) { }
Camera& Camera::operator= (const Camera& cam)
{
Lens* tmp_lens(cam.the_lens->clone());
the_lens = tmp_lens;
the_viewpt = cam.the_viewpt;
the_target = cam.the_target;
the_orient =cam.the_orient;
the_distance = cam.the_distance;
the_clip_range = cam.the_distance;
the_filter = cam.the_filter;
return *this;
}
Camera::~Camera()
{
delete the_lens;
}
// rotate up/down
Camera& Camera::tilt(double angle)
{
the_orient.rot1(-angle);
the_target = the_viewpt + (-the_distance)*the_orient.eye();
return *this;
}
// rotate left/right
Camera& Camera::pan(double angle)
{
the_orient.rot2(-angle);
the_target = the_viewpt + (-the_distance)*the_orient.eye();
return *this;
}
// rotate about viewing axis
Camera& Camera::roll(double angle)
{
the_orient.rot3(-angle); // target unchanged
return *this;
}
// fix target, move viewpt radially along eye()
Camera& Camera::range(double d)
{
if (d == 0)
d = EPIX_INFTY;
the_distance = d;
the_viewpt = the_target + d*the_orient.eye();
return *this;
}
// fix viewpt, move target radially along eye()
Camera& Camera::focus(double d)
{
if (d == 0)
d = EPIX_INFTY;
the_distance = d;
the_target = the_viewpt - d*the_orient.eye();
return *this;
}
// clip everything behind this
Camera& Camera::clip_range(double dist)
{
the_clip_range = (dist < MIN_CLIP ? MIN_CLIP : dist);
return *this;
}
// fix target, set viewpt
Camera& Camera::at(const P& vpt)
{
the_viewpt = vpt;
adjust();
return *this;
}
// fix viewpt, set target
Camera& Camera::look_at(const P& tgt)
{
the_target = tgt;
adjust();
return *this;
}
Camera& Camera::at(double x1, double x2, double x3)
{
the_viewpt = P(x1, x2, x3);
adjust();
return *this;
}
// fix viewpt, set target
Camera& Camera::look_at(double x1, double x2, double x3)
{
the_target = P(x1, x2, x3);
adjust();
return *this;
}
Camera& Camera::filter(const Color& filter)
{
the_filter=filter;
return *this;
}
Camera& Camera::perspective()
{
delete the_lens;
the_lens = new Perspective;
return *this;
}
Camera& Camera::orthog()
{
delete the_lens;
the_lens = new Orthog;
return *this;
}
Camera& Camera::fisheye()
{
delete the_lens;
the_lens = new Fisheye;
return *this;
}
Camera& Camera::bubble()
{
delete the_lens;
the_lens = new Bubble;
return *this;
}
Color Camera::operator() (const Color& in_color) const
{
Color temp = the_filter;
return temp.filter(in_color);
}
// lens interface
pair Camera::operator() (const P& arg) const
{
return (*the_lens)(arg, the_orient, the_viewpt, the_distance);
}
bool Camera::is_linear() const
{
return (*the_lens).is_linear();
}
bool Camera::needs_clip() const
{
return (*the_lens).needs_clip();
}
halfspace Camera::clip_plane() const
{
const P N(-the_orient.eye());
return halfspace(the_viewpt + the_clip_range*N, N);
}
P Camera::eye() const
{
return the_orient.eye();
}
P Camera::viewpt() const
{
return the_viewpt;
}
// private function
void Camera::adjust()
{
P temp(the_target - the_viewpt);
the_distance = norm(temp);
if (the_distance < EPIX_EPSILON)
{
epix_warning("Cannot make viewpoint and target the same, no action");
return;
}
// else
temp *= -1.0/the_distance; // reverse direction
double z_ht(temp|E_3);
// too close to "z-axis" through target?
if (sqrt((1-z_ht)*(1+z_ht)) < EPIX_EPSILON)
{
if (z_ht >= 0)
the_orient = frame();
else // z_ht < 0
the_orient = frame(-E_1, E_2, -E_3);
}
else // far enough from axis
the_orient = frame(P(-temp.x2(), temp.x1(), 0), E_3, temp);
}
Camera& cam()
{
static Camera* the_camera(new Camera());
return *the_camera;
}
Camera& camera(cam());
} // end of namespace
|