31 #ifndef MOAB_HAVE_EIGEN3
32 #error mbvisumap tool requires eigen3 configuration
35 #ifndef MOAB_HAVE_HDF5
36 #error mbvisumap tool requires hdf5 configuration
49 #include <Eigen/Sparse>
53 printf( "Error: %s\n", nc_strerror( e ) ); \
62 #define GET_DIM1( ncdim, name, val ) \
64 int gdfail = nc_inq_dimid( ncFile1, name, &( ncdim ) ); \
65 if( NC_NOERR == gdfail ) \
68 gdfail = nc_inq_dimlen( ncFile1, ncdim, &tmp_val ); \
69 if( NC_NOERR != gdfail ) \
80 #define GET_VAR1( name, id, dims ) \
83 int gvfail = nc_inq_varid( ncFile1, name, &( id ) ); \
84 if( NC_NOERR == gvfail ) \
87 gvfail = nc_inq_varndims( ncFile1, id, &ndims ); \
88 if( NC_NOERR == gvfail ) \
90 ( dims ).resize( ndims ); \
91 gvfail = nc_inq_vardimid( ncFile1, id, &( dims )[0] ); \
92 if( NC_NOERR != gvfail ) \
100 #define GET_1D_INT_VAR1( name, id, vals ) \
102 GET_VAR1( name, id, vals ); \
106 int ivfail = nc_inq_dimlen( ncFile1, ( vals )[0], &ntmp ); \
107 if( NC_NOERR != ivfail ) \
111 ( vals ).resize( ntmp ); \
113 ivfail = nc_get_vara_int( ncFile1, id, &ntmp1, &ntmp, &( vals )[0] ); \
114 if( NC_NOERR != ivfail ) \
121 #define GET_1D_DBL_VAR1( name, id, vals ) \
123 std::vector< int > dum_dims; \
124 GET_VAR1( name, id, dum_dims ); \
128 int dvfail = nc_inq_dimlen( ncFile1, dum_dims[0], &ntmp ); \
129 if( NC_NOERR != dvfail ) \
133 ( vals ).resize( ntmp ); \
135 dvfail = nc_get_vara_double( ncFile1, id, &ntmp1, &ntmp, &( vals )[0] ); \
136 if( NC_NOERR != dvfail ) \
143 using namespace moab;
146 int main(
int argc,
char* argv[] )
153 double fraction = 0.05;
154 std::string inputfile1, inputSource, inputTarget;
155 opts.
addOpt< std::string >(
"map,m",
"input map ", &inputfile1 );
156 opts.
addOpt< std::string >(
"source,s",
"source mesh", &inputSource );
157 opts.
addOpt< std::string >(
"target,t",
"target mesh", &inputTarget );
158 opts.
addOpt<
int >(
"dimSource,d",
"dimension of source ", &dimSource );
159 opts.
addOpt<
int >(
"dimTarget,g",
"dimension of target ", &dimTarget );
160 opts.
addOpt<
int >(
"typeOutput,o",
" output type vtk(0), h5m(1), view(default = 2) ", &otype );
162 int startSourceID = -1, endSourceID = -1, startTargetID = -1, endTargetID = -1;
163 opts.
addOpt<
int >(
"startSourceID,b",
"start source id ", &startSourceID );
164 opts.
addOpt<
int >(
"endSourceID,e",
"end source id ", &endSourceID );
165 opts.
addOpt<
int >(
"startTargetID,c",
"start target id ", &startTargetID );
166 opts.
addOpt<
int >(
"endTargetID,f",
"end target id ", &endTargetID );
167 opts.
addOpt<
double >(
"raiseFraction,r",
"fraction for raising source points height (default 0.05)", &fraction );
172 std::string extension =
".vtk";
173 if( 1 <= otype ) extension =
".h5m";
176 int fail = nc_open( inputfile1.c_str(), 0, &
ncFile1 );
177 if( NC_NOWRITE !=
fail )
182 std::cout <<
" opened " << inputfile1 <<
" for map 1 \n";
189 std::cout <<
" n_a, n_b, n_s : " << na1 <<
", " << nb1 <<
", " << ns1 <<
" for map 1 \n";
190 std::vector< int > col1( ns1 ), row1( ns1 );
191 std::vector< double > val1( ns1 );
192 int idrow1, idcol1, ids1;
206 std::string name_map = inputfile1;
208 name_map.erase( name_map.begin() + name_map.length() - 3, name_map.end() );
210 size_t pos = name_map.rfind(
'/', name_map.length() );
211 if( pos != std::string::npos ) name_map = name_map.erase( 0, pos + 1 );
214 "Failed to create weight" );
224 std::vector< double > xc_a( na1 ), yc_a( na1 );
225 std::vector< double > xc_b( nb1 ), yc_b( nb1 );
226 int idxc_a, idxc_b, idyc_a, idyc_b;
232 std::vector< double > vertex_coords_src( 3 * na1 );
234 for(
int i = 0; i < na1; i++ )
237 sph.
R = 1 + fraction;
238 sph.
lon = xc_a[i] * M_PI / 180;
239 sph.
lat = yc_a[i] * M_PI / 180;
241 vertex_coords_src[3 * i] = pos[0];
242 vertex_coords_src[3 * i + 1] = pos[1];
243 vertex_coords_src[3 * i + 2] = pos[2];
247 "can't create source vertices" );
252 std::vector< int > vgid( na1 );
253 for(
int i = 0; i < na1; i++ )
257 std::vector< double > vertex_coords_tgt( 3 * nb1 );
259 for(
int i = 0; i < nb1; i++ )
263 sph.
lon = xc_b[i] * M_PI / 180;
264 sph.
lat = yc_b[i] * M_PI / 180;
266 vertex_coords_tgt[3 * i] = pos[0];
267 vertex_coords_tgt[3 * i + 1] = pos[1];
268 vertex_coords_tgt[3 * i + 2] = pos[2];
272 "can't create target vertices" );
277 for(
int i = 0; i < nb1; i++ )
289 for(
int i = 0; i < ns1; i++ )
291 array[2 * i] = source_verts[col1[i] - 1];
292 array[2 * i + 1] = target_verts[row1[i] - 1];
294 Range edges( actual_start_handle, actual_start_handle + ns1 - 1 );
299 for(
int i = 0; i < ns1; i++ )
303 std::string name_file = name_map + extension;
305 std::cout <<
" wrote view map file " << name_file <<
" with source fraction height: " << fraction <<
"\n";
310 typedef Eigen::Triplet< double >
Triplet;
311 std::vector< Triplet > tripletList;
312 tripletList.reserve( ns1 );
313 for(
int iv = 0; iv < ns1; iv++ )
318 tripletList.push_back(
Triplet( row1[iv] - 1, col1[iv] - 1, val1[iv] ) );
320 Eigen::SparseMatrix< double > weight1( nb1, na1 );
322 weight1.setFromTriplets( tripletList.begin(), tripletList.end() );
323 weight1.makeCompressed();
327 map< int, EntityHandle > sourceHandles;
328 map< int, EntityHandle > targetHandles;
331 const char* readopts =
"";
337 sids.resize( sRange.
size() );
340 for(
size_t i = 0; i < sids.size(); i++ )
344 sourceHandles[gid] = eh;
349 tids.resize( tRange.
size() );
352 for(
size_t i = 0; i < tids.size(); i++ )
356 targetHandles[gid] = eh;
361 for(
int col = startSourceID - 1; col <= endSourceID - 1; col++ )
365 if( col < 0 )
continue;
366 Eigen::SparseVector< double > colVect = weight1.col( col );
368 for( Eigen::SparseVector< double >::InnerIterator it( colVect ); it; ++it )
370 double weight = it.value();
372 int globalIdRow = it.
index() + 1;
382 " can't get adj cells " );
383 targetEnts.
merge( adjCells );
388 std::stringstream fff;
389 fff << name_map <<
"_column" << col + 1 << extension;
396 for(
int row = startTargetID - 1; row <= endTargetID - 1; row++ )
400 if( row < 0 )
continue;
401 Eigen::SparseVector< double > rowVect = weight1.row( row );
403 for( Eigen::SparseVector< double >::InnerIterator it( rowVect ); it; ++it )
405 double weight = it.value();
406 int globalIdCol = it.
index() + 1;
415 " can't get adj cells " );
416 sourceEnts.
merge( adjCells );
420 std::stringstream fff;
421 fff << name_map <<
"_row" << row + 1 << extension;