File: contouring_octree.cpp

package info (click to toggle)
cgal 6.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 144,952 kB
  • sloc: cpp: 811,597; ansic: 208,576; sh: 493; python: 411; makefile: 286; javascript: 174
file content (288 lines) | stat: -rw-r--r-- 10,704 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include <CGAL/Simple_cartesian.h>

#include <CGAL/Isosurfacing_3/dual_contouring_3.h>
#include <CGAL/Isosurfacing_3/Dual_contouring_domain_3.h>
#include <CGAL/Isosurfacing_3/marching_cubes_3.h>
#include <CGAL/Isosurfacing_3/Marching_cubes_domain_3.h>
#include <CGAL/Isosurfacing_3/Value_function_3.h>
#include <CGAL/Isosurfacing_3/Gradient_function_3.h>
#include <CGAL/Isosurfacing_3/Octree_partition.h>

#include <CGAL/Bbox_3.h>
#include <CGAL/IO/polygon_soup_io.h>

#include <cmath>
#include <iostream>
#include <vector>

using Kernel = CGAL::Simple_cartesian<double>;
using FT = typename Kernel::FT;
using Vector = typename Kernel::Vector_3;
using Point = typename Kernel::Point_3;

using Point_range = std::vector<Point>;
using Polygon_range = std::vector<std::vector<std::size_t> >;

using Octree = CGAL::Octree<Kernel, std::vector<Point> >;
using Values = CGAL::Isosurfacing::Value_function_3<Octree>;
using Gradients = CGAL::Isosurfacing::Gradient_function_3<Octree>;
using MC_Domain = CGAL::Isosurfacing::Marching_cubes_domain_3<Octree, Values>;
using Domain = CGAL::Isosurfacing::Dual_contouring_domain_3<Octree, Values, Gradients>;

namespace IS = CGAL::Isosurfacing;

auto sphere_function = [](const Point& p) -> FT
{
  return std::sqrt(p.x()*p.x() + p.y()*p.y() + p.z()*p.z());
};

auto sphere_gradient = [](const Point& p) -> Vector
{
  const Vector g = p - CGAL::ORIGIN;
  return g / std::sqrt(g.squared_length());
};

auto blobby_function = [](const Point& p) -> FT
{
  return std::exp(-1.5 * ((p.x() - 0.2) * (p.x() - 0.2) + (p.y() - 0.2) * (p.y() - 0.2) + (p.z() - 0.2) * (p.z() - 0.2))) +
         std::exp(-1.5 * ((p.x() + 0.2) * (p.x() + 0.2) + (p.y() + 0.2) * (p.y() + 0.2) + (p.z() + 0.2) * (p.z() + 0.2))) +
         std::exp(-1.5 * ((p.x() - 0.4) * (p.x() - 0.4) + (p.y() + 0.4) * (p.y() + 0.4) + (p.z() - 0.4) * (p.z() - 0.4))) +
         std::exp(-6 * ((p.x() - 0.1) * (p.x() - 0.1) + (p.y() - 0.1) * (p.y() - 0.1))) +
         std::exp(-6 * ((p.y() + 0.1) * (p.y() + 0.1) + (p.z() + 0.1) * (p.z() + 0.1))) +
         std::exp(-6 * ((p.x() + 0.1) * (p.x() + 0.1) + (p.z() - 0.1) * (p.z() - 0.1))) -
         0.3;
};

auto blobby_gradient = [](const Point& p) -> Vector
{
  const FT g1 = -3 * std::exp(-1.5 * ((p.x() - 0.2) * (p.x() - 0.2) + (p.y() - 0.2) * (p.y() - 0.2) + (p.z() - 0.2) * (p.z() - 0.2)));
  const FT g2 = -3 * std::exp(-1.5 * ((p.x() + 0.2) * (p.x() + 0.2) + (p.y() + 0.2) * (p.y() + 0.2) + (p.z() + 0.2) * (p.z() + 0.2)));
  const FT g3 = -3 * std::exp(-1.5 * ((p.x() - 0.4) * (p.x() - 0.4) + (p.y() + 0.4) * (p.y() + 0.4) + (p.z() - 0.4) * (p.z() - 0.4)));
  const FT g4 = -12 * std::exp(-6 * ((p.x() - 0.1) * (p.x() - 0.1) + (p.y() - 0.1) * (p.y() - 0.1)));
  const FT g5 = -12 * std::exp(-6 * ((p.y() + 0.1) * (p.y() + 0.1) + (p.z() + 0.1) * (p.z() + 0.1)));
  const FT g6 = -12 * std::exp(-6 * ((p.x() + 0.1) * (p.x() + 0.1) + (p.z() - 0.1) * (p.z() - 0.1)));

  return Vector(g1 * (p.x() - 0.2) + g2 * (p.x() + 0.2) + g3 * (p.x() - 0.4) + g4 * (p.x() - 0.1) + g6 * (p.x() + 0.1),
                g1 * (p.y() - 0.2) + g2 * (p.y() + 0.2) + g3 * (p.y() + 0.4) + g4 * (p.y() - 0.1) + g5 * (p.y() + 0.1),
                g1 * (p.z() - 0.2) + g2 * (p.z() + 0.2) + g3 * (p.z() - 0.4) + g5 * (p.z() + 0.1) + g6 * (p.z() - 0.1));
};

