| 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 | #include <float.h> |
| 30 | #include <string.h> |
| 31 | #include <assert.h> |
| 32 | #include <set> |
| 33 | #include <map> |
| 34 | #include <vector> |
| 35 | #include <algorithm> |
| 36 | #include "cubicMesh.h" |
| 37 | #include "matrixMacros.h" |
| 38 | #include "matrixMultiplyMacros.h" |
| 39 | #include "triple.h" |
| 40 | using namespace std; |
| 41 | |
| 42 | const VolumetricMesh::elementType CubicMesh::elementType_ = CUBIC; |
| 43 | |
| 44 | CubicMesh::CubicMesh(const char * filename, fileFormatType fileFormat, int verbose) : VolumetricMesh(filename, fileFormat, 8, &temp, verbose), parallelepipedMode(0) |
| 45 | { |
| 46 | if (temp != elementType_) |
| 47 | { |
| 48 | printf("Error: mesh is not a cubic mesh.\n"); |
| 49 | throw 11; |
| 50 | } |
| 51 | |
| 52 | // set cube size |
| 53 | cubeSize = len(getVertex(0,1) - getVertex(0,0)); |
| 54 | SetInverseCubeSize(); |
| 55 | } |
| 56 | |
| 57 | CubicMesh::CubicMesh(void * binaryStream, int memoryLoad) : |
| 58 | VolumetricMesh(binaryStream, 8, &temp, memoryLoad), parallelepipedMode(0) |
| 59 | { |
| 60 | if (temp != elementType_) |
| 61 | { |
| 62 | printf("Error: mesh is not a cubic mesh.\n"); |
| 63 | throw 11; |
| 64 | } |
| 65 | |
| 66 | // set cube size |
| 67 | cubeSize = len(getVertex(0,1) - getVertex(0,0)); |
| 68 | SetInverseCubeSize(); |
| 69 | } |
| 70 | |
| 71 | CubicMesh::CubicMesh(int numVertices, double * vertices, |
| 72 | int numElements, int * elements, |
| 73 | double E, double nu, double density): |
| 74 | VolumetricMesh(numVertices, vertices, numElements, 8, elements, E, nu, density), parallelepipedMode(0) |
| 75 | { |
| 76 | if (numElements > 0) |
| 77 | cubeSize = len(getVertex(0,1) - getVertex(0,0)); |
| 78 | else |
| 79 | cubeSize = 0.0; |
| 80 | |
| 81 | SetInverseCubeSize(); |
| 82 | } |
| 83 | |
| 84 | CubicMesh::CubicMesh(int numVertices, double * vertices, |
| 85 | int numElements, int * elements, |
| 86 | int numMaterials, Material ** materials, |
| 87 | int numSets, Set ** sets, |
| 88 | int numRegions, Region ** regions): |
| 89 | VolumetricMesh(numVertices, vertices, numElements, 8, elements, numMaterials, materials, numSets, sets, numRegions, regions), parallelepipedMode(0) |
| 90 | { |
| 91 | if (numElements > 0) |
| 92 | cubeSize = len(getVertex(0,1) - getVertex(0,0)); |
| 93 | else |
| 94 | cubeSize = 0.0; |
| 95 | |
| 96 | SetInverseCubeSize(); |
| 97 | } |
| 98 | |
| 99 | CubicMesh::CubicMesh(const CubicMesh & source): VolumetricMesh(source), cubeSize(source.cubeSize), invCubeSize(source.invCubeSize), parallelepipedMode(source.parallelepipedMode) {} |
| 100 | |
| 101 | VolumetricMesh * CubicMesh::clone() |
| 102 | { |
| 103 | CubicMesh * mesh = new CubicMesh(*this); |
| 104 | return mesh; |
| 105 | } |
| 106 | |
| 107 | void CubicMesh::SetInverseCubeSize() |
| 108 | { |
| 109 | if (cubeSize > 0) |
| 110 | invCubeSize = 1.0 / cubeSize; |
| 111 | else |
| 112 | invCubeSize = 0; |
| 113 | } |
| 114 | |
| 115 | CubicMesh * CubicMesh::createFromUniformGrid(int resolution, int numVoxels, int * voxels, double E, double nu, double density) |
| 116 | { |
| 117 | int numElementVertices = 8; |
| 118 | |
| 119 | // create the indices of all vertices |
| 120 | typedef triple<int,int,int> tripleIndex; |
| 121 | set<tripleIndex> vertexSet; |
| 122 | int vtxI[8] = { 0, 1, 1, 0, 0, 1, 1, 0 }; |
| 123 | int vtxJ[8] = { 0, 0, 1, 1, 0, 0, 1, 1 }; |
| 124 | int vtxK[8] = { 0, 0, 0, 0, 1, 1, 1, 1 }; |
| 125 | for(int vox=0; vox<numVoxels; vox++) |
| 126 | { |
| 127 | int i = voxels[3*vox+0]; |
| 128 | int j = voxels[3*vox+1]; |
| 129 | int k = voxels[3*vox+2]; |
| 130 | |
| 131 | for(int corner=0; corner<numElementVertices; corner++) |
| 132 | { |
| 133 | tripleIndex triIndex(i+vtxI[corner], j+vtxJ[corner], k+vtxK[corner]); |
| 134 | vertexSet.insert(triIndex); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | int numVertices = (int)vertexSet.size(); |
| 139 | double * vertices = (double*) malloc (sizeof(double) * 3 * numVertices); |
| 140 | int count = 0; |
| 141 | map<tripleIndex, int> vertexMap; |
| 142 | for(set<tripleIndex> :: iterator iter = vertexSet.begin(); iter != vertexSet.end(); iter++) |
| 143 | { |
| 144 | int i = iter->first; |
| 145 | int j = iter->second; |
| 146 | int k = iter->third; |
| 147 | vertices[3*count+0] = -0.5 + 1.0 * i / resolution; |
| 148 | vertices[3*count+1] = -0.5 + 1.0 * j / resolution; |
| 149 | vertices[3*count+2] = -0.5 + 1.0 * k / resolution; |
| 150 | vertexMap.insert(make_pair(tripleIndex(i,j,k), count)); |
| 151 | //printf("%d %d %d: %d\n", i,j,k, count); |
| 152 | count++; |
| 153 | } |
| 154 | |
| 155 | int numElements = numVoxels; |
| 156 | |
| 157 | int * elements = (int*) malloc (sizeof(int) * numElements * numElementVertices); |
| 158 | for(int vox=0; vox<numElements; vox++) |
| 159 | { |
| 160 | int i = voxels[3*vox+0]; |
| 161 | int j = voxels[3*vox+1]; |
| 162 | int k = voxels[3*vox+2]; |
| 163 | for(int corner=0; corner<numElementVertices; corner++) |
| 164 | { |
| 165 | int I = i + vtxI[corner]; |
| 166 | int J = j + vtxJ[corner]; |
| 167 | int K = k + vtxK[corner]; |
| 168 | //printf("I=%d J=%d K=%d\n", I, J, K); |
| 169 | |
| 170 | // find I, J, K |
| 171 | int vtxIndex = vertexMap[tripleIndex(I, J, K)]; |
| 172 | //printf("vtxIndex = %d\n", vtxIndex); |
| 173 | elements[numElementVertices * vox + corner] = vtxIndex; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | CubicMesh * cubeMesh = new CubicMesh(numVertices, vertices, numElements, elements, E, nu, density); |
| 178 | |
| 179 | free(vertices); |
| 180 | free(elements); |
| 181 | |
| 182 | return cubeMesh; |
| 183 | } |
| 184 | |
| 185 | CubicMesh::CubicMesh(const CubicMesh & cubeMesh, int numElements, int * elements, map<int,int> * vertexMap_): VolumetricMesh(cubeMesh, numElements, elements, vertexMap_) |
| 186 | { |
| 187 | cubeSize = cubeMesh.getCubeSize(); |
| 188 | SetInverseCubeSize(); |
| 189 | } |
| 190 | |
| 191 | CubicMesh::~CubicMesh() {} |
| 192 | |
| 193 | int CubicMesh::saveToAscii(const char * filename) const |
| 194 | { |
| 195 | return VolumetricMesh::saveToAscii(filename, elementType_); |
| 196 | } |
| 197 | |
| 198 | int CubicMesh::saveToBinary(const char * filename, unsigned int * bytesWritten) const |
| 199 | { |
| 200 | return VolumetricMesh::saveToBinary(filename, bytesWritten, elementType_); |
| 201 | } |
| 202 | |
| 203 | int CubicMesh::saveToBinary(FILE * binaryOutputStream, unsigned int * bytesWritten, bool countBytesOnly) const |
| 204 | { |
| 205 | return VolumetricMesh::saveToBinary(binaryOutputStream, bytesWritten, elementType_, countBytesOnly); |
| 206 | } |
| 207 | |
| 208 | bool CubicMesh::containsVertex(int element, Vec3d pos) const |
| 209 | { |
| 210 | /* |
| 211 | AABB version |
| 212 | Vec3d bmin = (getVertex(element, 0)); |
| 213 | Vec3d bmax = (getVertex(element, 6)); |
| 214 | |
| 215 | return (pos[0] >= bmin[0]) && (pos[1] >= bmin[1]) && (pos[2] >= bmin[2]) && |
| 216 | (pos[0] <= bmax[0]) && (pos[1] <= bmax[1]) && (pos[2] <= bmax[2]); |
| 217 | */ |
| 218 | |
| 219 | // general, parallelepied version (supports cubes transformed by a linear transformation; although the class does not "officially" support this) |
| 220 | double alpha, beta, gamma; |
| 221 | computeAlphaBetaGamma(element, pos, &alpha, &beta, &gamma); |
| 222 | |
| 223 | return ( (0 <= alpha) && (alpha <= 1) && |
| 224 | (0 <= beta) && (beta <= 1) && |
| 225 | (0 <= gamma) && (gamma <= 1) ); |
| 226 | } |
| 227 | |
| 228 | // interpolates given cubic mesh vertex 3D data to the destination locations |
| 229 | // vertexData size must a vector of length 3 * nElements |
| 230 | // destMatrix must be a vector of length 3 * numLocations |
| 231 | // if a given location does not belong to any element, extrapolation to the nearest element will be used |
| 232 | // vertices more than distanceThreshold away from any element vertex are assigned zero data |
| 233 | int CubicMesh::interpolateData(double * vertexData, int numInterpolationLocations, int r, double * interpolationLocations, double * destMatrix, double distanceThreshold) const |
| 234 | { |
| 235 | assert(numElementVertices == 8); |
| 236 | |
| 237 | int numExternalVertices = 0; |
| 238 | |
| 239 | for (int i=0; i < numInterpolationLocations; i++) // over all interpolation locations |
| 240 | { |
| 241 | if (i % 100 == 0) |
| 242 | { |
| 243 | printf("%d ", i); fflush(NULL); |
| 244 | } |
| 245 | |
| 246 | Vec3d pos = Vec3d(interpolationLocations[3*i+0], interpolationLocations[3*i+1], interpolationLocations[3*i+2]); |
| 247 | |
| 248 | // find element containing pos |
| 249 | int element = getClosestElement(pos); |
| 250 | |
| 251 | if (!containsVertex(element, pos)) |
| 252 | numExternalVertices++; |
| 253 | |
| 254 | if (distanceThreshold > 0) |
| 255 | { |
| 256 | // check whether vertex is close enough to the cube mesh |
| 257 | double minDistance = DBL_MAX; |
| 258 | int assignedZero = 0; |
| 259 | for(int ii=0; ii< numElementVertices; ii++) |
| 260 | { |
| 261 | const Vec3d & vpos = getVertex(element, ii); |
| 262 | if (len(vpos-pos) < minDistance) |
| 263 | { |
| 264 | minDistance = len(vpos-pos); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | if (minDistance > distanceThreshold) |
| 269 | { |
| 270 | // assign zero data |
| 271 | for(int j=0; j<r; j++) |
| 272 | { |
| 273 | destMatrix[ELT(3*numInterpolationLocations, 3 * i + 0, j)] = 0; |
| 274 | destMatrix[ELT(3*numInterpolationLocations, 3 * i + 1, j)] = 0; |
| 275 | destMatrix[ELT(3*numInterpolationLocations, 3 * i + 2, j)] = 0; |
| 276 | } |
| 277 | assignedZero++; |
| 278 | continue; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | // compute barycentric coordinates |
| 283 | Vec3d w = pos - getVertex(element, 0); |
| 284 | double alpha = w[0] * invCubeSize; |
| 285 | double beta = w[1] * invCubeSize; |
| 286 | double gamma = w[2] * invCubeSize; |
| 287 | |
| 288 | double f000 = (1-alpha)*(1-beta)*(1-gamma); |
| 289 | double f100 = (alpha)*(1-beta)*(1-gamma); |
| 290 | double f110 = (alpha)*(beta)*(1-gamma); |
| 291 | double f010 = (1-alpha)*(beta)*(1-gamma); |
| 292 | |
| 293 | double f001 = (1-alpha)*(1-beta)*(gamma); |
| 294 | double f101 = (alpha)*(1-beta)*(gamma); |
| 295 | double f111 = (alpha)*(beta)*(gamma); |
| 296 | double f011 = (1-alpha)*(beta)*(gamma); |
| 297 | |
| 298 | int v000 = getVertexIndex(element, 0); |
| 299 | int v100 = getVertexIndex(element, 1); |
| 300 | int v110 = getVertexIndex(element, 2); |
| 301 | int v010 = getVertexIndex(element, 3); |
| 302 | int v001 = getVertexIndex(element, 4); |
| 303 | int v101 = getVertexIndex(element, 5); |
| 304 | int v111 = getVertexIndex(element, 6); |
| 305 | int v011 = getVertexIndex(element, 7); |
| 306 | |
| 307 | for(int j=0; j<r; j++) |
| 308 | { |
| 309 | Vec3d data000 = Vec3d (vertexData[ELT(3*numVertices, 3 * v000 + 0, j)], vertexData[ELT(3*numVertices, 3 * v000 + 1, j)], vertexData[ELT(3*numVertices, 3 * v000 + 2, j)]); |
| 310 | Vec3d data100 = Vec3d (vertexData[ELT(3*numVertices, 3 * v100 + 0, j)], vertexData[ELT(3*numVertices, 3 * v100 + 1, j)], vertexData[ELT(3*numVertices, 3 * v100 + 2, j)]); |
| 311 | Vec3d data110 = Vec3d (vertexData[ELT(3*numVertices, 3 * v110 + 0, j)], vertexData[ELT(3*numVertices, 3 * v110 + 1, j)], vertexData[ELT(3*numVertices, 3 * v110 + 2, j)]); |
| 312 | Vec3d data010 = Vec3d (vertexData[ELT(3*numVertices, 3 * v010 + 0, j)], vertexData[ELT(3*numVertices, 3 * v010 + 1, j)], vertexData[ELT(3*numVertices, 3 * v010 + 2, j)]); |
| 313 | |
| 314 | Vec3d data001 = Vec3d (vertexData[ELT(3*numVertices, 3 * v001 + 0, j)], vertexData[ELT(3*numVertices, 3 * v001 + 1, j)], vertexData[ELT(3*numVertices, 3 * v001 + 2, j)]); |
| 315 | Vec3d data101 = Vec3d (vertexData[ELT(3*numVertices, 3 * v101 + 0, j)], vertexData[ELT(3*numVertices, 3 * v101 + 1, j)], vertexData[ELT(3*numVertices, 3 * v101 + 2, j)]); |
| 316 | Vec3d data111 = Vec3d (vertexData[ELT(3*numVertices, 3 * v111 + 0, j)], vertexData[ELT(3*numVertices, 3 * v111 + 1, j)], vertexData[ELT(3*numVertices, 3 * v111 + 2, j)]); |
| 317 | Vec3d data011 = Vec3d (vertexData[ELT(3*numVertices, 3 * v011 + 0, j)], vertexData[ELT(3*numVertices, 3 * v011 + 1, j)], vertexData[ELT(3*numVertices, 3 * v011 + 2, j)]); |
| 318 | |
| 319 | Vec3d interpolatedData = f000 * data000 + f100 * data100 + f110 * data110 + f010 * data010 + |
| 320 | f001 * data001 + f101 * data101 + f111 * data111 + f011 * data011 ; |
| 321 | |
| 322 | destMatrix[ELT(3*numInterpolationLocations,3*i+0,j)] = interpolatedData[0]; |
| 323 | destMatrix[ELT(3*numInterpolationLocations,3*i+1,j)] = interpolatedData[1]; |
| 324 | destMatrix[ELT(3*numInterpolationLocations,3*i+2,j)] = interpolatedData[2]; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | printf("\n"); |
| 329 | |
| 330 | return numExternalVertices; |
| 331 | } |
| 332 | |
| 333 | // computes approximation to the normal correction for the given deformation |
| 334 | // vertexData size must a vector of length 3 * nElements |
| 335 | // normalCorrection (output) must be a vector of length 3 * numLocations |
| 336 | // staticNormals must be a vector of length 3 * numLocations |
| 337 | // vertices more than distanceThreshold away from any element vertex are assigned zero data |
| 338 | int CubicMesh::normalCorrection(double * vertexData, int numInterpolationLocations, int r, double * interpolationLocations, double * staticNormals, double * normalCorrection, double distanceThreshold) const |
| 339 | { |
| 340 | assert(numElementVertices == 8); |
| 341 | |
| 342 | int numExternalVertices = 0; |
| 343 | |
| 344 | for (int i=0; i < numInterpolationLocations; i++) // over all interpolation locations |
| 345 | { |
| 346 | |
| 347 | if (i % 100 == 0) |
| 348 | { |
| 349 | printf("%d ", i); fflush(NULL); |
| 350 | } |
| 351 | |
| 352 | Vec3d pos = Vec3d(interpolationLocations[3*i+0], interpolationLocations[3*i+1], interpolationLocations[3*i+2]); |
| 353 | |
| 354 | // find element containing pos |
| 355 | int element = getClosestElement(pos); |
| 356 | |
| 357 | if (!containsVertex(element, pos)) |
| 358 | numExternalVertices++; |
| 359 | |
| 360 | if (distanceThreshold > 0) |
| 361 | { |
| 362 | // check whether vertex is close enough to the cube mesh |
| 363 | double minDistance = DBL_MAX; |
| 364 | int assignedZero = 0; |
| 365 | for(int ii=0; ii< numElementVertices; ii++) |
| 366 | { |
| 367 | const Vec3d & vpos = getVertex(element, ii); |
| 368 | if (len(vpos-pos) < minDistance) |
| 369 | { |
| 370 | minDistance = len(vpos-pos); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | if (minDistance > distanceThreshold) |
| 375 | { |
| 376 | // assign zero data |
| 377 | for(int j=0; j<r; j++) |
| 378 | { |
| 379 | normalCorrection[ELT(3*numInterpolationLocations, 3 * i + 0, j)] = 0; |
| 380 | normalCorrection[ELT(3*numInterpolationLocations, 3 * i + 1, j)] = 0; |
| 381 | normalCorrection[ELT(3*numInterpolationLocations, 3 * i + 2, j)] = 0; |
| 382 | } |
| 383 | assignedZero++; |
| 384 | |
| 385 | continue; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | // compute barycentric coordinates |
| 390 | Vec3d w = pos - getVertex(element, 0); |
| 391 | double alpha = w[0] * invCubeSize; |
| 392 | double beta = w[1] * invCubeSize; |
| 393 | double gamma = w[2] * invCubeSize; |
| 394 | |
| 395 | //double f000 = (1-alpha)*(1-beta)*(1-gamma); |
| 396 | //double f100 = (alpha)*(1-beta)*(1-gamma); |
| 397 | //double f110 = (alpha)*(beta)*(1-gamma); |
| 398 | //double f010 = (1-alpha)*(beta)*(1-gamma); |
| 399 | |
| 400 | //double f001 = (1-alpha)*(1-beta)*(gamma); |
| 401 | //double f101 = (alpha)*(1-beta)*(gamma); |
| 402 | //double f111 = (alpha)*(beta)*(gamma); |
| 403 | //double f011 = (1-alpha)*(beta)*(gamma); |
| 404 | |
| 405 | int v000 = getVertexIndex(element, 0); |
| 406 | int v100 = getVertexIndex(element, 1); |
| 407 | int v110 = getVertexIndex(element, 2); |
| 408 | int v010 = getVertexIndex(element, 3); |
| 409 | int v001 = getVertexIndex(element, 4); |
| 410 | int v101 = getVertexIndex(element, 5); |
| 411 | int v111 = getVertexIndex(element, 6); |
| 412 | int v011 = getVertexIndex(element, 7); |
| 413 | |
| 414 | Vec3d gradf000(invCubeSize * -(1-beta)*(1-gamma), invCubeSize * -(1-alpha)*(1-gamma), invCubeSize * -(1-alpha)*(1-beta)); |
| 415 | Vec3d gradf100(invCubeSize * (1-beta)*(1-gamma), invCubeSize * -alpha*(1-gamma), invCubeSize * -alpha*(1-beta)); |
| 416 | Vec3d gradf110(invCubeSize * beta*(1-gamma), invCubeSize * alpha*(1-gamma), invCubeSize * -alpha*beta); |
| 417 | Vec3d gradf010(invCubeSize * -beta*(1-gamma), invCubeSize * (1-alpha)*(1-gamma), invCubeSize * (1-alpha)*-beta); |
| 418 | |
| 419 | Vec3d gradf001(invCubeSize * -(1-beta)*gamma, invCubeSize * -(1-alpha)*gamma, invCubeSize * (1-alpha)*(1-beta)); |
| 420 | Vec3d gradf101(invCubeSize * (1-beta)*gamma, invCubeSize * -alpha*gamma, invCubeSize * alpha*(1-beta)); |
| 421 | Vec3d gradf111(invCubeSize * beta*gamma, invCubeSize * alpha*gamma, invCubeSize * alpha*beta); |
| 422 | Vec3d gradf011(invCubeSize * -beta*gamma, invCubeSize * (1-alpha)*gamma, invCubeSize * (1-alpha)*beta); |
| 423 | |
| 424 | Vec3d normal = Vec3d(staticNormals[3*i+0], staticNormals[3*i+1], staticNormals[3*i+2]); |
| 425 | |
| 426 | for(int j=0; j<r; j++) |
| 427 | { |
| 428 | Vec3d u000 = Vec3d(vertexData[ELT(3*numVertices, 3 * v000 + 0, j)], vertexData[ELT(3*numVertices, 3 * v000 + 1, j)], vertexData[ELT(3*numVertices, 3 * v000 + 2, j)]); |
| 429 | Vec3d u100 = Vec3d(vertexData[ELT(3*numVertices, 3 * v100 + 0, j)], vertexData[ELT(3*numVertices, 3 * v100 + 1, j)], vertexData[ELT(3*numVertices, 3 * v100 + 2, j)]); |
| 430 | Vec3d u110 = Vec3d(vertexData[ELT(3*numVertices, 3 * v110 + 0, j)], vertexData[ELT(3*numVertices, 3 * v110 + 1, j)], vertexData[ELT(3*numVertices, 3 * v110 + 2, j)]); |
| 431 | Vec3d u010 = Vec3d(vertexData[ELT(3*numVertices, 3 * v010 + 0, j)], vertexData[ELT(3*numVertices, 3 * v010 + 1, j)], vertexData[ELT(3*numVertices, 3 * v010 + 2, j)]); |
| 432 | |
| 433 | Vec3d u001 = Vec3d(vertexData[ELT(3*numVertices, 3 * v001 + 0, j)], vertexData[ELT(3*numVertices, 3 * v001 + 1, j)], vertexData[ELT(3*numVertices, 3 * v001 + 2, j)]); |
| 434 | Vec3d u101 = Vec3d(vertexData[ELT(3*numVertices, 3 * v101 + 0, j)], vertexData[ELT(3*numVertices, 3 * v101 + 1, j)], vertexData[ELT(3*numVertices, 3 * v101 + 2, j)]); |
| 435 | Vec3d u111 = Vec3d(vertexData[ELT(3*numVertices, 3 * v111 + 0, j)], vertexData[ELT(3*numVertices, 3 * v111 + 1, j)], vertexData[ELT(3*numVertices, 3 * v111 + 2, j)]); |
| 436 | Vec3d u011 = Vec3d(vertexData[ELT(3*numVertices, 3 * v011 + 0, j)], vertexData[ELT(3*numVertices, 3 * v011 + 1, j)], vertexData[ELT(3*numVertices, 3 * v011 + 2, j)]); |
| 437 | |
| 438 | Vec3d coef(0,0,0); |
| 439 | coef += dot(gradf000,normal) * u000; |
| 440 | coef += dot(gradf100,normal) * u100; |
| 441 | coef += dot(gradf110,normal) * u110; |
| 442 | coef += dot(gradf010,normal) * u010; |
| 443 | coef += dot(gradf001,normal) * u001; |
| 444 | coef += dot(gradf101,normal) * u101; |
| 445 | coef += dot(gradf111,normal) * u111; |
| 446 | coef += dot(gradf011,normal) * u011; |
| 447 | |
| 448 | normalCorrection[ELT(3*numInterpolationLocations,3*i+0,j)] = coef[0]; |
| 449 | normalCorrection[ELT(3*numInterpolationLocations,3*i+1,j)] = coef[1]; |
| 450 | normalCorrection[ELT(3*numInterpolationLocations,3*i+2,j)] = coef[2]; |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | printf("\n"); |
| 455 | |
| 456 | return numExternalVertices; |
| 457 | } |
| 458 | |
| 459 | void CubicMesh::interpolateGradient(int element, const double * U, int numFields, Vec3d pos, double * grad) const |
| 460 | { |
| 461 | // compute barycentric coordinates |
| 462 | Vec3d w = pos - getVertex(element, 0); |
| 463 | double alpha = w[0] * invCubeSize; |
| 464 | double beta = w[1] * invCubeSize; |
| 465 | double gamma = w[2] * invCubeSize; |
| 466 | |
| 467 | //double f000 = (1-alpha)*(1-beta)*(1-gamma); |
| 468 | //double f100 = (alpha)*(1-beta)*(1-gamma); |
| 469 | //double f110 = (alpha)*(beta)*(1-gamma); |
| 470 | //double f010 = (1-alpha)*(beta)*(1-gamma); |
| 471 | |
| 472 | //double f001 = (1-alpha)*(1-beta)*(gamma); |
| 473 | //double f101 = (alpha)*(1-beta)*(gamma); |
| 474 | //double f111 = (alpha)*(beta)*(gamma); |
| 475 | //double f011 = (1-alpha)*(beta)*(gamma); |
| 476 | |
| 477 | int v000 = getVertexIndex(element, 0); |
| 478 | int v100 = getVertexIndex(element, 1); |
| 479 | int v110 = getVertexIndex(element, 2); |
| 480 | int v010 = getVertexIndex(element, 3); |
| 481 | int v001 = getVertexIndex(element, 4); |
| 482 | int v101 = getVertexIndex(element, 5); |
| 483 | int v111 = getVertexIndex(element, 6); |
| 484 | int v011 = getVertexIndex(element, 7); |
| 485 | |
| 486 | Vec3d gradf000(invCubeSize * -(1-beta)*(1-gamma), invCubeSize * -(1-alpha)*(1-gamma), invCubeSize * -(1-alpha)*(1-beta)); |
| 487 | Vec3d gradf100(invCubeSize * (1-beta)*(1-gamma), invCubeSize * -alpha*(1-gamma), invCubeSize * -alpha*(1-beta)); |
| 488 | Vec3d gradf110(invCubeSize * beta*(1-gamma), invCubeSize * alpha*(1-gamma), invCubeSize * -alpha*beta); |
| 489 | Vec3d gradf010(invCubeSize * -beta*(1-gamma), invCubeSize * (1-alpha)*(1-gamma), invCubeSize * (1-alpha)*-beta); |
| 490 | |
| 491 | Vec3d gradf001(invCubeSize * -(1-beta)*gamma, invCubeSize * -(1-alpha)*gamma, invCubeSize * (1-alpha)*(1-beta)); |
| 492 | Vec3d gradf101(invCubeSize * (1-beta)*gamma, invCubeSize * -alpha*gamma, invCubeSize * alpha*(1-beta)); |
| 493 | Vec3d gradf111(invCubeSize * beta*gamma, invCubeSize * alpha*gamma, invCubeSize * alpha*beta); |
| 494 | Vec3d gradf011(invCubeSize * -beta*gamma, invCubeSize * (1-alpha)*gamma, invCubeSize * (1-alpha)*beta); |
| 495 | |
| 496 | for(int j=0; j<numFields; j++) |
| 497 | { |
| 498 | Vec3d u000 = Vec3d(U[ELT(3*numVertices, 3 * v000 + 0, j)], U[ELT(3*numVertices, 3 * v000 + 1, j)], U[ELT(3*numVertices, 3 * v000 + 2, j)]); |
| 499 | Vec3d u100 = Vec3d(U[ELT(3*numVertices, 3 * v100 + 0, j)], U[ELT(3*numVertices, 3 * v100 + 1, j)], U[ELT(3*numVertices, 3 * v100 + 2, j)]); |
| 500 | Vec3d u110 = Vec3d(U[ELT(3*numVertices, 3 * v110 + 0, j)], U[ELT(3*numVertices, 3 * v110 + 1, j)], U[ELT(3*numVertices, 3 * v110 + 2, j)]); |
| 501 | Vec3d u010 = Vec3d(U[ELT(3*numVertices, 3 * v010 + 0, j)], U[ELT(3*numVertices, 3 * v010 + 1, j)], U[ELT(3*numVertices, 3 * v010 + 2, j)]); |
| 502 | |
| 503 | Vec3d u001 = Vec3d(U[ELT(3*numVertices, 3 * v001 + 0, j)], U[ELT(3*numVertices, 3 * v001 + 1, j)], U[ELT(3*numVertices, 3 * v001 + 2, j)]); |
| 504 | Vec3d u101 = Vec3d(U[ELT(3*numVertices, 3 * v101 + 0, j)], U[ELT(3*numVertices, 3 * v101 + 1, j)], U[ELT(3*numVertices, 3 * v101 + 2, j)]); |
| 505 | Vec3d u111 = Vec3d(U[ELT(3*numVertices, 3 * v111 + 0, j)], U[ELT(3*numVertices, 3 * v111 + 1, j)], U[ELT(3*numVertices, 3 * v111 + 2, j)]); |
| 506 | Vec3d u011 = Vec3d(U[ELT(3*numVertices, 3 * v011 + 0, j)], U[ELT(3*numVertices, 3 * v011 + 1, j)], U[ELT(3*numVertices, 3 * v011 + 2, j)]); |
| 507 | |
| 508 | Mat3d FMode(0.0); |
| 509 | FMode += tensorProduct(u000, gradf000); |
| 510 | FMode += tensorProduct(u100, gradf100); |
| 511 | FMode += tensorProduct(u110, gradf110); |
| 512 | FMode += tensorProduct(u010, gradf010); |
| 513 | FMode += tensorProduct(u001, gradf001); |
| 514 | FMode += tensorProduct(u101, gradf101); |
| 515 | FMode += tensorProduct(u111, gradf111); |
| 516 | FMode += tensorProduct(u011, gradf011); |
| 517 | |
| 518 | for(int ii=0; ii<3; ii++) |
| 519 | for(int jj=0; jj<3; jj++) |
| 520 | { |
| 521 | // 9 x r matrix |
| 522 | grad[ELT(9, 3*ii+jj, j)] = FMode[ii][jj]; |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | void CubicMesh::computeAlphaBetaGamma(int el, Vec3d pos, double * alpha, double * beta, double * gamma) const |
| 528 | { |
| 529 | if (parallelepipedMode) |
| 530 | { |
| 531 | // general, parallelepied version (supports rotated cubes, and even cubes deformed via linear transformations; although the class "officially" only supports axis-aligned cubes) |
| 532 | const Vec3d & v0 = getVertex(el, 0); |
| 533 | const Vec3d & v1 = getVertex(el, 1); |
| 534 | const Vec3d & v3 = getVertex(el, 3); |
| 535 | const Vec3d & v4 = getVertex(el, 4); |
| 536 | |
| 537 | Vec3d axis0 = v1 - v0; |
| 538 | Vec3d axis1 = v3 - v0; |
| 539 | Vec3d axis2 = v4 - v0; |
| 540 | Vec3d p = pos - v0; |
| 541 | Vec3d v; |
| 542 | |
| 543 | // OBB code |
| 544 | //double invCubeSize2 = invCubeSize * invCubeSize; |
| 545 | //*alpha = invCubeSize2 * dot(p, axis0); |
| 546 | //*beta = invCubeSize2 * dot(p, axis1); |
| 547 | //*gamma = invCubeSize2 * dot(p, axis2); |
| 548 | |
| 549 | // parallelepiped code |
| 550 | double A[9], invA[9]; |
| 551 | A[0] = axis0[0]; |
| 552 | A[1] = axis1[0]; |
| 553 | A[2] = axis2[0]; |
| 554 | |
| 555 | A[3] = axis0[1]; |
| 556 | A[4] = axis1[1]; |
| 557 | A[5] = axis2[1]; |
| 558 | |
| 559 | A[6] = axis0[2]; |
| 560 | A[7] = axis1[2]; |
| 561 | A[8] = axis2[2]; |
| 562 | |
| 563 | inverse3x3(A, invA); |
| 564 | MATRIX_VECTOR_MULTIPLY3X3(invA, p, v); |
| 565 | |
| 566 | *alpha = v[0]; |
| 567 | *beta = v[1]; |
| 568 | *gamma = v[2]; |
| 569 | } |
| 570 | else |
| 571 | { |
| 572 | // AABB mode |
| 573 | const Vec3d & v0 = getVertex(el, 0); |
| 574 | Vec3d p = pos - v0; |
| 575 | *alpha = p[0] * invCubeSize; |
| 576 | *beta = p[1] * invCubeSize; |
| 577 | *gamma = p[2] * invCubeSize; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | void CubicMesh::computeBarycentricWeights(int el, const Vec3d & pos, double * weights) const |
| 582 | { |
| 583 | // compute barycentric coordinates |
| 584 | double alpha, beta, gamma; |
| 585 | computeAlphaBetaGamma(el, pos, &alpha, &beta, &gamma); |
| 586 | |
| 587 | weights[0] = (1-alpha)*(1-beta)*(1-gamma); // f000 |
| 588 | weights[1] = (alpha)*(1-beta)*(1-gamma); // f100 |
| 589 | weights[2] = (alpha)*(beta)*(1-gamma); // f110 |
| 590 | weights[3] = (1-alpha)*(beta)*(1-gamma); // f010 |
| 591 | |
| 592 | weights[4] = (1-alpha)*(1-beta)*(gamma); // f001 |
| 593 | weights[5] = (alpha)*(1-beta)*(gamma); // f101 |
| 594 | weights[6] = (alpha)*(beta)*(gamma); // f111 |
| 595 | weights[7] = (1-alpha)*(beta)*(gamma); // f011 |
| 596 | } |
| 597 | |
| 598 | double CubicMesh::getElementVolume(int el) const |
| 599 | { |
| 600 | return cubeSize * cubeSize * cubeSize; |
| 601 | } |
| 602 | |
| 603 | void CubicMesh::getElementInertiaTensor(int el, Mat3d & inertiaTensor) const |
| 604 | { |
| 605 | // inertia tensor of a cube with side a, and mass m is spherical, with |
| 606 | // diagonal elements 1.0 / 6 * m * a * a |
| 607 | double cubeSize5 = cubeSize * cubeSize * cubeSize * cubeSize * cubeSize; |
| 608 | |
| 609 | inertiaTensor = cubeSize5 * Mat3d( 1.0/6 , 0 , 0, 0 , 1.0/6 , 0, 0 , 0 , 1.0/6 ); |
| 610 | } |
| 611 | |
| 612 | int CubicMesh::getNumElementEdges() const |
| 613 | { |
| 614 | return 12; |
| 615 | } |
| 616 | |
| 617 | void CubicMesh::getElementEdges(int el, int * edgeBuffer) const |
| 618 | { |
| 619 | int v[8]; |
| 620 | for(int i=0; i<8; i++) |
| 621 | v[i] = getVertexIndex(el,i); |
| 622 | |
| 623 | int edgeMask[12][2] = { |
| 624 | { 0, 1 }, { 1, 2 }, { 2, 3 }, { 3, 0 }, |
| 625 | { 4, 5 }, { 5, 6 }, { 6, 7 }, { 7, 4 }, |
| 626 | { 0, 4 }, { 1, 5 }, { 2, 6 }, { 3, 7 } } ; |
| 627 | |
| 628 | for(int edge=0; edge<12; edge++) |
| 629 | { |
| 630 | edgeBuffer[2*edge+0] = v[edgeMask[edge][0]]; |
| 631 | edgeBuffer[2*edge+1] = v[edgeMask[edge][1]]; |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | void CubicMesh::computeElementMassMatrix(int el, double * massMatrix) const |
| 636 | { |
| 637 | double singleMassMatrix[64] = |
| 638 | { 0.0370370335876942, 0.0185185167938471, 0.00925925839692354, 0.0185185167938471, |
| 639 | 0.0185185167938471, 0.00925925839692354, 0.00462962919846177, 0.00925925839692354, |
| 640 | 0.0185185167938471, 0.0370370335876942, 0.0185185167938471, 0.00925925839692354, |
| 641 | 0.00925925839692354, 0.0185185167938471, 0.00925925839692354, 0.00462962919846177, |
| 642 | 0.00925925839692354, 0.0185185167938471, 0.0370370335876942, 0.0185185167938471, |
| 643 | 0.00462962919846177, 0.00925925839692354, 0.0185185167938471, 0.00925925839692354, |
| 644 | 0.0185185167938471, 0.00925925839692354, 0.0185185167938471, 0.0370370335876942, |
| 645 | 0.00925925839692354, 0.00462962919846177, 0.00925925839692354, 0.0185185167938471, |
| 646 | 0.0185185167938471, 0.00925925839692354, 0.00462962919846177, 0.00925925839692354, |
| 647 | 0.0370370335876942, 0.0185185167938471, 0.00925925839692354, 0.0185185167938471, |
| 648 | 0.00925925839692354, 0.0185185167938471, 0.00925925839692354, 0.00462962919846177, |
| 649 | 0.0185185167938471, 0.0370370335876942, 0.0185185167938471, 0.00925925839692354, |
| 650 | 0.00462962919846177, 0.00925925839692354, 0.0185185167938471, 0.00925925839692354, |
| 651 | 0.00925925839692354, 0.0185185167938471, 0.0370370335876942, 0.0185185167938471, |
| 652 | 0.00925925839692354, 0.00462962919846177, 0.00925925839692354, 0.0185185167938471, |
| 653 | 0.0185185167938471, 0.00925925839692354, 0.0185185167938471, 0.0370370335876942 |
| 654 | }; |
| 655 | |
| 656 | double density = getElementDensity(el); |
| 657 | double voxelSpacing = getCubeSize(); |
| 658 | double voxelSpacing3 = voxelSpacing * voxelSpacing * voxelSpacing; |
| 659 | double factor = density * voxelSpacing3; |
| 660 | |
| 661 | for(int i=0; i<64; i++) |
| 662 | massMatrix[i] = factor * singleMassMatrix[i]; |
| 663 | } |
| 664 | |
| 665 | void CubicMesh::subdivide() |
| 666 | { |
| 667 | int numNewElements = 8 * numElements; |
| 668 | int ** newElements = (int**) malloc (sizeof(int*) * numNewElements); |
| 669 | |
| 670 | int parentMask[8][3] = { |
| 671 | { 0, 0, 0 }, |
| 672 | { 1, 0, 0 }, |
| 673 | { 1, 1, 0 }, |
| 674 | { 0, 1, 0 }, |
| 675 | { 0, 0, 1 }, |
| 676 | { 1, 0, 1 }, |
| 677 | { 1, 1, 1 }, |
| 678 | { 0, 1, 1 } }; |
| 679 | |
| 680 | int mask[8][8][3]; |
| 681 | for(int el=0; el<8; el++) |
| 682 | for(int vtx=0; vtx<8; vtx++) |
| 683 | for(int dim=0; dim<3; dim++) |
| 684 | { |
| 685 | mask[el][vtx][dim] = parentMask[el][dim] + parentMask[vtx][dim]; |
| 686 | //printf("%d\n", mask[el][vtx][dim]); |
| 687 | } |
| 688 | |
| 689 | vector<Vec3d> newVertices; |
| 690 | for(int el=0; el<numElements; el++) |
| 691 | { |
| 692 | const Vec3d & v0 = getVertex(el, 0); |
| 693 | |
| 694 | // create the 8 children cubes |
| 695 | for(int child=0; child<8; child++) |
| 696 | { |
| 697 | newElements[8 * el + child] = (int*) malloc (sizeof(int) * 8); |
| 698 | |
| 699 | int childVtx[8]; |
| 700 | for(int vtx=0; vtx<8; vtx++) |
| 701 | { |
| 702 | Vec3d pos = v0 + 0.5 * cubeSize * Vec3d(mask[child][vtx][0], mask[child][vtx][1], mask[child][vtx][2]); |
| 703 | //printf("%G %G %G\n", pos[0], pos[1], pos[2]); |
| 704 | // search for vertex |
| 705 | int found = -1; |
| 706 | for(int i=0; i<(int)newVertices.size(); i++) |
| 707 | { |
| 708 | if (len(pos - newVertices[i]) < 0.25 * cubeSize) |
| 709 | { |
| 710 | found = i; |
| 711 | break; |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | if (found == -1) |
| 716 | { |
| 717 | // new vertex |
| 718 | newVertices.push_back(pos); |
| 719 | found = (int)newVertices.size() - 1; |
| 720 | } |
| 721 | |
| 722 | childVtx[vtx] = found; |
| 723 | newElements[8 * el + child][vtx] = childVtx[vtx]; |
| 724 | } |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | cubeSize *= 0.5; |
| 729 | |
| 730 | // deallocate old vertices |
| 731 | delete [] vertices; |
| 732 | |
| 733 | // copy new vertices into place |
| 734 | numVertices = (int)newVertices.size(); |
| 735 | vertices = new Vec3d [numVertices]; |
| 736 | memcpy(vertices, newVertices.data(), sizeof(Vec3d) * numVertices); |
| 737 | |
| 738 | // deallocate old elements |
| 739 | for(int i=0; i<numElements; i++) |
| 740 | free(elements[i]); |
| 741 | free(elements); |
| 742 | |
| 743 | // copy new elements into place |
| 744 | numElements = numNewElements; |
| 745 | elements = newElements; |
| 746 | |
| 747 | // update sets (expand each entry in each set into 8 new entries) |
| 748 | for(int setIndex=0; setIndex<numSets; setIndex++) |
| 749 | { |
| 750 | set<int> oldElements; |
| 751 | sets[setIndex]->getElements(oldElements); |
| 752 | Set * newSet = new Set(sets[setIndex]->getName()); |
| 753 | for(set<int> :: iterator iter = oldElements.begin(); iter != oldElements.end(); iter++) |
| 754 | { |
| 755 | for(int i=0; i<8; i++) |
| 756 | newSet->insert(8 * *iter + i); |
| 757 | } |
| 758 | |
| 759 | delete(sets[setIndex]); |
| 760 | sets[setIndex] = newSet; |
| 761 | } |
| 762 | |
| 763 | if(numElements > 0) |
| 764 | { |
| 765 | elementMaterial = (int *) realloc(elementMaterial, sizeof(int) * numElements); |
| 766 | memset(elementMaterial, 0, sizeof(int) * numElements); |
| 767 | } |
| 768 | |
| 769 | propagateRegionsToElements(); |
| 770 | } |
| 771 | |
| 772 | void CubicMesh::setParallelepipedMode(int parallelepipedMode_) |
| 773 | { |
| 774 | parallelepipedMode = parallelepipedMode_; |
| 775 | } |
| 776 |