Cycles Refactor: Add SSE Utility code from Embree for cleaner SSE code.
[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, DeviceScene *dscene, Scene *scene, Progress& progress)
107 {
108         if(!need_update)
109                 return;
110
111         device_free(device, dscene);
112
113         progress.set_status("Updating Hair settings", "Copying Hair settings to device");
114
115         KernelCurves *kcurve = &dscene->data.curve;
116
117         kcurve->curveflags = 0;
118
119         if(use_curves) {
120                 if(primitive == CURVE_SEGMENTS || primitive == CURVE_RIBBONS)
121                         kcurve->curveflags |= CURVE_KN_INTERPOLATE;
122                 if(primitive == CURVE_RIBBONS)
123                         kcurve->curveflags |= CURVE_KN_RIBBONS;
124
125                 if(line_method == CURVE_ACCURATE)
126                         kcurve->curveflags |= CURVE_KN_ACCURATE;
127                 else if(line_method == CURVE_CORRECTED)
128                         kcurve->curveflags |= CURVE_KN_INTERSECTCORRECTION;
129
130                 if(use_tangent_normal_geometry)
131                         kcurve->curveflags |= CURVE_KN_TRUETANGENTGNORMAL;
132                 if(use_backfacing)
133                         kcurve->curveflags |= CURVE_KN_BACKFACING;
134                 if(use_encasing)
135                         kcurve->curveflags |= CURVE_KN_ENCLOSEFILTER;
136
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                 minimum_width == CurveSystemManager.minimum_width &&
160                 maximum_width == CurveSystemManager.maximum_width &&
161                 use_backfacing == CurveSystemManager.use_backfacing &&
162                 triangle_method == CurveSystemManager.triangle_method &&
163                 resolution == CurveSystemManager.resolution &&
164                 use_curves == CurveSystemManager.use_curves &&
165                 subdivisions == CurveSystemManager.subdivisions);
166 }
167
168 bool CurveSystemManager::modified_mesh(const CurveSystemManager& CurveSystemManager)
169 {
170         return !(primitive == CurveSystemManager.primitive &&
171                 curve_shape == CurveSystemManager.curve_shape &&
172                 triangle_method == CurveSystemManager.triangle_method &&
173                 resolution == CurveSystemManager.resolution &&
174                 use_curves == CurveSystemManager.use_curves);
175 }
176
177 void CurveSystemManager::tag_update(Scene *scene)
178 {
179         need_update = true;
180 }
181
182 void CurveSystemManager::tag_update_mesh()
183 {
184         need_mesh_update = true;
185 }
186 CCL_NAMESPACE_END
187