|
| 1 | +/*This file is part of the FEBio Studio source code and is licensed under the MIT license |
| 2 | +listed below. |
| 3 | +
|
| 4 | +See Copyright-FEBio-Studio.txt for details. |
| 5 | +
|
| 6 | +Copyright (c) 2021 University of Utah, The Trustees of Columbia University in |
| 7 | +the City of New York, and others. |
| 8 | +
|
| 9 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | +of this software and associated documentation files (the "Software"), to deal |
| 11 | +in the Software without restriction, including without limitation the rights |
| 12 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +copies of the Software, and to permit persons to whom the Software is |
| 14 | +furnished to do so, subject to the following conditions: |
| 15 | +
|
| 16 | +The above copyright notice and this permission notice shall be included in all |
| 17 | +copies or substantial portions of the Software. |
| 18 | +
|
| 19 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | +SOFTWARE.*/ |
| 26 | + |
| 27 | +#include "stdafx.h" |
| 28 | +#include "FEInsertTriangle.h" |
| 29 | +#include <MeshLib/FSSurfaceMesh.h> |
| 30 | +#include <map> |
| 31 | + |
| 32 | +// This routine fills triangles where there are reentrant corners along a boundary |
| 33 | +// curve consisting of a chain of edges. The user-specified "angle" determines the |
| 34 | +// threshold of the deviation of the angle at the reentrant apex from 180 degrees |
| 35 | +// (an angle of zero means that a nearly straight pair of consecutive edges could be |
| 36 | +// filled with a triangle). |
| 37 | + |
| 38 | +FEInsertTriangle::FEInsertTriangle() : FESurfaceModifier("Insert Triangle") |
| 39 | +{ |
| 40 | + m_n = vec3d(0,0,1); |
| 41 | + AddVecParam(m_n,"normal", "approximate triangle normal"); |
| 42 | +} |
| 43 | + |
| 44 | +FSSurfaceMesh* FEInsertTriangle::Apply(FSSurfaceMesh* pm) |
| 45 | +{ |
| 46 | + // create a copy of this mesh |
| 47 | + FSSurfaceMesh* mesh = new FSSurfaceMesh(*pm); |
| 48 | + |
| 49 | + // get approximate surface normal |
| 50 | + m_n = GetVecValue(0); |
| 51 | + |
| 52 | + // get selected nodes |
| 53 | + std::vector<FSNode> node; |
| 54 | + node.reserve(mesh->Nodes()); |
| 55 | + std::vector<int> ID; |
| 56 | + ID.reserve(mesh->Nodes()); |
| 57 | + |
| 58 | + for (int i=0; i<mesh->Nodes(); ++i) { |
| 59 | + FSNode& ni = mesh->Node(i); |
| 60 | + if (ni.IsSelected()) { |
| 61 | + node.push_back(ni); |
| 62 | + ID.push_back(i); |
| 63 | + } |
| 64 | + } |
| 65 | + if (node.size() != 3) { |
| 66 | + error("Three nodes should be selected."); |
| 67 | + return nullptr; |
| 68 | + } |
| 69 | + |
| 70 | + // create triangle from three selected nodes |
| 71 | + // allocate room for the new face |
| 72 | + int NF = mesh->Faces(); |
| 73 | + mesh->Create(0, 0, NF + 1); |
| 74 | + FSFace& face = mesh->Face(NF); |
| 75 | + vec3d fn = (node[1].r - node[0].r) ^ (node[2].r - node[0].r); |
| 76 | + face.SetType(FE_FACE_TRI3); |
| 77 | + if (fn*m_n > 0) { |
| 78 | + face.n[0] = ID[0]; |
| 79 | + face.n[1] = ID[1]; |
| 80 | + face.n[2] = ID[2]; |
| 81 | + } else { |
| 82 | + face.n[0] = ID[0]; |
| 83 | + face.n[1] = ID[2]; |
| 84 | + face.n[2] = ID[1]; |
| 85 | + } |
| 86 | + face.m_gid = 0; |
| 87 | + |
| 88 | + // rebuild the mesh |
| 89 | + mesh->RebuildMesh(); |
| 90 | + |
| 91 | + // unselect all the nodes |
| 92 | + for (int i=0; i< mesh->Nodes(); ++i) { |
| 93 | + FSNode& ni = mesh->Node(i); |
| 94 | + ni.Unselect(); |
| 95 | + } |
| 96 | + |
| 97 | + // determine which nodes should remain selected |
| 98 | + NF = mesh->Faces(); |
| 99 | + face = mesh->Face(NF-1); |
| 100 | + for (int i=0; i< face.Edges(); ++i) { |
| 101 | + FSEdge edge = face.GetEdge(i); |
| 102 | + // check how many faces in the mesh have this edge |
| 103 | + int count = 0; |
| 104 | + for (int j=0; j< mesh->Faces(); ++j) { |
| 105 | + FSFace& oface = mesh->Face(j); |
| 106 | + if (oface.HasEdge(edge.n[0], edge.n[1])) ++count; |
| 107 | + } |
| 108 | + if (count == 1) { |
| 109 | + FSNode& n0 = mesh->Node(edge.n[0]); |
| 110 | + n0.Select(); |
| 111 | + FSNode& n1 = mesh->Node(edge.n[1]); |
| 112 | + n1.Select(); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + return mesh; |
| 117 | +} |
0 commit comments