static int set_Cmiss_mesh(struct Parse_state *state, void *region_void, void *mesh_address_void)
{
	Cmiss_region_id region = reinterpret_cast<Cmiss_region_id>(region_void);
	Cmiss_mesh_id *mesh_address = reinterpret_cast<Cmiss_mesh_id*>(mesh_address_void);
	if (!(state && region && mesh_address))
		return 0;
	const char *current_token = state->current_token;
	int return_code = 1;
	if (!current_token)
	{
		display_message(WARNING_MESSAGE, "Missing mesh name");
		display_parse_state_location(state);
		return_code =  0;
	}
	else if ((0 == strcmp(PARSER_HELP_STRING, current_token)) ||
		(0 == strcmp(PARSER_RECURSIVE_HELP_STRING, current_token)))
	{
		display_message(INFORMATION_MESSAGE, " ELEMENT_GROUP_FIELD_NAME|[GROUP_NAME.]cmiss_mesh_1d|cmiss_mesh_2d|cmiss_mesh_3d[");
		if (*mesh_address)
		{
			char *mesh_name = Cmiss_mesh_get_name(*mesh_address);
			make_valid_token(&mesh_name);
			display_message(INFORMATION_MESSAGE, "%s]", mesh_name);
			DEALLOCATE(mesh_name);
		}
		else
		{
			display_message(INFORMATION_MESSAGE, "none]");
		}
	}
	else
	{
		Cmiss_field_module_id field_module = Cmiss_region_get_field_module(region);
		Cmiss_mesh_id new_mesh = Cmiss_field_module_find_mesh_by_name(field_module, current_token);
		if (new_mesh)
		{
			if (*mesh_address)
				Cmiss_mesh_destroy(mesh_address);
			*mesh_address = new_mesh;
			shift_Parse_state(state, 1);
		}
		else
		{
			display_message(ERROR_MESSAGE, "Invalid mesh: %s", current_token);
			display_parse_state_location(state);
			return_code = 0;
		}
		Cmiss_field_module_destroy(&field_module);
	}
	return return_code;
}
Esempio n. 2
0
StrainMeasures::StrainMeasures(std::vector<std::string> mesh) :
		mySegments(NULL) {
	num_level_sets = 10;
	num_elements = 48;
	num_nodes = 98;
	elements.resize(num_elements);
	x_discret = 25;
	y_discret = 25;
	lagrangian = true;
	modelPGS = 0.0;
	computeModelPGS = false;
	ejectionFraction = 0.0;
	extraNodeStartID = 100000;

	LVMyocardialVolume* lvm = new LVMyocardialVolume(mesh);
	volumes = lvm->getMyocardialVolumes();
	delete lvm;

	//Read the mesh
	context_ = Cmiss_context_create("strainmeasure");
	/**< Handle to the context */
	Cmiss_region_id root_region = Cmiss_context_get_default_region(context_);

	//This required for using the cmiss_context_execute_command
	Cmiss_context_enable_user_interface(context_, NULL);

	//Load the finite element mesh of the heart

	std::string region_name = "heart";
	Cmiss_region_id heart_region = Cmiss_region_create_child(root_region, region_name.c_str());
	//Get the field module of the heart region
	field_module = Cmiss_region_get_field_module(heart_region);

	fieldCache = Cmiss_field_module_create_cache(field_module);
	numberOfModelFrames_ = mesh.size();
	double denom = numberOfModelFrames_ - 1;
	for (unsigned int i = 0; i < numberOfModelFrames_; i++) {
		double time = static_cast<double>(i) / denom;
		Cmiss_stream_information_id stream_information = Cmiss_region_create_stream_information(root_region);
		Cmiss_stream_information_region_id stream_information_region = Cmiss_stream_information_cast_region(stream_information);
		Cmiss_stream_resource_id stream_resource = Cmiss_stream_information_create_resource_memory_buffer(stream_information, mesh[i].c_str(), mesh[i].length());
		Cmiss_stream_information_region_set_resource_attribute_real(stream_information_region, stream_resource, CMISS_STREAM_INFORMATION_REGION_ATTRIBUTE_TIME, time);

		Cmiss_region_read(root_region, stream_information);

		Cmiss_stream_resource_destroy(&stream_resource);
		Cmiss_stream_information_destroy(&stream_information);
		Cmiss_stream_information_region_destroy(&stream_information_region);
	}

	{ 	//Define the necessary fields
		std::string referenceMesh = mesh[0];
		//Change coordinate name to reference_coordinates
		boost::replace_all(referenceMesh, "1) coordinates", "1) reference_coordinates");
		//Load the reference coordinates mesh
		Cmiss_stream_information_id stream_information = Cmiss_region_create_stream_information(root_region);
		Cmiss_stream_information_region_id stream_information_region = Cmiss_stream_information_cast_region(stream_information);
		Cmiss_stream_resource_id stream_resource = Cmiss_stream_information_create_resource_memory_buffer(stream_information, referenceMesh.c_str(), referenceMesh.length());

		Cmiss_region_read(root_region, stream_information);
		Cmiss_stream_resource_destroy(&stream_resource);
		Cmiss_stream_information_destroy(&stream_information);
	}
	coordianteField = Cmiss_field_module_find_field_by_name(field_module, "reference_coordinates");
	if (!coordianteField) {
		std::cout << "reference_coordinates field not found " << std::endl;
	}

	Cmiss_region_destroy(&heart_region);
	//#Calculate the strains
	Cmiss_field_module_define_field(field_module, "F", "gradient coordinate reference_coordinates field coordinates");
	Cmiss_field_module_define_field(field_module, "F_transpose", "transpose source_number_of_rows 3 field F");
	Cmiss_field_module_define_field(field_module, "C", "matrix_multiply number_of_rows 3 fields F_transpose F");
	Cmiss_field_module_define_field(field_module, "principal_strains", "eigenvalues field C");

	Cmiss_field_destroy(&coordianteField);
	//Assign the coordinates field to the handle for downstream use
	coordianteField = Cmiss_field_module_find_field_by_name(field_module, "coordinates");
	//Define the fibre field
	{
		//Load the reference fibre data and reference coordinates
		{
			if (Cmiss_field_module_find_field_by_name(field_module, "fibres") == NULL) {
				Cmiss_stream_information_id stream_information = Cmiss_region_create_stream_information(root_region);
				Cmiss_stream_information_region_id stream_information_region = Cmiss_stream_information_cast_region(stream_information);
				//Cmiss_stream_resource_id stream_resource = Cmiss_stream_information_create_resource_memory_buffer(stream_information, reffibre_exregion, reffibre_exregion_len);
				Cmiss_stream_resource_id stream_resource = Cmiss_stream_information_create_resource_memory_buffer(stream_information, humandtifibre_exregion,
						humandtifibre_exregion_len);

				Cmiss_region_read(root_region, stream_information);
				Cmiss_stream_resource_destroy(&stream_resource);
				Cmiss_stream_information_destroy(&stream_information);
			}

			//Define fibre field

			//#Calculate the deformed fibre axes
			Cmiss_field_module_define_field(field_module, "fibre_axes", "fibre_axes coordinate reference_coordinates fibre fibres");
			Cmiss_field_module_define_field(field_module, "deformed_fibre_axes", "matrix_multiply number_of_rows 3 fields fibre_axes F_transpose");
			Cmiss_field_module_define_field(field_module, "deformed_fibre", "composite deformed_fibre_axes.1 deformed_fibre_axes.2 deformed_fibre_axes.3");
			Cmiss_field_module_define_field(field_module, "principal_fibre_strain1", "composite deformed_fibre_axes.1");
		}
	}
	//Determine the myocardial volume

	//Get the element and node handles
	Cmiss_mesh_id cmiss_mesh = Cmiss_field_module_find_mesh_by_name(field_module, "cmiss_mesh_3d");

	Cmiss_element_iterator_id elementIterator = Cmiss_mesh_create_element_iterator(cmiss_mesh);
	Cmiss_element_id element = Cmiss_element_iterator_next(elementIterator);

	while (element != NULL) {
		int elementId = Cmiss_element_get_identifier(element) - 1; //Cmiss numbering starts at 1
		elements[elementId] = element;
		element = Cmiss_element_iterator_next(elementIterator);
	}
	Cmiss_element_iterator_destroy(&elementIterator);
	Cmiss_mesh_destroy(&cmiss_mesh);

	Cmiss_region_destroy(&root_region);
}