| 1 | /************************************************************************* |
| 2 | * * |
| 3 | * Vega FEM Simulation Library Version 3.1 * |
| 4 | * * |
| 5 | * "windingNumber" library , Copyright (C) 2016 USC * |
| 6 | * All rights reserved. * |
| 7 | * * |
| 8 | * Code author: Yijing Li * |
| 9 | * http://www.jernejbarbic.com/code * |
| 10 | * * |
| 11 | * Funding: National Science Foundation * |
| 12 | * * |
| 13 | * This library is free software; you can redistribute it and/or * |
| 14 | * modify it under the terms of the BSD-style license that is * |
| 15 | * included with this library in the file LICENSE.txt * |
| 16 | * * |
| 17 | * This library is distributed in the hope that it will be useful, * |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file * |
| 20 | * LICENSE.TXT for more details. * |
| 21 | * * |
| 22 | *************************************************************************/ |
| 23 | |
| 24 | #include "windingNumber.h" |
| 25 | #include "macros.h" |
| 26 | |
| 27 | double WindingNumber::computeWindingNumber(ObjMesh * objMesh, const Vec3d & p) |
| 28 | { |
| 29 | assert(objMesh->isTriangularMesh()); |
| 30 | double w = 0; |
| 31 | // double maxO = -DBL_MAX; |
| 32 | // double minO = DBL_MAX; |
| 33 | |
| 34 | for(size_t i = 0; i < objMesh->getNumGroups(); i++) |
| 35 | { |
| 36 | const ObjMesh::Group* group = objMesh->getGroupHandle(i); |
| 37 | for(size_t j = 0; j < group->getNumFaces(); j++) |
| 38 | { |
| 39 | const ObjMesh::Face* face = group->getFaceHandle(j); |
| 40 | assert(face->getNumVertices() == 3); |
| 41 | Vec3d vtx[3]; |
| 42 | |
| 43 | for(int k = 0; k < 3; k++) |
| 44 | { |
| 45 | const ObjMesh::Vertex* v = face->getVertexHandle(k); |
| 46 | //unsigned int posInd = v->getPositionIndex(); |
| 47 | vtx[k] = objMesh->getPosition(*v); |
| 48 | } |
| 49 | |
| 50 | Vec3d a = vtx[0] - p; |
| 51 | Vec3d b = vtx[1] - p; |
| 52 | Vec3d c = vtx[2] - p; |
| 53 | double la = len(a), lb = len(b), lc = len(c); |
| 54 | Mat3d mat(a,b,c); |
| 55 | double omega = 2*atan2(det(mat), (la * lb * lc + dot(a,b) * lc + dot(b,c) * la + dot(c,a) * lb)); |
| 56 | // if(omega > maxO) maxO = omega; |
| 57 | // if(omega < minO) minO = omega; |
| 58 | w += omega; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | w /= 4 * M_PI; |
| 63 | //PRINT(maxO / (4*M_PI)); |
| 64 | //PRINT(minO / (4*M_PI)); |
| 65 | return w; |
| 66 | } |
| 67 |