Fixed a bug in SilhouetteGeomEngine::ImageToWorldParameter() that caused
[blender.git] / source / blender / freestyle / intern / view_map / SilhouetteGeomEngine.cpp
1
2 //
3 //  Copyright (C) : Please refer to the COPYRIGHT file distributed 
4 //   with this source distribution. 
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 //
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "SilhouetteGeomEngine.h"
23 #include "Silhouette.h"
24 #include "../geometry/GeomUtils.h"
25
26 using namespace std;
27
28 Vec3r SilhouetteGeomEngine::_Viewpoint = Vec3r(0,0,0);
29 real SilhouetteGeomEngine::_translation[3] = {0,0,0};
30 real SilhouetteGeomEngine::_modelViewMatrix[4][4] = {{1,0,0,0},
31                                                      {0,1,0,0},
32                                                      {0,0,1,0},
33                                                      {0,0,0,1}};
34 real SilhouetteGeomEngine::_projectionMatrix[4][4] = {{1,0,0,0},
35                                                      {0,1,0,0},
36                                                      {0,0,1,0},
37                                                      {0,0,0,1}};
38 real SilhouetteGeomEngine::_transform[4][4] = {{1,0,0,0},
39                                                {0,1,0,0},
40                                                {0,0,1,0},
41                                                {0,0,0,1}};
42 int SilhouetteGeomEngine::_viewport[4] = {1,1,1,1};              // the viewport
43 real SilhouetteGeomEngine::_Focal = 0.0;
44   
45 real SilhouetteGeomEngine::_glProjectionMatrix[4][4] = {{1,0,0,0},
46                                                         {0,1,0,0},
47                                                         {0,0,1,0},
48                                                         {0,0,0,1}};
49 real SilhouetteGeomEngine::_glModelViewMatrix[4][4] = {{1,0,0,0},
50                                                        {0,1,0,0},
51                                                        {0,0,1,0},
52                                                        {0,0,0,1}};
53 real SilhouetteGeomEngine::_znear = 0.0;
54 real SilhouetteGeomEngine::_zfar = 100.0;
55 bool SilhouetteGeomEngine::_isOrthographicProjection = false;  
56
57 SilhouetteGeomEngine * SilhouetteGeomEngine::_pInstance = 0;
58
59 void SilhouetteGeomEngine::setTransform(const real iModelViewMatrix[4][4], const real iProjectionMatrix[4][4], const int iViewport[4], real iFocal) 
60 {
61   unsigned int i,j;
62   _translation[0] = iModelViewMatrix[3][0];
63   _translation[1] = iModelViewMatrix[3][1];
64   _translation[2] = iModelViewMatrix[3][2];
65   
66   for(i=0; i<4; i++){
67     for(j=0; j<4; j++)
68     {
69       _modelViewMatrix[i][j] = iModelViewMatrix[j][i];
70       _glModelViewMatrix[i][j] = iModelViewMatrix[i][j];
71     }
72   }
73
74   for(i=0; i<4; i++){
75     for(j=0; j<4; j++)
76     {
77       _projectionMatrix[i][j] = iProjectionMatrix[j][i];
78       _glProjectionMatrix[i][j] = iProjectionMatrix[i][j];
79     }
80   }
81       
82   for(i=0; i<4; i++){
83     for(j=0; j<4; j++)
84     {
85       _transform[i][j] = 0;
86       for(unsigned int k=0; k<4; k++)
87         _transform[i][j] += _projectionMatrix[i][k] * _modelViewMatrix[k][j];
88     }
89   }
90       
91   for(i=0; i<4; i++){
92     _viewport[i] = iViewport[i];
93   }
94   _Focal = iFocal;
95
96         _isOrthographicProjection = (iProjectionMatrix[3][3] != 0.0);
97 }
98
99 void SilhouetteGeomEngine::setFrustum(real iZNear, real iZFar) 
100 {
101   _znear = iZNear;
102   _zfar = iZFar;
103 }
104
105 void SilhouetteGeomEngine::retrieveViewport(int viewport[4]){
106   memcpy(viewport, _viewport, 4*sizeof(int));
107 }
108 //#define HUGE 1e9
109
110 void SilhouetteGeomEngine::ProjectSilhouette(vector<SVertex*>& ioVertices)
111 {
112   Vec3r newPoint;
113   //  real min=HUGE;
114   //  real max=-HUGE;
115   vector<SVertex*>::iterator sv, svend;
116   
117   for(sv=ioVertices.begin(), svend=ioVertices.end();
118       sv!=svend;
119       sv++)
120     {
121       GeomUtils::fromWorldToImage((*sv)->point3D(), newPoint, _modelViewMatrix, _projectionMatrix, _viewport);
122       newPoint[2] = (-newPoint[2]-_znear)/(_zfar-_znear); // normalize Z between 0 and 1
123       (*sv)->setPoint2D(newPoint);  
124       //cerr << (*sv)->point2d().z() << "  ";
125       //      real d=(*sv)->point2d()[2];
126       //      if (d>max) max =d;
127       //      if (d<min) min =d;
128     }
129       //  for(sv=ioVertices.begin(), svend=ioVertices.end();
130       //      sv!=svend;
131       //      sv++)
132       //    {
133       //      Vec3r P((*sv)->point2d());
134       //      (*sv)->setPoint2D(Vec3r(P[0], P[1], 1.0-(P[2]-min)/(max-min)));
135       //      //cerr<<(*sv)->point2d()[2]<<"  ";
136       //    }
137 }
138
139 void SilhouetteGeomEngine::ProjectSilhouette(SVertex* ioVertex)
140 {
141   Vec3r newPoint;
142   //  real min=HUGE;
143   //  real max=-HUGE;
144   vector<SVertex*>::iterator sv, svend;
145   GeomUtils::fromWorldToImage(ioVertex->point3D(), newPoint, _modelViewMatrix, _projectionMatrix, _viewport);
146   newPoint[2] = (-newPoint[2]-_znear)/(_zfar-_znear); // normalize Z between 0 and 1
147   ioVertex->setPoint2D(newPoint);  
148 }
149
150 real SilhouetteGeomEngine::ImageToWorldParameter(FEdge *fe, real t)
151 {
152         
153         if( _isOrthographicProjection )
154                 return t;
155         
156   // we need to compute for each parameter t the corresponding 
157   // parameter T which gives the intersection in 3D.
158 #if 0
159   //currentEdge = (*fe);
160   Vec3r A = (fe)->vertexA()->point3D();
161   Vec3r B = (fe)->vertexB()->point3D();
162   Vec3r Ai = (fe)->vertexA()->point2D();
163   Vec3r Bi = (fe)->vertexB()->point2D();
164   Vec3r AB = Vec3r((B-A)); // the edge
165   Vec3r ABi(Bi-Ai);
166   Vec3r Ac, Bc;
167   GeomUtils::fromWorldToCamera(A, Ac, _modelViewMatrix);
168   GeomUtils::fromWorldToCamera(B, Bc, _modelViewMatrix);
169       
170   Vec3r Ii = Vec3r((Ai+t*ABi)); // I image
171   // let us compute the 3D point corresponding to the 2D intersection point
172   // and lying on the edge:
173   Vec3r Ir, Ic;
174   GeomUtils::fromImageToRetina(Ii, Ir, _viewport);
175   GeomUtils::fromRetinaToCamera(Ir, Ic, -_Focal, _projectionMatrix);
176   
177   real T;
178   T = (Ic[2]*Ac[1] - Ic[1]*Ac[2])/(Ic[1]*(Bc[2]-Ac[2])-Ic[2]*(Bc[1]-Ac[1]));
179 #else
180         // suffix w for world, i for image
181         Vec3r Aw = (fe)->vertexA()->point3D();
182         Vec3r Bw = (fe)->vertexB()->point3D();
183         Vec3r Ai = (fe)->vertexA()->point2D();
184         Vec3r Bi = (fe)->vertexB()->point2D();
185         Vec3r Ii = Ai + t * (Bi - Ai); // the intersection point in 2D
186         Vec3r Pw, Pi;
187         real T_sta = 0.0;
188         real T_end = 1.0;
189         real T;
190         real delta_x, delta_y, dist, dist_threshold = 1e-6;
191         int i, max_iters = 100;
192         for (i = 0; i < max_iters; i++) {
193         T = T_sta + 0.5 * (T_end - T_sta);
194         Pw = Aw + T * (Bw - Aw);
195                 GeomUtils::fromWorldToImage(Pw, Pi, _transform, _viewport);
196                 delta_x = Ii[0] - Pi[0];
197                 delta_y = Ii[1] - Pi[1];
198         dist = sqrt(delta_x * delta_x + delta_y * delta_y);
199         if (dist < dist_threshold)
200             break;
201                 if (Ai[0] < Bi[0]) {
202             if (Pi[0] < Ii[0])
203                 T_sta = T;
204             else
205                 T_end = T;
206                 } else {
207             if (Pi[0] > Ii[0])
208                 T_sta = T;
209             else
210                 T_end = T;
211                 }
212         }
213 #if 0
214         printf("SilhouetteGeomEngine::ImageToWorldParameter(): #iters = %d, dist = %e\n", i, dist);
215 #endif
216         if (i == max_iters)
217                 printf("SilhouetteGeomEngine::ImageToWorldParameter(): reached to max_iters (dist = %e)\n", dist);
218 #endif
219   
220   return T;
221 }
222
223 Vec3r SilhouetteGeomEngine::WorldToImage(const Vec3r& M)
224
225 {
226
227   Vec3r newPoint;
228   GeomUtils::fromWorldToImage(M, newPoint, _transform, _viewport);
229   newPoint[2] = (-newPoint[2]-_znear)/(_zfar-_znear); // normalize Z between 0 and 1
230   return newPoint;
231
232 }
233