TexGen
SectionHybrid.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 "SectionHybrid.h"
22#include "SectionEllipse.h"
23#include "SectionRotated.h"
24
25using namespace TexGen;
26CSectionHybrid::CSectionHybrid(void)
27{
28 // m_Sections should never be emtpy
29 m_Sections.resize(1, CSectionEllipse(1, 1));
30}
31
32CSectionHybrid::CSectionHybrid(const CSection &TopHalf, const CSection &BottomHalf)
33{
34 AddDivision(0);
35 AddDivision(0.5);
36 AssignSection(0, TopHalf);
37 AssignSection(1, BottomHalf);
38}
39
40CSectionHybrid::CSectionHybrid(const CSection &TopRight, const CSection &TopLeft, const CSection &BottomLeft, const CSection &BottomRight)
41{
42 AddDivision(0);
43 AddDivision(0.25);
44 AddDivision(0.5);
45 AddDivision(0.75);
46 AssignSection(0, TopRight);
47 AssignSection(1, TopLeft);
48 AssignSection(2, BottomLeft);
49 AssignSection(3, BottomRight);
50}
51
53{
54}
55
56bool CSectionHybrid::operator == (const CSection &CompareMe) const
57{
58 if (CompareMe.GetType() != GetType())
59 return false;
60 return (m_Divisions == ((CSectionHybrid*)&CompareMe)->m_Divisions &&
61 m_Sections == ((CSectionHybrid*)&CompareMe)->m_Sections);
62}
63
64CSectionHybrid::CSectionHybrid(TiXmlElement &Element)
65: CSection(Element)
66{
67 FOR_EACH_TIXMLELEMENT(pDivision, Element, "Division")
68 {
69 double dVal = 0;
70 pDivision->Attribute("Value", &dVal);
71 AddDivision(dVal);
72 }
73 int i = 0;
74 FOR_EACH_TIXMLELEMENT(pSection, Element, "Section")
75 {
76 AssignSection(i++, *CreateSection(*pSection));
77 }
78}
79
80void CSectionHybrid::PopulateTiXmlElement(TiXmlElement &Element, OUTPUT_TYPE OutputType) const
81{
82 CSection::PopulateTiXmlElement(Element, OutputType);
83 int i;
84 for (i=0; i<(int)m_Divisions.size(); ++i)
85 {
86 TiXmlElement Division("Division");
87 Division.SetAttribute("Value", stringify(m_Divisions[i]));
88 Element.InsertEndChild(Division);
89 }
90 for (i=0; i<(int)m_Sections.size(); ++i)
91 {
92 TiXmlElement Section("Section");
93 m_Sections[i]->PopulateTiXmlElement(Section, OutputType);
94 Element.InsertEndChild(Section);
95 }
96}
97
98void CSectionHybrid::AddDivision(double dFraction)
99{
100 m_Divisions.push_back(dFraction);
101 sort(m_Divisions.begin(), m_Divisions.end());
102 m_Sections.resize(max((int)m_Divisions.size(), 1), CSectionEllipse(1, 1));
103}
104
105bool CSectionHybrid::AssignSection(int iIndex, const CSection &Section)
106{
107 if (iIndex < 0 || iIndex >= (int)m_Sections.size())
108 {
109 TGERROR("Unable to assign section, index invalid: " << iIndex);
110 return false;
111 }
112 m_Sections[iIndex] = Section;
113 return true;
114}
115
117{
118 vector<double>::const_iterator itDiv;
119 int i;
120 for (itDiv = m_Divisions.begin(), i=0; itDiv != m_Divisions.end(); ++itDiv, ++i)
121 {
122 if (t <= *itDiv)
123 break;
124 }
125 --i;
126 if (i < 0)
127 i = max((int)m_Divisions.size()-1, 0);
128
129 assert(i >= 0 && i < (int)m_Sections.size());
130
131 if ( m_Sections[i]->GetType() == "CSectionRotated" )
132 {
133 CSectionRotated* rotSection = (CSectionRotated*)m_Sections[i]->Copy();
134 double angle = rotSection->GetAngle();
135 double t_angle = t*2*PI;
136 double new_angle = t_angle - angle;
137 if ( new_angle < 0.0 )
138 new_angle += (2*PI);
139 t = new_angle / (2*PI);
140 delete rotSection;
141 }
142
143 return m_Sections[i]->GetPoint(t);
144}
145
147{
148 return "Hybrid(N:" + stringify(m_Sections.size()) + ")";
149}
150
151double CSectionHybrid::GetDivision(int iIndex) const
152{
153 assert(iIndex>=0 && iIndex<(int)m_Divisions.size());
154 return m_Divisions[iIndex];
155}
156
157const CSection &CSectionHybrid::GetSection(int iIndex) const
158{
159 assert(iIndex>=0 && iIndex<(int)m_Sections.size());
160 return *m_Sections[iIndex];
161}
162
163
164
165
166
167
#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
Elliptical Section.
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
static CObjectContainer< CSection > CreateSection(TiXmlElement &Element)
Create a section from TiXmlElement.
Definition: Section.cpp:78
virtual string GetType() const =0
Derived class should return the class name.
Hybrid of any number of other sections.
Definition: SectionHybrid.h:29
XY GetPoint(double t) const
Get a point lying on the perimeter correspending to parametric value t.
vector< CObjectContainer< CSection > > m_Sections
Definition: SectionHybrid.h:62
void AddDivision(double dFraction)
Add a division where the sections will transfer from one to the other.
bool operator==(const CSection &CompareMe) const
Overloaded equality operator to determine if two sections are the same.
CSection * Copy() const
Create a copy of the derived section and return a pointer to the newly created instance.
Definition: SectionHybrid.h:41
string GetType() const
Derived class should return the class name.
Definition: SectionHybrid.h:45
vector< double > m_Divisions
Definition: SectionHybrid.h:61
string GetDefaultName() const
Get the default name to assign to a section.
void PopulateTiXmlElement(TiXmlElement &Element, OUTPUT_TYPE OutputType) const
Used for saving data to XML.
const CSection & GetSection(int iIndex) const
bool AssignSection(int iIndex, const CSection &Section)
Assign a section between divisions.
CSectionHybrid(void)
Empty hybrid section, parts can be added with the AddDivision and AssignSection functions.
double GetDivision(int iIndex) const
Section which represents a rotation of another section angle given in radians.
double GetAngle() 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
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