TexGen
SectionRectangle.cpp
Go to the documentation of this file.
1/*=============================================================================
2TexGen: Geometric textile modeller.
3Copyright (C) 2012 Louise Brown
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 "SectionRectangle.h"
22using namespace TexGen;
23
24CSectionRectangle::CSectionRectangle(double dWidth, double dHeight)
25: m_dWidth(dWidth)
26, m_dHeight(dHeight)
27{
29}
30
32{
33}
34
35bool CSectionRectangle::operator == (const CSection &CompareMe) const
36{
37 if (CompareMe.GetType() != GetType())
38 return false;
39 return m_dWidth == ((CSectionRectangle*)&CompareMe)->m_dWidth &&
40 m_dHeight == ((CSectionRectangle*)&CompareMe)->m_dHeight;
41}
42
44: CSection(Element)
45{
46 Element.Attribute("Width", &m_dWidth);
47 Element.Attribute("Height", &m_dHeight);
48}
49
50void CSectionRectangle::PopulateTiXmlElement(TiXmlElement &Element, OUTPUT_TYPE OutputType) const
51{
52 CSection::PopulateTiXmlElement(Element, OutputType);
53 Element.SetAttribute("Width", stringify(m_dWidth));
54 Element.SetAttribute("Height", stringify(m_dHeight));
55}
56
58{
59 double dTotalLength = 2 * m_dWidth + 2 * m_dHeight;
60 double dLength = t * dTotalLength;
61 double dPartLength = m_dHeight/2.0;
62 if ( dLength <= dPartLength )
63 {
64 return XY( m_dWidth/2.0, dLength );
65 }
66 dPartLength += m_dWidth;
67 if ( dLength <= dPartLength )
68 {
69 return XY( m_dWidth/2.0 - (dLength - m_dHeight/2.0), m_dHeight/2.0 );
70 }
71 dPartLength += m_dHeight;
72 if ( dLength <= dPartLength )
73 {
74 return XY( -m_dWidth/2.0, m_dHeight/2.0 - (dLength - m_dWidth - m_dHeight/2.0) );
75 }
76 dPartLength += m_dWidth;
77 if ( dLength <= dPartLength )
78 {
79 return XY( -m_dWidth/2.0 + (dLength - m_dWidth - 1.5*m_dHeight), -m_dHeight/2.0 );
80 }
81 return XY( m_dWidth/2.0, -(dTotalLength - dLength) );
82}
83
85{
86 return "Rectangle(W:" + stringify(m_dWidth) + ",H:" + stringify(m_dHeight) + ")";
87}
88
89const vector<XY> &CSectionRectangle::GetPoints(int iNumPoints, bool bEquiSpaced) const
90{
91 if (iNumPoints != (int)m_EdgePoints.size() ) // Doesn't do anything with equispaced at the moment || bEquiSpaced != m_bEquiSpaced)
92 {
93 m_EdgePoints.resize(iNumPoints);
94 if ( iNumPoints == 2 ) // If only 2 points requested want to force them to be on centre plane
95 {
96 m_EdgePoints[0].x = m_dWidth/2.0;
97 m_EdgePoints[0].y = 0.0;
98
99 m_EdgePoints[1].x = -m_dWidth/2.0;
100 m_EdgePoints[1].y = 0.0;
101 return m_EdgePoints;
102 }
103
104 double dTotalLength = 2 * m_dWidth + 2 * m_dHeight;
105 double dAveSpacing = dTotalLength / (iNumPoints-1);
106
107 int iNumYSpaces = (int)(m_dHeight / dAveSpacing);
108 if ( ( iNumYSpaces % 2 ) || iNumYSpaces == 0 )
109 {
110 iNumYSpaces++;
111 }
112 double dYSpacing = m_dHeight/iNumYSpaces;
113
114 int iNumXSpaces = iNumPoints/2 - iNumYSpaces;
115 double dXSpacing = m_dWidth/iNumXSpaces;
116
117 int j = 0;
118 XY SectionPoint( m_dWidth/2.0, 0.0 );
119 m_EdgePoints[j] = SectionPoint;
120 ++j;
121
122 for ( int i=0; i < iNumYSpaces/2; ++i )
123 {
124 SectionPoint.y += dYSpacing;
125 m_EdgePoints[j] = SectionPoint;
126 ++j;
127 }
128 for ( int i = 0; i < iNumXSpaces; ++i )
129 {
130 SectionPoint.x -= dXSpacing;
131 m_EdgePoints[j] = SectionPoint;
132 ++j;
133 }
134 for ( int i = 0; i < iNumYSpaces; ++i )
135 {
136 SectionPoint.y -= dYSpacing;
137 m_EdgePoints[j] = SectionPoint;
138 ++j;
139 }
140 for ( int i = 0; i < iNumXSpaces; ++i )
141 {
142 SectionPoint.x += dXSpacing;
143 m_EdgePoints[j] = SectionPoint;
144 ++j;
145 }
146 for ( int i = 0; i < (iNumYSpaces/2)-1; ++i )
147 {
148 SectionPoint.y += dYSpacing;
149 m_EdgePoints[j] = SectionPoint;
150 ++j;
151 }
152 }
153 return m_EdgePoints;
154}
Abstract base class respresenting a yarn cross-section.
Definition: Section.h:31
virtual void PopulateTiXmlElement(TiXmlElement &Element, OUTPUT_TYPE OutputType) const
Used for saving data to XML.
Definition: Section.cpp:51
vector< XY > m_EdgePoints
List of 2d points creating the outline of the cross-section.
Definition: Section.h:119
virtual string GetType() const =0
Derived class should return the class name.
CObjectContainer< CSectionMesh > m_pSectionMesh
Pointer to a derived class of SectionMesh, this class is in charge of creating the section mesh.
Definition: Section.h:125
Create a rectangular mesh, the number of layers can be specified or set as -1 for automatic determina...
CSectionRectangle(double dWidth, double dHeight)
string GetType() const
Derived class should return the class name.
void PopulateTiXmlElement(TiXmlElement &Element, OUTPUT_TYPE OutputType) const
Used for saving data to XML.
virtual const vector< XY > & GetPoints(int iNumPoints, bool bEquiSpaced=false) const
Get a section with given number of points on the perimeter.
bool operator==(const CSection &CompareMe) const
Overloaded equality operator to determine if two sections are the same.
XY GetPoint(double t) const
Get a point lying on the perimeter correspending to parametric value t.
string GetDefaultName() const
Get the default name to assign to a section.
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
Struct for representing points in 2D space.
Definition: mymath.h:103
double x
Definition: mymath.h:104
double y
Definition: mymath.h:104