TexGen
YarnSectionInterpPosition.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 "Section.h"
23using namespace TexGen;
24
25CYarnSectionInterpPosition::CYarnSectionInterpPosition(bool bRamped, bool bPolar, bool bConstMesh)
26: CYarnSectionInterp(bRamped, bPolar, bConstMesh)
27{
28}
29
31{
32}
33
35: CYarnSectionInterp(Element)
36{
37 FOR_EACH_TIXMLELEMENT(pPositionSection, Element, "PositionSection")
38 {
39 TiXmlElement* pSection = pPositionSection->FirstChildElement("Section");
40 if (pSection)
41 {
42 m_Sections.push_back(make_pair(
43 valueify<double>(pPositionSection->Attribute("t")),
45 ));
46 }
47 }
48}
49
50void CYarnSectionInterpPosition::PopulateTiXmlElement(TiXmlElement &Element, OUTPUT_TYPE OutputType) const
51{
53
54 int i;
55 for (i=0; i<(int)m_Sections.size(); ++i)
56 {
57 TiXmlElement PositionSection("PositionSection");
58 PositionSection.SetAttribute("t", stringify(m_Sections[i].first));
59 TiXmlElement Section("Section");
60 m_Sections[i].second->PopulateTiXmlElement(Section, OutputType);
61 PositionSection.InsertEndChild(Section);
62 Element.InsertEndChild(PositionSection);
63 }
64}
65
66vector<XY> CYarnSectionInterpPosition::GetSection(const YARN_POSITION_INFORMATION PositionInfo, int iNumPoints, bool bEquiSpaced) const
67{
68 int iSection1 = -1;
69 int iSection2 = -1;
70 double u;
71 double dYarnPosition = PositionInfo.GetYarnPosition();
72
73 int i;
74 for (i = 0; i<(int)m_Sections.size(); ++i)
75 {
76 if (m_Sections[i].first <= dYarnPosition)
77 iSection1 = i;
78 if (m_Sections[i].first >= dYarnPosition && iSection2 == -1)
79 iSection2 = i;
80 }
81
82 if (iSection1 == -1 && iSection2 == -1)
83 {
84 TGERROR("Unable to get section, no sections specified");
85 assert(false);
86 return vector<XY>();
87 }
88
89 double du1, du2;
90 if (iSection1 != -1 && iSection2 != -1)
91 {
92 du1 = m_Sections[iSection1].first;
93 du2 = m_Sections[iSection2].first;
94 }
95 else if (iSection1 == -1)
96 {
97 iSection1 = m_Sections.size()-1;
98 du1 = m_Sections[iSection1].first-1;
99 du2 = m_Sections[iSection2].first;
100 }
101 else
102 {
103 iSection2 = 0;
104 du1 = m_Sections[iSection1].first;
105 du2 = m_Sections[iSection2].first+1;
106 }
107 if (du1 == du2)
108 u = 0;
109 else
110 u = (dYarnPosition-du1)/(du2 - du1);
111
112 vector<XY> Points;
113 GetInterpedSection(*m_Sections[iSection1].second, *m_Sections[iSection2].second, u, iNumPoints, bEquiSpaced, Points);
114
115 return Points;
116}
117
118CMesh CYarnSectionInterpPosition::GetSectionMesh(const YARN_POSITION_INFORMATION PositionInfo, int iNumPoints, bool bEquiSpaced) const
119{
120 int iSection1 = -1;
121 int iSection2 = -1;
122 double u;
123 double dYarnPosition = PositionInfo.GetYarnPosition();
124
125 int i;
126 for (i = 0; i<(int)m_Sections.size(); ++i)
127 {
128 if (m_Sections[i].first <= dYarnPosition)
129 iSection1 = i;
130 if (m_Sections[i].first >= dYarnPosition && iSection2 == -1)
131 iSection2 = i;
132 }
133
134 if (iSection1 == -1 && iSection2 == -1)
135 {
136 TGERROR("Unable to get section mesh, no sections specified");
137 assert(false);
138 return CMesh();
139 }
140
141 double du1, du2;
142 if (iSection1 != -1 && iSection2 != -1)
143 {
144 du1 = m_Sections[iSection1].first;
145 du2 = m_Sections[iSection2].first;
146 }
147 else if (iSection1 == -1)
148 {
149 iSection1 = m_Sections.size()-1;
150 du1 = m_Sections[iSection1].first-1;
151 du2 = m_Sections[iSection2].first;
152 }
153 else
154 {
155 iSection2 = 0;
156 du1 = m_Sections[iSection1].first;
157 du2 = m_Sections[iSection2].first+1;
158 }
159
160 CMesh Mesh;
161 if ( (iSection1 != -1 && iSection2 != -1) && (du1 == du2) ) // same section
162 {
163 Mesh = m_Sections[iSection1].second->GetMesh(iNumPoints, bEquiSpaced);
164 }
165 else
166 {
167 u = (dYarnPosition-du1)/(du2 - du1);
168 GetInterpedSectionMesh(*m_Sections[iSection1].second, *m_Sections[iSection2].second, u, iNumPoints, bEquiSpaced, Mesh);
169 }
170
171 return Mesh;
172}
173
174void CYarnSectionInterpPosition::AddSection(double dPosition, const CSection &Section)
175{
176 m_Sections.push_back(pair<double, CObjectContainer<CSection> >(dPosition, Section));
178}
179
181{
182 assert(iIndex >= 0 && iIndex < (int)m_Sections.size());
183 return *m_Sections[iIndex].second;
184}
185
187{
188 assert(iIndex >= 0 && iIndex < (int)m_Sections.size());
189 return m_Sections[iIndex].first;
190}
191
193{
194 int iMaxLayers = -1;
195 for ( int i = 0; i < (int)m_Sections.size(); ++i )
196 {
197 int iNumLayers = CalculateNumberofLayers(m_Sections[i].second->GetPoints( iNumPoints, true ));
198
199 if ( iNumLayers > iMaxLayers )
200 iMaxLayers = iNumLayers;
201 }
202 if ( iMaxLayers != -1 )
203 {
204 for ( int i = 0; i < (int)m_Sections.size(); ++i )
205 {
206 const CSection* pSection = m_Sections[i].second; // Convoluted but can't find another way to get around this at the moment
207 CSection* pSection2 = const_cast<CSection*> (pSection);
208 pSection2->SetSectionMeshLayers(iMaxLayers);
209 }
210 }
211}
212
213
214
215
216
217
#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
Object container to help handle memory management issues.
Abstract base class respresenting a yarn cross-section.
Definition: Section.h:31
static CObjectContainer< CSection > CreateSection(TiXmlElement &Element)
Create a section from TiXmlElement.
Definition: Section.cpp:78
void SetSectionMeshLayers(int iNum)
Specify number of layers for associated section mesh.
Definition: Section.cpp:232
Abstract base class to handle interpolation for derived classes.
virtual void PopulateTiXmlElement(TiXmlElement &Element, OUTPUT_TYPE OutputType) const
Used for saving data to XML.
int CalculateNumberofLayers(const vector< XY > &Section) const
bool GetInterpedSection(const CSection &Section1, const CSection &Section2, double u, int iNumPoints, bool bEquiSpaced, vector< XY > &Points) const
bool GetInterpedSectionMesh(const CSection &Section1, const CSection &Section2, double u, int iNumPoints, bool bEquiSpaced, CMesh &Mesh) const
CYarnSectionInterpPosition(bool bRamped=true, bool bPolar=false, bool bConstMesh=true)
vector< pair< double, CObjectContainer< CSection > > > m_Sections
void SetSectionMeshLayersEqual(int iNumPoints) const
Function to set the number of layers equal for all sections in a given yarn.
void PopulateTiXmlElement(TiXmlElement &Element, OUTPUT_TYPE OutputType) const
Used for saving data to XML.
vector< XY > GetSection(const YARN_POSITION_INFORMATION PositionInfo, int iNumPoints, bool bEquiSpaced=false) const
This function must be implemented by derived classes.
void AddSection(double dPosition, const CSection &Section)
Add a section at a specific point along the path of the yarn.
CMesh GetSectionMesh(const YARN_POSITION_INFORMATION PositionInfo, int iNumPoints, bool bEquiSpaced) const
This function must be implemented by derived classes.
Namespace containing a series of customised math operations not found in the standard c++ library.
OUTPUT_TYPE
Definition: Misc.h:105
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
Used to sort double-objectref pairs.
Structure used to represent the position along the length of a yarn.
Definition: YarnSection.h:36