forked from LHEEA/meshmagick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeshmagick_cli.py
executable file
·1316 lines (1068 loc) · 54 KB
/
meshmagick_cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# PYTHON_ARGCOMPLTETE_OK
# Python module to manipulate 2D meshes for hydrodynamics purposes
# TODO: Change the following docstring as it is no more up to date
"""
This module contains utility function to manipulate, load, save and
convert surface mesh files used by the hydrodynamics community.
Two numpy arrays are manipulated in this module : vertices and faces.
vertices is the array of nodes coordinates. It is an array of shape (nv, 3) where
nv is the number of nodes in the mesh.
faces is the array of cell connectivities. It is an array of shape (nf, 4) where
nf is the number of cells in the mesh. Not that it has 4 columns as we consider
flat polygonal cells up to 4 edges (quads). Triangles are obtained by repeating
the first node at the end of the cell node ID list.
IMPORTANT NOTE:
IDs of _vertices are internally idexed from 0 in meshmagick. However, several mesh
file format use indexing starting at 1. This different convention might be transparent
to user and 1-indexing may not be present outside the I/O functions
"""
# TODO: move meshmagick.py at the root level of the project ?
import os, sys
# import numpy as np
# import math
from datetime import datetime
# from warnings import warn
from time import strftime
import argparse
from meshmagick.mesh import *
from meshmagick import mmio
from meshmagick.mesh_clipper import MeshClipper
from meshmagick import hydrostatics as hs
from meshmagick import densities
from meshmagick import __version__
__year__ = datetime.now().year
__author__ = "Francois Rongere"
__copyright__ = "Copyright 2014-%u, Ecole Centrale de Nantes" % __year__
__credits__ = "Francois Rongere"
__licence__ = "GPLv3"
__maintainer__ = "Francois Rongere"
__email__ = "Francois.Rongere@ec-nantes.fr"
__status__ = "Development"
__all__ = ['main']
# =======================================================================
# MESH MANIPULATION HELPERS
# =======================================================================
# TODO: those functions should disappear from this module
def _is_point_inside_polygon(point, poly):
"""_is_point_inside_polygon(point, poly)
Internal function to Determine if a point is inside a given polygon.
This algorithm is a ray casting method.
Parameters:
point: ndarray
2D coordinates of the point to be tested
poly: ndarray
numpy array of shape (nv, 2) of 2D coordinates
of the polygon
"""
# TODO: place this code into a utils module
# FIXME : Do we have to repeat the first point at the last position
# of polygon ???
x = point[0]
y = point[1]
n = len(poly)
inside = False
p1x, p1y = poly[0]
for i in range(n):
p2x, p2y = poly[i]
if y > min(p1y, p2y):
if y <= max(p1y, p2y):
if x <= max(p1x, p2x):
if p1y != p2y:
xints = (y - p1y) * (p2x - p1x) / (p2y - p1y) + p1x
if p1x == p2x or x <= xints:
inside = not inside
p1x, p1y = p2x, p2y
return inside
def generate_lid(V, F, max_area=None, verbose=False):
"""generate_lid(_vertices, _faces, max_area=None, verbose=False)
Meshes the lid of a mesh with triangular _faces to be used in irregular frequency
removal in BEM softwares. It clips the mesh againt the plane Oxy, extract the intersection
polygon and relies on meshpy (that is a wrapper around the TRIANGLE meshing library).
It is able to deal with moonpools.
Parameters:
V: ndarray
numpy array of the coordinates of the mesh's nodes
F: ndarray
numpy array of the _faces' nodes connectivities
max_area[optional]: float
The maximum area of triangles to be generated
verbose[optional]: bool
If set to True, generates output along the processing
"""
# TODO: rely on the wrapper done for the triangle lib that has been done in cython and no more on meshpy.
# TODO: Put the reference of TRIANGLE and meshpy (authors...) in the docstring
# TODO: remove verbose mode and place it into the main of meshmagick !!!
# TODO: Faire de cette fonction une methode dans mesh ??? --> non on a un autre module qui wrappe triangle avec
# cython...
try:
import meshpy.triangle as triangle
except:
raise ImportError('Meshpy has to be available to use the generate_lid() function')
if verbose:
print('\n--------------')
print('Lid generation')
print('--------------\n')
# Clipping the mesh with Oxy plane
V, F, clip_infos = clip_by_plane(V, F, Plane(), infos=True)
nv = V.shape[0]
nf = F.shape[0]
if max_area is None:
max_area = get_all_faces_properties(V, F)[0].mean()
# Analysing polygons to find holes
polygons = clip_infos['PolygonsNewID']
nb_pol = len(polygons)
holes = []
boundaries = []
for ipoly, polygon in enumerate(polygons):
points = V[polygon][:, :2]
n = points.shape[0]
# Testing the orientation of each polygon by computing the signed area of it
signed_area = np.array(
[points[j][0] * points[j + 1][1] - points[j + 1][0] * points[j][1] for j in range(n - 1)],
dtype=np.float).sum()
if signed_area < 0.:
holes.append(polygon)
else:
boundaries.append(polygon)
nb_hole = len(holes)
nb_bound = len(boundaries)
hole_dict = dict([(j, []) for j in range(nb_bound)])
if nb_hole > 0:
if verbose:
if nb_hole == 1:
word = 'moonpool has'
else:
word = 'moonpools have'
print(('\t-> %u %s been detected' % (nb_hole, word)))
# TODO : getting a point inside the hole polygon
def pick_point_inside_hole(hole):
# First testing with the geometric center of the hole
point = np.array(hole).sum(axis=0) / len(hole)
if not _is_point_inside_polygon(point, hole):
# Testing something else
raise RuntimeError('The algorithm should be refined to more complex polygon topologies... up to you ?')
return point
# Assigning holes to boundaries
if nb_bound == 1 and nb_hole == 1:
# Obvious case
hole_dict[0].append((0, pick_point_inside_hole(V[holes[0]][:, :2])))
else:
# We may do a more elaborate search
for ihole, hole in enumerate(holes):
P0 = V[hole[0]][:2]
# Testing against all boundary polygons
for ibound, bound in enumerate(boundaries):
if _is_point_inside_polygon(P0, V[bound][:, :2]):
hole_dict[ibound].append((ihole, pick_point_inside_hole(V[hole][:, :2])))
break
def round_trip_connect(start, end):
return [(j, j + 1) for j in range(start, end)] + [(end, start)]
# Meshing every boundaries, taking into account holes
for ibound, bound in enumerate(boundaries):
nvp = len(bound) - 1
# Building the loop
points = list(map(tuple, list(V[bound][:-1, :2])))
edges = round_trip_connect(0, nvp - 1)
info = triangle.MeshInfo()
if len(hole_dict[ibound]) > 0:
for ihole, point in hole_dict[ibound]:
hole = holes[ihole]
points.extend(list(map(tuple, list(V[hole][:-1, :2]))))
edges.extend(round_trip_connect(nvp, len(points) - 1))
# Marking the point as a hole
info.set_holes([tuple(point)])
info.set_points(points)
info.set_facets(edges)
# Generating the lid
mesh = triangle.build(info, max_volume=max_area, allow_boundary_steiner=False)
mesh_points = np.array(mesh.points)
nmp = len(mesh_points)
mesh_tri = np.array(mesh.elements, dtype=np.int32)
# Resizing
nmt = mesh_tri.shape[0]
mesh_quad = np.zeros((nmt, 4), dtype=np.int32)
mesh_quad[:, :-1] = mesh_tri + nv
mesh_quad[:, -1] = mesh_quad[:, 0]
mesh_points_3D = np.zeros((nmp, 3))
mesh_points_3D[:, :-1] = mesh_points
# show(_vertices, _faces)
# return
# Adding the lid to the initial mesh
V = np.append(V, mesh_points_3D, axis=0)
nv += nmp
F = np.append(F, mesh_quad, axis=0)
nf += nmt
# Merging duplicates
V, F = merge_duplicates(V, F)
if verbose:
if nb_bound == 1:
verb = 'lid has'
else:
verb = 'lids have'
print(("\n\t-> %u %s been added successfully\n" % (nb_bound, verb)))
return V, F
def fill_holes(V, F, verbose=False):
import vtk
if verbose:
print("Filling holes")
polydata = _build_vtkPolyData(V, F)
fillHolesFilter = vtk.vtkFillHolesFilter()
if vtk.VTK_MAJOR_VERSION <= 5:
fillHolesFilter.SetInputConnection(polydata.GetProducerPort())
else:
fillHolesFilter.SetInputData(polydata)
fillHolesFilter.Update()
polydata_filled = fillHolesFilter.GetOutput()
V, F = _dump_vtk(polydata_filled)
if verbose:
print("\t--> Done!")
return V, F
def detect_features(V, F, verbose=True):
mesh = Mesh(V, F, verbose=verbose)
mesh.detect_features(verbose=verbose)
return
def _build_polyline(curve):
import vtk
npoints = len(curve)
points = vtk.vtkPoints()
for point in curve:
points.InsertNextPoint(point)
polyline = vtk.vtkPolyLine()
polyline.GetPointIds().SetNumberOfIds(npoints)
for id in range(npoints):
polyline.GetPointIds().SetId(id, id)
cells = vtk.vtkCellArray()
cells.InsertNextCell(polyline)
polydata = vtk.vtkPolyData()
polydata.SetPoints(points)
polydata.SetLines(cells)
return polydata
def list_medium():
return ', '.join(densities.list_medium())
# =======================================================================
# COMMAND LINE USAGE
# =======================================================================
try:
import argcomplete
acok = True
except:
acok = False
parser = argparse.ArgumentParser(
description=""" -- MESHMAGICK --
A python module and a command line utility to manipulate meshes from
different format used in hydrodynamics as well as for visualization.
The formats currently supported by meshmagick are :
+-----------+------------+-----------------+----------------------+
| File | R: Reading | Software | Keywords |
| extension | W: writing | | |
+===========+============+=================+======================+
| .mar | R/W | NEMOH [#f1]_ | nemoh, mar |
+-----------+------------+-----------------+----------------------+
| .nem | R | NEMOH [#f1]_ | nemoh_mesh, nem |
+-----------+------------+-----------------+----------------------+
| .gdf | R/W | WAMIT [#f2]_ | wamit, gdf |
+-----------+------------+-----------------+----------------------+
| .inp | R | DIODORE [#f3]_ | diodore-inp, inp |
+-----------+------------+-----------------+----------------------+
| .DAT | W | DIODORE [#f3]_ | diodore-dat |
+-----------+------------+-----------------+----------------------+
| .hst | R/W | HYDROSTAR [#f4]_| hydrostar, hst |
+-----------+------------+-----------------+----------------------+
| .nat | R/W | - | natural, nat |
+-----------+------------+-----------------+----------------------+
| .msh | R | GMSH [#f5]_ | gmsh, msh |
+-----------+------------+-----------------+----------------------+
| .rad | R | RADIOSS | rad, radioss |
+-----------+------------+-----------------+----------------------+
| .stl | R/W | - | stl |
+-----------+------------+-----------------+----------------------+
| .vtu | R/W | PARAVIEW [#f6]_ | vtu |
+-----------+------------+-----------------+----------------------+
| .vtp | R/W | PARAVIEW [#f6]_ | vtp |
+-----------+------------+-----------------+----------------------+
| .vtk | R/W | PARAVIEW [#f6]_ | paraview-legacy, vtk |
+-----------+------------+-----------------+----------------------+
| .tec | R/W | TECPLOT [#f7]_ | tecplot, tec |
+-----------+------------+-----------------+----------------------+
| .med | R | SALOME [#f8]_ | med, salome |
+-----------+------------+-----------------+----------------------+
| .obj | R | WAVEFRONT | obj |
+-----------+------------+-----------------+----------------------+
By default, Meshmagick uses the filename extensions to choose the
appropriate reader/writer. This behaviour might be bypassed using the
-ifmt and -ofmt optional arguments. When using these options, keywords
defined in the table above must be used as format identifiers.
.. rubric:: Footnotes
.. [#f1] NEMOH is an open source BEM Software for seakeeping developped at
Ecole Centrale de Nantes (LHHEA)
.. [#f2] WAMIT is a BEM Software for seakeeping developped by WAMIT, Inc.
.. [#f3] DIODORE is a BEM Software for seakeeping developped by PRINCIPIA
.. [#f4] HYDROSTAR is a BEM Software for seakeeping developped by
BUREAU VERITAS
.. [#f5] GMSH is an open source meshing software developped by C. Geuzaine
and J.-_faces. Remacle
.. [#f6] PARAVIEW is an open source visualization software developped by
Kitware
.. [#f7] TECPLOT is a visualization software developped by Tecplot
.. [#f8] SALOME-MECA is an open source software for computational mechanics
developped by EDF-R&D
""",
epilog='-- Copyright 2014-%u - Francois Rongere / Ecole Centrale de Nantes --' % __year__,
formatter_class=argparse.RawDescriptionHelpFormatter)
# TODO: ajouter option pour voir l'ensemble des formats de fichier geres par meshmagick avec une explication du logiciel utilise
parser.add_argument('infilename', # TODO : voir pour un typ=file pour tester l'existence
help='path of the input mesh file in any supported format')
parser.add_argument('-o', '--outfilename', type=str,
help="""path of the output mesh file. The format of
this file is determined from the given extension.
""")
parser.add_argument('-ifmt', '--input-format',
help="""Input format. Meshmagick will read the input file considering the
INPUT_FORMAT rather than using the extension
""")
parser.add_argument('-ofmt', '--output-format',
help="""Output format. Meshmagick will write the output file considering
the OUTPUT_FORMAT rather than using the extension
""")
parser.add_argument('-q', '--quiet',
help="""switch of verbosity of meshmagick""",
action='store_true')
parser.add_argument('-i', '--info',
help="""extract informations on the mesh on the standard output""",
action='store_true')
parser.add_argument('--quality',
help="""prints mesh quality""",
action='store_true')
parser.add_argument('-t', '--translate', metavar=('Tx', 'Ty', 'Tz'),
nargs=3, type=float,
help="""translates the mesh in 3D
Usage -translate tx ty tz""")
parser.add_argument('-tx', '--translatex', type=float, metavar='Tx',
help="""translates the mesh following the x direction""")
parser.add_argument('-ty', '--translatey', type=float, metavar='Ty',
help="""translates the mesh following the y direction""")
parser.add_argument('-tz', '--translatez', type=float, metavar='Tz',
help="""translates the mesh following the z direction""")
parser.add_argument('-r', '--rotate', metavar=('Rx', 'Ry', 'Rz'),
nargs=3, type=float,
help="""rotates the mesh in 3D following a rotation
coordinate vector. It is done around fixed axis. Angles
must be given in degrees.""")
parser.add_argument('-rx', '--rotatex', type=float, metavar='Rx',
help="""rotates the mesh around the x direction.
Angles must be given in degrees.""")
parser.add_argument('-ry', '--rotatey', type=float, metavar='Ry',
help="""rotates the mesh around the y direction.
Angles must be given in degrees.""")
parser.add_argument('-rz', '--rotatez', type=float, metavar='Rz',
help="""rotates the mesh around the z direction.
Angles must be given in degrees.""")
parser.add_argument('-s', '--scale', type=float, metavar='S',
help="""scales the mesh. CAUTION : if used along
with a translation option, the scaling is done after
the translations. The translation magnitude should be set
accordingly to the original mesh.
""")
parser.add_argument('-sx', '--scalex', type=float, metavar='Sx',
help="""scales the mesh along x axis. CAUTION : if used along
with a translation option, the scaling is done after
the translations. The translation magnitude should be set
accordingly to the original mesh.
""")
parser.add_argument('-sy', '--scaley', type=float, metavar='Sy',
help="""scales the mesh along y axis. CAUTION : if used along
with a translation option, the scaling is done after
the translations. The translation magnitude should be set
accordingly to the original mesh.
""")
parser.add_argument('-sz', '--scalez', type=float, metavar='Sz',
help="""scales the mesh along z axis. CAUTION : if used along
with a translation option, the scaling is done after
the translations. The translation magnitude should be set
accordingly to the original mesh.
""")
parser.add_argument('-hn', '--heal-normals', action='store_true',
help="""Checks and heals the normals consistency and
verify if they are outward.
""")
parser.add_argument('-fn', '--flip-normals', action='store_true',
help="""flips the normals of the mesh""")
parser.add_argument('-hm', '--heal-mesh', action='store_true',
help="""Applies the following sanity transformation on the
mesh: Removes unused vertices, Removes degenerated faces,
Merge duplicate vertices, Heal triangles description,
Heal normal orientations.
""")
parser.add_argument('-p', '--plane', nargs='+', action='append', metavar='Arg',
help="""Defines a plane used by the --clip_by_plane and --symmetrize options.
It can be defined by the floats nx ny nz c where [nx, ny, nz]
is a normal vector to the plane and c defines its position
following the equation <N|X> = c with X a point belonging
to the plane.
It can also be defined by a string among [Oxy, Oxz, Oyz, /Oxy, /Oxz, /Oyz]
for quick definition. Several planes may be defined on the same command
line. Planes with a prepended '/' have normals inverted i.e. if Oxy has its
normal pointing upwards, /Oxy has its normal pointing downwards.
In that case, the planes are indexed by an integer starting by
0 following the order given in the command line.
""")
parser.add_argument('-c', '--clip-by-plane', nargs='*', action='append', metavar='Arg',
help="""cuts the mesh with a plane. Is no arguments are given, the Oxy plane
is used. If an integer is given, it should correspond to a plane defined with
the --plane option. If a key string is given, it should be a valid key (see
help of --plane option for valid plane keys). A normal and a scalar could
also be given for the plane definition just as for the --plane option. Several
clipping planes may be defined on the same command line.""")
parser.add_argument('-cc', '--concatenate-file', type=str,
help="""Concatenate a mesh from the specified path. The file format has to be
the same as the input file.""")
parser.add_argument('-md', '--merge-duplicates', nargs='?', const='1e-8',
default=None, metavar='Tol',
help="""merges the duplicate nodes in the mesh with the absolute tolerance
given as argument (default 1e-8). Tolerance must be lower than 1""")
parser.add_argument('-tq', '--triangulate-quadrangles', action='store_true',
help="""Triangulate all quadrangle _faces by a simple splitting procedure.
Twho triangles are generated and from both solution, the one with the best
aspect ratios is kept. This option may be used in conjunction with a
mesh export in a format that only deal with triangular cells like STL format.""")
parser.add_argument('-sym', '--symmetrize', nargs='*', action='append', metavar='Arg',
help="""Symmetrize the mesh by a plane defined wether by 4 scalars, i.e.
the plane normal vector coordinates and a scalar c such as N.X=c is the
plane equation (with X a point of the plane) or a string among ['Oxy',
'Oxz', 'Oyz', '/Oxy', '/Oxz', '/Oyz'] which are shortcuts for planes
passing by the origin and whose normals are the reference axes. Default
is Oxz if no argument is given to --sym option.
Be careful that symmetry is applied before any rotation so as the plane
equation is defined in the initial frame of reference.""")
parser.add_argument('--mirror', nargs='+', metavar='Arg',
help="""Mirror the mesh through the specified plane. Plane may be specified
with reference planes keys (see --plane option), or by 4 scalars, or by the
id of a plane defined with the --plane option. By default, the Oxy plane
is used when the option has no argument.""")
# FIXME: on devrait pouvoir laisser les valeurs par defaut --> creer une option --rho-medium
parser.add_argument('-pi', '--plain-inertia', action='store_true',
help="""Evaluates the inertia properties of the mesh condidering it as
uniformly plain of a medium of density rho_medium in kg/m**3. Default
is 1023 kg/m**3.""")
# TODO: creer une option --thickness
parser.add_argument('-si', '--shell-inertia', action='store_true',
help="""Evaluates the inertia properties of the mesh condidering it as
uniformly plain of a medium of density rho_medium in kg/m**3. Default
is 1023 kg/m**3.""")
parser.add_argument('--rho-medium', type=float,
help="""The density (in kg/m**3) of the medium used for evaluation of
inertia parameters of the mesh. For the hypothesis of plain homogeneous
mesh, the default is that of salt water (1023 kg/m**3) . For the
hypothesis of a shell, default is that of steel (7850 kg/m**3).
It is possible to specify medium by a name. Available medium are
currently: %s
""" % list_medium())
parser.add_argument('--list-medium', action='store_true',
help="""Lists the available medium keywords along with their density.
"""
)
parser.add_argument('--thickness', type=float,
help="""The thickness of the shell used for the evaluation of inertia
parameters of the mesh. The default value is 0.02m.""")
# Arguments for hydrostatics computations
# TODO: l'option -hs devrait etre une sous-commande au sens de argparse
# TODO: completer l'aide de -hs
parser.add_argument('-hs', '--hydrostatics', action='store_true',
help="""Compute hydrostatics data and throws a report on the
command line. When used along with options --disp, --cog or
--zcog, the behaviour is different.""")
# TODO: replace disp by mass as it is more correct...
parser.add_argument('--disp', default=None, type=float, metavar='Disp',
help="""Specifies the mass of the mesh for hydrostatic computations.
It must be given in tons.
""")
parser.add_argument('--cog', nargs=3, metavar=('Xg', 'Yg', 'Zg'),
help="""Specifies the 3D position of the center of gravity.
The third coordinate given has priority over the value given
with the --zcog option.""")
parser.add_argument('--zcog', default=None, type=float, metavar='Zcog',
help="""Specifies the z position of the center of gravity. This
is the minimal data needed for hydrostatic stiffness matrix
computation. It is however overwriten by the third component
of cog when --cog option is used. If none of these two option
is given, zcog is set to 0.
""")
parser.add_argument('--rho-water', default=1023., type=float, metavar='Rho',
help="""Specifies the density of salt water. Default is 1023 kg/m**3.
""")
parser.add_argument('-g', '--grav', default=9.81, type=float, metavar='G',
help="""Specifies the acceleration of gravity on the earth surface.
Default is 9.81 m/s**2.
""")
# parser.add_argument('--hs_solver_params', nargs='+')
parser.add_argument('-af', '--absolute-force', nargs=6, action='append',
metavar=('X', 'Y', 'Z', 'Fx', 'Fy', 'Fz'),
help="""Add an additional absolute force applied to the mesh in
hydrostatics equilibrium computations. It is absolute as force
orientation does not change during hydrostatic equilibrium
computations, but application point follows the mesh motion.
The option is called with 6 arguments such that:
-af x y z fx fy fz
where [x, y, z] are the coordinates of the application point
(in meters) and [fx, fy, fz] are the components of the force.
Those are expressed in the initial mesh frame.
""")
parser.add_argument('-rf', '--relative-force', nargs=6, action='append', type=float,
metavar=('X', 'Y', 'Z', 'Fx', 'Fy', 'Fz'),
help="""Add an additional relative force applied to the mesh in
hydrostatics equilibrium computations. It is relative as force
orientation follows the orientation of the mesh during equilibrium
computations. The option is called with 6 arguments such that:
-af x y z fx fy fz
where [x, y, z] are the coordinates of the application point
of the force and [fx, fy, fz] are the components of the force.
Those are expressed in the initial mesh frame.
""")
parser.add_argument('--hs-report', type=str, metavar='filename',
help="""Write the hydrostatic report into the file given as an argument""")
# ARGUMENTS RELATED TO THE COMPUTATION OF INERTIA PARAMETERS
# parser.add_argument('--rho-medium', default=7500., type=float,
# help="""Specified the density of the medium used for the device. Default
# is steel and is 7500 kg/m**3.
# """)
# parser.add_argument('--thickness', default=0.01, type=float,
# help="""Specifies the thickness of the hull. This option is only used if
# both the --inertias and --hull are used. Default is 0.01 m.
# """)
# parser.add_argument('-gz', '--gz-curves', nargs='?', const=5., default=None, type=float,
# help=""" [EXPERIMENTAL] Computes the GZ curves with angle spacing given as argument.
# Default is 5 degrees (if no argument given)
# """)
# TODO : permettre de rajouter des ballasts
# parser.add_argument('--inertias', action='store_true', # TODO : specifier un point de calcul
# help="""Compute the principal inertia properties of the mesh. By default,
# the device is considered to be a hull. Then the --thickness and --rho-medium
# options may be used to tune the properties.
# If the --no-hull option is used, then the device will be considered to be
# filled with the medium of density rho-medium. Be carefull that the default
# medium is steel so that you may get a really heavy device. Please consider
# to specify an other density with the --rho-medium option.
#
# Note that the inertia matrix is expressed at center of gravity
# location.
#
# A side effect of this option is that for hydrostatics computations, the values
# computed by this option will be used instead of other options --mass and --cog
# that will be overriden. Be carefull that the device may need some ballast.
# """)
# parser.add_argument('--no-hull', action='store_true',
# help="""Specifies that the device should be considered as being filled with
# the material of density rho-medium. It is only used by the --inertias option.
# """)
# parser.add_argument('--lid', nargs='?', const=1., default=None, type=float,
# help="""Generate a triangle mesh lid on the mesh clipped by the Oxy plane.
# """)
# parser.add_argument('--fill-holes', '-fh', action='store_true',
# help="""Fill little holes by triangulation if any.
# """)
parser.add_argument('--show', action='store_true',
help="""Shows the input mesh in an interactive window""")
parser.add_argument('--version', action='version',
version='meshmagick - version %s\n%s' % (__version__, __copyright__),
help="""Shows the version number and exit""")
def main():
if acok:
argcomplete.autocomplete(parser)
# TODO : Utiliser des sous-commandes pour l'utilisation de meshmagick
args, unknown = parser.parse_known_args()
if args.quiet:
verbose = False
else:
verbose = True
if verbose:
print('\n=============================================')
print(('meshmagick - version %s\n%s' % (__version__, __copyright__)))
print('=============================================')
# LOADING DATA FROM FILE
if args.input_format is not None:
format = args.input_format
else:
# Format based on extension
_, ext = os.path.splitext(args.infilename)
format = ext[1:].lower()
if format == '':
raise IOError('Unable to determine the input file format from its extension. Please specify an input format.')
# Loading mesh elements from file
if os.path.isfile(args.infilename):
V, F = mmio.load_mesh(args.infilename, format)
# Give the name of the mesh the filename
basename = os.path.basename(args.infilename)
mesh_name, _ = os.path.splitext(basename)
mesh = Mesh(V, F, name=mesh_name)
# Ensuring triangles are following the right convention (last id = first id)
mesh.heal_triangles()
if verbose:
mesh.verbose_on()
print(('%s successfully loaded' % args.infilename))
else:
raise IOError('file %s not found' % args.infilename)
if args.concatenate_file is not None:
print('Concatenate %s with %s' % (args.infilename, args.concatenate_file))
# Loading the file
if os.path.isfile(args.concatenate_file):
Vc, Fc = mmio.load_mesh(args.concatenate_file, format)
# Give the name of the mesh the filename
basename = os.path.basename(args.concatenate_file)
mesh_name, _ = os.path.splitext(basename)
mesh_c = Mesh(Vc, Fc, name=mesh_name)
# Ensuring triangles are following the right convention (last id = first id)
mesh_c.heal_triangles()
if verbose:
mesh_c.verbose_on()
print(('%s successfully loaded' % args.concatenate_file))
else:
raise IOError('file %s not found' % args.concatenate_file)
mesh += mesh_c
# Merge duplicate _vertices
if args.merge_duplicates is not None:
tol = float(args.merge_duplicates)
mesh.merge_duplicates(atol=tol)
# TODO : put that dict at the begining of the main function or in the module
plane_str_list = {'Oxy': [0., 0., 1.],
'Oxz': [0., 1., 0.],
'Oyz': [1., 0., 0.],
'/Oxy': [0., 0., -1.],
'/Oxz': [0., -1., 0.],
'/Oyz': [-1., 0., 0.]}
if args.quality:
mesh.print_quality()
# Defining planes
planes = []
if args.plane is not None:
nb_planes = len(args.plane)
if verbose:
if nb_planes == 1:
verb = 'plane has'
else:
verb = 'planes have'
print(('\n%u %s been defined:' % (nb_planes, verb)))
# TODO: ajouter un recapitulatif des plans definis
planes = [Plane() for i in range(nb_planes)]
for (iplane, plane) in enumerate(args.plane):
if len(plane) == 4:
# plane is defined by normal and scalar
try:
planes[iplane] = Plane(normal=list(map(float, plane[:3])), scalar=plane[3])
except:
raise AssertionError('Defining a plane by normal and scalar requires four scalars')
elif len(plane) == 1:
if plane[0] in plane_str_list:
planes[iplane].normal = np.array(plane_str_list[plane[0]], dtype=np.float)
planes[iplane].c = 0.
else:
raise AssertionError('%s key for plane is not known. Choices are [%s].'
% (plane[0], ', '.join(list(plane_str_list.keys()))))
else:
raise AssertionError('Planes should be defined by a normal and a scalar '
'or by a key to choose among [%s]' % (', '.join(list(plane_str_list.keys()))))
if verbose:
for plane_id, plane in enumerate(planes):
print(("\t%u: %s" % (plane_id, plane)))
# Mirroring the mesh
if args.mirror is not None:
sym_plane = Plane()
print((args.mirror))
if len(args.mirror) == 1:
# May be a standard plane or a plane id
if len(planes) > 0:
try:
plane_id = int(args.mirror[0])
if plane_id >= 0 and plane_id < len(planes):
sym_plane = planes[plane_id]
else:
raise AssertionError('Only planes IDs from 0 to %u have been defined. %u is outside the range.'
% (len(planes) - 1, plane_id))
except ValueError:
# Cannot be converted to an int, it should be the key of a plane
try:
sym_plane.normal = plane_str_list[args.mirror[0]]
except KeyError as err:
raise KeyError('%s is not a standard plane identifier' % err)
else:
try:
sym_plane.normal = plane_str_list[args.mirror[0]]
except KeyError as err:
raise KeyError('%s is not a standard plane identifier' % err)
elif len(args.mirror) == 4:
sym_plane.normal = args.mirror[:3]
sym_plane.c = args.mirror[3]
else:
raise ValueError('Bad plane definition.')
if verbose:
print(('Mirroring the mesh by :\n\t%s' % sym_plane))
mesh.mirror(sym_plane)
if verbose:
print('\t-> Done.')
# Symmetrizing the mesh
if args.symmetrize is not None:
nb_sym = len(args.symmetrize)
for iplane, plane in enumerate(args.symmetrize):
if len(plane) == 0:
args.symmetrize[iplane] = ['Oxz']
if verbose:
if nb_sym == 1:
verb = 'plane'
else:
verb = 'planes'
print(('\nMesh is being symmetrized by %u %s:' % (nb_sym, verb)))
for plane in args.symmetrize:
sym_plane = Plane()
if len(plane) == 1:
if len(planes) > 0:
try:
plane_id = int(plane[0])
if plane_id >= 0 and plane_id < len(planes):
sym_plane = planes[plane_id]
else:
raise AssertionError(
'Only planes IDs from 0 to %u have been defined. %u is outside the range.' % (
len(planes) - 1, plane_id))
except ValueError:
try:
sym_plane.normal = plane_str_list[plane[0]]
except KeyError as err:
raise KeyError('%s is not a standard plane identifier' % err)
else:
try:
sym_plane.normal = plane_str_list[plane[0]]
except KeyError as err:
raise KeyError('%s is not a standard plane identifier' % err)
elif len(plane) == 4:
sym_plane.normal = plane[:3]
sym_plane.c = plane[3]
else:
raise ValueError('Bad plane definition.')
if verbose:
print(('\t%s' % sym_plane))
mesh.symmetrize(sym_plane)
if verbose:
print('\t-> Done.')
# Globally heal the mesh
if args.heal_mesh:
if verbose:
print('\nOPERATION: heal the mesh')
mesh.heal_mesh()
if verbose:
print('\tDone.')
# Heal normals
if args.heal_normals and not args.heal_mesh:
if verbose:
print('\nOPERATION: heal normals')
mesh.heal_normals()
if verbose:
print('\t-> Done.')
# Mesh translations
if args.translate is not None:
if verbose:
print(('\nOPERATION: Translation by [%f, %f, %f]' % tuple(args.translate)))
mesh.translate(args.translate)
if verbose:
print('\t-> Done.')
if args.translatex is not None:
if verbose:
print(('\nOPERATION: Translation by %f along X' % args.translatex))
mesh.translate_x(args.translatex)
if verbose:
print('\t-> Done.')
if args.translatey is not None:
if verbose:
print(('\nOPERATION: Translation by %f along Y' % args.translatey))
mesh.translate_y(args.translatey)
if verbose:
print('\t-> Done.')
if args.translatez is not None:
if verbose:
print(('\nOPERATION: Translation by %f along Z' % args.translatez))
mesh.translate_z(args.translatez)
if verbose:
print('\t-> Done.')
# Mesh rotations
if args.rotate is not None:
if verbose:
print(('\nOPERATION: Rotation by [%f, %f, %f] (degrees)' % tuple(args.rotate)))
mesh.rotate(list(map(math.radians, args.rotate)))
if verbose:
print('\t-> Done.')
if args.rotatex is not None:
if verbose:
print(('\nOPERATION: Rotation by %f around X (Roll)' % args.rotatex))
mesh.rotate_x(math.radians(args.rotatex))
if verbose:
print('\t-> Done.')
if args.rotatey is not None:
if verbose:
print(('\nOPERATION: Rotation by %f around Y (Pitch)' % args.rotatey))
mesh.rotate_y(math.radians(args.rotatey))
if verbose:
print('\t-> Done.')