// This is a naive refinement that is adapted to the isosurface:
// This refines:
// - at the minimum till minimum depth
// - at the maximum till maximum depth
// - we split if the the isovalue goes through the voxel, i.e. if not all vertices of the cell
//   are on the same side of the isosurface defined by a function
// It's not a great refinement technique because the surface can enter and leave a cell
// without involving the cell's vertex. In practice, that means a hole if at nearby adjacent
// cells the voxels did get refined and registered the surface.
struct Refine_around_isovalue
{
  std::size_t min_depth_;
  std::size_t max_depth_;
  std::function<FT(const Point&)> function_;
  FT isovalue_;

  Refine_around_isovalue(std::size_t min_depth,
                         std::size_t max_depth,
                         std::function<FT(const Point&)> function,
                         FT isovalue)
    : min_depth_(min_depth),
      max_depth_(max_depth),
      function_(function),
      isovalue_(isovalue)
  {}

  bool operator()(const Octree::Node_index& ni, const Octree& octree) const
  {
    // Ensure minimum depth refinement
    if (octree.depth(ni) < min_depth_)
      return true;

    // Stop refinement at maximum depth
    if (octree.depth(ni) >= max_depth_)
      return false;

    // Get the bounding box of the node
    auto bbox = octree.bbox(ni);

    // Evaluate the function at the corners of the bounding box
    std::array<FT, 8> corner_values;
    int index = 0;
    for (FT x : {bbox.xmin(), bbox.xmax()})
      for (FT y : {bbox.ymin(), bbox.ymax()})
        for (FT z : {bbox.zmin(), bbox.zmax()})
          corner_values[index++] = function_(Point(x, y, z));

    // Check if the function values cross the isovalue
    bool has_positive = false, has_negative = false;
    for (const auto& value : corner_values)
    {
      if (value > isovalue_)
        has_positive = true;
      if (value < isovalue_)
        has_negative = true;
      if (has_positive && has_negative)
        return true; // Refine if the isosurface intersects the voxel
    }

    return false; // No refinement needed
  }
};

// This is a refinement that is NOT adapted to the isosurface
struct Refine_one_eighth
{
  std::size_t min_depth_;
  std::size_t max_depth_;

  std::size_t octree_dim_;

  Refine_one_eighth(std::size_t min_depth,
                    std::size_t max_depth)
    : min_depth_(min_depth),
      max_depth_(max_depth)
  {
    octree_dim_ = std::size_t(1) << max_depth_;
  }

  Octree::Global_coordinates uniform_coordinates(const Octree::Node_index& node_index, const Octree& octree) const
  {
    auto coords = octree.global_coordinates(node_index);
    const std::size_t depth_factor = std::size_t(1) << (max_depth_ - octree.depth(node_index));
    for(int i=0; i < 3; ++i)
      coords[i] *= uint32_t(depth_factor);

    return coords;
  }

  bool operator()(const Octree::Node_index& ni, const Octree& octree) const
  {
    if(octree.depth(ni) < min_depth_)
      return true;

    if(octree.depth(ni) == max_depth_)
      return false;

    auto leaf_coords = uniform_coordinates(ni, octree);

    if(leaf_coords[0] >= octree_dim_ / 2)
      return false;

    if(leaf_coords[1] >= octree_dim_ / 2)
      return false;

    if(leaf_coords[2] >= octree_dim_ / 2)
      return false;

    return true;
  }
};

