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