TexGen
SectionBezier.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 "SectionBezier.h"
22
23using namespace TexGen;
24CSectionBezier::CSectionBezier(const vector<XY> &BezierPoints, bool bSingleQuadrant)
25: m_BezierPoints(BezierPoints)
26{
27 if (!bSingleQuadrant)
28 {
29 if (m_BezierPoints.size() % 3 != 0)
30 {
31 TGERROR("Unable to create bezier section, number of points must be multiple of 3");
32 m_BezierPoints.clear();
33 }
34 }
35 else
36 {
37 if ((m_BezierPoints.size()-1) % 3 != 0)
38 {
39 TGERROR("Unable to create bezier section, number of points-1 must be multiple of 3");
40 m_BezierPoints.clear();
41 return;
42 }
43 vector<XY>::const_iterator itPoint;
44 XY P;
45 // Reflect to top left quadrant
46 for (itPoint = BezierPoints.end()-2; itPoint != BezierPoints.begin(); --itPoint)
47 {
48 P = *itPoint;
49 P.x *= -1;
50 m_BezierPoints.push_back(P);
51 }
52 // Reflect to bottom left quadrant
53 for (itPoint = BezierPoints.begin(); itPoint != BezierPoints.end(); ++itPoint)
54 {
55 P = *itPoint;
56 P.y *= -1;
57 P.x *= -1;
58 m_BezierPoints.push_back(P);
59 }
60 // Reflect to bottom right quadrant
61 for (itPoint = BezierPoints.end()-2; itPoint != BezierPoints.begin(); --itPoint)
62 {
63 P = *itPoint;
64 P.y *= -1;
65 m_BezierPoints.push_back(P);
66 }
67 }
68}
69
71{
72}
73
74bool CSectionBezier::operator == (const CSection &CompareMe) const
75{
76 if (CompareMe.GetType() != GetType())
77 return false;
78 return m_BezierPoints == ((CSectionBezier*)&CompareMe)->m_BezierPoints;
79}
80
81CSectionBezier::CSectionBezier(TiXmlElement &Element)
82: CSection(Element)
83{
84 FOR_EACH_TIXMLELEMENT(pPoint, Element, "BezierPoint")
85 {
86 m_BezierPoints.push_back(valueify<XY>(pPoint->Attribute("value")));
87 }
88}
89
90void CSectionBezier::PopulateTiXmlElement(TiXmlElement &Element, OUTPUT_TYPE OutputType) const
91{
92 CSection::PopulateTiXmlElement(Element, OutputType);
93 vector<XY>::const_iterator itPoint;
94 for (itPoint = m_BezierPoints.begin(); itPoint != m_BezierPoints.end(); ++itPoint)
95 {
96 TiXmlElement Point("BezierPoint");
97 Point.SetAttribute("value", stringify(*itPoint));
98 Element.InsertEndChild(Point);
99 }
100}
101
103{
104 int iNumSections = m_BezierPoints.size()/3;
105 t *= iNumSections;
106 int iIndex = int(t);
107 t -= iIndex;
108 if (iIndex >= iNumSections)
109 {
110 iIndex = iNumSections-1;
111 t = 1;
112 }
113 XY P1, P2, P3, P4;
114
115 P1 = m_BezierPoints[iIndex*3];
116 P2 = m_BezierPoints[iIndex*3+1];
117 P3 = m_BezierPoints[iIndex*3+2];
118 P4 = m_BezierPoints[(iIndex*3+3)%m_BezierPoints.size()];
119
120 return CalculateBezierPoint(P1, P2, P3, P4, t);
121}
122
124{
125 return "Bezier(N:" + stringify(m_BezierPoints.size()/3) + ")";
126}
127
128
129
130
#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
Section made up of bezier curves.
Definition: SectionBezier.h:38
CSectionBezier(const vector< XY > &BezierPoints, bool bSingleQuadrant=false)
The number of points given must be a multiple of 3.
vector< XY > m_BezierPoints
Definition: SectionBezier.h:56
bool operator==(const CSection &CompareMe) const
Overloaded equality operator to determine if two sections are the same.
void PopulateTiXmlElement(TiXmlElement &Element, OUTPUT_TYPE OutputType) const
Used for saving data to XML.
string GetDefaultName() const
Get the default name to assign to a section.
XY GetPoint(double t) const
Get a point lying on the perimeter correspending to parametric value t.
string GetType() const
Derived class should return the class name.
Definition: SectionBezier.h:50
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
virtual string GetType() const =0
Derived class should return the class name.
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
T CalculateBezierPoint(T p1, T p2, T p3, T p4, double mu)
Definition: mymath.h:1175
Struct for representing points in 2D space.
Definition: mymath.h:103
double x
Definition: mymath.h:104
double y
Definition: mymath.h:104