TexGen
InterpolationAdjusted.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
23using namespace TexGen;
24CInterpolationAdjusted::CInterpolationAdjusted(const CInterpolation &Interpolation)
25: CInterpolation(true, false, false) // this is not important
26, m_pInterpolation(Interpolation)
27{
28}
29
31{
32}
33
35: CInterpolation(Element)
36{
37 FOR_EACH_TIXMLELEMENT(pAdjustment, Element, "Adjustment")
38 {
39 vector<pair<double, XYZ> > Adjustments;
40 FOR_EACH_TIXMLELEMENT(pAdjustmentSection, *pAdjustment, "AdjustmentSection")
41 {
42 pair<double, XYZ> Adjustment(0.0, XYZ());
43 pAdjustmentSection->Attribute("T", &Adjustment.first);
44 Adjustment.second = valueify<XYZ>(pAdjustmentSection->Attribute("Vector"));
45 Adjustments.push_back(Adjustment);
46 }
47 m_Adjustments.push_back(Adjustments);
48 }
49 TiXmlElement* pInterpolation = Element.FirstChildElement("Interpolation");
50 if (pInterpolation)
51 m_pInterpolation = CreateInterpolation(*pInterpolation);
52}
53
54void CInterpolationAdjusted::PopulateTiXmlElement(TiXmlElement &Element, OUTPUT_TYPE OutputType) const
55{
56 CInterpolation::PopulateTiXmlElement(Element, OutputType);
57 int i, j;
58 for (i=0; i<(int)m_Adjustments.size(); ++i)
59 {
60 TiXmlElement Adjustment("Adjustment");
61 Adjustment.SetAttribute("index", i);
62 for (j=0; j<(int)m_Adjustments[i].size(); ++j)
63 {
64 TiXmlElement AdjustmentSection("AdjustmentSection");
65 AdjustmentSection.SetAttribute("T", stringify(m_Adjustments[i][j].first));
66 AdjustmentSection.SetAttribute("Vector", stringify(m_Adjustments[i][j].second));
67 Adjustment.InsertEndChild(AdjustmentSection);
68 }
69 Element.InsertEndChild(Adjustment);
70 }
71 TiXmlElement Interpolation("Interpolation");
72 m_pInterpolation->PopulateTiXmlElement(Interpolation, OutputType);
73 Element.InsertEndChild(Interpolation);
74}
75
76CSlaveNode CInterpolationAdjusted::GetNode(const vector<CNode> &MasterNodes, int iIndex, double t) const
77{
78 CSlaveNode Node = m_pInterpolation->GetNode(MasterNodes, iIndex, t);
79
80 if (iIndex < 0 || iIndex >= (int)m_Adjustments.size())
81 {
82 TGERROR("Unable to adjust node position. Index out of range: " << iIndex);
83 return Node;
84 }
85
86 XYZ NewPos = Node.GetPosition() + GetInterpedValue(m_Adjustments[iIndex], t);
87 Node.SetPosition(NewPos);
88
89 return Node;
90}
91
92void CInterpolationAdjusted::Initialise(const vector<CNode> &MasterNodes) const
93{
94 m_pInterpolation->Initialise(MasterNodes);
95}
96
97void CInterpolationAdjusted::AddAdjustment(int iIndex, double t, XYZ Vector)
98{
99 if (iIndex < 0)
100 {
101 TGERROR("Unable to add adjustment. Index out of range: " << iIndex);
102 return;
103 }
104 if (iIndex >= (int)m_Adjustments.size())
105 {
106 m_Adjustments.resize(iIndex+1);
107 }
108 m_Adjustments[iIndex].push_back(make_pair(t, Vector));
109 // Sort the vector to make sure t values are ordered from lowest to highest
110 sort(m_Adjustments[iIndex].begin(), m_Adjustments[iIndex].end());
111}
112
113
114
115
#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
CSlaveNode GetNode(const vector< CNode > &MasterNodes, int iIndex, double t) const
Get a node from parametric function where t is specified.
void PopulateTiXmlElement(TiXmlElement &Element, OUTPUT_TYPE OutputType) const
Used for saving data to XML.
vector< vector< pair< double, XYZ > > > m_Adjustments
Represents the adjustements to the interpolation.
CObjectContainer< CInterpolation > m_pInterpolation
CInterpolationAdjusted(const CInterpolation &Interpolation)
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 Initialise(const vector< CNode > &MasterNodes) const
Calculate the node tangents (use node tangents of they exist) and store them in m_Tangents.
Abstract base class for describing the yarn path interpolations.
Definition: Interpolation.h:33
virtual void PopulateTiXmlElement(TiXmlElement &Element, OUTPUT_TYPE OutputType) const
Used for saving data to XML.
static CObjectContainer< CInterpolation > CreateInterpolation(TiXmlElement &Element)
Create an interpolation from TiXmlElement.
XYZ GetPosition() const
Definition: Node.h:57
void SetPosition(XYZ Position)
Definition: Node.h:62
A derivation of the CNode class which contains data specific to slave nodes such as sections.
Definition: SlaveNode.h:30
Namespace containing a series of customised math operations not found in the standard c++ library.
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 3D space.
Definition: mymath.h:56