File: marching_cubes.h

package info (click to toggle)
pymol 3.1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 74,084 kB
  • sloc: cpp: 482,660; python: 89,328; ansic: 29,512; javascript: 6,792; sh: 84; makefile: 25
file content (56 lines) | stat: -rw-r--r-- 1,362 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
/**
 * @file Marching cubes
 *
 * Based on https://github.com/ilastik/marching_cubes
 *
 * License: BSD-3-Clause
 *
 * Copyright 2018, The ilastik development team
 * Copyright 2020, Schrodinger, Inc.
 */

#ifndef MARCHING_CUBES_H
#define MARCHING_CUBES_H

#include <memory>

namespace mc
{

struct Point {
  float x, y, z;
  float operator[](size_t pos) const { return (&x)[pos]; }
  float& operator[](size_t pos) { return (&x)[pos]; }
};

class Field
{
public:
  virtual ~Field() = default;
  virtual size_t xDim() const = 0;
  virtual size_t yDim() const = 0;
  virtual size_t zDim() const = 0;
  virtual float get(size_t x, size_t y, size_t z) const = 0;
  virtual Point get_point(size_t x, size_t y, size_t z) const = 0;
  Point get_gradient(size_t x, size_t y, size_t z) const;
};

/**
 * Isosurface mesh with faces, vertices and normals
 */
struct Mesh {
  size_t vertexCount = 0;            //!< the number of vertices
  std::unique_ptr<Point[]> vertices; //!< vertex positions
  std::unique_ptr<Point[]> normals;  //!< normal direction of each vertex

  size_t faceCount = 0; //!< the number of faces
  std::unique_ptr<size_t[]>
      faces; //!< the faces given by 3 vertex indices (length = faceCount * 3)
};

Mesh march(const Field& volume, float isoLevel, bool gradient_normals = true);

void calculateNormals(Mesh& mesh);

} // namespace mc
#endif