Modeling Module

Contents

Modeling Module#

The modeling module contains geometric modeling and grid generation components.

Functions for creating Level-1 of Aligned Edge Polycube mesh and Adaptive Exterior Grid.

class EMFieldML.Modeling.make_first_grid.Point(point)#

Bases: object

Class for points that divide the surface of the ferrite shield into a mesh.

xyz#

Coordinates of the point.

Type:

ndarray(3)

normal#

Vector indicating the direction the point will move next.

Type:

ndarray(3)

corner#

Whether the point is a corner of the shape or not.

Type:

bool

__init__(point)#
Parameters:

point (ndarray(3)) – Coordinates of the point.

class EMFieldML.Modeling.make_first_grid.Point_outside(point)#

Bases: object

Class for predicted points on the outside of the ferrite shield.

xyz#

Coordinates of the point.

Type:

ndarray(3)

index#

If the point is the same as a point on the surface of the ferrite shield, the index of that point in ‘points’. Default is -1.

Type:

int

mode#

Where this point belongs. 0 : predicted point, 1 : TX, 2 : RX.

Type:

int

default_point#

Coordinates of the point before it moves (used for calculating weights, etc. when the shape of the ferrite changes).

Type:

ndarray(3)

weight#

Weight for performing linear blend skinning on the points of the ferrite shield.

Type:

ndarray(n)

parent#

Index of the parent points.

Type:

list(int,)

edges#

Store the index of the edge extending from this point to simplify the search.

Type:

list(int,)

plane#

Store the index of the plane to which this point belongs to simplify the search.

Type:

list(int,)

__init__(point)#
Parameters:

point (ndarray(3)) – Coordinates of the point.

calculate_weight(point)#

Calculate the distance from the given point to the points of the initial shape of the ferrite shield and convert it to weight.

Parameters:

point (ndarray(3)) – Coordinates of the point.

Returns:

np.array(all_d) – Weight used for shape transformation.

Return type:

ndarray(n)

calculate_nextpoint(next_point)#

Calculate next point position.

class EMFieldML.Modeling.make_first_grid.Edge(points, point1_index, point2_index)#

Bases: object

Edge class for mesh generation.

__init__(points, point1_index, point2_index)#

Initialize edge between two points.

calculate_ray(vector)#

Calculate ray intersection with edge.

class EMFieldML.Modeling.make_first_grid.Edge_outside(index1, index2)#

Bases: object

Edge class for outside points.

__init__(index1, index2)#

Initialize outside edge between two points.

class EMFieldML.Modeling.make_first_grid.Plane(points, edges, points_index)#

Bases: object

Plane class for mesh generation.

__init__(points, edges, points_index)#

Initialize plane with four points.

class EMFieldML.Modeling.make_first_grid.Plane_outside(index1, index2, index3, index4)#

Bases: object

Plane class for outside points.

__init__(index1, index2, index3, index4)#

Initialize outside plane with four points.

class EMFieldML.Modeling.make_first_grid.Box(points_index)#

Bases: object

Box class for mesh generation.

__init__(points_index)#

Initialize box with eight corner points.

EMFieldML.Modeling.make_first_grid.initial_point(nowpoints, model_points, i, A, B, C, normal)#

Project 8 corner points of a cube onto the shape. Create a plane using adjacent corner points and project to the nearest point on the plane.

Parameters:
  • nowpoints (list(ndarray(3))) – 投影する前の角の8点の座標のリスト

  • model_points (list(ndarray(3))) – 形状の点の座標のみを抽出したリスト

  • i (int) – 対応するnow_pointsのindex

  • A (int) – 隣接する角の点のindex

  • B (int) – 隣接する角の点のindex

  • C (int) – 隣接する角の点のindex

  • normal (ndarray(3)) – その点における法線ベクトル。角の点なので隣接する平面の法線ベクトルを合計したもの。

Returns:

投影後の点

Return type:

class Point

EMFieldML.Modeling.make_first_grid.initial_edge(nodes, initial_point, vector1, vector2)#

Create initial edge with ray intersection.

EMFieldML.Modeling.make_first_grid.calculate_ray(vector, normal)#

Calculate ray direction from vector and normal.

EMFieldML.Modeling.make_first_grid.initial_edge_point(nodes, initial_point, vector1, vector2, normal)#

Create initial edge point with normal.

EMFieldML.Modeling.make_first_grid.initial_plane_point(nodes, point, ray, normal)#

Create initial plane point with normal.

EMFieldML.Modeling.make_first_grid.define_initial_normal(points, center, neighbor)#

Define initial normal vector for points.

EMFieldML.Modeling.make_first_grid.calculate_weight(initial)#

Calculate weights for linear blend skinning.

EMFieldML.Modeling.make_first_grid.make_first_point_edge_plane(nodes, model_points)#

