~mht/surcut

ref: 16669cc44af1793364ac9aa8db371c55a234ff74 surcut/evaluate.cpp -rw-r--r-- 7.2 KiB
16669cc4 — Martin Hafskjold Thoresen Printing and mesh evaluation continues 1 year, 8 months ago
                                                                                
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
#include <Eigen/Core>
#include <Eigen/Geometry>
#include <igl/doublearea.h>
#include <igl/readOBJ.h>

#include <random>
#include <stdio.h>

#include "mesh.h"
#include "surcut.h"

using Eigen::AngleAxisd;
using Eigen::Matrix3d;
using Eigen::Vector3d;

double score_mesh(Mesh &, int, int);
Vector3d face_center(Mesh &, int);

Vector3d face_center(Mesh &mesh, int face) {
  Vector3d x = mesh.vertices.row(mesh.faces(face, 0));
  Vector3d y = mesh.vertices.row(mesh.faces(face, 1));
  Vector3d z = mesh.vertices.row(mesh.faces(face, 2));
  return (x + y + z) / 3.0;
}

template <typename T> T avg(std::vector<T> v, T t) {
  for (T e : v) {
    t += e;
  }
  t /= v.size();
  return t;
}

// Give a score to the mesh, to estimate how good it is for molding with stitching
double score_mesh(Mesh &mesh, int iterations, int range) {
  Eigen::VectorXd face_areas;
  {
    igl::doublearea(mesh.vertices, mesh.faces, face_areas);
    face_areas /= 2.0;
  }
  double total_area = 0.0;
  for (int i = 0; i < face_areas.rows(); i++)
    total_area += face_areas(i);

  /* Even consider adaptive sampling? */
  std::mt19937 gen(0);
  std::uniform_int_distribution<int> rand_positive(0, range);
  std::uniform_int_distribution<int> rand_centered(-range / 2, range / 2);

  MatrixXd random_directions(iterations, 3);
  // Sample directions deterministically (since we're using OpenMP in the next `for` loop)
  for (int i = 0; i < iterations; i++) {
    // See:
    // https://math.stackexchange.com/questions/44689/how-to-find-a-random-axis-or-unit-vector-in-3d
    double z = ((double) rand_centered(gen)) * 2.0 / (double) range;
    double theta = ((double) rand_positive(gen)) / (double) range * M_PI;
    double s = sqrt(1.0 - z * z);
    random_directions(i, 0) = s * cos(theta);
    random_directions(i, 1) = s * sin(theta);
    random_directions(i, 2) = z;
    random_directions.row(i) = random_directions.row(i).normalized();
  }

  double best_score = 0.0;
  Vector3d best_dir;
  Vector3d best_plane_point;

#pragma omp parallel for
  for (int i = 0; i < iterations; i++) { /* O(I) */
    double score = 0;

    Vector3d dir = random_directions.row(i);

    Vector3d plane_point;
    { /* Find the point on which the cut plane is */
      std::vector<Vector3d> projecteds;
      std::vector<double> dots;
      for (int f = 0; f < mesh.faces.rows(); f++) { /* O(F) */
        Vector3d normal = mesh.normals.row(f);
        if (normal.dot(dir) >= 0.0) {
          Vector3d center = face_center(mesh, f);
          double dot = center.dot(dir);
          Vector3d projected = dir * dot;
          dots.push_back(dot);
          projecteds.push_back(center - projected);
        }
      }
      double avg_dot = avg(dots, 0.0);
      plane_point = avg(projecteds, Vector3d(0.0, 0.0, 0.0)) + avg_dot * dir;
    }

    double moldable_area = 0.0;
    for (int f = 0; f < mesh.faces.rows(); f++) { /* O(F^2) */
      Vector3d normal = mesh.normals.row(f);
      Vector3d center = face_center(mesh, f);

      Ray ray;
      // Move 1% away from the face to avoid intersecting with triangle boundaries
      ray.src = center + normal * 0.01 * sqrt(face_areas(f));
      if ((center - plane_point).dot(dir) >= 0.0) {
        ray.dir = dir;
      } else {
        ray.dir = -dir;
      }

      double dot = normal.dot(ray.dir);
      if (dot >= -0.0001) {
        // It can be molded. But is it obstructed? We approximate this by shooting a ray from the
        // triangle center.
        for (int other_f = 0; other_f < mesh.faces.rows(); other_f++) { /* O(F) */
          if (other_f == f) continue;
          Vector3d other_normal = mesh.normals.row(other_f);
          // If we're going along its face, don't check ;) 
          if (abs(ray.dir.dot(other_normal)) < 0.1) {
            continue;
          }
          Vector3d _intersection_point;
          if (intersect3D_RayTriangle(ray, other_normal, other_f, mesh.vertices, mesh.faces, 
                &_intersection_point) == 1) {
            goto obstructed;
          }
        }
        moldable_area += face_areas(f);
obstructed:
        (void)0;
      }
    }
    score += moldable_area / total_area;

    if (score > best_score) {
      best_score = score;
      best_dir = dir;
      best_plane_point = plane_point;
    }
    printf("  %4.2lf  ", score);
    printf("[%7.2lf, %7.2lf, %7.2lf]  ", plane_point(0), plane_point(1), plane_point(2));
    printf("[%7.2lf, %7.2lf, %7.2lf]\n", dir(0), dir(1), dir(2));
  }
  printf("      score:  %4.2lf\n", best_score);
  printf("plane point: [%7.2lf, %7.2lf, %7.2lf]\n", best_plane_point(0), best_plane_point(1),
         best_plane_point(2));
  printf("  direction: [%7.2lf, %7.2lf, %7.2lf]\n", best_dir(0), best_dir(1), best_dir(2));

  return best_score;
}

