Cycles / Hair: Further cleanup of UI and internals.
[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         encasing_ratio = 1.01f;
89         minimum_width = 0.0f;
90         maximum_width = 0.0f;
91
92         use_curves = true;
93         use_encasing = true;
94         use_backfacing = false;
95         use_tangent_normal_geometry = false;
96
97         need_update = true;
98         need_mesh_update = false;
99 }
100
101 CurveSystemManager::~CurveSystemManager()
102 {
103 }
104
105 void CurveSystemManager::device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
106 {
107         if(!need_update)
108                 return;
109
110         device_free(device, dscene);
111
112         progress.set_status("Updating Hair settings", "Copying Hair settings to device");
113
114         KernelCurves *kcurve= &dscene->data.curve;
115
116         kcurve->curveflags = 0;
117
118         if(use_curves) {
119                 if(primitive == CURVE_SEGMENTS || primitive == CURVE_RIBBONS)
120                         kcurve->curveflags |= CURVE_KN_INTERPOLATE;
121                 if(primitive == CURVE_RIBBONS)
122                         kcurve->curveflags |= CURVE_KN_RIBBONS;
123
124                 if(line_method == CURVE_ACCURATE)
125                         kcurve->curveflags |= CURVE_KN_ACCURATE;
126                 else if(line_method == CURVE_CORRECTED)
127                         kcurve->curveflags |= CURVE_KN_INTERSECTCORRECTION;
128
129                 if(use_tangent_normal_geometry)
130                         kcurve->curveflags |= CURVE_KN_TRUETANGENTGNORMAL;
131                 if(use_backfacing)
132                         kcurve->curveflags |= CURVE_KN_BACKFACING;
133                 if(use_encasing)
134                         kcurve->curveflags |= CURVE_KN_ENCLOSEFILTER;
135
136                 kcurve->encasing_ratio = encasing_ratio;
137                 kcurve->minimum_width = minimum_width;
138                 kcurve->maximum_width = maximum_width;
139                 kcurve->subdivisions = subdivisions;
140         }
141
142         if(progress.get_cancel()) return;
143
144         need_update = false;
145 }
146
147 void CurveSystemManager::device_free(Device *device, DeviceScene *dscene)
148 {
149
150 }
151
152 bool CurveSystemManager::modified(const CurveSystemManager& CurveSystemManager)
153 {
154         return !(curve_shape == CurveSystemManager.curve_shape &&
155                 line_method == CurveSystemManager.line_method &&
156                 primitive == CurveSystemManager.primitive &&
157                 use_encasing == CurveSystemManager.use_encasing &&
158                 use_tangent_normal_geometry == CurveSystemManager.use_tangent_normal_geometry &&
159                 encasing_ratio == CurveSystemManager.encasing_ratio &&
160                 minimum_width == CurveSystemManager.minimum_width &&
161                 maximum_width == CurveSystemManager.maximum_width &&
162                 use_backfacing == CurveSystemManager.use_backfacing &&
163                 triangle_method == CurveSystemManager.triangle_method &&
164                 resolution == CurveSystemManager.resolution &&
165                 use_curves == CurveSystemManager.use_curves &&
166                 subdivisions == CurveSystemManager.subdivisions);
167 }
168
169 bool CurveSystemManager::modified_mesh(const CurveSystemManager& CurveSystemManager)
170 {
171         return !(primitive == CurveSystemManager.primitive &&
172                 curve_shape == CurveSystemManager.curve_shape &&
173                 triangle_method == CurveSystemManager.triangle_method &&
174                 resolution == CurveSystemManager.resolution &&
175                 use_curves == CurveSystemManager.use_curves);
176 }
177
178 void CurveSystemManager::tag_update(Scene *scene)
179 {
180         need_update = true;
181 }
182
183 void CurveSystemManager::tag_update_mesh()
184 {
185         need_mesh_update = true;
186 }
187 CCL_NAMESPACE_END
188