Create first level points, edges, and planes.

EMFieldML.Modeling.make_first_grid.make_first_out_points(points, planes)#

Determine the level-1 placement of predicted points (out_points).

Parameters:
  • points (list(class Point,)) – List of existing points

  • planes (list(class Plane)) – List of existing planes

Returns:

  • out_points (list(class Point_outside,)) – List of existing out_points.

  • boxes (list(class Box,)) – List of existing boxes.

  • edges_outside (list(class Edge_outside,)) – List of existing edges_outside.

  • planes_outside (list(class Plane_outside,)) – List of existing planes_outside.

Functions for creating Level-2 and Level-3 of Aligned Edge Polycube mesh and Adaptive Exterior Grid.

EMFieldML.Modeling.make_second_grid.calculate_ray(vector, normal)#

Calculate ray direction for edge intersection.

EMFieldML.Modeling.make_second_grid.search_edge_point(nodes, points, edge)#

Search for edge points with specific normal adjustments.

EMFieldML.Modeling.make_second_grid.make_points_from_edge(nodes, points, edge)#

Create points from edge with proper normal direction.

EMFieldML.Modeling.make_second_grid.make_points_from_parallel_edge(nodes, points, edge)#

Create points from parallel edge with vector calculations.

EMFieldML.Modeling.make_second_grid.check_totu(point1, point2, point3, point4)#

Check if four points form a valid quadrilateral.

EMFieldML.Modeling.make_second_grid.make_points_from_plane(nodes, points, edges, plane)#

Create points from plane with validation.

EMFieldML.Modeling.make_second_grid.set_boxes(box, out_points, edges_outside, planes_outside)#

Set box properties with edges and planes.

EMFieldML.Modeling.make_second_grid.make_second_grid(nodes, points, edges, planes)#

Create second level grid from existing mesh elements.

EMFieldML.Modeling.make_second_grid.make_second_out_points(out_points, boxes, edges_outside, planes_outside, points, edges, planes)#

Create second level outside points from existing elements.

File for making Aligned Edge Polycube mesh.

class EMFieldML.Modeling.PolyCubeMaker.PolyCubeMaker#

Bases: object

Class for creating and managing PolyCube mesh models. Shield Geometry and Aligned-Edge Polycube Mesh This process generates a computational mesh for the shield geometry using an aligned-edge polycube approach.

Initial Point Generation: The mesh begins with a set of initial points that include both corner and surface points, forming cubes that precisely fit the shield. Mesh Subdivision: The initial mesh is then subdivided by interpolating new points between the existing ones. By aligning the polycube edges to the geometry, this method minimizes the number of points required to accurately represent the shield.

Adaptive Exterior Grid Generation An adaptive exterior grid is generated to support electromagnetic field prediction.

Initial Positioning: Exterior cubes are initially positioned around the shield-fitting polycubes. They are adjusted to conform to the shield geometry’s shape. Subdivision: These exterior polycubes are further subdivided through interpolation relative to the exterior cubes, creating a refined grid. Dynamic Adjustment: This adaptive exterior grid is designed to maintain its relative position to the transmitter and receiver coils. This adjustment accounts for any changes in the coils’ relative positions and helps prevent abrupt variations in the magnetic field predictions that would otherwise occur with movement.

For further details, please refer to Figure 2 in the paper.

static make_modeling(input_path: Path, output_path_polycube: Path, output_path_exterior: Path) None#

Create PolyCube mesh modeling from STL file. Aligned-Edge Polycube Mesh Generation The Aligned-Edge Polycube Mesh is created by fitting large cubes to the geometry’s shape and then subdividing them into a finer mesh.

Adaptive Exterior Grid Generation The Adaptive Exterior Grid is created by moving points on a base grid to align with the Aligned-Edge Polycube Mesh, followed by further subdivision.

This process is refined up to a maximum of three levels.

Parameters:
  • input_path – Path to the STL file

  • output_path_polycube – Path to save the PolyCube mesh file

  • output_path_exterior – Path to save the exterior grid file

Returns:

None

static make_modeling_all(n_shield_shape: int = EMFieldML.config.config.n_shield_shape, path_stl_dir: Path = EMFieldML.config.paths.CIRCLE_STL_DIR, path_stl_file: str = EMFieldML.config.template.circle_stl, path_polycube_dir: Path = EMFieldML.config.paths.CIRCLE_X_DIR, path_polycube_file: str = EMFieldML.config.template.circle_x, path_exterior_dir: Path = EMFieldML.config.paths.CIRCLE_PREDICTION_POINT_DIR, path_exterior_file: str = EMFieldML.config.template.circle_prediction_point) None#

Create PolyCube mesh modeling for all shield shapes. This function processes multiple STL files to generate corresponding PolyCube mesh models and exterior grids.

