Style cleanup: indentation, braces
[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         if(discroot >= 0) {
50                 discroot = sqrt(discroot);
51                 ta = (-curve_coef[2] - discroot) / (3 * curve_coef[3]);
52                 tb = (-curve_coef[2] + discroot) / (3 * curve_coef[3]);
53                 ta = (ta > 1.0f || ta < 0.0f) ? -1.0f : ta;
54                 tb = (tb > 1.0f || tb < 0.0f) ? -1.0f : tb;
55         }
56
57         *upper = max(p1[dim],p2[dim]);
58         *lower = min(p1[dim],p2[dim]);
59         float exa = p1[dim];
60         float exb = p2[dim];
61         float t2;
62         float t3;
63         if(ta >= 0.0f) {
64                 t2 = ta * ta;
65                 t3 = t2 * ta;
66                 exa = curve_coef[3] * t3 + curve_coef[2] * t2 + curve_coef[1] * ta + curve_coef[0];
67         }
68         if(tb >= 0.0f) {
69                 t2 = tb * tb;
70                 t3 = t2 * tb;
71                 exb = curve_coef[3] * t3 + curve_coef[2] * t2 + curve_coef[1] * tb + curve_coef[0];
72         }
73         *upper = max(*upper, max(exa,exb));
74         *lower = min(*lower, min(exa,exb));
75 }
76
77 /* Hair System Manager */
78
79 CurveSystemManager::CurveSystemManager()
80 {
81         primitive = CURVE_LINE_SEGMENTS;
82         curve_shape = CURVE_THICK;
83         line_method = CURVE_CORRECTED;
84         triangle_method = CURVE_CAMERA_TRIANGLES;
85         resolution = 3;
86         subdivisions = 3;
87
88         minimum_width = 0.0f;
89         maximum_width = 0.0f;
90
91         use_curves = true;
92         use_encasing = true;
93         use_backfacing = false;
94         use_tangent_normal_geometry = false;
95
96         need_update = true;
97         need_mesh_update = false;
98 }
99
100 CurveSystemManager::~CurveSystemManager()
101 {
102 }
103
104 void CurveSystemManager::device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
105 {
106         if(!need_update)
107                 return;
108
109         device_free(device, dscene);
110
111         progress.set_status("Updating Hair settings", "Copying Hair settings to device");
112
113         KernelCurves *kcurve = &dscene->data.curve;
114
115         kcurve->curveflags = 0;
116
117         if(use_curves) {
118                 if(primitive == CURVE_SEGMENTS || primitive == CURVE_RIBBONS)
119                         kcurve->curveflags |= CURVE_KN_INTERPOLATE;
120                 if(primitive == CURVE_RIBBONS)
121                         kcurve->curveflags |= CURVE_KN_RIBBONS;
122
123                 if(line_method == CURVE_ACCURATE)
124                         kcurve->curveflags |= CURVE_KN_ACCURATE;
125                 else if(line_method == CURVE_CORRECTED)
126                         kcurve->curveflags |= CURVE_KN_INTERSECTCORRECTION;
127
128                 if(use_tangent_normal_geometry)
129                         kcurve->curveflags |= CURVE_KN_TRUETANGENTGNORMAL;
130                 if(use_backfacing)
131                         kcurve->curveflags |= CURVE_KN_BACKFACING;
132                 if(use_encasing)
133                         kcurve->curveflags |= CURVE_KN_ENCLOSEFILTER;
134
135                 kcurve->minimum_width = minimum_width;
136                 kcurve->maximum_width = maximum_width;
137                 kcurve->subdivisions = subdivisions;
138         }
139
140         if(progress.get_cancel()) return;
141
142         need_update = false;
143 }
144
145 void CurveSystemManager::device_free(Device *device, DeviceScene *dscene)
146 {
147
148 }
149
150 bool CurveSystemManager::modified(const CurveSystemManager& CurveSystemManager)
151 {
152         return !(curve_shape == CurveSystemManager.curve_shape &&
153                 line_method == CurveSystemManager.line_method &&
154                 primitive == CurveSystemManager.primitive &&
155                 use_encasing == CurveSystemManager.use_encasing &&
156                 use_tangent_normal_geometry == CurveSystemManager.use_tangent_normal_geometry &&
157                 minimum_width == CurveSystemManager.minimum_width &&
158                 maximum_width == CurveSystemManager.maximum_width &&
159                 use_backfacing == CurveSystemManager.use_backfacing &&
160                 triangle_method == CurveSystemManager.triangle_method &&
161                 resolution == CurveSystemManager.resolution &&
162                 use_curves == CurveSystemManager.use_curves &&
163                 subdivisions == CurveSystemManager.subdivisions);
164 }
165
166 bool CurveSystemManager::modified_mesh(const CurveSystemManager& CurveSystemManager)
167 {
168         return !(primitive == CurveSystemManager.primitive &&
169                 curve_shape == CurveSystemManager.curve_shape &&
170                 triangle_method == CurveSystemManager.triangle_method &&
171                 resolution == CurveSystemManager.resolution &&
172                 use_curves == CurveSystemManager.use_curves);
173 }
174
175 void CurveSystemManager::tag_update(Scene *scene)
176 {
177         need_update = true;
178 }
179
180 void CurveSystemManager::tag_update_mesh()
181 {
182         need_mesh_update = true;
183 }
184 CCL_NAMESPACE_END
185