Merged changes in the trunk up to revision 44266 (including BMesh).
[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 "Silhouette.h"
23 #include "SilhouetteGeomEngine.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         if( _isOrthographicProjection )
153                 return t;
154
155         // we need to compute for each parameter t the corresponding 
156         // parameter T which gives the intersection in 3D.
157         real T;
158
159         // suffix w for world, c for camera, r for retina, i for image
160         Vec3r Aw = (fe)->vertexA()->point3D();
161         Vec3r Bw = (fe)->vertexB()->point3D();
162         Vec3r Ac, Bc;
163         GeomUtils::fromWorldToCamera(Aw, Ac, _modelViewMatrix);
164         GeomUtils::fromWorldToCamera(Bw, Bc, _modelViewMatrix);
165         Vec3r ABc = Bc - Ac;
166 #if 0
167         cout << "Ac " << Ac << endl;
168         cout << "Bc " << Bc << endl;
169         cout << "ABc " << ABc << endl;
170 #endif
171         Vec3r Ai = (fe)->vertexA()->point2D();
172         Vec3r Bi = (fe)->vertexB()->point2D();
173         Vec3r Ii = Ai + t * (Bi - Ai); // the intersection point in the 2D image space
174         Vec3r Ir, Ic;
175         GeomUtils::fromImageToRetina(Ii, Ir, _viewport);
176
177         real alpha, beta, denom;
178         real m11 = _projectionMatrix[0][0];
179         real m13 = _projectionMatrix[0][2];
180         real m22 = _projectionMatrix[1][1];
181         real m23 = _projectionMatrix[1][2];
182
183         if (fabs(ABc[0]) > 1e-6) {
184
185                 alpha = ABc[2] / ABc[0];
186                 beta = Ac[2] - alpha * Ac[0];
187                 denom = alpha * (Ir[0] + m13) + m11;
188                 if (fabs(denom) < 1e-6)
189                         goto iter;
190                 Ic[0] = -beta * (Ir[0] + m13) / denom;
191 //              Ic[1] = -(Ir[1] + m23) * (alpha * Ic[0] + beta) / m22;
192 //              Ic[2] = alpha * (Ic[0] - Ac[0]) + Ac[2];
193                 T = (Ic[0] - Ac[0]) / ABc[0];
194
195         } else if (fabs(ABc[1]) > 1e-6) {
196
197                 alpha = ABc[2] / ABc[1];
198                 beta = Ac[2] - alpha * Ac[1];
199                 denom = alpha * (Ir[1] + m23) + m22;
200                 if (fabs(denom) < 1e-6)
201                         goto iter;
202                 Ic[1] = -beta * (Ir[1] + m23) / denom;
203 //              Ic[0] = -(Ir[0] + m13) * (alpha * Ic[1] + beta) / m11;
204 //              Ic[2] = alpha * (Ic[1] - Ac[1]) + Ac[2];
205                 T = (Ic[1] - Ac[1]) / ABc[1];
206
207         } else {
208
209 iter:   bool x_coords, less_than;
210                 if (fabs(Bi[0] - Ai[0]) > 1e-6) {
211                         x_coords = true;
212                         less_than = Ai[0] < Bi[0];
213                 } else {
214                         x_coords = false;
215                         less_than = Ai[1] < Bi[1];
216                 }
217                 Vec3r Pc, Pr, Pi;
218                 real T_sta = 0.0;
219                 real T_end = 1.0;
220                 real delta_x, delta_y, dist, dist_threshold = 1e-6;
221                 int i, max_iters = 100;
222                 for (i = 0; i < max_iters; i++) {
223                         T = T_sta + 0.5 * (T_end - T_sta);
224                         Pc = Ac + T * ABc;
225                         GeomUtils::fromCameraToRetina(Pc, Pr, _projectionMatrix);
226                         GeomUtils::fromRetinaToImage(Pr, Pi, _viewport);
227                         delta_x = Ii[0] - Pi[0];
228                         delta_y = Ii[1] - Pi[1];
229                         dist = sqrt(delta_x * delta_x + delta_y * delta_y);
230                         if (dist < dist_threshold)
231                                 break;
232                         if (x_coords) {
233                                 if (less_than) {
234                                         if (Pi[0] < Ii[0]) { T_sta = T; } else { T_end = T; }
235                                 } else {
236                                         if (Pi[0] > Ii[0]) { T_sta = T; } else { T_end = T; }
237                                 }
238                         } else {
239                                 if (less_than) {
240                                         if (Pi[1] < Ii[1]) { T_sta = T; } else { T_end = T; }
241                                 } else {
242                                         if (Pi[1] > Ii[1]) { T_sta = T; } else { T_end = T; }
243                                 }
244                         }
245                 }
246 #if 0
247                 printf("SilhouetteGeomEngine::ImageToWorldParameter(): #iters = %d, dist = %e\n", i, dist);
248 #endif
249                 if (i == max_iters)
250                         printf("SilhouetteGeomEngine::ImageToWorldParameter(): reached to max_iters (dist = %e)\n", dist);
251         }
252
253         return T;
254 }
255
256 Vec3r SilhouetteGeomEngine::WorldToImage(const Vec3r& M)
257
258 {
259
260   Vec3r newPoint;
261   GeomUtils::fromWorldToImage(M, newPoint, _transform, _viewport);
262   newPoint[2] = (-newPoint[2]-_znear)/(_zfar-_znear); // normalize Z between 0 and 1
263   return newPoint;
264
265 }
266