LowStretchTorusParametrization.h
Go to the documentation of this file.
1 #ifndef LOWSTRETCHTORUSPARAMETRIZATION_HEADER
2 #define LOWSTRETCHTORUSPARAMETRIZATION_HEADER
3 #include "Torus.h"
4 #include "basic.h"
5 #include <GfxTL/VectorXD.h>
7 #include <GfxTL/MathHelper.h>
8 #include <GfxTL/AABox.h>
9 #include <GfxTL/Frame.h>
10 #include <utility>
11 #include <MiscLib/Vector.h>
12 
14 {
15 public:
17  void Shape(const Torus& torus);
18  const Torus& Shape() const
19  {
20  return *m_torus;
21  }
22  inline void Parameters(const Vec3f& p,
23  std::pair< float, float >* param) const;
24  inline bool InSpace(float u, float v, Vec3f* p) const;
25  inline bool InSpace(float u, float v, Vec3f* p, Vec3f* n) const;
27  float epsilon, bool* uwrap, bool* vwrap) const
28  {
29  *uwrap = false;
30  *vwrap = false;
31  }
33  float epsilon, size_t uextent, size_t vextent,
34  MiscLib::Vector< int >* componentImg,
35  MiscLib::Vector< std::pair< int, size_t > >* labels) const;
36  template< class IteratorT >
37  void Optimize(IteratorT begin, IteratorT end, float epsilon);
38  static size_t SerializedSize();
39  void Serialize(std::ostream* o, bool binary) const;
40  void Deserialize(std::istream* i, bool binary);
41 
42 private:
43  float MinorFrameRotation() const;
44  float MajorFrameRotation() const;
45 
46 private:
47  const Torus* m_torus;
49  GfxTL::Frame< 2, float > m_minorFrame;
50 };
51 
53  std::pair< float, float >* param) const
54 {
55  Vec3f s = p - m_torus->Center();
56  float planex = s.dot(m_hcs[0].Data());
57  float planey = s.dot(m_hcs[1].Data());
58  param->first = std::atan2(planey, planex); // major angle
59  GfxTL::Vector2Df minorVec, minorL;
60  minorVec[1] = s.dot(m_torus->AxisDirection());
61  minorVec[0] = std::sqrt(planex * planex + planey * planey) - m_torus->MajorRadius();
62  float majorRadiusAngle = std::atan2(minorVec[1], minorVec[0]);
63  m_minorFrame.ToLocal(minorVec, &minorL);
64  param->second = std::atan2(minorL[1], minorL[0]); // minor angle
65  if (m_torus->IsAppleShaped())
66  {
67  if (abs(param->second) > m_torus->AppleCutOffAngle())
68  param->second = GfxTL::Math< float >::Sign(param->second)
69  * m_torus->AppleCutOffAngle();
70  }
71  // multiply major angle with radius
72  float majorRadius = m_torus->MajorRadius()
73  + std::cos(majorRadiusAngle) * m_torus->MinorRadius();
74  param->first = param->first * majorRadius;
75  param->second *= m_torus->MinorRadius();
76 }
77 
78 bool LowStretchTorusParametrization::InSpace(float u, float v, Vec3f* p) const
79 {
80  float vangle = v / m_torus->MinorRadius();
81  GfxTL::Vector2Df minorVec, minorL;
82  minorL[0] = std::cos(vangle);
83  minorL[1] = std::sin(vangle);
84  m_minorFrame.ToGlobal(minorL, &minorVec);
85  float majorRadiusAngle = std::atan2(minorVec[1], minorVec[0]);
86  minorVec[0] = m_torus->MinorRadius() * minorVec[0] + m_torus->MajorRadius();
87  minorVec[1] *= m_torus->MinorRadius();
88  Vec3f pp = minorVec[0] * Vec3f(m_hcs[0].Data()) + minorVec[1] * m_torus->AxisDirection();
90  float majorRadius = m_torus->MajorRadius()
91  + std::cos(majorRadiusAngle) * m_torus->MinorRadius();
92  float uangle = u / majorRadius;
93  q.RotationRad(uangle, m_torus->AxisDirection()[0], m_torus->AxisDirection()[1],
94  m_torus->AxisDirection()[2]);
95  q.Rotate(pp, p);
96  *p += m_torus->Center();
97  return true;
98 }
99 
101  Vec3f* n) const
102 {
103  float vangle = v / m_torus->MinorRadius();
104  GfxTL::Vector2Df minorVec, minorL;
105  minorL[0] = std::cos(vangle);
106  minorL[1] = std::sin(vangle);
107  m_minorFrame.ToGlobal(minorL, &minorVec);
108  float majorRadiusAngle = std::atan2(minorVec[1], minorVec[0]);
109  Vec3f nn = minorVec[0] * Vec3f(m_hcs[0].Data()) + minorVec[1] * m_torus->AxisDirection();
110  minorVec[0] = m_torus->MinorRadius() * minorVec[0] + m_torus->MajorRadius();
111  minorVec[1] *= m_torus->MinorRadius();
112  Vec3f pp = minorVec[0] * Vec3f(m_hcs[0].Data()) + minorVec[1] * m_torus->AxisDirection();
114  float majorRadius = m_torus->MajorRadius()
115  + std::cos(majorRadiusAngle) * m_torus->MinorRadius();
116  float uangle = u / majorRadius;
117  q.RotationRad(uangle, m_torus->AxisDirection()[0], m_torus->AxisDirection()[1],
118  m_torus->AxisDirection()[2]);
119  q.Rotate(pp, p);
120  q.Rotate(nn, n);
121  *p += m_torus->Center();
122  return true;
123 }
124 
125 template< class IteratorT >
126 void LowStretchTorusParametrization::Optimize(IteratorT begin, IteratorT end,
127  float epsilon)
128 {
129  // tries to find a rotation of the parametrization that does not cut
130  // the points
131  // find a rotation along the major radius (u-direction) and minor radius (v-direction)
133  nframe.FromNormal(m_torus->AxisDirection());
134  MiscLib::Vector< float > uangles(end - begin), vangles(end - begin);
135  size_t j = 0;
136  for (IteratorT i = begin; i != end; ++i, ++j)
137  {
138  Vec3f s = *i - m_torus->Center();
139  float planex = s.dot(nframe[0].Data());
140  float planey = s.dot(nframe[1].Data());
141  uangles[j] = std::atan2(planey, planex); // major angle
142  GfxTL::Vector2Df minorVec;
143  minorVec[1] = s.dot(m_torus->AxisDirection());
144  minorVec[0] = std::sqrt(planex * planex + planey * planey) - m_torus->MajorRadius();
145  vangles[j] = std::atan2(minorVec[1], minorVec[0]); // minor angle
146  if (m_torus->IsAppleShaped())
147  {
148  if (abs(uangles[j]) > m_torus->AppleCutOffAngle())
149  uangles[j] = GfxTL::Math< float >::Sign(uangles[j])
150  * m_torus->AppleCutOffAngle();
151  }
152  }
153  std::sort(vangles.begin(), vangles.end());
154  std::sort(uangles.begin(), uangles.end());
155  // try to find a large gap along u direction
156  float maxGap = uangles.front() + 2 * float(M_PI) - uangles.back();
157  float lower = uangles.back(), upper = uangles.front() + 2 * float(M_PI);
158  for (size_t i = 1; i < uangles.size(); ++i)
159  {
160  float gap = uangles[i] - uangles[i - 1];
161  if (gap > maxGap)
162  {
163  maxGap = gap;
164  lower = uangles[i - 1];
165  upper = uangles[i];
166  }
167  }
168  // rotate m_hcs into gap
169  float rotationAngle = (lower + upper) / 2;
170  nframe.RotateOnNormal(rotationAngle + float(M_PI));
171  m_hcs[0] = nframe[0];
172  m_hcs[1] = nframe[1];
173  // try to find a large gap along v direction
174  maxGap = vangles.front() + 2 * float(M_PI) - vangles.back();
175  lower = vangles.back();
176  upper = vangles.front() + 2 * float(M_PI);
177  for (size_t i = 1; i < vangles.size(); ++i)
178  {
179  float gap = vangles[i] - vangles[i - 1];
180  if (gap > maxGap)
181  {
182  maxGap = gap;
183  lower = vangles[i - 1];
184  upper = vangles[i];
185  }
186  }
187  rotationAngle = (lower + upper) / 2;
188  m_minorFrame.Canonical();
189  m_minorFrame.RotateFrame(rotationAngle + float(M_PI));
190 }
191 
192 #endif
MiscLib::Vector::front
T & front()
Definition: Vector.h:510
Torus::IsAppleShaped
bool IsAppleShaped() const
Definition: Torus.h:59
GfxTL::sqrt
VectorXD< D, T > sqrt(const VectorXD< D, T > &a)
Definition: VectorXD.h:662
GfxTL::HyperplaneCoordinateSystem< float, 3 >
GfxTL::VectorXD
Definition: MatrixXX.h:21
MiscLib::Vector::begin
T * begin()
Definition: Vector.h:460
GfxTL::Frame< 2, float >
Vector.h
Torus::MinorRadius
const float MinorRadius() const
Definition: Torus.h:42
Vec3f
Definition: basic.h:16
Torus.h
LowStretchTorusParametrization::LowStretchTorusParametrization
LowStretchTorusParametrization(const Torus &torus)
Definition: LowStretchTorusParametrization.cpp:4
Torus
Definition: Torus.h:18
LowStretchTorusParametrization::InSpace
bool InSpace(float u, float v, Vec3f *p) const
Definition: LowStretchTorusParametrization.h:78
LowStretchTorusParametrization::Parameters
void Parameters(const Vec3f &p, std::pair< float, float > *param) const
Definition: LowStretchTorusParametrization.h:52
GfxTL::Quaternion
Definition: VectorXD.h:707
LowStretchTorusParametrization
Definition: LowStretchTorusParametrization.h:13
LowStretchTorusParametrization::WrapComponents
void WrapComponents(const GfxTL::AABox< GfxTL::Vector2Df > &bbox, float epsilon, size_t uextent, size_t vextent, MiscLib::Vector< int > *componentImg, MiscLib::Vector< std::pair< int, size_t > > *labels) const
Definition: LowStretchTorusParametrization.cpp:19
LowStretchTorusParametrization::Optimize
void Optimize(IteratorT begin, IteratorT end, float epsilon)
Definition: LowStretchTorusParametrization.h:126
MiscLib::Vector< int >
VectorXD.h
LowStretchTorusParametrization::Deserialize
void Deserialize(std::istream *i, bool binary)
Definition: LowStretchTorusParametrization.cpp:161
GfxTL::Math::Sign
static ScalarT Sign(ScalarT s)
Definition: MathHelper.h:23
MiscLib::Vector::back
T & back()
Definition: Vector.h:500
Torus::AxisDirection
const Vec3f & AxisDirection() const
Definition: Torus.h:38
MiscLib::Vector::size
size_type size() const
Definition: Vector.h:212
armarx::abs
std::vector< T > abs(const std::vector< T > &v)
Definition: VectorHelpers.h:253
M_PI
#define M_PI
Definition: MathTools.h:17
AABox.h
HyperplaneCoordinateSystem.h
MiscLib::Vector::end
T * end()
Definition: Vector.h:470
Frame.h
LowStretchTorusParametrization::Serialize
void Serialize(std::ostream *o, bool binary) const
Definition: LowStretchTorusParametrization.cpp:145
q
#define q
LowStretchTorusParametrization::SerializedSize
static size_t SerializedSize()
Definition: LowStretchTorusParametrization.cpp:140
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
LowStretchTorusParametrization::Shape
const Torus & Shape() const
Definition: LowStretchTorusParametrization.h:18
float
#define float
Definition: 16_Level.h:22
basic.h
LowStretchTorusParametrization::WrapBitmap
void WrapBitmap(const GfxTL::AABox< GfxTL::Vector2Df > &bbox, float epsilon, bool *uwrap, bool *vwrap) const
Definition: LowStretchTorusParametrization.h:26
GfxTL::Vec3f
VectorXD< 3, float > Vec3f
Definition: VectorXD.h:691
Torus::MajorRadius
const float MajorRadius() const
Definition: Torus.h:46
MathHelper.h
Torus::Center
const Vec3f & Center() const
Definition: Torus.h:34
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
GfxTL::AABox
Definition: AABox.h:18
Torus::AppleCutOffAngle
float AppleCutOffAngle() const
Definition: Torus.h:63