|
19 | 19 | """ |
20 | 20 |
|
21 | 21 | import numpy as np |
22 | | -import warp as wp |
23 | 22 | from stl import mesh as np_mesh |
24 | 23 |
|
25 | 24 | from .geometry import Geometry |
26 | 25 | from .parameterization import Parameterization, Bounds, Parameter |
27 | 26 | from .curve import Curve |
28 | 27 | from physicsnemo.sym.constants import diff_str |
29 | | -from physicsnemo.utils.sdf import signed_distance_field |
30 | 28 |
|
31 | 29 |
|
32 | 30 | class Tessellation(Geometry): |
@@ -118,47 +116,57 @@ def _sdf(triangles, airtight): |
118 | 116 | def sdf(invar, params, compute_sdf_derivatives=False): |
119 | 117 | # gather points |
120 | 118 | points = np.stack([invar["x"], invar["y"], invar["z"]], axis=1) |
| 119 | + points = np.squeeze(points) |
| 120 | + if points.ndim == 1: |
| 121 | + points = points.reshape(1, -1) |
121 | 122 |
|
122 | 123 | # normalize triangles and points |
123 | 124 | minx, maxx, miny, maxy, minz, maxz = _find_mins_maxs(points) |
124 | 125 | max_dis = max(max((maxx - minx), (maxy - miny)), (maxz - minz)) |
125 | | - store_triangles = np.array(triangles, dtype=np.float64) |
| 126 | + store_triangles = np.array(triangles, dtype=np.float32) |
126 | 127 | store_triangles[:, :, 0] -= minx |
127 | 128 | store_triangles[:, :, 1] -= miny |
128 | 129 | store_triangles[:, :, 2] -= minz |
129 | 130 | store_triangles *= 1 / max_dis |
130 | | - store_triangles = store_triangles.reshape(-1, 3) |
| 131 | + store_triangles = store_triangles.reshape(-1, 3) # (n_vertices, 3) |
131 | 132 | points[:, 0] -= minx |
132 | 133 | points[:, 1] -= miny |
133 | 134 | points[:, 2] -= minz |
134 | 135 | points *= 1 / max_dis |
135 | | - points = points.astype(np.float64).flatten() |
| 136 | + points = points.astype(np.float32) # Keep as (n_points, 3) |
136 | 137 |
|
137 | 138 | # compute sdf values |
138 | 139 | outputs = {} |
139 | 140 | if airtight: |
| 141 | + # Import locally to avoid pickling issues with multiprocessing |
| 142 | + import torch |
| 143 | + from physicsnemo.utils.sdf import signed_distance_field |
| 144 | + |
| 145 | + torch_vertices = torch.from_numpy(store_triangles) |
| 146 | + torch_indices = torch.arange( |
| 147 | + store_triangles.shape[0], dtype=torch.int32 |
| 148 | + ) |
| 149 | + torch_points = torch.from_numpy(points) |
| 150 | + |
140 | 151 | sdf_field, sdf_derivative = signed_distance_field( |
141 | | - store_triangles, |
142 | | - np.arange((store_triangles.shape[0])), |
143 | | - points, |
144 | | - include_hit_points=True, |
| 152 | + torch_vertices, |
| 153 | + torch_indices, |
| 154 | + torch_points, |
| 155 | + max_dist=1e8, |
| 156 | + use_sign_winding_number=False, |
145 | 157 | ) |
146 | | - if isinstance(sdf_field, wp.types.array): |
147 | | - sdf_field = sdf_field.numpy() |
148 | | - if isinstance(sdf_derivative, wp.types.array): |
149 | | - sdf_derivative = sdf_derivative.numpy() |
| 158 | + |
| 159 | + sdf_field = sdf_field.numpy() |
| 160 | + sdf_derivative = sdf_derivative.numpy() # Shape: (n_points, 3) |
| 161 | + |
150 | 162 | sdf_field = -np.expand_dims(max_dis * sdf_field, axis=1) |
151 | | - sdf_derivative = sdf_derivative.reshape(-1) |
152 | 163 | else: |
153 | 164 | sdf_field = np.zeros_like(invar["x"]) |
154 | 165 | outputs["sdf"] = sdf_field |
155 | 166 |
|
156 | 167 | # get sdf derivatives |
157 | 168 | if compute_sdf_derivatives: |
158 | 169 | sdf_derivative = -(sdf_derivative - points) |
159 | | - sdf_derivative = np.reshape( |
160 | | - sdf_derivative, (sdf_derivative.shape[0] // 3, 3) |
161 | | - ) |
162 | 170 | sdf_derivative = sdf_derivative / np.linalg.norm( |
163 | 171 | sdf_derivative, axis=1, keepdims=True |
164 | 172 | ) |
|
0 commit comments