| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#ifndef _VOLUMETRICMESH_H_ |
| 30 |
#define _VOLUMETRICMESH_H_ |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
#include <stdio.h> |
| 50 |
#include <stdlib.h> |
| 51 |
#include <string.h> |
| 52 |
#include <vector> |
| 53 |
#include <set> |
| 54 |
#include <string> |
| 55 |
#include <map> |
| 56 |
#include "minivector.h" |
| 57 |
|
| 58 |
class VolumetricMesh |
| 59 |
{ |
| 60 |
public: |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
VolumetricMesh(const VolumetricMesh & volumetricMesh); |
| 66 |
virtual VolumetricMesh * clone() = 0; |
| 67 |
virtual ~VolumetricMesh(); |
| 68 |
|
| 69 |
|
| 70 |
class Set; |
| 71 |
class Material; |
| 72 |
class Region; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
virtual int saveToAscii(const char * filename) const = 0; |
| 78 |
virtual int save(const char * filename) const; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
virtual int saveToBinary(const char * filename, unsigned int * bytesWritten = NULL) const = 0; |
| 84 |
|
| 85 |
virtual int saveToBinary(FILE * binaryOutputStream, unsigned int * bytesWritten = NULL, bool countBytesOnly = false) const = 0; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
int exportToEle(const char * baseFilename, int includeRegions=0) const; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
void exportMeshGeometry(int * numVertices, double ** vertices, int * numElements = NULL, int * numElementVertices = NULL, int ** elements = NULL) const; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
typedef enum { INVALID, TET, CUBIC } elementType; |
| 98 |
typedef enum { ASCII, BINARY, NUM_FILE_FORMATS } fileFormatType; |
| 99 |
|
| 100 |
static elementType getElementType(const char * filename, fileFormatType fileFormat = ASCII); |
| 101 |
virtual elementType getElementType() const = 0; |
| 102 |
|
| 103 |
|
| 104 |
static elementType getElementType(void * fin, int memoryLoad = 0); |
| 105 |
|
| 106 |
inline int getNumVertices() const { return numVertices; } |
| 107 |
inline Vec3d & getVertex(int i) { return vertices[i]; } |
| 108 |
inline const Vec3d & getVertex(int i) const { return vertices[i]; } |
| 109 |
inline Vec3d & getVertex(int element, int vertex) { return vertices[elements[element][vertex]]; } |
| 110 |
inline const Vec3d & getVertex(int element, int vertex) const { return vertices[elements[element][vertex]]; } |
| 111 |
inline int getVertexIndex(int element, int vertex) const { return elements[element][vertex]; } |
| 112 |
inline const int * getVertexIndices(int element) const { return elements[element]; } |
| 113 |
inline Vec3d * getVertices() { return vertices; } |
| 114 |
inline const Vec3d * getVertices() const { return vertices; } |
| 115 |
inline int getNumElements() const { return numElements; } |
| 116 |
inline int getNumElementVertices() const { return numElementVertices; } |
| 117 |
void renumberVertices(const std::vector<int> & permutation); |
| 118 |
inline void setVertex(int i, const Vec3d & pos) { vertices[i] = pos; } |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
inline int getNumMaterials() const { return numMaterials; } |
| 123 |
inline const Material * getMaterial(int i) const { return materials[i]; } |
| 124 |
inline const Material * getElementMaterial(int el) const { return materials[elementMaterial[el]]; } |
| 125 |
static void getDefaultMaterial(double * E, double * nu, double * density); |
| 126 |
|
| 127 |
inline int getNumSets() const { return numSets; } |
| 128 |
inline const Set * getSet(int i) const { return sets[i]; } |
| 129 |
|
| 130 |
inline int getNumRegions() const { return numRegions; } |
| 131 |
inline const Region * getRegion(int i) const { return regions[i]; } |
| 132 |
|
| 133 |
|
| 134 |
inline Material * getMaterial(int i) { return materials[i]; } |
| 135 |
inline Material * getElementMaterial(int el) { return materials[elementMaterial[el]]; } |
| 136 |
void setMaterial(int i, const Material * material); |
| 137 |
void setSingleMaterial(double E, double nu, double density); |
| 138 |
void addMaterial(const Material * material, const Set & newSet, bool removeEmptySets, bool removeEmptyMaterials); |
| 139 |
|
| 140 |
|
| 141 |
double getElementDensity(int el) const { return materials[elementMaterial[el]]->getDensity(); } |
| 142 |
|
| 143 |
|
| 144 |
virtual void computeElementMassMatrix(int element, double * massMatrix) const = 0; |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
Vec3d getElementCenter(int el) const; |
| 149 |
|
| 150 |
|
| 151 |
double getVolume() const; |
| 152 |
virtual double getElementVolume(int el) const = 0; |
| 153 |
void getVertexVolumes(double * vertexVolumes) const; |
| 154 |
virtual void getElementInertiaTensor(int el, Mat3d & inertiaTensor) const = 0; |
| 155 |
double getMass() const; |
| 156 |
void getInertiaParameters(double & mass, Vec3d & centerOfMass, Mat3d & inertiaTensor) const ; |
| 157 |
|
| 158 |
|
| 159 |
void getMeshGeometricParameters(Vec3d & centroid, double * radius) const; |
| 160 |
|
| 161 |
|
| 162 |
void getVerticesInElements(std::vector<int> & elements, std::vector<int> & vertices) const; |
| 163 |
void getElementsTouchingVertices(std::vector<int> & vertices, std::vector<int> & elements) const; |
| 164 |
void getVertexNeighborhood(std::vector<int> & vertices, std::vector<int> & neighborhood) const; |
| 165 |
|
| 166 |
|
| 167 |
int getClosestElement(Vec3d pos) const; |
| 168 |
int getClosestVertex(Vec3d pos) const; |
| 169 |
int getContainingElement(Vec3d pos) const; |
| 170 |
virtual bool containsVertex(int element, Vec3d pos) const = 0; |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
void computeGravity(double * gravityForce, double g=9.81, bool addForce=false) const; |
| 175 |
|
| 176 |
|
| 177 |
virtual int getNumElementEdges() const = 0; |
| 178 |
virtual void getElementEdges(int el, int * edgeBuffer) const = 0; |
| 179 |
|
| 180 |
|
| 181 |
void applyDeformation(double * u); |
| 182 |
void applyLinearTransformation(double * pos, double * R); |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
void setToSubsetMesh(std::set<int> & subsetElements, int removeIsolatedVertices=1, std::map<int,int> * vertexMap = NULL); |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
int generateInterpolationWeights(int numTargetLocations, const double * targetLocations, int ** vertices, double ** weights, double zeroThreshold = -1.0, int ** elements = NULL, int verbose=0) const; |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
static void interpolate(const double * u, double * uTarget, int numTargetLocations, int numElementVertices, const int * vertices, const double * weights); |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
int generateInterpolationWeights(int numTargetLocations, const double * targetLocations, int * elements, int ** vertices, double ** weights, double zeroThreshold = -1.0, int verbose=0) const; |
| 220 |
|
| 221 |
int generateContainingElements(int numTargetLocations, const double * targetLocations, int ** elements, int useClosestElementIfOutside=1, int verbose=0) const; |
| 222 |
static int getNumInterpolationElementVertices(const char * filename); |
| 223 |
static int loadInterpolationWeights(const char * filename, int numTargetLocations, int numElementVertices, int ** vertices, double ** weights); |
| 224 |
static int saveInterpolationWeights(const char * filename, int numTargetLocations, int numElementVertices, const int * vertices, const double * weights); |
| 225 |
static int loadInterpolationWeightsBinary(const char * filename, int * numTargetLocations, int * numElementVertices, int ** vertices, double ** weights); |
| 226 |
static int saveInterpolationWeightsBinary(const char * filename, int numTargetLocations, int numElementVertices, const int * vertices, const double * weights); |
| 227 |
static int loadInterpolationWeightsBinary(FILE * fin, int * numTargetLocations, int * numElementVertices, int ** vertices, double ** weights); |
| 228 |
static int saveInterpolationWeightsBinary(FILE * fout, int numTargetLocations, int numElementVertices, const int * vertices, const double * weights); |
| 229 |
|
| 230 |
|
| 231 |
virtual void computeBarycentricWeights(int element, const Vec3d & pos, double * weights) const = 0; |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
int interpolateGradient(const double * U, int numFields, Vec3d pos, double * grad) const; |
| 239 |
|
| 240 |
virtual void interpolateGradient(int element, const double * U, int numFields, Vec3d pos, double * grad) const = 0; |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
class Set |
| 246 |
{ |
| 247 |
public: |
| 248 |
|
| 249 |
Set(const std::string & name); |
| 250 |
Set(const Set & set); |
| 251 |
Set(const std::string & name, const std::set<int> & elements); |
| 252 |
|
| 253 |
inline std::string getName() const; |
| 254 |
inline int getNumElements() const; |
| 255 |
inline void getElements(std::set<int> & elements) const; |
| 256 |
inline const std::set<int> & getElements() const; |
| 257 |
inline bool isMember(int element) const; |
| 258 |
|
| 259 |
inline std::set<int> & getElements(); |
| 260 |
inline void insert(int element); |
| 261 |
inline void clear(); |
| 262 |
|
| 263 |
protected: |
| 264 |
std::string name; |
| 265 |
std::set<int> elements; |
| 266 |
}; |
| 267 |
|
| 268 |
|
| 269 |
class Material |
| 270 |
{ |
| 271 |
public: |
| 272 |
Material(const std::string name, double density); |
| 273 |
Material(const Material & material); |
| 274 |
virtual ~Material() {}; |
| 275 |
virtual Material * clone() const = 0; |
| 276 |
|
| 277 |
inline std::string getName() const; |
| 278 |
inline double getDensity() const; |
| 279 |
inline void setName(const std::string name); |
| 280 |
inline void setDensity(double density); |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
typedef enum { INVALID, ENU, ORTHOTROPIC, MOONEYRIVLIN } materialType; |
| 286 |
virtual materialType getType() = 0; |
| 287 |
|
| 288 |
typedef enum { ENU_DENSITY, ENU_E, ENU_NU, ENU_NUM_PROPERTIES } enuMaterialProperties; |
| 289 |
typedef enum { ORTHOTROPIC_DENSITY, ORTHOTROPIC_E1, ORTHOTROPIC_E2, ORTHOTROPIC_E3, ORTHOTROPIC_NU12, ORTHOTROPIC_NU23, ORTHOTROPIC_NU31, ORTHOTROPIC_G12, ORTHOTROPIC_G23, ORTHOTROPIC_G31, ORTHOTROPIC_NUM_PROPERTIES } orthotropicMaterialProperties; |
| 290 |
typedef enum { MOONEYRIVLIN_DENSITY, MOONEYRIVLIN_MU01, MOONEYRIVLIN_MU10, MOONEYRIVLIN_V1, MOONEYRIVLIN_NUM_PROPERTIES } mooneyrivlinMaterialProperties; |
| 291 |
|
| 292 |
protected: |
| 293 |
std::string name; |
| 294 |
double density; |
| 295 |
}; |
| 296 |
|
| 297 |
|
| 298 |
class ENuMaterial; |
| 299 |
|
| 300 |
class MooneyRivlinMaterial; |
| 301 |
|
| 302 |
class OrthotropicMaterial; |
| 303 |
|
| 304 |
|
| 305 |
class Region |
| 306 |
{ |
| 307 |
public: |
| 308 |
Region(int materialIndex, int setIndex); |
| 309 |
inline int getMaterialIndex() const; |
| 310 |
inline int getSetIndex() const; |
| 311 |
inline void setMaterialIndex(int index); |
| 312 |
inline void setSetIndex(int index); |
| 313 |
|
| 314 |
protected: |
| 315 |
int setIndex, materialIndex; |
| 316 |
}; |
| 317 |
|
| 318 |
static double E_default; |
| 319 |
static double nu_default; |
| 320 |
static double density_default; |
| 321 |
|
| 322 |
protected: |
| 323 |
int numVertices; |
| 324 |
Vec3d * vertices; |
| 325 |
|
| 326 |
int numElementVertices; |
| 327 |
int numElements; |
| 328 |
int ** elements; |
| 329 |
|
| 330 |
int numMaterials; |
| 331 |
int numSets; |
| 332 |
int numRegions; |
| 333 |
Material ** materials; |
| 334 |
Set ** sets; |
| 335 |
Region ** regions; |
| 336 |
int * elementMaterial; |
| 337 |
|
| 338 |
|
| 339 |
VolumetricMesh(const char * filename, fileFormatType fileFormat, int numElementVertices, elementType * elementType_, int verbose); |
| 340 |
|
| 341 |
VolumetricMesh(void * binaryInputStream, int numElementVertices, elementType * elementType_, int memoryLoad = 0); |
| 342 |
VolumetricMesh(int numElementVertices_) { numElementVertices = numElementVertices_; } |
| 343 |
void propagateRegionsToElements(); |
| 344 |
void loadFromBinaryGeneric(void * binaryInputStream, elementType * elementType_, int memoryLoad); |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
VolumetricMesh(int numVertices, double * vertices, |
| 351 |
int numElements, int numElementVertices, int * elements, |
| 352 |
double E=E_default, double nu=nu_default, double density=density_default); |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
VolumetricMesh(int numVertices, double * vertices, |
| 361 |
int numElements, int numElementVertices, int * elements, |
| 362 |
int numMaterials, Material ** materials, |
| 363 |
int numSets, Set ** sets, |
| 364 |
int numRegions, Region ** regions); |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
VolumetricMesh(const VolumetricMesh & mesh, int numElements, int * elements, std::map<int,int> * vertexMap = NULL); |
| 369 |
|
| 370 |
int saveToAscii(const char * filename, elementType elementType_) const; |
| 371 |
int saveToBinary(const char * filename, unsigned int * bytesWritten, elementType elementType_) const; |
| 372 |
int saveToBinary(FILE * binaryOutputStream, unsigned int * bytesWritten, elementType elementType_, bool countBytesOnly = false) const; |
| 373 |
|
| 374 |
void loadFromAscii(const char * filename, elementType * elementType_, int verbose = 0); |
| 375 |
void loadFromBinary(const char * filename, elementType * elementType_); |
| 376 |
void loadFromBinary(FILE * binaryInputStream, elementType * elementType_); |
| 377 |
void loadFromMemory(unsigned char * binaryInputStream, elementType * elementType_); |
| 378 |
void assignMaterialsToElements(int verbose); |
| 379 |
|
| 380 |
static elementType getElementTypeASCII(const char * filename); |
| 381 |
static elementType getElementTypeBinary(const char * filename); |
| 382 |
|
| 383 |
elementType temp; |
| 384 |
|
| 385 |
static unsigned int readFromFile(void * buf, unsigned int elementSize, unsigned int numElements, void * fin); |
| 386 |
static unsigned int readFromMemory(void * buf, unsigned int elementSize, unsigned int numElements, void * memoryLocation); |
| 387 |
}; |
| 388 |
|
| 389 |
inline VolumetricMesh::Set::Set(const std::string & name_) { name = name_; } |
| 390 |
inline VolumetricMesh::Set::Set(const Set & set) { elements = set.elements; name = set.getName(); } |
| 391 |
inline VolumetricMesh::Set::Set(const std::string & name_, const std::set<int> & elements_) : name(name_), elements(elements_) {} |
| 392 |
inline std::string VolumetricMesh::Set::getName() const { return name; } |
| 393 |
inline int VolumetricMesh::Set::getNumElements() const { return (int)(this->elements.size()); } |
| 394 |
inline void VolumetricMesh::Set::getElements(std::set<int> & elements) const { elements = this->elements; } |
| 395 |
inline const std::set<int> & VolumetricMesh::Set::getElements() const { return elements; } |
| 396 |
inline std::set<int> & VolumetricMesh::Set::getElements() { return elements; } |
| 397 |
inline bool VolumetricMesh::Set::isMember(int element) const {return (elements.find(element) != elements.end());} |
| 398 |
inline void VolumetricMesh::Set::insert(int element) { elements.insert(element); } |
| 399 |
inline void VolumetricMesh::Set::clear() { elements.clear(); } |
| 400 |
|
| 401 |
inline VolumetricMesh::Material::Material(const std::string name_, double density_): density(density_) { name = name_; } |
| 402 |
inline VolumetricMesh::Material::Material(const Material & material) : density(material.getDensity()) { name = material.getName(); } |
| 403 |
inline std::string VolumetricMesh::Material::getName() const { return name; } |
| 404 |
inline double VolumetricMesh::Material::getDensity() const { return density; } |
| 405 |
inline void VolumetricMesh::Material::setName(const std::string name_) { name = name_; } |
| 406 |
inline void VolumetricMesh::Material::setDensity(double density_) { density = density_; } |
| 407 |
|
| 408 |
inline VolumetricMesh::Region::Region(int materialIndex_, int setIndex_): setIndex(setIndex_), materialIndex(materialIndex_) {} |
| 409 |
inline int VolumetricMesh::Region::getMaterialIndex() const { return materialIndex; } |
| 410 |
inline int VolumetricMesh::Region::getSetIndex() const { return setIndex; } |
| 411 |
inline void VolumetricMesh::Region::setMaterialIndex(int index) { materialIndex = index; } |
| 412 |
inline void VolumetricMesh::Region::setSetIndex(int index) { setIndex = index; } |
| 413 |
|
| 414 |
#endif |
| 415 |
|