TexGen
TextileDeformerVolumeMesh.cpp
Go to the documentation of this file.
1/*=============================================================================
2TexGen: Geometric textile modeller.
3Copyright (C) 2006 Martin Sherburn
4
5This program is free software; you can redistribute it and/or
6modify it under the terms of the GNU General Public License
7as published by the Free Software Foundation; either version 2
8of the License, or (at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18=============================================================================*/
19
20#include "PrecompiledHeaders.h"
22#include "TexGen.h"
23
24using namespace TexGen;
25
26CTextileDeformerVolumeMesh::CTextileDeformerVolumeMesh(void)
27{
28}
29
31{
32}
33
34bool CTextileDeformerVolumeMesh::SetYarnMeshDisplacements(int iYarn, const CMesh &Mesh, vector<XYZ> &Displacements)
35{
36 if (Mesh.GetNodes().size() != Displacements.size())
37 {
38 TGERROR("Cannot set yarn mesh displacements"
39 ",the number of nodal displacements (" << Displacements.size() <<
40 ") does not equal the number of nodes (" << Mesh.GetNodes().size() << ")");
41 return false;
42 }
43 if (iYarn < 0)
44 {
45 TGERROR("Cannot set yarn mesh displacements, yarn index given is invalid: " << iYarn);
46 return false;
47 }
48 m_YarnMeshes.resize(iYarn+1);
49 m_YarnMeshes[iYarn].Mesh = Mesh;
50 m_YarnMeshes[iYarn].NodeDisplacements = Displacements;
51 // The GetDisplacement function only works with a Tet mesh so let's
52 // convert to a tet mesh now
53 m_YarnMeshes[iYarn].Mesh.ConvertToTetMesh();
54 return true;
55}
56
57double CTextileDeformerVolumeMesh::GetDisplacement(XYZ Pos, int iYarn, XYZ &Disp) const
58{
59 if (iYarn < 0 || iYarn >= (int)m_YarnMeshes.size())
60 return false;
61 const CMesh &Mesh = m_YarnMeshes[iYarn].Mesh;
62 const vector<XYZ> &NodeDisplacements = m_YarnMeshes[iYarn].NodeDisplacements;
63 list<int>::const_iterator itIndex;
64 const list<int> &Indices = Mesh.GetIndices(CMesh::TET);
65 int i1, i2, i3, i4;
66 XYZ N1, N2, N3, N4;
67 double a, b, c, d, dMin;
68 double dAccuracy = -1;
69 bool bFirst = true;
70 for (itIndex = Indices.begin(); itIndex != Indices.end(); )
71 {
72 i1 = *(itIndex++);
73 i2 = *(itIndex++);
74 i3 = *(itIndex++);
75 i4 = *(itIndex++);
76 N1 = Mesh.GetNode(i1);
77 N2 = Mesh.GetNode(i2);
78 N3 = Mesh.GetNode(i3);
79 N4 = Mesh.GetNode(i4);
80 GetBarycentricCoordinates(Pos, N1, N2, N3, N4, a, b, c, d);
81 dMin = min(a, b);
82 dMin = min(dMin, c);
83 dMin = min(dMin, d);
84 if (bFirst || dMin > dAccuracy)
85 {
86 // If all barycentric coordinates are non-negative the point lies inside the Tet
87 dAccuracy = dMin;
88 Disp = a*NodeDisplacements[i1];
89 Disp += b*NodeDisplacements[i2];
90 Disp += c*NodeDisplacements[i3];
91 Disp += d*NodeDisplacements[i4];
92 bFirst = false;
93 }
94 }
95 return dAccuracy;
96}
97
98void CTextileDeformerVolumeMesh::GetBarycentricCoordinates(const XYZ &P, const XYZ &P1, const XYZ &P2, const XYZ &P3, const XYZ &P4, double &a, double &b, double &c, double &d) const
99{
100 // Code from Maple generated from:
101 /*
102 eqn1 := x = a*x1 + b*x2 + c*x3 + d*x4;
103 eqn2 := y = a*y1 + b*y2 + c*y3 + d*y4;
104 eqn3 := z = a*z1 + b*z2 + c*z3 + d*z4;
105 eqn4 := a + b + c + d = 1;
106 sol := solve({eqn1, eqn2, eqn3, eqn4}, [a,b,c,d]);
107 with(CodeGeneration):
108 C(sol[1]);
109 */
110 a = -(-P3.x * P.z * P2.y - P3.x * P4.z * P.y + P3.x * P2.z * P.y + P3.x * P4.z * P2.y + P4.y * P3.x * P.z - P4.y * P3.x * P2.z - P4.x * P3.y * P.z + P4.x * P3.y * P2.z - P3.z * P4.y * P.x - P3.z * P4.x * P2.y + P3.y * P4.z * P.x + P3.z * P4.x * P.y - P3.z * P2.x * P.y + P3.z * P4.y * P2.x + P4.z * P2.x * P.y - P4.x * P2.z * P.y - P2.z * P3.y * P.x + P2.z * P4.y * P.x - P4.z * P.x * P2.y - P3.y * P4.z * P2.x + P3.z * P.x * P2.y + P.z * P4.x * P2.y + P.z * P3.y * P2.x - P.z * P4.y * P2.x) / (P4.z * P1.x * P2.y + P1.x * P3.y * P2.z - P1.x * P3.y * P4.z - P1.x * P4.y * P2.z - P4.x * P3.y * P2.z + P3.x * P1.z * P2.y - P3.x * P1.z * P4.y - P3.x * P4.z * P2.y - P4.x * P1.z * P2.y - P4.z * P1.y * P2.x + P1.y * P3.x * P4.z + P1.y * P4.x * P2.z - P1.y * P3.x * P2.z + P4.y * P3.x * P2.z - P3.y * P1.z * P2.x + P3.y * P1.z * P4.x + P3.y * P4.z * P2.x + P4.y * P1.z * P2.x + P3.z * P1.y * P2.x - P3.z * P1.y * P4.x + P3.z * P1.x * P4.y - P3.z * P1.x * P2.y - P3.z * P4.y * P2.x + P3.z * P4.x * P2.y);
111 b = -0.1e1 / (P4.z * P1.x * P2.y + P1.x * P3.y * P2.z - P1.x * P3.y * P4.z - P1.x * P4.y * P2.z - P4.x * P3.y * P2.z + P3.x * P1.z * P2.y - P3.x * P1.z * P4.y - P3.x * P4.z * P2.y - P4.x * P1.z * P2.y - P4.z * P1.y * P2.x + P1.y * P3.x * P4.z + P1.y * P4.x * P2.z - P1.y * P3.x * P2.z + P4.y * P3.x * P2.z - P3.y * P1.z * P2.x + P3.y * P1.z * P4.x + P3.y * P4.z * P2.x + P4.y * P1.z * P2.x + P3.z * P1.y * P2.x - P3.z * P1.y * P4.x + P3.z * P1.x * P4.y - P3.z * P1.x * P2.y - P3.z * P4.y * P2.x + P3.z * P4.x * P2.y) * (-P3.z * P1.x * P4.y - P3.z * P4.x * P.y + P4.z * P1.y * P.x + P1.x * P3.y * P4.z + P4.x * P3.y * P.z - P1.y * P3.x * P4.z + P3.x * P4.z * P.y + P3.z * P1.x * P.y + P1.x * P4.y * P.z - P4.z * P1.x * P.y - P1.x * P3.y * P.z - P3.x * P1.z * P.y + P4.x * P1.z * P.y + P1.y * P3.x * P.z + P3.x * P1.z * P4.y - P1.y * P4.x * P.z - P3.z * P1.y * P.x - P4.y * P3.x * P.z + P3.y * P1.z * P.x - P3.y * P1.z * P4.x - P3.y * P4.z * P.x - P4.y * P1.z * P.x + P3.z * P4.y * P.x + P3.z * P1.y * P4.x);
112 c = (-P4.z * P1.x * P.y + P1.x * P4.y * P.z + P4.x * P1.z * P.y + P4.z * P1.y * P.x - P1.y * P4.x * P.z - P4.y * P1.z * P.x - P1.x * P4.y * P2.z + P4.z * P1.x * P2.y - P4.x * P1.z * P2.y - P.z * P4.y * P2.x + P4.z * P2.x * P.y - P4.x * P2.z * P.y - P4.z * P.x * P2.y + P.z * P4.x * P2.y + P2.z * P4.y * P.x - P2.x * P1.z * P.y + P4.y * P1.z * P2.x + P2.z * P1.x * P.y + P.x * P1.z * P2.y - P.z * P1.x * P2.y - P2.z * P1.y * P.x + P1.y * P4.x * P2.z + P.z * P1.y * P2.x - P4.z * P1.y * P2.x) / (P4.z * P1.x * P2.y + P1.x * P3.y * P2.z - P1.x * P3.y * P4.z - P1.x * P4.y * P2.z - P4.x * P3.y * P2.z + P3.x * P1.z * P2.y - P3.x * P1.z * P4.y - P3.x * P4.z * P2.y - P4.x * P1.z * P2.y - P4.z * P1.y * P2.x + P1.y * P3.x * P4.z + P1.y * P4.x * P2.z - P1.y * P3.x * P2.z + P4.y * P3.x * P2.z - P3.y * P1.z * P2.x + P3.y * P1.z * P4.x + P3.y * P4.z * P2.x + P4.y * P1.z * P2.x + P3.z * P1.y * P2.x - P3.z * P1.y * P4.x + P3.z * P1.x * P4.y - P3.z * P1.x * P2.y - P3.z * P4.y * P2.x + P3.z * P4.x * P2.y);
113 d = -(-P3.z * P1.x * P.y - P3.z * P.x * P2.y + P1.x * P3.y * P.z + P3.x * P1.z * P.y - P1.y * P3.x * P.z + P2.z * P3.y * P.x + P3.z * P1.y * P.x - P.z * P3.y * P2.x - P3.y * P1.z * P.x - P1.x * P3.y * P2.z - P3.x * P1.z * P2.y + P1.y * P3.x * P2.z + P3.y * P1.z * P2.x - P3.z * P1.y * P2.x + P3.z * P1.x * P2.y - P3.x * P2.z * P.y + P3.x * P.z * P2.y + P3.z * P2.x * P.y - P2.x * P1.z * P.y + P2.z * P1.x * P.y + P.x * P1.z * P2.y - P2.z * P1.y * P.x + P.z * P1.y * P2.x - P.z * P1.x * P2.y) / (P4.z * P1.x * P2.y + P1.x * P3.y * P2.z - P1.x * P3.y * P4.z - P1.x * P4.y * P2.z - P4.x * P3.y * P2.z + P3.x * P1.z * P2.y - P3.x * P1.z * P4.y - P3.x * P4.z * P2.y - P4.x * P1.z * P2.y - P4.z * P1.y * P2.x + P1.y * P3.x * P4.z + P1.y * P4.x * P2.z - P1.y * P3.x * P2.z + P4.y * P3.x * P2.z - P3.y * P1.z * P2.x + P3.y * P1.z * P4.x + P3.y * P4.z * P2.x + P4.y * P1.z * P2.x + P3.z * P1.y * P2.x - P3.z * P1.y * P4.x + P3.z * P1.x * P4.y - P3.z * P1.x * P2.y - P3.z * P4.y * P2.x + P3.z * P4.x * P2.y);
114}
115
116
117
118
119
120
121
122
123
124
125
#define TGERROR(MESSAGE)
Macros used to report the file name and line number to the TexGenError and TexGenLog functions.
Definition: Logger.h:29
Defines the nodes and elements of a surface or volume mesh.
Definition: Mesh.h:58
const vector< XYZ > & GetNodes() const
Get a const reference to the nodes.
Definition: Mesh.cpp:2656
const XYZ & GetNode(int iIndex) const
Get the node with given ID.
Definition: Mesh.cpp:2636
const list< int > & GetIndices(ELEMENT_TYPE ElemType) const
Get the element indices of a given element type.
Definition: Mesh.cpp:2671
bool SetYarnMeshDisplacements(int iYarn, const CMesh &Mesh, vector< XYZ > &Displacements)
void GetBarycentricCoordinates(const XYZ &P, const XYZ &P1, const XYZ &P2, const XYZ &P3, const XYZ &P4, double &a, double &b, double &c, double &d) const
virtual double GetDisplacement(XYZ Pos, int iYarn, XYZ &Disp) const
Namespace containing a series of customised math operations not found in the standard c++ library.
Struct for representing points in 3D space.
Definition: mymath.h:56
double z
Definition: mymath.h:57
double x
Definition: mymath.h:57
double y
Definition: mymath.h:57