Skip to content

Commit 1248538

Browse files
authored
[MISC] Various code cleanup and performance improvements. (#1314)
* Refactor math helper methods to be more generic and improve performance. * Speed up primitive mesh generation for viewer.
1 parent 411c1d9 commit 1248538

File tree

11 files changed

+1466
-911
lines changed

11 files changed

+1466
-911
lines changed

genesis/engine/entities/emitter.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,10 @@ def emit(
147147
else:
148148
gs.raise_exception()
149149

150-
positions = gu.transform_by_T(
151-
positions, gu.trans_R_to_T(pos, gu.z_to_R(direction) @ gu.axis_angle_to_R(np.array([0, 0, 1]), theta))
150+
positions = gu.transform_by_trans_R(
151+
positions,
152+
pos,
153+
gu.z_up_to_R(direction) @ gu.axis_angle_to_R(np.array([0, 0, 1]), theta),
152154
).astype(gs.np_float)
153155

154156
positions = np.tile(positions[np.newaxis], (self._sim._B, 1, 1))

genesis/engine/entities/rigid_entity/rigid_link.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ def _build(self):
144144

145145
if inertia_mesh.is_watertight and self._init_mesh.mass > 0:
146146
# TODO: check if this is correct. This is correct if the inertia frame is w.r.t to link frame
147-
T_inertia = gu.trans_quat_to_T(self._inertial_pos, self._inertial_quat)
147+
dtype = np.result_type(self._inertial_pos, self._inertial_quat)
148+
T_inertia = gu.trans_quat_to_T(self._inertial_pos.astype(dtype), self._inertial_quat.astype(dtype))
148149
self._inertial_i = (
149150
self._init_mesh.moment_inertia_frame(T_inertia) / self._init_mesh.mass * self._inertial_mass
150151
)

genesis/engine/solvers/rigid/constraint_solver_decomp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def add_collision_constraints(self):
122122
link_a_maybe_batch = [link_a, i_b] if ti.static(self._solver._options.batch_links_info) else link_a
123123
link_b_maybe_batch = [link_b, i_b] if ti.static(self._solver._options.batch_links_info) else link_b
124124

125-
d1, d2 = gu.orthogonals(contact_data.normal)
125+
d1, d2 = gu.ti_orthogonals(contact_data.normal)
126126

127127
invweight = self._solver.links_info[link_a_maybe_batch].invweight[0]
128128
if link_b > -1:
@@ -775,7 +775,7 @@ def _func_update_contact_force(self):
775775
contact_data = self._collider.contact_data[i_c, i_b]
776776

777777
force = ti.Vector.zero(gs.ti_float, 3)
778-
d1, d2 = gu.orthogonals(contact_data.normal)
778+
d1, d2 = gu.ti_orthogonals(contact_data.normal)
779779
for i_dir in range(4):
780780
d = (2 * (i_dir % 2) - 1) * (d1 if i_dir < 2 else d2)
781781
n = d * contact_data.friction - contact_data.normal

genesis/engine/solvers/rigid/constraint_solver_decomp_island.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def add_collision_constraints(self, island, i_b):
139139
link_a_maybe_batch = [link_a, i_b] if ti.static(self._solver._options.batch_links_info) else link_a
140140
link_b_maybe_batch = [link_b, i_b] if ti.static(self._solver._options.batch_links_info) else link_b
141141

142-
d1, d2 = gu.orthogonals(contact_data.normal)
142+
d1, d2 = gu.ti_orthogonals(contact_data.normal)
143143

144144
invweight = self._solver.links_info[link_a_maybe_batch].invweight[0] + self._solver.links_info[
145145
link_b_maybe_batch
@@ -526,7 +526,7 @@ def _func_update_contact_force(self, island, i_b):
526526
contact_data = self._collider.contact_data[i_col, i_b]
527527

528528
force = ti.Vector.zero(gs.ti_float, 3)
529-
d1, d2 = gu.orthogonals(contact_data.normal)
529+
d1, d2 = gu.ti_orthogonals(contact_data.normal)
530530
for i in range(4):
531531
d = (2 * (i % 2) - 1) * (d1 if i < 2 else d2)
532532
n = d * contact_data.friction - contact_data.normal

genesis/engine/solvers/rigid/rigid_solver_decomp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,7 @@ def _init_geom_fields(self):
10061006
self.geoms_state = struct_geom_state.field(
10071007
shape=self._batch_shape(self.n_geoms_), needs_grad=False, layout=ti.Layout.SOA
10081008
)
1009-
self._geoms_render_T = np.empty((self.n_geoms_, self._B, 4, 4), order="F", dtype=np.float32)
1009+
self._geoms_render_T = np.empty((self.n_geoms_, self._B, 4, 4), dtype=np.float32)
10101010

10111011
if self.n_geoms > 0:
10121012
# Make sure that the constraints parameters are valid
@@ -1189,7 +1189,7 @@ def _init_vgeom_fields(self):
11891189
self.vgeoms_state = struct_vgeom_state.field(
11901190
shape=self._batch_shape(self.n_vgeoms_), needs_grad=False, layout=ti.Layout.SOA
11911191
)
1192-
self._vgeoms_render_T = np.empty((self.n_vgeoms_, self._B, 4, 4), order="F", dtype=np.float32)
1192+
self._vgeoms_render_T = np.empty((self.n_vgeoms_, self._B, 4, 4), dtype=np.float32)
11931193

11941194
if self.n_vgeoms > 0:
11951195
vgeoms = self.vgeoms

0 commit comments

Comments
 (0)