|
34 | 34 | #include <geode/geometry/intersection.hpp> |
35 | 35 | #include <geode/geometry/intersection_detection.hpp> |
36 | 36 |
|
| 37 | +#include <geode/mesh/core/edged_curve.hpp> |
37 | 38 | #include <geode/mesh/core/surface_mesh.hpp> |
38 | 39 |
|
39 | 40 | namespace |
40 | 41 | { |
41 | | - template < typename Line > |
42 | | - geode::Point3D begin( const geode::SurfaceMesh3D& mesh, const Line& line ) |
| 42 | + template < typename Mesh, typename Line > |
| 43 | + geode::Point< Mesh::dim > begin( const Mesh& mesh, const Line& line ) |
43 | 44 | { |
44 | 45 | auto bbox = mesh.bounding_box(); |
45 | 46 | bbox.add_point( line.origin() ); |
46 | 47 | const auto diagonal = bbox.diagonal(); |
47 | 48 | return line.origin() - line.direction() * diagonal.length(); |
48 | 49 | } |
49 | 50 |
|
50 | | - template < typename Line > |
51 | | - geode::Point3D end( const geode::SurfaceMesh3D& mesh, const Line& line ) |
| 51 | + template < typename Mesh, typename Line > |
| 52 | + geode::Point< Mesh::dim > end( const Mesh& mesh, const Line& line ) |
52 | 53 | { |
53 | 54 | auto bbox = mesh.bounding_box(); |
54 | 55 | bbox.add_point( line.origin() ); |
55 | 56 | const auto diagonal = bbox.diagonal(); |
56 | 57 | return line.origin() + line.direction() * diagonal.length(); |
57 | 58 | } |
58 | 59 |
|
| 60 | + bool test_vertex_mode( const geode::EdgedCurve2D& mesh, |
| 61 | + const geode::RayTracing2D::EdgeDistance& edge0, |
| 62 | + const geode::RayTracing2D::EdgeDistance& edge1 ) |
| 63 | + { |
| 64 | + geode::EdgeVertex edge_vertex0{ edge0.edge, geode::NO_LID }; |
| 65 | + if( edge0.position == geode::POSITION::vertex0 ) |
| 66 | + { |
| 67 | + edge_vertex0.vertex_id = 0; |
| 68 | + } |
| 69 | + else if( edge0.position == geode::POSITION::vertex1 ) |
| 70 | + { |
| 71 | + edge_vertex0.vertex_id = 1; |
| 72 | + } |
| 73 | + else |
| 74 | + { |
| 75 | + return false; |
| 76 | + } |
| 77 | + geode::EdgeVertex edge_vertex1{ edge1.edge, geode::NO_LID }; |
| 78 | + if( edge1.position == geode::POSITION::vertex0 ) |
| 79 | + { |
| 80 | + edge_vertex1.vertex_id = 0; |
| 81 | + } |
| 82 | + else if( edge1.position == geode::POSITION::vertex1 ) |
| 83 | + { |
| 84 | + edge_vertex1.vertex_id = 1; |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + return false; |
| 89 | + } |
| 90 | + return mesh.edge_vertex( edge_vertex0 ) |
| 91 | + == mesh.edge_vertex( edge_vertex1 ); |
| 92 | + } |
| 93 | + |
59 | 94 | bool test_vertex_mode( const geode::SurfaceMesh3D& mesh, |
60 | 95 | const geode::RayTracing3D::PolygonDistance& polygon0, |
61 | 96 | const geode::RayTracing3D::PolygonDistance& polygon1 ) |
@@ -139,6 +174,28 @@ namespace |
139 | 174 | return mesh.polygon_adjacent_edge( polygon_edge0 ) == polygon_edge1; |
140 | 175 | } |
141 | 176 |
|
| 177 | + bool are_equal( const geode::EdgedCurve2D& mesh, |
| 178 | + const geode::RayTracing2D::EdgeDistance& edge0, |
| 179 | + const geode::RayTracing2D::EdgeDistance& edge1 ) |
| 180 | + { |
| 181 | + if( std::fabs( edge0.distance - edge1.distance ) |
| 182 | + > geode::GLOBAL_EPSILON ) |
| 183 | + { |
| 184 | + return false; |
| 185 | + } |
| 186 | + if( edge0.position == geode::POSITION::inside |
| 187 | + || edge0.position == geode::POSITION::parallel ) |
| 188 | + { |
| 189 | + return false; |
| 190 | + } |
| 191 | + if( edge1.position == geode::POSITION::inside |
| 192 | + || edge1.position == geode::POSITION::parallel ) |
| 193 | + { |
| 194 | + return false; |
| 195 | + } |
| 196 | + return test_vertex_mode( mesh, edge0, edge1 ); |
| 197 | + } |
| 198 | + |
142 | 199 | bool are_equal( const geode::SurfaceMesh3D& mesh, |
143 | 200 | const geode::RayTracing3D::PolygonDistance& polygon0, |
144 | 201 | const geode::RayTracing3D::PolygonDistance& polygon1 ) |
@@ -168,6 +225,153 @@ namespace |
168 | 225 |
|
169 | 226 | namespace geode |
170 | 227 | { |
| 228 | + class RayTracing2D::Impl |
| 229 | + { |
| 230 | + public: |
| 231 | + Impl( const EdgedCurve2D& mesh, const Ray2D& ray ) |
| 232 | + : mesh_( mesh ), |
| 233 | + origin_( ray.origin() ), |
| 234 | + segment_{ ray.origin(), end( mesh, ray ) } |
| 235 | + { |
| 236 | + } |
| 237 | + |
| 238 | + Impl( const EdgedCurve2D& mesh, const InfiniteLine2D& infinite_line ) |
| 239 | + : mesh_( mesh ), |
| 240 | + origin_( infinite_line.origin() ), |
| 241 | + segment_{ begin( mesh, infinite_line ), |
| 242 | + end( mesh, infinite_line ) } |
| 243 | + { |
| 244 | + } |
| 245 | + |
| 246 | + std::optional< EdgeDistance > closest_edge() const |
| 247 | + { |
| 248 | + if( results_.empty() ) |
| 249 | + { |
| 250 | + return std::nullopt; |
| 251 | + } |
| 252 | + sort_results(); |
| 253 | + return results_.front(); |
| 254 | + } |
| 255 | + |
| 256 | + std::optional< absl::FixedArray< RayTracing2D::EdgeDistance > > |
| 257 | + closest_edges( index_t size ) const |
| 258 | + { |
| 259 | + if( results_.empty() ) |
| 260 | + { |
| 261 | + return std::nullopt; |
| 262 | + } |
| 263 | + sort_results(); |
| 264 | + std::optional< absl::FixedArray< RayTracing2D::EdgeDistance > > |
| 265 | + closest_edges{ std::min( |
| 266 | + size, static_cast< index_t >( results_.size() ) ) }; |
| 267 | + for( const auto i : Indices{ closest_edges.value() } ) |
| 268 | + { |
| 269 | + closest_edges->at( i ) = results_[i]; |
| 270 | + } |
| 271 | + return closest_edges; |
| 272 | + } |
| 273 | + |
| 274 | + std::vector< EdgeDistance > all_intersections() const |
| 275 | + { |
| 276 | + if( results_.empty() ) |
| 277 | + { |
| 278 | + return {}; |
| 279 | + } |
| 280 | + sort_results(); |
| 281 | + return results_; |
| 282 | + } |
| 283 | + |
| 284 | + bool compute( index_t edge_id ) |
| 285 | + { |
| 286 | + const auto segment = mesh_.segment( edge_id ); |
| 287 | + const auto result = |
| 288 | + segment_segment_intersection_detection( segment_, segment ); |
| 289 | + if( result.first == POSITION::outside ) |
| 290 | + { |
| 291 | + return false; |
| 292 | + } |
| 293 | + if( auto intersection = |
| 294 | + segment_segment_intersection( segment_, segment ) ) |
| 295 | + { |
| 296 | + auto& intersection_result = intersection.result.value(); |
| 297 | + auto distance = |
| 298 | + point_point_distance( origin_, intersection_result ); |
| 299 | + if( Vector2D{ origin_, intersection_result }.dot( |
| 300 | + segment_.direction() ) |
| 301 | + < 0 ) |
| 302 | + { |
| 303 | + distance *= -1.; |
| 304 | + } |
| 305 | + std::lock_guard< std::mutex > lock{ mutex_ }; |
| 306 | + results_.emplace_back( edge_id, distance, result.second, |
| 307 | + std::move( intersection_result ) ); |
| 308 | + } |
| 309 | + return false; |
| 310 | + } |
| 311 | + |
| 312 | + private: |
| 313 | + void sort_results() const |
| 314 | + { |
| 315 | + if( are_results_sorted_ ) |
| 316 | + { |
| 317 | + return; |
| 318 | + } |
| 319 | + absl::c_sort( results_ ); |
| 320 | + const auto last = std::unique( results_.begin(), results_.end(), |
| 321 | + [this]( const EdgeDistance& edge0, const EdgeDistance& edge1 ) { |
| 322 | + return are_equal( this->mesh_, edge0, edge1 ); |
| 323 | + } ); |
| 324 | + results_.erase( last, results_.end() ); |
| 325 | + are_results_sorted_ = true; |
| 326 | + } |
| 327 | + |
| 328 | + private: |
| 329 | + const EdgedCurve2D& mesh_; |
| 330 | + const Point2D& origin_; |
| 331 | + DEBUG_CONST OwnerSegment2D segment_; |
| 332 | + mutable std::vector< EdgeDistance > results_; |
| 333 | + mutable bool are_results_sorted_{ false }; |
| 334 | + std::mutex mutex_; |
| 335 | + }; |
| 336 | + |
| 337 | + RayTracing2D::RayTracing2D( const EdgedCurve2D& mesh, const Ray2D& ray ) |
| 338 | + : impl_{ mesh, ray } |
| 339 | + { |
| 340 | + } |
| 341 | + |
| 342 | + RayTracing2D::RayTracing2D( |
| 343 | + const EdgedCurve2D& mesh, const InfiniteLine2D& infinite_line ) |
| 344 | + : impl_{ mesh, infinite_line } |
| 345 | + { |
| 346 | + } |
| 347 | + |
| 348 | + RayTracing2D::RayTracing2D( RayTracing2D&& ) noexcept = default; |
| 349 | + |
| 350 | + RayTracing2D::~RayTracing2D() = default; |
| 351 | + |
| 352 | + std::optional< RayTracing2D::EdgeDistance > |
| 353 | + RayTracing2D::closest_edge() const |
| 354 | + { |
| 355 | + return impl_->closest_edge(); |
| 356 | + } |
| 357 | + |
| 358 | + std::optional< absl::FixedArray< RayTracing2D::EdgeDistance > > |
| 359 | + RayTracing2D::closest_edges( index_t size ) const |
| 360 | + { |
| 361 | + return impl_->closest_edges( size ); |
| 362 | + } |
| 363 | + |
| 364 | + std::vector< RayTracing2D::EdgeDistance > |
| 365 | + RayTracing2D::all_intersections() const |
| 366 | + { |
| 367 | + return impl_->all_intersections(); |
| 368 | + } |
| 369 | + |
| 370 | + bool RayTracing2D::operator()( index_t edge_id ) |
| 371 | + { |
| 372 | + return impl_->compute( edge_id ); |
| 373 | + } |
| 374 | + |
171 | 375 | class RayTracing3D::Impl |
172 | 376 | { |
173 | 377 | public: |
|
0 commit comments