HyperspaceExplorer 0.7.1
Projector.h
00001 /*
00002 Hyperspace Explorer - visualizing higher-dimensional geometry
00003 Copyright (C) 2010  Lene Preuss <lene.preuss@gmail.com>
00004 
00005 This program is free software; you can redistribute it and/or modify
00006 it under the terms of the GNU General Public License as published by
00007 the Free Software Foundation; either version 2 of the License, or
00008 (at your option) any later version.
00009 
00010 This program is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License along
00016 with this program; if not, write to the Free Software Foundation, Inc.,
00017 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00018 
00019 */
00020 
00021 #ifndef PROJECTOR_H
00022 #define PROJECTOR_H
00023 
00024 #include "Vector.impl.h"
00025 
00027 
00038 template <unsigned D, unsigned D_> class Projector: protected Projector<D-1, D_> {
00039     public:
00041 
00048         std::vector<VecMath::Vector<D_> > operator()(
00049                 const std::vector<VecMath::Vector<D> > &x,
00050                 double scrW, double camW) {
00051 
00052             checkConsistency(scrW, camW);
00053 
00054             std::vector<VecMath::Vector<D-1> > x_proj = doProject(x, scrW, camW);
00055             return Projector<D-1, D_>::operator()(x_proj, scrW, camW);
00056         }
00057 
00059 
00069         std::vector<VecMath::Vector<D_> > operator()(
00070                 const std::vector<VecMath::Vector<D> > &x,
00071                 const std::vector<double> &scrW, const std::vector<double> &camW) {
00072             throw NotYetImplementedException("std::vector<VecMath::Vector<D_> > \n"
00073                     "    Projector<D, D_>::operator()(\n"
00074                     "        vector<Vector<D> > &, vector<double> &, "
00075                     "vector<double> &)");
00076         }
00077 
00078     private:
00080 
00084         void checkConsistency(double scrW, double camW) {
00085             if (D_ > D) {
00086                 throw std::logic_error("Tried to project to a higher dimension");
00087             }
00088             if (D_ == D) {
00089                 throw std::logic_error("Explicit specialization should be called");
00090             }
00091             if (camW <= scrW) {
00092                 throw std::logic_error("Screen must be closer to object than eye");
00093             }
00094         }
00095 
00097 
00104         std::vector<VecMath::Vector<D-1> > doProject(
00105                 const std::vector<VecMath::Vector<D> > &x,
00106                 double scrW, double camW) {
00107             std::vector<VecMath::Vector<D-1> > x_proj(x.size());
00108 
00109             for (unsigned i = 0; i < x.size(); i++) {
00110                 double ProjectionFactor = (scrW-camW)/(x[i][D-1]-camW);
00111 
00112                 for (unsigned j = 0; j < D-1; j++)
00113                     x_proj.at(i)[j] = ProjectionFactor*x[i][j];
00114             }
00115             return x_proj;
00116         }
00117 
00118 };
00119 
00121 
00123 template <unsigned D> class Projector<D, D> {
00124     public:
00126         std::vector<VecMath::Vector<D> > operator()(
00127             const std::vector<VecMath::Vector<D> > &x, double, double) {
00128             return x;
00129         }
00130 };
00131 
00132 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends

Generated on Mon Apr 9 2012 20:25:15 for HyperspaceExplorer 0.7.1 by doxygen 1.7.4  -  Hosted bySourceForge.net Logo