9087d05939a6a3cd46decf9de7a1acba985cbf9c
[blender.git] / source / blender / freestyle / intern / view_map / FEdgeXDetector.h
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * ***** END GPL LICENSE BLOCK *****
19  */
20
21 #ifndef __FREESTYLE_FEDGE_X_DETECTOR_H__
22 #define __FREESTYLE_FEDGE_X_DETECTOR_H__
23
24 /** \file blender/freestyle/intern/view_map/FEdgeXDetector.h
25  *  \ingroup freestyle
26  *  \brief Detects/flags/builds extended features edges on the WXEdge structure
27  *  \author Stephane Grabli
28  *  \date 26/10/2003
29  */
30
31 #include <vector>
32
33 #include "../geometry/Geom.h"
34
35 #include "../system/FreestyleConfig.h"
36 #include "../system/ProgressBar.h"
37 #include "../system/RenderMonitor.h"
38
39 #include "../winged_edge/Curvature.h"
40 #include "../winged_edge/WXEdge.h"
41
42 #include "BLI_math.h"
43
44 #ifdef WITH_CXX_GUARDEDALLOC
45 #include "MEM_guardedalloc.h"
46 #endif
47
48 namespace Freestyle {
49
50 using namespace Geometry;
51
52 /*! This class takes as input a WXEdge structure and fills it */
53 class FEdgeXDetector
54 {
55 public:
56         FEdgeXDetector()
57         {
58                 _pProgressBar = NULL;
59                 _pRenderMonitor = NULL;
60                 _computeViewIndependent = true;
61 #if 0
62                 _bbox_diagonal = 1.0;
63 #endif
64                 _meanEdgeSize = 0;
65                 _computeRidgesAndValleys = true;
66                 _computeSuggestiveContours = true;
67                 _computeMaterialBoundaries = true;
68                 _sphereRadius = 1.0;
69                 _orthographicProjection = false;
70                 _faceSmoothness = false;
71                 _changes = false;
72                 _kr_derivative_epsilon = 0.0;
73                 _creaseAngle = 0.7; // angle of 134.43 degrees
74         }
75
76         virtual ~FEdgeXDetector() {}
77
78         /*! Process shapes from a WingedEdge containing a list of WShapes */
79         virtual void processShapes(WingedEdge&);
80
81         // GENERAL STUFF
82         virtual void preProcessShape(WXShape *iShape);
83         virtual void preProcessFace(WXFace *iFace);
84         virtual void computeCurvatures(WXVertex *iVertex);
85
86         // SILHOUETTE
87         virtual void processSilhouetteShape(WXShape *iShape);
88         virtual void ProcessSilhouetteFace(WXFace *iFace);
89         virtual void ProcessSilhouetteEdge(WXEdge *iEdge);
90
91         // CREASE
92         virtual void processCreaseShape(WXShape *iShape);
93         virtual void ProcessCreaseEdge(WXEdge *iEdge);
94
95         /*! Sets the minimum angle for detecting crease edges
96          *  \param angle
97          *    The angular threshold in degrees (between 0 and 180) for detecting crease edges. An edge is considered
98          *    a crease edge if the angle between two faces sharing the edge is smaller than the given threshold.
99          */
100         // XXX angle should be in radian...
101         inline void setCreaseAngle(real angle)
102         {
103                 if (angle < 0.0)
104                         angle = 0.0;
105                 else if (angle > 180.0)
106                         angle = 180.0;
107                 angle = cos(M_PI * (180.0 - angle) / 180.0);
108                 if (angle != _creaseAngle) {
109                         _creaseAngle = angle;
110                         _changes = true;
111                 }
112         }
113
114         // BORDER
115         virtual void processBorderShape(WXShape *iShape);
116         virtual void ProcessBorderEdge(WXEdge *iEdge);
117
118         // RIDGES AND VALLEYS
119         virtual void processRidgesAndValleysShape(WXShape *iShape);
120         virtual void ProcessRidgeFace(WXFace *iFace);
121
122         // SUGGESTIVE CONTOURS
123         virtual void processSuggestiveContourShape(WXShape *iShape);
124         virtual void ProcessSuggestiveContourFace(WXFace *iFace);
125         virtual void postProcessSuggestiveContourShape(WXShape *iShape);
126         virtual void postProcessSuggestiveContourFace(WXFace *iFace);
127         /*! Sets the minimal derivative of the radial curvature for suggestive contours
128          *  \param dkr
129          *    The minimal derivative of the radial curvature
130          */
131         inline void setSuggestiveContourKrDerivativeEpsilon(real dkr)
132         {
133                 if (dkr != _kr_derivative_epsilon) {
134                         _kr_derivative_epsilon = dkr;
135                         _changes = true;
136                 }
137         }
138
139         // MATERIAL BOUNDARY
140         virtual void processMaterialBoundaryShape(WXShape *iWShape);
141         virtual void ProcessMaterialBoundaryEdge(WXEdge *iEdge);
142
143         // EDGE MARKS
144         virtual void processEdgeMarksShape(WXShape *iShape);
145         virtual void ProcessEdgeMarks(WXEdge *iEdge);
146
147         // EVERYBODY
148         virtual void buildSmoothEdges(WXShape *iShape);
149
150         /*! Sets the current viewpoint */
151         inline void setViewpoint(const Vec3r& ivp)
152         {
153                 _Viewpoint = ivp;
154         }
155
156         inline void enableOrthographicProjection(bool b)
157         {
158                 _orthographicProjection = b;
159         }
160
161         inline void enableRidgesAndValleysFlag(bool b)
162         {
163                 _computeRidgesAndValleys = b;
164         }
165
166         inline void enableSuggestiveContours(bool b)
167         {
168                 _computeSuggestiveContours = b;
169         }
170
171         inline void enableMaterialBoundaries(bool b)
172         {
173                 _computeMaterialBoundaries = b;
174         }
175
176         inline void enableFaceSmoothness(bool b)
177         {
178                 if (b != _faceSmoothness) {
179                         _faceSmoothness = b;
180                         _changes = true;
181                 }
182         }
183
184         inline void enableFaceMarks(bool b)
185         {
186                 if (b != _faceMarks) {
187                         _faceMarks = b;
188                         _changes = true;
189                 }
190         }
191
192         /*! Sets the radius of the geodesic sphere around each vertex (for the curvature computation)
193          *  \param r
194          *    The radius of the sphere expressed as a ratio of the mean edge size
195          */
196         inline void setSphereRadius(real r)
197         {
198                 if (r != _sphereRadius) {
199                         _sphereRadius = r;
200                         _changes = true;
201                 }
202         }
203
204         inline void setProgressBar(ProgressBar *iProgressBar)
205         {
206                 _pProgressBar = iProgressBar;
207         }
208
209         inline void setRenderMonitor(RenderMonitor *iRenderMonitor)
210         {
211                 _pRenderMonitor = iRenderMonitor;
212         }
213
214 protected:
215         Vec3r _Viewpoint;
216 #if 0
217         real _bbox_diagonal; // diagonal of the current processed shape bbox
218 #endif
219         //oldtmp values
220         bool _computeViewIndependent;
221         real _meanK1;
222         real _meanKr;
223         real _minK1;
224         real _minKr;
225         real _maxK1;
226         real _maxKr;
227         unsigned _nPoints;
228         real _meanEdgeSize;
229         bool _orthographicProjection;
230
231         bool _computeRidgesAndValleys;
232         bool _computeSuggestiveContours;
233         bool _computeMaterialBoundaries;
234         bool _faceSmoothness;
235         bool _faceMarks;
236         real _sphereRadius; // expressed as a ratio of the mean edge size
237         real _creaseAngle; // [-1, 1] compared with the inner product of face normals
238         bool _changes;
239
240         real _kr_derivative_epsilon;
241
242         ProgressBar *_pProgressBar;
243         RenderMonitor *_pRenderMonitor;
244
245 #ifdef WITH_CXX_GUARDEDALLOC
246         MEM_CXX_CLASS_ALLOC_FUNCS("Freestyle:FEdgeXDetector")
247 #endif
248 };
249
250 } /* namespace Freestyle */
251
252 #endif // __FREESTYLE_FEDGE_X_DETECTOR_H__