Cycles: remove option to disable transparent shadows globally.
[blender-staging.git] / intern / cycles / blender / blender_curves.cpp
index 9d51aadec538ad4f027ad22f9f3ae66ab71c4b76..42b985305ea1f7e530c809473b8514f0087777f3 100644 (file)
  * limitations under the License.
  */
 
-#include "attribute.h"
-#include "camera.h"
-#include "curves.h"
-#include "mesh.h"
-#include "object.h"
-#include "scene.h"
+#include "render/attribute.h"
+#include "render/camera.h"
+#include "render/curves.h"
+#include "render/mesh.h"
+#include "render/object.h"
+#include "render/scene.h"
 
-#include "blender_sync.h"
-#include "blender_util.h"
+#include "blender/blender_sync.h"
+#include "blender/blender_util.h"
 
-#include "util_foreach.h"
-#include "util_logging.h"
+#include "util/util_foreach.h"
+#include "util/util_logging.h"
 
 CCL_NAMESPACE_BEGIN
 
-/* Utilities */
-
-/* Hair curve functions */
-
-void curveinterp_v3_v3v3v3v3(float3 *p, float3 *v1, float3 *v2, float3 *v3, float3 *v4, const float w[4]);
-void interp_weights(float t, float data[4]);
-float shaperadius(float shape, float root, float tip, float time);
-void InterpolateKeySegments(int seg, int segno, int key, int curve, float3 *keyloc, float *time, ParticleCurveData *CData);
-bool ObtainCacheParticleUV(Mesh *mesh, BL::Mesh *b_mesh, BL::Object *b_ob, ParticleCurveData *CData, bool background, int uv_num);
-bool ObtainCacheParticleVcol(Mesh *mesh, BL::Mesh *b_mesh, BL::Object *b_ob, ParticleCurveData *CData, bool background, int vcol_num);
-bool ObtainCacheParticleData(Mesh *mesh, BL::Mesh *b_mesh, BL::Object *b_ob, ParticleCurveData *CData, bool background);
-void ExportCurveSegments(Scene *scene, Mesh *mesh, ParticleCurveData *CData);
-void ExportCurveTrianglePlanes(Mesh *mesh, ParticleCurveData *CData,
-                               float3 RotCam, bool is_ortho);
-void ExportCurveTriangleGeometry(Mesh *mesh, ParticleCurveData *CData, int resolution);
-void ExportCurveTriangleUV(Mesh *mesh, ParticleCurveData *CData, int vert_offset, int resol, float3 *uvdata);
-void ExportCurveTriangleVcol(Mesh *mesh, ParticleCurveData *CData, int vert_offset, int resol, uchar4 *cdata);
-
 ParticleCurveData::ParticleCurveData()
 {
 }
@@ -55,7 +37,7 @@ ParticleCurveData::~ParticleCurveData()
 {
 }
 
-void interp_weights(float t, float data[4])
+static void interp_weights(float t, float data[4])
 {
        /* Cardinal curve interpolation */
        float t2 = t * t;
@@ -68,17 +50,19 @@ void interp_weights(float t, float data[4])
        data[3] =  fc          * t3  - fc * t2;
 }
 
-void curveinterp_v3_v3v3v3v3(float3 *p, float3 *v1, float3 *v2, float3 *v3, float3 *v4, const float w[4])
+static void curveinterp_v3_v3v3v3v3(float3 *p,
+                                    float3 *v1, float3 *v2, float3 *v3, float3 *v4,
+                                    const float w[4])
 {
        p->x = v1->x * w[0] + v2->x * w[1] + v3->x * w[2] + v4->x * w[3];
        p->y = v1->y * w[0] + v2->y * w[1] + v3->y * w[2] + v4->y * w[3];
        p->z = v1->z * w[0] + v2->z * w[1] + v3->z * w[2] + v4->z * w[3];
 }
 
