LowStretchSphereParametrization.h
Go to the documentation of this file.
1 #ifndef LOWSTRETCHSPHEREPARAMETRIZATION_HEADER
2 #define LOWSTRETCHSPHEREPARAMETRIZATION_HEADER
3 #include "Sphere.h"
4 #include "basic.h"
5 #include <GfxTL/VectorXD.h>
6 #include <GfxTL/Frame.h>
7 #include <GfxTL/MathHelper.h>
8 #include <GfxTL/AABox.h>
9 #include <utility>
10 #include <MiscLib/Vector.h>
11 
13 {
14 public:
16  void Shape(const Sphere& sphere);
17  const Sphere& Shape() const
18  {
19  return *m_sphere;
20  }
21  inline void Parameters(const Vec3f& p,
22  std::pair< float, float >* param) const;
23  inline bool InSpace(float u, float v, Vec3f* p) const;
24  inline bool InSpace(float u, float v, Vec3f* p, Vec3f* n) const;
26  float epsilon, bool* uwrap, bool* vwrap) const
27  {
28  *uwrap = false;
29  *vwrap = false;
30  }
32  float epsilon, size_t uextent, size_t vextent,
33  MiscLib::Vector< int >* componentImg,
34  MiscLib::Vector< std::pair< int, size_t > >* labels) const;
35  template< class IteratorT >
36  void Optimize(IteratorT begin, IteratorT end, float epsilon);
37  static size_t SerializedSize();
38  void Serialize(std::ostream* o, bool binary) const;
39  void Deserialize(std::istream* i, bool binary);
40 
41 private:
42  const Sphere* m_sphere;
44 };
45 
47  std::pair< float, float >* param) const
48 {
49  Vec3f s = p - m_sphere->Center();
50  float slength = s.length();
52  m_frame.ToLocal(*((GfxTL::Vector3Df*)&s), &l);
53  if (slength > 0)
54  {
55  l[2] /= slength;
56  }
57  l[2] = GfxTL::Math< float >::Clamp(l[2], -1, 1);
58  float radius = std::sqrt(1.f - l[2] * l[2]) * m_sphere->Radius();
59  param->first = std::acos(l[2]) * m_sphere->Radius();
60  param->second = std::atan2(l[1], l[0]) * radius;
61 }
62 
63 bool LowStretchSphereParametrization::InSpace(float u, float v, Vec3f* p) const
64 {
65  float uangle = u / m_sphere->Radius();
66  float cosu = std::cos(uangle);
67  float sinu = std::sin(uangle);
68  float radius = sinu * m_sphere->Radius();
69  float vangle = v / radius;
70  float sinv = std::sin(vangle);
71  float cosv = std::cos(vangle);
73  m_frame.ToGlobal(GfxTL::Vector3Df(sinu * cosv, sinu * sinv, cosu), &g);
74  *p = m_sphere->Radius() * Vec3f(g) + m_sphere->Center();
75  return true;
76 }
77 
79  Vec3f* n) const
80 {
81  float uangle = u / m_sphere->Radius();
82  float cosu = std::cos(uangle);
83  float sinu = std::sin(uangle);
84  float radius = sinu * m_sphere->Radius();
85  float vangle = v / radius;
86  float sinv = std::sin(vangle);
87  float cosv = std::cos(vangle);
89  m_frame.ToGlobal(GfxTL::Vector3Df(sinu * cosv, sinu * sinv, cosu), &g);
90  *n = Vec3f(g);
91  *p = m_sphere->Radius() * (*n) + m_sphere->Center();
92  return true;
93 }
94 
95 template< class IteratorT >
96 void LowStretchSphereParametrization::Optimize(IteratorT begin, IteratorT end,
97  float epsilon)
98 {
99  // tries to find a rotation of the parametrization that does not cut
100  // the points
101 
102  // pull the center of the points onto the equator
103  float minUangle = float(M_PI), maxUangle = 0;
104  if (end - begin <= 1)
105  {
106  return;
107  }
108  for (IteratorT i = begin; i != end; ++i)
109  {
110  Vec3f s = *i - m_sphere->Center();
111  float slength = s.length();
112  float h = m_frame.Height(*((GfxTL::Vector3Df*)&s));
113  if (slength > 0)
114  {
115  h /= slength;
116  }
117  h = GfxTL::Math< float >::Clamp(h, -1, 1);
118  float uangle = std::acos(h);
119  if (minUangle > uangle)
120  {
121  minUangle = uangle;
122  }
123  if (maxUangle < uangle)
124  {
125  maxUangle = uangle;
126  }
127  }
128  float centerUangle = (minUangle + maxUangle) / 2;
129  // centerUangle should be located at 90 degrees
131  q.RotationRad(-2 * (centerUangle - float(M_PI / 2)), m_frame[0][0], m_frame[0][1], m_frame[0][2]);
132  GfxTL::Vector3Df newNormal;
133  q.Rotate(m_frame[2], &newNormal);
134  m_frame.FromNormal(newNormal);
135 
136  // a rotation around the normal is searched
137  // for this find all the angles and then extract the largest gap
138  MiscLib::Vector< float > vangles;
139  for (IteratorT i = begin; i != end; ++i)
140  {
141  Vec3f s = *i - m_sphere->Center();
142  float slength = s.length();
144  m_frame.ToLocal(*((GfxTL::Vector3Df*)&s), &l);
145  if (slength > 0)
146  {
147  l[2] /= slength;
148  }
149  l[2] = GfxTL::Math< float >::Clamp(l[2], -1, 1);
150  float uangle = std::acos(l[2]);
151  if (uangle * m_sphere->Radius() < float(M_PI) * m_sphere->Radius() - 2 * epsilon
152  && uangle * m_sphere->Radius() > float(-M_PI) * m_sphere->Radius() + 2 * epsilon)
153  {
154  vangles.push_back(std::atan2(l[1], l[0]));
155  }
156  }
157  std::sort(vangles.begin(), vangles.end());
158  // try to find a large gap
159  float maxGap = vangles.front() + 2 * float(M_PI) - vangles.back();
160  float lower = vangles.back(), upper = vangles.front() + 2 * float(M_PI);
161  for (size_t i = 1; i < vangles.size(); ++i)
162  {
163  float gap = vangles[i] - vangles[i - 1];
164  if (gap > maxGap)
165  {
166  maxGap = gap;
167  lower = vangles[i - 1];
168  upper = vangles[i];
169  }
170  }
171  // reparameterize by rotating the frame on the normal such that x direction
172  // coincides with the gap
173  float rotationAngle = (lower + upper) / 2;
174  m_frame.RotateOnNormal(rotationAngle + float(M_PI));
175 }
176 
177 #endif
MiscLib::Vector::front
T & front()
Definition: Vector.h:510
LowStretchSphereParametrization::InSpace
bool InSpace(float u, float v, Vec3f *p) const
Definition: LowStretchSphereParametrization.h:63
GfxTL::sqrt
VectorXD< D, T > sqrt(const VectorXD< D, T > &a)
Definition: VectorXD.h:662
GfxTL::VectorXD
Definition: MatrixXX.h:21
LowStretchSphereParametrization::WrapBitmap
void WrapBitmap(const GfxTL::AABox< GfxTL::Vector2Df > &bbox, float epsilon, bool *uwrap, bool *vwrap) const
Definition: LowStretchSphereParametrization.h:25
MiscLib::Vector::begin
T * begin()
Definition: Vector.h:460
GfxTL::Frame< 3, float >
Vector.h
Sphere.h
Vec3f
Definition: basic.h:16
MiscLib::Vector::push_back
void push_back(const T &v)
Definition: Vector.h:346
GfxTL::Quaternion
Definition: VectorXD.h:707
MiscLib::Vector< int >
VectorXD.h
LowStretchSphereParametrization::LowStretchSphereParametrization
LowStretchSphereParametrization(const Sphere &sphere)
Definition: LowStretchSphereParametrization.cpp:4
MiscLib::Vector::back
T & back()
Definition: Vector.h:500
MiscLib::Vector::size
size_type size() const
Definition: Vector.h:212
M_PI
#define M_PI
Definition: MathTools.h:17
LowStretchSphereParametrization::Serialize
void Serialize(std::ostream *o, bool binary) const
Definition: LowStretchSphereParametrization.cpp:112
LowStretchSphereParametrization
Definition: LowStretchSphereParametrization.h:12
AABox.h
LowStretchSphereParametrization::SerializedSize
static size_t SerializedSize()
Definition: LowStretchSphereParametrization.cpp:107
MiscLib::Vector::end
T * end()
Definition: Vector.h:470
LowStretchSphereParametrization::Deserialize
void Deserialize(std::istream *i, bool binary)
Definition: LowStretchSphereParametrization.cpp:141
GfxTL::Math::Clamp
static ScalarT Clamp(ScalarT s, ScalarT bottom, ScalarT top)
Definition: MathHelper.h:36
Frame.h
q
#define q
LowStretchSphereParametrization::Shape
const Sphere & Shape() const
Definition: LowStretchSphereParametrization.h:17
Sphere::Center
const Vec3f & Center() const
Definition: Sphere.cpp:346
LowStretchSphereParametrization::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: LowStretchSphereParametrization.cpp:17
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
float
#define float
Definition: 16_Level.h:22
basic.h
LowStretchSphereParametrization::Optimize
void Optimize(IteratorT begin, IteratorT end, float epsilon)
Definition: LowStretchSphereParametrization.h:96
Sphere
Definition: Sphere.h:27
GfxTL::Vec3f
VectorXD< 3, float > Vec3f
Definition: VectorXD.h:691
MathHelper.h
LowStretchSphereParametrization::Parameters
void Parameters(const Vec3f &p, std::pair< float, float > *param) const
Definition: LowStretchSphereParametrization.h:46
Sphere::Radius
float Radius() const
Definition: Sphere.cpp:351
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