Skip to content

Commit ec7d4b4

Browse files
committed
GEOMLib
0 parents  commit ec7d4b4

27 files changed

+4979
-0
lines changed

.gitattributes

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.html linguist-detectable=false
2+
*.js linguist-detectable=false
3+
*.md linguist-detectable=false
4+
*.xml linguist-detectable=false

LICENSE

+661
Large diffs are not rendered by default.

README.md

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
GEOMLib - 3D CSG Geometry Library for MATLAB® and GNU Octave
2+
============================================================
3+
4+
**GEOMLib** is a 2D and 3D geometry library for MATLAB® and GNU Octave
5+
allowing _mesh based_ CSG (Composite Solid Geometry) operations.
6+
7+
Originally developed for use with the [FEATool Multiphysics](https://www.featool.com)
8+
FEA simulation toolbox, but now replaced with the OpenCASCADE geometry
9+
kernel to support BREP CAD geometry support.
10+
11+
![GEOMLib - MATLAB CSG Geometry Library](geomlib-screenshot.jpg)
12+
13+
14+
Installation
15+
------------
16+
17+
Download and copy the library to a folder. Start MATLAB® or Octave and
18+
start the `runtests` script (or `runtests 1` for verbose output) to
19+
run the test and validation suite.
20+
21+
22+
Examples
23+
--------
24+
25+
1. 2D example of the union of a circle and unit square
26+
27+
% Create circle
28+
c1 = gobj_circle([0, 0], 0.5, 'C1');
29+
c1 = convert_gobj_polygons( c1, 1 );
30+
c1 = csg_op( c1, 'b' );
31+
32+
% Create unit square
33+
r1 = gobj_rectangle(0, 1, 0, 1, 'R1');
34+
r1 = convert_gobj_polygons( r1, 2 );
35+
r1 = csg_op( r1, 'b' );
36+
37+
% Join circle and square
38+
[res,~,stat] = csg_op( c1, r1, '+' );
39+
40+
% Visualize result
41+
csg_op(res, 'v')
42+
43+
2. 3D subtraction of a sphere from a unit cube
44+
45+
% Create sphere
46+
s1 = gobj_sphere([0, 0, 1], 0.5, 'S1');
47+
s1 = convert_gobj_polygons( s1, 1 );
48+
s1 = csg_op( s1, 'b' );
49+
50+
% Create unit cube
51+
b1 = gobj_block(0, 1, 0, 1, 0, 1, 'B1');
52+
b1 = convert_gobj_polygons( b1, 2 );
53+
b1 = csg_op( b1, 'b' );
54+
55+
% Subtracting sphere from cube
56+
[res,~,stat] = csg_op( b1, s1, '-' );
57+
58+
% Visualize result
59+
csg_op(res, 'v')
60+
61+
3. For more examples see the tests in the _test_ directory.
62+
63+
64+
Functions
65+
---------
66+
67+
% Main CSG functions:
68+
69+
csg_op - apply CSG operation on polygons
70+
csg_polygon_recombination - recombine and tessellate polygons
71+
csg_polygon_tesselation - recombine and tessellate polygons
72+
73+
% Geometry object primitives:
74+
75+
gobj_block - create block
76+
gobj_circle - create circle
77+
gobj_cylinder - create cylinder
78+
gobj_ellipse - create ellipse
79+
gobj_polygon - create polygon
80+
gobj_rectangle - create rectangle
81+
gobj_sphere - create sphere
82+
83+
% Geometry utility and help functions:
84+
85+
convert_gobj_polygons - extract polygons from geometry object
86+
deduplicate - remove duplicate rows or columns within tolerance
87+
uunique - unsorted set unique
88+
89+
90+
Support
91+
-------
92+
93+
This library has been open sourced on an _as is_ basis under the
94+
AGPLv3 License (see included LICENSE file) without warranty or
95+
support.
96+
97+
For technical support, consulting, and custom development of this
98+
library, commercial licensing, or use of the newer OpenCASCADE based
99+
geometry library (allowing BREP CAD geometry modeling such as STEP and
100+
IGES formats) please [contact Precise
101+
Simulation](https://www.precisesimulation.com#contact) directly.
102+
103+
104+
License
105+
-------
106+
107+
Copyright (C) 2013-2022 Precise Simulation Ltd.
108+
109+
Keywords: Geometry, CSG, Mesh, MATLAB®, Octave
110+
111+
This program is free software; you can redistribute it and/or modify
112+
it under the terms of version 3 of the GNU Affero General Public
113+
License (AGPLv3) as published by the Free Software Foundation.
114+
115+
This program is distributed in the hope that it will be useful, but
116+
WITHOUT ANY WARRANTY; without even the implied warranty of
117+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
118+
General Public License for more details.
119+
120+
You should have received a copy of the GNU Affero General Public
121+
License along with this program. If not, see
122+
[http://www.gnu.org/licenses](http://www.gnu.org/licenses).
123+
124+
125+
Trademarks
126+
----------
127+
128+
FEATool Multiphysics™ is a trademark of Precise Simulation
129+
Limited. MATLAB® is a registered trademark of The MathWorks, Inc. All
130+
other trademarks are the property of their respective owners. Precise
131+
Simulation and its products are not affiliated with, endorsed, or
132+
sponsored by these trademark owners.

geomlib-screenshot.jpg

67.4 KB
Loading

runtests.m

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
function [ results ] = runtests( varargin )
2+
%RUNTESTS Runs the testsuite
3+
%
4+
% [ RESULTS ] = RUNTESTS( VARARGIN ) Runs the test suite (VERBOSELY if
5+
% any input argument is given). To run a single test suite/case enter
6+
% the testSuite:testCase can be specified as an argument. If two input
7+
% arguments are given verbose tests are run and output to the file
8+
% given by the second argument. RESULTS is a NTESTSUITES x 2 array
9+
% with the number of passed tests in first column and failed in second.
10+
11+
% The SSRCDIR with source files (which are added to the path) and
12+
% STESTDIR with mloct_test files are specified manually below.
13+
14+
% Initial version 180216.
15+
% Copyright 2013-2022 Precise Simulation Ltd.
16+
% License: AGPL v3, see LICENSE for more details or contact
17+
% Precise Simulation for alternative licensing options.
18+
19+
sSrcDir = 'src';
20+
sTestDir = 'test';
21+
sTest = '';
22+
fid = 1;
23+
isVerbose = false;
24+
25+
26+
if( nargin>=1 )
27+
arg1 = varargin{1};
28+
isVerbose = true;
29+
if( ischar(arg1) && length(arg1)>1 )
30+
sTest = arg1;
31+
end
32+
33+
if( nargin>=2 )
34+
arg2 = varargin{2};
35+
if( ischar(arg2) )
36+
fid = fopen( arg2, 'a+' );
37+
end
38+
end
39+
end
40+
addpath( genpath('test/mloct_test') );
41+
42+
43+
results = mloct_test_run( sSrcDir, sTestDir, sTest, fid, isVerbose );
44+
45+
46+
if( fid>1 )
47+
fclose( fid );
48+
end
49+
if( ~nargout )
50+
clear results
51+
end

src/convert_gobj_polygons.m

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
function [ polygons ] = convert_gobj_polygons( gobj, id )
2+
%CONVERT_GOBJ_POLYGONS Constructs polygons from geometry object.
3+
%
4+
% [ POLYGONS ] = CONVERT_GOBJ_POLYGONS( GOBJ, ID ) Constructs
5+
% polygons from the boundaries of geometry object GOBJ by calling
6+
% the CSG_OP build operation. ID if present will be appended to the
7+
% polygon id property. Thus the final polygon identity field will
8+
% consist of the local boundary number, local polygon number, and ID.
9+
10+
% Initial version 171228.
11+
% Copyright 2013-2022 Precise Simulation Ltd.
12+
% License: AGPL v3, see LICENSE for more details or contact
13+
% Precise Simulation for alternative licensing options.
14+
if( ~(nargin || nargout) ),help convert_gobj_polygons, return, end
15+
16+
if( nargin<2 || ~isnumeric(id) )
17+
id = 0;
18+
end
19+
20+
21+
if( isfield(gobj,'boundaries') )
22+
23+
polygons = {};
24+
boundaries = gobj.boundaries;
25+
for i_bdr=1:length(boundaries)
26+
polygons_ibdr = boundary_to_polygons( boundaries(i_bdr), [i_bdr,id] );
27+
polygons = [ polygons, polygons_ibdr ];
28+
end
29+
30+
elseif( isfield(gobj,'edges') || isfield(gobj,'faces') ) % Input is boundary struct.
31+
32+
polygons = boundary_to_polygons( gobj, id );
33+
34+
else
35+
error( 'Could not create polygons from geometry object.' )
36+
end
37+
38+
%------------------------------------------------------------------------------%
39+
function [ polygons ] = boundary_to_polygons( boundary, id )
40+
41+
polygons = {};
42+
if( isfield(boundary,'vertices') && isfield(boundary,'faces') )
43+
44+
vertices = boundary.vertices;
45+
faces = boundary.faces;
46+
if( ~iscell(faces) )
47+
faces = { faces };
48+
end
49+
50+
i_cnt = 0;
51+
for i=1:length(faces)
52+
f = faces{i};
53+
for j=1:size(f,1)
54+
v = vertices(f(j,:),:);
55+
i_cnt = i_cnt + 1;
56+
identity = [i_cnt,id];
57+
58+
polygon_i = csg_op( v, identity, 'b' );
59+
polygons = [ polygons, polygon_i ];
60+
end
61+
end
62+
63+
elseif( isfield(boundary,'edges') )
64+
65+
p_e = boundary.edges;
66+
n_e = size(p_e,1) - 1;
67+
for i=1:n_e
68+
v = p_e([i,i+1],:);
69+
identity = [i,id];
70+
71+
polygon_i = csg_op( v, identity, 'b' );
72+
polygons = [ polygons, polygon_i ];
73+
end
74+
75+
else
76+
error( 'Could not create polygons from boundary.' )
77+
end

0 commit comments

Comments
 (0)