frustum.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 * VCGLib                                                            o o     *
00003 * Visual and Computer Graphics Library                            o     o   *
00004 *                                                                _   O  _   *
00005 * Copyright(C) 2004                                                \/)\/    *
00006 * Visual Computing Lab                                            /\/|      *
00007 * ISTI - Italian National Research Council                           |      *
00008 *                                                                    \      *
00009 * All rights reserved.                                                      *
00010 *                                                                           *
00011 * This program is free software; you can redistribute it and/or modify      *   
00012 * it under the terms of the GNU General Public License as published by      *
00013 * the Free Software Foundation; either version 2 of the License, or         *
00014 * (at your option) any later version.                                       *
00015 *                                                                           *
00016 * This program is distributed in the hope that it will be useful,           *
00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
00019 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
00020 * for more details.                                                         *
00021 *                                                                           *
00022 ****************************************************************************/
00023 /****************************************************************************
00024   History
00025 
00026 $Log: not supported by cvs2svn $
00027 Revision 1.9  2005/03/02 15:13:45  ponchio
00028 Minimal fix in remoteness (Bugged anyway)
00029 
00030 Revision 1.8  2005/02/22 14:33:04  ponchio
00031 small bugs
00032 
00033 Revision 1.7  2005/01/21 18:06:05  ponchio
00034 Added remoteness ("distance" from frustum)
00035 
00036 Revision 1.6  2004/10/04 12:33:02  ponchio
00037 Cleaning up and planes init more stable.
00038 
00039 Revision 1.5  2004/09/28 10:23:28  ponchio
00040 Various generic changes.
00041 
00042 Revision 1.4  2004/05/12 20:55:18  ponchio
00043 *** empty log message ***
00044 
00045 Revision 1.3  2004/03/31 15:06:41  ponchio
00046 #include <camera> -> #include <view>
00047 
00048 Revision 1.2  2004/03/25 14:55:25  ponchio
00049 Adding copyright.
00050 
00051 
00052 ****************************************************************************/
00053 
00054 #ifndef FRUSTUM_H
00055 #define FRUSTUM_H
00056 
00057 #include <wrap/gui/view.h>
00058 #include <vcg/space/plane3.h>
00059 #include <vcg/space/line3.h>
00060 
00061 #include <iostream>
00062 using namespace std;
00063 
00064 namespace vcg {
00065 
00066 template <class T> class Frustum: public View<T> {
00067 public:                                            
00068   void GetView();
00069   void SetView(const float *_proj, const float *_modelview, const int *_viewport);
00070   Point3<T> ViewPoint();
00071   T Resolution(float dist = 1);
00072   bool IsOutside(Point3<T> &point);
00073   T Remoteness(Point3<T> &point, T radius);
00074   T IsOutside(Point3<T> &point, T radius);
00075   T Distance(Point3<T> &point, int plane);  
00076   T range(Point3<T> &point, T radius, T &closest, T &farthest);
00077   
00078 protected:
00079   T resolution;  
00080   Plane3<T> planes[6];  
00081   Point3<T> view_point;
00082   void UpdateView();
00083 };
00084 
00085 
00086 typedef Frustum<float> Frustumf;
00087 typedef Frustum<double> Frustumd;
00088 
00089 
00090 //Implementation
00091 template <class T> Point3<T> Frustum<T>::ViewPoint() {
00092   return view_point;  
00093 }
00094 
00095 template <class T> T Frustum<T>::Resolution(float dist) {
00096   return resolution * dist;  
00097 }
00098 
00099 template <class T> bool Frustum<T>::IsOutside(Point3<T> &point) {
00100   Point3<T> r = Project(point);
00101   if(r[0] < View<T>::viewport[0] || r[0] > View<T>::viewport[0]+View<T>::viewport[2] ||
00102      r[1] < View<T>::viewport[1] || r[1] > View<T>::viewport[1]+View<T>::viewport[3]) 
00103     return true;
00104   return false;
00105 }
00106 
00107 template <class T> T Frustum<T>::Remoteness(Point3<T> &point, T radius) {
00108   Point3<T> r = Project(point);
00109   T dist = (point - view_point).Norm();
00110   if(dist < radius) return 0;
00111   T rad =  1 + radius / (resolution * dist);
00112   T mindist = 0;
00113   T tmp;
00114   tmp = View<T>::viewport[0] - r[0] - rad;
00115   if(tmp > mindist) mindist = tmp;
00116   tmp = r[0] - rad - (View<T>::viewport[0] + View<T>::viewport[2]);
00117   if(tmp > mindist) mindist = tmp;
00118   
00119   tmp = View<T>::viewport[1] - r[1] - rad;
00120   if(tmp > mindist) mindist = tmp;
00121   tmp = r[1] - rad - (View<T>::viewport[1] + View<T>::viewport[3]);
00122   if(tmp > mindist) mindist = tmp;
00123   
00124   if(mindist == 0) return 0;
00125   return 1 + (mindist / (View<T>::viewport[0] + View<T>::viewport[2]));
00126 }
00127 
00128 template <class T> T Frustum<T>::IsOutside(Point3<T> &point, T radius) {
00129   T dist = 0;
00130   for(int i = 0; i < 4; i++) {
00131     T d = -Distance(point, i) - radius;
00132     if(d > dist) dist = d;
00133   }
00134   return dist;
00135 }
00136 
00137 template <class T> T Frustum<T>::range(Point3<T> &point, T radius, T &closest, T &farthest) {
00138   //4 near 5 far
00139   T dist = (view_point - point).Norm();
00140   closest = dist - radius;
00141   farthest = dist + radius;
00142 }
00143 
00144 template <class T> T Frustum<T>::Distance(Point3<T> &point, int plane) {    
00145   return vcg::SignedDistancePlanePoint(planes[plane], point);
00146 }
00147 
00148 template <class T> void Frustum<T>::GetView() {
00149   View<T>::GetView();
00150   UpdateView();
00151 }
00152 template <class T> void Frustum<T>::SetView(const float *_proj = NULL,
00153                                             const float *_modelview = NULL,
00154                                             const int *_viewport = NULL) {
00155   View<T>::SetView(_proj, _modelview, _viewport);
00156   UpdateView();
00157 }
00158 
00159 template <class T> void Frustum<T>::UpdateView() {
00160   float t = (float)(View<T>::viewport[1] +View<T>:: viewport[3]);
00161   float b = (float)View<T>::viewport[1];
00162   float r = (float)(View<T>::viewport[0] + View<T>::viewport[2]);
00163   float l = (float)View<T>::viewport[0];
00164   
00165   Point3<T> nw = UnProject(Point3<T>(l, b, 0.0f));
00166   Point3<T> sw = UnProject(Point3<T>(l, t, 0.0f));
00167   Point3<T> ne = UnProject(Point3<T>(r, b, 0.0f));
00168   Point3<T> se = UnProject(Point3<T>(r, t, 0.0f));
00169   Point3<T> NW = UnProject(Point3<T>(l, b, 1.0f));
00170   Point3<T> SW = UnProject(Point3<T>(l, t, 1.0f));
00171   Point3<T> NE = UnProject(Point3<T>(r, b, 1.0f));
00172   Point3<T> SE = UnProject(Point3<T>(r, t, 1.0f));
00173 
00174   view_point = View<T>::ViewPoint();    
00175 
00176   planes[0].Init(nw, NW, NE);  
00177   planes[1].Init(ne, NE, SE);
00178   planes[2].Init(se, SE, SW);
00179   planes[3].Init(sw, SW, NW);
00180   planes[4].Init(se, sw, nw);
00181   planes[5].Init(SW, SE, NE);   
00182 
00183   //compute resolution: sizeo of a pixel unitary distance from view_point
00184   resolution = ((ne + NE)/2 - (nw + NW)/2).Norm() /
00185                (View<T>::viewport[2] * ((ne + NE + nw + NW)/4 - view_point).Norm());
00186 }
00187 
00188 }//namespace
00189 
00190 #endif
00191 
00192   
00193         
00194 
00195   


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:31:02