forked from HandmadeMath/HandmadeMath
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_hmm.py
More file actions
executable file
·166 lines (141 loc) · 5.5 KB
/
update_hmm.py
File metadata and controls
executable file
·166 lines (141 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env python3
import argparse
import os
import re
typeReplacements = [
('hmm_', 'HMM_'),
('vec', 'Vec'),
('mat', 'Mat'),
('quaternion', 'Quaternion'),
('bool', 'Bool'),
('.InternalElementsSSE', '.SSE'),
]
funcReplacements = [
('HMM_', 'HMM_'),
('Vec', 'V'),
('Mat', 'M'),
('Quaternion', 'Q'),
('Equals', 'Eq'),
('Subtract', 'Sub'),
('Multiply', 'Mul'),
('Divide', 'Div'),
('Inverse', 'Inv'),
('RSquareRoot', 'InvSqrt'),
('SquareRoot', 'Sqrt'),
('Squared', 'Sqr'),
('Length', 'Len'),
('Slerp', 'SLerp'),
('By', ''),
('LinearCombineSSE', 'LinearCombineV4M4'),
('Transpose', 'TransposeM4'),
('Fast', ''), # TODO(port): emit warning, lower precision
('Normalize', 'Norm'),
('ToRadians', 'ToRad')
]
handedFuncs = [
'Perspective',
'Rotate',
'Orthographic',
'LookAt',
'FromAxisAngle',
'ToQuaternion',
]
projectionFuncs = [
'Perspective',
'Orthographic',
]
numFiles = 0
numWarnings = 0
def printWarning(msg):
global numWarnings
numWarnings += 1
print('WARNING: {}'.format(msg))
def updateFile(filename):
global numFiles
print('Updating: {}'.format(filename))
numFiles += 1
result = ''
with open(filename, 'r', newline='') as f:
for lineNo, line in enumerate(f):
updatedLine = line
def printLineWarning(msg):
printWarning(' Line {}: {}'.format(lineNo + 1, msg))
def replaceName(m):
name = m.group()
if name.startswith('hmm_'):
# do type replacements
for before, after in typeReplacements:
if before not in name:
continue
name = name.replace(before, after)
else:
# do func replacements
for before, after in funcReplacements:
if before not in name:
continue
name = name.replace(before, after)
if after == 'LinearCombineV4M4':
printLineWarning('HMM_LinearCombineSSE is now HMM_LinearCombineV4M4, and will now use a fallback method when SSE is not available. You no longer need to check for the availability of SSE.')
if after == 'V' or after == 'M':
# uppercase the modifier, if any
name = re.sub(
r'[VM]\d[ivfd]?',
lambda m: m.group().upper(),
name
)
# and also nuke the integer constructors
vecIntMatch = re.search(r'(V\d)I', name)
if vecIntMatch:
name = name.replace(vecIntMatch.group(), vecIntMatch.group(1))
# add handedness / NDC modifiers
if not any(x in name for x in ['RH', 'LH', 'NO', 'ZO']):
for handedFunc in handedFuncs:
suffixed = handedFunc + '_RH'
if handedFunc in projectionFuncs:
suffixed += '_NO'
name = name.replace(handedFunc, suffixed)
return name
def wrapDegrees(m):
name = m.group('name')
arg = m.group('arg')
if '(' in arg:
# all bets are off, don't wrap the argument
printLineWarning('{} now takes radians, but we were unable to automatically wrap the first argument with HMM_AngleDeg().'.format(name))
return m.group()
return '{}(HMM_AngleDeg({}),'.format(name, arg)
updatedLine = re.sub(r'(hmm_|HMM_)\w+', replaceName, updatedLine)
updatedLine = re.sub(r'(?P<name>HMM_Perspective_RH_NO|HMM_Rotate_RH)\((?P<arg>.*?),', wrapDegrees, updatedLine)
result += updatedLine
with open(filename, 'w', newline='') as f:
f.write(result)
parser = argparse.ArgumentParser(
prog = 'update_hmm',
description = 'Updates C and C++ source code to use Handmade Math 2.0.',
)
parser.add_argument(
'filename', nargs='+',
help='A file or directory to update to HMM 2.0. If a directory, all files with extensions from --exts will be processed.',
)
parser.add_argument(
'--exts', nargs='+', default=['.c', '.cpp', '.h', '.hpp'],
help='File extensions to run the script on, when targeting a directory. Default: .c, .cpp, .h, .hpp.',
metavar='.foo',
)
args = parser.parse_args()
for path in args.filename:
filenames = []
if os.path.isfile(path):
filenames = [path]
else:
for root, dirs, files in os.walk(path):
for file in files:
if file == 'HandmadeMath.h':
printWarning('HandmadeMath.h will not be replaced by this script.')
elif file.endswith(tuple(args.exts)):
filenames.append(os.path.join(root, file))
for filename in filenames:
try:
updateFile(filename)
except UnicodeDecodeError:
pass
print('Updated {} files with {} warnings.'.format(numFiles, numWarnings))