Component

class pyvsim.Primitives.Component[source]

The class component is a representation of elements used in the simulation.

Most of its methods are abstract (throw a NotImplementedError) because the really useful classes are its derivatives:

  • Assembly
  • Part
  • Line

However, this class exists to stipulate a common interface for all elements in simulation, allowing a tree-like nesting of them.

Some attributes (x, y, z, origin, id) are not directly changeable (for obvious reasons), only with geometrical transforms, etc, so no setter is implemented, and if you try to change them, you will get an error.

There is also a implementation of the visitor pattern using the acceptVisitor method

Methods

alignTo(x_new, y_new, z_new=None, pivotPoint=None, tol=1e-08)[source]

This method allows the alignment of the part to a specific direction (this is very useful in optical systems definition).

The implementation assumes that at least two orthogonal vectors are given. There is an assertion to guarantee that.

With the new vector base, a rotation matrix M is calculated, and the formulation to convert rotation matrix to axis-angle is used for convenience (as then the method rotate can be directly called).

Obs: No concerns about code efficiency are made, as this method will probably not be used all the time.

Parameters :

x_new, [y_new, z_new] : numpy.ndarray \((1,3)\)

New vectors defining the orientation of the part. The vectors need NOT to be normalized, but MUST be orthogonal.

Vectors y_new or z_new can be omitted (one at a time) and will be implicitly calculated

pivotPoint : numpy.ndarray \((1,3)\)

Point in space around which the rotation occurs. If not given, rotates around local origin.

tol : 1e-8

Many checks are executed in order to guarantee that the new x, y and z vectors are orthogonal and normalized. This is the tolerance of these checks.

Raises :

AssertionError :

If the vectors are not perpendicular to the given tolerance (check is done with a dot product), or if the rotation is performed from a right-handed coordinate system to a left-handed and vice-versa.

LinAlgError :

If the calculation has other mathematical problems.

bounds[source]

Returns the extremities of the aligned to axis bounding box of the component in the format:

[[xmin, ymin, zmin],
[xmax, ymax, zmax]]

If the component is not visible by ray tracing rays, the function shall return None

clearData()[source]

This method must be implemented by each inheriting class. Its function is to avoid classes having inconsistent data after a geometric transform.

For example: a camera has a mapnp.ping funcion calculated from raytracing, then the user moves this camera, making the mapnp.ping invalid.

When a rotation or translation is called the clearData method is also called, and the class is in charge of cleaning all data that is now not valid anymore.

depth[source]

Return the depth of the component within a tree

intersections(p0, p1, tol=1e-08)[source]

This is a method used specifically for ray tracing. The method returns data about the first intersection between line segments and the polygons defined in the Component. The implementation of the intersection is given by the inheriting classes.

Parameters :

p0, p1 - numpy.ndarray :math:`(N, 3)` :

Coordinates defining N segments by 2 points (each p0, p1 pair), which will be tested for intersection with the polygons defined in the structure.

tol - double :

Tolerance used in the criteria for intersection (see documentation of each implementation)

Returns :

None :

If no intersections are found.

Otherwise returns a list with:: :

lineParameter :

This is used to indicate how far the intersection point is from the segment starting point, if 0, the intersection is at p0 and if 1, the intersection is at p1

Iff the parameter is > 1 (999), no intersection was found

intersectionCoordinates :

This is where the intersections are found

triangleIndexes :

This is the index of the triangle where the intersection was found. If no intersection found, will return 0, but attention, the only way to guarantee that no intersection was found is when the lineParameter is zero.

normals :

The normal vector at the intersection point (if the surface is defined with normals at vertices, interpolation is performed).

part :

A list with references to this object. This is, in this case, redundant, but that makes the function signature uniform with the Assembly class

origin[source]

Local origin of the component

parent = None

Reference to the component parent. Ideally this should be a weak reference to avoid memory leaks. As python does not provide a clean implementation of weakref, one must use always the remove method of Assembly to clear this field and avoid leaks

rotate(angle, axis, pivotPoint=None)[source]

This method should be used when there is a change in the component position. This method operates only with the origin and the x, y and z vectors. It delegates the responsibility to the inheriting class by means of the rotateImplementation method.

Parameters :

angle :

Angle : scalar (in radians)

axis : numpy.ndarray \((1, 3)\)

Vector around which the rotation occurs.

pivotPoint : numpy.ndarray \((1, 3)\)

Point in space around which the rotation occurs. If not given, rotates around origin.

rotateImplementation(angle, axis, pivotPoint)[source]

This method must be implemented by the interested inheriting class in case a rotation affects its internals.

For example: a class with a vector of points P will probably need to update them accordingly using the following code:

P = Utils.rotatePoints(P,angle,axis,pivotPoint)

This is a way of implementing the Chain of Responsibility pattern, so that these geometrical operations are executed recursively.

This is a protected method, do not use it unless you are inheriting from this class!

Parameters :

angle :

Angle : scalar (in radians)

axis : numpy.ndarray \((1,3)\)

Vector around which the rotation occurs.

pivotPoint : numpy.ndarray \((1,3)\)

Point in space around which the rotation occurs. If not given, rotates around origin.

Raises :

NotImplementedError :

translate(vector)[source]

This method should be used when there is a change in the component position. This method operates only with the origin position, and delegates the responsibility to the inheriting class by means of the translateImplementation method.

Parameters :

vector : numpy.ndarray \((1, 3)\)

Vector to translate the component. An array with x, y and z coordinates

translateImplementation(vector)[source]

This method must be implemented by the interested inheriting class in case a translation affects its internals.

For example: a class with a vector of points P will probably need to update that to P+vector

This is a way of implementing the Chain of Responsibility pattern, so that these geometrical operations are executed recursively.

This is a protected method, do not use it unless you are inheriting from this class!

Parameters :

vector : numpy.ndarray \((1, 3)\)

Vector to translate the component. An array with x, y and z coordinates

Raises :

NotImplementedError :

x[source]

Local x coordinate of the component. This functionality is unaware of the inheriting components, but this coordinate is chosen as the optical axis of all elements in Toolbox (e.g. Camera optical axis, Laser beam direction, Mirror normal to the surface).

y[source]

Local y coordinate of the component

z[source]

Local z coordinate of the component

This Page