Parameters:
  • n_shield_shape – Number of shield shapes to process

  • path_stl_dir – Directory containing STL files

  • path_stl_file – STL file names

  • path_polycube_dir – Directory to save PolyCube mesh files

  • path_polycube_file – PolyCube mesh file names

  • path_exterior_dir – Directory to save exterior grid files

  • path_exterior_file – Exterior grid file names

Returns:

None

static move_shield_polycube(input_path: Path, shield_index: int, path_output_dir: Path, path_output_file: str, n_shield_shape: int = EMFieldML.config.config.n_shield_shape, y_grid: int = EMFieldML.config.config.y_grid, z_grid: int = EMFieldML.config.config.z_grid, y_step: float = EMFieldML.config.config.y_step, z_step: float = EMFieldML.config.config.z_step) None#

Move shield polycube with grid-based positioning.

Input Data Generation for Machine Learning The input data for the machine learning model is generated from the polycube mesh and the coil positions. Mesh Point Coordinates: This includes the XYZ coordinates of each point on the polycube mesh. Coil Coordinates: This includes the YZ coordinates of the moved (transformed) coils.

Parameters:
  • input_path – Path to the PolyCube mesh file

  • shield_index – Index of the shield shape

  • path_output_dir – Directory to save output files

  • path_output_file – Template for output file names

  • n_shield_shape – Total number of shield shapes

  • y_grid – Number of grid points of coil move in the Y direction

  • z_grid – Number of grid points of coil move in the Z direction

  • y_step – Step size of coil move in the Y direction

  • z_step – Step size of coil move in the Z direction

Returns:

None

static move_shield_exterior(input_path_polycube: Path, input_path_exterior: Path, number: int, y: float, z: float, output_path_dir: Path = EMFieldML.config.paths.CIRCLE_PREDICTION_POINT_DIR, output_path_file: str = EMFieldML.config.template.circle_prediction_point_move, n_prediction_points: int = EMFieldML.config.config.n_prediction_points_level3) None#

Move shield exterior points with linear blend skinning. Linear Blend Skinning (LBS) This module provides an implementation of Linear Blend Skinning (LBS), a computationally efficient algorithm for deforming a 3D mesh based on a skeletal structure. LBS is the industry standard for character animation in computer graphics, including video games and films.

Core Principle LBS operates on the principle that the deformation of a mesh’s vertices is controlled by an underlying skeleton of “bones” or joints. A key challenge, such as deforming the mesh around an elbow joint, is to handle vertices that are influenced by multiple bones (e.g., both the upper arm and forearm bones).

This is solved by assigning a “weight” to each vertex for every bone that influences it. These weights define the degree of influence each bone has on the vertex’s final position. The sum of all weights for a single vertex must equal 1.0.

For further details, please refer to equation (6) in the paper.

Parameters:
  • input_path_polycube – Path to the PolyCube mesh file

  • input_path_exterior – Path to the exterior grid file

  • number – Unique identifier for the current configuration

  • y – Y-coordinate of the coil position

  • z – Z-coordinate of the coil position

  • output_path_dir – Directory to save output files

  • output_path_file – Output file names

  • n_prediction_points – Number of prediction points to process

Returns:

None

static move_shield_polycube_all(n_shield_shape: int = EMFieldML.config.config.n_shield_shape, path_polycube_dir: Path = EMFieldML.config.paths.CIRCLE_X_DIR, path_polycube_file: str = EMFieldML.config.template.circle_x, path_output_dir: Path = EMFieldML.config.paths.CIRCLE_MOVE_X_DIR, path_output_file: str = EMFieldML.config.template.circle_move_x) None#

Add position data to the ferrite shape data created so far. This creates 169 data for one shape.

Parameters:
  • n_shield_shape – Number of shield shapes to process

  • path_polycube_dir – Directory containing polycube data files

  • path_polycube_file – Template for polycube file names

  • path_output_dir – Directory to save output files

  • path_output_file – Template for output file names

static move_shield_exterior_need(n_shield_shape: int = None, input_path: Path = None) None#

Make Exterior Grid for selected shields and positions.

Module for Bounding Volume Hierarchy (BVH) implementation.

class EMFieldML.Modeling.BVH.Triangle(triangle: numpy.ndarray)#

Bases: object

A class to hold information for each triangular mesh.

__init__(triangle: numpy.ndarray)#
Parameters:

triangle (np.ndarray) – Coordinates of the triangle vertices. Expected shape is (3, 3).

class EMFieldML.Modeling.BVH.BVHNode#

Bases: object

Single node in a Bounding Volume Hierarchy (BVH).

__init__()#

Initialize BVH node.

class EMFieldML.Modeling.BVH.BVHBuilder#

Bases: object

Class for building Bounding Volume Hierarchy structures.

__init__()#

