Debian Codegraph @

src/gen_normal.h

raw symbols SCIP
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004 \/)\/ *
6 * Visual Computing Lab /\/| *
7 * ISTI - Italian National Research Council | *
8 * \ *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20 * for more details. *
21 * *
22 ****************************************************************************/
23 /****************************************************************************
24 History
25
26 $Log: gen_normal.h,v $
27 Revision 1.2 2006/08/12 04:28:00 tarini
28 Better AO prediction. New icon.
29
30 Revision 1.1 2006/07/27 07:05:25 cignoni
31 *** empty log message ***
32
33 Revision 1.1 2006/06/07 15:15:46 tarini
34 *** empty log message ***
35
36 Revision 1.4 2006/03/23 07:55:54 cignoni
37 *** empty log message ***
38
39 Revision 1.3 2006/03/21 12:05:35 cignoni
40 *** empty log message ***
41
42 Revision 1.2 2006/03/21 11:47:16 cignoni
43 *** empty log message ***
44
45 Revision 1.1 2006/03/21 10:39:45 cignoni
46 *** empty log message ***
47
48 Revision 1.4 2004/09/28 09:43:46 cignoni
49 minor changes
50
51 Revision 1.3 2004/09/16 14:23:57 ponchio
52 fixed gcc template compatibility issues.
53
54 Revision 1.2 2004/09/10 14:02:20 cignoni
55 Added Cone directions
56
57 Revision 1.1 2004/09/09 22:38:57 cignoni
58 Initial Update
59
60 ****************************************************************************/
61
62 #ifndef __VCG_GEN_NORMAL
63 #define __VCG_GEN_NORMAL
64 #include <wrap/gl/math.h>
65 #include <algorithm>
66 namespace vcg {
67
68
69 template <class ScalarType>
70 class GenNormal
71 {
72 public:
73 typedef Point3<ScalarType> Point3x;
74
75 static void Random(int vn, std::vector<Point3<ScalarType > > &NN)
76 {
77 NN.clear();
78 while(NN.size()<vn)
79 {
80 Point3x pp(((float)rand())/RAND_MAX,
81 ((float)rand())/RAND_MAX,
82 ((float)rand())/RAND_MAX);
83 pp=pp*2.0-Point3x(1,1,1);
84 if(pp.SquaredNorm()<1)
85 {
86 Normalize(pp);
87 NN.push_back(pp);
88 }
89 }
90 }
91 static void UniformCone(int vn, std::vector<Point3<ScalarType > > &NN, ScalarType AngleRad, Point3x dir=Point3x(0,1,0))
92 {
93 std::vector<Point3<ScalarType > > NNT;
94 NN.clear();
95 // per prima cosa si calcola il volume della spherical cap di angolo AngleRad
96 ScalarType Height= 1.0 - cos(AngleRad); // height is measured from top...
97 // Surface is the one of the tangent cylinder
98 ScalarType CapArea = 2.0*M_PI*Height;
99 ScalarType Ratio = CapArea / (4.0*M_PI );
100
101 printf("----------AngleRad %f Angledeg %f ratio %f vn %i vn2 %i \n",AngleRad,math::ToDeg(AngleRad),Ratio,vn,int(vn/Ratio));
102 Uniform(vn/Ratio,NNT);
103 printf("asked %i got %i (expecting %i instead of %i)\n", int(vn/Ratio), NNT.size(), int(NNT.size()*Ratio), vn);
104 typename std::vector<Point3<ScalarType> >::iterator vi;
105
106 ScalarType DotProd = cos(AngleRad);
107 for(vi=NNT.begin();vi!=NNT.end();++vi)
108 {
109 if(dir*(*vi) >= DotProd) NN.push_back(*vi);
110 }
111 }
112
113
114 static void Uniform(int vn, std::vector<Point3<ScalarType > > &NN)
115 {
116 OctaLevel pp;
117
118 int ll=10;
119 while(pow(4.0f,ll)+2>vn) ll--;
120
121 pp.Init(ll);
122 sort(pp.v.begin(),pp.v.end());
123 int newsize = unique(pp.v.begin(),pp.v.end())-pp.v.begin();
124 pp.v.resize(newsize);
125
126 NN=pp.v;
127 Perturb(NN);
128 }
129
130 static void Perturb(std::vector<Point3<ScalarType > > &NN)
131 {
132 float width=0.25f/sqrt(float(NN.size()));
133
134 typename std::vector<Point3<ScalarType> >::iterator vi;
135 for(vi=NN.begin(); vi!=NN.end();++vi)
136 {
137 Point3x pp(((float)rand())/RAND_MAX,
138 ((float)rand())/RAND_MAX,
139 ((float)rand())/RAND_MAX);
140 pp=pp*2.0-Point3x(1,1,1);
141 pp*=width;
142 (*vi)+=pp;
143 (*vi).Normalize();
144 }
145
146 }
147
148 private :
149 class OctaLevel
150 {
151 public:
152 std::vector<Point3x> v;
153 int level;
154 int sz;
155
156 Point3f &Val(int i, int j) {
157 assert(i>=0 && i<sz);
158 assert(j>=0 && j<sz);
159 return v[i+j*sz];
160 }
161
162 void Init(int lev)
163 {
164 sz=int(pow(2.0f,lev+1)+1);
165 v.resize(sz*sz);
166 if(lev==0)
167 {
168 Val(0,0)=Point3x( 0, 0,-1); Val(0,1)=Point3x( 0, 1, 0); Val(0,2)=Point3x( 0, 0,-1);
169
170 Val(1,0)=Point3x(-1, 0, 0); Val(1,1)=Point3x( 0, 0, 1); Val(1,2)=Point3x( 1, 0, 0);
171
172 Val(2,0)=Point3x( 0, 0,-1); Val(2,1)=Point3x( 0,-1, 0); Val(2,2)=Point3x( 0, 0,-1);
173 }
174 else
175 {
176 OctaLevel tmp;
177 tmp.Init(lev-1);
178 int i,j;
179 for(i=0;i<sz;++i)
180 for(j=0;j<sz;++j)
181 {
182 if((i%2)==0 && (j%2)==0)
183 Val(i,j)=tmp.Val(i/2,j/2);
184 if((i%2)!=0 && (j%2)==0)
185 Val(i,j)=(tmp.Val(i/2+0,j/2)+tmp.Val(i/2+1,j/2))/2.0;
186 if((i%2)==0 && (j%2)!=0)
187 Val(i,j)=(tmp.Val(i/2,j/2+0)+tmp.Val(i/2,j/2+1))/2.0;
188 if((i%2)!=0 && (j%2)!=0)
189 Val(i,j)=(tmp.Val(i/2+0,j/2+0)+tmp.Val(i/2+0,j/2+1)+tmp.Val(i/2+1,j/2+0)+tmp.Val(i/2+1,j/2+1))/4.0;
190 }
191 typename std::vector<Point3<ScalarType> >::iterator vi;
192 for(vi=v.begin(); vi!=v.end();++vi)
193 (*vi).Normalize();
194
195 }
196 }
197 };
198 };
199 }
200 #endif