[Bf-blender-cvs] [722f7f9] fluid-mantaflow: totally refactored, cleaned up manta object and its API

Sebastián Barschkis noreply at git.blender.org
Thu Mar 17 19:11:06 CET 2016


Commit: 722f7f95be6c77e4a5317e460063df18c00099b2
Author: Sebastián Barschkis
Date:   Tue Mar 15 20:09:47 2016 +0100
Branches: fluid-mantaflow
https://developer.blender.org/rB722f7f95be6c77e4a5317e460063df18c00099b2

totally refactored, cleaned up manta object and its API

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

M	intern/mantaflow/intern/MANTA.cpp
M	intern/mantaflow/intern/MANTA.h
M	intern/mantaflow/intern/manta_smoke_API.cpp

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

diff --git a/intern/mantaflow/intern/MANTA.cpp b/intern/mantaflow/intern/MANTA.cpp
index 2845b3e..0815393 100644
--- a/intern/mantaflow/intern/MANTA.cpp
+++ b/intern/mantaflow/intern/MANTA.cpp
@@ -29,54 +29,125 @@
 
 #include "MANTA.h"
 
-MANTA::MANTA(int *res, float dx, float dtdef, int init_heat, int init_fire, int init_colors, SmokeModifierData *smd) :
-_xRes(res[0]), _yRes(res[1]), _zRes(res[2]), _res(0.0f)
+bool MANTA::mantaInitialized = false;
+
+MANTA::MANTA(int *res, SmokeModifierData *smd)
 {
 	std::cout << "MANTA" << std::endl;
-	// set simulation consts
-	_dt = dtdef;	// just in case. set in step from a RNA factor
-
-	_iterations = 100;
-	_tempAmb = 0; 
-	_heatDiffusion = 1e-3;
-	_totalTime = 0.0f;
-	_totalSteps = 0;
-	_maxRes = MAX3(_xRes, _yRes, _zRes);
-	
-	if (!dx)
-		_dx = 1.0f / (float)_maxRes;
-	else
-		_dx = dx;
-	_constantScaling = 64.0f / _maxRes;
-	_constantScaling = (_constantScaling < 1.0f) ? 1.0f : _constantScaling;
-	_vorticityEps = 2.0f / _constantScaling; // Just in case set a default value
-
-	_totalCells   = _xRes * _yRes * _zRes;
-
-	// allocate arrays
-	_manta_inflow = NULL;
-	_fuel_inflow = NULL;
-	_manta_flags = NULL;
-	_xVelocity = NULL;
-	_yVelocity = NULL;
-	_zVelocity = NULL;
-	_xVelocityOb = NULL;
-	_yVelocityOb = NULL;
-	_zVelocityOb = NULL;
-	_xForce = NULL;
-	_yForce = NULL;
-	_zForce = NULL;
-	_density = NULL;
-	_obstacles = new unsigned char[_totalCells]; // set 0 at end of step
-
-	_using_heat = false;
-	_using_fire = false;
-	_using_colors = false;
-	
-	MANTA::start_mantaflow();
-	
-	// Base setup low res
-	std::string setup_script =
+	smd->domain->fluid = this;
+	
+	// General variables used for low and high res
+	std::string tmpScript = "";
+	std::string finalScript = "";
+	mUsingHighRes = smd->domain->flags & MOD_SMOKE_HIGHRES;
+	
+	// Make sure that string vector does not contain any previous commands
+	mCommands.clear();
+
+	// simulation constants
+	mTempAmb			= 0; // TODO: Maybe use this later for buoyancy calculation
+	mResX				= res[0];
+	mResY				= res[1];
+	mResZ				= res[2];
+	int maxRes			= MAX3(mResX, mResY, mResZ);
+	mConstantScaling	= 64.0f / maxRes;
+	mConstantScaling	= (mConstantScaling < 1.0f) ? 1.0f : mConstantScaling;
+	mTotalCells			= mResX * mResY * mResZ;
+	
+	// low res grids
+	mDensity		= NULL;
+	mHeat			= NULL;
+	mVelocityX		= NULL;
+	mVelocityY		= NULL;
+	mVelocityZ		= NULL;
+	mForceX			= NULL;
+	mForceY			= NULL;
+	mForceZ			= NULL;
+	mFlame			= NULL;
+	mFuel			= NULL;
+	mReact			= NULL;
+	mColorR			= NULL;
+	mColorG			= NULL;
+	mColorB			= NULL;
+	mDensityInflow	= NULL;
+	mFuelInflow		= NULL;
+	mMantaFlags		= NULL;
+	mObVelocityX	= new float[mTotalCells];				// TODO in Mantaflow
+	mObVelocityY	= new float[mTotalCells];				// TODO in Mantaflow
+	mObVelocityZ	= new float[mTotalCells];				// TODO in Mantaflow
+	mObstacles		= new unsigned char[mTotalCells];		// TODO in Mantaflow
+	mObstaclesAnim	= new unsigned char[mTotalCells];		// TODO in Mantaflow
+	
+	// high res grids
+	mDensityHigh	= NULL;
+	mFlameHigh		= NULL;
+	mFuelHigh		= NULL;
+	mReactHigh		= NULL;
+	mColorRHigh		= NULL;
+	mColorGHigh		= NULL;
+	mColorBHigh		= NULL;
+	
+	// TODO: Obstacle grid not mantaflow optimized
+	for (int x = 0; x < mTotalCells; x++)
+		mObstacles[x] = false;
+
+	if (!mantaInitialized)
+		startMantaflow();
+	
+	// Initialize Mantaflow variables in Python
+	initSetup(smd);
+	if (smd->domain->active_fields & SM_ACTIVE_HEAT)
+		initHeat(smd);
+	if (smd->domain->active_fields & SM_ACTIVE_FIRE)
+		initFire(smd);
+	if (smd->domain->active_fields & SM_ACTIVE_COLORS)
+		initColors(smd);
+	
+	updatePointers(smd); // Needs to be after heat, fire, color init
+	
+	if (smd->domain->flags & MOD_SMOKE_HIGHRES)
+	{		
+		// Make sure that string vector does not contain any previous commands
+		mCommands.clear();
+
+		// simulation constants
+		int amplify		= smd->domain->amplify + 1;
+		mResXHigh		= amplify * mResX;
+		mResYHigh		= amplify * mResY;
+		mResZHigh		= amplify * mResZ;
+		mTotalCellsHigh	= mResXHigh * mResYHigh * mResZHigh;
+		
+		mTextureU = new float[mTotalCells];					// TODO in Mantaflow
+		mTextureV = new float[mTotalCells];					// TODO in Mantaflow
+		mTextureW = new float[mTotalCells];					// TODO in Mantaflow
+		
+		const float dx = 1.0f/(float)(mResX);				// TODO in Mantaflow
+		const float dy = 1.0f/(float)(mResY);				// TODO in Mantaflow
+		const float dz = 1.0f/(float)(mResZ);				// TODO in Mantaflow
+		int index = 0;										// TODO in Mantaflow
+		for (int z = 0; z < mResZ; z++)						// TODO in Mantaflow
+			for (int y = 0; y < mResY; y++)					// TODO in Mantaflow
+				for (int x = 0; x < mResX; x++, index++)	// TODO in Mantaflow
+				{
+					mTextureU[index] = x*dx;				// TODO in Mantaflow
+					mTextureV[index] = y*dy;				// TODO in Mantaflow
+					mTextureW[index] = z*dz;				// TODO in Mantaflow
+				}
+		
+		// Initialize Mantaflow variables in Python
+		initSetupHigh(smd);
+		if (smd->domain->active_fields & SM_ACTIVE_FIRE)
+			initFireHigh(smd);
+		if (smd->domain->active_fields & SM_ACTIVE_COLORS)
+			initColorsHigh(smd);
+
+		updatePointersHigh(smd); // Needs to be after fire, color init
+	}
+}
+
+void MANTA::initSetup(SmokeModifierData *smd)
+{
+	std::string tmpString =
 		manta_import +
 		solver_setup_low +
 		uv_setup +
@@ -85,226 +156,131 @@ _xRes(res[0]), _yRes(res[1]), _zRes(res[2]), _res(0.0f)
 		flags +
 		manta_step +
 		smoke_step_low;
-	std::string final_script = MANTA::parse_script(setup_script, smd);
-	PyGILState_STATE gilstate = PyGILState_Ensure();
-	PyRun_SimpleString(final_script.c_str());
-	PyGILState_Release(gilstate);
-	
-	smd->domain->manta = this;
-	
-	/* heat */
-	_heat = NULL;
-	if (init_heat) {
-		initHeat();
-	}
-	// Fire simulation
-	_flame = NULL;
-	_fuel = NULL;
-	_react = NULL;
-	if (init_fire) {
-		initFire();
-	}
-	// Smoke color
-	_color_r = NULL;
-	_color_g = NULL;
-	_color_b = NULL;
-	if (init_colors) {
-		initColors(0.0f, 0.0f, 0.0f);
-	}
-	
-	MANTA::update_pointers();
-	
-	// High resolution
-	_xResBig = _amplify * _xRes;
-	_yResBig = _amplify * _yRes;
-	_zResBig = _amplify * _zRes;
-	_slabSizeBig = _xResBig * _yResBig;
-	_totalCellsBig = _slabSizeBig * _zResBig;
-	
-	// allocate high resolution density field
-	_densityBig = NULL;
-	_flameBig = NULL;
-	_fuelBig = NULL;
-	_reactBig = NULL;
-	
-	_color_rBig = NULL;
-	_color_gBig = NULL;
-	_color_bBig = NULL;
+	std::string finalString = parseScript(tmpString, smd);
+	mCommands.push_back(finalString);
 	
-	// Base setup high res
-	std::string setup_script =
+	runPythonString(mCommands);
+}
+
+void MANTA::initSetupHigh(SmokeModifierData *smd)
+{
+	std::string tmpString =
 		solver_setup_high +
 		alloc_base_grids_high +
 		prep_domain_high +
-		flags +
 		wavelet_turbulence_noise +
 		smoke_step_high;
-	std::string final_script = Manta_API::parse_script(setup_script);
-	PyGILState_STATE gilstate = PyGILState_Ensure();
-	PyRun_SimpleString(final_script.c_str());
-	PyGILState_Release(gilstate);
-	
-	// Fire grids
-	if (init_fire) {
-		initFireHigh();
-	}
-	
-	// Color grids
-	if (init_colors) {
-		initColorsHigh(0.0f, 0.0f, 0.0f);
-	}
-
-	// allocate & init texture coordinates
-	_tcU = NULL;
-	_tcV = NULL;
-	_tcW = NULL;
-	
-	MANTA::update_high_res_pointers();
+	std::string finalString = parseScript(tmpString, smd);
+	mCommands.push_back(finalString);
+		
+	runPythonString(mCommands);
 }
 
