Skip to content

Commit f9e68fd

Browse files
committed
minor improvements
1 parent 3516430 commit f9e68fd

File tree

4 files changed

+65
-62
lines changed

4 files changed

+65
-62
lines changed

pygem/nurbshandler.py

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,24 @@
88
import numpy as np
99
from OCC.BRep import BRep_Tool, BRep_Builder, BRep_Tool_Curve
1010
from OCC.BRepAlgo import brepalgo_IsValid
11-
from OCC.BRepBuilderAPI import BRepBuilderAPI_MakeEdge
12-
from OCC.BRepBuilderAPI import BRepBuilderAPI_MakeFace
13-
from OCC.BRepBuilderAPI import BRepBuilderAPI_NurbsConvert
14-
from OCC.BRepBuilderAPI import BRepBuilderAPI_MakeWire
15-
from OCC.BRepBuilderAPI import BRepBuilderAPI_Sewing
11+
from OCC.BRepBuilderAPI import (BRepBuilderAPI_MakeEdge,
12+
BRepBuilderAPI_MakeFace, BRepBuilderAPI_NurbsConvert,
13+
BRepBuilderAPI_MakeWire, BRepBuilderAPI_Sewing)
1614
from OCC.BRepOffsetAPI import BRepOffsetAPI_FindContigousEdges
1715
from OCC.Display.SimpleGui import init_display
18-
from OCC.GeomConvert import geomconvert_SurfaceToBSplineSurface
19-
from OCC.GeomConvert import geomconvert_CurveToBSplineCurve
16+
from OCC.GeomConvert import (geomconvert_SurfaceToBSplineSurface,
17+
geomconvert_CurveToBSplineCurve)
2018
from OCC.gp import gp_Pnt, gp_XYZ
2119
from OCC.Precision import precision_Confusion
2220
from OCC.ShapeAnalysis import ShapeAnalysis_WireOrder
2321
from OCC.ShapeFix import ShapeFix_ShapeTolerance, ShapeFix_Shell
2422
from OCC.StlAPI import StlAPI_Writer
2523
from OCC.TColgp import TColgp_Array1OfPnt, TColgp_Array2OfPnt
26-
from OCC.TopAbs import TopAbs_FACE, TopAbs_EDGE, TopAbs_WIRE
27-
from OCC.TopAbs import TopAbs_FORWARD, TopAbs_SHELL
24+
from OCC.TopAbs import (TopAbs_FACE, TopAbs_EDGE, TopAbs_WIRE, TopAbs_FORWARD,
25+
TopAbs_SHELL)
2826
from OCC.TopExp import TopExp_Explorer, topexp
29-
from OCC.TopoDS import topods_Face, TopoDS_Compound, topods_Shell
30-
from OCC.TopoDS import topods_Edge, topods_Wire, topods, TopoDS_Shape
27+
from OCC.TopoDS import (topods_Face, TopoDS_Compound, topods_Shell,
28+
topods_Edge, topods_Wire, topods, TopoDS_Shape)
3129
from matplotlib import pyplot
3230
from mpl_toolkits import mplot3d
3331
from stl import mesh
@@ -38,8 +36,8 @@ class NurbsHandler(fh.FileHandler):
3836
"""
3937
Nurbs file handler base class
4038
41-
:cvar string infile: name of the input file to be processed.
42-
:cvar string outfile: name of the output file where to write in.
39+
:cvar str infile: name of the input file to be processed.
40+
:cvar str outfile: name of the output file where to write in.
4341
:cvar list control_point_position: index of the first NURBS
4442
control point (or pole) of each face of the files.
4543
:cvar TopoDS_Shape shape: shape meant for modification.
@@ -48,11 +46,10 @@ class NurbsHandler(fh.FileHandler):
4846
4947
.. warning::
5048
51-
- For non trivial geometries it could be necessary to increase
52-
the tolerance. Linking edges into a single wire and then
53-
trimming the surface with the wire can be hard for the
54-
software, especially when the starting CAD has not been made
55-
for analysis but for design purposes.
49+
For non trivial geometries it could be necessary to increase the
50+
tolerance. Linking edges into a single wire and then trimming the
51+
surface with the wire can be hard for the software, especially when the
52+
starting CAD has not been made for analysis but for design purposes.
5653
"""
5754

