MCPcopy Create free account
hub / github.com/apple/ml-pointersect / get_random_direction_within_cone

Function get_random_direction_within_cone

plib/rigid_motion.py:365–407  ·  view source on GitHub ↗

Get uniformly sampled random directions within a cone centered at (0,0,1). Args: n: number of samples theta: the half angle of the cone (in degree) rng: numpy random state method: 'random' Returns:

(
        n: int,
        theta: float,
        rng: np.random.RandomState = None,
        method: str = 'random',
)

Source from the content-addressed store, hash-verified

363
364
365def get_random_direction_within_cone(
366 n: int,
367 theta: float,
368 rng: np.random.RandomState = None,
369 method: str = 'random',
370):
371 """
372 Get uniformly sampled random directions within a cone centered at (0,0,1).
373
374 Args:
375 n:
376 number of samples
377 theta:
378 the half angle of the cone (in degree)
379 rng:
380 numpy random state
381 method:
382 'random'
383
384 Returns:
385 (n, 3), float64
386
387 We are to use Archimedes' Hat-Box Theorem to sample the directions.
388 """
389 assert 0 < theta <= 180.
390 t_max = 1
391 t_min = np.cos(theta / 180. * np.pi)
392
393 if rng is None:
394 rng = np.random
395
396 # sample uniformly within [0,1]^2
397 samples = sample_utils.get_samples(total_samples=n, d=2, method=method, rng=rng) # (n, 2)
398 # adjust range
399 wzs = samples[..., 0] * (t_max - t_min) + t_min # (n,)
400 phis = samples[..., 1] * (2 * np.pi) # (n,)
401
402 rs = np.sqrt(1 - np.power(wzs, 2))
403 wxs = rs * np.cos(phis)
404 wys = rs * np.sin(phis)
405
406 ds = np.stack((wxs, wys, wzs), axis=-1) # (n,3)
407 return ds
408
409
410def construct_coord_frame(

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected