Cycles: Code cleanup, prepare for strict C++ flags
[blender.git] / intern / cycles / render / curves.cpp
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "device.h"
18 #include "curves.h"
19 #include "mesh.h"
20 #include "object.h"
21 #include "scene.h"
22
23 #include "util_foreach.h"
24 #include "util_map.h"
25 #include "util_progress.h"
26 #include "util_vector.h"
27
28 CCL_NAMESPACE_BEGIN
29
30 /* Curve functions */
31
32 void curvebounds(float *lower, float *upper, float3 *p, int dim)
33 {
34         float *p0 = &p[0].x;
35         float *p1 = &p[1].x;
36         float *p2 = &p[2].x;
37         float *p3 = &p[3].x;
38
39         float fc = 0.71f;
40         float curve_coef[4];
41         curve_coef[0] = p1[dim];
42         curve_coef[1] = -fc*p0[dim] + fc*p2[dim];
43         curve_coef[2] = 2.0f * fc * p0[dim] + (fc - 3.0f) * p1[dim] + (3.0f - 2.0f * fc) * p2[dim] - fc * p3[dim];
44         curve_coef[3] = -fc * p0[dim] + (2.0f - fc) * p1[dim] + (fc - 2.0f) * p2[dim] + fc * p3[dim];
45
46         float discroot = curve_coef[2] * curve_coef[2] - 3 * curve_coef[3] * curve_coef[1];
47         float ta = -1.0f;
48         float tb = -1.0f;
49
50         if(discroot >= 0) {
51                 discroot = sqrtf(discroot);
52                 ta = (-curve_coef[2] - discroot) / (3 * curve_coef[3]);
53                 tb = (-curve_coef[2] + discroot) / (3 * curve_coef[3]);
54                 ta = (ta > 1.0f || ta < 0.0f) ? -1.0f : ta;
55                 tb = (tb > 1.0f || tb < 0.0f) ? -1.0f : tb;
56         }
57
58         *upper = max(p1[dim],p2[dim]);
59         *lower = min(p1[dim],p2[dim]);
60
61         float exa = p1[dim];
62         float exb = p2[dim];
63
64         if(ta >= 0.0f) {
65                 float t2 = ta * ta;
66                 float t3 = t2 * ta;
67                 exa = curve_coef[3] * t3 + curve_coef[2] * t2 + curve_coef[1] * ta + curve_coef[0];
68         }
69         if(tb >= 0.0f) {
70                 float t2 = tb * tb;
71                 float t3 = t2 * tb;
72                 exb = curve_coef[3] * t3 + curve_coef[2] * t2 + curve_coef[1] * tb + curve_coef[0];
73         }
74
75         *upper = max(*upper, max(exa,exb));
76         *lower = min(*lower, min(exa,exb));
77 }
78
79 /* Hair System Manager */
80
81 CurveSystemManager::CurveSystemManager()
82 {
83         primitive = CURVE_LINE_SEGMENTS;
84         curve_shape = CURVE_THICK;
85         line_method = CURVE_CORRECTED;
86         triangle_method = CURVE_CAMERA_TRIANGLES;
87         resolution = 3;
88         subdivisions = 3;
89
90         minimum_width = 0.0f;
91         maximum_width = 0.0f;
92
93         use_curves = true;
94         use_encasing = true;
95         use_backfacing = false;
96         use_tangent_normal_geometry = false;
97
98         need_update = true;
99         need_mesh_update = false;
100 }
101
102 CurveSystemManager::~CurveSystemManager()
103 {
104 }
105
106 void CurveSystemManager::device_update(Device *device,
107                                        DeviceScene *dscene,
108                                        Scene * /*scene*/,
109                                        Progress& progress)
110 {
111         if(!need_update)
112                 return;
113
114         device_free(device, dscene);
115
116         progress.set_status("Updating Hair settings", "Copying Hair settings to device");
117
118         KernelCurves *kcurve = &dscene->data.curve;
119
120         kcurve->curveflags = 0;
121
122         if(use_curves) {
123                 if(primitive == CURVE_SEGMENTS || primitive == CURVE_RIBBONS)
124                         kcurve->curveflags |= CURVE_KN_INTERPOLATE;
125                 if(primitive == CURVE_RIBBONS)
126                         kcurve->curveflags |= CURVE_KN_RIBBONS;
127
128                 if(line_method == CURVE_ACCURATE)
129                         kcurve->curveflags |= CURVE_KN_ACCURATE;
130                 else if(line_method == CURVE_CORRECTED)
131                         kcurve->curveflags |= CURVE_KN_INTERSECTCORRECTION;
132
133                 if(use_tangent_normal_geometry)
134                         kcurve->curveflags |= CURVE_KN_TRUETANGENTGNORMAL;
135                 if(use_backfacing)
136                         kcurve->curveflags |= CURVE_KN_BACKFACING;
137                 if(use_encasing)
138                         kcurve->curveflags |= CURVE_KN_ENCLOSEFILTER;
139
140                 kcurve->minimum_width = minimum_width;
141                 kcurve->maximum_width = maximum_width;
142                 kcurve->subdivisions = subdivisions;
143         }
144
145         if(progress.get_cancel()) return;
146
147         need_update = false;
148 }
149
150 void CurveSystemManager::device_free(Device * /*device*/,
151                                      DeviceScene * /*dscene*/)
152 {
153
154 }
155
156 bool CurveSystemManager::modified(const CurveSystemManager& CurveSystemManager)
157 {
158         return !(curve_shape == CurveSystemManager.curve_shape &&
159                 line_method == CurveSystemManager.line_method &&
160                 primitive == CurveSystemManager.primitive &&
161                 use_encasing == CurveSystemManager.use_encasing &&
162                 use_tangent_normal_geometry == CurveSystemManager.use_tangent_normal_geometry &&
163                 minimum_width == CurveSystemManager.minimum_width &&
164                 maximum_width == CurveSystemManager.maximum_width &&
165                 use_backfacing == CurveSystemManager.use_backfacing &&
166                 triangle_method == CurveSystemManager.triangle_method &&
167                 resolution == CurveSystemManager.resolution &&
168                 use_curves == CurveSystemManager.use_curves &&
169                 subdivisions == CurveSystemManager.subdivisions);
170 }
171
172 bool CurveSystemManager::modified_mesh(const CurveSystemManager& CurveSystemManager)
173 {
174         return !(primitive == CurveSystemManager.primitive &&
175                 curve_shape == CurveSystemManager.curve_shape &&
176                 triangle_method == CurveSystemManager.triangle_method &&
177                 resolution == CurveSystemManager.resolution &&
178                 use_curves == CurveSystemManager.use_curves);
179 }
180
181 void CurveSystemManager::tag_update(Scene * /*scene*/)
182 {
183         need_update = true;
184 }
185
186 void CurveSystemManager::tag_update_mesh()
187 {
188         need_mesh_update = true;
189 }
190 CCL_NAMESPACE_END
191