5855
def __init__(self):
@@ -66,7 +63,7 @@ def _check_infile_instantiation(self):
6663
"""
6764
This private method checks if `self.infile` and `self.shape` are
6865
instantiated. If not it means that nobody called the parse method
69-
and at least one of them is None` If the check fails it raises a
66+
and at least one of them is None. If the check fails it raises a
7067
RuntimeError.
7168
"""
7269
if not self.shape or not self.infile:
@@ -80,8 +77,8 @@ def load_shape_from_file(self, filename):
8077
Not implemented, it has to be implemented in subclasses.
8178
"""
8279
raise NotImplementedError(
83-
'Subclass must implement abstract method ' +
84-
self.__class__.__name__ + '.load_shape_from_file')
80+
'Subclass must implement abstract method'
81+
'{}.load_shape_from_file'.format(self.__class__.__name__))
8582

8683
def parse(self, filename):
8784
"""
@@ -135,7 +132,8 @@ def parse(self, filename):
135132
control_point_coordinates.Z()
136133
]
137134
i += 1
138-
# pushing the control points coordinates to the mesh_points array (used for FFD)
135+
# pushing the control points coordinates to the mesh_points array
136+
# (used for FFD)
139137
mesh_points = np.append(
140138
mesh_points, control_polygon_coordinates, axis=0)
141139
control_point_position.append(
@@ -148,13 +146,13 @@ def parse(self, filename):
148146

149147
def write(self, mesh_points, filename, tolerance=None):
150148
"""
151-
Writes a output file, called filename, copying all the structures
149+
Writes a output file, called `filename`, copying all the structures
152150
from self.filename but the coordinates. `mesh_points` is a matrix
153151
that contains the new coordinates to write in the output file.
154152
155-
:param numpy.ndarray mesh_points: it is a `n_points`-by-3 matrix
156-
containing the coordinates of the points of the mesh
157-
:param string filename: name of the output file.
153+
:param numpy.ndarray mesh_points: it is a *n_points*-by-3 matrix
154+
containing the coordinates of the points of the mesh.
155+
:param str filename: name of the output file.
158156
:param float tolerance: tolerance for the construction of the faces
159157
and wires in the write function. If not given it uses
160158
`self.tolerance`.
@@ -217,7 +215,8 @@ def write(self, mesh_points, filename, tolerance=None):
217215
edge = topods_Edge(edge_explorer.Current())
218216
# edge in the (u,v) coordinates
219217
edge_uv_coordinates = BRep_Tool.CurveOnSurface(edge, face_aux)
220-
# evaluating the new edge: same (u,v) coordinates, but different (x,y,x) ones
218+
# evaluating the new edge: same (u,v) coordinates, but
219+
# different (x,y,x) ones
221220
edge_phis_coordinates_aux = BRepBuilderAPI_MakeEdge(
222221
edge_uv_coordinates[0], brep_face)
223222
edge_phis_coordinates = edge_phis_coordinates_aux.Edge()
@@ -238,10 +237,15 @@ def write(self, mesh_points, filename, tolerance=None):
238237

239238
def check_topology(self):
240239
"""
241-
Method to check the topology of imported geometry.
242-
:return: 0: 1 solid = 1 shell = n faces
243-
1: 1 solid = 0 shell = n free faces
244-
2: 1 solid = n shell = n faces (1 shell = 1 face)
240+
Method to check the topology of imported geometry; it sets
241+
*self.check_topo* as:
242+
243+
- 0 if 1 solid = 1 shell = n faces
244+
- 1 if 1 solid = 0 shell = n free faces
245+
- 2 if 1 solid = n shell = n faces (1 shell = 1 face)
246+
247+
:return: {0, 1, 2}
248+
:rtype: int
245249
"""
246250
# read shells and faces
247251
shells_explorer = TopExp_Explorer(self.shape, TopAbs_SHELL)
@@ -256,10 +260,10 @@ def check_topology(self):
256260
n_faces += 1
257261
faces_explorer.Next()
258262

259-
print("##############################################\n"
260-
"Model statistics -- Nb Shells: {0} Faces: {1} \n"
261-
"----------------------------------------------\n".format(
262-
n_shells, n_faces))
263+
# print("##############################################\n"
264+
# "Model statistics -- Nb Shells: {0} Faces: {1} \n"
265+
# "----------------------------------------------\n".format(
266+
# n_shells, n_faces))
263267

264268
if n_shells == 0:
265269
self.check_topo = 1
@@ -271,18 +275,14 @@ def check_topology(self):
271275
@staticmethod
272276
def parse_face(topo_face):
273277
"""
274-
Method to parse a single Face (a single patch nurbs surface).
278+
Method to parse a single `Face` (a single patch nurbs surface).
275279
It returns a matrix with all the coordinates of control points of the
276-
Face and a second list with all the control points related to the
277-
Edges of the Face.
278-
279-
:param topo_face: the input Face
280-
281-
:return: mesh_points_face: it is a `n_points`-by-3 matrix containing the
282-
coordinates of the control points of the Face (a nurbs surface)
280+
`Face` and a second list with all the control points related to the
281+
`Edges` of the `Face.`
283282
284-
:return: mesh_points_edge: it is a list of `n_points`-by-3 matrix
283+
:param Face topo_face: the input Face.
285284
285+
:return: control points of the `Face`, control points related to `Edges`.
286286
:rtype: tuple(numpy.ndarray, list)
287287
288288
"""
@@ -361,7 +361,7 @@ def parse_shape(self, filename):
361361
of each Face and a second list with all the control points related to
362362
Edges of each Face.
363363
364-
:param filename: the input filename.
364+
:param str filename: the input filename.
365365
366366
:return: list of (mesh_points: `n_points`-by-3 matrix containing
367367
the coordinates of the control points of the Face (surface),
@@ -694,8 +694,9 @@ def write_shape(self, l_shells, filename, tol):
694694
# add the new shell to the global compound
695695
global_compound_builder.Add(global_comp, new_shell)
696696

697-
print("Shell {0} of type {1} Processed ".format(0, itype))
698-
print "=============================================="
697+
# TODO print to logging
698+
# print("Shell {0} of type {1} Processed ".format(0, itype))
699+
# print "=============================================="
699700

700701
self.write_shape_to_file(global_comp, self.outfile)
701702

pygem/params/ffdparams.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -364,28 +364,29 @@ def save_points(self, filename, write_deformed=True):
364364
y = np.linspace(0, self.lenght_box[1], self.n_control_points[1])
365365
z = np.linspace(0, self.lenght_box[2], self.n_control_points[2])
366366

367-
lattice_y_coords, lattice_x_coords, lattice_z_coords = np.meshgrid(y, x,
368-
z)
367+
lattice_y_coords, lattice_x_coords, lattice_z_coords = np.meshgrid(
368+
y, x, z)
369369

370370
if write_deformed:
371371
box_points = np.array([
372-
lattice_x_coords.ravel() + self.array_mu_x.ravel() *
373-
self.lenght_box[0], lattice_y_coords.ravel() +
372+
lattice_x_coords.ravel() +
373+
self.array_mu_x.ravel() * self.lenght_box[0],
374+
lattice_y_coords.ravel() +
374375
self.array_mu_y.ravel() * self.lenght_box[1],
375-
lattice_z_coords.ravel() + self.array_mu_z.ravel() *
376-
self.lenght_box[2]
376+
lattice_z_coords.ravel() +
377+
self.array_mu_z.ravel() * self.lenght_box[2]
377378
])
378379
else:
379380
box_points = np.array([
380-
lattice_x_coords.ravel(), lattice_y_coords.ravel(),
381+
lattice_x_coords.ravel(),
382+
lattice_y_coords.ravel(),
381383
lattice_z_coords.ravel()
382384
])
383385

384386
n_rows = box_points.shape[1]
385387

386-
box_points = np.dot(
387-
self.rotation_matrix,
388-
box_points) + np.transpose(np.tile(self.origin_box, (n_rows, 1)))
388+
box_points = np.dot(self.rotation_matrix, box_points) + np.transpose(
389+
np.tile(self.origin_box, (n_rows, 1)))
389390

390391
points = vtk.vtkPoints()
391392

pygem/params/rbfparams.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,6 @@ def save_points(self, filename, write_deformed=True):
234234
writer.SetInputData(data)
235235
writer.Write()
236236

237-
238237
def plot_points(self, filename=None):
239238
"""
240239
Method to plot the control points. It is possible to save the resulting

pygem/radial.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -309,11 +309,13 @@ def perform(self):
309309
This method performs the deformation of the mesh points. After the
310310
execution it sets `self.modified_mesh_points`.
311311
"""
312-
n_points = self.original_mesh_points.shape[0]
313-
dist = self.basis(
312+
n_mesh_points = self.original_mesh_points.shape[0]
313+
n_control_points = self.parameters.original_control_points.shape[0]
314+
H = np.zeros((n_mesh_points, n_control_points+3+1))
315+
H[:, :n_control_points] = self.basis(
314316
cdist(self.original_mesh_points,
315317
self.parameters.original_control_points),
316318
self.parameters.radius)
317-
identity = np.ones((n_points, 1))
318-
H = np.bmat([[dist, identity, self.original_mesh_points]])
319+
H[:, n_control_points] = 1.0
320+
H[:, -3:] = self.original_mesh_points
319321
self.modified_mesh_points = np.asarray(np.dot(H, self.weights))

0 commit comments

Comments
 (0)