geoclide.trianglemesh module#
- class geoclide.trianglemesh.Triangle(p0=None, p1=None, p2=None, oTw=None, wTo=None, p0t=None, p1t=None, p2t=None)#
Bases:
Shape
Creation of the class Triangle
- Parameters:
- p0Point
The first point(s) of the triangle(s)
- p1Point
The second point(s) of the triangle(s)
- p2Point
The the third point(s) of the triangle(s)
- oTwTransform, optional
From object to world space or the transformation applied to the triangle
- wToTransform, optional
From world to object space or the in inverse transformation applied to the triangle
- p0tPoint, optional
If given circumvent the automatically computed p0t (p0 after applying transformation)
- p1tPoint, optional
If given circumvent the automatically computed p1t (p1 after applying transformation)
- p2tPoint, optional
If given circumvent the automatically computed p2t (p2 after applying transformation)
Methods
area
()compute the area of the triangle
intersect
(r[, method, diag_calc, ds_output])Test if a ray/set of rays intersect with the triangle(s) and return intersection information
intersect_v2
(r[, diag_calc, ds_output])intersect_v3
(r[, diag_calc, ds_output])is_intersection
(r[, method, diag_calc])Test if a ray/set of rays intersect with the triangle(s)
is_intersection_t
(r[, method, diag_calc])Test if a ray/set of rays intersect with the triangle(s)
is_intersection_v2
(r[, diag_calc])is_intersection_v2_t
(r[, diag_calc])is_intersection_v3
(r, diag_calc)is_intersection_v3_t
(r[, diag_calc])- area()#
compute the area of the triangle
Warning
the scale transformation is not considered for the area calculation!
- intersect(r, method='v3', diag_calc=False, ds_output=True)#
Test if a ray/set of rays intersect with the triangle(s) and return intersection information
- Parameters:
- rRay
The ray(s) to use for the intersection test(s)
- methodstr, optional
Tow choice -> ‘v2’ (use mainly pbrt v2 intersection test method) or ‘v3’ (pbrt v3)
- diag_calcbool, optional
Perform diagonal calculations in case Triangle and Ray have ndarray point components, meaning the output is a 1-D array instead of a 2-D array where out[i] is calculated using r(i) and triangle(i). The same size for the Triangle and the Ray is required.
- ds_outputBool, optional
If True the output is a dataset, else return a tuple with intersection information variables
- Returns:
- outxr.Dataset | tuple
Look-up table with the intersection information if ds_output is True, else return a tuple (ready to be an input for the function get_intersect_dataset in geoclide/shapes.py). Form of the tuple:
- shape_namestr
-> The shape class name
- rRay
-> The ray(s) used for the intersection test
- tNone | float | 1-D ndarray | 2-D ndarray
-> The t ray variable(s) for its first intersection at the shape surface
- is_intersectionbool | 1-D ndarray | 2-D ndarray
-> If there is an intersection return True, else False
- uNone | float | 1-D ndarray | 2-D ndarray
-> The u coordinate(s) of the parametric representation
- vNone | float | 1-D ndarray | 2-D ndarray
-> The u coordinate(s) of the parametric representation
- dpduNone | 1-D ndarray | 2-D ndarray
-> The surface partial derivative(s) of phit with respect to u
- dpdvNone | 1-D ndarray | 2-D ndarray
-> The surface partial derivative(s) of phit with respect to v
- diag_calbool
-> This indicates whether a diagonal calculation has been performed
Notes
By default the ‘v3’ method is used since there are more robustness tests. But the ‘v2’ method is at least twice faster than ‘v3’.
- is_intersection(r, method='v3', diag_calc=False)#
Test if a ray/set of rays intersect with the triangle(s)
- Parameters:
- rRay
The ray(s) to use for the intersection test(s)
- methodstr, optional
Tow choice -> ‘v2’ (use mainly pbrt v2 intersection test method) or ‘v3’ (pbrt v3)
- diag_calcbool, optional
Perform diagonal calculations in case Triangle and Ray have ndarray point components, meaning the output is a 1-D array instead of a 2-D array where out[i] is calculated using r(i) and triangle(i). The same size for the Triangle and the Ray is required.
- Returns:
- outbool | 1-D ndarray | 2-D ndarray
If there is an intersection -> True, else False
- is_intersection_t(r, method='v3', diag_calc=False)#
Test if a ray/set of rays intersect with the triangle(s)
- Parameters:
- rRay
The ray(s) to use for the intersection test(s)
- methodstr, optional
Tow choice -> ‘v2’ (use mainly pbrt v2 intersection test method) or ‘v3’ (pbrt v3)
- diag_calcbool, optional
Perform diagonal calculations in case Triangle and Ray have ndarray point components, meaning the output is a 1-D array instead of a 2-D array where out[i] is calculated using r(i) and triangle(i). The same size for the Triangle and the Ray is required.
- Returns:
- thitNone | float | 1-D ndarray | 2-D ndarray
The t ray variable(s) for its first intersection at the shape surface
- is_intersectionbool | 1-D ndarray | 2-D ndarray
If there is an intersection -> True, else False
- class geoclide.trianglemesh.TriangleMesh(vertices, faces, oTw=None, wTo=None)#
Bases:
Shape
Creation of the class TriangleMesh
- Parameters:
- vertices2-D ndarray
The vertices xyz coordinates. It is a 2d ndarray of size (nvertices, 3) where the first element is the coordinate of first vertex and so on
- faces2-D ndarray
The vertices indices of triangles, a 2d ndarray of shape (ntriangles, 3). The 3 first indices are the vertices (p0, p1 and p3) indices of the first triangle and so on
- oTwTransform, optional
From object to world space or the transformation applied to the triangle mesh
- wToTransform, optional
From world to object space or the in inverse transformation applied to the triangle mesh
Methods
apply_tf
(t)Apply transformation to the triangle mesh
area
()compute the area of the triangle mesh
intersect
(r[, method, diag_calc, ds_output, ...])Test if a ray/set of rays intersect with the triangle mesh and return intersection information
is_intersection
(r[, method, diag_calc, use_loop])Test if a ray/set of rays intersect with the triangle mesh
is_intersection_t
(r[, method, diag_calc, ...])Test if a ray/set of rays intersect with the triangle mesh
plot
([source, savefig_name])Plot the triangle mesh
to_dataset
([name])Create an xarray dataset where the triangle mesh information are stored
write
(path, **kwargs)Save the mesh
- apply_tf(t)#
Apply transformation to the triangle mesh
- Parameters:
- tTranform
The transfomation matrix to apply
- area()#
compute the area of the triangle mesh
Warning
the scale transformation is not considered for the area calculation!
- intersect(r, method='v3', diag_calc=False, ds_output=True, use_loop=False)#
Test if a ray/set of rays intersect with the triangle mesh and return intersection information
- Parameters:
- rRay
The ray(s) to use for the intersection test(s)
- methodstr, optional
Tow choice -> ‘v2’ (use mainly pbrt v2 triangle intersection test method) or ‘v3’ (pbrt v3)
- diag_calcbool, optional
Perform diagonal calculations between r(i) and triangle(i). The number of triangles must be equal to the number of rays
- use_loopbool, optional
If True -> scalar calculations over a loop (instead of using numpy). It can be useful for debugging
- ds_outputBool, optional
If True the output is a dataset, else return a tuple with intersection information variables
- Returns:
- outxr.Dataset | tuple
Look-up table with the intersection information if ds_output is True, else return a tuple (ready to be an input for the function get_intersect_dataset in geoclide/shapes.py). Form of the tuple:
- shape_namestr
-> The shape class name
- rRay
-> The ray(s) used for the intersection test
- tNone | float | 1-D ndarray
-> The t ray variable(s) for its first intersection at the shape surface
- is_intersectionbool | 1-D ndarray
-> If there is an intersection return True, else False
- uNone | float | 1-D ndarray
-> The u coordinate(s) of the parametric representation
- vNone | float | 1-D ndarray
-> The u coordinate(s) of the parametric representation
- dpduNone | 1-D ndarray | 2-D ndarray
-> The surface partial derivative(s) of phit with respect to u
- dpdvNone | 1-D ndarray | 2-D ndarray
-> The surface partial derivative(s) of phit with respect to v
- diag_calbool
-> This indicates whether a diagonal calculation has been performed
- is_intersection(r, method='v3', diag_calc=False, use_loop=False)#
Test if a ray/set of rays intersect with the triangle mesh
- Parameters:
- rRay
The ray(s) to use for the intersection test(s)
- methodstr, optional
Tow choice -> ‘v2’ (use mainly pbrt v2 triangle intersection test method) or ‘v3’ (pbrt v3)
- diag_calcbool, optional
Perform diagonal calculations between r(i) and triangle(i). The number of triangles must be equal to the number of rays
- use_loopbool, optional
If True -> scalar calculations over a loop (instead of using numpy). It can be useful for debugging
- Returns:
- outbool | 1-D ndarray
If there is an intersection -> True, else False
- is_intersection_t(r, method='v3', diag_calc=False, use_loop=False)#
Test if a ray/set of rays intersect with the triangle mesh
- Parameters:
- rRay
The ray(s) to use for the intersection test(s)
- methodstr, optional
Tow choice -> ‘v2’ (use mainly pbrt v2 triangle intersection test method) or ‘v3’ (pbrt v3)
- diag_calcbool, optional
Perform diagonal calculations between r(i) and triangle(i). The number of triangles must be equal to the number of rays
- use_loopbool, optional
If True -> scalar calculations over a loop (instead of using numpy). It can be useful for debugging
- Returns:
- thitNone | float | 1-D ndarray
The t ray variable(s) for its first intersection at the shape surface
- is_intersectionbool | 1-D ndarray
If there is an intersection -> True, else False
Notes
If use_loop = True, is_intersection_t can be significantly more consuming than is_intersection. Because it does not stop at the first intersection, but it finalize the complete loop to return the thit corresponding to the nearest triangle.
- plot(source=None, savefig_name=None, **kwargs)#
Plot the triangle mesh
- Parameters:
- sourcestr
The package used for the plot, only 2 option -> ‘matplotlib’ or ‘trimesh’. If source = None, use matplolib for mesh with ntriangle < 5000, else use trimesh
- savefig_namestr, optional
If savefig_name is given, the figure is saved with the given name (only if source=’matplotlib)
- **kwargs
All other keyword arguments are passed on to matplotlib plot_trisurf function. For example: alpha, color, shade, … If source = ‘trimesh’ then the keyword arguments passed on to show Trimesh method
Examples
>>> import geoclide as gc >>> prolate = gc.Spheroid(radius_xy=1.5, radius_z=3.) >>> msh = prolate.to_trianglemesh() >>> msh.plot(color='green', edgecolor='k') image
- to_dataset(name='none')#
Create an xarray dataset where the triangle mesh information are stored
- Parameters:
- namestr, optional
The name of the triangle mesh to be stored
- Returns:
- outxr.Dataset
The dataset with the triangle mesh information
- write(path, **kwargs)#
Save the mesh
if gcnc format use xarray, else use trimesh
- Parameters:
- pathstr
The xarray to_netcdf path paramter (or trimesh)
- **kwargs
The keyword arguments are passed on to xarray to_netcdf or trimesh export method
- geoclide.trianglemesh.read_gcnc_trianglemesh(path, **kwargs)#
Read geoclide netcdf4 format and convert it to a TriangleMesh class object
- Parameters:
- pathstr
The xarray filename_or_obj parameter
- **kwargs
The keyword arguments are passed on to xarray open_dataset method
- Returns:
- outTriangleMesh
The triangle mesh
- geoclide.trianglemesh.read_trianglemesh(path, **kwargs)#
Open mesh file
if gcnc format use xarray, else use trimesh
- Parameters:
- pathstr
The xarray filename_or_obj or trimesh file_obj parameter
- **kwargs
The keyword arguments are passed on to xarray open_dataset or trimesh load method
- Returns:
- outTriangleMesh
The triangle mesh