Go to the documentation of this file.00001 #ifndef OPENTISSUE_CORE_CONTAINERS_GRID_UTIL_GRID_JUNCTIONS_H
00002 #define OPENTISSUE_CORE_CONTAINERS_GRID_UTIL_GRID_JUNCTIONS_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <OpenTissue/core/math/math_basic_types.h>
00013
00014 #include <OpenTissue/core/containers/grid/util/grid_gradient.h>
00015 #include <OpenTissue/core/containers/grid/util/grid_coord2idx.h>
00016 #include <OpenTissue/core/containers/grid/util/grid_idx2coord.h>
00017 #include <OpenTissue/core/containers/grid/util/grid_gradient_at_point.h>
00018 #include <OpenTissue/core/containers/grid/util/grid_local_minima.h>
00019
00020 #include <OpenTissue/core/containers/grid/util/grid_aof.h>
00021
00022 #include <OpenTissue/collision/spatial_hashing/spatial_hashing.h>
00023 #include <OpenTissue/collision/intersect/intersect_sphere_triangle.h>
00024
00025 #include <cmath>
00026 #include <list>
00027
00028 namespace OpenTissue
00029 {
00030 namespace grid
00031 {
00032
00033 namespace detail
00034 {
00035
00036 template<typename math_types_>
00037 class JunctionCandidate
00038 {
00039 public:
00040
00041 typedef math_types_ math_types;
00042 typedef typename math_types::vector3_type vector3_type;
00043 typedef typename math_types::real_type real_type;
00044
00045 public:
00046
00047 vector3_type m_center;
00048 real_type m_radius;
00049 size_t m_cnt_boundary;
00050
00051 public:
00052
00053 JunctionCandidate(vector3_type const & center, real_type const & radius)
00054 : m_center(center)
00055 , m_radius(radius)
00056 , m_cnt_boundary()
00057 {
00058 assert(m_radius>0 || !"JunctionCandidate(): Non-positive radius");
00059 }
00060 };
00061
00062
00063
00064 template<typename face_type,typename junction_candiate_type>
00065 class JunctionCollisionPolicy
00066 {
00067 public:
00068
00069 typedef typename junction_candiate_type::math_types math_types;
00070 typedef typename math_types::vector3_type vector3_type;
00071 typedef typename math_types::real_type real_type;
00072 typedef face_type data_type;
00073 typedef junction_candiate_type query_type;
00074 typedef bool result_container;
00075 typedef OpenTissue::spatial_hashing::PrimeNumberHashFunction hash_function;
00076
00077 typedef OpenTissue::spatial_hashing::Grid< vector3_type, OpenTissue::math::Vector3<int>, data_type, hash_function> hash_grid;
00078
00079 public:
00080
00081 vector3_type min_coord(face_type const & face) const
00082 {
00083 vector3_type coord;
00084 mesh::compute_face_minimum_coord(face,coord);
00085 return coord;
00086 }
00087
00088 vector3_type max_coord(face_type const & face) const
00089 {
00090 vector3_type coord;
00091 mesh::compute_face_maximum_coord(face,coord);
00092 return coord;
00093 }
00094
00095 vector3_type min_coord(junction_candiate_type const & candidate) const
00096 {
00097 return vector3_type(
00098 candidate.m_center(0) - candidate.m_radius
00099 , candidate.m_center(1) - candidate.m_radius
00100 , candidate.m_center(2) - candidate.m_radius
00101 );
00102 }
00103
00104 vector3_type max_coord(junction_candiate_type const & candidate) const
00105 {
00106 return vector3_type(
00107 candidate.m_center(0) + candidate.m_radius
00108 , candidate.m_center(1) + candidate.m_radius
00109 , candidate.m_center(2) + candidate.m_radius
00110 );
00111 }
00112
00113 void reset(result_container & ) { }
00114
00115 void report(data_type const & data, query_type const & query,result_container & )
00116 {
00117 face_type * face = const_cast<face_type*>(&data);
00118 junction_candiate_type * candidate = const_cast<junction_candiate_type*>(&query);
00119
00120 if(candidate->m_cnt_boundary >= 4)
00121 return;
00122
00123
00124 OpenTissue::geometry::Triangle<math_types> triangle;
00125 triangle.set( face );
00126
00127 typedef OpenTissue::math::BasicMathTypes< real_type, size_t> math_types;
00128 OpenTissue::geometry::Sphere<math_types> sphere(candidate->m_center,candidate->m_radius);
00129
00130 if(OpenTissue::intersect::sphere_triangle(sphere,triangle))
00131 {
00132 ++(candidate->m_cnt_boundary);
00133 }
00134
00135 }
00136 };
00137
00138 }
00139
00140
00141
00150 template<typename grid_type, typename mesh_type, typename point_container >
00151 inline void junctions(
00152 grid_type const & aof
00153 , grid_type & phi
00154 , mesh_type & mesh
00155 , point_container & points
00156 )
00157 {
00158 using std::fabs;
00159 using std::min;
00160 using std::max;
00161 using std::sqrt;
00162
00163 typedef typename point_container::value_type vector3_type;
00164 typedef typename vector3_type::value_type real_type;
00165 typedef typename grid_type::value_type value_type;
00166 typedef typename grid_type::const_iterator const_iterator;
00167 typedef typename grid_type::const_index_iterator const_index_iterator;
00168 typedef typename grid_type::index_iterator index_iterator;
00169 typedef typename mesh_type::face_type face_type;
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435 std::cout << "junctions(): junction candiates = " << points.size() << std::endl;
00436 }
00437
00438
00439 template<typename grid_type, typename mesh_type, typename point_container >
00440 inline void junctions2(
00441 grid_type const & aof
00442 , grid_type const & phi
00443 , mesh_type & mesh
00444 , point_container & points
00445 )
00446 {
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471 using std::fabs;
00472 using std::min;
00473 using std::sqrt;
00474
00475 typedef typename point_container::value_type vector3_type;
00476 typedef typename vector3_type::value_type real_type;
00477 typedef typename grid_type::const_iterator const_iterator;
00478 typedef typename grid_type::const_index_iterator const_index_iterator;
00479
00480 typedef typename grid_type::math_types math_types;
00481 typedef typename mesh_type::face_type face_type;
00482 typedef detail::JunctionCandidate<math_types> candidate_type;
00483
00484 typedef std::list<candidate_type> candidate_container;
00485 typedef detail::JunctionCollisionPolicy<face_type,candidate_type> collision_policy;
00486 typedef AABBDataQuery< typename collision_policy::hash_grid, collision_policy > query_algorithm;
00487
00488 bool results = false;
00489 query_algorithm query;
00490 candidate_container candidates;
00491
00492 {
00493 real_type dx = phi.dx()*0.5;
00494 real_type dy = phi.dy()*0.5;
00495 real_type dz = phi.dz()*0.5;
00496 real_type rho = sqrt(dx*dx+dy*dy+dz*dz)*.5;
00497
00498 const_iterator p = phi.begin();
00499 const_index_iterator a = aof.begin();
00500 const_index_iterator end = aof.end();
00501 for(;a!=end;++a,++p)
00502 {
00503 if( (*a) < 1e-5 || (*p) >= 0)
00504 continue;
00505 vector3_type center;
00506 idx2coord(a,center);
00507 real_type radius = fabs( *p );
00508 candidate_type candidate(center,radius + rho);
00509 candidates.push_back(candidate);
00510 }
00511 }
00512
00513 query.init(candidates.begin(),candidates.end());
00514 query(mesh.face_begin(), mesh.face_end(), candidates.begin(), candidates.end(), results, typename query_algorithm::all_tag() );
00515
00516 size_t cnt = 0;
00517 for(typename candidate_container::iterator c = candidates.begin(); c!=candidates.end();++c)
00518 {
00519 if(c->m_cnt_boundary >= 4)
00520 {
00521 points.push_back(c->m_center );
00522 ++cnt;
00523 }
00524 }
00525 std::cout << "junctions2(): junction candiates = " << cnt << std::endl;
00526 }
00527
00528 }
00529 }
00530
00531
00532 #endif