| 1 | /************************************************************************* |
| 2 | * * |
| 3 | * Vega FEM Simulation Library Version 3.1 * |
| 4 | * * |
| 5 | * "volumetricMesh" library , Copyright (C) 2007 CMU, 2009 MIT, 2016 USC * |
| 6 | * All rights reserved. * |
| 7 | * * |
| 8 | * Code author: Jernej Barbic * |
| 9 | * http://www.jernejbarbic.com/code * |
| 10 | * * |
| 11 | * Research: Jernej Barbic, Fun Shing Sin, Daniel Schroeder, * |
| 12 | * Doug L. James, Jovan Popovic * |
| 13 | * * |
| 14 | * Funding: National Science Foundation, Link Foundation, * |
| 15 | * Singapore-MIT GAMBIT Game Lab, * |
| 16 | * Zumberge Research and Innovation Fund at USC * |
| 17 | * * |
| 18 | * This library is free software; you can redistribute it and/or * |
| 19 | * modify it under the terms of the BSD-style license that is * |
| 20 | * included with this library in the file LICENSE.txt * |
| 21 | * * |
| 22 | * This library is distributed in the hope that it will be useful, * |
| 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file * |
| 25 | * LICENSE.TXT for more details. * |
| 26 | * * |
| 27 | *************************************************************************/ |
| 28 | |
| 29 | /* |
| 30 | This class is a container for a tetrahedral volumetric 3D mesh. See |
| 31 | also volumetricMesh.h. The tetrahedra can take arbitrary shapes (not |
| 32 | limited to only a few shapes). |
| 33 | */ |
| 34 | |
| 35 | #ifndef _TETMESH_H_ |
| 36 | #define _TETMESH_H_ |
| 37 | |
| 38 | #include "volumetricMesh.h" |
| 39 | |
| 40 | // see also volumetricMesh.h for a description of the routines |
| 41 | |
| 42 | class TetMesh : public VolumetricMesh |
| 43 | { |
| 44 | public: |
| 45 | // loads the mesh from a file |
| 46 | // ASCII: .veg text input format, see documentation and the provided examples |
| 47 | // BINARY: .vegb binary input format |
| 48 | TetMesh(const char * filename, fileFormatType fileFormat = ASCII, int verbose=1); |
| 49 | |
| 50 | // load from a stream |
| 51 | // if memoryLoad is 0, binaryStream is FILE* (load from a file), otherwise, it is char* (load from a memory buffer) |
| 52 | TetMesh(void * binaryStream, int memoryLoad = 0); |
| 53 | |
| 54 | // constructs a tet mesh from the given vertices and elements, |
| 55 | // with a single region and material ("E, nu" material) |
| 56 | // "vertices" is double-precision array of length 3 x numVertices . |
| 57 | // "elements" is an integer array of length 4 x numElements |
| 58 | TetMesh(int numVertices, double * vertices, |
| 59 | int numElements, int * elements, |
| 60 | double E=1E6, double nu=0.45, double density=1000); |
| 61 | |
| 62 | // constructs a tet mesh from the given vertices and elements, |
| 63 | // with an arbitrary number of sets, regions and materials |
| 64 | // "vertices" is double-precision array of length 3 x numVertices |
| 65 | // "elements" is an integer array of length 4 x numElements |
| 66 | // "materials", "sets" and "regions" will be copied internally (deep copy), so you |
| 67 | // can release them after calling this constructor |
| 68 | TetMesh(int numVertices, double * vertices, |
| 69 | int numElements, int * elements, |
| 70 | int numMaterials, Material ** materials, |
| 71 | int numSets, Set ** sets, |
| 72 | int numRegions, Region ** regions); |
| 73 | |
| 74 | // loads a file of a "special" (not .veg) type |
| 75 | // currently one such special format is supported: |
| 76 | // specialFileType=0: |
| 77 | // the ".ele" and ".node" format, used by TetGen, |
| 78 | // "filename" is the basename, e.g., passing "mesh" will load the mesh from "mesh.ele" and "mesh.node" |
| 79 | // default material parameters will be used |
| 80 | TetMesh(const char * filename, int specialFileType, int verbose); |
| 81 | |
| 82 | // creates a mesh consisting of the specified element subset of the given TetMesh |
| 83 | TetMesh(const TetMesh & mesh, int numElements, int * elements, std::map<int,int> * vertexMap = NULL); |
| 84 | |
| 85 | TetMesh(const TetMesh & tetMesh); |
| 86 | virtual VolumetricMesh * clone(); |
| 87 | virtual ~TetMesh(); |
| 88 | |
| 89 | virtual int saveToAscii(const char * filename) const; |
| 90 | // saves the mesh to binary format |
| 91 | // returns: 0 = success, non-zero = error |
| 92 | // output: if bytesWritten is non-NULL, it will contain the number of bytes written |
| 93 | virtual int saveToBinary(const char * filename, unsigned int * bytesWritten = NULL) const; |
| 94 | virtual int saveToBinary(FILE * binaryOutputStream, unsigned int * bytesWritten = NULL, bool countBytesOnly = false) const; |
| 95 | |
| 96 | // === misc queries === |
| 97 | |
| 98 | static VolumetricMesh::elementType elementType() { return elementType_; } |
| 99 | virtual VolumetricMesh::elementType getElementType() const { return elementType(); } |
| 100 | |
| 101 | static double getTetVolume(const Vec3d & a, const Vec3d & b, const Vec3d & c, const Vec3d & d); |
| 102 | static double getTetDeterminant(const Vec3d & a, const Vec3d & b, const Vec3d & c, const Vec3d & d); |
| 103 | |
| 104 | virtual double getElementVolume(int el) const; |
| 105 | virtual void getElementInertiaTensor(int el, Mat3d & inertiaTensor) const; |
| 106 | virtual void computeElementMassMatrix(int element, double * massMatrix) const; |
| 107 | |
| 108 | virtual bool containsVertex(int element, Vec3d pos) const; // true if given element contain given position, false otherwise |
| 109 | |
| 110 | // edge queries |
| 111 | virtual int getNumElementEdges() const; |
| 112 | virtual void getElementEdges(int el, int * edgeBuffer) const; |
| 113 | |
| 114 | // === interpolation === |
| 115 | |
| 116 | static void computeBarycentricWeights(const Vec3d vertexPos[4], const Vec3d & pos, double * weights); |
| 117 | virtual void computeBarycentricWeights(int el, const Vec3d & pos, double * weights) const; |
| 118 | void computeGradient(int element, const double * U, int numFields, double * grad) const; // for tet meshes, gradient is constant inside each tet, hence no need to specify position |
| 119 | virtual void interpolateGradient(int element, const double * U, int numFields, Vec3d pos, double * grad) const; // conforms to the virtual function in the base class, "pos" does not affect the computation |
| 120 | |
| 121 | // === misc === |
| 122 | |
| 123 | void orient(); // orients the tets (re-orders vertices within each tet), so that each tet has positive orientation: ((v1 - v0) x (v2 - v0)) dot (v3 - v0) >= 0 |
| 124 | |
| 125 | protected: |
| 126 | static const VolumetricMesh::elementType elementType_; |
| 127 | TetMesh(int numElementVertices): VolumetricMesh(numElementVertices) {} |
| 128 | |
| 129 | friend class VolumetricMeshExtensions; |
| 130 | }; |
| 131 | |
| 132 | #endif |
| 133 |