Changeset 1112


Ignore:
Timestamp:
03/17/21 22:36:57 (3 weeks ago)
Author:
Maciej Komosinski
Message:

Added an example of sampling a 3D Model using the ModelGeometry? class

File:
1 edited

Legend:

Unmodified
Added
Removed
  • framspy/frams-test.py

    r1103 r1112  
    100100        print(json.loads(serialized_dict._string()))
    101101
     102# sampling a Model in 3D
     103geno = "RXX(X,CXXX)"
     104print("\nNow build a Model from the genotype '%s' and sample it in 3D, then print a 2D projection" % geno)
     105import numpy as np
     106
     107matrix = np.zeros((20, 20, 20), dtype=int)  # 3D matrix, "voxels"
     108m = frams.ModelGeometry.forModel(frams.Model.newFromString(geno));
     109m.geom_density = 20;
     110for p in m.voxels():
     111        # print('%f %f %f ' % (p.x._value(), p.y._value(), p.z._value()))
     112        matrix[int(p.x._value() * 5 + 2), int(p.y._value() * 5 + 5), int(p.z._value() * 5 + 6)] += 1  # scaling and offsets adjusted manually to fit the matrix nicely
     113matrix = np.sum(matrix, axis=1)  # sum along axis, make 2D from 3D ("projection")
     114np.set_printoptions(formatter={'int': lambda x: ('.' if x == 0 else str(x // 18))})  # print zeros as dots, x//18 to fit a larger range into a single digit
     115print(matrix)
     116np.set_printoptions()  # avoids straange python errors: frams.py, line 48, in __del__ AttributeError: 'NoneType' object has no attribute 'extFree'
     117
     118#
     119#
    102120# Note that implementing a complete expdef, especially a complex one, entirely in python may be inconvenient or impractical
    103121# because you do not have access to "event handlers" like you have in FramScript - onStep(), onBorn(), onDied(), onCollision() etc.,
Note: See TracChangeset for help on using the changeset viewer.