Commit: d5be72b05f6be3e0a7d126d7f43bef386d532811
Author: Lukas Tönne
Date:   Tue Apr 7 18:12:04 2015 +0200
Branches: alembic
https://developer.blender.org/rBd5be72b05f6be3e0a7d126d7f43bef386d532811

Nicer iterator for looping over strand edges directly.

Avoids the ugly construct with vertex iterators that have to drag an
extra 'prev' iterator along.

===================================================================

M       source/blender/blenkernel/BKE_strands.h
M       source/blender/physics/intern/BPH_mass_spring.cpp

===================================================================

diff --git a/source/blender/blenkernel/BKE_strands.h 
b/source/blender/blenkernel/BKE_strands.h
index 1ce3c57..7462e18 100644
--- a/source/blender/blenkernel/BKE_strands.h
+++ b/source/blender/blenkernel/BKE_strands.h
@@ -140,4 +140,47 @@ BLI_INLINE size_t 
BKE_strand_vertex_iter_vertex_offset(Strands *strands, StrandV
        return iter->vertex - strands->verts;
 }
 
+
+typedef struct StrandEdgeIterator {
+       int index, tot;
+       StrandsVertex *vertex0, *vertex1;
+       StrandsMotionState *state0, *state1;
+} StrandEdgeIterator;
+
+BLI_INLINE void BKE_strand_edge_iter_init(StrandEdgeIterator *iter, 
StrandIterator *strand_iter)
+{
+       iter->tot = strand_iter->curve->numverts - 1;
+       iter->index = 0;
+       iter->vertex0 = strand_iter->verts;
+       iter->state0 = strand_iter->state;
+       iter->vertex1 = strand_iter->verts + 1;
+       iter->state1 = strand_iter->state + 1;
+}
+
+BLI_INLINE bool BKE_strand_edge_iter_valid(StrandEdgeIterator *iter)
+{
+       return iter->index < iter->tot;
+}
+
+BLI_INLINE void BKE_strand_edge_iter_next(StrandEdgeIterator *iter)
+{
+       ++iter->vertex0;
+       ++iter->vertex1;
+       if (iter->state0) {
+               ++iter->state0;
+               ++iter->state1;
+       }
+       ++iter->index;
+}
+
+BLI_INLINE size_t BKE_strand_edge_iter_vertex0_offset(Strands *strands, 
StrandEdgeIterator *iter)
+{
+       return iter->vertex0 - strands->verts;
+}
+
+BLI_INLINE size_t BKE_strand_edge_iter_vertex1_offset(Strands *strands, 
StrandEdgeIterator *iter)
+{
+       return iter->vertex1 - strands->verts;
+}
+
 #endif  /* __BKE_STRANDS_H__ */
diff --git a/source/blender/physics/intern/BPH_mass_spring.cpp 
b/source/blender/physics/intern/BPH_mass_spring.cpp
index 53f3718..5c1aa6c 100644
--- a/source/blender/physics/intern/BPH_mass_spring.cpp
+++ b/source/blender/physics/intern/BPH_mass_spring.cpp
@@ -1197,6 +1197,28 @@ static void strands_setup_constraints(Strands *strands, 
Implicit_Data *data, Col
 #endif
 }
 
+/* calculates internal forces for a single strand curve */
+static void strands_calc_curve_stretch_forces(Strands *strands, HairSimParams 
*params, Implicit_Data *data, StrandIterator *it_strand)
+{
+       StrandEdgeIterator it_edge;
+       
+       for (BKE_strand_edge_iter_init(&it_edge, it_strand); 
BKE_strand_edge_iter_valid(&it_edge); BKE_strand_edge_iter_next(&it_edge)) {
+               int vi = BKE_strand_edge_iter_vertex0_offset(strands, &it_edge);
+               int vj = BKE_strand_edge_iter_vertex1_offset(strands, &it_edge);
+               float restlen = len_v3v3(it_edge.vertex0->co, 
it_edge.vertex1->co);
+               
+               float stiffness = params->stretch_stiffness;
+               float damping = stiffness * params->stretch_damping;
+               BPH_mass_spring_force_spring_linear(data, vi, vj, restlen, 
stiffness, damping, true, 0.0f, NULL, NULL, NULL);
+       }
+}
+
+/* calculates internal forces for a single strand curve */
+static void strands_calc_curve_forces(Strands *strands, HairSimParams *params, 
Implicit_Data *data, StrandIterator *it_strand)
+{
+       strands_calc_curve_stretch_forces(strands, params, data, it_strand);
+}
+
 /* Collect forces and derivatives:  F, dFdX, dFdV */
 static void strands_calc_force(Strands *strands, HairSimParams *params, 
Implicit_Data *data, float UNUSED(frame), Scene *scene, ListBase *effectors)
 {
@@ -1258,22 +1280,7 @@ static void strands_calc_force(Strands *strands, 
HairSimParams *params, Implicit
        /* spring forces */
        StrandIterator it_strand;
        for (BKE_strand_iter_init(&it_strand, strands); 
BKE_strand_iter_valid(&it_strand); BKE_strand_iter_next(&it_strand)) {
-               StrandVertexIterator it_vert, it_vert_prev;
-               BKE_strand_vertex_iter_init(&it_vert, &it_strand);
-               if (BKE_strand_vertex_iter_valid(&it_vert)) {
-                       it_vert_prev = it_vert;
-                       BKE_strand_vertex_iter_next(&it_vert);
-                       for (; BKE_strand_vertex_iter_valid(&it_vert); 
it_vert_prev = it_vert, BKE_strand_vertex_iter_next(&it_vert)) {
-                               
-                               int vi = 
BKE_strand_vertex_iter_vertex_offset(strands, &it_vert);
-                               int vj = 
BKE_strand_vertex_iter_vertex_offset(strands, &it_vert_prev);
-                               float restlen = len_v3v3(it_vert.vertex->co, 
it_vert_prev.vertex->co);
-                               
-                               float stiffness = params->stretch_stiffness;
-                               float damping = stiffness * 
params->stretch_damping;
-                               BPH_mass_spring_force_spring_linear(data, vi, 
vj, restlen, stiffness, damping, true, 0.0f, NULL, NULL, NULL);
-                       }
-               }
+               strands_calc_curve_forces(strands, params, data, &it_strand);
        }
 }

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to