/* * Copyright (C) 2008-2012 Thorsten Liebig (Thorsten.Liebig@gmx.de) * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published * by the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . */ #include "tinyxml.h" #include #include #include "vtkPolyData.h" #include "vtkCellArray.h" #include "vtkPoints.h" #include "ParameterCoord.h" #include "CSPropDiscMaterial.h" CSPropDiscMaterial::CSPropDiscMaterial(ParameterSet* paraSet) : CSPropMaterial(paraSet) { Type=(CSProperties::PropertyType)(DISCRETE_MATERIAL | MATERIAL); Init(); } CSPropDiscMaterial::CSPropDiscMaterial(CSProperties* prop) : CSPropMaterial(prop) { Type=(CSProperties::PropertyType)(DISCRETE_MATERIAL | MATERIAL); Init(); } CSPropDiscMaterial::CSPropDiscMaterial(unsigned int ID, ParameterSet* paraSet) : CSPropMaterial(ID, paraSet) { Type=(CSProperties::PropertyType)(DISCRETE_MATERIAL | MATERIAL); Init(); } CSPropDiscMaterial::~CSPropDiscMaterial() { for (int n=0;n<3;++n) { delete[] m_mesh[n]; m_mesh[n]=NULL; } delete[] m_Disc_Ind; m_Disc_Ind=NULL; delete[] m_Disc_epsR; m_Disc_epsR=NULL; delete[] m_Disc_kappa; m_Disc_kappa=NULL; delete[] m_Disc_mueR; m_Disc_mueR=NULL; delete[] m_Disc_sigma; m_Disc_sigma=NULL; delete[] m_Disc_Density; m_Disc_Density=NULL; delete m_Transform; m_Transform=NULL; } unsigned int CSPropDiscMaterial::GetWeightingPos(const double* inCoords) { double coords[3]; TransformCoordSystem(inCoords, coords, coordInputType, CARTESIAN); if (m_Transform) m_Transform->InvertTransform(coords,coords); for (int n=0;n<3;++n) coords[n]/=m_Scale; unsigned int pos[3]; if (!(m_mesh[0] && m_mesh[1] && m_mesh[2])) return -1; for (int n=0;n<3;++n) { if (coords[n]m_mesh[n][m_Size[n]-1]) return -1; pos[n]=0; for (unsigned int i=1;i=(int)m_DB_size) { //sanity check, this should not happen!!! std::cerr << __func__ << ": Error, false DB position!" << std::endl; return -1; } return db_pos; } double CSPropDiscMaterial::GetEpsilonWeighted(int ny, const double* inCoords) { if (m_Disc_epsR==NULL) return CSPropMaterial::GetEpsilonWeighted(ny,inCoords); int pos = GetDBPos(inCoords); if (pos<0) return CSPropMaterial::GetEpsilonWeighted(ny,inCoords); return m_Disc_epsR[pos]; } double CSPropDiscMaterial::GetKappaWeighted(int ny, const double* inCoords) { if (m_Disc_kappa==NULL) return CSPropMaterial::GetKappaWeighted(ny,inCoords); int pos = GetDBPos(inCoords); if (pos<0) return CSPropMaterial::GetKappaWeighted(ny,inCoords); return m_Disc_kappa[pos]; } double CSPropDiscMaterial::GetMueWeighted(int ny, const double* inCoords) { if (m_Disc_mueR==NULL) return CSPropMaterial::GetMueWeighted(ny,inCoords); int pos = GetDBPos(inCoords); if (pos<0) return CSPropMaterial::GetMueWeighted(ny,inCoords); return m_Disc_mueR[pos]; } double CSPropDiscMaterial::GetSigmaWeighted(int ny, const double* inCoords) { if (m_Disc_sigma==NULL) return CSPropMaterial::GetSigmaWeighted(ny,inCoords); int pos = GetDBPos(inCoords); if (pos<0) return CSPropMaterial::GetSigmaWeighted(ny,inCoords); return m_Disc_sigma[pos]; } double CSPropDiscMaterial::GetDensityWeighted(const double* inCoords) { if (m_Disc_Density==NULL) return CSPropMaterial::GetDensityWeighted(inCoords); int pos = GetDBPos(inCoords); if (pos<0) return CSPropMaterial::GetDensityWeighted(inCoords); return m_Disc_Density[pos]; } void CSPropDiscMaterial::Init() { m_Filename.clear(); m_FileType=-1; m_DB_size = 0; m_DB_Background = true; for (int n=0;n<3;++n) m_mesh[n]=NULL; m_Disc_Ind=NULL; m_Disc_epsR=NULL; m_Disc_kappa=NULL; m_Disc_mueR=NULL; m_Disc_sigma=NULL; m_Disc_Density=NULL; m_Scale=1; m_Transform=NULL; CSPropMaterial::Init(); } bool CSPropDiscMaterial::Write2XML(TiXmlNode& root, bool parameterised, bool sparse) { if (CSPropMaterial::Write2XML(root,parameterised,sparse) == false) return false; TiXmlElement* prop=root.ToElement(); if (prop==NULL) return false; TiXmlElement filename("DiscFile"); filename.SetAttribute("Type",m_FileType); filename.SetAttribute("File",m_Filename.c_str()); filename.SetAttribute("UseDBBackground",m_DB_Background); filename.SetAttribute("Scale",m_Scale); if (m_Transform) m_Transform->Write2XML(prop); prop->InsertEndChild(filename); return true; } bool CSPropDiscMaterial::ReadFromXML(TiXmlNode &root) { if (CSPropMaterial::ReadFromXML(root)==false) return false; TiXmlElement* prop=root.ToElement(); if (prop==NULL) return false; m_FileType = 0; prop->QueryIntAttribute("Type",&m_FileType); const char* c_filename = prop->Attribute("File"); int help; if (prop->QueryIntAttribute("UseDBBackground",&help)==TIXML_SUCCESS) SetUseDataBaseForBackground(help!=0); delete m_Transform; m_Transform = CSTransform::New(prop, clParaSet); if (prop->QueryDoubleAttribute("Scale",&m_Scale)!=TIXML_SUCCESS) m_Scale=1; if (c_filename==NULL) return true; if ((m_FileType==0) && (c_filename!=NULL)) return ReadHDF5(c_filename); else std::cerr << "CSPropDiscMaterial::ReadFromXML: Unknown file type or no filename given." << std::endl; return true; } void *CSPropDiscMaterial::ReadDataSet(std::string filename, std::string d_name, int type_id, int &rank, unsigned int &size, bool debug) { herr_t status; H5T_class_t class_id; size_t type_size; rank = -1; // open hdf5 file hid_t file_id = H5Fopen( filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT ); if (file_id < 0) { if (debug) std::cerr << __func__ << ": Failed to open file, skipping..." << std::endl; H5Fclose(file_id); return NULL; } if (H5Lexists(file_id, d_name.c_str(), H5P_DEFAULT)<=0) { if (debug) std::cerr << __func__ << ": Warning, dataset: \"" << d_name << "\" not found... skipping" << std::endl; H5Fclose(file_id); return NULL; } status = H5LTget_dataset_ndims(file_id, d_name.c_str(), &rank); if (status < 0) { if (debug) std::cerr << __func__ << ": Warning, failed to read dimension for dataset: \"" << d_name << "\" skipping..." << std::endl; H5Fclose(file_id); return NULL; } hsize_t dims[rank]; status = H5LTget_dataset_info( file_id, d_name.c_str(), dims, &class_id, &type_size); if (status < 0) { if (debug) std::cerr << __func__ << ": Warning, failed to read dataset info: \"" << d_name << "\" skipping..." << std::endl; H5Fclose(file_id); return NULL; } size = 1; for (int n=0;n0) bd=true; } else if (pos[n]==m_Size[n]-2) { if (m_Disc_Ind[mat_idx]>0) bu=true; } else { rpos[n] = pos[n]-1; // set relative pos mat_idx_down = rpos[0] + rpos[1]*(m_Size[0]-1) + rpos[2]*(m_Size[0]-1)*(m_Size[1]-1); rpos[n] = pos[n]; // reset relative pos if ((m_Disc_Ind[mat_idx]>0) && (m_Disc_Ind[mat_idx_down]==0)) bd=true; else if (m_Disc_Ind[mat_idx]==0 && (m_Disc_Ind[mat_idx_down]>0)) bu=true; } rpos[0]=pos[0]; rpos[1]=pos[1]; rpos[2]=0; if (bu) // draw poly for surface up { nP = (n+1)%3; nPP = (n+2)%3; poly->InsertNextCell(4); mesh_idx = rpos[0] + rpos[1]*m_Size[0]; if (pointIdx[rpos[2]][mesh_idx]<0) pointIdx[rpos[2]][mesh_idx] = (int)points->InsertNextPoint(m_mesh[0][rpos[0]],m_mesh[1][rpos[1]],m_mesh[2][pos[2]+rpos[2]]); poly->InsertCellPoint(pointIdx[rpos[2]][mesh_idx]); ++rpos[nP]; mesh_idx = rpos[0] + rpos[1]*m_Size[0]; if (pointIdx[rpos[2]][mesh_idx]<0) pointIdx[rpos[2]][mesh_idx] = (int)points->InsertNextPoint(m_mesh[0][rpos[0]],m_mesh[1][rpos[1]],m_mesh[2][pos[2]+rpos[2]]); poly->InsertCellPoint(pointIdx[rpos[2]][mesh_idx]); ++rpos[nPP]; mesh_idx = rpos[0] + rpos[1]*m_Size[0]; if (pointIdx[rpos[2]][mesh_idx]<0) pointIdx[rpos[2]][mesh_idx] = (int)points->InsertNextPoint(m_mesh[0][rpos[0]],m_mesh[1][rpos[1]],m_mesh[2][pos[2]+rpos[2]]); poly->InsertCellPoint(pointIdx[rpos[2]][mesh_idx]); --rpos[nP]; mesh_idx = rpos[0] + rpos[1]*m_Size[0]; if (pointIdx[rpos[2]][mesh_idx]<0) pointIdx[rpos[2]][mesh_idx] = (int)points->InsertNextPoint(m_mesh[0][rpos[0]],m_mesh[1][rpos[1]],m_mesh[2][pos[2]+rpos[2]]); poly->InsertCellPoint(pointIdx[rpos[2]][mesh_idx]); } else if (bd) // draw poly for surface down { nP = (n+1)%3; nPP = (n+2)%3; poly->InsertNextCell(4); mesh_idx = rpos[0] + rpos[1]*m_Size[0]; if (pointIdx[rpos[2]][mesh_idx]<0) pointIdx[rpos[2]][mesh_idx] = (int)points->InsertNextPoint(m_mesh[0][rpos[0]],m_mesh[1][rpos[1]],m_mesh[2][pos[2]+rpos[2]]); poly->InsertCellPoint(pointIdx[rpos[2]][mesh_idx]); ++rpos[nPP]; mesh_idx = rpos[0] + rpos[1]*m_Size[0]; if (pointIdx[rpos[2]][mesh_idx]<0) pointIdx[rpos[2]][mesh_idx] = (int)points->InsertNextPoint(m_mesh[0][rpos[0]],m_mesh[1][rpos[1]],m_mesh[2][pos[2]+rpos[2]]); poly->InsertCellPoint(pointIdx[rpos[2]][mesh_idx]); ++rpos[nP]; mesh_idx = rpos[0] + rpos[1]*m_Size[0]; if (pointIdx[rpos[2]][mesh_idx]<0) pointIdx[rpos[2]][mesh_idx] = (int)points->InsertNextPoint(m_mesh[0][rpos[0]],m_mesh[1][rpos[1]],m_mesh[2][pos[2]+rpos[2]]); poly->InsertCellPoint(pointIdx[rpos[2]][mesh_idx]); --rpos[nPP]; mesh_idx = rpos[0] + rpos[1]*m_Size[0]; if (pointIdx[rpos[2]][mesh_idx]<0) pointIdx[rpos[2]][mesh_idx] = (int)points->InsertNextPoint(m_mesh[0][rpos[0]],m_mesh[1][rpos[1]],m_mesh[2][pos[2]+rpos[2]]); poly->InsertCellPoint(pointIdx[rpos[2]][mesh_idx]); } } } } delete[] pointIdx[0]; delete[] pointIdx[1]; polydata->SetPoints(points); points->Delete(); polydata->SetPolys(poly); poly->Delete(); return polydata; }