TexGen
YarnSectionAdjusted.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 "YarnSectionAdjusted.h"
22
23using namespace TexGen;
24
25CYarnSectionAdjusted::CYarnSectionAdjusted(const CYarnSection &YarnSection)
26: m_pYarnSection(YarnSection)
27{
28}
29
31{
32}
33
35: CYarnSection(Element)
36{
37 FOR_EACH_TIXMLELEMENT(pAdjustment, Element, "Adjustment")
38 {
39 vector<pair<double, SECTION_ADJUST> > Adjustments;
40 FOR_EACH_TIXMLELEMENT(pAdjustmentSection, *pAdjustment, "AdjustmentSection")
41 {
42 pair<double, SECTION_ADJUST> Adjustment(0.0, SECTION_ADJUST());
43 pAdjustmentSection->Attribute("T", &Adjustment.first);
44 FOR_EACH_TIXMLELEMENT(pAdjustmentPerimeter, *pAdjustmentSection, "AdjustmentPerimeter")
45 {
46 pair<double, XY> AdjustmentPerimeter(0.0, XY());
47 pAdjustmentPerimeter->Attribute("T", &AdjustmentPerimeter.first);
48 AdjustmentPerimeter.second = valueify<XY>(pAdjustmentPerimeter->Attribute("Vector"));
49 Adjustment.second.push_back(AdjustmentPerimeter);
50 }
51 Adjustments.push_back(Adjustment);
52 }
53 m_Adjustments.push_back(Adjustments);
54 }
55 TiXmlElement* pYarnSection = Element.FirstChildElement("YarnSection");
56 if (pYarnSection)
57 m_pYarnSection = CreateYarnSection(*pYarnSection);
58}
59
60void CYarnSectionAdjusted::PopulateTiXmlElement(TiXmlElement &Element, OUTPUT_TYPE OutputType) const
61{
62 CYarnSection::PopulateTiXmlElement(Element, OutputType);
63 int i, j, k;
64 for (i=0; i<(int)m_Adjustments.size(); ++i)
65 {
66 TiXmlElement Adjustment("Adjustment");
67 Adjustment.SetAttribute("index", i);
68 for (j=0; j<(int)m_Adjustments[i].size(); ++j)
69 {
70 TiXmlElement AdjustmentSection("AdjustmentSection");
71 AdjustmentSection.SetAttribute("T", stringify(m_Adjustments[i][j].first));
72 for (k=0; k<(int)m_Adjustments[i][j].second.size(); ++k)
73 {
74 TiXmlElement AdjustmentPerimeter("AdjustmentPerimeter");
75 AdjustmentPerimeter.SetAttribute("T", stringify(m_Adjustments[i][j].second[k].first));
76 AdjustmentPerimeter.SetAttribute("Vector", stringify(m_Adjustments[i][j].second[k].second));
77 AdjustmentSection.InsertEndChild(AdjustmentPerimeter);
78 }
79 Adjustment.InsertEndChild(AdjustmentSection);
80 }
81 Element.InsertEndChild(Adjustment);
82 }
83 TiXmlElement YarnSection("YarnSection");
84 m_pYarnSection->PopulateTiXmlElement(YarnSection, OutputType);
85 Element.InsertEndChild(YarnSection);
86}
87
88vector<XY> CYarnSectionAdjusted::GetSection(const YARN_POSITION_INFORMATION PositionInfo, int iNumPoints, bool bEquiSpaced) const
89{
90 vector<XY> SectionPoints = m_pYarnSection->GetSection(PositionInfo, iNumPoints, bEquiSpaced);
91
92 if (PositionInfo.iSection < 0 || PositionInfo.iSection >= (int)m_Adjustments.size())
93 {
94 TGERROR("Unable to adjust section. Index out of range: " << PositionInfo.iSection);
95 return SectionPoints;
96 }
97
98 pair<double, pair<SECTION_ADJUST, SECTION_ADJUST> > ClosestVals =
99 GetClosestValues(m_Adjustments[PositionInfo.iSection], PositionInfo.dSectionPosition);
100
101 XY Disp, Disp1, Disp2;
102 vector<XY>::iterator itSecPt;
103 assert((int)SectionPoints.size() == iNumPoints);
104 int i;
105 for (itSecPt = SectionPoints.begin(), i=0; itSecPt != SectionPoints.end(); ++itSecPt, ++i)
106 {
107 double dU = i/double(iNumPoints);
108 Disp1 = GetInterpedValue(ClosestVals.second.first, dU);
109 Disp2 = GetInterpedValue(ClosestVals.second.second, dU);
110 Disp = Disp1 + ClosestVals.first * (Disp2-Disp1);
111 *itSecPt += Disp;
112 }
113
114 return SectionPoints;
115}
116
117CMesh CYarnSectionAdjusted::GetSectionMesh(const YARN_POSITION_INFORMATION PositionInfo, int iNumPoints, bool bEquiSpaced) const
118{
119 return m_pYarnSection->GetSectionMesh(PositionInfo, iNumPoints, bEquiSpaced);
120}
121
122void CYarnSectionAdjusted::AddAdjustment(int iIndex, double t, const vector<pair<double, XY> > &SectionAdjust)
123{
124 if (iIndex < 0)
125 {
126 TGERROR("Unable to add adjustment. Index out of range: " << iIndex);
127 return;
128 }
129 if (iIndex >= (int)m_Adjustments.size())
130 {
131 m_Adjustments.resize(iIndex+1);
132 }
133 m_Adjustments[iIndex].push_back(make_pair(t, SectionAdjust));
134 // Sort the vector to make sure t values are ordered from lowest to highest
135 sort(m_Adjustments[iIndex].begin(), m_Adjustments[iIndex].end());
136}
137
138
139
140
#define TGERROR(MESSAGE)
Macros used to report the file name and line number to the TexGenError and TexGenLog functions.
Definition: Logger.h:29
#define FOR_EACH_TIXMLELEMENT(CHILDELEMENT, PARENTELEMENT, ELEMENTNAME)
Macro to enable looping over tinyxml easier.
Definition: Misc.h:45
Defines the nodes and elements of a surface or volume mesh.
Definition: Mesh.h:58
YARN_ADJUST m_Adjustments
Represents the adjustements to the interpolation.
vector< pair< double, XY > > SECTION_ADJUST
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.
CMesh GetSectionMesh(const YARN_POSITION_INFORMATION PositionInfo, int iNumPoints, bool bEquiSpaced) const
This function must be implemented by derived classes.
CYarnSectionAdjusted(const CYarnSection &YarnSection)
vector< XY > GetSection(const YARN_POSITION_INFORMATION PositionInfo, int iNumPoints, bool bEquiSpaced=false) const
This function must be implemented by derived classes.
CObjectContainer< CYarnSection > m_pYarnSection
void PopulateTiXmlElement(TiXmlElement &Element, OUTPUT_TYPE OutputType) const
Used for saving data to XML.
Abstract base class used to define the sections along the length of a yarn.
Definition: YarnSection.h:58
virtual void PopulateTiXmlElement(TiXmlElement &Element, OUTPUT_TYPE OutputType) const
Used for saving data to XML.
Definition: YarnSection.cpp:68
static CObjectContainer< CYarnSection > CreateYarnSection(TiXmlElement &Element)
Create a yarn section from TiXmlElement.
Definition: YarnSection.cpp:49
Namespace containing a series of customised math operations not found in the standard c++ library.
std::pair< double, std::pair< T, T > > GetClosestValues(const std::vector< std::pair< double, T > > &InterpValues, double fraction)
Get the two closest values to fraction in the vector InterpValues.
Definition: Misc.h:179
OUTPUT_TYPE
Definition: Misc.h:105
T GetInterpedValue(const std::vector< std::pair< double, T > > &InterpValues, double fraction)
Get an interpolated value.
Definition: Misc.h:151
std::string stringify(const T &x, int iPrecision=12, bool bScientific=true)
Function to convert a value (e.g. int, double, etc...) to a string.
Definition: Misc.h:50
Struct for representing points in 2D space.
Definition: mymath.h:103
Structure used to represent the position along the length of a yarn.
Definition: YarnSection.h:36
double dSectionPosition
This variables varies linearly with distance from 0 to 1 from the start to the end of the current lin...
Definition: YarnSection.h:37
int iSection
This variable represents the index of the current section (where a section is defined as the part bet...
Definition: YarnSection.h:38