@@ -494,20 +494,35 @@ public override int GetHashCode()
494
494
495
495
#region IEquatable
496
496
497
- public bool Equals ( Mesh other )
497
+ public bool SomewhatEquals ( Mesh other , float trianglesToBeMatched , int precision )
498
498
{
499
499
if ( ReferenceEquals ( null , other ) ) {
500
500
return false ;
501
501
}
502
502
if ( ReferenceEquals ( this , other ) ) {
503
503
return true ;
504
504
}
505
- if ( ! Name . Equals ( other . Name ) || Vertices . Length != other . Vertices . Length
506
- || Indices . Length != other . Indices . Length
507
- || AnimationFrames . Count != other . AnimationFrames . Count
508
- || ! AnimationDefaultPosition . Equals ( other . AnimationDefaultPosition ) ) {
505
+ var triangles1 = IndexTriangles ( Vertices , Indices , precision ) ;
506
+ var triangles2 = IndexTriangles ( other . Vertices , other . Indices , precision ) ;
507
+
508
+ using var enumerator = triangles1 . GetEnumerator ( ) ;
509
+ var matched = 0 ;
510
+ foreach ( var hash in triangles1 . Keys ) {
511
+ if ( triangles2 . ContainsKey ( hash ) ) {
512
+ matched ++ ;
513
+ }
514
+ }
515
+ return matched / ( float ) triangles1 . Count > trianglesToBeMatched ;
516
+ }
517
+
518
+ public bool Equals ( Mesh other )
519
+ {
520
+ if ( ReferenceEquals ( null , other ) ) {
509
521
return false ;
510
522
}
523
+ if ( ReferenceEquals ( this , other ) ) {
524
+ return true ;
525
+ }
511
526
512
527
for ( var i = 0 ; i < Vertices . Length ; i ++ ) {
513
528
if ( ! Vertices [ i ] . Equals ( other . Vertices [ i ] ) ) {
@@ -532,6 +547,43 @@ public bool Equals(Mesh other)
532
547
return true ;
533
548
}
534
549
550
+ private Vertex3DNoTex2 [ ] Find ( IEnumerable < Vertex3DNoTex2 > vertices , Vertex3DNoTex2 vertex , int precision )
551
+ {
552
+ return vertices . Where ( v => v . Equals ( vertex , precision ) ) . ToArray ( ) ;
553
+ }
554
+
555
+ private static Dictionary < int , Vertex3DNoTex2 [ ] > IndexTriangles ( IReadOnlyList < Vertex3DNoTex2 > vertices , IReadOnlyList < int > indices , int precision )
556
+ {
557
+ var triangles = new Dictionary < int , Vertex3DNoTex2 [ ] > ( ) ;
558
+ for ( var i = 0 ; i < indices . Count ; i += 3 ) {
559
+ var hash = HashTriangle ( Sort (
560
+ vertices [ indices [ i ] ] ,
561
+ vertices [ indices [ i + 1 ] ] ,
562
+ vertices [ indices [ i + 2 ] ]
563
+ ) , precision ) ;
564
+ triangles . Add ( hash , new [ ] { vertices [ indices [ i ] ] , vertices [ indices [ i + 1 ] ] , vertices [ indices [ i + 2 ] ] } ) ;
565
+ }
566
+ return triangles ;
567
+ }
568
+
569
+ private static Vertex3DNoTex2 [ ] Sort ( params Vertex3DNoTex2 [ ] vertices )
570
+ {
571
+ var firstVertex = vertices
572
+ . OrderBy ( s => s . X ) . ThenBy ( s => s . Y ) . ThenBy ( s => s . Z )
573
+ . ThenBy ( s => s . Nx ) . ThenBy ( s => s . Ny ) . ThenBy ( s => s . Nz )
574
+ . ThenBy ( s => s . Tu ) . ThenBy ( s => s . Tv )
575
+ . First ( ) ;
576
+ var firstVertexIndex = Array . IndexOf ( vertices , firstVertex ) ;
577
+ return new [ ] {
578
+ vertices [ firstVertexIndex ] ,
579
+ vertices [ ( firstVertexIndex + 1 ) % 3 ] ,
580
+ vertices [ ( firstVertexIndex + 2 ) % 3 ]
581
+ } ;
582
+ }
583
+
584
+ private static int HashTriangle ( IReadOnlyList < Vertex3DNoTex2 > vertices , int precision )
585
+ => ( vertices [ 0 ] . GetRoundedHash ( precision ) , vertices [ 1 ] . GetRoundedHash ( precision ) , vertices [ 2 ] . GetRoundedHash ( precision ) ) . GetHashCode ( ) ;
586
+
535
587
public override bool Equals ( object obj )
536
588
{
537
589
if ( ReferenceEquals ( null , obj ) ) {
0 commit comments