LowStretchSphereParametrization.cpp
Go to the documentation of this file.
2 #include "Bitmap.h"
3 
5  const Sphere& sphere)
6  : m_sphere(&sphere)
7 {
8  m_frame.Canonical();
9 }
10 
12 {
13  m_sphere = &sphere;
14  m_frame.Canonical();
15 }
16 
18  float epsilon, size_t uextent, size_t vextent,
19  MiscLib::Vector< int >* componentImg,
20  MiscLib::Vector< std::pair< int, size_t > >* labels) const
21 {
22  // wraps are necessary only in v direction
23  // relabel the components
24  MiscLib::Vector< std::pair< int, size_t > > tempLabels(*labels);
25  // wrap along v
26  float vstartPrev, vendPrev, vstart = 0, vend = 0, vstartNext = 0, vendNext = 0;
27  size_t vsPrev, vePrev, vs = 0, ve = 0, vsNext = 0, veNext = 0;
28  float uangle = (bbox.Min()[0] + .5f * epsilon) / m_sphere->Radius();
29  float radius = std::sin(uangle) * m_sphere->Radius();
30  vstartNext = float(-M_PI) * radius;
31  vendNext = float(M_PI) * radius;
32  vsNext = std::min(vextent - 1, (size_t)std::max((intptr_t)0,
33  (intptr_t)((vstartNext - bbox.Min()[1]) / epsilon)));
34  veNext = (size_t)std::max((intptr_t)0, std::min((intptr_t)vextent - 1,
35  (intptr_t)((vendNext - bbox.Min()[1]) / epsilon)));
36  for (size_t u = 0; u < uextent; ++u)
37  {
38  vstartPrev = vstart;
39  vstart = vstartNext;
40  vendPrev = vend;
41  vend = vendNext;
42  vsPrev = vs;
43  vs = vsNext;
44  vePrev = ve;
45  ve = veNext;
46  // determine starting position in the next column
47  if (u < uextent - 1)
48  {
49  float uangleNext = ((u + 1.5f) * epsilon + bbox.Min()[0]) / m_sphere->Radius();
50  float radiusNext = std::sin(uangle) * m_sphere->Radius();
51  vstartNext = float(-M_PI) * radius;
52  vendNext = float(M_PI) * radius;
53  vsNext = std::min(vextent - 1, (size_t)std::max((intptr_t)0,
54  (intptr_t)((vstartNext - bbox.Min()[1]) / epsilon)));
55  veNext = (size_t)std::max((intptr_t)0, std::min((intptr_t)vextent - 1,
56  (intptr_t)((vendNext - bbox.Min()[1]) / epsilon)));
57  }
58  if (vstart <= bbox.Min()[1] - epsilon
59  || vend >= bbox.Max()[1] + epsilon
60  || !(*componentImg)[vs * uextent + u])
61  {
62  continue;
63  }
64  // check the three neighbors on the other side
65  if ((*componentImg)[ve * uextent + u])
66  AssociateLabel((*componentImg)[vs * uextent + u],
67  (*componentImg)[ve * uextent + u], &tempLabels);
68  if (u > 0
69  && vstartPrev > bbox.Min()[1] - epsilon
70  && vendPrev < bbox.Min()[1] + epsilon
71  && (*componentImg)[vePrev * uextent + u - 1])
72  AssociateLabel((*componentImg)[vs * uextent + u],
73  (*componentImg)[vePrev * uextent + u - 1], &tempLabels);
74  if (u < uextent - 1
75  && vstartNext > bbox.Min()[1] - epsilon
76  && vendNext < bbox.Min()[1] + epsilon
77  && (*componentImg)[veNext * uextent + u + 1])
78  AssociateLabel((*componentImg)[vs * uextent + u],
79  (*componentImg)[veNext * uextent + u + 1], &tempLabels);
80  }
81 
82  // condense labels
83  for (size_t i = tempLabels.size() - 1; i > 0; --i)
84  {
85  tempLabels[i].first = ReduceLabel(i, tempLabels);
86  }
87  MiscLib::Vector< int > condensed(tempLabels.size());
88  labels->clear();
89  labels->reserve(condensed.size());
90  int count = 0;
91  for (size_t i = 0; i < tempLabels.size(); ++i)
92  if (i == tempLabels[i].first)
93  {
94  labels->push_back(std::make_pair(count, tempLabels[i].second));
95  condensed[i] = count;
96  ++count;
97  }
98  else
99  (*labels)[condensed[tempLabels[i].first]].second
100  += tempLabels[i].second;
101  // set new component ids
102  for (size_t i = 0; i < componentImg->size(); ++i)
103  (*componentImg)[i] =
104  condensed[tempLabels[(*componentImg)[i]].first];
105 }
106 
108 {
109  return sizeof(GfxTL::Frame< 3, float >::PointType) + sizeof(float);
110 }
111 
112 void LowStretchSphereParametrization::Serialize(std::ostream* o, bool binary) const
113 {
114  // defer rotation from frame
116  nframe.FromNormal(m_frame[2]);
118  nframe.ToTangent(m_frame[0], &t);
119  for (unsigned int i = 0; i < 2; ++i)
120  {
121  t[i] = GfxTL::Math< float >::Clamp(t[i], -1, 1);
122  }
123  float angle = std::atan2(t[1], t[0]);
124  // write normal and dummy rotation
125  if (binary)
126  {
127  o->write((char*)&m_frame[2],
129  o->write((char*)&angle, sizeof(angle));
130  }
131  else
132  {
133  for (unsigned int i = 0; i < 3; ++i)
134  {
135  (*o) << m_frame[2][i] << " ";
136  }
137  (*o) << angle << " ";
138  }
139 }
140 
141 void LowStretchSphereParametrization::Deserialize(std::istream* i, bool binary)
142 {
143  float rot;
145  if (binary)
146  {
147  i->read((char*)&normal, sizeof(normal));
148  i->read((char*)&rot, sizeof(rot));
149  }
150  else
151  {
152  for (unsigned int j = 0; j < 3; ++j)
153  {
154  (*i) >> normal[j];
155  }
156  (*i) >> rot;
157  }
158  m_frame.FromNormal(normal);
159  m_frame.RotateOnNormal(rot);
160 }
GfxTL::VectorXD
Definition: MatrixXX.h:21
GfxTL::AABox::Max
Point & Max()
Definition: AABox.hpp:74
GfxTL::Frame
Definition: Frame.h:11
ReduceLabel
int ReduceLabel(int a, const MiscLib::Vector< std::pair< int, size_t > > &labels)
Definition: Bitmap.cpp:921
Bitmap.h
MiscLib::Vector< int >
LowStretchSphereParametrization::LowStretchSphereParametrization
LowStretchSphereParametrization(const Sphere &sphere)
Definition: LowStretchSphereParametrization.cpp:4
LowStretchSphereParametrization.h
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::SerializedSize
static size_t SerializedSize()
Definition: LowStretchSphereParametrization.cpp:107
LowStretchSphereParametrization::Deserialize
void Deserialize(std::istream *i, bool binary)
Definition: LowStretchSphereParametrization.cpp:141
max
T max(T t1, T t2)
Definition: gdiam.h:48
GfxTL::Math::Clamp
static ScalarT Clamp(ScalarT s, ScalarT bottom, ScalarT top)
Definition: MathHelper.h:36
LowStretchSphereParametrization::Shape
const Sphere & Shape() const
Definition: LowStretchSphereParametrization.h:17
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
float
#define float
Definition: 16_Level.h:22
GfxTL::AABox::Min
Point & Min()
Definition: AABox.hpp:62
angle
double angle(const Point &a, const Point &b, const Point &c)
Definition: point.hpp:100
Sphere
Definition: Sphere.h:27
min
T min(T t1, T t2)
Definition: gdiam.h:42
AssociateLabel
void AssociateLabel(int a, int b, MiscLib::Vector< std::pair< int, size_t > > *labels)
Definition: Bitmap.cpp:895
Sphere::Radius
float Radius() const
Definition: Sphere.cpp:351
GfxTL::AABox
Definition: AABox.h:18