-float shaperadius(float shape, float root, float tip, float time)
+static float shaperadius(float shape, float root, float tip, float time)
 {
        float radius = 1.0f - time;
-       
+
        if(shape != 0.0f) {
                if(shape < 0.0f)
                        radius = powf(radius, 1.0f + shape);
@@ -90,7 +74,13 @@ float shaperadius(float shape, float root, float tip, float time)
 
 /* curve functions */
 
-void InterpolateKeySegments(int seg, int segno, int key, int curve, float3 *keyloc, float *time, ParticleCurveData *CData)
+static void InterpolateKeySegments(int seg,
+                                   int segno,
+                                   int key,
+                                   int curve,
+                                   float3 *keyloc,
+                                   float *time,
+                                   ParticleCurveData *CData)
 {
        float3 ckey_loc1 = CData->curvekey_co[key];
        float3 ckey_loc2 = ckey_loc1;
@@ -119,7 +109,11 @@ void InterpolateKeySegments(int seg, int segno, int key, int curve, float3 *keyl
                curveinterp_v3_v3v3v3v3(keyloc, &ckey_loc1, &ckey_loc2, &ckey_loc3, &ckey_loc4, t);
 }
 
-bool ObtainCacheParticleData(Mesh *mesh, BL::Mesh *b_mesh, BL::Object *b_ob, ParticleCurveData *CData, bool background)
+static bool ObtainCacheParticleData(Mesh *mesh,
+                                    BL::Mesh *b_mesh,
+                                    BL::Object *b_ob,
+                                    ParticleCurveData *CData,
+                                    bool background)
 {
        int curvenum = 0;
        int keyno = 0;
@@ -138,51 +132,53 @@ bool ObtainCacheParticleData(Mesh *mesh, BL::Mesh *b_mesh, BL::Object *b_ob, Par
                        BL::ParticleSettings b_part((const PointerRNA)b_psys.settings().ptr);
 
                        if((b_part.render_type() == BL::ParticleSettings::render_type_PATH) && (b_part.type() == BL::ParticleSettings::type_HAIR)) {
-                               int mi = clamp(b_part.material()-1, 0, mesh->used_shaders.size()-1);
-                               int shader = mesh->used_shaders[mi];
+                               int shader = clamp(b_part.material()-1, 0, mesh->used_shaders.size()-1);
                                int draw_step = background ? b_part.render_step() : b_part.draw_step();
-                               int ren_step = (int)powf(2.0f, (float)draw_step);
                                int totparts = b_psys.particles.length();
                                int totchild = background ? b_psys.child_particles.length() : (int)((float)b_psys.child_particles.length() * (float)b_part.draw_percentage() / 100.0f);
                                int totcurves = totchild;
-                               
-                               if(b_part.child_type() == 0)
+
+                               if(b_part.child_type() == 0 || totchild == 0)
                                        totcurves += totparts;
 
                                if(totcurves == 0)
                                        continue;
 
+                               int ren_step = (1 << draw_step) + 1;
+                               if(b_part.kink() == BL::ParticleSettings::kink_SPIRAL)
+                                       ren_step += b_part.kink_extra_steps();
+
                                PointerRNA cpsys = RNA_pointer_get(&b_part.ptr, "cycles");
 
-                               CData->psys_firstcurve.push_back(curvenum);
-                               CData->psys_curvenum.push_back(totcurves);
-                               CData->psys_shader.push_back(shader);
+                               CData->psys_firstcurve.push_back_slow(curvenum);
+                               CData->psys_curvenum.push_back_slow(totcurves);
+                               CData->psys_shader.push_back_slow(shader);
 
                                float radius = get_float(cpsys, "radius_scale") * 0.5f;
-       
-                               CData->psys_rootradius.push_back(radius * get_float(cpsys, "root_width"));
-                               CData->psys_tipradius.push_back(radius * get_float(cpsys, "tip_width"));
-                               CData->psys_shape.push_back(get_float(cpsys, "shape"));
-                               CData->psys_closetip.push_back(get_boolean(cpsys, "use_closetip"));
+
+                               CData->psys_rootradius.push_back_slow(radius * get_float(cpsys, "root_width"));
+                               CData->psys_tipradius.push_back_slow(radius * get_float(cpsys, "tip_width"));
+                               CData->psys_shape.push_back_slow(get_float(cpsys, "shape"));
+                               CData->psys_closetip.push_back_slow(get_boolean(cpsys, "use_closetip"));
 
                                int pa_no = 0;
-                               if(!(b_part.child_type() == 0))
+                               if(!(b_part.child_type() == 0) && totchild != 0)
                                        pa_no = totparts;
 
                                int num_add = (totparts+totchild - pa_no);
                                CData->curve_firstkey.reserve(CData->curve_firstkey.size() + num_add);
                                CData->curve_keynum.reserve(CData->curve_keynum.size() + num_add);
                                CData->curve_length.reserve(CData->curve_length.size() + num_add);
-                               CData->curvekey_co.reserve(CData->curvekey_co.size() + num_add*(ren_step+1));
-                               CData->curvekey_time.reserve(CData->curvekey_time.size() + num_add*(ren_step+1));
+                               CData->curvekey_co.reserve(CData->curvekey_co.size() + num_add*ren_step);
+                               CData->curvekey_time.reserve(CData->curvekey_time.size() + num_add*ren_step);
 
                                for(; pa_no < totparts+totchild; pa_no++) {
                                        int keynum = 0;
-                                       CData->curve_firstkey.push_back(keyno);
-                                       
+                                       CData->curve_firstkey.push_back_slow(keyno);
+
                                        float curve_length = 0.0f;
                                        float3 pcKey;
-                                       for(int step_no = 0; step_no <= ren_step; step_no++) {
+                                       for(int step_no = 0; step_no < ren_step; step_no++) {
                                                float nco[3];
                                                b_psys.co_hair(*b_ob, pa_no, step_no, nco);
                                                float3 cKey = make_float3(nco[0], nco[1], nco[2]);
@@ -193,15 +189,15 @@ bool ObtainCacheParticleData(Mesh *mesh, BL::Mesh *b_mesh, BL::Object *b_ob, Par
                                                                continue;
                                                        curve_length += step_length;
                                                }
-                                               CData->curvekey_co.push_back(cKey);
-                                               CData->curvekey_time.push_back(curve_length);
+                                               CData->curvekey_co.push_back_slow(cKey);
+                                               CData->curvekey_time.push_back_slow(curve_length);
                                                pcKey = cKey;
-                                               keyno++;
                                                keynum++;
                                        }
+                                       keyno += keynum;
 
-                                       CData->curve_keynum.push_back(keynum);
-                                       CData->curve_length.push_back(curve_length);
+                                       CData->curve_keynum.push_back_slow(keynum);
+                                       CData->curve_length.push_back_slow(curve_length);
                                        curvenum++;
                                }
                        }
@@ -211,7 +207,12 @@ bool ObtainCacheParticleData(Mesh *mesh, BL::Mesh *b_mesh, BL::Object *b_ob, Par
        return true;
 }
 
-bool ObtainCacheParticleUV(Mesh *mesh, BL::Mesh *b_mesh, BL::Object *b_ob, ParticleCurveData *CData, bool background, int uv_num)
+static bool ObtainCacheParticleUV(Mesh *mesh,
+                                  BL::Mesh *b_mesh,
+                                  BL::Object *b_ob,
+                                  ParticleCurveData *CData,
+                                  bool background,
+                                  int uv_num)
 {
        if(!(mesh && b_mesh && b_ob && CData))
                return false;
@@ -229,15 +230,15 @@ bool ObtainCacheParticleUV(Mesh *mesh, BL::Mesh *b_mesh, BL::Object *b_ob, Parti
                                int totparts = b_psys.particles.length();
                                int totchild = background ? b_psys.child_particles.length() : (int)((float)b_psys.child_particles.length() * (float)b_part.draw_percentage() / 100.0f);
                                int totcurves = totchild;
-                               
-                               if (b_part.child_type() == 0)
+
+                               if(b_part.child_type() == 0 || totchild == 0)
                                        totcurves += totparts;
 
-                               if (totcurves == 0)
+                               if(totcurves == 0)
                                        continue;
 
                                int pa_no = 0;
-                               if(!(b_part.child_type() == 0))
+                               if(!(b_part.child_type() == 0) && totchild != 0)
                                        pa_no = totparts;
 
                                int num_add = (totparts+totchild - pa_no);
@@ -253,7 +254,7 @@ bool ObtainCacheParticleUV(Mesh *mesh, BL::Mesh *b_mesh, BL::Object *b_ob, Parti
                                        float3 uv = make_float3(0.0f, 0.0f, 0.0f);
                                        if(b_mesh->tessface_uv_textures.length())
                                                b_psys.uv_on_emitter(psmd, *b_pa, pa_no, uv_num, &uv.x);
-                                       CData->curve_uv.push_back(uv);
+                                       CData->curve_uv.push_back_slow(uv);
 
                                        if(pa_no < totparts && b_pa != b_psys.particles.end())
                                                ++b_pa;
@@ -265,7 +266,12 @@ bool ObtainCacheParticleUV(Mesh *mesh, BL::Mesh *b_mesh, BL::Object *b_ob, Parti
        return true;
 }
 
-bool ObtainCacheParticleVcol(Mesh *mesh, BL::Mesh *b_mesh, BL::Object *b_ob, ParticleCurveData *CData, bool background, int vcol_num)
+static bool ObtainCacheParticleVcol(Mesh *mesh,
+                                    BL::Mesh *b_mesh,
+                                    BL::Object *b_ob,
+                                    ParticleCurveData *CData,
+                                    bool background,
+                                    int vcol_num)
 {
        if(!(mesh && b_mesh && b_ob && CData))
                return false;
@@ -283,15 +289,15 @@ bool ObtainCacheParticleVcol(Mesh *mesh, BL::Mesh *b_mesh, BL::Object *b_ob, Par
                                int totparts = b_psys.particles.length();
                                int totchild = background ? b_psys.child_particles.length() : (int)((float)b_psys.child_particles.length() * (float)b_part.draw_percentage() / 100.0f);
                                int totcurves = totchild;
-                               
-                               if (b_part.child_type() == 0)
+
+                               if(b_part.child_type() == 0 || totchild == 0)
                                        totcurves += totparts;
 
-                               if (totcurves == 0)
+                               if(totcurves == 0)
                                        continue;
 
                                int pa_no = 0;
-                               if(!(b_part.child_type() == 0))
+                               if(!(b_part.child_type() == 0) && totchild != 0)
                                        pa_no = totparts;
 
                                int num_add = (totparts+totchild - pa_no);
@@ -307,7 +313,7 @@ bool ObtainCacheParticleVcol(Mesh *mesh, BL::Mesh *b_mesh, BL::Object *b_ob, Par
                                        float3 vcol = make_float3(0.0f, 0.0f, 0.0f);
                                        if(b_mesh->tessface_vertex_colors.length())
                                                b_psys.mcol_on_emitter(psmd, *b_pa, pa_no, vcol_num, &vcol.x);
-                                       CData->curve_vcol.push_back(vcol);
+                                       CData->curve_vcol.push_back_slow(vcol);
 
                                        if(pa_no < totparts && b_pa != b_psys.particles.end())
                                                ++b_pa;
@@ -319,11 +325,11 @@ bool ObtainCacheParticleVcol(Mesh *mesh, BL::Mesh *b_mesh, BL::Object *b_ob, Par
        return true;
 }
 
-static void set_resolution(Mesh *mesh, BL::Mesh *b_mesh, BL::Object *b_ob, BL::Scene *scene, bool render)
+static void set_resolution(BL::Object *b_ob, BL::Scene *scene, bool render)
 {
        BL::Object::modifiers_iterator b_mod;
        for(b_ob->modifiers.begin(b_mod); b_mod != b_ob->modifiers.end(); ++b_mod) {
-               if ((b_mod->type() == b_mod->type_PARTICLE_SYSTEM) && ((b_mod->show_viewport()) || (b_mod->show_render()))) {
+               if((b_mod->type() == b_mod->type_PARTICLE_SYSTEM) && ((b_mod->show_viewport()) || (b_mod->show_render()))) {
                        BL::ParticleSystemModifier psmd((const PointerRNA)b_mod->ptr);
                        BL::ParticleSystem b_psys((const PointerRNA)psmd.particle_system().ptr);
                        b_psys.set_resolution(*scene, *b_ob, (render)? 2: 1);
@@ -331,16 +337,16 @@ static void set_resolution(Mesh *mesh, BL::Mesh *b_mesh, BL::Object *b_ob, BL::S
        }
 }
 
-void ExportCurveTrianglePlanes(Mesh *mesh, ParticleCurveData *CData,
-                               float3 RotCam, bool is_ortho)
+static void ExportCurveTrianglePlanes(Mesh *mesh, ParticleCurveData *CData,
+                                      float3 RotCam, bool is_ortho)
 {
        int vertexno = mesh->verts.size();
        int vertexindex = vertexno;
        int numverts = 0, numtris = 0;
 
        /* compute and reserve size of arrays */
-       for(int sys = 0; sys < CData->psys_firstcurve.size() ; sys++) {
-               for(int curve = CData->psys_firstcurve[sys]; curve < CData->psys_firstcurve[sys] + CData->psys_curvenum[sys] ; curve++) {
+       for(int sys = 0; sys < CData->psys_firstcurve.size(); sys++) {
+               for(int curve = CData->psys_firstcurve[sys]; curve < CData->psys_firstcurve[sys] + CData->psys_curvenum[sys]; curve++) {
                        if(CData->curve_keynum[curve] <= 1 || CData->curve_length[curve] == 0.0f)
                                continue;
 
@@ -349,14 +355,11 @@ void ExportCurveTrianglePlanes(Mesh *mesh, ParticleCurveData *CData,
                }
        }
 
-       mesh->verts.reserve(mesh->verts.size() + numverts);
-       mesh->triangles.reserve(mesh->triangles.size() + numtris);
-       mesh->shader.reserve(mesh->shader.size() + numtris);
-       mesh->smooth.reserve(mesh->smooth.size() + numtris);
+       mesh->reserve_mesh(mesh->verts.size() + numverts, mesh->num_triangles() + numtris);
 
        /* actually export */
-       for(int sys = 0; sys < CData->psys_firstcurve.size() ; sys++) {
-               for(int curve = CData->psys_firstcurve[sys]; curve < CData->psys_firstcurve[sys] + CData->psys_curvenum[sys] ; curve++) {
+       for(int sys = 0; sys < CData->psys_firstcurve.size(); sys++) {
+               for(int curve = CData->psys_firstcurve[sys]; curve < CData->psys_firstcurve[sys] + CData->psys_curvenum[sys]; curve++) {
                        if(CData->curve_keynum[curve] <= 1 || CData->curve_length[curve] == 0.0f)
                                continue;
 
@@ -372,8 +375,8 @@ void ExportCurveTrianglePlanes(Mesh *mesh, ParticleCurveData *CData,
                                xbasis = normalize(cross(RotCam - ickey_loc, v1));
                        float3 ickey_loc_shfl = ickey_loc - radius * xbasis;
                        float3 ickey_loc_shfr = ickey_loc + radius * xbasis;
-                       mesh->verts.push_back(ickey_loc_shfl);
-                       mesh->verts.push_back(ickey_loc_shfr);
+                       mesh->add_vertex(ickey_loc_shfl);
+                       mesh->add_vertex(ickey_loc_shfr);
                        vertexindex += 2;
 
                        for(int curvekey = CData->curve_firstkey[curve] + 1; curvekey < CData->curve_firstkey[curve] + CData->curve_keynum[curve]; curvekey++) {
@@ -381,7 +384,7 @@ void ExportCurveTrianglePlanes(Mesh *mesh, ParticleCurveData *CData,
 
                                if(curvekey == CData->curve_firstkey[curve] + CData->curve_keynum[curve] - 1)
                                        v1 = CData->curvekey_co[curvekey] - CData->curvekey_co[max(curvekey - 1, CData->curve_firstkey[curve])];
-                               else 
+                               else
                                        v1 = CData->curvekey_co[curvekey + 1] - CData->curvekey_co[curvekey - 1];
 
                                time = CData->curvekey_time[curvekey]/CData->curve_length[curve];
@@ -399,8 +402,8 @@ void ExportCurveTrianglePlanes(Mesh *mesh, ParticleCurveData *CData,
                                        xbasis = normalize(cross(RotCam - ickey_loc, v1));
                                float3 ickey_loc_shfl = ickey_loc - radius * xbasis;
                                float3 ickey_loc_shfr = ickey_loc + radius * xbasis;
-                               mesh->verts.push_back(ickey_loc_shfl);
-                               mesh->verts.push_back(ickey_loc_shfr);
+                               mesh->add_vertex(ickey_loc_shfl);
+                               mesh->add_vertex(ickey_loc_shfr);
                                mesh->add_triangle(vertexindex-2, vertexindex, vertexindex-1, CData->psys_shader[sys], true);
                                mesh->add_triangle(vertexindex+1, vertexindex-1, vertexindex, CData->psys_shader[sys], true);
                                vertexindex += 2;
@@ -408,7 +411,7 @@ void ExportCurveTrianglePlanes(Mesh *mesh, ParticleCurveData *CData,
                }
        }
 
-       mesh->reserve(mesh->verts.size(), mesh->triangles.size(), 0, 0);
+       mesh->resize_mesh(mesh->verts.size(), mesh->num_triangles());
        mesh->attributes.remove(ATTR_STD_VERTEX_NORMAL);
        mesh->attributes.remove(ATTR_STD_FACE_NORMAL);
        mesh->add_face_normals();
@@ -418,31 +421,30 @@ void ExportCurveTrianglePlanes(Mesh *mesh, ParticleCurveData *CData,
        /* texture coords still needed */
 }
 
-void ExportCurveTriangleGeometry(Mesh *mesh, ParticleCurveData *CData, int resolution)
+static void ExportCurveTriangleGeometry(Mesh *mesh,
+                                        ParticleCurveData *CData,
+                                        int resolution)
 {
        int vertexno = mesh->verts.size();
        int vertexindex = vertexno;
        int numverts = 0, numtris = 0;
 
        /* compute and reserve size of arrays */
-       for(int sys = 0; sys < CData->psys_firstcurve.size() ; sys++) {
-               for(int curve = CData->psys_firstcurve[sys]; curve < CData->psys_firstcurve[sys] + CData->psys_curvenum[sys] ; curve++) {
+       for(int sys = 0; sys < CData->psys_firstcurve.size(); sys++) {
+               for(int curve = CData->psys_firstcurve[sys]; curve < CData->psys_firstcurve[sys] + CData->psys_curvenum[sys]; curve++) {
                        if(CData->curve_keynum[curve] <= 1 || CData->curve_length[curve] == 0.0f)
                                continue;
 
-                       numverts += (CData->curve_keynum[curve] - 2)*2*resolution + resolution;
-                       numtris += (CData->curve_keynum[curve] - 2)*resolution;
+                       numverts += (CData->curve_keynum[curve] - 1)*resolution + resolution;
+                       numtris += (CData->curve_keynum[curve] - 1)*2*resolution;
                }
        }
 
-       mesh->verts.reserve(mesh->verts.size() + numverts);
-       mesh->triangles.reserve(mesh->triangles.size() + numtris);
-       mesh->shader.reserve(mesh->shader.size() + numtris);
-       mesh->smooth.reserve(mesh->smooth.size() + numtris);
+       mesh->reserve_mesh(mesh->verts.size() + numverts, mesh->num_triangles() + numtris);
 
        /* actually export */
-       for(int sys = 0; sys < CData->psys_firstcurve.size() ; sys++) {
-               for(int curve = CData->psys_firstcurve[sys]; curve < CData->psys_firstcurve[sys] + CData->psys_curvenum[sys] ; curve++) {
+       for(int sys = 0; sys < CData->psys_firstcurve.size(); sys++) {
+               for(int curve = CData->psys_firstcurve[sys]; curve < CData->psys_firstcurve[sys] + CData->psys_curvenum[sys]; curve++) {
                        if(CData->curve_keynum[curve] <= 1 || CData->curve_length[curve] == 0.0f)
                                continue;
 
@@ -510,7 +512,7 @@ void ExportCurveTriangleGeometry(Mesh *mesh, ParticleCurveData *CData, int resol
 
                                ybasis = normalize(cross(xbasis, v2));
 
-                               for (; subv <= 1; subv++) {
+                               for(; subv <= 1; subv++) {
                                        float3 ickey_loc = make_float3(0.0f,0.0f,0.0f);
                                        float time = 0.0f;
 
@@ -527,7 +529,7 @@ void ExportCurveTriangleGeometry(Mesh *mesh, ParticleCurveData *CData, int resol
                                        float angle = M_2PI_F / (float)resolution;
                                        for(int section = 0; section < resolution; section++) {
                                                float3 ickey_loc_shf = ickey_loc + radius * (cosf(angle * section) * xbasis + sinf(angle * section) * ybasis);
-                                               mesh->verts.push_back(ickey_loc_shf);
+                                               mesh->add_vertex(ickey_loc_shf);
                                        }
 
                                        if(subv != 0) {
@@ -544,7 +546,7 @@ void ExportCurveTriangleGeometry(Mesh *mesh, ParticleCurveData *CData, int resol
                }
        }
 
-       mesh->reserve(mesh->verts.size(), mesh->triangles.size(), 0, 0);
+       mesh->resize_mesh(mesh->verts.size(), mesh->num_triangles());
        mesh->attributes.remove(ATTR_STD_VERTEX_NORMAL);
        mesh->attributes.remove(ATTR_STD_FACE_NORMAL);
        mesh->add_face_normals();
@@ -554,22 +556,22 @@ void ExportCurveTriangleGeometry(Mesh *mesh, ParticleCurveData *CData, int resol
        /* texture coords still needed */
 }
 
-void ExportCurveSegments(Scene *scene, Mesh *mesh, ParticleCurveData *CData)
+static void ExportCurveSegments(Scene *scene, Mesh *mesh, ParticleCurveData *CData)
 {
        int num_keys = 0;
        int num_curves = 0;
 
-       if(!(mesh->curves.empty() && mesh->curve_keys.empty()))
+       if(mesh->num_curves())
                return;
 
        Attribute *attr_intercept = NULL;
-       
+
        if(mesh->need_attribute(scene, ATTR_STD_CURVE_INTERCEPT))
                attr_intercept = mesh->curve_attributes.add(ATTR_STD_CURVE_INTERCEPT);
 
        /* compute and reserve size of arrays */
-       for(int sys = 0; sys < CData->psys_firstcurve.size() ; sys++) {
-               for(int curve = CData->psys_firstcurve[sys]; curve < CData->psys_firstcurve[sys] + CData->psys_curvenum[sys] ; curve++) {
+       for(int sys = 0; sys < CData->psys_firstcurve.size(); sys++) {
+               for(int curve = CData->psys_firstcurve[sys]; curve < CData->psys_firstcurve[sys] + CData->psys_curvenum[sys]; curve++) {
                        if(CData->curve_keynum[curve] <= 1 || CData->curve_length[curve] == 0.0f)
                                continue;
 
@@ -578,19 +580,18 @@ void ExportCurveSegments(Scene *scene, Mesh *mesh, ParticleCurveData *CData)
                }
        }
 
-       if (num_curves > 0) {
+       if(num_curves > 0) {
                VLOG(1) << "Exporting curve segments for mesh " << mesh->name;
        }
 
-       mesh->curve_keys.reserve(mesh->curve_keys.size() + num_keys);
-       mesh->curves.reserve(mesh->curves.size() + num_curves);
+       mesh->reserve_curves(mesh->num_curves() + num_curves, mesh->curve_keys.size() + num_keys);
 
        num_keys = 0;
        num_curves = 0;
 
        /* actually export */
-       for(int sys = 0; sys < CData->psys_firstcurve.size() ; sys++) {
-               for(int curve = CData->psys_firstcurve[sys]; curve < CData->psys_firstcurve[sys] + CData->psys_curvenum[sys] ; curve++) {
+       for(int sys = 0; sys < CData->psys_firstcurve.size(); sys++) {
+               for(int curve = CData->psys_firstcurve[sys]; curve < CData->psys_firstcurve[sys] + CData->psys_curvenum[sys]; curve++) {
                        if(CData->curve_keynum[curve] <= 1 || CData->curve_length[curve] == 0.0f)
                                continue;
 
@@ -611,22 +612,20 @@ void ExportCurveSegments(Scene *scene, Mesh *mesh, ParticleCurveData *CData)
                                num_curve_keys++;
                        }
 
-                       mesh->add_curve(num_keys, num_curve_keys, CData->psys_shader[sys]);
+                       mesh->add_curve(num_keys, CData->psys_shader[sys]);
                        num_keys += num_curve_keys;
                        num_curves++;
                }
        }
 
        /* check allocation */
-       if((mesh->curve_keys.size() !=  num_keys) || (mesh->curves.size() !=  num_curves)) {
+       if((mesh->curve_keys.size() != num_keys) || (mesh->num_curves() != num_curves)) {
                VLOG(1) << "Allocation failed, clearing data";
-               mesh->curve_keys.clear();
-               mesh->curves.clear();
-               mesh->curve_attributes.clear();
+               mesh->clear();
        }
 }
 
-static void ExportCurveSegmentsMotion(Scene *scene, Mesh *mesh, ParticleCurveData *CData, int time_index)
+static void ExportCurveSegmentsMotion(Mesh *mesh, ParticleCurveData *CData, int time_index)
 {
        VLOG(1) << "Exporting curve motion segments for mesh " << mesh->name
                << ", time index " << time_index;
@@ -665,13 +664,16 @@ static void ExportCurveSegmentsMotion(Scene *scene, Mesh *mesh, ParticleCurveDat
                                        if(CData->psys_closetip[sys] && (curvekey == CData->curve_firstkey[curve] + CData->curve_keynum[curve] - 1))
                                                radius = 0.0f;
 
+                                       /* curve motion keys store both position and radius in float4 */
                                        mP[i] = float3_to_float4(ickey_loc);
                                        mP[i].w = radius;
 
                                        /* unlike mesh coordinates, these tend to be slightly different
                                         * between frames due to particle transforms into/out of object
                                         * space, so we use an epsilon to detect actual changes */
-                                       if(len_squared(mP[i] - mesh->curve_keys[i]) > 1e-5f*1e-5f)
+                                       float4 curve_key = float3_to_float4(mesh->curve_keys[i]);
+                                       curve_key.w = mesh->curve_radius[i];
+                                       if(len_squared(mP[i] - curve_key) > 1e-5f*1e-5f)
                                                have_motion = true;
                                }
 
@@ -683,8 +685,13 @@ static void ExportCurveSegmentsMotion(Scene *scene, Mesh *mesh, ParticleCurveDat
        /* in case of new attribute, we verify if there really was any motion */
        if(new_attribute) {
                if(i != numkeys || !have_motion) {
-                       /* no motion, remove attributes again */
-                       VLOG(1) << "No motion, removing attribute";
+                       /* No motion or hair "topology" changed, remove attributes again. */
+                       if(i != numkeys) {
+                               VLOG(1) << "Hair topology changed, removing attribute.";
+                       }
+                       else {
+                               VLOG(1) << "No motion, removing attribute.";
+                       }
                        mesh->curve_attributes.remove(ATTR_STD_MOTION_VERTEX_POSITION);
                }
                else if(time_index > 0) {
@@ -695,14 +702,19 @@ static void ExportCurveSegmentsMotion(Scene *scene, Mesh *mesh, ParticleCurveDat
                        for(int step = 0; step < time_index; step++) {
                                float4 *mP = attr_mP->data_float4() + step*numkeys;
 
-                               for(int key = 0; key < numkeys; key++)
-                                       mP[key] = mesh->curve_keys[key];
+                               for(int key = 0; key < numkeys; key++) {
+                                       mP[key] = float3_to_float4(mesh->curve_keys[key]);
+                                       mP[key].w = mesh->curve_radius[key];
+                               }
                        }
                }
        }
 }
 
-void ExportCurveTriangleUV(Mesh *mesh, ParticleCurveData *CData, int vert_offset, int resol, float3 *uvdata)
+static void ExportCurveTriangleUV(ParticleCurveData *CData,
+                                  int vert_offset,
+                                  int resol,
+                                  float3 *uvdata)
 {
        if(uvdata == NULL)
                return;
@@ -712,8 +724,8 @@ void ExportCurveTriangleUV(Mesh *mesh, ParticleCurveData *CData, int vert_offset
 
        int vertexindex = vert_offset;
 
-       for(int sys = 0; sys < CData->psys_firstcurve.size() ; sys++) {
-               for(int curve = CData->psys_firstcurve[sys]; curve < CData->psys_firstcurve[sys] + CData->psys_curvenum[sys] ; curve++) {
+       for(int sys = 0; sys < CData->psys_firstcurve.size(); sys++) {
+               for(int curve = CData->psys_firstcurve[sys]; curve < CData->psys_firstcurve[sys] + CData->psys_curvenum[sys]; curve++) {
                        if(CData->curve_keynum[curve] <= 1 || CData->curve_length[curve] == 0.0f)
                                continue;
 
@@ -747,31 +759,34 @@ void ExportCurveTriangleUV(Mesh *mesh, ParticleCurveData *CData, int vert_offset
        }
 }
 
-void ExportCurveTriangleVcol(Mesh *mesh, ParticleCurveData *CData, int vert_offset, int resol, uchar4 *cdata)
+static void ExportCurveTriangleVcol(ParticleCurveData *CData,
+                                    int vert_offset,
+                                    int resol,
+                                    uchar4 *cdata)
 {
        if(cdata == NULL)
                return;
 
        int vertexindex = vert_offset;
 
-       for(int sys = 0; sys < CData->psys_firstcurve.size() ; sys++) {
-               for(int curve = CData->psys_firstcurve[sys]; curve < CData->psys_firstcurve[sys] + CData->psys_curvenum[sys] ; curve++) {
+       for(int sys = 0; sys < CData->psys_firstcurve.size(); sys++) {
+               for(int curve = CData->psys_firstcurve[sys]; curve < CData->psys_firstcurve[sys] + CData->psys_curvenum[sys]; curve++) {
                        if(CData->curve_keynum[curve] <= 1 || CData->curve_length[curve] == 0.0f)
                                continue;
 
                        for(int curvekey = CData->curve_firstkey[curve]; curvekey < CData->curve_firstkey[curve] + CData->curve_keynum[curve] - 1; curvekey++) {
                                for(int section = 0; section < resol; section++) {
-                                       cdata[vertexindex] = color_float_to_byte(color_srgb_to_scene_linear(CData->curve_vcol[curve]));
+                                       cdata[vertexindex] = color_float_to_byte(color_srgb_to_scene_linear_v3(CData->curve_vcol[curve]));
                                        vertexindex++;
-                                       cdata[vertexindex] = color_float_to_byte(color_srgb_to_scene_linear(CData->curve_vcol[curve]));
+                                       cdata[vertexindex] = color_float_to_byte(color_srgb_to_scene_linear_v3(CData->curve_vcol[curve]));
                                        vertexindex++;
-                                       cdata[vertexindex] = color_float_to_byte(color_srgb_to_scene_linear(CData->curve_vcol[curve]));
+                                       cdata[vertexindex] = color_float_to_byte(color_srgb_to_scene_linear_v3(CData->curve_vcol[curve]));
                                        vertexindex++;
-                                       cdata[vertexindex] = color_float_to_byte(color_srgb_to_scene_linear(CData->curve_vcol[curve]));
+                                       cdata[vertexindex] = color_float_to_byte(color_srgb_to_scene_linear_v3(CData->curve_vcol[curve]));
                                        vertexindex++;
-                                       cdata[vertexindex] = color_float_to_byte(color_srgb_to_scene_linear(CData->curve_vcol[curve]));
+                                       cdata[vertexindex] = color_float_to_byte(color_srgb_to_scene_linear_v3(CData->curve_vcol[curve]));
                                        vertexindex++;
-                                       cdata[vertexindex] = color_float_to_byte(color_srgb_to_scene_linear(CData->curve_vcol[curve]));
+                                       cdata[vertexindex] = color_float_to_byte(color_srgb_to_scene_linear_v3(CData->curve_vcol[curve]));
                                        vertexindex++;
                                }
                        }
@@ -792,36 +807,52 @@ void BlenderSync::sync_curve_settings()
        curve_system_manager->minimum_width = get_float(csscene, "minimum_width");
        curve_system_manager->maximum_width = get_float(csscene, "maximum_width");
 
-       curve_system_manager->primitive = get_enum(csscene, "primitive");
-       curve_system_manager->curve_shape = get_enum(csscene, "shape");
+       curve_system_manager->primitive =
+               (CurvePrimitiveType)get_enum(csscene,
+                                            "primitive",
+                                            CURVE_NUM_PRIMITIVE_TYPES,
+                                            CURVE_LINE_SEGMENTS);
+       curve_system_manager->curve_shape =
+               (CurveShapeType)get_enum(csscene,
+                                        "shape",
+                                        CURVE_NUM_SHAPE_TYPES,
+                                        CURVE_THICK);
        curve_system_manager->resolution = get_int(csscene, "resolution");
        curve_system_manager->subdivisions = get_int(csscene, "subdivisions");
        curve_system_manager->use_backfacing = !get_boolean(csscene, "cull_backfacing");
 
-       if(curve_system_manager->primitive == CURVE_TRIANGLES && curve_system_manager->curve_shape == CURVE_RIBBON) {
-               /* camera facing planes */
-               curve_system_manager->triangle_method = CURVE_CAMERA_TRIANGLES;
-               curve_system_manager->resolution = 1;
-       }
-       else if(curve_system_manager->primitive == CURVE_TRIANGLES && curve_system_manager->curve_shape == CURVE_THICK) {
+       /* Triangles */
+       if(curve_system_manager->primitive == CURVE_TRIANGLES) {
                /* camera facing planes */
-               curve_system_manager->triangle_method = CURVE_TESSELATED_TRIANGLES;
-       }
-       else if(curve_system_manager->primitive == CURVE_LINE_SEGMENTS && curve_system_manager->curve_shape == CURVE_RIBBON) {
-               /* tangent shading */
-               curve_system_manager->line_method = CURVE_UNCORRECTED;
-               curve_system_manager->use_encasing = true;
-               curve_system_manager->use_backfacing = false;
-               curve_system_manager->use_tangent_normal_geometry = true;
+               if(curve_system_manager->curve_shape == CURVE_RIBBON) {
+                       curve_system_manager->triangle_method = CURVE_CAMERA_TRIANGLES;
+                       curve_system_manager->resolution = 1;
+               }
+               else if(curve_system_manager->curve_shape == CURVE_THICK) {
+                       curve_system_manager->triangle_method = CURVE_TESSELATED_TRIANGLES;
+               }
        }
-       else if(curve_system_manager->primitive == CURVE_LINE_SEGMENTS && curve_system_manager->curve_shape == CURVE_THICK) {
-               curve_system_manager->line_method = CURVE_ACCURATE;
-               curve_system_manager->use_encasing = false;
-               curve_system_manager->use_tangent_normal_geometry = false;
+       /* Line Segments */
+       else if(curve_system_manager->primitive == CURVE_LINE_SEGMENTS) {
+               if(curve_system_manager->curve_shape == CURVE_RIBBON) {
+                       /* tangent shading */
+                       curve_system_manager->line_method = CURVE_UNCORRECTED;
+                       curve_system_manager->use_encasing = true;
+                       curve_system_manager->use_backfacing = false;
+                       curve_system_manager->use_tangent_normal_geometry = true;
+               }
+               else if(curve_system_manager->curve_shape == CURVE_THICK) {
+                       curve_system_manager->line_method = CURVE_ACCURATE;
+                       curve_system_manager->use_encasing = false;
+                       curve_system_manager->use_tangent_normal_geometry = false;
+               }
        }
-       else if(curve_system_manager->primitive == CURVE_SEGMENTS && curve_system_manager->curve_shape == CURVE_RIBBON) {
-               curve_system_manager->primitive = CURVE_RIBBONS;
-               curve_system_manager->use_backfacing = false;
+       /* Curve Segments */
+       else if(curve_system_manager->primitive == CURVE_SEGMENTS) {
+               if(curve_system_manager->curve_shape == CURVE_RIBBON) {
+                       curve_system_manager->primitive = CURVE_RIBBONS;
+                       curve_system_manager->use_backfacing = false;
+               }
        }
 
        if(curve_system_manager->modified_mesh(prev_curve_system_manager)) {
@@ -845,17 +876,23 @@ void BlenderSync::sync_curve_settings()
                curve_system_manager->tag_update(scene);
 }
 
-void BlenderSync::sync_curves(Mesh *mesh, BL::Mesh b_mesh, BL::Object b_ob, bool motion, int time_index)
+void BlenderSync::sync_curves(Mesh *mesh,
+                              BL::Mesh& b_mesh,
+                              BL::Object& b_ob,
+                              bool motion,
+                              int time_index)
 {
        if(!motion) {
                /* Clear stored curve data */
                mesh->curve_keys.clear();
-               mesh->curves.clear();
+               mesh->curve_radius.clear();
+               mesh->curve_first_key.clear();
+               mesh->curve_shader.clear();
                mesh->curve_attributes.clear();
        }
 
        /* obtain general settings */
-       bool use_curves = scene->curve_system_manager->use_curves;
+       const bool use_curves = scene->curve_system_manager->use_curves;
 
        if(!(use_curves && b_ob.mode() != b_ob.mode_PARTICLE_EDIT)) {
                if(!motion)
@@ -863,11 +900,11 @@ void BlenderSync::sync_curves(Mesh *mesh, BL::Mesh b_mesh, BL::Object b_ob, bool
                return;
        }
 
-       int primitive = scene->curve_system_manager->primitive;
-       int triangle_method = scene->curve_system_manager->triangle_method;
-       int resolution = scene->curve_system_manager->resolution;
-       size_t vert_num = mesh->verts.size();
-       size_t tri_num = mesh->triangles.size();
+       const int primitive = scene->curve_system_manager->primitive;
+       const int triangle_method = scene->curve_system_manager->triangle_method;
+       const int resolution = scene->curve_system_manager->resolution;
+       const size_t vert_num = mesh->verts.size();
+       const size_t tri_num = mesh->num_triangles();
        int used_res = 1;
 
        /* extract particle hair data - should be combined with connecting to mesh later*/
@@ -875,7 +912,7 @@ void BlenderSync::sync_curves(Mesh *mesh, BL::Mesh b_mesh, BL::Object b_ob, bool
        ParticleCurveData CData;
 
        if(!preview)
-               set_resolution(mesh, &b_mesh, &b_ob, &b_scene, true);
+               set_resolution(&b_ob, &b_scene, true);
 
        ObtainCacheParticleData(mesh, &b_mesh, &b_ob, &CData, !preview);
 
@@ -906,7 +943,7 @@ void BlenderSync::sync_curves(Mesh *mesh, BL::Mesh b_mesh, BL::Object b_ob, bool
        }
        else {
                if(motion)
-                       ExportCurveSegmentsMotion(scene, mesh, &CData, time_index);
+                       ExportCurveSegmentsMotion(mesh, &CData, time_index);
                else
                        ExportCurveSegments(scene, mesh, &CData);
        }
@@ -928,11 +965,10 @@ void BlenderSync::sync_curves(Mesh *mesh, BL::Mesh b_mesh, BL::Object b_ob, bool
                        else {
                                Attribute *attr_generated = mesh->curve_attributes.add(ATTR_STD_GENERATED);
                                float3 *generated = attr_generated->data_float3();
-                               size_t i = 0;
 
-                               foreach(Mesh::Curve& curve, mesh->curves) {
-                                       float3 co = float4_to_float3(mesh->curve_keys[curve.first_key]);
-                                       generated[i++] = co*size - loc;
+                               for(size_t i = 0; i < mesh->num_curves(); i++) {
+                                       float3 co = mesh->curve_keys[mesh->get_curve(i).first_key];
+                                       generated[i] = co*size - loc;
                                }
                        }
                }
@@ -955,7 +991,7 @@ void BlenderSync::sync_curves(Mesh *mesh, BL::Mesh b_mesh, BL::Object b_ob, bool
 
                                uchar4 *cdata = attr_vcol->data_uchar4();
 
-                               ExportCurveTriangleVcol(mesh, &CData, tri_num * 3, used_res, cdata);
+                               ExportCurveTriangleVcol(&CData, tri_num * 3, used_res, cdata);
                        }
                        else {
                                Attribute *attr_vcol = mesh->curve_attributes.add(
@@ -968,7 +1004,7 @@ void BlenderSync::sync_curves(Mesh *mesh, BL::Mesh b_mesh, BL::Object b_ob, bool
 
                                        for(size_t curve = 0; curve < CData.curve_vcol.size(); curve++)
                                                if(!(CData.curve_keynum[curve] <= 1 || CData.curve_length[curve] == 0.0f))
-                                                       fdata[i++] = color_srgb_to_scene_linear(CData.curve_vcol[curve]);
+                                                       fdata[i++] = color_srgb_to_scene_linear_v3(CData.curve_vcol[curve]);
                                }
                        }
                }
@@ -998,7 +1034,7 @@ void BlenderSync::sync_curves(Mesh *mesh, BL::Mesh b_mesh, BL::Object b_ob, bool
 
                                        float3 *uv = attr_uv->data_float3();
 
-                                       ExportCurveTriangleUV(mesh, &CData, tri_num * 3, used_res, uv);
+                                       ExportCurveTriangleUV(&CData, tri_num * 3, used_res, uv);
                                }
                                else {
                                        if(active_render)
@@ -1021,10 +1057,9 @@ void BlenderSync::sync_curves(Mesh *mesh, BL::Mesh b_mesh, BL::Object b_ob, bool
        }
 
        if(!preview)
-               set_resolution(mesh, &b_mesh, &b_ob, &b_scene, false);
+               set_resolution(&b_ob, &b_scene, false);
 
        mesh->compute_bounds();
 }
 
 CCL_NAMESPACE_END
-