RMF
alternatives.py
1 ## \example alternatives.py
2 # The RMF format supports storing alternative representations of the structure
3 # through the RMF::decorator::Alternatives decorator and associated types.
4 # Multiple versions of the hierarchy are stored, all representing the same
5 # biological entity. Different representations can be used for different
6 # purposes based on resolution or other criteria.
7 
8 import RMF
9 
10 tfn = RMF._get_temporary_file_path("alternatives.rmf3")
11 print("File is", tfn)
12 
13 f = RMF.create_rmf_file(tfn)
14 f.add_frame("frame", RMF.FRAME)
15 
16 # make a trivial set of multiple resolution represntations
17 rh = f.get_root_node()
18 
19 pf = RMF.ParticleFactory(f)
20 gpf = RMF.GaussianParticleFactory(f)
21 rff = RMF.ReferenceFrameFactory(f)
22 
23 
24 def frange(x, y, jump):
25  while x < y:
26  yield x
27  x += jump
28 
29 
30 def create_hierarchy(radius):
31  root = f.add_node(str(radius), RMF.REPRESENTATION)
32  for i in frange(0., 8, radius):
33  for j in frange(0., 8, radius):
34  for k in frange(0., 8, radius):
35  center = RMF.Vector3(
36  i + radius / 2,
37  j + radius / 2,
38  k + radius / 2)
39  n = root.add_child(
40  str(i) + "-" + str(j) + "-" + str(k),
41  RMF.REPRESENTATION)
42  d = pf.get(n)
43  d.set_static_radius(radius)
44  d.set_frame_coordinates(center)
45  d.set_static_mass(radius ** 3)
46  return root
47 
48 
49 def create_gmm(radius=1.):
50  root = f.add_node("gmm" + str(radius), RMF.REPRESENTATION)
51  for i in frange(0., 8, radius):
52  for j in frange(0., 8, radius):
53  center = RMF.Vector3(
54  i + radius / 2,
55  j + radius / 2,
56  4)
57 
58  n = root.add_child(
59  str(i) + "-" + str(j),
60  RMF.REPRESENTATION)
61  d = gpf.get(n)
62  d.set_variances(RMF.Vector3(radius / 2, radius / 2, 4))
63  d.set_static_mass(radius ** 3)
64  rf = rff.get(n)
65  rf.set_rotation(RMF.Vector4(1, 0, 0, 0))
66  rf.set_translation(center)
67  return root
68 
69 n = create_hierarchy(4)
70 rh.add_child(n)
71 
72 af = RMF.AlternativesFactory(f)
73 da = af.get(n)
74 da.add_alternative(create_hierarchy(1), RMF.PARTICLE)
75 da.add_alternative(create_hierarchy(2), RMF.PARTICLE)
76 da.add_alternative(create_hierarchy(4), RMF.PARTICLE)
77 da.add_alternative(create_gmm(), RMF.GAUSSIAN_PARTICLE)
78 
80 
81 print("particles", RMF.get_resolutions(n, RMF.PARTICLE))
82 print("gaussian particles", RMF.get_resolutions(n, RMF.GAUSSIAN_PARTICLE))
void show_hierarchy(NodeConstHandle root)
FileHandle create_rmf_file(std::string path)
Create an RMF from a file system path.