Table of Contents
A collection of routines for performing analysis on spatial and geometric data. The term "ag" comes from "Analytical Geometry".
Note
This module will not provide the full suite of standard matrix functions. Matrix work is done much, much more efficiently with the Numeric [1] library that is distributed alongside MineSight® Grail.
Some standards for working with this module,
All face lists are described such that the first index indicates how many faces are in the face. For example,
[3, 1, 2, 3, 3, 4, 5, 6]
States that there are 3 vertexes in the first face, and those vertexes are 1, 2, 3. Then the next face contains 3 vertexes, which are 4, 5, 6. This may seem a bit confusing, but using this structure buys us some definite efficiencies.
Note that all faces in MineSight® products have 3 vertexes.
The plane equation is defined as,
a + b + c + d = 0
Whenever you are asked for a, b, c, and d, these match to the above plane equation.
In general most functions in this module will either raise an ArithmeticError or one of its sub-classes, FloatingPointError [2].
This exceptions are defined by the Python Library itself.
The functions in this module can be divided into four distinct groups. These are functions that work with vectors, functions that are used in unit conversions, functions used in spatial/geometric computations, and functions used to compute equality.
cm2inch(centimeters)
Converts centimeters into inches.
feet2meter(feet)
Converts feet into meters.
inch2cm(inches)
Converts inches into centimeters.
meter2feet(meters)
Converts meters into feet.
round_sig(x, digits)
Rounds the value of x to the number of digits given. The standard is the value given for ag.STD_RND_PRECISION.
A value of digits that is less than 1 will generate a ValueError.
todegrees(radians)
Converts radians to degrees.
toradians(degrees)
Converts degrees to radians.
Determines if two real numbers are approximately equal.
Since mathematical reals have infinite precision, and since a machine can not represent infinite precision, there is no precise equality on a machine.
Instead this function looks at the absolute value of the difference and compares it against epsilon, if the difference is less than epsilon then the numbers are considered equal.
The comparison can be written as,
isequal = (| a - b | < epsilon)
Note that specifying your epsilon value leads to an approximate 30% decrease in efficiency.
The default value for epsilon is ag.EPSILON.
Returns a plane based on the argument plane (a, b, c, d) and the vector.
Returns azimuth from point (x1, y1) to point (x2,y2) on a plane.
Returns the dip from one point to another.
Returns the 2D angle between line segments p1p2 and p2p3 (where p2 is the elbow). This will only consider the X,Y components of the points (i.e indices 0 and 1).
Finds the endpoint of a line given a starting point, 3d length, dip, and azimuth. New in version 4.70.
Finds the centroid of a block model cell. Support for regular models and horizontally-rotated models. New in version 5.10.
Determines if line segments p1p2 and p3p4 intersect. This will only consider the X,Y components of the points (i.e indices 0 and 1).
Returns 1 if the segments intersect, 0 otherwise.
Indicates if two points are nearly the same (within a given epsilon).
Arguments:
Determines the nearest point on the line to the given point.
Given a 3-D unbounded line, this computes the nearest point on that line to the point given.
See Standard Definitions for more information about how a point is defined.
Determines if the point is within the given pointlist. This will only consider the X,Y components of point and pointlist (i.e indices 0 and 1).
Note
If you supply a pointlist that is not a closed polygon (i.e. the end point is not the same as the start point), then this algorithm will effectively "close" the pointlist.
This may not be the desired behavior, and you can check your if your pointlist is a polygon using the polygon_check function.
Returns the distance between two points.
pointdistancefromplane(p, a, b, c, d)
Returns the distance from a given plane.
- Arguments:
- p : list
- A point (see Standard Definitions).
- a, b, c, d : floats
- Coefficients for the plane equation (see Standard Definitions).
- Returns:
- The distance from the plane.
Determine if the point list defines a line.
Line is defined by taking the vector between two points and determining if there is any variation from one segment to the next. Therefore the following two point lists would be considered a line,
points = [
[1.0, 1.0, 1.0],
[2.0, 2.0, 2.0],
[3.0, 3.0, 3.0],
]
points = [
[2.0, 2.0, 2.0],
[1.0, 1.0, 1.0],
[3.0, 3.0, 3.0],
]
But this would not be a valid line,
points = [
[0.0, 0.0, 12121.0],
[-1., 0.0, 0.0],
[0.0, 1.0, 0.0],
]
Returns 1 if the points are define a line, 0 otherwise.
Indicates if a group of points exist on the specified plane.
Shifts the contents of the pointlist by the given shiftVector. Useful for shifting points quickly based on some given offset.
Returns the length of a point list in 2-D.
This just sums along from one point to the next only considering the XY dimensions.
Determines the area of a list of points.
To get the correct area all points should lie on the same plane. Furthermore, it is implicit in the calculation of the area that the points in the point list are supposed to connect completely (for example, a polygon).
Returns the minimum and maximum points in the point list.
The minimum and maximum points describe the bounding volume for the list of all points. They should describe the extreme corners of a box that is the bounding volume.
Notice that this calculation can apply to a polyline or to just a cloud of points.
Computes the center of mass for a point list.
If there is only one point in the list, then that point is returned.
If all the points on the list are co-incident, then the first point is returned.
Note
If your data is polygon (end-point repeated) this returns the center of the polygon.
Usage:
Consider the simple case where we have a square polygon defined by the following points,
from grail import ag
points = [[2.0, 0.0, 0.0],
[2.0, 2.0, 0.0],
[0.0, 2.0, 0.0],
[0.0, 0.0, 0.0],
[2.0, 0.0, 0.0]]
center = ag.pointlistcenter(points)At this point center will be equal to [1., 1., 0.].
The above call could have also been made as,
x, y, z = ag.pointlistcenter(points)With this call, you would have x=1.0, y=1.0, and z=0.0.
Returns the direction of the points.
The points are read in sequence to determine if they are leading in a clockwise or counter-clockwise direction.
Returns the length of segments along the point list.
Traverses the points list and returns the total length. Just like walking along the line with a pedometer in 3D space.
Returns the plane equation for the pointlist.
Rotate each point in pointlist about the X-axis.
Rotate each point in pointlist about the Y-axis.
Rotate each point in pointlist about the Z-axis.
Determines if the line segment represented by endpoints p1 and p2 intersects the plane. New in version 4.70.
Determines the nearest point on the plane to the given point.
Given a 3-D plane equation co-efficients and a point, this returns the nearest point on the plane. Can also be thought of as projecting a point directly onto that plane.
Computes the normal to the given plane.
Reorients the plane defined by a, b, c, and d into the standard used by MS3D. This is useful for when you have the following plane equation:
0.00000 1.00000 0.00000 1895.00000
and:
0.00000 -1.00000 0.00000 -1895.00000
both are the same, but the latter one is pointing downwad. The big problem comes from the fact that MS3D will report two different planes one at 1895.00 and one at -1895. By reorienting your planes in a consistent manner, you guarantee that they are stored correctly.
An example of calling this function is,
after calling plane_reorient_normal, the reorient_pln will be equal to the nrm_pln. Notice the trick using the * to pass in a tuple with 4 elements as a, b, c, and d.
If you supply an invalid plane equation, such as 0, 0, 0, 0 or 0, 0, 0, N, where N is any non-zero number, this equation will not generate an error. Instead it will merrily attempt to reorient the plane.
Returns True if the pointlist is a polygon (i.e. it has an end point that is equivalent to it's start point).
The epsilon is used to help determine equality similar to ispointcoincident. It has a default value defined as EPSILON.
Determine if the polygon is convex or concave.
Returns 1 if the polygon is convex, 0 if the polygon is concave.
Detects whether a polygon intersects itself.
Returns 1 if the polygon is self-intersecting, 0 otherwise.
Returns a polygon containing the vertices of a minimal bounding hull of the point list.
Returns the new smoothed pointlist.
Creates a new smoothed pointlist by computing a running average of the points for all the nodes within the pointlist.
The averagecount indicates how many nodes to average across.
An integer equal to the number of nodes to average when creating the new smoothed pointlist.
The minimum value for averagecount is 2.
An integer indicating whether to preserve end points.
SMOOTH_PIN : preserve end points
SMOOTH_NO_PIN : do not preserve end points
Pinning applies to polylines only, which are defined as a pointlist where the first point is not equal to the last point.
Usage:
This function applies to polyline and polygon pointlists.
For this function to work correctly you will require at least three points to perform an average across. If you have less than three points this function will return the original pointlist.
For speed, you may want to screen the pointlist to see if it actually can be smoothed before calling this function. If the pointlist can not be smoothed, the original pointlist is returned. Examples of pointlists that can not be smoothed are: 1. a polyline with two points; 2. a polygon or polyline having a total number of points less than or equal to averagecount.
Returns the new smoothed pointlist. Function also known as "Node Preserving Spline."
Creates a new smoothed pointlist by applying the Bezier algorithm to the polyline/polygon for all nodes in the pointlist. The Bezier smoothing algorithm will intersect nodes while it smooths the curve.
The addnodes specifies how many nodes to add during the smoothing process, and the smoothfactor indicates how smooth to make the curve. The larger the smoothfactor the greater the curve will deviate from the original.
Usage:
This function applies to polyline and polygon pointlists.
For this function to work correctly you will require at least three points to perform the smooth. If you have less than three points this function will return the original pointlist.
For speed, you may want to screen the pointlist to see if it actually can be smoothed before calling this function.
Returns the new smoothed pointlist.
Creates a new smoothed pointlist by applying the Spline smoothing algorithm to the polyline/polygon for all nodes within the pointlist. The Spline smoothing algorithm does not respect nodes while it smooths the curve.
The addnodes specifies how many nodes to add during the smoothing process.
An integer indicating whether to preserve end points.
SMOOTH_PIN : preserve end points
SMOOTH_NO_PIN : do not preserve end points
Pinning applies to polylines only, which are defined as a pointlist where the first point is not equal to the last point.
Usage:
This function applies to polyline and polygon pointlists.
For this function to work correctly you will require at least three points to perform the smooth. If you have less than three points this function will return the original pointlist.
For speed, you may want to screen the pointlist to see if it actually can be smoothed before calling this function.
Computes the area for a surface.
Returns the angle between point at x,y and the origin.
Positive angle computed in a counter-clockwise fashion. When x is 0 and y is 0, the return angle will be 0.
Arguments:
- x : float
- A point along the x-axis.
- y : float
- A point along the y-axis.
Computes the cross product between two vectors.
Arguments:
- v1, v2 : list
- Vectors (see Standard Definitions).
Computes the dot product between two vectors.
Arguments:
- v1, v2 : list
- Vectors (see Standard Definitions).
The dot product between v1 and v2, in other words,
Returns the length of a vector.
Scales a vector.
A vector [i, j, k] that is scaled by the scaling factor.
Equivalent to,
[el*scale for el in vector]
Returns change basis matrix as a 4x4 list of floats.
Arguments:
- x : list
- Vector (see Standard Definitions).
- y : list
- Vector (see Standard Definitions).
- z : list
- Vector (see Standard Definitions).
example: [1,0,0], [0,1,0], [0,0,1]
This will create a change basis matrix going from cartesian coordinates to the given cartesian coordinates in the x,y,z vectors out in space.
To do the opposite of going from the x,y,z vectors out in space to 0,0,0 cartesian then transpose the matrix.
The x, y, z points in represent the coordinate system out in space. These should be at right angles to each other. In other words, the dot product of each vector with any of the other vectors will be 0.
Returns modeling matrix for the desired point.
Arguments:
position : a list of three vectors representing the point, up vector, right vector
example: [[59661.46,58083.77,0.00],[59661.46,58083.77,1.00],[59662.46,58083.77,0.00]]origin : a point representing the origin of the geometry object
example: [55300.0, 52300.0, -400.0]
Returns position from the modeling matrix and origin of the geometry object.
Arguments:
matrix : a 4x4 list of floats representing the modeling matrix
example: [[1.0, 0.0, 0.0, 4361.],[0.0, 1.0, 0.0, 5783.],[0.0, 0.0, -1.0, 100.0],[0.0, 0.0, 0.0, 1.0]]origin : a point representing the origin of the geometry object
example: (55300.0, 52300.0, -400.0)
Returns transposed 4x4 matrix of floats.
Arguments:
matrix : List of 4 lists (or tuples) containing four floats each.
CLOCKWISE COUNTER_CLOCKWISE
Constants indicate direction of polyline/polygon.
EPSILON
Default constant generally used to determine closeness between two floating point numbers. The value is 0.001.
MAX_REAL
The maximum size of a real number on the operating system.
MAX_REAL_DIGITS
The maximum number of digits in a real number on the operating system.
SMOOTH_PIN SMOOTH_NO_PIN
Constants indicate whether to preserve original nodes of polyline/polygon.
STD_RND_PRECISION
Number of digits to use when doing a signficant figure reduction.
[1] | "Homepage". Numerical Python. 11 May 2004. |
[2] | (1, 2) "Built-in Exceptions" Python Library Reference. 19 December 2001. 31 May 2004. <http://www.python.org/doc/2.2/lib/module-exceptions.html> |