File: testing_diff_mesh.cpp

package info (click to toggle)
mrtrix3 3.0~rc3%2Bgit135-g2b8e7d0c2-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 34,548 kB
  • sloc: cpp: 117,101; python: 6,472; sh: 638; makefile: 231; xml: 39; ansic: 20
file content (143 lines) | stat: -rw-r--r-- 4,628 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
/*
 * Copyright (c) 2008-2018 the MRtrix3 contributors.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, you can obtain one at http://mozilla.org/MPL/2.0/
 *
 * MRtrix3 is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * For more details, see http://www.mrtrix.org/
 */


#include "command.h"
#include "progressbar.h"

#include "types.h"
#include "surface/mesh.h"
#include "surface/mesh_multi.h"

using namespace MR;
using namespace App;

void usage ()
{
  AUTHOR = "Robert E. Smith (robert.smith@florey.edu.au)";

  SYNOPSIS = "Compare two mesh files for differences, within specified tolerance";

  DESCRIPTION
  + "Note that vertex normals are currently not tested.";

  ARGUMENTS
  + Argument ("in1", "a mesh file.").type_file_in ()
  + Argument ("in2", "another mesh file.").type_file_in ()
  + Argument ("tolerance", "the maximum distance to consider acceptable").type_float (0.0);
}



void run ()
{
  const default_type dist_sq = Math::pow2 (default_type(argument[2]));

  MR::Surface::MeshMulti multi_in1, multi_in2;

  // Read in the mesh data
  try {
    MR::Surface::Mesh mesh (argument[0]);
    multi_in1.push_back (mesh);
  } catch (...) {
    multi_in1.load (argument[0]);
  }
  
  try {
    MR::Surface::Mesh mesh (argument[1]);
    multi_in2.push_back (mesh);
  } catch (...) {
    multi_in2.load (argument[1]);
  }
  
  if (multi_in1.size() != multi_in2.size())
    throw Exception ("Mismatched number of mesh objects (" + str(multi_in1.size()) + " - " + str(multi_in2.size()) + ") - test FAILED");
  
  for (size_t mesh_index = 0; mesh_index != multi_in1.size(); ++mesh_index) {
  
    const MR::Surface::Mesh& in1 (multi_in1[mesh_index]);
    const MR::Surface::Mesh& in2 (multi_in2[mesh_index]);

  // Can't test this: Some formats have to duplicate the vertex positions
  //if (in1.num_vertices() != in2.num_vertices())
  //  throw Exception ("Mismatched vertex count (" + str(in1.num_vertices()) + " - " + str(in2.num_vertices()) + ") - test FAILED");
  if (in1.num_triangles() != in2.num_triangles())
    throw Exception ("Mismatched triangle count (" + str(in1.num_triangles()) + " - " + str(in2.num_triangles()) + ") - test FAILED");
  if (in1.num_quads() != in2.num_quads())
    throw Exception ("Mismatched quad count (" + str(in1.num_quads()) + " - " + str(in2.num_quads()) + ") - test FAILED");

  // For every triangle and quad in one file, there must be a matching triangle/quad in the other
  // Can't rely on a preserved order; need to look through the entire list for a triangle/quad for
  //   which every vertex has a corresponding vertex

  for (size_t i = 0; i != in1.num_triangles(); ++i) {
    // Explicitly load the vertex data
    std::array<Eigen::Vector3, 3> v1;
    for (size_t vertex = 0; vertex != 3; ++vertex)
      v1[vertex] = in1.vert(in1.tri(i)[vertex]);
    bool match_found = false;
    for (size_t j = 0; j != in2.num_triangles() && !match_found; ++j) {
      std::array<Eigen::Vector3, 3> v2;
      for (size_t vertex = 0; vertex != 3; ++vertex)
        v2[vertex] = in2.vert (in2.tri(j)[vertex]);
      bool all_vertices_matched = true;
      for (size_t a = 0; a != 3; ++a) {
        size_t b = 0;
        for (; b != 3; ++b) {
          if ((v1[a]-v2[b]).squaredNorm() < dist_sq)
            break;

        }
        if (b == 3)
          all_vertices_matched = false;
      }
      if (all_vertices_matched)
        match_found = true;
    }
    if (!match_found)
      throw Exception ("Unmatched triangle - test FAILED");
  }

  for (size_t i = 0; i != in1.num_quads(); ++i) {
    std::array<Eigen::Vector3, 4> v1;
    for (size_t vertex = 0; vertex != 4; ++vertex)
      v1[vertex] = in1.vert (in1.quad(i)[vertex]);
    bool match_found = false;
    for (size_t j = 0; j != in2.num_quads() && !match_found; ++j) {
      std::array<Eigen::Vector3, 4> v2;
      for (size_t vertex = 0; vertex != 4; ++vertex)
        v2[vertex] = in2.vert (in2.quad(j)[vertex]);
      bool all_vertices_matched = true;
      for (size_t a = 0; a != 4; ++a) {
        size_t b = 0;
        for (; b != 4; ++b) {
          if ((v1[a]-v2[b]).squaredNorm() < dist_sq)
            break;

        }
        if (b == 4)
          all_vertices_matched = false;
      }
      if (all_vertices_matched)
        match_found = true;
    }
    if (!match_found)
      throw Exception ("Unmatched quad - test FAILED");
  }

  }

  CONSOLE ("data checked OK");
}