00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
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 bool 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
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> bool Frustum<T>::IsOutside(Point3<T> &point, T radius) {
00129 for(int i = 0; i < 4; i++) {
00130 T dist = Distance(point, i);
00131 if(dist < -radius)
00132 return true;
00133 }
00134 return false;
00135 }
00136
00137 template <class T> T Frustum<T>::range(Point3<T> &point, T radius, T &closest, T &farthest) {
00138
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::Distance(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
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 }
00189
00190 #endif
00191
00192
00193
00194
00195