[Bf-blender-cvs] [702d6a9] hair_system: Basic solver class, to perform iterative simulation of hair (eventually).

Lukas Tönne noreply at git.blender.org
Sun Jul 27 17:05:35 CEST 2014


Commit: 702d6a91c5d66741798d141285f68c00cc21f429
Author: Lukas Tönne
Date:   Sun Jul 27 14:00:01 2014 +0200
Branches: hair_system
https://developer.blender.org/rB702d6a91c5d66741798d141285f68c00cc21f429

Basic solver class, to perform iterative simulation of hair (eventually).

Data from DNA can be copied into a SolverData struct for optimized
solver access. This should happen any time the data layout is changed in
the DNA (points/hairs added or removed), since the solver works on fixed
datasets only. After each time step the solver result can be applied
back to the DNA data for display, tools etc.

It may be beneficial to keep the solver and its data around between time
steps, so the data does not get deleted automatically after applying.
Initializing the data, applying and freeing are separate functions.

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

M	source/blender/editors/space_view3d/drawhair.c
M	source/blender/hair/CMakeLists.txt
M	source/blender/hair/HAIR_capi.cpp
M	source/blender/hair/HAIR_capi.h
M	source/blender/hair/intern/HAIR_curve.cpp
M	source/blender/hair/intern/HAIR_curve.h
A	source/blender/hair/intern/HAIR_solver.cpp
A	source/blender/hair/intern/HAIR_solver.h

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

