Mitsuba Renderer  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sfcurve.h
Go to the documentation of this file.
1 /*
2  This file is part of Mitsuba, a physically based rendering system.
3 
4  Copyright (c) 2007-2014 by Wenzel Jakob and others.
5 
6  Mitsuba is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License Version 3
8  as published by the Free Software Foundation.
9 
10  Mitsuba is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #if !defined(__MITSUBA_CORE_SFCURVE_H_)
21 #define __MITSUBA_CORE_SFCURVE_H_
22 
23 #include <mitsuba/core/bsphere.h>
24 
26 
27 /**
28  * \brief 2D version of the Hilbert space-filling curve
29  *
30  * Based on http://voxelizator3d.wordpress.com/
31  *
32  * \ingroup libcore
33  */
34 template <typename T> class HilbertCurve2D {
35 public:
36  enum EDirection {
40  EWest
41  };
44 
45  /// Create an empty Hilbert curve
46  inline HilbertCurve2D() : m_size((T) 0), m_pos((T) 0) { }
47 
48  /// Initialize for the specified 2D size
49  void initialize(const VectorType &size) {
50  if (size == m_size)
51  return;
52  m_points.clear();
53  m_points.reserve(m_size.x*m_size.y);
54  m_size = size; m_pos = PointType((T) 0);
55  const Float invLog2 = (Float) 1 / math::fastlog((Float) 2);
56  generate(
57  (int) std::ceil(invLog2 * math::fastlog((Float) std::max(m_size.x, m_size.y))),
58  ENorth, EEast, ESouth, EWest);
59  }
60 
61  /// Return one of the generated points
62  inline const PointType &operator[](size_t idx) const {
63  return m_points[idx];
64  }
65 
66  /// Return a reference to the computed points
67  inline const std::vector<PointType> &getPoints() const {
68  return m_points;
69  }
70 
71  /// Return the total number of points
72  inline size_t getPointCount() const {
73  return m_points.size();
74  }
75 
76  /// Return the size of the underlying 2D rectangle
77  inline const VectorType &getSize() const {
78  return m_size;
79  }
80 protected:
81  inline void move(EDirection dir) {
82  switch (dir) {
83  case ENorth: m_pos.y--; break;
84  case EEast: m_pos.x++; break;
85  case ESouth: m_pos.y++; break;
86  case EWest: m_pos.x--; break;
87  }
88  }
89 
90  void generate(int order,
91  EDirection front, EDirection right,
92  EDirection back, EDirection left) {
93  if (order == 0) {
94  if (m_pos.x < m_size.x && m_pos.y < m_size.y)
95  m_points.push_back(m_pos);
96  } else {
97  generate(order-1, left, back, right, front); move(right);
98  generate(order-1, front, right, back, left); move(back);
99  generate(order-1, front, right, back, left); move(left);
100  generate(order-1, right, front, left, back);
101  }
102  }
103 private:
104  VectorType m_size;
105  PointType m_pos;
106  std::vector<PointType> m_points;
107 };
108 
110 
111 #endif /* __MITSUBA_CORE_SFCURVE_H_ */
float fastlog(float value)
Definition: math.h:209
const PointType & operator[](size_t idx) const
Return one of the generated points.
Definition: sfcurve.h:62
const std::vector< PointType > & getPoints() const
Return a reference to the computed points.
Definition: sfcurve.h:67
HilbertCurve2D()
Create an empty Hilbert curve.
Definition: sfcurve.h:46
void generate(int order, EDirection front, EDirection right, EDirection back, EDirection left)
Definition: sfcurve.h:90
const VectorType & getSize() const
Return the size of the underlying 2D rectangle.
Definition: sfcurve.h:77
#define MTS_NAMESPACE_BEGIN
Definition: platform.h:137
Definition: sfcurve.h:38
Definition: sfcurve.h:39
TPoint2< T > PointType
Definition: sfcurve.h:43
void initialize(const VectorType &size)
Initialize for the specified 2D size.
Definition: sfcurve.h:49
Parameterizable two-dimensional point data structure.
Definition: point.h:222
size_t getPointCount() const
Return the total number of points.
Definition: sfcurve.h:72
void move(EDirection dir)
Definition: sfcurve.h:81
Parameterizable two-dimensional vector data structure.
Definition: vector.h:246
Definition: sfcurve.h:37
TVector2< T > VectorType
Definition: sfcurve.h:42
EDirection
Definition: sfcurve.h:36
#define MTS_NAMESPACE_END
Definition: platform.h:138
2D version of the Hilbert space-filling curve
Definition: sfcurve.h:34