TexGen
YarnSectionInterp.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 "YarnSectionInterp.h"
22
23using namespace TexGen;
24CYarnSectionInterp::CYarnSectionInterp(bool bRamped, bool bPolar, bool bConstMesh)
25:CYarnSection(bConstMesh)
26, m_bRamped(bRamped)
27, m_bPolar(bPolar)
28{
29}
30
32{
33}
34
36: CYarnSection(Element)
37{
38 m_bRamped = valueify<bool>(Element.Attribute("Ramped"));
39 m_bPolar = valueify<bool>(Element.Attribute("Polar"));
40}
41
42void CYarnSectionInterp::PopulateTiXmlElement(TiXmlElement &Element, OUTPUT_TYPE OutputType) const
43{
44 CYarnSection::PopulateTiXmlElement(Element, OutputType);
45 Element.SetAttribute("Ramped", stringify(m_bRamped));
46 Element.SetAttribute("Polar", stringify(m_bPolar));
47}
48
49bool CYarnSectionInterp::GetInterpedSection(const CSection &Section1, const CSection &Section2, double u, int iNumPoints, bool bEquiSpaced, vector<XY> &Points) const
50{
51 Points.clear();
52
53 const vector<XY> &Points1 = Section1.GetPoints(iNumPoints, bEquiSpaced);
54 const vector<XY> &Points2 = Section2.GetPoints(iNumPoints, bEquiSpaced);
55
56 int i;
57
58// XY InterpedPoint, P1, P2;
59 for (i=0; i<iNumPoints; ++i)
60 {
61 Points.push_back(InterpolatePoints(Points1[i], Points2[i], u));
62// P1 = Points1[i];
63// P2 = Points2[i];
64// InterpedPoint = P1+(P2-P1)*u;
65// Points.push_back(InterpedPoint);
66 }
67
68 return true;
69
70}
71
72bool CYarnSectionInterp::GetInterpedSectionMesh(const CSection &Section1, const CSection &Section2, double u, int iNumPoints, bool bEquiSpaced, CMesh &Mesh) const
73{
74 Mesh.Clear();
75
76 const CMesh &Mesh1 = Section1.GetMesh(iNumPoints, bEquiSpaced);
77 const CMesh &Mesh2 = Section2.GetMesh(iNumPoints, bEquiSpaced);
78
79 bool bCompatible = Mesh1.GetNumNodes() == Mesh2.GetNumNodes();
80 int i;
81 for (i = 0; i < CMesh::NUM_ELEMENT_TYPES; ++i)
82 {
83 if (!bCompatible)
84 break;
86 bCompatible = false;
87 }
88
89 if (!bCompatible)
90 {
91 // The two meshes to be interpolated are not compatible
92 TGERROR("Unable to interpolate, section meshes are not compatible: " << Section1.GetDefaultName() << ", " << Section2.GetDefaultName());
93 //assert(false);
94 return false;
95 }
96
97 XY InterpedPoint, P1, P2;
98 for (i=0; i<(int)Mesh1.GetNumNodes(); ++i) // Note: Mesh1.m_Nodes.size() == Mesh2.m_Nodes.size()
99 {
100 P1.x = Mesh1.GetNode(i).x;
101 P1.y = Mesh1.GetNode(i).y;
102 P2.x = Mesh2.GetNode(i).x;
103 P2.y = Mesh2.GetNode(i).y;
104 InterpedPoint = InterpolatePoints(P1, P2, u);
105 Mesh.AddNode(XYZ(InterpedPoint.x, InterpedPoint.y, 0));
106 }
107
108 // Note: Mesh1.m_Indices == Mesh2.m_Indices
109 for (i=0; i<CMesh::NUM_ELEMENT_TYPES; ++i)
110 {
112 }
113
114 return true;
115}
116
118{
119 if (m_bRamped)
120 u = 3 * u * u - 2 * u * u * u;
121 if (m_bPolar)
122 {
123 double l1, l2, l;
124 double a1, a2, a;
125 l1 = GetLength(P1);
126 l2 = GetLength(P2);
127 l = l1+(l2-l1)*u;
128 a1 = atan2(P1.y, P1.x);
129 a2 = atan2(P2.y, P2.x);
130 if (abs(a1-a2) > PI)
131 {
132 if (a1 < a2)
133 a1+=2*PI;
134 else
135 a2+=2*PI;
136 }
137 a = a1+(a2-a1)*u;
138 XY P;
139 P.x = l*cos(a);
140 P.y = l*sin(a);
141 return P;
142 }
143 else
144 return P1+(P2-P1)*u;
145}
146
147int CYarnSectionInterp::CalculateNumberofLayers(const vector<XY> &Section) const
148{
149 // Section may either be rotated section or not start from point on x axis
150 // in which case the points need rotating back otherwise test for number of layers is invalid
151 XY p1, p2, dp;
152 p1 = Section[0];
153 p2 = Section[Section.size()/2];
154 dp = p1 - p2;
155 double dAngle = atan2( dp.y, dp.x );
156
157 vector<XY> RotatedSection;
158 for ( int i = 1; i <= (int)Section.size()/4+1; ++i )
159 {
160 XY RotPoint;
161 RotPoint.x = Section[i].x*cos(dAngle) + Section[i].y*sin(dAngle);
162 RotPoint.y = Section[i].y*cos(dAngle) - Section[i].x*sin(dAngle);
163 RotatedSection.push_back(RotPoint);
164 }
165
166 // Assuming the section is symmetric about all 4 quadrants
167 // 25-9-12 Can't assume this. Both hybrid and polygon sections may result in non-symmetrical sections
168 int i;
169 XY P1, P2, DP;
170 vector<XY>::iterator itRotatedSection;
171 //for (i=1; i<=(int)RotatedSection.size()/4; ++i)
172 for ( i = 0; i < (int)RotatedSection.size()-1; ++i )
173 {
174 P1 = RotatedSection[i];
175 P2 = RotatedSection[i+1];
176 DP = P2 - P1;
177 if (abs(DP.x) > abs(DP.y))
178 {
179 return (i+1)*2; // +1 because RotatedSection starts at 2nd point in Section
180 }
181 }
182 return i*2;
183 // Should never reach this point
184 //return 0;
185}
186
#define TGERROR(MESSAGE)
Macros used to report the file name and line number to the TexGenError and TexGenLog functions.
Definition: Logger.h:29
Defines the nodes and elements of a surface or volume mesh.
Definition: Mesh.h:58
const int AddNode(XYZ Node)
Append a node to the list of nodes, the integer returns the index of the node
Definition: Mesh.cpp:2624
const XYZ & GetNode(int iIndex) const
Get the node with given ID.
Definition: Mesh.cpp:2636
static int GetNumNodes(ELEMENT_TYPE Type)
Get the number of nodes a particular element type contains.
Definition: Mesh.h:81
const list< int > & GetIndices(ELEMENT_TYPE ElemType) const
Get the element indices of a given element type.
Definition: Mesh.cpp:2671
ELEMENT_TYPE
Each element type is represented by a unique integer value.
Definition: Mesh.h:66
@ NUM_ELEMENT_TYPES
Definition: Mesh.h:77
void Clear()
Empty mesh nodes and indices.
Definition: Mesh.cpp:1448
Abstract base class respresenting a yarn cross-section.
Definition: Section.h:31
const CMesh & GetMesh(int iNumPoints, bool bEquiSpaced=true) const
Definition: Section.cpp:107
virtual string GetDefaultName() const =0
Get the default name to assign to a section.
virtual const vector< XY > & GetPoints(int iNumPoints, bool bEquiSpaced=false) const
Get a section with given number of points on the perimeter.
Definition: Section.cpp:119
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
XY InterpolatePoints(XY P1, XY P2, double u) const
virtual void PopulateTiXmlElement(TiXmlElement &Element, OUTPUT_TYPE OutputType) const
Used for saving data to XML.
int CalculateNumberofLayers(const vector< XY > &Section) const
CYarnSectionInterp(bool bRamped, bool bPolar, bool bConstMesh=true)
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
#define PI
Definition: mymath.h:30
Namespace containing a series of customised math operations not found in the standard c++ library.
OUTPUT_TYPE
Definition: Misc.h:105
double GetLength(const XYZ &Point1, const XYZ &Point2)
Get the length between two points.
Definition: mymath.h:540
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
double x
Definition: mymath.h:104
double y
Definition: mymath.h:104
Struct for representing points in 3D space.
Definition: mymath.h:56
double x
Definition: mymath.h:57
double y
Definition: mymath.h:57