File: frustum.h

package info (click to toggle)
meshlab 2020.09%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 45,132 kB
  • sloc: cpp: 400,238; ansic: 31,952; javascript: 1,578; sh: 387; yacc: 238; lex: 139; python: 86; makefile: 30
file content (195 lines) | stat: -rw-r--r-- 6,671 bytes parent folder | download | duplicates (2)
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
/****************************************************************************
* VCGLib                                                            o o     *
* Visual and Computer Graphics Library                            o     o   *
*                                                                _   O  _   *
* Copyright(C) 2004-2016                                           \/)\/    *
* Visual Computing Lab                                            /\/|      *
* ISTI - Italian National Research Council                           |      *
*                                                                    \      *
* All rights reserved.                                                      *
*                                                                           *
* This program 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.                                       *
*                                                                           *
* This program 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 (http://www.gnu.org/licenses/gpl.txt)          *
* for more details.                                                         *
*                                                                           *
****************************************************************************/
/****************************************************************************
  History

$Log: not supported by cvs2svn $
Revision 1.9  2005/03/02 15:13:45  ponchio
Minimal fix in remoteness (Bugged anyway)

Revision 1.8  2005/02/22 14:33:04  ponchio
small bugs

Revision 1.7  2005/01/21 18:06:05  ponchio
Added remoteness ("distance" from frustum)

Revision 1.6  2004/10/04 12:33:02  ponchio
Cleaning up and planes init more stable.

Revision 1.5  2004/09/28 10:23:28  ponchio
Various generic changes.

Revision 1.4  2004/05/12 20:55:18  ponchio
*** empty log message ***

Revision 1.3  2004/03/31 15:06:41  ponchio
#include <camera> -> #include <view>

Revision 1.2  2004/03/25 14:55:25  ponchio
Adding copyright.


****************************************************************************/

#ifndef FRUSTUM_H
#define FRUSTUM_H

#include <wrap/gui/view.h>
#include <vcg/space/plane3.h>
#include <vcg/space/line3.h>

#include <iostream>
using namespace std;

namespace vcg {

template <class T> class Frustum: public View<T> {
public:                                            
  void GetView();
  void SetView(const float *_proj, const float *_modelview, const int *_viewport);
  Point3<T> ViewPoint();
  T Resolution(float dist = 1);
  bool IsOutside(Point3<T> &point);
  T Remoteness(Point3<T> &point, T radius);
  T IsOutside(Point3<T> &point, T radius);
  T Distance(Point3<T> &point, int plane);  
  T range(Point3<T> &point, T radius, T &closest, T &farthest);
  
protected:
  T resolution;  
  Plane3<T> planes[6];  
  Point3<T> view_point;
  void UpdateView();
};


typedef Frustum<float> Frustumf;
typedef Frustum<double> Frustumd;


//Implementation
template <class T> Point3<T> Frustum<T>::ViewPoint() {
  return view_point;  
}

template <class T> T Frustum<T>::Resolution(float dist) {
  return resolution * dist;  
}

template <class T> bool Frustum<T>::IsOutside(Point3<T> &point) {
  Point3<T> r = Project(point);
  if(r[0] < View<T>::viewport[0] || r[0] > View<T>::viewport[0]+View<T>::viewport[2] ||
     r[1] < View<T>::viewport[1] || r[1] > View<T>::viewport[1]+View<T>::viewport[3]) 
    return true;
  return false;
}

template <class T> T Frustum<T>::Remoteness(Point3<T> &point, T radius) {
  Point3<T> r = Project(point);
  T dist = (point - view_point).Norm();
  if(dist < radius) return 0;
  T rad =  1 + radius / (resolution * dist);
  T mindist = 0;
  T tmp;
  tmp = View<T>::viewport[0] - r[0] - rad;
  if(tmp > mindist) mindist = tmp;
  tmp = r[0] - rad - (View<T>::viewport[0] + View<T>::viewport[2]);
  if(tmp > mindist) mindist = tmp;
  
  tmp = View<T>::viewport[1] - r[1] - rad;
  if(tmp > mindist) mindist = tmp;
  tmp = r[1] - rad - (View<T>::viewport[1] + View<T>::viewport[3]);
  if(tmp > mindist) mindist = tmp;
  
  if(mindist == 0) return 0;
  return 1 + (mindist / (View<T>::viewport[0] + View<T>::viewport[2]));
}

template <class T> T Frustum<T>::IsOutside(Point3<T> &point, T radius) {
  T dist = 0;
  for(int i = 0; i < 4; i++) {
    T d = -Distance(point, i) - radius;
    if(d > dist) dist = d;
  }
  return dist;
}

template <class T> T Frustum<T>::range(Point3<T> &point, T radius, T &closest, T &farthest) {
  //4 near 5 far
  T dist = (view_point - point).Norm();
  closest = dist - radius;
  farthest = dist + radius;
}

template <class T> T Frustum<T>::Distance(Point3<T> &point, int plane) {    
  return vcg::SignedDistancePlanePoint(planes[plane], point);
}

template <class T> void Frustum<T>::GetView() {
  View<T>::GetView();
  UpdateView();
}
template <class T> void Frustum<T>::SetView(const float *_proj = NULL,
                                            const float *_modelview = NULL,
                                            const int *_viewport = NULL) {
  View<T>::SetView(_proj, _modelview, _viewport);
  UpdateView();
}

template <class T> void Frustum<T>::UpdateView() {
  float t = (float)(View<T>::viewport[1] +View<T>:: viewport[3]);
  float b = (float)View<T>::viewport[1];
  float r = (float)(View<T>::viewport[0] + View<T>::viewport[2]);
  float l = (float)View<T>::viewport[0];
  
  Point3<T> nw = UnProject(Point3<T>(l, b, 0.0f));
  Point3<T> sw = UnProject(Point3<T>(l, t, 0.0f));
  Point3<T> ne = UnProject(Point3<T>(r, b, 0.0f));
  Point3<T> se = UnProject(Point3<T>(r, t, 0.0f));
  Point3<T> NW = UnProject(Point3<T>(l, b, 1.0f));
  Point3<T> SW = UnProject(Point3<T>(l, t, 1.0f));
  Point3<T> NE = UnProject(Point3<T>(r, b, 1.0f));
  Point3<T> SE = UnProject(Point3<T>(r, t, 1.0f));

  view_point = View<T>::ViewPoint();  	

  planes[0].Init(nw, NW, NE);  
  planes[1].Init(ne, NE, SE);
  planes[2].Init(se, SE, SW);
  planes[3].Init(sw, SW, NW);
  planes[4].Init(se, sw, nw);
  planes[5].Init(SW, SE, NE);   

  //compute resolution: sizeo of a pixel unitary distance from view_point
  resolution = ((ne + NE)/2 - (nw + NW)/2).Norm() /
               (View<T>::viewport[2] * ((ne + NE + nw + NW)/4 - view_point).Norm());
}

}//namespace

#endif