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
|
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture, version 1.0 beta 4 *
* (c) 2006-2009 MGH, INRIA, USTL, UJF, CNRS *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU General Public License as published by the Free *
* Software Foundation; either version 2 of the License, or (at your option) *
* any later version. *
* *
* This program 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. See the GNU General Public License for *
* more details. *
* *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., 51 *
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
*******************************************************************************
* SOFA :: Applications *
* *
* Authors: M. Adam, J. Allard, B. Andre, P-J. Bensoussan, S. Cotin, C. Duriez,*
* H. Delingette, F. Falipou, F. Faure, S. Fonteneau, L. Heigeas, C. Mendoza, *
* M. Nesme, P. Neumann, J-P. de la Plata Alcade, F. Poyer and F. Roy *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
/******* COPYRIGHT ************************************************
* *
* FlowVR Render *
* Parallel Rendering Modules *
* *
*-----------------------------------------------------------------*
* COPYRIGHT (C) 2005 by *
* Laboratoire Informatique et Distribution (UMR5132) and *
* INRIA Project MOVI. ALL RIGHTS RESERVED. *
* *
* This source is covered by the GNU GPL, please refer to the *
* COPYING file for further information. *
* *
*-----------------------------------------------------------------*
* *
* Original Contributors: *
* Jeremie Allard, *
* Clement Menier. *
* *
*******************************************************************
* *
* File: ./src/utils/meshconv.cpp *
* *
* Contacts: *
* *
******************************************************************/
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <ftl/cmdline.h>
#include <flowvr/render/mesh.h>
using namespace flowvr::render;
extern void tesselateMesh(Mesh& obj, int rec=1, bool onSphere=false);
int main(int argc, char** argv)
{
bool normalize = false;
bool flip = false;
bool wnormals = false;
bool rnormals = false;
bool rtexcoords = false;
bool closemesh = false;
Vec3f translation;
Vec3f rotation;
Vec3f scale(1,1,1);
float dilate = 0.0f;
int tesselate = 0;
bool sphere = false;
bool dist = false;
int res = 16;
int rx=0,ry=0,rz=0;
float border = 0.25f;
ftl::CmdLine cmd("Usage: meshconv [options] mesh.input [mesh.output]");
cmd.opt("normalize",'n',"transform points so that the center is at <0,0,0> and the max coodinate is 1",&normalize);
cmd.opt("flip",'f',"flip normals",&flip);
cmd.opt("wnormals",'N',"force writing of normals",&wnormals);
cmd.opt("rnormals",'O',"remove normals",&rnormals);
cmd.opt("rtexcoords",'T',"remove texcoords",&rtexcoords);
cmd.opt("close",'c',"close mesh",&closemesh);
cmd.opt("translate",'t',"translate the mesh",&translation);
cmd.opt("rotate",'r',"rotate the mesh using euler angles in degree",&rotation);
cmd.opt("scale",'s',"scale the mesh using 3 coefficients",&scale);
cmd.opt("dilate",'d',"dilate (i.e. displace vertices of the given distance along their normals)",&dilate);
cmd.opt("tesselate",'a',"tesselate (split each edge in 2 resursivly n times)",&tesselate);
cmd.opt("sphere",'S',"consider the mesh as a sphere for tesselation",&sphere);
cmd.opt("dist",'D',"compute distance field",&dist);
cmd.opt("res",'R',"resolution of distance field",&res);
cmd.opt("rx",'X',"X resolution of distance field",&rx);
cmd.opt("ry",'Y',"Y resolution of distance field",&ry);
cmd.opt("rz",'Z',"Z resolution of distance field",&rz);
cmd.opt("border",'B',"distance field border size relative to the object's BBox size (or negative for exact size)",&border);
bool error=false;
if (!cmd.parse(argc,argv,&error))
return error?1:0;
if (cmd.args.size()<1 || cmd.args.size()>2)
{
std::cerr << cmd.help() << std::endl;
return 1;
}
std::string file_in = cmd.args[0];
std::string file_out;
if (cmd.args.size()>=2) file_out = cmd.args[1];
Mesh obj;
if (!obj.load(file_in.c_str()))
{
std::cerr << "Failed to read "<<file_in<<std::endl;
return 1;
}
if (normalize)
{
BBox bb = obj.calcBBox();
std::cout << "Mesh bbox="<<bb<<std::endl;
std::cout << "Normalizing mesh..."<<std::endl;
float size = 0;
for (int i=0;i<3;i++)
{
float d = bb.b[i]-bb.a[i];
if (d>size) size=d;
}
Vec3f center = (bb.a+bb.b)*0.5;
float sc = 2/size;
Mat4x4f m; m.identity();
m(0,0)=sc; m(0,3) = -center[0]*sc;
m(1,1)=sc; m(1,3) = -center[1]*sc;
m(2,2)=sc; m(2,3) = -center[2]*sc;
for (int i=0;i<obj.nbp();i++)
obj.PP(i) = transform(m,obj.getPP(i));
}
if (scale != Vec3f(1,1,1))
{
std::cout << "Scaling mesh..."<<std::endl;
for (int i=0;i<obj.nbp();i++)
{
Vec3f p = obj.getPP(i);
p[0] *= scale[0];
p[1] *= scale[1];
p[2] *= scale[2];
obj.PP(i) = p;
}
}
Mat4x4f xform;
bool hasXForm = false;
xform.identity();
if (rotation != Vec3f(0,0,0))
{
std::cout << "Rotating mesh..."<<std::endl;
Mat3x3f mat;
Quat qx,qy,qz;
qx.fromDegreeAngAxis(rotation[0],Vec3f(1,0,0));
qy.fromDegreeAngAxis(rotation[1],Vec3f(0,1,0));
qz.fromDegreeAngAxis(rotation[2],Vec3f(0,0,1));
Quat q = qx*qy*qz;
q.toMatrix(&mat);
std::cout << "mat = "<<mat<<std::endl;
for (int i=0;i<obj.nbp();i++)
{
obj.PP(i) = mat*obj.getPP(i);
obj.PN(i) = mat*obj.getPN(i);
}
hasXForm = true;
xform = mat;
}
if (translation != Vec3f(0,0,0))
{
std::cout << "Translating mesh..."<<std::endl;
for (int i=0;i<obj.nbp();i++)
{
obj.PP(i) = obj.getPP(i) + translation;
}
hasXForm = true;
xform[0][3] = translation[0];
xform[1][3] = translation[1];
xform[2][3] = translation[2];
}
if (obj.distmap && hasXForm)
{
//Mat4x4f m; m.invert(xform);
std::cout << "distmap mat = "<<xform <<" * " << obj.distmap->mat<<" = ";
obj.distmap->mat = xform * obj.distmap->mat;
std::cout << obj.distmap->mat<<std::endl;
}
{
BBox bb = obj.calcBBox();
std::cout << "Mesh bbox = "<<bb<<std::endl;
std::cout << "Mesh center and radius = "<<(bb.a+bb.b)*0.5<<" "<<(bb.b-bb.a)*0.5 << std::endl;
}
if (flip)
{
std::cout << "Flipping mesh..."<<std::endl;
obj.flipAll();
//obj.calcFlip();
}
if (closemesh)
{
bool closed = obj.isClosed();
std::cout << "Mesh is "<<(closed?"":"NOT ")<<"closed."<<std::endl;
if (closemesh && !closed)
{
obj.calcFlip();
std::cout << "Closing mesh..."<<std::endl;
obj.close();
std::cout << "Mesh is "<<(obj.isClosed()?"":"NOT ")<<"closed."<<std::endl;
}
}
if (dilate != 0.0f)
obj.dilate(dilate);
if (tesselate > 0 || sphere)
{
std::cout << "Tesselating mesh..."<<std::endl;
tesselateMesh(obj, tesselate, sphere);
}
if (dist)
{
if (!rx) rx = res;
if (!ry) ry = res;
if (!rz) rz = res;
std::cout << "Flipping mesh..."<<std::endl;
obj.calcFlip();
obj.calcEdges();
bool closed = obj.isClosed();
std::cout << "Mesh is "<<(closed?"":"NOT ")<<"closed."<<std::endl;
if (!closed)
{
std::cout << "Closing mesh..."<<std::endl;
obj.close();
std::cout << "Mesh is "<<(obj.isClosed()?"":"NOT ")<<"closed."<<std::endl;
}
std::cout << "Computing "<<rx<<'x'<<ry<<'x'<<rz<<" DistMap..."<<std::endl;
obj.calcDistMap(rx,ry,rz,(border<0 ? -border : obj.calcBBox().size()*border));
}
if (wnormals)
obj.setAttrib(Mesh::MESH_POINTS_NORMAL,true);
if (rnormals)
obj.setAttrib(Mesh::MESH_POINTS_NORMAL,false);
if (rtexcoords)
obj.setAttrib(Mesh::MESH_POINTS_TEXCOORD,false);
if (!file_out.empty())
{
std::cout << "Saving result..."<<std::endl;
obj.save(file_out.c_str());
}
return 0;
}
|