| 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 | |
| 31 | This class is a container for a cubic ("voxel") volumetric 3D mesh. |
| 32 | See also volumetricMesh.h. |
| 33 | |
| 34 | All cubes are of equal size, aligned with coordinate system axes, and |
| 35 | follow a grid pattern. They are typically obtained by voxelizing a |
| 36 | given input triangle geometry. |
| 37 | |
| 38 | To generate a CubicMesh from an input triangle mesh (optionally flood-filling |
| 39 | interior chambers), you can use the "Large Modal Deformation Factory" |
| 40 | application ( http://www.jernejbarbic.com/code ) . |
| 41 | |
| 42 | */ |
| 43 | |
| 44 | #ifndef _CUBICMESH_H_ |
| 45 | #define _CUBICMESH_H_ |
| 46 | |
| 47 | #include "volumetricMesh.h" |
| 48 | // see also volumetricMesh.h for a description of the routines |
| 49 | |
| 50 | class CubicMesh : public VolumetricMesh |
| 51 | { |
| 52 | public: |
| 53 | // loads the mesh from a file |
| 54 | // ASCII: .veg text input formut, see documentation and the provided examples |
| 55 | // BINARY: .vegb binary input format |
| 56 | CubicMesh(const char * filename, fileFormatType fileFormat = ASCII, int verbose=1); |
| 57 | |
| 58 | // load from a stream |
| 59 | // if memoryLoad is 0, binaryStream is FILE* (load from a file), otherwise, it is char* (load from a memory buffer) |
| 60 | CubicMesh(void * binaryStream, int memoryLoad = 0); |
| 61 | |
| 62 | // constructs a mesh from the given vertices and elements, with a single region and material |
| 63 | // "vertices" is double-precision array of length 3 x numVertices |
| 64 | // "elements" is an integer array of length 8 x numElements |
| 65 | CubicMesh(int numVertices, double * vertices, |
| 66 | int numElements, int * elements, |
| 67 | double E=1E6, double nu=0.45, double density=1000); |
| 68 | |
| 69 | // constructs a mesh from the given vertices and elements, |
| 70 | // with an arbitrary number of sets, regions and materials |
| 71 | // "vertices" is double-precision array of length 3 x numVertices |
| 72 | // "elements" is an integer array of length 8 x numElements |
| 73 | // "materials", "sets" and "regions" will be copied internally (deep copy), so they |
| 74 | // can be released after calling this constructor |
| 75 | CubicMesh(int numVertices, double * vertices, |
| 76 | int numElements, int * elements, |
| 77 | int numMaterials, Material ** materials, |
| 78 | int numSets, Set ** sets, |
| 79 | int numRegions, Region ** regions); |
| 80 | |
| 81 | // constructs a voxel mesh with the given voxels, as a subset of a regular 3D grid |
| 82 | // 'voxels' gives the grid indices (3 per voxel) of the voxels that are to be included in the mesh |
| 83 | // 'voxels' has length 3 x numVoxels |
| 84 | static CubicMesh * createFromUniformGrid(int resolution, int numVoxels, int * voxels, double E=1E6, double nu=0.45, double density=1000); |
| 85 | |
| 86 | // creates a mesh consisting of the specified element subset of the given CubicMesh |
| 87 | CubicMesh(const CubicMesh & mesh, int numElements, int * elements, std::map<int,int> * vertexMap = NULL); |
| 88 | |
| 89 | CubicMesh (const CubicMesh & CubicMesh); |
| 90 | virtual VolumetricMesh * clone(); |
| 91 | virtual ~CubicMesh(); |
| 92 | |
| 93 | // saves the mesh to a text file (.veg format, see examples and documentation) |
| 94 | virtual int saveToAscii(const char * filename) const; |
| 95 | |
| 96 | // saves the mesh to binary format |
| 97 | // returns: 0 = success, non-zero = error |
| 98 | // output: if bytesWritten is non-NULL, it will contain the number of bytes written |
| 99 | virtual int saveToBinary(const char * filename, unsigned int * bytesWritten = NULL) const; |
| 100 | virtual int saveToBinary(FILE * binaryOutputStream, unsigned int * bytesWritten = NULL, bool countBytesOnly = false) const; |
| 101 | |
| 102 | // === misc queries === |
| 103 | |
| 104 | static VolumetricMesh::elementType elementType() { return elementType_; } |
| 105 | virtual VolumetricMesh::elementType getElementType() const { return elementType(); } |
| 106 | |
| 107 | inline double getCubeSize() const { return cubeSize; } |
| 108 | |
| 109 | virtual double getElementVolume(int el) const; |
| 110 | virtual void computeElementMassMatrix(int el, double * massMatrix) const; |
| 111 | virtual void getElementInertiaTensor(int el, Mat3d & inertiaTensor) const; |
| 112 | |
| 113 | virtual bool containsVertex(int element, Vec3d pos) const; // true if given element contain given position, false otherwise |
| 114 | |
| 115 | // edge queries |
| 116 | virtual int getNumElementEdges() const; |
| 117 | virtual void getElementEdges(int el, int * edgeBuffer) const; |
| 118 | |
| 119 | // subdivides the cube mesh |
| 120 | void subdivide(); |
| 121 | |
| 122 | // === interpolation === |
| 123 | |
| 124 | virtual void computeBarycentricWeights(int el, const Vec3d & pos, double * weights) const; |
| 125 | |
| 126 | int interpolateData(double * volumetricMeshVertexData, int numLocations, int r, double * interpolationLocations, double * destMatrix, double zeroThreshold = -1.0) const; |
| 127 | |
| 128 | // computes approximation to the normal correction for the given deformations |
| 129 | // vertexData size must a matrix of size 3 * nElements x r |
| 130 | // destMatrix must be a vector of length 3 * numLocations x r |
| 131 | // staticNormals must be a vector of length 3 * numLocations |
| 132 | // returns the number of vertices that were not contained inside any element |
| 133 | // vertices more than distanceThreshold away from any element vertex are assigned zero data |
| 134 | int normalCorrection(double * vertexData, int numLocations, int r, double * interpolationLocations, double * staticNormals, double * normalCorrection, double zeroThreshold = -1.0) const; // note: this routine could be promoted to volumetricMesh.h |
| 135 | |
| 136 | virtual void interpolateGradient(int element, const double * U, int numFields, Vec3d pos, double * grad) const; |
| 137 | |
| 138 | // advanced, to ensure computeBarycentricWeights, containsVertex, generateInterpolationWeights, generateContainingElements work even when elements are cubes, transformed via a general linear transformation |
| 139 | // parallelepiped=1 : the elements are cubes transformed via a linear transformation (i.e., they are parallelepipeds) |
| 140 | // parallelepiped=0 : (default) the elements are axis-aligned cubes |
| 141 | void setParallelepipedMode(int parallelepipedMode); |
| 142 | |
| 143 | protected: |
| 144 | double cubeSize; |
| 145 | double invCubeSize; |
| 146 | static const VolumetricMesh::elementType elementType_; |
| 147 | CubicMesh(int numElementVertices): VolumetricMesh(numElementVertices) {} |
| 148 | void SetInverseCubeSize(); |
| 149 | int parallelepipedMode; // normally this is 0; in advanced usage, it can be 1 (see above) |
| 150 | |
| 151 | // computes the normalized location of "pos" inside el |
| 152 | // when inside the element, one has 0 <= alpha <= 1, 0 <= beta <= 1, 0 <= gamma <= 1 |
| 153 | void computeAlphaBetaGamma(int el, Vec3d pos, double * alpha, double * beta, double * gamma) const; |
| 154 | |
| 155 | friend class VolumetricMeshExtensions; |
| 156 | }; |
| 157 | |
| 158 | #endif |
| 159 |