soc-2008-mxcurioni: first version of lib3ds code. It does NOT work yet and has to...
[blender.git] / source / blender / freestyle / intern / view_map / Silhouette.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 "ViewMap.h"
24
25                   /**********************************/
26                   /*                                */
27                   /*                                */
28                   /*             SVertex            */
29                   /*                                */
30                   /*                                */
31                   /**********************************/
32
33 Nature::VertexNature SVertex::getNature() const {
34   Nature::VertexNature nature = Nature::S_VERTEX;
35   if (_pViewVertex)
36     nature |= _pViewVertex->getNature();
37   return nature;
38 }
39
40 SVertex * SVertex::castToSVertex(){
41   return this;
42 }
43
44 ViewVertex * SVertex::castToViewVertex(){
45   return _pViewVertex;
46 }
47
48 NonTVertex * SVertex::castToNonTVertex(){
49   return dynamic_cast<NonTVertex*>(_pViewVertex);
50 }
51
52 TVertex * SVertex::castToTVertex(){
53   return dynamic_cast<TVertex*>(_pViewVertex);
54 }
55
56 float SVertex::shape_importance() const 
57 {
58   return shape()->importance();
59 }
60  
61 //Material SVertex::material() const {return _Shape->material();}
62 Id SVertex::shape_id() const {return _Shape->getId();}
63 const SShape *  SVertex::shape() const {return _Shape;}
64
65 const int SVertex::qi() const 
66 {
67   if (getNature() & Nature::T_VERTEX)
68     Exception::raiseException();
69   return (_FEdges[0])->qi();
70 }
71
72 occluder_container::const_iterator SVertex::occluders_begin() const 
73 {
74   if (getNature() & Nature::T_VERTEX)
75     Exception::raiseException();
76   return (_FEdges[0])->occluders_begin();
77 }
78
79 occluder_container::const_iterator SVertex::occluders_end() const 
80 {
81   if (getNature() & Nature::T_VERTEX)
82     Exception::raiseException();
83   return (_FEdges[0])->occluders_end();
84 }
85
86 bool SVertex::occluders_empty() const 
87 {
88   if (getNature() & Nature::T_VERTEX)
89     Exception::raiseException();
90   return (_FEdges[0])->occluders_empty();
91 }
92
93 int SVertex::occluders_size() const 
94 {
95   if (getNature() & Nature::T_VERTEX)
96     Exception::raiseException();
97   return (_FEdges[0])->occluders_size();
98 }
99
100 const Polygon3r& SVertex::occludee() const 
101 {
102   if (getNature() & Nature::T_VERTEX)
103     Exception::raiseException();
104   return (_FEdges[0])->occludee();
105 }
106
107 const SShape* SVertex::occluded_shape() const 
108 {
109   if (getNature() & Nature::T_VERTEX)
110     Exception::raiseException();
111   return (_FEdges[0])->occluded_shape();
112 }
113
114 const bool  SVertex::occludee_empty() const
115 {
116   if (getNature() & Nature::T_VERTEX)
117     Exception::raiseException();
118   return (_FEdges[0])->occludee_empty();
119 }
120
121 real SVertex::z_discontinuity() const 
122 {
123   if (getNature() & Nature::T_VERTEX)
124     Exception::raiseException();
125   return (_FEdges[0])->z_discontinuity();
126 }
127
128 FEdge* SVertex::fedge()
129 {
130   if (getNature() & Nature::T_VERTEX)
131     return 0;
132   return _FEdges[0];
133 }
134
135 FEdge* SVertex::getFEdge(Interface0D& inter)
136 {
137   FEdge * result = 0;
138   SVertex* iVertexB = dynamic_cast<SVertex*>(&inter);
139   if (!iVertexB)
140     return result;
141   vector<FEdge*>::const_iterator fe=_FEdges.begin(), feend=_FEdges.end();
142   for(;
143   fe!=feend;
144   ++fe)
145   {
146     if(  (((*fe)->vertexA() == this) && ((*fe)->vertexB() == iVertexB))
147       || (((*fe)->vertexB() == this) && ((*fe)->vertexA() == iVertexB)))
148       result = (*fe);
149   }
150   if((result == 0) && (getNature() & Nature::T_VERTEX))
151   {
152     SVertex *brother;
153     ViewVertex *vvertex = viewvertex();
154     TVertex * tvertex = dynamic_cast<TVertex*>(vvertex);
155     if(tvertex)
156     {
157       brother = tvertex->frontSVertex();
158       if(this == brother)
159         brother = tvertex->backSVertex();
160       const vector<FEdge*>& fedges = brother->fedges();
161       for(fe=fedges.begin(),feend=fedges.end();
162       fe!=feend;
163       ++fe)
164       {
165         if(  (((*fe)->vertexA() == brother) && ((*fe)->vertexB() == iVertexB))
166           || (((*fe)->vertexB() == brother) && ((*fe)->vertexA() == iVertexB)))
167         result = (*fe);
168       }
169     }
170   }
171   if((result == 0) && (iVertexB->getNature() & Nature::T_VERTEX))
172   {
173     SVertex *brother;
174     ViewVertex *vvertex = iVertexB->viewvertex();
175     TVertex * tvertex = dynamic_cast<TVertex*>(vvertex);
176     if(tvertex)
177     {
178       brother = tvertex->frontSVertex();
179       if(iVertexB == brother)
180         brother = tvertex->backSVertex();
181       for(fe=_FEdges.begin(),feend=_FEdges.end();
182       fe!=feend;
183       ++fe)
184       {
185         if(  (((*fe)->vertexA() == this) && ((*fe)->vertexB() == brother))
186           || (((*fe)->vertexB() == this) && ((*fe)->vertexA() == brother)))
187         result = (*fe);
188       }
189     }
190   }
191   
192   return result;
193 }
194
195
196                   /**********************************/
197                   /*                                */
198                   /*                                */
199                   /*             FEdge              */
200                   /*                                */
201                   /*                                */
202                   /**********************************/
203
204
205 int FEdge::viewedge_nature() const {return _ViewEdge->getNature();}
206 //float FEdge::viewedge_length() const {return _ViewEdge->viewedge_length();}
207 const SShape* FEdge::occluded_shape() const 
208 {
209   ViewShape * aShape = _ViewEdge->aShape(); 
210   if(aShape == 0) 
211     return 0;
212   return aShape->sshape();
213 }
214
215 float FEdge::shape_importance() const 
216 {
217   return _VertexA->shape()->importance();
218
219
220 int FEdge::invisibility() const 
221 {
222   return _ViewEdge->qi();
223 }
224
225 occluder_container::const_iterator FEdge::occluders_begin() const {return _ViewEdge->occluders_begin();}
226 occluder_container::const_iterator FEdge::occluders_end() const {return _ViewEdge->occluders_end();}
227 bool FEdge::occluders_empty() const {return _ViewEdge->occluders_empty();}
228 int FEdge::occluders_size() const {return _ViewEdge->occluders_size();}
229 const bool  FEdge::occludee_empty() const
230 {
231   return _ViewEdge->occludee_empty();
232 }
233
234
235
236 Id FEdge::shape_id() const 
237 {
238   return _VertexA->shape()->getId();
239 }
240 const SShape* FEdge::shape() const 
241 {
242   return _VertexA->shape();
243 }
244
245 real FEdge::z_discontinuity() const 
246 {
247   if(!(getNature() & Nature::SILHOUETTE) && !(getNature() & Nature::BORDER))
248   {
249     return 0;
250   }
251
252   BBox<Vec3r> box = ViewMap::getInstance()->getScene3dBBox();
253
254   Vec3r bbox_size_vec(box.getMax() - box.getMin());
255   real bboxsize = bbox_size_vec.norm();
256   if(occludee_empty())
257
258   {
259     //return FLT_MAX;
260
261     return 1.0;
262
263     //return bboxsize;
264
265   }
266   //  real result;
267   //  z_discontinuity_functor<SVertex> _functor;
268
269   //  Evaluate<SVertex,z_discontinuity_functor<SVertex> >(&_functor, iCombination, result )
270   Vec3r middle((_VertexB->point3d()-_VertexA->point3d()));
271
272   middle /= 2;
273   Vec3r disc_vec(middle - _occludeeIntersection);
274   real res = disc_vec.norm() / bboxsize;
275   return res;
276   
277   //return fabs((middle.z()-_occludeeIntersection.z()));
278 }
279
280
281 //float FEdge::local_average_depth(int iCombination ) const 
282 //{
283 //
284 //  float result;
285 //  local_average_depth_functor<SVertex> functor;
286 //  Evaluate(&functor, iCombination, result);
287 //
288 //  return result;
289 //}
290 //float FEdge::local_depth_variance(int iCombination ) const
291 //{
292 //  float result;
293 //
294 //  local_depth_variance_functor<SVertex> functor;
295 //
296 //  Evaluate(&functor, iCombination, result);
297 //
298 //  return result;
299 //}
300 //
301 //
302 //real FEdge::local_average_density( float sigma, int iCombination) const 
303 //{
304 //  float result;
305 //
306 //  density_functor<SVertex> functor(sigma);
307 //
308 //  Evaluate(&functor, iCombination, result);
309 //
310 //  return result;
311 //}
312 //
313 ////Vec3r FEdge::normal(int& oException /* = Exception::NO_EXCEPTION */)
314 ////{
315 ////  Vec3r Na = _VertexA->normal(oException);
316 ////  if(oException != Exception::NO_EXCEPTION)
317 ////    return Na;
318 ////  Vec3r Nb = _VertexB->normal(oException);
319 ////  if(oException != Exception::NO_EXCEPTION)
320 ////    return Nb;
321 ////  return (Na+Nb)/2.0;
322 ////}  
323 //
324 //Vec3r FEdge::curvature2d_as_vector(int iCombination) const 
325 //{
326 //  Vec3r result;
327 //  curvature2d_as_vector_functor<SVertex> _functor;
328 //  Evaluate<Vec3r,curvature2d_as_vector_functor<SVertex> >(&_functor, iCombination, result );
329 //  return result;
330 //}
331 //
332 //real FEdge::curvature2d_as_angle(int iCombination) const 
333 //{
334 //  real result;
335 //  curvature2d_as_angle_functor<SVertex> _functor;
336 //  Evaluate<real,curvature2d_as_angle_functor<SVertex> >(&_functor, iCombination, result );
337 //  return result;
338 //}
339
340                   /**********************************/
341                   /*                                */
342                   /*                                */
343                   /*             FEdgeSharp        */
344                   /*                                */
345                   /*                                */
346                   /**********************************/
347
348 //Material FEdge::material() const 
349 //{
350 //  return _VertexA->shape()->material();
351 //}
352 const FrsMaterial& FEdgeSharp::aFrsMaterial() const {
353   return _VertexA->shape()->frs_material(_aFrsMaterialIndex);
354 }
355
356 const FrsMaterial& FEdgeSharp::bFrsMaterial() const {
357   return _VertexA->shape()->frs_material(_bFrsMaterialIndex);
358 }
359
360                  /**********************************/
361                   /*                                */
362                   /*                                */
363                   /*             FEdgeSmooth         */
364                   /*                                */
365                   /*                                */
366                   /**********************************/
367
368 const FrsMaterial& FEdgeSmooth::frs_material() const {
369   return _VertexA->shape()->frs_material(_FrsMaterialIndex);
370 }