-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.cpp
185 lines (164 loc) · 6.25 KB
/
main.cpp
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
/*
* Copyright (c) 2012-2014, Bruno Levy
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of the ALICE Project-Team nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* If you modify this software, you should include a notice giving the
* name of the person performing the modification, the date of modification,
* and the reason for such modification.
*
* Contact: Bruno Levy
*
* http://www.loria.fr/~levy
*
* ALICE Project
* LORIA, INRIA Lorraine,
* Campus Scientifique, BP 239
* 54506 VANDOEUVRE LES NANCY CEDEX
* FRANCE
*
*/
#include <geogram/basic/command_line.h>
#include <geogram/basic/command_line_args.h>
#include <geogram/basic/logger.h>
#include <geogram/basic/progress.h>
#include <geogram/basic/stopwatch.h>
#include <geogram/mesh/mesh.h>
#include <geogram/mesh/mesh_geometry.h>
#include <geogram/mesh/mesh_repair.h>
#include <geogram/mesh/mesh_io.h>
////////////////////////////////////////////////////////////////////////////////
// Compute center of mass of a mesh (M need not be a volumetric mesh)
GEO::vec3 centerOfMass(const GEO::Mesh &M) {
GEO::vec3 com(0, 0, 0);
GEO::vec3 t[4];
t[3] = GEO::vec3(0, 0, 0);
double volume_total = 0;
for (int f = 0; f < (int) M.facets.nb(); ++f) {
for(GEO::index_t c = M.facets.corners_begin(f), i = 0;
c < M.facets.corners_end(f); ++c, ++i)
{
geo_assert(i < 3);
t[i] = M.vertices.point(M.facet_corners.vertex(c));
}
double vol = GEO::Geom::tetra_signed_volume(t[0], t[1], t[2], t[3]);
com += vol * 0.25 * (t[0] + t[1] + t[2] + t[3]);
volume_total += vol;
}
com /= volume_total;
return com;
}
////////////////////////////////////////////////////////////////////////////////
int main(int argc, char** argv) {
// Initialize the Geogram library
GEO::initialize();
// Import standard command line arguments, and custom ones
GEO::CmdLine::import_arg_group("standard");
GEO::CmdLine::import_arg_group("pre");
GEO::CmdLine::declare_arg("extent", 1.0, "Maximum physical extent (in mm)");
GEO::CmdLine::declare_arg("com", false, "Normalize around center of mass instead of min-corner");
GEO::CmdLine::declare_arg("barycenter", false, "Normalize around barycenter instead of min-corner");
// Parse command line options and filenames
std::vector<std::string> filenames;
if (!GEO::CmdLine::parse(argc, argv, filenames, "in_mesh_file <out_mesh_file>")) {
return 1;
}
double target_extent = GEO::CmdLine::get_arg_double("extent");
bool use_com = GEO::CmdLine::get_arg_bool("com");
bool use_bary = GEO::CmdLine::get_arg_bool("barycenter");
// Default output filename is "output" if unspecified
if (filenames.size() == 1) {
filenames.push_back("output.ply");
}
// Display input and output filenames
GEO::Logger::div("Command line");
GEO::Logger::out("Normalize") << "Input file: " << filenames[0] << std::endl;
GEO::Logger::out("Normalize") << "Output file: " << filenames[1] << std::endl;
// Declare a mesh
GEO::Mesh M;
// Load the mesh and display timings
GEO::Logger::div("Loading");
{
GEO::Stopwatch W("Load");
if(!GEO::mesh_load(filenames[0], M)) {
return 1;
}
geo_assert(M.vertices.dimension() == 3);
}
// Merge identical vertices
if (GEO::CmdLine::get_arg_bool("pre:repair")) {
GEO::MeshRepairMode mode = GEO::MESH_REPAIR_COLOCATE;
double radius = bbox_diagonal(M);
double epsilon = GEO::CmdLine::get_arg_percent("pre:epsilon", radius);
mesh_repair(M, mode, epsilon);
}
// Rescale to unit box, and set min corner to 0
GEO::vec3 min_corner, max_corner;
GEO::get_bbox(M, &min_corner[0], &max_corner[0]);
GEO::vec3 extent = max_corner - min_corner;
double scaling = std::max(extent[0], std::max(extent[1], extent[2]));
GEO::vec3 origin = min_corner;
if (use_com) {
origin = centerOfMass(M);
}
if (use_bary) {
origin = 0.5 * (min_corner + max_corner);
}
for (int v = 0; v < M.vertices.nb(); ++v) {
M.vertices.point(v) = target_extent * (M.vertices.point(v) - origin) / scaling;
}
//max_corner = min_corner + 0.5 * extent;
// -------------------------------------------------------------------------
// Extract submesh
// -------------------------------------------------------------------------
// GEO::Box bbox;
// for (int i = 0; i < 3; ++i) {
// bbox.xyz_min[i] = min_corner[i];
// bbox.xyz_max[i] = max_corner[i];
// }
// GEO::vector<GEO::index_t> to_delete(M.facets.nb(), 0);
// for (int f = 0; f < M.facets.nb(); ++f) {
// for(GEO::index_t c = M.facets.corners_begin(f);
// c < M.facets.corners_end(f); ++c)
// {
// GEO::vec3 pts = M.vertices.point(M.facet_corners.vertex(c));
// if (! bbox.contains(pts)) {
// to_delete[f] = 1;
// break;
// }
// }
// }
// M.facets.delete_elements(to_delete, true);
// -------------------------------------------------------------------------
// Save mesh
GEO::Logger::div("Saving");
{
GEO::Stopwatch W("Save");
GEO::mesh_save(M, filenames[1]);
}
return 0;
}