Initialize BVH builder.

surface_area(bbox: numpy.ndarray) float#

Calculate surface area of bounding box.

empty_aabb(bbox: numpy.ndarray)#

Initialize empty axis-aligned bounding box.

merge_aabb(bbox1: numpy.ndarray, bbox2: numpy.ndarray, result: numpy.ndarray)#

Merge two axis-aligned bounding boxes.

create_aabb_from_triangles(triangles: list[Triangle], bbox: numpy.ndarray)#

Create AABB from list of triangles.

make_leaf(polygons: list[Triangle], node: BVHNode)#

Create leaf node with polygons.

construct_bvh_internal(polygons: list[Triangle], node_index: int)#

Construct BVH internally.

construct_bvh(triangles: list[Triangle])#

Construct BVH from triangles.

build_bvh(filename: str) list[BVHNode]#

Build BVH from STL file.

EMFieldML.Modeling.BVH.BVH(filename: str) list[BVHNode]#

Create BVH from STL file.

Functions for ray-object intersection and coordinate calculation.

class EMFieldML.Modeling.RayIntersect.Ray#

Bases: object

Class representing a ray for intersection calculations.

__init__()#

Initialize ray with origin and direction.

class EMFieldML.Modeling.RayIntersect.IntersectInformation#

Bases: object

Class for storing intersection information.

__init__()#

Initialize intersection information.

EMFieldML.Modeling.RayIntersect.IntersectAABBvsRay(aabb, ray)#

Check intersection between AABB and ray.

EMFieldML.Modeling.RayIntersect.ray_intersects_triangle(triangle, ray)#

Check intersection between ray and triangle.

EMFieldML.Modeling.RayIntersect.Intersect(nodes, index, ray, info)#

Find intersection with BVH nodes.

EMFieldML.Modeling.RayIntersect.Intersect_all(nodes, index, ray)#

Find all intersections with BVH nodes.

EMFieldML.Modeling.RayIntersect.calculate_point_ray_hit(nodes, ray_org, ray_dir)#

Calculate point where ray hits mesh.

EMFieldML.Modeling.RayIntersect.calculate_point_ray_hit_all(nodes, ray_org, ray_dir)#

Calculate all points where ray hits mesh.

EMFieldML.Modeling.RayIntersect.judge_in_out_for_onevector(nodes, ray)#

Judge if point is inside or outside mesh.

EMFieldML.Modeling.RayIntersect.judge_in_out(nodes, point)#

Judge whether the point is in, out, or on the ferrite shield.

Parameters:
  • nodes (list(class Point)) – The number to specify a ferrite shield.

  • point (Point) – The point to judge in or out of 3-D shape.

Returns:

0: out of the ferrite shield 1: in the ferrite shield 2: on the surface of the ferrite shield

Return type:

int

Calculate the distance between a plane and a point.

EMFieldML.Modeling.calculate_plane_point_distance.distance_from_plane(point, a, b, c, d)#

Calculate the distance between a plane and a point.

Parameters:
  • point (ndarray(3)) – Coordinates of the point.

  • a (float) – Equation of the plane. ax + by + cz + d = 0

  • b (float) – Equation of the plane. ax + by + cz + d = 0

  • c (float) – Equation of the plane. ax + by + cz + d = 0

  • d (float) – Equation of the plane. ax + by + cz + d = 0

Returns:

The distance between a plane and a point

Return type:

float

EMFieldML.Modeling.calculate_plane_point_distance.make_plane_from_edge(point, pointA, pointB, pointC)#

Calculate the equation of a plane that passes through “point” and is parallel to the plane that passes through “pointA”, “pointB”, and “pointC”.

Parameters:
  • point (ndarray(3)) – Point through which the plane passes.

  • pointA (ndarray(3)) – The plane will be parallel to the plane passing through these three points.

  • pointB (ndarray(3)) – The plane will be parallel to the plane passing through these three points.

  • pointC (ndarray(3)) – The plane will be parallel to the plane passing through these three points.

Returns:

normal_vector[0], normal_vector[1], normal_vector[2], d – They are a, b, c, d of the plane equation ax + by + cz + d = 0.

Return type:

float

EMFieldML.Modeling.calculate_plane_point_distance.search_near_points_from_plane(points, a, b, c, d)#

Find the point closest to the given plane from the set of points.

Parameters:
  • points (list(ndarray(3))) – the set of points.

  • a (float) – Equation of the plane. ax + by + cz + d = 0

  • b (float) – Equation of the plane. ax + by + cz + d = 0

  • c (float) – Equation of the plane. ax + by + cz + d = 0

  • d (float) – Equation of the plane. ax + by + cz + d = 0

Returns:

  • ans_distance (float) – Distance between the closest point and the plane.

  • ans_point (ndarray(3)) – The closest point.