int main(int argc, char *argv[]) {
  int iterations = 100;
  int range = 100;
  char *filename = nullptr;
  for (int i = 1; i < argc; i++) {
    char *arg = argv[i];
    if (arg[0] == '-') {
      switch (arg[1]) {
      case 'i': {
        if (arg[2] != 0) {
          int res = sscanf(arg + 2, "%d", &iterations);
          if (res < 1) {
            fprintf(stderr, "failed to read number '%s'\n", arg + 2);
            return 1;
          }
        } else {
          if (i + 1 == argc) {
            fprintf(stderr, "Ran out of arguments! Missing number to -i\n");
          }
          char *arg = argv[++i];
          int res = sscanf(arg, "%d", &iterations);
          if (res < 1) {
            fprintf(stderr, "failed to read number '%s'\n", arg);
            return 1;
          }
        }
      } break;
      case 'r': {
        if (arg[2] != 0) {
          int res = sscanf(arg + 2, "%d", &range);
          if (res < 1) {
            fprintf(stderr, "failed to read number '%s'\n", arg + 2);
            return 1;
          }
        } else {
          if (i + 1 == argc) {
            fprintf(stderr, "Ran out of arguments! Missing number to -i\n");
          }
          char *arg = argv[++i];
          int res = sscanf(arg, "%d", &range);
          if (res < 1) {
            fprintf(stderr, "failed to read number '%s'\n", arg);
            return 1;
          }
        }
      } break;
      default: {
        fprintf(stderr, "Unknown argument '-%c'\n", arg[1]);
        return 1;
      }
      }
    } else {
      filename = arg;
    }
  }

  TetmeshOut tetmesh_out;
  {
    MatrixXd read_verts;
    MatrixXi read_faces;
    igl::readOBJ(filename, read_verts, read_faces);

    TetmeshIn tetmesh_in;
    tetmesh_in.vertices = &read_verts;
    tetmesh_in.faces = &read_faces;

    int error = make_tetmesh(tetmesh_in, tetmesh_out);
    if (error) {

      fprintf(stderr, "tetmesh returned error: %d\n", error);
      return 1;
    }
  }

  Mesh mesh(tetmesh_out);
  mesh.boundary = VectorXi(4);
  mesh.boundary << 0, 1, 2, 3;

  arap_data.faces = &mesh.faces;
  arap_data.normals = &mesh.normals;
  arap_data.mht_tf2t = mesh.face2tets;
  arap_data.mht_plane_p = &plane_p;
  arap_data.mht_plane_n = &plane_n;
  arap_data.vert_is_above = &is_point_above;
  arap_data.moldable = &moldable;
  arap_data.orientation = &orientation;
  arap_data.U0 = mesh.vertices;
  arap_data.with_dynamics = true;
  // XXX(todo): what's a reasonable value here?
  arap_data.h = 0.01;
  arap::precomputation(mesh, arap_data);
  compute_things(mesh);

  score_mesh(mesh, iterations, range);

  return 0;
}