883 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
80 lines
4.0 KiB
Python
80 lines
4.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Format controls: vertex reordering must not hide changed topology/winding."""
|
|
import copy
|
|
from pathlib import Path
|
|
import struct
|
|
import sys
|
|
import unittest
|
|
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'ops'))
|
|
import m2_format_compare as paired
|
|
|
|
|
|
def fixtures(order=(2,0,1)):
|
|
vertices=[struct.pack('<3f4B4B7f',*p,255,0,0,0,0,0,0,0,0,0,1,0,0,0,0)
|
|
for p in ((0,0,0),(1,0,0),(0,1,0))]
|
|
source=bytearray(324)
|
|
source[:8]=struct.pack('<4sI',b'MD20',256)
|
|
struct.pack_into('<II',source,68,3,len(source));source.extend(b''.join(vertices))
|
|
struct.pack_into('<II',source,76,1,len(source));view=len(source);source.extend(bytes(44))
|
|
def array(raw,header,k,values):
|
|
struct.pack_into('<II',raw,header+k*4,len(values)//2,len(raw));raw.extend(values)
|
|
array(source,view,0,struct.pack('<3H',0,1,2))
|
|
array(source,view,2,struct.pack('<3H',0,1,2))
|
|
target=bytearray(304);target[:8]=struct.pack('<4sI',b'MD20',272)
|
|
struct.pack_into('<II',target,60,3,len(target));target.extend(b''.join(vertices[i] for i in order))
|
|
struct.pack_into('<I',target,68,1)
|
|
skin=bytearray(b'SKIN'+bytes(60))
|
|
array(skin,4,0,struct.pack('<3H',*(order.index(i)for i in range(3))))
|
|
array(skin,4,2,struct.pack('<3H',0,1,2))
|
|
return bytes(source),bytes(target),bytes(skin)
|
|
|
|
|
|
class PairedTest(unittest.TestCase):
|
|
def test_reordering_retains_full_attributes_and_oriented_topology(self):
|
|
r=paired.compare(*fixtures())
|
|
self.assertTrue(all(r['exact_unordered_attribute_multisets_equal'].values()))
|
|
self.assertTrue(r['oriented_full_vertex_triangle_multisets_equal'])
|
|
self.assertFalse(r['source_vertex_order_preserved'])
|
|
self.assertFalse(r['material_semantics_equivalent'])
|
|
self.assertFalse(r['converted']);self.assertFalse(r['engine_loaded'])
|
|
self.assertEqual(r,paired.compare(*fixtures()))
|
|
|
|
def test_reversed_winding_is_not_equivalent(self):
|
|
s,t,k=fixtures();k=bytearray(k);off=struct.unpack_from('<I',k,16)[0]
|
|
struct.pack_into('<3H',k,off,0,2,1)
|
|
r=paired.compare(s,t,bytes(k))
|
|
self.assertTrue(r['exact_unordered_attribute_multisets_equal']['full_vertex'])
|
|
self.assertFalse(r['oriented_full_vertex_triangle_multisets_equal'])
|
|
|
|
def test_duplicate_vertex_multiplicity_is_not_ignored(self):
|
|
s,t,k=fixtures((0,1,2));t=bytearray(t);t[304:352]=t[352:400]
|
|
self.assertFalse(paired.compare(s,bytes(t),k)['exact_unordered_attribute_multisets_equal']['position'])
|
|
|
|
def test_invalid_vertex_and_triangle_indices_reject(self):
|
|
s,t,k=fixtures()
|
|
for offset_field in (8,16):
|
|
bad=bytearray(k);off=struct.unpack_from('<I',bad,offset_field)[0];struct.pack_into('<H',bad,off,3)
|
|
with self.assertRaisesRegex(ValueError,'topology index'):paired.compare(s,t,bytes(bad))
|
|
|
|
def test_nonfinite_vertex_and_truncated_skin_reject(self):
|
|
s,t,k=fixtures();bad=bytearray(t);struct.pack_into('<f',bad,304,float('nan'))
|
|
with self.assertRaisesRegex(ValueError,'Nonfinite'):paired.compare(s,bytes(bad),k)
|
|
with self.assertRaises(ValueError):paired.compare(s,t,k[:-1])
|
|
|
|
def test_array_bounds_and_input_direction_reject(self):
|
|
s,t,k=fixtures();bad=bytearray(k);struct.pack_into('<II',bad,4,3,1)
|
|
with self.assertRaisesRegex(ValueError,'extent'):paired.compare(s,t,bytes(bad))
|
|
with self.assertRaisesRegex(ValueError,'source256'):paired.compare(t,t,k)
|
|
|
|
def test_native_lod_base_is_not_legacy_bone_limit(self):
|
|
s,t,k=fixtures();bad=bytearray(k);struct.pack_into('<I',bad,44,21)
|
|
with self.assertRaisesRegex(ValueError,'topology index'):paired.compare(s,t,bytes(bad))
|
|
|
|
def test_native_shadow_rows_are_twelve_bytes_and_bounded(self):
|
|
s,t,k=fixtures();k=bytearray(k);offset=len(k);k.extend(bytes(4*12));struct.pack_into('<II',k,48,4,offset)
|
|
r=paired.compare(s,t,bytes(k));self.assertEqual(r['target']['skin']['shadow_batch_count'],4)
|
|
with self.assertRaisesRegex(ValueError,'extent'):paired.compare(s,t,bytes(k[:-1]))
|
|
|
|
|
|
if __name__=='__main__':unittest.main()
|