LowStretchTorusParametrization.cpp
Go to the documentation of this file.
2 #include "Bitmap.h"
3 
5  const Torus& torus)
6  : m_torus(&torus)
7 {
8  m_hcs.FromNormal(m_torus->AxisDirection());
9  m_minorFrame.Canonical();
10 }
11 
13 {
14  m_torus = &torus;
15  m_hcs.FromNormal(m_torus->AxisDirection());
16  m_minorFrame.Canonical();
17 }
18 
20  const GfxTL::AABox< GfxTL::Vector2Df >& bbox, float epsilon,
21  size_t uextent, size_t vextent, MiscLib::Vector< int >* componentImg,
22  MiscLib::Vector< std::pair< int, size_t > >* labels) const
23 {
24  // there are wraps along both u and v direction
25  // the first pixel of each column/row is copied to the last element
26  // and components are merged accordingly
27 
28  // relabel the components
29  MiscLib::Vector< std::pair< int, size_t > > tempLabels(*labels);
30 
31  // first wrap along v (minor radius)
32  // but only if necessary
33  if (bbox.Max()[1] - bbox.Min()[1]
34  >= 2 * M_PI * m_torus->MinorRadius() - 2 * epsilon)
35  {
36  for (size_t u = 0; u < uextent; ++u)
37  {
38  if (!(*componentImg)[u])
39  {
40  continue;
41  }
42  // check the three neighbors on the other side
43  if ((*componentImg)[(vextent - 1) * uextent + u])
44  AssociateLabel((*componentImg)[u],
45  (*componentImg)[(vextent - 1) * uextent + u], &tempLabels);
46  if (u > 0 && u < uextent - 1 && (*componentImg)[(vextent - 1) * uextent + u + 1])
47  AssociateLabel((*componentImg)[u],
48  (*componentImg)[(vextent - 1) * uextent + u + 1], &tempLabels);
49  if (u > 0 && (*componentImg)[(vextent - 1) * uextent + u - 1])
50  AssociateLabel((*componentImg)[u],
51  (*componentImg)[(vextent - 1) * uextent + u - 1], &tempLabels);
52  }
53  }
54 
55  // wrap along u (major radius)
56  float minorRot = MinorFrameRotation(); // get the rotation of the minor frame
57  float ustartPrev, uendPrev, ustart = 0, uend = 0, ustartNext = 0, uendNext = 0;
58  size_t usPrev, uePrev, us = 0, ue = 0, usNext = 0, ueNext = 0;
59  float vangle = (bbox.Min()[1] + .5f * epsilon) / m_torus->MinorRadius();
60  float majorRadius = m_torus->MajorRadius()
61  + std::cos(vangle + minorRot) * m_torus->MinorRadius();
62  ustartNext = float(-M_PI) * majorRadius;
63  uendNext = float(M_PI) * majorRadius;
64  usNext = std::min(uextent - 1, (size_t)std::max((intptr_t)0,
65  (intptr_t)((ustartNext - bbox.Min()[0]) / epsilon)));
66  ueNext = (size_t)std::max((intptr_t)0, std::min((intptr_t)uextent - 1,
67  (intptr_t)((uendNext - bbox.Min()[0]) / epsilon)));
68  for (size_t v = 0; v < vextent; ++v)
69  {
70  ustartPrev = ustart;
71  ustart = ustartNext;
72  uendPrev = uend;
73  uend = uendNext;
74  usPrev = us;
75  us = usNext;
76  uePrev = ue;
77  ue = ueNext;
78  // determine starting position in the next column
79  if (v < vextent - 1)
80  {
81  float vangle = ((v + 1.5f) * epsilon + bbox.Min()[1]) / m_torus->MinorRadius();
82  float majorRadius = m_torus->MajorRadius()
83  + std::cos(vangle + minorRot) * m_torus->MinorRadius();
84  ustartNext = float(-M_PI) * majorRadius;
85  uendNext = float(M_PI) * majorRadius;
86  usNext = std::min(uextent - 1, (size_t)std::max((intptr_t)0,
87  (intptr_t)((ustartNext - bbox.Min()[0]) / epsilon)));
88  ueNext = (size_t)std::max((intptr_t)0, std::min((intptr_t)uextent - 1,
89  (intptr_t)((uendNext - bbox.Min()[0]) / epsilon)));
90  }
91  if (ustart <= bbox.Min()[0] - epsilon
92  || uend >= bbox.Max()[0] + epsilon
93  || !(*componentImg)[v * uextent + us])
94  {
95  continue;
96  }
97  // check the three neighbors on the other side
98  if ((*componentImg)[v * uextent + ue])
99  AssociateLabel((*componentImg)[v * uextent + us],
100  (*componentImg)[v * uextent + ue], &tempLabels);
101  if (v > 0
102  && ustartPrev > bbox.Min()[0] - epsilon
103  && uendPrev < bbox.Min()[0] + epsilon
104  && (*componentImg)[(v - 1) * uextent + uePrev])
105  AssociateLabel((*componentImg)[v * uextent + us],
106  (*componentImg)[(v - 1) * uextent + uePrev], &tempLabels);
107  if (v < vextent - 1
108  && ustartNext > bbox.Min()[0] - epsilon
109  && uendNext < bbox.Min()[0] + epsilon
110  && (*componentImg)[(v + 1) * uextent + ueNext])
111  AssociateLabel((*componentImg)[v * uextent + us],
112  (*componentImg)[(v + 1) * uextent + ueNext], &tempLabels);
113  }
114 
115  // condense labels
116  for (size_t i = tempLabels.size() - 1; i > 0; --i)
117  {
118  tempLabels[i].first = ReduceLabel(i, tempLabels);
119  }
120  MiscLib::Vector< int > condensed(tempLabels.size());
121  labels->clear();
122  labels->reserve(condensed.size());
123  int count = 0;
124  for (size_t i = 0; i < tempLabels.size(); ++i)
125  if (i == tempLabels[i].first)
126  {
127  labels->push_back(std::make_pair(count, tempLabels[i].second));
128  condensed[i] = count;
129  ++count;
130  }
131  else
132  (*labels)[condensed[tempLabels[i].first]].second
133  += tempLabels[i].second;
134  // set new component ids
135  for (size_t i = 0; i < componentImg->size(); ++i)
136  (*componentImg)[i] =
137  condensed[tempLabels[(*componentImg)[i]].first];
138 }
139 
141 {
142  return 2 * sizeof(float);
143 }
144 
145 void LowStretchTorusParametrization::Serialize(std::ostream* o, bool binary) const
146 {
147  float rot = 0;
148  if (binary)
149  {
150  rot = MajorFrameRotation();
151  o->write((char*)&rot, sizeof(rot));
152  rot = MinorFrameRotation();
153  o->write((char*)&rot, sizeof(rot));
154  }
155  else
156  {
157  *o << MajorFrameRotation() << " " << MinorFrameRotation() << " ";
158  }
159 }
160 
161 void LowStretchTorusParametrization::Deserialize(std::istream* i, bool binary)
162 {
163  float majorRot, minorRot;
164  if (binary)
165  {
166  i->read((char*)&majorRot, sizeof(majorRot));
167  i->read((char*)&minorRot, sizeof(minorRot));
168  }
169  else
170  {
171  *i >> majorRot >> minorRot;
172  }
174  nframe.FromNormal(m_torus->AxisDirection());
175  nframe.RotateOnNormal(majorRot);
176  m_hcs[0] = nframe[0];
177  m_hcs[1] = nframe[1];
178  m_minorFrame.Canonical();
179  m_minorFrame.RotateFrame(minorRot);
180 }
181 
182 float LowStretchTorusParametrization::MinorFrameRotation() const
183 {
184  return std::atan2(GfxTL::Math< float >::Clamp(m_minorFrame[0][1], -1, 1),
185  GfxTL::Math< float >::Clamp(m_minorFrame[0][0], -1, 1));
186 }
187 
188 float LowStretchTorusParametrization::MajorFrameRotation() const
189 {
190  // defer rotation from frame
192  nframe.FromNormal(m_torus->AxisDirection());
194  nframe.ToTangent(m_hcs[0], &t);
195  for (unsigned int i = 0; i < 2; ++i)
196  {
197  t[i] = GfxTL::Math< float >::Clamp(t[i], -1, 1);
198  }
199  return std::atan2(t[1], t[0]);
200 }
GfxTL::VectorXD
Definition: MatrixXX.h:21
GfxTL::AABox::Max
Point & Max()
Definition: AABox.hpp:74
GfxTL::Frame< 3, float >
ReduceLabel
int ReduceLabel(int a, const MiscLib::Vector< std::pair< int, size_t > > &labels)
Definition: Bitmap.cpp:921
Torus::MinorRadius
const float MinorRadius() const
Definition: Torus.h:42
LowStretchTorusParametrization::LowStretchTorusParametrization
LowStretchTorusParametrization(const Torus &torus)
Definition: LowStretchTorusParametrization.cpp:4
Torus
Definition: Torus.h:18
Bitmap.h
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
MiscLib::Vector< int >
LowStretchTorusParametrization::Deserialize
void Deserialize(std::istream *i, bool binary)
Definition: LowStretchTorusParametrization.cpp:161
Torus::AxisDirection
const Vec3f & AxisDirection() const
Definition: Torus.h:38
MiscLib::Vector::size
size_type size() const
Definition: Vector.h:212
M_PI
#define M_PI
Definition: MathTools.h:17
max
T max(T t1, T t2)
Definition: gdiam.h:48
GfxTL::Math
Definition: MathHelper.h:11
GfxTL::Math::Clamp
static ScalarT Clamp(ScalarT s, ScalarT bottom, ScalarT top)
Definition: MathHelper.h:36
LowStretchTorusParametrization::Serialize
void Serialize(std::ostream *o, bool binary) const
Definition: LowStretchTorusParametrization.cpp:145
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
GfxTL::AABox::Min
Point & Min()
Definition: AABox.hpp:62
Torus::MajorRadius
const float MajorRadius() const
Definition: Torus.h:46
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
GfxTL::AABox
Definition: AABox.h:18
LowStretchTorusParametrization.h