diff --git a/source/blender/editors/space_view3d/drawhair.c b/source/blender/editors/space_view3d/drawhair.c
index 3dd989d..b8d0153 100644
--- a/source/blender/editors/space_view3d/drawhair.c
+++ b/source/blender/editors/space_view3d/drawhair.c
@@ -71,7 +71,7 @@ static void draw_hair_curve(HairSystem *hsys, HairCurve *hair)
 	
 	/* smoothed curve */
 	if (hair->totpoints >= 2) {
-		struct SmoothingIteratorFloat3 *iter;
+		struct HAIR_SmoothingIteratorFloat3 *iter;
 		float smooth_co[3];
 		
 		glColor3f(0.5f, 1.0f, 0.1f);
diff --git a/source/blender/hair/CMakeLists.txt b/source/blender/hair/CMakeLists.txt
index 3bcc367..7529cad 100644
--- a/source/blender/hair/CMakeLists.txt
+++ b/source/blender/hair/CMakeLists.txt
@@ -45,6 +45,8 @@ set(SRC
 	intern/HAIR_memalloc.h
 	intern/HAIR_smoothing.h
 	intern/HAIR_smoothing.cpp
+	intern/HAIR_solver.h
+	intern/HAIR_solver.cpp
 	intern/HAIR_types.h
 )
 
diff --git a/source/blender/hair/HAIR_capi.cpp b/source/blender/hair/HAIR_capi.cpp
index 93a34b3..8197976 100644
--- a/source/blender/hair/HAIR_capi.cpp
+++ b/source/blender/hair/HAIR_capi.cpp
@@ -26,16 +26,87 @@
 
 extern "C" {
 #include "DNA_hair_types.h"
+
+#include "BKE_hair.h"
 }
 
 #include "HAIR_capi.h"
 
 #include "HAIR_smoothing.h"
+#include "HAIR_solver.h"
 #include "HAIR_types.h"
 
 using namespace HAIR_NAMESPACE;
 
-struct SmoothingIteratorFloat3 *HAIR_smoothing_iter_new(HairCurve *curve, float rest_length, float amount, float cval[3])
+struct HAIR_Solver *HAIR_solver_new()
+{
+	Solver *solver = new Solver();
+	
+	return (HAIR_Solver *)solver;
+}
+
+void HAIR_solver_free(struct HAIR_Solver *csolver)
+{
+	Solver *solver = (Solver *)csolver;
+	
+	delete solver;
+}
+
+void HAIR_solver_init(struct HAIR_Solver *csolver, HairSystem *hsys)
+{
+	Solver *solver = (Solver *)csolver;
+	HairCurve *hair;
+	int i;
+	
+	/* count points */
+	int totpoints = 0;
+	for (hair = hsys->curves, i = 0; i < hsys->totcurves; ++hair, ++i) {
+		totpoints += hair->totpoints;
+	}
+	
+	/* allocate data */
+	solver->init_data(hsys->totcurves, totpoints);
+	Curve *solver_curves = solver->data().curves;
+	Point *solver_points = solver->data().points;
+	
+	/* copy data to solver data */
+	Point *point = solver_points;
+	for (hair = hsys->curves, i = 0; i < hsys->totcurves; ++hair, ++i) {
+		solver_curves[i] = Curve(hair->totpoints, point);
+		
+		for (int k = 0; k < hair->totpoints; ++k, ++point) {
+			HairPoint *hair_pt = hair->points + k;
+			*point = Point(float3(hair_pt->co[0], hair_pt->co[1], hair_pt->co[2]));
+		}
+	}
+}
+
+void HAIR_solver_apply(struct HAIR_Solver *csolver, HairSystem *hsys)
+{
+	Solver *solver = (Solver *)csolver;
+	int i;
+	
+	Curve *solver_curves = solver->data().curves;
+	int totcurves = solver->data().totcurves;
+	
+	/* copy solver data to DNA */
+	for (i = 0; i < totcurves && i < hsys->totcurves; ++i) {
+		Curve *curve = solver_curves + i;
+		HairCurve *hcurve = hsys->curves + i;
+		
+		for (int k = 0; k < curve->totpoints && k < hcurve->totpoints; ++k) {
+			Point *point = curve->points + k;
+			HairPoint *hpoint = hcurve->points + k;
+			
+			hpoint->co[0] = point->co.x;
+			hpoint->co[1] = point->co.y;
+			hpoint->co[2] = point->co.z;
+		}
+	}
+}
+
+
+struct HAIR_SmoothingIteratorFloat3 *HAIR_smoothing_iter_new(HairCurve *curve, float rest_length, float amount, float cval[3])
 {
 	SmoothingIterator<float3> *iter = new SmoothingIterator<float3>(rest_length, amount);
 	
@@ -56,24 +127,24 @@ struct SmoothingIteratorFloat3 *HAIR_smoothing_iter_new(HairCurve *curve, float
 	else
 		iter->num = 1;
 	
-	return (struct SmoothingIteratorFloat3 *)iter;
+	return (struct HAIR_SmoothingIteratorFloat3 *)iter;
 }
 
-void HAIR_smoothing_iter_free(struct SmoothingIteratorFloat3 *citer)
+void HAIR_smoothing_iter_free(struct HAIR_SmoothingIteratorFloat3 *citer)
 {
 	SmoothingIterator<float3> *iter = (SmoothingIterator<float3> *)citer;
 	
 	delete iter;
 }
 
-bool HAIR_smoothing_iter_valid(HairCurve *curve, struct SmoothingIteratorFloat3 *citer)
+bool HAIR_smoothing_iter_valid(HairCurve *curve, struct HAIR_SmoothingIteratorFloat3 *citer)
 {
 	SmoothingIterator<float3> *iter = (SmoothingIterator<float3> *)citer;
 	
 	return iter->num < curve->totpoints;
 }
 
-void HAIR_smoothing_iter_next(HairCurve *curve, struct SmoothingIteratorFloat3 *citer, float cval[3])
+void HAIR_smoothing_iter_next(HairCurve *curve, struct HAIR_SmoothingIteratorFloat3 *citer, float cval[3])
 {
 	SmoothingIterator<float3> *iter = (SmoothingIterator<float3> *)citer;
 	
@@ -84,7 +155,7 @@ void HAIR_smoothing_iter_next(HairCurve *curve, struct SmoothingIteratorFloat3 *
 	cval[2] = val.z;
 }
 
-void HAIR_smoothing_iter_end(HairCurve *curve, struct SmoothingIteratorFloat3 *citer, float cval[3])
+void HAIR_smoothing_iter_end(HairCurve *curve, struct HAIR_SmoothingIteratorFloat3 *citer, float cval[3])
 {
 	SmoothingIterator<float3> *iter = (SmoothingIterator<float3> *)citer;
 	
diff --git a/source/blender/hair/HAIR_capi.h b/source/blender/hair/HAIR_capi.h
index 779d22d..8ccb2ae 100644
--- a/source/blender/hair/HAIR_capi.h
+++ b/source/blender/hair/HAIR_capi.h
@@ -29,14 +29,21 @@ extern "C" {
 #endif
 
 struct HairCurve;
+struct HairSystem;
 
-struct SmoothingIteratorFloat3;
+struct HAIR_Solver;
+struct HAIR_SmoothingIteratorFloat3;
 
-struct SmoothingIteratorFloat3 *HAIR_smoothing_iter_new(struct HairCurve *curve, float rest_length, float amount, float cval[3]);
-void HAIR_smoothing_iter_free(struct SmoothingIteratorFloat3 *iter);
-bool HAIR_smoothing_iter_valid(struct HairCurve *curve, struct SmoothingIteratorFloat3 *iter);
-void HAIR_smoothing_iter_next(struct HairCurve *curve, struct SmoothingIteratorFloat3 *iter, float val[3]);
-void HAIR_smoothing_iter_end(struct HairCurve *curve, struct SmoothingIteratorFloat3 *citer, float cval[3]);
+struct HAIR_Solver *HAIR_solver_new();
+void HAIR_solver_free(struct HAIR_Solver *solver);
+void HAIR_solver_init(struct HAIR_Solver *solver, struct HairSystem *hsys);
+void HAIR_solver_apply(struct HAIR_Solver *solver, struct HairSystem *hsys);
+
+struct HAIR_SmoothingIteratorFloat3 *HAIR_smoothing_iter_new(struct HairCurve *curve, float rest_length, float amount, float cval[3]);
+void HAIR_smoothing_iter_free(struct HAIR_SmoothingIteratorFloat3 *iter);
+bool HAIR_smoothing_iter_valid(struct HairCurve *curve, struct HAIR_SmoothingIteratorFloat3 *iter);
+void HAIR_smoothing_iter_next(struct HairCurve *curve, struct HAIR_SmoothingIteratorFloat3 *iter, float val[3]);
+void HAIR_smoothing_iter_end(struct HairCurve *curve, struct HAIR_SmoothingIteratorFloat3 *citer, float cval[3]);
 
 #ifdef __cplusplus
 }
diff --git a/source/blender/hair/intern/HAIR_curve.cpp b/source/blender/hair/intern/HAIR_curve.cpp
index a5de85d..8a9cfb1 100644
--- a/source/blender/hair/intern/HAIR_curve.cpp
+++ b/source/blender/hair/intern/HAIR_curve.cpp
@@ -28,6 +28,10 @@
 
 HAIR_NAMESPACE_BEGIN
 
+Point::Point()
+{
+}
+
 Point::Point(const float3 &co) :
     co(co)
 {
@@ -39,4 +43,10 @@ Curve::Curve(int totpoints, Point *points) :
 {
 }
 
+Curve::Curve() :
+    points(NULL),
+    totpoints(0)
+{
+}
+
 HAIR_NAMESPACE_END
diff --git a/source/blender/hair/intern/HAIR_curve.h b/source/blender/hair/intern/HAIR_curve.h
index eb7e32c..70b4f13 100644
--- a/source/blender/hair/intern/HAIR_curve.h
+++ b/source/blender/hair/intern/HAIR_curve.h
@@ -33,7 +33,8 @@
 
 HAIR_NAMESPACE_BEGIN
 
-class Point {
+struct Point {
+	Point();
 	Point(const float3 &co);
 	
 	float3 co;
@@ -41,7 +42,8 @@ class Point {
 	HAIR_CXX_CLASS_ALLOC(Point)
 };
 
-class Curve {
+struct Curve {
+	Curve();
 	Curve(int totpoints, Point *points);
 	
 	Point *points;
diff --git a/source/blender/hair/intern/HAIR_curve.cpp b/source/blender/hair/intern/HAIR_solver.cpp
similarity index 59%
copy from source/blender/hair/intern/HAIR_curve.cpp
copy to source/blender/hair/intern/HAIR_solver.cpp
index a5de85d..7c2530b 100644
--- a/source/blender/hair/intern/HAIR_curve.cpp
+++ b/source/blender/hair/intern/HAIR_solver.cpp
@@ -24,19 +24,56 @@
  * ***** END GPL LICENSE BLOCK *****
  */
 
-#include "HAIR_curve.h"
+#include "HAIR_solver.h"
 
 HAIR_NAMESPACE_BEGIN
 
-Point::Point(const float3 &co) :
-    co(co)
+SolverData::SolverData() :
+    curves(NULL),
+    points(NULL),
+    totcurves(0),
+    totpoints(0)
 {
 }
 
-Curve::Curve(int totpoints, Point *points) :
-    points(points),
+SolverData::SolverData(int totcurves, int totpoints) :
+    totcurves(totcurves),
     totpoints(totpoints)
 {
+	curves = new Curve[totcurves];
+	points = new Point[totpoints];
+}
+
+SolverData::SolverData(const SolverData &other) :
+    curves(other.curves),
+    points(other.points),
+    totcurves(other.totcurves),
+    totpoints(other.totpoints)
+{
+}
+
+SolverData::~SolverData()
+{
+	delete curves;
+	delete points;
+}
+
+Solver::Solver()
+{
+}
+
+Solver::~Solver()
+{
+}
+
+void Solver::init_data(int totcurves, int totpoints)
+{
+	m_data = SolverData(totcurves, totpoints);
+}
+
+void Solver::free_data()
+{
+	m_data = SolverData();
 }
 
 HAIR_NAMESPACE_END
diff --git a/source/blender/hair/intern/HAIR_curve.h b/source/blender/hair/intern/HAIR_solver.h
similarity index 65%
copy from source/blender/hair/intern/HAIR_curve.h
copy to source/blender/hair/intern/HAIR_solver.h
index eb7e32c..d7f64ef 100644
--- a/source/blender/hair/intern/HAIR_curve.h
+++ b/source/blender/hair/intern/HAIR_solver.h
@@ -24,30 +24,45 @@
  * ***** END GPL LICENSE BLOCK *****
  */
 
-#ifndef __HAIR_CURVE_H__
-#define __HAIR_CURVE_H__
+#ifndef __HAIR_SOLVER_H__
+#define __HAIR_SOLVER_H__
 
+#include "HAIR_curve.h"
 #include "HAIR_memalloc.h"
 
-#include "HAIR_types.h"
-
 HAIR_NAMESPACE_BEGIN
 
-class Point {
-	Point(const float3 &co);
+struct SolverData {
+	SolverData();
+	SolverData(int totcurves, int totpoints);
+	SolverData(const SolverData &other);
+	~SolverData();
 	
-	float3 co;
+	Curve *curves;
+	Point *points;
+	int totcurves;
+	int totpoints;
 	
-	HAIR_CXX_CLASS_ALLOC(Point)
+	HAIR_CXX_CLASS_ALLOC(SolverData)
 };
 
-class Curve {
-	Curve(int totpoints, Point *points);
+class Solver
+{
+public:
+	Solver();
+	~Solver();
 	
-	Point *points;
-	int totpoints;
+	void init_data(int totcurves, int totpoints);
+	void free_data();
+	SolverData &data() { return m_data; }
+	const SolverData &data() co

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list