TexGen
YarnSection.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 "YarnSectionConstant.h"
24#include "YarnSectionAdjusted.h"
25#include "SectionEllipse.h"
26
27using namespace TexGen;
28
29CYarnSection::CYarnSection(bool bConstMesh)
30: m_bForceEqualMeshLayers(bConstMesh)
31{
32}
33
35{
36}
37
38CYarnSection::CYarnSection(TiXmlElement &Element)
39{
40 const char* result = Element.Attribute("ConstMesh"); // Need to check whether attribute present so can set to true if not
41 if (result != NULL )
42 {
43 m_bForceEqualMeshLayers = valueify<bool>(result);
44 }
45 else
47}
48
50{
51 const string* pType = Element.Attribute(string("type"));
52 if (pType)
53 {
54 if (*pType == "CYarnSectionConstant")
55 return CYarnSectionConstant(Element);
56 else if (*pType == "CYarnSectionInterpNode")
57 return CYarnSectionInterpNode(Element);
58 else if (*pType == "CYarnSectionInterpPosition")
59 return CYarnSectionInterpPosition(Element);
60 else if (*pType == "CYarnSectionAdjusted")
61 return CYarnSectionAdjusted(Element);
62 }
63 // If the yarn section was not recognised for some reason, revert to using a constant
64 // circular yarn section of diameter 1
66}
67
68void CYarnSection::PopulateTiXmlElement(TiXmlElement &Element, OUTPUT_TYPE OutputType) const
69{
70 Element.SetAttribute("type", GetType());
71 Element.SetAttribute("ConstMesh", stringify(m_bForceEqualMeshLayers));
72}
73
75{
76 double dYarnPosition;
77
78 if (iSection < 0 || iSection >= (int)SectionLengths.size())
79 {
80 TGERROR("Unable to get yarn position, yarn section index out of range: " << iSection);
81 return 0;
82 }
83
84 dYarnPosition = accumulate(SectionLengths.begin(), SectionLengths.begin()+iSection, 0.0);
85 dYarnPosition += SectionLengths[iSection] * dSectionPosition;
86 dYarnPosition /= accumulate(SectionLengths.begin(), SectionLengths.end(), 0.0);
87
88 return dYarnPosition;
89}
90
92{
93 return dynamic_cast<CYarnSectionConstant*>(this);
94}
95
97{
98 return dynamic_cast<CYarnSectionInterpNode*>(this);
99}
101{
102 return dynamic_cast<CYarnSectionInterpPosition*>(this);
103}
104
105
106
107
108
109
#define TGERROR(MESSAGE)
Macros used to report the file name and line number to the TexGenError and TexGenLog functions.
Definition: Logger.h:29
#define NULL
Definition: ShinyConfig.h:50
Object container to help handle memory management issues.
Elliptical Section.
Bezier interpolation for yarn paths.
Creates a section which is constant all along the yarn.
CYarnSectionConstant * GetSectionConstant()
Get a pointer of that the inherited class.
Definition: YarnSection.cpp:91
CYarnSection(bool bConstMesh=true)
Definition: YarnSection.cpp:29
CYarnSectionInterpNode * GetSectionInterpNode()
Definition: YarnSection.cpp:96
virtual string GetType() const =0
Derived class should return the class name.
virtual ~CYarnSection(void)
Definition: YarnSection.cpp:34
CYarnSectionInterpPosition * GetSectionInterpPosition()
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
Creates a section which is linearly interpolated between sections defined at the nodes.
Interpolate sections between arbritrary positions along the length of the yarn.
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
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
vector< double > SectionLengths
This contains a list of lengths representing the length of each section.
Definition: YarnSection.h:39