-void MANTA::initHeat()
-{
-	if (!_heat) {
-		_using_heat = true;
-		PyGILState_STATE gilstate = PyGILState_Ensure();
-		PyRun_SimpleString(alloc_heat_low.c_str());
-		PyRun_SimpleString(with_heat.c_str());
-		PyGILState_Release(gilstate);
-		MANTA::update_pointers();
-	}
-}
 
-void MANTA::initFire()
+void MANTA::initHeat(SmokeModifierData *smd)
 {
-	if (!_flame) {
-		_using_fire = true;
-		PyGILState_STATE gilstate = PyGILState_Ensure();
-		PyRun_SimpleString(alloc_fire_low.c_str());
-		PyRun_SimpleString(with_fire.c_str());
-		PyGILState_Release(gilstate);
-		MANTA::update_pointers();
+	if (!mHeat) {
+		mCommands.clear();
+		mCommands.push_back(alloc_heat_low);
+		mCommands.push_back(with_heat);
+		
+		runPythonString(mCommands);
 	}
 }
 
-void MANTA::initFireHigh()
+void MANTA::initFire(SmokeModifierData *smd)
 {
-	if (!_fuelBig) {
-		using_fire = true;
-		PyGILState_STATE gilstate = PyGILState_Ensure();
-		PyRun_SimpleString(alloc_fire_high.c_str());
-		PyGILState_Release(gilstate);
-		MANTA::update_high_res_pointers();
+	if (!mFuel) {
+		mCommands.clear();
+		mCommands.push_back(alloc_fire_low);
+		mCommands.push_back(with_fire);
+
+		runPythonString(mCommands);
 	}
 }
 
-void MANTA::initColors(float init_r, float init_g, float init_b)
+void MANTA::initFireHigh(SmokeModifierData *smd)
 {
-	if (!_color_r) {
-		_using_colors = true;
-		PyGILState_STATE gilstate = PyGILState_Ensure();
-		std::stringstream ss;
-		ss << "manta_color_r = " << init_r << endl;
-		ss << "manta_color_g = " << init_g << endl;
-		ss << "manta_color_b = " << init_b << endl;
-		PyRun_SimpleString(ss.str().c_str());
-		PyRun_SimpleString(alloc_colors_low.c_str());
-		PyRun_SimpleString(init_colors_low.c_str());
-		PyRun_SimpleString(with_colors.c_str());
-		PyGILState_Release(gilstate);
-		MANTA::update_pointers();
+	if (!mFuelHigh) {
+		mCommands.clear();
+		mCommands.push_back(alloc_fire_high);
+		mCommands.push_back(with_fire);
+
+		runPythonString(mCommands);
 	}
 }
 
-void MANTA::initColorsHigh(float init_r, float init_g, float init_b)
+void MANTA::initColors(SmokeModifierData *smd)
 {
-	if (!_color_rBig) {
-		using_colors = true;
-		PyGILState_STATE gilstate = PyGILState_Ensure();
-		stringstream ss;
-		ss << "manta_color_r = " << init_r << endl;
-		ss << "manta_color_g = " << init_g << endl;
-		ss << "manta_color_b = " << init_b << endl;
-		PyRun_SimpleString(ss.str().c_str());
-		PyRun_SimpleString(alloc_colors_high.c_str());
-		PyRun_SimpleString(init_colors_high.c_str());
-		PyRun_SimpleString(with_fire.c_str());
-		PyGILState_Release(gilstate);
-		MANTA::update_high_res_pointers();
+	if (!mColorR) {
+		mCommands.clear();
+		std:: string colorCodes = parseScript(set_color_codes, smd);
+		mCommands.push_back(colorCodes);
+		mCommands.push_back(alloc_colors_low);
+		mCommands.push_back(init_colors_low);
+		mCommands.push_back(

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list