TexGen
SectionLenticular.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 "SectionLenticular.h"
22using namespace TexGen;
23
24CSectionLenticular::CSectionLenticular(double dWidth, double dHeight, double dDistortion)
25: m_dWidth(dWidth)
26, m_dHeight(dHeight)
27, m_dDistortion(dDistortion)
28{
29 if (dHeight > dWidth)
30 TGERROR("Warning: Lenticular section has greater height than width");
31 if (abs(dDistortion) > 0.5*dHeight+1e-9)
32 TGERROR("Warning: Lenticular section distortion is greater than half the height");
33}
34
36{
37}
38
39bool CSectionLenticular::operator == (const CSection &CompareMe) const
40{
41 if (CompareMe.GetType() != GetType())
42 return false;
43 return m_dWidth == ((CSectionLenticular*)&CompareMe)->m_dWidth &&
44 m_dHeight == ((CSectionLenticular*)&CompareMe)->m_dHeight &&
45 m_dDistortion == ((CSectionLenticular*)&CompareMe)->m_dDistortion;
46}
47
49: CSection(Element)
50, m_dWidth(0)
51, m_dHeight(0)
52, m_dDistortion(0)
53{
54 // For backwards compatibility, previously lenticular shapes could
55 // not be distorted
56 if (Element.Attribute("Radius") && Element.Attribute("Offset"))
57 {
58 double dRadius, dOffset;
59 Element.Attribute("Radius", &dRadius);
60 Element.Attribute("Offset", &dOffset);
61 m_dWidth = 2*sqrt(dRadius*dRadius-dOffset*dOffset);
62 m_dHeight = 2*(dRadius-dOffset);
63 m_dDistortion = 0;
64 }
65 else
66 {
67 Element.Attribute("Width", &m_dWidth);
68 Element.Attribute("Height", &m_dHeight);
69 Element.Attribute("Distortion", &m_dDistortion);
70 }
71}
72
73void CSectionLenticular::PopulateTiXmlElement(TiXmlElement &Element, OUTPUT_TYPE OutputType) const
74{
75 CSection::PopulateTiXmlElement(Element, OutputType);
76 Element.SetAttribute("Width", stringify(m_dWidth));
77 Element.SetAttribute("Height", stringify(m_dHeight));
78 Element.SetAttribute("Distortion", stringify(m_dDistortion));
79}
80
82{
83 XY Point;
84 double dH;
85 double dRadius;
86 double dO;
87 double dMaxAngle;
88 double dAngle;
89
90 if (t < 0.5)
92 else
94
95 if (abs(dH) < 1e-9)
96 {
97 if (t < 0.5)
98 {
99 Point.x = 0.5*m_dWidth-2*t*m_dWidth;
100 Point.y = 0.5*m_dHeight;
101 }
102 else
103 {
104 Point.x = 0.5*m_dWidth-2*(1-t)*m_dWidth;
105 Point.y = -0.5*m_dHeight;
106 }
107 }
108 else
109 {
110 dRadius = (m_dWidth*m_dWidth + dH*dH) / (4*dH);
111 dO = dRadius-0.5*m_dHeight;
112 dMaxAngle = asin(0.5*m_dWidth/dRadius);
113
114 if (t < 0.5)
115 {
116 dAngle = (1-4*t)*dMaxAngle;
117 Point.x = dRadius*sin(dAngle);
118 Point.y = dRadius*cos(dAngle)-dO;
119 }
120 else
121 {
122 dAngle = (-3+4*t)*dMaxAngle;
123 Point.x = dRadius*sin(dAngle);
124 Point.y = -(dRadius*cos(dAngle)-dO);
125 }
126 }
127
128 return Point;
129}
130
132{
133 return "Lenticular(W:" + stringify(m_dWidth) + ",H:" + stringify(m_dHeight) + ",D:" + stringify(m_dDistortion) + ")";
134}
135
136
137
138
139
140
#define TGERROR(MESSAGE)
Macros used to report the file name and line number to the TexGenError and TexGenLog functions.
Definition: Logger.h:29
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.
void PopulateTiXmlElement(TiXmlElement &Element, OUTPUT_TYPE OutputType) const
Used for saving data to XML.
CSectionLenticular(double dWidth, double dHeight, double dDistortion=0)
string GetType() const
Derived class should return the class name.
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.
bool operator==(const CSection &CompareMe) const
Overloaded equality operator to determine if two sections are the same.
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