coords.Coords#
- class coords.Coords(data=None, dtyp=at.Float, copy=False)#
Bases:
ndarrayA structured collection of points in a 3D cartesian space.
The
Coordsclass is the basic data structure used throughout HelloTriangle to store the coordinates of points in a 3D space. It is used by other classes, such asMesh. Applications will mostly use the higher level classes.Coordsis implemented as a subclass ofnumpy.ndarray, and thus inherits all its methods and atttributes. The last axis of theCoordshowever always has a length equal to 3. Each set of 3 values along the last axis are the coordinates (in a global 3D cartesian coordinate system) of a single point in space. The full Coords array thus is a collection of points. If the array is 2-dimensional, the Coords is a flat list of points. But if the array has more dimensions, the collection of points itself becomes structured.The float datatype is only checked at creation time. It is the responsibility of the user to keep this consistent throughout the lifetime of the object.
Note#
Methods that transform a Coords object, like
scale(),translate(),rotate(), … do not change the original Coords object, but return a new object. Some methods however have an inplace option that allows the user to force coordinates to be changed in place. This option is seldom used however: rather we conveniently use statements like:X = X.some_transform()
and Python can immediately free and recollect the memory used for the old object X.
Parameters#
- data: float array_like
Data to initialize the Coords. The last axis should have a length of 1, 2 or 3, but will be expanded to 3 if it is less, filling the missing coordinates with zeros. Thus, if you only specify two coordinates, all points are lying in the z=0 plane. Specifying only one coordinate creates points along the x-axis.
If no data are provided, an empty Coords with shape (0,3) is created.
- dtyp: float datatype, optional
It not provided, the datatype of
datais used, or the defaultFloat(set tonumpy.float32).- copy: bool
If True, the data are copied. The default setting will try to use the original data if possible, e.g. if data is a correctly shaped and typed
numpy.ndarray.
Returns#
- Coords
An instance of the Coords class, which is basically an
numpy.ndarrayof floats, with the last axis having a length of 3.
The Coords instance has a number of attributes that provide views on (part of) the data. They are a notational convenience over using indexing. These attributes can be used to set all or some of the coordinates by direct assignment. The assigned data should however be broadcast compatible with the assigned shape: the shape of the Coords can not be changed.
Examples#
>>> Coords([1.,2.]) Coords([1., 2., 0.]) >>> X = Coords(np.arange(6).reshape(2,3)) >>> print(X) [[0. 1. 2.] [3. 4. 5.]] >>> print(X.y) [1. 4.] >>> X.z[1] = 9. >>> print(X) [[0. 1. 2.] [3. 4. 9.]] >>> print(X.xz) [[0. 2.] [3. 9.]] >>> X.x = 0. >>> print(X) [[0. 1. 2.] [0. 4. 9.]]
>>> Y = Coords(X) # Y shares its data with X >>> Z = Coords(X, copy=True) # Z is independent >>> Y.y = 5 >>> Z.z = 6 >>> print(X) [[0. 5. 2.] [0. 5. 9.]] >>> print(Y) [[0. 5. 2.] [0. 5. 9.]] >>> print(Z) [[0. 1. 6.] [0. 4. 6.]] >>> X.coords is X True >>> Z.xyz = [1,2,3] >>> print(Z) [[1. 2. 3.] [1. 2. 3.]]
Create an empty Coords object:
>>> E = Coords() >>> print(E) []