template <typename Splitter>
void run_DC_octree(const CGAL::Bbox_3 bbox,
                   const Splitter& split_predicate,
                   const std::function<FT(const Point&)> function,
                   const std::function<Vector(const Point&)> gradient,
                   const FT isovalue,
                   const std::string& name)
{
  std::vector<Point> bbox_points { { bbox.xmin(), bbox.ymin(), bbox.zmin() },
                                   { bbox.xmax(), bbox.ymax(), bbox.zmax() } };

  Octree octree(bbox_points);
  octree.refine(split_predicate);


  auto leaf_range = octree.traverse(CGAL::Orthtrees::Leaves_traversal<Octree>(octree));
  std::size_t leaf_counter = std::distance(leaf_range.begin(), leaf_range.end());

  std::cout << "octree has " << leaf_counter << " leaves" << std::endl;

  // fill up values and gradients
  Values values { function, octree };
  Gradients gradients { gradient, octree };
  Domain domain { octree, values, gradients };

  // output containers
  Point_range points;
  Polygon_range triangles;

  std::cout << "Running Dual Contouring with isovalue = " << isovalue << std::endl;

  // run Dual Contouring
  IS::dual_contouring<CGAL::Parallel_if_available_tag>(domain, isovalue, points, triangles,
                                                       CGAL::parameters::do_not_triangulate_faces(true)
                                                                        .constrain_to_cell(false));

  std::cout << "Output #vertices (DC): " << points.size() << std::endl;
  std::cout << "Output #triangles (DC): " << triangles.size() << std::endl;

  std::ofstream oo("octree_DC_" + name + ".polylines.txt");
  oo.precision(17);
  octree.dump_to_polylines(oo);

  CGAL::IO::write_polygon_soup("DC_" + name + ".off", points, triangles);
}

template <typename Splitter>
void run_MC_octree(const CGAL::Bbox_3 bbox,
                   const Splitter& split_predicate,
                   const std::function<FT(const Point&)> function,
                   const FT isovalue,
                   const std::string& name)
{
  std::vector<Point> bbox_points { { bbox.xmin(), bbox.ymin(), bbox.zmin() },
  { bbox.xmax(), bbox.ymax(), bbox.zmax() } };

  Octree octree(bbox_points);
  octree.refine(split_predicate);

  Values values { function, octree };

  Point_range points;
  Polygon_range triangles;
  MC_Domain mcdomain { octree, values };

  std::cout << "Running MC" << std::endl;

  CGAL::Isosurfacing::marching_cubes<CGAL::Parallel_if_available_tag>(mcdomain, isovalue, points, triangles);

  std::cout << "Output #vertices (MC): " << points.size() << std::endl;
  std::cout << "Output #triangles (MC): " << triangles.size() << std::endl;

  std::ofstream oo("octree_MC_" + name + ".polylines.txt");
  oo.precision(17);
  octree.dump_to_polylines(oo);

  CGAL::IO::write_polygon_soup("MC_" + name + ".off", points, triangles);
}

// Whether you are using MC, TMC, or DC, there is no guarantee for an octree:
// it should behave well if your nodes are split with a uniform size around the surface,
// but it is sure to produce cracks if you have varying depths around the isosurface.
int main(int argc, char** argv)
{
  const FT isovalue = (argc > 1) ? std::stod(argv[1]) : 0.3;

  const CGAL::Bbox_3 bbox { -1., -1., -1.,  1., 1., 1. };

  Refine_one_eighth one_eight_splitter(3, 5);
  run_DC_octree(bbox, one_eight_splitter, sphere_function, sphere_gradient, isovalue, "one_eight");

  // This is
  Refine_around_isovalue isovalue_splitter(3, 5, sphere_function, isovalue);
  run_DC_octree(bbox, isovalue_splitter, sphere_function, sphere_gradient, isovalue, "sphere_adapted");

  Refine_around_isovalue isvalue_splitter_2(3, 5, blobby_function, isovalue);
  run_DC_octree(bbox, isvalue_splitter_2, blobby_function, blobby_gradient, isovalue, "blobby_adapted");

  // to illustrate cracks
  run_MC_octree(bbox, isovalue_splitter, sphere_function, isovalue, "adapted");

  run_MC_octree(bbox, one_eight_splitter, sphere_function, isovalue, "one_eight");

  std::cout << "Done" << std::endl;

  return EXIT_SUCCESS;
}