TexGen
TextileDeformer.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"
21#include "TextileDeformer.h"
22#include "TexGen.h"
23
24using namespace TexGen;
25
26CTextileDeformer::CTextileDeformer(void)
27{
29}
30
32{
33}
34
35void CTextileDeformer::DeformTextile(CTextile &Textile, bool bDeformDomain)
36{
37 const CDomain* pDomain = Textile.GetDomain();
38 if (!pDomain)
39 return;
40 int i, j;
41 CYarn* pYarn;
42// const CNode *pNode;
43 CNode NewNode;
44 XYZ Pos, Disp, BestDisp;
45 XYZ NodeDisp, Adjust;
46 double dAccuracy, dBestAccuracy;
47 vector<CSlaveNode>::const_iterator itNode;
48 vector<XYZ> Translations;
49 vector<XYZ>::iterator itTranslation;
50// vector<XYZ>::const_iterator itSecPt;
51// vector<XY>::const_iterator itSecPt2D;
52// double dX;
53// XYZ Side;
54 for (i=0; i<Textile.GetNumYarns(); ++i)
55 {
56 pYarn = Textile.GetYarn(i);
57
58 // Deform the textile path and cross-sections
59 CInterpolationAdjusted AdjustedInterp(*pYarn->GetInterpolation());
60 CYarnSectionAdjusted AdjustedYarnSection(*pYarn->GetYarnSection());
61 Translations = pDomain->GetTranslations(*pYarn);
62 const vector<CSlaveNode> &Nodes = pYarn->GetSlaveNodes(CYarn::SURFACE);
63 for (itNode = Nodes.begin(); itNode != Nodes.end(); ++itNode)
64 {
65 // Adjust center-line
66 for (itTranslation = Translations.begin(); itTranslation != Translations.end(); ++itTranslation)
67 {
68 dAccuracy = GetDisplacement(itNode->GetPosition()+*itTranslation, i, NodeDisp);
69 if (itTranslation == Translations.begin() || dAccuracy>dBestAccuracy)
70 {
71 Adjust = m_RepeatDeformation * (*itTranslation) - *itTranslation;
72 dBestAccuracy = dAccuracy;
73 BestDisp = NodeDisp-Adjust;
74 }
75 }
76 NodeDisp = BestDisp;
77 AdjustedInterp.AddAdjustment(itNode->GetIndex(), itNode->GetT(), NodeDisp);
78
79 // Adjust yarn twist
80 vector<pair<double, XY> > SectionAdjust;
81 const vector<XYZ> &SectionPoints = itNode->GetSectionPoints();
82 const int iSectionPoints = SectionPoints.size();
83 for (j=0; j<iSectionPoints; ++j)
84 {
85 double dU = j/double(iSectionPoints);
86 Pos = SectionPoints[j];
87 dAccuracy = 0;
88 for (itTranslation = Translations.begin(); itTranslation != Translations.end(); ++itTranslation)
89 {
90 dAccuracy = GetDisplacement(Pos+*itTranslation, i, Disp);
91 if (itTranslation == Translations.begin() || dAccuracy>dBestAccuracy)
92 {
93 Adjust = m_RepeatDeformation * (*itTranslation) - *itTranslation;
94 dBestAccuracy = dAccuracy;
95 BestDisp = Disp-Adjust;
96 }
97 }
98 Disp = BestDisp;
99 // Subtract the node displacement otherwise we are adjusting twice for node
100 // displacement
101 Disp -= NodeDisp;
102 XY PlaneDisp;
103 PlaneDisp.x = DotProduct(itNode->GetSide(), Disp);
104 PlaneDisp.y = DotProduct(itNode->GetUp(), Disp);
105 SectionAdjust.push_back(make_pair(dU, PlaneDisp));
106 }
107 if (!SectionAdjust.empty())
108 {
109 // Add the first displacement back to the end of the list with dU = 1
110 SectionAdjust.push_back(SectionAdjust.front());
111 SectionAdjust.back().first = 1.0;
112 AdjustedYarnSection.AddAdjustment(itNode->GetIndex(), itNode->GetT(), SectionAdjust);
113 }
114 }
115 pYarn->AssignInterpolation(AdjustedInterp);
116 pYarn->AssignSection(AdjustedYarnSection);
117
118 // Adjust the repeat vectors
119 vector<XYZ> Repeats = pYarn->GetRepeats();
120 vector<XYZ>::iterator itRepeat;
121 for (itRepeat = Repeats.begin(); itRepeat != Repeats.end(); ++itRepeat)
122 {
123 *itRepeat = m_RepeatDeformation * (*itRepeat);
124 }
125 pYarn->SetRepeats(Repeats);
126 }
127 if (bDeformDomain)
128 {
129 CDomain* pDomain = Textile.GetDomain();
130 if (pDomain)
131 {
132 pDomain->Deform(m_RepeatDeformation);
133 }
134 }
135}
136
138{
139 CTextile* pCopy = Textile.Copy();
140 DeformTextile(*pCopy, bDeformDomain);
141 string Name = TEXGEN.AddTextile(*pCopy);
142 delete pCopy;
143 return TEXGEN.GetTextile(Name);
144}
145
146
147
148
149
150
151
152
153
154
155
#define TEXGEN
Helper macro to get the texgen instance.
Definition: TexGen.h:76
Abstract base class representing the domain in which a textile cell may lie.
Definition: Domain.h:34
virtual void Deform(CLinearTransformation Transformation)=0
Deform the domain by given linear transformation.
vector< XYZ > GetTranslations(const CYarn &Yarn) const
Get the translation vectors necessary to fully fill the domain.
Definition: Domain.cpp:83
Bezier interpolation for yarn paths.
void AddAdjustment(int iIndex, double t, XYZ Vector)
At given index and value t the position of the node should be adjusted by given vector.
void InitialiseIdentity(int iSize)
Definition: Matrix.h:155
Represents a point on the centreline of a yarn.
Definition: Node.h:28
virtual double GetDisplacement(XYZ Pos, int iYarn, XYZ &Disp) const =0
CTextile * GetDeformedCopyOfTextile(CTextile &Textile, bool bDeformDomain=true)
virtual void DeformTextile(CTextile &Textile, bool bDeformDomain=true)
CLinearTransformation m_RepeatDeformation
Represents a textile cell containing yarns.
Definition: Textile.h:39
const CDomain * GetDomain() const
Definition: Textile.h:287
int GetNumYarns() const
Definition: Textile.cpp:704
virtual CTextile * Copy() const
Definition: Textile.h:48
const CYarn * GetYarn(int iIndex) const
Definition: Textile.cpp:693
Represents a yarn consisting of master nodes, section and interpolation function.
Definition: Yarn.h:49
const CInterpolation * GetInterpolation() const
Definition: Yarn.h:450
void AssignInterpolation(const CInterpolation &Interpolation)
Assign an interpolation function to the yarn.
Definition: Yarn.cpp:641
void SetRepeats(const vector< XYZ > &Repeats)
Set the repeat vectors.
Definition: Yarn.cpp:1263
void AssignSection(const CYarnSection &YarnSection)
Assign a section to the yarn.
Definition: Yarn.cpp:649
const vector< CSlaveNode > & GetSlaveNodes(BUILD_TYPE Usage) const
Get the slave nodes and build them if necessary.
Definition: Yarn.cpp:1749
@ SURFACE
Definition: Yarn.h:60
const CYarnSection * GetYarnSection() const
Definition: Yarn.h:449
const vector< XYZ > & GetRepeats() const
Definition: Yarn.h:448
Bezier interpolation for yarn paths.
void AddAdjustment(int iIndex, double t, const vector< pair< double, XY > > &SectionAdjust)
At given index and value t the position of the node should be adjusted by given vector.
Namespace containing a series of customised math operations not found in the standard c++ library.
double DotProduct(const XYZ &left, const XYZ &right)
Get the dot product of two vectors.
Definition: mymath.h:512
Struct for representing points in 2D space.
Definition: mymath.h:103
double x
Definition: mymath.h:104
double y
Definition: mymath.h:104
Struct for representing points in 3D space.
Definition: mymath.h:56