34#ifndef AIMS_SURFACE_SURFACEOPERATION_D_H
35#define AIMS_SURFACE_SURFACEOPERATION_D_H
44 template<
int D,
class T> std::vector<std::set<uint> >
47 const std::vector< AimsVector<uint,D> > & poly = surf.
polygon();
49 std::vector<std::set<uint> > neigh( surf.
vertex().size() );
51 for (
uint i=0; i<n; ++i )
52 for (
uint j=0; j<D; ++j )
53 for (
uint k=0; k<D; ++k )
55 neigh[poly[i][j]].insert( poly[i][k] );
60 template<
int D,
class T> std::vector<std::set<uint> >
63 const std::vector< AimsVector<uint,D> > & poly = surf.
polygon();
65 std::vector<std::set<uint> > neigh( surf.
vertex().size() );
67 for (
uint i=0; i<n; ++i )
68 for (
uint j=0; j<D; ++j )
69 for (
uint k=0; k<D; ++k )
71 neigh[poly[i][j]].insert( poly[i][k] );
76 template<
int D,
class T> std::vector<std::set<uint> >
79 const std::vector< AimsVector<uint,D> > & poly = surf.
polygon();
81 std::vector<std::set<uint> > neigh( surf.
vertex().size() );
82 std::vector<std::set<uint> > neigh_final( surf.
vertex().size() );
84 for (
uint i=0; i<n; ++i )
85 for (
uint j=0; j<D; ++j )
86 for (
uint k=0; k<D; ++k )
88 neigh[poly[i][j]].insert( poly[i][k] );
90 for (
uint i=0; i<neigh.size() ; ++i)
92 std::set<uint> voisin=neigh[i];
93 std::set<uint> voisin_final=neigh[i];
94 std::set<uint>::iterator voisin_pt=voisin.begin();
95 for ( ; voisin_pt!=voisin.end(); ++voisin_pt)
98 std::set<uint> vois2=neigh[j];
99 std::set<uint>::iterator v_pt=vois2.begin();
100 for ( ; v_pt!=vois2.end(); ++v_pt)
101 voisin_final.insert(*v_pt);
103 neigh_final[i]=voisin_final;
110 template<
int D,
class T> std::vector<std::map<uint, float> >
113 const std::vector< AimsVector<uint,D> > & poly = surf.
polygon();
114 uint n = poly.size();
115 const std::vector<Point3df> & vert = surf.
vertex();
116 std::vector<std::map<uint, float> > neighDist(surf.
vertex().size());
120 for (
uint i=0; i<n; ++i )
121 for (
uint j=0; j<D; ++j )
122 for (
uint k=0; k<D; ++k )
125 diff=vert[poly[i][j]] - vert[poly[i][k]];
127 neighDist[poly[i][j]][poly[i][k]] = dist;
133 template<
int D,
class T>
137 typename std::vector< AimsVector<uint,D> >
::iterator ip, fp;
139 for ( is=surface.begin(); is!=fs; ++is )
142 std::vector< AimsVector<uint,D> > & poly = surf.
polygon();
144 for ( ip=poly.begin(), fp=poly.end(); ip!=fp; ++ip )
147 for (
uint i=0; i<n; ++i )
161 std::string fface =
"counterclockwise";
162 if( mat->hasProperty(
"front_face" ) )
164 std::string oldface = mat->getProperty(
"front_face" )->getString();
165 if( oldface ==
"counterclockwise" )
168 mat->setProperty(
"front_face", fface );
174 template<
int D,
class T>
181 for( is=add.begin(); is!=es; ++is )
186 std::vector<Point3df> & vert = omesh.
vertex();
187 std::vector<Point3df> &
norm = omesh.
normal();
188 std::vector<AimsVector<uint,D> > & poly = omesh.
polygon();
189 const std::vector<Point3df> & vert2 = imesh.
vertex();
190 const std::vector<Point3df> &
norm2 = imesh.
normal();
191 const std::vector<AimsVector<uint,D> > & poly2 = imesh.
polygon();
192 unsigned i, j, n = vert.size(), m = vert2.size(), p = poly2.size();
195 for ( i=0; i<m; ++i )
196 vert.push_back( vert2[i] );
197 for ( i=0, m=
norm2.size(); i<m; ++i )
201 for ( i=0; i<p; ++i )
204 for ( j=0; j<D; ++j )
205 pol[j] = poly2[i][j] + n;
206 poly.push_back( pol );
213 template<
int D,
class T>
220 for ( im=src.begin(); im!=em; ++im )
225 template<
int D,
class T>
232 for( itime=mesh.begin(); itime!=tend; ++itime )
234 typedef std::vector< Point3df > Points;
235 Points& vertex = itime->second.vertex();
236 Points& normal = itime->second.normal();
237 unsigned nnorm = normal.size();
238 for( Points::size_type k = 0; k < vertex.size(); ++k )
240 vertex[ k ] = motion.
transform( vertex[ k ] );
245 normal[ k ] = motion.
transform( normal[ k ] )
248 norm = normal[k][0] * normal[k][0] + normal[k][1] * normal[k][1]
249 + normal[k][2] * normal[k][2];
251 normal[k][0] *=
norm;
252 normal[k][1] *=
norm;
253 normal[k][2] *=
norm;
262 template <
int D,
typename T>
267 std::vector<size_t> ** overtIndex )
273 * overtIndex =
new std::vector<size_t>;
279 while( jmt != emt || jtt != ett )
286 std::vector<Point3df> & overt = mh.
vertex();
287 std::vector<Point3df> & onorm = mh.
normal();
288 std::vector<AimsVector<uint, D> > & opoly = mh.
polygon();
289 const std::vector<Point3df> & vert = imt->second.vertex();
290 const std::vector<Point3df> &
norm = imt->second.normal();
291 const std::vector<AimsVector<uint, D> > & poly = imt->second.polygon();
296 std::map<uint, uint> vmap;
297 typename std::map<uint, uint>::iterator iv, ev = vmap.end();
299 bool absmaj, hasdist;
303 for( im=poly.begin(); im != em; ++im )
305 std::map<int16_t, unsigned> vals;
312 g += vert[ (*im)[k] ];
314 unsigned & x = vals[y];
325 if( vals[ value ] == nt )
337 if( vals[ y ] == nt )
339 dist2 = ( vert[ (*im)[k] ] - g ).
norm2();
340 if( !hasdist || dist2 < dist )
360 iv = vmap.find( (*im)[k] );
363 vmap[ (*im)[k] ] = i;
365 overt.push_back( vert[ (*im)[k] ] );
366 onorm.push_back(
norm[ (*im)[k] ] );
368 (** overtIndex).push_back((*im)[k]);
374 opoly.push_back( p );
387 template <
int D,
typename T>
396 for( is=mesh.begin(); is!=es; ++is )
399 const std::vector<Point3df> & vert = surf.
vertex();
400 const std::vector<AimsVector<uint,D> > & poly = surf.
polygon();
403 std::vector<float> & tx = (*tex)[ is->first ].data();
404 std::vector<unsigned> counts;
405 tx.reserve( vert.size() );
406 tx.insert( tx.end(), vert.size(), 0. );
407 counts.reserve( vert.size() );
408 counts.insert( counts.end(), vert.size(), 0 );
412 ip, ep = poly.end(), jp;
415 for( ip=poly.begin(); ip!=ep; ++ip )
421 uint vj = (*ip)[ (i+1) % D ];
422 n = ( vert[ vi ] - vert[ vj ] ).
norm();
432 std::vector<float>::iterator it, et = tx.end();
433 std::vector<unsigned>::const_iterator itc;
435 for( it=tx.begin(), itc=counts.begin(); it!=et; ++it, ++itc )
437 *it /= float( *itc );
441 for( it=tx.begin(), itc=counts.begin(); it!=et; ++it, ++itc )
442 *it = float( *itc ) / *it;
449 template <
int D,
typename T>
457 ids, eds = denommesh.end();
460 for( is=nummesh.begin(), ids=denommesh.begin(); is!=es && ids!=eds;
464 const std::vector<Point3df> & vert = surf.
vertex();
465 const std::vector<AimsVector<uint,D> > & poly = surf.
polygon();
466 const std::vector<Point3df> & dvert = ids->second.vertex();
469 std::vector<float> & tx = (*tex)[ is->first ].data();
470 std::vector<unsigned> counts;
471 tx.reserve( vert.size() );
472 tx.insert( tx.end(), vert.size(), 0. );
473 counts.reserve( vert.size() );
474 counts.insert( counts.end(), vert.size(), 0 );
478 ip, ep = poly.end(), jp;
481 for( ip=poly.begin(); ip!=ep; ++ip )
487 uint vj = (*ip)[ (i+1) % D ];
488 n = ( vert[ vi ] - vert[ vj ] ).
norm();
489 dn = ( dvert[ vi ] - dvert[ vj ] ).
norm();
494 tx[ vi ] = std::max( tx[ vi ], n );
495 tx[ vj ] = std::max( tx[ vj ], n );
521 std::map<std::pair<uint,uint>,
uint> & segments,
523 const std::vector<Point3df> & vert,
524 std::vector<Point3df> & overt )
526 std::pair<uint,uint> seg = std::make_pair( i, j );
528 iseg = segments.find( seg );
530 if( iseg == segments.end() )
532 Point3df p = ( vert[i] + vert[j] ) / 2;
534 overt.push_back( p );
535 segments[ seg ] = iout;
545 template <
typename T>
550 const std::vector<Point3df> & vert = mesh.
vertex();
551 const std::vector<AimsVector<uint,3> > & poly = mesh.
polygon();
553 std::map<std::pair<uint,uint>,
uint> segments;
557 std::vector<Point3df> & overt = outmesh->
vertex();
558 std::vector<AimsVector<uint,2> > & opoly = outmesh->
polygon();
560 for( ip=poly.begin(); ip!=ep; ++ip )
586 if( tex[i] == region )
590 if( tex[j] == region )
594 if( tex[k] == region )
601 if( tex[i] != tex[j] && tex[i] != tex[k] && tex[j] != tex[k] )
604 Point3df center = ( vert[i] + vert[j] + vert[k] ) / 3.;
605 uint iop = overt.size();
606 overt.push_back( center );
621 if( tex[i] == tex[j] )
625 if( tex[i] == tex[k] )
632 if( !done && ( ii != jj || ii != kk ) )
634 std::vector<uint> ipts;
651 template <
typename T>
656 AimsSurfaceTriangle::const_iterator imesh, jmesh, emesh = mesh.end();
657 imesh = mesh.begin();
662 itex = tex.begin(), jtex=itex, etex = tex.end();
666 while( imesh != emesh && itex != etex )
668 unsigned mtimestep = imesh->first;
669 unsigned ttimestep = itex->first;
673 (*outmesh)[ std::max( mtimestep, ttimestep ) ] = *tmesh;
675 if( jmesh == emesh && jtex == etex )
677 if( jmesh != emesh && ( jtex == etex || jmesh->first < jtex->first ) )
682 else if( jtex != etex
683 && ( jmesh == emesh || jmesh->first > jtex->first ) )
706 template <
int D,
typename T>
711 const std::vector<Point3df> & vert = mesh[timestep].
vertex();
712 std::vector<AimsVector<uint, D> > & poly = mesh[timestep].
polygon();
713 std::multimap<float, AimsVector<uint, D> > sorted;
714 typename std::vector<AimsVector<uint, D> >
::iterator
716 typename std::multimap<float, AimsVector<uint, D> >
::iterator
717 im, em = sorted.end();
722 for( ip=poly.begin(); ip!=ep; ++ip )
724 center = vert[(*ip)[0]];
726 center += vert[(*ip)[d]];
727 sorted.insert( std::make_pair( center.
dot( direction ), *ip ) );
731 for( im=sorted.begin(), ip=poly.begin(); im!=em; ++im, ++ip )
736 template <
int D,
typename T>
737 std::pair<carto::rc_ptr< AimsTimeSurface<D, Void> >,
746 for( im=texmesh.begin(); im!=em; ++im )
750 m.
vertex() = im->second.vertex();
751 m.
normal() = im->second.normal();
752 m.
polygon() = im->second.polygon();
753 t.
data() = im->second.texture();
756 mesh->header().copyProperties(
758 tex->header().copyProperties(
761 return std::make_pair( mesh, tex );
765 template <
int D,
typename T>
776 std::set<int> timesteps;
777 for( im=mesh.begin(); im!=em; ++im )
778 timesteps.insert( im->first );
779 for( it=texture.begin(); it!=et; ++it )
780 timesteps.insert( it->first );
781 std::set<int>::iterator is, es = timesteps.end();
783 for( is=timesteps.begin(); is!=es; ++is )
785 im = mesh.lower_bound( *is );
788 it = texture.lower_bound( *is );
791 m.
vertex() = im->second.vertex();
792 m.
normal() = im->second.normal();
793 m.
polygon() = im->second.polygon();
795 m.
texture() = it->second.data();
798 texmesh->header().copyProperties(
800 texmesh->header().copyProperties(
The template class to manage a mesh.
const std::vector< Point3df > & normal() const
Get a const reference to the vector of normals.
const std::vector< AimsVector< uint, D > > & polygon() const
Get a const reference to the vector of polygons.
const std::vector< T > & texture() const
Get a const reference to the vector of textures.
const std::vector< Point3df > & vertex() const
Get a const reference to the vector of vertices.
The template class to manage a mesh with time if needed.
void erase()
Clear all the meshes.
const aims::PythonHeader & header() const
Get the header.
std::map< int, AimsSurface< D, T > >::const_iterator const_iterator
const std::vector< AimsVector< uint, D > > & polygon() const
Get a const reference to the vector of polygons of the 0 surface.
std::map< int, AimsSurface< D, T > >::iterator iterator
const std::vector< Point3df > & vertex() const
Get a const reference to the vector of verteces of the surface of index 0.
T dot(const AimsVector< T, D > &other) const
const std::vector< T > & data() const
std::map< int, Texture< T > >::const_iterator const_iterator
const aims::PythonHeader & header() const
Get the header.
static void sortPolygonsAlongDirection(AimsTimeSurface< D, T > &mesh, int timestep, const Point3df &direction)
Sort polygons along a given direction.
static AimsTimeSurface< D, T > * meshExtract(const AimsTimeSurface< D, T > &mesh, const TimeTexture< int16_t > &tex, int16_t value, std::vector< size_t > **overtIndex=0)
Extracts a sub-mesh region from a label texture.
static void invertSurfacePolygons(AimsTimeSurface< D, T > &surface)
static TimeTexture< float > * meshEdgeLengthRatioTexture(const AimsTimeSurface< D, T > &nummesh, const AimsTimeSurface< D, T > &denommesh)
Calculate an edge length ratio in edges of two meshes with the same topology.
static std::vector< std::map< uint, float > > surfaceNeighbourDistance(const AimsTimeSurface< D, T > &surf)
static void meshTransform(AimsTimeSurface< D, T > &mesh, const Motion &trans)
Applies a transformation to a mesh.
static void meshMerge(AimsTimeSurface< D, T > &dst, const AimsTimeSurface< D, T > &add)
concatenates 2 meshes into one (adds the second to the first one)
static std::pair< carto::rc_ptr< AimsTimeSurface< D, Void > >, carto::rc_ptr< TimeTexture< T > > > splitTexturedMesh(const AimsTimeSurface< D, T > &texmesh)
Split a textured mesh into a mesh and a texture.
static carto::rc_ptr< AimsTimeSurface< D, T > > joinTexturedMesh(const AimsTimeSurface< D, Void > &mesh, const TimeTexture< T > &texture)
Join a mesh and a texture into a textured mesh.
static TimeTexture< float > * meshDensity(const AimsTimeSurface< D, T > &mesh, bool asDistance=false)
Calculate a mesh density: inverse of the average edges distance If asDistance is true,...
static std::vector< std::set< uint > > surfaceNeighbours(const AimsSurface< D, T > &surf)
static std::vector< std::set< uint > > surfaceNeighbours2ndOrder(const AimsTimeSurface< D, T > &surf)
static AimsSurface< 2, Void > * meshTextureBoundary(const AimsSurface< 3, Void > &mesh, const Texture< T > &tex, T region)
Extracts the boundary of region of value <region> of the input texture, on the mesh.
virtual void copyProperties(Object source)
Object reference(Object &value)
virtual bool getProperty(const std::string &, Object &) const
virtual void setProperty(const std::string &, Object)
virtual bool hasProperty(const std::string &) const
uint take_mid_point_and_insert(std::map< std::pair< uint, uint >, uint > &segments, uint i, uint j, const std::vector< Point3df > &vert, std::vector< Point3df > &overt)
The class for EcatSino data write operation.
std::map< std::string, Object > Dictionary
aims::AffineTransformation3d Motion
AIMSDATA_API AimsTimeSurface< 3, Void > AimsSurfaceTriangle
AimsVector< float, 3 > Point3df
float norm2(const AimsVector< T, D > &v1)
float norm(const AimsVector< T, D > &v1)